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