[asterisk-commits] murf: branch murf/bug8684-trunk r81261 - /team/murf/bug8684-trunk/main/config.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Aug 28 13:36:50 CDT 2007


Author: murf
Date: Tue Aug 28 13:36:49 2007
New Revision: 81261

URL: http://svn.digium.com/view/asterisk?view=rev&rev=81261
Log:
Forgot categories; Fleshed out the file writing. Now, to make it WORK.

Modified:
    team/murf/bug8684-trunk/main/config.c

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=81261&r1=81260&r2=81261
==============================================================================
--- team/murf/bug8684-trunk/main/config.c (original)
+++ team/murf/bug8684-trunk/main/config.c Tue Aug 28 13:36:49 2007
@@ -180,7 +180,9 @@
 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,20 +194,21 @@
 	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;
-	int output; /*!< a flag to indicate if the inclusion has been output */
+	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 */
 };
 
@@ -746,6 +749,12 @@
 		if (!(*cat = newcat = ast_category_new(catname))) {
 			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 */
 		if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && *comment_buffer && (*comment_buffer)[0] ) {
 			newcat->precomments = ALLOC_COMMENT(*comment_buffer);
@@ -1174,15 +1183,54 @@
 	struct ast_variable *var;
 	struct ast_category *cat;
 	struct ast_comment *cmt;
+	struct ast_config_include *incl;
 	int blanklines = 0;
+
+	time(&t);
+	ast_copy_string(date, ctime(&t), sizeof(date));
+
+	/* 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;
+			if (incl->included_file[0] == '/') {
+				ast_copy_string(fn, incl->included_file, sizeof(fn));
+			} else {
+				snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, incl->included_file);
+			}
+			f1 = fopen(fn,"w");
+			if (f1) {
+				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");
+				
+				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));
+			}
+		}
+	}
 
 	if (configfile[0] == '/') {
 		ast_copy_string(fn, configfile, sizeof(fn));
 	} else {
 		snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
 	}
-	time(&t);
-	ast_copy_string(date, ctime(&t), sizeof(date));
 #ifdef __CYGWIN__	
 	if ((f = fopen(fn, "w+"))) {
 #else
@@ -1199,26 +1247,86 @@
 		fprintf(f, ";! Creation Date: %s", date);
 		fprintf(f, ";!\n");
 		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) {
+			if (!cat->file || cat->file[0] == 0) {
+				if (configfile[0] == '/')
+					ast_copy_string(fn, configfile, sizeof(fn));
+				else
+					snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
+			} else if (cat->file[0] == '/') 
+				ast_copy_string(fn, cat->file, sizeof(fn));
+			else
+				snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, cat->file);
+
+			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) {
+						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)
-			{
+			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)
+				if (!var->file || var->file[0] == 0) {
+					if (configfile[0] == '/')
+						ast_copy_string(fn, configfile, sizeof(fn));
+					else
+						snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
+				} else if (var->file[0] == '/') 
+					ast_copy_string(fn, var->file, sizeof(fn));
+				else
+					snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, var->file);
+				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) {
+							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);
 				}
@@ -1231,13 +1339,11 @@
 					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)




More information about the asterisk-commits mailing list