[asterisk-commits] murf: branch murf/macrogosub r41533 - /team/murf/macrogosub/apps/app_stack.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Aug 30 20:00:48 MST 2006


Author: murf
Date: Wed Aug 30 22:00:47 2006
New Revision: 41533

URL: http://svn.digium.com/view/asterisk?rev=41533&view=rev
Log:
Added the gosub arguments patch.


Modified:
    team/murf/macrogosub/apps/app_stack.c

Modified: team/murf/macrogosub/apps/app_stack.c
URL: http://svn.digium.com/view/asterisk/team/murf/macrogosub/apps/app_stack.c?rev=41533&r1=41532&r2=41533&view=diff
==============================================================================
--- team/murf/macrogosub/apps/app_stack.c (original)
+++ team/murf/macrogosub/apps/app_stack.c Wed Aug 30 22:00:47 2006
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (c) 2004-2006 Tilghman Lesher <app_stack_v002 at the-tilghman.com>.
+ * Copyright (c) 2004-2006 Tilghman Lesher <app_stack_v003 at the-tilghman.com>.
  *
  * This code is released by the author with no restrictions on usage.
  *
@@ -20,7 +20,7 @@
  *
  * \brief Stack applications Gosub, Return, etc.
  *
- * \author Tilghman Lesher <app_stack_v002 at the-tilghman.com>
+ * \author Tilghman Lesher <app_stack_v003 at the-tilghman.com>
  * 
  * \ingroup applications
  */
@@ -41,6 +41,7 @@
 #include "asterisk/pbx.h"
 #include "asterisk/module.h"
 #include "asterisk/config.h"
+#include "asterisk/app.h"
 
 #define STACKVAR	"~GOSUB~STACK~"
 
@@ -56,10 +57,10 @@
 static const char *pop_synopsis = "Remove one address from gosub stack";
 
 static const char *gosub_descrip =
-"Gosub([[context|]exten|]priority)\n"
+"Gosub([[context|]exten|]priority[:arg1[|...][|argN]])\n"
 "  Jumps to the label specified, saving the return address.\n";
 static const char *gosubif_descrip =
-"GosubIf(condition?labeliftrue[:labeliffalse])\n"
+"GosubIf(condition?labeliftrue[:labeliffalse[:arg1[|...][|argN]]])\n"
 "  If the condition is true, then jump to labeliftrue.  If false, jumps to\n"
 "labeliffalse, if specified.  In either case, a jump saves the return point\n"
 "in the dialplan, to be returned to with a Return.\n";
@@ -73,6 +74,20 @@
 
 static int pop_exec(struct ast_channel *chan, void *data)
 {
+	const char *frame = pbx_builtin_getvar_helper(chan, STACKVAR);
+	int numargs = 0, i;
+	char argname[15];
+
+	/* Pop any arguments for this stack frame off the variable stack */
+	if (frame) {
+		numargs = atoi(frame);
+		for (i = 1; i <= numargs; i++) {
+			snprintf(argname, sizeof(argname), "ARG%d", i);
+			pbx_builtin_setvar_helper(chan, argname, NULL);
+		}
+	}
+
+	/* Remove the last frame from the Gosub stack */
 	pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
 
 	return 0;
@@ -81,35 +96,69 @@
 static int return_exec(struct ast_channel *chan, void *data)
 {
 	const char *label = pbx_builtin_getvar_helper(chan, STACKVAR);
+	char argname[15];
+	int numargs, i;
 
 	if (ast_strlen_zero(label)) {
 		ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
 		return -1;
-	} else if (ast_parseable_goto(chan, label)) {
+	}
+
+	/* Pop any arguments for this stack frame off the variable stack */
+	numargs = atoi(label);
+	for (i = 1; i <= numargs; i++) {
+		snprintf(argname, sizeof(argname), "ARG%d", i);
+		pbx_builtin_setvar_helper(chan, argname, NULL);
+	}
+
+	/* If the label exists, it will always have a ':' */
+	label = strchr(label, ':') + 1;
+
+	if (ast_parseable_goto(chan, label)) {
 		ast_log(LOG_WARNING, "No next statement after Gosub?\n");
 		return -1;
 	}
 
+	/* Remove the current frame from the Gosub stack */
 	pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
 	return 0;
 }
 
 static int gosub_exec(struct ast_channel *chan, void *data)
 {
-	char newlabel[AST_MAX_EXTENSION * 2 + 3 + 11];
+	char newlabel[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 11 + 11 + 4];
+	char argname[15], *tmp = ast_strdupa(data);
+	int i;
 	struct ast_module_user *u;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(label);
+		AST_APP_ARG(args);
+	);
+	AST_DECLARE_APP_ARGS(args2,
+		AST_APP_ARG(argval)[100];
+	);
 
 	if (ast_strlen_zero(data)) {
-		ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority)\n", app_gosub, app_gosub);
+		ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority[:arg1[|...][|argN]])\n", app_gosub, app_gosub);
 		return -1;
 	}
 
 	u = ast_module_user_add(chan);
-	snprintf(newlabel, sizeof(newlabel), "%s|%s|%d", chan->context, chan->exten, chan->priority + 1);
+
+	AST_NONSTANDARD_APP_ARGS(args, tmp, ':');
+	AST_STANDARD_APP_ARGS(args2, args.args);
+
+	snprintf(newlabel, sizeof(newlabel), "%d:%s|%s|%d", args2.argc, chan->context, chan->exten, chan->priority + 1);
 
 	if (ast_parseable_goto(chan, data)) {
 		ast_module_user_remove(u);
 		return -1;
+	}
+
+	/* Now that we know for certain that we're going to a new location, set our arguments */
+	for (i = 0; i < args2.argc; i++) {
+		snprintf(argname, sizeof(argname), "ARG%d", i + 1);
+		pbx_builtin_pushvar_helper(chan, argname, args2.argval[i]);
 	}
 
 	pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel);
@@ -135,14 +184,22 @@
 
 	condition = strsep(&args, "?");
 	label1 = strsep(&args, ":");
-	label2 = args;
+	label2 = strsep(&args, ":");
 
 	if (pbx_checkcondition(condition)) {
 		if (label1) {
-			res = gosub_exec(chan, label1);
+			int len = (args ? strlen(args) : 0) + strlen(label1) + 2;
+			char *args2 = alloca(len);
+
+			snprintf(args2, len, "%s%c%s", label1, args ? ':' : '\0', args ? args : "");
+			res = gosub_exec(chan, args2);
 		}
 	} else if (label2) {
-		res = gosub_exec(chan, label2);
+		int len = (args ? strlen(args) : 0) + strlen(label2) + 2;
+		char *args2 = alloca(len);
+
+		snprintf(args2, len, "%s%c%s", label2, args ? ':' : '\0', args ? args : "");
+		res = gosub_exec(chan, args2);
 	}
 
 	ast_module_user_remove(u);



More information about the asterisk-commits mailing list