[svn-commits] twilson: branch twilson/config_work r361951 - in /team/twilson/config_work: a...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Apr 11 23:26:12 CDT 2012


Author: twilson
Date: Wed Apr 11 23:26:08 2012
New Revision: 361951

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=361951
Log:
Bug fixes + handle defaults and regex context matching

Modified:
    team/twilson/config_work/apps/app_skel.c
    team/twilson/config_work/main/config_options.c

Modified: team/twilson/config_work/apps/app_skel.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/config_work/apps/app_skel.c?view=diff&rev=361951&r1=361950&r2=361951
==============================================================================
--- team/twilson/config_work/apps/app_skel.c (original)
+++ team/twilson/config_work/apps/app_skel.c Wed Apr 11 23:26:08 2012
@@ -139,29 +139,31 @@
 	NUM_GLOBAL_OBJECTS,
 };
 
+#define PVT_BUCKETS 17
+
 static AO2_GLOBAL_OBJ_STATIC(global_config, NUM_GLOBAL_OBJECTS);
 AST_MUTEX_DEFINE_STATIC(reload_lock);
 
 static struct ao2_container *config_opts;
 
-static void *skel_global_alloc(const char *cat);
-static void *skel_pvt_alloc(const char *cat);
-static void *skel_pvt_cfg_alloc(const char *cat);
+static struct skel_global_config *skel_global_alloc(const char *cat);
+static struct skel_pvt *skel_pvt_alloc(const char *cat);
+static struct skel_pvt_config *skel_pvt_cfg_alloc(const char *cat);
 static struct ao2_container *skel_pvt_container_alloc(void);
 static struct ao2_container *skel_pvt_cfg_container_alloc(void);
-static void *skel_find_pvt_by_category(struct ao2_container *tmp_container, const char *category);
+static struct skel_pvt *skel_find_pvt_by_category(struct ao2_container *tmp_container, const char *category);
 static int skel_apply_config(void *new_global, struct ao2_container *new_pvt_container, struct ao2_container *new_cfg_container);
 
 static struct ast_config_option_info config_info = {
 	.module = AST_MODULE,
 	.filename = "app_skel.conf",
 	.global_contexts = "general",
-	.global_alloc = skel_global_alloc,
-	.pvt_alloc = skel_pvt_alloc,
-	.pvt_cfg_alloc = skel_pvt_cfg_alloc,
+	.global_alloc = (ast_config_option_user_item_alloc) skel_global_alloc,
+	.pvt_alloc = (ast_config_option_user_item_alloc) skel_pvt_alloc,
+	.pvt_cfg_alloc = (ast_config_option_user_item_alloc) skel_pvt_cfg_alloc,
 	.pvt_container_alloc = skel_pvt_container_alloc,
 	.pvt_cfg_container_alloc = skel_pvt_cfg_container_alloc,
-	.find_pvt = skel_find_pvt_by_category,
+	.find_pvt = (ast_config_option_find_pvt_by_category) skel_find_pvt_by_category,
 	.apply_config = skel_apply_config,
 };
 
@@ -212,16 +214,17 @@
 	return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
 }
 
-#define PVT_BUCKETS 17
 static struct ao2_container *skel_pvt_container_alloc(void)
 {
 	return ao2_container_alloc(PVT_BUCKETS, skel_pvt_hash, skel_pvt_cmp);
 }
+
 static struct ao2_container *skel_pvt_cfg_container_alloc(void)
 {
 	return ao2_container_alloc(PVT_BUCKETS, skel_pvt_config_hash, skel_pvt_config_cmp);
 }
-void *skel_find_pvt_by_category(struct ao2_container *tmp_container, const char *category)
+
+static struct skel_pvt *skel_find_pvt_by_category(struct ao2_container *tmp_container, const char *category)
 {
 	if (tmp_container) {
 		return ao2_find(tmp_container, category, OBJ_KEY);
@@ -317,7 +320,7 @@
 	return res;
 }
 
-static void *skel_pvt_alloc(const char *name)
+static struct skel_pvt *skel_pvt_alloc(const char *name)
 {
 	struct skel_pvt *pvt;
 
@@ -333,7 +336,7 @@
 	return pvt;
 }
 
-static void *skel_pvt_cfg_alloc(const char *cat)
+static struct skel_pvt_config *skel_pvt_cfg_alloc(const char *cat)
 {
 	struct skel_pvt_config *pvtcfg;
 
@@ -353,15 +356,10 @@
 
 	ast_string_field_set(pvtcfg, name, cat);
 
-	if (ast_config_option_set_defaults(config_opts, cat, pvtcfg)) {
-		ao2_ref(pvtcfg, -1);
-		return NULL;
-	}
-
 	return pvtcfg;
 }
 
-static void *skel_global_alloc(const char *cat)
+static struct skel_global_config *skel_global_alloc(const char *cat)
 {
 	struct skel_global_config *cfg;
 
@@ -375,12 +373,6 @@
 	}
 
 	if (ast_string_field_init(cfg, 128)) {
-		ao2_ref(cfg, -1);
-		return NULL;
-	}
-
-	/* Set defaults */
-	if (ast_config_option_set_defaults(config_opts, "general", cfg)) {
 		ao2_ref(cfg, -1);
 		return NULL;
 	}
@@ -420,7 +412,7 @@
 {
 	RAII_VAR(struct ao2_container *, pvts, NULL, ao2_cleanup);
 	RAII_VAR(struct ao2_container *, cfgs, NULL, ao2_cleanup);
-	struct ao2_iterator iter __attribute__((cleanup(ao2_iterator_destroy)));
+	struct ao2_iterator iter;
 	struct skel_pvt *pvt;
 	char codec_buf[128];
 
@@ -460,8 +452,9 @@
 		ast_cli(a->fd, SKEL_FORMAT1, pvt->name, cfg->description, codec_buf, AST_CLI_YESNO(cfg->permit != NULL), AST_CLI_YESNO(cfg->bit1), AST_CLI_YESNO(cfg->bit2), pvt->non_config_state);
 		ao2_ref(pvt, -1);
 	}
+	ao2_iterator_destroy(&iter);
 #undef SKEL_FORMAT
-#undef SKEL_FORAMT1
+#undef SKEL_FORMAT1
 
 	return CLI_SUCCESS;
 }

