[asterisk-commits] tilghman: branch tilghman/str_substitution r175764 - in /team/tilghman/str_su...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Feb 13 19:21:30 CST 2009


Author: tilghman
Date: Fri Feb 13 19:21:30 2009
New Revision: 175764

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=175764
Log:
Several more conversions

Modified:
    team/tilghman/str_substitution/funcs/func_curl.c
    team/tilghman/str_substitution/res/res_config_curl.c
    team/tilghman/str_substitution/res/res_phoneprov.c

Modified: team/tilghman/str_substitution/funcs/func_curl.c
URL: http://svn.digium.com/svn-view/asterisk/team/tilghman/str_substitution/funcs/func_curl.c?view=diff&rev=175764&r1=175763&r2=175764
==============================================================================
--- team/tilghman/str_substitution/funcs/func_curl.c (original)
+++ team/tilghman/str_substitution/funcs/func_curl.c Fri Feb 13 19:21:30 2009
@@ -276,7 +276,7 @@
 	return 0;
 }
 
-static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+static int acf_curlopt_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, int len)
 {
 	struct ast_datastore *store;
 	struct global_curl_info *list[2] = { &global_curl_info, NULL };
@@ -303,38 +303,78 @@
 		AST_LIST_TRAVERSE(list[i], cur, list) {
 			if (cur->key == key) {
 				if (ot == OT_BOOLEAN || ot == OT_INTEGER) {
-					snprintf(buf, len, "%ld", (long)cur->value);
+					if (buf) {
+						snprintf(buf, len, "%ld", (long) cur->value);
+					} else {
+						ast_str_set(bufstr, len, "%ld", (long) cur->value);
+					}
 				} else if (ot == OT_INTEGER_MS) {
-					if ((long)cur->value % 1000 == 0) {
-						snprintf(buf, len, "%ld", (long)cur->value / 1000);
+					if ((long) cur->value % 1000 == 0) {
+						if (buf) {
+							snprintf(buf, len, "%ld", (long)cur->value / 1000);
+						} else {
+							ast_str_set(bufstr, len, "%ld", (long) cur->value / 1000);
+						}
 					} else {
-						snprintf(buf, len, "%.3f", (double)((long)cur->value) / 1000.0);
+						if (buf) {
+							snprintf(buf, len, "%.3f", (double) ((long) cur->value) / 1000.0);
+						} else {
+							ast_str_set(bufstr, len, "%.3f", (double) ((long) cur->value) / 1000.0);
+						}
 					}
 				} else if (ot == OT_STRING) {
 					ast_debug(1, "Found entry %p, with key %d and value %p\n", cur, cur->key, cur->value);
-					ast_copy_string(buf, cur->value, len);
+					if (buf) {
+						ast_copy_string(buf, cur->value, len);
+					} else {
+						ast_str_set(bufstr, 0, "%s", (char *) cur->value);
+					}
 				} else if (key == CURLOPT_PROXYTYPE) {
 					if (0) {
 #if CURLVERSION_ATLEAST(7,15,2)
 					} else if ((long)cur->value == CURLPROXY_SOCKS4) {
-						ast_copy_string(buf, "socks4", len);
+						if (buf) {
+							ast_copy_string(buf, "socks4", len);
+						} else {
+							ast_str_set(bufstr, 0, "socks4");
+						}
 #endif
 #if CURLVERSION_ATLEAST(7,18,0)
 					} else if ((long)cur->value == CURLPROXY_SOCKS4A) {
-						ast_copy_string(buf, "socks4a", len);
+						if (buf) {
+							ast_copy_string(buf, "socks4a", len);
+						} else {
+							ast_str_set(bufstr, 0, "socks4a");
+						}
 #endif
 					} else if ((long)cur->value == CURLPROXY_SOCKS5) {
-						ast_copy_string(buf, "socks5", len);
+						if (buf) {
+							ast_copy_string(buf, "socks5", len);
+						} else {
+							ast_str_set(bufstr, 0, "socks5");
+						}
 #if CURLVERSION_ATLEAST(7,18,0)
 					} else if ((long)cur->value == CURLPROXY_SOCKS5_HOSTNAME) {
-						ast_copy_string(buf, "socks5hostname", len);
+						if (buf) {
+							ast_copy_string(buf, "socks5hostname", len);
+						} else {
+							ast_str_set(bufstr, 0, "socks5hostname");
+						}
 #endif
 #if CURLVERSION_ATLEAST(7,10,0)
 					} else if ((long)cur->value == CURLPROXY_HTTP) {
-						ast_copy_string(buf, "http", len);
+						if (buf) {
+							ast_copy_string(buf, "http", len);
+						} else {
+							ast_str_set(bufstr, 0, "http");
+						}
 #endif
 					} else {
-						ast_copy_string(buf, "unknown", len);
+						if (buf) {
+							ast_copy_string(buf, "unknown", len);
+						} else {
+							ast_str_set(bufstr, 0, "unknown");
+						}
 					}
 				}
 				break;
@@ -347,6 +387,16 @@
 	}
 
 	return cur ? 0 : -1;
+}
+
+static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	return acf_curlopt_helper(chan, cmd, data, buf, NULL, len);
+}
+
+static int acf_curlopt_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, int len)
+{
+	return acf_curlopt_helper(chan, cmd, data, NULL, buf, len);
 }
 
 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
