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