[asterisk-commits] branch tilghman/func_odbc_extras r22343 - in /team/tilghman/func_odbc_extras:...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Apr 24 20:49:12 MST 2006


Author: tilghman
Date: Mon Apr 24 22:49:12 2006
New Revision: 22343

URL: http://svn.digium.com/view/asterisk?rev=22343&view=rev
Log:
Move the ARRAY function to func_odbc, so it can share a config file with func_odbc

Modified:
    team/tilghman/func_odbc_extras/configs/func_odbc.conf.sample
    team/tilghman/func_odbc_extras/funcs/func_odbc.c
    team/tilghman/func_odbc_extras/funcs/func_strings.c

Modified: team/tilghman/func_odbc_extras/configs/func_odbc.conf.sample
URL: http://svn.digium.com/view/asterisk/team/tilghman/func_odbc_extras/configs/func_odbc.conf.sample?rev=22343&r1=22342&r2=22343&view=diff
==============================================================================
--- team/tilghman/func_odbc_extras/configs/func_odbc.conf.sample (original)
+++ team/tilghman/func_odbc_extras/configs/func_odbc.conf.sample Mon Apr 24 22:49:12 2006
@@ -19,10 +19,21 @@
 ; inclusion in the SQL statement.
 
 
+; The general section defines delimiters that are created as ARRAY_<foo>
+; functions.  By using the same delimiter both with an ARRAY_<foo> variant,
+; as well as with the output of a read ODBC statement, you can avoid using
+; characters which are in the output as delimiters.
+[general]
+delimiter => TILDE,~
+delimiter => TAB,	
+delimiter => DASH,-
+delimiter => SPACE, 
+
 ; ODBC_SQL - Allow an SQL statement to be built entirely in the dialplan
 [SQL]
 dsn=mysql1
 read=${ARG1}
+;output_delimiter=~
 
 ; ODBC_ANTIGF - A blacklist.
 [ANTIGF]

Modified: team/tilghman/func_odbc_extras/funcs/func_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/func_odbc_extras/funcs/func_odbc.c?rev=22343&r1=22342&r2=22343&view=diff
==============================================================================
--- team/tilghman/func_odbc_extras/funcs/func_odbc.c (original)
+++ team/tilghman/func_odbc_extras/funcs/func_odbc.c Mon Apr 24 22:49:12 2006
@@ -47,23 +47,35 @@
 #include "asterisk/module.h"
 #include "asterisk/config.h"
 #include "asterisk/res_odbc.h"
+#include "asterisk/app.h"
 
 static char *tdesc = "ODBC lookups";
 
 static char *config = "func_odbc.conf";
 
 struct acf_odbc_query {
-	char name[30];
+	AST_LIST_ENTRY(acf_odbc_query) list;
+	char name[80];
 	char dsn[30];
 	char sql_read[2048];
 	char sql_write[2048];
+	char delimiter[2];
 	struct ast_custom_function *acf;
 	unsigned int deleteme:1;
-	struct acf_odbc_query *next;
 };
 
-static struct acf_odbc_query *queries = NULL;
-AST_MUTEX_DEFINE_STATIC(query_lock);
+AST_LIST_HEAD_STATIC(queries, acf_odbc_query);
+
+struct acf_array {
+	AST_LIST_ENTRY(acf_array) list;
+	struct ast_custom_function *acf;
+	char name[30];
+	char delimiter[2];
+	unsigned int deleteme:1;
+};
+
+/* For race reasons, we use the same lock as the queries list */
+AST_LIST_HEAD_NOLOCK_STATIC(arrays, acf_array);
 
 #ifdef NEEDTRACE
 static void acf_odbc_error(SQLHSTMT stmt, int res)
