Remove debug output.
[asterisk-bristuff.git] / pbx / pbx_ael.c
blobbc0eeca2fb42a55bc032f64b1b1c84798bdf781b
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Digium, Inc.
6 * Steve Murphy <murf@parsetree.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
25 #include "asterisk.h"
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <regex.h>
37 #include <sys/stat.h>
39 #include "asterisk/pbx.h"
40 #include "asterisk/config.h"
41 #include "asterisk/module.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/app.h"
45 #include "asterisk/callerid.h"
46 #include "asterisk/ael_structs.h"
47 #ifdef AAL_ARGCHECK
48 #include "asterisk/argdesc.h"
49 #endif
51 static char expr_output[2096];
53 /* these functions are in ../ast_expr2.fl */
55 #define DEBUG_READ (1 << 0)
56 #define DEBUG_TOKENS (1 << 1)
57 #define DEBUG_MACROS (1 << 2)
58 #define DEBUG_CONTEXTS (1 << 3)
60 static char *config = "extensions.ael";
61 static char *registrar = "pbx_ael";
62 static int pbx_load_module(void);
64 static int errs, warns;
65 static int notes;
67 #ifndef AAL_ARGCHECK
68 /* for the time being, short circuit all the AAL related structures
69 without permanently removing the code; after/during the AAL
70 development, this code can be properly re-instated
73 /* null definitions for structs passed down the infrastructure */
74 struct argapp
76 struct argapp *next;
79 #endif
81 #ifdef AAL_ARGCHECK
82 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
83 int option_matches( struct argdesc *should, pval *is, struct argapp *app);
84 int ael_is_funcname(char *name);
85 #endif
87 int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
88 void check_pval(pval *item, struct argapp *apps, int in_globals);
89 void check_pval_item(pval *item, struct argapp *apps, int in_globals);
90 void check_switch_expr(pval *item, struct argapp *apps);
91 void ast_expr_register_extra_error_info(char *errmsg);
92 void ast_expr_clear_extra_error_info(void);
93 int ast_expr(char *expr, char *buf, int length);
94 struct pval *find_macro(char *name);
95 struct pval *find_context(char *name);
96 struct pval *find_context(char *name);
97 struct pval *find_macro(char *name);
98 struct ael_priority *new_prio(void);
99 struct ael_extension *new_exten(void);
100 void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten);
101 void destroy_extensions(struct ael_extension *exten);
102 static void linkexten(struct ael_extension *exten, struct ael_extension *add);
103 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *context );
104 void set_priorities(struct ael_extension *exten);
105 void add_extensions(struct ael_extension *exten);
106 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root);
107 void destroy_pval(pval *item);
108 void destroy_pval_item(pval *item);
109 int is_float(char *arg );
110 int is_int(char *arg );
111 int is_empty(char *arg);
112 static pval *current_db=0;
113 static pval *current_context=0;
114 static pval *current_extension=0;
116 static const char *match_context;
117 static const char *match_exten;
118 static const char *match_label;
119 static int in_abstract_context;
120 static int count_labels; /* true, put matcher in label counting mode */
121 static int label_count; /* labels are only meant to be counted in a context or exten */
122 static int return_on_context_match;
123 static pval *last_matched_label;
124 struct pval *match_pval(pval *item);
125 static void check_timerange(pval *p);
126 static void check_dow(pval *DOW);
127 static void check_day(pval *DAY);
128 static void check_month(pval *MON);
129 static void check_expr2_input(pval *expr, char *str);
130 static int extension_matches(pval *here, const char *exten, const char *pattern);
131 static void check_goto(pval *item);
132 static void find_pval_goto_item(pval *item, int lev);
133 static void find_pval_gotos(pval *item, int lev);
135 static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
136 static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
137 static void print_pval_list(FILE *fin, pval *item, int depth);
139 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
140 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
141 static pval *get_goto_target(pval *item);
142 static int label_inside_case(pval *label);
143 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
144 static void fix_gotos_in_extensions(struct ael_extension *exten);
145 static pval *get_extension_or_contxt(pval *p);
146 static pval *get_contxt(pval *p);
147 static void remove_spaces_before_equals(char *str);
148 static void substitute_commas(char *str);
150 /* I am adding this code to substitute commas with vertbars in the args to apps */
151 static void substitute_commas(char *str)
153 char *p = str;
155 while (p && *p)
157 if (*p == ',' && ((p != str && *(p-1) != '\\')
158 || p == str))
159 *p = '|';
160 if (*p == '\\' && *(p+1) == ',') { /* learning experience: the '\,' is turned into just ',' by pbx_config; So we need to do the same */
161 char *q = p;
162 while (*q) { /* move the ',' and everything after it up 1 char */
163 *q = *(q+1);
164 q++;
167 p++;
172 /* PRETTY PRINTER FOR AEL: ============================================================================= */
174 static void print_pval(FILE *fin, pval *item, int depth)
176 int i;
177 pval *lp;
179 for (i=0; i<depth; i++) {
180 fprintf(fin, "\t"); /* depth == indentation */
183 switch ( item->type ) {
184 case PV_WORD:
185 fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
186 break;
188 case PV_MACRO:
189 fprintf(fin,"macro %s(", item->u1.str);
190 for (lp=item->u2.arglist; lp; lp=lp->next) {
191 if (lp != item->u2.arglist )
192 fprintf(fin,", ");
193 fprintf(fin,"%s", lp->u1.str);
195 fprintf(fin,") {\n");
196 print_pval_list(fin,item->u3.macro_statements,depth+1);
197 for (i=0; i<depth; i++) {
198 fprintf(fin,"\t"); /* depth == indentation */
200 fprintf(fin,"};\n\n");
201 break;
203 case PV_CONTEXT:
204 if ( item->u3.abstract )
205 fprintf(fin,"abstract context %s {\n", item->u1.str);
206 else
207 fprintf(fin,"context %s {\n", item->u1.str);
208 print_pval_list(fin,item->u2.statements,depth+1);
209 for (i=0; i<depth; i++) {
210 fprintf(fin,"\t"); /* depth == indentation */
212 fprintf(fin,"};\n\n");
213 break;
215 case PV_MACRO_CALL:
216 fprintf(fin,"&%s(", item->u1.str);
217 for (lp=item->u2.arglist; lp; lp=lp->next) {
218 if ( lp != item->u2.arglist )
219 fprintf(fin,", ");
220 fprintf(fin,"%s", lp->u1.str);
222 fprintf(fin,");\n");
223 break;
225 case PV_APPLICATION_CALL:
226 fprintf(fin,"%s(", item->u1.str);
227 for (lp=item->u2.arglist; lp; lp=lp->next) {
228 if ( lp != item->u2.arglist )
229 fprintf(fin,",");
230 fprintf(fin,"%s", lp->u1.str);
232 fprintf(fin,");\n");
233 break;
235 case PV_CASE:
236 fprintf(fin,"case %s:\n", item->u1.str);
237 print_pval_list(fin,item->u2.statements, depth+1);
238 break;
240 case PV_PATTERN:
241 fprintf(fin,"pattern %s:\n", item->u1.str);
242 print_pval_list(fin,item->u2.statements, depth+1);
243 break;
245 case PV_DEFAULT:
246 fprintf(fin,"default:\n");
247 print_pval_list(fin,item->u2.statements, depth+1);
248 break;
250 case PV_CATCH:
251 fprintf(fin,"catch %s {\n", item->u1.str);
252 print_pval_list(fin,item->u2.statements, depth+1);
253 for (i=0; i<depth; i++) {
254 fprintf(fin,"\t"); /* depth == indentation */
256 fprintf(fin,"};\n");
257 break;
259 case PV_SWITCHES:
260 fprintf(fin,"switches {\n");
261 print_pval_list(fin,item->u1.list,depth+1);
262 for (i=0; i<depth; i++) {
263 fprintf(fin,"\t"); /* depth == indentation */
265 fprintf(fin,"};\n");
266 break;
268 case PV_ESWITCHES:
269 fprintf(fin,"eswitches {\n");
270 print_pval_list(fin,item->u1.list,depth+1);
271 for (i=0; i<depth; i++) {
272 fprintf(fin,"\t"); /* depth == indentation */
274 fprintf(fin,"};\n");
275 break;
277 case PV_INCLUDES:
278 fprintf(fin,"includes {\n");
279 for (lp=item->u1.list; lp; lp=lp->next) {
280 for (i=0; i<depth+1; i++) {
281 fprintf(fin,"\t"); /* depth == indentation */
283 fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
284 if ( lp->u2.arglist )
285 fprintf(fin,"|%s|%s|%s|%s",
286 lp->u2.arglist->u1.str,
287 lp->u2.arglist->next->u1.str,
288 lp->u2.arglist->next->next->u1.str,
289 lp->u2.arglist->next->next->next->u1.str
291 fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
294 print_pval_list(fin,item->u1.list,depth+1);
295 for (i=0; i<depth; i++) {
296 fprintf(fin,"\t"); /* depth == indentation */
298 fprintf(fin,"};\n");
299 break;
301 case PV_STATEMENTBLOCK:
302 fprintf(fin,"{\n");
303 print_pval_list(fin,item->u1.list, depth+1);
304 for (i=0; i<depth; i++) {
305 fprintf(fin,"\t"); /* depth == indentation */
307 fprintf(fin,"};\n");
308 break;
310 case PV_VARDEC:
311 fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
312 break;
314 case PV_GOTO:
315 fprintf(fin,"goto %s", item->u1.list->u1.str);
316 if ( item->u1.list->next )
317 fprintf(fin,"|%s", item->u1.list->next->u1.str);
318 if ( item->u1.list->next && item->u1.list->next->next )
319 fprintf(fin,"|%s", item->u1.list->next->next->u1.str);
320 fprintf(fin,"\n");
321 break;
323 case PV_LABEL:
324 fprintf(fin,"%s:\n", item->u1.str);
325 break;
327 case PV_FOR:
328 fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
329 print_pval_list(fin,item->u4.for_statements,depth+1);
330 break;
332 case PV_WHILE:
333 fprintf(fin,"while (%s)\n", item->u1.str);
334 print_pval_list(fin,item->u2.statements,depth+1);
335 break;
337 case PV_BREAK:
338 fprintf(fin,"break;\n");
339 break;
341 case PV_RETURN:
342 fprintf(fin,"return;\n");
343 break;
345 case PV_CONTINUE:
346 fprintf(fin,"continue;\n");
347 break;
349 case PV_RANDOM:
350 case PV_IFTIME:
351 case PV_IF:
352 if ( item->type == PV_IFTIME ) {
354 fprintf(fin,"ifTime ( %s|%s|%s|%s )\n",
355 item->u1.list->u1.str,
356 item->u1.list->next->u1.str,
357 item->u1.list->next->next->u1.str,
358 item->u1.list->next->next->next->u1.str
360 } else if ( item->type == PV_RANDOM ) {
361 fprintf(fin,"random ( %s )\n", item->u1.str );
362 } else
363 fprintf(fin,"if ( %s )\n", item->u1.str);
364 if ( item->u2.statements && item->u2.statements->next ) {
365 for (i=0; i<depth; i++) {
366 fprintf(fin,"\t"); /* depth == indentation */
368 fprintf(fin,"{\n");
369 print_pval_list(fin,item->u2.statements,depth+1);
370 for (i=0; i<depth; i++) {
371 fprintf(fin,"\t"); /* depth == indentation */
373 if ( item->u3.else_statements )
374 fprintf(fin,"}\n");
375 else
376 fprintf(fin,"};\n");
377 } else if (item->u2.statements ) {
378 print_pval_list(fin,item->u2.statements,depth+1);
379 } else {
380 if (item->u3.else_statements )
381 fprintf(fin, " {} ");
382 else
383 fprintf(fin, " {}; ");
385 if ( item->u3.else_statements ) {
386 for (i=0; i<depth; i++) {
387 fprintf(fin,"\t"); /* depth == indentation */
389 fprintf(fin,"else\n");
390 print_pval_list(fin,item->u3.else_statements, depth);
392 break;
394 case PV_SWITCH:
395 fprintf(fin,"switch( %s ) {\n", item->u1.str);
396 print_pval_list(fin,item->u2.statements,depth+1);
397 for (i=0; i<depth; i++) {
398 fprintf(fin,"\t"); /* depth == indentation */
400 fprintf(fin,"}\n");
401 break;
403 case PV_EXTENSION:
404 if ( item->u4.regexten )
405 fprintf(fin, "regexten ");
406 if ( item->u3.hints )
407 fprintf(fin,"hints(%s) ", item->u3.hints);
409 fprintf(fin,"%s => \n", item->u1.str);
410 print_pval_list(fin,item->u2.statements,depth+1);
411 break;
413 case PV_IGNOREPAT:
414 fprintf(fin,"ignorepat => %s\n", item->u1.str);
415 break;
417 case PV_GLOBALS:
418 fprintf(fin,"globals {\n");
419 print_pval_list(fin,item->u1.statements,depth+1);
420 for (i=0; i<depth; i++) {
421 fprintf(fin,"\t"); /* depth == indentation */
423 fprintf(fin,"}\n");
424 break;
428 static void print_pval_list(FILE *fin, pval *item, int depth)
430 pval *i;
432 for (i=item; i; i=i->next) {
433 print_pval(fin, i, depth);
437 #if 0
438 static void ael2_print(char *fname, pval *tree)
440 FILE *fin = fopen(fname,"w");
441 if ( !fin ) {
442 ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
443 return;
445 print_pval_list(fin, tree, 0);
446 fclose(fin);
448 #endif
451 /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL: ============================================================================= */
453 void traverse_pval_template(pval *item, int depth);
454 void traverse_pval_item_template(pval *item, int depth);
457 void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
458 but you may not need it */
460 pval *lp;
462 switch ( item->type ) {
463 case PV_WORD:
464 /* fields: item->u1.str == string associated with this (word). */
465 break;
467 case PV_MACRO:
468 /* fields: item->u1.str == name of macro
469 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
470 item->u2.arglist->u1.str == argument
471 item->u2.arglist->next == next arg
473 item->u3.macro_statements == pval list of statements in macro body.
475 for (lp=item->u2.arglist; lp; lp=lp->next) {
478 traverse_pval_item_template(item->u3.macro_statements,depth+1);
479 break;
481 case PV_CONTEXT:
482 /* fields: item->u1.str == name of context
483 item->u2.statements == pval list of statements in context body
484 item->u3.abstract == int 1 if an abstract keyword were present
486 traverse_pval_item_template(item->u2.statements,depth+1);
487 break;
489 case PV_MACRO_CALL:
490 /* fields: item->u1.str == name of macro to call
491 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
492 item->u2.arglist->u1.str == argument
493 item->u2.arglist->next == next arg
495 for (lp=item->u2.arglist; lp; lp=lp->next) {
497 break;
499 case PV_APPLICATION_CALL:
500 /* fields: item->u1.str == name of application to call
501 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
502 item->u2.arglist->u1.str == argument
503 item->u2.arglist->next == next arg
505 for (lp=item->u2.arglist; lp; lp=lp->next) {
507 break;
509 case PV_CASE:
510 /* fields: item->u1.str == value of case
511 item->u2.statements == pval list of statements under the case
513 traverse_pval_item_template(item->u2.statements,depth+1);
514 break;
516 case PV_PATTERN:
517 /* fields: item->u1.str == value of case
518 item->u2.statements == pval list of statements under the case
520 traverse_pval_item_template(item->u2.statements,depth+1);
521 break;
523 case PV_DEFAULT:
524 /* fields:
525 item->u2.statements == pval list of statements under the case
527 traverse_pval_item_template(item->u2.statements,depth+1);
528 break;
530 case PV_CATCH:
531 /* fields: item->u1.str == name of extension to catch
532 item->u2.statements == pval list of statements in context body
534 traverse_pval_item_template(item->u2.statements,depth+1);
535 break;
537 case PV_SWITCHES:
538 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
540 traverse_pval_item_template(item->u1.list,depth+1);
541 break;
543 case PV_ESWITCHES:
544 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
546 traverse_pval_item_template(item->u1.list,depth+1);
547 break;
549 case PV_INCLUDES:
550 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
551 item->u2.arglist == pval list of 4 PV_WORD elements for time values
553 traverse_pval_item_template(item->u1.list,depth+1);
554 traverse_pval_item_template(item->u2.arglist,depth+1);
555 break;
557 case PV_STATEMENTBLOCK:
558 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
560 traverse_pval_item_template(item->u1.list,depth+1);
561 break;
563 case PV_VARDEC:
564 /* fields: item->u1.str == variable name
565 item->u2.val == variable value to assign
567 break;
569 case PV_GOTO:
570 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
571 item->u1.list->u1.str == where the data on a PV_WORD will always be.
574 if ( item->u1.list->next )
576 if ( item->u1.list->next && item->u1.list->next->next )
579 break;
581 case PV_LABEL:
582 /* fields: item->u1.str == label name
584 break;
586 case PV_FOR:
587 /* fields: item->u1.for_init == a string containing the initalizer
588 item->u2.for_test == a string containing the loop test
589 item->u3.for_inc == a string containing the loop increment
591 item->u4.for_statements == a pval list of statements in the for ()
593 traverse_pval_item_template(item->u4.for_statements,depth+1);
594 break;
596 case PV_WHILE:
597 /* fields: item->u1.str == the while conditional, as supplied by user
599 item->u2.statements == a pval list of statements in the while ()
601 traverse_pval_item_template(item->u2.statements,depth+1);
602 break;
604 case PV_BREAK:
605 /* fields: none
607 break;
609 case PV_RETURN:
610 /* fields: none
612 break;
614 case PV_CONTINUE:
615 /* fields: none
617 break;
619 case PV_IFTIME:
620 /* fields: item->u1.list == there are 4 linked PV_WORDs here.
622 item->u2.statements == a pval list of statements in the if ()
623 item->u3.else_statements == a pval list of statements in the else
624 (could be zero)
626 traverse_pval_item_template(item->u2.statements,depth+1);
627 if ( item->u3.else_statements ) {
628 traverse_pval_item_template(item->u3.else_statements,depth+1);
630 break;
632 case PV_RANDOM:
633 /* fields: item->u1.str == the random number expression, as supplied by user
635 item->u2.statements == a pval list of statements in the if ()
636 item->u3.else_statements == a pval list of statements in the else
637 (could be zero)
639 traverse_pval_item_template(item->u2.statements,depth+1);
640 if ( item->u3.else_statements ) {
641 traverse_pval_item_template(item->u3.else_statements,depth+1);
643 break;
645 case PV_IF:
646 /* fields: item->u1.str == the if conditional, as supplied by user
648 item->u2.statements == a pval list of statements in the if ()
649 item->u3.else_statements == a pval list of statements in the else
650 (could be zero)
652 traverse_pval_item_template(item->u2.statements,depth+1);
653 if ( item->u3.else_statements ) {
654 traverse_pval_item_template(item->u3.else_statements,depth+1);
656 break;
658 case PV_SWITCH:
659 /* fields: item->u1.str == the switch expression
661 item->u2.statements == a pval list of statements in the switch,
662 (will be case statements, most likely!)
664 traverse_pval_item_template(item->u2.statements,depth+1);
665 break;
667 case PV_EXTENSION:
668 /* fields: item->u1.str == the extension name, label, whatever it's called
670 item->u2.statements == a pval list of statements in the extension
671 item->u3.hints == a char * hint argument
672 item->u4.regexten == an int boolean. non-zero says that regexten was specified
674 traverse_pval_item_template(item->u2.statements,depth+1);
675 break;
677 case PV_IGNOREPAT:
678 /* fields: item->u1.str == the ignorepat data
680 break;
682 case PV_GLOBALS:
683 /* fields: item->u1.statements == pval list of statements, usually vardecs
685 traverse_pval_item_template(item->u1.statements,depth+1);
686 break;
690 void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
691 but you may not need it */
693 pval *i;
695 for (i=item; i; i=i->next) {
696 traverse_pval_item_template(i, depth);
701 /* SEMANTIC CHECKING FOR AEL: ============================================================================= */
703 /* (not all that is syntactically legal is good! */
707 static int extension_matches(pval *here, const char *exten, const char *pattern)
709 int err1;
710 regex_t preg;
712 /* simple case, they match exactly, the pattern and exten name */
713 if( !strcmp(pattern,exten) == 0 )
714 return 1;
716 if ( pattern[0] == '_' ) {
717 char reg1[2000];
718 const char *p;
719 char *r = reg1;
721 if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
722 ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
723 pattern);
724 return 0;
726 /* form a regular expression from the pattern, and then match it against exten */
727 *r++ = '^'; /* what if the extension is a pattern ?? */
728 *r++ = '_'; /* what if the extension is a pattern ?? */
729 *r++ = '?';
730 for (p=pattern+1; *p; p++) {
731 switch ( *p ) {
732 case 'X':
733 *r++ = '[';
734 *r++ = '0';
735 *r++ = '-';
736 *r++ = '9';
737 *r++ = 'X';
738 *r++ = ']';
739 break;
741 case 'Z':
742 *r++ = '[';
743 *r++ = '1';
744 *r++ = '-';
745 *r++ = '9';
746 *r++ = 'Z';
747 *r++ = ']';
748 break;
750 case 'N':
751 *r++ = '[';
752 *r++ = '2';
753 *r++ = '-';
754 *r++ = '9';
755 *r++ = 'N';
756 *r++ = ']';
757 break;
759 case '[':
760 while ( *p && *p != ']' ) {
761 *r++ = *p++;
763 if ( *p != ']') {
764 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
765 here->filename, here->startline, here->endline, pattern);
767 break;
769 case '.':
770 case '!':
771 *r++ = '.';
772 *r++ = '*';
773 break;
774 case '*':
775 *r++ = '\\';
776 *r++ = '*';
777 break;
778 default:
779 *r++ = *p;
780 break;
784 *r++ = '$'; /* what if the extension is a pattern ?? */
785 *r++ = *p++; /* put in the closing null */
786 err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
787 if ( err1 ) {
788 char errmess[500];
789 regerror(err1,&preg,errmess,sizeof(errmess));
790 regfree(&preg);
791 ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
792 reg1, err1);
793 return 0;
795 err1 = regexec(&preg, exten, 0, 0, 0);
796 regfree(&preg);
798 if ( err1 ) {
799 /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
800 err1,exten, pattern, reg1); */
801 return 0; /* no match */
802 } else {
803 /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
804 exten, pattern); */
805 return 1;
809 } else {
810 if ( strcmp(exten,pattern) == 0 ) {
811 return 1;
812 } else
813 return 0;
818 static void check_expr2_input(pval *expr, char *str)
820 int spaces = strspn(str,"\t \n");
821 if ( !strncmp(str+spaces,"$[",2) ) {
822 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
823 expr->filename, expr->startline, expr->endline, str);
824 warns++;
828 static void check_includes(pval *includes)
830 struct pval *p4;
831 for (p4=includes->u1.list; p4; p4=p4->next) {
832 /* for each context pointed to, find it, then find a context/label that matches the
833 target here! */
834 char *incl_context = p4->u1.str;
835 /* find a matching context name */
836 struct pval *that_other_context = find_context(incl_context);
837 if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
838 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n\
839 (You may ignore this warning if '%s' exists in extensions.conf, or is created by another module. I cannot check for those.)\n",
840 includes->filename, includes->startline, includes->endline, incl_context, incl_context);
841 warns++;
847 static void check_timerange(pval *p)
849 char *times;
850 char *e;
851 int s1, s2;
852 int e1, e2;
854 times = ast_strdupa(p->u1.str);
856 /* Star is all times */
857 if (ast_strlen_zero(times) || !strcmp(times, "*")) {
858 return;
860 /* Otherwise expect a range */
861 e = strchr(times, '-');
862 if (!e) {
863 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",
864 p->filename, p->startline, p->endline, times);
865 warns++;
866 return;
868 *e = '\0';
869 e++;
870 while (*e && !isdigit(*e))
871 e++;
872 if (!*e) {
873 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
874 p->filename, p->startline, p->endline, p->u1.str);
875 warns++;
877 if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
878 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
879 p->filename, p->startline, p->endline, times);
880 warns++;
882 if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
883 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
884 p->filename, p->startline, p->endline, times);
885 warns++;
888 s1 = s1 * 30 + s2/2;
889 if ((s1 < 0) || (s1 >= 24*30)) {
890 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
891 p->filename, p->startline, p->endline, times);
892 warns++;
894 e1 = e1 * 30 + e2/2;
895 if ((e1 < 0) || (e1 >= 24*30)) {
896 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
897 p->filename, p->startline, p->endline, e);
898 warns++;
900 return;
903 static char *days[] =
905 "sun",
906 "mon",
907 "tue",
908 "wed",
909 "thu",
910 "fri",
911 "sat",
914 /*! \brief get_dow: Get day of week */
915 static void check_dow(pval *DOW)
917 char *dow;
918 char *c;
919 /* The following line is coincidence, really! */
920 int s, e;
922 dow = ast_strdupa(DOW->u1.str);
924 /* Check for all days */
925 if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
926 return;
927 /* Get start and ending days */
928 c = strchr(dow, '-');
929 if (c) {
930 *c = '\0';
931 c++;
932 } else
933 c = NULL;
934 /* Find the start */
935 s = 0;
936 while ((s < 7) && strcasecmp(dow, days[s])) s++;
937 if (s >= 7) {
938 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",
939 DOW->filename, DOW->startline, DOW->endline, dow);
940 warns++;
942 if (c) {
943 e = 0;
944 while ((e < 7) && strcasecmp(c, days[e])) e++;
945 if (e >= 7) {
946 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",
947 DOW->filename, DOW->startline, DOW->endline, c);
948 warns++;
950 } else
951 e = s;
954 static void check_day(pval *DAY)
956 char *day;
957 char *c;
958 /* The following line is coincidence, really! */
959 int s, e;
961 day = ast_strdupa(DAY->u1.str);
963 /* Check for all days */
964 if (ast_strlen_zero(day) || !strcmp(day, "*")) {
965 return;
967 /* Get start and ending days */
968 c = strchr(day, '-');
969 if (c) {
970 *c = '\0';
971 c++;
973 /* Find the start */
974 if (sscanf(day, "%d", &s) != 1) {
975 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
976 DAY->filename, DAY->startline, DAY->endline, day);
977 warns++;
979 else if ((s < 1) || (s > 31)) {
980 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",
981 DAY->filename, DAY->startline, DAY->endline, day);
982 warns++;
984 s--;
985 if (c) {
986 if (sscanf(c, "%d", &e) != 1) {
987 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
988 DAY->filename, DAY->startline, DAY->endline, c);
989 warns++;
991 else if ((e < 1) || (e > 31)) {
992 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",
993 DAY->filename, DAY->startline, DAY->endline, day);
994 warns++;
996 e--;
997 } else
998 e = s;
1001 static char *months[] =
1003 "jan",
1004 "feb",
1005 "mar",
1006 "apr",
1007 "may",
1008 "jun",
1009 "jul",
1010 "aug",
1011 "sep",
1012 "oct",
1013 "nov",
1014 "dec",
1017 static void check_month(pval *MON)
1019 char *mon;
1020 char *c;
1021 /* The following line is coincidence, really! */
1022 int s, e;
1024 mon = ast_strdupa(MON->u1.str);
1026 /* Check for all days */
1027 if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
1028 return ;
1029 /* Get start and ending days */
1030 c = strchr(mon, '-');
1031 if (c) {
1032 *c = '\0';
1033 c++;
1035 /* Find the start */
1036 s = 0;
1037 while ((s < 12) && strcasecmp(mon, months[s])) s++;
1038 if (s >= 12) {
1039 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1040 MON->filename, MON->startline, MON->endline, mon);
1041 warns++;
1043 if (c) {
1044 e = 0;
1045 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1046 if (e >= 12) {
1047 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1048 MON->filename, MON->startline, MON->endline, c);
1049 warns++;
1051 } else
1052 e = s;
1055 static int check_break(pval *item)
1057 pval *p = item;
1059 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1060 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1061 no sense */
1062 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
1063 || p->type == PV_WHILE || p->type == PV_FOR ) {
1064 return 1;
1066 p = p->dad;
1068 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1069 item->filename, item->startline, item->endline);
1070 errs++;
1072 return 0;
1075 static int check_continue(pval *item)
1077 pval *p = item;
1079 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1080 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1081 no sense */
1082 if( p->type == PV_WHILE || p->type == PV_FOR ) {
1083 return 1;
1085 p = p->dad;
1087 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1088 item->filename, item->startline, item->endline);
1089 errs++;
1091 return 0;
1095 /* general purpose goto finder */
1097 static void check_label(pval *item)
1099 /* basically, ensure that a label is not repeated in a context. Period.
1100 The method: well, for each label, find the first label in the context
1101 with the same name. If it's not the current label, then throw an error. */
1102 struct pval *curr;
1103 struct pval *x;
1105 /* printf("==== check_label: ====\n"); */
1106 if( !current_extension )
1107 curr = current_context;
1108 else
1109 curr = current_extension;
1111 x = find_first_label_in_current_context((char *)item->u1.str, curr);
1112 /* 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); */
1113 if( x && x != item )
1115 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1116 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1117 errs++;
1119 /* printf("<<<<< check_label: ====\n"); */
1122 static pval *get_goto_target(pval *item)
1124 /* just one item-- the label should be in the current extension */
1125 pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1126 pval *curr_cont;
1128 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1129 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1130 return x;
1133 curr_cont = get_contxt(item);
1135 /* TWO items */
1136 if (item->u1.list->next && !item->u1.list->next->next) {
1137 if (!strstr((item->u1.list)->u1.str,"${")
1138 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1139 struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1140 return x;
1144 /* All 3 items! */
1145 if (item->u1.list->next && item->u1.list->next->next) {
1146 /* all three */
1147 pval *first = item->u1.list;
1148 pval *second = item->u1.list->next;
1149 pval *third = item->u1.list->next->next;
1151 if (!strstr((item->u1.list)->u1.str,"${")
1152 && !strstr(item->u1.list->next->u1.str,"${")
1153 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1154 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1155 if (!x) {
1157 struct pval *p3;
1158 struct pval *that_context = find_context(item->u1.list->u1.str);
1160 /* the target of the goto could be in an included context!! Fancy that!! */
1161 /* look for includes in the current context */
1162 if (that_context) {
1163 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1164 if (p3->type == PV_INCLUDES) {
1165 struct pval *p4;
1166 for (p4=p3->u1.list; p4; p4=p4->next) {
1167 /* for each context pointed to, find it, then find a context/label that matches the
1168 target here! */
1169 char *incl_context = p4->u1.str;
1170 /* find a matching context name */
1171 struct pval *that_other_context = find_context(incl_context);
1172 if (that_other_context) {
1173 struct pval *x3;
1174 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1175 if (x3) {
1176 return x3;
1184 return x;
1187 return 0;
1190 static void check_goto(pval *item)
1192 /* check for the target of the goto-- does it exist? */
1193 if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1194 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
1195 item->filename, item->startline, item->endline);
1196 errs++;
1199 /* just one item-- the label should be in the current extension */
1201 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1202 struct pval *z = get_extension_or_contxt(item);
1203 struct pval *x = 0;
1204 if (z)
1205 x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1206 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1207 (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1208 if (!x) {
1209 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
1210 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1211 errs++;
1213 else
1214 return;
1217 /* TWO items */
1218 if (item->u1.list->next && !item->u1.list->next->next) {
1219 /* two items */
1220 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1221 (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1222 if (!strstr((item->u1.list)->u1.str,"${")
1223 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1224 struct pval *z = get_contxt(item);
1225 struct pval *x = 0;
1227 if (z)
1228 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1230 if (!x) {
1231 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",
1232 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1233 errs++;
1235 else
1236 return;
1240 /* All 3 items! */
1241 if (item->u1.list->next && item->u1.list->next->next) {
1242 /* all three */
1243 pval *first = item->u1.list;
1244 pval *second = item->u1.list->next;
1245 pval *third = item->u1.list->next->next;
1247 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1248 (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1249 if (!strstr((item->u1.list)->u1.str,"${")
1250 && !strstr(item->u1.list->next->u1.str,"${")
1251 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1252 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1253 if (!x) {
1254 struct pval *p3;
1255 struct pval *found = 0;
1256 struct pval *that_context = find_context(item->u1.list->u1.str);
1258 /* the target of the goto could be in an included context!! Fancy that!! */
1259 /* look for includes in the current context */
1260 if (that_context) {
1261 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1262 if (p3->type == PV_INCLUDES) {
1263 struct pval *p4;
1264 for (p4=p3->u1.list; p4; p4=p4->next) {
1265 /* for each context pointed to, find it, then find a context/label that matches the
1266 target here! */
1267 char *incl_context = p4->u1.str;
1268 /* find a matching context name */
1269 struct pval *that_other_context = find_context(incl_context);
1270 if (that_other_context) {
1271 struct pval *x3;
1272 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1273 if (x3) {
1274 found = x3;
1275 break;
1281 if (!found) {
1282 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
1283 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1284 errs++;
1286 } else {
1287 /* here is where code would go to check for target existence in extensions.conf files */
1288 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: no context %s could be found that matches the goto target!\n",
1289 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1290 warns++; /* this is just a warning, because this context could be in extensions.conf or somewhere */
1298 static void find_pval_goto_item(pval *item, int lev)
1300 struct pval *p4;
1301 if (lev>100) {
1302 ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
1303 return;
1306 switch ( item->type ) {
1307 case PV_MACRO:
1308 /* fields: item->u1.str == name of macro
1309 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1310 item->u2.arglist->u1.str == argument
1311 item->u2.arglist->next == next arg
1313 item->u3.macro_statements == pval list of statements in macro body.
1316 /* printf("Descending into matching macro %s\n", match_context); */
1317 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1319 break;
1321 case PV_CONTEXT:
1322 /* fields: item->u1.str == name of context
1323 item->u2.statements == pval list of statements in context body
1324 item->u3.abstract == int 1 if an abstract keyword were present
1326 break;
1328 case PV_CASE:
1329 /* fields: item->u1.str == value of case
1330 item->u2.statements == pval list of statements under the case
1332 find_pval_gotos(item->u2.statements,lev+1);
1333 break;
1335 case PV_PATTERN:
1336 /* fields: item->u1.str == value of case
1337 item->u2.statements == pval list of statements under the case
1339 find_pval_gotos(item->u2.statements,lev+1);
1340 break;
1342 case PV_DEFAULT:
1343 /* fields:
1344 item->u2.statements == pval list of statements under the case
1346 find_pval_gotos(item->u2.statements,lev+1);
1347 break;
1349 case PV_CATCH:
1350 /* fields: item->u1.str == name of extension to catch
1351 item->u2.statements == pval list of statements in context body
1353 find_pval_gotos(item->u2.statements,lev+1);
1354 break;
1356 case PV_STATEMENTBLOCK:
1357 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1359 find_pval_gotos(item->u1.list,lev+1);
1360 break;
1362 case PV_GOTO:
1363 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1364 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1366 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1367 break;
1369 case PV_INCLUDES:
1370 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1372 for (p4=item->u1.list; p4; p4=p4->next) {
1373 /* for each context pointed to, find it, then find a context/label that matches the
1374 target here! */
1375 char *incl_context = p4->u1.str;
1376 /* find a matching context name */
1377 struct pval *that_context = find_context(incl_context);
1378 if (that_context) {
1379 find_pval_gotos(that_context,lev+1); /* keep working up the includes */
1382 break;
1384 case PV_FOR:
1385 /* fields: item->u1.for_init == a string containing the initalizer
1386 item->u2.for_test == a string containing the loop test
1387 item->u3.for_inc == a string containing the loop increment
1389 item->u4.for_statements == a pval list of statements in the for ()
1391 find_pval_gotos(item->u4.for_statements,lev+1);
1392 break;
1394 case PV_WHILE:
1395 /* fields: item->u1.str == the while conditional, as supplied by user
1397 item->u2.statements == a pval list of statements in the while ()
1399 find_pval_gotos(item->u2.statements,lev+1);
1400 break;
1402 case PV_RANDOM:
1403 /* fields: item->u1.str == the random number expression, as supplied by user
1405 item->u2.statements == a pval list of statements in the if ()
1406 item->u3.else_statements == a pval list of statements in the else
1407 (could be zero)
1408 fall thru to PV_IF */
1410 case PV_IFTIME:
1411 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1413 item->u2.statements == a pval list of statements in the if ()
1414 item->u3.else_statements == a pval list of statements in the else
1415 (could be zero)
1416 fall thru to PV_IF*/
1417 case PV_IF:
1418 /* fields: item->u1.str == the if conditional, as supplied by user
1420 item->u2.statements == a pval list of statements in the if ()
1421 item->u3.else_statements == a pval list of statements in the else
1422 (could be zero)
1424 find_pval_gotos(item->u2.statements,lev+1);
1426 if (item->u3.else_statements) {
1427 find_pval_gotos(item->u3.else_statements,lev+1);
1429 break;
1431 case PV_SWITCH:
1432 /* fields: item->u1.str == the switch expression
1434 item->u2.statements == a pval list of statements in the switch,
1435 (will be case statements, most likely!)
1437 find_pval_gotos(item->u3.else_statements,lev+1);
1438 break;
1440 case PV_EXTENSION:
1441 /* fields: item->u1.str == the extension name, label, whatever it's called
1443 item->u2.statements == a pval list of statements in the extension
1444 item->u3.hints == a char * hint argument
1445 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1448 find_pval_gotos(item->u2.statements,lev+1);
1449 break;
1451 default:
1452 break;
1456 static void find_pval_gotos(pval *item,int lev)
1458 pval *i;
1460 for (i=item; i; i=i->next) {
1462 find_pval_goto_item(i, lev);
1468 /* general purpose label finder */
1469 static struct pval *match_pval_item(pval *item)
1471 pval *x;
1473 switch ( item->type ) {
1474 case PV_MACRO:
1475 /* fields: item->u1.str == name of macro
1476 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1477 item->u2.arglist->u1.str == argument
1478 item->u2.arglist->next == next arg
1480 item->u3.macro_statements == pval list of statements in macro body.
1482 /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1483 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1485 /* printf("MACRO: match context is: %s\n", match_context); */
1487 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 */ {
1488 /* printf("Returning on matching macro %s\n", match_context); */
1489 return item;
1493 if (!return_on_context_match) {
1494 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1495 if ((x=match_pval(item->u3.macro_statements))) {
1496 /* printf("Responded with pval match %x\n", x); */
1497 return x;
1500 } else {
1501 /* printf("Skipping context/macro %s\n", item->u1.str); */
1504 break;
1506 case PV_CONTEXT:
1507 /* fields: item->u1.str == name of context
1508 item->u2.statements == pval list of statements in context body
1509 item->u3.abstract == int 1 if an abstract keyword were present
1511 /* printf(" matching in CONTEXT\n"); */
1512 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1513 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1514 /* printf("Returning on matching context %s\n", match_context); */
1515 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1516 return item;
1519 if (!return_on_context_match ) {
1520 /* printf("Descending into matching context %s\n", match_context); */
1521 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1522 /* printf("CONTEXT: Responded with pval match %x\n", x); */
1523 return x;
1526 } else {
1527 /* printf("Skipping context/macro %s\n", item->u1.str); */
1529 break;
1531 case PV_CASE:
1532 /* fields: item->u1.str == value of case
1533 item->u2.statements == pval list of statements under the case
1535 /* printf(" matching in CASE\n"); */
1536 if ((x=match_pval(item->u2.statements))) {
1537 /* printf("CASE: Responded with pval match %x\n", x); */
1538 return x;
1540 break;
1542 case PV_PATTERN:
1543 /* fields: item->u1.str == value of case
1544 item->u2.statements == pval list of statements under the case
1546 /* printf(" matching in PATTERN\n"); */
1547 if ((x=match_pval(item->u2.statements))) {
1548 /* printf("PATTERN: Responded with pval match %x\n", x); */
1549 return x;
1551 break;
1553 case PV_DEFAULT:
1554 /* fields:
1555 item->u2.statements == pval list of statements under the case
1557 /* printf(" matching in DEFAULT\n"); */
1558 if ((x=match_pval(item->u2.statements))) {
1559 /* printf("DEFAULT: Responded with pval match %x\n", x); */
1560 return x;
1562 break;
1564 case PV_CATCH:
1565 /* fields: item->u1.str == name of extension to catch
1566 item->u2.statements == pval list of statements in context body
1568 /* printf(" matching in CATCH\n"); */
1569 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1570 /* printf("Descending into matching catch %s => %s\n", match_exten, item->u1.str); */
1571 if (strcmp(match_label,"1") == 0) {
1572 if (item->u2.statements) {
1573 struct pval *p5 = item->u2.statements;
1574 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1575 p5 = p5->next;
1576 if (p5)
1577 return p5;
1578 else
1579 return 0;
1581 else
1582 return 0;
1585 if ((x=match_pval(item->u2.statements))) {
1586 /* printf("CATCH: Responded with pval match %x\n", (unsigned int)x); */
1587 return x;
1589 } else {
1590 /* printf("Skipping catch %s\n", item->u1.str); */
1592 break;
1594 case PV_STATEMENTBLOCK:
1595 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1597 /* printf(" matching in STATEMENTBLOCK\n"); */
1598 if ((x=match_pval(item->u1.list))) {
1599 /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1600 return x;
1602 break;
1604 case PV_LABEL:
1605 /* fields: item->u1.str == label name
1607 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1608 item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1610 if (count_labels) {
1611 if (!strcmp(match_label, item->u1.str)) {
1612 label_count++;
1613 last_matched_label = item;
1616 } else {
1617 if (!strcmp(match_label, item->u1.str)) {
1618 /* printf("LABEL: Responded with pval match %x\n", x); */
1619 return item;
1622 break;
1624 case PV_FOR:
1625 /* fields: item->u1.for_init == a string containing the initalizer
1626 item->u2.for_test == a string containing the loop test
1627 item->u3.for_inc == a string containing the loop increment
1629 item->u4.for_statements == a pval list of statements in the for ()
1631 /* printf(" matching in FOR\n"); */
1632 if ((x=match_pval(item->u4.for_statements))) {
1633 /* printf("FOR: Responded with pval match %x\n", x);*/
1634 return x;
1636 break;
1638 case PV_WHILE:
1639 /* fields: item->u1.str == the while conditional, as supplied by user
1641 item->u2.statements == a pval list of statements in the while ()
1643 /* printf(" matching in WHILE\n"); */
1644 if ((x=match_pval(item->u2.statements))) {
1645 /* printf("WHILE: Responded with pval match %x\n", x); */
1646 return x;
1648 break;
1650 case PV_RANDOM:
1651 /* fields: item->u1.str == the random number expression, as supplied by user
1653 item->u2.statements == a pval list of statements in the if ()
1654 item->u3.else_statements == a pval list of statements in the else
1655 (could be zero)
1656 fall thru to PV_IF */
1658 case PV_IFTIME:
1659 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1661 item->u2.statements == a pval list of statements in the if ()
1662 item->u3.else_statements == a pval list of statements in the else
1663 (could be zero)
1664 fall thru to PV_IF*/
1665 case PV_IF:
1666 /* fields: item->u1.str == the if conditional, as supplied by user
1668 item->u2.statements == a pval list of statements in the if ()
1669 item->u3.else_statements == a pval list of statements in the else
1670 (could be zero)
1672 /* printf(" matching in IF/IFTIME/RANDOM\n"); */
1673 if ((x=match_pval(item->u2.statements))) {
1674 return x;
1676 if (item->u3.else_statements) {
1677 if ((x=match_pval(item->u3.else_statements))) {
1678 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1679 return x;
1682 break;
1684 case PV_SWITCH:
1685 /* fields: item->u1.str == the switch expression
1687 item->u2.statements == a pval list of statements in the switch,
1688 (will be case statements, most likely!)
1690 /* printf(" matching in SWITCH\n"); */
1691 if ((x=match_pval(item->u2.statements))) {
1692 /* printf("SWITCH: Responded with pval match %x\n", x); */
1693 return x;
1695 break;
1697 case PV_EXTENSION:
1698 /* fields: item->u1.str == the extension name, label, whatever it's called
1700 item->u2.statements == a pval list of statements in the extension
1701 item->u3.hints == a char * hint argument
1702 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1704 /* printf(" matching in EXTENSION\n"); */
1705 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1706 /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1707 if (strcmp(match_label,"1") == 0) {
1708 if (item->u2.statements) {
1709 struct pval *p5 = item->u2.statements;
1710 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1711 p5 = p5->next;
1712 if (p5)
1713 return p5;
1714 else
1715 return 0;
1717 else
1718 return 0;
1721 if ((x=match_pval(item->u2.statements))) {
1722 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1723 return x;
1725 } else {
1726 /* printf("Skipping exten %s\n", item->u1.str); */
1728 break;
1729 default:
1730 /* printf(" matching in default = %d\n", item->type); */
1731 break;
1733 return 0;
1736 struct pval *match_pval(pval *item)
1738 pval *i;
1740 for (i=item; i; i=i->next) {
1741 pval *x;
1742 /* printf(" -- match pval: item %d\n", i->type); */
1744 if ((x = match_pval_item(i))) {
1745 /* printf("match_pval: returning x=%x\n", (int)x); */
1746 return x; /* cut the search short */
1749 return 0;
1752 #if 0
1753 int count_labels_in_current_context(char *label)
1755 label_count = 0;
1756 count_labels = 1;
1757 return_on_context_match = 0;
1758 match_pval(current_context->u2.statements);
1760 return label_count;
1762 #endif
1764 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1766 /* printf(" --- Got args %s, %s\n", exten, label); */
1767 struct pval *ret;
1768 struct pval *p3;
1769 struct pval *startpt = ((curr_cont->type==PV_MACRO)?curr_cont->u3.macro_statements: curr_cont->u2.statements);
1771 count_labels = 0;
1772 return_on_context_match = 0;
1773 match_context = "*";
1774 match_exten = "*";
1775 match_label = label;
1777 ret = match_pval(curr_cont);
1778 if (ret)
1779 return ret;
1781 /* the target of the goto could be in an included context!! Fancy that!! */
1782 /* look for includes in the current context */
1783 for (p3=startpt; p3; p3=p3->next) {
1784 if (p3->type == PV_INCLUDES) {
1785 struct pval *p4;
1786 for (p4=p3->u1.list; p4; p4=p4->next) {
1787 /* for each context pointed to, find it, then find a context/label that matches the
1788 target here! */
1789 char *incl_context = p4->u1.str;
1790 /* find a matching context name */
1791 struct pval *that_context = find_context(incl_context);
1792 if (that_context) {
1793 struct pval *x3;
1794 x3 = find_first_label_in_current_context(label, that_context);
1795 if (x3) {
1796 return x3;
1802 return 0;
1805 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1807 /* printf(" --- Got args %s, %s\n", exten, label); */
1808 struct pval *ret;
1809 struct pval *p3;
1810 struct pval *startpt;
1812 count_labels = 0;
1813 return_on_context_match = 0;
1814 match_context = "*";
1815 match_exten = exten;
1816 match_label = label;
1817 if (curr_cont->type == PV_MACRO)
1818 startpt = curr_cont->u3.macro_statements;
1819 else
1820 startpt = curr_cont->u2.statements;
1822 ret = match_pval(startpt);
1823 if (ret)
1824 return ret;
1826 /* the target of the goto could be in an included context!! Fancy that!! */
1827 /* look for includes in the current context */
1828 for (p3=startpt; p3; p3=p3->next) {
1829 if (p3->type == PV_INCLUDES) {
1830 struct pval *p4;
1831 for (p4=p3->u1.list; p4; p4=p4->next) {
1832 /* for each context pointed to, find it, then find a context/label that matches the
1833 target here! */
1834 char *incl_context = p4->u1.str;
1835 /* find a matching context name */
1836 struct pval *that_context = find_context(incl_context);
1837 if (that_context) {
1838 struct pval *x3;
1839 x3 = find_label_in_current_context(exten, label, that_context);
1840 if (x3) {
1841 return x3;
1847 return 0;
1850 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1852 /* printf(" --- Got args %s\n", label); */
1853 count_labels = 0;
1854 return_on_context_match = 0;
1855 match_context = "*";
1856 match_exten = "*";
1857 match_label = label;
1858 return match_pval(curr_ext);
1861 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1863 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1864 count_labels = 0;
1865 return_on_context_match = 0;
1867 match_context = context;
1868 match_exten = exten;
1869 match_label = label;
1871 return match_pval(current_db);
1875 struct pval *find_macro(char *name)
1877 return_on_context_match = 1;
1878 count_labels = 0;
1879 match_context = name;
1880 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1881 match_label = "*";
1882 return match_pval(current_db);
1885 struct pval *find_context(char *name)
1887 return_on_context_match = 1;
1888 count_labels = 0;
1889 match_context = name;
1890 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1891 match_label = "*";
1892 return match_pval(current_db);
1895 int is_float(char *arg )
1897 char *s;
1898 for (s=arg; *s; s++) {
1899 if (*s != '.' && (*s < '0' || *s > '9'))
1900 return 0;
1902 return 1;
1904 int is_int(char *arg )
1906 char *s;
1907 for (s=arg; *s; s++) {
1908 if (*s < '0' || *s > '9')
1909 return 0;
1911 return 1;
1913 int is_empty(char *arg)
1915 if (!arg)
1916 return 1;
1917 if (*arg == 0)
1918 return 1;
1919 while (*arg) {
1920 if (*arg != ' ' && *arg != '\t')
1921 return 0;
1922 arg++;
1924 return 1;
1927 #ifdef AAL_ARGCHECK
1928 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1930 struct argchoice *ac;
1931 char *opcop,*q,*p;
1933 switch (should->dtype) {
1934 case ARGD_OPTIONSET:
1935 if ( strstr(is->u1.str,"${") )
1936 return 0; /* no checking anything if there's a var reference in there! */
1938 opcop = ast_strdupa(is->u1.str);
1940 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
1941 if ( *q == '(' ) {
1942 p = q+1;
1943 while (*p && *p != ')' )
1944 *p++ = '+';
1945 q = p+1;
1949 for (ac=app->opts; ac; ac=ac->next) {
1950 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
1951 return 0;
1953 for (ac=app->opts; ac; ac=ac->next) {
1954 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
1955 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
1957 if (p && *p == 'j') {
1958 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
1959 is->filename, is->startline, is->endline, app->name);
1960 errs++;
1963 if (p) {
1964 *p = '+';
1965 if (ac->name[1] == '(') {
1966 if (*(p+1) != '(') {
1967 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",
1968 is->filename, is->startline, is->endline, ac->name[0], app->name);
1969 warns++;
1975 for (q=opcop; *q; q++) {
1976 if ( *q != '+' && *q != '(' && *q != ')') {
1977 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",
1978 is->filename, is->startline, is->endline, *q, app->name);
1979 warns++;
1982 return 1;
1983 break;
1984 default:
1985 return 0;
1990 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
1992 struct argchoice *ac;
1993 char *opcop;
1995 switch (should->dtype) {
1996 case ARGD_STRING:
1997 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
1998 return 0;
1999 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2000 return 1;
2001 break;
2003 case ARGD_INT:
2004 if (is_int(is->u1.str))
2005 return 1;
2006 else
2007 return 0;
2008 break;
2010 case ARGD_FLOAT:
2011 if (is_float(is->u1.str))
2012 return 1;
2013 else
2014 return 0;
2015 break;
2017 case ARGD_ENUM:
2018 if( !is->u1.str || strlen(is->u1.str) == 0 )
2019 return 1; /* a null arg in the call will match an enum, I guess! */
2020 for (ac=should->choices; ac; ac=ac->next) {
2021 if (strcmp(ac->name,is->u1.str) == 0)
2022 return 1;
2024 return 0;
2025 break;
2027 case ARGD_OPTIONSET:
2028 opcop = ast_strdupa(is->u1.str);
2030 for (ac=app->opts; ac; ac=ac->next) {
2031 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2032 return 1;
2034 for (ac=app->opts; ac; ac=ac->next) {
2035 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2036 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2038 if (p) {
2039 *p = '+';
2040 if (ac->name[1] == '(') {
2041 if (*(p+1) == '(') {
2042 char *q = p+1;
2043 while (*q && *q != ')') {
2044 *q++ = '+';
2046 *q = '+';
2052 return 1;
2053 break;
2054 case ARGD_VARARG:
2055 return 1; /* matches anything */
2056 break;
2058 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2060 #endif
2062 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2064 #ifdef AAL_ARGCHECK
2065 struct argdesc *ad = app->args;
2066 pval *pa;
2067 int z;
2069 for (pa = arglist; pa; pa=pa->next) {
2070 if (!ad) {
2071 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2072 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2073 warns++;
2074 return 1;
2075 } else {
2076 /* find the first entry in the ad list that will match */
2077 do {
2078 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2079 break;
2081 z= option_matches( ad, pa, app);
2082 if (!z) {
2083 if ( !arglist )
2084 arglist=appcall;
2086 if (ad->type == ARGD_REQUIRED) {
2087 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2088 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2089 warns++;
2090 return 1;
2092 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2093 option_matches_j( ad, pa, app);
2095 ad = ad->next;
2096 } while (ad && !z);
2099 /* any app nodes left, that are not optional? */
2100 for ( ; ad; ad=ad->next) {
2101 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2102 if ( !arglist )
2103 arglist=appcall;
2104 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2105 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2106 warns++;
2107 return 1;
2110 return 0;
2111 #else
2112 return 0;
2113 #endif
2116 void check_switch_expr(pval *item, struct argapp *apps)
2118 #ifdef AAL_ARGCHECK
2119 /* get and clean the variable name */
2120 char *buff1, *p;
2121 struct argapp *a,*a2;
2122 struct appsetvar *v,*v2;
2123 struct argchoice *c;
2124 pval *t;
2126 p = item->u1.str;
2127 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2128 p++;
2130 buff1 = ast_strdupa(p);
2132 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2133 buff1[strlen(buff1)-1] = 0;
2134 /* buff1 now contains the variable name */
2135 v = 0;
2136 for (a=apps; a; a=a->next) {
2137 for (v=a->setvars;v;v=v->next) {
2138 if (strcmp(v->name,buff1) == 0) {
2139 break;
2142 if ( v )
2143 break;
2145 if (v && v->vals) {
2146 /* we have a match, to a variable that has a set of determined values */
2147 int def= 0;
2148 int pat = 0;
2149 int f1 = 0;
2151 /* first of all, does this switch have a default case ? */
2152 for (t=item->u2.statements; t; t=t->next) {
2153 if (t->type == PV_DEFAULT) {
2154 def =1;
2155 break;
2157 if (t->type == PV_PATTERN) {
2158 pat++;
2161 if (def || pat) /* nothing to check. All cases accounted for! */
2162 return;
2163 for (c=v->vals; c; c=c->next) {
2164 f1 = 0;
2165 for (t=item->u2.statements; t; t=t->next) {
2166 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2167 if (!strcmp(t->u1.str,c->name)) {
2168 f1 = 1;
2169 break;
2173 if (!f1) {
2174 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2175 item->filename, item->startline, item->endline, item->u1.str, c->name);
2176 warns++;
2179 /* next, is there an app call in the current exten, that would set this var? */
2180 f1 = 0;
2181 t = current_extension->u2.statements;
2182 if ( t && t->type == PV_STATEMENTBLOCK )
2183 t = t->u1.statements;
2184 for (; t && t != item; t=t->next) {
2185 if (t->type == PV_APPLICATION_CALL) {
2186 /* find the application that matches the u1.str */
2187 for (a2=apps; a2; a2=a2->next) {
2188 if (strcasecmp(a2->name, t->u1.str)==0) {
2189 for (v2=a2->setvars; v2; v2=v2->next) {
2190 if (strcmp(v2->name, buff1) == 0) {
2191 /* found an app that sets the var */
2192 f1 = 1;
2193 break;
2197 if (f1)
2198 break;
2201 if (f1)
2202 break;
2205 /* see if it sets the var */
2206 if (!f1) {
2207 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",
2208 item->filename, item->startline, item->endline, item->u1.str);
2209 warns++;
2212 #else
2213 pval *t,*tl=0,*p2;
2214 int def= 0;
2216 /* first of all, does this switch have a default case ? */
2217 for (t=item->u2.statements; t; t=t->next) {
2218 if (t->type == PV_DEFAULT) {
2219 def =1;
2220 break;
2222 tl = t;
2224 if (def) /* nothing to check. All cases accounted for! */
2225 return;
2226 /* if no default, warn and insert a default case at the end */
2227 p2 = tl->next = calloc(1, sizeof(struct pval));
2229 p2->type = PV_DEFAULT;
2230 p2->startline = tl->startline;
2231 p2->endline = tl->endline;
2232 p2->startcol = tl->startcol;
2233 p2->endcol = tl->endcol;
2234 p2->filename = strdup(tl->filename);
2235 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2236 p2->filename, p2->startline, p2->endline);
2237 warns++;
2239 #endif
2242 static void check_context_names(void)
2244 pval *i,*j;
2245 for (i=current_db; i; i=i->next) {
2246 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2247 for (j=i->next; j; j=j->next) {
2248 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2249 if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
2251 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d! (and neither is marked 'extend')\n",
2252 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
2253 errs++;
2261 static void check_abstract_reference(pval *abstract_context)
2263 pval *i,*j;
2264 /* find some context includes that reference this context */
2267 /* otherwise, print out a warning */
2268 for (i=current_db; i; i=i->next) {
2269 if (i->type == PV_CONTEXT) {
2270 for (j=i->u2. statements; j; j=j->next) {
2271 if ( j->type == PV_INCLUDES ) {
2272 struct pval *p4;
2273 for (p4=j->u1.list; p4; p4=p4->next) {
2274 /* for each context pointed to, find it, then find a context/label that matches the
2275 target here! */
2276 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2277 return; /* found a match! */
2283 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",
2284 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2285 warns++;
2289 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2291 pval *lp;
2292 #ifdef AAL_ARGCHECK
2293 struct argapp *app, *found;
2294 #endif
2295 struct pval *macro_def;
2296 struct pval *app_def;
2298 char errmsg[4096];
2299 char *strp;
2301 switch (item->type) {
2302 case PV_WORD:
2303 /* fields: item->u1.str == string associated with this (word).
2304 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2305 break;
2307 case PV_MACRO:
2308 /* fields: item->u1.str == name of macro
2309 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2310 item->u2.arglist->u1.str == argument
2311 item->u2.arglist->next == next arg
2313 item->u3.macro_statements == pval list of statements in macro body.
2315 in_abstract_context = 0;
2316 current_context = item;
2317 current_extension = 0;
2318 for (lp=item->u2.arglist; lp; lp=lp->next) {
2321 check_pval(item->u3.macro_statements, apps,in_globals);
2322 break;
2324 case PV_CONTEXT:
2325 /* fields: item->u1.str == name of context
2326 item->u2.statements == pval list of statements in context body
2327 item->u3.abstract == int 1 if an abstract keyword were present
2329 current_context = item;
2330 current_extension = 0;
2331 if ( item->u3.abstract ) {
2332 in_abstract_context = 1;
2333 check_abstract_reference(item);
2334 } else
2335 in_abstract_context = 0;
2336 check_pval(item->u2.statements, apps,in_globals);
2337 break;
2339 case PV_MACRO_CALL:
2340 /* fields: item->u1.str == name of macro to call
2341 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2342 item->u2.arglist->u1.str == argument
2343 item->u2.arglist->next == next arg
2345 macro_def = find_macro(item->u1.str);
2346 if (!macro_def) {
2347 /* here is a good place to check to see if the definition is in extensions.conf! */
2348 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s ! Hopefully it is present in extensions.conf! \n",
2349 item->filename, item->startline, item->endline, item->u1.str);
2350 warns++;
2351 } else if (macro_def->type != PV_MACRO) {
2352 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2353 item->filename, item->startline, item->endline, item->u1.str);
2354 errs++;
2355 } else {
2356 /* macro_def is a MACRO, so do the args match in number? */
2357 int hereargs = 0;
2358 int thereargs = 0;
2360 for (lp=item->u2.arglist; lp; lp=lp->next) {
2361 hereargs++;
2363 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2364 thereargs++;
2366 if (hereargs != thereargs ) {
2367 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",
2368 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2369 errs++;
2372 break;
2374 case PV_APPLICATION_CALL:
2375 /* fields: item->u1.str == name of application to call
2376 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2377 item->u2.arglist->u1.str == argument
2378 item->u2.arglist->next == next arg
2380 /* Need to check to see if the application is available! */
2381 app_def = find_context(item->u1.str);
2382 if (app_def && app_def->type == PV_MACRO) {
2383 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2384 item->filename, item->startline, item->endline, item->u1.str);
2385 errs++;
2387 if (strcasecmp(item->u1.str,"GotoIf") == 0
2388 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2389 || strcasecmp(item->u1.str,"while") == 0
2390 || strcasecmp(item->u1.str,"endwhile") == 0
2391 || strcasecmp(item->u1.str,"random") == 0
2392 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2393 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s needs to be re-written using AEL if, while, goto, etc. keywords instead!\n",
2394 item->filename, item->startline, item->endline, item->u1.str);
2395 warns++;
2397 #ifdef AAL_ARGCHECK
2398 found = 0;
2399 for (app=apps; app; app=app->next) {
2400 if (strcasecmp(app->name, item->u1.str) == 0) {
2401 found =app;
2402 break;
2405 if (!found) {
2406 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2407 item->filename, item->startline, item->endline, item->u1.str);
2408 warns++;
2409 } else
2410 check_app_args(item, item->u2.arglist, app);
2411 #endif
2412 break;
2414 case PV_CASE:
2415 /* fields: item->u1.str == value of case
2416 item->u2.statements == pval list of statements under the case
2418 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2419 /* find the last statement */
2420 check_pval(item->u2.statements, apps,in_globals);
2421 break;
2423 case PV_PATTERN:
2424 /* fields: item->u1.str == value of case
2425 item->u2.statements == pval list of statements under the case
2427 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2428 /* find the last statement */
2430 check_pval(item->u2.statements, apps,in_globals);
2431 break;
2433 case PV_DEFAULT:
2434 /* fields:
2435 item->u2.statements == pval list of statements under the case
2438 check_pval(item->u2.statements, apps,in_globals);
2439 break;
2441 case PV_CATCH:
2442 /* fields: item->u1.str == name of extension to catch
2443 item->u2.statements == pval list of statements in context body
2445 check_pval(item->u2.statements, apps,in_globals);
2446 break;
2448 case PV_SWITCHES:
2449 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2451 check_pval(item->u1.list, apps,in_globals);
2452 break;
2454 case PV_ESWITCHES:
2455 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2457 check_pval(item->u1.list, apps,in_globals);
2458 break;
2460 case PV_INCLUDES:
2461 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2463 check_pval(item->u1.list, apps,in_globals);
2464 check_includes(item);
2465 for (lp=item->u1.list; lp; lp=lp->next){
2466 char *incl_context = lp->u1.str;
2467 struct pval *that_context = find_context(incl_context);
2469 if ( lp->u2.arglist ) {
2470 check_timerange(lp->u2.arglist);
2471 check_dow(lp->u2.arglist->next);
2472 check_day(lp->u2.arglist->next->next);
2473 check_month(lp->u2.arglist->next->next->next);
2476 if (that_context) {
2477 find_pval_gotos(that_context->u2.statements,0);
2481 break;
2483 case PV_STATEMENTBLOCK:
2484 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2486 check_pval(item->u1.list, apps,in_globals);
2487 break;
2489 case PV_VARDEC:
2490 /* fields: item->u1.str == variable name
2491 item->u2.val == variable value to assign
2493 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2494 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2495 snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.val);
2496 ast_expr_register_extra_error_info(errmsg);
2497 ast_expr(item->u2.val, expr_output, sizeof(expr_output));
2498 ast_expr_clear_extra_error_info();
2499 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2500 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2501 item->filename, item->startline, item->endline, item->u2.val);
2502 warns++;
2504 check_expr2_input(item,item->u2.val);
2506 break;
2508 case PV_GOTO:
2509 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2510 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2512 /* don't check goto's in abstract contexts */
2513 if ( in_abstract_context )
2514 break;
2516 check_goto(item);
2517 break;
2519 case PV_LABEL:
2520 /* fields: item->u1.str == label name
2522 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2523 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2524 item->filename, item->startline, item->endline, item->u1.str);
2525 warns++;
2528 check_label(item);
2529 break;
2531 case PV_FOR:
2532 /* fields: item->u1.for_init == a string containing the initalizer
2533 item->u2.for_test == a string containing the loop test
2534 item->u3.for_inc == a string containing the loop increment
2536 item->u4.for_statements == a pval list of statements in the for ()
2538 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, for test expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.for_test);
2539 ast_expr_register_extra_error_info(errmsg);
2541 strp = strchr(item->u1.for_init, '=');
2542 if (strp) {
2543 ast_expr(strp+1, expr_output, sizeof(expr_output));
2545 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output));
2546 strp = strchr(item->u3.for_inc, '=');
2547 if (strp) {
2548 ast_expr(strp+1, expr_output, sizeof(expr_output));
2550 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2551 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2552 item->filename, item->startline, item->endline, item->u2.for_test);
2553 warns++;
2555 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2556 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2557 item->filename, item->startline, item->endline, item->u3.for_inc);
2558 warns++;
2560 check_expr2_input(item,item->u2.for_test);
2561 check_expr2_input(item,item->u3.for_inc);
2563 ast_expr_clear_extra_error_info();
2564 check_pval(item->u4.for_statements, apps,in_globals);
2565 break;
2567 case PV_WHILE:
2568 /* fields: item->u1.str == the while conditional, as supplied by user
2570 item->u2.statements == a pval list of statements in the while ()
2572 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2573 ast_expr_register_extra_error_info(errmsg);
2574 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2575 ast_expr_clear_extra_error_info();
2576 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2577 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2578 item->filename, item->startline, item->endline, item->u1.str);
2579 warns++;
2581 check_expr2_input(item,item->u1.str);
2582 check_pval(item->u2.statements, apps,in_globals);
2583 break;
2585 case PV_BREAK:
2586 /* fields: none
2588 check_break(item);
2589 break;
2591 case PV_RETURN:
2592 /* fields: none
2594 break;
2596 case PV_CONTINUE:
2597 /* fields: none
2599 check_continue(item);
2600 break;
2602 case PV_RANDOM:
2603 /* fields: item->u1.str == the random number expression, as supplied by user
2605 item->u2.statements == a pval list of statements in the if ()
2606 item->u3.else_statements == a pval list of statements in the else
2607 (could be zero)
2609 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2610 ast_expr_register_extra_error_info(errmsg);
2611 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2612 ast_expr_clear_extra_error_info();
2613 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2614 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2615 item->filename, item->startline, item->endline, item->u1.str);
2616 warns++;
2618 check_expr2_input(item,item->u1.str);
2619 check_pval(item->u2.statements, apps,in_globals);
2620 if (item->u3.else_statements) {
2621 check_pval(item->u3.else_statements, apps,in_globals);
2623 break;
2625 case PV_IFTIME:
2626 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2628 item->u2.statements == a pval list of statements in the if ()
2629 item->u3.else_statements == a pval list of statements in the else
2630 (could be zero)
2632 if ( item->u2.arglist ) {
2633 check_timerange(item->u1.list);
2634 check_dow(item->u1.list->next);
2635 check_day(item->u1.list->next->next);
2636 check_month(item->u1.list->next->next->next);
2639 check_pval(item->u2.statements, apps,in_globals);
2640 if (item->u3.else_statements) {
2641 check_pval(item->u3.else_statements, apps,in_globals);
2643 break;
2645 case PV_IF:
2646 /* fields: item->u1.str == the if conditional, as supplied by user
2648 item->u2.statements == a pval list of statements in the if ()
2649 item->u3.else_statements == a pval list of statements in the else
2650 (could be zero)
2652 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2653 ast_expr_register_extra_error_info(errmsg);
2654 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2655 ast_expr_clear_extra_error_info();
2656 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2657 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2658 item->filename, item->startline, item->endline, item->u1.str);
2659 warns++;
2661 check_expr2_input(item,item->u1.str);
2662 check_pval(item->u2.statements, apps,in_globals);
2663 if (item->u3.else_statements) {
2664 check_pval(item->u3.else_statements, apps,in_globals);
2666 break;
2668 case PV_SWITCH:
2669 /* fields: item->u1.str == the switch expression
2671 item->u2.statements == a pval list of statements in the switch,
2672 (will be case statements, most likely!)
2674 /* we can check the switch expression, see if it matches any of the app variables...
2675 if it does, then, are all the possible cases accounted for? */
2676 check_switch_expr(item, apps);
2677 check_pval(item->u2.statements, apps,in_globals);
2678 break;
2680 case PV_EXTENSION:
2681 /* fields: item->u1.str == the extension name, label, whatever it's called
2683 item->u2.statements == a pval list of statements in the extension
2684 item->u3.hints == a char * hint argument
2685 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2687 current_extension = item ;
2689 check_pval(item->u2.statements, apps,in_globals);
2690 break;
2692 case PV_IGNOREPAT:
2693 /* fields: item->u1.str == the ignorepat data
2695 break;
2697 case PV_GLOBALS:
2698 /* fields: item->u1.statements == pval list of statements, usually vardecs
2700 in_abstract_context = 0;
2701 check_pval(item->u1.statements, apps, 1);
2702 break;
2703 default:
2704 break;
2708 void check_pval(pval *item, struct argapp *apps, int in_globals)
2710 pval *i;
2712 /* checks to do:
2713 1. Do goto's point to actual labels?
2714 2. Do macro calls reference a macro?
2715 3. Does the number of macro args match the definition?
2716 4. Is a macro call missing its & at the front?
2717 5. Application calls-- we could check syntax for existing applications,
2718 but I need some some sort of universal description bnf for a general
2719 sort of method for checking arguments, in number, maybe even type, at least.
2720 Don't want to hand code checks for hundreds of applications.
2723 for (i=item; i; i=i->next) {
2724 check_pval_item(i,apps,in_globals);
2728 static void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2731 #ifdef AAL_ARGCHECK
2732 int argapp_errs =0;
2733 char *rfilename;
2734 #endif
2735 struct argapp *apps=0;
2737 if (!item)
2738 return; /* don't check an empty tree */
2739 #ifdef AAL_ARGCHECK
2740 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2741 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2743 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2744 #endif
2745 current_db = item;
2746 errs = warns = notes = 0;
2748 check_context_names();
2749 check_pval(item, apps, 0);
2751 #ifdef AAL_ARGCHECK
2752 argdesc_destroy(apps); /* taketh away */
2753 #endif
2754 current_db = 0;
2756 *arg_errs = errs;
2757 *arg_warns = warns;
2758 *arg_notes = notes;
2761 /* =============================================================================================== */
2762 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2763 /* =============================================================================================== */
2765 static int control_statement_count = 0;
2767 struct ael_priority *new_prio(void)
2769 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2770 return x;
2773 struct ael_extension *new_exten(void)
2775 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2776 return x;
2779 void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten)
2781 char *p1, *p2;
2783 if (!exten->plist) {
2784 exten->plist = prio;
2785 exten->plist_last = prio;
2786 } else {
2787 exten->plist_last->next = prio;
2788 exten->plist_last = prio;
2790 if( !prio->exten )
2791 prio->exten = exten; /* don't override the switch value */
2792 /* The following code will cause all priorities within an extension
2793 to have ${EXTEN} or ${EXTEN: replaced with ~~EXTEN~~, which is
2794 set just before the first switch in an exten. The switches
2795 will muck up the original ${EXTEN} value, so we save it away
2796 and the user accesses this copy instead. */
2797 if (prio->appargs && ((mother_exten && mother_exten->has_switch) || exten->has_switch) ) {
2798 while ((p1 = strstr(prio->appargs, "${EXTEN}"))) {
2799 p2 = malloc(strlen(prio->appargs)+5);
2800 *p1 = 0;
2801 strcpy(p2, prio->appargs);
2802 strcat(p2, "${~~EXTEN~~}");
2803 if (*(p1+8))
2804 strcat(p2, p1+8);
2805 free(prio->appargs);
2806 prio->appargs = p2;
2808 while ((p1 = strstr(prio->appargs, "${EXTEN:"))) {
2809 p2 = malloc(strlen(prio->appargs)+5);
2810 *p1 = 0;
2811 strcpy(p2, prio->appargs);
2812 strcat(p2, "${~~EXTEN~~:");
2813 if (*(p1+8))
2814 strcat(p2, p1+8);
2815 free(prio->appargs);
2816 prio->appargs = p2;
2821 void destroy_extensions(struct ael_extension *exten)
2823 struct ael_extension *ne, *nen;
2824 for (ne=exten; ne; ne=nen) {
2825 struct ael_priority *pe, *pen;
2827 if (ne->name)
2828 free(ne->name);
2830 /* cidmatch fields are allocated with name, and freed when
2831 the name field is freed. Don't do a free for this field,
2832 unless you LIKE to see a crash! */
2834 if (ne->hints)
2835 free(ne->hints);
2837 for (pe=ne->plist; pe; pe=pen) {
2838 pen = pe->next;
2839 if (pe->app)
2840 free(pe->app);
2841 pe->app = 0;
2842 if (pe->appargs)
2843 free(pe->appargs);
2844 pe->appargs = 0;
2845 pe->origin = 0;
2846 pe->goto_true = 0;
2847 pe->goto_false = 0;
2848 free(pe);
2850 nen = ne->next_exten;
2851 ne->next_exten = 0;
2852 ne->plist =0;
2853 ne->plist_last = 0;
2854 ne->next_exten = 0;
2855 ne->loop_break = 0;
2856 ne->loop_continue = 0;
2857 free(ne);
2861 static int label_inside_case(pval *label)
2863 pval *p = label;
2865 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
2866 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
2867 return 1;
2870 p = p->dad;
2872 return 0;
2875 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
2877 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2878 exten->next_exten = add;
2881 static void remove_spaces_before_equals(char *str)
2883 char *p;
2884 while( str && *str && *str != '=' )
2886 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
2888 p = str;
2889 while( *p )
2891 *p = *(p+1);
2892 p++;
2895 else
2896 str++;
2900 static void gen_match_to_pattern(char *pattern, char *result)
2902 /* the result will be a string that will be matched by pattern */
2903 char *p=pattern, *t=result;
2904 while (*p) {
2905 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
2906 *t++ = '9';
2907 else if (*p == '[') {
2908 char *z = p+1;
2909 while (*z != ']')
2910 z++;
2911 if (*(z+1)== ']')
2912 z++;
2913 *t++=*(p+1); /* use the first char in the set */
2914 p = z;
2915 } else {
2916 *t++ = *p;
2918 p++;
2920 *t++ = 0; /* cap it off */
2923 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
2925 pval *p,*p2,*p3;
2926 struct ael_priority *pr;
2927 struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
2928 struct ael_priority *while_test, *while_loop, *while_end;
2929 struct ael_priority *switch_set, *switch_test, *switch_end, *fall_thru, *switch_empty;
2930 struct ael_priority *if_test, *if_end, *if_skip, *if_false;
2931 #ifdef OLD_RAND_ACTION
2932 struct ael_priority *rand_test, *rand_end, *rand_skip;
2933 #endif
2934 char buf1[2000];
2935 char buf2[2000];
2936 char *strp, *strp2;
2937 char new_label[2000];
2938 int default_exists;
2939 int local_control_statement_count;
2940 struct ael_priority *loop_break_save;
2941 struct ael_priority *loop_continue_save;
2942 struct ael_extension *switch_case,*switch_null;
2944 for (p=statement; p; p=p->next) {
2945 switch (p->type) {
2946 case PV_VARDEC:
2947 pr = new_prio();
2948 pr->type = AEL_APPCALL;
2949 snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
2950 pr->app = strdup("Set");
2951 remove_spaces_before_equals(buf1);
2952 pr->appargs = strdup(buf1);
2953 pr->origin = p;
2954 linkprio(exten, pr, mother_exten);
2955 break;
2957 case PV_GOTO:
2958 pr = new_prio();
2959 pr->type = AEL_APPCALL;
2960 p->u2.goto_target = get_goto_target(p);
2961 if( p->u2.goto_target ) {
2962 p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
2965 if (!p->u1.list->next) /* just one */ {
2966 pr->app = strdup("Goto");
2967 if (!mother_exten)
2968 pr->appargs = strdup(p->u1.list->u1.str);
2969 else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
2970 snprintf(buf1,sizeof(buf1),"%s|%s", mother_exten->name, p->u1.list->u1.str);
2971 pr->appargs = strdup(buf1);
2974 } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
2975 snprintf(buf1,sizeof(buf1),"%s|%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
2976 pr->app = strdup("Goto");
2977 pr->appargs = strdup(buf1);
2978 } else if (p->u1.list->next && p->u1.list->next->next) {
2979 snprintf(buf1,sizeof(buf1),"%s|%s|%s", p->u1.list->u1.str,
2980 p->u1.list->next->u1.str,
2981 p->u1.list->next->next->u1.str);
2982 pr->app = strdup("Goto");
2983 pr->appargs = strdup(buf1);
2985 pr->origin = p;
2986 linkprio(exten, pr, mother_exten);
2987 break;
2989 case PV_LABEL:
2990 pr = new_prio();
2991 pr->type = AEL_LABEL;
2992 pr->origin = p;
2993 p->u3.compiled_label = exten;
2994 linkprio(exten, pr, mother_exten);
2995 break;
2997 case PV_FOR:
2998 control_statement_count++;
2999 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3000 loop_continue_save = exten->loop_continue;
3001 snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
3002 for_init = new_prio();
3003 for_inc = new_prio();
3004 for_test = new_prio();
3005 for_loop = new_prio();
3006 for_end = new_prio();
3007 for_init->type = AEL_APPCALL;
3008 for_inc->type = AEL_APPCALL;
3009 for_test->type = AEL_FOR_CONTROL;
3010 for_test->goto_false = for_end;
3011 for_loop->type = AEL_CONTROL1; /* simple goto */
3012 for_end->type = AEL_APPCALL;
3013 for_init->app = strdup("Set");
3015 strcpy(buf2,p->u1.for_init);
3016 remove_spaces_before_equals(buf2);
3017 strp = strchr(buf2, '=');
3018 if (strp) {
3019 strp2 = strchr(p->u1.for_init, '=');
3020 *(strp+1) = 0;
3021 strcat(buf2,"$[");
3022 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3023 strcat(buf2,"]");
3024 for_init->appargs = strdup(buf2);
3025 /* for_init->app = strdup("Set"); just set! */
3026 } else {
3027 strp2 = p->u1.for_init;
3028 while (*strp2 && isspace(*strp2))
3029 strp2++;
3030 if (*strp2 == '&') { /* itsa macro call */
3031 char *strp3 = strp2+1;
3032 while (*strp3 && isspace(*strp3))
3033 strp3++;
3034 strcpy(buf2, strp3);
3035 strp3 = strchr(buf2,'(');
3036 if (strp3) {
3037 *strp3 = '|';
3039 while ((strp3=strchr(buf2,','))) {
3040 *strp3 = '|';
3042 strp3 = strrchr(buf2, ')');
3043 if (strp3)
3044 *strp3 = 0; /* remove the closing paren */
3046 for_init->appargs = strdup(buf2);
3047 if (for_init->app)
3048 free(for_init->app);
3049 for_init->app = strdup("Macro");
3050 } else { /* must be a regular app call */
3051 char *strp3;
3052 strcpy(buf2, strp2);
3053 strp3 = strchr(buf2,'(');
3054 if (strp3) {
3055 *strp3 = 0;
3056 if (for_init->app)
3057 free(for_init->app);
3058 for_init->app = strdup(buf2);
3059 for_init->appargs = strdup(strp3+1);
3060 strp3 = strrchr(for_init->appargs, ')');
3061 if (strp3)
3062 *strp3 = 0; /* remove the closing paren */
3067 strcpy(buf2,p->u3.for_inc);
3068 remove_spaces_before_equals(buf2);
3069 strp = strchr(buf2, '=');
3070 if (strp) { /* there's an = in this part; that means an assignment. set it up */
3071 strp2 = strchr(p->u3.for_inc, '=');
3072 *(strp+1) = 0;
3073 strcat(buf2,"$[");
3074 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3075 strcat(buf2,"]");
3076 for_inc->appargs = strdup(buf2);
3077 for_inc->app = strdup("Set");
3078 } else {
3079 strp2 = p->u3.for_inc;
3080 while (*strp2 && isspace(*strp2))
3081 strp2++;
3082 if (*strp2 == '&') { /* itsa macro call */
3083 char *strp3 = strp2+1;
3084 while (*strp3 && isspace(*strp3))
3085 strp3++;
3086 strcpy(buf2, strp3);
3087 strp3 = strchr(buf2,'(');
3088 if (strp3) {
3089 *strp3 = '|';
3091 while ((strp3=strchr(buf2,','))) {
3092 *strp3 = '|';
3094 strp3 = strrchr(buf2, ')');
3095 if (strp3)
3096 *strp3 = 0; /* remove the closing paren */
3098 for_inc->appargs = strdup(buf2);
3100 for_inc->app = strdup("Macro");
3101 } else { /* must be a regular app call */
3102 char *strp3;
3103 strcpy(buf2, strp2);
3104 strp3 = strchr(buf2,'(');
3105 if (strp3) {
3106 *strp3 = 0;
3107 for_inc->app = strdup(buf2);
3108 for_inc->appargs = strdup(strp3+1);
3109 strp3 = strrchr(for_inc->appargs, ')');
3110 if (strp3)
3111 *strp3 = 0; /* remove the closing paren */
3115 snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
3116 for_test->app = 0;
3117 for_test->appargs = strdup(buf1);
3118 for_loop->goto_true = for_test;
3119 snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
3120 for_end->app = strdup("NoOp");
3121 for_end->appargs = strdup(buf1);
3122 /* link & load! */
3123 linkprio(exten, for_init, mother_exten);
3124 linkprio(exten, for_test, mother_exten);
3126 /* now, put the body of the for loop here */
3127 exten->loop_break = for_end;
3128 exten->loop_continue = for_inc;
3130 gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context); /* this will link in all the statements here */
3132 linkprio(exten, for_inc, mother_exten);
3133 linkprio(exten, for_loop, mother_exten);
3134 linkprio(exten, for_end, mother_exten);
3137 exten->loop_break = loop_break_save;
3138 exten->loop_continue = loop_continue_save;
3139 for_loop->origin = p;
3140 break;
3142 case PV_WHILE:
3143 control_statement_count++;
3144 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3145 loop_continue_save = exten->loop_continue;
3146 snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
3147 while_test = new_prio();
3148 while_loop = new_prio();
3149 while_end = new_prio();
3150 while_test->type = AEL_FOR_CONTROL;
3151 while_test->goto_false = while_end;
3152 while_loop->type = AEL_CONTROL1; /* simple goto */
3153 while_end->type = AEL_APPCALL;
3154 snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
3155 while_test->app = 0;
3156 while_test->appargs = strdup(buf1);
3157 while_loop->goto_true = while_test;
3158 snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
3159 while_end->app = strdup("NoOp");
3160 while_end->appargs = strdup(buf1);
3162 linkprio(exten, while_test, mother_exten);
3164 /* now, put the body of the for loop here */
3165 exten->loop_break = while_end;
3166 exten->loop_continue = while_test;
3168 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the while body statements here */
3170 linkprio(exten, while_loop, mother_exten);
3171 linkprio(exten, while_end, mother_exten);
3174 exten->loop_break = loop_break_save;
3175 exten->loop_continue = loop_continue_save;
3176 while_loop->origin = p;
3177 break;
3179 case PV_SWITCH:
3180 control_statement_count++;
3181 local_control_statement_count = control_statement_count;
3182 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3183 loop_continue_save = exten->loop_continue;
3184 snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
3185 if ((mother_exten && !mother_exten->has_switch)) {
3186 switch_set = new_prio();
3187 switch_set->type = AEL_APPCALL;
3188 switch_set->app = strdup("Set");
3189 switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
3190 linkprio(exten, switch_set, mother_exten);
3191 mother_exten->has_switch = 1;
3192 } else if ((exten && !exten->has_switch)) {
3193 switch_set = new_prio();
3194 switch_set->type = AEL_APPCALL;
3195 switch_set->app = strdup("Set");
3196 switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
3197 linkprio(exten, switch_set, exten);
3198 exten->has_switch = 1;
3200 switch_test = new_prio();
3201 switch_end = new_prio();
3202 switch_test->type = AEL_APPCALL;
3203 switch_end->type = AEL_APPCALL;
3204 strncpy(buf2,p->u1.str,sizeof(buf2));
3205 buf2[sizeof(buf2)-1] = 0; /* just in case */
3206 substitute_commas(buf2);
3207 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",control_statement_count, buf2);
3208 switch_test->app = strdup("Goto");
3209 switch_test->appargs = strdup(buf1);
3210 snprintf(buf1,sizeof(buf1),"Finish switch-%s-%d", label, control_statement_count);
3211 switch_end->app = strdup("NoOp");
3212 switch_end->appargs = strdup(buf1);
3213 switch_end->origin = p;
3214 switch_end->exten = exten;
3216 linkprio(exten, switch_test, mother_exten);
3217 linkprio(exten, switch_end, mother_exten);
3219 exten->loop_break = switch_end;
3220 exten->loop_continue = 0;
3221 default_exists = 0;
3223 for (p2=p->u2.statements; p2; p2=p2->next) {
3224 /* now, for each case/default put the body of the for loop here */
3225 if (p2->type == PV_CASE) {
3226 /* ok, generate a extension and link it in */
3227 switch_case = new_exten();
3228 switch_case->context = this_context;
3229 switch_case->is_switch = 1;
3230 /* the break/continue locations are inherited from parent */
3231 switch_case->loop_break = exten->loop_break;
3232 switch_case->loop_continue = exten->loop_continue;
3234 linkexten(exten,switch_case);
3235 strncpy(buf2,p2->u1.str,sizeof(buf2));
3236 buf2[sizeof(buf2)-1] = 0; /* just in case */
3237 substitute_commas(buf2);
3238 snprintf(buf1,sizeof(buf1),"sw-%d-%s", local_control_statement_count, buf2);
3239 switch_case->name = strdup(buf1);
3240 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
3242 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the case body statements here */
3244 /* here is where we write code to "fall thru" to the next case... if there is one... */
3245 for (p3=p2->u2.statements; p3; p3=p3->next) {
3246 if (!p3->next)
3247 break;
3249 /* p3 now points the last statement... */
3250 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN) ) {
3251 /* is there a following CASE/PATTERN/DEFAULT? */
3252 if (p2->next && p2->next->type == PV_CASE) {
3253 fall_thru = new_prio();
3254 fall_thru->type = AEL_APPCALL;
3255 fall_thru->app = strdup("Goto");
3256 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3257 buf2[sizeof(buf2)-1] = 0; /* just in case */
3258 substitute_commas(buf2);
3259 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3260 fall_thru->appargs = strdup(buf1);
3261 linkprio(switch_case, fall_thru, mother_exten);
3262 } else if (p2->next && p2->next->type == PV_PATTERN) {
3263 fall_thru = new_prio();
3264 fall_thru->type = AEL_APPCALL;
3265 fall_thru->app = strdup("Goto");
3266 gen_match_to_pattern(p2->next->u1.str, buf2);
3267 substitute_commas(buf2);
3268 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10", local_control_statement_count, buf2);
3269 fall_thru->appargs = strdup(buf1);
3270 linkprio(switch_case, fall_thru, mother_exten);
3271 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3272 fall_thru = new_prio();
3273 fall_thru->type = AEL_APPCALL;
3274 fall_thru->app = strdup("Goto");
3275 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3276 fall_thru->appargs = strdup(buf1);
3277 linkprio(switch_case, fall_thru, mother_exten);
3278 } else if (!p2->next) {
3279 fall_thru = new_prio();
3280 fall_thru->type = AEL_CONTROL1;
3281 fall_thru->goto_true = switch_end;
3282 fall_thru->app = strdup("Goto");
3283 linkprio(switch_case, fall_thru, mother_exten);
3286 if (switch_case->return_needed) {
3287 char buf[2000];
3288 struct ael_priority *np2 = new_prio();
3289 np2->type = AEL_APPCALL;
3290 np2->app = strdup("NoOp");
3291 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3292 np2->appargs = strdup(buf);
3293 linkprio(switch_case, np2, mother_exten);
3294 switch_case-> return_target = np2;
3296 } else if (p2->type == PV_PATTERN) {
3297 /* ok, generate a extension and link it in */
3298 switch_case = new_exten();
3299 switch_case->context = this_context;
3300 switch_case->is_switch = 1;
3301 /* the break/continue locations are inherited from parent */
3302 switch_case->loop_break = exten->loop_break;
3303 switch_case->loop_continue = exten->loop_continue;
3305 linkexten(exten,switch_case);
3306 strncpy(buf2,p2->u1.str,sizeof(buf2));
3307 buf2[sizeof(buf2)-1] = 0; /* just in case */
3308 substitute_commas(buf2);
3309 snprintf(buf1,sizeof(buf1),"_sw-%d-%s", local_control_statement_count, buf2);
3310 switch_case->name = strdup(buf1);
3311 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
3313 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the while body statements here */
3314 /* here is where we write code to "fall thru" to the next case... if there is one... */
3315 for (p3=p2->u2.statements; p3; p3=p3->next) {
3316 if (!p3->next)
3317 break;
3319 /* p3 now points the last statement... */
3320 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
3321 /* is there a following CASE/PATTERN/DEFAULT? */
3322 if (p2->next && p2->next->type == PV_CASE) {
3323 fall_thru = new_prio();
3324 fall_thru->type = AEL_APPCALL;
3325 fall_thru->app = strdup("Goto");
3326 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3327 buf2[sizeof(buf2)-1] = 0; /* just in case */
3328 substitute_commas(buf2);
3329 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3330 fall_thru->appargs = strdup(buf1);
3331 linkprio(switch_case, fall_thru, mother_exten);
3332 } else if (p2->next && p2->next->type == PV_PATTERN) {
3333 fall_thru = new_prio();
3334 fall_thru->type = AEL_APPCALL;
3335 fall_thru->app = strdup("Goto");
3336 gen_match_to_pattern(p2->next->u1.str, buf2);
3337 substitute_commas(buf2);
3338 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3339 fall_thru->appargs = strdup(buf1);
3340 linkprio(switch_case, fall_thru, mother_exten);
3341 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3342 fall_thru = new_prio();
3343 fall_thru->type = AEL_APPCALL;
3344 fall_thru->app = strdup("Goto");
3345 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3346 fall_thru->appargs = strdup(buf1);
3347 linkprio(switch_case, fall_thru, mother_exten);
3348 } else if (!p2->next) {
3349 fall_thru = new_prio();
3350 fall_thru->type = AEL_CONTROL1;
3351 fall_thru->goto_true = switch_end;
3352 fall_thru->app = strdup("Goto");
3353 linkprio(switch_case, fall_thru, mother_exten);
3356 if (switch_case->return_needed) {
3357 char buf[2000];
3358 struct ael_priority *np2 = new_prio();
3359 np2->type = AEL_APPCALL;
3360 np2->app = strdup("NoOp");
3361 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3362 np2->appargs = strdup(buf);
3363 linkprio(switch_case, np2, mother_exten);
3364 switch_case-> return_target = np2;
3366 } else if (p2->type == PV_DEFAULT) {
3367 /* ok, generate a extension and link it in */
3368 switch_case = new_exten();
3369 switch_case->context = this_context;
3370 switch_case->is_switch = 1;
3372 /* new: the default case intros a pattern with ., which covers ALMOST everything.
3373 but it doesn't cover a NULL pattern. So, we'll define a null extension to match
3374 that goto's the default extension. */
3376 default_exists++;
3377 switch_null = new_exten();
3378 switch_null->context = this_context;
3379 switch_null->is_switch = 1;
3380 switch_empty = new_prio();
3381 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3382 switch_empty->app = strdup("Goto");
3383 switch_empty->appargs = strdup(buf1);
3384 linkprio(switch_null, switch_empty, mother_exten);
3385 snprintf(buf1,sizeof(buf1),"sw-%d-", local_control_statement_count);
3386 switch_null->name = strdup(buf1);
3387 switch_null->loop_break = exten->loop_break;
3388 switch_null->loop_continue = exten->loop_continue;
3389 linkexten(exten,switch_null);
3391 /* the break/continue locations are inherited from parent */
3392 switch_case->loop_break = exten->loop_break;
3393 switch_case->loop_continue = exten->loop_continue;
3394 linkexten(exten,switch_case);
3395 snprintf(buf1,sizeof(buf1),"_sw-%d-.", local_control_statement_count);
3396 switch_case->name = strdup(buf1);
3398 snprintf(new_label,sizeof(new_label),"sw-%s-default-%d", label, local_control_statement_count);
3400 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the default: body statements here */
3402 /* here is where we write code to "fall thru" to the next case... if there is one... */
3403 for (p3=p2->u2.statements; p3; p3=p3->next) {
3404 if (!p3->next)
3405 break;
3407 /* p3 now points the last statement... */
3408 if (!p3 || (p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
3409 /* is there a following CASE/PATTERN/DEFAULT? */
3410 if (p2->next && p2->next->type == PV_CASE) {
3411 fall_thru = new_prio();
3412 fall_thru->type = AEL_APPCALL;
3413 fall_thru->app = strdup("Goto");
3414 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3415 buf2[sizeof(buf2)-1] = 0; /* just in case */
3416 substitute_commas(buf2);
3417 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3418 fall_thru->appargs = strdup(buf1);
3419 linkprio(switch_case, fall_thru, mother_exten);
3420 } else if (p2->next && p2->next->type == PV_PATTERN) {
3421 fall_thru = new_prio();
3422 fall_thru->type = AEL_APPCALL;
3423 fall_thru->app = strdup("Goto");
3424 gen_match_to_pattern(p2->next->u1.str, buf2);
3425 substitute_commas(buf2);
3426 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3427 fall_thru->appargs = strdup(buf1);
3428 linkprio(switch_case, fall_thru, mother_exten);
3429 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3430 fall_thru = new_prio();
3431 fall_thru->type = AEL_APPCALL;
3432 fall_thru->app = strdup("Goto");
3433 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3434 fall_thru->appargs = strdup(buf1);
3435 linkprio(switch_case, fall_thru, mother_exten);
3436 } else if (!p2->next) {
3437 fall_thru = new_prio();
3438 fall_thru->type = AEL_CONTROL1;
3439 fall_thru->goto_true = switch_end;
3440 fall_thru->app = strdup("Goto");
3441 linkprio(switch_case, fall_thru, mother_exten);
3444 if (switch_case->return_needed) {
3445 char buf[2000];
3446 struct ael_priority *np2 = new_prio();
3447 np2->type = AEL_APPCALL;
3448 np2->app = strdup("NoOp");
3449 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3450 np2->appargs = strdup(buf);
3451 linkprio(switch_case, np2, mother_exten);
3452 switch_case-> return_target = np2;
3454 } else {
3455 /* what could it be??? */
3459 exten->loop_break = loop_break_save;
3460 exten->loop_continue = loop_continue_save;
3461 switch_test->origin = p;
3462 switch_end->origin = p;
3463 break;
3465 case PV_MACRO_CALL:
3466 pr = new_prio();
3467 pr->type = AEL_APPCALL;
3468 snprintf(buf1,sizeof(buf1),"%s", p->u1.str);
3469 for (p2 = p->u2.arglist; p2; p2 = p2->next) {
3470 strcat(buf1,"|");
3471 strcat(buf1,p2->u1.str);
3473 pr->app = strdup("Macro");
3474 pr->appargs = strdup(buf1);
3475 pr->origin = p;
3476 linkprio(exten, pr, mother_exten);
3477 break;
3479 case PV_APPLICATION_CALL:
3480 pr = new_prio();
3481 pr->type = AEL_APPCALL;
3482 buf1[0] = 0;
3483 for (p2 = p->u2.arglist; p2; p2 = p2->next) {
3484 if (p2 != p->u2.arglist )
3485 strcat(buf1,"|");
3486 substitute_commas(p2->u1.str);
3487 strcat(buf1,p2->u1.str);
3489 pr->app = strdup(p->u1.str);
3490 pr->appargs = strdup(buf1);
3491 pr->origin = p;
3492 linkprio(exten, pr, mother_exten);
3493 break;
3495 case PV_BREAK:
3496 pr = new_prio();
3497 pr->type = AEL_CONTROL1; /* simple goto */
3498 pr->goto_true = exten->loop_break;
3499 pr->origin = p;
3500 linkprio(exten, pr, mother_exten);
3501 break;
3503 case PV_RETURN: /* hmmmm */
3504 pr = new_prio();
3505 pr->type = AEL_RETURN; /* simple goto */
3506 exten->return_needed++;
3507 pr->app = strdup("Goto");
3508 pr->appargs = strdup("");
3509 pr->origin = p;
3510 linkprio(exten, pr, mother_exten);
3511 break;
3513 case PV_CONTINUE:
3514 pr = new_prio();
3515 pr->type = AEL_CONTROL1; /* simple goto */
3516 pr->goto_true = exten->loop_continue;
3517 pr->origin = p;
3518 linkprio(exten, pr, mother_exten);
3519 break;
3521 #ifdef OLD_RAND_ACTION
3522 case PV_RANDOM:
3523 control_statement_count++;
3524 snprintf(new_label,sizeof(new_label),"rand-%s-%d", label, control_statement_count);
3525 rand_test = new_prio();
3526 rand_test->type = AEL_RAND_CONTROL;
3527 snprintf(buf1,sizeof(buf1),"$[%s]",
3528 p->u1.str );
3529 rand_test->app = 0;
3530 rand_test->appargs = strdup(buf1);
3531 rand_test->origin = p;
3533 rand_end = new_prio();
3534 rand_end->type = AEL_APPCALL;
3535 snprintf(buf1,sizeof(buf1),"Finish rand-%s-%d", label, control_statement_count);
3536 rand_end->app = strdup("NoOp");
3537 rand_end->appargs = strdup(buf1);
3539 rand_skip = new_prio();
3540 rand_skip->type = AEL_CONTROL1; /* simple goto */
3541 rand_skip->goto_true = rand_end;
3542 rand_skip->origin = p;
3544 rand_test->goto_true = rand_skip; /* +1, really */
3546 linkprio(exten, rand_test, mother_exten);
3548 if (p->u3.else_statements) {
3549 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the else statements here */
3552 linkprio(exten, rand_skip, mother_exten);
3554 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the "true" statements here */
3556 linkprio(exten, rand_end, mother_exten);
3558 break;
3559 #endif
3561 case PV_IFTIME:
3562 control_statement_count++;
3563 snprintf(new_label,sizeof(new_label),"iftime-%s-%d", label, control_statement_count);
3565 if_test = new_prio();
3566 if_test->type = AEL_IFTIME_CONTROL;
3567 snprintf(buf1,sizeof(buf1),"%s|%s|%s|%s",
3568 p->u1.list->u1.str,
3569 p->u1.list->next->u1.str,
3570 p->u1.list->next->next->u1.str,
3571 p->u1.list->next->next->next->u1.str);
3572 if_test->app = 0;
3573 if_test->appargs = strdup(buf1);
3574 if_test->origin = p;
3576 if_end = new_prio();
3577 if_end->type = AEL_APPCALL;
3578 snprintf(buf1,sizeof(buf1),"Finish iftime-%s-%d", label, control_statement_count);
3579 if_end->app = strdup("NoOp");
3580 if_end->appargs = strdup(buf1);
3582 if (p->u3.else_statements) {
3583 if_skip = new_prio();
3584 if_skip->type = AEL_CONTROL1; /* simple goto */
3585 if_skip->goto_true = if_end;
3586 if_skip->origin = p;
3588 } else {
3589 if_skip = 0;
3591 if_test->goto_false = if_end;
3594 if_false = new_prio();
3595 if_false->type = AEL_CONTROL1;
3596 if (p->u3.else_statements) {
3597 if_false->goto_true = if_skip; /* +1 */
3598 } else {
3599 if_false->goto_true = if_end;
3602 /* link & load! */
3603 linkprio(exten, if_test, mother_exten);
3604 linkprio(exten, if_false, mother_exten);
3606 /* now, put the body of the if here */
3608 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
3610 if (p->u3.else_statements) {
3611 linkprio(exten, if_skip, mother_exten);
3612 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
3616 linkprio(exten, if_end, mother_exten);
3618 break;
3620 case PV_RANDOM:
3621 case PV_IF:
3622 control_statement_count++;
3623 snprintf(new_label,sizeof(new_label),"if-%s-%d", label, control_statement_count);
3625 if_test = new_prio();
3626 if_end = new_prio();
3627 if_test->type = AEL_IF_CONTROL;
3628 if_end->type = AEL_APPCALL;
3629 if ( p->type == PV_RANDOM )
3630 snprintf(buf1,sizeof(buf1),"$[${RAND(0,99)} < (%s)]",p->u1.str);
3631 else
3632 snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
3633 if_test->app = 0;
3634 if_test->appargs = strdup(buf1);
3635 snprintf(buf1,sizeof(buf1),"Finish if-%s-%d", label, control_statement_count);
3636 if_end->app = strdup("NoOp");
3637 if_end->appargs = strdup(buf1);
3638 if_test->origin = p;
3640 if (p->u3.else_statements) {
3641 if_skip = new_prio();
3642 if_skip->type = AEL_CONTROL1; /* simple goto */
3643 if_skip->goto_true = if_end;
3644 if_test->goto_false = if_skip;;
3645 } else {
3646 if_skip = 0;
3647 if_test->goto_false = if_end;;
3650 /* link & load! */
3651 linkprio(exten, if_test, mother_exten);
3653 /* now, put the body of the if here */
3655 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
3657 if (p->u3.else_statements) {
3658 linkprio(exten, if_skip, mother_exten);
3659 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
3663 linkprio(exten, if_end, mother_exten);
3665 break;
3667 case PV_STATEMENTBLOCK:
3668 gen_prios(exten, label, p->u1.list, mother_exten, this_context ); /* recurse into the block */
3669 break;
3671 case PV_CATCH:
3672 control_statement_count++;
3673 /* generate an extension with name of catch, put all catch stats
3674 into this exten! */
3675 switch_case = new_exten();
3676 switch_case->context = this_context;
3677 linkexten(exten,switch_case);
3678 switch_case->name = strdup(p->u1.str);
3679 snprintf(new_label,sizeof(new_label),"catch-%s-%d",p->u1.str, control_statement_count);
3681 gen_prios(switch_case, new_label, p->u2.statements,mother_exten,this_context); /* this will link in all the catch body statements here */
3682 if (switch_case->return_needed) {
3683 char buf[2000];
3684 struct ael_priority *np2 = new_prio();
3685 np2->type = AEL_APPCALL;
3686 np2->app = strdup("NoOp");
3687 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3688 np2->appargs = strdup(buf);
3689 linkprio(switch_case, np2, mother_exten);
3690 switch_case-> return_target = np2;
3693 break;
3694 default:
3695 break;
3700 void set_priorities(struct ael_extension *exten)
3702 int i;
3703 struct ael_priority *pr;
3704 do {
3705 if (exten->is_switch)
3706 i = 10;
3707 else if (exten->regexten)
3708 i=2;
3709 else
3710 i=1;
3712 for (pr=exten->plist; pr; pr=pr->next) {
3713 pr->priority_num = i;
3715 if (!pr->origin || (pr->origin && pr->origin->type != PV_LABEL) ) /* Labels don't show up in the dialplan,
3716 but we want them to point to the right
3717 priority, which would be the next line
3718 after the label; */
3719 i++;
3722 exten = exten->next_exten;
3723 } while ( exten );
3726 void add_extensions(struct ael_extension *exten)
3728 struct ael_priority *pr;
3729 char *label=0;
3730 char realext[AST_MAX_EXTENSION];
3731 if (!exten) {
3732 ast_log(LOG_WARNING, "This file is Empty!\n" );
3733 return;
3735 do {
3736 struct ael_priority *last = 0;
3738 memset(realext, '\0', sizeof(realext));
3739 pbx_substitute_variables_helper(NULL, exten->name, realext, sizeof(realext) - 1);
3740 if (exten->hints) {
3741 if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, PRIORITY_HINT, NULL, exten->cidmatch,
3742 exten->hints, NULL, ast_free, registrar)) {
3743 ast_log(LOG_WARNING, "Unable to add step at priority 'hint' of extension '%s'\n",
3744 exten->name);
3748 for (pr=exten->plist; pr; pr=pr->next) {
3749 char app[2000];
3750 char appargs[2000];
3752 /* before we can add the extension, we need to prep the app/appargs;
3753 the CONTROL types need to be done after the priority numbers are calculated.
3755 if (pr->type == AEL_LABEL) /* don't try to put labels in the dialplan! */ {
3756 last = pr;
3757 continue;
3760 if (pr->app)
3761 strcpy(app, pr->app);
3762 else
3763 app[0] = 0;
3764 if (pr->appargs )
3765 strcpy(appargs, pr->appargs);
3766 else
3767 appargs[0] = 0;
3768 switch( pr->type ) {
3769 case AEL_APPCALL:
3770 /* easy case. Everything is all set up */
3771 break;
3773 case AEL_CONTROL1: /* FOR loop, WHILE loop, BREAK, CONTINUE, IF, IFTIME */
3774 /* simple, unconditional goto. */
3775 strcpy(app,"Goto");
3776 if (pr->goto_true->origin && pr->goto_true->origin->type == PV_SWITCH ) {
3777 snprintf(appargs,sizeof(appargs),"%s|%d", pr->goto_true->exten->name, pr->goto_true->priority_num);
3778 } else if (pr->goto_true->origin && pr->goto_true->origin->type == PV_IFTIME && pr->goto_true->origin->u3.else_statements ) {
3779 snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num+1);
3780 } else
3781 snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num);
3782 break;
3784 case AEL_FOR_CONTROL: /* WHILE loop test, FOR loop test */
3785 strcpy(app,"GotoIf");
3786 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
3787 break;
3789 case AEL_IF_CONTROL:
3790 strcpy(app,"GotoIf");
3791 if (pr->origin->u3.else_statements )
3792 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num+1);
3793 else
3794 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
3795 break;
3797 case AEL_RAND_CONTROL:
3798 strcpy(app,"Random");
3799 snprintf(appargs,sizeof(appargs),"%s:%d", pr->appargs, pr->goto_true->priority_num+1);
3800 break;
3802 case AEL_IFTIME_CONTROL:
3803 strcpy(app,"GotoIfTime");
3804 snprintf(appargs,sizeof(appargs),"%s?%d", pr->appargs, pr->priority_num+2);
3805 break;
3807 case AEL_RETURN:
3808 strcpy(app,"Goto");
3809 snprintf(appargs,sizeof(appargs), "%d", exten->return_target->priority_num);
3810 break;
3812 default:
3813 break;
3815 if (last && last->type == AEL_LABEL ) {
3816 label = last->origin->u1.str;
3818 else
3819 label = 0;
3821 if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, pr->priority_num, (label?label:NULL), exten->cidmatch,
3822 app, strdup(appargs), ast_free, registrar)) {
3823 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of extension '%s'\n", pr->priority_num,
3824 exten->name);
3826 last = pr;
3828 exten = exten->next_exten;
3829 } while ( exten );
3832 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem)
3834 /* travel to the end of the list... */
3835 struct ael_extension *lptr;
3836 if( !*list ) {
3837 *list = newmem;
3838 return;
3840 lptr = *list;
3842 while( lptr->next_exten ) {
3843 lptr = lptr->next_exten;
3845 /* lptr should now pointing to the last element in the list; it has a null next_exten pointer */
3846 lptr->next_exten = newmem;
3849 static pval *get_extension_or_contxt(pval *p)
3851 while( p && p->type != PV_EXTENSION && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
3853 p = p->dad;
3856 return p;
3859 static pval *get_contxt(pval *p)
3861 while( p && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
3863 p = p->dad;
3866 return p;
3869 static void fix_gotos_in_extensions(struct ael_extension *exten)
3871 struct ael_extension *e;
3872 for(e=exten;e;e=e->next_exten) {
3874 struct ael_priority *p;
3875 for(p=e->plist;p;p=p->next) {
3877 if( p->origin && p->origin->type == PV_GOTO && p->origin->u3.goto_target_in_case ) {
3879 /* fix the extension of the goto target to the actual extension in the post-compiled dialplan */
3881 pval *target = p->origin->u2.goto_target;
3882 struct ael_extension *z = target->u3.compiled_label;
3883 pval *pv2 = p->origin;
3884 char buf1[500];
3885 char *apparg_save = p->appargs;
3887 p->appargs = 0;
3888 if (!pv2->u1.list->next) /* just one -- it won't hurt to repeat the extension */ {
3889 snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->u1.str);
3890 p->appargs = strdup(buf1);
3892 } else if (pv2->u1.list->next && !pv2->u1.list->next->next) /* two */ {
3893 snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->next->u1.str);
3894 p->appargs = strdup(buf1);
3895 } else if (pv2->u1.list->next && pv2->u1.list->next->next) {
3896 snprintf(buf1,sizeof(buf1),"%s|%s|%s", pv2->u1.list->u1.str,
3897 z->name,
3898 pv2->u1.list->next->next->u1.str);
3899 p->appargs = strdup(buf1);
3901 else
3902 printf("WHAT? The goto doesn't fall into one of three cases for GOTO????\n");
3904 if( apparg_save ) {
3905 free(apparg_save);
3913 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root)
3915 pval *p,*p2;
3916 struct ast_context *context;
3917 char buf[2000];
3918 struct ael_extension *exten;
3919 struct ael_extension *exten_list = 0;
3921 for (p=root; p; p=p->next ) { /* do the globals first, so they'll be there
3922 when we try to eval them */
3923 switch (p->type) {
3924 case PV_GLOBALS:
3925 /* just VARDEC elements */
3926 for (p2=p->u1.list; p2; p2=p2->next) {
3927 char buf2[2000];
3928 snprintf(buf2,sizeof(buf2),"%s=%s", p2->u1.str, p2->u2.val);
3929 pbx_builtin_setvar(NULL, buf2);
3931 break;
3932 default:
3933 break;
3937 for (p=root; p; p=p->next ) {
3938 pval *lp;
3939 int argc;
3941 switch (p->type) {
3942 case PV_MACRO:
3943 strcpy(buf,"macro-");
3944 strcat(buf,p->u1.str);
3945 context = ast_context_create(local_contexts, buf, registrar);
3947 exten = new_exten();
3948 exten->context = context;
3949 exten->name = strdup("s");
3950 argc = 1;
3951 for (lp=p->u2.arglist; lp; lp=lp->next) {
3952 /* for each arg, set up a "Set" command */
3953 struct ael_priority *np2 = new_prio();
3954 np2->type = AEL_APPCALL;
3955 np2->app = strdup("Set");
3956 snprintf(buf,sizeof(buf),"%s=${ARG%d}", lp->u1.str, argc++);
3957 remove_spaces_before_equals(buf);
3958 np2->appargs = strdup(buf);
3959 linkprio(exten, np2, NULL);
3961 /* add any includes */
3962 for (p2=p->u3.macro_statements; p2; p2=p2->next) {
3963 pval *p3;
3965 switch (p2->type) {
3966 case PV_INCLUDES:
3967 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
3968 if ( p3->u2.arglist ) {
3969 snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s",
3970 p3->u1.str,
3971 p3->u2.arglist->u1.str,
3972 p3->u2.arglist->next->u1.str,
3973 p3->u2.arglist->next->next->u1.str,
3974 p3->u2.arglist->next->next->next->u1.str);
3975 ast_context_add_include2(context, buf, registrar);
3976 } else
3977 ast_context_add_include2(context, p3->u1.str, registrar);
3979 break;
3980 default:
3981 break;
3984 /* CONTAINS APPCALLS, CATCH, just like extensions... */
3985 gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context );
3986 if (exten->return_needed) {
3987 struct ael_priority *np2 = new_prio();
3988 np2->type = AEL_APPCALL;
3989 np2->app = strdup("NoOp");
3990 snprintf(buf,sizeof(buf),"End of Macro %s-%s",p->u1.str, exten->name);
3991 np2->appargs = strdup(buf);
3992 linkprio(exten, np2, NULL);
3993 exten-> return_target = np2;
3996 set_priorities(exten);
3997 attach_exten(&exten_list, exten);
3998 break;
4000 case PV_GLOBALS:
4001 /* already done */
4002 break;
4004 case PV_CONTEXT:
4005 context = ast_context_find_or_create(local_contexts, p->u1.str, registrar);
4007 /* contexts contain: ignorepat, includes, switches, eswitches, extensions, */
4008 for (p2=p->u2.statements; p2; p2=p2->next) {
4009 pval *p3;
4010 char *s3;
4012 switch (p2->type) {
4013 case PV_EXTENSION:
4014 exten = new_exten();
4015 exten->name = strdup(p2->u1.str);
4016 exten->context = context;
4018 if( (s3=strchr(exten->name, '/') ) != 0 )
4020 *s3 = 0;
4021 exten->cidmatch = s3+1;
4024 if ( p2->u3.hints )
4025 exten->hints = strdup(p2->u3.hints);
4026 exten->regexten = p2->u4.regexten;
4027 gen_prios(exten, p->u1.str, p2->u2.statements, 0, context );
4028 if (exten->return_needed) {
4029 struct ael_priority *np2 = new_prio();
4030 np2->type = AEL_APPCALL;
4031 np2->app = strdup("NoOp");
4032 snprintf(buf,sizeof(buf),"End of Extension %s", exten->name);
4033 np2->appargs = strdup(buf);
4034 linkprio(exten, np2, NULL);
4035 exten-> return_target = np2;
4037 /* is the last priority in the extension a label? Then add a trailing no-op */
4038 if( !exten->plist_last )
4040 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Empty Extension!\n",
4041 p2->filename, p2->startline, p2->endline);
4044 if ( exten->plist_last && exten->plist_last->type == AEL_LABEL ) {
4045 struct ael_priority *np2 = new_prio();
4046 np2->type = AEL_APPCALL;
4047 np2->app = strdup("NoOp");
4048 snprintf(buf,sizeof(buf),"A NoOp to follow a trailing label %s", exten->plist_last->origin->u1.str);
4049 np2->appargs = strdup(buf);
4050 linkprio(exten, np2, NULL);
4053 set_priorities(exten);
4054 attach_exten(&exten_list, exten);
4055 break;
4057 case PV_IGNOREPAT:
4058 ast_context_add_ignorepat2(context, p2->u1.str, registrar);
4059 break;
4061 case PV_INCLUDES:
4062 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4063 if ( p3->u2.arglist ) {
4064 snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s",
4065 p3->u1.str,
4066 p3->u2.arglist->u1.str,
4067 p3->u2.arglist->next->u1.str,
4068 p3->u2.arglist->next->next->u1.str,
4069 p3->u2.arglist->next->next->next->u1.str);
4070 ast_context_add_include2(context, buf, registrar);
4071 } else
4072 ast_context_add_include2(context, p3->u1.str, registrar);
4074 break;
4076 case PV_SWITCHES:
4077 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4078 char *c = strchr(p3->u1.str, '/');
4079 if (c) {
4080 *c = '\0';
4081 c++;
4082 } else
4083 c = "";
4085 ast_context_add_switch2(context, p3->u1.str, c, 0, registrar);
4087 break;
4089 case PV_ESWITCHES:
4090 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4091 char *c = strchr(p3->u1.str, '/');
4092 if (c) {
4093 *c = '\0';
4094 c++;
4095 } else
4096 c = "";
4098 ast_context_add_switch2(context, p3->u1.str, c, 1, registrar);
4100 break;
4101 default:
4102 break;
4106 break;
4108 default:
4109 /* huh? what? */
4110 break;
4114 /* moved these from being done after a macro or extension were processed,
4115 to after all processing is done, for the sake of fixing gotos to labels inside cases... */
4116 /* I guess this would be considered 2nd pass of compiler now... */
4117 fix_gotos_in_extensions(exten_list); /* find and fix extension ref in gotos to labels that are in case statements */
4118 add_extensions(exten_list); /* actually makes calls to create priorities in ast_contexts -- feeds dialplan to asterisk */
4119 destroy_extensions(exten_list); /* all that remains is an empty husk, discard of it as is proper */
4124 static int aeldebug = 0;
4126 /* interface stuff */
4128 /* if all the below are static, who cares if they are present? */
4130 static int pbx_load_module(void)
4132 int errs=0, sem_err=0, sem_warn=0, sem_note=0;
4133 char *rfilename;
4134 struct ast_context *local_contexts=NULL, *con;
4135 struct pval *parse_tree;
4137 ast_log(LOG_NOTICE, "Starting AEL load process.\n");
4138 if (config[0] == '/')
4139 rfilename = (char *)config;
4140 else {
4141 rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
4142 sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
4144 ast_log(LOG_NOTICE, "AEL load process: calculated config file name '%s'.\n", rfilename);
4146 if (access(rfilename,R_OK) != 0) {
4147 ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
4148 return AST_MODULE_LOAD_DECLINE;
4151 parse_tree = ael2_parse(rfilename, &errs);
4152 ast_log(LOG_NOTICE, "AEL load process: parsed config file name '%s'.\n", rfilename);
4153 ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
4154 if (errs == 0 && sem_err == 0) {
4155 ast_log(LOG_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename);
4156 ast_compile_ael2(&local_contexts, parse_tree);
4157 ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename);
4159 ast_merge_contexts_and_delete(&local_contexts, registrar);
4160 ast_log(LOG_NOTICE, "AEL load process: merged config file name '%s'.\n", rfilename);
4161 for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
4162 ast_context_verify_includes(con);
4163 ast_log(LOG_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename);
4164 } else {
4165 ast_log(LOG_ERROR, "Sorry, but %d syntax errors and %d semantic errors were detected. It doesn't make sense to compile.\n", errs, sem_err);
4166 destroy_pval(parse_tree); /* free up the memory */
4167 return AST_MODULE_LOAD_DECLINE;
4169 destroy_pval(parse_tree); /* free up the memory */
4171 return AST_MODULE_LOAD_SUCCESS;
4174 /* CLI interface */
4175 static int ael2_debug_read(int fd, int argc, char *argv[])
4177 aeldebug |= DEBUG_READ;
4178 return 0;
4181 static int ael2_debug_tokens(int fd, int argc, char *argv[])
4183 aeldebug |= DEBUG_TOKENS;
4184 return 0;
4187 static int ael2_debug_macros(int fd, int argc, char *argv[])
4189 aeldebug |= DEBUG_MACROS;
4190 return 0;
4193 static int ael2_debug_contexts(int fd, int argc, char *argv[])
4195 aeldebug |= DEBUG_CONTEXTS;
4196 return 0;
4199 static int ael2_no_debug(int fd, int argc, char *argv[])
4201 aeldebug = 0;
4202 return 0;
4205 static int ael2_reload(int fd, int argc, char *argv[])
4207 return (pbx_load_module());
4210 static struct ast_cli_entry cli_ael_no_debug = {
4211 { "ael", "no", "debug", NULL },
4212 ael2_no_debug, NULL,
4213 NULL };
4215 static struct ast_cli_entry cli_ael[] = {
4216 { { "ael", "reload", NULL },
4217 ael2_reload, "Reload AEL configuration" },
4219 { { "ael", "debug", "read", NULL },
4220 ael2_debug_read, "Enable AEL read debug (does nothing)" },
4222 { { "ael", "debug", "tokens", NULL },
4223 ael2_debug_tokens, "Enable AEL tokens debug (does nothing)" },
4225 { { "ael", "debug", "macros", NULL },
4226 ael2_debug_macros, "Enable AEL macros debug (does nothing)" },
4228 { { "ael", "debug", "contexts", NULL },
4229 ael2_debug_contexts, "Enable AEL contexts debug (does nothing)" },
4231 { { "ael", "nodebug", NULL },
4232 ael2_no_debug, "Disable AEL debug messages",
4233 NULL, NULL, &cli_ael_no_debug },
4236 static int unload_module(void)
4238 ast_context_destroy(NULL, registrar);
4239 ast_cli_unregister_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
4240 return 0;
4243 static int load_module(void)
4245 ast_cli_register_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
4246 return (pbx_load_module());
4249 static int reload(void)
4251 return pbx_load_module();
4254 #ifdef STANDALONE_AEL
4255 #define AST_MODULE "ael"
4256 int ael_external_load_module(void);
4257 int ael_external_load_module(void)
4259 pbx_load_module();
4260 return 1;
4262 #endif
4264 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
4265 .load = load_module,
4266 .unload = unload_module,
4267 .reload = reload,
4271 /* DESTROY the PVAL tree ============================================================================ */
4275 void destroy_pval_item(pval *item)
4277 if (item == NULL) {
4278 ast_log(LOG_WARNING, "null item\n");
4279 return;
4282 if (item->filename)
4283 free(item->filename);
4285 switch (item->type) {
4286 case PV_WORD:
4287 /* fields: item->u1.str == string associated with this (word). */
4288 if (item->u1.str )
4289 free(item->u1.str);
4290 if ( item->u2.arglist )
4291 destroy_pval(item->u2.arglist);
4292 break;
4294 case PV_MACRO:
4295 /* fields: item->u1.str == name of macro
4296 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
4297 item->u2.arglist->u1.str == argument
4298 item->u2.arglist->next == next arg
4300 item->u3.macro_statements == pval list of statements in macro body.
4302 destroy_pval(item->u2.arglist);
4303 if (item->u1.str )
4304 free(item->u1.str);
4305 destroy_pval(item->u3.macro_statements);
4306 break;
4308 case PV_CONTEXT:
4309 /* fields: item->u1.str == name of context
4310 item->u2.statements == pval list of statements in context body
4311 item->u3.abstract == int 1 if an abstract keyword were present
4313 if (item->u1.str)
4314 free(item->u1.str);
4315 destroy_pval(item->u2.statements);
4316 break;
4318 case PV_MACRO_CALL:
4319 /* fields: item->u1.str == name of macro to call
4320 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
4321 item->u2.arglist->u1.str == argument
4322 item->u2.arglist->next == next arg
4324 if (item->u1.str)
4325 free(item->u1.str);
4326 destroy_pval(item->u2.arglist);
4327 break;
4329 case PV_APPLICATION_CALL:
4330 /* fields: item->u1.str == name of application to call
4331 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
4332 item->u2.arglist->u1.str == argument
4333 item->u2.arglist->next == next arg
4335 if (item->u1.str)
4336 free(item->u1.str);
4337 destroy_pval(item->u2.arglist);
4338 break;
4340 case PV_CASE:
4341 /* fields: item->u1.str == value of case
4342 item->u2.statements == pval list of statements under the case
4344 if (item->u1.str)
4345 free(item->u1.str);
4346 destroy_pval(item->u2.statements);
4347 break;
4349 case PV_PATTERN:
4350 /* fields: item->u1.str == value of case
4351 item->u2.statements == pval list of statements under the case
4353 if (item->u1.str)
4354 free(item->u1.str);
4355 destroy_pval(item->u2.statements);
4356 break;
4358 case PV_DEFAULT:
4359 /* fields:
4360 item->u2.statements == pval list of statements under the case
4362 destroy_pval(item->u2.statements);
4363 break;
4365 case PV_CATCH:
4366 /* fields: item->u1.str == name of extension to catch
4367 item->u2.statements == pval list of statements in context body
4369 if (item->u1.str)
4370 free(item->u1.str);
4371 destroy_pval(item->u2.statements);
4372 break;
4374 case PV_SWITCHES:
4375 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4377 destroy_pval(item->u1.list);
4378 break;
4380 case PV_ESWITCHES:
4381 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4383 destroy_pval(item->u1.list);
4384 break;
4386 case PV_INCLUDES:
4387 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4388 item->u2.arglist == pval list of 4 PV_WORD elements for time values
4390 destroy_pval(item->u1.list);
4391 break;
4393 case PV_STATEMENTBLOCK:
4394 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
4396 destroy_pval(item->u1.list);
4397 break;
4399 case PV_VARDEC:
4400 /* fields: item->u1.str == variable name
4401 item->u2.val == variable value to assign
4403 if (item->u1.str)
4404 free(item->u1.str);
4405 if (item->u2.val)
4406 free(item->u2.val);
4407 break;
4409 case PV_GOTO:
4410 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
4411 item->u1.list->u1.str == where the data on a PV_WORD will always be.
4414 destroy_pval(item->u1.list);
4415 break;
4417 case PV_LABEL:
4418 /* fields: item->u1.str == label name
4420 if (item->u1.str)
4421 free(item->u1.str);
4422 break;
4424 case PV_FOR:
4425 /* fields: item->u1.for_init == a string containing the initalizer
4426 item->u2.for_test == a string containing the loop test
4427 item->u3.for_inc == a string containing the loop increment
4429 item->u4.for_statements == a pval list of statements in the for ()
4431 if (item->u1.for_init)
4432 free(item->u1.for_init);
4433 if (item->u2.for_test)
4434 free(item->u2.for_test);
4435 if (item->u3.for_inc)
4436 free(item->u3.for_inc);
4437 destroy_pval(item->u4.for_statements);
4438 break;
4440 case PV_WHILE:
4441 /* fields: item->u1.str == the while conditional, as supplied by user
4443 item->u2.statements == a pval list of statements in the while ()
4445 if (item->u1.str)
4446 free(item->u1.str);
4447 destroy_pval(item->u2.statements);
4448 break;
4450 case PV_BREAK:
4451 /* fields: none
4453 break;
4455 case PV_RETURN:
4456 /* fields: none
4458 break;
4460 case PV_CONTINUE:
4461 /* fields: none
4463 break;
4465 case PV_IFTIME:
4466 /* fields: item->u1.list == the 4 time values, in PV_WORD structs, linked list
4468 item->u2.statements == a pval list of statements in the if ()
4469 item->u3.else_statements == a pval list of statements in the else
4470 (could be zero)
4472 destroy_pval(item->u1.list);
4473 destroy_pval(item->u2.statements);
4474 if (item->u3.else_statements) {
4475 destroy_pval(item->u3.else_statements);
4477 break;
4479 case PV_RANDOM:
4480 /* fields: item->u1.str == the random percentage, as supplied by user
4482 item->u2.statements == a pval list of statements in the true part ()
4483 item->u3.else_statements == a pval list of statements in the else
4484 (could be zero)
4485 fall thru to If */
4486 case PV_IF:
4487 /* fields: item->u1.str == the if conditional, as supplied by user
4489 item->u2.statements == a pval list of statements in the if ()
4490 item->u3.else_statements == a pval list of statements in the else
4491 (could be zero)
4493 if (item->u1.str)
4494 free(item->u1.str);
4495 destroy_pval(item->u2.statements);
4496 if (item->u3.else_statements) {
4497 destroy_pval(item->u3.else_statements);
4499 break;
4501 case PV_SWITCH:
4502 /* fields: item->u1.str == the switch expression
4504 item->u2.statements == a pval list of statements in the switch,
4505 (will be case statements, most likely!)
4507 if (item->u1.str)
4508 free(item->u1.str);
4509 destroy_pval(item->u2.statements);
4510 break;
4512 case PV_EXTENSION:
4513 /* fields: item->u1.str == the extension name, label, whatever it's called
4515 item->u2.statements == a pval list of statements in the extension
4516 item->u3.hints == a char * hint argument
4517 item->u4.regexten == an int boolean. non-zero says that regexten was specified
4519 if (item->u1.str)
4520 free(item->u1.str);
4521 if (item->u3.hints)
4522 free(item->u3.hints);
4523 destroy_pval(item->u2.statements);
4524 break;
4526 case PV_IGNOREPAT:
4527 /* fields: item->u1.str == the ignorepat data
4529 if (item->u1.str)
4530 free(item->u1.str);
4531 break;
4533 case PV_GLOBALS:
4534 /* fields: item->u1.statements == pval list of statements, usually vardecs
4536 destroy_pval(item->u1.statements);
4537 break;
4539 free(item);
4542 void destroy_pval(pval *item)
4544 pval *i,*nxt;
4546 for (i=item; i; i=nxt) {
4547 nxt = i->next;
4549 destroy_pval_item(i);
4553 #ifdef AAL_ARGCHECK
4554 static char *ael_funclist[] =
4556 "AGENT",
4557 "ARRAY",
4558 "BASE64_DECODE",
4559 "BASE64_ENCODE",
4560 "CALLERID",
4561 "CDR",
4562 "CHANNEL",
4563 "CHECKSIPDOMAIN",
4564 "CHECK_MD5",
4565 "CURL",
4566 "CUT",
4567 "DB",
4568 "DB_EXISTS",
4569 "DUNDILOOKUP",
4570 "ENUMLOOKUP",
4571 "ENV",
4572 "EVAL",
4573 "EXISTS",
4574 "FIELDQTY",
4575 "FILTER",
4576 "GROUP",
4577 "GROUP_COUNT",
4578 "GROUP_LIST",
4579 "GROUP_MATCH_COUNT",
4580 "IAXPEER",
4581 "IF",
4582 "IFTIME",
4583 "ISNULL",
4584 "KEYPADHASH",
4585 "LANGUAGE",
4586 "LEN",
4587 "MATH",
4588 "MD5",
4589 "MUSICCLASS",
4590 "QUEUEAGENTCOUNT",
4591 "QUEUE_MEMBER_COUNT",
4592 "QUEUE_MEMBER_LIST",
4593 "QUOTE",
4594 "RAND",
4595 "REGEX",
4596 "SET",
4597 "SHA1",
4598 "SIPCHANINFO",
4599 "SIPPEER",
4600 "SIP_HEADER",
4601 "SORT",
4602 "STAT",
4603 "STRFTIME",
4604 "STRPTIME",
4605 "TIMEOUT",
4606 "TXTCIDNAME",
4607 "URIDECODE",
4608 "URIENCODE",
4609 "VMCOUNT"
4613 int ael_is_funcname(char *name)
4615 int s,t;
4616 t = sizeof(ael_funclist)/sizeof(char*);
4617 s = 0;
4618 while ((s < t) && strcasecmp(name, ael_funclist[s]))
4619 s++;
4620 if ( s < t )
4621 return 1;
4622 else
4623 return 0;
4625 #endif