Modified: team/twilson/config_work/main/config_options.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/config_work/main/config_options.c?view=diff&rev=361951&r1=361950&r2=361951
==============================================================================
--- team/twilson/config_work/main/config_options.c (original)
+++ team/twilson/config_work/main/config_options.c Wed Apr 11 23:26:08 2012
@@ -88,6 +88,34 @@
 	return NULL;
 }
 
+static void regex_cleanup(regex_t *regex)
+{
+	if (regex) {
+		regfree(regex);
+		ast_free(regex);
+	}
+}
+
+static regex_t *build_context_regex(const char *text)
+{
+	int res;
+	regex_t *regex;
+
+	if (!(regex = ast_malloc(sizeof(*regex)))) {
+		return NULL;
+	}
+
+	if ((res = regcomp(regex, text, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
+		char buf[80];
+		regerror(res, regex, buf, sizeof(buf));
+		ast_log(LOG_ERROR, "Could not compile regex '%s': %s\n", text, buf);
+		ast_free(regex);
+		return NULL;
+	}
+
+	return regex;
+}
+
 /*! \brief build a config option
  * \note this should probably only be called by one of the ast_config_option_register* macros
  */
@@ -104,17 +132,13 @@
 		return NULL;
 	}
 
-	if (!(regex = ast_calloc(1, sizeof(*regex)))) {
-		return NULL;
-	}
-	if ((tmp = regcomp(regex, context, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
-		char buf[80];
-		regerror(tmp, regex, buf, sizeof(buf));
-		ast_log(LOG_ERROR, "Could not compile regex '%s': %s\n", context, buf);
+	if (!(regex = build_context_regex(context))) {
 		return NULL;
 	}
 
 	if (!(opt = ao2_alloc(sizeof(*opt) + argc * sizeof(opt->args[0]), config_option_destroy))) {
+		regfree(regex);
+		ast_free(regex);
 		return NULL;
 	}
 
@@ -136,6 +160,8 @@
 
 	if (!opt->handler && !(opt->handler = ast_config_option_default_handler(opt->type))) {
 		ao2_ref(opt, -1);
+		regfree(regex);
+		ast_free(regex);
 		return NULL;
 	};
 
@@ -185,12 +211,6 @@
 	return ao2_container_alloc(CONFIG_OPT_BUCKETS, config_opt_hash, config_opt_cmp);
 }
 
-static int is_global_context(struct ast_config_option_info *info, const char *cat)
-{
-	/* XXX Do regex comparison so we can do things like "general|authentication", etc. */
-	return !strcasecmp(cat, "general");
-}
-
 int ast_config_option_parse_config(struct ast_config_option_info *info, int reload)
 {
 	struct ast_config *cfg;
@@ -198,6 +218,7 @@
 	const char *cat = NULL;
 	void *new_global_obj = NULL;
 	struct ao2_container *new_pvt_container = NULL, *new_pvt_cfg_container = NULL;
+	RAII_VAR(regex_t *, regex, NULL, regex_cleanup);
 	
 	if (ast_strlen_zero(info->filename)) {
 		ast_log(LOG_ERROR, "No filename given, cannot proceed!\n");
@@ -233,11 +254,18 @@
 		goto error;
 	}
 
+	if (new_global_obj && !ast_strlen_zero(info->global_contexts) && !(regex = build_context_regex(info->global_contexts))) {
+		goto error;
+	}
+
 	while ((cat = ast_category_browse(cfg, cat))) {
 		RAII_VAR(void *, tmppvt, NULL, ao2_cleanup);
 		RAII_VAR(void *, tmpcfg, NULL, ao2_cleanup);
 
-		if (new_global_obj && is_global_context(info, cat)) {
+		if (new_global_obj && !ast_strlen_zero(info->global_contexts) && !regexec(regex, cat, 0, NULL, 0)) {
+			if (ast_config_option_set_defaults(info->opts_container, cat, new_global_obj)) {
+				goto error;
+			}
 			if (ast_config_parse_category_options(info->opts_container, cfg, cat, new_global_obj)) {
 				goto error;
 			}
@@ -248,14 +276,20 @@
 			}
 
 			/* Get a ref to the existing private, or create a new one */
-			if (!(tmppvt = info->find_pvt(NULL, cat)) && !(tmppvt = info->pvt_alloc(cat))) {
-				/* Since we will be replacing the whole private container, bail out on errors instead of just
-				 * skipping privates with config errors */
-				ast_log(LOG_WARNING, "Could not create pvt '%s,' ignoring all private config changes.\n", cat);
-				goto error;
+			if (!(tmppvt = info->find_pvt(NULL, cat))) {
+				if (!(tmppvt = info->pvt_alloc(cat))) {
+					/* Since we will be replacing the whole private container, bail out on errors instead of just
+					 * skipping privates with config errors */
+					ast_log(LOG_WARNING, "Could not create pvt '%s,' ignoring all private config changes.\n", cat);
+					goto error;
+				}
 			}
 
 			if (!(tmpcfg = info->pvt_cfg_alloc(cat))) {
+				goto error;
+			}
+
+			if (ast_config_option_set_defaults(info->opts_container, cat, tmpcfg)) {
 				goto error;
 			}
 




More information about the svn-commits mailing list