@@ -83,7 +95,7 @@
 {
 	struct odbc_obj *obj;
 	struct acf_odbc_query *query;
-	char *t, *arg, buf[2048]="", varname[15];
+	char *t, *arg, buf[2048]="", varname[15], delimiter[2];
 	int res, argcount=0, valcount=0, i, retry=0;
 	struct ast_channel *ast;
 	SQLHSTMT stmt;
@@ -95,8 +107,8 @@
 	char *tracefile = "/tmp/odbc.trace";
 #endif
 
-	ast_mutex_lock(&query_lock);
-	for (query=queries; query; query = query->next) {
+	AST_LIST_LOCK(&queries);
+	AST_LIST_TRAVERSE(&queries, query, list) {
 		if (!strcasecmp(query->name, cmd + 5)) {
 			break;
 		}
@@ -104,15 +116,16 @@
 
 	if (!query) {
 		ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
-		ast_mutex_unlock(&query_lock);
-		return -1;
-	}
-
+		AST_LIST_UNLOCK(&queries);
+		return -1;
+	}
+
+	ast_copy_string(delimiter, query->delimiter, sizeof(delimiter));
 	obj = odbc_request_obj(query->dsn, 0);
 
 	if (!obj) {
 		ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
-		ast_mutex_unlock(&query_lock);
+		AST_LIST_UNLOCK(&queries);
 		return -1;
 	}
 
@@ -121,7 +134,7 @@
 
 	if (!s || !t) {
 		ast_log(LOG_ERROR, "Out of memory\n");
-		ast_mutex_unlock(&query_lock);
+		AST_LIST_UNLOCK(&queries);
 		return -1;
 	}
 
@@ -171,7 +184,7 @@
 	pbx_builtin_setvar_helper(chan, "VALUE", pbx_builtin_getvar_helper(ast, "VALUE"));
 
 	ast_channel_free(ast);
-	ast_mutex_unlock(&query_lock);
+	AST_LIST_UNLOCK(&queries);
 
 retry_write:
 #ifdef NEEDTRACE
@@ -241,7 +254,7 @@
 {
 	struct odbc_obj *obj;
 	struct acf_odbc_query *query;
-	char *arg, sql[2048] = "", varname[15];
+	char *arg, sql[2048] = "", varname[15], delimiter[2];
 	int count=0, res, x;
 	SQLHSTMT stmt;
 	SQLSMALLINT colcount=0;
@@ -251,8 +264,8 @@
 	char *tracefile = "/tmp/odbc.trace";
 #endif
 
-	ast_mutex_lock(&query_lock);
-	for (query=queries; query; query = query->next) {
+	AST_LIST_LOCK(&queries);
+	AST_LIST_TRAVERSE(&queries, query, list) {
 		if (!strcasecmp(query->name, cmd + 5)) {
 			break;
 		}
@@ -260,15 +273,16 @@
 
 	if (!query) {
 		ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
-		ast_mutex_unlock(&query_lock);
-		return -1;
-	}
-
+		AST_LIST_UNLOCK(&queries);
+		return -1;
+	}
+
+	ast_copy_string(delimiter, query->delimiter, sizeof(delimiter));
 	obj = odbc_request_obj(query->dsn, 0);
 
 	if (!obj) {
 		ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
-		ast_mutex_unlock(&query_lock);
+		AST_LIST_UNLOCK(&queries);
 		return -1;
 	}
 
@@ -292,7 +306,7 @@
 		pbx_builtin_setvar_helper(chan, varname, NULL);
 	}
 
-	ast_mutex_unlock(&query_lock);
+	AST_LIST_UNLOCK(&queries);
 
 	res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
@@ -354,7 +368,7 @@
 
 		strncat(buf + buflen, coldata, len - buflen);
 		coldatalen = strlen(coldata);
-		strncat(buf + buflen + coldatalen, ",", len - buflen - coldatalen);
+		strncat(buf + buflen + coldatalen, delimiter, len - buflen - coldatalen);
 	}
 	/* Trim trailing comma */
 	buf[strlen(buf) - 1] = '\0';
@@ -392,6 +406,161 @@
 	.write = NULL,
 };
 
+static int acf_array_write(struct ast_channel *chan, char *cmd, char *var, const char *value)
+{
+	AST_DECLARE_APP_ARGS(arg1,
+			     AST_APP_ARG(var)[100];
+	);
+	AST_DECLARE_APP_ARGS(arg2,
+			     AST_APP_ARG(val)[100];
+	);
+	char *value2;
+	int i;
+	char *delimiter = ",";
+
+	value2 = ast_strdupa(value);
+	if (!var || !value2)
+		return -1;
+
+	ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
+
+	/* The functions this will generally be used with are SORT and ODBC_*, which
+	 * both return comma-delimited lists.  However, if somebody uses literal lists,
+	 * their commas will be translated to vertical bars by the load, and I don't
+	 * want them to be surprised by the result.  Hence, we prefer commas as the
+	 * delimiter, but we'll fall back to vertical bars if commas aren't found.
+	 */
+	if (strchr(var, ','))
+		AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
+	else
+		AST_STANDARD_APP_ARGS(arg1, var);
+
+	if (!strcmp(cmd, "ARRAY")) {
+		if (strchr(value2, ','))
+			AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
+		else
+			AST_STANDARD_APP_ARGS(arg2, value2);
+	} else {
+		struct acf_array *array;
+
+		AST_LIST_LOCK(&queries);
+		AST_LIST_TRAVERSE(&arrays, array, list) {
+			if (!strcmp(array->acf->name, cmd)) {
+				delimiter = array->delimiter;
+				break;
+			}
+		}
+		AST_LIST_UNLOCK(&queries);
+
+		AST_NONSTANDARD_APP_ARGS(arg2, value2, delimiter[0]);
+	}
+
+	for (i = 0; i < arg1.argc; i++) {
+		ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
+			arg2.val[i]);
+		if (i < arg2.argc) {
+			pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
+		} else {
+			/* We could unset the variable, by passing a NULL, but due to
+			 * pushvar semantics, that could create some undesired behavior. */
+			pbx_builtin_setvar_helper(chan, arg1.var[i], "");
+		}
+	}
+
+	return 0;
+}
+
+static struct ast_custom_function array_function = {
+	.name = "ARRAY",
+	.synopsis = "Allows setting multiple variables at once",
+	.syntax = "ARRAY(var1[,var2[...][,varN]])",
+	.write = acf_array_write,
+	.desc =
+		"The comma-separated list passed as a value to which the function is set will\n"
+		"be interpreted as a set of values to which the comma-separated list of\n"
+		"variable names in the argument should be set.\n"
+		"Hence, Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2\n"
+		"Note: remember to either backslash your commas in extensions.conf or quote the\n"
+		"entire argument, since Set can take multiple arguments itself.\n",
+};
+
+static int init_array(struct acf_array **array, const char *value)
+{
+	char *name, *delimiter;
+
+	name = ast_strdupa(value);
+	if (!name) {
+		return -1;
+	}
+
+	if ((delimiter = strchr(name, ','))) {
+		*delimiter++ = '\0';
+	} else {
+		return -1;
+	}
+
+	*array = ast_calloc(1, sizeof(struct acf_array));
+	if (!(*array))
+		return -1;
+
+	ast_copy_string((*array)->name, name, sizeof((*array)->name));
+	ast_copy_string((*array)->delimiter, delimiter, sizeof((*array)->delimiter));
+
+	/* Explicitly disallow '|' */
+	if ((*array)->delimiter[0] == '|') {
+		ast_log(LOG_ERROR, "The pipe is not a supported delimiter, due to Set syntax.\n");
+		free(*array);
+		return -1;
+	}
+
+	(*array)->acf = ast_calloc(1, sizeof(struct ast_custom_function));
+	if (! (*array)->acf) {
+		free(*array);
+		return -1;
+	}
+
+	asprintf((char **)&((*array)->acf->name), "ARRAY_%s", (*array)->name);
+
+	if (!((*array)->acf->name)) {
+		free((*array)->acf);
+		free(*array);
+		return -1;
+	}
+
+	asprintf((char **)&((*array)->acf->syntax), "%s(<var1>[...[,<varN>]])", (*array)->acf->name);
+
+	if (!((*array)->acf->syntax)) {
+		free((char *)(*array)->acf->name);
+		free((*array)->acf);
+		free(*array);
+		return -1;
+	}
+
+	asprintf((char **)&((*array)->acf->desc),
+		"The comma-separated list passed as a value to which the function is set will\n"
+		"be interpreted as a set of values to which the comma-separated list of\n"
+		"variable names in the argument should be set.\n"
+		"Hence, Set(%s(var1,var2)=1%c2) will set var1 to 1 and var2 to 2\n%s",
+		(*array)->acf->name,
+		(*array)->delimiter[0],
+		(*array)->delimiter[0] == ',' ?
+		"Note: remember to either backslash your commas in extensions.conf or quote the\n"
+		"entire argument, since Set can take multiple arguments itself.\n" : "");
+
+	if (! ((*array)->acf->desc)) {
+		free((char *)(*array)->acf->syntax);
+		free((char *)(*array)->acf->name);
+		free((*array)->acf);
+		free(*array);
+		return -1;
+	}
+
+	(*array)->acf->synopsis = "Assign multiple variables";
+	(*array)->acf->write = acf_array_write;
+
+	return 0;
+}
+
 static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query)
 {
 	char *tmp;
@@ -400,7 +569,7 @@
 		return -1;
 	}
 
