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