From 62ac640b113103648d6d8f9028f15be66b775fa0 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Tue, 10 Nov 2020 12:29:16 +0100 Subject: [PATCH] core: new command line parameter --cfg-print - print the config file by evaluating include_file, import_file, ifdef and ifndef preprocessor directive - define and subst (and the variants) tokens are not replaced --- src/core/cfg.lex | 42 ++++++++++++++++++++++++++++++------------ src/core/globals.h | 1 + src/main.c | 18 ++++++++++++++++-- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/core/cfg.lex b/src/core/cfg.lex index b105b7b37cd..3a863b46e56 100644 --- a/src/core/cfg.lex +++ b/src/core/cfg.lex @@ -124,7 +124,7 @@ /* start conditions */ %x STRING1 STRING2 STR_BETWEEN COMMENT COMMENT_LN ATTR SELECT AVP_PVAR PVAR_P -%x PVARID INCLF IMPTF EVRTNAME +%x PVARID INCLF IMPTF EVRTNAME CFGPRINTMODE %x LINECOMMENT DEFINE_ID DEFINE_EOL DEFINE_DATA IFDEF_ID IFDEF_EOL IFDEF_SKIP /* config script types : #!SER or #!KAMAILIO or #!MAX_COMPAT */ @@ -670,11 +670,11 @@ IMPORTFILE "import_file" {DEFAULT} { count(); yylval.strval=yytext; return DEFAULT; } {WHILE} { count(); yylval.strval=yytext; return WHILE; } -{INCLUDEFILE} { count(); BEGIN(INCLF); } -{PREP_START}{INCLUDEFILE} { count(); BEGIN(INCLF); } +{INCLUDEFILE} { count(); BEGIN(INCLF); } +{PREP_START}{INCLUDEFILE} { count(); BEGIN(INCLF); } -{IMPORTFILE} { count(); BEGIN(IMPTF); } -{PREP_START}{IMPORTFILE} { count(); BEGIN(IMPTF); } +{IMPORTFILE} { count(); BEGIN(IMPTF); } +{PREP_START}{IMPORTFILE} { count(); BEGIN(IMPTF); } {CFG_SELECT} { count(); yylval.strval=yytext; return CFG_SELECT; } {CFG_RESET} { count(); yylval.strval=yytext; return CFG_RESET; } @@ -1267,6 +1267,10 @@ IMPORTFILE "import_file" {COM_LINE}!{SER_CFG}{CR} { count(); sr_cfg_compat=SR_COMPAT_SER;} {COM_LINE}!{KAMAILIO_CFG}{CR} { count(); + if(ksr_cfg_print_mode == 1) { + printf("%s", yytext); + BEGIN(CFGPRINTMODE); + } sr_cfg_compat=SR_COMPAT_KAMAILIO;} {COM_LINE}!{MAXCOMPAT_CFG}{CR} { count(); sr_cfg_compat=SR_COMPAT_MAX;} @@ -1304,10 +1308,10 @@ IMPORTFILE "import_file" {PREP_START}{SUBSTDEF} { count(); return SUBSTDEF;} {PREP_START}{SUBSTDEFS} { count(); return SUBSTDEFS;} -{PREP_START}{IFDEF}{EAT_ABLE}+ { count(); +{PREP_START}{IFDEF}{EAT_ABLE}+ { count(); if (pp_ifdef_type(1)) return 1; state = IFDEF_S; BEGIN(IFDEF_ID); } -{PREP_START}{IFNDEF}{EAT_ABLE}+ { count(); +{PREP_START}{IFNDEF}{EAT_ABLE}+ { count(); if (pp_ifdef_type(0)) return 1; state = IFDEF_S; BEGIN(IFDEF_ID); } {ID}{MINUS} { count(); @@ -1321,9 +1325,9 @@ IMPORTFILE "import_file" state = IFDEF_EOL_S; BEGIN(IFDEF_EOL); } {EAT_ABLE}*{CR} { count(); pp_ifdef(); } -{PREP_START}{ELSE}{EAT_ABLE}*{CR} { count(); pp_else(); } +{PREP_START}{ELSE}{EAT_ABLE}*{CR} { count(); pp_else(); } -{PREP_START}{ENDIF}{EAT_ABLE}*{CR} { count(); +{PREP_START}{ENDIF}{EAT_ABLE}*{CR} { count(); pp_endif(); } /* we're in an ifdef that evaluated to false -- throw it away */ @@ -1364,7 +1368,11 @@ IMPORTFILE "import_file" exit(-1); } memset(&s_buf, 0, sizeof(s_buf)); - BEGIN(INITIAL); + if(ksr_cfg_print_mode == 1) { + BEGIN(CFGPRINTMODE); + } else { + BEGIN(INITIAL); + } } [ \t]* /* eat the whitespace */ @@ -1378,9 +1386,15 @@ IMPORTFILE "import_file" exit(-1); } memset(&s_buf, 0, sizeof(s_buf)); - BEGIN(INITIAL); + if(ksr_cfg_print_mode == 1) { + BEGIN(CFGPRINTMODE); + } else { + BEGIN(INITIAL); + } } +.|{CR} { count(); printf("%s", yytext); } + <> { switch(state){ @@ -1948,7 +1962,11 @@ static void pp_update_state() return; } - state = INITIAL; BEGIN(INITIAL); + if(ksr_cfg_print_mode == 1) { + state = CFGPRINTMODE; BEGIN(CFGPRINTMODE); + } else { + state = INITIAL; BEGIN(INITIAL); + } } static void pp_ifdef() diff --git a/src/core/globals.h b/src/core/globals.h index 4c120ce36dc..8d4c4677418 100644 --- a/src/core/globals.h +++ b/src/core/globals.h @@ -215,6 +215,7 @@ extern int ksr_route_locks_size; extern str _ksr_xavp_via_params; extern str _ksr_xavp_via_fields; extern int ksr_sip_parser_mode; +extern int ksr_cfg_print_mode; extern char *_sr_uri_host_extra_chars; extern unsigned char *_ksr_hname_extra_chars; diff --git a/src/main.c b/src/main.c index 70ce32ea0c3..722ad6c33b8 100644 --- a/src/main.c +++ b/src/main.c @@ -533,6 +533,8 @@ char *sr_memmng_shm = NULL; static int *_sr_instance_started = NULL; +int ksr_cfg_print_mode = 0; + /** * return 1 if all child processes were forked * - note: they might still be in init phase (i.e., child init) @@ -1933,6 +1935,7 @@ int main(int argc, char** argv) {"modparam", required_argument, 0, KARGOPTVAL + 6}, {"log-engine", required_argument, 0, KARGOPTVAL + 7}, {"debug", required_argument, 0, KARGOPTVAL + 8}, + {"cfg-print", no_argument, 0, KARGOPTVAL + 9}, {0, 0, 0, 0 } }; @@ -2003,6 +2006,9 @@ int main(int argc, char** argv) goto error; } break; + case KARGOPTVAL+9: + ksr_cfg_print_mode = 1; + break; default: if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) { @@ -2165,6 +2171,7 @@ int main(int argc, char** argv) case KARGOPTVAL+6: case KARGOPTVAL+7: case KARGOPTVAL+8: + case KARGOPTVAL+9: break; /* long options */ @@ -2318,13 +2325,20 @@ int main(int argc, char** argv) yyin=cfg_stream; debug_save = default_core_cfg.debug; - if ((yyparse()!=0)||(cfg_errors)||(pp_ifdef_level_check()<0)){ - fprintf(stderr, "ERROR: bad config file (%d errors)\n", cfg_errors); + r = yyparse(); + if (ksr_cfg_print_mode == 1) { + /* printed evaluated content of config file based on include and ifdef */ + return 0; + } + if ((r!=0)||(cfg_errors)||(pp_ifdef_level_check()<0)){ + fprintf(stderr, "ERROR: bad config file (%d errors) (parsing code: %d)\n", + cfg_errors, r); if (debug_flag) default_core_cfg.debug = debug_save; pp_ifdef_level_error(); goto error; } + if (cfg_warnings){ fprintf(stderr, "%d config warnings\n", cfg_warnings); }