-	*query = calloc(1, sizeof(struct acf_odbc_query));
+	*query = ast_calloc(1, sizeof(struct acf_odbc_query));
 	if (! (*query))
 		return -1;
 
@@ -420,53 +589,102 @@
 		ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
 	}
 
-	(*query)->acf = calloc(1, sizeof(struct ast_custom_function));
-	if ((*query)->acf) {
+	if ((tmp = ast_variable_retrieve(cfg, catg, "output_delimiter"))) {
+		ast_copy_string((*query)->delimiter, tmp, sizeof((*query)->delimiter));
+	} else {
+		ast_copy_string((*query)->delimiter, ",", sizeof((*query)->delimiter));
+	}
+
+	(*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function));
+	if (! (*query)->acf) {
+		free(*query);
+		return -1;
+	}
+
+	if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) {
+		asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg);
+	} else {
 		asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg);
-		asprintf((char **)&((*query)->acf->syntax), "ODBC_%s(<arg1>[...[,<argN>]])", catg);
-		(*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
-		if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
-			asprintf((char **)&((*query)->acf->desc),
-						"Runs the following query, as defined in func_odbc.conf, performing\n"
-					   	"substitution of the arguments into the query as specified by ${ARG1},\n"
-						"${ARG2}, ... ${ARGn}.  When setting the function, the values are provided\n"
-						"either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
-						"\nRead:\n%s\n\nWrite:\n%s\n",
-						(*query)->sql_read,
-						(*query)->sql_write);
-		} else if (!ast_strlen_zero((*query)->sql_read)) {
-			asprintf((char **)&((*query)->acf->desc),
-						"Runs the following query, as defined in func_odbc.conf, performing\n"
-					   	"substitution of the arguments into the query as specified by ${ARG1},\n"
-						"${ARG2}, ... ${ARGn}.  This function may only be read, not set.\n\nSQL:\n%s\n",
-						(*query)->sql_read);
-		} else if (!ast_strlen_zero((*query)->sql_write)) {
-			asprintf((char **)&((*query)->acf->desc),
-						"Runs the following query, as defined in func_odbc.conf, performing\n"
-					   	"substitution of the arguments into the query as specified by ${ARG1},\n"
-						"${ARG2}, ... ${ARGn}.  The values are provided either in whole as\n"
-						"${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
-						"This function may only be set.\nSQL:\n%s\n",
-						(*query)->sql_write);
-		}
-
-		if (ast_strlen_zero((*query)->sql_read)) {
-			(*query)->acf->read = NULL;
-		} else {
-			(*query)->acf->read = acf_odbc_read;
-		}
-
-		if (ast_strlen_zero((*query)->sql_write)) {
-			(*query)->acf->write = NULL;
-		} else {
-			(*query)->acf->write = acf_odbc_write;
-		}
-
-		if (! (*query)->acf->name || ! (*query)->acf->syntax || ! (*query)->acf->desc) {
-			return -1;
-		}
+	}
+
+	if (!((*query)->acf->name)) {
+		free((*query)->acf);
+		free(*query);
+		return -1;
+	}
+
+	asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name);
+
+	if (!((*query)->acf->syntax)) {
+		free((char *)(*query)->acf->name);
+		free((*query)->acf);
+		free(*query);
+		return -1;
+	}
+
+	(*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
+	if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
+		asprintf((char **)&((*query)->acf->desc),
+					"Runs the following query, as defined in func_odbc.conf, performing\n"
+				   	"substitution of the arguments into the query as specified by ${ARG1},\n"
+					"${ARG2}, ... ${ARGn}.  When setting the function, the values are provided\n"
+					"either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
+					"\nRead:\n%s\n\nWrite:\n%s\n",
+					(*query)->sql_read,
+					(*query)->sql_write);
+	} else if (!ast_strlen_zero((*query)->sql_read)) {
+		asprintf((char **)&((*query)->acf->desc),
+					"Runs the following query, as defined in func_odbc.conf, performing\n"
+				   	"substitution of the arguments into the query as specified by ${ARG1},\n"
+					"${ARG2}, ... ${ARGn}.  This function may only be read, not set.\n\nSQL:\n%s\n",
+					(*query)->sql_read);
+	} else if (!ast_strlen_zero((*query)->sql_write)) {
+		asprintf((char **)&((*query)->acf->desc),
+					"Runs the following query, as defined in func_odbc.conf, performing\n"
+				   	"substitution of the arguments into the query as specified by ${ARG1},\n"
+					"${ARG2}, ... ${ARGn}.  The values are provided either in whole as\n"
+					"${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
+					"This function may only be set.\nSQL:\n%s\n",
+					(*query)->sql_write);
+	}
+
+	/* Could be out of memory, or could be we have neither sql_read nor sql_write */
+	if (! ((*query)->acf->desc)) {
+		free((char *)(*query)->acf->syntax);
+		free((char *)(*query)->acf->name);
+		free((*query)->acf);
+		free(*query);
+		return -1;
+	}
+
+	if (ast_strlen_zero((*query)->sql_read)) {
+		(*query)->acf->read = NULL;
 	} else {
-		return -1;
+		(*query)->acf->read = acf_odbc_read;
+	}
+
+	if (ast_strlen_zero((*query)->sql_write)) {
+		(*query)->acf->write = NULL;
+	} else {
+		(*query)->acf->write = acf_odbc_write;
+	}
+
+	return 0;
+}
+
+static int free_array(struct acf_array *array)
+{
+	if (array) {
+		if (array->acf) {
+			if (array->acf->name)
+				free((char *)array->acf->name);
+			if (array->acf->syntax)
+				free((char *)array->acf->syntax);
+			if (array->acf->desc)
+				free((char *)array->acf->desc);
+			free(array->acf);
+		}
+		free(array);
 	}
 	return 0;
 }
