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