code clean up and macro implementation from
authorMatt O'Gorman <mogorman@digium.com>
Mon, 23 Jan 2006 18:07:12 +0000 (18:07 +0000)
committerMatt O'Gorman <mogorman@digium.com>
Mon, 23 Jan 2006 18:07:12 +0000 (18:07 +0000)
bug 6247

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8505 65c4cc65-6c06-0410-ace0-fbb531ad65f3

funcs/func_db.c
funcs/func_rand.c
manager.c

index 1837c8b..3125d8e 100644 (file)
 
 static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
 {
-       int argc;       
-       char *args;
-       char *argv[2];
-       char *family;
-       char *key;
+       char *parse;    
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(family);
+               AST_APP_ARG(key);
+       );
 
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
+               buf[0] = '\0';
                return buf;
        }
 
-       args = ast_strdupa(data);
-       argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
+       parse = ast_strdupa(data);
+       if (!parse) {
+               ast_log(LOG_ERROR, "Out of memory!\n");
+               buf[0] = '\0';
+               return buf;
+       }
+               
+       AST_NONSTANDARD_APP_ARGS(args, parse, '/');
        
-       if (argc > 1) {
-               family = argv[0];
-               key = argv[1];
-       } else {
+       if (args.argc < 2) {
                ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
+               buf[0] = '\0';
                return buf;
        }
 
-       if (ast_db_get(family, key, buf, len-1)) {
-               ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", family, key);
+       if (ast_db_get(args.family, args.key, buf, len-1)) {
+               ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", args.family, args.key);
        } else
                pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
 
@@ -77,29 +82,31 @@ static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, c
 
 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
 {
-       int argc;       
-       char *args;
-       char *argv[2];
-       char *family;
-       char *key;
+       char *parse;    
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(family);
+               AST_APP_ARG(key);
+       );
 
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
                return;
        }
 
-       args = ast_strdupa(data);
-       argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
-       
-       if (argc > 1) {
-               family = argv[0];
-               key = argv[1];
-       } else {
+       parse = ast_strdupa(data);
+       if (!parse) { 
+               ast_log(LOG_ERROR, "Out of memory!\n"); 
+               return;
+       }
+
+       AST_NONSTANDARD_APP_ARGS(args, parse, '/');
+
+       if (args.argc < 2) {
                ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
                return;
        }
 
-       if (ast_db_put(family, key, (char*)value)) {
+       if (ast_db_put(args.family, args.key, (char*)value)) {
                ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
        }
 }
@@ -123,29 +130,34 @@ struct ast_custom_function db_function = {
 
 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
 {
-       int argc;       
-       char *args;
-       char *argv[2];
-       char *family;
-       char *key;
+       char *parse;    
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(family);
+               AST_APP_ARG(key);
+       );
 
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
+               buf[0] = '\0';
                return buf;
        }
 
-       args = ast_strdupa(data);
-       argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
+       parse = ast_strdupa(data);
+       if (!parse) {
+               ast_log(LOG_ERROR, "Out of memory!\n");
+               buf[0] = '\0';
+               return buf;
+       }
+               
+       AST_NONSTANDARD_APP_ARGS(args, parse, '/');
        
-       if (argc > 1) {
-               family = argv[0];
-               key = argv[1];
-       } else {
+       if (args.argc < 2) {
                ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
+               buf[0] = '\0';
                return buf;
        }
 
-       if (ast_db_get(family, key, buf, len-1))
+       if (ast_db_get(args.family, args.key, buf, len-1))
                ast_copy_string(buf, "0", len); 
        else {
                pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
index 0f1bd00..4b7f7b6 100644 (file)
@@ -50,24 +50,29 @@ LOCAL_USER_DECL;
 static char *acf_rand_exec(struct ast_channel *chan, char *cmd, char *data, char *buffer, size_t buflen)
 {
        struct localuser *u;
-       char *args[2] = { "", "" }, *s;
        int min_int, response_int, max_int;
-
-       LOCAL_USER_ACF_ADD(u);
-
-       if (!(s = ast_strdupa(data))) {
+       char *parse;
+
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(min);
+               AST_APP_ARG(max);
+       );
+       
+       if (!(parse = ast_strdupa(data))) {
                *buffer = '\0';
-               LOCAL_USER_REMOVE(u);
                return buffer;
        }
 
-       ast_app_separate_args(s, '|', args, sizeof(args) / sizeof(args[0]));
+       LOCAL_USER_ACF_ADD(u);
 
-       if (ast_strlen_zero(args[0]) || sscanf(args[0], "%d", &min_int) != 1) {
+       AST_STANDARD_APP_ARGS(args, parse);
+       
+       if (ast_strlen_zero(args.min) || sscanf(args.min, "%d", &min_int) != 1) {
                min_int = 0;
        }
 
-       if (ast_strlen_zero(args[1]) || sscanf(args[1], "%d", &max_int) != 1) {
+
+       if (ast_strlen_zero(args.max) || sscanf(args.max, "%d", &max_int) != 1) {
                max_int = RAND_MAX;
        }
 
index ce4d262..91537c1 100644 (file)
--- a/manager.c
+++ b/manager.c
@@ -320,23 +320,27 @@ struct ast_variable *astman_get_variables(struct message *m)
        int varlen, x, y;
        struct ast_variable *head = NULL, *cur;
        char *var, *val;
-       unsigned int var_count;
-        char *vars[32];
-       
+
+       char *parse;    
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(vars)[32];
+       );
+
        varlen = strlen("Variable: ");  
 
        for (x = 0; x < m->hdrcount; x++) {
                if (strncasecmp("Variable: ", m->headers[x], varlen))
                        continue;
 
-               if (!(var = ast_strdupa(m->headers[x] + varlen)))
+               if (!(parse = ast_strdupa(m->headers[x] + varlen)))
                        return head;
 
-               if ((var_count = ast_app_separate_args(var, '|', vars, sizeof(vars) / sizeof(vars[0])))) {
-                       for (y = 0; y < var_count; y++) {
-                               if (!vars[y])
+               AST_STANDARD_APP_ARGS(args, parse);
+               if (args.argc) {
+                       for (y = 0; y < args.argc; y++) {
+                               if (!args.vars[y])
                                        continue;
-                               var = val = ast_strdupa(vars[y]);
+                               var = val = ast_strdupa(args.vars[y]);
                                strsep(&val, "=");
                                if (!val || ast_strlen_zero(var))
                                        continue;