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

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Apr 25 11:18:37 MST 2006


Author: tilghman
Date: Tue Apr 25 13:18:36 2006
New Revision: 22437

URL: http://svn.digium.com/view/asterisk?rev=22437&view=rev
Log:
Instead of creating multiple ARRAY functions, escape the data instead (and everything magically works, all the time)

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=22437&r1=22436&r2=22437&view=diff
==============================================================================
--- team/tilghman/func_odbc_extras/configs/func_odbc.conf.sample (original)
+++ team/tilghman/func_odbc_extras/configs/func_odbc.conf.sample Tue Apr 25 13:18:36 2006
@@ -19,21 +19,10 @@
 ; 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]
@@ -45,4 +34,5 @@
 dsn=mysql1
 read=SELECT location FROM presence WHERE id='${SQL_ESC(${ARG1})}'
 write=UPDATE presence SET location='${SQL_ESC(${VAL1})}' WHERE id='${SQL_ESC(${ARG1})}'
+;prefix=OFFICE		; Changes this function from ODBC_PRESENCE to OFFICE_PRESENCE
 

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=22437&r1=22436&r2=22437&view=diff
==============================================================================
--- team/tilghman/func_odbc_extras/funcs/func_odbc.c (original)
+++ team/tilghman/func_odbc_extras/funcs/func_odbc.c Tue Apr 25 13:18:36 2006
@@ -55,27 +55,13 @@
 
 struct acf_odbc_query {
 	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;
 };
 
 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)
