From: Mark Spencer Date: Mon, 22 Nov 2004 23:41:34 +0000 (+0000) Subject: Merge anthm's MacroIf patch (bug #2912) X-Git-Tag: 1.2.0-beta1~1766 X-Git-Url: http://git.asterisk.org/gitweb/?p=asterisk%2Fasterisk.git;a=commitdiff_plain;h=9cd104ff6fa7947ec744168f12f6f2eec2cfe7a6;ds=sidebyside Merge anthm's MacroIf patch (bug #2912) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@4319 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/apps/app_macro.c b/apps/app_macro.c index dd65868..a48defa 100755 --- a/apps/app_macro.c +++ b/apps/app_macro.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -42,9 +43,16 @@ static char *descrip = "termination, Macro will attempt to continue at priority\n" "MACRO_OFFSET + N + 1 if such a step exists, and N + 1 otherwise.\n"; +static char *if_descrip = +" MacroIf(?label_a[|arg1][:label_b[|arg1]]):\n" +"Executes macro defined in if is true\n" +"(otherwise if provided)\n"; + static char *app = "Macro"; +static char *if_app = "MacroIf"; static char *synopsis = "Macro Implementation"; +static char *if_synopsis = "Conditional Macro Implementation"; STANDARD_LOCAL_USER; @@ -52,7 +60,7 @@ LOCAL_USER_DECL; static int macro_exec(struct ast_channel *chan, void *data) { - char tmp[256] = ""; + char *tmp; char *cur, *rest; char *macro; char fullmacro[80]; @@ -72,12 +80,14 @@ static int macro_exec(struct ast_channel *chan, void *data) char *save_macro_context; char *save_macro_priority; char *save_macro_offset; + struct localuser *u; if (!data || ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Invalid Macro incantation\n"); return 0; } - strncpy(tmp, data, sizeof(tmp) - 1); + + tmp = ast_strdupa((char *) data); rest = tmp; macro = strsep(&rest, "|"); if (!macro || ast_strlen_zero(macro)) { @@ -92,6 +102,8 @@ static int macro_exec(struct ast_channel *chan, void *data) ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro); return 0; } + + LOCAL_USER_ADD(u); /* Save old info */ oldpriority = chan->priority; strncpy(oldexten, chan->exten, sizeof(oldexten) - 1); @@ -220,17 +232,45 @@ out: pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset); if (save_macro_offset) free(save_macro_offset); + LOCAL_USER_REMOVE(u); return res; } +static int macroif_exec(struct ast_channel *chan, void *data) +{ + char *expr = NULL, *label_a = NULL, *label_b = NULL; + int res = 0; + + if((expr = ast_strdupa((char *) data))) { + if ((label_a = strchr(expr, '?'))) { + *label_a = '\0'; + label_a++; + if ((label_b = strchr(label_a, ':'))) { + *label_b = '\0'; + label_b++; + } + if (ast_true(expr)) + macro_exec(chan, label_a); + else if (label_b) + macro_exec(chan, label_b); + + } else + ast_log(LOG_WARNING, "Invalid Syntax.\n"); + } else + ast_log(LOG_ERROR, "Out of Memory!\n"); + return res; +} + int unload_module(void) { STANDARD_HANGUP_LOCALUSERS; + ast_unregister_application(if_app); return ast_unregister_application(app); } int load_module(void) { + ast_register_application(if_app, macroif_exec, if_synopsis, if_descrip); return ast_register_application(app, macro_exec, synopsis, descrip); }