Added macros from 6198 and 6203. also added better
authorMatt O'Gorman <mogorman@digium.com>
Wed, 11 Jan 2006 19:52:29 +0000 (19:52 +0000)
committerMatt O'Gorman <mogorman@digium.com>
Wed, 11 Jan 2006 19:52:29 +0000 (19:52 +0000)
error reporting in func_cut

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

funcs/func_cdr.c
funcs/func_cut.c

index c934e87..c5cadc7 100644 (file)
 #include "asterisk/app.h"
 #include "asterisk/cdr.h"
 
+enum {
+       OPT_RECURSIVE = (1 << 0),
+} cdr_option_flags;
+
+AST_APP_OPTIONS(cdr_func_options, {
+       AST_APP_OPTION('r', OPT_RECURSIVE),
+});
+
 static char *builtin_function_cdr_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
 {
        char *ret;
-       char *mydata;
-       int argc;
-       char *argv[2];
-       int recursive = 0;
+       char *parse;
+       struct ast_flags flags = {0};
+       
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(variable);
+               AST_APP_ARG(options);
+       );
 
        if (ast_strlen_zero(data))
                return NULL;
@@ -52,47 +63,54 @@ static char *builtin_function_cdr_read(struct ast_channel *chan, char *cmd, char
        if (!chan->cdr)
                return NULL;
 
-       mydata = ast_strdupa(data);
-       argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
-
-       /* check for a trailing flags argument */
-       if (argc > 1) {
-               argc--;
-               if (strchr(argv[argc], 'r'))
-                       recursive = 1;
+       parse = ast_strdupa(data);
+       if (!parse) {
+               ast_log(LOG_ERROR, "Out of memory!\n");
+               return NULL;
        }
 
-       ast_cdr_getvar(chan->cdr, argv[0], &ret, buf, len, recursive);
+       AST_STANDARD_APP_ARGS(args, parse);
+       
+       if(!ast_strlen_zero(args.options) ) {
+               ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
+       }
+       ast_cdr_getvar(chan->cdr, args.variable, &ret, buf, len, (ast_test_flag(&flags,OPT_RECURSIVE) ) ? 1 : 0 );
 
        return ret;
 }
 
 static void builtin_function_cdr_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
 {
-       char *mydata;
-       int argc;
-       char *argv[2];
-       int recursive = 0;
+       char *parse;
+       struct ast_flags flags = {0};
+
+       AST_DECLARE_APP_ARGS(args, 
+               AST_APP_ARG(variable);
+               AST_APP_ARG(options);
+       );      
 
        if (ast_strlen_zero(data) || !value)
                return;
        
-       mydata = ast_strdupa(data);
-       argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
+       parse = ast_strdupa(data);
+       if (!parse) {
+               ast_log(LOG_ERROR, "Out of memory!\n");
+               return;
+       }
+
+       AST_STANDARD_APP_ARGS(args, parse);
 
        /* check for a trailing flags argument */
-       if (argc > 1) {
-               argc--;
-               if (strchr(argv[argc], 'r'))
-                       recursive = 1;
+       if(!ast_strlen_zero(args.options) ) {
+               ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
        }
 
-       if (!strcasecmp(argv[0], "accountcode"))
+       if (!strcasecmp(args.variable, "accountcode"))
                ast_cdr_setaccount(chan, value);
-       else if (!strcasecmp(argv[0], "userfield"))
+       else if (!strcasecmp(args.variable, "userfield"))
                ast_cdr_setuserfield(chan, value);
        else if (chan->cdr)
-               ast_cdr_setvar(chan->cdr, argv[0], value, recursive);
+               ast_cdr_setvar(chan->cdr, args.variable, value, (ast_test_flag(&flags,OPT_RECURSIVE) ) ? 1 : 0 );
 }
 
 #ifndef BUILTIN_FUNC
index 1c3eaa4..1df4703 100644 (file)
@@ -133,42 +133,40 @@ static int sort_internal(struct ast_channel *chan, char *data, char *buffer, siz
 
 static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
 {
-       char *s, *args[3], *varname=NULL, *delimiter=NULL, *field=NULL;
-       int args_okay = 0;
+       char *parse;
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(varname);
+               AST_APP_ARG(delimiter);
+               AST_APP_ARG(field);
+       );
+
+       memset(buffer, 0, buflen); 
+       
+       parse = ast_strdupa(data);
+       if (!parse) {
+               ast_log(LOG_ERROR, "Out of memory!\n");
+               return ERROR_NOMEM;
+       }
 
-       memset(buffer, 0, buflen);
+       AST_STANDARD_APP_ARGS(args, parse);
 
        /* Check and parse arguments */
-       if (data) {
-               s = ast_strdupa((char *)data);
-               if (s) {
-                       ast_app_separate_args(s, '|', args, 3);
-                       varname = args[0];
-                       delimiter = args[1];
-                       field = args[2];
-
-                       if (field) {
-                               args_okay = 1;
-                       }
-               } else {
-                       return ERROR_NOMEM;
-               }
-       }
-
-       if (args_okay) {
+       if(args.argc < 3){
+               return ERROR_NOARG;
+       } else {
                char d, ds[2];
-               char *tmp = alloca(strlen(varname) + 4);
+               char *tmp = alloca(strlen(args.varname) + 4);
                char varvalue[MAXRESULT], *tmp2=varvalue;
 
                if (tmp) {
-                       snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
+                       snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname);
                        memset(varvalue, 0, sizeof(varvalue));
                } else {
                        return ERROR_NOMEM;
                }
 
-               if (delimiter[0])
-                       d = delimiter[0];
+               if (args.delimiter[0])
+                       d = args.delimiter[0];
                else
                        d = '-';
 
@@ -179,8 +177,8 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
 
                if (tmp2) {
                        int curfieldnum = 1;
-                       while ((tmp2 != NULL) && (field != NULL)) {
-                               char *nextgroup = strsep(&field, "&");
+                       while ((tmp2 != NULL) && (args.field != NULL)) {
+                               char *nextgroup = strsep(&(args.field), "&");
                                int num1 = 0, num2 = MAXRESULT;
                                char trashchar;
 
@@ -231,8 +229,6 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
                                }
                        }
                }
-       } else {
-               return ERROR_NOARG;
        }
        return 0;
 }
@@ -267,7 +263,7 @@ static char *acf_cut_exec(struct ast_channel *chan, char *cmd, char *data, char
 
        switch (cut_internal(chan, data, buf, len)) {
        case ERROR_NOARG:
-               ast_log(LOG_ERROR, "CUT() requires an argument\n");
+               ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
                break;
        case ERROR_NOMEM:
                ast_log(LOG_ERROR, "Out of memory\n");