appending one list to another should leave the first list empty, and not require...
[asterisk-bristuff.git] / pbx / pbx_ael.c
blobf4f85eca8e2b73db039d793a91bddaac9afc9730
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);
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 includes->filename, includes->startline, includes->endline, incl_context);
840 warns++;
846 static void check_timerange(pval *p)
848 char *times;
849 char *e;
850 int s1, s2;
851 int e1, e2;
853 times = ast_strdupa(p->u1.str);
855 /* Star is all times */
856 if (ast_strlen_zero(times) || !strcmp(times, "*")) {
857 return;
859 /* Otherwise expect a range */
860 e = strchr(times, '-');
861 if (!e) {
862 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",
863 p->filename, p->startline, p->endline, times);
864 warns++;
865 return;
867 *e = '\0';
868 e++;
869 while (*e && !isdigit(*e))
870 e++;
871 if (!*e) {
872 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
873 p->filename, p->startline, p->endline, p->u1.str);
874 warns++;
876 if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
877 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
878 p->filename, p->startline, p->endline, times);
879 warns++;
881 if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
882 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
883 p->filename, p->startline, p->endline, times);
884 warns++;
887 s1 = s1 * 30 + s2/2;
888 if ((s1 < 0) || (s1 >= 24*30)) {
889 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
890 p->filename, p->startline, p->endline, times);
891 warns++;
893 e1 = e1 * 30 + e2/2;
894 if ((e1 < 0) || (e1 >= 24*30)) {
895 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
896 p->filename, p->startline, p->endline, e);
897 warns++;
899 return;
902 static char *days[] =
904 "sun",
905 "mon",
906 "tue",
907 "wed",
908 "thu",
909 "fri",
910 "sat",
913 /*! \brief get_dow: Get day of week */
914 static void check_dow(pval *DOW)
916 char *dow;
917 char *c;
918 /* The following line is coincidence, really! */
919 int s, e;
921 dow = ast_strdupa(DOW->u1.str);
923 /* Check for all days */
924 if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
925 return;
926 /* Get start and ending days */
927 c = strchr(dow, '-');
928 if (c) {
929 *c = '\0';
930 c++;
931 } else
932 c = NULL;
933 /* Find the start */
934 s = 0;
935 while ((s < 7) && strcasecmp(dow, days[s])) s++;
936 if (s >= 7) {
937 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",
938 DOW->filename, DOW->startline, DOW->endline, dow);
939 warns++;
941 if (c) {
942 e = 0;
943 while ((e < 7) && strcasecmp(c, days[e])) e++;
944 if (e >= 7) {
945 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",
946 DOW->filename, DOW->startline, DOW->endline, c);
947 warns++;
949 } else
950 e = s;
953 static void check_day(pval *DAY)
955 char *day;
956 char *c;
957 /* The following line is coincidence, really! */
958 int s, e;
960 day = ast_strdupa(DAY->u1.str);
962 /* Check for all days */
963 if (ast_strlen_zero(day) || !strcmp(day, "*")) {
964 return;
966 /* Get start and ending days */
967 c = strchr(day, '-');
968 if (c) {
969 *c = '\0';
970 c++;
972 /* Find the start */
973 if (sscanf(day, "%d", &s) != 1) {
974 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
975 DAY->filename, DAY->startline, DAY->endline, day);
976 warns++;
978 else if ((s < 1) || (s > 31)) {
979 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",
980 DAY->filename, DAY->startline, DAY->endline, day);
981 warns++;
983 s--;
984 if (c) {
985 if (sscanf(c, "%d", &e) != 1) {
986 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
987 DAY->filename, DAY->startline, DAY->endline, c);
988 warns++;
990 else if ((e < 1) || (e > 31)) {
991 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",
992 DAY->filename, DAY->startline, DAY->endline, day);
993 warns++;
995 e--;
996 } else
997 e = s;
1000 static char *months[] =
1002 "jan",
1003 "feb",
1004 "mar",
1005 "apr",
1006 "may",
1007 "jun",
1008 "jul",
1009 "aug",
1010 "sep",
1011 "oct",
1012 "nov",
1013 "dec",
1016 static void check_month(pval *MON)
1018 char *mon;
1019 char *c;
1020 /* The following line is coincidence, really! */
1021 int s, e;
1023 mon = ast_strdupa(MON->u1.str);
1025 /* Check for all days */
1026 if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
1027 return ;
1028 /* Get start and ending days */
1029 c = strchr(mon, '-');
1030 if (c) {
1031 *c = '\0';
1032 c++;
1034 /* Find the start */
1035 s = 0;
1036 while ((s < 12) && strcasecmp(mon, months[s])) s++;
1037 if (s >= 12) {
1038 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1039 MON->filename, MON->startline, MON->endline, mon);
1040 warns++;
1042 if (c) {
1043 e = 0;
1044 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1045 if (e >= 12) {
1046 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1047 MON->filename, MON->startline, MON->endline, c);
1048 warns++;
1050 } else
1051 e = s;
1054 static int check_break(pval *item)
1056 pval *p = item;
1058 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1059 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1060 no sense */
1061 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
1062 || p->type == PV_WHILE || p->type == PV_FOR ) {
1063 return 1;
1065 p = p->dad;
1067 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1068 item->filename, item->startline, item->endline);
1069 errs++;
1071 return 0;
1074 static int check_continue(pval *item)
1076 pval *p = item;
1078 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1079 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1080 no sense */
1081 if( p->type == PV_WHILE || p->type == PV_FOR ) {
1082 return 1;
1084 p = p->dad;
1086 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1087 item->filename, item->startline, item->endline);
1088 errs++;
1090 return 0;
1094 /* general purpose goto finder */
1096 static void check_label(pval *item)
1098 /* basically, ensure that a label is not repeated in a context. Period.
1099 The method: well, for each label, find the first label in the context
1100 with the same name. If it's not the current label, then throw an error. */
1101 struct pval *curr;
1102 struct pval *x;
1104 /* printf("==== check_label: ====\n"); */
1105 if( !current_extension )
1106 curr = current_context;
1107 else
1108 curr = current_extension;
1110 x = find_first_label_in_current_context((char *)item->u1.str, curr);
1111 /* 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); */
1112 if( x && x != item )
1114 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1115 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1116 errs++;
1118 /* printf("<<<<< check_label: ====\n"); */
1121 static pval *get_goto_target(pval *item)
1123 /* just one item-- the label should be in the current extension */
1124 pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1125 pval *curr_cont;
1127 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1128 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1129 return x;
1132 curr_cont = get_contxt(item);
1134 /* TWO items */
1135 if (item->u1.list->next && !item->u1.list->next->next) {
1136 if (!strstr((item->u1.list)->u1.str,"${")
1137 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1138 struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1139 return x;
1143 /* All 3 items! */
1144 if (item->u1.list->next && item->u1.list->next->next) {
1145 /* all three */
1146 pval *first = item->u1.list;
1147 pval *second = item->u1.list->next;
1148 pval *third = item->u1.list->next->next;
1150 if (!strstr((item->u1.list)->u1.str,"${")
1151 && !strstr(item->u1.list->next->u1.str,"${")
1152 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1153 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1154 if (!x) {
1156 struct pval *p3;
1157 struct pval *that_context = find_context(item->u1.list->u1.str);
1159 /* the target of the goto could be in an included context!! Fancy that!! */
1160 /* look for includes in the current context */
1161 if (that_context) {
1162 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1163 if (p3->type == PV_INCLUDES) {
1164 struct pval *p4;
1165 for (p4=p3->u1.list; p4; p4=p4->next) {
1166 /* for each context pointed to, find it, then find a context/label that matches the
1167 target here! */
1168 char *incl_context = p4->u1.str;
1169 /* find a matching context name */
1170 struct pval *that_other_context = find_context(incl_context);
1171 if (that_other_context) {
1172 struct pval *x3;
1173 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1174 if (x3) {
1175 return x3;
1183 return x;
1186 return 0;
1189 static void check_goto(pval *item)
1191 /* check for the target of the goto-- does it exist? */
1192 if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1193 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
1194 item->filename, item->startline, item->endline);
1195 errs++;
1198 /* just one item-- the label should be in the current extension */
1200 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1201 struct pval *z = get_extension_or_contxt(item);
1202 struct pval *x = 0;
1203 if (z)
1204 x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1205 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1206 (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1207 if (!x) {
1208 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
1209 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1210 errs++;
1212 else
1213 return;
1216 /* TWO items */
1217 if (item->u1.list->next && !item->u1.list->next->next) {
1218 /* two items */
1219 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1220 (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1221 if (!strstr((item->u1.list)->u1.str,"${")
1222 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1223 struct pval *z = get_contxt(item);
1224 struct pval *x = 0;
1226 if (z)
1227 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1229 if (!x) {
1230 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",
1231 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1232 errs++;
1234 else
1235 return;
1239 /* All 3 items! */
1240 if (item->u1.list->next && item->u1.list->next->next) {
1241 /* all three */
1242 pval *first = item->u1.list;
1243 pval *second = item->u1.list->next;
1244 pval *third = item->u1.list->next->next;
1246 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1247 (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1248 if (!strstr((item->u1.list)->u1.str,"${")
1249 && !strstr(item->u1.list->next->u1.str,"${")
1250 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1251 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1252 if (!x) {
1253 struct pval *p3;
1254 struct pval *found = 0;
1255 struct pval *that_context = find_context(item->u1.list->u1.str);
1257 /* the target of the goto could be in an included context!! Fancy that!! */
1258 /* look for includes in the current context */
1259 if (that_context) {
1260 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1261 if (p3->type == PV_INCLUDES) {
1262 struct pval *p4;
1263 for (p4=p3->u1.list; p4; p4=p4->next) {
1264 /* for each context pointed to, find it, then find a context/label that matches the
1265 target here! */
1266 char *incl_context = p4->u1.str;
1267 /* find a matching context name */
1268 struct pval *that_other_context = find_context(incl_context);
1269 if (that_other_context) {
1270 struct pval *x3;
1271 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1272 if (x3) {
1273 found = x3;
1274 break;
1280 if (!found) {
1281 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
1282 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1283 errs++;
1285 } else {
1286 /* here is where code would go to check for target existence in extensions.conf files */
1287 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: no context %s could be found that matches the goto target!\n",
1288 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1289 warns++; /* this is just a warning, because this context could be in extensions.conf or somewhere */
1297 static void find_pval_goto_item(pval *item, int lev)
1299 struct pval *p4;
1300 if (lev>100) {
1301 ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
1302 return;
1305 switch ( item->type ) {
1306 case PV_MACRO:
1307 /* fields: item->u1.str == name of macro
1308 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1309 item->u2.arglist->u1.str == argument
1310 item->u2.arglist->next == next arg
1312 item->u3.macro_statements == pval list of statements in macro body.
1315 /* printf("Descending into matching macro %s\n", match_context); */
1316 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1318 break;
1320 case PV_CONTEXT:
1321 /* fields: item->u1.str == name of context
1322 item->u2.statements == pval list of statements in context body
1323 item->u3.abstract == int 1 if an abstract keyword were present
1325 break;
1327 case PV_CASE:
1328 /* fields: item->u1.str == value of case
1329 item->u2.statements == pval list of statements under the case
1331 find_pval_gotos(item->u2.statements,lev+1);
1332 break;
1334 case PV_PATTERN:
1335 /* fields: item->u1.str == value of case
1336 item->u2.statements == pval list of statements under the case
1338 find_pval_gotos(item->u2.statements,lev+1);
1339 break;
1341 case PV_DEFAULT:
1342 /* fields:
1343 item->u2.statements == pval list of statements under the case
1345 find_pval_gotos(item->u2.statements,lev+1);
1346 break;
1348 case PV_CATCH:
1349 /* fields: item->u1.str == name of extension to catch
1350 item->u2.statements == pval list of statements in context body
1352 find_pval_gotos(item->u2.statements,lev+1);
1353 break;
1355 case PV_STATEMENTBLOCK:
1356 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1358 find_pval_gotos(item->u1.list,lev+1);
1359 break;
1361 case PV_GOTO:
1362 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1363 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1365 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1366 break;
1368 case PV_INCLUDES:
1369 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1371 for (p4=item->u1.list; p4; p4=p4->next) {
1372 /* for each context pointed to, find it, then find a context/label that matches the
1373 target here! */
1374 char *incl_context = p4->u1.str;
1375 /* find a matching context name */
1376 struct pval *that_context = find_context(incl_context);
1377 if (that_context) {
1378 find_pval_gotos(that_context,lev+1); /* keep working up the includes */
1381 break;
1383 case PV_FOR:
1384 /* fields: item->u1.for_init == a string containing the initalizer
1385 item->u2.for_test == a string containing the loop test
1386 item->u3.for_inc == a string containing the loop increment
1388 item->u4.for_statements == a pval list of statements in the for ()
1390 find_pval_gotos(item->u4.for_statements,lev+1);
1391 break;
1393 case PV_WHILE:
1394 /* fields: item->u1.str == the while conditional, as supplied by user
1396 item->u2.statements == a pval list of statements in the while ()
1398 find_pval_gotos(item->u2.statements,lev+1);
1399 break;
1401 case PV_RANDOM:
1402 /* fields: item->u1.str == the random number expression, as supplied by user
1404 item->u2.statements == a pval list of statements in the if ()
1405 item->u3.else_statements == a pval list of statements in the else
1406 (could be zero)
1407 fall thru to PV_IF */
1409 case PV_IFTIME:
1410 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1412 item->u2.statements == a pval list of statements in the if ()
1413 item->u3.else_statements == a pval list of statements in the else
1414 (could be zero)
1415 fall thru to PV_IF*/
1416 case PV_IF:
1417 /* fields: item->u1.str == the if conditional, as supplied by user
1419 item->u2.statements == a pval list of statements in the if ()
1420 item->u3.else_statements == a pval list of statements in the else
1421 (could be zero)
1423 find_pval_gotos(item->u2.statements,lev+1);
1425 if (item->u3.else_statements) {
1426 find_pval_gotos(item->u3.else_statements,lev+1);
1428 break;
1430 case PV_SWITCH:
1431 /* fields: item->u1.str == the switch expression
1433 item->u2.statements == a pval list of statements in the switch,
1434 (will be case statements, most likely!)
1436 find_pval_gotos(item->u3.else_statements,lev+1);
1437 break;
1439 case PV_EXTENSION:
1440 /* fields: item->u1.str == the extension name, label, whatever it's called
1442 item->u2.statements == a pval list of statements in the extension
1443 item->u3.hints == a char * hint argument
1444 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1447 find_pval_gotos(item->u2.statements,lev+1);
1448 break;
1450 default:
1451 break;
1455 static void find_pval_gotos(pval *item,int lev)
1457 pval *i;
1459 for (i=item; i; i=i->next) {
1461 find_pval_goto_item(i, lev);
1467 /* general purpose label finder */
1468 static struct pval *match_pval_item(pval *item)
1470 pval *x;
1472 switch ( item->type ) {
1473 case PV_MACRO:
1474 /* fields: item->u1.str == name of macro
1475 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1476 item->u2.arglist->u1.str == argument
1477 item->u2.arglist->next == next arg
1479 item->u3.macro_statements == pval list of statements in macro body.
1481 /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1482 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1484 /* printf("MACRO: match context is: %s\n", match_context); */
1486 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 */ {
1487 /* printf("Returning on matching macro %s\n", match_context); */
1488 return item;
1492 if (!return_on_context_match) {
1493 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1494 if ((x=match_pval(item->u3.macro_statements))) {
1495 /* printf("Responded with pval match %x\n", x); */
1496 return x;
1499 } else {
1500 /* printf("Skipping context/macro %s\n", item->u1.str); */
1503 break;
1505 case PV_CONTEXT:
1506 /* fields: item->u1.str == name of context
1507 item->u2.statements == pval list of statements in context body
1508 item->u3.abstract == int 1 if an abstract keyword were present
1510 /* printf(" matching in CONTEXT\n"); */
1511 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1512 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1513 /* printf("Returning on matching context %s\n", match_context); */
1514 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1515 return item;
1518 if (!return_on_context_match ) {
1519 /* printf("Descending into matching context %s\n", match_context); */
1520 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1521 /* printf("CONTEXT: Responded with pval match %x\n", x); */
1522 return x;
1525 } else {
1526 /* printf("Skipping context/macro %s\n", item->u1.str); */
1528 break;
1530 case PV_CASE:
1531 /* fields: item->u1.str == value of case
1532 item->u2.statements == pval list of statements under the case
1534 /* printf(" matching in CASE\n"); */
1535 if ((x=match_pval(item->u2.statements))) {
1536 /* printf("CASE: Responded with pval match %x\n", x); */
1537 return x;
1539 break;
1541 case PV_PATTERN:
1542 /* fields: item->u1.str == value of case
1543 item->u2.statements == pval list of statements under the case
1545 /* printf(" matching in PATTERN\n"); */
1546 if ((x=match_pval(item->u2.statements))) {
1547 /* printf("PATTERN: Responded with pval match %x\n", x); */
1548 return x;
1550 break;
1552 case PV_DEFAULT:
1553 /* fields:
1554 item->u2.statements == pval list of statements under the case
1556 /* printf(" matching in DEFAULT\n"); */
1557 if ((x=match_pval(item->u2.statements))) {
1558 /* printf("DEFAULT: Responded with pval match %x\n", x); */
1559 return x;
1561 break;
1563 case PV_CATCH:
1564 /* fields: item->u1.str == name of extension to catch
1565 item->u2.statements == pval list of statements in context body
1567 /* printf(" matching in CATCH\n"); */
1568 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1569 /* printf("Descending into matching catch %s => %s\n", match_exten, item->u1.str); */
1570 if (strcmp(match_label,"1") == 0) {
1571 if (item->u2.statements) {
1572 struct pval *p5 = item->u2.statements;
1573 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1574 p5 = p5->next;
1575 if (p5)
1576 return p5;
1577 else
1578 return 0;
1580 else
1581 return 0;
1584 if ((x=match_pval(item->u2.statements))) {
1585 /* printf("CATCH: Responded with pval match %x\n", (unsigned int)x); */
1586 return x;
1588 } else {
1589 /* printf("Skipping catch %s\n", item->u1.str); */
1591 break;
1593 case PV_STATEMENTBLOCK:
1594 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1596 /* printf(" matching in STATEMENTBLOCK\n"); */
1597 if ((x=match_pval(item->u1.list))) {
1598 /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1599 return x;
1601 break;
1603 case PV_LABEL:
1604 /* fields: item->u1.str == label name
1606 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1607 item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1609 if (count_labels) {
1610 if (!strcmp(match_label, item->u1.str)) {
1611 label_count++;
1612 last_matched_label = item;
1615 } else {
1616 if (!strcmp(match_label, item->u1.str)) {
1617 /* printf("LABEL: Responded with pval match %x\n", x); */
1618 return item;
1621 break;
1623 case PV_FOR:
1624 /* fields: item->u1.for_init == a string containing the initalizer
1625 item->u2.for_test == a string containing the loop test
1626 item->u3.for_inc == a string containing the loop increment
1628 item->u4.for_statements == a pval list of statements in the for ()
1630 /* printf(" matching in FOR\n"); */
1631 if ((x=match_pval(item->u4.for_statements))) {
1632 /* printf("FOR: Responded with pval match %x\n", x);*/
1633 return x;
1635 break;
1637 case PV_WHILE:
1638 /* fields: item->u1.str == the while conditional, as supplied by user
1640 item->u2.statements == a pval list of statements in the while ()
1642 /* printf(" matching in WHILE\n"); */
1643 if ((x=match_pval(item->u2.statements))) {
1644 /* printf("WHILE: Responded with pval match %x\n", x); */
1645 return x;
1647 break;
1649 case PV_RANDOM:
1650 /* fields: item->u1.str == the random number expression, as supplied by user
1652 item->u2.statements == a pval list of statements in the if ()
1653 item->u3.else_statements == a pval list of statements in the else
1654 (could be zero)
1655 fall thru to PV_IF */
1657 case PV_IFTIME:
1658 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1660 item->u2.statements == a pval list of statements in the if ()
1661 item->u3.else_statements == a pval list of statements in the else
1662 (could be zero)
1663 fall thru to PV_IF*/
1664 case PV_IF:
1665 /* fields: item->u1.str == the if conditional, as supplied by user
1667 item->u2.statements == a pval list of statements in the if ()
1668 item->u3.else_statements == a pval list of statements in the else
1669 (could be zero)
1671 /* printf(" matching in IF/IFTIME/RANDOM\n"); */
1672 if ((x=match_pval(item->u2.statements))) {
1673 return x;
1675 if (item->u3.else_statements) {
1676 if ((x=match_pval(item->u3.else_statements))) {
1677 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1678 return x;
1681 break;
1683 case PV_SWITCH:
1684 /* fields: item->u1.str == the switch expression
1686 item->u2.statements == a pval list of statements in the switch,
1687 (will be case statements, most likely!)
1689 /* printf(" matching in SWITCH\n"); */
1690 if ((x=match_pval(item->u2.statements))) {
1691 /* printf("SWITCH: Responded with pval match %x\n", x); */
1692 return x;
1694 break;
1696 case PV_EXTENSION:
1697 /* fields: item->u1.str == the extension name, label, whatever it's called
1699 item->u2.statements == a pval list of statements in the extension
1700 item->u3.hints == a char * hint argument
1701 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1703 /* printf(" matching in EXTENSION\n"); */
1704 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1705 /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1706 if (strcmp(match_label,"1") == 0) {
1707 if (item->u2.statements) {
1708 struct pval *p5 = item->u2.statements;
1709 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1710 p5 = p5->next;
1711 if (p5)
1712 return p5;
1713 else
1714 return 0;
1716 else
1717 return 0;
1720 if ((x=match_pval(item->u2.statements))) {
1721 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1722 return x;
1724 } else {
1725 /* printf("Skipping exten %s\n", item->u1.str); */
1727 break;
1728 default:
1729 /* printf(" matching in default = %d\n", item->type); */
1730 break;
1732 return 0;
1735 struct pval *match_pval(pval *item)
1737 pval *i;
1739 for (i=item; i; i=i->next) {
1740 pval *x;
1741 /* printf(" -- match pval: item %d\n", i->type); */
1743 if ((x = match_pval_item(i))) {
1744 /* printf("match_pval: returning x=%x\n", (int)x); */
1745 return x; /* cut the search short */
1748 return 0;
1751 #if 0
1752 int count_labels_in_current_context(char *label)
1754 label_count = 0;
1755 count_labels = 1;
1756 return_on_context_match = 0;
1757 match_pval(current_context->u2.statements);
1759 return label_count;
1761 #endif
1763 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1765 /* printf(" --- Got args %s, %s\n", exten, label); */
1766 struct pval *ret;
1767 struct pval *p3;
1768 struct pval *startpt = ((curr_cont->type==PV_MACRO)?curr_cont->u3.macro_statements: curr_cont->u2.statements);
1770 count_labels = 0;
1771 return_on_context_match = 0;
1772 match_context = "*";
1773 match_exten = "*";
1774 match_label = label;
1776 ret = match_pval(curr_cont);
1777 if (ret)
1778 return ret;
1780 /* the target of the goto could be in an included context!! Fancy that!! */
1781 /* look for includes in the current context */
1782 for (p3=startpt; p3; p3=p3->next) {
1783 if (p3->type == PV_INCLUDES) {
1784 struct pval *p4;
1785 for (p4=p3->u1.list; p4; p4=p4->next) {
1786 /* for each context pointed to, find it, then find a context/label that matches the
1787 target here! */
1788 char *incl_context = p4->u1.str;
1789 /* find a matching context name */
1790 struct pval *that_context = find_context(incl_context);
1791 if (that_context) {
1792 struct pval *x3;
1793 x3 = find_first_label_in_current_context(label, that_context);
1794 if (x3) {
1795 return x3;
1801 return 0;
1804 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1806 /* printf(" --- Got args %s, %s\n", exten, label); */
1807 struct pval *ret;
1808 struct pval *p3;
1809 struct pval *startpt;
1811 count_labels = 0;
1812 return_on_context_match = 0;
1813 match_context = "*";
1814 match_exten = exten;
1815 match_label = label;
1816 if (curr_cont->type == PV_MACRO)
1817 startpt = curr_cont->u3.macro_statements;
1818 else
1819 startpt = curr_cont->u2.statements;
1821 ret = match_pval(startpt);
1822 if (ret)
1823 return ret;
1825 /* the target of the goto could be in an included context!! Fancy that!! */
1826 /* look for includes in the current context */
1827 for (p3=startpt; p3; p3=p3->next) {
1828 if (p3->type == PV_INCLUDES) {
1829 struct pval *p4;
1830 for (p4=p3->u1.list; p4; p4=p4->next) {
1831 /* for each context pointed to, find it, then find a context/label that matches the
1832 target here! */
1833 char *incl_context = p4->u1.str;
1834 /* find a matching context name */
1835 struct pval *that_context = find_context(incl_context);
1836 if (that_context) {
1837 struct pval *x3;
1838 x3 = find_label_in_current_context(exten, label, that_context);
1839 if (x3) {
1840 return x3;
1846 return 0;
1849 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1851 /* printf(" --- Got args %s\n", label); */
1852 count_labels = 0;
1853 return_on_context_match = 0;
1854 match_context = "*";
1855 match_exten = "*";
1856 match_label = label;
1857 return match_pval(curr_ext);
1860 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1862 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1863 count_labels = 0;
1864 return_on_context_match = 0;
1866 match_context = context;
1867 match_exten = exten;
1868 match_label = label;
1870 return match_pval(current_db);
1874 struct pval *find_macro(char *name)
1876 return_on_context_match = 1;
1877 count_labels = 0;
1878 match_context = name;
1879 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1880 match_label = "*";
1881 return match_pval(current_db);
1884 struct pval *find_context(char *name)
1886 return_on_context_match = 1;
1887 count_labels = 0;
1888 match_context = name;
1889 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1890 match_label = "*";
1891 return match_pval(current_db);
1894 int is_float(char *arg )
1896 char *s;
1897 for (s=arg; *s; s++) {
1898 if (*s != '.' && (*s < '0' || *s > '9'))
1899 return 0;
1901 return 1;
1903 int is_int(char *arg )
1905 char *s;
1906 for (s=arg; *s; s++) {
1907 if (*s < '0' || *s > '9')
1908 return 0;
1910 return 1;
1912 int is_empty(char *arg)
1914 if (!arg)
1915 return 1;
1916 if (*arg == 0)
1917 return 1;
1918 while (*arg) {
1919 if (*arg != ' ' && *arg != '\t')
1920 return 0;
1921 arg++;
1923 return 1;
1926 #ifdef AAL_ARGCHECK
1927 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1929 struct argchoice *ac;
1930 char *opcop,*q,*p;
1932 switch (should->dtype) {
1933 case ARGD_OPTIONSET:
1934 if ( strstr(is->u1.str,"${") )
1935 return 0; /* no checking anything if there's a var reference in there! */
1937 opcop = ast_strdupa(is->u1.str);
1939 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
1940 if ( *q == '(' ) {
1941 p = q+1;
1942 while (*p && *p != ')' )
1943 *p++ = '+';
1944 q = p+1;
1948 for (ac=app->opts; ac; ac=ac->next) {
1949 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
1950 return 0;
1952 for (ac=app->opts; ac; ac=ac->next) {
1953 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
1954 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
1956 if (p && *p == 'j') {
1957 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
1958 is->filename, is->startline, is->endline, app->name);
1959 errs++;
1962 if (p) {
1963 *p = '+';
1964 if (ac->name[1] == '(') {
1965 if (*(p+1) != '(') {
1966 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",
1967 is->filename, is->startline, is->endline, ac->name[0], app->name);
1968 warns++;
1974 for (q=opcop; *q; q++) {
1975 if ( *q != '+' && *q != '(' && *q != ')') {
1976 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",
1977 is->filename, is->startline, is->endline, *q, app->name);
1978 warns++;
1981 return 1;
1982 break;
1983 default:
1984 return 0;
1989 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
1991 struct argchoice *ac;
1992 char *opcop;
1994 switch (should->dtype) {
1995 case ARGD_STRING:
1996 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
1997 return 0;
1998 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
1999 return 1;
2000 break;
2002 case ARGD_INT:
2003 if (is_int(is->u1.str))
2004 return 1;
2005 else
2006 return 0;
2007 break;
2009 case ARGD_FLOAT:
2010 if (is_float(is->u1.str))
2011 return 1;
2012 else
2013 return 0;
2014 break;
2016 case ARGD_ENUM:
2017 if( !is->u1.str || strlen(is->u1.str) == 0 )
2018 return 1; /* a null arg in the call will match an enum, I guess! */
2019 for (ac=should->choices; ac; ac=ac->next) {
2020 if (strcmp(ac->name,is->u1.str) == 0)
2021 return 1;
2023 return 0;
2024 break;
2026 case ARGD_OPTIONSET:
2027 opcop = ast_strdupa(is->u1.str);
2029 for (ac=app->opts; ac; ac=ac->next) {
2030 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2031 return 1;
2033 for (ac=app->opts; ac; ac=ac->next) {
2034 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2035 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2037 if (p) {
2038 *p = '+';
2039 if (ac->name[1] == '(') {
2040 if (*(p+1) == '(') {
2041 char *q = p+1;
2042 while (*q && *q != ')') {
2043 *q++ = '+';
2045 *q = '+';
2051 return 1;
2052 break;
2053 case ARGD_VARARG:
2054 return 1; /* matches anything */
2055 break;
2057 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2059 #endif
2061 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2063 #ifdef AAL_ARGCHECK
2064 struct argdesc *ad = app->args;
2065 pval *pa;
2066 int z;
2068 for (pa = arglist; pa; pa=pa->next) {
2069 if (!ad) {
2070 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2071 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2072 warns++;
2073 return 1;
2074 } else {
2075 /* find the first entry in the ad list that will match */
2076 do {
2077 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2078 break;
2080 z= option_matches( ad, pa, app);
2081 if (!z) {
2082 if ( !arglist )
2083 arglist=appcall;
2085 if (ad->type == ARGD_REQUIRED) {
2086 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2087 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2088 warns++;
2089 return 1;
2091 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2092 option_matches_j( ad, pa, app);
2094 ad = ad->next;
2095 } while (ad && !z);
2098 /* any app nodes left, that are not optional? */
2099 for ( ; ad; ad=ad->next) {
2100 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2101 if ( !arglist )
2102 arglist=appcall;
2103 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2104 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2105 warns++;
2106 return 1;
2109 return 0;
2110 #else
2111 return 0;
2112 #endif
2115 void check_switch_expr(pval *item, struct argapp *apps)
2117 #ifdef AAL_ARGCHECK
2118 /* get and clean the variable name */
2119 char *buff1, *p;
2120 struct argapp *a,*a2;
2121 struct appsetvar *v,*v2;
2122 struct argchoice *c;
2123 pval *t;
2125 p = item->u1.str;
2126 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2127 p++;
2129 buff1 = ast_strdupa(p);
2131 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2132 buff1[strlen(buff1)-1] = 0;
2133 /* buff1 now contains the variable name */
2134 v = 0;
2135 for (a=apps; a; a=a->next) {
2136 for (v=a->setvars;v;v=v->next) {
2137 if (strcmp(v->name,buff1) == 0) {
2138 break;
2141 if ( v )
2142 break;
2144 if (v && v->vals) {
2145 /* we have a match, to a variable that has a set of determined values */
2146 int def= 0;
2147 int pat = 0;
2148 int f1 = 0;
2150 /* first of all, does this switch have a default case ? */
2151 for (t=item->u2.statements; t; t=t->next) {
2152 if (t->type == PV_DEFAULT) {
2153 def =1;
2154 break;
2156 if (t->type == PV_PATTERN) {
2157 pat++;
2160 if (def || pat) /* nothing to check. All cases accounted for! */
2161 return;
2162 for (c=v->vals; c; c=c->next) {
2163 f1 = 0;
2164 for (t=item->u2.statements; t; t=t->next) {
2165 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2166 if (!strcmp(t->u1.str,c->name)) {
2167 f1 = 1;
2168 break;
2172 if (!f1) {
2173 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2174 item->filename, item->startline, item->endline, item->u1.str, c->name);
2175 warns++;
2178 /* next, is there an app call in the current exten, that would set this var? */
2179 f1 = 0;
2180 t = current_extension->u2.statements;
2181 if ( t && t->type == PV_STATEMENTBLOCK )
2182 t = t->u1.statements;
2183 for (; t && t != item; t=t->next) {
2184 if (t->type == PV_APPLICATION_CALL) {
2185 /* find the application that matches the u1.str */
2186 for (a2=apps; a2; a2=a2->next) {
2187 if (strcasecmp(a2->name, t->u1.str)==0) {
2188 for (v2=a2->setvars; v2; v2=v2->next) {
2189 if (strcmp(v2->name, buff1) == 0) {
2190 /* found an app that sets the var */
2191 f1 = 1;
2192 break;
2196 if (f1)
2197 break;
2200 if (f1)
2201 break;
2204 /* see if it sets the var */
2205 if (!f1) {
2206 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",
2207 item->filename, item->startline, item->endline, item->u1.str);
2208 warns++;
2211 #else
2212 pval *t,*tl=0,*p2;
2213 int def= 0;
2215 /* first of all, does this switch have a default case ? */
2216 for (t=item->u2.statements; t; t=t->next) {
2217 if (t->type == PV_DEFAULT) {
2218 def =1;
2219 break;
2221 tl = t;
2223 if (def) /* nothing to check. All cases accounted for! */
2224 return;
2225 /* if no default, warn and insert a default case at the end */
2226 p2 = tl->next = calloc(1, sizeof(struct pval));
2228 p2->type = PV_DEFAULT;
2229 p2->startline = tl->startline;
2230 p2->endline = tl->endline;
2231 p2->startcol = tl->startcol;
2232 p2->endcol = tl->endcol;
2233 p2->filename = strdup(tl->filename);
2234 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2235 p2->filename, p2->startline, p2->endline);
2236 warns++;
2238 #endif
2241 static void check_context_names(void)
2243 pval *i,*j;
2244 for (i=current_db; i; i=i->next) {
2245 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2246 for (j=i->next; j; j=j->next) {
2247 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2248 if ( !strcmp(i->u1.str, j->u1.str) )
2250 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d!\n",
2251 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
2252 errs++;
2260 static void check_abstract_reference(pval *abstract_context)
2262 pval *i,*j;
2263 /* find some context includes that reference this context */
2266 /* otherwise, print out a warning */
2267 for (i=current_db; i; i=i->next) {
2268 if (i->type == PV_CONTEXT) {
2269 for (j=i->u2. statements; j; j=j->next) {
2270 if ( j->type == PV_INCLUDES ) {
2271 struct pval *p4;
2272 for (p4=j->u1.list; p4; p4=p4->next) {
2273 /* for each context pointed to, find it, then find a context/label that matches the
2274 target here! */
2275 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2276 return; /* found a match! */
2282 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",
2283 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2284 warns++;
2288 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2290 pval *lp;
2291 #ifdef AAL_ARGCHECK
2292 struct argapp *app, *found;
2293 #endif
2294 struct pval *macro_def;
2295 struct pval *app_def;
2297 char errmsg[4096];
2298 char *strp;
2300 switch (item->type) {
2301 case PV_WORD:
2302 /* fields: item->u1.str == string associated with this (word).
2303 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2304 break;
2306 case PV_MACRO:
2307 /* fields: item->u1.str == name of macro
2308 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2309 item->u2.arglist->u1.str == argument
2310 item->u2.arglist->next == next arg
2312 item->u3.macro_statements == pval list of statements in macro body.
2314 in_abstract_context = 0;
2315 current_context = item;
2316 current_extension = 0;
2317 for (lp=item->u2.arglist; lp; lp=lp->next) {
2320 check_pval(item->u3.macro_statements, apps,in_globals);
2321 break;
2323 case PV_CONTEXT:
2324 /* fields: item->u1.str == name of context
2325 item->u2.statements == pval list of statements in context body
2326 item->u3.abstract == int 1 if an abstract keyword were present
2328 current_context = item;
2329 current_extension = 0;
2330 if ( item->u3.abstract ) {
2331 in_abstract_context = 1;
2332 check_abstract_reference(item);
2333 } else
2334 in_abstract_context = 0;
2335 check_pval(item->u2.statements, apps,in_globals);
2336 break;
2338 case PV_MACRO_CALL:
2339 /* fields: item->u1.str == name of macro to call
2340 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2341 item->u2.arglist->u1.str == argument
2342 item->u2.arglist->next == next arg
2344 macro_def = find_macro(item->u1.str);
2345 if (!macro_def) {
2346 /* here is a good place to check to see if the definition is in extensions.conf! */
2347 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s ! Hopefully it is present in extensions.conf! \n",
2348 item->filename, item->startline, item->endline, item->u1.str);
2349 warns++;
2350 } else if (macro_def->type != PV_MACRO) {
2351 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2352 item->filename, item->startline, item->endline, item->u1.str);
2353 errs++;
2354 } else {
2355 /* macro_def is a MACRO, so do the args match in number? */
2356 int hereargs = 0;
2357 int thereargs = 0;
2359 for (lp=item->u2.arglist; lp; lp=lp->next) {
2360 hereargs++;
2362 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2363 thereargs++;
2365 if (hereargs != thereargs ) {
2366 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",
2367 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2368 errs++;
2371 break;
2373 case PV_APPLICATION_CALL:
2374 /* fields: item->u1.str == name of application to call
2375 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2376 item->u2.arglist->u1.str == argument
2377 item->u2.arglist->next == next arg
2379 /* Need to check to see if the application is available! */
2380 app_def = find_context(item->u1.str);
2381 if (app_def && app_def->type == PV_MACRO) {
2382 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2383 item->filename, item->startline, item->endline, item->u1.str);
2384 errs++;
2386 if (strcasecmp(item->u1.str,"GotoIf") == 0
2387 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2388 || strcasecmp(item->u1.str,"while") == 0
2389 || strcasecmp(item->u1.str,"endwhile") == 0
2390 || strcasecmp(item->u1.str,"random") == 0
2391 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2392 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",
2393 item->filename, item->startline, item->endline, item->u1.str);
2394 warns++;
2396 #ifdef AAL_ARGCHECK
2397 found = 0;
2398 for (app=apps; app; app=app->next) {
2399 if (strcasecmp(app->name, item->u1.str) == 0) {
2400 found =app;
2401 break;
2404 if (!found) {
2405 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2406 item->filename, item->startline, item->endline, item->u1.str);
2407 warns++;
2408 } else
2409 check_app_args(item, item->u2.arglist, app);
2410 #endif
2411 break;
2413 case PV_CASE:
2414 /* fields: item->u1.str == value of case
2415 item->u2.statements == pval list of statements under the case
2417 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2418 /* find the last statement */
2419 check_pval(item->u2.statements, apps,in_globals);
2420 break;
2422 case PV_PATTERN:
2423 /* fields: item->u1.str == value of case
2424 item->u2.statements == pval list of statements under the case
2426 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2427 /* find the last statement */
2429 check_pval(item->u2.statements, apps,in_globals);
2430 break;
2432 case PV_DEFAULT:
2433 /* fields:
2434 item->u2.statements == pval list of statements under the case
2437 check_pval(item->u2.statements, apps,in_globals);
2438 break;
2440 case PV_CATCH:
2441 /* fields: item->u1.str == name of extension to catch
2442 item->u2.statements == pval list of statements in context body
2444 check_pval(item->u2.statements, apps,in_globals);
2445 break;
2447 case PV_SWITCHES:
2448 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2450 check_pval(item->u1.list, apps,in_globals);
2451 break;
2453 case PV_ESWITCHES:
2454 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2456 check_pval(item->u1.list, apps,in_globals);
2457 break;
2459 case PV_INCLUDES:
2460 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2462 check_pval(item->u1.list, apps,in_globals);
2463 check_includes(item);
2464 for (lp=item->u1.list; lp; lp=lp->next){
2465 char *incl_context = lp->u1.str;
2466 struct pval *that_context = find_context(incl_context);
2468 if ( lp->u2.arglist ) {
2469 check_timerange(lp->u2.arglist);
2470 check_dow(lp->u2.arglist->next);
2471 check_day(lp->u2.arglist->next->next);
2472 check_month(lp->u2.arglist->next->next->next);
2475 if (that_context) {
2476 find_pval_gotos(that_context->u2.statements,0);
2480 break;
2482 case PV_STATEMENTBLOCK:
2483 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2485 check_pval(item->u1.list, apps,in_globals);
2486 break;
2488 case PV_VARDEC:
2489 /* fields: item->u1.str == variable name
2490 item->u2.val == variable value to assign
2492 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2493 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2494 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);
2495 ast_expr_register_extra_error_info(errmsg);
2496 ast_expr(item->u2.val, expr_output, sizeof(expr_output));
2497 ast_expr_clear_extra_error_info();
2498 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2499 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2500 item->filename, item->startline, item->endline, item->u2.val);
2501 warns++;
2503 check_expr2_input(item,item->u2.val);
2505 break;
2507 case PV_GOTO:
2508 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2509 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2511 /* don't check goto's in abstract contexts */
2512 if ( in_abstract_context )
2513 break;
2515 check_goto(item);
2516 break;
2518 case PV_LABEL:
2519 /* fields: item->u1.str == label name
2521 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2522 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2523 item->filename, item->startline, item->endline, item->u1.str);
2524 warns++;
2527 check_label(item);
2528 break;
2530 case PV_FOR:
2531 /* fields: item->u1.for_init == a string containing the initalizer
2532 item->u2.for_test == a string containing the loop test
2533 item->u3.for_inc == a string containing the loop increment
2535 item->u4.for_statements == a pval list of statements in the for ()
2537 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);
2538 ast_expr_register_extra_error_info(errmsg);
2540 strp = strchr(item->u1.for_init, '=');
2541 if (strp) {
2542 ast_expr(strp+1, expr_output, sizeof(expr_output));
2544 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output));
2545 strp = strchr(item->u3.for_inc, '=');
2546 if (strp) {
2547 ast_expr(strp+1, expr_output, sizeof(expr_output));
2549 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2550 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2551 item->filename, item->startline, item->endline, item->u2.for_test);
2552 warns++;
2554 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2555 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2556 item->filename, item->startline, item->endline, item->u3.for_inc);
2557 warns++;
2559 check_expr2_input(item,item->u2.for_test);
2560 check_expr2_input(item,item->u3.for_inc);
2562 ast_expr_clear_extra_error_info();
2563 check_pval(item->u4.for_statements, apps,in_globals);
2564 break;
2566 case PV_WHILE:
2567 /* fields: item->u1.str == the while conditional, as supplied by user
2569 item->u2.statements == a pval list of statements in the while ()
2571 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2572 ast_expr_register_extra_error_info(errmsg);
2573 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2574 ast_expr_clear_extra_error_info();
2575 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2576 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2577 item->filename, item->startline, item->endline, item->u1.str);
2578 warns++;
2580 check_expr2_input(item,item->u1.str);
2581 check_pval(item->u2.statements, apps,in_globals);
2582 break;
2584 case PV_BREAK:
2585 /* fields: none
2587 check_break(item);
2588 break;
2590 case PV_RETURN:
2591 /* fields: none
2593 break;
2595 case PV_CONTINUE:
2596 /* fields: none
2598 check_continue(item);
2599 break;
2601 case PV_RANDOM:
2602 /* fields: item->u1.str == the random number expression, as supplied by user
2604 item->u2.statements == a pval list of statements in the if ()
2605 item->u3.else_statements == a pval list of statements in the else
2606 (could be zero)
2608 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2609 ast_expr_register_extra_error_info(errmsg);
2610 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2611 ast_expr_clear_extra_error_info();
2612 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2613 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2614 item->filename, item->startline, item->endline, item->u1.str);
2615 warns++;
2617 check_expr2_input(item,item->u1.str);
2618 check_pval(item->u2.statements, apps,in_globals);
2619 if (item->u3.else_statements) {
2620 check_pval(item->u3.else_statements, apps,in_globals);
2622 break;
2624 case PV_IFTIME:
2625 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2627 item->u2.statements == a pval list of statements in the if ()
2628 item->u3.else_statements == a pval list of statements in the else
2629 (could be zero)
2631 if ( item->u2.arglist ) {
2632 check_timerange(item->u1.list);
2633 check_dow(item->u1.list->next);
2634 check_day(item->u1.list->next->next);
2635 check_month(item->u1.list->next->next->next);
2638 check_pval(item->u2.statements, apps,in_globals);
2639 if (item->u3.else_statements) {
2640 check_pval(item->u3.else_statements, apps,in_globals);
2642 break;
2644 case PV_IF:
2645 /* fields: item->u1.str == the if conditional, as supplied by user
2647 item->u2.statements == a pval list of statements in the if ()
2648 item->u3.else_statements == a pval list of statements in the else
2649 (could be zero)
2651 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2652 ast_expr_register_extra_error_info(errmsg);
2653 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2654 ast_expr_clear_extra_error_info();
2655 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2656 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2657 item->filename, item->startline, item->endline, item->u1.str);
2658 warns++;
2660 check_expr2_input(item,item->u1.str);
2661 check_pval(item->u2.statements, apps,in_globals);
2662 if (item->u3.else_statements) {
2663 check_pval(item->u3.else_statements, apps,in_globals);
2665 break;
2667 case PV_SWITCH:
2668 /* fields: item->u1.str == the switch expression
2670 item->u2.statements == a pval list of statements in the switch,
2671 (will be case statements, most likely!)
2673 /* we can check the switch expression, see if it matches any of the app variables...
2674 if it does, then, are all the possible cases accounted for? */
2675 check_switch_expr(item, apps);
2676 check_pval(item->u2.statements, apps,in_globals);
2677 break;
2679 case PV_EXTENSION:
2680 /* fields: item->u1.str == the extension name, label, whatever it's called
2682 item->u2.statements == a pval list of statements in the extension
2683 item->u3.hints == a char * hint argument
2684 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2686 current_extension = item ;
2688 check_pval(item->u2.statements, apps,in_globals);
2689 break;
2691 case PV_IGNOREPAT:
2692 /* fields: item->u1.str == the ignorepat data
2694 break;
2696 case PV_GLOBALS:
2697 /* fields: item->u1.statements == pval list of statements, usually vardecs
2699 in_abstract_context = 0;
2700 check_pval(item->u1.statements, apps, 1);
2701 break;
2702 default:
2703 break;
2707 void check_pval(pval *item, struct argapp *apps, int in_globals)
2709 pval *i;
2711 /* checks to do:
2712 1. Do goto's point to actual labels?
2713 2. Do macro calls reference a macro?
2714 3. Does the number of macro args match the definition?
2715 4. Is a macro call missing its & at the front?
2716 5. Application calls-- we could check syntax for existing applications,
2717 but I need some some sort of universal description bnf for a general
2718 sort of method for checking arguments, in number, maybe even type, at least.
2719 Don't want to hand code checks for hundreds of applications.
2722 for (i=item; i; i=i->next) {
2723 check_pval_item(i,apps,in_globals);
2727 static void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2730 #ifdef AAL_ARGCHECK
2731 int argapp_errs =0;
2732 char *rfilename;
2733 #endif
2734 struct argapp *apps=0;
2736 if (!item)
2737 return; /* don't check an empty tree */
2738 #ifdef AAL_ARGCHECK
2739 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2740 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2742 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2743 #endif
2744 current_db = item;
2745 errs = warns = notes = 0;
2747 check_context_names();
2748 check_pval(item, apps, 0);
2750 #ifdef AAL_ARGCHECK
2751 argdesc_destroy(apps); /* taketh away */
2752 #endif
2753 current_db = 0;
2755 *arg_errs = errs;
2756 *arg_warns = warns;
2757 *arg_notes = notes;
2760 /* =============================================================================================== */
2761 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2762 /* =============================================================================================== */
2764 static int control_statement_count = 0;
2766 struct ael_priority *new_prio(void)
2768 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2769 return x;
2772 struct ael_extension *new_exten(void)
2774 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2775 return x;
2778 void linkprio(struct ael_extension *exten, struct ael_priority *prio)
2780 if (!exten->plist) {
2781 exten->plist = prio;
2782 exten->plist_last = prio;
2783 } else {
2784 exten->plist_last->next = prio;
2785 exten->plist_last = prio;
2787 if( !prio->exten )
2788 prio->exten = exten; /* don't override the switch value */
2791 void destroy_extensions(struct ael_extension *exten)
2793 struct ael_extension *ne, *nen;
2794 for (ne=exten; ne; ne=nen) {
2795 struct ael_priority *pe, *pen;
2797 if (ne->name)
2798 free(ne->name);
2800 /* cidmatch fields are allocated with name, and freed when
2801 the name field is freed. Don't do a free for this field,
2802 unless you LIKE to see a crash! */
2804 if (ne->hints)
2805 free(ne->hints);
2807 for (pe=ne->plist; pe; pe=pen) {
2808 pen = pe->next;
2809 if (pe->app)
2810 free(pe->app);
2811 pe->app = 0;
2812 if (pe->appargs)
2813 free(pe->appargs);
2814 pe->appargs = 0;
2815 pe->origin = 0;
2816 pe->goto_true = 0;
2817 pe->goto_false = 0;
2818 free(pe);
2820 nen = ne->next_exten;
2821 ne->next_exten = 0;
2822 ne->plist =0;
2823 ne->plist_last = 0;
2824 ne->next_exten = 0;
2825 ne->loop_break = 0;
2826 ne->loop_continue = 0;
2827 free(ne);
2831 static int label_inside_case(pval *label)
2833 pval *p = label;
2835 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
2836 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
2837 return 1;
2840 p = p->dad;
2842 return 0;
2845 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
2847 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2848 exten->next_exten = add;
2851 static void remove_spaces_before_equals(char *str)
2853 char *p;
2854 while( str && *str && *str != '=' )
2856 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
2858 p = str;
2859 while( *p )
2861 *p = *(p+1);
2862 p++;
2865 else
2866 str++;
2870 static void gen_match_to_pattern(char *pattern, char *result)
2872 /* the result will be a string that will be matched by pattern */
2873 char *p=pattern, *t=result;
2874 while (*p) {
2875 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
2876 *t++ = '9';
2877 else if (*p == '[') {
2878 char *z = p+1;
2879 while (*z != ']')
2880 z++;
2881 if (*(z+1)== ']')
2882 z++;
2883 *t++=*(p+1); /* use the first char in the set */
2884 p = z;
2885 } else {
2886 *t++ = *p;
2888 p++;
2890 *t++ = 0; /* cap it off */
2893 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
2895 pval *p,*p2,*p3;
2896 struct ael_priority *pr;
2897 struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
2898 struct ael_priority *while_test, *while_loop, *while_end;
2899 struct ael_priority *switch_test, *switch_end, *fall_thru, *switch_empty;
2900 struct ael_priority *if_test, *if_end, *if_skip, *if_false;
2901 #ifdef OLD_RAND_ACTION
2902 struct ael_priority *rand_test, *rand_end, *rand_skip;
2903 #endif
2904 char buf1[2000];
2905 char buf2[2000];
2906 char *strp, *strp2;
2907 char new_label[2000];
2908 int default_exists;
2909 int local_control_statement_count;
2910 struct ael_priority *loop_break_save;
2911 struct ael_priority *loop_continue_save;
2912 struct ael_extension *switch_case,*switch_null;
2914 for (p=statement; p; p=p->next) {
2915 switch (p->type) {
2916 case PV_VARDEC:
2917 pr = new_prio();
2918 pr->type = AEL_APPCALL;
2919 snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
2920 pr->app = strdup("Set");
2921 remove_spaces_before_equals(buf1);
2922 pr->appargs = strdup(buf1);
2923 pr->origin = p;
2924 linkprio(exten, pr);
2925 break;
2927 case PV_GOTO:
2928 pr = new_prio();
2929 pr->type = AEL_APPCALL;
2930 p->u2.goto_target = get_goto_target(p);
2931 if( p->u2.goto_target ) {
2932 p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
2935 if (!p->u1.list->next) /* just one */ {
2936 pr->app = strdup("Goto");
2937 if (!mother_exten)
2938 pr->appargs = strdup(p->u1.list->u1.str);
2939 else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
2940 snprintf(buf1,sizeof(buf1),"%s|%s", mother_exten->name, p->u1.list->u1.str);
2941 pr->appargs = strdup(buf1);
2944 } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
2945 snprintf(buf1,sizeof(buf1),"%s|%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
2946 pr->app = strdup("Goto");
2947 pr->appargs = strdup(buf1);
2948 } else if (p->u1.list->next && p->u1.list->next->next) {
2949 snprintf(buf1,sizeof(buf1),"%s|%s|%s", p->u1.list->u1.str,
2950 p->u1.list->next->u1.str,
2951 p->u1.list->next->next->u1.str);
2952 pr->app = strdup("Goto");
2953 pr->appargs = strdup(buf1);
2955 pr->origin = p;
2956 linkprio(exten, pr);
2957 break;
2959 case PV_LABEL:
2960 pr = new_prio();
2961 pr->type = AEL_LABEL;
2962 pr->origin = p;
2963 p->u3.compiled_label = exten;
2964 linkprio(exten, pr);
2965 break;
2967 case PV_FOR:
2968 control_statement_count++;
2969 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
2970 loop_continue_save = exten->loop_continue;
2971 snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
2972 for_init = new_prio();
2973 for_inc = new_prio();
2974 for_test = new_prio();
2975 for_loop = new_prio();
2976 for_end = new_prio();
2977 for_init->type = AEL_APPCALL;
2978 for_inc->type = AEL_APPCALL;
2979 for_test->type = AEL_FOR_CONTROL;
2980 for_test->goto_false = for_end;
2981 for_loop->type = AEL_CONTROL1; /* simple goto */
2982 for_end->type = AEL_APPCALL;
2983 for_init->app = strdup("Set");
2985 strcpy(buf2,p->u1.for_init);
2986 remove_spaces_before_equals(buf2);
2987 strp = strchr(buf2, '=');
2988 if (strp) {
2989 strp2 = strchr(p->u1.for_init, '=');
2990 *(strp+1) = 0;
2991 strcat(buf2,"$[");
2992 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
2993 strcat(buf2,"]");
2994 for_init->appargs = strdup(buf2);
2995 for_init->app = strdup("Set");
2996 } else {
2997 strp2 = p->u1.for_init;
2998 while (*strp2 && isspace(*strp2))
2999 strp2++;
3000 if (*strp2 == '&') { /* itsa macro call */
3001 char *strp3 = strp2+1;
3002 while (*strp3 && isspace(*strp3))
3003 strp3++;
3004 strcpy(buf2, strp3);
3005 strp3 = strchr(buf2,'(');
3006 if (strp3) {
3007 *strp3 = '|';
3009 while ((strp3=strchr(buf2,','))) {
3010 *strp3 = '|';
3012 strp3 = strrchr(buf2, ')');
3013 if (strp3)
3014 *strp3 = 0; /* remove the closing paren */
3016 for_init->appargs = strdup(buf2);
3018 for_init->app = strdup("Macro");
3019 } else { /* must be a regular app call */
3020 char *strp3;
3021 strcpy(buf2, strp2);
3022 strp3 = strchr(buf2,'(');
3023 if (strp3) {
3024 *strp3 = 0;
3025 for_init->app = strdup(buf2);
3026 for_init->appargs = strdup(strp3+1);
3027 strp3 = strrchr(for_init->appargs, ')');
3028 if (strp3)
3029 *strp3 = 0; /* remove the closing paren */
3034 strcpy(buf2,p->u3.for_inc);
3035 remove_spaces_before_equals(buf2);
3036 strp = strchr(buf2, '=');
3037 if (strp) { /* there's an = in this part; that means an assignment. set it up */
3038 strp2 = strchr(p->u3.for_inc, '=');
3039 *(strp+1) = 0;
3040 strcat(buf2,"$[");
3041 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3042 strcat(buf2,"]");
3043 for_inc->appargs = strdup(buf2);
3044 for_inc->app = strdup("Set");
3045 } else {
3046 strp2 = p->u3.for_inc;
3047 while (*strp2 && isspace(*strp2))
3048 strp2++;
3049 if (*strp2 == '&') { /* itsa macro call */
3050 char *strp3 = strp2+1;
3051 while (*strp3 && isspace(*strp3))
3052 strp3++;
3053 strcpy(buf2, strp3);
3054 strp3 = strchr(buf2,'(');
3055 if (strp3) {
3056 *strp3 = '|';
3058 while ((strp3=strchr(buf2,','))) {
3059 *strp3 = '|';
3061 strp3 = strrchr(buf2, ')');
3062 if (strp3)
3063 *strp3 = 0; /* remove the closing paren */
3065 for_inc->appargs = strdup(buf2);
3067 for_inc->app = strdup("Macro");
3068 } else { /* must be a regular app call */
3069 char *strp3;
3070 strcpy(buf2, strp2);
3071 strp3 = strchr(buf2,'(');
3072 if (strp3) {
3073 *strp3 = 0;
3074 for_inc->app = strdup(buf2);
3075 for_inc->appargs = strdup(strp3+1);
3076 strp3 = strrchr(for_inc->appargs, ')');
3077 if (strp3)
3078 *strp3 = 0; /* remove the closing paren */
3082 snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
3083 for_test->app = 0;
3084 for_test->appargs = strdup(buf1);
3085 for_loop->goto_true = for_test;
3086 snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
3087 for_end->app = strdup("NoOp");
3088 for_end->appargs = strdup(buf1);
3089 /* link & load! */
3090 linkprio(exten, for_init);
3091 linkprio(exten, for_test);
3093 /* now, put the body of the for loop here */
3094 exten->loop_break = for_end;
3095 exten->loop_continue = for_inc;
3097 gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context); /* this will link in all the statements here */
3099 linkprio(exten, for_inc);
3100 linkprio(exten, for_loop);
3101 linkprio(exten, for_end);
3104 exten->loop_break = loop_break_save;
3105 exten->loop_continue = loop_continue_save;
3106 for_loop->origin = p;
3107 break;
3109 case PV_WHILE:
3110 control_statement_count++;
3111 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3112 loop_continue_save = exten->loop_continue;
3113 snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
3114 while_test = new_prio();
3115 while_loop = new_prio();
3116 while_end = new_prio();
3117 while_test->type = AEL_FOR_CONTROL;
3118 while_test->goto_false = while_end;
3119 while_loop->type = AEL_CONTROL1; /* simple goto */
3120 while_end->type = AEL_APPCALL;
3121 snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
3122 while_test->app = 0;
3123 while_test->appargs = strdup(buf1);
3124 while_loop->goto_true = while_test;
3125 snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
3126 while_end->app = strdup("NoOp");
3127 while_end->appargs = strdup(buf1);
3129 linkprio(exten, while_test);
3131 /* now, put the body of the for loop here */
3132 exten->loop_break = while_end;
3133 exten->loop_continue = while_test;
3135 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the while body statements here */
3137 linkprio(exten, while_loop);
3138 linkprio(exten, while_end);
3141 exten->loop_break = loop_break_save;
3142 exten->loop_continue = loop_continue_save;
3143 while_loop->origin = p;
3144 break;
3146 case PV_SWITCH:
3147 control_statement_count++;
3148 local_control_statement_count = control_statement_count;
3149 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3150 loop_continue_save = exten->loop_continue;
3151 snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
3153 switch_test = new_prio();
3154 switch_end = new_prio();
3155 switch_test->type = AEL_APPCALL;
3156 switch_end->type = AEL_APPCALL;
3157 strncpy(buf2,p->u1.str,sizeof(buf2));
3158 buf2[sizeof(buf2)-1] = 0; /* just in case */
3159 substitute_commas(buf2);
3160 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",control_statement_count, buf2);
3161 switch_test->app = strdup("Goto");
3162 switch_test->appargs = strdup(buf1);
3163 snprintf(buf1,sizeof(buf1),"Finish switch-%s-%d", label, control_statement_count);
3164 switch_end->app = strdup("NoOp");
3165 switch_end->appargs = strdup(buf1);
3166 switch_end->origin = p;
3167 switch_end->exten = exten;
3169 linkprio(exten, switch_test);
3170 linkprio(exten, switch_end);
3172 exten->loop_break = switch_end;
3173 exten->loop_continue = 0;
3174 default_exists = 0;
3176 for (p2=p->u2.statements; p2; p2=p2->next) {
3177 /* now, for each case/default put the body of the for loop here */
3178 if (p2->type == PV_CASE) {
3179 /* ok, generate a extension and link it in */
3180 switch_case = new_exten();
3181 switch_case->context = this_context;
3182 switch_case->is_switch = 1;
3183 /* the break/continue locations are inherited from parent */
3184 switch_case->loop_break = exten->loop_break;
3185 switch_case->loop_continue = exten->loop_continue;
3187 linkexten(exten,switch_case);
3188 strncpy(buf2,p2->u1.str,sizeof(buf2));
3189 buf2[sizeof(buf2)-1] = 0; /* just in case */
3190 substitute_commas(buf2);
3191 snprintf(buf1,sizeof(buf1),"sw-%d-%s", local_control_statement_count, buf2);
3192 switch_case->name = strdup(buf1);
3193 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
3195 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the case body statements here */
3197 /* here is where we write code to "fall thru" to the next case... if there is one... */
3198 for (p3=p2->u2.statements; p3; p3=p3->next) {
3199 if (!p3->next)
3200 break;
3202 /* p3 now points the last statement... */
3203 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN) ) {
3204 /* is there a following CASE/PATTERN/DEFAULT? */
3205 if (p2->next && p2->next->type == PV_CASE) {
3206 fall_thru = new_prio();
3207 fall_thru->type = AEL_APPCALL;
3208 fall_thru->app = strdup("Goto");
3209 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3210 buf2[sizeof(buf2)-1] = 0; /* just in case */
3211 substitute_commas(buf2);
3212 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3213 fall_thru->appargs = strdup(buf1);
3214 linkprio(switch_case, fall_thru);
3215 } else if (p2->next && p2->next->type == PV_PATTERN) {
3216 fall_thru = new_prio();
3217 fall_thru->type = AEL_APPCALL;
3218 fall_thru->app = strdup("Goto");
3219 gen_match_to_pattern(p2->next->u1.str, buf2);
3220 substitute_commas(buf2);
3221 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10", local_control_statement_count, buf2);
3222 fall_thru->appargs = strdup(buf1);
3223 linkprio(switch_case, fall_thru);
3224 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3225 fall_thru = new_prio();
3226 fall_thru->type = AEL_APPCALL;
3227 fall_thru->app = strdup("Goto");
3228 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3229 fall_thru->appargs = strdup(buf1);
3230 linkprio(switch_case, fall_thru);
3231 } else if (!p2->next) {
3232 fall_thru = new_prio();
3233 fall_thru->type = AEL_CONTROL1;
3234 fall_thru->goto_true = switch_end;
3235 fall_thru->app = strdup("Goto");
3236 linkprio(switch_case, fall_thru);
3239 if (switch_case->return_needed) {
3240 char buf[2000];
3241 struct ael_priority *np2 = new_prio();
3242 np2->type = AEL_APPCALL;
3243 np2->app = strdup("NoOp");
3244 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3245 np2->appargs = strdup(buf);
3246 linkprio(switch_case, np2);
3247 switch_case-> return_target = np2;
3249 } else if (p2->type == PV_PATTERN) {
3250 /* ok, generate a extension and link it in */
3251 switch_case = new_exten();
3252 switch_case->context = this_context;
3253 switch_case->is_switch = 1;
3254 /* the break/continue locations are inherited from parent */
3255 switch_case->loop_break = exten->loop_break;
3256 switch_case->loop_continue = exten->loop_continue;
3258 linkexten(exten,switch_case);
3259 strncpy(buf2,p2->u1.str,sizeof(buf2));
3260 buf2[sizeof(buf2)-1] = 0; /* just in case */
3261 substitute_commas(buf2);
3262 snprintf(buf1,sizeof(buf1),"_sw-%d-%s", local_control_statement_count, buf2);
3263 switch_case->name = strdup(buf1);
3264 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, buf2, local_control_statement_count);
3266 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the while body statements here */
3267 /* here is where we write code to "fall thru" to the next case... if there is one... */
3268 for (p3=p2->u2.statements; p3; p3=p3->next) {
3269 if (!p3->next)
3270 break;
3272 /* p3 now points the last statement... */
3273 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
3274 /* is there a following CASE/PATTERN/DEFAULT? */
3275 if (p2->next && p2->next->type == PV_CASE) {
3276 fall_thru = new_prio();
3277 fall_thru->type = AEL_APPCALL;
3278 fall_thru->app = strdup("Goto");
3279 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3280 buf2[sizeof(buf2)-1] = 0; /* just in case */
3281 substitute_commas(buf2);
3282 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3283 fall_thru->appargs = strdup(buf1);
3284 linkprio(switch_case, fall_thru);
3285 } else if (p2->next && p2->next->type == PV_PATTERN) {
3286 fall_thru = new_prio();
3287 fall_thru->type = AEL_APPCALL;
3288 fall_thru->app = strdup("Goto");
3289 gen_match_to_pattern(p2->next->u1.str, buf2);
3290 substitute_commas(buf2);
3291 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3292 fall_thru->appargs = strdup(buf1);
3293 linkprio(switch_case, fall_thru);
3294 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3295 fall_thru = new_prio();
3296 fall_thru->type = AEL_APPCALL;
3297 fall_thru->app = strdup("Goto");
3298 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3299 fall_thru->appargs = strdup(buf1);
3300 linkprio(switch_case, fall_thru);
3301 } else if (!p2->next) {
3302 fall_thru = new_prio();
3303 fall_thru->type = AEL_CONTROL1;
3304 fall_thru->goto_true = switch_end;
3305 fall_thru->app = strdup("Goto");
3306 linkprio(switch_case, fall_thru);
3309 if (switch_case->return_needed) {
3310 char buf[2000];
3311 struct ael_priority *np2 = new_prio();
3312 np2->type = AEL_APPCALL;
3313 np2->app = strdup("NoOp");
3314 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3315 np2->appargs = strdup(buf);
3316 linkprio(switch_case, np2);
3317 switch_case-> return_target = np2;
3319 } else if (p2->type == PV_DEFAULT) {
3320 /* ok, generate a extension and link it in */
3321 switch_case = new_exten();
3322 switch_case->context = this_context;
3323 switch_case->is_switch = 1;
3325 /* new: the default case intros a pattern with ., which covers ALMOST everything.
3326 but it doesn't cover a NULL pattern. So, we'll define a null extension to match
3327 that goto's the default extension. */
3329 default_exists++;
3330 switch_null = new_exten();
3331 switch_null->context = this_context;
3332 switch_null->is_switch = 1;
3333 switch_empty = new_prio();
3334 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3335 switch_empty->app = strdup("Goto");
3336 switch_empty->appargs = strdup(buf1);
3337 linkprio(switch_null, switch_empty);
3338 snprintf(buf1,sizeof(buf1),"sw-%d-", local_control_statement_count);
3339 switch_null->name = strdup(buf1);
3340 switch_null->loop_break = exten->loop_break;
3341 switch_null->loop_continue = exten->loop_continue;
3342 linkexten(exten,switch_null);
3344 /* the break/continue locations are inherited from parent */
3345 switch_case->loop_break = exten->loop_break;
3346 switch_case->loop_continue = exten->loop_continue;
3347 linkexten(exten,switch_case);
3348 snprintf(buf1,sizeof(buf1),"_sw-%d-.", local_control_statement_count);
3349 switch_case->name = strdup(buf1);
3351 snprintf(new_label,sizeof(new_label),"sw-%s-default-%d", label, local_control_statement_count);
3353 gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the default: body statements here */
3355 /* here is where we write code to "fall thru" to the next case... if there is one... */
3356 for (p3=p2->u2.statements; p3; p3=p3->next) {
3357 if (!p3->next)
3358 break;
3360 /* p3 now points the last statement... */
3361 if (!p3 || (p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
3362 /* is there a following CASE/PATTERN/DEFAULT? */
3363 if (p2->next && p2->next->type == PV_CASE) {
3364 fall_thru = new_prio();
3365 fall_thru->type = AEL_APPCALL;
3366 fall_thru->app = strdup("Goto");
3367 strncpy(buf2,p2->next->u1.str,sizeof(buf2));
3368 buf2[sizeof(buf2)-1] = 0; /* just in case */
3369 substitute_commas(buf2);
3370 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3371 fall_thru->appargs = strdup(buf1);
3372 linkprio(switch_case, fall_thru);
3373 } else if (p2->next && p2->next->type == PV_PATTERN) {
3374 fall_thru = new_prio();
3375 fall_thru->type = AEL_APPCALL;
3376 fall_thru->app = strdup("Goto");
3377 gen_match_to_pattern(p2->next->u1.str, buf2);
3378 substitute_commas(buf2);
3379 snprintf(buf1,sizeof(buf1),"sw-%d-%s|10",local_control_statement_count, buf2);
3380 fall_thru->appargs = strdup(buf1);
3381 linkprio(switch_case, fall_thru);
3382 } else if (p2->next && p2->next->type == PV_DEFAULT) {
3383 fall_thru = new_prio();
3384 fall_thru->type = AEL_APPCALL;
3385 fall_thru->app = strdup("Goto");
3386 snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
3387 fall_thru->appargs = strdup(buf1);
3388 linkprio(switch_case, fall_thru);
3389 } else if (!p2->next) {
3390 fall_thru = new_prio();
3391 fall_thru->type = AEL_CONTROL1;
3392 fall_thru->goto_true = switch_end;
3393 fall_thru->app = strdup("Goto");
3394 linkprio(switch_case, fall_thru);
3397 if (switch_case->return_needed) {
3398 char buf[2000];
3399 struct ael_priority *np2 = new_prio();
3400 np2->type = AEL_APPCALL;
3401 np2->app = strdup("NoOp");
3402 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3403 np2->appargs = strdup(buf);
3404 linkprio(switch_case, np2);
3405 switch_case-> return_target = np2;
3407 } else {
3408 /* what could it be??? */
3412 exten->loop_break = loop_break_save;
3413 exten->loop_continue = loop_continue_save;
3414 switch_test->origin = p;
3415 switch_end->origin = p;
3416 break;
3418 case PV_MACRO_CALL:
3419 pr = new_prio();
3420 pr->type = AEL_APPCALL;
3421 snprintf(buf1,sizeof(buf1),"%s", p->u1.str);
3422 for (p2 = p->u2.arglist; p2; p2 = p2->next) {
3423 strcat(buf1,"|");
3424 strcat(buf1,p2->u1.str);
3426 pr->app = strdup("Macro");
3427 pr->appargs = strdup(buf1);
3428 pr->origin = p;
3429 linkprio(exten, pr);
3430 break;
3432 case PV_APPLICATION_CALL:
3433 pr = new_prio();
3434 pr->type = AEL_APPCALL;
3435 buf1[0] = 0;
3436 for (p2 = p->u2.arglist; p2; p2 = p2->next) {
3437 if (p2 != p->u2.arglist )
3438 strcat(buf1,"|");
3439 substitute_commas(p2->u1.str);
3440 strcat(buf1,p2->u1.str);
3442 pr->app = strdup(p->u1.str);
3443 pr->appargs = strdup(buf1);
3444 pr->origin = p;
3445 linkprio(exten, pr);
3446 break;
3448 case PV_BREAK:
3449 pr = new_prio();
3450 pr->type = AEL_CONTROL1; /* simple goto */
3451 pr->goto_true = exten->loop_break;
3452 pr->origin = p;
3453 linkprio(exten, pr);
3454 break;
3456 case PV_RETURN: /* hmmmm */
3457 pr = new_prio();
3458 pr->type = AEL_RETURN; /* simple goto */
3459 exten->return_needed++;
3460 pr->app = strdup("Goto");
3461 pr->appargs = strdup("");
3462 pr->origin = p;
3463 linkprio(exten, pr);
3464 break;
3466 case PV_CONTINUE:
3467 pr = new_prio();
3468 pr->type = AEL_CONTROL1; /* simple goto */
3469 pr->goto_true = exten->loop_continue;
3470 pr->origin = p;
3471 linkprio(exten, pr);
3472 break;
3474 #ifdef OLD_RAND_ACTION
3475 case PV_RANDOM:
3476 control_statement_count++;
3477 snprintf(new_label,sizeof(new_label),"rand-%s-%d", label, control_statement_count);
3478 rand_test = new_prio();
3479 rand_test->type = AEL_RAND_CONTROL;
3480 snprintf(buf1,sizeof(buf1),"$[%s]",
3481 p->u1.str );
3482 rand_test->app = 0;
3483 rand_test->appargs = strdup(buf1);
3484 rand_test->origin = p;
3486 rand_end = new_prio();
3487 rand_end->type = AEL_APPCALL;
3488 snprintf(buf1,sizeof(buf1),"Finish rand-%s-%d", label, control_statement_count);
3489 rand_end->app = strdup("NoOp");
3490 rand_end->appargs = strdup(buf1);
3492 rand_skip = new_prio();
3493 rand_skip->type = AEL_CONTROL1; /* simple goto */
3494 rand_skip->goto_true = rand_end;
3495 rand_skip->origin = p;
3497 rand_test->goto_true = rand_skip; /* +1, really */
3499 linkprio(exten, rand_test);
3501 if (p->u3.else_statements) {
3502 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the else statements here */
3505 linkprio(exten, rand_skip);
3507 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the "true" statements here */
3509 linkprio(exten, rand_end);
3511 break;
3512 #endif
3514 case PV_IFTIME:
3515 control_statement_count++;
3516 snprintf(new_label,sizeof(new_label),"iftime-%s-%d", label, control_statement_count);
3518 if_test = new_prio();
3519 if_test->type = AEL_IFTIME_CONTROL;
3520 snprintf(buf1,sizeof(buf1),"%s|%s|%s|%s",
3521 p->u1.list->u1.str,
3522 p->u1.list->next->u1.str,
3523 p->u1.list->next->next->u1.str,
3524 p->u1.list->next->next->next->u1.str);
3525 if_test->app = 0;
3526 if_test->appargs = strdup(buf1);
3527 if_test->origin = p;
3529 if_end = new_prio();
3530 if_end->type = AEL_APPCALL;
3531 snprintf(buf1,sizeof(buf1),"Finish iftime-%s-%d", label, control_statement_count);
3532 if_end->app = strdup("NoOp");
3533 if_end->appargs = strdup(buf1);
3535 if (p->u3.else_statements) {
3536 if_skip = new_prio();
3537 if_skip->type = AEL_CONTROL1; /* simple goto */
3538 if_skip->goto_true = if_end;
3539 if_skip->origin = p;
3541 } else {
3542 if_skip = 0;
3544 if_test->goto_false = if_end;
3547 if_false = new_prio();
3548 if_false->type = AEL_CONTROL1;
3549 if (p->u3.else_statements) {
3550 if_false->goto_true = if_skip; /* +1 */
3551 } else {
3552 if_false->goto_true = if_end;
3555 /* link & load! */
3556 linkprio(exten, if_test);
3557 linkprio(exten, if_false);
3559 /* now, put the body of the if here */
3561 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
3563 if (p->u3.else_statements) {
3564 linkprio(exten, if_skip);
3565 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
3569 linkprio(exten, if_end);
3571 break;
3573 case PV_RANDOM:
3574 case PV_IF:
3575 control_statement_count++;
3576 snprintf(new_label,sizeof(new_label),"if-%s-%d", label, control_statement_count);
3578 if_test = new_prio();
3579 if_end = new_prio();
3580 if_test->type = AEL_IF_CONTROL;
3581 if_end->type = AEL_APPCALL;
3582 if ( p->type == PV_RANDOM )
3583 snprintf(buf1,sizeof(buf1),"$[${RAND(0,99)} < (%s)]",p->u1.str);
3584 else
3585 snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
3586 if_test->app = 0;
3587 if_test->appargs = strdup(buf1);
3588 snprintf(buf1,sizeof(buf1),"Finish if-%s-%d", label, control_statement_count);
3589 if_end->app = strdup("NoOp");
3590 if_end->appargs = strdup(buf1);
3591 if_test->origin = p;
3593 if (p->u3.else_statements) {
3594 if_skip = new_prio();
3595 if_skip->type = AEL_CONTROL1; /* simple goto */
3596 if_skip->goto_true = if_end;
3597 if_test->goto_false = if_skip;;
3598 } else {
3599 if_skip = 0;
3600 if_test->goto_false = if_end;;
3603 /* link & load! */
3604 linkprio(exten, if_test);
3606 /* now, put the body of the if here */
3608 gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
3610 if (p->u3.else_statements) {
3611 linkprio(exten, if_skip);
3612 gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
3616 linkprio(exten, if_end);
3618 break;
3620 case PV_STATEMENTBLOCK:
3621 gen_prios(exten, label, p->u1.list, mother_exten, this_context ); /* recurse into the block */
3622 break;
3624 case PV_CATCH:
3625 control_statement_count++;
3626 /* generate an extension with name of catch, put all catch stats
3627 into this exten! */
3628 switch_case = new_exten();
3629 switch_case->context = this_context;
3630 linkexten(exten,switch_case);
3631 switch_case->name = strdup(p->u1.str);
3632 snprintf(new_label,sizeof(new_label),"catch-%s-%d",p->u1.str, control_statement_count);
3634 gen_prios(switch_case, new_label, p->u2.statements,mother_exten,this_context); /* this will link in all the catch body statements here */
3635 if (switch_case->return_needed) {
3636 char buf[2000];
3637 struct ael_priority *np2 = new_prio();
3638 np2->type = AEL_APPCALL;
3639 np2->app = strdup("NoOp");
3640 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
3641 np2->appargs = strdup(buf);
3642 linkprio(switch_case, np2);
3643 switch_case-> return_target = np2;
3646 break;
3647 default:
3648 break;
3653 void set_priorities(struct ael_extension *exten)
3655 int i;
3656 struct ael_priority *pr;
3657 do {
3658 if (exten->is_switch)
3659 i = 10;
3660 else if (exten->regexten)
3661 i=2;
3662 else
3663 i=1;
3665 for (pr=exten->plist; pr; pr=pr->next) {
3666 pr->priority_num = i;
3668 if (!pr->origin || (pr->origin && pr->origin->type != PV_LABEL) ) /* Labels don't show up in the dialplan,
3669 but we want them to point to the right
3670 priority, which would be the next line
3671 after the label; */
3672 i++;
3675 exten = exten->next_exten;
3676 } while ( exten );
3679 void add_extensions(struct ael_extension *exten)
3681 struct ael_priority *pr;
3682 char *label=0;
3683 char realext[AST_MAX_EXTENSION];
3684 if (!exten) {
3685 ast_log(LOG_WARNING, "This file is Empty!\n" );
3686 return;
3688 do {
3689 struct ael_priority *last = 0;
3691 memset(realext, '\0', sizeof(realext));
3692 pbx_substitute_variables_helper(NULL, exten->name, realext, sizeof(realext) - 1);
3693 if (exten->hints) {
3694 if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, PRIORITY_HINT, NULL, exten->cidmatch,
3695 exten->hints, NULL, ast_free, registrar)) {
3696 ast_log(LOG_WARNING, "Unable to add step at priority 'hint' of extension '%s'\n",
3697 exten->name);
3701 for (pr=exten->plist; pr; pr=pr->next) {
3702 char app[2000];
3703 char appargs[2000];
3705 /* before we can add the extension, we need to prep the app/appargs;
3706 the CONTROL types need to be done after the priority numbers are calculated.
3708 if (pr->type == AEL_LABEL) /* don't try to put labels in the dialplan! */ {
3709 last = pr;
3710 continue;
3713 if (pr->app)
3714 strcpy(app, pr->app);
3715 else
3716 app[0] = 0;
3717 if (pr->appargs )
3718 strcpy(appargs, pr->appargs);
3719 else
3720 appargs[0] = 0;
3721 switch( pr->type ) {
3722 case AEL_APPCALL:
3723 /* easy case. Everything is all set up */
3724 break;
3726 case AEL_CONTROL1: /* FOR loop, WHILE loop, BREAK, CONTINUE, IF, IFTIME */
3727 /* simple, unconditional goto. */
3728 strcpy(app,"Goto");
3729 if (pr->goto_true->origin && pr->goto_true->origin->type == PV_SWITCH ) {
3730 snprintf(appargs,sizeof(appargs),"%s|%d", pr->goto_true->exten->name, pr->goto_true->priority_num);
3731 } else if (pr->goto_true->origin && pr->goto_true->origin->type == PV_IFTIME && pr->goto_true->origin->u3.else_statements ) {
3732 snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num+1);
3733 } else
3734 snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num);
3735 break;
3737 case AEL_FOR_CONTROL: /* WHILE loop test, FOR loop test */
3738 strcpy(app,"GotoIf");
3739 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
3740 break;
3742 case AEL_IF_CONTROL:
3743 strcpy(app,"GotoIf");
3744 if (pr->origin->u3.else_statements )
3745 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num+1);
3746 else
3747 snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
3748 break;
3750 case AEL_RAND_CONTROL:
3751 strcpy(app,"Random");
3752 snprintf(appargs,sizeof(appargs),"%s:%d", pr->appargs, pr->goto_true->priority_num+1);
3753 break;
3755 case AEL_IFTIME_CONTROL:
3756 strcpy(app,"GotoIfTime");
3757 snprintf(appargs,sizeof(appargs),"%s?%d", pr->appargs, pr->priority_num+2);
3758 break;
3760 case AEL_RETURN:
3761 strcpy(app,"Goto");
3762 snprintf(appargs,sizeof(appargs), "%d", exten->return_target->priority_num);
3763 break;
3765 default:
3766 break;
3768 if (last && last->type == AEL_LABEL ) {
3769 label = last->origin->u1.str;
3771 else
3772 label = 0;
3774 if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, pr->priority_num, (label?label:NULL), exten->cidmatch,
3775 app, strdup(appargs), ast_free, registrar)) {
3776 ast_log(LOG_WARNING, "Unable to add step at priority '%d' of extension '%s'\n", pr->priority_num,
3777 exten->name);
3779 last = pr;
3781 exten = exten->next_exten;
3782 } while ( exten );
3785 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem)
3787 /* travel to the end of the list... */
3788 struct ael_extension *lptr;
3789 if( !*list ) {
3790 *list = newmem;
3791 return;
3793 lptr = *list;
3795 while( lptr->next_exten ) {
3796 lptr = lptr->next_exten;
3798 /* lptr should now pointing to the last element in the list; it has a null next_exten pointer */
3799 lptr->next_exten = newmem;
3802 static pval *get_extension_or_contxt(pval *p)
3804 while( p && p->type != PV_EXTENSION && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
3806 p = p->dad;
3809 return p;
3812 static pval *get_contxt(pval *p)
3814 while( p && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
3816 p = p->dad;
3819 return p;
3822 static void fix_gotos_in_extensions(struct ael_extension *exten)
3824 struct ael_extension *e;
3825 for(e=exten;e;e=e->next_exten) {
3827 struct ael_priority *p;
3828 for(p=e->plist;p;p=p->next) {
3830 if( p->origin && p->origin->type == PV_GOTO && p->origin->u3.goto_target_in_case ) {
3832 /* fix the extension of the goto target to the actual extension in the post-compiled dialplan */
3834 pval *target = p->origin->u2.goto_target;
3835 struct ael_extension *z = target->u3.compiled_label;
3836 pval *pv2 = p->origin;
3837 char buf1[500];
3838 char *apparg_save = p->appargs;
3840 p->appargs = 0;
3841 if (!pv2->u1.list->next) /* just one -- it won't hurt to repeat the extension */ {
3842 snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->u1.str);
3843 p->appargs = strdup(buf1);
3845 } else if (pv2->u1.list->next && !pv2->u1.list->next->next) /* two */ {
3846 snprintf(buf1,sizeof(buf1),"%s|%s", z->name, pv2->u1.list->next->u1.str);
3847 p->appargs = strdup(buf1);
3848 } else if (pv2->u1.list->next && pv2->u1.list->next->next) {
3849 snprintf(buf1,sizeof(buf1),"%s|%s|%s", pv2->u1.list->u1.str,
3850 z->name,
3851 pv2->u1.list->next->next->u1.str);
3852 p->appargs = strdup(buf1);
3854 else
3855 printf("WHAT? The goto doesn't fall into one of three cases for GOTO????\n");
3857 if( apparg_save ) {
3858 free(apparg_save);
3866 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root)
3868 pval *p,*p2;
3869 struct ast_context *context;
3870 char buf[2000];
3871 struct ael_extension *exten;
3872 struct ael_extension *exten_list = 0;
3874 for (p=root; p; p=p->next ) { /* do the globals first, so they'll be there
3875 when we try to eval them */
3876 switch (p->type) {
3877 case PV_GLOBALS:
3878 /* just VARDEC elements */
3879 for (p2=p->u1.list; p2; p2=p2->next) {
3880 char buf2[2000];
3881 snprintf(buf2,sizeof(buf2),"%s=%s", p2->u1.str, p2->u2.val);
3882 pbx_builtin_setvar(NULL, buf2);
3884 break;
3885 default:
3886 break;
3890 for (p=root; p; p=p->next ) {
3891 pval *lp;
3892 int argc;
3894 switch (p->type) {
3895 case PV_MACRO:
3896 strcpy(buf,"macro-");
3897 strcat(buf,p->u1.str);
3898 context = ast_context_create(local_contexts, buf, registrar);
3900 exten = new_exten();
3901 exten->context = context;
3902 exten->name = strdup("s");
3903 argc = 1;
3904 for (lp=p->u2.arglist; lp; lp=lp->next) {
3905 /* for each arg, set up a "Set" command */
3906 struct ael_priority *np2 = new_prio();
3907 np2->type = AEL_APPCALL;
3908 np2->app = strdup("Set");
3909 snprintf(buf,sizeof(buf),"%s=${ARG%d}", lp->u1.str, argc++);
3910 remove_spaces_before_equals(buf);
3911 np2->appargs = strdup(buf);
3912 linkprio(exten, np2);
3914 /* add any includes */
3915 for (p2=p->u3.macro_statements; p2; p2=p2->next) {
3916 pval *p3;
3918 switch (p2->type) {
3919 case PV_INCLUDES:
3920 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
3921 if ( p3->u2.arglist ) {
3922 snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s",
3923 p3->u1.str,
3924 p3->u2.arglist->u1.str,
3925 p3->u2.arglist->next->u1.str,
3926 p3->u2.arglist->next->next->u1.str,
3927 p3->u2.arglist->next->next->next->u1.str);
3928 ast_context_add_include2(context, buf, registrar);
3929 } else
3930 ast_context_add_include2(context, p3->u1.str, registrar);
3932 break;
3933 default:
3934 break;
3937 /* CONTAINS APPCALLS, CATCH, just like extensions... */
3938 gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context );
3939 if (exten->return_needed) {
3940 struct ael_priority *np2 = new_prio();
3941 np2->type = AEL_APPCALL;
3942 np2->app = strdup("NoOp");
3943 snprintf(buf,sizeof(buf),"End of Macro %s-%s",p->u1.str, exten->name);
3944 np2->appargs = strdup(buf);
3945 linkprio(exten, np2);
3946 exten-> return_target = np2;
3949 set_priorities(exten);
3950 attach_exten(&exten_list, exten);
3951 break;
3953 case PV_GLOBALS:
3954 /* already done */
3955 break;
3957 case PV_CONTEXT:
3958 context = ast_context_create(local_contexts, p->u1.str, registrar);
3960 /* contexts contain: ignorepat, includes, switches, eswitches, extensions, */
3961 for (p2=p->u2.statements; p2; p2=p2->next) {
3962 pval *p3;
3963 char *s3;
3965 switch (p2->type) {
3966 case PV_EXTENSION:
3967 exten = new_exten();
3968 exten->name = strdup(p2->u1.str);
3969 exten->context = context;
3971 if( (s3=strchr(exten->name, '/') ) != 0 )
3973 *s3 = 0;
3974 exten->cidmatch = s3+1;
3977 if ( p2->u3.hints )
3978 exten->hints = strdup(p2->u3.hints);
3979 exten->regexten = p2->u4.regexten;
3980 gen_prios(exten, p->u1.str, p2->u2.statements, 0, context );
3981 if (exten->return_needed) {
3982 struct ael_priority *np2 = new_prio();
3983 np2->type = AEL_APPCALL;
3984 np2->app = strdup("NoOp");
3985 snprintf(buf,sizeof(buf),"End of Extension %s", exten->name);
3986 np2->appargs = strdup(buf);
3987 linkprio(exten, np2);
3988 exten-> return_target = np2;
3990 /* is the last priority in the extension a label? Then add a trailing no-op */
3991 if( !exten->plist_last )
3993 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Empty Extension!\n",
3994 p2->filename, p2->startline, p2->endline);
3997 if ( exten->plist_last && exten->plist_last->type == AEL_LABEL ) {
3998 struct ael_priority *np2 = new_prio();
3999 np2->type = AEL_APPCALL;
4000 np2->app = strdup("NoOp");
4001 snprintf(buf,sizeof(buf),"A NoOp to follow a trailing label %s", exten->plist_last->origin->u1.str);
4002 np2->appargs = strdup(buf);
4003 linkprio(exten, np2);
4006 set_priorities(exten);
4007 attach_exten(&exten_list, exten);
4008 break;
4010 case PV_IGNOREPAT:
4011 ast_context_add_ignorepat2(context, p2->u1.str, registrar);
4012 break;
4014 case PV_INCLUDES:
4015 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4016 if ( p3->u2.arglist ) {
4017 snprintf(buf,sizeof(buf), "%s|%s|%s|%s|%s",
4018 p3->u1.str,
4019 p3->u2.arglist->u1.str,
4020 p3->u2.arglist->next->u1.str,
4021 p3->u2.arglist->next->next->u1.str,
4022 p3->u2.arglist->next->next->next->u1.str);
4023 ast_context_add_include2(context, buf, registrar);
4024 } else
4025 ast_context_add_include2(context, p3->u1.str, registrar);
4027 break;
4029 case PV_SWITCHES:
4030 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4031 char *c = strchr(p3->u1.str, '/');
4032 if (c) {
4033 *c = '\0';
4034 c++;
4035 } else
4036 c = "";
4038 ast_context_add_switch2(context, p3->u1.str, c, 0, registrar);
4040 break;
4042 case PV_ESWITCHES:
4043 for (p3 = p2->u1.list; p3 ;p3=p3->next) {
4044 char *c = strchr(p3->u1.str, '/');
4045 if (c) {
4046 *c = '\0';
4047 c++;
4048 } else
4049 c = "";
4051 ast_context_add_switch2(context, p3->u1.str, c, 1, registrar);
4053 break;
4054 default:
4055 break;
4059 break;
4061 default:
4062 /* huh? what? */
4063 break;
4067 /* moved these from being done after a macro or extension were processed,
4068 to after all processing is done, for the sake of fixing gotos to labels inside cases... */
4069 /* I guess this would be considered 2nd pass of compiler now... */
4070 fix_gotos_in_extensions(exten_list); /* find and fix extension ref in gotos to labels that are in case statements */
4071 add_extensions(exten_list); /* actually makes calls to create priorities in ast_contexts -- feeds dialplan to asterisk */
4072 destroy_extensions(exten_list); /* all that remains is an empty husk, discard of it as is proper */
4077 static int aeldebug = 0;
4079 /* interface stuff */
4081 /* if all the below are static, who cares if they are present? */
4083 static int pbx_load_module(void)
4085 int errs=0, sem_err=0, sem_warn=0, sem_note=0;
4086 char *rfilename;
4087 struct ast_context *local_contexts=NULL, *con;
4088 struct pval *parse_tree;
4090 ast_log(LOG_NOTICE, "Starting AEL load process.\n");
4091 if (config[0] == '/')
4092 rfilename = (char *)config;
4093 else {
4094 rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
4095 sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
4097 ast_log(LOG_NOTICE, "AEL load process: calculated config file name '%s'.\n", rfilename);
4099 if (access(rfilename,R_OK) != 0) {
4100 ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
4101 return AST_MODULE_LOAD_DECLINE;
4104 parse_tree = ael2_parse(rfilename, &errs);
4105 ast_log(LOG_NOTICE, "AEL load process: parsed config file name '%s'.\n", rfilename);
4106 ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
4107 if (errs == 0 && sem_err == 0) {
4108 ast_log(LOG_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename);
4109 ast_compile_ael2(&local_contexts, parse_tree);
4110 ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename);
4112 ast_merge_contexts_and_delete(&local_contexts, registrar);
4113 ast_log(LOG_NOTICE, "AEL load process: merged config file name '%s'.\n", rfilename);
4114 for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
4115 ast_context_verify_includes(con);
4116 ast_log(LOG_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename);
4117 } else {
4118 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);
4119 destroy_pval(parse_tree); /* free up the memory */
4120 return AST_MODULE_LOAD_DECLINE;
4122 destroy_pval(parse_tree); /* free up the memory */
4124 return AST_MODULE_LOAD_SUCCESS;
4127 /* CLI interface */
4128 static int ael2_debug_read(int fd, int argc, char *argv[])
4130 aeldebug |= DEBUG_READ;
4131 return 0;
4134 static int ael2_debug_tokens(int fd, int argc, char *argv[])
4136 aeldebug |= DEBUG_TOKENS;
4137 return 0;
4140 static int ael2_debug_macros(int fd, int argc, char *argv[])
4142 aeldebug |= DEBUG_MACROS;
4143 return 0;
4146 static int ael2_debug_contexts(int fd, int argc, char *argv[])
4148 aeldebug |= DEBUG_CONTEXTS;
4149 return 0;
4152 static int ael2_no_debug(int fd, int argc, char *argv[])
4154 aeldebug = 0;
4155 return 0;
4158 static int ael2_reload(int fd, int argc, char *argv[])
4160 return (pbx_load_module());
4163 static struct ast_cli_entry cli_ael_no_debug = {
4164 { "ael", "no", "debug", NULL },
4165 ael2_no_debug, NULL,
4166 NULL };
4168 static struct ast_cli_entry cli_ael[] = {
4169 { { "ael", "reload", NULL },
4170 ael2_reload, "Reload AEL configuration" },
4172 { { "ael", "debug", "read", NULL },
4173 ael2_debug_read, "Enable AEL read debug (does nothing)" },
4175 { { "ael", "debug", "tokens", NULL },
4176 ael2_debug_tokens, "Enable AEL tokens debug (does nothing)" },
4178 { { "ael", "debug", "macros", NULL },
4179 ael2_debug_macros, "Enable AEL macros debug (does nothing)" },
4181 { { "ael", "debug", "contexts", NULL },
4182 ael2_debug_contexts, "Enable AEL contexts debug (does nothing)" },
4184 { { "ael", "nodebug", NULL },
4185 ael2_no_debug, "Disable AEL debug messages",
4186 NULL, NULL, &cli_ael_no_debug },
4189 static int unload_module(void)
4191 ast_context_destroy(NULL, registrar);
4192 ast_cli_unregister_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
4193 return 0;
4196 static int load_module(void)
4198 ast_cli_register_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
4199 return (pbx_load_module());
4202 static int reload(void)
4204 return pbx_load_module();
4207 #ifdef STANDALONE_AEL
4208 #define AST_MODULE "ael"
4209 int ael_external_load_module(void);
4210 int ael_external_load_module(void)
4212 pbx_load_module();
4213 return 1;
4215 #endif
4217 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
4218 .load = load_module,
4219 .unload = unload_module,
4220 .reload = reload,
4224 /* DESTROY the PVAL tree ============================================================================ */
4228 void destroy_pval_item(pval *item)
4230 if (item == NULL) {
4231 ast_log(LOG_WARNING, "null item\n");
4232 return;
4235 if (item->filename)
4236 free(item->filename);
4238 switch (item->type) {
4239 case PV_WORD:
4240 /* fields: item->u1.str == string associated with this (word). */
4241 if (item->u1.str )
4242 free(item->u1.str);
4243 if ( item->u2.arglist )
4244 destroy_pval(item->u2.arglist);
4245 break;
4247 case PV_MACRO:
4248 /* fields: item->u1.str == name of macro
4249 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
4250 item->u2.arglist->u1.str == argument
4251 item->u2.arglist->next == next arg
4253 item->u3.macro_statements == pval list of statements in macro body.
4255 destroy_pval(item->u2.arglist);
4256 if (item->u1.str )
4257 free(item->u1.str);
4258 destroy_pval(item->u3.macro_statements);
4259 break;
4261 case PV_CONTEXT:
4262 /* fields: item->u1.str == name of context
4263 item->u2.statements == pval list of statements in context body
4264 item->u3.abstract == int 1 if an abstract keyword were present
4266 if (item->u1.str)
4267 free(item->u1.str);
4268 destroy_pval(item->u2.statements);
4269 break;
4271 case PV_MACRO_CALL:
4272 /* fields: item->u1.str == name of macro to call
4273 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
4274 item->u2.arglist->u1.str == argument
4275 item->u2.arglist->next == next arg
4277 if (item->u1.str)
4278 free(item->u1.str);
4279 destroy_pval(item->u2.arglist);
4280 break;
4282 case PV_APPLICATION_CALL:
4283 /* fields: item->u1.str == name of application to call
4284 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
4285 item->u2.arglist->u1.str == argument
4286 item->u2.arglist->next == next arg
4288 if (item->u1.str)
4289 free(item->u1.str);
4290 destroy_pval(item->u2.arglist);
4291 break;
4293 case PV_CASE:
4294 /* fields: item->u1.str == value of case
4295 item->u2.statements == pval list of statements under the case
4297 if (item->u1.str)
4298 free(item->u1.str);
4299 destroy_pval(item->u2.statements);
4300 break;
4302 case PV_PATTERN:
4303 /* fields: item->u1.str == value of case
4304 item->u2.statements == pval list of statements under the case
4306 if (item->u1.str)
4307 free(item->u1.str);
4308 destroy_pval(item->u2.statements);
4309 break;
4311 case PV_DEFAULT:
4312 /* fields:
4313 item->u2.statements == pval list of statements under the case
4315 destroy_pval(item->u2.statements);
4316 break;
4318 case PV_CATCH:
4319 /* fields: item->u1.str == name of extension to catch
4320 item->u2.statements == pval list of statements in context body
4322 if (item->u1.str)
4323 free(item->u1.str);
4324 destroy_pval(item->u2.statements);
4325 break;
4327 case PV_SWITCHES:
4328 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4330 destroy_pval(item->u1.list);
4331 break;
4333 case PV_ESWITCHES:
4334 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4336 destroy_pval(item->u1.list);
4337 break;
4339 case PV_INCLUDES:
4340 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
4341 item->u2.arglist == pval list of 4 PV_WORD elements for time values
4343 destroy_pval(item->u1.list);
4344 break;
4346 case PV_STATEMENTBLOCK:
4347 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
4349 destroy_pval(item->u1.list);
4350 break;
4352 case PV_VARDEC:
4353 /* fields: item->u1.str == variable name
4354 item->u2.val == variable value to assign
4356 if (item->u1.str)
4357 free(item->u1.str);
4358 if (item->u2.val)
4359 free(item->u2.val);
4360 break;
4362 case PV_GOTO:
4363 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
4364 item->u1.list->u1.str == where the data on a PV_WORD will always be.
4367 destroy_pval(item->u1.list);
4368 break;
4370 case PV_LABEL:
4371 /* fields: item->u1.str == label name
4373 if (item->u1.str)
4374 free(item->u1.str);
4375 break;
4377 case PV_FOR:
4378 /* fields: item->u1.for_init == a string containing the initalizer
4379 item->u2.for_test == a string containing the loop test
4380 item->u3.for_inc == a string containing the loop increment
4382 item->u4.for_statements == a pval list of statements in the for ()
4384 if (item->u1.for_init)
4385 free(item->u1.for_init);
4386 if (item->u2.for_test)
4387 free(item->u2.for_test);
4388 if (item->u3.for_inc)
4389 free(item->u3.for_inc);
4390 destroy_pval(item->u4.for_statements);
4391 break;
4393 case PV_WHILE:
4394 /* fields: item->u1.str == the while conditional, as supplied by user
4396 item->u2.statements == a pval list of statements in the while ()
4398 if (item->u1.str)
4399 free(item->u1.str);
4400 destroy_pval(item->u2.statements);
4401 break;
4403 case PV_BREAK:
4404 /* fields: none
4406 break;
4408 case PV_RETURN:
4409 /* fields: none
4411 break;
4413 case PV_CONTINUE:
4414 /* fields: none
4416 break;
4418 case PV_IFTIME:
4419 /* fields: item->u1.list == the 4 time values, in PV_WORD structs, linked list
4421 item->u2.statements == a pval list of statements in the if ()
4422 item->u3.else_statements == a pval list of statements in the else
4423 (could be zero)
4425 destroy_pval(item->u1.list);
4426 destroy_pval(item->u2.statements);
4427 if (item->u3.else_statements) {
4428 destroy_pval(item->u3.else_statements);
4430 break;
4432 case PV_RANDOM:
4433 /* fields: item->u1.str == the random percentage, as supplied by user
4435 item->u2.statements == a pval list of statements in the true part ()
4436 item->u3.else_statements == a pval list of statements in the else
4437 (could be zero)
4438 fall thru to If */
4439 case PV_IF:
4440 /* fields: item->u1.str == the if conditional, as supplied by user
4442 item->u2.statements == a pval list of statements in the if ()
4443 item->u3.else_statements == a pval list of statements in the else
4444 (could be zero)
4446 if (item->u1.str)
4447 free(item->u1.str);
4448 destroy_pval(item->u2.statements);
4449 if (item->u3.else_statements) {
4450 destroy_pval(item->u3.else_statements);
4452 break;
4454 case PV_SWITCH:
4455 /* fields: item->u1.str == the switch expression
4457 item->u2.statements == a pval list of statements in the switch,
4458 (will be case statements, most likely!)
4460 if (item->u1.str)
4461 free(item->u1.str);
4462 destroy_pval(item->u2.statements);
4463 break;
4465 case PV_EXTENSION:
4466 /* fields: item->u1.str == the extension name, label, whatever it's called
4468 item->u2.statements == a pval list of statements in the extension
4469 item->u3.hints == a char * hint argument
4470 item->u4.regexten == an int boolean. non-zero says that regexten was specified
4472 if (item->u1.str)
4473 free(item->u1.str);
4474 if (item->u3.hints)
4475 free(item->u3.hints);
4476 destroy_pval(item->u2.statements);
4477 break;
4479 case PV_IGNOREPAT:
4480 /* fields: item->u1.str == the ignorepat data
4482 if (item->u1.str)
4483 free(item->u1.str);
4484 break;
4486 case PV_GLOBALS:
4487 /* fields: item->u1.statements == pval list of statements, usually vardecs
4489 destroy_pval(item->u1.statements);
4490 break;
4492 free(item);
4495 void destroy_pval(pval *item)
4497 pval *i,*nxt;
4499 for (i=item; i; i=nxt) {
4500 nxt = i->next;
4502 destroy_pval_item(i);
4506 #ifdef AAL_ARGCHECK
4507 static char *ael_funclist[] =
4509 "AGENT",
4510 "ARRAY",
4511 "BASE64_DECODE",
4512 "BASE64_ENCODE",
4513 "CALLERID",
4514 "CDR",
4515 "CHANNEL",
4516 "CHECKSIPDOMAIN",
4517 "CHECK_MD5",
4518 "CURL",
4519 "CUT",
4520 "DB",
4521 "DB_EXISTS",
4522 "DUNDILOOKUP",
4523 "ENUMLOOKUP",
4524 "ENV",
4525 "EVAL",
4526 "EXISTS",
4527 "FIELDQTY",
4528 "FILTER",
4529 "GROUP",
4530 "GROUP_COUNT",
4531 "GROUP_LIST",
4532 "GROUP_MATCH_COUNT",
4533 "IAXPEER",
4534 "IF",
4535 "IFTIME",
4536 "ISNULL",
4537 "KEYPADHASH",
4538 "LANGUAGE",
4539 "LEN",
4540 "MATH",
4541 "MD5",
4542 "MUSICCLASS",
4543 "QUEUEAGENTCOUNT",
4544 "QUEUE_MEMBER_COUNT",
4545 "QUEUE_MEMBER_LIST",
4546 "QUOTE",
4547 "RAND",
4548 "REGEX",
4549 "SET",
4550 "SHA1",
4551 "SIPCHANINFO",
4552 "SIPPEER",
4553 "SIP_HEADER",
4554 "SORT",
4555 "STAT",
4556 "STRFTIME",
4557 "STRPTIME",
4558 "TIMEOUT",
4559 "TXTCIDNAME",
4560 "URIDECODE",
4561 "URIENCODE",
4562 "VMCOUNT"
4566 int ael_is_funcname(char *name)
4568 int s,t;
4569 t = sizeof(ael_funclist)/sizeof(char*);
4570 s = 0;
4571 while ((s < t) && strcasecmp(name, ael_funclist[s]))
4572 s++;
4573 if ( s < t )
4574 return 1;
4575 else
4576 return 0;
4578 #endif