[asterisk-commits] jpeeler: branch jpeeler/manager-configactions r101941 - /team/jpeeler/manager...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Feb 1 14:56:16 CST 2008


Author: jpeeler
Date: Fri Feb  1 14:56:15 2008
New Revision: 101941

URL: http://svn.digium.com/view/asterisk?view=rev&rev=101941
Log:
listcontexts renamed to listcategories, updated updateconfig to be able to empty categories, updateconfig stops processing commands upon error

Modified:
    team/jpeeler/manager-configactions/main/config.c
    team/jpeeler/manager-configactions/main/manager.c

Modified: team/jpeeler/manager-configactions/main/config.c
URL: http://svn.digium.com/view/asterisk/team/jpeeler/manager-configactions/main/config.c?view=diff&rev=101941&r1=101940&r2=101941
==============================================================================
--- team/jpeeler/manager-configactions/main/config.c (original)
+++ team/jpeeler/manager-configactions/main/config.c Fri Feb  1 14:56:15 2008
@@ -764,17 +764,13 @@
 {
 	struct ast_category *cat;
 
-	cat = cfg->root;
-	while (cat) {
-		if (cat->name == category) {
+	for (cat = cfg->root; cat; cat = cat->next) {
+		if (!strcasecmp(cat->name, category)) {
+			ast_variables_destroy(cat->root);
+			cat->root = NULL;
+			cat->last = NULL;
 			return 0;
 		}
-
-		cat = cfg->root;
-		if (!strcasecmp(cat->name, category)) {
-			return 0;
-		}
-		cat = cat->next;
 	}
 
 	return -1;

Modified: team/jpeeler/manager-configactions/main/manager.c
URL: http://svn.digium.com/view/asterisk/team/jpeeler/manager-configactions/main/manager.c?view=diff&rev=101941&r1=101940&r2=101941
==============================================================================
--- team/jpeeler/manager-configactions/main/manager.c (original)
+++ team/jpeeler/manager-configactions/main/manager.c Fri Feb  1 14:56:15 2008
@@ -1055,19 +1055,19 @@
 
 static char mandescr_getconfig[] =
 "Description: A 'GetConfig' action will dump the contents of a configuration\n"
-"file by category and contents or optionally by specified context only.\n"
+"file by category and contents or optionally by specified category only.\n"
 "Variables: (Names marked with * are required)\n"
 "   *Filename: Configuration filename (e.g. foo.conf)\n"
-"   Context: Context in configuration file\n";
+"   Category: Category in configuration file\n";
 
 static int action_getconfig(struct mansession *s, const struct message *m)
 {
 	struct ast_config *cfg;
 	const char *fn = astman_get_header(m, "Filename");
-	const char *context = astman_get_header(m, "Context");
+	const char *category = astman_get_header(m, "Category");
 	int catcount = 0;
 	int lineno = 0;
-	char *category=NULL;
+	char *cur_category=NULL;
 	struct ast_variable *v;
 	struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };
 
@@ -1081,30 +1081,30 @@
 	}
 
 	astman_start_ack(s, m);
-	while ((category = ast_category_browse(cfg, category))) {
-		if (ast_strlen_zero(context) || (!ast_strlen_zero(context) && !strcmp(context, category))) {
+	while ((cur_category = ast_category_browse(cfg, cur_category))) {
+		if (ast_strlen_zero(category) || (!ast_strlen_zero(category) && !strcmp(category, cur_category))) {
 			lineno = 0;
-			astman_append(s, "Category-%06d: %s\r\n", catcount, category);
-			for (v = ast_variable_browse(cfg, category); v; v = v->next)
+			astman_append(s, "Category-%06d: %s\r\n", catcount, cur_category);
+			for (v = ast_variable_browse(cfg, cur_category); v; v = v->next)
 				astman_append(s, "Line-%06d-%06d: %s=%s\r\n", catcount, lineno++, v->name, v->value);
 			catcount++;
 		}
 	}
-	if (!ast_strlen_zero(context) && catcount == 0) /* TODO: actually, a config with no categories doesn't even get loaded */
-		astman_append(s, "No contexts found");
+	if (!ast_strlen_zero(category) && catcount == 0) /* TODO: actually, a config with no categories doesn't even get loaded */
+		astman_append(s, "No categories found");
 	ast_config_destroy(cfg);
 	astman_append(s, "\r\n");
 
 	return 0;
 }
 
-static char mandescr_listcontexts[] =
-"Description: A 'ListContexts' action will dump the contents of a context\n"
+static char mandescr_listcategories[] =
+"Description: A 'ListCategories' action will dump the contents of a category\n"
 "in a given file.\n"
 "Variables:\n"
 "   Filename: Configuration filename (e.g. foo.conf)\n";
 
-static int action_listcontexts(struct mansession *s, const struct message *m)
+static int action_listcategories(struct mansession *s, const struct message *m)
 {
 	struct ast_config *cfg;
 	const char *fn = astman_get_header(m, "Filename");
@@ -1126,7 +1126,7 @@
 		catcount++;
 	}
 	if (catcount == 0) /* TODO: actually, a config with no categories doesn't even get loaded */
-		astman_append(s, "Error: no contexts found");
+		astman_append(s, "Error: no categories found");
 	ast_config_destroy(cfg);
 	astman_append(s, "\r\n");
 
@@ -1218,7 +1218,7 @@
 }
 
 /* helper function for action_updateconfig */
-static void handle_updates(struct mansession *s, const struct message *m, struct ast_config *cfg, const char *dfn)
+static int handle_updates(struct mansession *s, const struct message *m, struct ast_config *cfg, const char *dfn)
 {
 	int x;
 	char hdr[40];
@@ -1251,25 +1251,35 @@
 				if (category) {
 					ast_category_append(cfg, category);
 				}
-			}
+			} else
+				return -2;
 		} else if (!strcasecmp(action, "renamecat")) {
 			if (!ast_strlen_zero(cat) && !ast_strlen_zero(value)) {
 				category = ast_category_get(cfg, cat);
 				if (category)
 					ast_category_rename(category, value);
-			}
+			} else 
+				return -3;
 		} else if (!strcasecmp(action, "delcat")) {
 			if (!ast_strlen_zero(cat))
 				ast_category_delete(cfg, cat);
+			else
+				return -2;
 		} else if (!strcasecmp(action, "emptycat")) {
 			if (!ast_strlen_zero(cat))
 				ast_category_empty(cfg, cat);
+			else
+				return -2;
 		} else if (!strcasecmp(action, "update")) {
 			if (!ast_strlen_zero(cat) && !ast_strlen_zero(var) && (category = ast_category_get(cfg, cat)))
 				ast_variable_update(category, var, value, match, object);
+			else
+				return -3;
 		} else if (!strcasecmp(action, "delete")) {
 			if (!ast_strlen_zero(cat) && !ast_strlen_zero(var) && (category = ast_category_get(cfg, cat)))
 				ast_variable_delete(category, var, match);
+			else
+				return -3;
 		} else if (!strcasecmp(action, "append")) {
 			if (!ast_strlen_zero(cat) && !ast_strlen_zero(var) &&
 				(category = ast_category_get(cfg, cat)) &&
@@ -1278,8 +1288,15 @@
 					v->object = 1;
 				ast_variable_append(category, v);
 			}
-		}
-	}
+			else
+				return -3;
+		}
+		else {
+			ast_log(LOG_WARNING, "Action-%06d: %s not handled\n", x, action);
+			return -1;
+		}
+	}
+	return 0;
 }
 
 static char mandescr_updateconfig[] =
