Start untangling header inclusion in a way that does not affect
[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/file.h"
33 #include "asterisk/logger.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/config.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/options.h"
41
42 static char *start_app = "While";
43 static char *start_desc = 
44 "  While(<expr>): Start a While Loop.  Execution will return to this\n"
45 "point when EndWhile() is called until expr is no longer true.\n";
46
47 static char *start_synopsis = "Start a while loop";
48
49
50 static char *stop_app = "EndWhile";
51 static char *stop_desc = 
52 "  EndWhile(): Return to the previous called While()\n";
53
54 static char *stop_synopsis = "End a while loop";
55
56 static char *exit_app = "ExitWhile";
57 static char *exit_desc =
58 "  ExitWhile(): Exits a While() loop, whether or not the conditional has been satisfied.\n";
59 static char *exit_synopsis = "End a While loop";
60
61 static char *continue_app = "ContinueWhile";
62 static char *continue_desc =
63 "  ContinueWhile(): Returns to the top of the while loop and re-evaluates the conditional.\n";
64 static char *continue_synopsis = "Restart a While loop";
65
66 #define VAR_SIZE 64
67
68
69 static const char *get_index(struct ast_channel *chan, const char *prefix, int index) {
70         char varname[VAR_SIZE];
71
72         snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
73         return pbx_builtin_getvar_helper(chan, varname);
74 }
75
76 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
77 {
78         struct ast_exten *e;
79         struct ast_include *i;
80         struct ast_context *c2;
81
82         for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
83                 if (ast_extension_match(ast_get_extension_name(e), exten)) {
84                         int needmatch = ast_get_extension_matchcid(e);
85                         if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
86                                 (!needmatch)) {
87                                 /* This is the matching extension we want */
88                                 struct ast_exten *p;
89                                 for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
90                                         if (priority != ast_get_extension_priority(p))
91                                                 continue;
92                                         return p;
93                                 }
94                         }
95                 }
96         }
97
98         /* No match; run through includes */
99         for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
100                 for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
101                         if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
102                                 e = find_matching_priority(c2, exten, priority, callerid);
103                                 if (e)
104                                         return e;
105                         }
106                 }
107         }
108         return NULL;
109 }
110
111 static int find_matching_endwhile(struct ast_channel *chan)
112 {
113         struct ast_context *c;
114         int res=-1;
115
116         if (ast_rdlock_contexts()) {
117                 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
118                 return -1;
119         }
120
121         for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
122                 struct ast_exten *e;
123
124                 if (!ast_rdlock_context(c)) {
125                         if (!strcmp(ast_get_context_name(c), chan->context)) {
126                                 /* This is the matching context we want */
127                                 int cur_priority = chan->priority + 1, level=1;
128
129                                 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)) {
130                                         if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
131                                                 level++;
132                                         } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
133                                                 level--;
134                                         }
135
136                                         if (level == 0) {
137                                                 res = cur_priority;
138                                                 break;
139                                         }
140                                 }
141                         }
142                         ast_unlock_context(c);
143                         if (res > 0) {
144                                 break;
145                         }
146                 }
147         }
148         ast_unlock_contexts();
149         return res;
150 }
151
152 static int _while_exec(struct ast_channel *chan, void *data, int end)
153 {
154         int res=0;
155         const char *while_pri = NULL;
156         char *my_name = NULL;
157         const char *condition = NULL, *label = NULL;
158         char varname[VAR_SIZE], end_varname[VAR_SIZE];
159         const char *prefix = "WHILE";
160         size_t size=0;
161         int used_index_i = -1, x=0;
162         char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
163
164         if (!chan) {
165                 /* huh ? */
166                 return -1;
167         }
168
169         /* dont want run away loops if the chan isn't even up
170            this is up for debate since it slows things down a tad ......
171         */
172         if (ast_waitfordigit(chan,1) < 0)
173                 return -1;
174
175         for (x=0;;x++) {
176                 if (get_index(chan, prefix, x)) {
177                         used_index_i = x;
178                 } else 
179                         break;
180         }
181         
182         snprintf(used_index, VAR_SIZE, "%d", used_index_i);
183         snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
184         
185         if (!end)
186                 condition = ast_strdupa(data);
187
188         size = strlen(chan->context) + strlen(chan->exten) + 32;
189         my_name = alloca(size);
190         memset(my_name, 0, size);
191         snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
192         
193         if (ast_strlen_zero(label)) {
194                 if (end) 
195                         label = used_index;
196                 else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
197                         label = new_index;
198                         pbx_builtin_setvar_helper(chan, my_name, label);
199                 }
200                 
201         }
202         
203         snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
204         while_pri = pbx_builtin_getvar_helper(chan, varname);
205         
206         if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
207                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
208         }
209         
210
211         if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
212                 /* Condition Met (clean up helper vars) */
213                 const char *goto_str;
214                 pbx_builtin_setvar_helper(chan, varname, NULL);
215                 pbx_builtin_setvar_helper(chan, my_name, NULL);
216                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
217                 if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
218                         ast_parseable_goto(chan, goto_str);
219                         pbx_builtin_setvar_helper(chan, end_varname, NULL);
220                 } else {
221                         int pri = find_matching_endwhile(chan);
222                         if (pri > 0) {
223                                 ast_verb(3, "Jumping to priority %d\n", pri);
224                                 chan->priority = pri;
225                         } else {
226                                 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
227                         }
228                 }
229                 return res;
230         }
231
232         if (!end && !while_pri) {
233                 char *goto_str;
234                 size = strlen(chan->context) + strlen(chan->exten) + 32;
235                 goto_str = alloca(size);
236                 memset(goto_str, 0, size);
237                 snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority);
238                 pbx_builtin_setvar_helper(chan, varname, goto_str);
239         } 
240
241         else if (end && while_pri) {
242                 /* END of loop */
243                 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
244                 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
245                         char *goto_str;
246                         size = strlen(chan->context) + strlen(chan->exten) + 32;
247                         goto_str = alloca(size);
248                         memset(goto_str, 0, size);
249                         snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority+1);
250                         pbx_builtin_setvar_helper(chan, end_varname, goto_str);
251                 }
252                 ast_parseable_goto(chan, while_pri);
253         }
254
255         return res;
256 }
257
258 static int while_start_exec(struct ast_channel *chan, void *data) {
259         return _while_exec(chan, data, 0);
260 }
261
262 static int while_end_exec(struct ast_channel *chan, void *data) {
263         return _while_exec(chan, data, 1);
264 }
265
266 static int while_exit_exec(struct ast_channel *chan, void *data) {
267         return _while_exec(chan, data, 2);
268 }
269
270 static int while_continue_exec(struct ast_channel *chan, void *data)
271 {
272         int x;
273         const char *prefix = "WHILE", *while_pri=NULL;
274
275         for (x = 0; ; x++) {
276                 const char *tmp = get_index(chan, prefix, x);
277                 if (tmp)
278                         while_pri = tmp;
279                 else
280                         break;
281         }
282
283         if (while_pri)
284                 ast_parseable_goto(chan, while_pri);
285
286         return 0;
287 }
288
289 static int unload_module(void)
290 {
291         int res;
292         
293         res = ast_unregister_application(start_app);
294         res |= ast_unregister_application(stop_app);
295         res |= ast_unregister_application(exit_app);
296         res |= ast_unregister_application(continue_app);
297
298         return res;
299 }
300
301 static int load_module(void)
302 {
303         int res;
304
305         res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
306         res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
307         res |= ast_register_application(exit_app, while_exit_exec, exit_synopsis, exit_desc);
308         res |= ast_register_application(continue_app, while_continue_exec, continue_synopsis, continue_desc);
309
310         return res;
311 }
312
313 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");