From 4fef98682b278338fbd10ab8ab587407cfbbc4c0 Mon Sep 17 00:00:00 2001 From: Damien Stuart Date: Sun, 6 Sep 2009 02:38:30 +0000 Subject: [PATCH] More tweaks to config file processing, including simple variable expansion. git-svn-id: file:///home/mbr/svn/fwknop/trunk@141 510a4753-2344-4c79-9c09-4d669213fbeb --- server/config_init.c | 38 +++++++++++++++++++++++++++++++++++++- server/config_init.h | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/server/config_init.c b/server/config_init.c index 07749296..063e5667 100644 --- a/server/config_init.c +++ b/server/config_init.c @@ -74,6 +74,20 @@ set_config_entry(fko_srv_options_t *opts, int var_ndx, char *value) return; } +/* Given a config parameter name, return its index or -1 if not found. +*/ +int +config_entry_index(fko_srv_options_t *opts, char *var) +{ + int i; + + for(i=0; iconfig[i] != NULL && CONF_VAR_IS(var, config_map[i])) + return(i); + + return(-1); +} + /* Parse the config file... */ static void @@ -82,10 +96,13 @@ parse_config_file(fko_srv_options_t *options, char *config_file) FILE *cfile_ptr; unsigned int numLines = 0; unsigned int i, good_ent; + int cndx; char conf_line_buf[MAX_LINE_LEN] = {0}; char var[MAX_LINE_LEN] = {0}; char val[MAX_LINE_LEN] = {0}; + char tmp1[MAX_LINE_LEN] = {0}; + char tmp2[MAX_LINE_LEN] = {0}; struct stat st; @@ -130,7 +147,10 @@ parse_config_file(fko_srv_options_t *options, char *config_file) } /* - fprintf(stderr, "CONF FILE: %s, LINE: %s\tVar: %s, Val: '%s'\n", config_file, conf_line_buf, var, val); + fprintf(stderr, + "CONF FILE: %s, LINE: %s\tVar: %s, Val: '%s'\n", + config_file, conf_line_buf, var, val + ); */ good_ent = 0; @@ -138,6 +158,22 @@ parse_config_file(fko_srv_options_t *options, char *config_file) { if(CONF_VAR_IS(config_map[i], var)) { + /* First check to see if we need to do a varable expansion + * on this value. Note: this only supports one expansion and + * only if the value starts with the variable. + */ + if(*val == '$') + { + if(sscanf((val+1), "%[A-Z_]%s", tmp1, tmp2)) + { + if((cndx = config_entry_index(options, tmp1)) >= 0) + { + strlcpy(val, options->config[cndx], MAX_LINE_LEN); + strlcat(val, tmp2, MAX_LINE_LEN); + } + } + } + set_config_entry(options, i, val); good_ent++; break; diff --git a/server/config_init.h b/server/config_init.h index 21f03bf1..2a25e824 100644 --- a/server/config_init.h +++ b/server/config_init.h @@ -46,7 +46,7 @@ x == '#' || x == '\n' || x == '\r' || x == ';' || x == '\0' \ ) -/* String compare of macro +/* String compare macro. */ #define CONF_VAR_IS(n, v) (strcmp(n, v) == 0)