app_stack: protect against missing parameters to STACK_PEEK and LOCAL_PEEK
[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 /*** MODULEINFO
29         <use type="module">res_agi</use>
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34  
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/app.h"
40 #include "asterisk/manager.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/agi.h"
43 #include "asterisk/stasis_channels.h"
44
45 /*** DOCUMENTATION
46         <application name="Gosub" language="en_US">
47                 <synopsis>
48                         Jump to label, saving return address.
49                 </synopsis>
50                 <syntax>
51                         <parameter name="context" />
52                         <parameter name="exten" />
53                         <parameter name="priority" required="true" hasparams="optional">
54                                 <argument name="arg1" multiple="true" required="true" />
55                                 <argument name="argN" />
56                         </parameter>
57                 </syntax>
58                 <description>
59                         <para>Jumps to the label specified, saving the return address.</para>
60                 </description>
61                 <see-also>
62                         <ref type="application">GosubIf</ref>
63                         <ref type="application">Macro</ref>
64                         <ref type="application">Goto</ref>
65                         <ref type="application">Return</ref>
66                         <ref type="application">StackPop</ref>
67                 </see-also>
68         </application>
69         <application name="GosubIf" language="en_US">
70                 <synopsis>
71                         Conditionally jump to label, saving return address.
72                 </synopsis>
73                 <syntax argsep="?">
74                         <parameter name="condition" required="true" />
75                         <parameter name="destination" required="true" argsep=":">
76                                 <argument name="labeliftrue" hasparams="optional">
77                                         <para>Continue at <replaceable>labeliftrue</replaceable> if the condition is true.
78                                         Takes the form similar to Goto() of [[context,]extension,]priority.</para>
79                                         <argument name="arg1" required="true" multiple="true" />
80                                         <argument name="argN" />
81                                 </argument>
82                                 <argument name="labeliffalse" hasparams="optional">
83                                         <para>Continue at <replaceable>labeliffalse</replaceable> if the condition is false.
84                                         Takes the form similar to Goto() of [[context,]extension,]priority.</para>
85                                         <argument name="arg1" required="true" multiple="true" />
86                                         <argument name="argN" />
87                                 </argument>
88                         </parameter>
89                 </syntax>
90                 <description>
91                         <para>If the condition is true, then jump to labeliftrue.  If false, jumps to
92                         labeliffalse, if specified.  In either case, a jump saves the return point
93                         in the dialplan, to be returned to with a Return.</para>
94                 </description>
95                 <see-also>
96                         <ref type="application">Gosub</ref>
97                         <ref type="application">Return</ref>
98                         <ref type="application">MacroIf</ref>
99                         <ref type="function">IF</ref>
100                         <ref type="application">GotoIf</ref>
101                         <ref type="application">Goto</ref>
102                 </see-also>
103         </application>
104         <application name="Return" language="en_US">
105                 <synopsis>
106                         Return from gosub routine.
107                 </synopsis>
108                 <syntax>
109                         <parameter name="value">
110                                 <para>Return value.</para>
111                         </parameter>
112                 </syntax>
113                 <description>
114                         <para>Jumps to the last label on the stack, removing it. The return <replaceable>value</replaceable>, if
115                         any, is saved in the channel variable <variable>GOSUB_RETVAL</variable>.</para>
116                 </description>
117                 <see-also>
118                         <ref type="application">Gosub</ref>
119                         <ref type="application">StackPop</ref>
120                 </see-also>
121         </application>
122         <application name="StackPop" language="en_US">
123                 <synopsis>
124                         Remove one address from gosub stack.
125                 </synopsis>
126                 <syntax />
127                 <description>
128                         <para>Removes last label on the stack, discarding it.</para>
129                 </description>
130                 <see-also>
131                         <ref type="application">Return</ref>
132                         <ref type="application">Gosub</ref>
133                 </see-also>
134         </application>
135         <function name="LOCAL" language="en_US">
136                 <synopsis>
137                         Manage variables local to the gosub stack frame.
138                 </synopsis>
139                 <syntax>
140                         <parameter name="varname" required="true" />
141                 </syntax>
142                 <description>
143                         <para>Read and write a variable local to the gosub stack frame, once we Return() it will be lost
144                         (or it will go back to whatever value it had before the Gosub()).</para>
145                 </description>
146                 <see-also>
147                         <ref type="application">Gosub</ref>
148                         <ref type="application">GosubIf</ref>
149                         <ref type="application">Return</ref>
150                 </see-also>
151         </function>
152         <function name="LOCAL_PEEK" language="en_US">
153                 <synopsis>
154                         Retrieve variables hidden by the local gosub stack frame.
155                 </synopsis>
156                 <syntax>
157                         <parameter name="n" required="true" />
158                         <parameter name="varname" required="true" />
159                 </syntax>
160                 <description>
161                         <para>Read a variable <replaceable>varname</replaceable> hidden by
162                         <replaceable>n</replaceable> levels of gosub stack frames.  Note that ${LOCAL_PEEK(0,foo)}
163                         is the same as <variable>foo</variable>, since the value of <replaceable>n</replaceable>
164                         peeks under 0 levels of stack frames; in other words, 0 is the current level.  If
165                         <replaceable>n</replaceable> exceeds the available number of stack frames, then an empty
166                         string is returned.</para>
167                 </description>
168                 <see-also>
169                         <ref type="application">Gosub</ref>
170                         <ref type="application">GosubIf</ref>
171                         <ref type="application">Return</ref>
172                 </see-also>
173         </function>
174         <function name="STACK_PEEK" language="en_US">
175                 <synopsis>
176                         View info about the location which called Gosub
177                 </synopsis>
178                 <syntax>
179                         <parameter name="n" required="true" />
180                         <parameter name="which" required="true" />
181                         <parameter name="suppress" required="false" />
182                 </syntax>
183                 <description>
184                         <para>Read the calling <literal>c</literal>ontext, <literal>e</literal>xtension,
185                         <literal>p</literal>riority, or <literal>l</literal>abel, as specified by
186                         <replaceable>which</replaceable>, by going up <replaceable>n</replaceable> frames
187                         in the Gosub stack.  If <replaceable>suppress</replaceable> is true, then if the
188                         number of available stack frames is exceeded, then no error message will be
189                         printed.</para>
190                 </description>
191         </function>
192         <agi name="gosub" language="en_US">
193                 <synopsis>
194                         Cause the channel to execute the specified dialplan subroutine.
195                 </synopsis>
196                 <syntax>
197                         <parameter name="context" required="true" />
198                         <parameter name="extension" required="true" />
199                         <parameter name="priority" required="true" />
200                         <parameter name="optional-argument" />
201                 </syntax>
202                 <description>
203                         <para>Cause the channel to execute the specified dialplan subroutine,
204                         returning to the dialplan with execution of a Return().</para>
205                 </description>
206                 <see-also>
207                         <ref type="application">GoSub</ref>
208                 </see-also>
209         </agi>
210         <managerEvent language="en_US" name="VarSet">
211                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
212                         <synopsis>Raised when a variable local to the gosub stack frame is set due to a subroutine call.</synopsis>
213                         <syntax>
214                                 <channel_snapshot/>
215                                 <parameter name="Variable">
216                                         <para>The LOCAL variable being set.</para>
217                                         <note><para>The variable name will always be enclosed with
218                                         <literal>LOCAL()</literal></para></note>
219                                 </parameter>
220                                 <parameter name="Value">
221                                         <para>The new value of the variable.</para>
222                                 </parameter>
223                         </syntax>
224                         <see-also>
225                                 <ref type="application">GoSub</ref>
226                                 <ref type="agi">gosub</ref>
227                                 <ref type="function">LOCAL</ref>
228                                 <ref type="function">LOCAL_PEEK</ref>
229                         </see-also>
230                 </managerEventInstance>
231         </managerEvent>
232  ***/
233
234 static const char app_gosub[] = "Gosub";
235 static const char app_gosubif[] = "GosubIf";
236 static const char app_return[] = "Return";
237 static const char app_pop[] = "StackPop";
238
239 static void gosub_free(void *data);
240
241 static const struct ast_datastore_info stack_info = {
242         .type = "GOSUB",
243         .destroy = gosub_free,
244 };
245
246 struct gosub_stack_frame {
247         AST_LIST_ENTRY(gosub_stack_frame) entries;
248         /* 100 arguments is all that we support anyway, but this will handle up to 255 */
249         unsigned char arguments;
250         struct varshead varshead;
251         int priority;
252         /*! TRUE if the return location marks the end of a special routine. */
253         unsigned int is_special:1;
254         char *context;
255         char extension[0];
256 };
257
258 AST_LIST_HEAD(gosub_stack_list, gosub_stack_frame);
259
260 static int frame_set_var(struct ast_channel *chan, struct gosub_stack_frame *frame, const char *var, const char *value)
261 {
262         struct ast_var_t *variables;
263         int found = 0;
264         int len;
265         RAII_VAR(char *, local_buffer, NULL, ast_free);
266
267         /* Does this variable already exist? */
268         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
269                 if (!strcmp(var, ast_var_name(variables))) {
270                         found = 1;
271                         break;
272                 }
273         }
274
275         if (!found) {
276                 if ((variables = ast_var_assign(var, ""))) {
277                         AST_LIST_INSERT_HEAD(&frame->varshead, variables, entries);
278                 }
279                 pbx_builtin_pushvar_helper(chan, var, value);
280         } else {
281                 pbx_builtin_setvar_helper(chan, var, value);
282         }
283
284         len = 8 + strlen(var); /* LOCAL() + var */
285         local_buffer = ast_malloc(len);
286         if (!local_buffer) {
287                 return 0;
288         }
289         sprintf(local_buffer, "LOCAL(%s)", var);
290         ast_channel_publish_varset(chan, local_buffer, value);
291         return 0;
292 }
293
294 static void gosub_release_frame(struct ast_channel *chan, struct gosub_stack_frame *frame)
295 {
296         struct ast_var_t *vardata;
297
298         /* If chan is not defined, then we're calling it as part of gosub_free,
299          * and the channel variables will be deallocated anyway.  Otherwise, we're
300          * just releasing a single frame, so we need to clean up the arguments for
301          * that frame, so that we re-expose the variables from the previous frame
302          * that were hidden by this one.
303          */
304         while ((vardata = AST_LIST_REMOVE_HEAD(&frame->varshead, entries))) {
305                 if (chan)
306                         pbx_builtin_setvar_helper(chan, ast_var_name(vardata), NULL);   
307                 ast_var_delete(vardata);
308         }
309
310         ast_free(frame);
311 }
312
313 static struct gosub_stack_frame *gosub_allocate_frame(const char *context, const char *extension, int priority, unsigned char arguments)
314 {
315         struct gosub_stack_frame *new = NULL;
316         int len_extension = strlen(extension), len_context = strlen(context);
317
318         if ((new = ast_calloc(1, sizeof(*new) + 2 + len_extension + len_context))) {
319                 AST_LIST_HEAD_INIT_NOLOCK(&new->varshead);
320                 strcpy(new->extension, extension);
321                 new->context = new->extension + len_extension + 1;
322                 strcpy(new->context, context);
323                 new->priority = priority;
324                 new->arguments = arguments;
325         }
326         return new;
327 }
328
329 static void gosub_free(void *data)
330 {
331         struct gosub_stack_list *oldlist = data;
332         struct gosub_stack_frame *oldframe;
333
334         AST_LIST_LOCK(oldlist);
335         while ((oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
336                 gosub_release_frame(NULL, oldframe);
337         }
338         AST_LIST_UNLOCK(oldlist);
339         AST_LIST_HEAD_DESTROY(oldlist);
340         ast_free(oldlist);
341 }
342
343 static int pop_exec(struct ast_channel *chan, const char *data)
344 {
345         struct ast_datastore *stack_store;
346         struct gosub_stack_frame *oldframe;
347         struct gosub_stack_list *oldlist;
348         int res = 0;
349
350         ast_channel_lock(chan);
351         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
352                 ast_log(LOG_WARNING, "%s called with no gosub stack allocated.\n", app_pop);
353                 ast_channel_unlock(chan);
354                 return 0;
355         }
356
357         oldlist = stack_store->data;
358         AST_LIST_LOCK(oldlist);
359         oldframe = AST_LIST_FIRST(oldlist);
360         if (oldframe) {
361                 if (oldframe->is_special) {
362                         ast_debug(1, "%s attempted to pop special return location.\n", app_pop);
363
364                         /* Abort the special routine dialplan execution.  Dialplan programming error. */
365                         res = -1;
366                 } else {
367                         AST_LIST_REMOVE_HEAD(oldlist, entries);
368                         gosub_release_frame(chan, oldframe);
369                 }
370         } else {
371                 ast_debug(1, "%s called with an empty gosub stack\n", app_pop);
372         }
373         AST_LIST_UNLOCK(oldlist);
374         ast_channel_unlock(chan);
375         return res;
376 }
377
378 static int return_exec(struct ast_channel *chan, const char *data)
379 {
380         struct ast_datastore *stack_store;
381         struct gosub_stack_frame *oldframe;
382         struct gosub_stack_list *oldlist;
383         const char *retval = data;
384         int res = 0;
385
386         ast_channel_lock(chan);
387         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
388                 ast_log(LOG_ERROR, "Return without Gosub: stack is unallocated\n");
389                 ast_channel_unlock(chan);
390                 return -1;
391         }
392
393         oldlist = stack_store->data;
394         AST_LIST_LOCK(oldlist);
395         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
396         AST_LIST_UNLOCK(oldlist);
397
398         if (!oldframe) {
399                 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
400                 ast_channel_unlock(chan);
401                 return -1;
402         }
403         if (oldframe->is_special) {
404                 /* Exit from special routine. */
405                 res = -1;
406         }
407
408         /*
409          * We cannot use ast_explicit_goto() because we MUST restore
410          * what was there before.  Channels that do not have a PBX may
411          * not have the context or exten set.
412          */
413         ast_channel_context_set(chan, oldframe->context);
414         ast_channel_exten_set(chan, oldframe->extension);
415         if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) {
416                 --oldframe->priority;
417         }
418         ast_channel_priority_set(chan, oldframe->priority);
419
420         gosub_release_frame(chan, oldframe);
421
422         /* Set a return value, if any */
423         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
424         ast_channel_unlock(chan);
425         return res;
426 }
427
428 /*!
429  * \internal
430  * \brief Add missing context and/or exten to Gosub application argument string.
431  * \since 11.0
432  *
433  * \param chan Channel to obtain context/exten.
434  * \param args Gosub application argument string.
435  *
436  * \details
437  * Fills in the optional context and exten from the given channel.
438  * Convert: [[context,]exten,]priority[(arg1[,...][,argN])]
439  * To: context,exten,priority[(arg1[,...][,argN])]
440  *
441  * \retval expanded Gosub argument string on success.  Must be freed.
442  * \retval NULL on error.
443  *
444  * \note The parsing needs to be kept in sync with the
445  * gosub_exec() argument format.
446  */
447 static const char *expand_gosub_args(struct ast_channel *chan, const char *args)
448 {
449         int len;
450         char *parse;
451         char *label;
452         char *new_args;
453         const char *context;
454         const char *exten;
455         const char *pri;
456
457         /* Separate the context,exten,pri from the optional routine arguments. */
458         parse = ast_strdupa(args);
459         label = strsep(&parse, "(");
460         if (parse) {
461                 char *endparen;
462
463                 endparen = strrchr(parse, ')');
464                 if (endparen) {
465                         *endparen = '\0';
466                 } else {
467                         ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", args);
468                 }
469         }
470
471         /* Split context,exten,pri */
472         context = strsep(&label, ",");
473         exten = strsep(&label, ",");
474         pri = strsep(&label, ",");
475         if (!exten) {
476                 /* Only a priority in this one */
477                 pri = context;
478                 exten = NULL;
479                 context = NULL;
480         } else if (!pri) {
481                 /* Only an extension and priority in this one */
482                 pri = exten;
483                 exten = context;
484                 context = NULL;
485         }
486
487         ast_channel_lock(chan);
488         if (ast_strlen_zero(exten)) {
489                 exten = ast_channel_exten(chan);
490         }
491         if (ast_strlen_zero(context)) {
492                 context = ast_channel_context(chan);
493         }
494         len = strlen(context) + strlen(exten) + strlen(pri) + 3;
495         if (!ast_strlen_zero(parse)) {
496                 len += 2 + strlen(parse);
497         }
498         new_args = ast_malloc(len);
499         if (new_args) {
500                 if (ast_strlen_zero(parse)) {
501                         snprintf(new_args, len, "%s,%s,%s", context, exten, pri);
502                 } else {
503                         snprintf(new_args, len, "%s,%s,%s(%s)", context, exten, pri, parse);
504                 }
505         }
506         ast_channel_unlock(chan);
507
508         ast_debug(4, "Gosub args:%s new_args:%s\n", args, new_args ? new_args : "");
509
510         return new_args;
511 }
512
513 static int gosub_exec(struct ast_channel *chan, const char *data)
514 {
515         struct ast_datastore *stack_store;
516         struct gosub_stack_list *oldlist;
517         struct gosub_stack_frame *newframe;
518         struct gosub_stack_frame *lastframe;
519         char argname[15];
520         char *parse;
521         char *label;
522         char *caller_id;
523         char *orig_context;
524         char *orig_exten;
525         char *dest_context;
526         char *dest_exten;
527         int orig_priority;
528         int dest_priority;
529         int i;
530         int max_argc = 0;
531         AST_DECLARE_APP_ARGS(args2,
532                 AST_APP_ARG(argval)[100];
533         );
534
535         if (ast_strlen_zero(data)) {
536                 ast_log(LOG_ERROR, "%s requires an argument: %s([[context,]exten,]priority[(arg1[,...][,argN])])\n", app_gosub, app_gosub);
537                 return -1;
538         }
539
540         /*
541          * Separate the arguments from the label
542          *
543          * NOTE:  You cannot use ast_app_separate_args for this, because
544          * '(' cannot be used as a delimiter.
545          */
546         parse = ast_strdupa(data);
547         label = strsep(&parse, "(");
548         if (parse) {
549                 char *endparen;
550
551                 endparen = strrchr(parse, ')');
552                 if (endparen) {
553                         *endparen = '\0';
554                 } else {
555                         ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", data);
556                 }
557                 AST_STANDARD_RAW_ARGS(args2, parse);
558         } else {
559                 args2.argc = 0;
560         }
561
562         ast_channel_lock(chan);
563         orig_context = ast_strdupa(ast_channel_context(chan));
564         orig_exten = ast_strdupa(ast_channel_exten(chan));
565         orig_priority = ast_channel_priority(chan);
566         ast_channel_unlock(chan);
567
568         if (ast_parseable_goto(chan, label)) {
569                 ast_log(LOG_ERROR, "%s address is invalid: '%s'\n", app_gosub, data);
570                 goto error_exit;
571         }
572
573         ast_channel_lock(chan);
574         dest_context = ast_strdupa(ast_channel_context(chan));
575         dest_exten = ast_strdupa(ast_channel_exten(chan));
576         dest_priority = ast_channel_priority(chan);
577         if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) {
578                 ++dest_priority;
579         }
580         caller_id = S_COR(ast_channel_caller(chan)->id.number.valid,
581                 ast_channel_caller(chan)->id.number.str, NULL);
582         if (caller_id) {
583                 caller_id = ast_strdupa(caller_id);
584         }
585         ast_channel_unlock(chan);
586
587         if (!ast_exists_extension(chan, dest_context, dest_exten, dest_priority, caller_id)) {
588                 ast_log(LOG_ERROR, "Attempt to reach a non-existent destination for %s: (Context:%s, Extension:%s, Priority:%d)\n",
589                         app_gosub, dest_context, dest_exten, dest_priority);
590                 goto error_exit;
591         }
592
593         /* Now we know that we're going to a new location */
594
595         ast_channel_lock(chan);
596
597         /* Find stack datastore return list. */
598         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
599                 ast_debug(1, "Channel %s has no datastore, so we're allocating one.\n",
600                         ast_channel_name(chan));
601                 stack_store = ast_datastore_alloc(&stack_info, NULL);
602                 if (!stack_store) {
603                         ast_log(LOG_ERROR, "Unable to allocate new datastore.  %s failed.\n",
604                                 app_gosub);
605                         goto error_exit_locked;
606                 }
607
608                 oldlist = ast_calloc(1, sizeof(*oldlist));
609                 if (!oldlist) {
610                         ast_log(LOG_ERROR, "Unable to allocate datastore list head.  %s failed.\n",
611                                 app_gosub);
612                         ast_datastore_free(stack_store);
613                         goto error_exit_locked;
614                 }
615                 AST_LIST_HEAD_INIT(oldlist);
616
617                 stack_store->data = oldlist;
618                 ast_channel_datastore_add(chan, stack_store);
619         } else {
620                 oldlist = stack_store->data;
621         }
622
623         if ((lastframe = AST_LIST_FIRST(oldlist))) {
624                 max_argc = lastframe->arguments;
625         }
626
627         /* Mask out previous Gosub arguments in this invocation */
628         if (args2.argc > max_argc) {
629                 max_argc = args2.argc;
630         }
631
632         /* Create the return address */
633         newframe = gosub_allocate_frame(orig_context, orig_exten, orig_priority + 1, max_argc);
634         if (!newframe) {
635                 goto error_exit_locked;
636         }
637
638         /* Set our arguments */
639         for (i = 0; i < max_argc; i++) {
640                 snprintf(argname, sizeof(argname), "ARG%d", i + 1);
641                 frame_set_var(chan, newframe, argname, i < args2.argc ? args2.argval[i] : "");
642                 ast_debug(1, "Setting '%s' to '%s'\n", argname, i < args2.argc ? args2.argval[i] : "");
643         }
644         snprintf(argname, sizeof(argname), "%d", args2.argc);
645         frame_set_var(chan, newframe, "ARGC", argname);
646
647         /* And finally, save our return address */
648         AST_LIST_LOCK(oldlist);
649         AST_LIST_INSERT_HEAD(oldlist, newframe, entries);
650         AST_LIST_UNLOCK(oldlist);
651         ast_channel_unlock(chan);
652
653         return 0;
654
655 error_exit:
656         ast_channel_lock(chan);
657
658 error_exit_locked:
659         /* Restore the original dialplan location. */
660         ast_channel_context_set(chan, orig_context);
661         ast_channel_exten_set(chan, orig_exten);
662         ast_channel_priority_set(chan, orig_priority);
663         ast_channel_unlock(chan);
664         return -1;
665 }
666
667 static int gosubif_exec(struct ast_channel *chan, const char *data)
668 {
669         char *args;
670         int res=0;
671         AST_DECLARE_APP_ARGS(cond,
672                 AST_APP_ARG(ition);
673                 AST_APP_ARG(labels);
674         );
675         AST_DECLARE_APP_ARGS(label,
676                 AST_APP_ARG(iftrue);
677                 AST_APP_ARG(iffalse);
678         );
679
680         if (ast_strlen_zero(data)) {
681                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
682                 return 0;
683         }
684
685         args = ast_strdupa(data);
686         AST_NONSTANDARD_RAW_ARGS(cond, args, '?');
687         if (cond.argc != 2) {
688                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
689                 return 0;
690         }
691
692         AST_NONSTANDARD_RAW_ARGS(label, cond.labels, ':');
693
694         if (pbx_checkcondition(cond.ition)) {
695                 if (!ast_strlen_zero(label.iftrue))
696                         res = gosub_exec(chan, label.iftrue);
697         } else if (!ast_strlen_zero(label.iffalse)) {
698                 res = gosub_exec(chan, label.iffalse);
699         }
700
701         return res;
702 }
703
704 static int local_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
705 {
706         struct ast_datastore *stack_store;
707         struct gosub_stack_list *oldlist;
708         struct gosub_stack_frame *frame;
709         struct ast_var_t *variables;
710
711         ast_channel_lock(chan);
712         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
713                 ast_channel_unlock(chan);
714                 return -1;
715         }
716
717         oldlist = stack_store->data;
718         AST_LIST_LOCK(oldlist);
719         if (!(frame = AST_LIST_FIRST(oldlist))) {
720                 /* Not within a Gosub routine */
721                 AST_LIST_UNLOCK(oldlist);
722                 ast_channel_unlock(chan);
723                 return -1;
724         }
725
726         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
727                 if (!strcmp(data, ast_var_name(variables))) {
728                         const char *tmp;
729                         tmp = pbx_builtin_getvar_helper(chan, data);
730                         ast_copy_string(buf, S_OR(tmp, ""), len);
731                         break;
732                 }
733         }
734         AST_LIST_UNLOCK(oldlist);
735         ast_channel_unlock(chan);
736         return 0;
737 }
738
739 static int local_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
740 {
741         struct ast_datastore *stack_store;
742         struct gosub_stack_list *oldlist;
743         struct gosub_stack_frame *frame;
744
745         ast_channel_lock(chan);
746         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
747                 ast_log(LOG_ERROR, "Tried to set LOCAL(%s), but we aren't within a Gosub routine\n", var);
748                 ast_channel_unlock(chan);
749                 return -1;
750         }
751
752         oldlist = stack_store->data;
753         AST_LIST_LOCK(oldlist);
754         frame = AST_LIST_FIRST(oldlist);
755
756         if (frame) {
757                 frame_set_var(chan, frame, var, value);
758         }
759
760         AST_LIST_UNLOCK(oldlist);
761         ast_channel_unlock(chan);
762
763         return 0;
764 }
765
766 static struct ast_custom_function local_function = {
767         .name = "LOCAL",
768         .write = local_write,
769         .read = local_read,
770 };
771
772 static int peek_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
773 {
774         int found = 0, n;
775         struct ast_var_t *variables;
776         AST_DECLARE_APP_ARGS(args,
777                 AST_APP_ARG(n);
778                 AST_APP_ARG(name);
779         );
780
781         if (!chan) {
782                 ast_log(LOG_ERROR, "LOCAL_PEEK must be called on an active channel\n");
783                 return -1;
784         }
785
786         AST_STANDARD_RAW_ARGS(args, data);
787
788         if (ast_strlen_zero(args.n) || ast_strlen_zero(args.name)) {
789                 ast_log(LOG_ERROR, "LOCAL_PEEK requires parameters n and varname\n");
790                 return -1;
791         }
792
793         n = atoi(args.n);
794         *buf = '\0';
795
796         ast_channel_lock(chan);
797         AST_LIST_TRAVERSE(ast_channel_varshead(chan), variables, entries) {
798                 if (!strcmp(args.name, ast_var_name(variables)) && ++found > n) {
799                         ast_copy_string(buf, ast_var_value(variables), len);
800                         break;
801                 }
802         }
803         ast_channel_unlock(chan);
804         return 0;
805 }
806
807 static struct ast_custom_function peek_function = {
808         .name = "LOCAL_PEEK",
809         .read = peek_read,
810 };
811
812 static int stackpeek_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
813 {
814         struct ast_datastore *stack_store;
815         struct gosub_stack_list *oldlist;
816         struct gosub_stack_frame *frame;
817         int n;
818         AST_DECLARE_APP_ARGS(args,
819                 AST_APP_ARG(n);
820                 AST_APP_ARG(which);
821                 AST_APP_ARG(suppress);
822         );
823
824         if (!chan) {
825                 ast_log(LOG_ERROR, "STACK_PEEK must be called on an active channel\n");
826                 return -1;
827         }
828
829         data = ast_strdupa(data);
830         AST_STANDARD_APP_ARGS(args, data);
831
832         if (ast_strlen_zero(args.n) || ast_strlen_zero(args.which)) {
833                 ast_log(LOG_ERROR, "STACK_PEEK requires parameters n and which\n");
834                 return -1;
835         }
836
837         n = atoi(args.n);
838         if (n <= 0) {
839                 ast_log(LOG_ERROR, "STACK_PEEK must be called with a positive peek value\n");
840                 return -1;
841         }
842
843         ast_channel_lock(chan);
844         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
845                 if (!ast_true(args.suppress)) {
846                         ast_log(LOG_ERROR, "STACK_PEEK called on a channel without a gosub stack\n");
847                 }
848                 ast_channel_unlock(chan);
849                 return -1;
850         }
851
852         oldlist = stack_store->data;
853
854         AST_LIST_LOCK(oldlist);
855         AST_LIST_TRAVERSE(oldlist, frame, entries) {
856                 if (--n == 0) {
857                         break;
858                 }
859         }
860
861         if (!frame) {
862                 /* Too deep */
863                 if (!ast_true(args.suppress)) {
864                         ast_log(LOG_ERROR, "Stack peek of '%s' is more stack frames than I have\n", args.n);
865                 }
866                 ast_channel_unlock(chan);
867                 return -1;
868         }
869
870         args.which = ast_skip_blanks(args.which);
871
872         switch (args.which[0]) {
873         case 'l': /* label */
874                 ast_str_set(str, len, "%s,%s,%d", frame->context, frame->extension, frame->priority - 1);
875                 break;
876         case 'c': /* context */
877                 ast_str_set(str, len, "%s", frame->context);
878                 break;
879         case 'e': /* extension */
880                 ast_str_set(str, len, "%s", frame->extension);
881                 break;
882         case 'p': /* priority */
883                 ast_str_set(str, len, "%d", frame->priority - 1);
884                 break;
885         default:
886                 ast_log(LOG_ERROR, "Unknown argument '%s' to STACK_PEEK\n", args.which);
887                 break;
888         }
889
890         AST_LIST_UNLOCK(oldlist);
891         ast_channel_unlock(chan);
892
893         return 0;
894 }
895
896 static struct ast_custom_function stackpeek_function = {
897         .name = "STACK_PEEK",
898         .read2 = stackpeek_read,
899 };
900
901 /*!
902  * \internal
903  * \brief Pop stack frames until remove a special return location.
904  * \since 11.0
905  *
906  * \param chan Channel to balance stack on.
907  *
908  * \note The channel is already locked when called.
909  *
910  * \return Nothing
911  */
912 static void balance_stack(struct ast_channel *chan)
913 {
914         struct ast_datastore *stack_store;
915         struct gosub_stack_list *oldlist;
916         struct gosub_stack_frame *oldframe;
917         int found;
918
919         stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
920         if (!stack_store) {
921                 ast_log(LOG_WARNING, "No %s stack allocated.\n", app_gosub);
922                 return;
923         }
924
925         oldlist = stack_store->data;
926         AST_LIST_LOCK(oldlist);
927         do {
928                 oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
929                 if (!oldframe) {
930                         break;
931                 }
932                 found = oldframe->is_special;
933                 gosub_release_frame(chan, oldframe);
934         } while (!found);
935         AST_LIST_UNLOCK(oldlist);
936 }
937
938 /*!
939  * \internal
940  * \brief Run a subroutine on a channel.
941  * \since 11.0
942  *
943  * \note Absolutely _NO_ channel locks should be held before calling this function.
944  *
945  * \param chan Channel to execute subroutine on.
946  * \param sub_args Gosub application argument string.
947  * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
948  *
949  * \retval 0 success
950  * \retval -1 on error
951  */
952 static int gosub_run(struct ast_channel *chan, const char *sub_args, int ignore_hangup)
953 {
954         const char *saved_context;
955         const char *saved_exten;
956         int saved_priority;
957         int saved_hangup_flags;
958         int saved_autoloopflag;
959         int res;
960
961         ast_channel_lock(chan);
962
963         ast_verb(3, "%s Internal %s(%s) start\n",
964                 ast_channel_name(chan), app_gosub, sub_args);
965
966         /* Save non-hangup softhangup flags. */
967         saved_hangup_flags = ast_channel_softhangup_internal_flag(chan)
968                 & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE);
969         if (saved_hangup_flags) {
970                 ast_channel_clear_softhangup(chan,
971                         AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE);
972         }
973
974         /* Save autoloop flag */
975         saved_autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
976         ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
977
978         /* Save current dialplan location */
979         saved_context = ast_strdupa(ast_channel_context(chan));
980         saved_exten = ast_strdupa(ast_channel_exten(chan));
981         saved_priority = ast_channel_priority(chan);
982
983         ast_debug(4, "%s Original location: %s,%s,%d\n", ast_channel_name(chan),
984                 saved_context, saved_exten, saved_priority);
985
986         ast_channel_unlock(chan);
987         res = gosub_exec(chan, sub_args);
988         ast_debug(4, "%s exited with status %d\n", app_gosub, res);
989         ast_channel_lock(chan);
990         if (!res) {
991                 struct ast_datastore *stack_store;
992
993                 /* Mark the return location as special. */
994                 stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
995                 if (!stack_store) {
996                         /* Should never happen! */
997                         ast_log(LOG_ERROR, "No %s stack!\n", app_gosub);
998                         res = -1;
999                 } else {
1000                         struct gosub_stack_list *oldlist;
1001                         struct gosub_stack_frame *cur;
1002
1003                         oldlist = stack_store->data;
1004                         cur = AST_LIST_FIRST(oldlist);
1005                         cur->is_special = 1;
1006                 }
1007         }
1008         if (!res) {
1009                 int found = 0;  /* set if we find at least one match */
1010
1011                 /*
1012                  * Run gosub body autoloop.
1013                  *
1014                  * Note that this loop is inverted from the normal execution
1015                  * loop because we just executed the Gosub application as the
1016                  * first extension of the autoloop.
1017                  */
1018                 do {
1019                         /* Check for hangup. */
1020                         if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_UNBRIDGE) {
1021                                 saved_hangup_flags |= AST_SOFTHANGUP_UNBRIDGE;
1022                                 ast_channel_clear_softhangup(chan, AST_SOFTHANGUP_UNBRIDGE);
1023                         }
1024                         if (ast_check_hangup(chan)) {
1025                                 if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
1026                                         ast_log(LOG_ERROR, "%s An async goto just messed up our execution location.\n",
1027                                                 ast_channel_name(chan));
1028                                         break;
1029                                 }
1030                                 if (!ignore_hangup) {
1031                                         break;
1032                                 }
1033                         }
1034
1035                         /* Next dialplan priority. */
1036                         ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
1037
1038                         ast_channel_unlock(chan);
1039                         res = ast_spawn_extension(chan, ast_channel_context(chan),
1040                                 ast_channel_exten(chan), ast_channel_priority(chan),
1041                                 S_COR(ast_channel_caller(chan)->id.number.valid,
1042                                         ast_channel_caller(chan)->id.number.str, NULL),
1043                                 &found, 1);
1044                         ast_channel_lock(chan);
1045                 } while (!res);
1046                 if (found && res) {
1047                         /* Something bad happened, or a hangup has been requested. */
1048                         ast_debug(1, "Spawn extension (%s,%s,%d) exited with %d on '%s'\n",
1049                                 ast_channel_context(chan), ast_channel_exten(chan),
1050                                 ast_channel_priority(chan), res, ast_channel_name(chan));
1051                         ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n",
1052                                 ast_channel_context(chan), ast_channel_exten(chan),
1053                                 ast_channel_priority(chan), ast_channel_name(chan));
1054                 }
1055
1056                 /* Did the routine return? */
1057                 if (ast_channel_priority(chan) == saved_priority
1058                         && !strcmp(ast_channel_context(chan), saved_context)
1059                         && !strcmp(ast_channel_exten(chan), saved_exten)) {
1060                         ast_verb(3, "%s Internal %s(%s) complete GOSUB_RETVAL=%s\n",
1061                                 ast_channel_name(chan), app_gosub, sub_args,
1062                                 S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), ""));
1063                 } else {
1064                         ast_log(LOG_NOTICE, "%s Abnormal '%s(%s)' exit.  Popping routine return locations.\n",
1065                                 ast_channel_name(chan), app_gosub, sub_args);
1066                         balance_stack(chan);
1067                         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", "");
1068                 }
1069
1070                 /* We executed the requested subroutine to the best of our ability. */
1071                 res = 0;
1072         }
1073
1074         ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan),
1075                 ast_channel_context(chan), ast_channel_exten(chan),
1076                 ast_channel_priority(chan));
1077
1078         /* Restore dialplan location */
1079         if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)) {
1080                 ast_channel_context_set(chan, saved_context);
1081                 ast_channel_exten_set(chan, saved_exten);
1082                 ast_channel_priority_set(chan, saved_priority);
1083         }
1084
1085         /* Restore autoloop flag */
1086         ast_set2_flag(ast_channel_flags(chan), saved_autoloopflag, AST_FLAG_IN_AUTOLOOP);
1087
1088         /* Restore non-hangup softhangup flags. */
1089         if (saved_hangup_flags) {
1090                 ast_softhangup_nolock(chan, saved_hangup_flags);
1091         }
1092
1093         ast_channel_unlock(chan);
1094
1095         return res;
1096 }
1097
1098 static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, const char * const *argv)
1099 {
1100         int res;
1101         int priority;
1102         int old_autoloopflag;
1103         int old_priority;
1104         const char *old_context;
1105         const char *old_extension;
1106         char *gosub_args;
1107
1108         if (argc < 4 || argc > 5) {
1109                 return RESULT_SHOWUSAGE;
1110         }
1111
1112         ast_debug(1, "Gosub called with %d arguments: 0:%s 1:%s 2:%s 3:%s 4:%s\n", argc, argv[0], argv[1], argv[2], argv[3], argc == 5 ? argv[4] : "");
1113
1114         if (sscanf(argv[3], "%30d", &priority) != 1 || priority < 1) {
1115                 /* Lookup the priority label */
1116                 priority = ast_findlabel_extension(chan, argv[1], argv[2], argv[3],
1117                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
1118                 if (priority < 0) {
1119                         ast_log(LOG_ERROR, "Priority '%s' not found in '%s@%s'\n", argv[3], argv[2], argv[1]);
1120                         ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
1121                         return RESULT_FAILURE;
1122                 }
1123         } else if (!ast_exists_extension(chan, argv[1], argv[2], priority,
1124                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
1125                 ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
1126                 return RESULT_FAILURE;
1127         }
1128
1129         if (argc == 5) {
1130                 if (ast_asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority, argv[4]) < 0) {
1131                         gosub_args = NULL;
1132                 }
1133         } else {
1134                 if (ast_asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority) < 0) {
1135                         gosub_args = NULL;
1136                 }
1137         }
1138         if (!gosub_args) {
1139                 ast_agi_send(agi->fd, chan, "503 result=-2 Memory allocation failure\n");
1140                 return RESULT_FAILURE;
1141         }
1142
1143         ast_channel_lock(chan);
1144
1145         ast_verb(3, "%s AGI %s(%s) start\n", ast_channel_name(chan), app_gosub, gosub_args);
1146
1147         /* Save autoloop flag */
1148         old_autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
1149         ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
1150
1151         /* Save previous location, since we're going to change it */
1152         old_context = ast_strdupa(ast_channel_context(chan));
1153         old_extension = ast_strdupa(ast_channel_exten(chan));
1154         old_priority = ast_channel_priority(chan);
1155
1156         ast_debug(4, "%s Original location: %s,%s,%d\n", ast_channel_name(chan),
1157                 old_context, old_extension, old_priority);
1158         ast_channel_unlock(chan);
1159
1160         res = gosub_exec(chan, gosub_args);
1161         if (!res) {
1162                 struct ast_datastore *stack_store;
1163
1164                 /* Mark the return location as special. */
1165                 ast_channel_lock(chan);
1166                 stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
1167                 if (!stack_store) {
1168                         /* Should never happen! */
1169                         ast_log(LOG_ERROR, "No %s stack!\n", app_gosub);
1170                         res = -1;
1171                 } else {
1172                         struct gosub_stack_list *oldlist;
1173                         struct gosub_stack_frame *cur;
1174
1175                         oldlist = stack_store->data;
1176                         cur = AST_LIST_FIRST(oldlist);
1177                         cur->is_special = 1;
1178                 }
1179                 ast_channel_unlock(chan);
1180         }
1181         if (!res) {
1182                 struct ast_pbx *pbx;
1183                 struct ast_pbx_args args;
1184                 int abnormal_exit;
1185
1186                 memset(&args, 0, sizeof(args));
1187                 args.no_hangup_chan = 1;
1188
1189                 ast_channel_lock(chan);
1190
1191                 /* Next dialplan priority. */
1192                 ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
1193
1194                 /* Suppress warning about PBX already existing */
1195                 pbx = ast_channel_pbx(chan);
1196                 ast_channel_pbx_set(chan, NULL);
1197                 ast_channel_unlock(chan);
1198
1199                 ast_agi_send(agi->fd, chan, "100 result=0 Trying...\n");
1200                 ast_pbx_run_args(chan, &args);
1201
1202                 ast_channel_lock(chan);
1203                 ast_free(ast_channel_pbx(chan));
1204                 ast_channel_pbx_set(chan, pbx);
1205
1206                 /* Did the routine return? */
1207                 if (ast_channel_priority(chan) == old_priority
1208                         && !strcmp(ast_channel_context(chan), old_context)
1209                         && !strcmp(ast_channel_exten(chan), old_extension)) {
1210                         ast_verb(3, "%s AGI %s(%s) complete GOSUB_RETVAL=%s\n",
1211                                 ast_channel_name(chan), app_gosub, gosub_args,
1212                                 S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), ""));
1213                         abnormal_exit = 0;
1214                 } else {
1215                         ast_log(LOG_NOTICE, "%s Abnormal AGI %s(%s) exit.  Popping routine return locations.\n",
1216                                 ast_channel_name(chan), app_gosub, gosub_args);
1217                         balance_stack(chan);
1218                         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", "");
1219                         abnormal_exit = 1;
1220                 }
1221                 ast_channel_unlock(chan);
1222
1223                 ast_agi_send(agi->fd, chan, "200 result=0 Gosub complete%s\n",
1224                         abnormal_exit ? " (abnormal exit)" : "");
1225         } else {
1226                 ast_agi_send(agi->fd, chan, "200 result=%d Gosub failed\n", res);
1227         }
1228
1229         ast_free(gosub_args);
1230
1231         ast_channel_lock(chan);
1232         ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan),
1233                 ast_channel_context(chan), ast_channel_exten(chan),
1234                 ast_channel_priority(chan));
1235
1236         /* Restore previous location */
1237         ast_channel_context_set(chan, old_context);
1238         ast_channel_exten_set(chan, old_extension);
1239         ast_channel_priority_set(chan, old_priority);
1240
1241         /* Restore autoloop flag */
1242         ast_set2_flag(ast_channel_flags(chan), old_autoloopflag, AST_FLAG_IN_AUTOLOOP);
1243         ast_channel_unlock(chan);
1244
1245         return RESULT_SUCCESS;
1246 }
1247
1248 static struct agi_command gosub_agi_command =
1249         { { "gosub", NULL }, handle_gosub, NULL, NULL, 0 };
1250
1251 static int unload_module(void)
1252 {
1253         ast_install_stack_functions(NULL);
1254
1255         ast_agi_unregister(ast_module_info->self, &gosub_agi_command);
1256
1257         ast_unregister_application(app_return);
1258         ast_unregister_application(app_pop);
1259         ast_unregister_application(app_gosubif);
1260         ast_unregister_application(app_gosub);
1261         ast_custom_function_unregister(&local_function);
1262         ast_custom_function_unregister(&peek_function);
1263         ast_custom_function_unregister(&stackpeek_function);
1264
1265         return 0;
1266 }
1267
1268 static int load_module(void)
1269 {
1270         /* Setup the stack application callback functions. */
1271         static struct ast_app_stack_funcs funcs = {
1272                 .run_sub = gosub_run,
1273                 .expand_sub_args = expand_gosub_args,
1274         };
1275
1276         ast_agi_register(ast_module_info->self, &gosub_agi_command);
1277
1278         ast_register_application_xml(app_pop, pop_exec);
1279         ast_register_application_xml(app_return, return_exec);
1280         ast_register_application_xml(app_gosubif, gosubif_exec);
1281         ast_register_application_xml(app_gosub, gosub_exec);
1282         ast_custom_function_register(&local_function);
1283         ast_custom_function_register(&peek_function);
1284         ast_custom_function_register(&stackpeek_function);
1285
1286         funcs.module = ast_module_info->self,
1287         ast_install_stack_functions(&funcs);
1288
1289         return 0;
1290 }
1291
1292 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT | AST_MODFLAG_LOAD_ORDER, "Dialplan subroutines (Gosub, Return, etc)",
1293                 .load = load_module,
1294                 .unload = unload_module,
1295                 .load_pri = AST_MODPRI_APP_DEPEND,
1296                 .nonoptreq = "res_agi",
1297                 );