@@ -494,64 +712,76 @@
 	struct ast_config *cfg;
 	char *catg;
 
-	ast_mutex_lock(&query_lock);
+	AST_LIST_LOCK(&queries);
 
 	cfg = ast_config_load(config);
 	if (!cfg) {
 		ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
-		goto out;
+		AST_LIST_UNLOCK(&queries);
+		return -1;
 	}
 
 	for (catg = ast_category_browse(cfg, NULL);
 	     catg;
 	     catg = ast_category_browse(cfg, catg)) {
-		struct acf_odbc_query *query=NULL;
-
-		if (init_acf_query(cfg, catg, &query)) {
+		struct acf_odbc_query *query = NULL;
+		struct acf_array *array = NULL;
+
+		if (!strcmp(catg, "general")) {
+			struct ast_variable *var;
+			for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
+				if (!strcmp(var->name, "delimiter")) {
+					if (init_array(&array, var->value)) {
+						ast_log(LOG_ERROR, "Unable to initialize delimiter class: %s\n", var->value);
+					} else {
+						AST_LIST_INSERT_HEAD(&arrays, array, list);
+						ast_custom_function_register(array->acf);
+					}
+				}
+			}
+		} else if (init_acf_query(cfg, catg, &query)) {
 			ast_log(LOG_ERROR, "Out of memory\n");
 			free_acf_query(query);
 		} else {
-			query->next = queries;
-			queries = query;
+			AST_LIST_INSERT_HEAD(&queries, query, list);
 			ast_custom_function_register(query->acf);
 		}
 	}
 
 	ast_config_destroy(cfg);
 	ast_custom_function_register(&escape_function);
