00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00031
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <unistd.h>
00035 #include <string.h>
00036
00037 #include "asterisk/options.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/chanvars.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/config.h"
00044 #include "asterisk/app.h"
00045
00046 #define STACKVAR "~GOSUB~STACK~"
00047
00048
00049 static const char *app_gosub = "Gosub";
00050 static const char *app_gosubif = "GosubIf";
00051 static const char *app_return = "Return";
00052 static const char *app_pop = "StackPop";
00053
00054 static const char *gosub_synopsis = "Jump to label, saving return address";
00055 static const char *gosubif_synopsis = "Conditionally jump to label, saving return address";
00056 static const char *return_synopsis = "Return from gosub routine";
00057 static const char *pop_synopsis = "Remove one address from gosub stack";
00058
00059 static const char *gosub_descrip =
00060 "Gosub([[context|]exten|]priority[(arg1[|...][|argN])])\n"
00061 " Jumps to the label specified, saving the return address.\n";
00062 static const char *gosubif_descrip =
00063 "GosubIf(condition?labeliftrue[(arg1[|...])][:labeliffalse[(arg1[|...])]])\n"
00064 " If the condition is true, then jump to labeliftrue. If false, jumps to\n"
00065 "labeliffalse, if specified. In either case, a jump saves the return point\n"
00066 "in the dialplan, to be returned to with a Return.\n";
00067 static const char *return_descrip =
00068 "Return([return-value])\n"
00069 " Jumps to the last label on the stack, removing it. The return value, if\n"
00070 "any, is saved in the channel variable GOSUB_RETVAL.\n";
00071 static const char *pop_descrip =
00072 "StackPop()\n"
00073 " Removes last label on the stack, discarding it.\n";
00074
00075
00076 static int pop_exec(struct ast_channel *chan, void *data)
00077 {
00078 const char *frame = pbx_builtin_getvar_helper(chan, STACKVAR);
00079 int numargs = 0, i;
00080 char argname[15];
00081
00082
00083 if (frame) {
00084 numargs = atoi(frame);
00085 for (i = 1; i <= numargs; i++) {
00086 snprintf(argname, sizeof(argname), "ARG%d", i);
00087 pbx_builtin_setvar_helper(chan, argname, NULL);
00088 }
00089 }
00090
00091
00092 pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
00093
00094 return 0;
00095 }
00096
00097 static int return_exec(struct ast_channel *chan, void *data)
00098 {
00099 const char *label = pbx_builtin_getvar_helper(chan, STACKVAR);
00100 char argname[15], *retval = data;
00101 int numargs, i;
00102
00103 if (ast_strlen_zero(label)) {
00104 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
00105 return -1;
00106 }
00107
00108
00109 numargs = atoi(label);
00110 for (i = 1; i <= numargs; i++) {
00111 snprintf(argname, sizeof(argname), "ARG%d", i);
00112 pbx_builtin_setvar_helper(chan, argname, NULL);
00113 }
00114
00115
00116 label = strchr(label, ':') + 1;
00117
00118 if (ast_parseable_goto(chan, label)) {
00119 ast_log(LOG_WARNING, "No next statement after Gosub?\n");
00120 return -1;
00121 }
00122
00123
00124 pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
00125
00126
00127 pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
00128 return 0;
00129 }
00130
00131 static int gosub_exec(struct ast_channel *chan, void *data)
00132 {
00133 char newlabel[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 11 + 11 + 4];
00134 char argname[15], *tmp = ast_strdupa(data), *label, *endparen;
00135 int i;
00136 struct ast_module_user *u;
00137 AST_DECLARE_APP_ARGS(args2,
00138 AST_APP_ARG(argval)[100];
00139 );
00140
00141 if (ast_strlen_zero(data)) {
00142 ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority[(arg1[|...][|argN])])\n", app_gosub, app_gosub);
00143 return -1;
00144 }
00145
00146 u = ast_module_user_add(chan);
00147
00148
00149
00150 label = strsep(&tmp, "(");
00151 if (tmp) {
00152 endparen = strrchr(tmp, ')');
00153 if (endparen)
00154 *endparen = '\0';
00155 else
00156 ast_log(LOG_WARNING, "Ouch. No closing paren: '%s'?\n", (char *)data);
00157 AST_STANDARD_APP_ARGS(args2, tmp);
00158 } else
00159 args2.argc = 0;
00160
00161
00162 snprintf(newlabel, sizeof(newlabel), "%d:%s|%s|%d", args2.argc, chan->context, chan->exten, chan->priority + 1);
00163
00164 if (ast_parseable_goto(chan, label)) {
00165 ast_log(LOG_ERROR, "Gosub address is invalid: '%s'\n", (char *)data);
00166 ast_module_user_remove(u);
00167 return -1;
00168 }
00169
00170
00171 for (i = 0; i < args2.argc; i++) {
00172 snprintf(argname, sizeof(argname), "ARG%d", i + 1);
00173 pbx_builtin_pushvar_helper(chan, argname, args2.argval[i]);
00174 ast_log(LOG_DEBUG, "Setting '%s' to '%s'\n", argname, args2.argval[i]);
00175 }
00176
00177
00178 pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel);
00179 ast_log(LOG_DEBUG, "Setting gosub return address to '%s'\n", newlabel);
00180 ast_module_user_remove(u);
00181
00182 return 0;
00183 }
00184
00185 static int gosubif_exec(struct ast_channel *chan, void *data)
00186 {
00187 struct ast_module_user *u;
00188 char *args;
00189 int res=0;
00190 AST_DECLARE_APP_ARGS(cond,
00191 AST_APP_ARG(ition);
00192 AST_APP_ARG(labels);
00193 );
00194 AST_DECLARE_APP_ARGS(label,
00195 AST_APP_ARG(iftrue);
00196 AST_APP_ARG(iffalse);
00197 );
00198
00199 if (ast_strlen_zero(data)) {
00200 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
00201 return 0;
00202 }
00203
00204 u = ast_module_user_add(chan);
00205
00206 args = ast_strdupa(data);
00207 AST_NONSTANDARD_APP_ARGS(cond, args, '?');
00208 if (cond.argc != 2) {
00209 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
00210 ast_module_user_remove(u);
00211 return 0;
00212 }
00213
00214 AST_NONSTANDARD_APP_ARGS(label, cond.labels, ':');
00215
00216 if (pbx_checkcondition(cond.ition)) {
00217 if (!ast_strlen_zero(label.iftrue))
00218 res = gosub_exec(chan, label.iftrue);
00219 } else if (!ast_strlen_zero(label.iffalse)) {
00220 res = gosub_exec(chan, label.iffalse);
00221 }
00222
00223 ast_module_user_remove(u);
00224 return res;
00225 }
00226
00227 static int unload_module(void)
00228 {
00229 ast_unregister_application(app_return);
00230 ast_unregister_application(app_pop);
00231 ast_unregister_application(app_gosubif);
00232 ast_unregister_application(app_gosub);
00233
00234 ast_module_user_hangup_all();
00235
00236 return 0;
00237 }
00238
00239 static int load_module(void)
00240 {
00241 ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
00242 ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
00243 ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
00244 ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
00245
00246 return 0;
00247 }
00248
00249 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stack Routines");