res_ael: Fix pattern matching against literal '+'
[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 '*': /* regex metacharacter */
763                         case '+': /* regex metacharacter */
764                                 *r++ = '\\';
765                                 /* fall through */
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         return 0;
798 }
799
800
801 static void check_expr2_input(pval *expr, char *str)
802 {
803         int spaces = strspn(str,"\t \n");
804         if ( !strncmp(str+spaces,"$[",2) ) {
805                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
806                                 expr->filename, expr->startline, expr->endline, str);
807                 warns++;
808         }
809 }
810
811 static void check_includes(pval *includes)
812 {
813         struct pval *p4;
814         for (p4=includes->u1.list; p4; p4=p4->next) {
815                 /* for each context pointed to, find it, then find a context/label that matches the
816                    target here! */
817                 char *incl_context = p4->u1.str;
818                 /* find a matching context name */
819                 struct pval *that_other_context = find_context(incl_context);
820                 if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
821                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n\
822  (You may ignore this warning if '%s' exists in extensions.conf, or is created by another module. I cannot check for those.)\n",
823                                         includes->filename, includes->startline, includes->endline, incl_context, incl_context);
824                         warns++;
825                 }
826         }
827 }
828
829
830 static void check_timerange(pval *p)
831 {
832         char *times;
833         char *e;
834         int s1, s2;
835         int e1, e2;
836
837         times = ast_strdupa(p->u1.str);
838
839         /* Star is all times */
840         if (ast_strlen_zero(times) || !strcmp(times, "*")) {
841                 return;
842         }
843         /* Otherwise expect a range */
844         e = strchr(times, '-');
845         if (!e) {
846                 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",
847                                 p->filename, p->startline, p->endline, times);
848                 warns++;
849                 return;
850         }
851         *e = '\0';
852         e++;
853         while (*e && !isdigit(*e))
854                 e++;
855         if (!*e) {
856                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
857                                 p->filename, p->startline, p->endline, p->u1.str);
858                 warns++;
859         }
860         if (sscanf(times, "%2d:%2d", &s1, &s2) != 2) {
861                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
862                                 p->filename, p->startline, p->endline, times);
863                 warns++;
864         }
865         if (sscanf(e, "%2d:%2d", &e1, &e2) != 2) {
866                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
867                                 p->filename, p->startline, p->endline, times);
868                 warns++;
869         }
870
871         s1 = s1 * 30 + s2/2;
872         if ((s1 < 0) || (s1 >= 24*30)) {
873                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
874                                 p->filename, p->startline, p->endline, times);
875                 warns++;
876         }
877         e1 = e1 * 30 + e2/2;
878         if ((e1 < 0) || (e1 >= 24*30)) {
879                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
880                                 p->filename, p->startline, p->endline, e);
881                 warns++;
882         }
883         return;
884 }
885
886 static char *days[] =
887 {
888         "sun",
889         "mon",
890         "tue",
891         "wed",
892         "thu",
893         "fri",
894         "sat",
895 };
896
897 /*! \brief  get_dow: Get day of week */
898 static void check_dow(pval *DOW)
899 {
900         char *dow;
901         char *c;
902         /* The following line is coincidence, really! */
903         int s, e;
904
905         dow = ast_strdupa(DOW->u1.str);
906
907         /* Check for all days */
908         if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
909                 return;
910         /* Get start and ending days */
911         c = strchr(dow, '-');
912         if (c) {
913                 *c = '\0';
914                 c++;
915         } else
916                 c = NULL;
917         /* Find the start */
918         s = 0;
919         while ((s < 7) && strcasecmp(dow, days[s])) s++;
920         if (s >= 7) {
921                 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",
922                                 DOW->filename, DOW->startline, DOW->endline, dow);
923                 warns++;
924         }
925         if (c) {
926                 e = 0;
927                 while ((e < 7) && strcasecmp(c, days[e])) e++;
928                 if (e >= 7) {
929                         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",
930                                         DOW->filename, DOW->startline, DOW->endline, c);
931                         warns++;
932                 }
933         } else
934                 e = s;
935 }
936
937 static void check_day(pval *DAY)
938 {
939         char *day;
940         char *c;
941         /* The following line is coincidence, really! */
942         int s, e;
943
944         day = ast_strdupa(DAY->u1.str);
945
946         /* Check for all days */
947         if (ast_strlen_zero(day) || !strcmp(day, "*")) {
948                 return;
949         }
950         /* Get start and ending days */
951         c = strchr(day, '-');
952         if (c) {
953                 *c = '\0';
954                 c++;
955         }
956         /* Find the start */
957         if (sscanf(day, "%2d", &s) != 1) {
958                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
959                                 DAY->filename, DAY->startline, DAY->endline, day);
960                 warns++;
961         }
962         else if ((s < 1) || (s > 31)) {
963                 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",
964                                 DAY->filename, DAY->startline, DAY->endline, day);
965                 warns++;
966         }
967         s--;
968         if (c) {
969                 if (sscanf(c, "%2d", &e) != 1) {
970                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
971                                         DAY->filename, DAY->startline, DAY->endline, c);
972                         warns++;
973                 }
974                 else if ((e < 1) || (e > 31)) {
975                         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",
976                                         DAY->filename, DAY->startline, DAY->endline, day);
977                         warns++;
978                 }
979                 e--;
980         } else
981                 e = s;
982 }
983
984 static char *months[] =
985 {
986         "jan",
987         "feb",
988         "mar",
989         "apr",
990         "may",
991         "jun",
992         "jul",
993         "aug",
994         "sep",
995         "oct",
996         "nov",
997         "dec",
998 };
999
1000 static void check_month(pval *MON)
1001 {
1002         char *mon;
1003         char *c;
1004         /* The following line is coincidence, really! */
1005         int s, e;
1006
1007         mon = ast_strdupa(MON->u1.str);
1008
1009         /* Check for all days */
1010         if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
1011                 return ;
1012         /* Get start and ending days */
1013         c = strchr(mon, '-');
1014         if (c) {
1015                 *c = '\0';
1016                 c++;
1017         }
1018         /* Find the start */
1019         s = 0;
1020         while ((s < 12) && strcasecmp(mon, months[s])) s++;
1021         if (s >= 12) {
1022                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1023                                 MON->filename, MON->startline, MON->endline, mon);
1024                 warns++;
1025         }
1026         if (c) {
1027                 e = 0;
1028                 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1029                 if (e >= 12) {
1030                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1031                                         MON->filename, MON->startline, MON->endline, c);
1032                         warns++;
1033                 }
1034         } else
1035                 e = s;
1036 }
1037
1038 static int check_break(pval *item)
1039 {
1040         pval *p = item;
1041
1042         while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1043                 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1044                    no sense */
1045                 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
1046                         || p->type == PV_WHILE || p->type == PV_FOR   ) {
1047                         return 1;
1048                 }
1049                 p = p->dad;
1050         }
1051         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1052                         item->filename, item->startline, item->endline);
1053         errs++;
1054
1055         return 0;
1056 }
1057
1058 static int check_continue(pval *item)
1059 {
1060         pval *p = item;
1061
1062         while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1063                 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1064                    no sense */
1065                 if( p->type == PV_WHILE || p->type == PV_FOR   ) {
1066                         return 1;
1067                 }
1068                 p = p->dad;
1069         }
1070         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1071                         item->filename, item->startline, item->endline);
1072         errs++;
1073
1074         return 0;
1075 }
1076
1077 static struct pval *in_macro(pval *item)
1078 {
1079         struct pval *curr;
1080         curr = item;
1081         while( curr ) {
1082                 if( curr->type == PV_MACRO  ) {
1083                         return curr;
1084                 }
1085                 curr = curr->dad;
1086         }
1087         return 0;
1088 }
1089
1090 static struct pval *in_context(pval *item)
1091 {
1092         struct pval *curr;
1093         curr = item;
1094         while( curr ) {
1095                 if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
1096                         return curr;
1097                 }
1098                 curr = curr->dad;
1099         }
1100         return 0;
1101 }
1102
1103
1104 /* general purpose goto finder */
1105
1106 static void check_label(pval *item)
1107 {
1108         struct pval *curr;
1109         struct pval *x;
1110         int alright = 0;
1111
1112         /* A label outside an extension just plain does not make sense! */
1113
1114         curr = item;
1115
1116         while( curr ) {
1117                 if( curr->type == PV_MACRO || curr->type == PV_EXTENSION   ) {
1118                         alright = 1;
1119                         break;
1120                 }
1121                 curr = curr->dad;
1122         }
1123         if( !alright )
1124         {
1125                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
1126                                 item->filename, item->startline, item->endline, item->u1.str);
1127                 errs++;
1128         }
1129
1130
1131         /* basically, ensure that a label is not repeated in a context. Period.
1132            The method:  well, for each label, find the first label in the context
1133            with the same name. If it's not the current label, then throw an error. */
1134
1135
1136         /* printf("==== check_label:   ====\n"); */
1137         if( !current_extension )
1138                 curr = current_context;
1139         else
1140                 curr = current_extension;
1141
1142         x = find_first_label_in_current_context((char *)item->u1.str, curr);
1143         /* 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); */
1144         if( x && x != item )
1145         {
1146                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1147                                 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1148                 errs++;
1149         }
1150         /* printf("<<<<< check_label:   ====\n"); */
1151 }
1152
1153 static pval *get_goto_target(pval *item)
1154 {
1155         /* just one item-- the label should be in the current extension */
1156         pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1157         pval *curr_cont;
1158
1159         if (!item->u1.list) {
1160                 return NULL;
1161         }
1162
1163         if (!item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1164                 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1165                         return x;
1166         }
1167
1168         curr_cont = get_contxt(item);
1169
1170         /* TWO items */
1171         if (item->u1.list->next && !item->u1.list->next->next) {
1172                 if (!strstr((item->u1.list)->u1.str,"${")
1173                         && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1174                         struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1175                                 return x;
1176                 }
1177         }
1178
1179         /* All 3 items! */
1180         if (item->u1.list->next && item->u1.list->next->next) {
1181                 /* all three */
1182                 pval *first = item->u1.list;
1183                 pval *second = item->u1.list->next;
1184                 pval *third = item->u1.list->next->next;
1185
1186                 if (!strstr((item->u1.list)->u1.str,"${")
1187                         && !strstr(item->u1.list->next->u1.str,"${")
1188                         && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1189                         struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1190                         if (!x) {
1191
1192                                 struct pval *p3;
1193                                 struct pval *that_context = find_context(item->u1.list->u1.str);
1194
1195                                 /* the target of the goto could be in an included context!! Fancy that!! */
1196                                 /* look for includes in the current context */
1197                                 if (that_context) {
1198                                         for (p3=that_context->u2.statements; p3; p3=p3->next) {
1199                                                 if (p3->type == PV_INCLUDES) {
1200                                                         struct pval *p4;
1201                                                         for (p4=p3->u1.list; p4; p4=p4->next) {
1202                                                                 /* for each context pointed to, find it, then find a context/label that matches the
1203                                                                    target here! */
1204                                                                 char *incl_context = p4->u1.str;
1205                                                                 /* find a matching context name */
1206                                                                 struct pval *that_other_context = find_context(incl_context);
1207                                                                 if (that_other_context) {
1208                                                                         struct pval *x3;
1209                                                                         x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1210                                                                         if (x3) {
1211                                                                                 return x3;
1212                                                                         }
1213                                                                 }
1214                                                         }
1215                                                 }
1216                                         }
1217                                 }
1218                         }
1219                         return x;
1220                 }
1221         }
1222         return NULL;
1223 }
1224
1225 static void check_goto(pval *item)
1226 {
1227         if (!item->u1.list) {
1228                 return;
1229         }
1230
1231         /* check for the target of the goto-- does it exist? */
1232         if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1233                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  empty label reference found!\n",
1234                                 item->filename, item->startline, item->endline);
1235                 errs++;
1236         }
1237
1238         /* just one item-- the label should be in the current extension */
1239         if (!item->u1.list->next && !strstr(item->u1.list->u1.str,"${")) {
1240                 struct pval *z = get_extension_or_contxt(item);
1241                 struct pval *x = 0;
1242                 if (z)
1243                         x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1244                 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1245                    (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1246                 if (!x) {
1247                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s exists in the current extension!\n",
1248                                         item->filename, item->startline, item->endline, item->u1.list->u1.str);
1249                         errs++;
1250                 }
1251                 else
1252                         return;
1253         }
1254
1255         /* TWO items */
1256         if (item->u1.list->next && !item->u1.list->next->next) {
1257                 /* two items */
1258                 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1259                    (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1260                 if (!strstr((item->u1.list)->u1.str,"${")
1261                         && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1262                         struct pval *z = get_contxt(item);
1263                         struct pval *x = 0;
1264
1265                         if (z)
1266                                 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1267
1268                         if (!x) {
1269                                 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",
1270                                                 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1271                                 errs++;
1272                         }
1273                         else
1274                                 return;
1275                 }
1276         }
1277
1278         /* All 3 items! */
1279         if (item->u1.list->next && item->u1.list->next->next) {
1280                 /* all three */
1281                 pval *first = item->u1.list;
1282                 pval *second = item->u1.list->next;
1283                 pval *third = item->u1.list->next->next;
1284
1285                 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1286                    (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1287                 if (!strstr((item->u1.list)->u1.str,"${")
1288                         && !strstr(item->u1.list->next->u1.str,"${")
1289                         && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1290                         struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1291                         if (!x) {
1292                                 struct pval *p3;
1293                                 struct pval *found = 0;
1294                                 struct pval *that_context = find_context(item->u1.list->u1.str);
1295
1296                                 /* the target of the goto could be in an included context!! Fancy that!! */
1297                                 /* look for includes in the current context */
1298                                 if (that_context) {
1299                                         for (p3=that_context->u2.statements; p3; p3=p3->next) {
1300                                                 if (p3->type == PV_INCLUDES) {
1301                                                         struct pval *p4;
1302                                                         for (p4=p3->u1.list; p4; p4=p4->next) {
1303                                                                 /* for each context pointed to, find it, then find a context/label that matches the
1304                                                                    target here! */
1305                                                                 char *incl_context = p4->u1.str;
1306                                                                 /* find a matching context name */
1307                                                                 struct pval *that_other_context = find_context(incl_context);
1308                                                                 if (that_other_context) {
1309                                                                         struct pval *x3;
1310                                                                         x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1311                                                                         if (x3) {
1312                                                                                 found = x3;
1313                                                                                 break;
1314                                                                         }
1315                                                                 }
1316                                                         }
1317                                                 }
1318                                         }
1319                                         if (!found) {
1320                                                 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  no label %s|%s exists in the context %s or its inclusions!\n",
1321                                                                 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1322                                                 errs++;
1323                                         } else {
1324                                                 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1325                                                 if( mac ) {    /* yes! */
1326                                                         struct pval *targ = in_context(found);
1327                                                         if( mac != targ )
1328                                                         {
1329                                                                 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",
1330                                                                                 item->filename, item->startline, item->endline);
1331                                                                 warns++;
1332                                                         }
1333                                                 }
1334                                         }
1335                                 } else {
1336                                         /* here is where code would go to check for target existence in extensions.conf files */
1337 #ifdef STANDALONE
1338                                         struct pbx_find_info pfiq = {.stacklen = 0 };
1339                                         extern int localized_pbx_load_module(void);
1340                                         /* if this is a standalone, we will need to make sure the
1341                                            localized load of extensions.conf is done */
1342                                         if (!extensions_dot_conf_loaded) {
1343                                                 localized_pbx_load_module();
1344                                                 extensions_dot_conf_loaded++;
1345                                         }
1346
1347                                         pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
1348                                                                                         atoi(third->u1.str) ? NULL : third->u1.str, NULL,
1349                                                                                         atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
1350
1351                                         if (pfiq.status != STATUS_SUCCESS) {
1352                                                 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",
1353                                                                 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1354                                                 warns++;
1355                                         }
1356 #else
1357                                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto:  Couldn't find goto target %s|%s|%s in the AEL code!\n",
1358                                                         item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1359                                         warns++;
1360 #endif
1361                                 }
1362                         } else {
1363                                 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1364                                 if( mac ) {    /* yes! */
1365                                         struct pval *targ = in_context(x);
1366                                         if( mac != targ )
1367                                         {
1368                                                 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",
1369                                                                 item->filename, item->startline, item->endline);
1370                                                 warns++;
1371                                         }
1372                                 }
1373                         }
1374                 }
1375         }
1376 }
1377
1378
1379 static void find_pval_goto_item(pval *item, int lev)
1380 {
1381         struct pval *p4;
1382
1383         if (lev>100) {
1384                 ast_log(LOG_ERROR,"find_pval_goto in infinite loop! item_type: %u\n\n", item->type);
1385                 return;
1386         }
1387
1388         switch ( item->type ) {
1389         case PV_MACRO:
1390                 /* fields: item->u1.str     == name of macro
1391                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1392                                    item->u2.arglist->u1.str  == argument
1393                                    item->u2.arglist->next   == next arg
1394
1395                                    item->u3.macro_statements == pval list of statements in macro body.
1396                 */
1397
1398                 /* printf("Descending into macro %s at line %d\n", item->u1.str, item->startline); */
1399                 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1400
1401                 break;
1402
1403         case PV_CONTEXT:
1404                 /* fields: item->u1.str     == name of context
1405                            item->u2.statements == pval list of statements in context body
1406                                    item->u3.abstract == int 1 if an abstract keyword were present
1407                 */
1408                 break;
1409
1410         case PV_CASE:
1411                 /* fields: item->u1.str     == value of case
1412                            item->u2.statements == pval list of statements under the case
1413                 */
1414                 /* printf("Descending into Case of %s\n", item->u1.str); */
1415                 find_pval_gotos(item->u2.statements,lev+1);
1416                 break;
1417
1418         case PV_PATTERN:
1419                 /* fields: item->u1.str     == value of case
1420                            item->u2.statements == pval list of statements under the case
1421                 */
1422                 /* printf("Descending into Pattern of %s\n", item->u1.str); */
1423                 find_pval_gotos(item->u2.statements,lev+1);
1424                 break;
1425
1426         case PV_DEFAULT:
1427                 /* fields:
1428                            item->u2.statements == pval list of statements under the case
1429                 */
1430                 /* printf("Descending into default\n"); */
1431                 find_pval_gotos(item->u2.statements,lev+1);
1432                 break;
1433
1434         case PV_CATCH:
1435                 /* fields: item->u1.str     == name of extension to catch
1436                            item->u2.statements == pval list of statements in context body
1437                 */
1438                 /* printf("Descending into catch of %s\n", item->u1.str); */
1439                 find_pval_gotos(item->u2.statements,lev+1);
1440                 break;
1441
1442         case PV_STATEMENTBLOCK:
1443                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
1444                 */
1445                 /* printf("Descending into statement block\n"); */
1446                 find_pval_gotos(item->u1.list,lev+1);
1447                 break;
1448
1449         case PV_GOTO:
1450                 /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
1451                            item->u1.list->u1.str  == where the data on a PV_WORD will always be.
1452                 */
1453                 check_goto(item);  /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1454                 break;
1455
1456         case PV_INCLUDES:
1457                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
1458                 */
1459                 for (p4=item->u1.list; p4; p4=p4->next) {
1460                         /* for each context pointed to, find it, then find a context/label that matches the
1461                            target here! */
1462                         char *incl_context = p4->u1.str;
1463                         /* find a matching context name */
1464                         struct pval *that_context = find_context(incl_context);
1465                         if (that_context && that_context->u2.statements) {
1466                                 /* 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); */
1467                                 find_pval_gotos(that_context->u2.statements,lev+1); /* keep working up the includes */
1468                         }
1469                 }
1470                 break;
1471
1472         case PV_FOR:
1473                 /* fields: item->u1.for_init     == a string containing the initalizer
1474                            item->u2.for_test     == a string containing the loop test
1475                            item->u3.for_inc      == a string containing the loop increment
1476
1477                                    item->u4.for_statements == a pval list of statements in the for ()
1478                 */
1479                 /* printf("Descending into for at line %d\n", item->startline); */
1480                 find_pval_gotos(item->u4.for_statements,lev+1);
1481                 break;
1482
1483         case PV_WHILE:
1484                 /* fields: item->u1.str        == the while conditional, as supplied by user
1485
1486                                    item->u2.statements == a pval list of statements in the while ()
1487                 */
1488                 /* printf("Descending into while at line %d\n", item->startline); */
1489                 find_pval_gotos(item->u2.statements,lev+1);
1490                 break;
1491
1492         case PV_RANDOM:
1493                 /* fields: item->u1.str        == the random number expression, as supplied by user
1494
1495                                    item->u2.statements == a pval list of statements in the if ()
1496                                    item->u3.else_statements == a pval list of statements in the else
1497                                                                                            (could be zero)
1498                  fall thru to PV_IF */
1499
1500         case PV_IFTIME:
1501                 /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
1502
1503                                    item->u2.statements == a pval list of statements in the if ()
1504                                    item->u3.else_statements == a pval list of statements in the else
1505                                                                                            (could be zero)
1506                 fall thru to PV_IF*/
1507         case PV_IF:
1508                 /* fields: item->u1.str        == the if conditional, as supplied by user
1509
1510                                    item->u2.statements == a pval list of statements in the if ()
1511                                    item->u3.else_statements == a pval list of statements in the else
1512                                                                                            (could be zero)
1513                 */
1514                 /* printf("Descending into random/iftime/if at line %d\n", item->startline); */
1515                 find_pval_gotos(item->u2.statements,lev+1);
1516
1517                 if (item->u3.else_statements) {
1518                         /* printf("Descending into random/iftime/if's ELSE at line %d\n", item->startline); */
1519                         find_pval_gotos(item->u3.else_statements,lev+1);
1520                 }
1521                 break;
1522
1523         case PV_SWITCH:
1524                 /* fields: item->u1.str        == the switch expression
1525
1526                                    item->u2.statements == a pval list of statements in the switch,
1527                                                                                         (will be case statements, most likely!)
1528                 */
1529                 /* printf("Descending into switch at line %d\n", item->startline); */
1530                 find_pval_gotos(item->u3.else_statements,lev+1);
1531                 break;
1532
1533         case PV_EXTENSION:
1534                 /* fields: item->u1.str        == the extension name, label, whatever it's called
1535
1536                                    item->u2.statements == a pval list of statements in the extension
1537                                    item->u3.hints      == a char * hint argument
1538                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
1539                 */
1540
1541                 /* printf("Descending into extension %s at line %d\n", item->u1.str, item->startline); */
1542                 find_pval_gotos(item->u2.statements,lev+1);
1543                 break;
1544
1545         default:
1546                 break;
1547         }
1548 }
1549
1550 static void find_pval_gotos(pval *item,int lev)
1551 {
1552         pval *i;
1553
1554         for (i=item; i; i=i->next) {
1555                 /* printf("About to call pval_goto_item, itemcount=%d, itemtype=%d\n", item_count, i->type); */
1556                 find_pval_goto_item(i, lev);
1557         }
1558 }
1559
1560
1561
1562 /* general purpose label finder */
1563 static struct pval *match_pval_item(pval *item)
1564 {
1565         pval *x;
1566
1567         switch ( item->type ) {
1568         case PV_MACRO:
1569                 /* fields: item->u1.str     == name of macro
1570                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1571                                    item->u2.arglist->u1.str  == argument
1572                                    item->u2.arglist->next   == next arg
1573
1574                                    item->u3.macro_statements == pval list of statements in macro body.
1575                 */
1576                 /* printf("    matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1577                 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1578
1579                         /* printf("MACRO: match context is: %s\n", match_context); */
1580
1581                         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 */ {
1582                                 /* printf("Returning on matching macro %s\n", match_context); */
1583                                 return item;
1584                         }
1585
1586
1587                         if (!return_on_context_match) {
1588                                 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1589                                 if ((x=match_pval(item->u3.macro_statements)))  {
1590                                         /* printf("Responded with pval match %x\n", x); */
1591                                         return x;
1592                                 }
1593                         }
1594                 } else {
1595                         /* printf("Skipping context/macro %s\n", item->u1.str); */
1596                 }
1597
1598                 break;
1599
1600         case PV_CONTEXT:
1601                 /* fields: item->u1.str     == name of context
1602                            item->u2.statements == pval list of statements in context body
1603                                    item->u3.abstract == int 1 if an abstract keyword were present
1604                 */
1605                 /* printf("    matching in CONTEXT\n"); */
1606                 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1607                         if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1608                                 /* printf("Returning on matching context %s\n", match_context); */
1609                                 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1610                                 return item;
1611                         }
1612
1613                         if (!return_on_context_match ) {
1614                                 /* printf("Descending into matching context %s\n", match_context); */
1615                                 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1616                                         /* printf("CONTEXT: Responded with pval match %x\n", x); */
1617                                         return x;
1618                                 }
1619                         }
1620                 } else {
1621                         /* printf("Skipping context/macro %s\n", item->u1.str); */
1622                 }
1623                 break;
1624
1625         case PV_CASE:
1626                 /* fields: item->u1.str     == value of case
1627                            item->u2.statements == pval list of statements under the case
1628                 */
1629                 /* printf("    matching in CASE\n"); */
1630                 if ((x=match_pval(item->u2.statements))) {
1631                         /* printf("CASE: Responded with pval match %x\n", x); */
1632                         return x;
1633                 }
1634                 break;
1635
1636         case PV_PATTERN:
1637                 /* fields: item->u1.str     == value of case
1638                            item->u2.statements == pval list of statements under the case
1639                 */
1640                 /* printf("    matching in PATTERN\n"); */
1641                 if ((x=match_pval(item->u2.statements))) {
1642                         /* printf("PATTERN: Responded with pval match %x\n", x); */
1643                         return x;
1644                 }
1645                 break;
1646
1647         case PV_DEFAULT:
1648                 /* fields:
1649                            item->u2.statements == pval list of statements under the case
1650                 */
1651                 /* printf("    matching in DEFAULT\n"); */
1652                 if ((x=match_pval(item->u2.statements))) {
1653                         /* printf("DEFAULT: Responded with pval match %x\n", x); */
1654                         return x;
1655                 }
1656                 break;
1657
1658         case PV_CATCH:
1659                 /* fields: item->u1.str     == name of extension to catch
1660                            item->u2.statements == pval list of statements in context body
1661                 */
1662                 /* printf("    matching in CATCH\n"); */
1663                 if ((x=match_pval(item->u2.statements))) {
1664                         /* printf("CATCH: Responded with pval match %x\n", x); */
1665                         return x;
1666                 }
1667                 break;
1668
1669         case PV_STATEMENTBLOCK:
1670                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
1671                 */
1672                 /* printf("    matching in STATEMENTBLOCK\n"); */
1673                 if ((x=match_pval(item->u1.list))) {
1674                         /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1675                         return x;
1676                 }
1677                 break;
1678
1679         case PV_LABEL:
1680                 /* fields: item->u1.str     == label name
1681                 */
1682                 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1683                    item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1684
1685                 if (count_labels) {
1686                         if (!strcmp(match_label, item->u1.str)) {
1687                                 label_count++;
1688                                 last_matched_label = item;
1689                         }
1690
1691                 } else {
1692                         if (!strcmp(match_label, item->u1.str)) {
1693                                 /* printf("LABEL: Responded with pval match %x\n", x); */
1694                                 return item;
1695                         }
1696                 }
1697                 break;
1698
1699         case PV_FOR:
1700                 /* fields: item->u1.for_init     == a string containing the initalizer
1701                            item->u2.for_test     == a string containing the loop test
1702                            item->u3.for_inc      == a string containing the loop increment
1703
1704                                    item->u4.for_statements == a pval list of statements in the for ()
1705                 */
1706                 /* printf("    matching in FOR\n"); */
1707                 if ((x=match_pval(item->u4.for_statements))) {
1708                         /* printf("FOR: Responded with pval match %x\n", x);*/
1709                         return x;
1710                 }
1711                 break;
1712
1713         case PV_WHILE:
1714                 /* fields: item->u1.str        == the while conditional, as supplied by user
1715
1716                                    item->u2.statements == a pval list of statements in the while ()
1717                 */
1718                 /* printf("    matching in WHILE\n"); */
1719                 if ((x=match_pval(item->u2.statements))) {
1720                         /* printf("WHILE: Responded with pval match %x\n", x); */
1721                         return x;
1722                 }
1723                 break;
1724
1725         case PV_RANDOM:
1726                 /* fields: item->u1.str        == the random number expression, as supplied by user
1727
1728                                    item->u2.statements == a pval list of statements in the if ()
1729                                    item->u3.else_statements == a pval list of statements in the else
1730                                                                                            (could be zero)
1731                  fall thru to PV_IF */
1732
1733         case PV_IFTIME:
1734                 /* fields: item->u1.list        == the time values, 4 of them, as PV_WORD structs in a list
1735
1736                                    item->u2.statements == a pval list of statements in the if ()
1737                                    item->u3.else_statements == a pval list of statements in the else
1738                                                                                            (could be zero)
1739                 fall thru to PV_IF*/
1740         case PV_IF:
1741                 /* fields: item->u1.str        == the if conditional, as supplied by user
1742
1743                                    item->u2.statements == a pval list of statements in the if ()
1744                                    item->u3.else_statements == a pval list of statements in the else
1745                                                                                            (could be zero)
1746                 */
1747                 /* printf("    matching in IF/IFTIME/RANDOM\n"); */
1748                 if ((x=match_pval(item->u2.statements))) {
1749                         return x;
1750                 }
1751                 if (item->u3.else_statements) {
1752                         if ((x=match_pval(item->u3.else_statements))) {
1753                                 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1754                                 return x;
1755                         }
1756                 }
1757                 break;
1758
1759         case PV_SWITCH:
1760                 /* fields: item->u1.str        == the switch expression
1761
1762                                    item->u2.statements == a pval list of statements in the switch,
1763                                                                                         (will be case statements, most likely!)
1764                 */
1765                 /* printf("    matching in SWITCH\n"); */
1766                 if ((x=match_pval(item->u2.statements))) {
1767                         /* printf("SWITCH: Responded with pval match %x\n", x); */
1768                         return x;
1769                 }
1770                 break;
1771
1772         case PV_EXTENSION:
1773                 /* fields: item->u1.str        == the extension name, label, whatever it's called
1774
1775                                    item->u2.statements == a pval list of statements in the extension
1776                                    item->u3.hints      == a char * hint argument
1777                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
1778                 */
1779                 /* printf("    matching in EXTENSION\n"); */
1780                 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1781                         /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1782                         if (strcmp(match_label,"1") == 0) {
1783                                 if (item->u2.statements) {
1784                                         struct pval *p5 = item->u2.statements;
1785                                         while (p5 && p5->type == PV_LABEL)  /* find the first non-label statement in this context. If it exists, there's a "1" */
1786                                                 p5 = p5->next;
1787                                         if (p5)
1788                                                 return p5;
1789                                         else
1790                                                 return 0;
1791                                 }
1792                                 else
1793                                         return 0;
1794                         }
1795
1796                         if ((x=match_pval(item->u2.statements))) {
1797                                 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1798                                 return x;
1799                         }
1800                 } else {
1801                         /* printf("Skipping exten %s\n", item->u1.str); */
1802                 }
1803                 break;
1804         default:
1805                 /* printf("    matching in default = %d\n", item->type); */
1806                 break;
1807         }
1808         return 0;
1809 }
1810
1811 struct pval *match_pval(pval *item)
1812 {
1813         pval *i;
1814
1815         for (i=item; i; i=i->next) {
1816                 pval *x;
1817                 /* printf("   -- match pval: item %d\n", i->type); */
1818
1819                 if ((x = match_pval_item(i))) {
1820                         /* printf("match_pval: returning x=%x\n", (int)x); */
1821                         return x; /* cut the search short */
1822                 }
1823         }
1824         return 0;
1825 }
1826
1827 #if 0
1828 int count_labels_in_current_context(char *label)
1829 {
1830         label_count = 0;
1831         count_labels = 1;
1832         return_on_context_match = 0;
1833         match_pval(current_context->u2.statements);
1834
1835         return label_count;
1836 }
1837 #endif
1838
1839 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1840 {
1841         /* printf("  --- Got args %s, %s\n", exten, label); */
1842         struct pval *ret;
1843         struct pval *p3;
1844
1845         count_labels = 0;
1846         return_on_context_match = 0;
1847         match_context = "*";
1848         match_exten = "*";
1849         match_label = label;
1850
1851         ret =  match_pval(curr_cont);
1852         if (ret)
1853                 return ret;
1854
1855         /* the target of the goto could be in an included context!! Fancy that!! */
1856         /* look for includes in the current context */
1857         for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1858                 if (p3->type == PV_INCLUDES) {
1859                         struct pval *p4;
1860                         for (p4=p3->u1.list; p4; p4=p4->next) {
1861                                 /* for each context pointed to, find it, then find a context/label that matches the
1862                                    target here! */
1863                                 char *incl_context = p4->u1.str;
1864                                 /* find a matching context name */
1865                                 struct pval *that_context = find_context(incl_context);
1866                                 if (that_context) {
1867                                         struct pval *x3;
1868                                         x3 = find_first_label_in_current_context(label, that_context);
1869                                         if (x3) {
1870                                                 return x3;
1871                                         }
1872                                 }
1873                         }
1874                 }
1875         }
1876         return 0;
1877 }
1878
1879 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1880 {
1881         /* printf("  --- Got args %s, %s\n", exten, label); */
1882         struct pval *ret;
1883         struct pval *p3;
1884
1885         count_labels = 0;
1886         return_on_context_match = 0;
1887         match_context = "*";
1888         match_exten = exten;
1889         match_label = label;
1890         ret =  match_pval(curr_cont->u2.statements);
1891         if (ret)
1892                 return ret;
1893
1894         /* the target of the goto could be in an included context!! Fancy that!! */
1895         /* look for includes in the current context */
1896         for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1897                 if (p3->type == PV_INCLUDES) {
1898                         struct pval *p4;
1899                         for (p4=p3->u1.list; p4; p4=p4->next) {
1900                                 /* for each context pointed to, find it, then find a context/label that matches the
1901                                    target here! */
1902                                 char *incl_context = p4->u1.str;
1903                                 /* find a matching context name */
1904                                 struct pval *that_context = find_context(incl_context);
1905                                 if (that_context) {
1906                                         struct pval *x3;
1907                                         x3 = find_label_in_current_context(exten, label, that_context);
1908                                         if (x3) {
1909                                                 return x3;
1910                                         }
1911                                 }
1912                         }
1913                 }
1914         }
1915         return 0;
1916 }
1917
1918 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1919 {
1920         /* printf("  --- Got args %s\n", label); */
1921         count_labels = 0;
1922         return_on_context_match = 0;
1923         match_context = "*";
1924         match_exten = "*";
1925         match_label = label;
1926         return match_pval(curr_ext);
1927 }
1928
1929 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1930 {
1931         /* printf("  --- Got args %s, %s, %s\n", context, exten, label); */
1932         count_labels = 0;
1933         return_on_context_match = 0;
1934
1935         match_context = context;
1936         match_exten = exten;
1937         match_label = label;
1938
1939         return match_pval(current_db);
1940 }
1941
1942
1943 struct pval *find_macro(char *name)
1944 {
1945         return_on_context_match = 1;
1946         count_labels = 0;
1947         match_context = name;
1948         match_exten = "*";  /* don't really need to set these, shouldn't be reached */
1949         match_label = "*";
1950         return match_pval(current_db);
1951 }
1952
1953 struct pval *find_context(char *name)
1954 {
1955         return_on_context_match = 1;
1956         count_labels = 0;
1957         match_context = name;
1958         match_exten = "*";  /* don't really need to set these, shouldn't be reached */
1959         match_label = "*";
1960         return match_pval(current_db);
1961 }
1962
1963 int is_float(char *arg )
1964 {
1965         char *s;
1966         for (s=arg; *s; s++) {
1967                 if (*s != '.' && (*s < '0' || *s > '9'))
1968                         return 0;
1969         }
1970         return 1;
1971 }
1972 int is_int(char *arg )
1973 {
1974         char *s;
1975         for (s=arg; *s; s++) {
1976                 if (*s < '0' || *s > '9')
1977                         return 0;
1978         }
1979         return 1;
1980 }
1981 int is_empty(char *arg)
1982 {
1983         if (!arg)
1984                 return 1;
1985         if (*arg == 0)
1986                 return 1;
1987         while (*arg) {
1988                 if (*arg != ' ' && *arg != '\t')
1989                         return 0;
1990                 arg++;
1991         }
1992         return 1;
1993 }
1994
1995 #ifdef AAL_ARGCHECK
1996 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1997 {
1998         struct argchoice *ac;
1999         char *opcop,*q,*p;
2000
2001         switch (should->dtype) {
2002         case ARGD_OPTIONSET:
2003                 if ( strstr(is->u1.str,"${") )
2004                         return 0;  /* no checking anything if there's a var reference in there! */
2005
2006                 opcop = ast_strdupa(is->u1.str);
2007
2008                 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
2009                         if ( *q == '(' ) {
2010                                 p = q+1;
2011                                 while (*p && *p != ')' )
2012                                         *p++ = '+';
2013                                 q = p+1;
2014                         }
2015                 }
2016
2017                 for (ac=app->opts; ac; ac=ac->next) {
2018                         if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2019                                 return 0;
2020                 }
2021                 for (ac=app->opts; ac; ac=ac->next) {
2022                         if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
2023                                 char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
2024
2025                                 if (p && *p == 'j') {
2026                                         ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
2027                                                         is->filename, is->startline, is->endline, app->name);
2028                                         errs++;
2029                                 }
2030
2031                                 if (p) {
2032                                         *p = '+';
2033                                         if (ac->name[1] == '(') {
2034                                                 if (*(p+1) != '(') {
2035                                                         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",
2036                                                                         is->filename, is->startline, is->endline, ac->name[0], app->name);
2037                                                         warns++;
2038                                                 }
2039                                         }
2040                                 }
2041                         }
2042                 }
2043                 for (q=opcop; *q; q++) {
2044                         if ( *q != '+' && *q != '(' && *q != ')') {
2045                                 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",
2046                                                 is->filename, is->startline, is->endline, *q, app->name);
2047                                 warns++;
2048                         }
2049                 }
2050                 return 1;
2051                 break;
2052         default:
2053                 return 0;
2054         }
2055
2056 }
2057
2058 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2059 {
2060         struct argchoice *ac;
2061         char *opcop;
2062
2063         switch (should->dtype) {
2064         case ARGD_STRING:
2065                 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2066                         return 0;
2067                 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2068                         return 1;
2069                 break;
2070
2071         case ARGD_INT:
2072                 if (is_int(is->u1.str))
2073                         return 1;
2074                 else
2075                         return 0;
2076                 break;
2077
2078         case ARGD_FLOAT:
2079                 if (is_float(is->u1.str))
2080                         return 1;
2081                 else
2082                         return 0;
2083                 break;
2084
2085         case ARGD_ENUM:
2086                 if( !is->u1.str || strlen(is->u1.str) == 0 )
2087                         return 1; /* a null arg in the call will match an enum, I guess! */
2088                 for (ac=should->choices; ac; ac=ac->next) {
2089                         if (strcmp(ac->name,is->u1.str) == 0)
2090                                 return 1;
2091                 }
2092                 return 0;
2093                 break;
2094
2095         case ARGD_OPTIONSET:
2096                 opcop = ast_strdupa(is->u1.str);
2097
2098                 for (ac=app->opts; ac; ac=ac->next) {
2099                         if (strlen(ac->name)>1  && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2100                                 return 1;
2101                 }
2102                 for (ac=app->opts; ac; ac=ac->next) {
2103                         if (strlen(ac->name)==1  ||  strchr(ac->name,'(')) {
2104                                 char *p = strchr(opcop,ac->name[0]);  /* wipe out all matched options in the user-supplied string */
2105
2106                                 if (p) {
2107                                         *p = '+';
2108                                         if (ac->name[1] == '(') {
2109                                                 if (*(p+1) == '(') {
2110                                                         char *q = p+1;
2111                                                         while (*q && *q != ')') {
2112                                                                 *q++ = '+';
2113                                                         }
2114                                                         *q = '+';
2115                                                 }
2116                                         }
2117                                 }
2118                         }
2119                 }
2120                 return 1;
2121                 break;
2122         case ARGD_VARARG:
2123                 return 1; /* matches anything */
2124                 break;
2125         }
2126         return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2127 }
2128 #endif
2129
2130 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2131 {
2132 #ifdef AAL_ARGCHECK
2133         struct argdesc *ad = app->args;
2134         pval *pa;
2135         int z;
2136
2137         for (pa = arglist; pa; pa=pa->next) {
2138                 if (!ad) {
2139                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2140                                         arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2141                         warns++;
2142                         return 1;
2143                 } else {
2144                         /* find the first entry in the ad list that will match */
2145                         do {
2146                                 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2147                                         break;
2148
2149                                 z= option_matches( ad, pa, app);
2150                                 if (!z) {
2151                                         if ( !arglist )
2152                                                 arglist=appcall;
2153
2154                                         if (ad->type == ARGD_REQUIRED) {
2155                                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2156                                                                 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2157                                                 warns++;
2158                                                 return 1;
2159                                         }
2160                                 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2161                                         option_matches_j( ad, pa, app);
2162                                 }
2163                                 ad = ad->next;
2164                         } while (ad && !z);
2165                 }
2166         }
2167         /* any app nodes left, that are not optional? */
2168         for ( ; ad; ad=ad->next) {
2169                 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2170                         if ( !arglist )
2171                                 arglist=appcall;
2172                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2173                                         arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2174                         warns++;
2175                         return 1;
2176                 }
2177         }
2178         return 0;
2179 #else
2180         return 0;
2181 #endif
2182 }
2183
2184 void check_switch_expr(pval *item, struct argapp *apps)
2185 {
2186 #ifdef AAL_ARGCHECK
2187         /* get and clean the variable name */
2188         char *buff1, *p;
2189         struct argapp *a,*a2;
2190         struct appsetvar *v,*v2;
2191         struct argchoice *c;
2192         pval *t;
2193
2194         p = item->u1.str;
2195         while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2196                 p++;
2197
2198         buff1 = ast_strdupa(p);
2199
2200         while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2201                 buff1[strlen(buff1)-1] = 0;
2202         /* buff1 now contains the variable name */
2203         v = 0;
2204         for (a=apps; a; a=a->next) {
2205                 for (v=a->setvars;v;v=v->next) {
2206                         if (strcmp(v->name,buff1) == 0) {
2207                                 break;
2208                         }
2209                 }
2210                 if ( v )
2211                         break;
2212         }
2213         if (v && v->vals) {
2214                 /* we have a match, to a variable that has a set of determined values */
2215                 int def= 0;
2216                 int pat = 0;
2217                 int f1 = 0;
2218
2219                 /* first of all, does this switch have a default case ? */
2220                 for (t=item->u2.statements; t; t=t->next) {
2221                         if (t->type == PV_DEFAULT) {
2222                                 def =1;
2223                                 break;
2224                         }
2225                         if (t->type == PV_PATTERN) {
2226                                 pat++;
2227                         }
2228                 }
2229                 if (def || pat) /* nothing to check. All cases accounted for! */
2230                         return;
2231                 for (c=v->vals; c; c=c->next) {
2232                         f1 = 0;
2233                         for (t=item->u2.statements; t; t=t->next) {
2234                                 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2235                                         if (!strcmp(t->u1.str,c->name)) {
2236                                                 f1 = 1;
2237                                                 break;
2238                                         }
2239                                 }
2240                         }
2241                         if (!f1) {
2242                                 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2243                                                 item->filename, item->startline, item->endline, item->u1.str, c->name);
2244                                 warns++;
2245                         }
2246                 }
2247                 /* next, is there an app call in the current exten, that would set this var? */
2248                 f1 = 0;
2249                 t = current_extension->u2.statements;
2250                 if ( t && t->type == PV_STATEMENTBLOCK )
2251                         t = t->u1.statements;
2252                 for (; t && t != item; t=t->next) {
2253                         if (t->type == PV_APPLICATION_CALL) {
2254                                 /* find the application that matches the u1.str */
2255                                 for (a2=apps; a2; a2=a2->next) {
2256                                         if (strcasecmp(a2->name, t->u1.str)==0) {
2257                                                 for (v2=a2->setvars; v2; v2=v2->next) {
2258                                                         if (strcmp(v2->name, buff1) == 0) {
2259                                                                 /* found an app that sets the var */
2260                                                                 f1 = 1;
2261                                                                 break;
2262                                                         }
2263                                                 }
2264                                         }
2265                                         if (f1)
2266                                                 break;
2267                                 }
2268                         }
2269                         if (f1)
2270                                 break;
2271                 }
2272
2273                 /* see if it sets the var */
2274                 if (!f1) {
2275                         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",
2276                                         item->filename, item->startline, item->endline, item->u1.str);
2277                         warns++;
2278                 }
2279         }
2280 #else
2281         pval *t,*tl=0,*p2;
2282         int def= 0;
2283
2284         /* first of all, does this switch have a default case ? */
2285         for (t=item->u2.statements; t; t=t->next) {
2286                 if (t->type == PV_DEFAULT) {
2287                         def =1;
2288                         break;
2289                 }
2290                 tl = t;
2291         }
2292         if (def) /* nothing to check. All cases accounted for! */
2293                 return;
2294         /* if no default, warn and insert a default case at the end */
2295         p2 = tl->next = calloc(1, sizeof(struct pval));
2296
2297         p2->type = PV_DEFAULT;
2298         p2->startline = tl->startline;
2299         p2->endline = tl->endline;
2300         p2->startcol = tl->startcol;
2301         p2->endcol = tl->endcol;
2302         p2->filename = strdup(tl->filename);
2303         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2304                         p2->filename, p2->startline, p2->endline);
2305         warns++;
2306
2307 #endif
2308 }
2309
2310 static void check_context_names(void)
2311 {
2312         pval *i,*j;
2313         for (i=current_db; i; i=i->next) {
2314                 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2315                         for (j=i->next; j; j=j->next) {
2316                                 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2317                                         if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
2318                                         {
2319                                                 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",
2320                                                                 i->filename, i->startline, i->endline, i->u1.str,  j->filename, j->startline, j->endline);
2321                                                 warns++;
2322                                         }
2323                                 }
2324                         }
2325                 }
2326         }
2327 }
2328
2329 static void check_abstract_reference(pval *abstract_context)
2330 {
2331         pval *i,*j;
2332         /* find some context includes that reference this context */
2333
2334
2335         /* otherwise, print out a warning */
2336         for (i=current_db; i; i=i->next) {
2337                 if (i->type == PV_CONTEXT) {
2338                         for (j=i->u2. statements; j; j=j->next) {
2339                                 if ( j->type == PV_INCLUDES ) {
2340                                         struct pval *p4;
2341                                         for (p4=j->u1.list; p4; p4=p4->next) {
2342                                                 /* for each context pointed to, find it, then find a context/label that matches the
2343                                                    target here! */
2344                                                 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2345                                                         return; /* found a match! */
2346                                         }
2347                                 }
2348                         }
2349                 }
2350         }
2351         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",
2352                         abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2353         warns++;
2354 }
2355
2356
2357 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2358 {
2359         pval *lp;
2360 #ifdef AAL_ARGCHECK
2361         struct argapp *app, *found;
2362 #endif
2363         struct pval *macro_def;
2364         struct pval *app_def;
2365
2366         char errmsg[4096];
2367         char *strp;
2368
2369         switch (item->type) {
2370         case PV_WORD:
2371                 /* fields: item->u1.str == string associated with this (word).
2372                            item->u2.arglist  == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2373                 break;
2374
2375         case PV_MACRO:
2376                 /* fields: item->u1.str     == name of macro
2377                            item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2378                                    item->u2.arglist->u1.str  == argument
2379                                    item->u2.arglist->next   == next arg
2380
2381                                    item->u3.macro_statements == pval list of statements in macro body.
2382                 */
2383                 in_abstract_context = 0;
2384                 current_context = item;
2385                 current_extension = 0;
2386
2387                 check_macro_returns(item);
2388
2389                 for (lp=item->u2.arglist; lp; lp=lp->next) {
2390
2391                 }
2392                 check_pval(item->u3.macro_statements, apps,in_globals);
2393                 break;
2394
2395         case PV_CONTEXT:
2396                 /* fields: item->u1.str     == name of context
2397                            item->u2.statements == pval list of statements in context body
2398                                    item->u3.abstract == int 1 if an abstract keyword were present
2399                 */
2400                 current_context = item;
2401                 current_extension = 0;
2402                 if ( item->u3.abstract ) {
2403                         in_abstract_context = 1;
2404                         check_abstract_reference(item);
2405                 } else
2406                         in_abstract_context = 0;
2407                 check_pval(item->u2.statements, apps,in_globals);
2408                 break;
2409
2410         case PV_MACRO_CALL:
2411                 /* fields: item->u1.str     == name of macro to call
2412                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2413                                    item->u2.arglist->u1.str  == argument
2414                                    item->u2.arglist->next   == next arg
2415                 */
2416 #ifdef STANDALONE
2417                 /* if this is a standalone, we will need to make sure the
2418                    localized load of extensions.conf is done */
2419                 if (!extensions_dot_conf_loaded) {
2420                         localized_pbx_load_module();
2421                         extensions_dot_conf_loaded++;
2422                 }
2423 #endif
2424                 macro_def = find_macro(item->u1.str);
2425                 if (!macro_def) {
2426 #ifdef STANDALONE
2427                         struct pbx_find_info pfiq = {.stacklen = 0 };
2428                         struct pbx_find_info pfiq2 = {.stacklen = 0 };
2429
2430                         /* look for the macro in the extensions.conf world */
2431                         pbx_find_extension(NULL, NULL, &pfiq, item->u1.str, "s", 1, NULL, NULL, E_MATCH);
2432
2433                         if (pfiq.status != STATUS_SUCCESS) {
2434                                 char namebuf2[256];
2435                                 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2436
2437                                 /* look for the macro in the extensions.conf world */
2438                                 pbx_find_extension(NULL, NULL, &pfiq2, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2439
2440                                 if (pfiq2.status == STATUS_SUCCESS) {
2441                                         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",
2442                                                         item->filename, item->startline, item->endline, item->u1.str, item->u1.str);
2443                                         warns++;
2444                                 } else {
2445                                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (Not even in the extensions.conf stuff!)\n",
2446                                                         item->filename, item->startline, item->endline, item->u1.str);
2447                                         warns++;
2448                                 }
2449                         }
2450 #else
2451                         ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s cannot be found in the AEL code!\n",
2452                                         item->filename, item->startline, item->endline, item->u1.str);
2453                         warns++;
2454
2455 #endif
2456 #ifdef THIS_IS_1DOT4
2457                         char namebuf2[256];
2458                         snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2459
2460                         /* look for the macro in the extensions.conf world */
2461                         pbx_find_extension(NULL, NULL, &pfiq, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2462
2463                         if (pfiq.status != STATUS_SUCCESS) {
2464                                 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",
2465                                                 item->filename, item->startline, item->endline, item->u1.str);
2466                                 warns++;
2467                         }
2468
2469 #endif
2470
2471                 } else if (macro_def->type != PV_MACRO) {
2472                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2473                                         item->filename, item->startline, item->endline, item->u1.str);
2474                         errs++;
2475                 } else {
2476                         /* macro_def is a MACRO, so do the args match in number? */
2477                         int hereargs = 0;
2478                         int thereargs = 0;
2479
2480                         for (lp=item->u2.arglist; lp; lp=lp->next) {
2481                                 hereargs++;
2482                         }
2483                         for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2484                                 thereargs++;
2485                         }
2486                         if (hereargs != thereargs ) {
2487                                 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",
2488                                                 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2489                                 errs++;
2490                         }
2491                 }
2492                 break;
2493
2494         case PV_APPLICATION_CALL:
2495                 /* fields: item->u1.str     == name of application to call
2496                            item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2497                                    item->u2.arglist->u1.str  == argument
2498                                    item->u2.arglist->next   == next arg
2499                 */
2500                 /* Need to check to see if the application is available! */
2501                 app_def = find_context(item->u1.str);
2502                 if (app_def && app_def->type == PV_MACRO) {
2503                         ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2504                                         item->filename, item->startline, item->endline, item->u1.str);
2505                         errs++;
2506                 }
2507                 if (strcasecmp(item->u1.str,"GotoIf") == 0
2508                         || strcasecmp(item->u1.str,"GotoIfTime") == 0
2509                         || strcasecmp(item->u1.str,"while") == 0
2510                         || strcasecmp(item->u1.str,"endwhile") == 0
2511                         || strcasecmp(item->u1.str,"random") == 0
2512                         || strcasecmp(item->u1.str,"gosub") == 0
2513                         || strcasecmp(item->u1.str,"gosubif") == 0
2514                         || strcasecmp(item->u1.str,"continuewhile") == 0
2515                         || strcasecmp(item->u1.str,"endwhile") == 0
2516                         || strcasecmp(item->u1.str,"execif") == 0
2517                         || strcasecmp(item->u1.str,"execiftime") == 0
2518                         || strcasecmp(item->u1.str,"exitwhile") == 0
2519                         || strcasecmp(item->u1.str,"goto") == 0
2520                         || strcasecmp(item->u1.str,"macro") == 0
2521                         || strcasecmp(item->u1.str,"macroexclusive") == 0
2522                         || strcasecmp(item->u1.str,"macroif") == 0
2523                         || strcasecmp(item->u1.str,"stackpop") == 0
2524                         || strcasecmp(item->u1.str,"execIf") == 0 ) {
2525                         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",
2526                                         item->filename, item->startline, item->endline, item->u1.str);
2527                         warns++;
2528                 }
2529                 if (strcasecmp(item->u1.str,"macroexit") == 0) {
2530                                 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
2531                                                 item->filename, item->startline, item->endline);
2532                                 item->type = PV_RETURN;
2533                                 free(item->u1.str);
2534                                 item->u1.str = 0;
2535                 }
2536
2537 #ifdef AAL_ARGCHECK
2538                 found = 0;
2539                 for (app=apps; app; app=app->next) {
2540                         if (strcasecmp(app->name, item->u1.str) == 0) {
2541                                 found =app;
2542                                 break;
2543                         }
2544                 }
2545                 if (!found) {
2546                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2547                                         item->filename, item->startline, item->endline, item->u1.str);
2548                         warns++;
2549                 } else
2550                         check_app_args(item, item->u2.arglist, app);
2551 #endif
2552                 break;
2553
2554         case PV_CASE:
2555                 /* fields: item->u1.str     == value of case
2556                            item->u2.statements == pval list of statements under the case
2557                 */
2558                 /* Make sure sequence of statements under case is terminated with  goto, return, or break */
2559                 /* find the last statement */
2560                 check_pval(item->u2.statements, apps,in_globals);
2561                 break;
2562
2563         case PV_PATTERN:
2564                 /* fields: item->u1.str     == value of case
2565                            item->u2.statements == pval list of statements under the case
2566                 */
2567                 /* Make sure sequence of statements under case is terminated with  goto, return, or break */
2568                 /* find the last statement */
2569
2570                 check_pval(item->u2.statements, apps,in_globals);
2571                 break;
2572
2573         case PV_DEFAULT:
2574                 /* fields:
2575                            item->u2.statements == pval list of statements under the case
2576                 */
2577
2578                 check_pval(item->u2.statements, apps,in_globals);
2579                 break;
2580
2581         case PV_CATCH:
2582                 /* fields: item->u1.str     == name of extension to catch
2583                            item->u2.statements == pval list of statements in context body
2584                 */
2585                 check_pval(item->u2.statements, apps,in_globals);
2586                 break;
2587
2588         case PV_SWITCHES:
2589                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2590                 */
2591                 check_pval(item->u1.list, apps,in_globals);
2592                 break;
2593
2594         case PV_ESWITCHES:
2595                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2596                 */
2597                 check_pval(item->u1.list, apps,in_globals);
2598                 break;
2599
2600         case PV_INCLUDES:
2601                 /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
2602                 */
2603                 check_pval(item->u1.list, apps,in_globals);
2604                 check_includes(item);
2605                 for (lp=item->u1.list; lp; lp=lp->next){
2606                         char *incl_context = lp->u1.str;
2607                         struct pval *that_context = find_context(incl_context);
2608
2609                         if ( lp->u2.arglist ) {
2610                                 check_timerange(lp->u2.arglist);
2611                                 check_dow(lp->u2.arglist->next);
2612                                 check_day(lp->u2.arglist->next->next);
2613                                 check_month(lp->u2.arglist->next->next->next);
2614                         }
2615
2616                         if (that_context) {
2617                                 find_pval_gotos(that_context->u2.statements,0);
2618
2619                         }
2620                 }
2621                 break;
2622
2623         case PV_STATEMENTBLOCK:
2624                 /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
2625                 */
2626                 check_pval(item->u1.list, apps,in_globals);
2627                 break;
2628
2629         case PV_VARDEC:
2630                 /* fields: item->u1.str     == variable name
2631                            item->u2.val     == variable value to assign
2632                 */
2633                 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2634                 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2635                         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);
2636                         ast_expr_register_extra_error_info(errmsg);
2637                         ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2638                         ast_expr_clear_extra_error_info();
2639                         if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2640                                 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2641                                                 item->filename, item->startline, item->endline, item->u2.val);
2642                                 warns++;
2643                         }
2644                         check_expr2_input(item,item->u2.val);
2645                 }
2646                 break;
2647
2648         case PV_LOCALVARDEC:
2649                 /* fields: item->u1.str     == variable name
2650                            item->u2.val     == variable value to assign
2651                 */
2652                 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2653                 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);
2654                 ast_expr_register_extra_error_info(errmsg);
2655                 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2656                 ast_expr_clear_extra_error_info();
2657                 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2658                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2659                                         item->filename, item->startline, item->endline, item->u2.val);
2660                         warns++;
2661                 }
2662                 check_expr2_input(item,item->u2.val);
2663                 break;
2664
2665         case PV_GOTO:
2666                 /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
2667                            item->u1.list->u1.str  == where the data on a PV_WORD will always be.
2668                 */
2669                 /* don't check goto's in abstract contexts */
2670                 if ( in_abstract_context )
2671                         break;
2672
2673                 check_goto(item);
2674                 break;
2675
2676         case PV_LABEL:
2677                 /* fields: item->u1.str     == label name
2678                 */
2679                 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2680                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2681                                         item->filename, item->startline, item->endline, item->u1.str);
2682                         warns++;
2683                 }
2684
2685                 check_label(item);
2686                 break;
2687
2688         case PV_FOR:
2689                 /* fields: item->u1.for_init     == a string containing the initalizer
2690                            item->u2.for_test     == a string containing the loop test
2691                            item->u3.for_inc      == a string containing the loop increment
2692
2693                                    item->u4.for_statements == a pval list of statements in the for ()
2694                 */
2695                 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);
2696                 ast_expr_register_extra_error_info(errmsg);
2697
2698                 strp = strchr(item->u1.for_init, '=');
2699                 if (strp) {
2700                         ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2701                 }
2702                 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
2703                 strp = strchr(item->u3.for_inc, '=');
2704                 if (strp) {
2705                         ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2706                 }
2707                 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2708                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2709                                         item->filename, item->startline, item->endline, item->u2.for_test);
2710                         warns++;
2711                 }
2712                 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
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->u3.for_inc);
2715                         warns++;
2716                 }
2717                 check_expr2_input(item,item->u2.for_test);
2718                 check_expr2_input(item,item->u3.for_inc);
2719
2720                 ast_expr_clear_extra_error_info();
2721                 check_pval(item->u4.for_statements, apps,in_globals);
2722                 break;
2723
2724         case PV_WHILE:
2725                 /* fields: item->u1.str        == the while conditional, as supplied by user
2726
2727                                    item->u2.statements == a pval list of statements in the while ()
2728                 */
2729                 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);
2730                 ast_expr_register_extra_error_info(errmsg);
2731                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2732                 ast_expr_clear_extra_error_info();
2733                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2734                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2735                                         item->filename, item->startline, item->endline, item->u1.str);
2736                         warns++;
2737                 }
2738                 check_expr2_input(item,item->u1.str);
2739                 check_pval(item->u2.statements, apps,in_globals);
2740                 break;
2741
2742         case PV_BREAK:
2743                 /* fields: none
2744                 */
2745                 check_break(item);
2746                 break;
2747
2748         case PV_RETURN:
2749                 /* fields: none
2750                 */
2751                 break;
2752
2753         case PV_CONTINUE:
2754                 /* fields: none
2755                 */
2756                 check_continue(item);
2757                 break;
2758
2759         case PV_RANDOM:
2760                 /* fields: item->u1.str        == the random number expression, as supplied by user
2761
2762                                    item->u2.statements == a pval list of statements in the if ()
2763                                    item->u3.else_statements == a pval list of statements in the else
2764                                                                                            (could be zero)
2765                 */
2766                 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);
2767                 ast_expr_register_extra_error_info(errmsg);
2768                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2769                 ast_expr_clear_extra_error_info();
2770                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2771                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2772                                         item->filename, item->startline, item->endline, item->u1.str);
2773                         warns++;
2774                 }
2775                 check_expr2_input(item,item->u1.str);
2776                 check_pval(item->u2.statements, apps,in_globals);
2777                 if (item->u3.else_statements) {
2778                         check_pval(item->u3.else_statements, apps,in_globals);
2779                 }
2780                 break;
2781
2782         case PV_IFTIME:
2783                 /* fields: item->u1.list        == the if time values, 4 of them, each in PV_WORD, linked list
2784
2785                                    item->u2.statements == a pval list of statements in the if ()
2786                                    item->u3.else_statements == a pval list of statements in the else
2787                                                                                            (could be zero)
2788                 */
2789                 if ( item->u2.arglist ) {
2790                         check_timerange(item->u1.list);
2791                         check_dow(item->u1.list->next);
2792                         check_day(item->u1.list->next->next);
2793                         check_month(item->u1.list->next->next->next);
2794                 }
2795
2796                 check_pval(item->u2.statements, apps,in_globals);
2797                 if (item->u3.else_statements) {
2798                         check_pval(item->u3.else_statements, apps,in_globals);
2799                 }
2800                 break;
2801
2802         case PV_IF:
2803                 /* fields: item->u1.str        == the if conditional, as supplied by user
2804
2805                                    item->u2.statements == a pval list of statements in the if ()
2806                                    item->u3.else_statements == a pval list of statements in the else
2807                                                                                            (could be zero)
2808                 */
2809                 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);
2810                 ast_expr_register_extra_error_info(errmsg);
2811                 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2812                 ast_expr_clear_extra_error_info();
2813                 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2814                         ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2815                                         item->filename, item->startline, item->endline, item->u1.str);
2816                         warns++;
2817                 }
2818                 check_expr2_input(item,item->u1.str);
2819                 check_pval(item->u2.statements, apps,in_globals);
2820                 if (item->u3.else_statements) {
2821                         check_pval(item->u3.else_statements, apps,in_globals);
2822                 }
2823                 break;
2824
2825         case PV_SWITCH:
2826                 /* fields: item->u1.str        == the switch expression
2827
2828                                    item->u2.statements == a pval list of statements in the switch,
2829                                                                                         (will be case statements, most likely!)
2830                 */
2831                 /* we can check the switch expression, see if it matches any of the app variables...
2832            if it does, then, are all the possible cases accounted for? */
2833                 check_switch_expr(item, apps);
2834                 check_pval(item->u2.statements, apps,in_globals);
2835                 break;
2836
2837         case PV_EXTENSION:
2838                 /* fields: item->u1.str        == the extension name, label, whatever it's called
2839
2840                                    item->u2.statements == a pval list of statements in the extension
2841                                    item->u3.hints      == a char * hint argument
2842                                    item->u4.regexten   == an int boolean. non-zero says that regexten was specified
2843                 */
2844                 current_extension = item ;
2845
2846                 check_pval(item->u2.statements, apps,in_globals);
2847                 break;
2848
2849         case PV_IGNOREPAT:
2850                 /* fields: item->u1.str        == the ignorepat data
2851                 */
2852                 break;
2853
2854         case PV_GLOBALS:
2855                 /* fields: item->u1.statements     == pval list of statements, usually vardecs
2856                 */
2857                 in_abstract_context = 0;
2858                 check_pval(item->u1.statements, apps, 1);
2859                 break;
2860         default:
2861                 break;
2862         }
2863 }
2864
2865 void check_pval(pval *item, struct argapp *apps, int in_globals)
2866 {
2867         pval *i;
2868
2869         /* checks to do:
2870            1. Do goto's point to actual labels?
2871            2. Do macro calls reference a macro?
2872            3. Does the number of macro args match the definition?
2873            4. Is a macro call missing its & at the front?
2874            5. Application calls-- we could check syntax for existing applications,
2875               but I need some sort of universal description bnf for a general
2876                   sort of method for checking arguments, in number, maybe even type, at least.