-out:
-	ast_mutex_unlock(&query_lock);
+	ast_custom_function_register(&array_function);
+
+	AST_LIST_UNLOCK(&queries);
 	return res;
 }
 
 static int odbc_unload_module(void)
 {
-	struct acf_odbc_query *query, *lastquery = NULL;
-
-	ast_mutex_lock(&query_lock);
-	for (query = queries; query; query = query->next) {
-		if (lastquery)
-			free_acf_query(lastquery);
-		if (ast_custom_function_unregister(query->acf)) {
-			ast_log(LOG_ERROR, "Cannot unregister function '%s'?\n", query->acf->name);
-			/* Keep state valid */
-			queries = query;
-			ast_mutex_unlock(&query_lock);
-			return -1;
-		} else {
-			/* If anything is waiting on this lock, this will let it pass (avoids a race) */
-			ast_mutex_unlock(&query_lock);
-			ast_mutex_lock(&query_lock);
-			lastquery = query;
-		}
-	}
-	if (lastquery)
-		free(lastquery);
-	queries = NULL;
+	struct acf_odbc_query *query;
+	struct acf_array *array;
+
+	AST_LIST_LOCK(&queries);
+	while (!AST_LIST_EMPTY(&queries)) {
+		query = AST_LIST_REMOVE_HEAD(&queries, list);
+		ast_custom_function_unregister(query->acf);
+		free_acf_query(query);
+	}
+
+	while (!AST_LIST_EMPTY(&arrays)) {
+		array = AST_LIST_REMOVE_HEAD(&arrays, list);
+		ast_custom_function_unregister(array->acf);
+		free_array(array);
+	}
 
 	ast_custom_function_unregister(&escape_function);
-
-	ast_mutex_unlock(&query_lock);
+	ast_custom_function_unregister(&array_function);
+
+	/* Allow any threads waiting for this lock to pass (avoids a race) */
+	AST_LIST_UNLOCK(&queries);
+	AST_LIST_LOCK(&queries);
+
+	AST_LIST_UNLOCK(&queries);
 	return 0;
 }
 