@@ -551,6 +601,7 @@
 "  hashcompat   - Result data will be compatible for use with HASH()\n"
 "",
 	.read = acf_curlopt_read,
+	.read2 = acf_curlopt_read2,
 	.write = acf_curlopt_write,
 };
 

Modified: team/tilghman/str_substitution/res/res_config_curl.c
URL: http://svn.digium.com/svn-view/asterisk/team/tilghman/str_substitution/res/res_config_curl.c?view=diff&rev=175764&r1=175763&r2=175764
==============================================================================
--- team/tilghman/str_substitution/res/res_config_curl.c (original)
+++ team/tilghman/str_substitution/res/res_config_curl.c Fri Feb 13 19:21:30 2009
@@ -43,6 +43,10 @@
 #include "asterisk/module.h"
 #include "asterisk/lock.h"
 #include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
+
+AST_THREADSTORAGE(query_buf);
+AST_THREADSTORAGE(result_buf);
 
 /*!
  * \brief Execute a curl query and return ast_variable list
@@ -55,25 +59,24 @@
 */
 static struct ast_variable *realtime_curl(const char *url, const char *unused, va_list ap)
 {
-	struct ast_str *query;
-	char buf1[200], buf2[200];
+	struct ast_str *query, *buffer;
+	char buf1[256], buf2[256];
 	const char *newparam, *newval;
 	char *stringp, *pair, *key;
 	int i;
-	struct ast_variable *var=NULL, *prev=NULL;
-	const int EncodeSpecialChars = 1, bufsize = 64000;
-	char *buffer;
-
-	if (!ast_custom_function_find("CURL")) {
-		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
-		return NULL;
-	}
-
-	if (!(query = ast_str_create(1000)))
-		return NULL;
-
-	if (!(buffer = ast_malloc(bufsize))) {
-		ast_free(query);
+	struct ast_variable *var = NULL, *prev = NULL;
+	const int EncodeSpecialChars = 1;
+
+	if (!ast_custom_function_find("CURL")) {
+		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
+		return NULL;
+	}
+
+	if (!(query = ast_str_thread_get(&query_buf, 16))) {
+		return NULL;
+	}
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return NULL;
 	}
 
@@ -88,31 +91,33 @@
 	va_end(ap);
 
 	ast_str_append(&query, 0, ")}");
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, bufsize);
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
 
 	/* Remove any trailing newline characters */
-	if ((stringp = strchr(buffer, '\r')) || (stringp = strchr(buffer, '\n')))
+	if ((stringp = strchr(ast_str_buffer(buffer), '\r')) || (stringp = strchr(ast_str_buffer(buffer), '\n'))) {
 		*stringp = '\0';
-
-	stringp = buffer;
+	}
+
+	stringp = ast_str_buffer(buffer);
 	while ((pair = strsep(&stringp, "&"))) {
 		key = strsep(&pair, "=");
 		ast_uri_decode(key);
-		if (pair)
+		if (pair) {
 			ast_uri_decode(pair);
+		}
 
 		if (!ast_strlen_zero(key)) {
 			if (prev) {
 				prev->next = ast_variable_new(key, S_OR(pair, ""), "");
-				if (prev->next)
+				if (prev->next) {
 					prev = prev->next;
-			} else 
+				}
+			} else {
 				prev = var = ast_variable_new(key, S_OR(pair, ""), "");
-		}
-	}
-
-	ast_free(buffer);
-	ast_free(query);
+			}
+		}
+	}
+
 	return var;
 }
 
