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