[Asterisk-cvs] asterisk/apps app_macro.c,1.16,1.17

markster at lists.digium.com markster at lists.digium.com
Mon Nov 22 18:41:51 CST 2004


Update of /usr/cvsroot/asterisk/apps
In directory mongoose.digium.com:/tmp/cvs-serv5139/apps

Modified Files:
	app_macro.c 
Log Message:
Merge anthm's MacroIf patch (bug #2912)


Index: app_macro.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_macro.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- app_macro.c	2 Oct 2004 00:58:31 -0000	1.16
+++ app_macro.c	22 Nov 2004 23:41:34 -0000	1.17
@@ -18,6 +18,7 @@
 #include <asterisk/pbx.h>
 #include <asterisk/module.h>
 #include <asterisk/options.h>
+#include <asterisk/config.h>
 #include <asterisk/utils.h>
 #include <asterisk/lock.h>
 #include <stdlib.h>
@@ -42,9 +43,16 @@
 "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(<expr>?label_a[|arg1][:label_b[|arg1]]):\n"
+"Executes macro defined in <label_a> if <expr> is true\n"
+"(otherwise <label_b> 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 @@
 
 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 @@
   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 @@
 	  	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 @@
 
   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);
 }
 




More information about the svn-commits mailing list