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