Debian Codegraph @

apps/app_while.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright 2004 - 2005, Anthony Minessale <[email protected]>
5 *
6 * Anthony Minessale <[email protected]>
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
19 /*! \file
20 *
21 * \brief While Loop Implementation
22 *
23 * \author Anthony Minessale <[email protected]>
24 *
25 * \ingroup applications
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37
38 /*** DOCUMENTATION
39 <application name="While" language="en_US">
40 <since>
41 <version>1.2.0</version>
42 </since>
43 <synopsis>
44 Start a while loop.
45 </synopsis>
46 <syntax>
47 <parameter name="expr" required="true" />
48 </syntax>
49 <description>
50 <para>Start a While Loop. Execution will return to this point when
51 <literal>EndWhile()</literal> is called until expr is no longer true.</para>
52 </description>
53 <see-also>
54 <ref type="application">EndWhile</ref>
55 <ref type="application">ExitWhile</ref>
56 <ref type="application">ContinueWhile</ref>
57 </see-also>
58 </application>
59 <application name="EndWhile" language="en_US">
60 <since>
61 <version>1.2.0</version>
62 </since>
63 <synopsis>
64 End a while loop.
65 </synopsis>
66 <syntax />
67 <description>
68 <para>Return to the previous called <literal>While()</literal>.</para>
69 </description>
70 <see-also>
71 <ref type="application">While</ref>
72 <ref type="application">ExitWhile</ref>
73 <ref type="application">ContinueWhile</ref>
74 </see-also>
75 </application>
76 <application name="ExitWhile" language="en_US">
77 <since>
78 <version>1.4.0</version>
79 </since>
80 <synopsis>
81 End a While loop.
82 </synopsis>
83 <syntax />
84 <description>
85 <para>Exits a <literal>While()</literal> loop, whether or not the conditional has been satisfied.</para>
86 </description>
87 <see-also>
88 <ref type="application">While</ref>
89 <ref type="application">EndWhile</ref>
90 <ref type="application">ContinueWhile</ref>
91 </see-also>
92 </application>
93 <application name="ContinueWhile" language="en_US">
94 <since>
95 <version>1.4.0</version>
96 </since>
97 <synopsis>
98 Restart a While loop.
99 </synopsis>
100 <syntax />
101 <description>
102 <para>Returns to the top of the while loop and re-evaluates the conditional.</para>
103 </description>
104 <see-also>
105 <ref type="application">While</ref>
106 <ref type="application">EndWhile</ref>
107 <ref type="application">ExitWhile</ref>
108 </see-also>
109 </application>
110 ***/
111
112 static char *start_app = "While";
113 static char *stop_app = "EndWhile";
114 static char *exit_app = "ExitWhile";
115 static char *continue_app = "ContinueWhile";
116
117 #define VAR_SIZE 64
118
119
120 static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) {
121 char varname[VAR_SIZE];
122
123 snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx);
124 return pbx_builtin_getvar_helper(chan, varname);
125 }
126
127 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
128 {
129 struct ast_exten *e;
130 struct ast_context *c2;
131 int idx;
132
133 for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
134 if (ast_extension_match(ast_get_extension_name(e), exten)) {
135 int needmatch = ast_get_extension_matchcid(e);
136 if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
137 (!needmatch)) {
138 /* This is the matching extension we want */
139 struct ast_exten *p;
140 for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
141 if (priority != ast_get_extension_priority(p))
142 continue;
143 return p;
144 }
145 }
146 }
147 }
148
149 /* No match; run through includes */
150 for (idx = 0; idx < ast_context_includes_count(c); idx++) {
151 const struct ast_include *i = ast_context_includes_get(c, idx);
152
153 for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
154 if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
155 e = find_matching_priority(c2, exten, priority, callerid);
156 if (e)
157 return e;
158 }
159 }
160 }
161 return NULL;
162 }
163
164 static int find_matching_endwhile(struct ast_channel *chan)
165 {
166 struct ast_context *c;
167 int res=-1;
168
169 if (ast_rdlock_contexts()) {
170 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
171 return -1;
172 }
173
174 for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
175 struct ast_exten *e;
176
177 if (!ast_rdlock_context(c)) {
178 if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) {
179 /* This is the matching context we want */
180 int cur_priority = ast_channel_priority(chan) + 1, level=1;
181
182 for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority,
183 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
184 e;
185 e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority,
186 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
187 if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
188 level++;
189 } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
190 level--;
191 }
192
193 if (level == 0) {
194 res = cur_priority;
195 break;
196 }
197 }
198 }
199 ast_unlock_context(c);
200 if (res > 0) {
201 break;
202 }
203 }
204 }
205 ast_unlock_contexts();
206 return res;
207 }
208
209 static int _while_exec(struct ast_channel *chan, const char *data, int end)
210 {
211 int res=0;
212 const char *while_pri = NULL;
213 char *my_name = NULL;
214 const char *condition = NULL, *label = NULL;
215 char varname[VAR_SIZE], end_varname[VAR_SIZE];
216 const char *prefix = "WHILE";
217 size_t size=0;
218 int used_index_i = -1, x=0;
219 char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
220
221 if (!chan) {
222 /* huh ? */
223 return -1;
224 }
225
226 #if 0
227 /* don't want run away loops if the chan isn't even up
228 this is up for debate since it slows things down a tad ......
229
230 Debate is over... this prevents While/EndWhile from working
231 within the "h" extension. Not good.
232 */
233 if (ast_waitfordigit(chan,1) < 0)
234 return -1;
235 #endif
236
237 for (x=0;;x++) {
238 if (get_index(chan, prefix, x)) {
239 used_index_i = x;
240 } else
241 break;
242 }
243
244 snprintf(used_index, VAR_SIZE, "%d", used_index_i);
245 snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
246
247 if (!end)
248 condition = ast_strdupa(data);
249
250 size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
251 my_name = ast_alloca(size);
252 memset(my_name, 0, size);
253 snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
254
255 ast_channel_lock(chan);
256 if (end) {
257 label = used_index;
258 } else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
259 label = new_index;
260 pbx_builtin_setvar_helper(chan, my_name, label);
261 }
262 snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
263 if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
264 while_pri = ast_strdupa(while_pri);
265 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
266 }
267 ast_channel_unlock(chan);
268
269
270 if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
271 /* Condition Met (clean up helper vars) */
272 const char *goto_str;
273 pbx_builtin_setvar_helper(chan, varname, NULL);
274 pbx_builtin_setvar_helper(chan, my_name, NULL);
275 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
276 ast_channel_lock(chan);
277 if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) {
278 ast_parseable_goto(chan, goto_str);
279 pbx_builtin_setvar_helper(chan, end_varname, NULL);
280 } else {
281 int pri = find_matching_endwhile(chan);
282 if (pri > 0) {
283 ast_verb(3, "Jumping to priority %d\n", pri);
284 ast_channel_priority_set(chan, pri);
285 } else {
286 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
287 }
288 }
289 ast_channel_unlock(chan);
290 return res;
291 }
292
293 if (!end && !while_pri) {
294 char *goto_str;
295 size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
296 goto_str = ast_alloca(size);
297 memset(goto_str, 0, size);
298 snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
299 pbx_builtin_setvar_helper(chan, varname, goto_str);
300 }
301
302 else if (end && while_pri) {
303 /* END of loop */
304 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
305 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
306 char *goto_str;
307 size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
308 goto_str = ast_alloca(size);
309 memset(goto_str, 0, size);
310 snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1);
311 pbx_builtin_setvar_helper(chan, end_varname, goto_str);
312 }
313 ast_parseable_goto(chan, while_pri);
314 }
315
316 return res;
317 }
318
319 static int while_start_exec(struct ast_channel *chan, const char *data) {
320 return _while_exec(chan, data, 0);
321 }
322
323 static int while_end_exec(struct ast_channel *chan, const char *data) {
324 return _while_exec(chan, data, 1);
325 }
326
327 static int while_exit_exec(struct ast_channel *chan, const char *data) {
328 return _while_exec(chan, data, 2);
329 }
330
331 static int while_continue_exec(struct ast_channel *chan, const char *data)
332 {
333 int x;
334 const char *prefix = "WHILE", *while_pri=NULL;
335
336 for (x = 0; ; x++) {
337 const char *tmp = get_index(chan, prefix, x);
338 if (tmp)
339 while_pri = tmp;
340 else
341 break;
342 }
343
344 if (while_pri)
345 ast_parseable_goto(chan, while_pri);
346
347 return 0;
348 }
349
350 static int unload_module(void)
351 {
352 int res;
353
354 res = ast_unregister_application(start_app);
355 res |= ast_unregister_application(stop_app);
356 res |= ast_unregister_application(exit_app);
357 res |= ast_unregister_application(continue_app);
358
359 return res;
360 }
361
362 static int load_module(void)
363 {
364 int res;
365
366 res = ast_register_application_xml(start_app, while_start_exec);
367 res |= ast_register_application_xml(stop_app, while_end_exec);
368 res |= ast_register_application_xml(exit_app, while_exit_exec);
369 res |= ast_register_application_xml(continue_app, while_continue_exec);
370
371 return res;
372 }
373
374 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");