From: Gunnar Beutner Date: Thu, 31 May 2012 07:43:46 +0000 (+0200) Subject: Moved config parser into dyn/. X-Git-Tag: v0.0.1~455 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c92c7b2d7ce462b4bf90591ec74994bd73dd1c6;p=icinga2 Moved config parser into dyn/. --- diff --git a/components/configfile/Makefile.am b/components/configfile/Makefile.am index f1d6783ff..931ae6790 100644 --- a/components/configfile/Makefile.am +++ b/components/configfile/Makefile.am @@ -3,17 +3,9 @@ pkglib_LTLIBRARIES = \ configfile.la -BUILD_SOURCES = icinga_parser.h - -AM_YFLAGS = -d - configfile_la_SOURCES = \ - configcontext.cpp \ - configcontext.h \ configfilecomponent.cpp \ configfilecomponent.h \ - icinga_lexer.ll \ - icinga_parser.yy \ i2-configfile.h configfile_la_CPPFLAGS = \ diff --git a/components/configfile/configcontext.cpp b/components/configfile/configcontext.cpp deleted file mode 100644 index 263c77c5d..000000000 --- a/components/configfile/configcontext.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "i2-configfile.h" - -using namespace icinga; - -ConfigContext::ConfigContext(istream *input) -{ - Input = input; - InitializeScanner(); -} - -ConfigContext::~ConfigContext(void) -{ - DestroyScanner(); -} - diff --git a/components/configfile/i2-configfile.h b/components/configfile/i2-configfile.h index 96776dee3..25dbe9334 100644 --- a/components/configfile/i2-configfile.h +++ b/components/configfile/i2-configfile.h @@ -30,7 +30,6 @@ #include #include -#include "configcontext.h" #include "configfilecomponent.h" #endif /* I2CONFIGFILECOMPONENT_H */ diff --git a/dyn/Makefile.am b/dyn/Makefile.am index 2d8136059..7987149bb 100644 --- a/dyn/Makefile.am +++ b/dyn/Makefile.am @@ -4,7 +4,15 @@ pkglib_LTLIBRARIES = \ libdyn.la +BUILT_SOURCES = config_parser.h + +AM_YFLAGS = -d + libdyn_la_SOURCES = \ + configcontext.cpp \ + configcontext.h \ + config_lexer.ll + config_parser.yy \ i2-dyn.h \ dynamicobject.cpp \ dynamicobject.h \ diff --git a/components/configfile/icinga_lexer.cc b/dyn/config_lexer.cc similarity index 98% rename from components/configfile/icinga_lexer.cc rename to dyn/config_lexer.cc index cd05cccec..3e4487299 100644 --- a/components/configfile/icinga_lexer.cc +++ b/dyn/config_lexer.cc @@ -1,5 +1,5 @@ -#line 3 "icinga_lexer.cc" +#line 3 "config_lexer.cc" #define YY_INT_ALIGNED short int @@ -500,24 +500,23 @@ static yyconst flex_int32_t yy_rule_can_match_eol[23] = #define yymore() yymore_used_but_not_detected #define YY_MORE_ADJ 0 #define YY_RESTORE_YY_MORE_OFFSET -#line 1 "icinga_lexer.ll" -#line 2 "icinga_lexer.ll" -#include "i2-configfile.h" -#include "icinga_parser.h" +#line 1 "config_lexer.ll" +#line 2 "config_lexer.ll" +#include "i2-dyn.h" +#include "config_parser.h" using namespace icinga; #define YY_EXTRA_TYPE ConfigContext * #define YY_USER_ACTION yylloc->first_line = yylineno; -#define YY_INPUT(buf, result, max_size) \ -do { \ - yyextra->Input->read(buf, max_size); \ - result = yyextra->Input->gcount(); \ +#define YY_INPUT(buf, result, max_size) \ +do { \ + result = yyextra->ReadInput(buf, max_size); \ } while (0) #define YY_NO_UNISTD_H 1 -#line 521 "icinga_lexer.cc" +#line 520 "config_lexer.cc" #define INITIAL 0 #define IN_C_COMMENT 1 @@ -764,9 +763,9 @@ YY_DECL register int yy_act; struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; -#line 23 "icinga_lexer.ll" +#line 22 "config_lexer.ll" -#line 770 "icinga_lexer.cc" +#line 769 "config_lexer.cc" yylval = yylval_param; @@ -863,122 +862,122 @@ do_action: /* This label is used only to access EOF actions. */ case 1: YY_RULE_SETUP -#line 24 "icinga_lexer.ll" +#line 23 "config_lexer.ll" return T_ABSTRACT; YY_BREAK case 2: YY_RULE_SETUP -#line 25 "icinga_lexer.ll" +#line 24 "config_lexer.ll" return T_LOCAL; YY_BREAK case 3: YY_RULE_SETUP -#line 26 "icinga_lexer.ll" +#line 25 "config_lexer.ll" return T_OBJECT; YY_BREAK case 4: YY_RULE_SETUP -#line 27 "icinga_lexer.ll" +#line 26 "config_lexer.ll" return T_INCLUDE; YY_BREAK case 5: YY_RULE_SETUP -#line 28 "icinga_lexer.ll" +#line 27 "config_lexer.ll" return T_INHERITS; YY_BREAK case 6: YY_RULE_SETUP -#line 29 "icinga_lexer.ll" +#line 28 "config_lexer.ll" return T_IDENTIFIER; YY_BREAK case 7: /* rule 7 can match eol */ YY_RULE_SETUP -#line 30 "icinga_lexer.ll" +#line 29 "config_lexer.ll" { yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING; } YY_BREAK case 8: YY_RULE_SETUP -#line 31 "icinga_lexer.ll" +#line 30 "config_lexer.ll" { yylval->num = atoi(yytext); return T_NUMBER; } YY_BREAK case 9: YY_RULE_SETUP -#line 32 "icinga_lexer.ll" +#line 31 "config_lexer.ll" return T_OPEN_BRACE; YY_BREAK case 10: YY_RULE_SETUP -#line 33 "icinga_lexer.ll" +#line 32 "config_lexer.ll" return T_CLOSE_BRACE; YY_BREAK case 11: YY_RULE_SETUP -#line 34 "icinga_lexer.ll" +#line 33 "config_lexer.ll" return T_OPEN_BRACKET; YY_BREAK case 12: YY_RULE_SETUP -#line 35 "icinga_lexer.ll" +#line 34 "config_lexer.ll" return T_CLOSE_BRACKET; YY_BREAK case 13: YY_RULE_SETUP -#line 36 "icinga_lexer.ll" +#line 35 "config_lexer.ll" return T_COMMA; YY_BREAK case 14: YY_RULE_SETUP -#line 37 "icinga_lexer.ll" +#line 36 "config_lexer.ll" return T_EQUAL; YY_BREAK case 15: YY_RULE_SETUP -#line 40 "icinga_lexer.ll" +#line 39 "config_lexer.ll" BEGIN(IN_C_COMMENT); YY_BREAK case 16: YY_RULE_SETUP -#line 44 "icinga_lexer.ll" +#line 43 "config_lexer.ll" BEGIN(INITIAL); YY_BREAK case 17: /* rule 17 can match eol */ YY_RULE_SETUP -#line 45 "icinga_lexer.ll" +#line 44 "config_lexer.ll" /* ignore comment */ YY_BREAK case 18: YY_RULE_SETUP -#line 46 "icinga_lexer.ll" +#line 45 "config_lexer.ll" /* ignore star */ YY_BREAK case 19: YY_RULE_SETUP -#line 49 "icinga_lexer.ll" +#line 48 "config_lexer.ll" /* ignore C++-style comments */ YY_BREAK case 20: YY_RULE_SETUP -#line 50 "icinga_lexer.ll" +#line 49 "config_lexer.ll" /* ignore shell-style comments */ YY_BREAK case 21: /* rule 21 can match eol */ YY_RULE_SETUP -#line 51 "icinga_lexer.ll" +#line 50 "config_lexer.ll" /* ignore whitespace */ YY_BREAK case 22: YY_RULE_SETUP -#line 52 "icinga_lexer.ll" +#line 51 "config_lexer.ll" ECHO; YY_BREAK -#line 982 "icinga_lexer.cc" +#line 981 "config_lexer.cc" case YY_STATE_EOF(INITIAL): case YY_STATE_EOF(IN_C_COMMENT): yyterminate(); @@ -2166,20 +2165,20 @@ void yyfree (void * ptr , yyscan_t yyscanner) #define YYTABLES_NAME "yytables" -#line 52 "icinga_lexer.ll" +#line 51 "config_lexer.ll" void ConfigContext::InitializeScanner(void) { - yylex_init(&Scanner); - yyset_extra(this,Scanner); + yylex_init(&m_Scanner); + yyset_extra(this,m_Scanner); } void ConfigContext::DestroyScanner(void) { - yylex_destroy(Scanner); + yylex_destroy(m_Scanner); } diff --git a/components/configfile/icinga_lexer.ll b/dyn/config_lexer.ll similarity index 80% rename from components/configfile/icinga_lexer.ll rename to dyn/config_lexer.ll index c0af41a24..3fc488c9b 100644 --- a/components/configfile/icinga_lexer.ll +++ b/dyn/config_lexer.ll @@ -1,16 +1,15 @@ %{ -#include "i2-configfile.h" -#include "icinga_parser.h" +#include "i2-dyn.h" +#include "config_parser.h" using namespace icinga; #define YY_EXTRA_TYPE ConfigContext * #define YY_USER_ACTION yylloc->first_line = yylineno; -#define YY_INPUT(buf, result, max_size) \ -do { \ - yyextra->Input->read(buf, max_size); \ - result = yyextra->Input->gcount(); \ +#define YY_INPUT(buf, result, max_size) \ +do { \ + result = yyextra->ReadInput(buf, max_size); \ } while (0) %} @@ -54,12 +53,12 @@ inherits return T_INHERITS; void ConfigContext::InitializeScanner(void) { - yylex_init(&Scanner); - yyset_extra(this, Scanner); + yylex_init(&m_Scanner); + yyset_extra(this, m_Scanner); } void ConfigContext::DestroyScanner(void) { - yylex_destroy(Scanner); + yylex_destroy(m_Scanner); } diff --git a/components/configfile/icinga_parser.cc b/dyn/config_parser.cc similarity index 99% rename from components/configfile/icinga_parser.cc rename to dyn/config_parser.cc index 0f44e3f36..55d4656f0 100644 --- a/components/configfile/icinga_parser.cc +++ b/dyn/config_parser.cc @@ -176,18 +176,22 @@ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner); void yyerror(YYLTYPE *locp, ConfigContext *context, const char *err) { - std::cout << locp->first_line << ":" << locp->first_column + stringstream message; + + message << locp->first_line << ":" << locp->first_column << "-" << locp->last_line << ":" << locp->last_column << ": " << err << std::endl; + + throw runtime_error(message.str()); } -#define scanner context->Scanner +#define scanner (context->GetScanner()) /* Line 343 of yacc.c */ -#line 191 "icinga_parser.cc" +#line 195 "icinga_parser.cc" #ifdef short # undef short @@ -486,10 +490,10 @@ static const yytype_int8 yyrhs[] = /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint8 yyrline[] = { - 0, 50, 50, 51, 54, 54, 57, 60, 61, 64, - 67, 68, 71, 72, 75, 76, 79, 80, 83, 86, - 87, 88, 91, 94, 94, 94, 94, 97, 100, 102, - 103 + 0, 54, 54, 55, 58, 58, 61, 64, 65, 68, + 71, 72, 75, 76, 79, 80, 83, 84, 87, 90, + 91, 92, 95, 98, 98, 98, 98, 101, 104, 106, + 107 }; #endif @@ -1494,7 +1498,7 @@ yyreduce: /* Line 1806 of yacc.c */ -#line 1498 "icinga_parser.cc" +#line 1502 "icinga_parser.cc" default: break; } /* User semantic actions sometimes alter yychar, and that requires @@ -1732,6 +1736,6 @@ yyreturn: /* Line 2067 of yacc.c */ -#line 105 "icinga_parser.yy" +#line 109 "icinga_parser.yy" diff --git a/components/configfile/icinga_parser.h b/dyn/config_parser.h similarity index 100% rename from components/configfile/icinga_parser.h rename to dyn/config_parser.h diff --git a/components/configfile/icinga_parser.yy b/dyn/config_parser.yy similarity index 88% rename from components/configfile/icinga_parser.yy rename to dyn/config_parser.yy index 7fcfe96e9..0432255b9 100644 --- a/components/configfile/icinga_parser.yy +++ b/dyn/config_parser.yy @@ -36,13 +36,17 @@ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner); void yyerror(YYLTYPE *locp, ConfigContext *context, const char *err) { - std::cout << locp->first_line << ":" << locp->first_column + stringstream message; + + message << locp->first_line << ":" << locp->first_column << "-" << locp->last_line << ":" << locp->last_column - << ": " << err << std::endl; + << ": " << err << endl; + + throw runtime_error(message.str()); } -#define scanner context->Scanner +#define scanner (context->GetScanner()) %} diff --git a/dyn/configcontext.cpp b/dyn/configcontext.cpp new file mode 100644 index 000000000..99963c910 --- /dev/null +++ b/dyn/configcontext.cpp @@ -0,0 +1,26 @@ +#include "i2-dyn.h" + +using namespace icinga; + +ConfigContext::ConfigContext(istream *input) +{ + m_Input = input; + InitializeScanner(); +} + +ConfigContext::~ConfigContext(void) +{ + DestroyScanner(); +} + +size_t ConfigContext::ReadInput(char *buffer, size_t max_size) +{ + m_Input->read(buffer, max_size); + return m_Input->gcount(); +} + +void *ConfigContext::GetScanner(void) const +{ + return m_Scanner; +} + diff --git a/components/configfile/configcontext.h b/dyn/configcontext.h similarity index 93% rename from components/configfile/configcontext.h rename to dyn/configcontext.h index 2d49b300c..6dca6f9ec 100644 --- a/components/configfile/configcontext.h +++ b/dyn/configcontext.h @@ -29,10 +29,13 @@ public: ConfigContext(istream *input = &cin); virtual ~ConfigContext(void); - istream *Input; - void *Scanner; + size_t ReadInput(char *buffer, size_t max_bytes); + void *GetScanner(void) const; private: + istream *m_Input; + void *m_Scanner; + void InitializeScanner(void); void DestroyScanner(void); }; diff --git a/dyn/i2-dyn.h b/dyn/i2-dyn.h index 13ede7c25..e930716c8 100644 --- a/dyn/i2-dyn.h +++ b/dyn/i2-dyn.h @@ -35,6 +35,7 @@ # define I2_DYN_API I2_IMPORT #endif /* I2_DYN_BUILD */ +#include "configcontext.h" #include "dynamicdictionary.h" #include "dynamicobject.h" #include "objectset.h" diff --git a/dyntest/Makefile.am b/dyntest/Makefile.am index 8dcdc755c..3c271c109 100644 --- a/dyntest/Makefile.am +++ b/dyntest/Makefile.am @@ -12,6 +12,9 @@ dyntest_CPPFLAGS = \ $(BOOST_CPPFLAGS) \ -I${top_srcdir}/base \ -I${top_srcdir}/dyn \ + -I${top_srcdir}/jsonrpc \ + -I${top_srcdir}/icinga \ + -I${top_srcdir}/components/configfile \ -I${top_srcdir} dyntest_LDFLAGS = \ @@ -19,4 +22,5 @@ dyntest_LDFLAGS = \ dyntest_LDADD = \ ${top_builddir}/base/libbase.la \ - ${top_builddir}/dyn/libdyn.la + ${top_builddir}/dyn/libdyn.la \ + ${top_builddir}/components/configfile/configfile.la diff --git a/dyntest/dyntest.cpp b/dyntest/dyntest.cpp index 1b8faba5f..3ad489a21 100644 --- a/dyntest/dyntest.cpp +++ b/dyntest/dyntest.cpp @@ -1,38 +1,12 @@ -#include +#include using namespace icinga; -bool propgetter(string prop, const Object::Ptr& object, string *key) -{ - DynamicObject::Ptr dobj = dynamic_pointer_cast(object); - return dobj->GetConfig()->GetProperty(prop, key); -} - int main(int argc, char **argv) { - for (int i = 0; i < 10000; i++) { - stringstream sname; - sname << "foo" << i; - - DynamicObject::Ptr dobj = make_shared(); - dobj->GetConfig()->SetProperty("type", "process"); - dobj->GetConfig()->SetProperty("name", sname.str()); - dobj->Commit(); - } - - ObjectMap::Ptr byType = make_shared(ObjectSet::GetAllObjects(), - bind(&propgetter, "type", _1, _2)); - byType->Start(); - - ObjectMap::Ptr byName = make_shared(ObjectSet::GetAllObjects(), - bind(&propgetter, "name", _1, _2)); - byName->Start(); - - ObjectMap::Range processes = byType->GetRange("process"); - cout << distance(processes.first, processes.second) << " processes" << endl; - - ObjectMap::Range foo55 = byName->GetRange("foo55"); - cout << distance(foo55.first, foo55.second) << " foo55s" << endl; - + stringstream config; + config << "object process \"foo\" {}"; + ConfigContext ctx(&config); + yyparse(&ctx); return 0; }