@@ -127,27 +132,26 @@
 */
 static struct ast_config *realtime_multi_curl(const char *url, const char *unused, va_list ap)
 {
-	struct ast_str *query;
-	char buf1[200], buf2[200];
+	struct ast_str *query, *buffer;
+	char buf1[256], buf2[256];
 	const char *newparam, *newval;
 	char *stringp, *line, *pair, *key, *initfield = NULL;
 	int i;
-	const int EncodeSpecialChars = 1, bufsize = 256000;
-	struct ast_variable *var=NULL;
-	struct ast_config *cfg=NULL;
-	struct ast_category *cat=NULL;
-	char *buffer;
-
-	if (!ast_custom_function_find("CURL")) {
-		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
-		return NULL;
-	}
-
-	if (!(query = ast_str_create(1000)))
-		return NULL;
-
-	if (!(buffer = ast_malloc(bufsize))) {
-		ast_free(query);
+	const int EncodeSpecialChars = 1;
+	struct ast_variable *var = NULL;
+	struct ast_config *cfg = NULL;
+	struct ast_category *cat = NULL;
+
+	if (!ast_custom_function_find("CURL")) {
+		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
+		return NULL;
+	}
+
+	if (!(query = ast_str_thread_get(&query_buf, 16))) {
+		return NULL;
+	}
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return NULL;
 	}
 
@@ -170,28 +174,33 @@
 	ast_str_append(&query, 0, ")}");
 
 	/* Do the CURL query */
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, bufsize);
-
-	if (!(cfg = ast_config_new()))
-		goto exit_multi;
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
+
+	if (!(cfg = ast_config_new())) {
+		return NULL;
+	}
 
 	/* Line oriented output */
-	stringp = buffer;
+	stringp = ast_str_buffer(buffer);
 	while ((line = strsep(&stringp, "\r\n"))) {
-		if (ast_strlen_zero(line))
+		if (ast_strlen_zero(line)) {
 			continue;
-
-		if (!(cat = ast_category_new("", "", 99999)))
+		}
+
+		if (!(cat = ast_category_new("", "", 99999))) {
 			continue;
+		}
 
 		while ((pair = strsep(&line, "&"))) {
 			key = strsep(&pair, "=");
 			ast_uri_decode(key);
-			if (pair)
+			if (pair) {
 				ast_uri_decode(pair);
-
-			if (!strcasecmp(key, initfield) && pair)
+			}
+
+			if (!strcasecmp(key, initfield) && pair) {
 				ast_category_rename(cat, pair);
+			}
 
 			if (!ast_strlen_zero(key)) {
 				var = ast_variable_new(key, S_OR(pair, ""), "");
@@ -201,9 +210,6 @@
 		ast_category_append(cfg, cat);
 	}
 
-exit_multi:
-	ast_free(buffer);
-	ast_free(query);
 	return cfg;
 }
 
@@ -224,24 +230,23 @@
 */
 static int update_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
 {
-	struct ast_str *query;
-	char buf1[200], buf2[200];
+	struct ast_str *query, *buffer;
+	char buf1[256], buf2[256];
 	const char *newparam, *newval;
 	char *stringp;
 	int i, rowcount = -1;
-	const int EncodeSpecialChars = 1, bufsize = 100;
-	char *buffer;
-
-	if (!ast_custom_function_find("CURL")) {
-		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
-		return -1;
-	}
-
-	if (!(query = ast_str_create(1000)))
-		return -1;
-
-	if (!(buffer = ast_malloc(bufsize))) {
-		ast_free(query);
+	const int EncodeSpecialChars = 1;
+
+	if (!ast_custom_function_find("CURL")) {
+		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
+		return -1;
+	}
+
+	if (!(query = ast_str_thread_get(&query_buf, 16))) {
+		return -1;
+	}
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return -1;
 	}
 
@@ -258,43 +263,40 @@
 	va_end(ap);
 
 	ast_str_append(&query, 0, ")}");
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, bufsize);
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
 
 	/* Line oriented output */
-	stringp = buffer;
-	while (*stringp <= ' ')
+	stringp = ast_str_buffer(buffer);
+	while (*stringp <= ' ') {
 		stringp++;
+	}
 	sscanf(stringp, "%d", &rowcount);
 
-	ast_free(buffer);
-	ast_free(query);
-
-	if (rowcount >= 0)
+	if (rowcount >= 0) {
 		return (int)rowcount;
+	}
 
 	return -1;
 }
 
 static int update2_curl(const char *url, const char *unused, va_list ap)
 {
-	struct ast_str *query;
+	struct ast_str *query, *buffer;
 	char buf1[200], buf2[200];
 	const char *newparam, *newval;
 	char *stringp;
 	int rowcount = -1, lookup = 1, first = 1;
-	const int EncodeSpecialChars = 1, bufsize = 100;
-	char *buffer;
-
-	if (!ast_custom_function_find("CURL")) {
-		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
-		return -1;
-	}
-
-	if (!(query = ast_str_create(1000)))
-		return -1;
-
-	if (!(buffer = ast_malloc(bufsize))) {
-		ast_free(query);
+	const int EncodeSpecialChars = 1;
+
+	if (!ast_custom_function_find("CURL")) {
+		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
+		return -1;
+	}
+
+	if (!(query = ast_str_thread_get(&query_buf, 1000)))
+		return -1;
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return -1;
 	}
 
@@ -316,24 +318,27 @@
 		ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
 		ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
 		ast_str_append(&query, 0, "%s%s=%s", first ? "" : "&", buf1, buf2);
+		first = 0;
 	}
 	va_end(ap);
 
 	ast_str_append(&query, 0, ")}");
