2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2004-2006 Tilghman Lesher <app_stack_v003@the-tilghman.com>.
6 * This code is released by the author with no restrictions on usage.
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Stack applications Gosub, Return, etc.
23 * \author Tilghman Lesher <app_stack_v003@the-tilghman.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/options.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/chanvars.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/config.h"
44 #include "asterisk/app.h"
46 #define STACKVAR "~GOSUB~STACK~"
49 static const char *app_gosub = "Gosub";
50 static const char *app_gosubif = "GosubIf";
51 static const char *app_return = "Return";
52 static const char *app_pop = "StackPop";
54 static const char *gosub_synopsis = "Jump to label, saving return address";
55 static const char *gosubif_synopsis = "Conditionally jump to label, saving return address";
56 static const char *return_synopsis = "Return from gosub routine";
57 static const char *pop_synopsis = "Remove one address from gosub stack";
59 static const char *gosub_descrip =
60 "Gosub([[context|]exten|]priority[:arg1[|...][|argN]])\n"
61 " Jumps to the label specified, saving the return address.\n";
62 static const char *gosubif_descrip =
63 "GosubIf(condition?labeliftrue[:labeliffalse[:arg1[|...][|argN]]])\n"
64 " If the condition is true, then jump to labeliftrue. If false, jumps to\n"
65 "labeliffalse, if specified. In either case, a jump saves the return point\n"
66 "in the dialplan, to be returned to with a Return.\n";
67 static const char *return_descrip =
69 " Jumps to the last label on the stack, removing it.\n";
70 static const char *pop_descrip =
72 " Removes last label on the stack, discarding it.\n";
75 static int pop_exec(struct ast_channel *chan, void *data)
77 const char *frame = pbx_builtin_getvar_helper(chan, STACKVAR);
81 /* Pop any arguments for this stack frame off the variable stack */
83 numargs = atoi(frame);
84 for (i = 1; i <= numargs; i++) {
85 snprintf(argname, sizeof(argname), "ARG%d", i);
86 pbx_builtin_setvar_helper(chan, argname, NULL);
90 /* Remove the last frame from the Gosub stack */
91 pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
96 static int return_exec(struct ast_channel *chan, void *data)
98 const char *label = pbx_builtin_getvar_helper(chan, STACKVAR);
99 char argname[15], *retval = data;
102 if (ast_strlen_zero(label)) {
103 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
107 /* Pop any arguments for this stack frame off the variable stack */
108 numargs = atoi(label);
109 for (i = 1; i <= numargs; i++) {
110 snprintf(argname, sizeof(argname), "ARG%d", i);
111 pbx_builtin_setvar_helper(chan, argname, NULL);
114 /* If the label exists, it will always have a ':' */
115 label = strchr(label, ':') + 1;
117 if (ast_parseable_goto(chan, label)) {
118 ast_log(LOG_WARNING, "No next statement after Gosub?\n");
122 /* Remove the current frame from the Gosub stack */
123 pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
125 /* Set a return value, if any */
126 pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
130 static int gosub_exec(struct ast_channel *chan, void *data)
132 char newlabel[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 11 + 11 + 4];
133 char argname[15], *tmp = ast_strdupa(data);
135 struct ast_module_user *u;
136 AST_DECLARE_APP_ARGS(args,
140 AST_DECLARE_APP_ARGS(args2,
141 AST_APP_ARG(argval)[100];
144 if (ast_strlen_zero(data)) {
145 ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority[:arg1[|...][|argN]])\n", app_gosub, app_gosub);
149 u = ast_module_user_add(chan);
151 AST_NONSTANDARD_APP_ARGS(args, tmp, ':');
152 AST_STANDARD_APP_ARGS(args2, args.args);
154 snprintf(newlabel, sizeof(newlabel), "%d:%s|%s|%d", args2.argc, chan->context, chan->exten, chan->priority + 1);
156 if (ast_parseable_goto(chan, data)) {
157 ast_module_user_remove(u);
161 /* Now that we know for certain that we're going to a new location, set our arguments */
162 for (i = 0; i < args2.argc; i++) {
163 snprintf(argname, sizeof(argname), "ARG%d", i + 1);
164 pbx_builtin_pushvar_helper(chan, argname, args2.argval[i]);
167 pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel);
168 ast_module_user_remove(u);
173 static int gosubif_exec(struct ast_channel *chan, void *data)
175 struct ast_module_user *u;
176 char *condition="", *label1, *label2, *args;
179 if (ast_strlen_zero(data)) {
180 ast_log(LOG_WARNING, "GosubIf requires an argument\n");
184 args = ast_strdupa(data);
186 u = ast_module_user_add(chan);
188 condition = strsep(&args, "?");
189 label1 = strsep(&args, ":");
190 label2 = strsep(&args, ":");
192 if (pbx_checkcondition(condition)) {
194 int len = (args ? strlen(args) : 0) + strlen(label1) + 2;
195 char *args2 = alloca(len);
197 snprintf(args2, len, "%s%c%s", label1, args ? ':' : '\0', args ? args : "");
198 res = gosub_exec(chan, args2);
201 int len = (args ? strlen(args) : 0) + strlen(label2) + 2;
202 char *args2 = alloca(len);
204 snprintf(args2, len, "%s%c%s", label2, args ? ':' : '\0', args ? args : "");
205 res = gosub_exec(chan, args2);
208 ast_module_user_remove(u);
212 static int unload_module(void)
214 ast_unregister_application(app_return);
215 ast_unregister_application(app_pop);
216 ast_unregister_application(app_gosubif);
217 ast_unregister_application(app_gosub);
219 ast_module_user_hangup_all();
224 static int load_module(void)
226 ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
227 ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
228 ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
229 ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
234 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stack Routines");