app_stack.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. );