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