2 * Asterisk -- An open source telephony toolkit.
4 * Copyright 2004 - 2005, Anthony Minessale <anthmct@yahoo.com>
6 * Anthony Minessale <anthmct@yahoo.com>
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.
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.
21 * \brief While Loop Implementation
23 * \author Anthony Minessale <anthmct@yahoo.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/config.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/options.h"
47 static char *start_app = "While";
48 static char *start_desc =
49 "Usage: While(<expr>)\n"
50 "Start a While Loop. Execution will return to this point when\n"
51 "EndWhile is called until expr is no longer true.\n";
53 static char *start_synopsis = "Start a while loop";
56 static char *stop_app = "EndWhile";
57 static char *stop_desc =
59 "Return to the previous called While\n";
61 static char *stop_synopsis = "End a while loop";
63 static char *exit_app = "ExitWhile";
64 static char *exit_desc =
65 "Usage: ExitWhile()\n"
66 "Exits a While loop, whether or not the conditional has been satisfied.\n";
67 static char *exit_synopsis = "End a While loop";
69 static char *continue_app = "ContinueWhile";
70 static char *continue_desc =
71 "Usage: ContinueWhile()\n"
72 "Returns to the top of the while loop and re-evaluates the conditional.\n";
73 static char *continue_synopsis = "Restart a While loop";
78 static const char *get_index(struct ast_channel *chan, const char *prefix, int index) {
79 char varname[VAR_SIZE];
81 snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
82 return pbx_builtin_getvar_helper(chan, varname);
85 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
88 struct ast_include *i;
89 struct ast_context *c2;
91 for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
92 if (ast_extension_match(ast_get_extension_name(e), exten)) {
93 int needmatch = ast_get_extension_matchcid(e);
94 if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
96 /* This is the matching extension we want */
98 for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
99 if (priority != ast_get_extension_priority(p))
107 /* No match; run through includes */
108 for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
109 for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
110 if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
111 e = find_matching_priority(c2, exten, priority, callerid);
120 static int find_matching_endwhile(struct ast_channel *chan)
122 struct ast_context *c;
125 if (ast_rdlock_contexts()) {
126 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
130 for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
133 if (!ast_rdlock_context(c)) {
134 if (!strcmp(ast_get_context_name(c), chan->context)) {
135 /* This is the matching context we want */
136 int cur_priority = chan->priority + 1, level=1;
138 for (e = find_matching_priority(c, chan->exten, cur_priority, chan->cid.cid_num); e; e = find_matching_priority(c, chan->exten, ++cur_priority, chan->cid.cid_num)) {
139 if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
141 } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
151 ast_unlock_context(c);
157 ast_unlock_contexts();
161 static int _while_exec(struct ast_channel *chan, void *data, int end)
164 const char *while_pri = NULL;
165 char *my_name = NULL;
166 const char *condition = NULL, *label = NULL;
167 char varname[VAR_SIZE], end_varname[VAR_SIZE];
168 const char *prefix = "WHILE";
170 int used_index_i = -1, x=0;
171 char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
178 /* dont want run away loops if the chan isn't even up
179 this is up for debate since it slows things down a tad ......
181 if (ast_waitfordigit(chan,1) < 0)
185 if (get_index(chan, prefix, x)) {
191 snprintf(used_index, VAR_SIZE, "%d", used_index_i);
192 snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
195 condition = ast_strdupa(data);
197 size = strlen(chan->context) + strlen(chan->exten) + 32;
198 my_name = alloca(size);
199 memset(my_name, 0, size);
200 snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
202 if (ast_strlen_zero(label)) {
205 else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
207 pbx_builtin_setvar_helper(chan, my_name, label);
212 snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
213 while_pri = pbx_builtin_getvar_helper(chan, varname);
215 if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
216 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
220 if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
221 /* Condition Met (clean up helper vars) */
222 const char *goto_str;
223 pbx_builtin_setvar_helper(chan, varname, NULL);
224 pbx_builtin_setvar_helper(chan, my_name, NULL);
225 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
226 if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
227 ast_parseable_goto(chan, goto_str);
228 pbx_builtin_setvar_helper(chan, end_varname, NULL);
230 int pri = find_matching_endwhile(chan);
232 if (option_verbose > 2)
233 ast_verbose(VERBOSE_PREFIX_3 "Jumping to priority %d\n", pri);
234 chan->priority = pri;
236 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
242 if (!end && !while_pri) {
244 size = strlen(chan->context) + strlen(chan->exten) + 32;
245 goto_str = alloca(size);
246 memset(goto_str, 0, size);
247 snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority);
248 pbx_builtin_setvar_helper(chan, varname, goto_str);
251 else if (end && while_pri) {
253 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
254 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
256 size = strlen(chan->context) + strlen(chan->exten) + 32;
257 goto_str = alloca(size);
258 memset(goto_str, 0, size);
259 snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority+1);
260 pbx_builtin_setvar_helper(chan, end_varname, goto_str);
262 ast_parseable_goto(chan, while_pri);
268 static int while_start_exec(struct ast_channel *chan, void *data) {
269 return _while_exec(chan, data, 0);
272 static int while_end_exec(struct ast_channel *chan, void *data) {
273 return _while_exec(chan, data, 1);
276 static int while_exit_exec(struct ast_channel *chan, void *data) {
277 return _while_exec(chan, data, 2);
280 static int while_continue_exec(struct ast_channel *chan, void *data)
283 const char *prefix = "WHILE", *while_pri=NULL;
286 const char *tmp = get_index(chan, prefix, x);
294 ast_parseable_goto(chan, while_pri);
299 static int unload_module(void)
303 res = ast_unregister_application(start_app);
304 res |= ast_unregister_application(stop_app);
305 res |= ast_unregister_application(exit_app);
306 res |= ast_unregister_application(continue_app);
311 static int load_module(void)
315 res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
316 res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
317 res |= ast_register_application(exit_app, while_exit_exec, exit_synopsis, exit_desc);
318 res |= ast_register_application(continue_app, while_continue_exec, continue_synopsis, continue_desc);
323 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");