app_stack.c 38 KB

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