e9dde83ade63e020c0d832bc34421c6357c8151e
[asterisk/asterisk.git] / pbx / pbx_ael.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * Steve Murphy <murf@parsetree.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 Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
22  * 
23  */
24
25 #include "asterisk.h"
26
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28
29 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <regex.h>
37 #include <sys/stat.h>
38
39 #include "asterisk/pbx.h"
40 #include "asterisk/config.h"
41 #include "asterisk/module.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/app.h"
45 #include "asterisk/callerid.h"
46 #include "asterisk/ael_structs.h"
47 #ifdef AAL_ARGCHECK
48 #include "asterisk/argdesc.h"
49 #endif
50
51 static char expr_output[2096];
52
53 /* these functions are in ../ast_expr2.fl */
54
55 #define DEBUG_READ   (1 << 0)
56 #define DEBUG_TOKENS (1 << 1)
57 #define DEBUG_MACROS (1 << 2)
58 #define DEBUG_CONTEXTS (1 << 3)
59
60 static char *config = "extensions.ael";
61 static char *registrar = "pbx_ael";
62 static int pbx_load_module(void);
63
64 static int errs, warns;
65 static int notes;
66
67 #ifndef AAL_ARGCHECK
68 /* for the time being, short circuit all the AAL related structures
69    without permanently removing the code; after/during the AAL 
70    development, this code can be properly re-instated 
71 */
72
73 /*! \brief null definitions for structs passed down the infrastructure */
74 struct argapp
75 {
76         struct argapp *next;
77 };
78
79 #endif
80
81 #ifdef AAL_ARGCHECK
82 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
83 int option_matches( struct argdesc *should, pval *is, struct argapp *app);
84 int ael_is_funcname(char *name);
85 #endif
86
87 int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
88 void check_pval(pval *item, struct argapp *apps, int in_globals);
89 void check_pval_item(pval *item, struct argapp *apps, int in_globals);
90 void check_switch_expr(pval *item, struct argapp *apps);
91 void ast_expr_register_extra_error_info(char *errmsg);
92 void ast_expr_clear_extra_error_info(void);
93 int  ast_expr(char *expr, char *buf, int length,struct ast_channel *chan);
94 struct pval *find_macro(char *name);
95 struct pval *find_context(char *name);
96 struct pval *find_context(char *name);
97 struct pval *find_macro(char *name);
98 struct ael_priority *new_prio(void);
99 struct ael_extension *new_exten(void);
100 void linkprio(struct ael_extension *exten, struct ael_priority *prio);
101 void destroy_extensions(struct ael_extension *exten);
102 static void linkexten(struct ael_extension *exten, struct ael_extension *add);
103 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *context );
104 void set_priorities(struct ael_extension *exten);
105 void add_extensions(struct ael_extension *exten);
106 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root);
107 void destroy_pval(pval *item);
108 void destroy_pval_item(pval *item);
109 int is_float(char *arg );
110 int is_int(char *arg );
111 int is_empty(char *arg);
112 static pval *current_db;
113 static pval *current_context;
114 static pval *current_extension;
115
116 static const char *match_context;
117 static const char *match_exten;
118 static const char *match_label;
119 static int in_abstract_context;
120 static int count_labels; /* true, put matcher in label counting mode */
121 static int label_count;  /* labels are only meant to be counted in a context or exten */
122 static int return_on_context_match;
123 static pval *last_matched_label;
124 struct pval *match_pval(pval *item);
125 static void check_timerange(pval *p);
126 static void check_dow(pval *DOW);
127 static void check_day(pval *DAY);
128 static void check_month(pval *MON);
129 static void check_expr2_input(pval *expr, char *str);
130 static int extension_matches(pval *here, const char *exten, const char *pattern);
131 static void check_goto(pval *item);
132 static void find_pval_goto_item(pval *item, int lev);
133 static void find_pval_gotos(pval *item, int lev);
134 static int check_break(pval *item);
135 static int check_continue(pval *item);
136 static void check_label(pval *item);
137 static void check_macro_returns(pval *macro);
138
139 static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
140 static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
141 static void print_pval_list(FILE *fin, pval *item, int depth);
142
143 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
144 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
145 static pval *get_goto_target(pval *item);
146 static int label_inside_case(pval *label);
147 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
148 static void fix_gotos_in_extensions(struct ael_extension *exten);
149 static pval *get_extension_or_contxt(pval *p);
150 static pval *get_contxt(pval *p);
151 static void remove_spaces_before_equals(char *str);
152 /* static void substitute_commas(char *str); */
153 #ifdef NOMORE
154 /*! \brief I am adding this code to substitute commas with vertbars in the args to apps */
155 static void substitute_commas(char *str)
156 {
157         char *p = str;
158         
159         while (p && *p)
160         {
161                 if (*p == ',' && ((p != str && *(p-1) != '\\')
162                                 || p == str))
163                         *p = '|';
164                 if (*p == '\\' && *(p+1) == ',') { /* learning experience: the '\,' is turned into just ',' by pbx_config; So we need to do the same */
165                         char *q = p;
166                         while (*q) {  /* move the ',' and everything after it up 1 char */
167                                 *q = *(q+1);
168                                 q++;
169                         }
170                 }
171                 p++;
172         }
173 }
174 #endif
175
176 /* PRETTY PRINTER FOR AEL:  ============================================================================= */
177
178 static void print_pval(FILE *fin, pval *item, int depth)
179 {
180         int i;
181         pval *lp;
182         
183         for (i=0; i<depth; i++) {
184                 fprintf(fin, "\t"); /* depth == indentation */
185         }
186         
187         switch ( item->type ) {
188         case PV_WORD:
189                 fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
190                 break;
191                 
192         case PV_MACRO:
193                 fprintf(fin,"macro %s(", item->u1.str);
194                 for (lp=item->u2.arglist; lp; lp=lp->next) {
195                         if (lp != item->u2.arglist )
196                                 fprintf(fin,", ");
197                         fprintf(fin,"%s", lp->u1.str);
198                 }
199                 fprintf(fin,") {\n");
200                 print_pval_list(fin,item->u3.macro_statements,depth+1);
201                 for (i=0; i<depth; i++) {
202                         fprintf(fin,"\t"); /* depth == indentation */
203                 }
204                 fprintf(fin,"};\n\n");
205                 break;
206                         
207         case PV_CONTEXT:
208                 if ( item->u3.abstract )
209                         fprintf(fin,"abstract context %s {\n", item->u1.str);
210                 else
211                         fprintf(fin,"context %s {\n", item->u1.str);
212                 print_pval_list(fin,item->u2.statements,depth+1);
213                 for (i=0; i<depth; i++) {
214                         fprintf(fin,"\t"); /* depth == indentation */
215                 }
216                 fprintf(fin,"};\n\n");
217                 break;
218                         
219         case PV_MACRO_CALL:
220                 fprintf(fin,"&%s(", item->u1.str);
221                 for (lp=item->u2.arglist; lp; lp=lp->next) {
222                         if ( lp != item->u2.arglist )
223                                 fprintf(fin,", ");
224                         fprintf(fin,"%s", lp->u1.str);
225                 }
226                 fprintf(fin,");\n");
227                 break;
228                         
229         case PV_APPLICATION_CALL:
230                 fprintf(fin,"%s(", item->u1.str);
231                 for (lp=item->u2.arglist; lp; lp=lp->next) {
232                         if ( lp != item->u2.arglist )
233                                 fprintf(fin,",");
234                         fprintf(fin,"%s", lp->u1.str);
235                 }
236                 fprintf(fin,");\n");
237                 break;
238                         
239         case PV_CASE:
240                 fprintf(fin,"case %s:\n", item->u1.str);
241                 print_pval_list(fin,item->u2.statements, depth+1);
242                 break;
243                         
244         case PV_PATTERN:
245                 fprintf(fin,"pattern %s:\n", item->u1.str);
246                 print_pval_list(fin,item->u2.statements, depth+1);
247                 break;
248                         
249         case PV_DEFAULT:
250                 fprintf(fin,"default:\n");
251                 print_pval_list(fin,item->u2.statements, depth+1);
252                 break;
253                         
254         case PV_CATCH:
255                 fprintf(fin,"catch %s {\n", item->u1.str);
256                 print_pval_list(fin,item->u2.statements, depth+1);
257                 for (i=0; i<depth; i++) {
258                         fprintf(fin,"\t"); /* depth == indentation */
259                 }
260                 fprintf(fin,"};\n");
261                 break;
262                         
263         case PV_SWITCHES:
264                 fprintf(fin,"switches {\n");
265                 print_pval_list(fin,item->u1.list,depth+1);
266                 for (i=0; i<depth; i++) {
267                         fprintf(fin,"\t"); /* depth == indentation */
268                 }
269                 fprintf(fin,"};\n");
270                 break;
271                         
272         case PV_ESWITCHES:
273                 fprintf(fin,"eswitches {\n");
274                 print_pval_list(fin,item->u1.list,depth+1);
275                 for (i=0; i<depth; i++) {
276                         fprintf(fin,"\t"); /* depth == indentation */
277                 }
278                 fprintf(fin,"};\n");
279                 break;
280                         
281         case PV_INCLUDES:
282                 fprintf(fin,"includes {\n");
283                 for (lp=item->u1.list; lp; lp=lp->next) {
284                         for (i=0; i<depth+1; i++) {
285                                 fprintf(fin,"\t"); /* depth == indentation */
286                         }
287                         fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
288                         if ( lp->u2.arglist )
289                                 fprintf(fin,"|%s|%s|%s|%s", 
290                                                 lp->u2.arglist->u1.str,
291                                                 lp->u2.arglist->next->u1.str,
292                                                 lp->u2.arglist->next->next->u1.str,
293                                                 lp->u2.arglist->next->next->next->u1.str
294                                         );
295                         fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
296                 }
297                 
298                 print_pval_list(fin,item->u1.list,depth+1);
299                 for (i=0; i<depth; i++) {
300                         fprintf(fin,"\t"); /* depth == indentation */
301                 }
302                 fprintf(fin,"};\n");
303                 break;
304                         
305         case PV_STATEMENTBLOCK:
306                 fprintf(fin,"{\n");
307                 print_pval_list(fin,item->u1.list, depth+1);
308                 for (i=0; i<depth; i++) {
309                         fprintf(fin,"\t"); /* depth == indentation */
310                 }
311                 fprintf(fin,"};\n");
312                 break;
313                         
314         case PV_VARDEC:
315                 fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
316                 break;
317                         
318         case PV_LOCALVARDEC:
319                 fprintf(fin,"local %s=%s;\n", item->u1.str, item->u2.val);
320                 break;
321                         
322         case PV_GOTO:
323                 fprintf(fin,"goto %s", item->u1.list->u1.str);
324                 if ( item->u1.list->next )
325                         fprintf(fin,",%s", item->u1.list->next->u1.str);
326                 if ( item->u1.list->next && item->u1.list->next->next )
327                         fprintf(fin,",%s", item->u1.list->next->next->u1.str);
328                 fprintf(fin,"\n");
329                 break;
330                         
331         case PV_LABEL:
332                 fprintf(fin,"%s:\n", item->u1.str);
333                 break;
334                         
335         case PV_FOR:
336                 fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
337                 print_pval_list(fin,item->u4.for_statements,depth+1);
338                 break;
339                         
340         case PV_WHILE:
341                 fprintf(fin,"while (%s)\n", item->u1.str);
342                 print_pval_list(fin,item->u2.statements,depth+1);
343                 break;
344                         
345         case PV_BREAK:
346                 fprintf(fin,"break;\n");
347                 break;
348                         
349         case PV_RETURN:
350                 fprintf(fin,"return;\n");
351                 break;
352                         
353         case PV_CONTINUE:
354                 fprintf(fin,"continue;\n");
355                 break;
356                         
357         case PV_RANDOM:
358         case PV_IFTIME:
359         case PV_IF:
360                 if ( item->type == PV_IFTIME ) {
361                         
362                         fprintf(fin,"ifTime ( %s|%s|%s|%s )\n", 
363                                         item->u1.list->u1.str, 
364                                         item->u1.list->next->u1.str, 
365                                         item->u1.list->next->next->u1.str, 
366                                         item->u1.list->next->next->next->u1.str
367                                         );
368                 } else if ( item->type == PV_RANDOM ) {
369                         fprintf(fin,"random ( %s )\n", item->u1.str );
370                 } else
371                         fprintf(fin,"if ( %s )\n", item->u1.str);
372                 if ( item->u2.statements && item->u2.statements->next ) {
373                         for (i=0; i<depth; i++) {
374                                 fprintf(fin,"\t"); /* depth == indentation */
375                         }
376                         fprintf(fin,"{\n");
377                         print_pval_list(fin,item->u2.statements,depth+1);
378                         for (i=0; i<depth; i++) {
379                                 fprintf(fin,"\t"); /* depth == indentation */
380                         }
381                         if ( item->u3.else_statements )
382                                 fprintf(fin,"}\n");
383                         else
384                                 fprintf(fin,"};\n");
385                 } else if (item->u2.statements ) {
386                         print_pval_list(fin,item->u2.statements,depth+1);
387                 } else {
388                         if (item->u3.else_statements )
389                                 fprintf(fin, " {} ");
390                         else
391                                 fprintf(fin, " {}; ");
392                 }
393                 if ( item->u3.else_statements ) {
394                         for (i=0; i<depth; i++) {
395                                 fprintf(fin,"\t"); /* depth == indentation */
396                         }
397                         fprintf(fin,"else\n");
398                         print_pval_list(fin,item->u3.else_statements, depth);
399                 }
400                 break;
401                         
402         case PV_SWITCH:
403                 fprintf(fin,"switch( %s ) {\n", item->u1.str);
404                 print_pval_list(fin,item->u2.statements,depth+1);
405                 for (i=0; i<depth; i++) {
406                         fprintf(fin,"\t"); /* depth == indentation */
407                 }
408                 fprintf(fin,"}\n");
409                 break;
410                         
411         case PV_EXTENSION:
412                 if ( item->u4.regexten )
413                         fprintf(fin, "regexten ");
414                 if ( item->u3.hints )
415                         fprintf(fin,"hints(%s) ", item->u3.hints);
416                 
417                 fprintf(fin,"%s => \n", item->u1.str);
418                 print_pval_list(fin,item->u2.statements,depth+1);
419                 break;
420                         
421         case PV_IGNOREPAT:
422                 fprintf(fin,"ignorepat => %s\n", item->u1.str);
423                 break;
424                         
425         case PV_GLOBALS:
426                 fprintf(fin,"globals {\n");
427                 print_pval_list(fin,item->u1.statements,depth+1);
428                 for (i=0; i<depth; i++) {
429                         fprintf(fin,"\t"); /* depth == indentation */
430                 }
431                 fprintf(fin,"}\n");
432                 break;
433         }
434 }
435
436 static void print_pval_list(FILE *fin, pval *item, int depth)
437 {
438         pval *i;
439         
440         for (i=item; i; i=i->next) {
441                 print_pval(fin, i, depth);
442         }
443 }
444
445 #if 0
446 static void ael2_print(char *fname, pval *tree)
447 {
448         FILE *fin = fopen(fname,"w");
449         if ( !fin ) {
450                 ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
451                 return;
452         }
453         print_pval_list(fin, tree, 0);
454         fclose(fin);
455 }
456 #endif
457
458
459 /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL:  ============================================================================= */
460
461 void traverse_pval_template(pval *item, int depth);
462 void traverse_pval_item_template(pval *item, int depth);
463
464
465 void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
466                                                                                                                   but you may not need it */
467 {
468         pval *lp;
469         
470         switch ( item->type ) {
471         case PV_WORD:
472                 /* fields: item->u1.str == string associated with this (word). */
473                 break;
474                 
475         case PV_MACRO:
476                 /* fields: item->u1.str     == name of macro
477                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
478                                    item->u2.arglist->u1.str  == argument
479                                    item->u2.arglist->next   == next arg
480
481                                    item->u3.macro_statements == pval list of statements in macro body.
482                 */
483                 for (lp=item->u2.arglist; lp; lp=lp->next) {
484                 
485                 }
486                 traverse_pval_item_template(item->u3.macro_statements,depth+1);
487                 break;
488                         
489         case PV_CONTEXT:
490                 /* fields: item->u1.str     == name of context
491                            item->u2.statements == pval list of statements in context body
492                                    item->u3.abstract == int 1 if an abstract keyword were present
493                 */
494                 traverse_pval_item_template(item->u2.statements,depth+1);
495                 break;
496                         
497         case PV_MACRO_CALL:
498                 /* fields: item->u1.str     == name of macro to call
499                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
500                                    item->u2.arglist->u1.str  == argument
501                                    item->u2.arglist->next   == next arg
502                 */
503                 for (lp=item->u2.arglist; lp; lp=lp->next) {
504                 }
505                 break;
506                         
507         case PV_APPLICATION_CALL:
508                 /* fields: item->u1.str     == name of application to call
509                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
510                                    item->u2.arglist->u1.str  == argument
511                                    item->u2.arglist->next   == next arg
512                 */
513                 for (lp=item->u2.arglist; lp; lp=lp->next) {
514                 }
515                 break;
516                         
517         case PV_CASE:
518                 /* fields: item->u1.str     == value of case
519                            item->u2.statements == pval list of statements under the case
520                 */
521                 traverse_pval_item_template(item->u2.statements,depth+1);
522                 break;
523                         
524         case PV_PATTERN:
525                 /* fields: item->u1.str     == value of case
526                            item->u2.statements == pval list of statements under the case
527                 */
528                 traverse_pval_item_template(item->u2.statements,depth+1);
529                 break;
530                         
531         case PV_DEFAULT:
532                 /* fields: 
533                            item->u2.statements == pval list of statements under the case
534                 */
535                 traverse_pval_item_template(item->u2.statements,depth+1);
536                 break;
537                         
538         case PV_CATCH:
539                 /* fields: item->u1.str     == name of extension to catch
540                            item->u2.statements == pval list of statements in context body
541                 */
542                 traverse_pval_item_template(item->u2.statements,depth+1);
543                 break;
544                         
545         case PV_SWITCHES:
546                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
547                 */
548                 traverse_pval_item_template(item->u1.list,depth+1);
549                 break;
550                         
551         case PV_ESWITCHES:
552                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
553                 */
554                 traverse_pval_item_template(item->u1.list,depth+1);
555                 break;
556                         
557         case PV_INCLUDES:
558                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
559                            item->u2.arglist  == pval list of 4 PV_WORD elements for time values
560                 */
561                 traverse_pval_item_template(item->u1.list,depth+1);
562                 traverse_pval_item_template(item->u2.arglist,depth+1);
563                 break;
564                         
565         case PV_STATEMENTBLOCK:
566                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
567                 */
568                 traverse_pval_item_template(item->u1.list,depth+1);
569                 break;
570                         
571         case PV_VARDEC:
572         case PV_LOCALVARDEC:
573                 /* fields: item->u1.str     == variable name
574                            item->u2.val     == variable value to assign
575                 */
576                 break;
577                         
578         case PV_GOTO:
579                 /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
580                            item->u1.list->u1.str  == where the data on a PV_WORD will always be.
581                 */
582                 
583                 if ( item->u1.list->next )
584                         ;
585                 if ( item->u1.list->next && item->u1.list->next->next )
586                         ;
587                 
588                 break;
589                         
590         case PV_LABEL:
591                 /* fields: item->u1.str     == label name
592                 */
593                 break;
594                         
595         case PV_FOR:
596                 /* fields: item->u1.for_init     == a string containing the initalizer
597                            item->u2.for_test     == a string containing the loop test
598                            item->u3.for_inc      == a string containing the loop increment
599
600                                    item->u4.for_statements == a pval list of statements in the for ()
601                 */
602                 traverse_pval_item_template(item->u4.for_statements,depth+1);
603                 break;
604                         
605         case PV_WHILE:
606                 /* fields: item->u1.str        == the while conditional, as supplied by user
607
608                                    item->u2.statements == a pval list of statements in the while ()
609                 */
610                 traverse_pval_item_template(item->u2.statements,depth+1);
611                 break;
612                         
613         case PV_BREAK:
614                 /* fields: none
615                 */
616                 break;
617                         
618         case PV_RETURN:
619                 /* fields: none
620                 */
621                 break;
622                         
623         case PV_CONTINUE:
624                 /* fields: none
625                 */
626                 break;
627                         
628         case PV_IFTIME:
629                 /* fields: item->u1.list        == there are 4 linked PV_WORDs here.
630
631                                    item->u2.statements == a pval list of statements in the if ()
632                                    item->u3.else_statements == a pval list of statements in the else
633                                                                                            (could be zero)
634                 */
635                 traverse_pval_item_template(item->u2.statements,depth+1);
636                 if ( item->u3.else_statements ) {
637                         traverse_pval_item_template(item->u3.else_statements,depth+1);
638                 }
639                 break;
640                         
641         case PV_RANDOM:
642                 /* fields: item->u1.str        == the random number expression, as supplied by user
643
644                                    item->u2.statements == a pval list of statements in the if ()
645                                    item->u3.else_statements == a pval list of statements in the else
646                                                                                            (could be zero)
647                 */
648                 traverse_pval_item_template(item->u2.statements,depth+1);
649                 if ( item->u3.else_statements ) {
650                         traverse_pval_item_template(item->u3.else_statements,depth+1);
651                 }
652                 break;
653                         
654         case PV_IF:
655                 /* fields: item->u1.str        == the if conditional, as supplied by user
656
657                                    item->u2.statements == a pval list of statements in the if ()
658                                    item->u3.else_statements == a pval list of statements in the else
659                                                                                            (could be zero)
660                 */
661                 traverse_pval_item_template(item->u2.statements,depth+1);
662                 if ( item->u3.else_statements ) {
663                         traverse_pval_item_template(item->u3.else_statements,depth+1);
664                 }
665                 break;
666                         
667         case PV_SWITCH:
668                 /* fields: item->u1.str        == the switch expression
669
670                                    item->u2.statements == a pval list of statements in the switch, 
671                                                                                         (will be case statements, most likely!)
672                 */
673                 traverse_pval_item_template(item->u2.statements,depth+1);
674                 break;
675                         
676         case PV_EXTENSION:
677                 /* fields: item->u1.str        == the extension name, label, whatever it's called
678
679                                    item->u2.statements == a pval list of statements in the extension
680                                    item->u3.hints      == a char * hint argument
681                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
682                 */
683                 traverse_pval_item_template(item->u2.statements,depth+1);
684                 break;
685                         
686         case PV_IGNOREPAT:
687                 /* fields: item->u1.str        == the ignorepat data
688                 */
689                 break;
690                         
691         case PV_GLOBALS:
692                 /* fields: item->u1.statements     == pval list of statements, usually vardecs
693                 */
694                 traverse_pval_item_template(item->u1.statements,depth+1);
695                 break;
696         }
697 }
698
699 void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
700                                                                                                           but you may not need it */
701 {
702         pval *i;
703         
704         for (i=item; i; i=i->next) {
705                 traverse_pval_item_template(i, depth);
706         }
707 }
708
709
710 /* SEMANTIC CHECKING FOR AEL:  ============================================================================= */
711
712 /*   (not all that is syntactically legal is good! */
713
714
715 static void check_macro_returns(pval *macro)
716 {
717         pval *i;
718         if (!macro->u3.macro_statements)
719         {
720                 pval *z = calloc(1, sizeof(struct pval));
721                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s is empty! I will insert a return.\n",
722                                 macro->filename, macro->startline, macro->endline, macro->u1.str);
723
724                 z->type = PV_RETURN;
725                 z->startline = macro->startline;
726                 z->endline = macro->endline;
727                 z->startcol = macro->startcol;
728                 z->endcol = macro->endcol;
729                 z->filename = strdup(macro->filename);
730
731                 macro->u3.macro_statements = z;
732                 return;
733         }
734         for (i=macro->u3.macro_statements; i; i=i->next) {
735                 /* if the last statement in the list is not return, then insert a return there */
736                 if (i->next == NULL) {
737                         if (i->type != PV_RETURN) {
738                                 pval *z = calloc(1, sizeof(struct pval));
739                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s does not end with a return; I will insert one.\n",
740                                                 macro->filename, macro->startline, macro->endline, macro->u1.str);
741
742                                 z->type = PV_RETURN;
743                                 z->startline = macro->startline;
744                                 z->endline = macro->endline;
745                                 z->startcol = macro->startcol;
746                                 z->endcol = macro->endcol;
747                                 z->filename = strdup(macro->filename);
748
749                                 i->next = z;
750                                 return;
751                         }
752                 }
753         }
754         return;
755 }
756
757
758
759 static int extension_matches(pval *here, const char *exten, const char *pattern)
760 {
761         int err1;
762         regex_t preg;
763         
764         /* simple case, they match exactly, the pattern and exten name */
765         if (!strcmp(pattern,exten) == 0)
766                 return 1;
767         
768         if (pattern[0] == '_') {
769                 char reg1[2000];
770                 const char *p;
771                 char *r = reg1;
772                 
773                 if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
774                         ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
775                                         pattern);
776                         return 0;
777                 }
778                 /* form a regular expression from the pattern, and then match it against exten */
779                 *r++ = '^'; /* what if the extension is a pattern ?? */
780                 *r++ = '_'; /* what if the extension is a pattern ?? */
781                 *r++ = '?';
782                 for (p=pattern+1; *p; p++) {
783                         switch ( *p ) {
784                         case 'X':
785                                 *r++ = '[';
786                                 *r++ = '0';
787                                 *r++ = '-';
788                                 *r++ = '9';
789                                 *r++ = 'X';
790                                 *r++ = ']';
791                                 break;
792                                 
793                         case 'Z':
794                                 *r++ = '[';
795                                 *r++ = '1';
796                                 *r++ = '-';
797                                 *r++ = '9';
798                                 *r++ = 'Z';
799                                 *r++ = ']';
800                                 break;
801                                 
802                         case 'N':
803                                 *r++ = '[';
804                                 *r++ = '2';
805                                 *r++ = '-';
806                                 *r++ = '9';
807                                 *r++ = 'N';
808                                 *r++ = ']';
809                                 break;
810                                 
811                         case '[':
812                                 while ( *p && *p != ']' ) {
813                                         *r++ = *p++;
814                                 }
815                                 if ( *p != ']') {
816                                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
817                                                         here->filename, here->startline, here->endline, pattern);
818                                 }
819                                 break;
820                                 
821                         case '.':
822                         case '!':
823                                 *r++ = '.';
824                                 *r++ = '*';
825                                 break;
826                         case '*':
827                                 *r++ = '\\';
828                                 *r++ = '*';
829                                 break;
830                         default:
831                                 *r++ = *p;
832                                 break;
833                                 
834                         }
835                 }
836                 *r++ = '$'; /* what if the extension is a pattern ?? */
837                 *r++ = *p++; /* put in the closing null */
838                 err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
839                 if ( err1 ) {
840                         char errmess[500];
841                         regerror(err1,&preg,errmess,sizeof(errmess));
842                         regfree(&preg);
843                         ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
844                                         reg1, err1);
845                         return 0;
846                 }
847                 err1 = regexec(&preg, exten, 0, 0, 0);
848                 regfree(&preg);
849                 
850                 if ( err1 ) {
851                         /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
852                            err1,exten, pattern, reg1); */
853                         return 0; /* no match */
854                 } else {
855                         /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
856                            exten, pattern); */
857                         return 1;
858                 }
859                 
860                 
861         } else {
862                 if ( strcmp(exten,pattern) == 0 ) {
863                         return 1;
864                 } else
865                         return 0;
866         }
867 }
868
869
870 static void check_expr2_input(pval *expr, char *str)
871 {
872         int spaces = strspn(str,"\t \n");
873         if ( !strncmp(str+spaces,"$[",2) ) {
874                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
875                                 expr->filename, expr->startline, expr->endline, str);
876                 warns++;
877         }
878 }
879
880 static void check_includes(pval *includes)
881 {
882         struct pval *p4;
883         for (p4=includes->u1.list; p4; p4=p4->next) {
884                 /* for each context pointed to, find it, then find a context/label that matches the
885                    target here! */
886                 char *incl_context = p4->u1.str;
887                 /* find a matching context name */
888                 struct pval *that_other_context = find_context(incl_context);
889                 if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
890                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n",
891                                         includes->filename, includes->startline, includes->endline, incl_context);
892                         warns++;
893                 }
894         }
895 }
896
897
898 static void check_timerange(pval *p)
899 {
900         char *times;
901         char *e;
902         int s1, s2;
903         int e1, e2;
904
905         times = ast_strdupa(p->u1.str);
906
907         /* Star is all times */
908         if (ast_strlen_zero(times) || !strcmp(times, "*")) {
909                 return;
910         }
911         /* Otherwise expect a range */
912         e = strchr(times, '-');
913         if (!e) {
914                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) requires a '-' surrounded by two 24-hour times of day!\n",
915                                 p->filename, p->startline, p->endline, times);
916                 warns++;
917                 return;
918         }
919         *e = '\0';
920         e++;
921         while (*e && !isdigit(*e)) 
922                 e++;
923         if (!*e) {
924                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
925                                 p->filename, p->startline, p->endline, p->u1.str);
926                 warns++;
927         }
928         if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
929                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
930                                 p->filename, p->startline, p->endline, times);
931                 warns++;
932         }
933         if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
934                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
935                                 p->filename, p->startline, p->endline, times);
936                 warns++;
937         }
938
939         s1 = s1 * 30 + s2/2;
940         if ((s1 < 0) || (s1 >= 24*30)) {
941                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
942                                 p->filename, p->startline, p->endline, times);
943                 warns++;
944         }
945         e1 = e1 * 30 + e2/2;
946         if ((e1 < 0) || (e1 >= 24*30)) {
947                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
948                                 p->filename, p->startline, p->endline, e);
949                 warns++;
950         }
951         return;
952 }
953
954 static char *days[] =
955 {
956         "sun",
957         "mon",
958         "tue",
959         "wed",
960         "thu",
961         "fri",
962         "sat",
963 };
964
965 /*! \brief  get_dow: Get day of week */
966 static void check_dow(pval *DOW)
967 {
968         char *dow;
969         char *c;
970         /* The following line is coincidence, really! */
971         int s, e;
972         
973         dow = ast_strdupa(DOW->u1.str);
974
975         /* Check for all days */
976         if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
977                 return;
978         /* Get start and ending days */
979         c = strchr(dow, '-');
980         if (c) {
981                 *c = '\0';
982                 c++;
983         } else
984                 c = NULL;
985         /* Find the start */
986         s = 0;
987         while ((s < 7) && strcasecmp(dow, days[s])) s++;
988         if (s >= 7) {
989                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The day (%s) must be one of 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', or 'sat'!\n",
990                                 DOW->filename, DOW->startline, DOW->endline, dow);
991                 warns++;
992         }
993         if (c) {
994                 e = 0;
995                 while ((e < 7) && strcasecmp(c, days[e])) e++;
996                 if (e >= 7) {
997                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day (%s) must be one of 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', or 'sat'!\n",
998                                         DOW->filename, DOW->startline, DOW->endline, c);
999                         warns++;
1000                 }
1001         } else
1002                 e = s;
1003 }
1004
1005 static void check_day(pval *DAY)
1006 {
1007         char *day;
1008         char *c;
1009         /* The following line is coincidence, really! */
1010         int s, e;
1011
1012         day = ast_strdupa(DAY->u1.str);
1013
1014         /* Check for all days */
1015         if (ast_strlen_zero(day) || !strcmp(day, "*")) {
1016                 return;
1017         }
1018         /* Get start and ending days */
1019         c = strchr(day, '-');
1020         if (c) {
1021                 *c = '\0';
1022                 c++;
1023         }
1024         /* Find the start */
1025         if (sscanf(day, "%d", &s) != 1) {
1026                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
1027                                 DAY->filename, DAY->startline, DAY->endline, day);
1028                 warns++;
1029         }
1030         else if ((s < 1) || (s > 31)) {
1031                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number in the range [1-31]!\n",
1032                                 DAY->filename, DAY->startline, DAY->endline, day);
1033                 warns++;
1034         }
1035         s--;
1036         if (c) {
1037                 if (sscanf(c, "%d", &e) != 1) {
1038                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
1039                                         DAY->filename, DAY->startline, DAY->endline, c);
1040                         warns++;
1041                 }
1042                 else if ((e < 1) || (e > 31)) {
1043                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number in the range [1-31]!\n",
1044                                         DAY->filename, DAY->startline, DAY->endline, day);
1045                         warns++;
1046                 }
1047                 e--;
1048         } else
1049                 e = s;
1050 }
1051
1052 static char *months[] =
1053 {
1054         "jan",
1055         "feb",
1056         "mar",
1057         "apr",
1058         "may",
1059         "jun",
1060         "jul",
1061         "aug",
1062         "sep",
1063         "oct",
1064         "nov",
1065         "dec",
1066 };
1067
1068 static void check_month(pval *MON)
1069 {
1070         char *mon;
1071         char *c;
1072         /* The following line is coincidence, really! */
1073         int s, e;
1074
1075         mon = ast_strdupa(MON->u1.str);
1076
1077         /* Check for all days */
1078         if (ast_strlen_zero(mon) || !strcmp(mon, "*")) 
1079                 return ;
1080         /* Get start and ending days */
1081         c = strchr(mon, '-');
1082         if (c) {
1083                 *c = '\0';
1084                 c++;
1085         }
1086         /* Find the start */
1087         s = 0;
1088         while ((s < 12) && strcasecmp(mon, months[s])) s++;
1089         if (s >= 12) {
1090                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1091                                 MON->filename, MON->startline, MON->endline, mon);
1092                 warns++;
1093         }
1094         if (c) {
1095                 e = 0;
1096                 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1097                 if (e >= 12) {
1098                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1099                                         MON->filename, MON->startline, MON->endline, c);
1100                         warns++;
1101                 }
1102         } else
1103                 e = s;
1104 }
1105
1106 static int check_break(pval *item)
1107 {
1108         pval *p = item;
1109         
1110         while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1111                 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1112                    no sense */
1113                 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN 
1114                         || p->type == PV_WHILE || p->type == PV_FOR   ) {
1115                         return 1;
1116                 }
1117                 p = p->dad;
1118         }
1119         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1120                         item->filename, item->startline, item->endline);
1121         errs++;
1122         
1123         return 0;
1124 }
1125
1126 static int check_continue(pval *item)
1127 {
1128         pval *p = item;
1129         
1130         while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1131                 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1132                    no sense */
1133                 if( p->type == PV_WHILE || p->type == PV_FOR   ) {
1134                         return 1;
1135                 }
1136                 p = p->dad;
1137         }
1138         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1139                         item->filename, item->startline, item->endline);
1140         errs++;
1141         
1142         return 0;
1143 }
1144
1145 static struct pval *in_macro(pval *item)
1146 {
1147         struct pval *curr;
1148         curr = item;    
1149         while( curr ) {
1150                 if( curr->type == PV_MACRO  ) {
1151                         return curr;
1152                 }
1153                 curr = curr->dad;
1154         }
1155         return 0;
1156 }
1157
1158 static struct pval *in_context(pval *item)
1159 {
1160         struct pval *curr;
1161         curr = item;    
1162         while( curr ) {
1163                 if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
1164                         return curr;
1165                 }
1166                 curr = curr->dad;
1167         }
1168         return 0;
1169 }
1170
1171
1172 /* general purpose goto finder */
1173
1174 static void check_label(pval *item)
1175 {
1176         struct pval *curr;
1177         struct pval *x;
1178         int alright = 0;
1179         
1180         /* A label outside an extension just plain does not make sense! */
1181         
1182         curr = item;
1183         
1184         while( curr ) {
1185                 if( curr->type == PV_MACRO || curr->type == PV_EXTENSION   ) {
1186                         alright = 1;
1187                         break;
1188                 }
1189                 curr = curr->dad;
1190         }
1191         if( !alright )
1192         {
1193                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
1194                                 item->filename, item->startline, item->endline, item->u1.str);
1195                 errs++; 
1196         }
1197         
1198         
1199         /* basically, ensure that a label is not repeated in a context. Period.
1200            The method:  well, for each label, find the first label in the context
1201            with the same name. If it's not the current label, then throw an error. */
1202
1203         
1204         /* printf("==== check_label:   ====\n"); */
1205         if( !current_extension )
1206                 curr = current_context;
1207         else
1208                 curr = current_extension;
1209         
1210         x = find_first_label_in_current_context((char *)item->u1.str, curr);
1211         /* printf("Hey, check_label found with item = %x, and x is %x, and currcont is %x, label name is %s\n", item,x, current_context, (char *)item->u1.str); */
1212         if( x && x != item )
1213         {
1214                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1215                                 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1216                 errs++;
1217         }
1218         /* printf("<<<<< check_label:   ====\n"); */
1219 }
1220
1221 static pval *get_goto_target(pval *item)
1222 {
1223         /* just one item-- the label should be in the current extension */
1224         pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1225         pval *curr_cont;
1226         
1227         if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1228                 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1229                         return x;
1230         }
1231
1232         curr_cont = get_contxt(item);
1233
1234         /* TWO items */
1235         if (item->u1.list->next && !item->u1.list->next->next) {
1236                 if (!strstr((item->u1.list)->u1.str,"${") 
1237                         && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1238                         struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1239                                 return x;
1240                 }
1241         }
1242         
1243         /* All 3 items! */
1244         if (item->u1.list->next && item->u1.list->next->next) {
1245                 /* all three */
1246                 pval *first = item->u1.list;
1247                 pval *second = item->u1.list->next;
1248                 pval *third = item->u1.list->next->next;
1249                 
1250                 if (!strstr((item->u1.list)->u1.str,"${") 
1251                         && !strstr(item->u1.list->next->u1.str,"${")
1252                         && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1253                         struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1254                         if (!x) {
1255
1256                                 struct pval *p3;
1257                                 struct pval *that_context = find_context(item->u1.list->u1.str);
1258                                 
1259                                 /* the target of the goto could be in an included context!! Fancy that!! */
1260                                 /* look for includes in the current context */
1261                                 if (that_context) {
1262                                         for (p3=that_context->u2.statements; p3; p3=p3->next) {
1263                                                 if (p3->type == PV_INCLUDES) {
1264                                                         struct pval *p4;
1265                                                         for (p4=p3->u1.list; p4; p4=p4->next) {
1266                                                                 /* for each context pointed to, find it, then find a context/label that matches the
1267                                                                    target here! */
1268                                                                 char *incl_context = p4->u1.str;
1269                                                                 /* find a matching context name */
1270                                                                 struct pval *that_other_context = find_context(incl_context);
1271                                                                 if (that_other_context) {
1272                                                                         struct pval *x3;
1273                                                                         x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1274                                                                         if (x3) {
1275                                                                                 return x3;
1276                                                                         }
1277                                                                 }
1278                                                         }
1279                                                 }
1280                                         }
1281                                 }
1282                         }
1283                         return x;
1284                 }
1285         }
1286         return 0;
1287 }
1288
1289 static void check_goto(pval *item)
1290 {
1291         /* check for the target of the goto-- does it exist? */
1292         if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1293                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  empty label reference found!\n",
1294                                 item->filename, item->startline, item->endline);
1295                 errs++;
1296         }
1297         
1298         /* just one item-- the label should be in the current extension */
1299         
1300         if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1301                 struct pval *z = get_extension_or_contxt(item);
1302                 struct pval *x = 0;
1303                 if (z)
1304                         x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1305                 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1306                    (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1307                 if (!x) {
1308                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s exists in the current extension!\n",
1309                                         item->filename, item->startline, item->endline, item->u1.list->u1.str);
1310                         errs++;
1311                 }
1312                 else
1313                         return;
1314         }
1315         
1316         /* TWO items */
1317         if (item->u1.list->next && !item->u1.list->next->next) {
1318                 /* two items */
1319                 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1320                    (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1321                 if (!strstr((item->u1.list)->u1.str,"${") 
1322                         && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1323                         struct pval *z = get_contxt(item);
1324                         struct pval *x = 0;
1325                         
1326                         if (z)
1327                                 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1328
1329                         if (!x) {
1330                                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s|%s exists in the current context, or any of its inclusions!\n",
1331                                                 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1332                                 errs++;
1333                         }
1334                         else
1335                                 return;
1336                 }
1337         }
1338         
1339         /* All 3 items! */
1340         if (item->u1.list->next && item->u1.list->next->next) {
1341                 /* all three */
1342                 pval *first = item->u1.list;
1343                 pval *second = item->u1.list->next;
1344                 pval *third = item->u1.list->next->next;
1345                 
1346                 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1347                    (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1348                 if (!strstr((item->u1.list)->u1.str,"${") 
1349                         && !strstr(item->u1.list->next->u1.str,"${")
1350                         && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1351                         struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1352                         if (!x) {
1353                                 struct pval *p3;
1354                                 struct pval *found = 0;
1355                                 struct pval *that_context = find_context(item->u1.list->u1.str);
1356                                 
1357                                 /* the target of the goto could be in an included context!! Fancy that!! */
1358                                 /* look for includes in the current context */
1359                                 if (that_context) {
1360                                         for (p3=that_context->u2.statements; p3; p3=p3->next) {
1361                                                 if (p3->type == PV_INCLUDES) {
1362                                                         struct pval *p4;
1363                                                         for (p4=p3->u1.list; p4; p4=p4->next) {
1364                                                                 /* for each context pointed to, find it, then find a context/label that matches the
1365                                                                    target here! */
1366                                                                 char *incl_context = p4->u1.str;
1367                                                                 /* find a matching context name */
1368                                                                 struct pval *that_other_context = find_context(incl_context);
1369                                                                 if (that_other_context) {
1370                                                                         struct pval *x3;
1371                                                                         x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1372                                                                         if (x3) {
1373                                                                                 found = x3;
1374                                                                                 break;
1375                                                                         }
1376                                                                 }
1377                                                         }
1378                                                 }
1379                                         }
1380                                         if (!found) {
1381                                                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s|%s exists in the context %s or its inclusions!\n",
1382                                                                 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1383                                                 errs++;
1384                                         } else {
1385                                                 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1386                                                 if( mac ) {    /* yes! */
1387                                                         struct pval *targ = in_context(found);
1388                                                         if( mac != targ )
1389                                                         {
1390                                                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
1391                                                                                 item->filename, item->startline, item->endline);
1392                                                                 warns++;                                                                
1393                                                         }
1394                                                 }
1395                                         }
1396                                 } else {
1397                                         /* here is where code would go to check for target existence in extensions.conf files */
1398                                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto:  no context %s could be found that matches the goto target!\n",
1399                                                         item->filename, item->startline, item->endline, item->u1.list->u1.str);
1400                                         warns++; /* this is just a warning, because this context could be in extensions.conf or somewhere */
1401                                 }
1402                         } else {
1403                                 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1404                                 if( mac ) {    /* yes! */
1405                                         struct pval *targ = in_context(x);
1406                                         if( mac != targ )
1407                                         {
1408                                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
1409                                                                 item->filename, item->startline, item->endline);
1410                                                 warns++;                                                                
1411                                         }
1412                                 }
1413                         }
1414                 }
1415         }
1416 }
1417         
1418
1419 static void find_pval_goto_item(pval *item, int lev)
1420 {
1421         struct pval *p4;
1422         if (lev>100) {
1423                 ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
1424                 return;
1425         }
1426         
1427         switch ( item->type ) {
1428         case PV_MACRO:
1429                 /* fields: item->u1.str     == name of macro
1430                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1431                                    item->u2.arglist->u1.str  == argument
1432                                    item->u2.arglist->next   == next arg
1433
1434                                    item->u3.macro_statements == pval list of statements in macro body.
1435                 */
1436                         
1437                 /* printf("Descending into matching macro %s\n", match_context); */
1438                 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1439                 
1440                 break;
1441                         
1442         case PV_CONTEXT:
1443                 /* fields: item->u1.str     == name of context
1444                            item->u2.statements == pval list of statements in context body
1445                                    item->u3.abstract == int 1 if an abstract keyword were present
1446                 */
1447                 break;
1448
1449         case PV_CASE:
1450                 /* fields: item->u1.str     == value of case
1451                            item->u2.statements == pval list of statements under the case
1452                 */
1453                 find_pval_gotos(item->u2.statements,lev+1);
1454                 break;
1455                         
1456         case PV_PATTERN:
1457                 /* fields: item->u1.str     == value of case
1458                            item->u2.statements == pval list of statements under the case
1459                 */
1460                 find_pval_gotos(item->u2.statements,lev+1);
1461                 break;
1462                         
1463         case PV_DEFAULT:
1464                 /* fields: 
1465                            item->u2.statements == pval list of statements under the case
1466                 */
1467                 find_pval_gotos(item->u2.statements,lev+1);
1468                 break;
1469                         
1470         case PV_CATCH:
1471                 /* fields: item->u1.str     == name of extension to catch
1472                            item->u2.statements == pval list of statements in context body
1473                 */
1474                 find_pval_gotos(item->u2.statements,lev+1);
1475                 break;
1476                         
1477         case PV_STATEMENTBLOCK:
1478                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
1479                 */
1480                 find_pval_gotos(item->u1.list,lev+1);
1481                 break;
1482                         
1483         case PV_GOTO:
1484                 /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
1485                            item->u1.list->u1.str  == where the data on a PV_WORD will always be.
1486                 */
1487                 check_goto(item);  /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1488                 break;
1489                         
1490         case PV_INCLUDES:
1491                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
1492                 */
1493                 for (p4=item->u1.list; p4; p4=p4->next) {
1494                         /* for each context pointed to, find it, then find a context/label that matches the
1495                            target here! */
1496                         char *incl_context = p4->u1.str;
1497                         /* find a matching context name */
1498                         struct pval *that_context = find_context(incl_context);
1499                         if (that_context) {
1500                                 find_pval_gotos(that_context,lev+1); /* keep working up the includes */
1501                         }
1502                 }
1503                 break;
1504                 
1505         case PV_FOR:
1506                 /* fields: item->u1.for_init     == a string containing the initalizer
1507                            item->u2.for_test     == a string containing the loop test
1508                            item->u3.for_inc      == a string containing the loop increment
1509
1510                                    item->u4.for_statements == a pval list of statements in the for ()
1511                 */
1512                 find_pval_gotos(item->u4.for_statements,lev+1);
1513                 break;
1514                         
1515         case PV_WHILE:
1516                 /* fields: item->u1.str        == the while conditional, as supplied by user
1517
1518                                    item->u2.statements == a pval list of statements in the while ()
1519                 */
1520                 find_pval_gotos(item->u2.statements,lev+1);
1521                 break;
1522                         
1523         case PV_RANDOM:
1524                 /* fields: item->u1.str        == the random number expression, as supplied by user
1525
1526                                    item->u2.statements == a pval list of statements in the if ()
1527                                    item->u3.else_statements == a pval list of statements in the else
1528                                                                                            (could be zero)
1529                  fall thru to PV_IF */
1530                 
1531         case PV_IFTIME:
1532                 /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
1533
1534                                    item->u2.statements == a pval list of statements in the if ()
1535                                    item->u3.else_statements == a pval list of statements in the else
1536                                                                                            (could be zero)
1537                 fall thru to PV_IF*/
1538         case PV_IF:
1539                 /* fields: item->u1.str        == the if conditional, as supplied by user
1540
1541                                    item->u2.statements == a pval list of statements in the if ()
1542                                    item->u3.else_statements == a pval list of statements in the else
1543                                                                                            (could be zero)
1544                 */
1545                 find_pval_gotos(item->u2.statements,lev+1);
1546
1547                 if (item->u3.else_statements) {
1548                         find_pval_gotos(item->u3.else_statements,lev+1);
1549                 }
1550                 break;
1551                         
1552         case PV_SWITCH:
1553                 /* fields: item->u1.str        == the switch expression
1554
1555                                    item->u2.statements == a pval list of statements in the switch, 
1556                                                                                         (will be case statements, most likely!)
1557                 */
1558                 find_pval_gotos(item->u3.else_statements,lev+1);
1559                 break;
1560                         
1561         case PV_EXTENSION:
1562                 /* fields: item->u1.str        == the extension name, label, whatever it's called
1563
1564                                    item->u2.statements == a pval list of statements in the extension
1565                                    item->u3.hints      == a char * hint argument
1566                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
1567                 */
1568
1569                 find_pval_gotos(item->u2.statements,lev+1);
1570                 break;
1571
1572         default:
1573                 break;
1574         }
1575 }
1576
1577 static void find_pval_gotos(pval *item,int lev)
1578 {
1579         pval *i;
1580
1581         for (i=item; i; i=i->next) {
1582                 
1583                 find_pval_goto_item(i, lev);
1584         }
1585 }
1586
1587
1588
1589 /* general purpose label finder */
1590 static struct pval *match_pval_item(pval *item)
1591 {
1592         pval *x;
1593         
1594         switch ( item->type ) {
1595         case PV_MACRO:
1596                 /* fields: item->u1.str     == name of macro
1597                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1598                                    item->u2.arglist->u1.str  == argument
1599                                    item->u2.arglist->next   == next arg
1600
1601                                    item->u3.macro_statements == pval list of statements in macro body.
1602                 */
1603                 /* printf("    matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1604                 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1605                         
1606                         /* printf("MACRO: match context is: %s\n", match_context); */
1607                         
1608                         if (return_on_context_match && !strcmp(item->u1.str, match_context)) /* if we're just searching for a context, don't bother descending into them */ {
1609                                 /* printf("Returning on matching macro %s\n", match_context); */
1610                                 return item;
1611                         }
1612                         
1613                         
1614                         if (!return_on_context_match) {
1615                                 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1616                                 if ((x=match_pval(item->u3.macro_statements)))  {
1617                                         /* printf("Responded with pval match %x\n", x); */
1618                                         return x;
1619                                 }
1620                         }
1621                 } else {
1622                         /* printf("Skipping context/macro %s\n", item->u1.str); */
1623                 }
1624                 
1625                 break;
1626                         
1627         case PV_CONTEXT:
1628                 /* fields: item->u1.str     == name of context
1629                            item->u2.statements == pval list of statements in context body
1630                                    item->u3.abstract == int 1 if an abstract keyword were present
1631                 */
1632                 /* printf("    matching in CONTEXT\n"); */
1633                 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1634                         if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1635                                 /* printf("Returning on matching context %s\n", match_context); */
1636                                 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1637                                 return item;
1638                         }
1639                         
1640                         if (!return_on_context_match ) {
1641                                 /* printf("Descending into matching context %s\n", match_context); */
1642                                 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1643                                         /* printf("CONTEXT: Responded with pval match %x\n", x); */
1644                                         return x;
1645                                 }
1646                         }
1647                 } else {
1648                         /* printf("Skipping context/macro %s\n", item->u1.str); */
1649                 }
1650                 break;
1651
1652         case PV_CASE:
1653                 /* fields: item->u1.str     == value of case
1654                            item->u2.statements == pval list of statements under the case
1655                 */
1656                 /* printf("    matching in CASE\n"); */
1657                 if ((x=match_pval(item->u2.statements))) {
1658                         /* printf("CASE: Responded with pval match %x\n", x); */
1659                         return x;
1660                 }
1661                 break;
1662                         
1663         case PV_PATTERN:
1664                 /* fields: item->u1.str     == value of case
1665                            item->u2.statements == pval list of statements under the case
1666                 */
1667                 /* printf("    matching in PATTERN\n"); */
1668                 if ((x=match_pval(item->u2.statements))) {
1669                         /* printf("PATTERN: Responded with pval match %x\n", x); */
1670                         return x;
1671                 }
1672                 break;
1673                         
1674         case PV_DEFAULT:
1675                 /* fields: 
1676                            item->u2.statements == pval list of statements under the case
1677                 */
1678                 /* printf("    matching in DEFAULT\n"); */
1679                 if ((x=match_pval(item->u2.statements))) {
1680                         /* printf("DEFAULT: Responded with pval match %x\n", x); */
1681                         return x;
1682                 }
1683                 break;
1684                         
1685         case PV_CATCH:
1686                 /* fields: item->u1.str     == name of extension to catch
1687                            item->u2.statements == pval list of statements in context body
1688                 */
1689                 /* printf("    matching in CATCH\n"); */
1690                 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1691                         /* printf("Descending into matching catch %s => %s\n", match_exten, item->u1.str); */
1692                         if (strcmp(match_label,"1") == 0) {
1693                                 if (item->u2.statements) {
1694                                         struct pval *p5 = item->u2.statements;
1695                                         while (p5 && p5->type == PV_LABEL)  /* find the first non-label statement in this context. If it exists, there's a "1" */
1696                                                 p5 = p5->next;
1697                                         if (p5)
1698                                                 return p5;
1699                                         else
1700                                                 return 0;
1701                                 }
1702                                 else
1703                                         return 0;
1704                         }
1705
1706                         if ((x=match_pval(item->u2.statements))) {
1707                                 /* printf("CATCH: Responded with pval match %x\n", (unsigned int)x); */
1708                                 return x;
1709                         }
1710                 } else {
1711                         /* printf("Skipping catch %s\n", item->u1.str); */
1712                 }
1713                 break;
1714                         
1715         case PV_STATEMENTBLOCK:
1716                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
1717                 */
1718                 /* printf("    matching in STATEMENTBLOCK\n"); */
1719                 if ((x=match_pval(item->u1.list))) {
1720                         /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1721                         return x;
1722                 }
1723                 break;
1724                         
1725         case PV_LABEL:
1726                 /* fields: item->u1.str     == label name
1727                 */
1728                 /* printf("PV_LABEL %s (cont=%s, exten=%s\n", 
1729                    item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1730                 
1731                 if (count_labels) {
1732                         if (!strcmp(match_label, item->u1.str)) {
1733                                 label_count++;
1734                                 last_matched_label = item;
1735                         }
1736                         
1737                 } else {
1738                         if (!strcmp(match_label, item->u1.str)) {
1739                                 /* printf("LABEL: Responded with pval match %x\n", x); */
1740                                 return item;
1741                         }
1742                 }
1743                 break;
1744                         
1745         case PV_FOR:
1746                 /* fields: item->u1.for_init     == a string containing the initalizer
1747                            item->u2.for_test     == a string containing the loop test
1748                            item->u3.for_inc      == a string containing the loop increment
1749
1750                                    item->u4.for_statements == a pval list of statements in the for ()
1751                 */
1752                 /* printf("    matching in FOR\n"); */
1753                 if ((x=match_pval(item->u4.for_statements))) {
1754                         /* printf("FOR: Responded with pval match %x\n", x);*/
1755                         return x;
1756                 }
1757                 break;
1758                         
1759         case PV_WHILE:
1760                 /* fields: item->u1.str        == the while conditional, as supplied by user
1761
1762                                    item->u2.statements == a pval list of statements in the while ()
1763                 */
1764                 /* printf("    matching in WHILE\n"); */
1765                 if ((x=match_pval(item->u2.statements))) {
1766                         /* printf("WHILE: Responded with pval match %x\n", x); */
1767                         return x;
1768                 }
1769                 break;
1770                         
1771         case PV_RANDOM:
1772                 /* fields: item->u1.str        == the random number expression, as supplied by user
1773
1774                                    item->u2.statements == a pval list of statements in the if ()
1775                                    item->u3.else_statements == a pval list of statements in the else
1776                                                                                            (could be zero)
1777                  fall thru to PV_IF */
1778                 
1779         case PV_IFTIME:
1780                 /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
1781
1782                                    item->u2.statements == a pval list of statements in the if ()
1783                                    item->u3.else_statements == a pval list of statements in the else
1784                                                                                            (could be zero)
1785                 fall thru to PV_IF*/
1786         case PV_IF:
1787                 /* fields: item->u1.str        == the if conditional, as supplied by user
1788
1789                                    item->u2.statements == a pval list of statements in the if ()
1790                                    item->u3.else_statements == a pval list of statements in the else
1791                                                                                            (could be zero)
1792                 */
1793                 /* printf("    matching in IF/IFTIME/RANDOM\n"); */
1794                 if ((x=match_pval(item->u2.statements))) {
1795                         return x;
1796                 }
1797                 if (item->u3.else_statements) {
1798                         if ((x=match_pval(item->u3.else_statements))) {
1799                                 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1800                                 return x;
1801                         }
1802                 }
1803                 break;
1804                         
1805         case PV_SWITCH:
1806                 /* fields: item->u1.str        == the switch expression
1807
1808                                    item->u2.statements == a pval list of statements in the switch, 
1809                                                                                         (will be case statements, most likely!)
1810                 */
1811                 /* printf("    matching in SWITCH\n"); */
1812                 if ((x=match_pval(item->u2.statements))) {
1813                         /* printf("SWITCH: Responded with pval match %x\n", x); */
1814                         return x;
1815                 }
1816                 break;
1817                         
1818         case PV_EXTENSION:
1819                 /* fields: item->u1.str        == the extension name, label, whatever it's called
1820
1821                                    item->u2.statements == a pval list of statements in the extension
1822                                    item->u3.hints      == a char * hint argument
1823                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
1824                 */
1825                 /* printf("    matching in EXTENSION\n"); */
1826                 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1827                         /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1828                         if (strcmp(match_label,"1") == 0) {
1829                                 if (item->u2.statements) {
1830                                         struct pval *p5 = item->u2.statements;
1831                                         while (p5 && p5->type == PV_LABEL)  /* find the first non-label statement in this context. If it exists, there's a "1" */
1832                                                 p5 = p5->next;
1833                                         if (p5)
1834                                                 return p5;
1835                                         else
1836                                                 return 0;
1837                                 }
1838                                 else
1839                                         return 0;
1840                         }
1841
1842                         if ((x=match_pval(item->u2.statements))) {
1843                                 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1844                                 return x;
1845                         }
1846                 } else {
1847                         /* printf("Skipping exten %s\n", item->u1.str); */
1848                 }
1849                 break;
1850         default:
1851                 /* printf("    matching in default = %d\n", item->type); */
1852                 break;
1853         }
1854         return 0;
1855 }
1856
1857 struct pval *match_pval(pval *item)
1858 {
1859         pval *i;
1860
1861         for (i=item; i; i=i->next) {
1862                 pval *x;
1863                 /* printf("   -- match pval: item %d\n", i->type); */
1864                 
1865                 if ((x = match_pval_item(i))) {
1866                         /* printf("match_pval: returning x=%x\n", (int)x); */
1867                         return x; /* cut the search short */
1868                 }
1869         }
1870         return 0;
1871 }
1872
1873 #if 0
1874 int count_labels_in_current_context(char *label)
1875 {
1876         label_count = 0;
1877         count_labels = 1;
1878         return_on_context_match = 0;
1879         match_pval(current_context->u2.statements);
1880         
1881         return label_count;
1882 }
1883 #endif
1884
1885 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1886 {
1887         /* printf("  --- Got args %s, %s\n", exten, label); */
1888         struct pval *ret;
1889         struct pval *p3;
1890         struct pval *startpt = ((curr_cont->type==PV_MACRO)?curr_cont->u3.macro_statements: curr_cont->u2.statements);
1891         
1892         count_labels = 0;
1893         return_on_context_match = 0;
1894         match_context = "*";
1895         match_exten = "*";
1896         match_label = label;
1897         
1898         ret =  match_pval(curr_cont);
1899         if (ret)
1900                 return ret;
1901                                         
1902         /* the target of the goto could be in an included context!! Fancy that!! */
1903         /* look for includes in the current context */
1904         for (p3=startpt; p3; p3=p3->next) {
1905                 if (p3->type == PV_INCLUDES) {
1906                         struct pval *p4;
1907                         for (p4=p3->u1.list; p4; p4=p4->next) {
1908                                 /* for each context pointed to, find it, then find a context/label that matches the
1909                                    target here! */
1910                                 char *incl_context = p4->u1.str;
1911                                 /* find a matching context name */
1912                                 struct pval *that_context = find_context(incl_context);
1913                                 if (that_context) {
1914                                         struct pval *x3;
1915                                         x3 = find_first_label_in_current_context(label, that_context);
1916                                         if (x3) {
1917                                                 return x3;
1918                                         }
1919                                 }
1920                         }
1921                 }
1922         }
1923         return 0;
1924 }
1925
1926 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1927 {
1928         /* printf("  --- Got args %s, %s\n", exten, label); */
1929         struct pval *ret;
1930         struct pval *p3;
1931         struct pval *startpt;
1932         
1933         count_labels = 0;
1934         return_on_context_match = 0;
1935         match_context = "*";
1936         match_exten = exten;
1937         match_label = label;
1938         if (curr_cont->type == PV_MACRO)
1939                 startpt = curr_cont->u3.macro_statements;
1940         else
1941                 startpt = curr_cont->u2.statements;
1942
1943         ret =  match_pval(startpt);
1944         if (ret)
1945                 return ret;
1946                                         
1947         /* the target of the goto could be in an included context!! Fancy that!! */
1948         /* look for includes in the current context */
1949         for (p3=startpt; p3; p3=p3->next) {
1950                 if (p3->type == PV_INCLUDES) {
1951                         struct pval *p4;
1952                         for (p4=p3->u1.list; p4; p4=p4->next) {
1953                                 /* for each context pointed to, find it, then find a context/label that matches the
1954                                    target here! */
1955                                 char *incl_context = p4->u1.str;
1956                                 /* find a matching context name */
1957                                 struct pval *that_context = find_context(incl_context);
1958                                 if (that_context) {
1959                                         struct pval *x3;
1960                                         x3 = find_label_in_current_context(exten, label, that_context);
1961                                         if (x3) {
1962                                                 return x3;
1963                                         }
1964                                 }
1965                         }
1966                 }
1967         }
1968         return 0;
1969 }
1970
1971 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1972 {
1973         /* printf("  --- Got args %s\n", label); */
1974         count_labels = 0;
1975         return_on_context_match = 0;
1976         match_context = "*";
1977         match_exten = "*";
1978         match_label = label;
1979         return match_pval(curr_ext);
1980 }
1981
1982 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1983 {
1984         /* printf("  --- Got args %s, %s, %s\n", context, exten, label); */
1985         count_labels = 0;
1986         return_on_context_match = 0;
1987
1988         match_context = context;
1989         match_exten = exten;
1990         match_label = label;
1991         
1992         return match_pval(current_db);
1993 }
1994
1995
1996 struct pval *find_macro(char *name)
1997 {
1998         return_on_context_match = 1;
1999         count_labels = 0;
2000         match_context = name;
2001         match_exten = "*";  /* don't really need to set these, shouldn't be reached */
2002         match_label = "*";
2003         return match_pval(current_db);
2004 }
2005
2006 struct pval *find_context(char *name)
2007 {
2008         return_on_context_match = 1;
2009         count_labels = 0;
2010         match_context = name;
2011         match_exten = "*";  /* don't really need to set these, shouldn't be reached */
2012         match_label = "*";
2013         return match_pval(current_db);
2014 }
2015
2016 int is_float(char *arg )
2017 {
2018         char *s;
2019         for (s=arg; *s; s++) {
2020                 if (*s != '.' && (*s < '0' || *s > '9'))
2021                         return 0;
2022         }
2023         return 1;
2024 }
2025 int is_int(char *arg )
2026 {
2027         char *s;
2028         for (s=arg; *s; s++) {
2029                 if (*s < '0' || *s > '9')
2030                         return 0;
2031         }
2032         return 1;
2033 }
2034 int is_empty(char *arg)
2035 {
2036         if (!arg)
2037                 return 1;
2038         if (*arg == 0)
2039                 return 1;
2040         while (*arg) {
2041                 if (*arg != ' ' && *arg != '\t')
2042                         return 0;
2043                 arg++;
2044         }
2045         return 1;
2046 }
2047
2048 #ifdef AAL_ARGCHECK
2049 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
2050 {
2051         struct argchoice *ac;
2052         char *opcop,*q,*p;
2053         
2054         switch (should->dtype) {
2055         case ARGD_OPTIONSET:
2056                 if ( strstr(is->u1.str,"${") )
2057                         return 0;  /* no checking anything if there's a var reference in there! */
2058                         
2059                 opcop = ast_strdupa(is->u1.str);
2060
2061                 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
2062                         if ( *q == '(' ) {
2063                                 p = q+1;
2064                                 while (*p && *p != ')' )
2065                                         *p++ = '+';
2066                                 q = p+1;
2067                         }
2068                 }
2069                 
2070                 for (ac=app->opts; ac; ac=ac->next) {
2071                         if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2072                                 return 0;
2073                 }
2074                 for (ac=app->opts; ac; ac=ac->next) {
2075                         if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
2076                                 char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
2077                                 
2078                                 if (p && *p == 'j') {
2079                                         ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
2080                                                         is->filename, is->startline, is->endline, app->name);
2081                                         errs++;
2082                                 }
2083                                 
2084                                 if (p) {
2085                                         *p = '+';
2086                                         if (ac->name[1] == '(') {
2087                                                 if (*(p+1) != '(') {
2088                                                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The %c option in the %s application call should have an (argument), but doesn't!\n",
2089                                                                         is->filename, is->startline, is->endline, ac->name[0], app->name);
2090                                                         warns++;
2091                                                 }
2092                                         }
2093                                 }
2094                         }
2095                 }
2096                 for (q=opcop; *q; q++) {
2097                         if ( *q != '+' && *q != '(' && *q != ')') {
2098                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The %c option in the %s application call is not available as an option!\n",
2099                                                 is->filename, is->startline, is->endline, *q, app->name);
2100                                 warns++;
2101                         }
2102                 }
2103                 return 1;
2104                 break;
2105         default:
2106                 return 0;
2107         }
2108         
2109 }
2110
2111 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2112 {
2113         struct argchoice *ac;
2114         char *opcop;
2115         
2116         switch (should->dtype) {
2117         case ARGD_STRING:
2118                 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2119                         return 0;
2120                 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2121                         return 1;
2122                 break;
2123                 
2124         case ARGD_INT:
2125                 if (is_int(is->u1.str))
2126                         return 1;
2127                 else
2128                         return 0;
2129                 break;
2130                 
2131         case ARGD_FLOAT:
2132                 if (is_float(is->u1.str))
2133                         return 1;
2134                 else
2135                         return 0;
2136                 break;
2137                 
2138         case ARGD_ENUM:
2139                 if( !is->u1.str || strlen(is->u1.str) == 0 )
2140                         return 1; /* a null arg in the call will match an enum, I guess! */
2141                 for (ac=should->choices; ac; ac=ac->next) {
2142                         if (strcmp(ac->name,is->u1.str) == 0)
2143                                 return 1;
2144                 }
2145                 return 0;
2146                 break;
2147                 
2148         case ARGD_OPTIONSET:
2149                 opcop = ast_strdupa(is->u1.str);
2150                 
2151                 for (ac=app->opts; ac; ac=ac->next) {
2152                         if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2153                                 return 1;
2154                 }
2155                 for (ac=app->opts; ac; ac=ac->next) {
2156                         if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
2157                                 char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
2158                                 
2159                                 if (p) {
2160                                         *p = '+';
2161                                         if (ac->name[1] == '(') {
2162                                                 if (*(p+1) == '(') {
2163                                                         char *q = p+1;
2164                                                         while (*q && *q != ')') {
2165                                                                 *q++ = '+';
2166                                                         }
2167                                                         *q = '+';
2168                                                 }
2169                                         }
2170                                 }
2171                         }
2172                 }
2173                 return 1;
2174                 break;
2175         case ARGD_VARARG:
2176                 return 1; /* matches anything */
2177                 break;
2178         }
2179         return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2180 }
2181 #endif
2182
2183 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2184 {
2185 #ifdef AAL_ARGCHECK
2186         struct argdesc *ad = app->args;
2187         pval *pa;
2188         int z;
2189         
2190         for (pa = arglist; pa; pa=pa->next) {
2191                 if (!ad) {
2192                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2193                                         arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2194                         warns++;
2195                         return 1;
2196                 } else {
2197                         /* find the first entry in the ad list that will match */
2198                         do {
2199                                 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2200                                         break;
2201                                 
2202                                 z= option_matches( ad, pa, app);
2203                                 if (!z) {
2204                                         if ( !arglist )
2205                                                 arglist=appcall;
2206                                         
2207                                         if (ad->type == ARGD_REQUIRED) {
2208                                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2209                                                                 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2210                                                 warns++;
2211                                                 return 1;
2212                                         }
2213                                 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2214                                         option_matches_j( ad, pa, app);
2215                                 }
2216                                 ad = ad->next;
2217                         } while (ad && !z);
2218                 }
2219         }
2220         /* any app nodes left, that are not optional? */
2221         for ( ; ad; ad=ad->next) {
2222                 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2223                         if ( !arglist ) 
2224                                 arglist=appcall;
2225                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2226                                         arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2227                         warns++;
2228                         return 1;
2229                 }
2230         }
2231         return 0;
2232 #else
2233         return 0;
2234 #endif
2235 }
2236
2237 void check_switch_expr(pval *item, struct argapp *apps)
2238 {
2239 #ifdef AAL_ARGCHECK
2240         /* get and clean the variable name */
2241         char *buff1, *p;
2242         struct argapp *a,*a2;
2243         struct appsetvar *v,*v2;
2244         struct argchoice *c;
2245         pval *t;
2246         
2247         p = item->u1.str;
2248         while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2249                 p++;
2250         
2251         buff1 = ast_strdupa(p);
2252
2253         while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2254                 buff1[strlen(buff1)-1] = 0;
2255         /* buff1 now contains the variable name */
2256         v = 0;
2257         for (a=apps; a; a=a->next) {
2258                 for (v=a->setvars;v;v=v->next) {
2259                         if (strcmp(v->name,buff1) == 0) {
2260                                 break;
2261                         }
2262                 }
2263                 if ( v )
2264                         break;
2265         }
2266         if (v && v->vals) {
2267                 /* we have a match, to a variable that has a set of determined values */
2268                 int def= 0;
2269                 int pat = 0;
2270                 int f1 = 0;
2271                 
2272                 /* first of all, does this switch have a default case ? */
2273                 for (t=item->u2.statements; t; t=t->next) {
2274                         if (t->type == PV_DEFAULT) {
2275                                 def =1;
2276                                 break;
2277                         }
2278                         if (t->type == PV_PATTERN) {
2279                                 pat++;
2280                         }
2281                 }
2282                 if (def || pat) /* nothing to check. All cases accounted for! */
2283                         return;
2284                 for (c=v->vals; c; c=c->next) {
2285                         f1 = 0;
2286                         for (t=item->u2.statements; t; t=t->next) {
2287                                 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2288                                         if (!strcmp(t->u1.str,c->name)) {
2289                                                 f1 = 1;
2290                                                 break;
2291                                         }
2292                                 }
2293                         }
2294                         if (!f1) {
2295                                 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2296                                                 item->filename, item->startline, item->endline, item->u1.str, c->name);
2297                                 warns++;
2298                         }
2299                 }
2300                 /* next, is there an app call in the current exten, that would set this var? */
2301                 f1 = 0;
2302                 t = current_extension->u2.statements;
2303                 if ( t && t->type == PV_STATEMENTBLOCK )
2304                         t = t->u1.statements;
2305                 for (; t && t != item; t=t->next) {
2306                         if (t->type == PV_APPLICATION_CALL) {
2307                                 /* find the application that matches the u1.str */
2308                                 for (a2=apps; a2; a2=a2->next) {
2309                                         if (strcasecmp(a2->name, t->u1.str)==0) {
2310                                                 for (v2=a2->setvars; v2; v2=v2->next) {
2311                                                         if (strcmp(v2->name, buff1) == 0) {
2312                                                                 /* found an app that sets the var */
2313                                                                 f1 = 1;
2314                                                                 break;
2315                                                         }
2316                                                 }
2317                                         }
2318                                         if (f1)
2319                                                 break;
2320                                 }
2321                         }
2322                         if (f1)
2323                                 break;
2324                 }
2325                                 
2326                 /* see if it sets the var */
2327                 if (!f1) {
2328                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: Couldn't find an application call in this extension that sets the  expression (%s) value!\n",
2329                                         item->filename, item->startline, item->endline, item->u1.str);
2330                         warns++;
2331                 }
2332         }
2333 #else
2334         pval *t,*tl=0,*p2;
2335         int def= 0;
2336         
2337         /* first of all, does this switch have a default case ? */
2338         for (t=item->u2.statements; t; t=t->next) {
2339                 if (t->type == PV_DEFAULT) {
2340                         def =1;
2341                         break;
2342                 }
2343                 tl = t;
2344         }
2345         if (def) /* nothing to check. All cases accounted for! */
2346                 return;
2347         /* if no default, warn and insert a default case at the end */
2348         p2 = tl->next = calloc(1, sizeof(struct pval));
2349         
2350         p2->type = PV_DEFAULT;
2351         p2->startline = tl->startline;
2352         p2->endline = tl->endline;
2353         p2->startcol = tl->startcol;
2354         p2->endcol = tl->endcol;
2355         p2->filename = strdup(tl->filename);
2356         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2357                         p2->filename, p2->startline, p2->endline);
2358         warns++;
2359
2360 #endif
2361 }
2362
2363 static void check_context_names(void)
2364 {
2365         pval *i,*j;
2366         for (i=current_db; i; i=i->next) {
2367                 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2368                         for (j=i->next; j; j=j->next) {
2369                                 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2370                                         if ( !strcmp(i->u1.str, j->u1.str) )
2371                                         {
2372                                                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d!\n",
2373                                                                 i->filename, i->startline, i->endline, i->u1.str,  j->filename, j->startline, j->endline);
2374                                                 errs++;
2375                                         }
2376                                 }
2377                         }
2378                 }
2379         }
2380 }
2381
2382 static void check_abstract_reference(pval *abstract_context)
2383 {
2384         pval *i,*j;
2385         /* find some context includes that reference this context */
2386         
2387
2388         /* otherwise, print out a warning */
2389         for (i=current_db; i; i=i->next) {
2390                 if (i->type == PV_CONTEXT) {
2391                         for (j=i->u2. statements; j; j=j->next) {
2392                                 if ( j->type == PV_INCLUDES ) {
2393                                         struct pval *p4;
2394                                         for (p4=j->u1.list; p4; p4=p4->next) {
2395                                                 /* for each context pointed to, find it, then find a context/label that matches the
2396                                                    target here! */
2397                                                 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2398                                                         return; /* found a match! */
2399                                         }
2400                                 }
2401                         }
2402                 }
2403         }
2404         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: Couldn't find a reference to this abstract context (%s) in any other context!\n",
2405                         abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2406         warns++;
2407 }
2408
2409
2410 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2411 {
2412         pval *lp;
2413 #ifdef AAL_ARGCHECK
2414         struct argapp *app, *found;
2415 #endif
2416         struct pval *macro_def;
2417         struct pval *app_def;
2418
2419         char errmsg[4096];
2420         char *strp;
2421         
2422         switch (item->type) {
2423         case PV_WORD:
2424                 /* fields: item->u1.str == string associated with this (word).
2425                            item->u2.arglist  == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2426                 break;
2427                 
2428         case PV_MACRO:
2429                 /* fields: item->u1.str     == name of macro
2430                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2431                                    item->u2.arglist->u1.str  == argument
2432                                    item->u2.arglist->next   == next arg
2433
2434                                    item->u3.macro_statements == pval list of statements in macro body.
2435                 */
2436                 in_abstract_context = 0;
2437                 current_context = item;
2438                 current_extension = 0;
2439
2440                 check_macro_returns(item);
2441                 
2442                 for (lp=item->u2.arglist; lp; lp=lp->next) {
2443                 
2444                 }
2445                 check_pval(item->u3.macro_statements, apps,in_globals);
2446                 break;
2447                         
2448         case PV_CONTEXT:
2449                 /* fields: item->u1.str     == name of context
2450                            item->u2.statements == pval list of statements in context body
2451                                    item->u3.abstract == int 1 if an abstract keyword were present
2452                 */
2453                 current_context = item;
2454                 current_extension = 0;
2455                 if ( item->u3.abstract ) {
2456                         in_abstract_context = 1;
2457                         check_abstract_reference(item);
2458                 } else
2459                         in_abstract_context = 0;
2460                 check_pval(item->u2.statements, apps,in_globals);
2461                 break;
2462                         
2463         case PV_MACRO_CALL:
2464                 /* fields: item->u1.str     == name of macro to call
2465                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2466                                    item->u2.arglist->u1.str  == argument
2467                                    item->u2.arglist->next   == next arg
2468                 */
2469                 macro_def = find_macro(item->u1.str);
2470                 if (!macro_def) {
2471                         /* here is a good place to check to see if the definition is in extensions.conf! */
2472                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s ! Hopefully it is present in extensions.conf! \n",
2473                                         item->filename, item->startline, item->endline, item->u1.str);
2474                         warns++;
2475                 } else if (macro_def->type != PV_MACRO) {
2476                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2477                                         item->filename, item->startline, item->endline, item->u1.str);
2478                         errs++;
2479                 } else {
2480                         /* macro_def is a MACRO, so do the args match in number? */
2481                         int hereargs = 0;
2482                         int thereargs = 0;
2483                         
2484                         for (lp=item->u2.arglist; lp; lp=lp->next) {
2485                                 hereargs++;
2486                         }
2487                         for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2488                                 thereargs++;
2489                         }
2490                         if (hereargs != thereargs ) {
2491                                 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The macro call to %s has %d arguments, but the macro definition has %d arguments\n",
2492                                                 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2493                                 errs++;
2494                         }
2495                 }
2496                 break;
2497                         
2498         case PV_APPLICATION_CALL:
2499                 /* fields: item->u1.str     == name of application to call
2500                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2501                                    item->u2.arglist->u1.str  == argument
2502                                    item->u2.arglist->next   == next arg
2503                 */
2504                 /* Need to check to see if the application is available! */
2505                 app_def = find_context(item->u1.str);
2506                 if (app_def && app_def->type == PV_MACRO) {
2507                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2508                                         item->filename, item->startline, item->endline, item->u1.str);
2509                         errs++;
2510                 }
2511                 if (strcasecmp(item->u1.str,"GotoIf") == 0
2512                         || strcasecmp(item->u1.str,"GotoIfTime") == 0
2513                         || strcasecmp(item->u1.str,"while") == 0
2514                         || strcasecmp(item->u1.str,"endwhile") == 0
2515                         || strcasecmp(item->u1.str,"random") == 0
2516                         || strcasecmp(item->u1.str,"gosub") == 0
2517                         || strcasecmp(item->u1.str,"return") == 0
2518                         || strcasecmp(item->u1.str,"gosubif") == 0
2519                         || strcasecmp(item->u1.str,"continuewhile") == 0
2520                         || strcasecmp(item->u1.str,"endwhile") == 0
2521                         || strcasecmp(item->u1.str,"execif") == 0
2522                         || strcasecmp(item->u1.str,"execiftime") == 0
2523                         || strcasecmp(item->u1.str,"exitwhile") == 0
2524                         || strcasecmp(item->u1.str,"goto") == 0
2525                         || strcasecmp(item->u1.str,"macro") == 0
2526                         || strcasecmp(item->u1.str,"macroexclusive") == 0
2527                         || strcasecmp(item->u1.str,"macroif") == 0
2528                         || strcasecmp(item->u1.str,"stackpop") == 0
2529                         || strcasecmp(item->u1.str,"execIf") == 0 ) {
2530                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s affects flow of control, and needs to be re-written using AEL if, while, goto, etc. keywords instead!\n",
2531                                         item->filename, item->startline, item->endline, item->u1.str);
2532                         warns++;
2533                 }
2534                 if (strcasecmp(item->u1.str,"macroexit") == 0) {
2535                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
2536                                                 item->filename, item->startline, item->endline);
2537                                 item->type = PV_RETURN;
2538                                 free(item->u1.str);
2539                                 item->u1.str = 0;
2540                 }
2541                 
2542 #ifdef AAL_ARGCHECK
2543                 found = 0;
2544                 for (app=apps; app; app=app->next) {
2545                         if (strcasecmp(app->name, item->u1.str) == 0) {
2546                                 found =app;
2547                                 break;
2548                         }
2549                 }
2550                 if (!found) {
2551                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2552                                         item->filename, item->startline, item->endline, item->u1.str);
2553                         warns++;
2554                 } else
2555                         check_app_args(item, item->u2.arglist, app);
2556 #endif
2557                 break;
2558                 
2559         case PV_CASE:
2560                 /* fields: item->u1.str     == value of case
2561                            item->u2.statements == pval list of statements under the case
2562                 */
2563                 /* Make sure sequence of statements under case is terminated with  goto, return, or break */
2564                 /* find the last statement */
2565                 check_pval(item->u2.statements, apps,in_globals);
2566                 break;
2567                         
2568         case PV_PATTERN:
2569                 /* fields: item->u1.str     == value of case
2570                            item->u2.statements == pval list of statements under the case
2571                 */
2572                 /* Make sure sequence of statements under case is terminated with  goto, return, or break */
2573                 /* find the last statement */
2574                 
2575                 check_pval(item->u2.statements, apps,in_globals);
2576                 break;
2577                         
2578         case PV_DEFAULT:
2579                 /* fields: 
2580                            item->u2.statements == pval list of statements under the case
2581                 */
2582
2583                 check_pval(item->u2.statements, apps,in_globals);
2584                 break;
2585                         
2586         case PV_CATCH:
2587                 /* fields: item->u1.str     == name of extension to catch
2588                            item->u2.statements == pval list of statements in context body
2589                 */
2590                 check_pval(item->u2.statements, apps,in_globals);
2591                 break;
2592                         
2593         case PV_SWITCHES:
2594                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2595                 */
2596                 check_pval(item->u1.list, apps,in_globals);
2597                 break;
2598                         
2599         case PV_ESWITCHES:
2600                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2601                 */
2602                 check_pval(item->u1.list, apps,in_globals);
2603                 break;
2604                         
2605         case PV_INCLUDES:
2606                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2607                 */
2608                 check_pval(item->u1.list, apps,in_globals);
2609                 check_includes(item);
2610                 for (lp=item->u1.list; lp; lp=lp->next){
2611                         char *incl_context = lp->u1.str;
2612                         struct pval *that_context = find_context(incl_context);
2613
2614                         if ( lp->u2.arglist ) {
2615                                 check_timerange(lp->u2.arglist);
2616                                 check_dow(lp->u2.arglist->next);
2617                                 check_day(lp->u2.arglist->next->next);
2618                                 check_month(lp->u2.arglist->next->next->next);
2619                         }
2620                         
2621                         if (that_context) {
2622                                 find_pval_gotos(that_context->u2.statements,0);
2623                                 
2624                         }
2625                 }
2626                 break;
2627                         
2628         case PV_STATEMENTBLOCK:
2629                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
2630                 */
2631                 check_pval(item->u1.list, apps,in_globals);
2632                 break;
2633                         
2634         case PV_VARDEC:
2635         case PV_LOCALVARDEC:
2636                 /* fields: item->u1.str     == variable name
2637                            item->u2.val     == variable value to assign
2638                 */
2639                 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2640                 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2641                         snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.val);
2642                         ast_expr_register_extra_error_info(errmsg);
2643                         ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2644                         ast_expr_clear_extra_error_info();
2645                         if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2646                                 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2647                                                 item->filename, item->startline, item->endline, item->u2.val);
2648                                 warns++;
2649                         }
2650                         check_expr2_input(item,item->u2.val);
2651                 }
2652                 break;
2653                         
2654         case PV_GOTO:
2655                 /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
2656                            item->u1.list->u1.str  == where the data on a PV_WORD will always be.
2657                 */
2658                 /* don't check goto's in abstract contexts */
2659                 if ( in_abstract_context )
2660                         break;
2661                 
2662                 check_goto(item);
2663                 break;
2664                         
2665         case PV_LABEL:
2666                 /* fields: item->u1.str     == label name
2667                 */
2668                 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2669                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2670                                         item->filename, item->startline, item->endline, item->u1.str);
2671                         warns++;
2672                 }
2673
2674                 check_label(item);
2675                 break;
2676                         
2677         case PV_FOR:
2678                 /* fields: item->u1.for_init     == a string containing the initalizer
2679                            item->u2.for_test     == a string containing the loop test
2680                            item->u3.for_inc      == a string containing the loop increment
2681
2682                                    item->u4.for_statements == a pval list of statements in the for ()
2683                 */
2684                 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, for test expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.for_test);
2685                 ast_expr_register_extra_error_info(errmsg);
2686
2687                 strp = strchr(item->u1.for_init, '=');
2688                 if (strp) {
2689                         ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2690                 }
2691                 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
2692                 strp = strchr(item->u3.for_inc, '=');
2693                 if (strp) {
2694                         ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2695                 }
2696                 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2697                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2698                                         item->filename, item->startline, item->endline, item->u2.for_test);
2699                         warns++;
2700                 }
2701                 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2702                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2703                                         item->filename, item->startline, item->endline, item->u3.for_inc);
2704                         warns++;
2705                 }
2706                 check_expr2_input(item,item->u2.for_test);
2707                 check_expr2_input(item,item->u3.for_inc);
2708                 
2709                 ast_expr_clear_extra_error_info();
2710                 check_pval(item->u4.for_statements, apps,in_globals);
2711                 break;
2712                         
2713         case PV_WHILE:
2714                 /* fields: item->u1.str        == the while conditional, as supplied by user
2715
2716                                    item->u2.statements == a pval list of statements in the while ()
2717                 */
2718                 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2719                 ast_expr_register_extra_error_info(errmsg);
2720                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2721                 ast_expr_clear_extra_error_info();
2722                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2723                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2724                                         item->filename, item->startline, item->endline, item->u1.str);
2725                         warns++;
2726                 }
2727                 check_expr2_input(item,item->u1.str);
2728                 check_pval(item->u2.statements, apps,in_globals);
2729                 break;
2730                         
2731         case PV_BREAK:
2732                 /* fields: none
2733                 */
2734                 check_break(item);
2735                 break;
2736                         
2737         case PV_RETURN:
2738                 /* fields: none
2739                 */
2740                 break;
2741                         
2742         case PV_CONTINUE:
2743                 /* fields: none
2744                 */
2745                 check_continue(item);
2746                 break;
2747                         
2748         case PV_RANDOM:
2749                 /* fields: item->u1.str        == the random number expression, as supplied by user
2750
2751                                    item->u2.statements == a pval list of statements in the if ()
2752                                    item->u3.else_statements == a pval list of statements in the else
2753                                                                                            (could be zero)
2754                 */
2755                 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2756                 ast_expr_register_extra_error_info(errmsg);
2757                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2758                 ast_expr_clear_extra_error_info();
2759                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2760                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2761                                         item->filename, item->startline, item->endline, item->u1.str);
2762                         warns++;
2763                 }
2764                 check_expr2_input(item,item->u1.str);
2765                 check_pval(item->u2.statements, apps,in_globals);
2766                 if (item->u3.else_statements) {
2767                         check_pval(item->u3.else_statements, apps,in_globals);
2768                 }
2769                 break;
2770
2771         case PV_IFTIME:
2772                 /* fields: item->u1.list        == the if time values, 4 of them, each in PV_WORD, linked list 
2773
2774                                    item->u2.statements == a pval list of statements in the if ()
2775                                    item->u3.else_statements == a pval list of statements in the else
2776                                                                                            (could be zero)
2777                 */
2778                 if ( item->u2.arglist ) {
2779                         check_timerange(item->u1.list);
2780                         check_dow(item->u1.list->next);
2781                         check_day(item->u1.list->next->next);
2782                         check_month(item->u1.list->next->next->next);
2783                 }
2784
2785                 check_pval(item->u2.statements, apps,in_globals);
2786                 if (item->u3.else_statements) {
2787                         check_pval(item->u3.else_statements, apps,in_globals);
2788                 }
2789                 break;
2790                         
2791         case PV_IF:
2792                 /* fields: item->u1.str        == the if conditional, as supplied by user
2793
2794                                    item->u2.statements == a pval list of statements in the if ()
2795                                    item->u3.else_statements == a pval list of statements in the else
2796                                                                                            (could be zero)
2797                 */
2798                 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2799                 ast_expr_register_extra_error_info(errmsg);
2800                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2801                 ast_expr_clear_extra_error_info();
2802                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2803                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2804                                         item->filename, item->startline, item->endline, item->u1.str);
2805                         warns++;
2806                 }
2807                 check_expr2_input(item,item->u1.str);
2808                 check_pval(item->u2.statements, apps,in_globals);
2809                 if (item->u3.else_statements) {
2810                         check_pval(item->u3.else_statements, apps,in_globals);
2811                 }
2812                 break;
2813                         
2814         case PV_SWITCH:
2815                 /* fields: item->u1.str        == the switch expression
2816
2817                                    item->u2.statements == a pval list of statements in the switch, 
2818                                                                                         (will be case statements, most likely!)
2819                 */
2820                 /* we can check the switch expression, see if it matches any of the app variables...
2821            if it does, then, are all the possible cases accounted for? */
2822                 check_switch_expr(item, apps);
2823                 check_pval(item->u2.statements, apps,in_globals);
2824                 break;
2825                         
2826         case PV_EXTENSION:
2827                 /* fields: item->u1.str        == the extension name, label, whatever it's called
2828
2829                                    item->u2.statements == a pval list of statements in the extension
2830                                    item->u3.hints      == a char * hint argument
2831                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
2832                 */
2833                 current_extension = item ;
2834                 
2835                 check_pval(item->u2.statements, apps,in_globals);
2836                 break;
2837                         
2838         case PV_IGNOREPAT:
2839                 /* fields: item->u1.str        == the ignorepat data
2840                 */
2841                 break;
2842                         
2843         case PV_GLOBALS:
2844                 /* fields: item->u1.statements     == pval list of statements, usually vardecs
2845                 */
2846                 in_abstract_context = 0;
2847                 check_pval(item->u1.statements, apps, 1);
2848                 break;
2849         default:
2850                 break;
2851         }
2852 }
2853
2854 void check_pval(pval *item, struct argapp *apps, int in_globals)
2855 {
2856         pval *i;
2857
2858         /* checks to do:
2859            1. Do goto's point to actual labels? 
2860            2. Do macro calls reference a macro?
2861            3. Does the number of macro args match the definition?
2862            4. Is a macro call missing its & at the front?
2863            5. Application calls-- we could check syntax for existing applications,
2864               but I need some some sort of universal description bnf for a general
2865                   sort of method for checking arguments, in number, maybe even type, at least. 
2866                   Don't want to hand code checks for hundreds of applications.
2867         */
2868         
2869         for (i=item; i; i=i->next) {
2870                 check_pval_item(i,apps,in_globals);
2871         }
2872 }
2873
2874 static void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2875 {
2876         
2877 #ifdef AAL_ARGCHECK
2878         int argapp_errs =0;
2879         char *rfilename;
2880 #endif
2881         struct argapp *apps=0;
2882
2883 #ifdef AAL_ARGCHECK
2884         rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2885         sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2886         
2887         apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2888 #endif
2889         current_db = item;
2890         errs = warns = notes = 0;
2891
2892         check_context_names();
2893         check_pval(item, apps, 0);
2894
2895 #ifdef AAL_ARGCHECK
2896         argdesc_destroy(apps);  /* taketh away */
2897 #endif
2898         current_db = 0;
2899
2900         *arg_errs = errs;
2901         *arg_warns = warns;
2902         *arg_notes = notes;
2903 }
2904
2905 /* =============================================================================================== */
2906 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2907 /* =============================================================================================== */
2908
2909 static int control_statement_count = 0;
2910
2911 struct ael_priority *new_prio(void)
2912 {
2913         struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2914         return x;
2915 }
2916
2917 struct ael_extension *new_exten(void)
2918 {
2919         struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2920         return x;
2921 }
2922
2923 void linkprio(struct ael_extension *exten, struct ael_priority *prio)
2924 {
2925         if (!exten->plist) {
2926                 exten->plist = prio;
2927                 exten->plist_last = prio;
2928         } else {
2929                 exten->plist_last->next = prio;
2930                 exten->plist_last = prio;
2931         }
2932         if( !prio->exten )
2933                 prio->exten = exten; /* don't override the switch value */
2934 }
2935
2936 void destroy_extensions(struct ael_extension *exten)
2937 {
2938         struct ael_extension *ne, *nen;
2939         for (ne=exten; ne; ne=nen) {
2940                 struct ael_priority *pe, *pen;
2941                 
2942                 if (ne->name)
2943                         free(ne->name);
2944                 
2945                 /* cidmatch fields are allocated with name, and freed when
2946                    the name field is freed. Don't do a free for this field,
2947                    unless you LIKE to see a crash! */
2948
2949                 if (ne->hints)
2950                         free(ne->hints);
2951                 
2952                 for (pe=ne->plist; pe; pe=pen) {
2953                         pen = pe->next;
2954                         if (pe->app)
2955                                 free(pe->app);
2956                         pe->app = 0;
2957                         if (pe->appargs)
2958                                 free(pe->appargs);
2959                         pe->appargs = 0;
2960                         pe->origin = 0;
2961                         pe->goto_true = 0;
2962                         pe->goto_false = 0;
2963                         free(pe);
2964                 }
2965                 nen = ne->next_exten;
2966                 ne->next_exten = 0;
2967                 ne->plist =0;
2968                 ne->plist_last = 0;
2969                 ne->next_exten = 0;
2970                 ne->loop_break = 0;
2971                 ne->loop_continue = 0;
2972                 free(ne);
2973         }
2974 }
2975
2976 static int label_inside_case(pval *label)
2977 {
2978         pval *p = label;
2979         
2980         while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
2981                 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
2982                         return 1;
2983                 }
2984
2985                 p = p->dad;
2986         }
2987         return 0;
2988 }
2989
2990 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
2991 {
2992         add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2993         exten->next_exten = add;
2994 }
2995
2996 static void remove_spaces_before_equals(char *str)
2997 {
2998         char *p;
2999         while( str && *str && *str != '=' )
3000         {
3001                 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
3002                 {
3003                         p = str;
3004                         while( *p )
3005                         {
3006                                 *p = *(p+1);
3007                                 p++;
3008                         }
3009                 }
3010                 else
3011                         str++;
3012         }
3013 }
3014
3015 static void gen_match_to_pattern(char *pattern, char *result)
3016 {
3017         /* the result will be a string that will be matched by pattern */
3018         char *p=pattern, *t=result;
3019         while (*p) {
3020                 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
3021                         *t++ = '9';
3022                 else if (*p == '[') {
3023                         char *z = p+1;
3024                         while (*z != ']')
3025                                 z++;
3026                         if (*(z+1)== ']')
3027                                 z++;
3028                         *t++=*(p+1); /* use the first char in the set */
3029                         p = z;
3030                 } else {
3031                         *t++ = *p;
3032                 }
3033                 p++;
3034         }
3035         *t++ = 0; /* cap it off */
3036 }
3037
3038 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
3039 {
3040         pval *p,*p2,*p3;
3041         struct ael_priority *pr;
3042         struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
3043         struct ael_priority *while_test, *while_loop, *while_end;
3044         struct ael_priority *switch_test, *switch_end, *fall_thru;
3045         struct ael_priority *if_test, *if_end, *if_skip, *if_false;
3046 #ifdef OLD_RAND_ACTION
3047         struct ael_priority *rand_test, *rand_end, *rand_skip;
3048 #endif
3049         char buf1[2000];
3050         char buf2[2000];
3051         char *strp, *strp2;
3052         char new_label[2000];
3053         int default_exists;
3054         int local_control_statement_count;
3055         int first;
3056         struct ael_priority *loop_break_save;
3057         struct ael_priority *loop_continue_save;
3058         struct ael_extension *switch_case;
3059         
3060         for (p=statement; p; p=p->next) {
3061                 switch (p->type) {
3062                 case PV_VARDEC:
3063                         pr = new_prio();
3064                         pr->type = AEL_APPCALL;
3065                         snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
3066                         pr->app = strdup("Set");
3067                         remove_spaces_before_equals(buf1);
3068                         pr->appargs = strdup(buf1);
3069                         pr->origin = p;
3070                         linkprio(exten, pr);
3071                         break;
3072
3073                 case PV_LOCALVARDEC:
3074                         pr = new_prio();
3075                         pr->type = AEL_APPCALL;
3076                         snprintf(buf1,sizeof(buf1),"LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
3077                         pr->app = strdup("Set");
3078                         remove_spaces_before_equals(buf1);
3079                         pr->appargs = strdup(buf1);
3080                         pr->origin = p;
3081                         linkprio(exten, pr);
3082                         break;
3083
3084                 case PV_GOTO:
3085                         pr = new_prio();
3086                         pr->type = AEL_APPCALL;
3087                         p->u2.goto_target = get_goto_target(p);
3088                         if( p->u2.goto_target ) {
3089                                 p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
3090                         }
3091                         
3092                         if (!p->u1.list->next) /* just one */ {
3093                                 pr->app = strdup("Goto");
3094                                 if (!mother_exten)
3095                                         pr->appargs = strdup(p->u1.list->u1.str);
3096                                 else {  /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */ 
3097                                         snprintf(buf1,sizeof(buf1),"%s,%s", mother_exten->name, p->u1.list->u1.str);
3098                                         pr->appargs = strdup(buf1);
3099                                 }
3100                                 
3101                         } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
3102                                 snprintf(buf1,sizeof(buf1),"%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
3103                                 pr->app = strdup("Goto");
3104                                 pr->appargs = strdup(buf1);
3105                         } else if (p->u1.list->next && p->u1.list->next->next) {
3106                                 snprintf(buf1,sizeof(buf1),"%s,%s,%s", p->u1.list->u1.str, 
3107                                                 p->u1.list->next->u1.str,
3108                                                 p->u1.list->next->next->u1.str);
3109                                 pr->app = strdup("Goto");
3110                                 pr->appargs = strdup(buf1);
3111                         }
3112                         pr->origin = p;
3113                         linkprio(exten, pr);
3114                         break;
3115
3116                 case PV_LABEL:
3117                         pr = new_prio();
3118                         pr->type = AEL_LABEL;
3119                         pr->origin = p;
3120                         p->u3.compiled_label = exten;
3121                         linkprio(exten, pr);
3122                         break;
3123
3124                 case PV_FOR:
3125                         control_statement_count++;
3126                         loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3127                         loop_continue_save = exten->loop_continue;
3128                         snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
3129                         for_init = new_prio();
3130                         for_inc = new_prio();
3131                         for_test = new_prio();
3132                         for_loop = new_prio();
3133                         for_end = new_prio();
3134                         for_init->type = AEL_APPCALL;
3135                         for_inc->type = AEL_APPCALL;
3136                         for_test->type = AEL_FOR_CONTROL;
3137                         for_test->goto_false = for_end;
3138                         for_loop->type = AEL_CONTROL1; /* simple goto */
3139                         for_end->type = AEL_APPCALL;
3140                         for_init->app = strdup("Set");
3141                         
3142                         strcpy(buf2,p->u1.for_init);
3143                         remove_spaces_before_equals(buf2);
3144                         strp = strchr(buf2, '=');
3145                         strp2 = strchr(p->u1.for_init, '=');
3146                         if (strp) {
3147                                 *(strp+1) = 0;
3148                                 strcat(buf2,"$[");
3149                                 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3150                                 strcat(buf2,"]");
3151                                 for_init->appargs = strdup(buf2);
3152                         } else
3153                                 for_init->appargs = strdup(p->u1.for_init);
3154
3155                         for_inc->app = strdup("Set");
3156
3157                         strcpy(buf2,p->u3.for_inc);
3158                         remove_spaces_before_equals(buf2);
3159                         strp = strchr(buf2, '=');
3160                         strp2 = strchr(p->u3.for_inc, '=');
3161                         if (strp) {
3162                                 *(strp+1) = 0;
3163                                 strcat(buf2,"$[");
3164                                 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3165                                 strcat(buf2,"]");
3166                                 for_inc->appargs = strdup(buf2);
3167                         } else
3168                                 for_inc->appargs = strdup(p->u3.for_inc);
3169                         snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
3170                         for_test->app = 0;
3171                         for_test->appargs = strdup(buf1);
3172                         for_loop->goto_true = for_test;
3173                         snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
3174                         for_end->app = strdup("NoOp");
3175                         for_end->appargs = strdup(buf1);
3176                         /* link & load! */
3177                         linkprio(exten, for_init);
3178                         linkprio(exten, for_test);
3179                         
3180                         /* now, put the body of the for loop here */
3181                         exten->loop_break = for_end;
3182                         exten->loop_continue = for_inc;
3183                         
3184                         gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context); /* this will link in all the statements here */
3185                         
3186                         linkprio(exten, for_inc);
3187                         linkprio(exten, for_loop);
3188                         linkprio(exten, for_end);
3189                         
3190                         
3191                         exten->loop_break = loop_break_save;
3192                         exten->loop_continue = loop_continue_save;
3193                         for_loop->origin = p;
3194                         break;
3195
3196                 case PV_WHILE:
3197                         control_statement_count++;
3198                         loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3199                         loop_continue_save = exten->loop_continue;
3200                         snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
3201                         while_test = new_prio();
3202                         while_loop = new_prio();
3203                         while_end = new_prio();
3204                         while_test->type = AEL_FOR_CONTROL;
3205                         while_test->goto_false = while_end;
3206                         while_loop->type = AEL_CONTROL1; /* simple goto */
3207                         while_end->type = AEL_APPCALL;
3208                         snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
3209                         while_test->app = 0;
3210                         while_test->appargs = strdup(buf1);
3211                         while_loop->goto_true = while_test;
3212                         snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
3213                         while_end->app = strdup("NoOp");
3214                         while_end->appargs = strdup(buf1);
3215
3216                         linkprio(exten, while_test);
3217                         
3218                         /* now, put the body of the for loop here */
3219                         exten->loop_break = while_end;
3220                         exten->loop_continue = while_test;
3221                         
3222                         gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the while body statements here */
3223
3224                         linkprio(exten, while_loop);
3225                         linkprio(exten, while_end);
3226                         
3227                         
3228                         exten->loop_break = loop_break_save;
3229                         exten->loop_continue = loop_continue_save;
3230                         while_loop->origin = p;
3231                         break;
3232
3233                 case PV_SWITCH:
3234                         control_statement_count++;
3235                         local_control_statement_count = control_statement_count;
3236                         loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3237                         loop_continue_save = exten->loop_continue;
3238                         snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
3239
3240                         switch_test = new_prio();
3241                         switch_end = new_prio();
3242                         switch_test->type = AEL_APPCALL;<