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