[asterisk-commits] trunk r25234 - in /trunk:
configs/func_odbc.conf.sample funcs/func_odbc.c
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Mon May 8 04:11:34 MST 2006
Author: bweschke
Date: Sat May 6 08:36:29 2006
New Revision: 25234
URL: http://svn.digium.com/view/asterisk?rev=25234&view=rev
Log:
Fix output delimiters and add prefix parameter to func_odbc #7025 (Corydon76)
Modified:
trunk/configs/func_odbc.conf.sample
trunk/funcs/func_odbc.c
Modified: trunk/configs/func_odbc.conf.sample
URL: http://svn.digium.com/view/asterisk/trunk/configs/func_odbc.conf.sample?rev=25234&r1=25233&r2=25234&view=diff
==============================================================================
--- trunk/configs/func_odbc.conf.sample (original)
+++ trunk/configs/func_odbc.conf.sample Sat May 6 08:36:29 2006
@@ -34,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: trunk/funcs/func_odbc.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_odbc.c?rev=25234&r1=25233&r2=25234&view=diff
==============================================================================
--- trunk/funcs/func_odbc.c (original)
+++ trunk/funcs/func_odbc.c Sat May 6 08:36:29 2006
@@ -47,23 +47,21 @@
#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 dsn[30];
char sql_read[2048];
char sql_write[2048];
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);
#ifdef NEEDTRACE
static void acf_odbc_error(SQLHSTMT stmt, int res)
@@ -95,16 +93,16 @@
char *tracefile = "/tmp/odbc.trace";
#endif
- ast_mutex_lock(&query_lock);
- for (query=queries; query; query = query->next) {
- if (!strcasecmp(query->name, cmd + 5)) {
+ AST_LIST_LOCK(&queries);
+ AST_LIST_TRAVERSE(&queries, query, list) {
+ if (!strcmp(query->acf->name, cmd)) {
break;
}
}
if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
- ast_mutex_unlock(&query_lock);
+ AST_LIST_UNLOCK(&queries);
return -1;
}
@@ -112,7 +110,7 @@
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 +119,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 +169,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
@@ -242,7 +240,7 @@
struct odbc_obj *obj;
struct acf_odbc_query *query;
char *arg, sql[2048] = "", varname[15];
- int count=0, res, x;
+ int count=0, res, x, buflen = 0;
SQLHSTMT stmt;
SQLSMALLINT colcount=0;
SQLINTEGER indicator;
@@ -251,16 +249,16 @@
char *tracefile = "/tmp/odbc.trace";
#endif
- ast_mutex_lock(&query_lock);
- for (query=queries; query; query = query->next) {
- if (!strcasecmp(query->name, cmd + 5)) {
+ AST_LIST_LOCK(&queries);
+ AST_LIST_TRAVERSE(&queries, query, list) {
+ if (!strcmp(query->acf->name, cmd)) {
break;
}
}
if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
- ast_mutex_unlock(&query_lock);
+ AST_LIST_UNLOCK(&queries);
return -1;
}
@@ -268,7 +266,7 @@
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 +290,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)) {
@@ -336,7 +334,7 @@
}
for (x = 0; x < colcount; x++) {
- int buflen, coldatalen;
+ int i;
char coldata[256];
buflen = strlen(buf);
@@ -352,12 +350,24 @@
return -1;
}
- strncat(buf + buflen, coldata, len - buflen);
- coldatalen = strlen(coldata);
- strncat(buf + buflen + coldatalen, ",", 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 - 1] = ',';
}
/* Trim trailing comma */
- buf[strlen(buf) - 1] = '\0';
+ buf[buflen - 1] = '\0';
acf_out:
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
@@ -400,11 +410,9 @@
return -1;
}
- *query = calloc(1, sizeof(struct acf_odbc_query));
+ *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));
@@ -420,54 +428,80 @@
ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
}
- (*query)->acf = calloc(1, sizeof(struct ast_custom_function));
- if ((*query)->acf) {
+ (*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;
}
@@ -494,64 +528,54 @@
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;
+ struct acf_odbc_query *query = NULL;
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_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;
+
+ 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);
+ }
ast_custom_function_unregister(&escape_function);
- ast_mutex_unlock(&query_lock);
+ /* 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 +583,15 @@
{
int res = 0;
struct ast_config *cfg;
- struct acf_odbc_query *q, *prevq = NULL, *qdel = NULL;
+ struct acf_odbc_query *oldquery;
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);
}
cfg = ast_config_load(config);
@@ -579,74 +605,17 @@
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;
- }
- }
-
if (init_acf_query(cfg, catg, &query)) {
- ast_log(LOG_ERROR, "Cannot initialize query ODBC_%s\n", catg);
- free_acf_query(query);
+ ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
} 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);
- }
- } else {
- /* New */
- query->next = queries;
- queries = query;
- 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_LIST_INSERT_HEAD(&queries, query, list);
+ ast_custom_function_register(query->acf);
+ }
+ }
ast_config_destroy(cfg);
reload_out:
- ast_mutex_unlock(&query_lock);
+ AST_LIST_UNLOCK(&queries);
return res;
}
More information about the asterisk-commits
mailing list