@@ -559,13 +789,22 @@
 {
 	int res = 0;
 	struct ast_config *cfg;
-	struct acf_odbc_query *q, *prevq = NULL, *qdel = NULL;
+	struct acf_odbc_query *oldquery;
+	struct acf_array *oldarray;
 	char *catg;
 
-	ast_mutex_lock(&query_lock);
-
-	for (q = queries; q; q = q->next) {
-		q->deleteme = 1;
+	AST_LIST_LOCK(&queries);
+
+	while (!AST_LIST_EMPTY(&queries)) {
+		oldquery = AST_LIST_REMOVE_HEAD(&queries, list);
+		ast_custom_function_unregister(oldquery->acf);
+		free_acf_query(oldquery);
+	}
+
+	while (!AST_LIST_EMPTY(&arrays)) {
+		oldarray = AST_LIST_REMOVE_HEAD(&arrays, list);
+		ast_custom_function_unregister(oldarray->acf);
+		free_array(oldarray);
 	}
 
 	cfg = ast_config_load(config);
@@ -578,75 +817,33 @@
 	     catg;
 	     catg = ast_category_browse(cfg, catg)) {
 		struct acf_odbc_query *query = NULL;
-
-		/* We do this piecemeal, so that we stay in a consistent state, if there's ever an error */
-		for (q = queries, prevq=NULL; q; prevq=q, q = q->next) {
-			if (!strcasecmp(catg, q->name)) {
-				break;
+		struct acf_array *array = NULL;
+
+		if (!strcmp(catg, "general")) {
+			struct ast_variable *var;
+			for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
+				if (!strcmp(var->name, "delimiter")) {
+					if (init_array(&array, var->value)) {
+						ast_log(LOG_ERROR, "Unable to initialize delimiter class: %s\n", var->value);
+					} else {
+						AST_LIST_INSERT_HEAD(&arrays, array, list);
+						ast_custom_function_register(array->acf);
+					}
+				}
 			}
-		}
-
-		if (init_acf_query(cfg, catg, &query)) {
-			ast_log(LOG_ERROR, "Cannot initialize query ODBC_%s\n", catg);
-			free_acf_query(query);
 		} else {
-			if (q) {
-				/* Replacement */
-				if (ast_custom_function_unregister(q->acf)) {
-					ast_log(LOG_ERROR, "Cannot reload query %s\n", query->acf->name);
-					free_acf_query(query);
-				} else {
-					ast_custom_function_register(query->acf);
-					/* Add it to the list */
-					if (prevq)
-						prevq->next = query;
-					else
-						queries = query;
-					query->next = q->next;
-					/* Get rid of the old record */
-					free_acf_query(q);
-				}
+			if (init_acf_query(cfg, catg, &query)) {
+				ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
 			} else {
-				/* New */
-				query->next = queries;
-				queries = query;
+				AST_LIST_INSERT_HEAD(&queries, query, list);
 				ast_custom_function_register(query->acf);
 			}
 		}
 	}
 
-	/* Any remaining sets will now be destroyed */
-	for (q = queries; q; q = q->next) {
-		if (qdel) {
-			free_acf_query(qdel);
-			qdel = NULL;
-		}
-
-		if (q->deleteme) {
-			if (ast_custom_function_unregister(q->acf)) {
-				ast_log(LOG_ERROR, "Cannot unregister function?  Refusing to make 'ODBC_%s' go away.\n", q->name);
-			} else {
-				/* If anything is waiting on the lock to execute, this will dispose of it (without a race) */
-				ast_mutex_unlock(&query_lock);
-				ast_mutex_lock(&query_lock);
-
-				if (prevq) {
-					prevq->next = q->next;
-				} else {
-					queries = q->next;
-				}
-				qdel = q;
-			}
-		} else {
-			prevq = q;
-		}
-	}
-	if (qdel)
-		free_acf_query(qdel);
-
 	ast_config_destroy(cfg);
 reload_out:
-	ast_mutex_unlock(&query_lock);
+	AST_LIST_UNLOCK(&queries);
 	return res;
 }
 

Modified: team/tilghman/func_odbc_extras/funcs/func_strings.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/func_odbc_extras/funcs/func_strings.c?rev=22343&r1=22342&r2=22343&view=diff
==============================================================================
--- team/tilghman/func_odbc_extras/funcs/func_strings.c (original)
+++ team/tilghman/func_odbc_extras/funcs/func_strings.c Mon Apr 24 22:49:12 2006
@@ -140,68 +140,6 @@
 		"Regular Expression: Returns 1 if data matches regular expression.",
 	.syntax = "REGEX(\"<regular expression>\" <data>)",
 	.read = regex,
-};
-
-static int array(struct ast_channel *chan, char *cmd, char *var,
-		 const char *value)
-{
-	AST_DECLARE_APP_ARGS(arg1,
-			     AST_APP_ARG(var)[100];
-	);
-	AST_DECLARE_APP_ARGS(arg2,
-			     AST_APP_ARG(val)[100];
-	);
-	char *value2;
-	int i;
-
-	value2 = ast_strdupa(value);
-	if (!var || !value2)
-		return -1;
-
-	/* The functions this will generally be used with are SORT and ODBC_*, which
-	 * both return comma-delimited lists.  However, if somebody uses literal lists,
-	 * their commas will be translated to vertical bars by the load, and I don't
-	 * want them to be surprised by the result.  Hence, we prefer commas as the
-	 * delimiter, but we'll fall back to vertical bars if commas aren't found.
-	 */
-	ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
-	if (strchr(var, ','))
-		AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
-	else
-		AST_STANDARD_APP_ARGS(arg1, var);
-
-	if (strchr(value2, ','))
-		AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
-	else
-		AST_STANDARD_APP_ARGS(arg2, value2);
-
-	for (i = 0; i < arg1.argc; i++) {
-		ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
-			arg2.val[i]);
-		if (i < arg2.argc) {
-			pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
-		} else {
-			/* We could unset the variable, by passing a NULL, but due to
-			 * pushvar semantics, that could create some undesired behavior. */
-			pbx_builtin_setvar_helper(chan, arg1.var[i], "");
-		}
-	}
-
-	return 0;
-}
-
-static struct ast_custom_function array_function = {
-	.name = "ARRAY",
-	.synopsis = "Allows setting multiple variables at once",
-	.syntax = "ARRAY(var1[,var2[...][,varN]])",
-	.write = array,
-	.desc =
-		"The comma-separated list passed as a value to which the function is set will\n"
-		"be interpreted as a set of values to which the comma-separated list of\n"
-		"variable names in the argument should be set.\n"
-		"Hence, Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2\n"
-		"Note: remember to either backslash your commas in extensions.conf or quote the\n"
-		"entire argument, since Set can take multiple arguments itself.\n",
 };
 
 static int quote(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
@@ -431,7 +369,6 @@
 	res |= ast_custom_function_unregister(&fieldqty_function);
 	res |= ast_custom_function_unregister(&filter_function);
 	res |= ast_custom_function_unregister(&regex_function);
-	res |= ast_custom_function_unregister(&array_function);
 	res |= ast_custom_function_unregister(&quote_function);
 	res |= ast_custom_function_unregister(&len_function);
 	res |= ast_custom_function_unregister(&strftime_function);
@@ -449,7 +386,6 @@
 	res |= ast_custom_function_register(&fieldqty_function);
 	res |= ast_custom_function_register(&filter_function);
 	res |= ast_custom_function_register(&regex_function);
-	res |= ast_custom_function_register(&array_function);
 	res |= ast_custom_function_register(&quote_function);
 	res |= ast_custom_function_register(&len_function);
 	res |= ast_custom_function_register(&strftime_function);



More information about the asterisk-commits mailing list