From bf47a84713c6705326b0eb02e9fab7a061f86695 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 22 Apr 2009 18:15:43 +0400 Subject: [PATCH] * Fix variables substitution --- src/cfg_file.l | 2 +- src/cfg_utils.c | 21 +++++++++++++-------- src/main.c | 1 - 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cfg_file.l b/src/cfg_file.l index 77eeca0a9..ebabd3e0a 100644 --- a/src/cfg_file.l +++ b/src/cfg_file.l @@ -154,8 +154,8 @@ yes|YES|no|NO|[yY]|[nN] yylval.flag=parse_flag(yytext); return FLAG; \} BEGIN(INITIAL); return EBRACE; \; return SEMICOLON; = return EQSIGN; -[a-zA-Z0-9_%-]+ yylval.string=strdup(yytext); return PARAM; \$[a-zA-Z_][a-zA-Z0-9_]+ yylval.string=strdup(yytext + 1); return VARIABLE; +[a-zA-Z0-9_%-]+ yylval.string=strdup(yytext); return PARAM; \".+[^\\]\" yylval.string=strdup(yytext + 1); yylval.string[strlen(yylval.string) - 1] = '\0'; unescape_quotes(yylval.string); return QUOTEDSTRING; \n /* ignore EOL */; diff --git a/src/cfg_utils.c b/src/cfg_utils.c index a68f057ec..4907ce64c 100644 --- a/src/cfg_utils.c +++ b/src/cfg_utils.c @@ -354,7 +354,7 @@ parse_flag (const char *str) char * substitute_variable (struct config_file *cfg, char *name, char *str, u_char recursive) { - char *var, *new, *v_begin, *v_end; + char *var, *new, *v_begin, *v_end, *p, t; size_t len; gboolean changed = FALSE; @@ -362,30 +362,35 @@ substitute_variable (struct config_file *cfg, char *name, char *str, u_char recu yywarn ("substitute_variable: trying to substitute variable in NULL string"); return NULL; } - - while ((v_begin = strstr (str, "${")) != NULL) { + + p = str; + while ((v_begin = strstr (p, "${")) != NULL) { len = strlen (str); *v_begin = '\0'; v_begin += 2; if ((v_end = strstr (v_begin, "}")) == NULL) { /* Not a variable, skip */ + p = v_begin; continue; } + t = *v_end; *v_end = '\0'; var = g_hash_table_lookup (cfg->variables, v_begin); if (var == NULL) { yywarn ("substitute_variable: variable %s is not defined", v_begin); - /* Substitute unknown variables with empty string */ - var = ""; + *v_end = t; + p = v_end + 1; + continue; } else if (recursive) { - new = substitute_variable (cfg, v_begin, var, recursive); + var = substitute_variable (cfg, v_begin, var, recursive); } /* Allocate new string */ new = memory_pool_alloc (cfg->cfg_pool, len - strlen (v_begin) + strlen (var) + 3); snprintf (new, len - strlen (v_begin) + strlen (var) + 3, "%s(%s)%s", str, var, v_end + 1); + p = new; str = new; changed = TRUE; } @@ -393,6 +398,7 @@ substitute_variable (struct config_file *cfg, char *name, char *str, u_char recu if (changed && name != NULL) { g_hash_table_insert (cfg->variables, name, str); } + return str; } @@ -414,10 +420,9 @@ static void substitute_all_variables (gpointer key, gpointer value, gpointer data) { struct config_file *cfg = (struct config_file *)data; - char *new; /* Do recursive substitution */ - new = substitute_variable (cfg, (char *)key, (char *)value, 1); + (void)substitute_variable (cfg, (char *)key, (char *)value, 1); } static void diff --git a/src/main.c b/src/main.c index fcf3f3667..2bc79cf14 100644 --- a/src/main.c +++ b/src/main.c @@ -354,7 +354,6 @@ static void dump_cfg_vars () { g_hash_table_foreach (cfg->variables, dump_all_variables, NULL); - g_hash_table_foreach (cfg->modules_opts, dump_module_variables, NULL); } static int -- 2.39.5