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