app_stack.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2004-2005 Tilghman Lesher <app_stack_v002@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. * \ingroup applications
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include "asterisk/options.h"
  29. #include "asterisk/logger.h"
  30. #include "asterisk/channel.h"
  31. #include "asterisk/chanvars.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/config.h"
  35. #define STACKVAR "~GOSUB~STACK~"
  36. static const char *tdesc = "Stack Routines";
  37. static const char *app_gosub = "Gosub";
  38. static const char *app_gosubif = "GosubIf";
  39. static const char *app_return = "Return";
  40. static const char *app_pop = "StackPop";
  41. static const char *gosub_synopsis = "Jump to label, saving return address";
  42. static const char *gosubif_synopsis = "Jump to label, saving return address";
  43. static const char *return_synopsis = "Return from gosub routine";
  44. static const char *pop_synopsis = "Remove one address from gosub stack";
  45. static const char *gosub_descrip =
  46. "Gosub([[context|]exten|]priority)\n"
  47. " Jumps to the label specified, saving the return address.\n"
  48. " Returns 0 if the label exists or -1 otherwise.\n";
  49. static const char *gosubif_descrip =
  50. "Gosub(condition?labeliftrue[:labeliffalse])\n"
  51. " If the condition is true, then jump to labeliftrue. If false, jumps to\n"
  52. "labeliffalse, if specified. In either case, a jump saves the return point\n"
  53. "in the dialplan, to be returned to with a Return.\n"
  54. " Returns 0 if the label exists or -1 otherwise.\n";
  55. static const char *return_descrip =
  56. "Return()\n"
  57. " Jumps to the last label in the stack, removing it.\n"
  58. " Returns 0 if there's a label in the stack or -1 otherwise.\n";
  59. static const char *pop_descrip =
  60. "StackPop()\n"
  61. " Removes last label in the stack, discarding it.\n"
  62. " Always returns 0, even if the stack is empty.\n";
  63. STANDARD_LOCAL_USER;
  64. LOCAL_USER_DECL;
  65. static int pop_exec(struct ast_channel *chan, void *data)
  66. {
  67. pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
  68. return 0;
  69. }
  70. static int return_exec(struct ast_channel *chan, void *data)
  71. {
  72. char *label = pbx_builtin_getvar_helper(chan, STACKVAR);
  73. if (ast_strlen_zero(label)) {
  74. ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
  75. return -1;
  76. } else if (ast_parseable_goto(chan, label)) {
  77. ast_log(LOG_WARNING, "No next statement after Gosub?\n");
  78. return -1;
  79. }
  80. pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
  81. return 0;
  82. }
  83. static int gosub_exec(struct ast_channel *chan, void *data)
  84. {
  85. char newlabel[AST_MAX_EXTENSION * 2 + 3 + 11];
  86. struct localuser *u;
  87. if (ast_strlen_zero(data)) {
  88. ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority)\n", app_gosub, app_gosub);
  89. return -1;
  90. }
  91. LOCAL_USER_ADD(u);
  92. snprintf(newlabel, sizeof(newlabel), "%s|%s|%d", chan->context, chan->exten, chan->priority + 1);
  93. if (ast_parseable_goto(chan, data)) {
  94. LOCAL_USER_REMOVE(u);
  95. return -1;
  96. }
  97. pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel);
  98. LOCAL_USER_REMOVE(u);
  99. return 0;
  100. }
  101. static int gosubif_exec(struct ast_channel *chan, void *data)
  102. {
  103. struct localuser *u;
  104. char *condition="", *label1, *label2, *args;
  105. int res=0;
  106. if (ast_strlen_zero(data)) {
  107. ast_log(LOG_WARNING, "GosubIf requires an argument\n");
  108. return 0;
  109. }
  110. args = ast_strdupa((char *)data);
  111. if (!args) {
  112. ast_log(LOG_ERROR, "Out of memory\n");
  113. return -1;
  114. }
  115. LOCAL_USER_ADD(u);
  116. condition = strsep(&args, "?");
  117. label1 = strsep(&args, ":");
  118. label2 = args;
  119. if (ast_true(condition)) {
  120. if (label1) {
  121. res = gosub_exec(chan, label1);
  122. }
  123. } else if (label2) {
  124. res = gosub_exec(chan, label2);
  125. }
  126. LOCAL_USER_REMOVE(u);
  127. return res;
  128. }
  129. int unload_module(void)
  130. {
  131. ast_unregister_application(app_return);
  132. ast_unregister_application(app_pop);
  133. ast_unregister_application(app_gosubif);
  134. ast_unregister_application(app_gosub);
  135. STANDARD_HANGUP_LOCALUSERS;
  136. return 0;
  137. }
  138. int load_module(void)
  139. {
  140. ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
  141. ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
  142. ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
  143. ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
  144. return 0;
  145. }
  146. char *description(void)
  147. {
  148. return (char *) tdesc;
  149. }
  150. int usecount(void)
  151. {
  152. int res;
  153. STANDARD_USECOUNT(res);
  154. return res;
  155. }
  156. char *key()
  157. {
  158. return ASTERISK_GPL_KEY;
  159. }