-	/* TODO: Make proxies work */
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, bufsize);
+	/* Proxies work, by setting CURLOPT options in the [globals] section of
+	 * extensions.conf.  Unfortunately, this means preloading pbx_config.so
+	 * so that they have an opportunity to be set prior to startup realtime
+	 * queries. */
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
 
 	/* Line oriented output */
-	stringp = buffer;
-	while (*stringp <= ' ')
+	stringp = ast_str_buffer(buffer);
+	while (*stringp <= ' ') {
 		stringp++;
+	}
 	sscanf(stringp, "%d", &rowcount);
 
-	ast_free(buffer);
-	ast_free(query);
-
-	if (rowcount >= 0)
+	if (rowcount >= 0) {
 		return (int)rowcount;
+	}
 
 	return -1;
 }
@@ -353,24 +358,23 @@
 */
 static int store_curl(const char *url, const char *unused, va_list ap)
 {
-	struct ast_str *query;
-	char buf1[200], buf2[200];
+	struct ast_str *query, *buffer;
+	char buf1[256], buf2[256];
 	const char *newparam, *newval;
 	char *stringp;
 	int i, rowcount = -1;
-	const int EncodeSpecialChars = 1, bufsize = 100;
-	char *buffer;
-
-	if (!ast_custom_function_find("CURL")) {
-		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
-		return -1;
-	}
-
-	if (!(query = ast_str_create(1000)))
-		return -1;
-
-	if (!(buffer = ast_malloc(bufsize))) {
-		ast_free(query);
+	const int EncodeSpecialChars = 1;
+
+	if (!ast_custom_function_find("CURL")) {
+		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
+		return -1;
+	}
+
+	if (!(query = ast_str_thread_get(&query_buf, 1000))) {
+		return -1;
+	}
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return -1;
 	}
 
@@ -385,18 +389,17 @@
 	va_end(ap);
 
 	ast_str_append(&query, 0, ")}");
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, bufsize);
-
-	stringp = buffer;
-	while (*stringp <= ' ')
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
+
+	stringp = ast_str_buffer(buffer);
+	while (*stringp <= ' ') {
 		stringp++;
+	}
 	sscanf(stringp, "%d", &rowcount);
 
-	ast_free(buffer);
-	ast_free(query);
-
-	if (rowcount >= 0)
-		return (int)rowcount;
+	if (rowcount >= 0) {
+		return rowcount;
+	}
 
 	return -1;
 }
