From b5cd67adc3af2c42f39cfe01a2f2d318f47103f4 Mon Sep 17 00:00:00 2001 From: Steve Murphy Date: Wed, 29 Aug 2007 20:55:40 +0000 Subject: This code was in team/murf/bug8684-trunk; it should fix bug 8684 in trunk. I didn't add it to 1.4 yet, because it's not entirely clear to me if this is a bug fix or an enhancement. A lot of files were affected by small changes like ast_variable_new getting an added arg, for the file name the var was defined in; ast_category_new gets added args of filename and lineno; ast_category and ast_variable structures now record file and lineno for each entry; a list of all #include and #execs in a config file (or any of its inclusions are now kept in the ast_config struct; at save time, each entry is put back into its proper file of origin, in order. #include and #exec directives are folded in properly. Headers indicating that the file was generated, are generated also for each included file. Some changes to main/manager.c to take care of file renaming, via the UpdateConfig command. Multiple inclusions of the same file are handled by exploding these into multiple include files, uniquely named. There's probably more, but I can't remember it right now. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@81361 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- main/config.c | 384 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 334 insertions(+), 50 deletions(-) (limited to 'main/config.c') diff --git a/main/config.c b/main/config.c index c0e1669e6..17faa504c 100644 --- a/main/config.c +++ b/main/config.c @@ -180,7 +180,9 @@ static struct ast_config_engine *config_engine_list; struct ast_category { char name[80]; int ignored; /*!< do not let user of the config see this category */ - int include_level; + int include_level; + char *file; /*!< the file name from whence this declaration was read */ + int lineno; struct ast_comment *precomments; struct ast_comment *sameline; struct ast_variable *root; @@ -192,26 +194,141 @@ struct ast_config { struct ast_category *root; struct ast_category *last; struct ast_category *current; - struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */ + struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */ int include_level; int max_include_level; + struct ast_config_include *includes; /*!< a list of inclusions, which should describe the entire tree */ +}; + +struct ast_config_include { + char *include_location_file; /*!< file name in which the include occurs */ + int include_location_lineno; /*!< lineno where include occurred */ + int exec; /*!< set to non-zero if itsa #exec statement */ + char *exec_file; /*!< if it's an exec, you'll have both the /var/tmp to read, and the original script */ + char *included_file; /*!< file name included */ + int inclusion_count; /*!< if the file is included more than once, a running count thereof -- but, worry not, + we explode the instances and will include those-- so all entries will be unique */ + int output; /*!< a flag to indicate if the inclusion has been output */ + struct ast_config_include *next; /*!< ptr to next inclusion in the list */ }; -struct ast_variable *ast_variable_new(const char *name, const char *value) +struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename) { struct ast_variable *variable; int name_len = strlen(name) + 1; - if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) { + if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + strlen(filename) + 1 + sizeof(*variable)))) { variable->name = variable->stuff; variable->value = variable->stuff + name_len; + variable->file = variable->stuff + name_len + strlen(value) + 1; strcpy(variable->name,name); strcpy(variable->value,value); + strcpy(variable->file,filename); } - return variable; } +struct ast_config_include *ast_include_new(struct ast_config *conf, const char *from_file, const char *included_file, int is_exec, const char *exec_file, int from_lineno, char *real_included_file_name, int real_included_file_name_size) +{ + /* a file should be included ONCE. Otherwise, if one of the instances is changed, + then all be changed. -- how do we know to include it? -- Handling modified + instances is possible, I'd have + to create a new master for each instance. */ + struct ast_config_include *inc; + + inc = ast_include_find(conf, included_file); + if (inc) + { + inc->inclusion_count++; + snprintf(real_included_file_name, real_included_file_name_size, "%s~~%d", included_file, inc->inclusion_count); + ast_log(LOG_WARNING,"'%s', line %d: Same File included more than once! This data will be saved in %s if saved back to disk.\n", from_file, from_lineno, real_included_file_name); + } else + *real_included_file_name = 0; + + inc = ast_calloc(1,sizeof(struct ast_config_include)); + inc->include_location_file = ast_strdup(from_file); + inc->include_location_lineno = from_lineno; + if (!ast_strlen_zero(real_included_file_name)) + inc->included_file = ast_strdup(real_included_file_name); + else + inc->included_file = ast_strdup(included_file); + + inc->exec = is_exec; + if (is_exec) + inc->exec_file = ast_strdup(exec_file); + + /* attach this new struct to the conf struct */ + inc->next = conf->includes; + conf->includes = inc; + + return inc; +} + +void ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file) +{ + struct ast_config_include *incl; + struct ast_category *cat; + struct ast_variable *v; + + int from_len = strlen(from_file); + int to_len = strlen(to_file); + + if (strcmp(from_file, to_file) == 0) /* no use wasting time if the name is the same */ + return; + + /* the manager code allows you to read in one config file, then + write it back out under a different name. But, the new arrangement + ties output lines to the file name. So, before you try to write + the config file to disk, better riffle thru the data and make sure + the file names are changed. + */ + /* file names are on categories, includes (of course), and on variables. So, + traverse all this and swap names */ + + for (incl = conf->includes; incl; incl=incl->next) { + if (strcmp(incl->include_location_file,from_file) == 0) { + if (from_len >= to_len) + strcpy(incl->include_location_file, to_file); + else { + free(incl->include_location_file); + incl->include_location_file = strdup(to_file); + } + } + } + for (cat = conf->root; cat; cat = cat->next) { + if (strcmp(cat->file,from_file) == 0) { + if (from_len >= to_len) + strcpy(cat->file, to_file); + else { + free(cat->file); + cat->file = strdup(to_file); + } + } + for (v = cat->root; v; v = v->next) { + if (strcmp(v->file,from_file) == 0) { + if (from_len >= to_len) + strcpy(v->file, to_file); + else { + free(v->file); + v->file = strdup(to_file); + } + } + } + } +} + +struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file) +{ + struct ast_config_include *x; + for (x=conf->includes;x;x=x->next) + { + if (strcmp(x->included_file,included_file) == 0) + return x; + } + return 0; +} + + void ast_variable_append(struct ast_category *category, struct ast_variable *variable) { if (!variable) @@ -281,7 +398,7 @@ const char *ast_variable_retrieve(const struct ast_config *config, const char *c static struct ast_variable *variable_clone(const struct ast_variable *old) { - struct ast_variable *new = ast_variable_new(old->name, old->value); + struct ast_variable *new = ast_variable_new(old->name, old->value, old->file); if (new) { new->lineno = old->lineno; @@ -310,12 +427,14 @@ static void move_variables(struct ast_category *old, struct ast_category *new) #endif } -struct ast_category *ast_category_new(const char *name) +struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno) { struct ast_category *category; if ((category = ast_calloc(1, sizeof(*category)))) ast_copy_string(category->name, name, sizeof(category->name)); + category->file = strdup(in_file); + category->lineno = lineno; /* if you don't know the lineno, set it to 999999 or something real big */ return category; } @@ -361,9 +480,28 @@ void ast_category_append(struct ast_config *config, struct ast_category *categor void ast_category_destroy(struct ast_category *cat) { ast_variables_destroy(cat->root); + if (cat->file) + free(cat->file); + ast_free(cat); } +static void ast_includes_destroy(struct ast_config_include *incls) +{ + struct ast_config_include *incl,*inclnext; + + for (incl=incls; incl; incl = inclnext) { + inclnext = incl->next; + if (incl->include_location_file) + free(incl->include_location_file); + if (incl->exec_file) + free(incl->exec_file); + if (incl->included_file) + free(incl->included_file); + free(incl); + } +} + static struct ast_category *next_available_category(struct ast_category *cat) { for (; cat && cat->ignored; cat = cat->next); @@ -494,13 +632,10 @@ int ast_variable_delete(struct ast_category *category, const char *variable, con } int ast_variable_update(struct ast_category *category, const char *variable, - const char *value, const char *match, unsigned int object) + const char *value, const char *match, unsigned int object) { struct ast_variable *cur, *prev=NULL, *newer; - if (!(newer = ast_variable_new(variable, value))) - return -1; - newer->object = object; for (cur = category->root; cur; prev = cur, cur = cur->next) { @@ -508,6 +643,9 @@ int ast_variable_update(struct ast_category *category, const char *variable, (!ast_strlen_zero(match) && strcasecmp(cur->value, match))) continue; + if (!(newer = ast_variable_new(variable, value, cur->file))) + return -1; + newer->next = cur->next; newer->object = cur->object || object; if (prev) @@ -584,6 +722,8 @@ void ast_config_destroy(struct ast_config *cfg) if (!cfg) return; + ast_includes_destroy(cfg->includes); + cat = cfg->root; while (cat) { ast_variables_destroy(cat->root); @@ -657,7 +797,7 @@ static void config_cache_attribute(const char *configfile, enum config_cache_att } static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, struct ast_flags flags, - char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size) + char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size, const char *suggested_include_file) { char *c; char *cur = buf; @@ -681,9 +821,11 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat, if (*c++ != '(') c = NULL; catname = cur; - if (!(*cat = newcat = ast_category_new(catname))) { + if (!(*cat = newcat = ast_category_new(catname, ast_strlen_zero(suggested_include_file)?configfile:suggested_include_file, lineno))) { return -1; } + (*cat)->lineno = lineno; + /* add comments */ if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && *comment_buffer && (*comment_buffer)[0] ) { newcat->precomments = ALLOC_COMMENT(*comment_buffer); @@ -755,10 +897,15 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat, } if (do_include || do_exec) { if (c) { + char *cur2; + char real_inclusion_name[256]; + struct ast_config_include *inclu; + /* Strip off leading and trailing "'s and <>'s */ while ((*c == '<') || (*c == '>') || (*c == '\"')) c++; /* Get rid of leading mess */ cur = c; + cur2 = cur; while (!ast_strlen_zero(cur)) { c = cur + strlen(cur) - 1; if ((*c == '>') || (*c == '<') || (*c == '\"')) @@ -781,7 +928,10 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat, exec_file[0] = '\0'; } /* A #include */ - do_include = ast_config_internal_load(cur, cfg, flags) ? 1 : 0; + /* record this inclusion */ + inclu = ast_include_new(cfg, configfile, cur, do_exec, cur2, lineno, real_inclusion_name, sizeof(real_inclusion_name)); + + do_include = ast_config_internal_load(cur, cfg, flags, real_inclusion_name) ? 1 : 0; if (!ast_strlen_zero(exec_file)) unlink(exec_file); if (!do_include) @@ -814,7 +964,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat, c++; } else object = 0; - if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) { + if ((v = ast_variable_new(ast_strip(cur), ast_strip(c), *suggested_include_file ? suggested_include_file : configfile))) { v->lineno = lineno; v->object = object; /* Put and reset comments */ @@ -840,7 +990,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat, return 0; } -static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, struct ast_flags flags) +static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, struct ast_flags flags, const char *suggested_include_file) { char fn[256]; char buf[8192]; @@ -951,7 +1101,7 @@ static struct ast_config *config_text_file_load(const char *database, const char #else ast_copy_string(fn2, cfinclude->include); #endif - if (config_text_file_load(NULL, NULL, fn2, NULL, flags) == NULL) { + if (config_text_file_load(NULL, NULL, fn2, NULL, flags, "") == NULL) { /* that last field needs to be looked at in this case... TODO */ unchanged = 0; /* One change is enough to short-circuit and reload the whole shebang */ break; @@ -1058,7 +1208,7 @@ static struct ast_config *config_text_file_load(const char *database, const char if (process_buf) { char *buf = ast_strip(process_buf); if (!ast_strlen_zero(buf)) { - if (process_text_line(cfg, &cat, buf, lineno, fn, flags, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) { + if (process_text_line(cfg, &cat, buf, lineno, fn, flags, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size, suggested_include_file)) { cfg = NULL; break; } @@ -1095,60 +1245,171 @@ static struct ast_config *config_text_file_load(const char *database, const char return cfg; } + +/* NOTE: categories and variables each have a file and lineno attribute. On a save operation, these are used to determine + which file and line number to write out to. Thus, an entire hierarchy of config files (via #include statements) can be + recreated. BUT, care must be taken to make sure that every cat and var has the proper file name stored, or you may + be shocked and mystified as to why things are not showing up in the files! + + Also, All #include/#exec statements are recorded in the "includes" LL in the ast_config structure. The file name + and line number are stored for each include, plus the name of the file included, so that these statements may be + included in the output files on a file_save operation. + + The lineno's are really just for relative placement in the file. There is no attempt to make sure that blank lines + are included to keep the lineno's the same between input and output. The lineno fields are used mainly to determine + the position of the #include and #exec directives. So, blank lines tend to disappear from a read/rewrite operation, + and a header gets added. + + vars and category heads are output in the order they are stored in the config file. So, if the software + shuffles these at all, then the placement of #include directives might get a little mixed up, because the + file/lineno data probably won't get changed. + +*/ + +static void gen_header(FILE *f1, const char *configfile, const char *fn, const char *generator) +{ + char date[256]=""; + time_t t; + time(&t); + ast_copy_string(date, ctime(&t), sizeof(date)); + + fprintf(f1, ";!\n"); + fprintf(f1, ";! Automatically generated configuration file\n"); + if (strcmp(configfile, fn)) + fprintf(f1, ";! Filename: %s (%s)\n", configfile, fn); + else + fprintf(f1, ";! Filename: %s\n", configfile); + fprintf(f1, ";! Generator: %s\n", generator); + fprintf(f1, ";! Creation Date: %s", date); + fprintf(f1, ";!\n"); +} + +static void set_fn(char *fn, int fn_size, const char *file, const char *configfile) +{ + if (!file || file[0] == 0) { + if (configfile[0] == '/') + ast_copy_string(fn, configfile, fn_size); + else + snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, configfile); + } else if (file[0] == '/') + ast_copy_string(fn, file, fn_size); + else + snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, file); +} + int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator) { FILE *f; char fn[256]; - char date[256]=""; - time_t t; struct ast_variable *var; struct ast_category *cat; struct ast_comment *cmt; + struct ast_config_include *incl; int blanklines = 0; - if (configfile[0] == '/') { - ast_copy_string(fn, configfile, sizeof(fn)); - } else { - snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile); + /* reset all the output flags, in case this isn't our first time saving this data */ + + for (incl=cfg->includes; incl; incl = incl->next) + incl->output = 0; + + /* go thru all the inclusions and make sure all the files involved (configfile plus all its inclusions) + are all truncated to zero bytes and have that nice header*/ + + for (incl=cfg->includes; incl; incl = incl->next) + { + if (!incl->exec) { /* leave the execs alone -- we'll write out the #exec directives, but won't zero out the include files or exec files*/ + FILE *f1; + + set_fn(fn, sizeof(fn), incl->included_file, configfile); /* normally, fn is just set to incl->included_file, prepended with config dir if relative */ + f1 = fopen(fn,"w"); + if (f1) { + gen_header(f1, configfile, fn, generator); + fclose(f1); /* this should zero out the file */ + } else { + ast_debug(1, "Unable to open for writing: %s\n", fn); + ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno)); + } + } } - time(&t); - ast_copy_string(date, ctime(&t), sizeof(date)); + + set_fn(fn, sizeof(fn), 0, configfile); /* just set fn to absolute ver of configfile */ #ifdef __CYGWIN__ if ((f = fopen(fn, "w+"))) { #else if ((f = fopen(fn, "w"))) { #endif ast_verb(2, "Saving '%s': ", fn); - fprintf(f, ";!\n"); - fprintf(f, ";! Automatically generated configuration file\n"); - if (strcmp(configfile, fn)) - fprintf(f, ";! Filename: %s (%s)\n", configfile, fn); - else - fprintf(f, ";! Filename: %s\n", configfile); - fprintf(f, ";! Generator: %s\n", generator); - fprintf(f, ";! Creation Date: %s", date); - fprintf(f, ";!\n"); + gen_header(f, configfile, fn, generator); cat = cfg->root; + fclose(f); + + /* from here out, we open each involved file and concat the stuff we need to add to the end and immediately close... */ + /* since each var, cat, and associated comments can come from any file, we have to be + mobile, and open each file, print, and close it on an entry-by-entry basis */ + while (cat) { - /* Dump section with any appropriate comment */ - for (cmt = cat->precomments; cmt; cmt=cmt->next) + set_fn(fn, sizeof(fn), cat->file, configfile); + f = fopen(fn, "a"); + if (!f) { + ast_debug(1, "Unable to open for writing: %s\n", fn); + ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno)); + return -1; + } + + /* dump any includes that happen before this category header */ + for (incl=cfg->includes; incl; incl = incl->next) { + if (strcmp(incl->include_location_file, cat->file) == 0){ + if (cat->lineno > incl->include_location_lineno && !incl->output) { + if (incl->exec) + fprintf(f,"#exec \"%s\"\n", incl->exec_file); + else + fprintf(f,"#include \"%s\"\n", incl->included_file); + incl->output = 1; + } + } + } + + /* Dump section with any appropriate comment */ + for (cmt = cat->precomments; cmt; cmt=cmt->next) { if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!') fprintf(f,"%s", cmt->cmt); } if (!cat->precomments) fprintf(f,"\n"); fprintf(f, "[%s]", cat->name); - for (cmt = cat->sameline; cmt; cmt=cmt->next) - { + for (cmt = cat->sameline; cmt; cmt=cmt->next) { fprintf(f,"%s", cmt->cmt); } if (!cat->sameline) fprintf(f,"\n"); + fclose(f); + var = cat->root; while (var) { - for (cmt = var->precomments; cmt; cmt=cmt->next) + set_fn(fn, sizeof(fn), var->file, configfile); + f = fopen(fn, "a"); + if (!f) { + ast_debug(1, "Unable to open for writing: %s\n", fn); + ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno)); + return -1; + } + + /* dump any includes that happen before this category header */ + for (incl=cfg->includes; incl; incl = incl->next) { + if (strcmp(incl->include_location_file, var->file) == 0){ + if (var->lineno > incl->include_location_lineno && !incl->output) { + if (incl->exec) + fprintf(f,"#exec \"%s\"\n", incl->exec_file); + else + fprintf(f,"#include \"%s\"\n", incl->included_file); + incl->output = 1; + } + } + } + + for (cmt = var->precomments; cmt; cmt=cmt->next) { if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!') fprintf(f,"%s", cmt->cmt); } @@ -1161,13 +1422,11 @@ int config_text_file_save(const char *configfile, const struct ast_config *cfg, while (blanklines--) fprintf(f, "\n"); } - + + fclose(f); + var = var->next; } -#if 0 - /* Put an empty line */ - fprintf(f, "\n"); -#endif cat = cat->next; } if (!option_debug) @@ -1177,7 +1436,32 @@ int config_text_file_save(const char *configfile, const struct ast_config *cfg, ast_verb(2, "Unable to write (%s)", strerror(errno)); return -1; } - fclose(f); + + /* Now, for files with trailing #include/#exec statements, + we have to make sure every entry is output */ + + for (incl=cfg->includes; incl; incl = incl->next) { + if (!incl->output) { + /* open the respective file */ + set_fn(fn, sizeof(fn), incl->include_location_file, configfile); + f = fopen(fn, "a"); + if (!f) + { + ast_debug(1, "Unable to open for writing: %s\n", fn); + ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno)); + return -1; + } + + /* output the respective include */ + if (incl->exec) + fprintf(f,"#exec \"%s\"\n", incl->exec_file); + else + fprintf(f,"#include \"%s\"\n", incl->included_file); + fclose(f); + incl->output = 1; + } + } + return 0; } @@ -1240,7 +1524,7 @@ int read_config_maps(void) configtmp = ast_config_new(); configtmp->max_include_level = 1; - config = ast_config_internal_load(extconfig_conf, configtmp, flags); + config = ast_config_internal_load(extconfig_conf, configtmp, flags, ""); if (!config) { ast_config_destroy(configtmp); return 0; @@ -1379,7 +1663,7 @@ static struct ast_config_engine text_file_engine = { .load_func = config_text_file_load, }; -struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, struct ast_flags flags) +struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, struct ast_flags flags, const char *suggested_include_file) { char db[256]; char table[256]; @@ -1408,7 +1692,7 @@ struct ast_config *ast_config_internal_load(const char *filename, struct ast_con } } - result = loader->load_func(db, table, filename, cfg, flags); + result = loader->load_func(db, table, filename, cfg, flags, suggested_include_file); if (result && result != CONFIG_STATUS_FILEUNCHANGED) result->include_level--; @@ -1427,7 +1711,7 @@ struct ast_config *ast_config_load(const char *filename, struct ast_flags flags) if (!cfg) return NULL; - result = ast_config_internal_load(filename, cfg, flags); + result = ast_config_internal_load(filename, cfg, flags, ""); if (!result || result == CONFIG_STATUS_FILEUNCHANGED) ast_config_destroy(cfg); -- cgit v1.2.3