@@ -95,7 +81,7 @@
 {
 	struct odbc_obj *obj;
 	struct acf_odbc_query *query;
-	char *t, *arg, buf[2048]="", varname[15], delimiter[2];
+	char *t, *arg, buf[2048]="", varname[15];
 	int res, argcount=0, valcount=0, i, retry=0;
 	struct ast_channel *ast;
 	SQLHSTMT stmt;
@@ -109,7 +95,7 @@
 
 	AST_LIST_LOCK(&queries);
 	AST_LIST_TRAVERSE(&queries, query, list) {
-		if (!strcasecmp(query->name, cmd + 5)) {
+		if (!strcmp(query->acf->name, cmd)) {
 			break;
 		}
 	}
@@ -120,7 +106,6 @@
 		return -1;
 	}
 
-	ast_copy_string(delimiter, query->delimiter, sizeof(delimiter));
 	obj = odbc_request_obj(query->dsn, 0);
 
 	if (!obj) {
@@ -254,8 +239,8 @@
 {
 	struct odbc_obj *obj;
 	struct acf_odbc_query *query;
-	char *arg, sql[2048] = "", varname[15], delimiter[2];
-	int count=0, res, x;
+	char *arg, sql[2048] = "", varname[15];
+	int count=0, res, x, buflen = 0;
 	SQLHSTMT stmt;
 	SQLSMALLINT colcount=0;
 	SQLINTEGER indicator;
@@ -266,7 +251,7 @@
 
 	AST_LIST_LOCK(&queries);
 	AST_LIST_TRAVERSE(&queries, query, list) {
-		if (!strcasecmp(query->name, cmd + 5)) {
+		if (!strcmp(query->acf->name, cmd)) {
 			break;
 		}
 	}
@@ -277,7 +262,6 @@
 		return -1;
 	}
 
-	ast_copy_string(delimiter, query->delimiter, sizeof(delimiter));
 	obj = odbc_request_obj(query->dsn, 0);
 
 	if (!obj) {
@@ -350,7 +334,7 @@
 	}
 
 	for (x = 0; x < colcount; x++) {
-		int buflen, coldatalen;
+		int i;
 		char coldata[256];
 
 		buflen = strlen(buf);
@@ -366,12 +350,24 @@
 			return -1;
 		}
 
-		strncat(buf + buflen, coldata, len - buflen);
-		coldatalen = strlen(coldata);
-		strncat(buf + buflen + coldatalen, delimiter, len - buflen - coldatalen);
+		/* Copy data, encoding '\' and ',' for the argument parser */
+		for (i = 0; i < sizeof(coldata); i++) {
+			if (coldata[i] == '\\' || coldata[i] == ',') {
+				buf[buflen++] = '\\';
+			}
+			buf[buflen++] = coldata[i];
+
+			if (buflen >= len - 2)
+				break;
+
+			if (coldata[i] == '\0')
+				break;
+		}
+
+		buf[buflen++] = ',';
 	}
 	/* Trim trailing comma */
-	buf[strlen(buf) - 1] = '\0';
+	buf[buflen - 1] = '\0';
 
 acf_out:
 	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
@@ -406,161 +402,6 @@
 	.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;
@@ -572,8 +413,6 @@
 	*query = ast_calloc(1, sizeof(struct acf_odbc_query));
 	if (! (*query))
 		return -1;
-
-	ast_copy_string((*query)->name, catg, sizeof((*query)->name));
 
 	if ((tmp = ast_variable_retrieve(cfg, catg, "dsn"))) {
 		ast_copy_string((*query)->dsn, tmp, sizeof((*query)->dsn));
@@ -587,12 +426,6 @@
 
 	if ((tmp = ast_variable_retrieve(cfg, catg, "write"))) {
 		ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
-	}
-
-	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));
@@ -672,23 +505,6 @@
 	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;
-}
-
 static int free_acf_query(struct acf_odbc_query *query)
 {
 	if (query) {
@@ -725,21 +541,8 @@
 	     catg;
 	     catg = ast_category_browse(cfg, catg)) {
 		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)) {
+
+		if (init_acf_query(cfg, catg, &query)) {
 			ast_log(LOG_ERROR, "Out of memory\n");
 			free_acf_query(query);
 		} else {
@@ -750,7 +553,6 @@
 
 	ast_config_destroy(cfg);
 	ast_custom_function_register(&escape_function);
-	ast_custom_function_register(&array_function);
 
 	AST_LIST_UNLOCK(&queries);
 	return res;
@@ -759,7 +561,6 @@
 static int odbc_unload_module(void)
 {
 	struct acf_odbc_query *query;
-	struct acf_array *array;
 
 	AST_LIST_LOCK(&queries);
 	while (!AST_LIST_EMPTY(&queries)) {
@@ -768,14 +569,7 @@
 		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_custom_function_unregister(&array_function);
 
 	/* Allow any threads waiting for this lock to pass (avoids a race) */
 	AST_LIST_UNLOCK(&queries);
@@ -790,7 +584,6 @@
 	int res = 0;
 	struct ast_config *cfg;
 	struct acf_odbc_query *oldquery;
-	struct acf_array *oldarray;
 	char *catg;
 
 	AST_LIST_LOCK(&queries);
@@ -801,12 +594,6 @@
 		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);
 	if (!cfg) {
 		ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
@@ -817,27 +604,12 @@
 	     catg;
 	     catg = ast_category_browse(cfg, catg)) {
 		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);
-					}
-				}
-			}
+
+		if (init_acf_query(cfg, catg, &query)) {
+			ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
 		} else {
-			if (init_acf_query(cfg, catg, &query)) {
-				ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
-			} else {
-				AST_LIST_INSERT_HEAD(&queries, query, list);
-				ast_custom_function_register(query->acf);
-			}
+			AST_LIST_INSERT_HEAD(&queries, query, list);
+			ast_custom_function_register(query->acf);
 		}
 	}
 

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=22437&r1=22436&r2=22437&view=diff
==============================================================================
--- team/tilghman/func_odbc_extras/funcs/func_strings.c (original)
+++ team/tilghman/func_odbc_extras/funcs/func_strings.c Tue Apr 25 13:18:36 2006
@@ -140,6 +140,68 @@
 		"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)
@@ -369,6 +431,7 @@
 	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);
@@ -386,6 +449,7 @@
 	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