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$")
32 #include "asterisk/pbx.h"
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
36 static char *start_app
= "While";
37 static char *start_desc
=
38 " While(<expr>): Start a While Loop. Execution will return to this\n"
39 "point when EndWhile() is called until expr is no longer true.\n";
41 static char *start_synopsis
= "Start a while loop";
44 static char *stop_app
= "EndWhile";
45 static char *stop_desc
=
46 " EndWhile(): Return to the previous called While()\n";
48 static char *stop_synopsis
= "End a while loop";
50 static char *exit_app
= "ExitWhile";
51 static char *exit_desc
=
52 " ExitWhile(): Exits a While() loop, whether or not the conditional has been satisfied.\n";
53 static char *exit_synopsis
= "End a While loop";
55 static char *continue_app
= "ContinueWhile";
56 static char *continue_desc
=
57 " ContinueWhile(): Returns to the top of the while loop and re-evaluates the conditional.\n";
58 static char *continue_synopsis
= "Restart a While loop";
63 static const char *get_index(struct ast_channel
*chan
, const char *prefix
, int index
) {
64 char varname
[VAR_SIZE
];
66 snprintf(varname
, VAR_SIZE
, "%s_%d", prefix
, index
);
67 return pbx_builtin_getvar_helper(chan
, varname
);
70 static struct ast_exten
*find_matching_priority(struct ast_context
*c
, const char *exten
, int priority
, const char *callerid
)
73 struct ast_include
*i
;
74 struct ast_context
*c2
;
76 for (e
=ast_walk_context_extensions(c
, NULL
); e
; e
=ast_walk_context_extensions(c
, e
)) {
77 if (ast_extension_match(ast_get_extension_name(e
), exten
)) {
78 int needmatch
= ast_get_extension_matchcid(e
);
79 if ((needmatch
&& ast_extension_match(ast_get_extension_cidmatch(e
), callerid
)) ||
81 /* This is the matching extension we want */
83 for (p
=ast_walk_extension_priorities(e
, NULL
); p
; p
=ast_walk_extension_priorities(e
, p
)) {
84 if (priority
!= ast_get_extension_priority(p
))
92 /* No match; run through includes */
93 for (i
=ast_walk_context_includes(c
, NULL
); i
; i
=ast_walk_context_includes(c
, i
)) {
94 for (c2
=ast_walk_contexts(NULL
); c2
; c2
=ast_walk_contexts(c2
)) {
95 if (!strcmp(ast_get_context_name(c2
), ast_get_include_name(i
))) {
96 e
= find_matching_priority(c2
, exten
, priority
, callerid
);
105 static int find_matching_endwhile(struct ast_channel
*chan
)
107 struct ast_context
*c
;
110 if (ast_rdlock_contexts()) {
111 ast_log(LOG_ERROR
, "Failed to lock contexts list\n");
115 for (c
=ast_walk_contexts(NULL
); c
; c
=ast_walk_contexts(c
)) {
118 if (!ast_rdlock_context(c
)) {
119 if (!strcmp(ast_get_context_name(c
), chan
->context
)) {
120 /* This is the matching context we want */
121 int cur_priority
= chan
->priority
+ 1, level
=1;
123 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
)) {
124 if (!strcasecmp(ast_get_extension_app(e
), "WHILE")) {
126 } else if (!strcasecmp(ast_get_extension_app(e
), "ENDWHILE")) {
136 ast_unlock_context(c
);
142 ast_unlock_contexts();
146 static int _while_exec(struct ast_channel
*chan
, void *data
, int end
)
149 const char *while_pri
= NULL
;
150 char *my_name
= NULL
;
151 const char *condition
= NULL
, *label
= NULL
;
152 char varname
[VAR_SIZE
], end_varname
[VAR_SIZE
];
153 const char *prefix
= "WHILE";
155 int used_index_i
= -1, x
=0;
156 char used_index
[VAR_SIZE
] = "0", new_index
[VAR_SIZE
] = "0";
163 /* dont want run away loops if the chan isn't even up
164 this is up for debate since it slows things down a tad ......
166 if (ast_waitfordigit(chan
,1) < 0)
170 if (get_index(chan
, prefix
, x
)) {
176 snprintf(used_index
, VAR_SIZE
, "%d", used_index_i
);
177 snprintf(new_index
, VAR_SIZE
, "%d", used_index_i
+ 1);
180 condition
= ast_strdupa(data
);
182 size
= strlen(chan
->context
) + strlen(chan
->exten
) + 32;
183 my_name
= alloca(size
);
184 memset(my_name
, 0, size
);
185 snprintf(my_name
, size
, "%s_%s_%d", chan
->context
, chan
->exten
, chan
->priority
);
187 ast_channel_lock(chan
);
190 } else if (!(label
= pbx_builtin_getvar_helper(chan
, my_name
))) {
192 pbx_builtin_setvar_helper(chan
, my_name
, label
);
194 snprintf(varname
, VAR_SIZE
, "%s_%s", prefix
, label
);
195 if ((while_pri
= pbx_builtin_getvar_helper(chan
, varname
)) && !end
) {
196 while_pri
= ast_strdupa(while_pri
);
197 snprintf(end_varname
,VAR_SIZE
,"END_%s",varname
);
199 ast_channel_unlock(chan
);
202 if ((!end
&& !pbx_checkcondition(condition
)) || (end
== 2)) {
203 /* Condition Met (clean up helper vars) */
204 const char *goto_str
;
205 pbx_builtin_setvar_helper(chan
, varname
, NULL
);
206 pbx_builtin_setvar_helper(chan
, my_name
, NULL
);
207 snprintf(end_varname
,VAR_SIZE
,"END_%s",varname
);
208 ast_channel_lock(chan
);
209 if ((goto_str
= pbx_builtin_getvar_helper(chan
, end_varname
))) {
210 ast_parseable_goto(chan
, goto_str
);
211 pbx_builtin_setvar_helper(chan
, end_varname
, NULL
);
213 int pri
= find_matching_endwhile(chan
);
215 ast_verb(3, "Jumping to priority %d\n", pri
);
216 chan
->priority
= pri
;
218 ast_log(LOG_WARNING
, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan
->context
, chan
->exten
, chan
->priority
);
221 ast_channel_unlock(chan
);
225 if (!end
&& !while_pri
) {
227 size
= strlen(chan
->context
) + strlen(chan
->exten
) + 32;
228 goto_str
= alloca(size
);
229 memset(goto_str
, 0, size
);
230 snprintf(goto_str
, size
, "%s,%s,%d", chan
->context
, chan
->exten
, chan
->priority
);
231 pbx_builtin_setvar_helper(chan
, varname
, goto_str
);
234 else if (end
&& while_pri
) {
236 snprintf(end_varname
, VAR_SIZE
, "END_%s", varname
);
237 if (! pbx_builtin_getvar_helper(chan
, end_varname
)) {
239 size
= strlen(chan
->context
) + strlen(chan
->exten
) + 32;
240 goto_str
= alloca(size
);
241 memset(goto_str
, 0, size
);
242 snprintf(goto_str
, size
, "%s,%s,%d", chan
->context
, chan
->exten
, chan
->priority
+1);
243 pbx_builtin_setvar_helper(chan
, end_varname
, goto_str
);
245 ast_parseable_goto(chan
, while_pri
);
251 static int while_start_exec(struct ast_channel
*chan
, void *data
) {
252 return _while_exec(chan
, data
, 0);
255 static int while_end_exec(struct ast_channel
*chan
, void *data
) {
256 return _while_exec(chan
, data
, 1);
259 static int while_exit_exec(struct ast_channel
*chan
, void *data
) {
260 return _while_exec(chan
, data
, 2);
263 static int while_continue_exec(struct ast_channel
*chan
, void *data
)
266 const char *prefix
= "WHILE", *while_pri
=NULL
;
269 const char *tmp
= get_index(chan
, prefix
, x
);
277 ast_parseable_goto(chan
, while_pri
);
282 static int unload_module(void)
286 res
= ast_unregister_application(start_app
);
287 res
|= ast_unregister_application(stop_app
);
288 res
|= ast_unregister_application(exit_app
);
289 res
|= ast_unregister_application(continue_app
);
294 static int load_module(void)
298 res
= ast_register_application(start_app
, while_start_exec
, start_synopsis
, start_desc
);
299 res
|= ast_register_application(stop_app
, while_end_exec
, stop_synopsis
, stop_desc
);
300 res
|= ast_register_application(exit_app
, while_exit_exec
, exit_synopsis
, exit_desc
);
301 res
|= ast_register_application(continue_app
, while_continue_exec
, continue_synopsis
, continue_desc
);
306 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "While Loops and Conditional Execution");