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