reduce the likelihood that HTTP Manager session ids will consist of primarily '1...
[asterisk-bristuff.git] / apps / app_stack.c
blobace440a6361923cce6238b49a47a0a28c462be7a
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2004-2006 Tilghman Lesher <app_stack_v002@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.
19 /*! \file
21 * \brief Stack applications Gosub, Return, etc.
23 * \author Tilghman Lesher <app_stack_v002@the-tilghman.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
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"
45 #define STACKVAR "~GOSUB~STACK~"
48 static const char *app_gosub = "Gosub";
49 static const char *app_gosubif = "GosubIf";
50 static const char *app_return = "Return";
51 static const char *app_pop = "StackPop";
53 static const char *gosub_synopsis = "Jump to label, saving return address";
54 static const char *gosubif_synopsis = "Conditionally jump to label, saving return address";
55 static const char *return_synopsis = "Return from gosub routine";
56 static const char *pop_synopsis = "Remove one address from gosub stack";
58 static const char *gosub_descrip =
59 "Gosub([[context|]exten|]priority)\n"
60 " Jumps to the label specified, saving the return address.\n";
61 static const char *gosubif_descrip =
62 "GosubIf(condition?labeliftrue[:labeliffalse])\n"
63 " If the condition is true, then jump to labeliftrue. If false, jumps to\n"
64 "labeliffalse, if specified. In either case, a jump saves the return point\n"
65 "in the dialplan, to be returned to with a Return.\n";
66 static const char *return_descrip =
67 "Return()\n"
68 " Jumps to the last label on the stack, removing it.\n";
69 static const char *pop_descrip =
70 "StackPop()\n"
71 " Removes last label on the stack, discarding it.\n";
74 static int pop_exec(struct ast_channel *chan, void *data)
76 pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
78 return 0;
81 static int return_exec(struct ast_channel *chan, void *data)
83 const char *label = pbx_builtin_getvar_helper(chan, STACKVAR);
85 if (ast_strlen_zero(label)) {
86 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
87 return -1;
88 } else if (ast_parseable_goto(chan, label)) {
89 ast_log(LOG_WARNING, "No next statement after Gosub?\n");
90 return -1;
93 pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
94 return 0;
97 static int gosub_exec(struct ast_channel *chan, void *data)
99 char newlabel[AST_MAX_EXTENSION * 2 + 3 + 11];
100 struct ast_module_user *u;
102 if (ast_strlen_zero(data)) {
103 ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority)\n", app_gosub, app_gosub);
104 return -1;
107 u = ast_module_user_add(chan);
108 snprintf(newlabel, sizeof(newlabel), "%s|%s|%d", chan->context, chan->exten, chan->priority + 1);
110 if (ast_parseable_goto(chan, data)) {
111 ast_module_user_remove(u);
112 return -1;
115 pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel);
116 ast_module_user_remove(u);
118 return 0;
121 static int gosubif_exec(struct ast_channel *chan, void *data)
123 struct ast_module_user *u;
124 char *condition="", *label1, *label2, *args;
125 int res=0;
127 if (ast_strlen_zero(data)) {
128 ast_log(LOG_WARNING, "GosubIf requires an argument\n");
129 return 0;
132 args = ast_strdupa(data);
134 u = ast_module_user_add(chan);
136 condition = strsep(&args, "?");
137 label1 = strsep(&args, ":");
138 label2 = args;
140 if (pbx_checkcondition(condition)) {
141 if (!ast_strlen_zero(label1)) {
142 res = gosub_exec(chan, label1);
144 } else if (!ast_strlen_zero(label2)) {
145 res = gosub_exec(chan, label2);
148 ast_module_user_remove(u);
149 return res;
152 static int unload_module(void)
154 ast_unregister_application(app_return);
155 ast_unregister_application(app_pop);
156 ast_unregister_application(app_gosubif);
157 ast_unregister_application(app_gosub);
159 ast_module_user_hangup_all();
161 return 0;
164 static int load_module(void)
166 ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
167 ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
168 ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
169 ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
171 return 0;
174 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stack Routines");