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