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 struct ast_flags ast_compat;
56 extern int localized_pbx_load_module(void);
58 static char expr_output[2096];
59 #define AST_PBX_MAX_STACK 128
61 /* these functions are in ../ast_expr2.fl */
63 static int errs, warns;
66 static int extensions_dot_conf_loaded = 0;
68 static char *registrar = "pbx_ael";
70 static pval *current_db;
71 static pval *current_context;
72 static pval *current_extension;
74 static const char *match_context;
75 static const char *match_exten;
76 static const char *match_label;
77 static int in_abstract_context;
78 static int count_labels; /* true, put matcher in label counting mode */
79 static int label_count; /* labels are only meant to be counted in a context or exten */
80 static int return_on_context_match;
81 static pval *last_matched_label;
82 struct pval *match_pval(pval *item);
83 static void check_timerange(pval *p);
84 static void check_dow(pval *DOW);
85 static void check_day(pval *DAY);
86 static void check_month(pval *MON);
87 static void check_expr2_input(pval *expr, char *str);
88 static int extension_matches(pval *here, const char *exten, const char *pattern);
89 static void check_goto(pval *item);
90 static void find_pval_goto_item(pval *item, int lev);
91 static void find_pval_gotos(pval *item, int lev);
92 static int check_break(pval *item);
93 static int check_continue(pval *item);
94 static void check_label(pval *item);
95 static void check_macro_returns(pval *macro);
97 static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
98 static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
99 static void print_pval_list(FILE *fin, pval *item, int depth);
101 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
102 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
103 static pval *get_goto_target(pval *item);
104 static int label_inside_case(pval *label);
105 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
106 static void fix_gotos_in_extensions(struct ael_extension *exten);
107 static pval *get_extension_or_contxt(pval *p);
108 static pval *get_contxt(pval *p);
109 static void remove_spaces_before_equals(char *str);
111 /* PRETTY PRINTER FOR AEL: ============================================================================= */
113 static void print_pval(FILE *fin, pval *item, int depth)
118 for (i=0; i<depth; i++) {
119 fprintf(fin, "\t"); /* depth == indentation */
122 switch ( item->type ) {
124 fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
128 fprintf(fin,"macro %s(", item->u1.str);
129 for (lp=item->u2.arglist; lp; lp=lp->next) {
130 if (lp != item->u2.arglist )
132 fprintf(fin,"%s", lp->u1.str);
134 fprintf(fin,") {\n");
135 print_pval_list(fin,item->u3.macro_statements,depth+1);
136 for (i=0; i<depth; i++) {
137 fprintf(fin,"\t"); /* depth == indentation */
139 fprintf(fin,"};\n\n");
143 if ( item->u3.abstract )
144 fprintf(fin,"abstract context %s {\n", item->u1.str);
146 fprintf(fin,"context %s {\n", item->u1.str);
147 print_pval_list(fin,item->u2.statements,depth+1);
148 for (i=0; i<depth; i++) {
149 fprintf(fin,"\t"); /* depth == indentation */
151 fprintf(fin,"};\n\n");
155 fprintf(fin,"&%s(", item->u1.str);
156 for (lp=item->u2.arglist; lp; lp=lp->next) {
157 if ( lp != item->u2.arglist )
159 fprintf(fin,"%s", lp->u1.str);
164 case PV_APPLICATION_CALL:
165 fprintf(fin,"%s(", item->u1.str);
166 for (lp=item->u2.arglist; lp; lp=lp->next) {
167 if ( lp != item->u2.arglist )
169 fprintf(fin,"%s", lp->u1.str);
175 fprintf(fin,"case %s:\n", item->u1.str);
176 print_pval_list(fin,item->u2.statements, depth+1);
180 fprintf(fin,"pattern %s:\n", item->u1.str);
181 print_pval_list(fin,item->u2.statements, depth+1);
185 fprintf(fin,"default:\n");
186 print_pval_list(fin,item->u2.statements, depth+1);
190 fprintf(fin,"catch %s {\n", item->u1.str);
191 print_pval_list(fin,item->u2.statements, depth+1);
192 for (i=0; i<depth; i++) {
193 fprintf(fin,"\t"); /* depth == indentation */
199 fprintf(fin,"switches {\n");
200 print_pval_list(fin,item->u1.list,depth+1);
201 for (i=0; i<depth; i++) {
202 fprintf(fin,"\t"); /* depth == indentation */
208 fprintf(fin,"eswitches {\n");
209 print_pval_list(fin,item->u1.list,depth+1);
210 for (i=0; i<depth; i++) {
211 fprintf(fin,"\t"); /* depth == indentation */
217 fprintf(fin,"includes {\n");
218 for (lp=item->u1.list; lp; lp=lp->next) {
219 for (i=0; i<depth+1; i++) {
220 fprintf(fin,"\t"); /* depth == indentation */
222 fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
224 fprintf(fin,"|%s|%s|%s|%s",
225 lp->u2.arglist->u1.str,
226 lp->u2.arglist->next->u1.str,
227 lp->u2.arglist->next->next->u1.str,
228 lp->u2.arglist->next->next->next->u1.str
230 fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
233 for (i=0; i<depth; i++) {
234 fprintf(fin,"\t"); /* depth == indentation */
239 case PV_STATEMENTBLOCK:
241 print_pval_list(fin,item->u1.list, depth+1);
242 for (i=0; i<depth; i++) {
243 fprintf(fin,"\t"); /* depth == indentation */
249 fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
253 fprintf(fin,"local %s=%s;\n", item->u1.str, item->u2.val);
257 fprintf(fin,"goto %s", item->u1.list->u1.str);
258 if ( item->u1.list->next )
259 fprintf(fin,",%s", item->u1.list->next->u1.str);
260 if ( item->u1.list->next && item->u1.list->next->next )
261 fprintf(fin,",%s", item->u1.list->next->next->u1.str);
266 fprintf(fin,"%s:\n", item->u1.str);
270 fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
271 print_pval_list(fin,item->u4.for_statements,depth+1);
275 fprintf(fin,"while (%s)\n", item->u1.str);
276 print_pval_list(fin,item->u2.statements,depth+1);
280 fprintf(fin,"break;\n");
284 fprintf(fin,"return;\n");
288 fprintf(fin,"continue;\n");
294 if ( item->type == PV_IFTIME ) {
296 fprintf(fin,"ifTime ( %s|%s|%s|%s )\n",
297 item->u1.list->u1.str,
298 item->u1.list->next->u1.str,
299 item->u1.list->next->next->u1.str,
300 item->u1.list->next->next->next->u1.str
302 } else if ( item->type == PV_RANDOM ) {
303 fprintf(fin,"random ( %s )\n", item->u1.str );
305 fprintf(fin,"if ( %s )\n", item->u1.str);
306 if ( item->u2.statements && item->u2.statements->next ) {
307 for (i=0; i<depth; i++) {
308 fprintf(fin,"\t"); /* depth == indentation */
311 print_pval_list(fin,item->u2.statements,depth+1);
312 for (i=0; i<depth; i++) {
313 fprintf(fin,"\t"); /* depth == indentation */
315 if ( item->u3.else_statements )
319 } else if (item->u2.statements ) {
320 print_pval_list(fin,item->u2.statements,depth+1);
322 if (item->u3.else_statements )
323 fprintf(fin, " {} ");
325 fprintf(fin, " {}; ");
327 if ( item->u3.else_statements ) {
328 for (i=0; i<depth; i++) {
329 fprintf(fin,"\t"); /* depth == indentation */
331 fprintf(fin,"else\n");
332 print_pval_list(fin,item->u3.else_statements, depth);
337 fprintf(fin,"switch( %s ) {\n", item->u1.str);
338 print_pval_list(fin,item->u2.statements,depth+1);
339 for (i=0; i<depth; i++) {
340 fprintf(fin,"\t"); /* depth == indentation */
346 if ( item->u4.regexten )
347 fprintf(fin, "regexten ");
348 if ( item->u3.hints )
349 fprintf(fin,"hints(%s) ", item->u3.hints);
351 fprintf(fin,"%s => ", item->u1.str);
352 print_pval_list(fin,item->u2.statements,depth+1);
357 fprintf(fin,"ignorepat => %s;\n", item->u1.str);
361 fprintf(fin,"globals {\n");
362 print_pval_list(fin,item->u1.statements,depth+1);
363 for (i=0; i<depth; i++) {
364 fprintf(fin,"\t"); /* depth == indentation */
371 static void print_pval_list(FILE *fin, pval *item, int depth)
375 for (i=item; i; i=i->next) {
376 print_pval(fin, i, depth);
380 void ael2_print(char *fname, pval *tree)
382 FILE *fin = fopen(fname,"w");
384 ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
387 print_pval_list(fin, tree, 0);
392 /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL: ============================================================================= */
394 void traverse_pval_template(pval *item, int depth);
395 void traverse_pval_item_template(pval *item, int depth);
398 void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
399 but you may not need it */
403 switch ( item->type ) {
405 /* fields: item->u1.str == string associated with this (word). */
409 /* fields: item->u1.str == name of macro
410 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
411 item->u2.arglist->u1.str == argument
412 item->u2.arglist->next == next arg
414 item->u3.macro_statements == pval list of statements in macro body.
416 for (lp=item->u2.arglist; lp; lp=lp->next) {
419 traverse_pval_item_template(item->u3.macro_statements,depth+1);
423 /* fields: item->u1.str == name of context
424 item->u2.statements == pval list of statements in context body
425 item->u3.abstract == int 1 if an abstract keyword were present
427 traverse_pval_item_template(item->u2.statements,depth+1);
431 /* fields: item->u1.str == name of macro to call
432 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
433 item->u2.arglist->u1.str == argument
434 item->u2.arglist->next == next arg
436 for (lp=item->u2.arglist; lp; lp=lp->next) {
440 case PV_APPLICATION_CALL:
441 /* fields: item->u1.str == name of application to call
442 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
443 item->u2.arglist->u1.str == argument
444 item->u2.arglist->next == next arg
446 for (lp=item->u2.arglist; lp; lp=lp->next) {
451 /* fields: item->u1.str == value of case
452 item->u2.statements == pval list of statements under the case
454 traverse_pval_item_template(item->u2.statements,depth+1);
458 /* fields: item->u1.str == value of case
459 item->u2.statements == pval list of statements under the case
461 traverse_pval_item_template(item->u2.statements,depth+1);
466 item->u2.statements == pval list of statements under the case
468 traverse_pval_item_template(item->u2.statements,depth+1);
472 /* fields: item->u1.str == name of extension to catch
473 item->u2.statements == pval list of statements in context body
475 traverse_pval_item_template(item->u2.statements,depth+1);
479 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
481 traverse_pval_item_template(item->u1.list,depth+1);
485 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
487 traverse_pval_item_template(item->u1.list,depth+1);
491 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
492 item->u2.arglist == pval list of 4 PV_WORD elements for time values
494 traverse_pval_item_template(item->u1.list,depth+1);
495 traverse_pval_item_template(item->u2.arglist,depth+1);
498 case PV_STATEMENTBLOCK:
499 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
501 traverse_pval_item_template(item->u1.list,depth+1);
506 /* fields: item->u1.str == variable name
507 item->u2.val == variable value to assign
512 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
513 item->u1.list->u1.str == where the data on a PV_WORD will always be.
516 if ( item->u1.list->next )
518 if ( item->u1.list->next && item->u1.list->next->next )
524 /* fields: item->u1.str == label name
529 /* fields: item->u1.for_init == a string containing the initalizer
530 item->u2.for_test == a string containing the loop test
531 item->u3.for_inc == a string containing the loop increment
533 item->u4.for_statements == a pval list of statements in the for ()
535 traverse_pval_item_template(item->u4.for_statements,depth+1);
539 /* fields: item->u1.str == the while conditional, as supplied by user
541 item->u2.statements == a pval list of statements in the while ()
543 traverse_pval_item_template(item->u2.statements,depth+1);
562 /* fields: item->u1.list == there are 4 linked PV_WORDs here.
564 item->u2.statements == a pval list of statements in the if ()
565 item->u3.else_statements == a pval list of statements in the else
568 traverse_pval_item_template(item->u2.statements,depth+1);
569 if ( item->u3.else_statements ) {
570 traverse_pval_item_template(item->u3.else_statements,depth+1);
575 /* fields: item->u1.str == the random number expression, as supplied by user
577 item->u2.statements == a pval list of statements in the if ()
578 item->u3.else_statements == a pval list of statements in the else
581 traverse_pval_item_template(item->u2.statements,depth+1);
582 if ( item->u3.else_statements ) {
583 traverse_pval_item_template(item->u3.else_statements,depth+1);
588 /* fields: item->u1.str == the if conditional, as supplied by user
590 item->u2.statements == a pval list of statements in the if ()
591 item->u3.else_statements == a pval list of statements in the else
594 traverse_pval_item_template(item->u2.statements,depth+1);
595 if ( item->u3.else_statements ) {
596 traverse_pval_item_template(item->u3.else_statements,depth+1);
601 /* fields: item->u1.str == the switch expression
603 item->u2.statements == a pval list of statements in the switch,
604 (will be case statements, most likely!)
606 traverse_pval_item_template(item->u2.statements,depth+1);
610 /* fields: item->u1.str == the extension name, label, whatever it's called
612 item->u2.statements == a pval list of statements in the extension
613 item->u3.hints == a char * hint argument
614 item->u4.regexten == an int boolean. non-zero says that regexten was specified
616 traverse_pval_item_template(item->u2.statements,depth+1);
620 /* fields: item->u1.str == the ignorepat data
625 /* fields: item->u1.statements == pval list of statements, usually vardecs
627 traverse_pval_item_template(item->u1.statements,depth+1);
632 void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
633 but you may not need it */
637 for (i=item; i; i=i->next) {
638 traverse_pval_item_template(i, depth);
643 /* SEMANTIC CHECKING FOR AEL: ============================================================================= */
645 /* (not all that is syntactically legal is good! */
648 static void check_macro_returns(pval *macro)
651 if (!macro->u3.macro_statements)
653 pval *z = calloc(1, sizeof(struct pval));
654 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s is empty! I will insert a return.\n",
655 macro->filename, macro->startline, macro->endline, macro->u1.str);
658 z->startline = macro->startline;
659 z->endline = macro->endline;
660 z->startcol = macro->startcol;
661 z->endcol = macro->endcol;
662 z->filename = strdup(macro->filename);
664 macro->u3.macro_statements = z;
667 for (i=macro->u3.macro_statements; i; i=i->next) {
668 /* if the last statement in the list is not return, then insert a return there */
669 if (i->next == NULL) {
670 if (i->type != PV_RETURN) {
671 pval *z = calloc(1, sizeof(struct pval));
672 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s does not end with a return; I will insert one.\n",
673 macro->filename, macro->startline, macro->endline, macro->u1.str);
676 z->startline = macro->startline;
677 z->endline = macro->endline;
678 z->startcol = macro->startcol;
679 z->endcol = macro->endcol;
680 z->filename = strdup(macro->filename);
692 static int extension_matches(pval *here, const char *exten, const char *pattern)
697 /* simple case, they match exactly, the pattern and exten name */
698 if (!strcmp(pattern,exten) == 0)
701 if (pattern[0] == '_') {
706 if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
707 ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
711 /* form a regular expression from the pattern, and then match it against exten */
712 *r++ = '^'; /* what if the extension is a pattern ?? */
713 *r++ = '_'; /* what if the extension is a pattern ?? */
715 for (p=pattern+1; *p; p++) {
745 while ( *p && *p != ']' ) {
749 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
750 here->filename, here->startline, here->endline, pattern);
769 *r++ = '$'; /* what if the extension is a pattern ?? */
770 *r++ = *p++; /* put in the closing null */
771 err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
774 regerror(err1,&preg,errmess,sizeof(errmess));
776 ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
780 err1 = regexec(&preg, exten, 0, 0, 0);
784 /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
785 err1,exten, pattern, reg1); */
786 return 0; /* no match */
788 /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
795 if ( strcmp(exten,pattern) == 0 ) {
803 static void check_expr2_input(pval *expr, char *str)
805 int spaces = strspn(str,"\t \n");
806 if ( !strncmp(str+spaces,"$[",2) ) {
807 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
808 expr->filename, expr->startline, expr->endline, str);
813 static void check_includes(pval *includes)
816 for (p4=includes->u1.list; p4; p4=p4->next) {
817 /* for each context pointed to, find it, then find a context/label that matches the
819 char *incl_context = p4->u1.str;
820 /* find a matching context name */
821 struct pval *that_other_context = find_context(incl_context);
822 if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
823 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n\
824 (You may ignore this warning if '%s' exists in extensions.conf, or is created by another module. I cannot check for those.)\n",
825 includes->filename, includes->startline, includes->endline, incl_context, incl_context);
832 static void check_timerange(pval *p)
839 times = ast_strdupa(p->u1.str);
841 /* Star is all times */
842 if (ast_strlen_zero(times) || !strcmp(times, "*")) {
845 /* Otherwise expect a range */
846 e = strchr(times, '-');
848 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",
849 p->filename, p->startline, p->endline, times);
855 while (*e && !isdigit(*e))
858 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
859 p->filename, p->startline, p->endline, p->u1.str);
862 if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
863 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
864 p->filename, p->startline, p->endline, times);
867 if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
868 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
869 p->filename, p->startline, p->endline, times);
874 if ((s1 < 0) || (s1 >= 24*30)) {
875 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
876 p->filename, p->startline, p->endline, times);
880 if ((e1 < 0) || (e1 >= 24*30)) {
881 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
882 p->filename, p->startline, p->endline, e);
888 static char *days[] =
899 /*! \brief get_dow: Get day of week */
900 static void check_dow(pval *DOW)
904 /* The following line is coincidence, really! */
907 dow = ast_strdupa(DOW->u1.str);
909 /* Check for all days */
910 if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
912 /* Get start and ending days */
913 c = strchr(dow, '-');
921 while ((s < 7) && strcasecmp(dow, days[s])) s++;
923 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",
924 DOW->filename, DOW->startline, DOW->endline, dow);
929 while ((e < 7) && strcasecmp(c, days[e])) e++;
931 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",
932 DOW->filename, DOW->startline, DOW->endline, c);
939 static void check_day(pval *DAY)
943 /* The following line is coincidence, really! */
946 day = ast_strdupa(DAY->u1.str);
948 /* Check for all days */
949 if (ast_strlen_zero(day) || !strcmp(day, "*")) {
952 /* Get start and ending days */
953 c = strchr(day, '-');
959 if (sscanf(day, "%d", &s) != 1) {
960 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
961 DAY->filename, DAY->startline, DAY->endline, day);
964 else if ((s < 1) || (s > 31)) {
965 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",
966 DAY->filename, DAY->startline, DAY->endline, day);
971 if (sscanf(c, "%d", &e) != 1) {
972 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
973 DAY->filename, DAY->startline, DAY->endline, c);
976 else if ((e < 1) || (e > 31)) {
977 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",
978 DAY->filename, DAY->startline, DAY->endline, day);
986 static char *months[] =
1002 static void check_month(pval *MON)
1006 /* The following line is coincidence, really! */
1009 mon = ast_strdupa(MON->u1.str);
1011 /* Check for all days */
1012 if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
1014 /* Get start and ending days */
1015 c = strchr(mon, '-');
1020 /* Find the start */
1022 while ((s < 12) && strcasecmp(mon, months[s])) s++;
1024 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1025 MON->filename, MON->startline, MON->endline, mon);
1030 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1032 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1033 MON->filename, MON->startline, MON->endline, c);
1040 static int check_break(pval *item)
1044 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1045 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1047 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
1048 || p->type == PV_WHILE || p->type == PV_FOR ) {
1053 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1054 item->filename, item->startline, item->endline);
1060 static int check_continue(pval *item)
1064 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1065 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1067 if( p->type == PV_WHILE || p->type == PV_FOR ) {
1072 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1073 item->filename, item->startline, item->endline);
1079 static struct pval *in_macro(pval *item)
1084 if( curr->type == PV_MACRO ) {
1092 static struct pval *in_context(pval *item)
1097 if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
1106 /* general purpose goto finder */
1108 static void check_label(pval *item)
1114 /* A label outside an extension just plain does not make sense! */
1119 if( curr->type == PV_MACRO || curr->type == PV_EXTENSION ) {
1127 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
1128 item->filename, item->startline, item->endline, item->u1.str);
1133 /* basically, ensure that a label is not repeated in a context. Period.
1134 The method: well, for each label, find the first label in the context
1135 with the same name. If it's not the current label, then throw an error. */
1138 /* printf("==== check_label: ====\n"); */
1139 if( !current_extension )
1140 curr = current_context;
1142 curr = current_extension;
1144 x = find_first_label_in_current_context((char *)item->u1.str, curr);
1145 /* 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); */
1146 if( x && x != item )
1148 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1149 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1152 /* printf("<<<<< check_label: ====\n"); */
1155 static pval *get_goto_target(pval *item)
1157 /* just one item-- the label should be in the current extension */
1158 pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1161 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1162 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1166 curr_cont = get_contxt(item);
1169 if (item->u1.list->next && !item->u1.list->next->next) {
1170 if (!strstr((item->u1.list)->u1.str,"${")
1171 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1172 struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1178 if (item->u1.list->next && item->u1.list->next->next) {
1180 pval *first = item->u1.list;
1181 pval *second = item->u1.list->next;
1182 pval *third = item->u1.list->next->next;
1184 if (!strstr((item->u1.list)->u1.str,"${")
1185 && !strstr(item->u1.list->next->u1.str,"${")
1186 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1187 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1191 struct pval *that_context = find_context(item->u1.list->u1.str);
1193 /* the target of the goto could be in an included context!! Fancy that!! */
1194 /* look for includes in the current context */
1196 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1197 if (p3->type == PV_INCLUDES) {
1199 for (p4=p3->u1.list; p4; p4=p4->next) {
1200 /* for each context pointed to, find it, then find a context/label that matches the
1202 char *incl_context = p4->u1.str;
1203 /* find a matching context name */
1204 struct pval *that_other_context = find_context(incl_context);
1205 if (that_other_context) {
1207 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1223 static void check_goto(pval *item)
1225 /* check for the target of the goto-- does it exist? */
1226 if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1227 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
1228 item->filename, item->startline, item->endline);
1232 /* just one item-- the label should be in the current extension */
1234 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1235 struct pval *z = get_extension_or_contxt(item);
1238 x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1239 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1240 (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1242 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
1243 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1251 if (item->u1.list->next && !item->u1.list->next->next) {
1253 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1254 (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1255 if (!strstr((item->u1.list)->u1.str,"${")
1256 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1257 struct pval *z = get_contxt(item);
1261 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1264 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",
1265 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1274 if (item->u1.list->next && item->u1.list->next->next) {
1276 pval *first = item->u1.list;
1277 pval *second = item->u1.list->next;
1278 pval *third = item->u1.list->next->next;
1280 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1281 (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1282 if (!strstr((item->u1.list)->u1.str,"${")
1283 && !strstr(item->u1.list->next->u1.str,"${")
1284 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1285 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1288 struct pval *found = 0;
1289 struct pval *that_context = find_context(item->u1.list->u1.str);
1291 /* the target of the goto could be in an included context!! Fancy that!! */
1292 /* look for includes in the current context */
1294 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1295 if (p3->type == PV_INCLUDES) {
1297 for (p4=p3->u1.list; p4; p4=p4->next) {
1298 /* for each context pointed to, find it, then find a context/label that matches the
1300 char *incl_context = p4->u1.str;
1301 /* find a matching context name */
1302 struct pval *that_other_context = find_context(incl_context);
1303 if (that_other_context) {
1305 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1315 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
1316 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1319 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1320 if( mac ) { /* yes! */
1321 struct pval *targ = in_context(found);
1324 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",
1325 item->filename, item->startline, item->endline);
1331 /* here is where code would go to check for target existence in extensions.conf files */
1333 struct pbx_find_info pfiq = {.stacklen = 0 };
1334 extern int localized_pbx_load_module(void);
1335 /* if this is a standalone, we will need to make sure the
1336 localized load of extensions.conf is done */
1337 if (!extensions_dot_conf_loaded) {
1338 localized_pbx_load_module();
1339 extensions_dot_conf_loaded++;
1342 pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
1343 atoi(third->u1.str) ? NULL : third->u1.str, NULL,
1344 atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
1346 if (pfiq.status != STATUS_SUCCESS) {
1347 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",
1348 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1352 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: Couldn't find goto target %s|%s|%s in the AEL code!\n",
1353 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1358 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1359 if( mac ) { /* yes! */
1360 struct pval *targ = in_context(x);
1363 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",
1364 item->filename, item->startline, item->endline);
1374 static void find_pval_goto_item(pval *item, int lev)
1379 ast_log(LOG_ERROR,"find_pval_goto in infinite loop! item_type: %d\n\n", item->type);
1383 switch ( item->type ) {
1385 /* fields: item->u1.str == name of macro
1386 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1387 item->u2.arglist->u1.str == argument
1388 item->u2.arglist->next == next arg
1390 item->u3.macro_statements == pval list of statements in macro body.
1393 /* printf("Descending into macro %s at line %d\n", item->u1.str, item->startline); */
1394 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1399 /* fields: item->u1.str == name of context
1400 item->u2.statements == pval list of statements in context body
1401 item->u3.abstract == int 1 if an abstract keyword were present
1406 /* fields: item->u1.str == value of case
1407 item->u2.statements == pval list of statements under the case
1409 /* printf("Descending into Case of %s\n", item->u1.str); */
1410 find_pval_gotos(item->u2.statements,lev+1);
1414 /* fields: item->u1.str == value of case
1415 item->u2.statements == pval list of statements under the case
1417 /* printf("Descending into Pattern of %s\n", item->u1.str); */
1418 find_pval_gotos(item->u2.statements,lev+1);
1423 item->u2.statements == pval list of statements under the case
1425 /* printf("Descending into default\n"); */
1426 find_pval_gotos(item->u2.statements,lev+1);
1430 /* fields: item->u1.str == name of extension to catch
1431 item->u2.statements == pval list of statements in context body
1433 /* printf("Descending into catch of %s\n", item->u1.str); */
1434 find_pval_gotos(item->u2.statements,lev+1);
1437 case PV_STATEMENTBLOCK:
1438 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1440 /* printf("Descending into statement block\n"); */
1441 find_pval_gotos(item->u1.list,lev+1);
1445 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1446 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1448 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1452 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1454 for (p4=item->u1.list; p4; p4=p4->next) {
1455 /* for each context pointed to, find it, then find a context/label that matches the
1457 char *incl_context = p4->u1.str;
1458 /* find a matching context name */
1459 struct pval *that_context = find_context(incl_context);
1460 if (that_context && that_context->u2.statements) {
1461 /* printf("Descending into include of '%s' at line %d; that_context=%s, that_context type=%d\n", incl_context, item->startline, that_context->u1.str, that_context->type); */
1462 find_pval_gotos(that_context->u2.statements,lev+1); /* keep working up the includes */
1468 /* fields: item->u1.for_init == a string containing the initalizer
1469 item->u2.for_test == a string containing the loop test
1470 item->u3.for_inc == a string containing the loop increment
1472 item->u4.for_statements == a pval list of statements in the for ()
1474 /* printf("Descending into for at line %d\n", item->startline); */
1475 find_pval_gotos(item->u4.for_statements,lev+1);
1479 /* fields: item->u1.str == the while conditional, as supplied by user
1481 item->u2.statements == a pval list of statements in the while ()
1483 /* printf("Descending into while at line %d\n", item->startline); */
1484 find_pval_gotos(item->u2.statements,lev+1);
1488 /* fields: item->u1.str == the random number expression, as supplied by user
1490 item->u2.statements == a pval list of statements in the if ()
1491 item->u3.else_statements == a pval list of statements in the else
1493 fall thru to PV_IF */
1496 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1498 item->u2.statements == a pval list of statements in the if ()
1499 item->u3.else_statements == a pval list of statements in the else
1501 fall thru to PV_IF*/
1503 /* fields: item->u1.str == the if conditional, as supplied by user
1505 item->u2.statements == a pval list of statements in the if ()
1506 item->u3.else_statements == a pval list of statements in the else
1509 /* printf("Descending into random/iftime/if at line %d\n", item->startline); */
1510 find_pval_gotos(item->u2.statements,lev+1);
1512 if (item->u3.else_statements) {
1513 /* printf("Descending into random/iftime/if's ELSE at line %d\n", item->startline); */
1514 find_pval_gotos(item->u3.else_statements,lev+1);
1519 /* fields: item->u1.str == the switch expression
1521 item->u2.statements == a pval list of statements in the switch,
1522 (will be case statements, most likely!)
1524 /* printf("Descending into switch at line %d\n", item->startline); */
1525 find_pval_gotos(item->u3.else_statements,lev+1);
1529 /* fields: item->u1.str == the extension name, label, whatever it's called
1531 item->u2.statements == a pval list of statements in the extension
1532 item->u3.hints == a char * hint argument
1533 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1536 /* printf("Descending into extension %s at line %d\n", item->u1.str, item->startline); */
1537 find_pval_gotos(item->u2.statements,lev+1);
1545 static void find_pval_gotos(pval *item,int lev)
1549 for (i=item; i; i=i->next) {
1550 /* printf("About to call pval_goto_item, itemcount=%d, itemtype=%d\n", item_count, i->type); */
1551 find_pval_goto_item(i, lev);
1557 /* general purpose label finder */
1558 static struct pval *match_pval_item(pval *item)
1562 switch ( item->type ) {
1564 /* fields: item->u1.str == name of macro
1565 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1566 item->u2.arglist->u1.str == argument
1567 item->u2.arglist->next == next arg
1569 item->u3.macro_statements == pval list of statements in macro body.
1571 /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1572 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1574 /* printf("MACRO: match context is: %s\n", match_context); */
1576 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 */ {
1577 /* printf("Returning on matching macro %s\n", match_context); */
1582 if (!return_on_context_match) {
1583 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1584 if ((x=match_pval(item->u3.macro_statements))) {
1585 /* printf("Responded with pval match %x\n", x); */
1590 /* printf("Skipping context/macro %s\n", item->u1.str); */
1596 /* fields: item->u1.str == name of context
1597 item->u2.statements == pval list of statements in context body
1598 item->u3.abstract == int 1 if an abstract keyword were present
1600 /* printf(" matching in CONTEXT\n"); */
1601 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1602 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1603 /* printf("Returning on matching context %s\n", match_context); */
1604 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1608 if (!return_on_context_match ) {
1609 /* printf("Descending into matching context %s\n", match_context); */
1610 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1611 /* printf("CONTEXT: Responded with pval match %x\n", x); */
1616 /* printf("Skipping context/macro %s\n", item->u1.str); */
1621 /* fields: item->u1.str == value of case
1622 item->u2.statements == pval list of statements under the case
1624 /* printf(" matching in CASE\n"); */
1625 if ((x=match_pval(item->u2.statements))) {
1626 /* printf("CASE: Responded with pval match %x\n", x); */
1632 /* fields: item->u1.str == value of case
1633 item->u2.statements == pval list of statements under the case
1635 /* printf(" matching in PATTERN\n"); */
1636 if ((x=match_pval(item->u2.statements))) {
1637 /* printf("PATTERN: Responded with pval match %x\n", x); */
1644 item->u2.statements == pval list of statements under the case
1646 /* printf(" matching in DEFAULT\n"); */
1647 if ((x=match_pval(item->u2.statements))) {
1648 /* printf("DEFAULT: Responded with pval match %x\n", x); */
1654 /* fields: item->u1.str == name of extension to catch
1655 item->u2.statements == pval list of statements in context body
1657 /* printf(" matching in CATCH\n"); */
1658 if ((x=match_pval(item->u2.statements))) {
1659 /* printf("CATCH: Responded with pval match %x\n", x); */
1664 case PV_STATEMENTBLOCK:
1665 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1667 /* printf(" matching in STATEMENTBLOCK\n"); */
1668 if ((x=match_pval(item->u1.list))) {
1669 /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1675 /* fields: item->u1.str == label name
1677 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1678 item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1681 if (!strcmp(match_label, item->u1.str)) {
1683 last_matched_label = item;
1687 if (!strcmp(match_label, item->u1.str)) {
1688 /* printf("LABEL: Responded with pval match %x\n", x); */
1695 /* fields: item->u1.for_init == a string containing the initalizer
1696 item->u2.for_test == a string containing the loop test
1697 item->u3.for_inc == a string containing the loop increment
1699 item->u4.for_statements == a pval list of statements in the for ()
1701 /* printf(" matching in FOR\n"); */
1702 if ((x=match_pval(item->u4.for_statements))) {
1703 /* printf("FOR: Responded with pval match %x\n", x);*/
1709 /* fields: item->u1.str == the while conditional, as supplied by user
1711 item->u2.statements == a pval list of statements in the while ()
1713 /* printf(" matching in WHILE\n"); */
1714 if ((x=match_pval(item->u2.statements))) {
1715 /* printf("WHILE: Responded with pval match %x\n", x); */
1721 /* fields: item->u1.str == the random number expression, as supplied by user
1723 item->u2.statements == a pval list of statements in the if ()
1724 item->u3.else_statements == a pval list of statements in the else
1726 fall thru to PV_IF */
1729 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1731 item->u2.statements == a pval list of statements in the if ()
1732 item->u3.else_statements == a pval list of statements in the else
1734 fall thru to PV_IF*/
1736 /* fields: item->u1.str == the if conditional, as supplied by user
1738 item->u2.statements == a pval list of statements in the if ()
1739 item->u3.else_statements == a pval list of statements in the else
1742 /* printf(" matching in IF/IFTIME/RANDOM\n"); */
1743 if ((x=match_pval(item->u2.statements))) {
1746 if (item->u3.else_statements) {
1747 if ((x=match_pval(item->u3.else_statements))) {
1748 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1755 /* fields: item->u1.str == the switch expression
1757 item->u2.statements == a pval list of statements in the switch,
1758 (will be case statements, most likely!)
1760 /* printf(" matching in SWITCH\n"); */
1761 if ((x=match_pval(item->u2.statements))) {
1762 /* printf("SWITCH: Responded with pval match %x\n", x); */
1768 /* fields: item->u1.str == the extension name, label, whatever it's called
1770 item->u2.statements == a pval list of statements in the extension
1771 item->u3.hints == a char * hint argument
1772 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1774 /* printf(" matching in EXTENSION\n"); */
1775 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1776 /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1777 if (strcmp(match_label,"1") == 0) {
1778 if (item->u2.statements) {
1779 struct pval *p5 = item->u2.statements;
1780 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1791 if ((x=match_pval(item->u2.statements))) {
1792 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1796 /* printf("Skipping exten %s\n", item->u1.str); */
1800 /* printf(" matching in default = %d\n", item->type); */
1806 struct pval *match_pval(pval *item)
1810 for (i=item; i; i=i->next) {
1812 /* printf(" -- match pval: item %d\n", i->type); */
1814 if ((x = match_pval_item(i))) {
1815 /* printf("match_pval: returning x=%x\n", (int)x); */
1816 return x; /* cut the search short */
1823 int count_labels_in_current_context(char *label)
1827 return_on_context_match = 0;
1828 match_pval(current_context->u2.statements);
1834 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1836 /* printf(" --- Got args %s, %s\n", exten, label); */
1841 return_on_context_match = 0;
1842 match_context = "*";
1844 match_label = label;
1846 ret = match_pval(curr_cont);
1850 /* the target of the goto could be in an included context!! Fancy that!! */
1851 /* look for includes in the current context */
1852 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1853 if (p3->type == PV_INCLUDES) {
1855 for (p4=p3->u1.list; p4; p4=p4->next) {
1856 /* for each context pointed to, find it, then find a context/label that matches the
1858 char *incl_context = p4->u1.str;
1859 /* find a matching context name */
1860 struct pval *that_context = find_context(incl_context);
1863 x3 = find_first_label_in_current_context(label, that_context);
1874 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1876 /* printf(" --- Got args %s, %s\n", exten, label); */
1881 return_on_context_match = 0;
1882 match_context = "*";
1883 match_exten = exten;
1884 match_label = label;
1885 ret = match_pval(curr_cont->u2.statements);
1889 /* the target of the goto could be in an included context!! Fancy that!! */
1890 /* look for includes in the current context */
1891 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1892 if (p3->type == PV_INCLUDES) {
1894 for (p4=p3->u1.list; p4; p4=p4->next) {
1895 /* for each context pointed to, find it, then find a context/label that matches the
1897 char *incl_context = p4->u1.str;
1898 /* find a matching context name */
1899 struct pval *that_context = find_context(incl_context);
1902 x3 = find_label_in_current_context(exten, label, that_context);
1913 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1915 /* printf(" --- Got args %s\n", label); */
1917 return_on_context_match = 0;
1918 match_context = "*";
1920 match_label = label;
1921 return match_pval(curr_ext);
1924 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1926 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1928 return_on_context_match = 0;
1930 match_context = context;
1931 match_exten = exten;
1932 match_label = label;
1934 return match_pval(current_db);
1938 struct pval *find_macro(char *name)
1940 return_on_context_match = 1;
1942 match_context = name;
1943 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1945 return match_pval(current_db);
1948 struct pval *find_context(char *name)
1950 return_on_context_match = 1;
1952 match_context = name;
1953 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1955 return match_pval(current_db);
1958 int is_float(char *arg )
1961 for (s=arg; *s; s++) {
1962 if (*s != '.' && (*s < '0' || *s > '9'))
1967 int is_int(char *arg )
1970 for (s=arg; *s; s++) {
1971 if (*s < '0' || *s > '9')
1976 int is_empty(char *arg)
1983 if (*arg != ' ' && *arg != '\t')
1991 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1993 struct argchoice *ac;
1996 switch (should->dtype) {
1997 case ARGD_OPTIONSET:
1998 if ( strstr(is->u1.str,"${") )
1999 return 0; /* no checking anything if there's a var reference in there! */
2001 opcop = ast_strdupa(is->u1.str);
2003 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
2006 while (*p && *p != ')' )
2012 for (ac=app->opts; ac; ac=ac->next) {
2013 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2016 for (ac=app->opts; ac; ac=ac->next) {
2017 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2018 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2020 if (p && *p == 'j') {
2021 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
2022 is->filename, is->startline, is->endline, app->name);
2028 if (ac->name[1] == '(') {
2029 if (*(p+1) != '(') {
2030 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",
2031 is->filename, is->startline, is->endline, ac->name[0], app->name);
2038 for (q=opcop; *q; q++) {
2039 if ( *q != '+' && *q != '(' && *q != ')') {
2040 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",
2041 is->filename, is->startline, is->endline, *q, app->name);
2053 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2055 struct argchoice *ac;
2058 switch (should->dtype) {
2060 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2062 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2067 if (is_int(is->u1.str))
2074 if (is_float(is->u1.str))
2081 if( !is->u1.str || strlen(is->u1.str) == 0 )
2082 return 1; /* a null arg in the call will match an enum, I guess! */
2083 for (ac=should->choices; ac; ac=ac->next) {
2084 if (strcmp(ac->name,is->u1.str) == 0)
2090 case ARGD_OPTIONSET:
2091 opcop = ast_strdupa(is->u1.str);
2093 for (ac=app->opts; ac; ac=ac->next) {
2094 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2097 for (ac=app->opts; ac; ac=ac->next) {
2098 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2099 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2103 if (ac->name[1] == '(') {
2104 if (*(p+1) == '(') {
2106 while (*q && *q != ')') {
2118 return 1; /* matches anything */
2121 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2125 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2128 struct argdesc *ad = app->args;
2132 for (pa = arglist; pa; pa=pa->next) {
2134 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2135 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2139 /* find the first entry in the ad list that will match */
2141 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2144 z= option_matches( ad, pa, app);
2149 if (ad->type == ARGD_REQUIRED) {
2150 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2151 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2155 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2156 option_matches_j( ad, pa, app);
2162 /* any app nodes left, that are not optional? */
2163 for ( ; ad; ad=ad->next) {
2164 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2167 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2168 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2179 void check_switch_expr(pval *item, struct argapp *apps)
2182 /* get and clean the variable name */
2184 struct argapp *a,*a2;
2185 struct appsetvar *v,*v2;
2186 struct argchoice *c;
2190 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2193 buff1 = ast_strdupa(p);
2195 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2196 buff1[strlen(buff1)-1] = 0;
2197 /* buff1 now contains the variable name */
2199 for (a=apps; a; a=a->next) {
2200 for (v=a->setvars;v;v=v->next) {
2201 if (strcmp(v->name,buff1) == 0) {
2209 /* we have a match, to a variable that has a set of determined values */
2214 /* first of all, does this switch have a default case ? */
2215 for (t=item->u2.statements; t; t=t->next) {
2216 if (t->type == PV_DEFAULT) {
2220 if (t->type == PV_PATTERN) {
2224 if (def || pat) /* nothing to check. All cases accounted for! */
2226 for (c=v->vals; c; c=c->next) {
2228 for (t=item->u2.statements; t; t=t->next) {
2229 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2230 if (!strcmp(t->u1.str,c->name)) {
2237 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2238 item->filename, item->startline, item->endline, item->u1.str, c->name);
2242 /* next, is there an app call in the current exten, that would set this var? */
2244 t = current_extension->u2.statements;
2245 if ( t && t->type == PV_STATEMENTBLOCK )
2246 t = t->u1.statements;
2247 for (; t && t != item; t=t->next) {
2248 if (t->type == PV_APPLICATION_CALL) {
2249 /* find the application that matches the u1.str */
2250 for (a2=apps; a2; a2=a2->next) {
2251 if (strcasecmp(a2->name, t->u1.str)==0) {
2252 for (v2=a2->setvars; v2; v2=v2->next) {
2253 if (strcmp(v2->name, buff1) == 0) {
2254 /* found an app that sets the var */
2268 /* see if it sets the var */
2270 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",
2271 item->filename, item->startline, item->endline, item->u1.str);
2279 /* first of all, does this switch have a default case ? */
2280 for (t=item->u2.statements; t; t=t->next) {
2281 if (t->type == PV_DEFAULT) {
2287 if (def) /* nothing to check. All cases accounted for! */
2289 /* if no default, warn and insert a default case at the end */
2290 p2 = tl->next = calloc(1, sizeof(struct pval));
2292 p2->type = PV_DEFAULT;
2293 p2->startline = tl->startline;
2294 p2->endline = tl->endline;
2295 p2->startcol = tl->startcol;
2296 p2->endcol = tl->endcol;
2297 p2->filename = strdup(tl->filename);
2298 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2299 p2->filename, p2->startline, p2->endline);
2305 static void check_context_names(void)
2308 for (i=current_db; i; i=i->next) {
2309 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2310 for (j=i->next; j; j=j->next) {
2311 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2312 if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
2314 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",
2315 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
2324 static void check_abstract_reference(pval *abstract_context)
2327 /* find some context includes that reference this context */
2330 /* otherwise, print out a warning */
2331 for (i=current_db; i; i=i->next) {
2332 if (i->type == PV_CONTEXT) {
2333 for (j=i->u2. statements; j; j=j->next) {
2334 if ( j->type == PV_INCLUDES ) {
2336 for (p4=j->u1.list; p4; p4=p4->next) {
2337 /* for each context pointed to, find it, then find a context/label that matches the
2339 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2340 return; /* found a match! */
2346 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",
2347 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2352 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2356 struct argapp *app, *found;
2358 struct pval *macro_def;
2359 struct pval *app_def;
2364 switch (item->type) {
2366 /* fields: item->u1.str == string associated with this (word).
2367 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2371 /* fields: item->u1.str == name of macro
2372 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2373 item->u2.arglist->u1.str == argument
2374 item->u2.arglist->next == next arg
2376 item->u3.macro_statements == pval list of statements in macro body.
2378 in_abstract_context = 0;
2379 current_context = item;
2380 current_extension = 0;
2382 check_macro_returns(item);
2384 for (lp=item->u2.arglist; lp; lp=lp->next) {
2387 check_pval(item->u3.macro_statements, apps,in_globals);
2391 /* fields: item->u1.str == name of context
2392 item->u2.statements == pval list of statements in context body
2393 item->u3.abstract == int 1 if an abstract keyword were present
2395 current_context = item;
2396 current_extension = 0;
2397 if ( item->u3.abstract ) {
2398 in_abstract_context = 1;
2399 check_abstract_reference(item);
2401 in_abstract_context = 0;
2402 check_pval(item->u2.statements, apps,in_globals);
2406 /* fields: item->u1.str == name of macro to call
2407 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2408 item->u2.arglist->u1.str == argument
2409 item->u2.arglist->next == next arg
2412 /* if this is a standalone, we will need to make sure the
2413 localized load of extensions.conf is done */
2414 if (!extensions_dot_conf_loaded) {
2415 localized_pbx_load_module();
2416 extensions_dot_conf_loaded++;
2419 macro_def = find_macro(item->u1.str);
2422 struct pbx_find_info pfiq = {.stacklen = 0 };
2423 struct pbx_find_info pfiq2 = {.stacklen = 0 };
2425 /* look for the macro in the extensions.conf world */
2426 pbx_find_extension(NULL, NULL, &pfiq, item->u1.str, "s", 1, NULL, NULL, E_MATCH);
2428 if (pfiq.status != STATUS_SUCCESS) {
2430 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2432 /* look for the macro in the extensions.conf world */
2433 pbx_find_extension(NULL, NULL, &pfiq2, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2435 if (pfiq2.status == STATUS_SUCCESS) {
2436 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",
2437 item->filename, item->startline, item->endline, item->u1.str, item->u1.str);
2440 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (Not even in the extensions.conf stuff!)\n",
2441 item->filename, item->startline, item->endline, item->u1.str);
2446 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s cannot be found in the AEL code!\n",
2447 item->filename, item->startline, item->endline, item->u1.str);
2451 #ifdef THIS_IS_1DOT4
2453 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2455 /* look for the macro in the extensions.conf world */
2456 pbx_find_extension(NULL, NULL, &pfiq, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2458 if (pfiq.status != STATUS_SUCCESS) {
2459 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",
2460 item->filename, item->startline, item->endline, item->u1.str);
2466 } else if (macro_def->type != PV_MACRO) {
2467 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2468 item->filename, item->startline, item->endline, item->u1.str);
2471 /* macro_def is a MACRO, so do the args match in number? */
2475 for (lp=item->u2.arglist; lp; lp=lp->next) {
2478 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2481 if (hereargs != thereargs ) {
2482 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",
2483 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2489 case PV_APPLICATION_CALL:
2490 /* fields: item->u1.str == name of application to call
2491 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2492 item->u2.arglist->u1.str == argument
2493 item->u2.arglist->next == next arg
2495 /* Need to check to see if the application is available! */
2496 app_def = find_context(item->u1.str);
2497 if (app_def && app_def->type == PV_MACRO) {
2498 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2499 item->filename, item->startline, item->endline, item->u1.str);
2502 if (strcasecmp(item->u1.str,"GotoIf") == 0
2503 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2504 || strcasecmp(item->u1.str,"while") == 0
2505 || strcasecmp(item->u1.str,"endwhile") == 0
2506 || strcasecmp(item->u1.str,"random") == 0
2507 || strcasecmp(item->u1.str,"gosub") == 0
2508 || strcasecmp(item->u1.str,"return") == 0
2509 || strcasecmp(item->u1.str,"gosubif") == 0
2510 || strcasecmp(item->u1.str,"continuewhile") == 0
2511 || strcasecmp(item->u1.str,"endwhile") == 0
2512 || strcasecmp(item->u1.str,"execif") == 0
2513 || strcasecmp(item->u1.str,"execiftime") == 0
2514 || strcasecmp(item->u1.str,"exitwhile") == 0
2515 || strcasecmp(item->u1.str,"goto") == 0
2516 || strcasecmp(item->u1.str,"macro") == 0
2517 || strcasecmp(item->u1.str,"macroexclusive") == 0
2518 || strcasecmp(item->u1.str,"macroif") == 0
2519 || strcasecmp(item->u1.str,"stackpop") == 0
2520 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2521 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",
2522 item->filename, item->startline, item->endline, item->u1.str);
2525 if (strcasecmp(item->u1.str,"macroexit") == 0) {
2526 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
2527 item->filename, item->startline, item->endline);
2528 item->type = PV_RETURN;
2535 for (app=apps; app; app=app->next) {
2536 if (strcasecmp(app->name, item->u1.str) == 0) {
2542 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2543 item->filename, item->startline, item->endline, item->u1.str);
2546 check_app_args(item, item->u2.arglist, app);
2551 /* fields: item->u1.str == value of case
2552 item->u2.statements == pval list of statements under the case
2554 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2555 /* find the last statement */
2556 check_pval(item->u2.statements, apps,in_globals);
2560 /* fields: item->u1.str == value of case
2561 item->u2.statements == pval list of statements under the case
2563 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2564 /* find the last statement */
2566 check_pval(item->u2.statements, apps,in_globals);
2571 item->u2.statements == pval list of statements under the case
2574 check_pval(item->u2.statements, apps,in_globals);
2578 /* fields: item->u1.str == name of extension to catch
2579 item->u2.statements == pval list of statements in context body
2581 check_pval(item->u2.statements, apps,in_globals);
2585 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2587 check_pval(item->u1.list, apps,in_globals);
2591 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2593 check_pval(item->u1.list, apps,in_globals);
2597 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2599 check_pval(item->u1.list, apps,in_globals);
2600 check_includes(item);
2601 for (lp=item->u1.list; lp; lp=lp->next){
2602 char *incl_context = lp->u1.str;
2603 struct pval *that_context = find_context(incl_context);
2605 if ( lp->u2.arglist ) {
2606 check_timerange(lp->u2.arglist);
2607 check_dow(lp->u2.arglist->next);
2608 check_day(lp->u2.arglist->next->next);
2609 check_month(lp->u2.arglist->next->next->next);
2613 find_pval_gotos(that_context->u2.statements,0);
2619 case PV_STATEMENTBLOCK:
2620 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2622 check_pval(item->u1.list, apps,in_globals);
2626 /* fields: item->u1.str == variable name
2627 item->u2.val == variable value to assign
2629 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2630 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2631 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);
2632 ast_expr_register_extra_error_info(errmsg);
2633 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2634 ast_expr_clear_extra_error_info();
2635 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2636 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2637 item->filename, item->startline, item->endline, item->u2.val);
2640 check_expr2_input(item,item->u2.val);
2644 case PV_LOCALVARDEC:
2645 /* fields: item->u1.str == variable name
2646 item->u2.val == variable value to assign
2648 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2649 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);
2650 ast_expr_register_extra_error_info(errmsg);
2651 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2652 ast_expr_clear_extra_error_info();
2653 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2654 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2655 item->filename, item->startline, item->endline, item->u2.val);
2658 check_expr2_input(item,item->u2.val);
2662 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2663 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2665 /* don't check goto's in abstract contexts */
2666 if ( in_abstract_context )
2673 /* fields: item->u1.str == label name
2675 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2676 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2677 item->filename, item->startline, item->endline, item->u1.str);
2685 /* fields: item->u1.for_init == a string containing the initalizer
2686 item->u2.for_test == a string containing the loop test
2687 item->u3.for_inc == a string containing the loop increment
2689 item->u4.for_statements == a pval list of statements in the for ()
2691 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);
2692 ast_expr_register_extra_error_info(errmsg);
2694 strp = strchr(item->u1.for_init, '=');
2696 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2698 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
2699 strp = strchr(item->u3.for_inc, '=');
2701 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2703 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2704 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2705 item->filename, item->startline, item->endline, item->u2.for_test);
2708 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2709 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2710 item->filename, item->startline, item->endline, item->u3.for_inc);
2713 check_expr2_input(item,item->u2.for_test);
2714 check_expr2_input(item,item->u3.for_inc);
2716 ast_expr_clear_extra_error_info();
2717 check_pval(item->u4.for_statements, apps,in_globals);
2721 /* fields: item->u1.str == the while conditional, as supplied by user
2723 item->u2.statements == a pval list of statements in the while ()
2725 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);
2726 ast_expr_register_extra_error_info(errmsg);
2727 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2728 ast_expr_clear_extra_error_info();
2729 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2730 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2731 item->filename, item->startline, item->endline, item->u1.str);
2734 check_expr2_input(item,item->u1.str);
2735 check_pval(item->u2.statements, apps,in_globals);
2752 check_continue(item);
2756 /* fields: item->u1.str == the random number expression, as supplied by user
2758 item->u2.statements == a pval list of statements in the if ()
2759 item->u3.else_statements == a pval list of statements in the else
2762 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);
2763 ast_expr_register_extra_error_info(errmsg);
2764 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2765 ast_expr_clear_extra_error_info();
2766 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2767 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2768 item->filename, item->startline, item->endline, item->u1.str);
2771 check_expr2_input(item,item->u1.str);
2772 check_pval(item->u2.statements, apps,in_globals);
2773 if (item->u3.else_statements) {
2774 check_pval(item->u3.else_statements, apps,in_globals);
2779 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2781 item->u2.statements == a pval list of statements in the if ()
2782 item->u3.else_statements == a pval list of statements in the else
2785 if ( item->u2.arglist ) {
2786 check_timerange(item->u1.list);
2787 check_dow(item->u1.list->next);
2788 check_day(item->u1.list->next->next);
2789 check_month(item->u1.list->next->next->next);
2792 check_pval(item->u2.statements, apps,in_globals);
2793 if (item->u3.else_statements) {
2794 check_pval(item->u3.else_statements, apps,in_globals);
2799 /* fields: item->u1.str == the if conditional, as supplied by user
2801 item->u2.statements == a pval list of statements in the if ()
2802 item->u3.else_statements == a pval list of statements in the else
2805 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);
2806 ast_expr_register_extra_error_info(errmsg);
2807 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2808 ast_expr_clear_extra_error_info();
2809 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2810 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2811 item->filename, item->startline, item->endline, item->u1.str);
2814 check_expr2_input(item,item->u1.str);
2815 check_pval(item->u2.statements, apps,in_globals);
2816 if (item->u3.else_statements) {
2817 check_pval(item->u3.else_statements, apps,in_globals);
2822 /* fields: item->u1.str == the switch expression
2824 item->u2.statements == a pval list of statements in the switch,
2825 (will be case statements, most likely!)
2827 /* we can check the switch expression, see if it matches any of the app variables...
2828 if it does, then, are all the possible cases accounted for? */
2829 check_switch_expr(item, apps);
2830 check_pval(item->u2.statements, apps,in_globals);
2834 /* fields: item->u1.str == the extension name, label, whatever it's called
2836 item->u2.statements == a pval list of statements in the extension
2837 item->u3.hints == a char * hint argument
2838 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2840 current_extension = item ;
2842 check_pval(item->u2.statements, apps,in_globals);
2846 /* fields: item->u1.str == the ignorepat data
2851 /* fields: item->u1.statements == pval list of statements, usually vardecs
2853 in_abstract_context = 0;
2854 check_pval(item->u1.statements, apps, 1);
2861 void check_pval(pval *item, struct argapp *apps, int in_globals)
2866 1. Do goto's point to actual labels?
2867 2. Do macro calls reference a macro?
2868 3. Does the number of macro args match the definition?
2869 4. Is a macro call missing its & at the front?
2870 5. Application calls-- we could check syntax for existing applications,
2871 but I need some some sort of universal description bnf for a general
2872 sort of method for checking arguments, in number, maybe even type, at least.
2873 Don't want to hand code checks for hundreds of applications.
2876 for (i=item; i; i=i->next) {
2877 check_pval_item(i,apps,in_globals);
2881 void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2888 struct argapp *apps=0;
2891 return; /* don't check an empty tree */
2893 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2894 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2896 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2899 errs = warns = notes = 0;
2901 check_context_names();
2902 check_pval(item, apps, 0);
2905 argdesc_destroy(apps); /* taketh away */
2914 /* =============================================================================================== */
2915 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2916 /* =============================================================================================== */
2918 static int control_statement_count = 0;
2920 struct ael_priority *new_prio(void)
2922 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2926 struct ael_extension *new_exten(void)
2928 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2932 void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten)
2936 if (!exten->plist) {
2937 exten->plist = prio;
2938 exten->plist_last = prio;
2940 exten->plist_last->next = prio;
2941 exten->plist_last = prio;
2944 prio->exten = exten; /* don't override the switch value */
2945 /* The following code will cause all priorities within an extension
2946 to have ${EXTEN} or ${EXTEN: replaced with ~~EXTEN~~, which is
2947 set just before the first switch in an exten. The switches
2948 will muck up the original ${EXTEN} value, so we save it away
2949 and the user accesses this copy instead. */
2950 if (prio->appargs && ((mother_exten && mother_exten->has_switch) || exten->has_switch) ) {
2951 while ((p1 = strstr(prio->appargs, "${EXTEN}"))) {
2952 p2 = malloc(strlen(prio->appargs)+5);
2954 strcpy(p2, prio->appargs);
2955 strcat(p2, "${~~EXTEN~~}");
2958 free(prio->appargs);
2961 while ((p1 = strstr(prio->appargs, "${EXTEN:"))) {
2962 p2 = malloc(strlen(prio->appargs)+5);
2964 strcpy(p2, prio->appargs);
2965 strcat(p2, "${~~EXTEN~~:");
2968 free(prio->appargs);
2974 void destroy_extensions(struct ael_extension *exten)
2976 struct ael_extension *ne, *nen;
2977 for (ne=exten; ne; ne=nen) {
2978 struct ael_priority *pe, *pen;
2983 /* cidmatch fields are allocated with name, and freed when
2984 the name field is freed. Don't do a free for this field,
2985 unless you LIKE to see a crash! */
2990 for (pe=ne->plist; pe; pe=pen) {
3003 nen = ne->next_exten;
3009 ne->loop_continue = 0;
3014 static int label_inside_case(pval *label)
3018 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
3019 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
3028 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
3030 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
3031 exten->next_exten = add;
3034 static void remove_spaces_before_equals(char *str)
3037 while( str && *str && *str != '=' )
3039 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
3053 /* =============================================================================================== */
3054 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
3055 /* =============================================================================================== */
3057 static void gen_match_to_pattern(char *pattern, char *result)
3059 /* the result will be a string that will be matched by pattern */
3060 char *p=pattern, *t=result;
3062 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
3064 else if (*p == '[') {
3070 *t++=*(p+1); /* use the first char in the set */
3077 *t++ = 0; /* cap it off */
3080 /* ==== a set of routines to search for a switch statement contained in the pval description */
3082 int find_switch_item(pval *item);
3083 int contains_switch(pval *item);
3086 int find_switch_item(pval *item)
3088 switch ( item->type ) {
3089 case PV_LOCALVARDEC:
3090 /* fields: item->u1.str == string associated with this (word). */
3094 /* fields: item->u1.str == string associated with this (word). */
3098 /* fields: item->u1.str == name of macro
3099 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
3100 item->u2.arglist->u1.str == argument
3101 item->u2.arglist->next == next arg
3103 item->u3.macro_statements == pval list of statements in macro body.
3105 /* had better not see this */
3106 if (contains_switch(item->u3.macro_statements))
3111 /* fields: item->u1.str == name of context
3112 item->u2.statements == pval list of statements in context body
3113 item->u3.abstract == int 1 if an abstract keyword were present
3115 /* had better not see this */
3116 if (contains_switch(item->u2.statements))
3121 /* fields: item->u1.str == name of macro to call
3122 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
3123 item->u2.arglist->u1.str == argument
3124 item->u2.arglist->next == next arg
3128 case PV_APPLICATION_CALL:
3129 /* fields: item->u1.str == name of application to call
3130 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
3131 item->u2.arglist->u1.str == argument
3132 item->u2.arglist->next == next arg
3137 /* fields: item->u1.str == value of case
3138 item->u2.statements == pval list of statements under the case
3140 /* had better not see this */
3141 if (contains_switch(item->u2.statements))
3146 /* fields: item->u1.str == value of case
3147 item->u2.statements == pval list of statements under the case
3149 /* had better not see this */
3150 if (contains_switch(item->u2.statements))
3156 item->u2.statements == pval list of statements under the case
3158 /* had better not see this */
3159 if (contains_switch(item->u2.statements))
3164 /* fields: item->u1.str == name of extension to catch
3165 item->u2.statements == pval list of statements in context body
3167 /* had better not see this */
3168 if (contains_switch(item->u2.statements))
3173 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
3178 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
3183 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
3184 item->u2.arglist == pval list of 4 PV_WORD elements for time values
3188 case PV_STATEMENTBLOCK:
3189 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
3191 if (contains_switch(item->u1.list) )
3196 /* fields: item->u1.str == variable name
3197 item->u2.val == variable value to assign
3202 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.