3 * Asterisk -- An open source telephony toolkit.
5 * Copyright (C) 2006, Digium, Inc.
7 * Steve Murphy <murf@parsetree.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <sys/types.h>
40 #include "asterisk/pbx.h"
41 #include "asterisk/config.h"
42 #include "asterisk/module.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/cli.h"
45 #include "asterisk/app.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/callerid.h"
48 #include "asterisk/pval.h"
49 #include "asterisk/ael_structs.h"
51 #include "asterisk/argdesc.h"
53 #include "asterisk/utils.h"
55 extern int localized_pbx_load_module(void);
57 static char expr_output[2096];
58 #define AST_PBX_MAX_STACK 128
60 /* these functions are in ../ast_expr2.fl */
62 static int errs, warns;
65 static int extensions_dot_conf_loaded = 0;
67 static char *registrar = "pbx_ael";
69 static pval *current_db;
70 static pval *current_context;
71 static pval *current_extension;
73 static const char *match_context;
74 static const char *match_exten;
75 static const char *match_label;
76 static int in_abstract_context;
77 static int count_labels; /* true, put matcher in label counting mode */
78 static int label_count; /* labels are only meant to be counted in a context or exten */
79 static int return_on_context_match;
80 static pval *last_matched_label;
81 struct pval *match_pval(pval *item);
82 static void check_timerange(pval *p);
83 static void check_dow(pval *DOW);
84 static void check_day(pval *DAY);
85 static void check_month(pval *MON);
86 static void check_expr2_input(pval *expr, char *str);
87 static int extension_matches(pval *here, const char *exten, const char *pattern);
88 static void check_goto(pval *item);
89 static void find_pval_goto_item(pval *item, int lev);
90 static void find_pval_gotos(pval *item, int lev);
91 static int check_break(pval *item);
92 static int check_continue(pval *item);
93 static void check_label(pval *item);
94 static void check_macro_returns(pval *macro);
96 static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
97 static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
98 static void print_pval_list(FILE *fin, pval *item, int depth);
100 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
101 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
102 static pval *get_goto_target(pval *item);
103 static int label_inside_case(pval *label);
104 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
105 static void fix_gotos_in_extensions(struct ael_extension *exten);
106 static pval *get_extension_or_contxt(pval *p);
107 static pval *get_contxt(pval *p);
108 static void remove_spaces_before_equals(char *str);
110 /* PRETTY PRINTER FOR AEL: ============================================================================= */
112 static void print_pval(FILE *fin, pval *item, int depth)
117 for (i=0; i<depth; i++) {
118 fprintf(fin, "\t"); /* depth == indentation */
121 switch ( item->type ) {
123 fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
127 fprintf(fin,"macro %s(", item->u1.str);
128 for (lp=item->u2.arglist; lp; lp=lp->next) {
129 if (lp != item->u2.arglist )
131 fprintf(fin,"%s", lp->u1.str);
133 fprintf(fin,") {\n");
134 print_pval_list(fin,item->u3.macro_statements,depth+1);
135 for (i=0; i<depth; i++) {
136 fprintf(fin,"\t"); /* depth == indentation */
138 fprintf(fin,"};\n\n");
142 if ( item->u3.abstract )
143 fprintf(fin,"abstract context %s {\n", item->u1.str);
145 fprintf(fin,"context %s {\n", item->u1.str);
146 print_pval_list(fin,item->u2.statements,depth+1);
147 for (i=0; i<depth; i++) {
148 fprintf(fin,"\t"); /* depth == indentation */
150 fprintf(fin,"};\n\n");
154 fprintf(fin,"&%s(", item->u1.str);
155 for (lp=item->u2.arglist; lp; lp=lp->next) {
156 if ( lp != item->u2.arglist )
158 fprintf(fin,"%s", lp->u1.str);
163 case PV_APPLICATION_CALL:
164 fprintf(fin,"%s(", item->u1.str);
165 for (lp=item->u2.arglist; lp; lp=lp->next) {
166 if ( lp != item->u2.arglist )
168 fprintf(fin,"%s", lp->u1.str);
174 fprintf(fin,"case %s:\n", item->u1.str);
175 print_pval_list(fin,item->u2.statements, depth+1);
179 fprintf(fin,"pattern %s:\n", item->u1.str);
180 print_pval_list(fin,item->u2.statements, depth+1);
184 fprintf(fin,"default:\n");
185 print_pval_list(fin,item->u2.statements, depth+1);
189 fprintf(fin,"catch %s {\n", item->u1.str);
190 print_pval_list(fin,item->u2.statements, depth+1);
191 for (i=0; i<depth; i++) {
192 fprintf(fin,"\t"); /* depth == indentation */
198 fprintf(fin,"switches {\n");
199 print_pval_list(fin,item->u1.list,depth+1);
200 for (i=0; i<depth; i++) {
201 fprintf(fin,"\t"); /* depth == indentation */
207 fprintf(fin,"eswitches {\n");
208 print_pval_list(fin,item->u1.list,depth+1);
209 for (i=0; i<depth; i++) {
210 fprintf(fin,"\t"); /* depth == indentation */
216 fprintf(fin,"includes {\n");
217 for (lp=item->u1.list; lp; lp=lp->next) {
218 for (i=0; i<depth+1; i++) {
219 fprintf(fin,"\t"); /* depth == indentation */
221 fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
223 fprintf(fin,"|%s|%s|%s|%s",
224 lp->u2.arglist->u1.str,
225 lp->u2.arglist->next->u1.str,
226 lp->u2.arglist->next->next->u1.str,
227 lp->u2.arglist->next->next->next->u1.str
229 fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
232 for (i=0; i<depth; i++) {
233 fprintf(fin,"\t"); /* depth == indentation */
238 case PV_STATEMENTBLOCK:
240 print_pval_list(fin,item->u1.list, depth+1);
241 for (i=0; i<depth; i++) {
242 fprintf(fin,"\t"); /* depth == indentation */
248 fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
252 fprintf(fin,"local %s=%s;\n", item->u1.str, item->u2.val);
256 fprintf(fin,"goto %s", item->u1.list->u1.str);
257 if ( item->u1.list->next )
258 fprintf(fin,",%s", item->u1.list->next->u1.str);
259 if ( item->u1.list->next && item->u1.list->next->next )
260 fprintf(fin,",%s", item->u1.list->next->next->u1.str);
265 fprintf(fin,"%s:\n", item->u1.str);
269 fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
270 print_pval_list(fin,item->u4.for_statements,depth+1);
274 fprintf(fin,"while (%s)\n", item->u1.str);
275 print_pval_list(fin,item->u2.statements,depth+1);
279 fprintf(fin,"break;\n");
283 fprintf(fin,"return;\n");
287 fprintf(fin,"continue;\n");
293 if ( item->type == PV_IFTIME ) {
295 fprintf(fin,"ifTime ( %s|%s|%s|%s )\n",
296 item->u1.list->u1.str,
297 item->u1.list->next->u1.str,
298 item->u1.list->next->next->u1.str,
299 item->u1.list->next->next->next->u1.str
301 } else if ( item->type == PV_RANDOM ) {
302 fprintf(fin,"random ( %s )\n", item->u1.str );
304 fprintf(fin,"if ( %s )\n", item->u1.str);
305 if ( item->u2.statements && item->u2.statements->next ) {
306 for (i=0; i<depth; i++) {
307 fprintf(fin,"\t"); /* depth == indentation */
310 print_pval_list(fin,item->u2.statements,depth+1);
311 for (i=0; i<depth; i++) {
312 fprintf(fin,"\t"); /* depth == indentation */
314 if ( item->u3.else_statements )
318 } else if (item->u2.statements ) {
319 print_pval_list(fin,item->u2.statements,depth+1);
321 if (item->u3.else_statements )
322 fprintf(fin, " {} ");
324 fprintf(fin, " {}; ");
326 if ( item->u3.else_statements ) {
327 for (i=0; i<depth; i++) {
328 fprintf(fin,"\t"); /* depth == indentation */
330 fprintf(fin,"else\n");
331 print_pval_list(fin,item->u3.else_statements, depth);
336 fprintf(fin,"switch( %s ) {\n", item->u1.str);
337 print_pval_list(fin,item->u2.statements,depth+1);
338 for (i=0; i<depth; i++) {
339 fprintf(fin,"\t"); /* depth == indentation */
345 if ( item->u4.regexten )
346 fprintf(fin, "regexten ");
347 if ( item->u3.hints )
348 fprintf(fin,"hints(%s) ", item->u3.hints);
350 fprintf(fin,"%s => ", item->u1.str);
351 print_pval_list(fin,item->u2.statements,depth+1);
356 fprintf(fin,"ignorepat => %s;\n", item->u1.str);
360 fprintf(fin,"globals {\n");
361 print_pval_list(fin,item->u1.statements,depth+1);
362 for (i=0; i<depth; i++) {
363 fprintf(fin,"\t"); /* depth == indentation */
370 static void print_pval_list(FILE *fin, pval *item, int depth)
374 for (i=item; i; i=i->next) {
375 print_pval(fin, i, depth);
379 void ael2_print(char *fname, pval *tree)
381 FILE *fin = fopen(fname,"w");
383 ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
386 print_pval_list(fin, tree, 0);
391 /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL: ============================================================================= */
393 void traverse_pval_template(pval *item, int depth);
394 void traverse_pval_item_template(pval *item, int depth);
397 void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
398 but you may not need it */
402 switch ( item->type ) {
404 /* fields: item->u1.str == string associated with this (word). */
408 /* fields: item->u1.str == name of macro
409 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
410 item->u2.arglist->u1.str == argument
411 item->u2.arglist->next == next arg
413 item->u3.macro_statements == pval list of statements in macro body.
415 for (lp=item->u2.arglist; lp; lp=lp->next) {
418 traverse_pval_item_template(item->u3.macro_statements,depth+1);
422 /* fields: item->u1.str == name of context
423 item->u2.statements == pval list of statements in context body
424 item->u3.abstract == int 1 if an abstract keyword were present
426 traverse_pval_item_template(item->u2.statements,depth+1);
430 /* fields: item->u1.str == name of macro to call
431 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
432 item->u2.arglist->u1.str == argument
433 item->u2.arglist->next == next arg
435 for (lp=item->u2.arglist; lp; lp=lp->next) {
439 case PV_APPLICATION_CALL:
440 /* fields: item->u1.str == name of application to call
441 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
442 item->u2.arglist->u1.str == argument
443 item->u2.arglist->next == next arg
445 for (lp=item->u2.arglist; lp; lp=lp->next) {
450 /* fields: item->u1.str == value of case
451 item->u2.statements == pval list of statements under the case
453 traverse_pval_item_template(item->u2.statements,depth+1);
457 /* fields: item->u1.str == value of case
458 item->u2.statements == pval list of statements under the case
460 traverse_pval_item_template(item->u2.statements,depth+1);
465 item->u2.statements == pval list of statements under the case
467 traverse_pval_item_template(item->u2.statements,depth+1);
471 /* fields: item->u1.str == name of extension to catch
472 item->u2.statements == pval list of statements in context body
474 traverse_pval_item_template(item->u2.statements,depth+1);
478 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
480 traverse_pval_item_template(item->u1.list,depth+1);
484 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
486 traverse_pval_item_template(item->u1.list,depth+1);
490 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
491 item->u2.arglist == pval list of 4 PV_WORD elements for time values
493 traverse_pval_item_template(item->u1.list,depth+1);
494 traverse_pval_item_template(item->u2.arglist,depth+1);
497 case PV_STATEMENTBLOCK:
498 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
500 traverse_pval_item_template(item->u1.list,depth+1);
505 /* fields: item->u1.str == variable name
506 item->u2.val == variable value to assign
511 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
512 item->u1.list->u1.str == where the data on a PV_WORD will always be.
515 if ( item->u1.list->next )
517 if ( item->u1.list->next && item->u1.list->next->next )
523 /* fields: item->u1.str == label name
528 /* fields: item->u1.for_init == a string containing the initalizer
529 item->u2.for_test == a string containing the loop test
530 item->u3.for_inc == a string containing the loop increment
532 item->u4.for_statements == a pval list of statements in the for ()
534 traverse_pval_item_template(item->u4.for_statements,depth+1);
538 /* fields: item->u1.str == the while conditional, as supplied by user
540 item->u2.statements == a pval list of statements in the while ()
542 traverse_pval_item_template(item->u2.statements,depth+1);
561 /* fields: item->u1.list == there are 4 linked PV_WORDs here.
563 item->u2.statements == a pval list of statements in the if ()
564 item->u3.else_statements == a pval list of statements in the else
567 traverse_pval_item_template(item->u2.statements,depth+1);
568 if ( item->u3.else_statements ) {
569 traverse_pval_item_template(item->u3.else_statements,depth+1);
574 /* fields: item->u1.str == the random number expression, as supplied by user
576 item->u2.statements == a pval list of statements in the if ()
577 item->u3.else_statements == a pval list of statements in the else
580 traverse_pval_item_template(item->u2.statements,depth+1);
581 if ( item->u3.else_statements ) {
582 traverse_pval_item_template(item->u3.else_statements,depth+1);
587 /* fields: item->u1.str == the if conditional, as supplied by user
589 item->u2.statements == a pval list of statements in the if ()
590 item->u3.else_statements == a pval list of statements in the else
593 traverse_pval_item_template(item->u2.statements,depth+1);
594 if ( item->u3.else_statements ) {
595 traverse_pval_item_template(item->u3.else_statements,depth+1);
600 /* fields: item->u1.str == the switch expression
602 item->u2.statements == a pval list of statements in the switch,
603 (will be case statements, most likely!)
605 traverse_pval_item_template(item->u2.statements,depth+1);
609 /* fields: item->u1.str == the extension name, label, whatever it's called
611 item->u2.statements == a pval list of statements in the extension
612 item->u3.hints == a char * hint argument
613 item->u4.regexten == an int boolean. non-zero says that regexten was specified
615 traverse_pval_item_template(item->u2.statements,depth+1);
619 /* fields: item->u1.str == the ignorepat data
624 /* fields: item->u1.statements == pval list of statements, usually vardecs
626 traverse_pval_item_template(item->u1.statements,depth+1);
631 void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
632 but you may not need it */
636 for (i=item; i; i=i->next) {
637 traverse_pval_item_template(i, depth);
642 /* SEMANTIC CHECKING FOR AEL: ============================================================================= */
644 /* (not all that is syntactically legal is good! */
647 static void check_macro_returns(pval *macro)
650 if (!macro->u3.macro_statements)
652 pval *z = calloc(1, sizeof(struct pval));
653 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s is empty! I will insert a return.\n",
654 macro->filename, macro->startline, macro->endline, macro->u1.str);
657 z->startline = macro->startline;
658 z->endline = macro->endline;
659 z->startcol = macro->startcol;
660 z->endcol = macro->endcol;
661 z->filename = strdup(macro->filename);
663 macro->u3.macro_statements = z;
666 for (i=macro->u3.macro_statements; i; i=i->next) {
667 /* if the last statement in the list is not return, then insert a return there */
668 if (i->next == NULL) {
669 if (i->type != PV_RETURN) {
670 pval *z = calloc(1, sizeof(struct pval));
671 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s does not end with a return; I will insert one.\n",
672 macro->filename, macro->startline, macro->endline, macro->u1.str);
675 z->startline = macro->startline;
676 z->endline = macro->endline;
677 z->startcol = macro->startcol;
678 z->endcol = macro->endcol;
679 z->filename = strdup(macro->filename);
691 static int extension_matches(pval *here, const char *exten, const char *pattern)
696 /* simple case, they match exactly, the pattern and exten name */
697 if (!strcmp(pattern,exten) == 0)
700 if (pattern[0] == '_') {
705 if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
706 ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
710 /* form a regular expression from the pattern, and then match it against exten */
711 *r++ = '^'; /* what if the extension is a pattern ?? */
712 *r++ = '_'; /* what if the extension is a pattern ?? */
714 for (p=pattern+1; *p; p++) {
744 while ( *p && *p != ']' ) {
748 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
749 here->filename, here->startline, here->endline, pattern);
768 *r++ = '$'; /* what if the extension is a pattern ?? */
769 *r++ = *p++; /* put in the closing null */
770 err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
773 regerror(err1,&preg,errmess,sizeof(errmess));
775 ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
779 err1 = regexec(&preg, exten, 0, 0, 0);
783 /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
784 err1,exten, pattern, reg1); */
785 return 0; /* no match */
787 /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
794 if ( strcmp(exten,pattern) == 0 ) {
802 static void check_expr2_input(pval *expr, char *str)
804 int spaces = strspn(str,"\t \n");
805 if ( !strncmp(str+spaces,"$[",2) ) {
806 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
807 expr->filename, expr->startline, expr->endline, str);
812 static void check_includes(pval *includes)
815 for (p4=includes->u1.list; p4; p4=p4->next) {
816 /* for each context pointed to, find it, then find a context/label that matches the
818 char *incl_context = p4->u1.str;
819 /* find a matching context name */
820 struct pval *that_other_context = find_context(incl_context);
821 if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
822 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n\
823 (You may ignore this warning if '%s' exists in extensions.conf, or is created by another module. I cannot check for those.)\n",
824 includes->filename, includes->startline, includes->endline, incl_context, incl_context);
831 static void check_timerange(pval *p)
838 times = ast_strdupa(p->u1.str);
840 /* Star is all times */
841 if (ast_strlen_zero(times) || !strcmp(times, "*")) {
844 /* Otherwise expect a range */
845 e = strchr(times, '-');
847 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",
848 p->filename, p->startline, p->endline, times);
854 while (*e && !isdigit(*e))
857 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
858 p->filename, p->startline, p->endline, p->u1.str);
861 if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
862 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
863 p->filename, p->startline, p->endline, times);
866 if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
867 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
868 p->filename, p->startline, p->endline, times);
873 if ((s1 < 0) || (s1 >= 24*30)) {
874 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
875 p->filename, p->startline, p->endline, times);
879 if ((e1 < 0) || (e1 >= 24*30)) {
880 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
881 p->filename, p->startline, p->endline, e);
887 static char *days[] =
898 /*! \brief get_dow: Get day of week */
899 static void check_dow(pval *DOW)
903 /* The following line is coincidence, really! */
906 dow = ast_strdupa(DOW->u1.str);
908 /* Check for all days */
909 if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
911 /* Get start and ending days */
912 c = strchr(dow, '-');
920 while ((s < 7) && strcasecmp(dow, days[s])) s++;
922 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",
923 DOW->filename, DOW->startline, DOW->endline, dow);
928 while ((e < 7) && strcasecmp(c, days[e])) e++;
930 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",
931 DOW->filename, DOW->startline, DOW->endline, c);
938 static void check_day(pval *DAY)
942 /* The following line is coincidence, really! */
945 day = ast_strdupa(DAY->u1.str);
947 /* Check for all days */
948 if (ast_strlen_zero(day) || !strcmp(day, "*")) {
951 /* Get start and ending days */
952 c = strchr(day, '-');
958 if (sscanf(day, "%d", &s) != 1) {
959 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
960 DAY->filename, DAY->startline, DAY->endline, day);
963 else if ((s < 1) || (s > 31)) {
964 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",
965 DAY->filename, DAY->startline, DAY->endline, day);
970 if (sscanf(c, "%d", &e) != 1) {
971 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
972 DAY->filename, DAY->startline, DAY->endline, c);
975 else if ((e < 1) || (e > 31)) {
976 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",
977 DAY->filename, DAY->startline, DAY->endline, day);
985 static char *months[] =
1001 static void check_month(pval *MON)
1005 /* The following line is coincidence, really! */
1008 mon = ast_strdupa(MON->u1.str);
1010 /* Check for all days */
1011 if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
1013 /* Get start and ending days */
1014 c = strchr(mon, '-');
1019 /* Find the start */
1021 while ((s < 12) && strcasecmp(mon, months[s])) s++;
1023 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1024 MON->filename, MON->startline, MON->endline, mon);
1029 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1031 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1032 MON->filename, MON->startline, MON->endline, c);
1039 static int check_break(pval *item)
1043 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1044 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1046 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
1047 || p->type == PV_WHILE || p->type == PV_FOR ) {
1052 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1053 item->filename, item->startline, item->endline);
1059 static int check_continue(pval *item)
1063 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1064 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1066 if( p->type == PV_WHILE || p->type == PV_FOR ) {
1071 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1072 item->filename, item->startline, item->endline);
1078 static struct pval *in_macro(pval *item)
1083 if( curr->type == PV_MACRO ) {
1091 static struct pval *in_context(pval *item)
1096 if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
1105 /* general purpose goto finder */
1107 static void check_label(pval *item)
1113 /* A label outside an extension just plain does not make sense! */
1118 if( curr->type == PV_MACRO || curr->type == PV_EXTENSION ) {
1126 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
1127 item->filename, item->startline, item->endline, item->u1.str);
1132 /* basically, ensure that a label is not repeated in a context. Period.
1133 The method: well, for each label, find the first label in the context
1134 with the same name. If it's not the current label, then throw an error. */
1137 /* printf("==== check_label: ====\n"); */
1138 if( !current_extension )
1139 curr = current_context;
1141 curr = current_extension;
1143 x = find_first_label_in_current_context((char *)item->u1.str, curr);
1144 /* 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); */
1145 if( x && x != item )
1147 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1148 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1151 /* printf("<<<<< check_label: ====\n"); */
1154 static pval *get_goto_target(pval *item)
1156 /* just one item-- the label should be in the current extension */
1157 pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1160 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1161 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1165 curr_cont = get_contxt(item);
1168 if (item->u1.list->next && !item->u1.list->next->next) {
1169 if (!strstr((item->u1.list)->u1.str,"${")
1170 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1171 struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1177 if (item->u1.list->next && item->u1.list->next->next) {
1179 pval *first = item->u1.list;
1180 pval *second = item->u1.list->next;
1181 pval *third = item->u1.list->next->next;
1183 if (!strstr((item->u1.list)->u1.str,"${")
1184 && !strstr(item->u1.list->next->u1.str,"${")
1185 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1186 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1190 struct pval *that_context = find_context(item->u1.list->u1.str);
1192 /* the target of the goto could be in an included context!! Fancy that!! */
1193 /* look for includes in the current context */
1195 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1196 if (p3->type == PV_INCLUDES) {
1198 for (p4=p3->u1.list; p4; p4=p4->next) {
1199 /* for each context pointed to, find it, then find a context/label that matches the
1201 char *incl_context = p4->u1.str;
1202 /* find a matching context name */
1203 struct pval *that_other_context = find_context(incl_context);
1204 if (that_other_context) {
1206 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1222 static void check_goto(pval *item)
1224 /* check for the target of the goto-- does it exist? */
1225 if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1226 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
1227 item->filename, item->startline, item->endline);
1231 /* just one item-- the label should be in the current extension */
1233 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1234 struct pval *z = get_extension_or_contxt(item);
1237 x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1238 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1239 (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1241 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
1242 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1250 if (item->u1.list->next && !item->u1.list->next->next) {
1252 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1253 (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1254 if (!strstr((item->u1.list)->u1.str,"${")
1255 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1256 struct pval *z = get_contxt(item);
1260 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1263 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",
1264 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1273 if (item->u1.list->next && item->u1.list->next->next) {
1275 pval *first = item->u1.list;
1276 pval *second = item->u1.list->next;
1277 pval *third = item->u1.list->next->next;
1279 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1280 (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1281 if (!strstr((item->u1.list)->u1.str,"${")
1282 && !strstr(item->u1.list->next->u1.str,"${")
1283 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1284 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1287 struct pval *found = 0;
1288 struct pval *that_context = find_context(item->u1.list->u1.str);
1290 /* the target of the goto could be in an included context!! Fancy that!! */
1291 /* look for includes in the current context */
1293 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1294 if (p3->type == PV_INCLUDES) {
1296 for (p4=p3->u1.list; p4; p4=p4->next) {
1297 /* for each context pointed to, find it, then find a context/label that matches the
1299 char *incl_context = p4->u1.str;
1300 /* find a matching context name */
1301 struct pval *that_other_context = find_context(incl_context);
1302 if (that_other_context) {
1304 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1314 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
1315 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1318 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1319 if( mac ) { /* yes! */
1320 struct pval *targ = in_context(found);
1323 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
1324 item->filename, item->startline, item->endline);
1330 /* here is where code would go to check for target existence in extensions.conf files */
1332 struct pbx_find_info pfiq = {.stacklen = 0 };
1333 extern int localized_pbx_load_module(void);
1334 /* if this is a standalone, we will need to make sure the
1335 localized load of extensions.conf is done */
1336 if (!extensions_dot_conf_loaded) {
1337 localized_pbx_load_module();
1338 extensions_dot_conf_loaded++;
1341 pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
1342 atoi(third->u1.str) ? NULL : third->u1.str, NULL,
1343 atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
1345 if (pfiq.status != STATUS_SUCCESS) {
1346 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: Couldn't find goto target %s|%s|%s, not even in extensions.conf!\n",
1347 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1351 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: Couldn't find goto target %s|%s|%s in the AEL code!\n",
1352 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1357 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1358 if( mac ) { /* yes! */
1359 struct pval *targ = in_context(x);
1362 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
1363 item->filename, item->startline, item->endline);
1373 static void find_pval_goto_item(pval *item, int lev)
1377 ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
1381 switch ( item->type ) {
1383 /* fields: item->u1.str == name of macro
1384 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1385 item->u2.arglist->u1.str == argument
1386 item->u2.arglist->next == next arg
1388 item->u3.macro_statements == pval list of statements in macro body.
1391 /* printf("Descending into matching macro %s\n", match_context); */
1392 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1397 /* fields: item->u1.str == name of context
1398 item->u2.statements == pval list of statements in context body
1399 item->u3.abstract == int 1 if an abstract keyword were present
1404 /* fields: item->u1.str == value of case
1405 item->u2.statements == pval list of statements under the case
1407 find_pval_gotos(item->u2.statements,lev+1);
1411 /* fields: item->u1.str == value of case
1412 item->u2.statements == pval list of statements under the case
1414 find_pval_gotos(item->u2.statements,lev+1);
1419 item->u2.statements == pval list of statements under the case
1421 find_pval_gotos(item->u2.statements,lev+1);
1425 /* fields: item->u1.str == name of extension to catch
1426 item->u2.statements == pval list of statements in context body
1428 find_pval_gotos(item->u2.statements,lev+1);
1431 case PV_STATEMENTBLOCK:
1432 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1434 find_pval_gotos(item->u1.list,lev+1);
1438 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1439 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1441 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1445 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1447 for (p4=item->u1.list; p4; p4=p4->next) {
1448 /* for each context pointed to, find it, then find a context/label that matches the
1450 char *incl_context = p4->u1.str;
1451 /* find a matching context name */
1452 struct pval *that_context = find_context(incl_context);
1454 find_pval_gotos(that_context,lev+1); /* keep working up the includes */
1460 /* fields: item->u1.for_init == a string containing the initalizer
1461 item->u2.for_test == a string containing the loop test
1462 item->u3.for_inc == a string containing the loop increment
1464 item->u4.for_statements == a pval list of statements in the for ()
1466 find_pval_gotos(item->u4.for_statements,lev+1);
1470 /* fields: item->u1.str == the while conditional, as supplied by user
1472 item->u2.statements == a pval list of statements in the while ()
1474 find_pval_gotos(item->u2.statements,lev+1);
1478 /* fields: item->u1.str == the random number expression, as supplied by user
1480 item->u2.statements == a pval list of statements in the if ()
1481 item->u3.else_statements == a pval list of statements in the else
1483 fall thru to PV_IF */
1486 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1488 item->u2.statements == a pval list of statements in the if ()
1489 item->u3.else_statements == a pval list of statements in the else
1491 fall thru to PV_IF*/
1493 /* fields: item->u1.str == the if conditional, as supplied by user
1495 item->u2.statements == a pval list of statements in the if ()
1496 item->u3.else_statements == a pval list of statements in the else
1499 find_pval_gotos(item->u2.statements,lev+1);
1501 if (item->u3.else_statements) {
1502 find_pval_gotos(item->u3.else_statements,lev+1);
1507 /* fields: item->u1.str == the switch expression
1509 item->u2.statements == a pval list of statements in the switch,
1510 (will be case statements, most likely!)
1512 find_pval_gotos(item->u3.else_statements,lev+1);
1516 /* fields: item->u1.str == the extension name, label, whatever it's called
1518 item->u2.statements == a pval list of statements in the extension
1519 item->u3.hints == a char * hint argument
1520 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1523 find_pval_gotos(item->u2.statements,lev+1);
1531 static void find_pval_gotos(pval *item,int lev)
1535 for (i=item; i; i=i->next) {
1537 find_pval_goto_item(i, lev);
1543 /* general purpose label finder */
1544 static struct pval *match_pval_item(pval *item)
1548 switch ( item->type ) {
1550 /* fields: item->u1.str == name of macro
1551 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1552 item->u2.arglist->u1.str == argument
1553 item->u2.arglist->next == next arg
1555 item->u3.macro_statements == pval list of statements in macro body.
1557 /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1558 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1560 /* printf("MACRO: match context is: %s\n", match_context); */
1562 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 */ {
1563 /* printf("Returning on matching macro %s\n", match_context); */
1568 if (!return_on_context_match) {
1569 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1570 if ((x=match_pval(item->u3.macro_statements))) {
1571 /* printf("Responded with pval match %x\n", x); */
1576 /* printf("Skipping context/macro %s\n", item->u1.str); */
1582 /* fields: item->u1.str == name of context
1583 item->u2.statements == pval list of statements in context body
1584 item->u3.abstract == int 1 if an abstract keyword were present
1586 /* printf(" matching in CONTEXT\n"); */
1587 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1588 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1589 /* printf("Returning on matching context %s\n", match_context); */
1590 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1594 if (!return_on_context_match ) {
1595 /* printf("Descending into matching context %s\n", match_context); */
1596 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1597 /* printf("CONTEXT: Responded with pval match %x\n", x); */
1602 /* printf("Skipping context/macro %s\n", item->u1.str); */
1607 /* fields: item->u1.str == value of case
1608 item->u2.statements == pval list of statements under the case
1610 /* printf(" matching in CASE\n"); */
1611 if ((x=match_pval(item->u2.statements))) {
1612 /* printf("CASE: Responded with pval match %x\n", x); */
1618 /* fields: item->u1.str == value of case
1619 item->u2.statements == pval list of statements under the case
1621 /* printf(" matching in PATTERN\n"); */
1622 if ((x=match_pval(item->u2.statements))) {
1623 /* printf("PATTERN: Responded with pval match %x\n", x); */
1630 item->u2.statements == pval list of statements under the case
1632 /* printf(" matching in DEFAULT\n"); */
1633 if ((x=match_pval(item->u2.statements))) {
1634 /* printf("DEFAULT: Responded with pval match %x\n", x); */
1640 /* fields: item->u1.str == name of extension to catch
1641 item->u2.statements == pval list of statements in context body
1643 /* printf(" matching in CATCH\n"); */
1644 if ((x=match_pval(item->u2.statements))) {
1645 /* printf("CATCH: Responded with pval match %x\n", x); */
1650 case PV_STATEMENTBLOCK:
1651 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1653 /* printf(" matching in STATEMENTBLOCK\n"); */
1654 if ((x=match_pval(item->u1.list))) {
1655 /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1661 /* fields: item->u1.str == label name
1663 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1664 item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1667 if (!strcmp(match_label, item->u1.str)) {
1669 last_matched_label = item;
1673 if (!strcmp(match_label, item->u1.str)) {
1674 /* printf("LABEL: Responded with pval match %x\n", x); */
1681 /* fields: item->u1.for_init == a string containing the initalizer
1682 item->u2.for_test == a string containing the loop test
1683 item->u3.for_inc == a string containing the loop increment
1685 item->u4.for_statements == a pval list of statements in the for ()
1687 /* printf(" matching in FOR\n"); */
1688 if ((x=match_pval(item->u4.for_statements))) {
1689 /* printf("FOR: Responded with pval match %x\n", x);*/
1695 /* fields: item->u1.str == the while conditional, as supplied by user
1697 item->u2.statements == a pval list of statements in the while ()
1699 /* printf(" matching in WHILE\n"); */
1700 if ((x=match_pval(item->u2.statements))) {
1701 /* printf("WHILE: Responded with pval match %x\n", x); */
1707 /* fields: item->u1.str == the random number expression, as supplied by user
1709 item->u2.statements == a pval list of statements in the if ()
1710 item->u3.else_statements == a pval list of statements in the else
1712 fall thru to PV_IF */
1715 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1717 item->u2.statements == a pval list of statements in the if ()
1718 item->u3.else_statements == a pval list of statements in the else
1720 fall thru to PV_IF*/
1722 /* fields: item->u1.str == the if conditional, as supplied by user
1724 item->u2.statements == a pval list of statements in the if ()
1725 item->u3.else_statements == a pval list of statements in the else
1728 /* printf(" matching in IF/IFTIME/RANDOM\n"); */
1729 if ((x=match_pval(item->u2.statements))) {
1732 if (item->u3.else_statements) {
1733 if ((x=match_pval(item->u3.else_statements))) {
1734 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1741 /* fields: item->u1.str == the switch expression
1743 item->u2.statements == a pval list of statements in the switch,
1744 (will be case statements, most likely!)
1746 /* printf(" matching in SWITCH\n"); */
1747 if ((x=match_pval(item->u2.statements))) {
1748 /* printf("SWITCH: Responded with pval match %x\n", x); */
1754 /* fields: item->u1.str == the extension name, label, whatever it's called
1756 item->u2.statements == a pval list of statements in the extension
1757 item->u3.hints == a char * hint argument
1758 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1760 /* printf(" matching in EXTENSION\n"); */
1761 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1762 /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1763 if (strcmp(match_label,"1") == 0) {
1764 if (item->u2.statements) {
1765 struct pval *p5 = item->u2.statements;
1766 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1777 if ((x=match_pval(item->u2.statements))) {
1778 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1782 /* printf("Skipping exten %s\n", item->u1.str); */
1786 /* printf(" matching in default = %d\n", item->type); */
1792 struct pval *match_pval(pval *item)
1796 for (i=item; i; i=i->next) {
1798 /* printf(" -- match pval: item %d\n", i->type); */
1800 if ((x = match_pval_item(i))) {
1801 /* printf("match_pval: returning x=%x\n", (int)x); */
1802 return x; /* cut the search short */
1809 int count_labels_in_current_context(char *label)
1813 return_on_context_match = 0;
1814 match_pval(current_context->u2.statements);
1820 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1822 /* printf(" --- Got args %s, %s\n", exten, label); */
1827 return_on_context_match = 0;
1828 match_context = "*";
1830 match_label = label;
1832 ret = match_pval(curr_cont);
1836 /* the target of the goto could be in an included context!! Fancy that!! */
1837 /* look for includes in the current context */
1838 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1839 if (p3->type == PV_INCLUDES) {
1841 for (p4=p3->u1.list; p4; p4=p4->next) {
1842 /* for each context pointed to, find it, then find a context/label that matches the
1844 char *incl_context = p4->u1.str;
1845 /* find a matching context name */
1846 struct pval *that_context = find_context(incl_context);
1849 x3 = find_first_label_in_current_context(label, that_context);
1860 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1862 /* printf(" --- Got args %s, %s\n", exten, label); */
1867 return_on_context_match = 0;
1868 match_context = "*";
1869 match_exten = exten;
1870 match_label = label;
1871 ret = match_pval(curr_cont->u2.statements);
1875 /* the target of the goto could be in an included context!! Fancy that!! */
1876 /* look for includes in the current context */
1877 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1878 if (p3->type == PV_INCLUDES) {
1880 for (p4=p3->u1.list; p4; p4=p4->next) {
1881 /* for each context pointed to, find it, then find a context/label that matches the
1883 char *incl_context = p4->u1.str;
1884 /* find a matching context name */
1885 struct pval *that_context = find_context(incl_context);
1888 x3 = find_label_in_current_context(exten, label, that_context);
1899 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1901 /* printf(" --- Got args %s\n", label); */
1903 return_on_context_match = 0;
1904 match_context = "*";
1906 match_label = label;
1907 return match_pval(curr_ext);
1910 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1912 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1914 return_on_context_match = 0;
1916 match_context = context;
1917 match_exten = exten;
1918 match_label = label;
1920 return match_pval(current_db);
1924 struct pval *find_macro(char *name)
1926 return_on_context_match = 1;
1928 match_context = name;
1929 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1931 return match_pval(current_db);
1934 struct pval *find_context(char *name)
1936 return_on_context_match = 1;
1938 match_context = name;
1939 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1941 return match_pval(current_db);
1944 int is_float(char *arg )
1947 for (s=arg; *s; s++) {
1948 if (*s != '.' && (*s < '0' || *s > '9'))
1953 int is_int(char *arg )
1956 for (s=arg; *s; s++) {
1957 if (*s < '0' || *s > '9')
1962 int is_empty(char *arg)
1969 if (*arg != ' ' && *arg != '\t')
1977 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1979 struct argchoice *ac;
1982 switch (should->dtype) {
1983 case ARGD_OPTIONSET:
1984 if ( strstr(is->u1.str,"${") )
1985 return 0; /* no checking anything if there's a var reference in there! */
1987 opcop = ast_strdupa(is->u1.str);
1989 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
1992 while (*p && *p != ')' )
1998 for (ac=app->opts; ac; ac=ac->next) {
1999 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2002 for (ac=app->opts; ac; ac=ac->next) {
2003 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2004 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2006 if (p && *p == 'j') {
2007 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
2008 is->filename, is->startline, is->endline, app->name);
2014 if (ac->name[1] == '(') {
2015 if (*(p+1) != '(') {
2016 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",
2017 is->filename, is->startline, is->endline, ac->name[0], app->name);
2024 for (q=opcop; *q; q++) {
2025 if ( *q != '+' && *q != '(' && *q != ')') {
2026 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",
2027 is->filename, is->startline, is->endline, *q, app->name);
2039 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2041 struct argchoice *ac;
2044 switch (should->dtype) {
2046 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2048 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2053 if (is_int(is->u1.str))
2060 if (is_float(is->u1.str))
2067 if( !is->u1.str || strlen(is->u1.str) == 0 )
2068 return 1; /* a null arg in the call will match an enum, I guess! */
2069 for (ac=should->choices; ac; ac=ac->next) {
2070 if (strcmp(ac->name,is->u1.str) == 0)
2076 case ARGD_OPTIONSET:
2077 opcop = ast_strdupa(is->u1.str);
2079 for (ac=app->opts; ac; ac=ac->next) {
2080 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2083 for (ac=app->opts; ac; ac=ac->next) {
2084 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2085 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2089 if (ac->name[1] == '(') {
2090 if (*(p+1) == '(') {
2092 while (*q && *q != ')') {
2104 return 1; /* matches anything */
2107 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2111 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2114 struct argdesc *ad = app->args;
2118 for (pa = arglist; pa; pa=pa->next) {
2120 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2121 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2125 /* find the first entry in the ad list that will match */
2127 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2130 z= option_matches( ad, pa, app);
2135 if (ad->type == ARGD_REQUIRED) {
2136 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2137 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2141 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2142 option_matches_j( ad, pa, app);
2148 /* any app nodes left, that are not optional? */
2149 for ( ; ad; ad=ad->next) {
2150 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2153 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2154 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2165 void check_switch_expr(pval *item, struct argapp *apps)
2168 /* get and clean the variable name */
2170 struct argapp *a,*a2;
2171 struct appsetvar *v,*v2;
2172 struct argchoice *c;
2176 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2179 buff1 = ast_strdupa(p);
2181 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2182 buff1[strlen(buff1)-1] = 0;
2183 /* buff1 now contains the variable name */
2185 for (a=apps; a; a=a->next) {
2186 for (v=a->setvars;v;v=v->next) {
2187 if (strcmp(v->name,buff1) == 0) {
2195 /* we have a match, to a variable that has a set of determined values */
2200 /* first of all, does this switch have a default case ? */
2201 for (t=item->u2.statements; t; t=t->next) {
2202 if (t->type == PV_DEFAULT) {
2206 if (t->type == PV_PATTERN) {
2210 if (def || pat) /* nothing to check. All cases accounted for! */
2212 for (c=v->vals; c; c=c->next) {
2214 for (t=item->u2.statements; t; t=t->next) {
2215 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2216 if (!strcmp(t->u1.str,c->name)) {
2223 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2224 item->filename, item->startline, item->endline, item->u1.str, c->name);
2228 /* next, is there an app call in the current exten, that would set this var? */
2230 t = current_extension->u2.statements;
2231 if ( t && t->type == PV_STATEMENTBLOCK )
2232 t = t->u1.statements;
2233 for (; t && t != item; t=t->next) {
2234 if (t->type == PV_APPLICATION_CALL) {
2235 /* find the application that matches the u1.str */
2236 for (a2=apps; a2; a2=a2->next) {
2237 if (strcasecmp(a2->name, t->u1.str)==0) {
2238 for (v2=a2->setvars; v2; v2=v2->next) {
2239 if (strcmp(v2->name, buff1) == 0) {
2240 /* found an app that sets the var */
2254 /* see if it sets the var */
2256 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",
2257 item->filename, item->startline, item->endline, item->u1.str);
2265 /* first of all, does this switch have a default case ? */
2266 for (t=item->u2.statements; t; t=t->next) {
2267 if (t->type == PV_DEFAULT) {
2273 if (def) /* nothing to check. All cases accounted for! */
2275 /* if no default, warn and insert a default case at the end */
2276 p2 = tl->next = calloc(1, sizeof(struct pval));
2278 p2->type = PV_DEFAULT;
2279 p2->startline = tl->startline;
2280 p2->endline = tl->endline;
2281 p2->startcol = tl->startcol;
2282 p2->endcol = tl->endcol;
2283 p2->filename = strdup(tl->filename);
2284 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2285 p2->filename, p2->startline, p2->endline);
2291 static void check_context_names(void)
2294 for (i=current_db; i; i=i->next) {
2295 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2296 for (j=i->next; j; j=j->next) {
2297 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2298 if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
2300 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d! (and neither is marked 'extend')\n",
2301 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
2310 static void check_abstract_reference(pval *abstract_context)
2313 /* find some context includes that reference this context */
2316 /* otherwise, print out a warning */
2317 for (i=current_db; i; i=i->next) {
2318 if (i->type == PV_CONTEXT) {
2319 for (j=i->u2. statements; j; j=j->next) {
2320 if ( j->type == PV_INCLUDES ) {
2322 for (p4=j->u1.list; p4; p4=p4->next) {
2323 /* for each context pointed to, find it, then find a context/label that matches the
2325 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2326 return; /* found a match! */
2332 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",
2333 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2338 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2342 struct argapp *app, *found;
2344 struct pval *macro_def;
2345 struct pval *app_def;
2350 switch (item->type) {
2352 /* fields: item->u1.str == string associated with this (word).
2353 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2357 /* fields: item->u1.str == name of macro
2358 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2359 item->u2.arglist->u1.str == argument
2360 item->u2.arglist->next == next arg
2362 item->u3.macro_statements == pval list of statements in macro body.
2364 in_abstract_context = 0;
2365 current_context = item;
2366 current_extension = 0;
2368 check_macro_returns(item);
2370 for (lp=item->u2.arglist; lp; lp=lp->next) {
2373 check_pval(item->u3.macro_statements, apps,in_globals);
2377 /* fields: item->u1.str == name of context
2378 item->u2.statements == pval list of statements in context body
2379 item->u3.abstract == int 1 if an abstract keyword were present
2381 current_context = item;
2382 current_extension = 0;
2383 if ( item->u3.abstract ) {
2384 in_abstract_context = 1;
2385 check_abstract_reference(item);
2387 in_abstract_context = 0;
2388 check_pval(item->u2.statements, apps,in_globals);
2392 /* fields: item->u1.str == name of macro to call
2393 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2394 item->u2.arglist->u1.str == argument
2395 item->u2.arglist->next == next arg
2398 /* if this is a standalone, we will need to make sure the
2399 localized load of extensions.conf is done */
2400 if (!extensions_dot_conf_loaded) {
2401 localized_pbx_load_module();
2402 extensions_dot_conf_loaded++;
2405 macro_def = find_macro(item->u1.str);
2408 struct pbx_find_info pfiq = {.stacklen = 0 };
2409 struct pbx_find_info pfiq2 = {.stacklen = 0 };
2411 /* look for the macro in the extensions.conf world */
2412 pbx_find_extension(NULL, NULL, &pfiq, item->u1.str, "s", 1, NULL, NULL, E_MATCH);
2414 if (pfiq.status != STATUS_SUCCESS) {
2416 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2418 /* look for the macro in the extensions.conf world */
2419 pbx_find_extension(NULL, NULL, &pfiq2, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2421 if (pfiq2.status == STATUS_SUCCESS) {
2422 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (macro-%s was found in the extensions.conf stuff, but we are using gosubs!)\n",
2423 item->filename, item->startline, item->endline, item->u1.str, item->u1.str);
2426 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (Not even in the extensions.conf stuff!)\n",
2427 item->filename, item->startline, item->endline, item->u1.str);
2432 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s cannot be found in the AEL code!\n",
2433 item->filename, item->startline, item->endline, item->u1.str);
2437 #ifdef THIS_IS_1DOT4
2439 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2441 /* look for the macro in the extensions.conf world */
2442 pbx_find_extension(NULL, NULL, &pfiq, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2444 if (pfiq.status != STATUS_SUCCESS) {
2445 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s was not found in the AEL, nor the extensions.conf !\n",
2446 item->filename, item->startline, item->endline, item->u1.str);
2452 } else if (macro_def->type != PV_MACRO) {
2453 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2454 item->filename, item->startline, item->endline, item->u1.str);
2457 /* macro_def is a MACRO, so do the args match in number? */
2461 for (lp=item->u2.arglist; lp; lp=lp->next) {
2464 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2467 if (hereargs != thereargs ) {
2468 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",
2469 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2475 case PV_APPLICATION_CALL:
2476 /* fields: item->u1.str == name of application to call
2477 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2478 item->u2.arglist->u1.str == argument
2479 item->u2.arglist->next == next arg
2481 /* Need to check to see if the application is available! */
2482 app_def = find_context(item->u1.str);
2483 if (app_def && app_def->type == PV_MACRO) {
2484 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2485 item->filename, item->startline, item->endline, item->u1.str);
2488 if (strcasecmp(item->u1.str,"GotoIf") == 0
2489 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2490 || strcasecmp(item->u1.str,"while") == 0
2491 || strcasecmp(item->u1.str,"endwhile") == 0
2492 || strcasecmp(item->u1.str,"random") == 0
2493 || strcasecmp(item->u1.str,"gosub") == 0
2494 || strcasecmp(item->u1.str,"return") == 0
2495 || strcasecmp(item->u1.str,"gosubif") == 0
2496 || strcasecmp(item->u1.str,"continuewhile") == 0
2497 || strcasecmp(item->u1.str,"endwhile") == 0
2498 || strcasecmp(item->u1.str,"execif") == 0
2499 || strcasecmp(item->u1.str,"execiftime") == 0
2500 || strcasecmp(item->u1.str,"exitwhile") == 0
2501 || strcasecmp(item->u1.str,"goto") == 0
2502 || strcasecmp(item->u1.str,"macro") == 0
2503 || strcasecmp(item->u1.str,"macroexclusive") == 0
2504 || strcasecmp(item->u1.str,"macroif") == 0
2505 || strcasecmp(item->u1.str,"stackpop") == 0
2506 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2507 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s affects flow of control, and needs to be re-written using AEL if, while, goto, etc. keywords instead!\n",
2508 item->filename, item->startline, item->endline, item->u1.str);
2511 if (strcasecmp(item->u1.str,"macroexit") == 0) {
2512 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
2513 item->filename, item->startline, item->endline);
2514 item->type = PV_RETURN;
2521 for (app=apps; app; app=app->next) {
2522 if (strcasecmp(app->name, item->u1.str) == 0) {
2528 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2529 item->filename, item->startline, item->endline, item->u1.str);
2532 check_app_args(item, item->u2.arglist, app);
2537 /* fields: item->u1.str == value of case
2538 item->u2.statements == pval list of statements under the case
2540 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2541 /* find the last statement */
2542 check_pval(item->u2.statements, apps,in_globals);
2546 /* fields: item->u1.str == value of case
2547 item->u2.statements == pval list of statements under the case
2549 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2550 /* find the last statement */
2552 check_pval(item->u2.statements, apps,in_globals);
2557 item->u2.statements == pval list of statements under the case
2560 check_pval(item->u2.statements, apps,in_globals);
2564 /* fields: item->u1.str == name of extension to catch
2565 item->u2.statements == pval list of statements in context body
2567 check_pval(item->u2.statements, apps,in_globals);
2571 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2573 check_pval(item->u1.list, apps,in_globals);
2577 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2579 check_pval(item->u1.list, apps,in_globals);
2583 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2585 check_pval(item->u1.list, apps,in_globals);
2586 check_includes(item);
2587 for (lp=item->u1.list; lp; lp=lp->next){
2588 char *incl_context = lp->u1.str;
2589 struct pval *that_context = find_context(incl_context);
2591 if ( lp->u2.arglist ) {
2592 check_timerange(lp->u2.arglist);
2593 check_dow(lp->u2.arglist->next);
2594 check_day(lp->u2.arglist->next->next);
2595 check_month(lp->u2.arglist->next->next->next);
2599 find_pval_gotos(that_context->u2.statements,0);
2605 case PV_STATEMENTBLOCK:
2606 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2608 check_pval(item->u1.list, apps,in_globals);
2612 /* fields: item->u1.str == variable name
2613 item->u2.val == variable value to assign
2615 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2616 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2617 snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u2.val);
2618 ast_expr_register_extra_error_info(errmsg);
2619 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2620 ast_expr_clear_extra_error_info();
2621 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2622 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2623 item->filename, item->startline, item->endline, item->u2.val);
2626 check_expr2_input(item,item->u2.val);
2630 case PV_LOCALVARDEC:
2631 /* fields: item->u1.str == variable name
2632 item->u2.val == variable value to assign
2634 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2635 snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u2.val);
2636 ast_expr_register_extra_error_info(errmsg);
2637 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2638 ast_expr_clear_extra_error_info();
2639 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2640 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2641 item->filename, item->startline, item->endline, item->u2.val);
2644 check_expr2_input(item,item->u2.val);
2648 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2649 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2651 /* don't check goto's in abstract contexts */
2652 if ( in_abstract_context )
2659 /* fields: item->u1.str == label name
2661 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2662 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2663 item->filename, item->startline, item->endline, item->u1.str);
2671 /* fields: item->u1.for_init == a string containing the initalizer
2672 item->u2.for_test == a string containing the loop test
2673 item->u3.for_inc == a string containing the loop increment
2675 item->u4.for_statements == a pval list of statements in the for ()
2677 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, for test expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u2.for_test);
2678 ast_expr_register_extra_error_info(errmsg);
2680 strp = strchr(item->u1.for_init, '=');
2682 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2684 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
2685 strp = strchr(item->u3.for_inc, '=');
2687 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2689 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2690 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2691 item->filename, item->startline, item->endline, item->u2.for_test);
2694 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2695 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2696 item->filename, item->startline, item->endline, item->u3.for_inc);
2699 check_expr2_input(item,item->u2.for_test);
2700 check_expr2_input(item,item->u3.for_inc);
2702 ast_expr_clear_extra_error_info();
2703 check_pval(item->u4.for_statements, apps,in_globals);
2707 /* fields: item->u1.str == the while conditional, as supplied by user
2709 item->u2.statements == a pval list of statements in the while ()
2711 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u1.str);
2712 ast_expr_register_extra_error_info(errmsg);
2713 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2714 ast_expr_clear_extra_error_info();
2715 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2716 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2717 item->filename, item->startline, item->endline, item->u1.str);
2720 check_expr2_input(item,item->u1.str);
2721 check_pval(item->u2.statements, apps,in_globals);
2738 check_continue(item);
2742 /* fields: item->u1.str == the random number expression, as supplied by user
2744 item->u2.statements == a pval list of statements in the if ()
2745 item->u3.else_statements == a pval list of statements in the else
2748 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u1.str);
2749 ast_expr_register_extra_error_info(errmsg);
2750 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2751 ast_expr_clear_extra_error_info();
2752 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2753 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2754 item->filename, item->startline, item->endline, item->u1.str);
2757 check_expr2_input(item,item->u1.str);
2758 check_pval(item->u2.statements, apps,in_globals);
2759 if (item->u3.else_statements) {
2760 check_pval(item->u3.else_statements, apps,in_globals);
2765 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2767 item->u2.statements == a pval list of statements in the if ()
2768 item->u3.else_statements == a pval list of statements in the else
2771 if ( item->u2.arglist ) {
2772 check_timerange(item->u1.list);
2773 check_dow(item->u1.list->next);
2774 check_day(item->u1.list->next->next);
2775 check_month(item->u1.list->next->next->next);
2778 check_pval(item->u2.statements, apps,in_globals);
2779 if (item->u3.else_statements) {
2780 check_pval(item->u3.else_statements, apps,in_globals);
2785 /* fields: item->u1.str == the if conditional, as supplied by user
2787 item->u2.statements == a pval list of statements in the if ()
2788 item->u3.else_statements == a pval list of statements in the else
2791 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u1.str);
2792 ast_expr_register_extra_error_info(errmsg);
2793 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2794 ast_expr_clear_extra_error_info();
2795 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2796 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2797 item->filename, item->startline, item->endline, item->u1.str);
2800 check_expr2_input(item,item->u1.str);
2801 check_pval(item->u2.statements, apps,in_globals);
2802 if (item->u3.else_statements) {
2803 check_pval(item->u3.else_statements, apps,in_globals);
2808 /* fields: item->u1.str == the switch expression
2810 item->u2.statements == a pval list of statements in the switch,
2811 (will be case statements, most likely!)
2813 /* we can check the switch expression, see if it matches any of the app variables...
2814 if it does, then, are all the possible cases accounted for? */
2815 check_switch_expr(item, apps);
2816 check_pval(item->u2.statements, apps,in_globals);
2820 /* fields: item->u1.str == the extension name, label, whatever it's called
2822 item->u2.statements == a pval list of statements in the extension
2823 item->u3.hints == a char * hint argument
2824 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2826 current_extension = item ;
2828 check_pval(item->u2.statements, apps,in_globals);
2832 /* fields: item->u1.str == the ignorepat data
2837 /* fields: item->u1.statements == pval list of statements, usually vardecs
2839 in_abstract_context = 0;
2840 check_pval(item->u1.statements, apps, 1);
2847 void check_pval(pval *item, struct argapp *apps, int in_globals)
2852 1. Do goto's point to actual labels?
2853 2. Do macro calls reference a macro?
2854 3. Does the number of macro args match the definition?
2855 4. Is a macro call missing its & at the front?
2856 5. Application calls-- we could check syntax for existing applications,
2857 but I need some some sort of universal description bnf for a general
2858 sort of method for checking arguments, in number, maybe even type, at least.
2859 Don't want to hand code checks for hundreds of applications.
2862 for (i=item; i; i=i->next) {
2863 check_pval_item(i,apps,in_globals);
2867 void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2874 struct argapp *apps=0;
2877 return; /* don't check an empty tree */
2879 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2880 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2882 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2885 errs = warns = notes = 0;
2887 check_context_names();
2888 check_pval(item, apps, 0);
2891 argdesc_destroy(apps); /* taketh away */
2900 /* =============================================================================================== */
2901 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2902 /* =============================================================================================== */
2904 static int control_statement_count = 0;
2906 struct ael_priority *new_prio(void)
2908 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2912 struct ael_extension *new_exten(void)
2914 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2918 void linkprio(struct ael_extension *exten, struct ael_priority *prio)
2920 if (!exten->plist) {
2921 exten->plist = prio;
2922 exten->plist_last = prio;
2924 exten->plist_last->next = prio;
2925 exten->plist_last = prio;
2928 prio->exten = exten; /* don't override the switch value */
2931 void destroy_extensions(struct ael_extension *exten)
2933 struct ael_extension *ne, *nen;
2934 for (ne=exten; ne; ne=nen) {
2935 struct ael_priority *pe, *pen;
2940 /* cidmatch fields are allocated with name, and freed when
2941 the name field is freed. Don't do a free for this field,
2942 unless you LIKE to see a crash! */
2947 for (pe=ne->plist; pe; pe=pen) {
2960 nen = ne->next_exten;
2966 ne->loop_continue = 0;
2971 static int label_inside_case(pval *label)
2975 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
2976 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
2985 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
2987 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2988 exten->next_exten = add;
2991 static void remove_spaces_before_equals(char *str)
2994 while( str && *str && *str != '=' )
2996 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
3010 /* =============================================================================================== */
3011 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
3012 /* =============================================================================================== */
3014 static void gen_match_to_pattern(char *pattern, char *result)
3016 /* the result will be a string that will be matched by pattern */
3017 char *p=pattern, *t=result;
3019 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
3021 else if (*p == '[') {
3027 *t++=*(p+1); /* use the first char in the set */
3034 *t++ = 0; /* cap it off */
3037 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
3040 struct ael_priority *pr;
3041 struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
3042 struct ael_priority *while_test, *while_loop, *while_end;
3043 struct ael_priority *switch_test, *switch_end, *fall_thru, *switch_empty;
3044 struct ael_priority *if_test, *if_end, *if_skip, *if_false;
3045 #ifdef OLD_RAND_ACTION
3046 struct ael_priority *rand_test, *rand_end, *rand_skip;
3051 char new_label[2000];
3053 int local_control_statement_count;
3055 struct ael_priority *loop_break_save;
3056 struct ael_priority *loop_continue_save;
3057 struct ael_extension *switch_case,*switch_null;
3059 for (p=statement; p; p=p->next) {
3063 pr->type = AEL_APPCALL;
3064 snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
3065 pr->app = strdup("Set");
3066 remove_spaces_before_equals(buf1);
3067 pr->appargs = strdup(buf1);
3069 linkprio(exten, pr);
3072 case PV_LOCALVARDEC:
3074 pr->type = AEL_APPCALL;
3075 snprintf(buf1,sizeof(buf1),"LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
3076 pr->app = strdup("Set");
3077 remove_spaces_before_equals(buf1);
3078 pr->appargs = strdup(buf1);
3080 linkprio(exten, pr);
3085 pr->type = AEL_APPCALL;
3086 p->u2.goto_target = get_goto_target(p);
3087 if( p->u2.goto_target ) {
3088 p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
3091 if (!p->u1.list->next) /* just one */ {
3092 pr->app = strdup("Goto");
3094 pr->appargs = strdup(p->u1.list->u1.str);
3095 else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
3096 snprintf(buf1,sizeof(buf1),"%s,%s", mother_exten->name, p->u1.list->u1.str);
3097 pr->appargs = strdup(buf1);
3100 } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
3101 snprintf(buf1,sizeof(buf1),"%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
3102 pr->app = strdup("Goto");
3103 pr->appargs = strdup(buf1);
3104 } else if (p->u1.list->next && p->u1.list->next->next) {
3105 snprintf(buf1,sizeof(buf1),"%s,%s,%s", p->u1.list->u1.str,
3106 p->u1.list->next->u1.str,
3107 p->u1.list->next->next->u1.str);
3108 pr->app = strdup("Goto");
3109 pr->appargs = strdup(buf1);
3112 linkprio(exten, pr);
3117 pr->type = AEL_LABEL;
3119 p->u3.compiled_label = exten;
3120 linkprio(exten, pr);
3124 control_statement_count++;
3125 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3126 loop_continue_save = exten->loop_continue;
3127 snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
3128 for_init = new_prio();
3129 for_inc = new_prio();
3130 for_test = new_prio();
3131 for_loop = new_prio();
3132 for_end = new_prio();
3133 for_init->type = AEL_APPCALL;
3134 for_inc->type = AEL_APPCALL;
3135 for_test->type = AEL_FOR_CONTROL;
3136 for_test->goto_false = for_end;
3137 for_loop->type = AEL_CONTROL1; /* simple goto */
3138 for_end->type = AEL_APPCALL;
3139 for_init->app = strdup("Set");
3141 strcpy(buf2,p->u1.for_init);
3142 remove_spaces_before_equals(buf2);
3143 strp = strchr(buf2, '=');
3145 strp2 = strchr(p->u1.for_init, '=');
3148 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3150 for_init->appargs = strdup(buf2);
3152 strp2 = p->u1.for_init;
3153 while (*strp2 && isspace(*strp2))
3155 if (*strp2 == '&') { /* itsa macro call */
3156 char *strp3 = strp2+1;
3157 while (*strp3 && isspace(*strp3))
3159 strcpy(buf2, strp3);
3160 strp3 = strchr(buf2,'(');
3164 while ((strp3=strchr(buf2,','))) {
3167 strp3 = strrchr(buf2, ')');
3169 *strp3 = 0; /* remove the closing paren */
3171 for_init->appargs = strdup(buf2);
3172 free(for_init->app);
3173 for_init->app = strdup("Macro");
3174 } else { /* must be a regular app call */
3176 strcpy(buf2, strp2);
3177 strp3 = strchr(buf2,'(');
3180 free(for_init->app);
3181 for_init->app = strdup(buf2);
3182 for_init->appargs = strdup(strp3+1);
3183 strp3 = strrchr(for_init->appargs, ')');
3185 *strp3 = 0; /* remove the closing paren */