Get rid of code duplication in ConfigWriter/ConsoleCommand

refs #9099
This commit is contained in:
Gunnar Beutner 2015-08-11 06:44:42 +02:00
parent 930dc603af
commit aa00f4183a
4 changed files with 46 additions and 56 deletions

View File

@ -80,34 +80,9 @@ static char *ConsoleCompleteHelper(const char *word, int state)
if (state == 0) { if (state == 0) {
matches.clear(); matches.clear();
AddSuggestion(matches, word, "object"); BOOST_FOREACH(const String& keyword, ConfigCompiler::GetKeywords()) {
AddSuggestion(matches, word, "template"); AddSuggestion(matches, word, keyword);
AddSuggestion(matches, word, "include"); }
AddSuggestion(matches, word, "include_recursive");
AddSuggestion(matches, word, "library");
AddSuggestion(matches, word, "null");
AddSuggestion(matches, word, "true");
AddSuggestion(matches, word, "false");
AddSuggestion(matches, word, "const");
AddSuggestion(matches, word, "var");
AddSuggestion(matches, word, "this");
AddSuggestion(matches, word, "globals");
AddSuggestion(matches, word, "locals");
AddSuggestion(matches, word, "use");
AddSuggestion(matches, word, "apply");
AddSuggestion(matches, word, "to");
AddSuggestion(matches, word, "where");
AddSuggestion(matches, word, "import");
AddSuggestion(matches, word, "assign");
AddSuggestion(matches, word, "ignore");
AddSuggestion(matches, word, "function");
AddSuggestion(matches, word, "return");
AddSuggestion(matches, word, "break");
AddSuggestion(matches, word, "continue");
AddSuggestion(matches, word, "for");
AddSuggestion(matches, word, "if");
AddSuggestion(matches, word, "else");
AddSuggestion(matches, word, "while");
{ {
ObjectLock olock(l_ScriptFrame.Locals); ObjectLock olock(l_ScriptFrame.Locals);

View File

@ -329,3 +329,40 @@ void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, con
m_ZoneDirs[zoneName].push_back(zf); m_ZoneDirs[zoneName].push_back(zf);
} }
const std::vector<String>& ConfigCompiler::GetKeywords(void)
{
static std::vector<String> keywords;
if (keywords.empty()) {
keywords.push_back("object");
keywords.push_back("template");
keywords.push_back("include");
keywords.push_back("include_recursive");
keywords.push_back("library");
keywords.push_back("null");
keywords.push_back("true");
keywords.push_back("false");
keywords.push_back("const");
keywords.push_back("var");
keywords.push_back("this");
keywords.push_back("globals");
keywords.push_back("locals");
keywords.push_back("use");
keywords.push_back("apply");
keywords.push_back("to");
keywords.push_back("where");
keywords.push_back("import");
keywords.push_back("assign");
keywords.push_back("ignore");
keywords.push_back("function");
keywords.push_back("return");
keywords.push_back("break");
keywords.push_back("continue");
keywords.push_back("for");
keywords.push_back("if");
keywords.push_back("else");
keywords.push_back("while");
}
return keywords;
}

View File

@ -109,6 +109,8 @@ public:
static std::vector<ZoneFragment> GetZoneDirs(const String& zone); static std::vector<ZoneFragment> GetZoneDirs(const String& zone);
static void RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName); static void RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName);
static const std::vector<String>& GetKeywords(void);
private: private:
boost::promise<boost::shared_ptr<Expression> > m_Promise; boost::promise<boost::shared_ptr<Expression> > m_Promise;

View File

@ -18,10 +18,12 @@
******************************************************************************/ ******************************************************************************/
#include "config/configwriter.hpp" #include "config/configwriter.hpp"
#include "config/configcompiler.hpp"
#include "base/exception.hpp" #include "base/exception.hpp"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <iterator>
using namespace icinga; using namespace icinga;
@ -131,34 +133,8 @@ void ConfigWriter::EmitIdentifier(const String& identifier, bool inAssignment)
{ {
static std::set<String> keywords; static std::set<String> keywords;
if (keywords.empty()) { if (keywords.empty()) {
keywords.insert("object"); const std::vector<String>& vkeywords = ConfigCompiler::GetKeywords();
keywords.insert("template"); std::copy(vkeywords.begin(), vkeywords.end(), std::inserter(keywords, keywords.begin()));
keywords.insert("include");
keywords.insert("include_recursive");
keywords.insert("library");
keywords.insert("null");
keywords.insert("true");
keywords.insert("false");
keywords.insert("const");
keywords.insert("var");
keywords.insert("this");
keywords.insert("globals");
keywords.insert("locals");
keywords.insert("use");
keywords.insert("apply");
keywords.insert("to");
keywords.insert("where");
keywords.insert("import");
keywords.insert("assign");
keywords.insert("ignore");
keywords.insert("function");
keywords.insert("return");
keywords.insert("break");
keywords.insert("continue");
keywords.insert("for");
keywords.insert("if");
keywords.insert("else");
keywords.insert("while");
} }
if (keywords.find(identifier) != keywords.end()) { if (keywords.find(identifier) != keywords.end()) {