[asterisk-commits] murf: branch murf/bug8684-trunk r81339 - in /team/murf/bug8684-trunk: apps/ i...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 29 10:43:07 CDT 2007


Author: murf
Date: Wed Aug 29 10:43:07 2007
New Revision: 81339

URL: http://svn.digium.com/view/asterisk?view=rev&rev=81339
Log:
OK, the manager interface allows you write to a different config file than what you read in. This messes things up. Add some code to change the name. Also, set the file name correctly for NewCat and Append directives.

Modified:
    team/murf/bug8684-trunk/apps/app_directory.c
    team/murf/bug8684-trunk/include/asterisk/config.h
    team/murf/bug8684-trunk/main/config.c
    team/murf/bug8684-trunk/main/manager.c
    team/murf/bug8684-trunk/res/res_config_odbc.c
    team/murf/bug8684-trunk/res/res_config_pgsql.c
    team/murf/bug8684-trunk/res/res_config_sqlite.c

Modified: team/murf/bug8684-trunk/apps/app_directory.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/apps/app_directory.c?view=diff&rev=81339&r1=81338&r2=81339
==============================================================================
--- team/murf/bug8684-trunk/apps/app_directory.c (original)
+++ team/murf/bug8684-trunk/apps/app_directory.c Wed Aug 29 10:43:07 2007
@@ -386,7 +386,7 @@
 	/* Does the context exist within the config file? If not, make one */
 	cat = ast_category_get(cfg, context);
 	if (!cat) {
-		cat = ast_category_new(context);
+		cat = ast_category_new(context, "", 99999);
 		if (!cat) {
 			ast_log(LOG_WARNING, "Out of memory\n");
 			ast_config_destroy(cfg);

Modified: team/murf/bug8684-trunk/include/asterisk/config.h
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/include/asterisk/config.h?view=diff&rev=81339&r1=81338&r2=81339
==============================================================================
--- team/murf/bug8684-trunk/include/asterisk/config.h (original)
+++ team/murf/bug8684-trunk/include/asterisk/config.h Wed Aug 29 10:43:07 2007
@@ -243,7 +243,7 @@
 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat);
 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var);
 
-struct ast_category *ast_category_new(const char *name);
+struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno);
 void ast_category_append(struct ast_config *config, struct ast_category *cat);
 int ast_category_delete(struct ast_config *cfg, const char *category);
 void ast_category_destroy(struct ast_category *cat);
@@ -253,6 +253,7 @@
 struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename);
 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);
 struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file);
+void ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file);
 void ast_variable_append(struct ast_category *category, struct ast_variable *variable);
 int ast_variable_delete(struct ast_category *category, const char *variable, const char *match);
 int ast_variable_update(struct ast_category *category, const char *variable, 

Modified: team/murf/bug8684-trunk/main/config.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/main/config.c?view=diff&rev=81339&r1=81338&r2=81339
==============================================================================
--- team/murf/bug8684-trunk/main/config.c (original)
+++ team/murf/bug8684-trunk/main/config.c Wed Aug 29 10:43:07 2007
@@ -264,6 +264,59 @@
 	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;
@@ -374,12 +427,14 @@
 #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;
 }
 
@@ -766,13 +821,9 @@
  		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;
 		}
-		if (!ast_strlen_zero(suggested_include_file))
-			(*cat)->file = ast_strdup(suggested_include_file);
-		else
-			(*cat)->file = ast_strdup(configfile);
 		(*cat)->lineno = lineno;
 		
 		/* add comments */
@@ -1194,6 +1245,23 @@
 	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.
+
+*/
+
 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
 {
 	FILE *f;

Modified: team/murf/bug8684-trunk/main/manager.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/main/manager.c?view=diff&rev=81339&r1=81338&r2=81339
==============================================================================
--- team/murf/bug8684-trunk/main/manager.c (original)
+++ team/murf/bug8684-trunk/main/manager.c Wed Aug 29 10:43:07 2007
@@ -1224,7 +1224,7 @@
 }
 
 /* helper function for action_updateconfig */
