Resolve FORWARD_NULL static analysis warnings
[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) {
1164                 return NULL;
1165         }
1166
1167         if (!item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1168                 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1169                         return x;
1170         }
1171
1172         curr_cont = get_contxt(item);
1173
1174         /* TWO items */
1175         if (item->u1.list->next && !item->u1.list->next->next) {
1176                 if (!strstr((item->u1.list)->u1.str,"${") 
1177                         && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1178                         struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1179                                 return x;
1180                 }
1181         }
1182         
1183         /* All 3 items! */
1184         if (item->u1.list->next && item->u1.list->next->next) {
1185                 /* all three */
1186                 pval *first = item->u1.list;
1187                 pval *second = item->u1.list->next;
1188                 pval *third = item->u1.list->next->next;
1189                 
1190                 if (!strstr((item->u1.list)->u1.str,"${") 
1191                         && !strstr(item->u1.list->next->u1.str,"${")
1192                         && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1193                         struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1194                         if (!x) {
1195
1196                                 struct pval *p3;
1197                                 struct pval *that_context = find_context(item->u1.list->u1.str);
1198                                 
1199                                 /* the target of the goto could be in an included context!! Fancy that!! */
1200                                 /* look for includes in the current context */
1201                                 if (that_context) {
1202                                         for (p3=that_context->u2.statements; p3; p3=p3->next) {
1203                                                 if (p3->type == PV_INCLUDES) {
1204                                                         struct pval *p4;
1205                                                         for (p4=p3->u1.list; p4; p4=p4->next) {
1206                                                                 /* for each context pointed to, find it, then find a context/label that matches the
1207                                                                    target here! */
1208                                                                 char *incl_context = p4->u1.str;
1209                                                                 /* find a matching context name */
1210                                                                 struct pval *that_other_context = find_context(incl_context);
1211                                                                 if (that_other_context) {
1212                                                                         struct pval *x3;
1213                                                                         x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1214                                                                         if (x3) {
1215                                                                                 return x3;
1216                                                                         }
1217                                                                 }
1218                                                         }
1219                                                 }
1220                                         }
1221                                 }
1222                         }
1223                         return x;
1224                 }
1225         }
1226         return 0;
1227 }
1228
1229 static void check_goto(pval *item)
1230 {
1231         /* check for the target of the goto-- does it exist? */
1232         if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1233                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  empty label reference found!\n",
1234                                 item->filename, item->startline, item->endline);
1235                 errs++;
1236         }
1237         
1238         /* just one item-- the label should be in the current extension */
1239         
1240         if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1241                 struct pval *z = get_extension_or_contxt(item);
1242                 struct pval *x = 0;
1243                 if (z)
1244                         x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1245                 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1246                    (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1247                 if (!x) {
1248                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s exists in the current extension!\n",
1249                                         item->filename, item->startline, item->endline, item->u1.list->u1.str);
1250                         errs++;
1251                 }
1252                 else
1253                         return;
1254         }
1255         
1256         /* TWO items */
1257         if (item->u1.list->next && !item->u1.list->next->next) {
1258                 /* two items */
1259                 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1260                    (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1261                 if (!strstr((item->u1.list)->u1.str,"${") 
1262                         && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1263                         struct pval *z = get_contxt(item);
1264                         struct pval *x = 0;
1265                         
1266                         if (z)
1267                                 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1268
1269                         if (!x) {
1270                                 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",
1271                                                 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1272                                 errs++;
1273                         }
1274                         else
1275                                 return;
1276                 }
1277         }
1278         
1279         /* All 3 items! */
1280         if (item->u1.list->next && item->u1.list->next->next) {
1281                 /* all three */
1282                 pval *first = item->u1.list;
1283                 pval *second = item->u1.list->next;
1284                 pval *third = item->u1.list->next->next;
1285                 
1286                 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1287                    (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1288                 if (!strstr((item->u1.list)->u1.str,"${") 
1289                         && !strstr(item->u1.list->next->u1.str,"${")
1290                         && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1291                         struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1292                         if (!x) {
1293                                 struct pval *p3;
1294                                 struct pval *found = 0;
1295                                 struct pval *that_context = find_context(item->u1.list->u1.str);
1296                                 
1297                                 /* the target of the goto could be in an included context!! Fancy that!! */
1298                                 /* look for includes in the current context */
1299                                 if (that_context) {
1300                                         for (p3=that_context->u2.statements; p3; p3=p3->next) {
1301                                                 if (p3->type == PV_INCLUDES) {
1302                                                         struct pval *p4;
1303                                                         for (p4=p3->u1.list; p4; p4=p4->next) {
1304                                                                 /* for each context pointed to, find it, then find a context/label that matches the
1305                                                                    target here! */
1306                                                                 char *incl_context = p4->u1.str;
1307                                                                 /* find a matching context name */
1308                                                                 struct pval *that_other_context = find_context(incl_context);
1309                                                                 if (that_other_context) {
1310                                                                         struct pval *x3;
1311                                                                         x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1312                                                                         if (x3) {
1313                                                                                 found = x3;
1314                                                                                 break;
1315                                                                         }
1316                                                                 }
1317                                                         }
1318                                                 }
1319                                         }
1320                                         if (!found) {
1321                                                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s|%s exists in the context %s or its inclusions!\n",
1322                                                                 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1323                                                 errs++;
1324                                         } else {
1325                                                 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1326                                                 if( mac ) {    /* yes! */
1327                                                         struct pval *targ = in_context(found);
1328                                                         if( mac != targ )
1329                                                         {
1330                                                                 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",
1331                                                                                 item->filename, item->startline, item->endline);
1332                                                                 warns++;                                                                
1333                                                         }
1334                                                 }
1335                                         }
1336                                 } else {
1337                                         /* here is where code would go to check for target existence in extensions.conf files */
1338 #ifdef STANDALONE
1339                                         struct pbx_find_info pfiq = {.stacklen = 0 };
1340                                         extern int localized_pbx_load_module(void);
1341                                         /* if this is a standalone, we will need to make sure the 
1342                                            localized load of extensions.conf is done */
1343                                         if (!extensions_dot_conf_loaded) {
1344                                                 localized_pbx_load_module();
1345                                                 extensions_dot_conf_loaded++;
1346                                         }
1347
1348                                         pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
1349                                                                                         atoi(third->u1.str) ? NULL : third->u1.str, NULL, 
1350                                                                                         atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
1351                                         
1352                                         if (pfiq.status != STATUS_SUCCESS) {
1353                                                 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",
1354                                                                 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1355                                                 warns++;
1356                                         }
1357 #else
1358                                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto:  Couldn't find goto target %s|%s|%s in the AEL code!\n",
1359                                                         item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1360                                         warns++;
1361 #endif
1362                                 }
1363                         } else {
1364                                 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1365                                 if( mac ) {    /* yes! */
1366                                         struct pval *targ = in_context(x);
1367                                         if( mac != targ )
1368                                         {
1369                                                 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",
1370                                                                 item->filename, item->startline, item->endline);
1371                                                 warns++;                                                                
1372                                         }
1373                                 }
1374                         }
1375                 }
1376         }
1377 }
1378         
1379
1380 static void find_pval_goto_item(pval *item, int lev)
1381 {
1382         struct pval *p4;
1383         
1384         if (lev>100) {
1385                 ast_log(LOG_ERROR,"find_pval_goto in infinite loop! item_type: %d\n\n", item->type);
1386                 return;
1387         }
1388         
1389         switch ( item->type ) {
1390         case PV_MACRO:
1391                 /* fields: item->u1.str     == name of macro
1392                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1393                                    item->u2.arglist->u1.str  == argument
1394                                    item->u2.arglist->next   == next arg
1395
1396                                    item->u3.macro_statements == pval list of statements in macro body.
1397                 */
1398                         
1399                 /* printf("Descending into macro %s at line %d\n", item->u1.str, item->startline); */
1400                 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1401                 
1402                 break;
1403                         
1404         case PV_CONTEXT:
1405                 /* fields: item->u1.str     == name of context
1406                            item->u2.statements == pval list of statements in context body
1407                                    item->u3.abstract == int 1 if an abstract keyword were present
1408                 */
1409                 break;
1410
1411         case PV_CASE:
1412                 /* fields: item->u1.str     == value of case
1413                            item->u2.statements == pval list of statements under the case
1414                 */
1415                 /* printf("Descending into Case of %s\n", item->u1.str); */
1416                 find_pval_gotos(item->u2.statements,lev+1);
1417                 break;
1418                         
1419         case PV_PATTERN:
1420                 /* fields: item->u1.str     == value of case
1421                            item->u2.statements == pval list of statements under the case
1422                 */
1423                 /* printf("Descending into Pattern of %s\n", item->u1.str); */
1424                 find_pval_gotos(item->u2.statements,lev+1);
1425                 break;
1426                         
1427         case PV_DEFAULT:
1428                 /* fields: 
1429                            item->u2.statements == pval list of statements under the case
1430                 */
1431                 /* printf("Descending into default\n"); */
1432                 find_pval_gotos(item->u2.statements,lev+1);
1433                 break;
1434                         
1435         case PV_CATCH:
1436                 /* fields: item->u1.str     == name of extension to catch
1437                            item->u2.statements == pval list of statements in context body
1438                 */
1439                 /* printf("Descending into catch of %s\n", item->u1.str); */
1440                 find_pval_gotos(item->u2.statements,lev+1);
1441                 break;
1442                         
1443         case PV_STATEMENTBLOCK:
1444                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
1445                 */
1446                 /* printf("Descending into statement block\n"); */
1447                 find_pval_gotos(item->u1.list,lev+1);
1448                 break;
1449                         
1450         case PV_GOTO:
1451                 /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
1452                            item->u1.list->u1.str  == where the data on a PV_WORD will always be.
1453                 */
1454                 check_goto(item);  /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1455                 break;
1456                         
1457         case PV_INCLUDES:
1458                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
1459                 */
1460                 for (p4=item->u1.list; p4; p4=p4->next) {
1461                         /* for each context pointed to, find it, then find a context/label that matches the
1462                            target here! */
1463                         char *incl_context = p4->u1.str;
1464                         /* find a matching context name */
1465                         struct pval *that_context = find_context(incl_context);
1466                         if (that_context && that_context->u2.statements) {
1467                                 /* 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); */
1468                                 find_pval_gotos(that_context->u2.statements,lev+1); /* keep working up the includes */
1469                         }
1470                 }
1471                 break;
1472                 
1473         case PV_FOR:
1474                 /* fields: item->u1.for_init     == a string containing the initalizer
1475                            item->u2.for_test     == a string containing the loop test
1476                            item->u3.for_inc      == a string containing the loop increment
1477
1478                                    item->u4.for_statements == a pval list of statements in the for ()
1479                 */
1480                 /* printf("Descending into for at line %d\n", item->startline); */
1481                 find_pval_gotos(item->u4.for_statements,lev+1);
1482                 break;
1483                         
1484         case PV_WHILE:
1485                 /* fields: item->u1.str        == the while conditional, as supplied by user
1486
1487                                    item->u2.statements == a pval list of statements in the while ()
1488                 */
1489                 /* printf("Descending into while at line %d\n", item->startline); */
1490                 find_pval_gotos(item->u2.statements,lev+1);
1491                 break;
1492                         
1493         case PV_RANDOM:
1494                 /* fields: item->u1.str        == the random number expression, as supplied by user
1495
1496                                    item->u2.statements == a pval list of statements in the if ()
1497                                    item->u3.else_statements == a pval list of statements in the else
1498                                                                                            (could be zero)
1499                  fall thru to PV_IF */
1500                 
1501         case PV_IFTIME:
1502                 /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
1503
1504                                    item->u2.statements == a pval list of statements in the if ()
1505                                    item->u3.else_statements == a pval list of statements in the else
1506                                                                                            (could be zero)
1507                 fall thru to PV_IF*/
1508         case PV_IF:
1509                 /* fields: item->u1.str        == the if conditional, as supplied by user
1510
1511                                    item->u2.statements == a pval list of statements in the if ()
1512                                    item->u3.else_statements == a pval list of statements in the else
1513                                                                                            (could be zero)
1514                 */
1515                 /* printf("Descending into random/iftime/if at line %d\n", item->startline); */
1516                 find_pval_gotos(item->u2.statements,lev+1);
1517
1518                 if (item->u3.else_statements) {
1519                         /* printf("Descending into random/iftime/if's ELSE at line %d\n", item->startline); */
1520                         find_pval_gotos(item->u3.else_statements,lev+1);
1521                 }
1522                 break;
1523                         
1524         case PV_SWITCH:
1525                 /* fields: item->u1.str        == the switch expression
1526
1527                                    item->u2.statements == a pval list of statements in the switch, 
1528                                                                                         (will be case statements, most likely!)
1529                 */
1530                 /* printf("Descending into switch at line %d\n", item->startline); */
1531                 find_pval_gotos(item->u3.else_statements,lev+1);
1532                 break;
1533                         
1534         case PV_EXTENSION:
1535                 /* fields: item->u1.str        == the extension name, label, whatever it's called
1536
1537                                    item->u2.statements == a pval list of statements in the extension
1538                                    item->u3.hints      == a char * hint argument
1539                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
1540                 */
1541
1542                 /* printf("Descending into extension %s at line %d\n", item->u1.str, item->startline); */
1543                 find_pval_gotos(item->u2.statements,lev+1);
1544                 break;
1545
1546         default:
1547                 break;
1548         }
1549 }
1550
1551 static void find_pval_gotos(pval *item,int lev)
1552 {
1553         pval *i;
1554         
1555         for (i=item; i; i=i->next) {
1556                 /* printf("About to call pval_goto_item, itemcount=%d, itemtype=%d\n", item_count, i->type); */
1557                 find_pval_goto_item(i, lev);
1558         }
1559 }
1560
1561
1562
1563 /* general purpose label finder */
1564 static struct pval *match_pval_item(pval *item)
1565 {
1566         pval *x;
1567         
1568         switch ( item->type ) {
1569         case PV_MACRO:
1570                 /* fields: item->u1.str     == name of macro
1571                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1572                                    item->u2.arglist->u1.str  == argument
1573                                    item->u2.arglist->next   == next arg
1574
1575                                    item->u3.macro_statements == pval list of statements in macro body.
1576                 */
1577                 /* printf("    matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1578                 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1579                         
1580                         /* printf("MACRO: match context is: %s\n", match_context); */
1581                         
1582                         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 */ {
1583                                 /* printf("Returning on matching macro %s\n", match_context); */
1584                                 return item;
1585                         }
1586                         
1587                         
1588                         if (!return_on_context_match) {
1589                                 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1590                                 if ((x=match_pval(item->u3.macro_statements)))  {
1591                                         /* printf("Responded with pval match %x\n", x); */
1592                                         return x;
1593                                 }
1594                         }
1595                 } else {
1596                         /* printf("Skipping context/macro %s\n", item->u1.str); */
1597                 }
1598                 
1599                 break;
1600                         
1601         case PV_CONTEXT:
1602                 /* fields: item->u1.str     == name of context
1603                            item->u2.statements == pval list of statements in context body
1604                                    item->u3.abstract == int 1 if an abstract keyword were present
1605                 */
1606                 /* printf("    matching in CONTEXT\n"); */
1607                 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1608                         if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1609                                 /* printf("Returning on matching context %s\n", match_context); */
1610                                 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1611                                 return item;
1612                         }
1613                         
1614                         if (!return_on_context_match ) {
1615                                 /* printf("Descending into matching context %s\n", match_context); */
1616                                 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1617                                         /* printf("CONTEXT: Responded with pval match %x\n", x); */
1618                                         return x;
1619                                 }
1620                         }
1621                 } else {
1622                         /* printf("Skipping context/macro %s\n", item->u1.str); */
1623                 }
1624                 break;
1625
1626         case PV_CASE:
1627                 /* fields: item->u1.str     == value of case
1628                            item->u2.statements == pval list of statements under the case
1629                 */
1630                 /* printf("    matching in CASE\n"); */
1631                 if ((x=match_pval(item->u2.statements))) {
1632                         /* printf("CASE: Responded with pval match %x\n", x); */
1633                         return x;
1634                 }
1635                 break;
1636                         
1637         case PV_PATTERN:
1638                 /* fields: item->u1.str     == value of case
1639                            item->u2.statements == pval list of statements under the case
1640                 */
1641                 /* printf("    matching in PATTERN\n"); */
1642                 if ((x=match_pval(item->u2.statements))) {
1643                         /* printf("PATTERN: Responded with pval match %x\n", x); */
1644                         return x;
1645                 }
1646                 break;
1647                         
1648         case PV_DEFAULT:
1649                 /* fields: 
1650                            item->u2.statements == pval list of statements under the case
1651                 */
1652                 /* printf("    matching in DEFAULT\n"); */
1653                 if ((x=match_pval(item->u2.statements))) {
1654                         /* printf("DEFAULT: Responded with pval match %x\n", x); */
1655                         return x;
1656                 }
1657                 break;
1658                         
1659         case PV_CATCH:
1660                 /* fields: item->u1.str     == name of extension to catch
1661                            item->u2.statements == pval list of statements in context body
1662                 */
1663                 /* printf("    matching in CATCH\n"); */
1664                 if ((x=match_pval(item->u2.statements))) {
1665                         /* printf("CATCH: Responded with pval match %x\n", x); */
1666                         return x;
1667                 }
1668                 break;
1669                         
1670         case PV_STATEMENTBLOCK:
1671                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
1672                 */
1673                 /* printf("    matching in STATEMENTBLOCK\n"); */
1674                 if ((x=match_pval(item->u1.list))) {
1675                         /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1676                         return x;
1677                 }
1678                 break;
1679                         
1680         case PV_LABEL:
1681                 /* fields: item->u1.str     == label name
1682                 */
1683                 /* printf("PV_LABEL %s (cont=%s, exten=%s\n", 
1684                    item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1685                 
1686                 if (count_labels) {
1687                         if (!strcmp(match_label, item->u1.str)) {
1688                                 label_count++;
1689                                 last_matched_label = item;
1690                         }
1691                         
1692                 } else {
1693                         if (!strcmp(match_label, item->u1.str)) {
1694                                 /* printf("LABEL: Responded with pval match %x\n", x); */
1695                                 return item;
1696                         }
1697                 }
1698                 break;
1699                         
1700         case PV_FOR:
1701                 /* fields: item->u1.for_init     == a string containing the initalizer
1702                            item->u2.for_test     == a string containing the loop test
1703                            item->u3.for_inc      == a string containing the loop increment
1704
1705                                    item->u4.for_statements == a pval list of statements in the for ()
1706                 */
1707                 /* printf("    matching in FOR\n"); */
1708                 if ((x=match_pval(item->u4.for_statements))) {
1709                         /* printf("FOR: Responded with pval match %x\n", x);*/
1710                         return x;
1711                 }
1712                 break;
1713                         
1714         case PV_WHILE:
1715                 /* fields: item->u1.str        == the while conditional, as supplied by user
1716
1717                                    item->u2.statements == a pval list of statements in the while ()
1718                 */
1719                 /* printf("    matching in WHILE\n"); */
1720                 if ((x=match_pval(item->u2.statements))) {
1721                         /* printf("WHILE: Responded with pval match %x\n", x); */
1722                         return x;
1723                 }
1724                 break;
1725                         
1726         case PV_RANDOM:
1727                 /* fields: item->u1.str        == the random number expression, as supplied by user
1728
1729                                    item->u2.statements == a pval list of statements in the if ()
1730                                    item->u3.else_statements == a pval list of statements in the else
1731                                                                                            (could be zero)
1732                  fall thru to PV_IF */
1733                 
1734         case PV_IFTIME:
1735                 /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
1736
1737                                    item->u2.statements == a pval list of statements in the if ()
1738                                    item->u3.else_statements == a pval list of statements in the else
1739                                                                                            (could be zero)
1740                 fall thru to PV_IF*/
1741         case PV_IF:
1742                 /* fields: item->u1.str        == the if conditional, as supplied by user
1743
1744                                    item->u2.statements == a pval list of statements in the if ()
1745                                    item->u3.else_statements == a pval list of statements in the else
1746                                                                                            (could be zero)
1747                 */
1748                 /* printf("    matching in IF/IFTIME/RANDOM\n"); */
1749                 if ((x=match_pval(item->u2.statements))) {
1750                         return x;
1751                 }
1752                 if (item->u3.else_statements) {
1753                         if ((x=match_pval(item->u3.else_statements))) {
1754                                 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1755                                 return x;
1756                         }
1757                 }
1758                 break;
1759                         
1760         case PV_SWITCH:
1761                 /* fields: item->u1.str        == the switch expression
1762
1763                                    item->u2.statements == a pval list of statements in the switch, 
1764                                                                                         (will be case statements, most likely!)
1765                 */
1766                 /* printf("    matching in SWITCH\n"); */
1767                 if ((x=match_pval(item->u2.statements))) {
1768                         /* printf("SWITCH: Responded with pval match %x\n", x); */
1769                         return x;
1770                 }
1771                 break;
1772                         
1773         case PV_EXTENSION:
1774                 /* fields: item->u1.str        == the extension name, label, whatever it's called
1775
1776                                    item->u2.statements == a pval list of statements in the extension
1777                                    item->u3.hints      == a char * hint argument
1778                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
1779                 */
1780                 /* printf("    matching in EXTENSION\n"); */
1781                 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1782                         /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1783                         if (strcmp(match_label,"1") == 0) {
1784                                 if (item->u2.statements) {
1785                                         struct pval *p5 = item->u2.statements;
1786                                         while (p5 && p5->type == PV_LABEL)  /* find the first non-label statement in this context. If it exists, there's a "1" */
1787                                                 p5 = p5->next;
1788                                         if (p5)
1789                                                 return p5;
1790                                         else
1791                                                 return 0;
1792                                 }
1793                                 else
1794                                         return 0;
1795                         }
1796
1797                         if ((x=match_pval(item->u2.statements))) {
1798                                 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1799                                 return x;
1800                         }
1801                 } else {
1802                         /* printf("Skipping exten %s\n", item->u1.str); */
1803                 }
1804                 break;
1805         default:
1806                 /* printf("    matching in default = %d\n", item->type); */
1807                 break;
1808         }
1809         return 0;
1810 }
1811
1812 struct pval *match_pval(pval *item)
1813 {
1814         pval *i;
1815
1816         for (i=item; i; i=i->next) {
1817                 pval *x;
1818                 /* printf("   -- match pval: item %d\n", i->type); */
1819                 
1820                 if ((x = match_pval_item(i))) {
1821                         /* printf("match_pval: returning x=%x\n", (int)x); */
1822                         return x; /* cut the search short */
1823                 }
1824         }
1825         return 0;
1826 }
1827
1828 #if 0
1829 int count_labels_in_current_context(char *label)
1830 {
1831         label_count = 0;
1832         count_labels = 1;
1833         return_on_context_match = 0;
1834         match_pval(current_context->u2.statements);
1835         
1836         return label_count;
1837 }
1838 #endif
1839
1840 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1841 {
1842         /* printf("  --- Got args %s, %s\n", exten, label); */
1843         struct pval *ret;
1844         struct pval *p3;
1845         
1846         count_labels = 0;
1847         return_on_context_match = 0;
1848         match_context = "*";
1849         match_exten = "*";
1850         match_label = label;
1851         
1852         ret =  match_pval(curr_cont);
1853         if (ret)
1854                 return ret;
1855                                         
1856         /* the target of the goto could be in an included context!! Fancy that!! */
1857         /* look for includes in the current context */
1858         for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1859                 if (p3->type == PV_INCLUDES) {
1860                         struct pval *p4;
1861                         for (p4=p3->u1.list; p4; p4=p4->next) {
1862                                 /* for each context pointed to, find it, then find a context/label that matches the
1863                                    target here! */
1864                                 char *incl_context = p4->u1.str;
1865                                 /* find a matching context name */
1866                                 struct pval *that_context = find_context(incl_context);
1867                                 if (that_context) {
1868                                         struct pval *x3;
1869                                         x3 = find_first_label_in_current_context(label, that_context);
1870                                         if (x3) {
1871                                                 return x3;
1872                                         }
1873                                 }
1874                         }
1875                 }
1876         }
1877         return 0;
1878 }
1879
1880 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1881 {
1882         /* printf("  --- Got args %s, %s\n", exten, label); */
1883         struct pval *ret;
1884         struct pval *p3;
1885         
1886         count_labels = 0;
1887         return_on_context_match = 0;
1888         match_context = "*";
1889         match_exten = exten;
1890         match_label = label;
1891         ret =  match_pval(curr_cont->u2.statements);
1892         if (ret)
1893                 return ret;
1894                                         
1895         /* the target of the goto could be in an included context!! Fancy that!! */
1896         /* look for includes in the current context */
1897         for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1898                 if (p3->type == PV_INCLUDES) {
1899                         struct pval *p4;
1900                         for (p4=p3->u1.list; p4; p4=p4->next) {
1901                                 /* for each context pointed to, find it, then find a context/label that matches the
1902                                    target here! */
1903                                 char *incl_context = p4->u1.str;
1904                                 /* find a matching context name */
1905                                 struct pval *that_context = find_context(incl_context);
1906                                 if (that_context) {
1907                                         struct pval *x3;
1908                                         x3 = find_label_in_current_context(exten, label, that_context);
1909                                         if (x3) {
1910                                                 return x3;
1911                                         }
1912                                 }
1913                         }
1914                 }
1915         }
1916         return 0;
1917 }
1918
1919 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1920 {
1921         /* printf("  --- Got args %s\n", label); */
1922         count_labels = 0;
1923         return_on_context_match = 0;
1924         match_context = "*";
1925         match_exten = "*";
1926         match_label = label;
1927         return match_pval(curr_ext);
1928 }
1929
1930 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1931 {
1932         /* printf("  --- Got args %s, %s, %s\n", context, exten, label); */
1933         count_labels = 0;
1934         return_on_context_match = 0;
1935
1936         match_context = context;
1937         match_exten = exten;
1938         match_label = label;
1939         
1940         return match_pval(current_db);
1941 }
1942
1943
1944 struct pval *find_macro(char *name)
1945 {
1946         return_on_context_match = 1;
1947         count_labels = 0;
1948         match_context = name;
1949         match_exten = "*";  /* don't really need to set these, shouldn't be reached */
1950         match_label = "*";
1951         return match_pval(current_db);
1952 }
1953
1954 struct pval *find_context(char *name)
1955 {
1956         return_on_context_match = 1;
1957         count_labels = 0;
1958         match_context = name;
1959         match_exten = "*";  /* don't really need to set these, shouldn't be reached */
1960         match_label = "*";
1961         return match_pval(current_db);
1962 }
1963
1964 int is_float(char *arg )
1965 {
1966         char *s;
1967         for (s=arg; *s; s++) {
1968                 if (*s != '.' && (*s < '0' || *s > '9'))
1969                         return 0;
1970         }
1971         return 1;
1972 }
1973 int is_int(char *arg )
1974 {
1975         char *s;
1976         for (s=arg; *s; s++) {
1977                 if (*s < '0' || *s > '9')
1978                         return 0;
1979         }
1980         return 1;
1981 }
1982 int is_empty(char *arg)
1983 {
1984         if (!arg)
1985                 return 1;
1986         if (*arg == 0)
1987                 return 1;
1988         while (*arg) {
1989                 if (*arg != ' ' && *arg != '\t')
1990                         return 0;
1991                 arg++;
1992         }
1993         return 1;
1994 }
1995
1996 #ifdef AAL_ARGCHECK
1997 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1998 {
1999         struct argchoice *ac;
2000         char *opcop,*q,*p;
2001         
2002         switch (should->dtype) {
2003         case ARGD_OPTIONSET:
2004                 if ( strstr(is->u1.str,"${") )
2005                         return 0;  /* no checking anything if there's a var reference in there! */
2006                         
2007                 opcop = ast_strdupa(is->u1.str);
2008
2009                 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
2010                         if ( *q == '(' ) {
2011                                 p = q+1;
2012                                 while (*p && *p != ')' )
2013                                         *p++ = '+';
2014                                 q = p+1;
2015                         }
2016                 }
2017                 
2018                 for (ac=app->opts; ac; ac=ac->next) {
2019                         if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2020                                 return 0;
2021                 }
2022                 for (ac=app->opts; ac; ac=ac->next) {
2023                         if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
2024                                 char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
2025                                 
2026                                 if (p && *p == 'j') {
2027                                         ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
2028                                                         is->filename, is->startline, is->endline, app->name);
2029                                         errs++;
2030                                 }
2031                                 
2032                                 if (p) {
2033                                         *p = '+';
2034                                         if (ac->name[1] == '(') {
2035                                                 if (*(p+1) != '(') {
2036                                                         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",
2037                                                                         is->filename, is->startline, is->endline, ac->name[0], app->name);
2038                                                         warns++;
2039                                                 }
2040                                         }
2041                                 }
2042                         }
2043                 }
2044                 for (q=opcop; *q; q++) {
2045                         if ( *q != '+' && *q != '(' && *q != ')') {
2046                                 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",
2047                                                 is->filename, is->startline, is->endline, *q, app->name);
2048                                 warns++;
2049                         }
2050                 }
2051                 return 1;
2052                 break;
2053         default:
2054                 return 0;
2055         }
2056         
2057 }
2058
2059 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2060 {
2061         struct argchoice *ac;
2062         char *opcop;
2063         
2064         switch (should->dtype) {
2065         case ARGD_STRING:
2066                 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2067                         return 0;
2068                 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2069                         return 1;
2070                 break;
2071                 
2072         case ARGD_INT:
2073                 if (is_int(is->u1.str))
2074                         return 1;
2075                 else
2076                         return 0;
2077                 break;
2078                 
2079         case ARGD_FLOAT:
2080                 if (is_float(is->u1.str))
2081                         return 1;
2082                 else
2083                         return 0;
2084                 break;
2085                 
2086         case ARGD_ENUM:
2087                 if( !is->u1.str || strlen(is->u1.str) == 0 )
2088                         return 1; /* a null arg in the call will match an enum, I guess! */
2089                 for (ac=should->choices; ac; ac=ac->next) {
2090                         if (strcmp(ac->name,is->u1.str) == 0)
2091                                 return 1;
2092                 }
2093                 return 0;
2094                 break;
2095                 
2096         case ARGD_OPTIONSET:
2097                 opcop = ast_strdupa(is->u1.str);
2098                 
2099                 for (ac=app->opts; ac; ac=ac->next) {
2100                         if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2101                                 return 1;
2102                 }
2103                 for (ac=app->opts; ac; ac=ac->next) {
2104                         if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
2105                                 char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
2106                                 
2107                                 if (p) {
2108                                         *p = '+';
2109                                         if (ac->name[1] == '(') {
2110                                                 if (*(p+1) == '(') {
2111                                                         char *q = p+1;
2112                                                         while (*q && *q != ')') {
2113                                                                 *q++ = '+';
2114                                                         }
2115                                                         *q = '+';
2116                                                 }
2117                                         }
2118                                 }
2119                         }
2120                 }
2121                 return 1;
2122                 break;
2123         case ARGD_VARARG:
2124                 return 1; /* matches anything */
2125                 break;
2126         }
2127         return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2128 }
2129 #endif
2130
2131 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2132 {
2133 #ifdef AAL_ARGCHECK
2134         struct argdesc *ad = app->args;
2135         pval *pa;
2136         int z;
2137         
2138         for (pa = arglist; pa; pa=pa->next) {
2139                 if (!ad) {
2140                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2141                                         arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2142                         warns++;
2143                         return 1;
2144                 } else {
2145                         /* find the first entry in the ad list that will match */
2146                         do {
2147                                 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2148                                         break;
2149                                 
2150                                 z= option_matches( ad, pa, app);
2151                                 if (!z) {
2152                                         if ( !arglist )
2153                                                 arglist=appcall;
2154                                         
2155                                         if (ad->type == ARGD_REQUIRED) {
2156                                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2157                                                                 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2158                                                 warns++;
2159                                                 return 1;
2160                                         }
2161                                 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2162                                         option_matches_j( ad, pa, app);
2163                                 }
2164                                 ad = ad->next;
2165                         } while (ad && !z);
2166                 }
2167         }
2168         /* any app nodes left, that are not optional? */
2169         for ( ; ad; ad=ad->next) {
2170                 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2171                         if ( !arglist ) 
2172                                 arglist=appcall;
2173                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2174                                         arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2175                         warns++;
2176                         return 1;
2177                 }
2178         }
2179         return 0;
2180 #else
2181         return 0;
2182 #endif
2183 }
2184
2185 void check_switch_expr(pval *item, struct argapp *apps)
2186 {
2187 #ifdef AAL_ARGCHECK
2188         /* get and clean the variable name */
2189         char *buff1, *p;
2190         struct argapp *a,*a2;
2191         struct appsetvar *v,*v2;
2192         struct argchoice *c;
2193         pval *t;
2194         
2195         p = item->u1.str;
2196         while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2197                 p++;
2198         
2199         buff1 = ast_strdupa(p);
2200
2201         while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2202                 buff1[strlen(buff1)-1] = 0;
2203         /* buff1 now contains the variable name */
2204         v = 0;
2205         for (a=apps; a; a=a->next) {
2206                 for (v=a->setvars;v;v=v->next) {
2207                         if (strcmp(v->name,buff1) == 0) {
2208                                 break;
2209                         }
2210                 }
2211                 if ( v )
2212                         break;
2213         }
2214         if (v && v->vals) {
2215                 /* we have a match, to a variable that has a set of determined values */
2216                 int def= 0;
2217                 int pat = 0;
2218                 int f1 = 0;
2219                 
2220                 /* first of all, does this switch have a default case ? */
2221                 for (t=item->u2.statements; t; t=t->next) {
2222                         if (t->type == PV_DEFAULT) {
2223                                 def =1;
2224                                 break;
2225                         }
2226                         if (t->type == PV_PATTERN) {
2227                                 pat++;
2228                         }
2229                 }
2230                 if (def || pat) /* nothing to check. All cases accounted for! */
2231                         return;
2232                 for (c=v->vals; c; c=c->next) {
2233                         f1 = 0;
2234                         for (t=item->u2.statements; t; t=t->next) {
2235                                 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2236                                         if (!strcmp(t->u1.str,c->name)) {
2237                                                 f1 = 1;
2238                                                 break;
2239                                         }
2240                                 }
2241                         }
2242                         if (!f1) {
2243                                 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2244                                                 item->filename, item->startline, item->endline, item->u1.str, c->name);
2245                                 warns++;
2246                         }
2247                 }
2248                 /* next, is there an app call in the current exten, that would set this var? */
2249                 f1 = 0;
2250                 t = current_extension->u2.statements;
2251                 if ( t && t->type == PV_STATEMENTBLOCK )
2252                         t = t->u1.statements;
2253                 for (; t && t != item; t=t->next) {
2254                         if (t->type == PV_APPLICATION_CALL) {
2255                                 /* find the application that matches the u1.str */
2256                                 for (a2=apps; a2; a2=a2->next) {
2257                                         if (strcasecmp(a2->name, t->u1.str)==0) {
2258                                                 for (v2=a2->setvars; v2; v2=v2->next) {
2259                                                         if (strcmp(v2->name, buff1) == 0) {
2260                                                                 /* found an app that sets the var */
2261                                                                 f1 = 1;
2262                                                                 break;
2263                                                         }
2264                                                 }
2265                                         }
2266                                         if (f1)
2267                                                 break;
2268                                 }
2269                         }
2270                         if (f1)
2271                                 break;
2272                 }
2273                                 
2274                 /* see if it sets the var */
2275                 if (!f1) {
2276                         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",
2277                                         item->filename, item->startline, item->endline, item->u1.str);
2278                         warns++;
2279                 }
2280         }
2281 #else
2282         pval *t,*tl=0,*p2;
2283         int def= 0;
2284         
2285         /* first of all, does this switch have a default case ? */
2286         for (t=item->u2.statements; t; t=t->next) {
2287                 if (t->type == PV_DEFAULT) {
2288                         def =1;
2289                         break;
2290                 }
2291                 tl = t;
2292         }
2293         if (def) /* nothing to check. All cases accounted for! */
2294                 return;
2295         /* if no default, warn and insert a default case at the end */
2296         p2 = tl->next = calloc(1, sizeof(struct pval));
2297         
2298         p2->type = PV_DEFAULT;
2299         p2->startline = tl->startline;
2300         p2->endline = tl->endline;
2301         p2->startcol = tl->startcol;
2302         p2->endcol = tl->endcol;
2303         p2->filename = strdup(tl->filename);
2304         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2305                         p2->filename, p2->startline, p2->endline);
2306         warns++;
2307         
2308 #endif
2309 }
2310
2311 static void check_context_names(void)
2312 {
2313         pval *i,*j;
2314         for (i=current_db; i; i=i->next) {
2315                 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2316                         for (j=i->next; j; j=j->next) {
2317                                 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2318                                         if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
2319                                         {
2320                                                 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",
2321                                                                 i->filename, i->startline, i->endline, i->u1.str,  j->filename, j->startline, j->endline);
2322                                                 warns++;
2323                                         }
2324                                 }
2325                         }
2326                 }
2327         }
2328 }
2329
2330 static void check_abstract_reference(pval *abstract_context)
2331 {
2332         pval *i,*j;
2333         /* find some context includes that reference this context */
2334         
2335
2336         /* otherwise, print out a warning */
2337         for (i=current_db; i; i=i->next) {
2338                 if (i->type == PV_CONTEXT) {
2339                         for (j=i->u2. statements; j; j=j->next) {
2340                                 if ( j->type == PV_INCLUDES ) {
2341                                         struct pval *p4;
2342                                         for (p4=j->u1.list; p4; p4=p4->next) {
2343                                                 /* for each context pointed to, find it, then find a context/label that matches the
2344                                                    target here! */
2345                                                 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2346                                                         return; /* found a match! */
2347                                         }
2348                                 }
2349                         }
2350                 }
2351         }
2352         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",
2353                         abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2354         warns++;
2355 }
2356
2357
2358 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2359 {
2360         pval *lp;
2361 #ifdef AAL_ARGCHECK
2362         struct argapp *app, *found;
2363 #endif
2364         struct pval *macro_def;
2365         struct pval *app_def;
2366
2367         char errmsg[4096];
2368         char *strp;
2369         
2370         switch (item->type) {
2371         case PV_WORD:
2372                 /* fields: item->u1.str == string associated with this (word).
2373                            item->u2.arglist  == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2374                 break;
2375                 
2376         case PV_MACRO:
2377                 /* fields: item->u1.str     == name of macro
2378                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2379                                    item->u2.arglist->u1.str  == argument
2380                                    item->u2.arglist->next   == next arg
2381
2382                                    item->u3.macro_statements == pval list of statements in macro body.
2383                 */
2384                 in_abstract_context = 0;
2385                 current_context = item;
2386                 current_extension = 0;
2387
2388                 check_macro_returns(item);
2389                 
2390                 for (lp=item->u2.arglist; lp; lp=lp->next) {
2391                 
2392                 }
2393                 check_pval(item->u3.macro_statements, apps,in_globals);
2394                 break;
2395                         
2396         case PV_CONTEXT:
2397                 /* fields: item->u1.str     == name of context
2398                            item->u2.statements == pval list of statements in context body
2399                                    item->u3.abstract == int 1 if an abstract keyword were present
2400                 */
2401                 current_context = item;
2402                 current_extension = 0;
2403                 if ( item->u3.abstract ) {
2404                         in_abstract_context = 1;
2405                         check_abstract_reference(item);
2406                 } else
2407                         in_abstract_context = 0;
2408                 check_pval(item->u2.statements, apps,in_globals);
2409                 break;
2410                         
2411         case PV_MACRO_CALL:
2412                 /* fields: item->u1.str     == name of macro to call
2413                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2414                                    item->u2.arglist->u1.str  == argument
2415                                    item->u2.arglist->next   == next arg
2416                 */
2417 #ifdef STANDALONE
2418                 /* if this is a standalone, we will need to make sure the 
2419                    localized load of extensions.conf is done */
2420                 if (!extensions_dot_conf_loaded) {
2421                         localized_pbx_load_module();
2422                         extensions_dot_conf_loaded++;
2423                 }
2424 #endif
2425                 macro_def = find_macro(item->u1.str);
2426                 if (!macro_def) {
2427 #ifdef STANDALONE
2428                         struct pbx_find_info pfiq = {.stacklen = 0 };
2429                         struct pbx_find_info pfiq2 = {.stacklen = 0 };
2430
2431                         /* look for the macro in the extensions.conf world */
2432                         pbx_find_extension(NULL, NULL, &pfiq, item->u1.str, "s", 1, NULL, NULL, E_MATCH);
2433                         
2434                         if (pfiq.status != STATUS_SUCCESS) {
2435                                 char namebuf2[256];
2436                                 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2437                                 
2438                                 /* look for the macro in the extensions.conf world */
2439                                 pbx_find_extension(NULL, NULL, &pfiq2, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2440                                 
2441                                 if (pfiq2.status == STATUS_SUCCESS) {
2442                                         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",
2443                                                         item->filename, item->startline, item->endline, item->u1.str, item->u1.str);
2444                                         warns++;
2445                                 } else {
2446                                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (Not even in the extensions.conf stuff!)\n",
2447                                                         item->filename, item->startline, item->endline, item->u1.str);
2448                                         warns++;
2449                                 }
2450                         }
2451 #else
2452                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s cannot be found in the AEL code!\n",
2453                                         item->filename, item->startline, item->endline, item->u1.str);
2454                         warns++;
2455                         
2456 #endif
2457 #ifdef THIS_IS_1DOT4
2458                         char namebuf2[256];
2459                         snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2460
2461                         /* look for the macro in the extensions.conf world */
2462                         pbx_find_extension(NULL, NULL, &pfiq, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2463                         
2464                         if (pfiq.status != STATUS_SUCCESS) {
2465                                 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",
2466                                                 item->filename, item->startline, item->endline, item->u1.str);
2467                                 warns++;
2468                         }
2469                         
2470 #endif
2471
2472                 } else if (macro_def->type != PV_MACRO) {
2473                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2474                                         item->filename, item->startline, item->endline, item->u1.str);
2475                         errs++;
2476                 } else {
2477                         /* macro_def is a MACRO, so do the args match in number? */
2478                         int hereargs = 0;
2479                         int thereargs = 0;
2480                         
2481                         for (lp=item->u2.arglist; lp; lp=lp->next) {
2482                                 hereargs++;
2483                         }
2484                         for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2485                                 thereargs++;
2486                         }
2487                         if (hereargs != thereargs ) {
2488                                 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",
2489                                                 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2490                                 errs++;
2491                         }
2492                 }
2493                 break;
2494                         
2495         case PV_APPLICATION_CALL:
2496                 /* fields: item->u1.str     == name of application to call
2497                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2498                                    item->u2.arglist->u1.str  == argument
2499                                    item->u2.arglist->next   == next arg
2500                 */
2501                 /* Need to check to see if the application is available! */
2502                 app_def = find_context(item->u1.str);
2503                 if (app_def && app_def->type == PV_MACRO) {
2504                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2505                                         item->filename, item->startline, item->endline, item->u1.str);
2506                         errs++;
2507                 }
2508                 if (strcasecmp(item->u1.str,"GotoIf") == 0
2509                         || strcasecmp(item->u1.str,"GotoIfTime") == 0
2510                         || strcasecmp(item->u1.str,"while") == 0
2511                         || strcasecmp(item->u1.str,"endwhile") == 0
2512                         || strcasecmp(item->u1.str,"random") == 0
2513                         || strcasecmp(item->u1.str,"gosub") == 0
2514                         || strcasecmp(item->u1.str,"gosubif") == 0
2515                         || strcasecmp(item->u1.str,"continuewhile") == 0
2516                         || strcasecmp(item->u1.str,"endwhile") == 0
2517                         || strcasecmp(item->u1.str,"execif") == 0
2518                         || strcasecmp(item->u1.str,"execiftime") == 0
2519                         || strcasecmp(item->u1.str,"exitwhile") == 0
2520                         || strcasecmp(item->u1.str,"goto") == 0
2521                         || strcasecmp(item->u1.str,"macro") == 0
2522                         || strcasecmp(item->u1.str,"macroexclusive") == 0
2523                         || strcasecmp(item->u1.str,"macroif") == 0
2524                         || strcasecmp(item->u1.str,"stackpop") == 0
2525                         || strcasecmp(item->u1.str,"execIf") == 0 ) {
2526                         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",
2527                                         item->filename, item->startline, item->endline, item->u1.str);
2528                         warns++;
2529                 }
2530                 if (strcasecmp(item->u1.str,"macroexit") == 0) {
2531                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
2532                                                 item->filename, item->startline, item->endline);
2533                                 item->type = PV_RETURN;
2534                                 free(item->u1.str);
2535                                 item->u1.str = 0;
2536                 }
2537                 
2538 #ifdef AAL_ARGCHECK
2539                 found = 0;
2540                 for (app=apps; app; app=app->next) {
2541                         if (strcasecmp(app->name, item->u1.str) == 0) {
2542                                 found =app;
2543                                 break;
2544                         }
2545                 }
2546                 if (!found) {
2547                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2548                                         item->filename, item->startline, item->endline, item->u1.str);
2549                         warns++;
2550                 } else
2551                         check_app_args(item, item->u2.arglist, app);
2552 #endif
2553                 break;
2554                 
2555         case PV_CASE:
2556                 /* fields: item->u1.str     == value of case
2557                            item->u2.statements == pval list of statements under the case
2558                 */
2559                 /* Make sure sequence of statements under case is terminated with  goto, return, or break */
2560                 /* find the last statement */
2561                 check_pval(item->u2.statements, apps,in_globals);
2562                 break;
2563                         
2564         case PV_PATTERN:
2565                 /* fields: item->u1.str     == value of case
2566                            item->u2.statements == pval list of statements under the case
2567                 */
2568                 /* Make sure sequence of statements under case is terminated with  goto, return, or break */
2569                 /* find the last statement */
2570                 
2571                 check_pval(item->u2.statements, apps,in_globals);
2572                 break;
2573                         
2574         case PV_DEFAULT:
2575                 /* fields: 
2576                            item->u2.statements == pval list of statements under the case
2577                 */
2578
2579                 check_pval(item->u2.statements, apps,in_globals);
2580                 break;
2581                         
2582         case PV_CATCH:
2583                 /* fields: item->u1.str     == name of extension to catch
2584                            item->u2.statements == pval list of statements in context body
2585                 */
2586                 check_pval(item->u2.statements, apps,in_globals);
2587                 break;
2588                         
2589         case PV_SWITCHES:
2590                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2591                 */
2592                 check_pval(item->u1.list, apps,in_globals);
2593                 break;
2594                         
2595         case PV_ESWITCHES:
2596                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2597                 */
2598                 check_pval(item->u1.list, apps,in_globals);
2599                 break;
2600                         
2601         case PV_INCLUDES:
2602                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2603                 */
2604                 check_pval(item->u1.list, apps,in_globals);
2605                 check_includes(item);
2606                 for (lp=item->u1.list; lp; lp=lp->next){
2607                         char *incl_context = lp->u1.str;
2608                         struct pval *that_context = find_context(incl_context);
2609
2610                         if ( lp->u2.arglist ) {
2611                                 check_timerange(lp->u2.arglist);
2612                                 check_dow(lp->u2.arglist->next);
2613                                 check_day(lp->u2.arglist->next->next);
2614                                 check_month(lp->u2.arglist->next->next->next);
2615                         }
2616                         
2617                         if (that_context) {
2618                                 find_pval_gotos(that_context->u2.statements,0);
2619                                 
2620                         }
2621                 }
2622                 break;
2623                         
2624         case PV_STATEMENTBLOCK:
2625                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
2626                 */
2627                 check_pval(item->u1.list, apps,in_globals);
2628                 break;
2629                         
2630         case PV_VARDEC:
2631                 /* fields: item->u1.str     == variable name
2632                            item->u2.val     == variable value to assign
2633                 */
2634                 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2635                 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2636                         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);
2637                         ast_expr_register_extra_error_info(errmsg);
2638                         ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2639                         ast_expr_clear_extra_error_info();
2640                         if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2641                                 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2642                                                 item->filename, item->startline, item->endline, item->u2.val);
2643                                 warns++;
2644                         }
2645                         check_expr2_input(item,item->u2.val);
2646                 }
2647                 break;
2648                         
2649         case PV_LOCALVARDEC:
2650                 /* fields: item->u1.str     == variable name
2651                            item->u2.val     == variable value to assign
2652                 */
2653                 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2654                 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);
2655                 ast_expr_register_extra_error_info(errmsg);
2656                 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2657                 ast_expr_clear_extra_error_info();
2658                 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2659                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2660                                         item->filename, item->startline, item->endline, item->u2.val);
2661                         warns++;
2662                 }
2663                 check_expr2_input(item,item->u2.val);
2664                 break;
2665                         
2666         case PV_GOTO:
2667                 /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
2668                            item->u1.list->u1.str  == where the data on a PV_WORD will always be.
2669                 */
2670                 /* don't check goto's in abstract contexts */
2671                 if ( in_abstract_context )
2672                         break;
2673                 
2674                 check_goto(item);
2675                 break;
2676                         
2677         case PV_LABEL:
2678                 /* fields: item->u1.str     == label name
2679                 */
2680                 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2681                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2682                                         item->filename, item->startline, item->endline, item->u1.str);
2683                         warns++;
2684                 }
2685
2686                 check_label(item);
2687                 break;
2688                         
2689         case PV_FOR:
2690                 /* fields: item->u1.for_init     == a string containing the initalizer
2691                            item->u2.for_test     == a string containing the loop test
2692                            item->u3.for_inc      == a string containing the loop increment
2693
2694                                    item->u4.for_statements == a pval list of statements in the for ()
2695                 */
2696                 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);
2697                 ast_expr_register_extra_error_info(errmsg);
2698
2699                 strp = strchr(item->u1.for_init, '=');
2700                 if (strp) {
2701                         ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2702                 }
2703                 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
2704                 strp = strchr(item->u3.for_inc, '=');
2705                 if (strp) {
2706                         ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2707                 }
2708                 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2709                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2710                                         item->filename, item->startline, item->endline, item->u2.for_test);
2711                         warns++;
2712                 }
2713                 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2714                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2715                                         item->filename, item->startline, item->endline, item->u3.for_inc);
2716                         warns++;
2717                 }
2718                 check_expr2_input(item,item->u2.for_test);
2719                 check_expr2_input(item,item->u3.for_inc);
2720                 
2721                 ast_expr_clear_extra_error_info();
2722                 check_pval(item->u4.for_statements, apps,in_globals);
2723                 break;
2724                         
2725         case PV_WHILE:
2726                 /* fields: item->u1.str        == the while conditional, as supplied by user
2727
2728                                    item->u2.statements == a pval list of statements in the while ()
2729                 */
2730                 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);
2731                 ast_expr_register_extra_error_info(errmsg);
2732                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2733                 ast_expr_clear_extra_error_info();
2734                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2735                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2736                                         item->filename, item->startline, item->endline, item->u1.str);
2737                         warns++;
2738                 }
2739                 check_expr2_input(item,item->u1.str);
2740                 check_pval(item->u2.statements, apps,in_globals);
2741                 break;
2742                         
2743         case PV_BREAK:
2744                 /* fields: none
2745                 */
2746                 check_break(item);
2747                 break;
2748                         
2749         case PV_RETURN:
2750                 /* fields: none
2751                 */
2752                 break;
2753                         
2754         case PV_CONTINUE:
2755                 /* fields: none
2756                 */
2757                 check_continue(item);
2758                 break;
2759                         
2760         case PV_RANDOM:
2761                 /* fields: item->u1.str        == the random number expression, as supplied by user
2762
2763                                    item->u2.statements == a pval list of statements in the if ()
2764                                    item->u3.else_statements == a pval list of statements in the else
2765                                                                                            (could be zero)
2766                 */
2767                 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);
2768                 ast_expr_register_extra_error_info(errmsg);
2769                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2770                 ast_expr_clear_extra_error_info();
2771                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2772                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2773                                         item->filename, item->startline, item->endline, item->u1.str);
2774                         warns++;
2775                 }
2776                 check_expr2_input(item,item->u1.str);
2777                 check_pval(item->u2.statements, apps,in_globals);
2778                 if (item->u3.else_statements) {
2779                         check_pval(item->u3.else_statements, apps,in_globals);
2780                 }
2781                 break;
2782
2783         case PV_IFTIME:
2784                 /* fields: item->u1.list        == the if time values, 4 of them, each in PV_WORD, linked list 
2785
2786                                    item->u2.statements == a pval list of statements in the if ()
2787                                    item->u3.else_statements == a pval list of statements in the else
2788                                                                                            (could be zero)
2789                 */
2790                 if ( item->u2.arglist ) {
2791                         check_timerange(item->u1.list);
2792                         check_dow(item->u1.list->next);
2793                         check_day(item->u1.list->next->next);
2794                         check_month(item->u1.list->next->next->next);
2795                 }
2796
2797                 check_pval(item->u2.statements, apps,in_globals);
2798                 if (item->u3.else_statements) {
2799                         check_pval(item->u3.else_statements, apps,in_globals);
2800                 }
2801                 break;
2802                         
2803         case PV_IF:
2804                 /* fields: item->u1.str        == the if conditional, as supplied by user
2805
2806                                    item->u2.statements == a pval list of statements in the if ()
2807                                    item->u3.else_statements == a pval list of statements in the else
2808                                                                                            (could be zero)
2809                 */
2810                 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);
2811                 ast_expr_register_extra_error_info(errmsg);
2812                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2813                 ast_expr_clear_extra_error_info();
2814                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2815                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2816                                         item->filename, item->startline, item->endline, item->u1.str);
2817                         warns++;
2818                 }
2819                 check_expr2_input(item,item->u1.str);
2820                 check_pval(item->u2.statements, apps,in_globals);
2821                 if (item->u3.else_statements) {
2822                         check_pval(item->u3.else_statements, apps,in_globals);
2823                 }
2824                 break;
2825                         
2826         case PV_SWITCH:
2827                 /* fields: item->u1.str        == the switch expression
2828
2829                                    item->u2.statements == a pval list of statements in the switch, 
2830                                                                                         (will be case statements, most likely!)
2831                 */
2832                 /* we can check the switch expression, see if it matches any of the app variables...
2833            if it does, then, are all the possible cases accounted for? */
2834                 check_switch_expr(item, apps);
2835                 check_pval(item->u2.statements, apps,in_globals);
2836                 break;
2837                         
2838         case PV_EXTENSION:
2839                 /* fields: item->u1.str        == the extension name, label, whatever it's called
2840
2841                                    item->u2.statements == a pval list of statements in the extension
2842                                    item->u3.hints      == a char * hint argument
2843                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
2844                 */
2845                 current_extension = item ;
2846                 
2847                 check_pval(item->u2.statements, apps,in_globals);
2848                 break;
2849                         
2850         case PV_IGNOREPAT:
2851                 /* fields: item->u1.str        == the ignorepat data
2852                 */
2853                 break;
2854                         
2855         case PV_GLOBALS:
2856                 /* fields: item->u1.statements     == pval list of statements, usually vardecs
2857                 */
2858                 in_abstract_context = 0;
2859                 check_pval(item->u1.statements, apps, 1);
2860                 break;
2861         default:
2862                 break;
2863         }
2864 }
2865
2866 void check_pval(pval *item, struct argapp *apps, int in_globals)
2867 {
2868         pval *i;
2869
2870         /* checks to do:
2871            1. Do goto's point to actual labels? 
2872            2. Do macro calls reference a macro?
2873            3. Does the number of macro args match the definition?
2874            4. Is a macro call missing its & at the front?
2875            5. Application calls-- we could check syntax for existing applications,
2876               but I need some some sort of universal description bnf for a general
2877                   sort of method for checking arguments, in number, maybe even type, at least. 
2878                   Don't want to hand code checks for hundreds of applications.
2879         */
2880         
2881         for (i=item; i; i=i->next) {
2882                 check_pval_item(i,apps,in_globals);
2883         }
2884 }
2885
2886 void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2887 {
2888         
2889 #ifdef AAL_ARGCHECK
2890         int argapp_errs =0;
2891         char *rfilename;
2892 #endif
2893         struct argapp *apps=0;
2894
2895         if (!item)
2896                 return; /* don't check an empty tree */
2897 #ifdef AAL_ARGCHECK
2898         rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2899         sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2900         
2901         apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2902 #endif
2903         current_db = item;
2904         errs = warns = notes = 0;
2905
2906         check_context_names();
2907         check_pval(item, apps, 0);
2908
2909 #ifdef AAL_ARGCHECK
2910         argdesc_destroy(apps);  /* taketh away */
2911 #endif
2912         current_db = 0;
2913
2914         *arg_errs = errs;
2915         *arg_warns = warns;
2916         *arg_notes = notes;
2917 }
2918
2919 /* =============================================================================================== */
2920 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2921 /* =============================================================================================== */
2922
2923 static int control_statement_count = 0;
2924
2925 struct ael_priority *new_prio(void)
2926 {
2927         struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2928         return x;
2929 }
2930
2931 struct ael_extension *new_exten(void)
2932 {
2933         struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2934         return x;
2935 }
2936
2937 void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten)
2938 {
2939         char *p1, *p2;
2940         
2941         if (!exten->plist) {
2942                 exten->plist = prio;
2943                 exten->plist_last = prio;
2944         } else {
2945                 exten->plist_last->next = prio;
2946                 exten->plist_last = prio;
2947         }
2948         if( !prio->exten )
2949                 prio->exten = exten; /* don't override the switch value */
2950         /* The following code will cause all priorities within an extension 
2951            to have ${EXTEN} or ${EXTEN: replaced with ~~EXTEN~~, which is
2952            set just before the first switch in an exten. The switches
2953            will muck up the original ${EXTEN} value, so we save it away
2954            and the user accesses this copy instead. */
2955         if (prio->appargs && ((mother_exten && mother_exten->has_switch) || exten->has_switch) ) {
2956                 while ((p1 = strstr(prio->appargs, "${EXTEN}"))) {
2957                         p2 = malloc(strlen(prio->appargs)+5);
2958                         *p1 = 0;
2959                         strcpy(p2, prio->appargs);
2960                         strcat(p2, "${~~EXTEN~~}");
2961                         if (*(p1+8))
2962                                 strcat(p2, p1+8);
2963                         free(prio->appargs);
2964                         prio->appargs = p2;
2965                 }
2966                 while ((p1 = strstr(prio->appargs, "${EXTEN:"))) {
2967                         p2 = malloc(strlen(prio->appargs)+5);
2968                         *p1 = 0;
2969                         strcpy(p2, prio->appargs);
2970                         strcat(p2, "${~~EXTEN~~:");
2971                         if (*(p1+8))
2972                                 strcat(p2, p1+8);
2973                         free(prio->appargs);
2974                         prio->appargs = p2;
2975                 }
2976         }
2977 }
2978
2979 void destroy_extensions(struct ael_extension *exten)
2980 {
2981         struct ael_extension *ne, *nen;
2982         for (ne=exten; ne; ne=nen) {
2983                 struct ael_priority *pe, *pen;
2984                 
2985                 if (ne->name)
2986                         free(ne->name);
2987                 
2988                 /* cidmatch fields are allocated with name, and freed when
2989                    the name field is freed. Don't do a free for this field,
2990                    unless you LIKE to see a crash! */
2991
2992                 if (ne->hints)
2993                         free(ne->hints);
2994                 
2995                 for (pe=ne->plist; pe; pe=pen) {
2996                         pen = pe->next;
2997                         if (pe->app)
2998                                 free(pe->app);
2999                         pe->app = 0;
3000                         if (pe->appargs)
3001                                 free(pe->appargs);
3002                         pe->appargs = 0;
3003                         pe->origin = 0;
3004                         pe->goto_true = 0;
3005                         pe->goto_false = 0;
3006                         free(pe);
3007                 }
3008                 nen = ne->next_exten;
3009                 ne->next_exten = 0;
3010                 ne->plist =0;
3011                 ne->plist_last = 0;
3012                 ne->next_exten = 0;
3013                 ne->loop_break = 0;
3014                 ne->loop_continue = 0;
3015                 free(ne);
3016         }
3017 }
3018
3019 static int label_inside_case(pval *label)
3020 {
3021         pval *p = label;
3022         
3023         while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
3024                 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
3025                         return 1;
3026                 }
3027
3028                 p = p->dad;
3029         }
3030         return 0;
3031 }
3032
3033 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
3034 {
3035         add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
3036         exten->next_exten = add;
3037 }
3038
3039 static void remove_spaces_before_equals(char *str)
3040 {
3041         char *p;
3042         while( str && *str && *str != '=' )
3043         {
3044                 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
3045                 {
3046                         p = str;
3047                         while( *p )
3048                         {
3049                                 *p = *(p+1);
3050                                 p++;
3051                         }
3052                 }
3053                 else
3054                         str++;
3055         }
3056 }
3057
3058 /* =============================================================================================== */
3059 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
3060 /* =============================================================================================== */
3061
3062 static void gen_match_to_pattern(char *pattern, char *result)
3063 {
3064         /* the result will be a string that will be matched by pattern */
3065         char *p=pattern, *t=result;
3066         while (*p) {
3067                 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
3068                         *t++ = '9';
3069                 else if (*p == '[') {
3070                         char *z = p+1;
3071                         while (*z != ']')
3072                                 z++;
3073                         if (*(z+1)== ']')
3074                                 z++;
3075                         *t++=*(p+1); /* use the first char in the set */
3076                         p = z;
3077                 } else {
3078                         *t++ = *p;
3079                 }
3080                 p++;
3081         }
3082         *t++ = 0; /* cap it off */
3083 }
3084
3085 /* ==== a set of routines to search for a switch statement contained in the pval description */
3086
3087 int find_switch_item(pval *item);
3088 int contains_switch(pval *item);
3089
3090
3091 int find_switch_item(pval *item)
3092 {
3093         switch ( item->type ) {
3094         case PV_LOCALVARDEC:
3095                 /* fields: item->u1.str == string associated with this (word). */
3096                 break;
3097                 
3098         case PV_WORD:
3099                 /* fields: item->u1.str == string associated with this (word). */
3100                 break;
3101                 
3102         case PV_MACRO:
3103                 /* fields: item->u1.str     == name of macro
3104                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
3105                                    item->u2.arglist->u1.str  == argument
3106                                    item->u2.arglist->next   == next arg
3107
3108                                    item->u3.macro_statements == pval list of statements in macro body.
3109                 */
3110                 /* had better not see this */
3111                 if (contains_switch(item->u3.macro_statements))
3112                         return 1;
3113                 break;
3114                         
3115         case PV_CONTEXT:
3116                 /* fields: item->u1.str     == name of context
3117                            item->u2.statements == pval list of statements in context body
3118                                    item->u3.abstract == int 1 if an abstract keyword were present
3119                 */
3120                 /* had better not see this */
3121                 if (contains_switch(item->u2.statements))
3122                         return 1;
3123                 break;
3124                         
3125         case PV_MACRO_CALL:
3126                 /* fields: item->u1.str     == name of macro to call
3127                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
3128                                    item->u2.arglist->u1.str  == argument
3129                                    item->u2.arglist->next   == next arg
3130                 */
3131                 break;
3132                         
3133         case PV_APPLICATION_CALL:
3134                 /* fields: item->u1.str     == name of application to call
3135                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
3136                                    item->u2.arglist->u1.str  == argument
3137                                    item->u2.arglist->next   == next arg
3138                 */
3139                 break;
3140                         
3141         case PV_CASE:
3142                 /* fields: item->u1.str     == value of case
3143                            item->u2.statements == pval list of statements under the case
3144                 */
3145                 /* had better not see this */
3146                 if (contains_switch(item->u2.statements))
3147                         return 1;
3148                 break;
3149                         
3150         case PV_PATTERN:
3151                 /* fields: item->u1.str     == value of case
3152                            item->u2.statements == pval list of statements under the case
3153                 */
3154                 /* had better not see this */
3155                 if (contains_switch(item->u2.statements))
3156                         return 1;
3157                 break;
3158                         
3159         case PV_DEFAULT:
3160                 /* fields: 
3161                            item->u2.statements == pval list of statements under the case
3162                 */
3163                 /* had better not see this */
3164                 if (contains_switch(item->u2.statements))
3165                         return 1;
3166                 break;
3167                         
3168         case PV_CATCH:
3169                 /* fields: item->u1.str     == name of extension to catch
3170                            item->u2.statements == pval list of statements in context body
3171                 */
3172                 /* had better not see this */
3173                 if (contains_switch(item->u2.statements))
3174                         return 1;
3175                 break;
3176                         
3177         case PV_SWITCHES:
3178                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
3179                 */
3180                 break;
3181                         
3182         case PV_ESWITCHES:
3183                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
3184                 */
3185                 break;
3186                         
3187         case PV_INCLUDES:
3188                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
3189                            item->u2.arglist  == pval list of 4 PV_WORD elements for time values
3190                 */
3191                 break;
3192                         
3193         case PV_STATEMENTBLOCK:
3194                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
3195                 */
3196                 if (contains_switch(item->u1.list) )
3197                         return 1;
3198                 break;
3199                         
3200         case PV_VARDEC:
3201                 /* fields: item->u1.str     == variable name
3202                            item->u2.val     == variable value to assign
3203                 */
3204                 break;