more removal of redundant headers
[asterisk/asterisk.git] / apps / app_stack.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2004-2006 Tilghman Lesher <app_stack_v003@the-tilghman.com>.
5  *
6  * This code is released by the author with no restrictions on usage.
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Stack applications Gosub, Return, etc.
22  *
23  * \author Tilghman Lesher <app_stack_v003@the-tilghman.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29  
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/pbx.h"
33 #include "asterisk/module.h"
34 #include "asterisk/app.h"
35 #include "asterisk/manager.h"
36
37 static const char *app_gosub = "Gosub";
38 static const char *app_gosubif = "GosubIf";
39 static const char *app_return = "Return";
40 static const char *app_pop = "StackPop";
41
42 static const char *gosub_synopsis = "Jump to label, saving return address";
43 static const char *gosubif_synopsis = "Conditionally jump to label, saving return address";
44 static const char *return_synopsis = "Return from gosub routine";
45 static const char *pop_synopsis = "Remove one address from gosub stack";
46
47 static const char *gosub_descrip =
48 "  Gosub([[context,]exten,]priority[(arg1[,...][,argN])]):\n"
49 "Jumps to the label specified, saving the return address.\n";
50 static const char *gosubif_descrip =
51 "  GosubIf(condition?labeliftrue[(arg1[,...])][:labeliffalse[(arg1[,...])]]):\n"
52 "If the condition is true, then jump to labeliftrue.  If false, jumps to\n"
53 "labeliffalse, if specified.  In either case, a jump saves the return point\n"
54 "in the dialplan, to be returned to with a Return.\n";
55 static const char *return_descrip =
56 "  Return([return-value]):\n"
57 "Jumps to the last label on the stack, removing it.  The return value, if\n"
58 "any, is saved in the channel variable GOSUB_RETVAL.\n";
59 static const char *pop_descrip =
60 "  StackPop():\n"
61 "Removes last label on the stack, discarding it.\n";
62
63
64 static void gosub_free(void *data);
65
66 static struct ast_datastore_info stack_info = {
67         .type = "GOSUB",
68         .destroy = gosub_free,
69 };
70
71 struct gosub_stack_frame {
72         AST_LIST_ENTRY(gosub_stack_frame) entries;
73         /* 100 arguments is all that we support anyway, but this will handle up to 255 */
74         unsigned char arguments;
75         struct varshead varshead;
76         int priority;
77         char *context;
78         char extension[0];
79 };
80
81 static int frame_set_var(struct ast_channel *chan, struct gosub_stack_frame *frame, const char *var, const char *value)
82 {
83         struct ast_var_t *variables;
84         int found = 0;
85
86         /* Does this variable already exist? */
87         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
88                 if (!strcmp(var, ast_var_name(variables))) {
89                         found = 1;
90                         break;
91                 }
92         }
93
94         if (!ast_strlen_zero(value)) {
95                 if (!found) {
96                         variables = ast_var_assign(var, "");
97                         AST_LIST_INSERT_HEAD(&frame->varshead, variables, entries);
98                         pbx_builtin_pushvar_helper(chan, var, value);
99                 } else
100                         pbx_builtin_setvar_helper(chan, var, value);
101
102                 manager_event(EVENT_FLAG_CALL, "VarSet", 
103                         "Channel: %s\r\n"
104                         "Variable: LOCAL(%s)\r\n"
105                         "Value: %s\r\n"
106                         "Uniqueid: %s\r\n", 
107                         chan->name, var, value, chan->uniqueid);
108         }
109         return 0;
110 }
111
112 static void gosub_release_frame(struct ast_channel *chan, struct gosub_stack_frame *frame)
113 {
114         unsigned char i;
115         char argname[15];
116         struct ast_var_t *vardata;
117
118         /* If chan is not defined, then we're calling it as part of gosub_free,
119          * and the channel variables will be deallocated anyway.  Otherwise, we're
120          * just releasing a single frame, so we need to clean up the arguments for
121          * that frame, so that we re-expose the variables from the previous frame
122          * that were hidden by this one.
123          */
124         if (chan) {
125                 for (i = 1; i <= frame->arguments && i != 0; i++) {
126                         snprintf(argname, sizeof(argname), "ARG%hhd", i);
127                         pbx_builtin_setvar_helper(chan, argname, NULL);
128                 }
129         }
130
131         /* Delete local variables */
132         while ((vardata = AST_LIST_REMOVE_HEAD(&frame->varshead, entries))) {
133                 if (chan)
134                         pbx_builtin_setvar_helper(chan, ast_var_name(vardata), NULL);   
135                 ast_var_delete(vardata);
136         }
137
138         ast_free(frame);
139 }
140
141 static struct gosub_stack_frame *gosub_allocate_frame(const char *context, const char *extension, int priority, unsigned char arguments)
142 {
143         struct gosub_stack_frame *new = NULL;
144         int len_extension = strlen(extension), len_context = strlen(context);
145
146         if ((new = ast_calloc(1, sizeof(*new) + 2 + len_extension + len_context))) {
147                 AST_LIST_HEAD_INIT_NOLOCK(&new->varshead);
148                 strcpy(new->extension, extension);
149                 new->context = new->extension + len_extension + 1;
150                 strcpy(new->context, context);
151                 new->priority = priority;
152                 new->arguments = arguments;
153         }
154         return new;
155 }
156
157 static void gosub_free(void *data)
158 {
159         AST_LIST_HEAD(, gosub_stack_frame) *oldlist = data;
160         struct gosub_stack_frame *oldframe;
161         AST_LIST_LOCK(oldlist);
162         while ((oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
163                 gosub_release_frame(NULL, oldframe);
164         }
165         AST_LIST_UNLOCK(oldlist);
166         AST_LIST_HEAD_DESTROY(oldlist);
167         ast_free(oldlist);
168 }
169
170 static int pop_exec(struct ast_channel *chan, void *data)
171 {
172         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
173         struct gosub_stack_frame *oldframe;
174         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
175
176         if (!stack_store) {
177                 ast_log(LOG_WARNING, "%s called with no gosub stack allocated.\n", app_pop);
178                 return 0;
179         }
180
181         oldlist = stack_store->data;
182         AST_LIST_LOCK(oldlist);
183         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
184         AST_LIST_UNLOCK(oldlist);
185
186         if (oldframe) {
187                 gosub_release_frame(chan, oldframe);
188         } else {
189                 ast_debug(1, "%s called with an empty gosub stack\n", app_pop);
190         }
191         return 0;
192 }
193
194 static int return_exec(struct ast_channel *chan, void *data)
195 {
196         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
197         struct gosub_stack_frame *oldframe;
198         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
199         char *retval = data;
200
201         if (!stack_store) {
202                 ast_log(LOG_ERROR, "Return without Gosub: stack is unallocated\n");
203                 return -1;
204         }
205
206         oldlist = stack_store->data;
207         AST_LIST_LOCK(oldlist);
208         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
209         AST_LIST_UNLOCK(oldlist);
210
211         if (!oldframe) {
212                 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
213                 return -1;
214         }
215
216         ast_explicit_goto(chan, oldframe->context, oldframe->extension, oldframe->priority);
217         gosub_release_frame(chan, oldframe);
218
219         /* Set a return value, if any */
220         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
221         return 0;
222 }
223
224 static int gosub_exec(struct ast_channel *chan, void *data)
225 {
226         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
227         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
228         struct gosub_stack_frame *newframe;
229         char argname[15], *tmp = ast_strdupa(data), *label, *endparen;
230         int i;
231         AST_DECLARE_APP_ARGS(args2,
232                 AST_APP_ARG(argval)[100];
233         );
234
235         if (ast_strlen_zero(data)) {
236                 ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority[(arg1[|...][|argN])])\n", app_gosub, app_gosub);
237                 return -1;
238         }
239
240         if (!stack_store) {
241                 ast_debug(1, "Channel %s has no datastore, so we're allocating one.\n", chan->name);
242                 stack_store = ast_channel_datastore_alloc(&stack_info, NULL);
243                 if (!stack_store) {
244                         ast_log(LOG_ERROR, "Unable to allocate new datastore.  Gosub will fail.\n");
245                         return -1;
246                 }
247
248                 oldlist = ast_calloc(1, sizeof(*oldlist));
249                 if (!oldlist) {
250                         ast_log(LOG_ERROR, "Unable to allocate datastore list head.  Gosub will fail.\n");
251                         ast_channel_datastore_free(stack_store);
252                         return -1;
253                 }
254
255                 stack_store->data = oldlist;
256                 AST_LIST_HEAD_INIT(oldlist);
257                 ast_channel_datastore_add(chan, stack_store);
258         }
259
260         /* Separate the arguments from the label */
261         /* NOTE:  you cannot use ast_app_separate_args for this, because '(' cannot be used as a delimiter. */
262         label = strsep(&tmp, "(");
263         if (tmp) {
264                 endparen = strrchr(tmp, ')');
265                 if (endparen)
266                         *endparen = '\0';
267                 else
268                         ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", (char *)data);
269                 AST_STANDARD_APP_ARGS(args2, tmp);
270         } else
271                 args2.argc = 0;
272
273         /* Create the return address, but don't save it until we know that the Gosub destination exists */
274         newframe = gosub_allocate_frame(chan->context, chan->exten, chan->priority + 1, args2.argc);
275
276         if (!newframe)
277                 return -1;
278
279         if (ast_parseable_goto(chan, label)) {
280                 ast_log(LOG_ERROR, "Gosub address is invalid: '%s'\n", (char *)data);
281                 ast_free(newframe);
282                 return -1;
283         }
284
285         /* Now that we know for certain that we're going to a new location, set our arguments */
286         for (i = 0; i < args2.argc; i++) {
287                 snprintf(argname, sizeof(argname), "ARG%d", i + 1);
288                 frame_set_var(chan, newframe, argname, args2.argval[i]);
289                 ast_debug(1, "Setting '%s' to '%s'\n", argname, args2.argval[i]);
290         }
291
292         /* And finally, save our return address */
293         oldlist = stack_store->data;
294         AST_LIST_LOCK(oldlist);
295         AST_LIST_INSERT_HEAD(oldlist, newframe, entries);
296         AST_LIST_UNLOCK(oldlist);
297
298         return 0;
299 }
300
301 static int gosubif_exec(struct ast_channel *chan, void *data)
302 {
303         char *args;
304         int res=0;
305         AST_DECLARE_APP_ARGS(cond,
306                 AST_APP_ARG(ition);
307                 AST_APP_ARG(labels);
308         );
309         AST_DECLARE_APP_ARGS(label,
310                 AST_APP_ARG(iftrue);
311                 AST_APP_ARG(iffalse);
312         );
313
314         if (ast_strlen_zero(data)) {
315                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
316                 return 0;
317         }
318
319         args = ast_strdupa(data);
320         AST_NONSTANDARD_APP_ARGS(cond, args, '?');
321         if (cond.argc != 2) {
322                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
323                 return 0;
324         }
325
326         AST_NONSTANDARD_APP_ARGS(label, cond.labels, ':');
327
328         if (pbx_checkcondition(cond.ition)) {
329                 if (!ast_strlen_zero(label.iftrue))
330                         res = gosub_exec(chan, label.iftrue);
331         } else if (!ast_strlen_zero(label.iffalse)) {
332                 res = gosub_exec(chan, label.iffalse);
333         }
334
335         return res;
336 }
337
338 static int local_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
339 {
340         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
341         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
342         struct gosub_stack_frame *frame;
343         struct ast_var_t *variables;
344
345         if (!stack_store)
346                 return -1;
347
348         oldlist = stack_store->data;
349         AST_LIST_LOCK(oldlist);
350         frame = AST_LIST_FIRST(oldlist);
351         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
352                 if (!strcmp(data, ast_var_name(variables))) {
353                         const char *tmp = pbx_builtin_getvar_helper(chan, data);
354                         ast_copy_string(buf, S_OR(tmp, ""), len);
355                         break;
356                 }
357         }
358         AST_LIST_UNLOCK(oldlist);
359         return 0;
360 }
361
362 static int local_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
363 {
364         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
365         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
366         struct gosub_stack_frame *frame;
367
368         if (!stack_store) {
369                 ast_log(LOG_ERROR, "Tried to set LOCAL(%s), but we aren't within a Gosub routine\n", var);
370                 return -1;
371         }
372
373         oldlist = stack_store->data;
374         AST_LIST_LOCK(oldlist);
375         frame = AST_LIST_FIRST(oldlist);
376
377         if (frame)
378                 frame_set_var(chan, frame, var, value);
379
380         AST_LIST_UNLOCK(oldlist);
381
382         return 0;
383 }
384
385 static struct ast_custom_function local_function = {
386         .name = "LOCAL",
387         .synopsis = "Variables local to the gosub stack frame",
388         .syntax = "LOCAL(<varname>)",
389         .write = local_write,
390         .read = local_read,
391 };
392
393 static int unload_module(void)
394 {
395         ast_unregister_application(app_return);
396         ast_unregister_application(app_pop);
397         ast_unregister_application(app_gosubif);
398         ast_unregister_application(app_gosub);
399         ast_custom_function_unregister(&local_function);
400
401         return 0;
402 }
403
404 static int load_module(void)
405 {
406         ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
407         ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
408         ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
409         ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
410         ast_custom_function_register(&local_function);
411
412         return 0;
413 }
414
415 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialplan subroutines (Gosub, Return, etc)");