Sfoglia il codice sorgente

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
Daniel-Constantin Mierla 4 anni fa
parent
commit
62ac640b11
3 ha cambiato i file con 47 aggiunte e 14 eliminazioni
  1. 30 12
      src/core/cfg.lex
  2. 1 0
      src/core/globals.h
  3. 16 2
      src/main.c

+ 30 - 12
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"
 <INITIAL>{DEFAULT}	{ count(); yylval.strval=yytext; return DEFAULT; }
 <INITIAL>{WHILE}	{ count(); yylval.strval=yytext; return WHILE; }
 
-<INITIAL>{INCLUDEFILE}  { count(); BEGIN(INCLF); }
-<INITIAL>{PREP_START}{INCLUDEFILE}  { count(); BEGIN(INCLF); }
+<INITIAL,CFGPRINTMODE>{INCLUDEFILE}  { count(); BEGIN(INCLF); }
+<INITIAL,CFGPRINTMODE>{PREP_START}{INCLUDEFILE}  { count(); BEGIN(INCLF); }
 
-<INITIAL>{IMPORTFILE}  { count(); BEGIN(IMPTF); }
-<INITIAL>{PREP_START}{IMPORTFILE}  { count(); BEGIN(IMPTF); }
+<INITIAL,CFGPRINTMODE>{IMPORTFILE}  { count(); BEGIN(IMPTF); }
+<INITIAL,CFGPRINTMODE>{PREP_START}{IMPORTFILE}  { count(); BEGIN(IMPTF); }
 
 <INITIAL>{CFG_SELECT}	{ count(); yylval.strval=yytext; return CFG_SELECT; }
 <INITIAL>{CFG_RESET}	{ count(); yylval.strval=yytext; return CFG_RESET; }
@@ -1267,6 +1267,10 @@ IMPORTFILE      "import_file"
 <INITIAL>{COM_LINE}!{SER_CFG}{CR}		{ count();
 											sr_cfg_compat=SR_COMPAT_SER;}
 <INITIAL>{COM_LINE}!{KAMAILIO_CFG}{CR}	{ count();
+											if(ksr_cfg_print_mode == 1) {
+												printf("%s", yytext);
+												BEGIN(CFGPRINTMODE);
+											}
 											sr_cfg_compat=SR_COMPAT_KAMAILIO;}
 <INITIAL>{COM_LINE}!{MAXCOMPAT_CFG}{CR}	{ count();
 												sr_cfg_compat=SR_COMPAT_MAX;}
@@ -1304,10 +1308,10 @@ IMPORTFILE      "import_file"
 <INITIAL>{PREP_START}{SUBSTDEF}	{ count();  return SUBSTDEF;}
 <INITIAL>{PREP_START}{SUBSTDEFS}	{ count();  return SUBSTDEFS;}
 
-<INITIAL,IFDEF_SKIP>{PREP_START}{IFDEF}{EAT_ABLE}+    { count();
+<INITIAL,CFGPRINTMODE,IFDEF_SKIP>{PREP_START}{IFDEF}{EAT_ABLE}+    { count();
 								if (pp_ifdef_type(1)) return 1;
 								state = IFDEF_S; BEGIN(IFDEF_ID); }
-<INITIAL,IFDEF_SKIP>{PREP_START}{IFNDEF}{EAT_ABLE}+    { count();
+<INITIAL,CFGPRINTMODE,IFDEF_SKIP>{PREP_START}{IFNDEF}{EAT_ABLE}+    { count();
 								if (pp_ifdef_type(0)) return 1;
 								state = IFDEF_S; BEGIN(IFDEF_ID); }
 <IFDEF_ID>{ID}{MINUS}           { count();
@@ -1321,9 +1325,9 @@ IMPORTFILE      "import_file"
 								state = IFDEF_EOL_S; BEGIN(IFDEF_EOL); }
 <IFDEF_EOL>{EAT_ABLE}*{CR}    { count(); pp_ifdef(); }
 
-<INITIAL,IFDEF_SKIP>{PREP_START}{ELSE}{EAT_ABLE}*{CR}    { count(); pp_else(); }
+<INITIAL,CFGPRINTMODE,IFDEF_SKIP>{PREP_START}{ELSE}{EAT_ABLE}*{CR}    { count(); pp_else(); }
 
-<INITIAL,IFDEF_SKIP>{PREP_START}{ENDIF}{EAT_ABLE}*{CR}    { count();
+<INITIAL,CFGPRINTMODE,IFDEF_SKIP>{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);
+				}
 }
 
 <IMPTF>[ \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);
+				}
 }
 
+<CFGPRINTMODE>.|{CR}  { count(); printf("%s", yytext); }
+
 
 <<EOF>>							{
 									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()

+ 1 - 0
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;

+ 16 - 2
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 @@ try_again:
 
 	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);
 	}