@@ -418,24 +421,23 @@
 */
 static int destroy_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
 {
-	struct ast_str *query;
+	struct ast_str *query, *buffer;
 	char buf1[200], buf2[200];
 	const char *newparam, *newval;
 	char *stringp;
 	int i, rowcount = -1;
-	const int EncodeSpecialChars = 1, bufsize = 100;
-	char *buffer;
-
-	if (!ast_custom_function_find("CURL")) {
-		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
-		return -1;
-	}
-
-	if (!(query = ast_str_create(1000)))
-		return -1;
-
-	if (!(buffer = ast_malloc(bufsize))) {
-		ast_free(query);
+	const int EncodeSpecialChars = 1;
+
+	if (!ast_custom_function_find("CURL")) {
+		ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
+		return -1;
+	}
+
+	if (!(query = ast_str_thread_get(&query_buf, 1000))) {
+		return -1;
+	}
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return -1;
 	}
 
@@ -452,27 +454,26 @@
 	va_end(ap);
 
 	ast_str_append(&query, 0, ")}");
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, bufsize);
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
 
 	/* Line oriented output */
-	stringp = buffer;
-	while (*stringp <= ' ')
+	stringp = ast_str_buffer(buffer);
+	while (*stringp <= ' ') {
 		stringp++;
+	}
 	sscanf(stringp, "%d", &rowcount);
 
-	ast_free(buffer);
-	ast_free(query);
-
-	if (rowcount >= 0)
+	if (rowcount >= 0) {
 		return (int)rowcount;
+	}
 
 	return -1;
 }
 
 static int require_curl(const char *url, const char *unused, va_list ap)
 {
-	struct ast_str *query;
-	char *elm, field[256], buffer[128];
+	struct ast_str *query, *buffer;
+	char *elm, field[256];
 	int type, size;
 	const int EncodeSpecialChars = 1;
 
@@ -481,7 +482,11 @@
 		return -1;
 	}
 
-	if (!(query = ast_str_create(100))) {
+	if (!(query = ast_str_thread_get(&query_buf, 100))) {
+		return -1;
+	}
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return -1;
 	}
 
@@ -511,19 +516,19 @@
 	va_end(ap);
 
 	ast_str_append(&query, 0, ")}");
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, sizeof(buffer));
-	return atoi(buffer);
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
+	return atoi(ast_str_buffer(buffer));
 }
 
 static struct ast_config *config_curl(const char *url, const char *unused, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *sugg_incl, const char *who_asked)
 {
-	struct ast_str *query;
+	struct ast_str *query, *buffer;
 	char buf1[200];
 	char *stringp, *line, *pair, *key;
-	const int EncodeSpecialChars = 1, bufsize = 256000;
+	const int EncodeSpecialChars = 1;
 	int last_cat_metric = -1, cat_metric = -1;
-	struct ast_category *cat=NULL;
-	char *buffer, *cur_cat = "";
+	struct ast_category *cat = NULL;
+	char *cur_cat = "";
 	char *category = "", *var_name = "", *var_val = "";
 	struct ast_flags loader_flags = { 0 };
 
@@ -532,11 +537,11 @@
 		return NULL;
 	}
 
-	if (!(query = ast_str_create(1000)))
-		return NULL;
-
-	if (!(buffer = ast_malloc(bufsize))) {
-		ast_free(query);
+	if (!(query = ast_str_thread_get(&query_buf, 100))) {
+		return NULL;
+	}
+
+	if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
 		return NULL;
 	}
 
@@ -544,30 +549,33 @@
 	ast_str_set(&query, 0, "${CURL(%s/static?file=%s)}", url, buf1);
 
 	/* Do the CURL query */
-	pbx_substitute_variables_helper(NULL, ast_str_buffer(query), buffer, bufsize);
+	ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
 
 	/* Line oriented output */
-	stringp = buffer;
+	stringp = ast_str_buffer(buffer);
 	cat = ast_config_get_current_category(cfg);
 
 	while ((line = strsep(&stringp, "\r\n"))) {
-		if (ast_strlen_zero(line))
+		if (ast_strlen_zero(line)) {
 			continue;
+		}
 
 		while ((pair = strsep(&line, "&"))) {
 			key = strsep(&pair, "=");
 			ast_uri_decode(key);
-			if (pair)
+			if (pair) {
 				ast_uri_decode(pair);
-
-			if (!strcasecmp(key, "category"))
+			}
+
+			if (!strcasecmp(key, "category")) {
 				category = S_OR(pair, "");
-			else if (!strcasecmp(key, "var_name"))
+			} else if (!strcasecmp(key, "var_name")) {
 				var_name = S_OR(pair, "");
-			else if (!strcasecmp(key, "var_val"))
+			} else if (!strcasecmp(key, "var_val")) {
 				var_val = S_OR(pair, "");
-			else if (!strcasecmp(key, "cat_metric"))
+			} else if (!strcasecmp(key, "cat_metric")) {
 				cat_metric = pair ? atoi(pair) : 0;
+			}
 		}
 
 		if (!strcmp(var_name, "#include")) {
@@ -585,8 +593,6 @@
 		ast_variable_append(cat, ast_variable_new(var_name, var_val, ""));
 	}
 
-	ast_free(buffer);
-	ast_free(query);
 	return cfg;
 }
 

