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