remove the dlfcn compatibility stuff, because no platforms that Asterisk currently...
[asterisk-bristuff.git] / pbx / pbx_ael.c
blob6a6e05eef0e520c02526ebb1b35acbbe7f367b74
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;
1302 if (lev>100) {
1303 ast_log(LOG_ERROR,"find_pval_goto in infinite loop! item_type: %d\n\n", item->type);
1304 return;
1307 switch ( item->type ) {
1308 case PV_MACRO:
1309 /* fields: item->u1.str == name of macro
1310 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1311 item->u2.arglist->u1.str == argument
1312 item->u2.arglist->next == next arg
1314 item->u3.macro_statements == pval list of statements in macro body.
1317 /* printf("Descending into macro %s at line %d\n", item->u1.str, item->startline); */
1318 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1320 break;
1322 case PV_CONTEXT:
1323 /* fields: item->u1.str == name of context
1324 item->u2.statements == pval list of statements in context body
1325 item->u3.abstract == int 1 if an abstract keyword were present
1327 break;
1329 case PV_CASE:
1330 /* fields: item->u1.str == value of case
1331 item->u2.statements == pval list of statements under the case
1333 /* printf("Descending into Case of %s\n", item->u1.str); */
1334 find_pval_gotos(item->u2.statements,lev+1);
1335 break;
1337 case PV_PATTERN:
1338 /* fields: item->u1.str == value of case
1339 item->u2.statements == pval list of statements under the case
1341 /* printf("Descending into Pattern of %s\n", item->u1.str); */
1342 find_pval_gotos(item->u2.statements,lev+1);
1343 break;
1345 case PV_DEFAULT:
1346 /* fields:
1347 item->u2.statements == pval list of statements under the case
1349 /* printf("Descending into default\n"); */
1350 find_pval_gotos(item->u2.statements,lev+1);
1351 break;
1353 case PV_CATCH:
1354 /* fields: item->u1.str == name of extension to catch
1355 item->u2.statements == pval list of statements in context body
1357 /* printf("Descending into catch of %s\n", item->u1.str); */
1358 find_pval_gotos(item->u2.statements,lev+1);
1359 break;
1361 case PV_STATEMENTBLOCK:
1362 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1364 /* printf("Descending into statement block\n"); */
1365 find_pval_gotos(item->u1.list,lev+1);
1366 break;
1368 case PV_GOTO:
1369 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1370 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1372 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1373 break;
1375 case PV_INCLUDES:
1376 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1378 for (p4=item->u1.list; p4; p4=p4->next) {
1379 /* for each context pointed to, find it, then find a context/label that matches the
1380 target here! */
1381 char *incl_context = p4->u1.str;
1382 /* find a matching context name */
1383 struct pval *that_context = find_context(incl_context);
1384 if (that_context && that_context->u2.statements) {
1385 /* printf("Descending into include of '%s' at line %d; that_context=%s, that_context type=%d\n", incl_context, item->startline, that_context->u1.str, that_context->type); */
1386 find_pval_gotos(that_context->u2.statements,lev+1); /* keep working up the includes */
1389 break;
1391 case PV_FOR:
1392 /* fields: item->u1.for_init == a string containing the initalizer
1393 item->u2.for_test == a string containing the loop test
1394 item->u3.for_inc == a string containing the loop increment
1396 item->u4.for_statements == a pval list of statements in the for ()
1398 /* printf("Descending into for at line %d\n", item->startline); */
1399 find_pval_gotos(item->u4.for_statements,lev+1);
1400 break;
1402 case PV_WHILE:
1403 /* fields: item->u1.str == the while conditional, as supplied by user
1405 item->u2.statements == a pval list of statements in the while ()
1407 /* printf("Descending into while at line %d\n", item->startline); */
1408 find_pval_gotos(item->u2.statements,lev+1);
1409 break;
1411 case PV_RANDOM:
1412 /* fields: item->u1.str == the random number expression, as supplied by user
1414 item->u2.statements == a pval list of statements in the if ()
1415 item->u3.else_statements == a pval list of statements in the else
1416 (could be zero)
1417 fall thru to PV_IF */
1419 case PV_IFTIME:
1420 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1422 item->u2.statements == a pval list of statements in the if ()
1423 item->u3.else_statements == a pval list of statements in the else
1424 (could be zero)
1425 fall thru to PV_IF*/
1426 case PV_IF:
1427 /* fields: item->u1.str == the if conditional, as supplied by user
1429 item->u2.statements == a pval list of statements in the if ()
1430 item->u3.else_statements == a pval list of statements in the else
1431 (could be zero)
1433 /* printf("Descending into random/iftime/if at line %d\n", item->startline); */
1434 find_pval_gotos(item->u2.statements,lev+1);
1436 if (item->u3.else_statements) {
1437 /* printf("Descending into random/iftime/if's ELSE at line %d\n", item->startline); */
1438 find_pval_gotos(item->u3.else_statements,lev+1);
1440 break;
1442 case PV_SWITCH:
1443 /* fields: item->u1.str == the switch expression
1445 item->u2.statements == a pval list of statements in the switch,
1446 (will be case statements, most likely!)
1448 /* printf("Descending into switch at line %d\n", item->startline); */
1449 find_pval_gotos(item->u3.else_statements,lev+1);
1450 break;
1452 case PV_EXTENSION:
1453 /* fields: item->u1.str == the extension name, label, whatever it's called
1455 item->u2.statements == a pval list of statements in the extension
1456 item->u3.hints == a char * hint argument
1457 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1460 /* printf("Descending into extension %s at line %d\n", item->u1.str, item->startline); */
1461 find_pval_gotos(item->u2.statements,lev+1);
1462 break;
1464 default:
1465 break;
1469 static void find_pval_gotos(pval *item,int lev)
1471 pval *i;
1473 for (i=item; i; i=i->next) {
1474 /* printf("About to call pval_goto_item, itemcount=%d, itemtype=%d\n", item_count, i->type); */
1475 find_pval_goto_item(i, lev);
1481 /* general purpose label finder */
1482 static struct pval *match_pval_item(pval *item)
1484 pval *x;
1486 switch ( item->type ) {
1487 case PV_MACRO:
1488 /* fields: item->u1.str == name of macro
1489 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1490 item->u2.arglist->u1.str == argument
1491 item->u2.arglist->next == next arg
1493 item->u3.macro_statements == pval list of statements in macro body.
1495 /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1496 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1498 /* printf("MACRO: match context is: %s\n", match_context); */
1500 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 */ {
1501 /* printf("Returning on matching macro %s\n", match_context); */
1502 return item;
1506 if (!return_on_context_match) {
1507 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1508 if ((x=match_pval(item->u3.macro_statements))) {
1509 /* printf("Responded with pval match %x\n", x); */
1510 return x;
1513 } else {
1514 /* printf("Skipping context/macro %s\n", item->u1.str); */
1517 break;
1519 case PV_CONTEXT:
1520 /* fields: item->u1.str == name of context
1521 item->u2.statements == pval list of statements in context body
1522 item->u3.abstract == int 1 if an abstract keyword were present
1524 /* printf(" matching in CONTEXT\n"); */
1525 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1526 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1527 /* printf("Returning on matching context %s\n", match_context); */
1528 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1529 return item;
1532 if (!return_on_context_match ) {
1533 /* printf("Descending into matching context %s\n", match_context); */
1534 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1535 /* printf("CONTEXT: Responded with pval match %x\n", x); */
1536 return x;
1539 } else {
1540 /* printf("Skipping context/macro %s\n", item->u1.str); */
1542 break;
1544 case PV_CASE:
1545 /* fields: item->u1.str == value of case
1546 item->u2.statements == pval list of statements under the case
1548 /* printf(" matching in CASE\n"); */
1549 if ((x=match_pval(item->u2.statements))) {
1550 /* printf("CASE: Responded with pval match %x\n", x); */
1551 return x;
1553 break;
1555 case PV_PATTERN:
1556 /* fields: item->u1.str == value of case
1557 item->u2.statements == pval list of statements under the case
1559 /* printf(" matching in PATTERN\n"); */
1560 if ((x=match_pval(item->u2.statements))) {
1561 /* printf("PATTERN: Responded with pval match %x\n", x); */
1562 return x;
1564 break;
1566 case PV_DEFAULT:
1567 /* fields:
1568 item->u2.statements == pval list of statements under the case
1570 /* printf(" matching in DEFAULT\n"); */
1571 if ((x=match_pval(item->u2.statements))) {
1572 /* printf("DEFAULT: Responded with pval match %x\n", x); */
1573 return x;
1575 break;
1577 case PV_CATCH:
1578 /* fields: item->u1.str == name of extension to catch
1579 item->u2.statements == pval list of statements in context body
1581 /* printf(" matching in CATCH\n"); */
1582 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1583 /* printf("Descending into matching catch %s => %s\n", match_exten, item->u1.str); */
1584 if (strcmp(match_label,"1") == 0) {
1585 if (item->u2.statements) {
1586 struct pval *p5 = item->u2.statements;
1587 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1588 p5 = p5->next;
1589 if (p5)
1590 return p5;
1591 else
1592 return 0;
1594 else
1595 return 0;
1598 if ((x=match_pval(item->u2.statements))) {
1599 /* printf("CATCH: Responded with pval match %x\n", (unsigned int)x); */
1600 return x;
1602 } else {
1603 /* printf("Skipping catch %s\n", item->u1.str); */
1605 break;
1607 case PV_STATEMENTBLOCK:
1608 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1610 /* printf(" matching in STATEMENTBLOCK\n"); */
1611 if ((x=match_pval(item->u1.list))) {
1612 /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1613 return x;
1615 break;
1617 case PV_LABEL:
1618 /* fields: item->u1.str == label name
1620 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1621 item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1623 if (count_labels) {
1624 if (!strcmp(match_label, item->u1.str)) {
1625 label_count++;
1626 last_matched_label = item;
1629 } else {
1630 if (!strcmp(match_label, item->u1.str)) {
1631 /* printf("LABEL: Responded with pval match %x\n", x); */
1632 return item;
1635 break;
1637 case PV_FOR:
1638 /* fields: item->u1.for_init == a string containing the initalizer
1639 item->u2.for_test == a string containing the loop test
1640 item->u3.for_inc == a string containing the loop increment
1642 item->u4.for_statements == a pval list of statements in the for ()
1644 /* printf(" matching in FOR\n"); */
1645 if ((x=match_pval(item->u4.for_statements))) {
1646 /* printf("FOR: Responded with pval match %x\n", x);*/
1647 return x;
1649 break;
1651 case PV_WHILE:
1652 /* fields: item->u1.str == the while conditional, as supplied by user
1654 item->u2.statements == a pval list of statements in the while ()
1656 /* printf(" matching in WHILE\n"); */
1657 if ((x=match_pval(item->u2.statements))) {
1658 /* printf("WHILE: Responded with pval match %x\n", x); */
1659 return x;
1661 break;
1663 case PV_RANDOM:
1664 /* fields: item->u1.str == the random number expression, as supplied by user
1666 item->u2.statements == a pval list of statements in the if ()
1667 item->u3.else_statements == a pval list of statements in the else
1668 (could be zero)
1669 fall thru to PV_IF */
1671 case PV_IFTIME:
1672 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1674 item->u2.statements == a pval list of statements in the if ()
1675 item->u3.else_statements == a pval list of statements in the else
1676 (could be zero)
1677 fall thru to PV_IF*/
1678 case PV_IF:
1679 /* fields: item->u1.str == the if conditional, as supplied by user
1681 item->u2.statements == a pval list of statements in the if ()
1682 item->u3.else_statements == a pval list of statements in the else
1683 (could be zero)
1685 /* printf(" matching in IF/IFTIME/RANDOM\n"); */
1686 if ((x=match_pval(item->u2.statements))) {
1687 return x;
1689 if (item->u3.else_statements) {
1690 if ((x=match_pval(item->u3.else_statements))) {
1691 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1692 return x;
1695 break;
1697 case PV_SWITCH:
1698 /* fields: item->u1.str == the switch expression
1700 item->u2.statements == a pval list of statements in the switch,
1701 (will be case statements, most likely!)
1703 /* printf(" matching in SWITCH\n"); */
1704 if ((x=match_pval(item->u2.statements))) {
1705 /* printf("SWITCH: Responded with pval match %x\n", x); */
1706 return x;
1708 break;
1710 case PV_EXTENSION:
1711 /* fields: item->u1.str == the extension name, label, whatever it's called
1713 item->u2.statements == a pval list of statements in the extension
1714 item->u3.hints == a char * hint argument
1715 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1717 /* printf(" matching in EXTENSION\n"); */
1718 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1719 /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1720 if (strcmp(match_label,"1") == 0) {
1721 if (item->u2.statements) {
1722 struct pval *p5 = item->u2.statements;
1723 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1724 p5 = p5->next;
1725 if (p5)
1726 return p5;
1727 else
1728 return 0;
1730 else
1731 return 0;
1734 if ((x=match_pval(item->u2.statements))) {
1735 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1736 return x;
1738 } else {
1739 /* printf("Skipping exten %s\n", item->u1.str); */
1741 break;
1742 default:
1743 /* printf(" matching in default = %d\n", item->type); */
1744 break;
1746 return 0;
1749 struct pval *match_pval(pval *item)
1751 pval *i;
1753 for (i=item; i; i=i->next) {
1754 pval *x;
1755 /* printf(" -- match pval: item %d\n", i->type); */
1757 if ((x = match_pval_item(i))) {
1758 /* printf("match_pval: returning x=%x\n", (int)x); */
1759 return x; /* cut the search short */
1762 return 0;
1765 #if 0
1766 int count_labels_in_current_context(char *label)
1768 label_count = 0;
1769 count_labels = 1;
1770 return_on_context_match = 0;
1771 match_pval(current_context->u2.statements);
1773 return label_count;
1775 #endif
1777 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1779 /* printf(" --- Got args %s, %s\n", exten, label); */
1780 struct pval *ret;
1781 struct pval *p3;
1782 struct pval *startpt = ((curr_cont->type==PV_MACRO)?curr_cont->u3.macro_statements: curr_cont->u2.statements);
1784 count_labels = 0;
1785 return_on_context_match = 0;
1786 match_context = "*";
1787 match_exten = "*";
1788 match_label = label;
1790 ret = match_pval(curr_cont);
1791 if (ret)
1792 return ret;
1794 /* the target of the goto could be in an included context!! Fancy that!! */
1795 /* look for includes in the current context */
1796 for (p3=startpt; p3; p3=p3->next) {
1797 if (p3->type == PV_INCLUDES) {
1798 struct pval *p4;
1799 for (p4=p3->u1.list; p4; p4=p4->next) {
1800 /* for each context pointed to, find it, then find a context/label that matches the
1801 target here! */
1802 char *incl_context = p4->u1.str;
1803 /* find a matching context name */
1804 struct pval *that_context = find_context(incl_context);
1805 if (that_context) {
1806 struct pval *x3;
1807 x3 = find_first_label_in_current_context(label, that_context);
1808 if (x3) {
1809 return x3;
1815 return 0;
1818 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1820 /* printf(" --- Got args %s, %s\n", exten, label); */
1821 struct pval *ret;
1822 struct pval *p3;
1823 struct pval *startpt;
1825 count_labels = 0;
1826 return_on_context_match = 0;
1827 match_context = "*";
1828 match_exten = exten;
1829 match_label = label;
1830 if (curr_cont->type == PV_MACRO)
1831 startpt = curr_cont->u3.macro_statements;
1832 else
1833 startpt = curr_cont->u2.statements;
1835 ret = match_pval(startpt);
1836 if (ret)
1837 return ret;
1839 /* the target of the goto could be in an included context!! Fancy that!! */
1840 /* look for includes in the current context */
1841 for (p3=startpt; p3; p3=p3->next) {
1842 if (p3->type == PV_INCLUDES) {
1843 struct pval *p4;
1844 for (p4=p3->u1.list; p4; p4=p4->next) {
1845 /* for each context pointed to, find it, then find a context/label that matches the
1846 target here! */
1847 char *incl_context = p4->u1.str;
1848 /* find a matching context name */
1849 struct pval *that_context = find_context(incl_context);
1850 if (that_context) {
1851 struct pval *x3;
1852 x3 = find_label_in_current_context(exten, label, that_context);
1853 if (x3) {
1854 return x3;
1860 return 0;
1863 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1865 /* printf(" --- Got args %s\n", label); */
1866 count_labels = 0;
1867 return_on_context_match = 0;
1868 match_context = "*";
1869 match_exten = "*";
1870 match_label = label;
1871 return match_pval(curr_ext);
1874 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1876 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1877 count_labels = 0;
1878 return_on_context_match = 0;
1880 match_context = context;
1881 match_exten = exten;
1882 match_label = label;
1884 return match_pval(current_db);
1888 struct pval *find_macro(char *name)
1890 return_on_context_match = 1;
1891 count_labels = 0;
1892 match_context = name;
1893 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1894 match_label = "*";
1895 return match_pval(current_db);
1898 struct pval *find_context(char *name)
1900 return_on_context_match = 1;
1901 count_labels = 0;
1902 match_context = name;
1903 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1904 match_label = "*";
1905 return match_pval(current_db);
1908 int is_float(char *arg )
1910 char *s;
1911 for (s=arg; *s; s++) {
1912 if (*s != '.' && (*s < '0' || *s > '9'))
1913 return 0;
1915 return 1;
1917 int is_int(char *arg )
1919 char *s;
1920 for (s=arg; *s; s++) {
1921 if (*s < '0' || *s > '9')
1922 return 0;
1924 return 1;
1926 int is_empty(char *arg)
1928 if (!arg)
1929 return 1;
1930 if (*arg == 0)
1931 return 1;
1932 while (*arg) {
1933 if (*arg != ' ' && *arg != '\t')
1934 return 0;
1935 arg++;
1937 return 1;
1940 #ifdef AAL_ARGCHECK
1941 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1943 struct argchoice *ac;
1944 char *opcop,*q,*p;
1946 switch (should->dtype) {
1947 case ARGD_OPTIONSET:
1948 if ( strstr(is->u1.str,"${") )
1949 return 0; /* no checking anything if there's a var reference in there! */
1951 opcop = ast_strdupa(is->u1.str);
1953 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
1954 if ( *q == '(' ) {
1955 p = q+1;
1956 while (*p && *p != ')' )
1957 *p++ = '+';
1958 q = p+1;
1962 for (ac=app->opts; ac; ac=ac->next) {
1963 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
1964 return 0;
1966 for (ac=app->opts; ac; ac=ac->next) {
1967 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
1968 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
1970 if (p && *p == 'j') {
1971 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
1972 is->filename, is->startline, is->endline, app->name);
1973 errs++;
1976 if (p) {
1977 *p = '+';
1978 if (ac->name[1] == '(') {
1979 if (*(p+1) != '(') {
1980 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",
1981 is->filename, is->startline, is->endline, ac->name[0], app->name);
1982 warns++;
1988 for (q=opcop; *q; q++) {
1989 if ( *q != '+' && *q != '(' && *q != ')') {
1990 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",
1991 is->filename, is->startline, is->endline, *q, app->name);
1992 warns++;
1995 return 1;
1996 break;
1997 default:
1998 return 0;
2003 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2005 struct argchoice *ac;
2006 char *opcop;
2008 switch (should->dtype) {
2009 case ARGD_STRING:
2010 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2011 return 0;
2012 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2013 return 1;
2014 break;
2016 case ARGD_INT:
2017 if (is_int(is->u1.str))
2018 return 1;
2019 else
2020 return 0;
2021 break;
2023 case ARGD_FLOAT:
2024 if (is_float(is->u1.str))
2025 return 1;
2026 else
2027 return 0;
2028 break;
2030 case ARGD_ENUM:
2031 if( !is->u1.str || strlen(is->u1.str) == 0 )
2032 return 1; /* a null arg in the call will match an enum, I guess! */
2033 for (ac=should->choices; ac; ac=ac->next) {
2034 if (strcmp(ac->name,is->u1.str) == 0)
2035 return 1;
2037 return 0;
2038 break;
2040 case ARGD_OPTIONSET:
2041 opcop = ast_strdupa(is->u1.str);
2043 for (ac=app->opts; ac; ac=ac->next) {
2044 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2045 return 1;
2047 for (ac=app->opts; ac; ac=ac->next) {
2048 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2049 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2051 if (p) {
2052 *p = '+';
2053 if (ac->name[1] == '(') {
2054 if (*(p+1) == '(') {
2055 char *q = p+1;
2056 while (*q && *q != ')') {
2057 *q++ = '+';
2059 *q = '+';
2065 return 1;
2066 break;
2067 case ARGD_VARARG:
2068 return 1; /* matches anything */
2069 break;
2071 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2073 #endif
2075 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2077 #ifdef AAL_ARGCHECK
2078 struct argdesc *ad = app->args;
2079 pval *pa;
2080 int z;
2082 for (pa = arglist; pa; pa=pa->next) {
2083 if (!ad) {
2084 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2085 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2086 warns++;
2087 return 1;
2088 } else {
2089 /* find the first entry in the ad list that will match */
2090 do {
2091 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2092 break;
2094 z= option_matches( ad, pa, app);
2095 if (!z) {
2096 if ( !arglist )
2097 arglist=appcall;
2099 if (ad->type == ARGD_REQUIRED) {
2100 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2101 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2102 warns++;
2103 return 1;
2105 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2106 option_matches_j( ad, pa, app);
2108 ad = ad->next;
2109 } while (ad && !z);
2112 /* any app nodes left, that are not optional? */
2113 for ( ; ad; ad=ad->next) {
2114 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2115 if ( !arglist )
2116 arglist=appcall;
2117 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2118 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2119 warns++;
2120 return 1;
2123 return 0;
2124 #else
2125 return 0;
2126 #endif
2129 void check_switch_expr(pval *item, struct argapp *apps)
2131 #ifdef AAL_ARGCHECK
2132 /* get and clean the variable name */
2133 char *buff1, *p;
2134 struct argapp *a,*a2;
2135 struct appsetvar *v,*v2;
2136 struct argchoice *c;
2137 pval *t;
2139 p = item->u1.str;
2140 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2141 p++;
2143 buff1 = ast_strdupa(p);
2145 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2146 buff1[strlen(buff1)-1] = 0;
2147 /* buff1 now contains the variable name */
2148 v = 0;
2149 for (a=apps; a; a=a->next) {
2150 for (v=a->setvars;v;v=v->next) {
2151 if (strcmp(v->name,buff1) == 0) {
2152 break;
2155 if ( v )
2156 break;
2158 if (v && v->vals) {
2159 /* we have a match, to a variable that has a set of determined values */
2160 int def= 0;
2161 int pat = 0;
2162 int f1 = 0;
2164 /* first of all, does this switch have a default case ? */
2165 for (t=item->u2.statements; t; t=t->next) {
2166 if (t->type == PV_DEFAULT) {
2167 def =1;
2168 break;
2170 if (t->type == PV_PATTERN) {
2171 pat++;
2174 if (def || pat) /* nothing to check. All cases accounted for! */
2175 return;
2176 for (c=v->vals; c; c=c->next) {
2177 f1 = 0;
2178 for (t=item->u2.statements; t; t=t->next) {
2179 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2180 if (!strcmp(t->u1.str,c->name)) {
2181 f1 = 1;
2182 break;
2186 if (!f1) {
2187 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2188 item->filename, item->startline, item->endline, item->u1.str, c->name);
2189 warns++;
2192 /* next, is there an app call in the current exten, that would set this var? */
2193 f1 = 0;
2194 t = current_extension->u2.statements;
2195 if ( t && t->type == PV_STATEMENTBLOCK )
2196 t = t->u1.statements;
2197 for (; t && t != item; t=t->next) {
2198 if (t->type == PV_APPLICATION_CALL) {
2199 /* find the application that matches the u1.str */
2200 for (a2=apps; a2; a2=a2->next) {
2201 if (strcasecmp(a2->name, t->u1.str)==0) {
2202 for (v2=a2->setvars; v2; v2=v2->next) {
2203 if (strcmp(v2->name, buff1) == 0) {
2204 /* found an app that sets the var */
2205 f1 = 1;
2206 break;
2210 if (f1)
2211 break;
2214 if (f1)
2215 break;
2218 /* see if it sets the var */
2219 if (!f1) {
2220 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",
2221 item->filename, item->startline, item->endline, item->u1.str);
2222 warns++;
2225 #else
2226 pval *t,*tl=0,*p2;
2227 int def= 0;
2229 /* first of all, does this switch have a default case ? */
2230 for (t=item->u2.statements; t; t=t->next) {
2231 if (t->type == PV_DEFAULT) {
2232 def =1;
2233 break;
2235 tl = t;
2237 if (def) /* nothing to check. All cases accounted for! */
2238 return;
2239 /* if no default, warn and insert a default case at the end */
2240 p2 = tl->next = calloc(1, sizeof(struct pval));
2242 p2->type = PV_DEFAULT;
2243 p2->startline = tl->startline;
2244 p2->endline = tl->endline;
2245 p2->startcol = tl->startcol;
2246 p2->endcol = tl->endcol;
2247 p2->filename = strdup(tl->filename);
2248 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2249 p2->filename, p2->startline, p2->endline);
2250 warns++;
2252 #endif
2255 static void check_context_names(void)
2257 pval *i,*j;
2258 for (i=current_db; i; i=i->next) {
2259 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2260 for (j=i->next; j; j=j->next) {
2261 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2262 if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
2264 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",
2265 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
2266 errs++;
2274 static void check_abstract_reference(pval *abstract_context)
2276 pval *i,*j;
2277 /* find some context includes that reference this context */
2280 /* otherwise, print out a warning */
2281 for (i=current_db; i; i=i->next) {
2282 if (i->type == PV_CONTEXT) {
2283 for (j=i->u2. statements; j; j=j->next) {
2284 if ( j->type == PV_INCLUDES ) {
2285 struct pval *p4;
2286 for (p4=j->u1.list; p4; p4=p4->next) {
2287 /* for each context pointed to, find it, then find a context/label that matches the
2288 target here! */
2289 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2290 return; /* found a match! */
2296 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",
2297 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2298 warns++;
2302 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2304 pval *lp;
2305 #ifdef AAL_ARGCHECK
2306 struct argapp *app, *found;
2307 #endif
2308 struct pval *macro_def;
2309 struct pval *app_def;
2311 char errmsg[4096];
2312 char *strp;
2314 switch (item->type) {
2315 case PV_WORD:
2316 /* fields: item->u1.str == string associated with this (word).
2317 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2318 break;
2320 case PV_MACRO:
2321 /* fields: item->u1.str == name of macro
2322 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2323 item->u2.arglist->u1.str == argument
2324 item->u2.arglist->next == next arg
2326 item->u3.macro_statements == pval list of statements in macro body.
2328 in_abstract_context = 0;
2329 current_context = item;
2330 current_extension = 0;
2331 for (lp=item->u2.arglist; lp; lp=lp->next) {
2334 check_pval(item->u3.macro_statements, apps,in_globals);
2335 break;
2337 case PV_CONTEXT:
2338 /* fields: item->u1.str == name of context
2339 item->u2.statements == pval list of statements in context body
2340 item->u3.abstract == int 1 if an abstract keyword were present
2342 current_context = item;
2343 current_extension = 0;
2344 if ( item->u3.abstract ) {
2345 in_abstract_context = 1;
2346 check_abstract_reference(item);
2347 } else
2348 in_abstract_context = 0;
2349 check_pval(item->u2.statements, apps,in_globals);
2350 break;
2352 case PV_MACRO_CALL:
2353 /* fields: item->u1.str == name of macro to call
2354 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2355 item->u2.arglist->u1.str == argument
2356 item->u2.arglist->next == next arg
2358 macro_def = find_macro(item->u1.str);
2359 if (!macro_def) {
2360 /* here is a good place to check to see if the definition is in extensions.conf! */
2361 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s ! Hopefully it is present in extensions.conf! \n",
2362 item->filename, item->startline, item->endline, item->u1.str);
2363 warns++;
2364 } else if (macro_def->type != PV_MACRO) {
2365 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2366 item->filename, item->startline, item->endline, item->u1.str);
2367 errs++;
2368 } else {
2369 /* macro_def is a MACRO, so do the args match in number? */
2370 int hereargs = 0;
2371 int thereargs = 0;
2373 for (lp=item->u2.arglist; lp; lp=lp->next) {
2374 hereargs++;
2376 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2377 thereargs++;
2379 if (hereargs != thereargs ) {
2380 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",
2381 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2382 errs++;
2385 break;
2387 case PV_APPLICATION_CALL:
2388 /* fields: item->u1.str == name of application to call
2389 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2390 item->u2.arglist->u1.str == argument
2391 item->u2.arglist->next == next arg
2393 /* Need to check to see if the application is available! */
2394 app_def = find_context(item->u1.str);
2395 if (app_def && app_def->type == PV_MACRO) {
2396 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2397 item->filename, item->startline, item->endline, item->u1.str);
2398 errs++;
2400 if (strcasecmp(item->u1.str,"GotoIf") == 0
2401 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2402 || strcasecmp(item->u1.str,"while") == 0
2403 || strcasecmp(item->u1.str,"endwhile") == 0
2404 || strcasecmp(item->u1.str,"random") == 0
2405 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2406 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",
2407 item->filename, item->startline, item->endline, item->u1.str);
2408 warns++;
2410 #ifdef AAL_ARGCHECK
2411 found = 0;
2412 for (app=apps; app; app=app->next) {
2413 if (strcasecmp(app->name, item->u1.str) == 0) {
2414 found =app;
2415 break;
2418 if (!found) {
2419 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2420 item->filename, item->startline, item->endline, item->u1.str);
2421 warns++;
2422 } else
2423 check_app_args(item, item->u2.arglist, app);
2424 #endif
2425 break;
2427 case PV_CASE:
2428 /* fields: item->u1.str == value of case
2429 item->u2.statements == pval list of statements under the case
2431 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2432 /* find the last statement */
2433 check_pval(item->u2.statements, apps,in_globals);
2434 break;
2436 case PV_PATTERN:
2437 /* fields: item->u1.str == value of case
2438 item->u2.statements == pval list of statements under the case
2440 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2441 /* find the last statement */
2443 check_pval(item->u2.statements, apps,in_globals);
2444 break;
2446 case PV_DEFAULT:
2447 /* fields:
2448 item->u2.statements == pval list of statements under the case
2451 check_pval(item->u2.statements, apps,in_globals);
2452 break;
2454 case PV_CATCH:
2455 /* fields: item->u1.str == name of extension to catch
2456 item->u2.statements == pval list of statements in context body
2458 check_pval(item->u2.statements, apps,in_globals);
2459 break;
2461 case PV_SWITCHES:
2462 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2464 check_pval(item->u1.list, apps,in_globals);
2465 break;
2467 case PV_ESWITCHES:
2468 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2470 check_pval(item->u1.list, apps,in_globals);
2471 break;
2473 case PV_INCLUDES:
2474 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2476 check_pval(item->u1.list, apps,in_globals);
2477 check_includes(item);
2478 for (lp=item->u1.list; lp; lp=lp->next){
2479 char *incl_context = lp->u1.str;
2480 struct pval *that_context = find_context(incl_context);
2482 if ( lp->u2.arglist ) {
2483 check_timerange(lp->u2.arglist);
2484 check_dow(lp->u2.arglist->next);
2485 check_day(lp->u2.arglist->next->next);
2486 check_month(lp->u2.arglist->next->next->next);
2489 if (that_context) {
2490 find_pval_gotos(that_context->u2.statements,0);
2494 break;
2496 case PV_STATEMENTBLOCK:
2497 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2499 check_pval(item->u1.list, apps,in_globals);
2500 break;
2502 case PV_VARDEC:
2503 /* fields: item->u1.str == variable name
2504 item->u2.val == variable value to assign
2506 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2507 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2508 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);
2509 ast_expr_register_extra_error_info(errmsg);
2510 ast_expr(item->u2.val, expr_output, sizeof(expr_output));
2511 ast_expr_clear_extra_error_info();
2512 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2513 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2514 item->filename, item->startline, item->endline, item->u2.val);
2515 warns++;
2517 check_expr2_input(item,item->u2.val);
2519 break;
2521 case PV_GOTO:
2522 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2523 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2525 /* don't check goto's in abstract contexts */
2526 if ( in_abstract_context )
2527 break;
2529 check_goto(item);
2530 break;
2532 case PV_LABEL:
2533 /* fields: item->u1.str == label name
2535 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2536 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2537 item->filename, item->startline, item->endline, item->u1.str);
2538 warns++;
2541 check_label(item);
2542 break;
2544 case PV_FOR:
2545 /* fields: item->u1.for_init == a string containing the initalizer
2546 item->u2.for_test == a string containing the loop test
2547 item->u3.for_inc == a string containing the loop increment
2549 item->u4.for_statements == a pval list of statements in the for ()
2551 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);
2552 ast_expr_register_extra_error_info(errmsg);
2554 strp = strchr(item->u1.for_init, '=');
2555 if (strp) {
2556 ast_expr(strp+1, expr_output, sizeof(expr_output));
2558 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output));
2559 strp = strchr(item->u3.for_inc, '=');
2560 if (strp) {
2561 ast_expr(strp+1, expr_output, sizeof(expr_output));
2563 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2564 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2565 item->filename, item->startline, item->endline, item->u2.for_test);
2566 warns++;
2568 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2569 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2570 item->filename, item->startline, item->endline, item->u3.for_inc);
2571 warns++;
2573 check_expr2_input(item,item->u2.for_test);
2574 check_expr2_input(item,item->u3.for_inc);
2576 ast_expr_clear_extra_error_info();
2577 check_pval(item->u4.for_statements, apps,in_globals);
2578 break;
2580 case PV_WHILE:
2581 /* fields: item->u1.str == the while conditional, as supplied by user
2583 item->u2.statements == a pval list of statements in the while ()
2585 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2586 ast_expr_register_extra_error_info(errmsg);
2587 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2588 ast_expr_clear_extra_error_info();
2589 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2590 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2591 item->filename, item->startline, item->endline, item->u1.str);
2592 warns++;
2594 check_expr2_input(item,item->u1.str);
2595 check_pval(item->u2.statements, apps,in_globals);
2596 break;
2598 case PV_BREAK:
2599 /* fields: none
2601 check_break(item);
2602 break;
2604 case PV_RETURN:
2605 /* fields: none
2607 break;
2609 case PV_CONTINUE:
2610 /* fields: none
2612 check_continue(item);
2613 break;
2615 case PV_RANDOM:
2616 /* fields: item->u1.str == the random number expression, as supplied by user
2618 item->u2.statements == a pval list of statements in the if ()
2619 item->u3.else_statements == a pval list of statements in the else
2620 (could be zero)
2622 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2623 ast_expr_register_extra_error_info(errmsg);
2624 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2625 ast_expr_clear_extra_error_info();
2626 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2627 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2628 item->filename, item->startline, item->endline, item->u1.str);
2629 warns++;
2631 check_expr2_input(item,item->u1.str);
2632 check_pval(item->u2.statements, apps,in_globals);
2633 if (item->u3.else_statements) {
2634 check_pval(item->u3.else_statements, apps,in_globals);
2636 break;
2638 case PV_IFTIME:
2639 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2641 item->u2.statements == a pval list of statements in the if ()
2642 item->u3.else_statements == a pval list of statements in the else
2643 (could be zero)
2645 if ( item->u2.arglist ) {
2646 check_timerange(item->u1.list);
2647 check_dow(item->u1.list->next);
2648 check_day(item->u1.list->next->next);
2649 check_month(item->u1.list->next->next->next);
2652 check_pval(item->u2.statements, apps,in_globals);
2653 if (item->u3.else_statements) {
2654 check_pval(item->u3.else_statements, apps,in_globals);
2656 break;
2658 case PV_IF:
2659 /* fields: item->u1.str == the if conditional, as supplied by user
2661 item->u2.statements == a pval list of statements in the if ()
2662 item->u3.else_statements == a pval list of statements in the else
2663 (could be zero)
2665 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2666 ast_expr_register_extra_error_info(errmsg);
2667 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2668 ast_expr_clear_extra_error_info();
2669 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2670 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2671 item->filename, item->startline, item->endline, item->u1.str);
2672 warns++;
2674 check_expr2_input(item,item->u1.str);
2675 check_pval(item->u2.statements, apps,in_globals);
2676 if (item->u3.else_statements) {
2677 check_pval(item->u3.else_statements, apps,in_globals);
2679 break;
2681 case PV_SWITCH:
2682 /* fields: item->u1.str == the switch expression
2684 item->u2.statements == a pval list of statements in the switch,
2685 (will be case statements, most likely!)
2687 /* we can check the switch expression, see if it matches any of the app variables...
2688 if it does, then, are all the possible cases accounted for? */
2689 check_switch_expr(item, apps);
2690 check_pval(item->u2.statements, apps,in_globals);
2691 break;
2693 case PV_EXTENSION:
2694 /* fields: item->u1.str == the extension name, label, whatever it's called
2696 item->u2.statements == a pval list of statements in the extension
2697 item->u3.hints == a char * hint argument
2698 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2700 current_extension = item ;
2702 check_pval(item->u2.statements, apps,in_globals);
2703 break;
2705 case PV_IGNOREPAT:
2706 /* fields: item->u1.str == the ignorepat data
2708 break;
2710 case PV_GLOBALS:
2711 /* fields: item->u1.statements == pval list of statements, usually vardecs
2713 in_abstract_context = 0;
2714 check_pval(item->u1.statements, apps, 1);
2715 break;
2716 default:
2717 break;
2721 void check_pval(pval *item, struct argapp *apps, int in_globals)
2723 pval *i;
2725 /* checks to do:
2726 1. Do goto's point to actual labels?
2727 2. Do macro calls reference a macro?
2728 3. Does the number of macro args match the definition?
2729 4. Is a macro call missing its & at the front?
2730 5. Application calls-- we could check syntax for existing applications,
2731 but I need some some sort of universal description bnf for a general
2732 sort of method for checking arguments, in number, maybe even type, at least.
2733 Don't want to hand code checks for hundreds of applications.
2736 for (i=item; i; i=i->next) {
2737 check_pval_item(i,apps,in_globals);
2741 static void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2744 #ifdef AAL_ARGCHECK
2745 int argapp_errs =0;
2746 char *rfilename;
2747 #endif
2748 struct argapp *apps=0;
2750 if (!item)
2751 return; /* don't check an empty tree */
2752 #ifdef AAL_ARGCHECK
2753 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2754 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2756 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2757 #endif
2758 current_db = item;
2759 errs = warns = notes = 0;
2761 check_context_names();
2762 check_pval(item, apps, 0);
2764 #ifdef AAL_ARGCHECK
2765 argdesc_destroy(apps); /* taketh away */
2766 #endif
2767 current_db = 0;
2769 *arg_errs = errs;
2770 *arg_warns = warns;
2771 *arg_notes = notes;
2774 /* =============================================================================================== */
2775 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2776 /* =============================================================================================== */
2778 static int control_statement_count = 0;
2780 struct ael_priority *new_prio(void)
2782 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2783 return x;
2786 struct ael_extension *new_exten(void)
2788 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2789 return x;
2792 void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten)
2794 char *p1, *p2;
2796 if (!exten->plist) {
2797 exten->plist = prio;
2798 exten->plist_last = prio;
2799 } else {
2800 exten->plist_last->next = prio;
2801 exten->plist_last = prio;
2803 if( !prio->exten )
2804 prio->exten = exten; /* don't override the switch value */
2805 /* The following code will cause all priorities within an extension
2806 to have ${EXTEN} or ${EXTEN: replaced with ~~EXTEN~~, which is
2807 set just before the first switch in an exten. The switches
2808 will muck up the original ${EXTEN} value, so we save it away
2809 and the user accesses this copy instead. */
2810 if (prio->appargs && ((mother_exten && mother_exten->has_switch) || exten->has_switch) ) {
2811 while ((p1 = strstr(prio->appargs, "${EXTEN}"))) {
2812 p2 = malloc(strlen(prio->appargs)+5);
2813 *p1 = 0;
2814 strcpy(p2, prio->appargs);
2815 strcat(p2, "${~~EXTEN~~}");
2816 if (*(p1+8))
2817 strcat(p2, p1+8);
2818 free(prio->appargs);
2819 prio->appargs = p2;
2821 while ((p1 = strstr(prio->appargs, "${EXTEN:"))) {
2822 p2 = malloc(strlen(prio->appargs)+5);
2823 *p1 = 0;
2824 strcpy(p2, prio->appargs);
2825 strcat(p2, "${~~EXTEN~~:");
2826 if (*(p1+8))
2827 strcat(p2, p1+8);
2828 free(prio->appargs);
2829 prio->appargs = p2;
2834 void destroy_extensions(struct ael_extension *exten)
2836 struct ael_extension *ne, *nen;
2837 for (ne=exten; ne; ne=nen) {
2838 struct ael_priority *pe, *pen;
2840 if (ne->name)
2841 free(ne->name);
2843 /* cidmatch fields are allocated with name, and freed when
2844 the name field is freed. Don't do a free for this field,
2845 unless you LIKE to see a crash! */
2847 if (ne->hints)
2848 free(ne->hints);
2850 for (pe=ne->plist; pe; pe=pen) {
2851 pen = pe->next;
2852 if (pe->app)
2853 free(pe->app);
2854 pe->app = 0;
2855 if (pe->appargs)
2856 free(pe->appargs);
2857 pe->appargs = 0;
2858 pe->origin = 0;
2859 pe->goto_true = 0;
2860 pe->goto_false = 0;
2861 free(pe);
2863 nen = ne->next_exten;
2864 ne->next_exten = 0;
2865 ne->plist =0;
2866 ne->plist_last = 0;
2867 ne->next_exten = 0;
2868 ne->loop_break = 0;
2869 ne->loop_continue = 0;
2870 free(ne);
2874 static int label_inside_case(pval *label)
2876 pval *p = label;
2878 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
2879 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
2880 return 1;
2883 p = p->dad;
2885 return 0;
2888 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
2890 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2891 exten->next_exten = add;
2894 static void remove_spaces_before_equals(char *str)
2896 char *p;
2897 while( str && *str && *str != '=' )
2899 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
2901 p = str;
2902 while( *p )
2904 *p = *(p+1);
2905 p++;
2908 else
2909 str++;
2913 static void gen_match_to_pattern(char *pattern, char *result)
2915 /* the result will be a string that will be matched by pattern */
2916 char *p=pattern, *t=result;
2917 while (*p) {
2918 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
2919 *t++ = '9';
2920 else if (*p == '[') {
2921 char *z = p+1;
2922 while (*z != ']')
2923 z++;
2924 if (*(z+1)== ']')
2925 z++;
2926 *t++=*(p+1); /* use the first char in the set */
2927 p = z;
2928 } else {
2929 *t++ = *p;
2931 p++;
2933 *t++ = 0; /* cap it off */
2936 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
2938 pval *p,*p2,*p3;
2939 struct ael_priority *pr;
2940 struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
2941 struct ael_priority *while_test, *while_loop, *while_end;
2942 struct ael_priority *switch_set, *switch_test, *switch_end, *fall_thru, *switch_empty;
2943 struct ael_priority *if_test, *if_end, *if_skip, *if_false;
2944 #ifdef OLD_RAND_ACTION
2945 struct ael_priority *rand_test, *rand_end, *rand_skip;
2946 #endif
2947 char buf1[2000];
2948 char buf2[2000];
2949 char *strp, *strp2;
2950 char new_label[2000];
2951 int default_exists;
2952 int local_control_statement_count;
2953 struct ael_priority *loop_break_save;
2954 struct ael_priority *loop_continue_save;
2955 struct ael_extension *switch_case,*switch_null;
2957 for (p=statement; p; p=p->next) {
2958 switch (p->type) {
2959 case PV_VARDEC:
2960 pr = new_prio();
2961 pr->type = AEL_APPCALL;
2962 snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
2963 pr->app = strdup("Set");
2964 remove_spaces_before_equals(buf1);
2965 pr->appargs = strdup(buf1);
2966 pr->origin = p;
2967 linkprio(exten, pr, mother_exten);
2968 break;
2970 case PV_GOTO:
2971 pr = new_prio();
2972 pr->type = AEL_APPCALL;
2973 p->u2.goto_target = get_goto_target(p);
2974 if( p->u2.goto_target ) {
2975 p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
2978 if (!p->u1.list->next) /* just one */ {
2979 pr->app = strdup("Goto");
2980 if (!mother_exten)
2981 pr->appargs = strdup(p->u1.list->u1.str);
2982 else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
2983 snprintf(buf1,sizeof(buf1),"%s|%s", mother_exten->name, p->u1.list->u1.str);
2984 pr->appargs = strdup(buf1);
2987 } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
2988 snprintf(buf1,sizeof(buf1),"%s|%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
2989 pr->app = strdup("Goto");
2990 pr->appargs = strdup(buf1);
2991 } else if (p->u1.list->next && p->u1.list->next->next) {
2992 snprintf(buf1,sizeof(buf1),"%s|%s|%s", p->u1.list->u1.str,
2993 p->u1.list->next->u1.str,
2994 p->u1.list->next->next->u1.str);
2995 pr->app = strdup("Goto");
2996 pr->appargs = strdup(buf1);
2998 pr->origin = p;
2999 linkprio(exten, pr, mother_exten);
3000 break;
3002 case PV_LABEL:
3003 pr = new_prio();
3004 pr->type = AEL_LABEL;
3005 pr->origin = p;
3006 p->u3.compiled_label = exten;
3007 linkprio(exten, pr, mother_exten);
3008 break;
3010 case PV_FOR:
3011 control_statement_count++;
3012 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3013 loop_continue_save = exten->loop_continue;
3014 snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
3015 for_init = new_prio();
3016 for_inc = new_prio();
3017 for_test = new_prio();
3018 for_loop = new_prio();
3019 for_end = new_prio();
3020 for_init->type = AEL_APPCALL;
3021 for_inc->type = AEL_APPCALL;
3022 for_test->type = AEL_FOR_CONTROL;
3023 for_test->goto_false = for_end;
3024 for_loop->type = AEL_CONTROL1; /* simple goto */
3025 for_end->type = AEL_APPCALL;
3026 for_init->app = strdup("Set");
3028 strcpy(buf2,p->u1.for_init);
3029 remove_spaces_before_equals(buf2);
3030 strp = strchr(buf2, '=');
3031 if (strp) {
3032 strp2 = strchr(p->u1.for_init, '=');
3033 *(strp+1) = 0;
3034 strcat(buf2,"$[");
3035 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3036 strcat(buf2,"]");
3037 for_init->appargs = strdup(buf2);
3038 /* for_init->app = strdup("Set"); just set! */
3039 } else {
3040 strp2 = p->u1.for_init;
3041 while (*strp2 && isspace(*strp2))
3042 strp2++;
3043 if (*strp2 == '&') { /* itsa macro call */
3044 char *strp3 = strp2+1;
3045 while (*strp3 && isspace(*strp3))
3046 strp3++;
3047 strcpy(buf2, strp3);
3048 strp3 = strchr(buf2,'(');
3049 if (strp3) {
3050 *strp3 = '|';
3052 while ((strp3=strchr(buf2,','))) {
3053 *strp3 = '|';
3055 strp3 = strrchr(buf2, ')');
3056 if (strp3)
3057 *strp3 = 0; /* remove the closing paren */
3059 for_init->appargs = strdup(buf2);
3060 if (for_init->app)
3061 free(for_init->app);
3062 for_init->app = strdup("Macro");
3063 } else { /* must be a regular app call */
3064 char *strp3;
3065 strcpy(buf2, strp2);
3066 strp3 = strchr(buf2,'(');
3067 if (strp3) {
3068 *strp3 = 0;
3069 if (for_init->app)
3070 free(for_init->app);
3071 for_init->app = strdup(buf2);
3072 for_init->appargs = strdup(strp3+1);
3073 strp3 = strrchr(for_init->appargs, ')');
3074 if (strp3)
3075 *strp3 = 0; /* remove the closing paren */
3080 strcpy(buf2,p->u3.for_inc);
3081 remove_spaces_before_equals(buf2);
3082 strp = strchr(buf2, '=');
3083 if (strp) { /* there's an = in this part; that means an assignment. set it up */
3084 strp2 = strchr(p->u3.for_inc, '=');
3085 *(strp+1) = 0;
3086 strcat(buf2,"$[");
3087 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3088 strcat(buf2,"]");
3089 for_inc->appargs = strdup(buf2);
3090 for_inc->app = strdup("Set");
3091 } else {
3092 strp2 = p->u3.for_inc;
3093 while (*strp2 && isspace(*strp2))
3094 strp2++;
3095 if (*strp2 == '&') { /* itsa macro call */
3096 char *strp3 = strp2+1;
3097 while (*strp3 && isspace(*strp3))
3098 strp3++;
3099 strcpy(buf2, strp3);
3100 strp3 = strchr(buf2,'(');
3101 if (strp3) {
3102 *strp3 = '|';
3104 while ((strp3=strchr(buf2,','))) {
3105 *strp3 = '|';
3107 strp3 = strrchr(buf2, ')');
3108 if (strp3)
3109 *strp3 = 0; /* remove the closing paren */
3111 for_inc->appargs = strdup(buf2);
3113 for_inc->app = strdup("Macro");
3114 } else { /* must be a regular app call */
3115 char *strp3;
3116 strcpy(buf2, strp2);
3117 strp3 = strchr(buf2,'(');
3118 if (strp3) {
3119 *strp3 = 0;
3120 for_inc->app = strdup(buf2);
3121 for_inc->appargs = strdup(strp3+1);
3122 strp3 = strrchr(for_inc->appargs, ')');
3123 if (strp3)
3124 *strp3 = 0; /* remove the closing paren */
3128 snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
3129 for_test->app = 0;
3130 for_test->appargs = strdup(buf1);
3131 for_loop->goto_true = for_test;
3132 snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
3133 for_end->app = strdup("NoOp");
3134 for_end->appargs = strdup(buf1);
3135 /* link & load! */
3136 linkprio(exten, for_init, mother_exten);
3137 linkprio(exten, for_test, mother_exten);
3139 /* now, put the body of the for loop here */
3140 exten->loop_break = for_end;
3141 exten->loop_continue = for_inc;
3143 gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context); /* this will link in all the statements here */
3145 linkprio(exten, for_inc, mother_exten);
3146 linkprio(exten, for_loop, mother_exten);
3147 linkprio(exten, for_end, mother_exten);
3150 exten->loop_break = loop_break_save;
3151 exten->loop_continue = loop_continue_save;
3152 for_loop->origin = p;
3153 break;
3155 case PV_WHILE:
3156 control_statement_count++;
3157 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3158 loop_continue_save = exten->loop_continue;
3159 snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
3160 while_test = new_prio();
3161 while_loop = new_prio();
3162 while_end = new_prio();
3163 while_test->type = AEL_FOR_CONTROL;
3164 while_test->goto_false = while_end;
3165 while_loop->type = AEL_CONTROL1; /* simple goto */
3166 while_end->type = AEL_APPCALL;
3167 snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
3168 while_test->app = 0;
3169 while_test->appargs = strdup(buf1);
3170 while_loop->goto_true = while_test;
3171 snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
3172 while_end->app = strdup("NoOp");
3173 while_end->appargs = strdup(buf1);
3175 linkprio(exten, while_test, mother_exten);
3177 /* now, put the body of the for loop here */
3178 exten->loop_break = while_end;
3179 exten->loop_continue = while_test;
3181 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the while body statements here */
3183 linkprio(exten, while_loop, mother_exten);
3184 linkprio(exten, while_end, mother_exten);
3187 exten->loop_break = loop_break_save;
3188 exten->loop_continue = loop_continue_save;
3189 while_loop->origin = p;
3190 break;
3192 case PV_SWITCH:
3193 control_statement_count++;
3194 local_control_statement_count = control_statement_count;
3195 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3196 loop_continue_save = exten->loop_continue;
3197 snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
3198 if ((mother_exten && !mother_exten->has_switch)) {
3199 switch_set = new_prio();
3200 switch_set->type = AEL_APPCALL;
3201 switch_set->app = strdup("Set");
3202 switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
3203 linkprio(exten, switch_set, mother_exten);
3204 mother_exten->has_switch = 1;
3205 } else if ((exten && !exten->has_switch)) {
3206 switch_set = new_prio();
3207 switch_set->type = AEL_APPCALL;
3208 switch_set->app = strdup("Set");
3209 switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
3210 linkprio(exten, switch_set, exten);
3211 exten->has_switch = 1;
3213 switch_test = new_prio();
3214 switch_end = new_prio();
3215 switch_test->type = AEL_APPCALL;
3216 switch_end->type = AEL_APPCALL;
3217 strncpy(buf2,p->u1.str,sizeof(buf2));
3218 buf2[sizeof(buf2)-1] = 0; /* just in case */
3219 substitute_commas(buf2);
3220 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",control_statement_count, buf2);
3221 switch_test->app = strdup("Goto");
3222 switch_test->appargs = strdup(buf1);
3223 snprintf(buf1,sizeof(buf1),"Finish switch-%s-%d", label, control_statement_count);
3224 switch_end->app = strdup("NoOp");
3225 switch_end->appargs = strdup(buf1);
3226 switch_end->origin = p;
3227 switch_end->exten = exten;
3229 linkprio(exten, switch_test, mother_exten);
3230 linkprio(exten, switch_end, mother_exten);
3232 exten->loop_break = switch_end;
3233 exten->loop_continue = 0;
3234 default_exists = 0;
3236 for (p2=p->u2.statements; p2; p2=p2->next) {
3237 /* now, for each case/default put the body of the for loop here */
3238 if (p2->type == PV_CASE) {
3239 /* ok, generate a extension and link it in */
3240 switch_case = new_exten();
3241 switch_case->context = this_context;
3242 switch_case->is_switch = 1;
3243 /* the break/continue locations are inherited from parent */
3244 switch_case->loop_break = exten->loop_break;
3245 switch_case->loop_continue = exten->loop_continue;
3247 linkexten(exten,switch_case);
3248 strncpy(buf2,p2->u1.str,sizeof(buf2));
3249 buf2[sizeof(buf2)-1] = 0; /* just in case */
3250 substitute_commas(buf2);
3251 snprintf(buf1,sizeof(buf1),"sw-%d-%s", local_control_statement_count, buf2);
3252 switch_case->name = strdup(buf1);
3253 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
3255 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the case body statements here */
3257 /* here is where we write code to "fall thru" to the next case... if there is one... */
3258 for (p3=p2->u2.statements; p3; p3=p3->next) {
3259 if (!p3->next)
3260 break;
3262 /* p3 now points the last statement... */
3263 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN) ) {
3264 /* is there a following CASE/PATTERN/DEFAULT? */
3265 if (p2->next && p2->next->type == PV_CASE) {
3266 fall_thru = new_prio();
3267 fall_thru->type = AEL_APPCALL;
3268 fall_thru->app = strdup("Goto");
3269 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3270 buf2[sizeof(buf2)-1] = 0; /* just in case */
3271 substitute_commas(buf2);
3272 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3273 fall_thru->appargs = strdup(buf1);
3274 linkprio(switch_case, fall_thru, mother_exten);
3275 } else if (p2->next && p2->next->type == PV_PATTERN) {
3276 fall_thru = new_prio();
3277 fall_thru->type = AEL_APPCALL;
3278 fall_thru->app = strdup("Goto");
3279 gen_match_to_pattern(p2->next->u1.str, buf2);
3280 substitute_commas(buf2);
3281 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10", local_control_statement_count, buf2);
3282 fall_thru->appargs = strdup(buf1);
3283 linkprio(switch_case, fall_thru, mother_exten);
3284 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3285 fall_thru = new_prio();
3286 fall_thru->type = AEL_APPCALL;
3287 fall_thru->app = strdup("Goto");
3288 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3289 fall_thru->appargs = strdup(buf1);
3290 linkprio(switch_case, fall_thru, mother_exten);
3291 } else if (!p2->next) {
3292 fall_thru = new_prio();
3293 fall_thru->type = AEL_CONTROL1;
3294 fall_thru->goto_true = switch_end;
3295 fall_thru->app = strdup("Goto");
3296 linkprio(switch_case, fall_thru, mother_exten);
3299 if (switch_case->return_needed) {
3300 char buf[2000];
3301 struct ael_priority *np2 = new_prio();
3302 np2->type = AEL_APPCALL;
3303 np2->app = strdup("NoOp");
3304 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3305 np2->appargs = strdup(buf);
3306 linkprio(switch_case, np2, mother_exten);
3307 switch_case-> return_target = np2;
3309 } else if (p2->type == PV_PATTERN) {
3310 /* ok, generate a extension and link it in */
3311 switch_case = new_exten();
3312 switch_case->context = this_context;
3313 switch_case->is_switch = 1;
3314 /* the break/continue locations are inherited from parent */
3315 switch_case->loop_break = exten->loop_break;
3316 switch_case->loop_continue = exten->loop_continue;
3318 linkexten(exten,switch_case);
3319 strncpy(buf2,p2->u1.str,sizeof(buf2));
3320 buf2[sizeof(buf2)-1] = 0; /* just in case */
3321 substitute_commas(buf2);
3322 snprintf(buf1,sizeof(buf1),"_sw-%d-%s", local_control_statement_count, buf2);
3323 switch_case->name = strdup(buf1);
3324 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
3326 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the while body statements here */
3327 /* here is where we write code to "fall thru" to the next case... if there is one... */
3328 for (p3=p2->u2.statements; p3; p3=p3->next) {
3329 if (!p3->next)
3330 break;
3332 /* p3 now points the last statement... */
3333 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
3334 /* is there a following CASE/PATTERN/DEFAULT? */
3335 if (p2->next && p2->next->type == PV_CASE) {
3336 fall_thru = new_prio();
3337 fall_thru->type = AEL_APPCALL;
3338 fall_thru->app = strdup("Goto");
3339 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3340 buf2[sizeof(buf2)-1] = 0; /* just in case */
3341 substitute_commas(buf2);
3342 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3343 fall_thru->appargs = strdup(buf1);
3344 linkprio(switch_case, fall_thru, mother_exten);
3345 } else if (p2->next && p2->next->type == PV_PATTERN) {
3346 fall_thru = new_prio();
3347 fall_thru->type = AEL_APPCALL;
3348 fall_thru->app = strdup("Goto");
3349 gen_match_to_pattern(p2->next->u1.str, buf2);
3350 substitute_commas(buf2);
3351 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3352 fall_thru->appargs = strdup(buf1);
3353 linkprio(switch_case, fall_thru, mother_exten);
3354 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3355 fall_thru = new_prio();
3356 fall_thru->type = AEL_APPCALL;
3357 fall_thru->app = strdup("Goto");
3358 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3359 fall_thru->appargs = strdup(buf1);
3360 linkprio(switch_case, fall_thru, mother_exten);
3361 } else if (!p2->next) {
3362 fall_thru = new_prio();
3363 fall_thru->type = AEL_CONTROL1;
3364 fall_thru->goto_true = switch_end;
3365 fall_thru->app = strdup("Goto");
3366 linkprio(switch_case, fall_thru, mother_exten);
3369 if (switch_case->return_needed) {
3370 char buf[2000];
3371 struct ael_priority *np2 = new_prio();
3372 np2->type = AEL_APPCALL;
3373 np2->app = strdup("NoOp");
3374 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3375 np2->appargs = strdup(buf);
3376 linkprio(switch_case, np2, mother_exten);
3377 switch_case-> return_target = np2;
3379 } else if (p2->type == PV_DEFAULT) {
3380 /* ok, generate a extension and link it in */
3381 switch_case = new_exten();
3382 switch_case->context = this_context;
3383 switch_case->is_switch = 1;
3385 /* new: the default case intros a pattern with ., which covers ALMOST everything.
3386 but it doesn't cover a NULL pattern. So, we'll define a null extension to match
3387 that goto's the default extension. */
3389 default_exists++;
3390 switch_null = new_exten();
3391 switch_null->context = this_context;
3392 switch_null->is_switch = 1;
3393 switch_empty = new_prio();
3394 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3395 switch_empty->app = strdup("Goto");
3396 switch_empty->appargs = strdup(buf1);
3397 linkprio(switch_null, switch_empty, mother_exten);
3398 snprintf(buf1,sizeof(buf1),"sw-%d-", local_control_statement_count);
3399 switch_null->name = strdup(buf1);
3400 switch_null->loop_break = exten->loop_break;
3401 switch_null->loop_continue = exten->loop_continue;
3402 linkexten(exten,switch_null);
3404 /* the break/continue locations are inherited from parent */
3405 switch_case->loop_break = exten->loop_break;
3406 switch_case->loop_continue = exten->loop_continue;
3407 linkexten(exten,switch_case);
3408 snprintf(buf1,sizeof(buf1),"_sw-%d-.", local_control_statement_count);
3409 switch_case->name = strdup(buf1);
3411 snprintf(new_label,sizeof(new_label),"sw-%s-default-%d", label, local_control_statement_count);
3413 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the default: body statements here */
3415 /* here is where we write code to "fall thru" to the next case... if there is one... */
3416 for (p3=p2->u2.statements; p3; p3=p3->next) {
3417 if (!p3->next)
3418 break;
3420 /* p3 now points the last statement... */
3421 if (!p3 || (p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
3422 /* is there a following CASE/PATTERN/DEFAULT? */
3423 if (p2->next && p2->next->type == PV_CASE) {
3424 fall_thru = new_prio();
3425 fall_thru->type = AEL_APPCALL;
3426 fall_thru->app = strdup("Goto");
3427 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3428 buf2[sizeof(buf2)-1] = 0; /* just in case */
3429 substitute_commas(buf2);
3430 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3431 fall_thru->appargs = strdup(buf1);
3432 linkprio(switch_case, fall_thru, mother_exten);
3433 } else if (p2->next && p2->next->type == PV_PATTERN) {
3434 fall_thru = new_prio();
3435 fall_thru->type = AEL_APPCALL;
3436 fall_thru->app = strdup("Goto");
3437 gen_match_to_pattern(p2->next->u1.str, buf2);
3438 substitute_commas(buf2);
3439 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3440 fall_thru->appargs = strdup(buf1);
3441 linkprio(switch_case, fall_thru, mother_exten);
3442 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3443 fall_thru = new_prio();
3444 fall_thru->type = AEL_APPCALL;
3445 fall_thru->app = strdup("Goto");
3446 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3447 fall_thru->appargs = strdup(buf1);
3448 linkprio(switch_case, fall_thru, mother_exten);
3449 } else if (!p2->next) {
3450 fall_thru = new_prio();
3451 fall_thru->type = AEL_CONTROL1;
3452 fall_thru->goto_true = switch_end;
3453 fall_thru->app = strdup("Goto");
3454 linkprio(switch_case, fall_thru, mother_exten);
3457 if (switch_case->return_needed) {
3458 char buf[2000];
3459 struct ael_priority *np2 = new_prio();
3460 np2->type = AEL_APPCALL;
3461 np2->app = strdup("NoOp");
3462 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3463 np2->appargs = strdup(buf);
3464 linkprio(switch_case, np2, mother_exten);
3465 switch_case-> return_target = np2;
3467 } else {
3468 /* what could it be??? */
3472 exten->loop_break = loop_break_save;
3473 exten->loop_continue = loop_continue_save;
3474 switch_test->origin = p;
3475 switch_end->origin = p;
3476 break;
3478 case PV_MACRO_CALL:
3479 pr = new_prio();
3480 pr->type = AEL_APPCALL;
3481 snprintf(buf1,sizeof(buf1),"%s", p->u1.str);
3482 for (p2 = p->u2.arglist; p2; p2 = p2->next) {
3483 strcat(buf1,"|");
3484 strcat(buf1,p2->u1.str);
3486 pr->app = strdup("Macro");
3487 pr->appargs = strdup(buf1);
3488 pr->origin = p;
3489 linkprio(exten, pr, mother_exten);
3490 break;
3492 case PV_APPLICATION_CALL:
3493 pr = new_prio();
3494 pr->type = AEL_APPCALL;
3495 buf1[0] = 0;
3496 for (p2 = p->u2.arglist; p2; p2 = p2->next) {
3497 if (p2 != p->u2.arglist )
3498 strcat(buf1,"|");
3499 substitute_commas(p2->u1.str);
3500 strcat(buf1,p2->u1.str);
3502 pr->app = strdup(p->u1.str);
3503 pr->appargs = strdup(buf1);
3504 pr->origin = p;
3505 linkprio(exten, pr, mother_exten);
3506 break;
3508 case PV_BREAK:
3509 pr = new_prio();
3510 pr->type = AEL_CONTROL1; /* simple goto */
3511 pr->goto_true = exten->loop_break;
3512 pr->origin = p;
3513 linkprio(exten, pr, mother_exten);
3514 break;
3516 case PV_RETURN: /* hmmmm */
3517 pr = new_prio();
3518 pr->type = AEL_RETURN; /* simple goto */
3519 exten->return_needed++;
3520 pr->app = strdup("Goto");
3521 pr->appargs = strdup("");
3522 pr->origin = p;
3523 linkprio(exten, pr, mother_exten);
3524 break;
3526 case PV_CONTINUE:
3527 pr = new_prio();
3528 pr->type = AEL_CONTROL1; /* simple goto */
3529 pr->goto_true = exten->loop_continue;
3530 pr->origin = p;
3531 linkprio(exten, pr, mother_exten);
3532 break;
3534 #ifdef OLD_RAND_ACTION
3535 case PV_RANDOM:
3536 control_statement_count++;
3537 snprintf(new_label,sizeof(new_label),"rand-%s-%d", label, control_statement_count);
3538 rand_test = new_prio();
3539 rand_test->type = AEL_RAND_CONTROL;
3540 snprintf(buf1,sizeof(buf1),"$[%s]",
3541 p->u1.str );
3542 rand_test->app = 0;
3543 rand_test->appargs = strdup(buf1);
3544 rand_test->origin = p;
3546 rand_end = new_prio();
3547 rand_end->type = AEL_APPCALL;
3548 snprintf(buf1,sizeof(buf1),"Finish rand-%s-%d", label, control_statement_count);
3549 rand_end->app = strdup("NoOp");
3550 rand_end->appargs = strdup(buf1);
3552 rand_skip = new_prio();
3553 rand_skip->type = AEL_CONTROL1; /* simple goto */
3554 rand_skip->goto_true = rand_end;
3555 rand_skip->origin = p;
3557 rand_test->goto_true = rand_skip; /* +1, really */
3559 linkprio(exten, rand_test, mother_exten);
3561 if (p->u3.else_statements) {
3562 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the else statements here */
3565 linkprio(exten, rand_skip, mother_exten);
3567 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the "true" statements here */
3569 linkprio(exten, rand_end, mother_exten);
3571 break;
3572 #endif
3574 case PV_IFTIME:
3575 control_statement_count++;
3576 snprintf(new_label,sizeof(new_label),"iftime-%s-%d", label, control_statement_count);
3578 if_test = new_prio();
3579 if_test->type = AEL_IFTIME_CONTROL;
3580 snprintf(buf1,sizeof(buf1),"%s|%s|%s|%s",
3581 p->u1.list->u1.str,
3582 p->u1.list->next->u1.str,
3583 p->u1.list->next->next->u1.str,
3584 p->u1.list->next->next->next->u1.str);
3585 if_test->app = 0;
3586 if_test->appargs = strdup(buf1);
3587 if_test->origin = p;
3589 if_end = new_prio();
3590 if_end->type = AEL_APPCALL;
3591 snprintf(buf1,sizeof(buf1),"Finish iftime-%s-%d", label, control_statement_count);
3592 if_end->app = strdup("NoOp");
3593 if_end->appargs = strdup(buf1);
3595 if (p->u3.else_statements) {
3596 if_skip = new_prio();
3597 if_skip->type = AEL_CONTROL1; /* simple goto */
3598 if_skip->goto_true = if_end;
3599 if_skip->origin = p;
3601 } else {
3602 if_skip = 0;
3604 if_test->goto_false = if_end;
3607 if_false = new_prio();
3608 if_false->type = AEL_CONTROL1;
3609 if (p->u3.else_statements) {
3610 if_false->goto_true = if_skip; /* +1 */
3611 } else {
3612 if_false->goto_true = if_end;
3615 /* link & load! */
3616 linkprio(exten, if_test, mother_exten);
3617 linkprio(exten, if_false, mother_exten);
3619 /* now, put the body of the if here */
3621 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
3623 if (p->u3.else_statements) {
3624 linkprio(exten, if_skip, mother_exten);
3625 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
3629 linkprio(exten, if_end, mother_exten);
3631 break;
3633 case PV_RANDOM:
3634 case PV_IF:
3635 control_statement_count++;
3636 snprintf(new_label,sizeof(new_label),"if-%s-%d", label, control_statement_count);
3638 if_test = new_prio();
3639 if_end = new_prio();
3640 if_test->type = AEL_IF_CONTROL;
3641 if_end->type = AEL_APPCALL;
3642 if ( p->type == PV_RANDOM )
3643 snprintf(buf1,sizeof(buf1),"$[${RAND(0|99)} < (%s)]",p->u1.str);
3644 else
3645 snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
3646 if_test->app = 0;
3647 if_test->appargs = strdup(buf1);
3648 snprintf(buf1,sizeof(buf1),"Finish if-%s-%d", label, control_statement_count);
3649 if_end->app = strdup("NoOp");
3650 if_end->appargs = strdup(buf1);
3651 if_test->origin = p;
3653 if (p->u3.else_statements) {
3654 if_skip = new_prio();
3655 if_skip->type = AEL_CONTROL1; /* simple goto */
3656 if_skip->goto_true = if_end;
3657 if_test->goto_false = if_skip;;
3658 } else {
3659 if_skip = 0;
3660 if_test->goto_false = if_end;;
3663 /* link & load! */
3664 linkprio(exten, if_test, mother_exten);
3666 /* now, put the body of the if here */
3668 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
3670 if (p->u3.else_statements) {
3671 linkprio(exten, if_skip, mother_exten);
3672 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
3676 linkprio(exten, if_end, mother_exten);
3678 break;
3680 case PV_STATEMENTBLOCK:
3681 gen_prios(exten, label, p->u1.list, mother_exten, this_context ); /* recurse into the block */
3682 break;
3684 case PV_CATCH:
3685 control_statement_count++;
3686 /* generate an extension with name of catch, put all catch stats
3687 into this exten! */
3688 switch_case = new_exten();
3689 switch_case->context = this_context;
3690 linkexten(exten,switch_case);
3691 switch_case->name = strdup(p->u1.str);
3692 snprintf(new_label,sizeof(new_label),"catch-%s-%d",p->u1.str, control_statement_count);
3694 gen_prios(switch_case, new_label, p->u2.statements,mother_exten,this_context); /* this will link in all the catch body statements here */
3695 if (switch_case->return_needed) {
3696 char buf[2000];
3697 struct ael_priority *np2 = new_prio();
3698 np2->type = AEL_APPCALL;
3699 np2->app = strdup("NoOp");
3700 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3701 np2->appargs = strdup(buf);
3702 linkprio(switch_case, np2, mother_exten);
3703 switch_case-> return_target = np2;
3706 break;
3707 default:
3708 break;
3713 void set_priorities(struct ael_extension *exten)
3715 int i;
3716 struct ael_priority *pr;
3717 do {
3718 if (exten->is_switch)
3719 i = 10;
3720 else if (exten->regexten)
3721 i=2;
3722 else
3723 i=1;
3725 for (pr=exten->plist; pr; pr=pr->next) {
3726 pr->priority_num = i;
3728 if (!pr->origin || (pr->origin && pr->origin->type != PV_LABEL) ) /* Labels don't show up in the dialplan,
3729 but we want them to point to the right
3730 priority, which would be the next line
3731 after the label; */
3732 i++;
3735 exten = exten->next_exten;
3736 } while ( exten );
3739 void add_extensions(struct ael_extension *exten)
3741 struct ael_priority *pr;
3742 char *label=0;
3743 char realext[AST_MAX_EXTENSION];
3744 if (!exten) {
3745 ast_log(LOG_WARNING, "This file is Empty!\n" );
3746 return;
3748 do {
3749 struct ael_priority *last = 0;
3751 memset(realext, '\0', sizeof(realext));
3752 pbx_substitute_variables_helper(NULL, exten->name, realext, sizeof(realext) - 1);
3753 if (exten->hints) {
3754 if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, PRIORITY_HINT, NULL, exten->cidmatch,
3755 exten->hints, NULL, ast_free, registrar)) {
3756 ast_log(LOG_WARNING, "Unable to add step at priority 'hint' of extension '%s'\n",
3757 exten->name);
3761 for (pr=exten->plist; pr; pr=pr->next) {
3762 char app[2000];
3763 char appargs[2000];
3765 /* before we can add the extension, we need to prep the app/appargs;
3766 the CONTROL types need to be done after the priority numbers are calculated.
3768 if (pr->type == AEL_LABEL) /* don't try to put labels in the dialplan! */ {
3769 last = pr;
3770 continue;
3773 if (pr->app)
3774 strcpy(app, pr->app);
3775 else
3776 app[0] = 0;
3777 if (pr->appargs )
3778 strcpy(appargs, pr->appargs);
3779 else
3780 appargs[0] = 0;
3781 switch( pr->type ) {
3782 case AEL_APPCALL:
3783 /* easy case. Everything is all set up */
3784 break;
3786 case AEL_CONTROL1: /* FOR loop, WHILE loop, BREAK, CONTINUE, IF, IFTIME */
3787 /* simple, unconditional goto. */
3788 strcpy(app,"Goto");
3789 if (pr->goto_true->origin && pr->goto_true->origin->type == PV_SWITCH ) {
3790 snprintf(appargs,sizeof(appargs),"%s|%d", pr->goto_true->exten->name, pr->goto_true->priority_num);
3791 } else if (pr->goto_true->origin && pr->goto_true->origin->type == PV_IFTIME && pr->goto_true->origin->u3.else_statements ) {
3792 snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num+1);
3793 } else
3794 snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num);
3795 break;
3797 case AEL_FOR_CONTROL: /* WHILE loop test, FOR loop test */
3798 strcpy(app,"GotoIf");
3799 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
3800 break;
3802 case AEL_IF_CONTROL:
3803 strcpy(app,"GotoIf");
3804 if (pr->origin->u3.else_statements )
3805 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num+1);
3806 else
3807 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
3808 break;
3810 case AEL_RAND_CONTROL:
3811 strcpy(app,"Random");
3812 snprintf(appargs,sizeof(appargs),"%s:%d", pr->appargs, pr->goto_true->priority_num+1);
3813 break;
3815 case AEL_IFTIME_CONTROL:
3816 strcpy(app,"GotoIfTime");
3817 snprintf(appargs,sizeof(appargs),"%s?%d", pr->appargs, pr->priority_num+2);
3818 break;
3820 case AEL_RETURN:
3821 strcpy(app,"Goto");
3822 snprintf(appargs,sizeof(appargs), "%d", exten->return_target->priority_num);
3823 break;
3825 default:
3826 break;
3828 if (last && last->type == AEL_LABEL ) {
3829 label = last->origin->u1.str;
3831 else
3832 label = 0;
3834 if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, pr->priority_num, (label?label:NULL), exten->cidmatch,
3835 app, strdup(appargs), ast_free, registrar)) {
3836 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of extension '%s'\n", pr->priority_num,
3837 exten->name);
3839 last = pr;
3841 exten = exten->next_exten;
3842 } while ( exten );
3845 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem)
3847 /* travel to the end of the list... */
3848 struct ael_extension *lptr;
3849 if( !*list ) {
3850 *list = newmem;
3851 return;
3853 lptr = *list;
3855 while( lptr->next_exten ) {
3856 lptr = lptr->next_exten;
3858 /* lptr should now pointing to the last element in the list; it has a null next_exten pointer */
3859 lptr->next_exten = newmem;
3862 static pval *get_extension_or_contxt(pval *p)
3864 while( p && p->type != PV_EXTENSION && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
3866 p = p->dad;
3869 return p;
3872 static pval *get_contxt(pval *p)
3874 while( p && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
3876 p = p->dad;
3879 return p;
3882 static void fix_gotos_in_extensions(struct ael_extension *exten)
3884 struct ael_extension *e;
3885 for(e=exten;e;e=e->next_exten) {
3887 struct ael_priority *p;
3888 for(p=e->plist;p;p=p->next) {
3890 if( p->origin && p->origin->type == PV_GOTO && p->origin->u3.goto_target_in_case ) {
3892 /* fix the extension of the goto target to the actual extension in the post-compiled dialplan */
3894 pval *target = p->origin->u2.goto_target;
3895 struct ael_extension *z = target->u3.compiled_label;
3896 pval *pv2 = p->origin;
3897 char buf1[500];
3898 char *apparg_save = p->appargs;
3900 p->appargs = 0;
3901 if (!pv2->u1.list->next) /* just one -- it won't hurt to repeat the extension */ {
3902 snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->u1.str);
3903 p->appargs = strdup(buf1);
3905 } else if (pv2->u1.list->next && !pv2->u1.list->next->next) /* two */ {
3906 snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->next->u1.str);
3907 p->appargs = strdup(buf1);
3908 } else if (pv2->u1.list->next && pv2->u1.list->next->next) {
3909 snprintf(buf1,sizeof(buf1),"%s|%s|%s", pv2->u1.list->u1.str,
3910 z->name,
3911 pv2->u1.list->next->next->u1.str);
3912 p->appargs = strdup(buf1);
3914 else
3915 printf("WHAT? The goto doesn't fall into one of three cases for GOTO????\n");
3917 if( apparg_save ) {
3918 free(apparg_save);
3926 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root)
3928 pval *p,*p2;
3929 struct ast_context *context;
3930 char buf[2000];
3931 struct ael_extension *exten;
3932 struct ael_extension *exten_list = 0;
3934 for (p=root; p; p=p->next ) { /* do the globals first, so they'll be there
3935 when we try to eval them */
3936 switch (p->type) {
3937 case PV_GLOBALS:
3938 /* just VARDEC elements */
3939 for (p2=p->u1.list; p2; p2=p2->next) {
3940 char buf2[2000];
3941 snprintf(buf2,sizeof(buf2),"%s=%s", p2->u1.str, p2->u2.val);
3942 pbx_builtin_setvar(NULL, buf2);
3944 break;
3945 default:
3946 break;
3950 for (p=root; p; p=p->next ) {
3951 pval *lp;
3952 int argc;
3954 switch (p->type) {
3955 case PV_MACRO:
3956 strcpy(buf,"macro-");
3957 strcat(buf,p->u1.str);
3958 context = ast_context_create(local_contexts, buf, registrar);
3960 exten = new_exten();
3961 exten->context = context;
3962 exten->name = strdup("s");
3963 argc = 1;
3964 for (lp=p->u2.arglist; lp; lp=lp->next) {
3965 /* for each arg, set up a "Set" command */
3966 struct ael_priority *np2 = new_prio();
3967 np2->type = AEL_APPCALL;
3968 np2->app = strdup("Set");
3969 snprintf(buf,sizeof(buf),"%s=${ARG%d}", lp->u1.str, argc++);
3970 remove_spaces_before_equals(buf);
3971 np2->appargs = strdup(buf);
3972 linkprio(exten, np2, NULL);
3974 /* add any includes */
3975 for (p2=p->u3.macro_statements; p2; p2=p2->next) {
3976 pval *p3;
3978 switch (p2->type) {
3979 case PV_INCLUDES:
3980 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
3981 if ( p3->u2.arglist ) {
3982 snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s",
3983 p3->u1.str,
3984 p3->u2.arglist->u1.str,
3985 p3->u2.arglist->next->u1.str,
3986 p3->u2.arglist->next->next->u1.str,
3987 p3->u2.arglist->next->next->next->u1.str);
3988 ast_context_add_include2(context, buf, registrar);
3989 } else
3990 ast_context_add_include2(context, p3->u1.str, registrar);
3992 break;
3993 default:
3994 break;
3997 /* CONTAINS APPCALLS, CATCH, just like extensions... */
3998 gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context );
3999 if (exten->return_needed) {
4000 struct ael_priority *np2 = new_prio();
4001 np2->type = AEL_APPCALL;
4002 np2->app = strdup("NoOp");
4003 snprintf(buf,sizeof(buf),"End of Macro %s-%s",p->u1.str, exten->name);
4004 np2->appargs = strdup(buf);
4005 linkprio(exten, np2, NULL);
4006 exten-> return_target = np2;
4009 set_priorities(exten);
4010 attach_exten(&exten_list, exten);
4011 break;
4013 case PV_GLOBALS:
4014 /* already done */
4015 break;
4017 case PV_CONTEXT:
4018 context = ast_context_find_or_create(local_contexts, p->u1.str, registrar);
4020 /* contexts contain: ignorepat, includes, switches, eswitches, extensions, */
4021 for (p2=p->u2.statements; p2; p2=p2->next) {
4022 pval *p3;
4023 char *s3;
4025 switch (p2->type) {
4026 case PV_EXTENSION:
4027 exten = new_exten();
4028 exten->name = strdup(p2->u1.str);
4029 exten->context = context;
4031 if( (s3=strchr(exten->name, '/') ) != 0 )
4033 *s3 = 0;
4034 exten->cidmatch = s3+1;
4037 if ( p2->u3.hints )
4038 exten->hints = strdup(p2->u3.hints);
4039 exten->regexten = p2->u4.regexten;
4040 gen_prios(exten, p->u1.str, p2->u2.statements, 0, context );
4041 if (exten->return_needed) {
4042 struct ael_priority *np2 = new_prio();
4043 np2->type = AEL_APPCALL;
4044 np2->app = strdup("NoOp");
4045 snprintf(buf,sizeof(buf),"End of Extension %s", exten->name);
4046 np2->appargs = strdup(buf);
4047 linkprio(exten, np2, NULL);
4048 exten-> return_target = np2;
4050 /* is the last priority in the extension a label? Then add a trailing no-op */
4051 if ( exten->plist_last && exten->plist_last->type == AEL_LABEL ) {
4052 struct ael_priority *np2 = new_prio();
4053 np2->type = AEL_APPCALL;
4054 np2->app = strdup("NoOp");
4055 snprintf(buf,sizeof(buf),"A NoOp to follow a trailing label %s", exten->plist_last->origin->u1.str);
4056 np2->appargs = strdup(buf);
4057 linkprio(exten, np2, NULL);
4060 set_priorities(exten);
4061 attach_exten(&exten_list, exten);
4062 break;
4064 case PV_IGNOREPAT:
4065 ast_context_add_ignorepat2(context, p2->u1.str, registrar);
4066 break;
4068 case PV_INCLUDES:
4069 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4070 if ( p3->u2.arglist ) {
4071 snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s",
4072 p3->u1.str,
4073 p3->u2.arglist->u1.str,
4074 p3->u2.arglist->next->u1.str,
4075 p3->u2.arglist->next->next->u1.str,
4076 p3->u2.arglist->next->next->next->u1.str);
4077 ast_context_add_include2(context, buf, registrar);
4078 } else
4079 ast_context_add_include2(context, p3->u1.str, registrar);
4081 break;
4083 case PV_SWITCHES:
4084 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4085 char *c = strchr(p3->u1.str, '/');
4086 if (c) {
4087 *c = '\0';
4088 c++;
4089 } else
4090 c = "";
4092 ast_context_add_switch2(context, p3->u1.str, c, 0, registrar);
4094 break;
4096 case PV_ESWITCHES:
4097 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4098 char *c = strchr(p3->u1.str, '/');
4099 if (c) {
4100 *c = '\0';
4101 c++;
4102 } else
4103 c = "";
4105 ast_context_add_switch2(context, p3->u1.str, c, 1, registrar);
4107 break;
4108 default:
4109 break;
4113 break;
4115 default:
4116 /* huh? what? */
4117 break;
4121 /* moved these from being done after a macro or extension were processed,
4122 to after all processing is done, for the sake of fixing gotos to labels inside cases... */
4123 /* I guess this would be considered 2nd pass of compiler now... */
4124 fix_gotos_in_extensions(exten_list); /* find and fix extension ref in gotos to labels that are in case statements */
4125 add_extensions(exten_list); /* actually makes calls to create priorities in ast_contexts -- feeds dialplan to asterisk */
4126 destroy_extensions(exten_list); /* all that remains is an empty husk, discard of it as is proper */
4131 static int aeldebug = 0;
4133 /* interface stuff */
4135 /* if all the below are static, who cares if they are present? */
4137 static int pbx_load_module(void)
4139 int errs=0, sem_err=0, sem_warn=0, sem_note=0;
4140 char *rfilename;
4141 struct ast_context *local_contexts=NULL, *con;
4142 struct pval *parse_tree;
4144 ast_log(LOG_NOTICE, "Starting AEL load process.\n");
4145 if (config[0] == '/')
4146 rfilename = (char *)config;
4147 else {
4148 rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
4149 sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
4151 ast_log(LOG_NOTICE, "AEL load process: calculated config file name '%s'.\n", rfilename);
4153 if (access(rfilename,R_OK) != 0) {
4154 ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
4155 return AST_MODULE_LOAD_DECLINE;
4158 parse_tree = ael2_parse(rfilename, &errs);
4159 ast_log(LOG_NOTICE, "AEL load process: parsed config file name '%s'.\n", rfilename);
4160 ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
4161 if (errs == 0 && sem_err == 0) {
4162 ast_log(LOG_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename);
4163 ast_compile_ael2(&local_contexts, parse_tree);
4164 ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename);
4166 ast_merge_contexts_and_delete(&local_contexts, registrar);
4167 ast_log(LOG_NOTICE, "AEL load process: merged config file name '%s'.\n", rfilename);
4168 for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
4169 ast_context_verify_includes(con);
4170 ast_log(LOG_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename);
4171 } else {
4172 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);
4173 destroy_pval(parse_tree); /* free up the memory */
4174 return AST_MODULE_LOAD_DECLINE;
4176 destroy_pval(parse_tree); /* free up the memory */
4178 return AST_MODULE_LOAD_SUCCESS;
4181 /* CLI interface */
4182 static int ael2_debug_read(int fd, int argc, char *argv[])
4184 aeldebug |= DEBUG_READ;
4185 return 0;
4188 static int ael2_debug_tokens(int fd, int argc, char *argv[])
4190 aeldebug |= DEBUG_TOKENS;
4191 return 0;
4194 static int ael2_debug_macros(int fd, int argc, char *argv[])
4196 aeldebug |= DEBUG_MACROS;
4197 return 0;
4200 static int ael2_debug_contexts(int fd, int argc, char *argv[])
4202 aeldebug |= DEBUG_CONTEXTS;
4203 return 0;
4206 static int ael2_no_debug(int fd, int argc, char *argv[])
4208 aeldebug = 0;
4209 return 0;
4212 static int ael2_reload(int fd, int argc, char *argv[])
4214 return (pbx_load_module());
4217 static struct ast_cli_entry cli_ael_no_debug = {
4218 { "ael", "no", "debug", NULL },
4219 ael2_no_debug, NULL,
4220 NULL };
4222 static struct ast_cli_entry cli_ael[] = {
4223 { { "ael", "reload", NULL },
4224 ael2_reload, "Reload AEL configuration" },
4226 { { "ael", "debug", "read", NULL },
4227 ael2_debug_read, "Enable AEL read debug (does nothing)" },
4229 { { "ael", "debug", "tokens", NULL },
4230 ael2_debug_tokens, "Enable AEL tokens debug (does nothing)" },
4232 { { "ael", "debug", "macros", NULL },
4233 ael2_debug_macros, "Enable AEL macros debug (does nothing)" },
4235 { { "ael", "debug", "contexts", NULL },
4236 ael2_debug_contexts, "Enable AEL contexts debug (does nothing)" },
4238 { { "ael", "nodebug", NULL },
4239 ael2_no_debug, "Disable AEL debug messages",
4240 NULL, NULL, &cli_ael_no_debug },
4243 static int unload_module(void)
4245 ast_context_destroy(NULL, registrar);
4246 ast_cli_unregister_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
4247 return 0;
4250 static int load_module(void)
4252 ast_cli_register_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
4253 return (pbx_load_module());
4256 static int reload(void)
4258 return pbx_load_module();
4261 #ifdef STANDALONE_AEL
4262 #define AST_MODULE "ael"
4263 int ael_external_load_module(void);
4264 int ael_external_load_module(void)
4266 pbx_load_module();
4267 return 1;
4269 #endif
4271 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
4272 .load = load_module,
4273 .unload = unload_module,
4274 .reload = reload,
4278 /* DESTROY the PVAL tree ============================================================================ */
4282 void destroy_pval_item(pval *item)
4284 if (item == NULL) {
4285 ast_log(LOG_WARNING, "null item\n");
4286 return;
4289 if (item->filename)
4290 free(item->filename);
4292 switch (item->type) {
4293 case PV_WORD:
4294 /* fields: item->u1.str == string associated with this (word). */
4295 if (item->u1.str )
4296 free(item->u1.str);
4297 if ( item->u2.arglist )
4298 destroy_pval(item->u2.arglist);
4299 break;
4301 case PV_MACRO:
4302 /* fields: item->u1.str == name of macro
4303 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
4304 item->u2.arglist->u1.str == argument
4305 item->u2.arglist->next == next arg
4307 item->u3.macro_statements == pval list of statements in macro body.
4309 destroy_pval(item->u2.arglist);
4310 if (item->u1.str )
4311 free(item->u1.str);
4312 destroy_pval(item->u3.macro_statements);
4313 break;
4315 case PV_CONTEXT:
4316 /* fields: item->u1.str == name of context
4317 item->u2.statements == pval list of statements in context body
4318 item->u3.abstract == int 1 if an abstract keyword were present
4320 if (item->u1.str)
4321 free(item->u1.str);
4322 destroy_pval(item->u2.statements);
4323 break;
4325 case PV_MACRO_CALL:
4326 /* fields: item->u1.str == name of macro to call
4327 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
4328 item->u2.arglist->u1.str == argument
4329 item->u2.arglist->next == next arg
4331 if (item->u1.str)
4332 free(item->u1.str);
4333 destroy_pval(item->u2.arglist);
4334 break;
4336 case PV_APPLICATION_CALL:
4337 /* fields: item->u1.str == name of application to call
4338 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
4339 item->u2.arglist->u1.str == argument
4340 item->u2.arglist->next == next arg
4342 if (item->u1.str)
4343 free(item->u1.str);
4344 destroy_pval(item->u2.arglist);
4345 break;
4347 case PV_CASE:
4348 /* fields: item->u1.str == value of case
4349 item->u2.statements == pval list of statements under the case
4351 if (item->u1.str)
4352 free(item->u1.str);
4353 destroy_pval(item->u2.statements);
4354 break;
4356 case PV_PATTERN:
4357 /* fields: item->u1.str == value of case
4358 item->u2.statements == pval list of statements under the case
4360 if (item->u1.str)
4361 free(item->u1.str);
4362 destroy_pval(item->u2.statements);
4363 break;
4365 case PV_DEFAULT:
4366 /* fields:
4367 item->u2.statements == pval list of statements under the case
4369 destroy_pval(item->u2.statements);
4370 break;
4372 case PV_CATCH:
4373 /* fields: item->u1.str == name of extension to catch
4374 item->u2.statements == pval list of statements in context body
4376 if (item->u1.str)
4377 free(item->u1.str);
4378 destroy_pval(item->u2.statements);
4379 break;
4381 case PV_SWITCHES:
4382 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4384 destroy_pval(item->u1.list);
4385 break;
4387 case PV_ESWITCHES:
4388 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4390 destroy_pval(item->u1.list);
4391 break;
4393 case PV_INCLUDES:
4394 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4395 item->u2.arglist == pval list of 4 PV_WORD elements for time values
4397 destroy_pval(item->u1.list);
4398 break;
4400 case PV_STATEMENTBLOCK:
4401 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
4403 destroy_pval(item->u1.list);
4404 break;
4406 case PV_VARDEC:
4407 /* fields: item->u1.str == variable name
4408 item->u2.val == variable value to assign
4410 if (item->u1.str)
4411 free(item->u1.str);
4412 if (item->u2.val)
4413 free(item->u2.val);
4414 break;
4416 case PV_GOTO:
4417 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
4418 item->u1.list->u1.str == where the data on a PV_WORD will always be.
4421 destroy_pval(item->u1.list);
4422 break;
4424 case PV_LABEL:
4425 /* fields: item->u1.str == label name
4427 if (item->u1.str)
4428 free(item->u1.str);
4429 break;
4431 case PV_FOR:
4432 /* fields: item->u1.for_init == a string containing the initalizer
4433 item->u2.for_test == a string containing the loop test
4434 item->u3.for_inc == a string containing the loop increment
4436 item->u4.for_statements == a pval list of statements in the for ()
4438 if (item->u1.for_init)
4439 free(item->u1.for_init);
4440 if (item->u2.for_test)
4441 free(item->u2.for_test);
4442 if (item->u3.for_inc)
4443 free(item->u3.for_inc);
4444 destroy_pval(item->u4.for_statements);
4445 break;
4447 case PV_WHILE:
4448 /* fields: item->u1.str == the while conditional, as supplied by user
4450 item->u2.statements == a pval list of statements in the while ()
4452 if (item->u1.str)
4453 free(item->u1.str);
4454 destroy_pval(item->u2.statements);
4455 break;
4457 case PV_BREAK:
4458 /* fields: none
4460 break;
4462 case PV_RETURN:
4463 /* fields: none
4465 break;
4467 case PV_CONTINUE:
4468 /* fields: none
4470 break;
4472 case PV_IFTIME:
4473 /* fields: item->u1.list == the 4 time values, in PV_WORD structs, linked list
4475 item->u2.statements == a pval list of statements in the if ()
4476 item->u3.else_statements == a pval list of statements in the else
4477 (could be zero)
4479 destroy_pval(item->u1.list);
4480 destroy_pval(item->u2.statements);
4481 if (item->u3.else_statements) {
4482 destroy_pval(item->u3.else_statements);
4484 break;
4486 case PV_RANDOM:
4487 /* fields: item->u1.str == the random percentage, as supplied by user
4489 item->u2.statements == a pval list of statements in the true part ()
4490 item->u3.else_statements == a pval list of statements in the else
4491 (could be zero)
4492 fall thru to If */
4493 case PV_IF:
4494 /* fields: item->u1.str == the if conditional, as supplied by user
4496 item->u2.statements == a pval list of statements in the if ()
4497 item->u3.else_statements == a pval list of statements in the else
4498 (could be zero)
4500 if (item->u1.str)
4501 free(item->u1.str);
4502 destroy_pval(item->u2.statements);
4503 if (item->u3.else_statements) {
4504 destroy_pval(item->u3.else_statements);
4506 break;
4508 case PV_SWITCH:
4509 /* fields: item->u1.str == the switch expression
4511 item->u2.statements == a pval list of statements in the switch,
4512 (will be case statements, most likely!)
4514 if (item->u1.str)
4515 free(item->u1.str);
4516 destroy_pval(item->u2.statements);
4517 break;
4519 case PV_EXTENSION:
4520 /* fields: item->u1.str == the extension name, label, whatever it's called
4522 item->u2.statements == a pval list of statements in the extension
4523 item->u3.hints == a char * hint argument
4524 item->u4.regexten == an int boolean. non-zero says that regexten was specified
4526 if (item->u1.str)
4527 free(item->u1.str);
4528 if (item->u3.hints)
4529 free(item->u3.hints);
4530 destroy_pval(item->u2.statements);
4531 break;
4533 case PV_IGNOREPAT:
4534 /* fields: item->u1.str == the ignorepat data
4536 if (item->u1.str)
4537 free(item->u1.str);
4538 break;
4540 case PV_GLOBALS:
4541 /* fields: item->u1.statements == pval list of statements, usually vardecs
4543 destroy_pval(item->u1.statements);
4544 break;
4546 free(item);
4549 void destroy_pval(pval *item)
4551 pval *i,*nxt;
4553 for (i=item; i; i=nxt) {
4554 nxt = i->next;
4556 destroy_pval_item(i);
4560 #ifdef AAL_ARGCHECK
4561 static char *ael_funclist[] =
4563 "AGENT",
4564 "ARRAY",
4565 "BASE64_DECODE",
4566 "BASE64_ENCODE",
4567 "CALLERID",
4568 "CDR",
4569 "CHANNEL",
4570 "CHECKSIPDOMAIN",
4571 "CHECK_MD5",
4572 "CURL",
4573 "CUT",
4574 "DB",
4575 "DB_EXISTS",
4576 "DUNDILOOKUP",
4577 "ENUMLOOKUP",
4578 "ENV",
4579 "EVAL",
4580 "EXISTS",
4581 "FIELDQTY",
4582 "FILTER",
4583 "GROUP",
4584 "GROUP_COUNT",
4585 "GROUP_LIST",
4586 "GROUP_MATCH_COUNT",
4587 "IAXPEER",
4588 "IF",
4589 "IFTIME",
4590 "ISNULL",
4591 "KEYPADHASH",
4592 "LANGUAGE",
4593 "LEN",
4594 "MATH",
4595 "MD5",
4596 "MUSICCLASS",
4597 "QUEUEAGENTCOUNT",
4598 "QUEUE_MEMBER_COUNT",
4599 "QUEUE_MEMBER_LIST",
4600 "QUOTE",
4601 "RAND",
4602 "REGEX",
4603 "SET",
4604 "SHA1",
4605 "SIPCHANINFO",
4606 "SIPPEER",
4607 "SIP_HEADER",
4608 "SORT",
4609 "STAT",
4610 "STRFTIME",
4611 "STRPTIME",
4612 "TIMEOUT",
4613 "TXTCIDNAME",
4614 "URIDECODE",
4615 "URIENCODE",
4616 "VMCOUNT"
4620 int ael_is_funcname(char *name)
4622 int s,t;
4623 t = sizeof(ael_funclist)/sizeof(char*);
4624 s = 0;
4625 while ((s < t) && strcasecmp(name, ael_funclist[s]))
4626 s++;
4627 if ( s < t )
4628 return 1;
4629 else
4630 return 0;
4632 #endif