Modified: team/tilghman/str_substitution/res/res_phoneprov.c
URL: http://svn.digium.com/svn-view/asterisk/team/tilghman/str_substitution/res/res_phoneprov.c?view=diff&rev=175764&r1=175763&r2=175764
==============================================================================
--- team/tilghman/str_substitution/res/res_phoneprov.c (original)
+++ team/tilghman/str_substitution/res/res_phoneprov.c Fri Feb 13 19:21:30 2009
@@ -459,8 +459,7 @@
 		route = unref_route(route);
 		return NULL;
 	} else { /* Dynamic file */
-		int bufsize;
-		char *tmp;
+		struct ast_str *tmp;
 
 		len = load_file(path, &file);
 		if (len < 0) {
@@ -476,12 +475,7 @@
 			goto out500;
 		}
 
-		/* XXX This is a hack -- maybe sum length of all variables in route->user->headp and add that? */
- 		bufsize = len + VAR_BUF_SIZE;
-
-		/* malloc() instead of alloca() here, just in case the file is bigger than
-		 * we have enough stack space for. */
-		if (!(tmp = ast_calloc(1, bufsize))) {
+		if (!(tmp = ast_str_create(len))) {
 			if (file) {
 				ast_free(file);
 			}
@@ -510,7 +504,7 @@
 			}
 		}
 
-		pbx_substitute_variables_varshead(AST_LIST_FIRST(&route->user->extensions)->headp, file, tmp, bufsize);
+		ast_str_substitute_variables_varshead(&tmp, 0, AST_LIST_FIRST(&route->user->extensions)->headp, file);
 
 		if (file) {
 			ast_free(file);
@@ -520,7 +514,7 @@
 			"Content-Type: %s\r\n"
 			"Content-length: %d\r\n"
 			"\r\n"
-			"%s", route->file->mime_type, (int) strlen(tmp), tmp);
+			"%s", route->file->mime_type, ast_str_strlen(tmp), ast_str_buffer(tmp));
 
 		if (tmp) {
 			ast_free(tmp);
@@ -856,17 +850,24 @@
 static int add_user_extension(struct user *user, struct extension *exten)
 {
 	struct ast_var_t *var;
+	struct ast_str *str = ast_str_create(16);
+
+	if (!str) {
+		return -1;
+	}
 
 	/* Append profile variables here, and substitute variables on profile
 	 * setvars, so that we can use user specific variables in them */
 	AST_LIST_TRAVERSE(user->profile->headp, var, entries) {
-		char expand_buf[VAR_BUF_SIZE] = {0,};
 		struct ast_var_t *var2;
 
-		pbx_substitute_variables_varshead(exten->headp, var->value, expand_buf, sizeof(expand_buf));
-		if ((var2 = ast_var_assign(var->name, expand_buf)))
+		ast_str_substitute_variables_varshead(&str, 0, exten->headp, var->value);
+		if ((var2 = ast_var_assign(var->name, ast_str_buffer(str)))) {
 			AST_LIST_INSERT_TAIL(exten->headp, var2, entries);
-	}
+		}
+	}
+
+	ast_free(str);
 
 	if (AST_LIST_EMPTY(&user->extensions)) {
 		AST_LIST_INSERT_HEAD(&user->extensions, exten, entry);
@@ -893,14 +894,18 @@
 static int build_user_routes(struct user *user)
 {
 	struct phoneprov_file *pp_file;
+	struct ast_str *str;
+
+	if (!(str = ast_str_create(16))) {
+		return -1;
+	}
 
 	AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
-		char expand_buf[VAR_BUF_SIZE] = { 0, };
-
-		pbx_substitute_variables_varshead(AST_LIST_FIRST(&user->extensions)->headp, pp_file->format, expand_buf, sizeof(expand_buf));
-		build_route(pp_file, user, expand_buf);
-	}
-
+		ast_str_substitute_variables_varshead(&str, 0, AST_LIST_FIRST(&user->extensions)->headp, pp_file->format);
+		build_route(pp_file, user, ast_str_buffer(str));
+	}
+
+	ast_free(str);
 	return 0;
 }
 
