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