-static void handle_updates(struct mansession *s, const struct message *m, struct ast_config *cfg)
+static void handle_updates(struct mansession *s, const struct message *m, struct ast_config *cfg, const char *dfn)
 {
 	int x;
 	char hdr[40];
@@ -1253,7 +1253,7 @@
 		match = astman_get_header(m, hdr);
 		if (!strcasecmp(action, "newcat")) {
 			if (!ast_strlen_zero(cat)) {
-				category = ast_category_new(cat);
+				category = ast_category_new(cat, dfn, 99999);
 				if (category) {
 					ast_category_append(cfg, category);
 				}
@@ -1276,7 +1276,7 @@
 		} else if (!strcasecmp(action, "append")) {
 			if (!ast_strlen_zero(cat) && !ast_strlen_zero(var) &&
 				(category = ast_category_get(cfg, cat)) &&
-				(v = ast_variable_new(var, value, ""))){
+				(v = ast_variable_new(var, value, dfn))){
 				if (object || (match && !strcasecmp(match, "object")))
 					v->object = 1;
 				ast_variable_append(category, v);
@@ -1315,7 +1315,8 @@
 		astman_send_error(s, m, "Config file not found");
 		return 0;
 	}
-	handle_updates(s, m, cfg);
+	handle_updates(s, m, cfg, dfn);
+	ast_include_rename(cfg, sfn, dfn); /* change the include references from dfn to sfn, so things match up */
 	res = config_text_file_save(dfn, cfg, "Manager");
 	ast_config_destroy(cfg);
 	if (res) {

Modified: team/murf/bug8684-trunk/res/res_config_odbc.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/res/res_config_odbc.c?view=diff&rev=81339&r1=81338&r2=81339
==============================================================================
--- team/murf/bug8684-trunk/res/res_config_odbc.c (original)
+++ team/murf/bug8684-trunk/res/res_config_odbc.c Wed Aug 29 10:43:07 2007
@@ -334,7 +334,7 @@
 			ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
 			continue;
 		}
-		cat = ast_category_new("");
+		cat = ast_category_new("","",99999);
 		if (!cat) {
 			ast_log(LOG_WARNING, "Out of memory!\n");
 			continue;
@@ -690,7 +690,7 @@
 			continue;
 		} 
 		if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) {
-			cur_cat = ast_category_new(q.category);
+			cur_cat = ast_category_new(q.category, "", 99999);
 			if (!cur_cat) {
 				ast_log(LOG_WARNING, "Out of memory!\n");
 				break;

Modified: team/murf/bug8684-trunk/res/res_config_pgsql.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/res/res_config_pgsql.c?view=diff&rev=81339&r1=81338&r2=81339
==============================================================================
--- team/murf/bug8684-trunk/res/res_config_pgsql.c (original)
+++ team/murf/bug8684-trunk/res/res_config_pgsql.c Wed Aug 29 10:43:07 2007
@@ -313,7 +313,7 @@
 
 		for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
 			var = NULL;
-			if (!(cat = ast_category_new("")))
+			if (!(cat = ast_category_new("","",99999)))
 				continue;
 			for (i = 0; i < numFields; i++) {
 				stringp = PQgetvalue(result, rowIndex, i);
@@ -690,7 +690,7 @@
 			}
 
 			if (strcmp(last, field_category) || last_cat_metric != atoi(field_cat_metric)) {
-				cur_cat = ast_category_new(field_category);
+				cur_cat = ast_category_new(field_category, "", 99999);
 				if (!cur_cat)
 					break;
 				strcpy(last, field_category);

Modified: team/murf/bug8684-trunk/res/res_config_sqlite.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug8684-trunk/res/res_config_sqlite.c?view=diff&rev=81339&r1=81338&r2=81339
==============================================================================
--- team/murf/bug8684-trunk/res/res_config_sqlite.c (original)
+++ team/murf/bug8684-trunk/res/res_config_sqlite.c Wed Aug 29 10:43:07 2007
@@ -735,7 +735,7 @@
 	args = arg;
 
 	if (!args->cat_name || strcmp(args->cat_name, argv[RES_SQLITE_CONFIG_CATEGORY])) {
-		args->cat = ast_category_new(argv[RES_SQLITE_CONFIG_CATEGORY]);
+		args->cat = ast_category_new(argv[RES_SQLITE_CONFIG_CATEGORY], "", 99999);
 
 		if (!args->cat) {
 			ast_log(LOG_WARNING, "Unable to allocate category\n");
@@ -989,7 +989,7 @@
 		return 1;
 	}
 
-	if (!(cat = ast_category_new(cat_name))) {
+	if (!(cat = ast_category_new(cat_name, "", 99999))) {
 		ast_log(LOG_WARNING, "Unable to allocate category\n");
 		return 1;
 	}




More information about the asterisk-commits mailing list