@@ -1079,17 +1084,22 @@
 }
 
 /*! \brief A dialplan function that can be used to print a string for each phoneprov user */
-static int pp_each_user_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
-{
-	char *tmp, expand_buf[VAR_BUF_SIZE] = {0,};
+static int pp_each_user_helper(struct ast_channel *chan, char *data, char *buf, struct ast_str **bufstr, int len)
+{
+	char *tmp;
 	struct ao2_iterator i;
 	struct user *user;
+	struct ast_str *str;
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(string);
 		AST_APP_ARG(exclude_mac);
 	);
 	AST_STANDARD_APP_ARGS(args, data);
 
+	if (!(str = ast_str_create(16))) {
+		return -1;
+	}
+
 	/* Fix data by turning %{ into ${ */
 	while ((tmp = strstr(args.string, "%{")))
 		*tmp = '$';
@@ -1099,12 +1109,27 @@
 		if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac)) {
 			continue;
 		}
-		pbx_substitute_variables_varshead(AST_LIST_FIRST(&user->extensions)->headp, args.string, expand_buf, sizeof(expand_buf));
-		ast_build_string(&buf, &len, "%s", expand_buf);
+		ast_str_substitute_variables_varshead(&str, len, AST_LIST_FIRST(&user->extensions)->headp, args.string);
+		if (buf) {
+			ast_build_string(&buf, (size_t *) &len, "%s", ast_str_buffer(str));
+		} else {
+			ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
+		}
 		user = unref_user(user);
 	}
 
+	ast_free(str);
 	return 0;
+}
+
+static int pp_each_user_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	return pp_each_user_helper(chan, data, buf, NULL, len);
+}
+
+static int pp_each_user_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, int len)
+{
+	return pp_each_user_helper(chan, data, NULL, buf, len);
 }
 
 static struct ast_custom_function pp_each_user_function = {
@@ -1117,17 +1142,19 @@
 		"excluding ones with MAC address <exclude_mac>. Probably not useful outside of\n"
 		"res_phoneprov.\n"
 		"\nExample: ${PP_EACH_USER(<item><fn>%{DISPLAY_NAME}</fn></item>|${MAC})",
-	.read = pp_each_user_exec,
+	.read = pp_each_user_read,
+	.read2 = pp_each_user_read2,
 };
 
 /*! \brief A dialplan function that can be used to output a template for each extension attached to a user */
-static int pp_each_extension_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+static int pp_each_extension_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, int len)
 {
 	struct user *user;
 	struct extension *exten;
 	char path[PATH_MAX];
 	char *file;
 	int filelen;
+	struct ast_str *str;
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(mac);
 		AST_APP_ARG(template);
@@ -1159,17 +1186,35 @@
 		return 0;
 	}
 
+	if (!(str = ast_str_create(filelen))) {
+		return 0;
+	}
+
 	AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
-		char expand_buf[VAR_BUF_SIZE] = {0,};
-		pbx_substitute_variables_varshead(exten->headp, file, expand_buf, sizeof(expand_buf));
-		ast_build_string(&buf, &len, "%s", expand_buf);
+		ast_str_substitute_variables_varshead(&str, 0, exten->headp, file);
+		if (buf) {
+			ast_build_string(&buf, (size_t *) &len, "%s", ast_str_buffer(str));
+		} else {
+			ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
+		}
 	}
 
 	ast_free(file);
+	ast_free(str);
 
 	user = unref_user(user);
 
 	return 0;
+}
+
+static int pp_each_extension_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	return pp_each_extension_helper(chan, cmd, data, buf, NULL, len);
+}
+
+static int pp_each_extension_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, int len)
+{
+	return pp_each_extension_helper(chan, cmd, data, NULL, buf, len);
 }
 
 static struct ast_custom_function pp_each_extension_function = {
@@ -1179,7 +1224,8 @@
 	.desc =
 		"Output the specified template for each extension associated with the specified\n"
 		"MAC address.",
-	.read = pp_each_extension_exec,
+	.read = pp_each_extension_read,
+	.read2 = pp_each_extension_read2,
 };
 
 /*! \brief CLI command to list static and dynamic routes */




More information about the asterisk-commits mailing list