@@ -1303,6 +1320,7 @@
 	int res;
 	const char *rld = astman_get_header(m, "Reload");
 	struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };
+	int result;
 
 	if (ast_strlen_zero(sfn) || ast_strlen_zero(dfn)) {
 		astman_send_error(s, m, "Filename not specified");
@@ -1313,19 +1331,30 @@
 		fclose(fp);
 	}
 	cfg = ast_config_load(sfn, config_flags);
-	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) {
-		astman_send_error(s, m, "Save of config failed");
-		return 0;
-	}
-	astman_send_ack(s, m, NULL);
-	if (!ast_strlen_zero(rld)) {
-		if (ast_true(rld))
-			rld = NULL;
-		ast_module_reload(rld);
+	result = handle_updates(s, m, cfg, dfn);
+	if (!result) {
+		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) {
+			astman_send_error(s, m, "Save of config failed");
+			return 0;
+		}
+		astman_send_ack(s, m, NULL);
+		if (!ast_strlen_zero(rld)) {
+			if (ast_true(rld))
+				rld = NULL;
+			ast_module_reload(rld);
+		}
+	} else {
+		switch(result) {
+			case -1: astman_send_error(s, m, "Unknown action command");
+			break;
+			case -2: astman_send_error(s, m, "Category not specified");
+			break;
+			case -3: astman_send_error(s, m, "Category or value not specified");
+			break;
+		}
 	}
 	return 0;
 }
@@ -3526,7 +3555,7 @@
 		ast_manager_register2("GetConfig", EVENT_FLAG_SYSTEM | EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig);
 		ast_manager_register2("GetConfigJSON", EVENT_FLAG_SYSTEM | EVENT_FLAG_CONFIG, action_getconfigjson, "Retrieve configuration (JSON format)", mandescr_getconfigjson);
 		ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig);
-		ast_manager_register2("ListContexts", EVENT_FLAG_CONFIG, action_listcontexts, "List contexts in configuration file", mandescr_listcontexts);
+		ast_manager_register2("ListCategories", EVENT_FLAG_CONFIG, action_listcategories, "List categories in configuration file", mandescr_listcategories);
 		ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect );
 		ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);
 		ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command );




More information about the asterisk-commits mailing list