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/callerid.h"
47 #include "asterisk/pval.h"
48 #include "asterisk/ael_structs.h"
50 #include "asterisk/argdesc.h"
52 #include "asterisk/utils.h"
54 extern int localized_pbx_load_module(void);
56 static char expr_output[2096];
57 #define AST_PBX_MAX_STACK 128
59 /* these functions are in ../ast_expr2.fl */
61 static int errs, warns;
64 static int extensions_dot_conf_loaded = 0;
66 static char *registrar = "pbx_ael";
68 static pval *current_db;
69 static pval *current_context;
70 static pval *current_extension;
72 static const char *match_context;
73 static const char *match_exten;
74 static const char *match_label;
75 static int in_abstract_context;
76 static int count_labels; /* true, put matcher in label counting mode */
77 static int label_count; /* labels are only meant to be counted in a context or exten */
78 static int return_on_context_match;
79 static pval *last_matched_label;
80 struct pval *match_pval(pval *item);
81 static void check_timerange(pval *p);
82 static void check_dow(pval *DOW);
83 static void check_day(pval *DAY);
84 static void check_month(pval *MON);
85 static void check_expr2_input(pval *expr, char *str);
86 static int extension_matches(pval *here, const char *exten, const char *pattern);
87 static void check_goto(pval *item);
88 static void find_pval_goto_item(pval *item, int lev);
89 static void find_pval_gotos(pval *item, int lev);
90 static int check_break(pval *item);
91 static int check_continue(pval *item);
92 static void check_label(pval *item);
93 static void check_macro_returns(pval *macro);
95 static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
96 static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
97 static void print_pval_list(FILE *fin, pval *item, int depth);
99 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
100 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
101 static pval *get_goto_target(pval *item);
102 static int label_inside_case(pval *label);
103 static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
104 static void fix_gotos_in_extensions(struct ael_extension *exten);
105 static pval *get_extension_or_contxt(pval *p);
106 static pval *get_contxt(pval *p);
107 static void remove_spaces_before_equals(char *str);
109 /* PRETTY PRINTER FOR AEL: ============================================================================= */
111 static void print_pval(FILE *fin, pval *item, int depth)
116 for (i=0; i<depth; i++) {
117 fprintf(fin, "\t"); /* depth == indentation */
120 switch ( item->type ) {
122 fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
126 fprintf(fin,"macro %s(", item->u1.str);
127 for (lp=item->u2.arglist; lp; lp=lp->next) {
128 if (lp != item->u2.arglist )
130 fprintf(fin,"%s", lp->u1.str);
132 fprintf(fin,") {\n");
133 print_pval_list(fin,item->u3.macro_statements,depth+1);
134 for (i=0; i<depth; i++) {
135 fprintf(fin,"\t"); /* depth == indentation */
137 fprintf(fin,"};\n\n");
141 if ( item->u3.abstract )
142 fprintf(fin,"abstract context %s {\n", item->u1.str);
144 fprintf(fin,"context %s {\n", item->u1.str);
145 print_pval_list(fin,item->u2.statements,depth+1);
146 for (i=0; i<depth; i++) {
147 fprintf(fin,"\t"); /* depth == indentation */
149 fprintf(fin,"};\n\n");
153 fprintf(fin,"&%s(", item->u1.str);
154 for (lp=item->u2.arglist; lp; lp=lp->next) {
155 if ( lp != item->u2.arglist )
157 fprintf(fin,"%s", lp->u1.str);
162 case PV_APPLICATION_CALL:
163 fprintf(fin,"%s(", item->u1.str);
164 for (lp=item->u2.arglist; lp; lp=lp->next) {
165 if ( lp != item->u2.arglist )
167 fprintf(fin,"%s", lp->u1.str);
173 fprintf(fin,"case %s:\n", item->u1.str);
174 print_pval_list(fin,item->u2.statements, depth+1);
178 fprintf(fin,"pattern %s:\n", item->u1.str);
179 print_pval_list(fin,item->u2.statements, depth+1);
183 fprintf(fin,"default:\n");
184 print_pval_list(fin,item->u2.statements, depth+1);
188 fprintf(fin,"catch %s {\n", item->u1.str);
189 print_pval_list(fin,item->u2.statements, depth+1);
190 for (i=0; i<depth; i++) {
191 fprintf(fin,"\t"); /* depth == indentation */
197 fprintf(fin,"switches {\n");
198 print_pval_list(fin,item->u1.list,depth+1);
199 for (i=0; i<depth; i++) {
200 fprintf(fin,"\t"); /* depth == indentation */
206 fprintf(fin,"eswitches {\n");
207 print_pval_list(fin,item->u1.list,depth+1);
208 for (i=0; i<depth; i++) {
209 fprintf(fin,"\t"); /* depth == indentation */
215 fprintf(fin,"includes {\n");
216 for (lp=item->u1.list; lp; lp=lp->next) {
217 for (i=0; i<depth+1; i++) {
218 fprintf(fin,"\t"); /* depth == indentation */
220 fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
222 fprintf(fin,"|%s|%s|%s|%s",
223 lp->u2.arglist->u1.str,
224 lp->u2.arglist->next->u1.str,
225 lp->u2.arglist->next->next->u1.str,
226 lp->u2.arglist->next->next->next->u1.str
228 fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
231 for (i=0; i<depth; i++) {
232 fprintf(fin,"\t"); /* depth == indentation */
237 case PV_STATEMENTBLOCK:
239 print_pval_list(fin,item->u1.list, depth+1);
240 for (i=0; i<depth; i++) {
241 fprintf(fin,"\t"); /* depth == indentation */
247 fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
251 fprintf(fin,"local %s=%s;\n", item->u1.str, item->u2.val);
255 fprintf(fin,"goto %s", item->u1.list->u1.str);
256 if ( item->u1.list->next )
257 fprintf(fin,",%s", item->u1.list->next->u1.str);
258 if ( item->u1.list->next && item->u1.list->next->next )
259 fprintf(fin,",%s", item->u1.list->next->next->u1.str);
264 fprintf(fin,"%s:\n", item->u1.str);
268 fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
269 print_pval_list(fin,item->u4.for_statements,depth+1);
273 fprintf(fin,"while (%s)\n", item->u1.str);
274 print_pval_list(fin,item->u2.statements,depth+1);
278 fprintf(fin,"break;\n");
282 fprintf(fin,"return;\n");
286 fprintf(fin,"continue;\n");
292 if ( item->type == PV_IFTIME ) {
294 fprintf(fin,"ifTime ( %s|%s|%s|%s )\n",
295 item->u1.list->u1.str,
296 item->u1.list->next->u1.str,
297 item->u1.list->next->next->u1.str,
298 item->u1.list->next->next->next->u1.str
300 } else if ( item->type == PV_RANDOM ) {
301 fprintf(fin,"random ( %s )\n", item->u1.str );
303 fprintf(fin,"if ( %s )\n", item->u1.str);
304 if ( item->u2.statements && item->u2.statements->next ) {
305 for (i=0; i<depth; i++) {
306 fprintf(fin,"\t"); /* depth == indentation */
309 print_pval_list(fin,item->u2.statements,depth+1);
310 for (i=0; i<depth; i++) {
311 fprintf(fin,"\t"); /* depth == indentation */
313 if ( item->u3.else_statements )
317 } else if (item->u2.statements ) {
318 print_pval_list(fin,item->u2.statements,depth+1);
320 if (item->u3.else_statements )
321 fprintf(fin, " {} ");
323 fprintf(fin, " {}; ");
325 if ( item->u3.else_statements ) {
326 for (i=0; i<depth; i++) {
327 fprintf(fin,"\t"); /* depth == indentation */
329 fprintf(fin,"else\n");
330 print_pval_list(fin,item->u3.else_statements, depth);
335 fprintf(fin,"switch( %s ) {\n", item->u1.str);
336 print_pval_list(fin,item->u2.statements,depth+1);
337 for (i=0; i<depth; i++) {
338 fprintf(fin,"\t"); /* depth == indentation */
344 if ( item->u4.regexten )
345 fprintf(fin, "regexten ");
346 if ( item->u3.hints )
347 fprintf(fin,"hints(%s) ", item->u3.hints);
349 fprintf(fin,"%s => ", item->u1.str);
350 print_pval_list(fin,item->u2.statements,depth+1);
355 fprintf(fin,"ignorepat => %s;\n", item->u1.str);
359 fprintf(fin,"globals {\n");
360 print_pval_list(fin,item->u1.statements,depth+1);
361 for (i=0; i<depth; i++) {
362 fprintf(fin,"\t"); /* depth == indentation */
369 static void print_pval_list(FILE *fin, pval *item, int depth)
373 for (i=item; i; i=i->next) {
374 print_pval(fin, i, depth);
378 void ael2_print(char *fname, pval *tree)
380 FILE *fin = fopen(fname,"w");
382 ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
385 print_pval_list(fin, tree, 0);
390 /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL: ============================================================================= */
392 void traverse_pval_template(pval *item, int depth);
393 void traverse_pval_item_template(pval *item, int depth);
396 void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
397 but you may not need it */
401 switch ( item->type ) {
403 /* fields: item->u1.str == string associated with this (word). */
407 /* fields: item->u1.str == name of macro
408 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
409 item->u2.arglist->u1.str == argument
410 item->u2.arglist->next == next arg
412 item->u3.macro_statements == pval list of statements in macro body.
414 for (lp=item->u2.arglist; lp; lp=lp->next) {
417 traverse_pval_item_template(item->u3.macro_statements,depth+1);
421 /* fields: item->u1.str == name of context
422 item->u2.statements == pval list of statements in context body
423 item->u3.abstract == int 1 if an abstract keyword were present
425 traverse_pval_item_template(item->u2.statements,depth+1);
429 /* fields: item->u1.str == name of macro to call
430 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
431 item->u2.arglist->u1.str == argument
432 item->u2.arglist->next == next arg
434 for (lp=item->u2.arglist; lp; lp=lp->next) {
438 case PV_APPLICATION_CALL:
439 /* fields: item->u1.str == name of application to call
440 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
441 item->u2.arglist->u1.str == argument
442 item->u2.arglist->next == next arg
444 for (lp=item->u2.arglist; lp; lp=lp->next) {
449 /* fields: item->u1.str == value of case
450 item->u2.statements == pval list of statements under the case
452 traverse_pval_item_template(item->u2.statements,depth+1);
456 /* fields: item->u1.str == value of case
457 item->u2.statements == pval list of statements under the case
459 traverse_pval_item_template(item->u2.statements,depth+1);
464 item->u2.statements == pval list of statements under the case
466 traverse_pval_item_template(item->u2.statements,depth+1);
470 /* fields: item->u1.str == name of extension to catch
471 item->u2.statements == pval list of statements in context body
473 traverse_pval_item_template(item->u2.statements,depth+1);
477 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
479 traverse_pval_item_template(item->u1.list,depth+1);
483 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
485 traverse_pval_item_template(item->u1.list,depth+1);
489 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
490 item->u2.arglist == pval list of 4 PV_WORD elements for time values
492 traverse_pval_item_template(item->u1.list,depth+1);
493 traverse_pval_item_template(item->u2.arglist,depth+1);
496 case PV_STATEMENTBLOCK:
497 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
499 traverse_pval_item_template(item->u1.list,depth+1);
504 /* fields: item->u1.str == variable name
505 item->u2.val == variable value to assign
510 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
511 item->u1.list->u1.str == where the data on a PV_WORD will always be.
514 if ( item->u1.list->next )
516 if ( item->u1.list->next && item->u1.list->next->next )
522 /* fields: item->u1.str == label name
527 /* fields: item->u1.for_init == a string containing the initalizer
528 item->u2.for_test == a string containing the loop test
529 item->u3.for_inc == a string containing the loop increment
531 item->u4.for_statements == a pval list of statements in the for ()
533 traverse_pval_item_template(item->u4.for_statements,depth+1);
537 /* fields: item->u1.str == the while conditional, as supplied by user
539 item->u2.statements == a pval list of statements in the while ()
541 traverse_pval_item_template(item->u2.statements,depth+1);
560 /* fields: item->u1.list == there are 4 linked PV_WORDs here.
562 item->u2.statements == a pval list of statements in the if ()
563 item->u3.else_statements == a pval list of statements in the else
566 traverse_pval_item_template(item->u2.statements,depth+1);
567 if ( item->u3.else_statements ) {
568 traverse_pval_item_template(item->u3.else_statements,depth+1);
573 /* fields: item->u1.str == the random number expression, as supplied by user
575 item->u2.statements == a pval list of statements in the if ()
576 item->u3.else_statements == a pval list of statements in the else
579 traverse_pval_item_template(item->u2.statements,depth+1);
580 if ( item->u3.else_statements ) {
581 traverse_pval_item_template(item->u3.else_statements,depth+1);
586 /* fields: item->u1.str == the if conditional, as supplied by user
588 item->u2.statements == a pval list of statements in the if ()
589 item->u3.else_statements == a pval list of statements in the else
592 traverse_pval_item_template(item->u2.statements,depth+1);
593 if ( item->u3.else_statements ) {
594 traverse_pval_item_template(item->u3.else_statements,depth+1);
599 /* fields: item->u1.str == the switch expression
601 item->u2.statements == a pval list of statements in the switch,
602 (will be case statements, most likely!)
604 traverse_pval_item_template(item->u2.statements,depth+1);
608 /* fields: item->u1.str == the extension name, label, whatever it's called
610 item->u2.statements == a pval list of statements in the extension
611 item->u3.hints == a char * hint argument
612 item->u4.regexten == an int boolean. non-zero says that regexten was specified
614 traverse_pval_item_template(item->u2.statements,depth+1);
618 /* fields: item->u1.str == the ignorepat data
623 /* fields: item->u1.statements == pval list of statements, usually vardecs
625 traverse_pval_item_template(item->u1.statements,depth+1);
630 void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
631 but you may not need it */
635 for (i=item; i; i=i->next) {
636 traverse_pval_item_template(i, depth);
641 /* SEMANTIC CHECKING FOR AEL: ============================================================================= */
643 /* (not all that is syntactically legal is good! */
646 static void check_macro_returns(pval *macro)
649 if (!macro->u3.macro_statements)
651 pval *z = calloc(1, sizeof(struct pval));
652 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s is empty! I will insert a return.\n",
653 macro->filename, macro->startline, macro->endline, macro->u1.str);
656 z->startline = macro->startline;
657 z->endline = macro->endline;
658 z->startcol = macro->startcol;
659 z->endcol = macro->endcol;
660 z->filename = strdup(macro->filename);
662 macro->u3.macro_statements = z;
665 for (i=macro->u3.macro_statements; i; i=i->next) {
666 /* if the last statement in the list is not return, then insert a return there */
667 if (i->next == NULL) {
668 if (i->type != PV_RETURN) {
669 pval *z = calloc(1, sizeof(struct pval));
670 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s does not end with a return; I will insert one.\n",
671 macro->filename, macro->startline, macro->endline, macro->u1.str);
674 z->startline = macro->startline;
675 z->endline = macro->endline;
676 z->startcol = macro->startcol;
677 z->endcol = macro->endcol;
678 z->filename = strdup(macro->filename);
690 static int extension_matches(pval *here, const char *exten, const char *pattern)
695 /* simple case, they match exactly, the pattern and exten name */
696 if (!strcmp(pattern,exten) == 0)
699 if (pattern[0] == '_') {
704 if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
705 ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
709 /* form a regular expression from the pattern, and then match it against exten */
710 *r++ = '^'; /* what if the extension is a pattern ?? */
711 *r++ = '_'; /* what if the extension is a pattern ?? */
713 for (p=pattern+1; *p; p++) {
743 while ( *p && *p != ']' ) {
747 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
748 here->filename, here->startline, here->endline, pattern);
767 *r++ = '$'; /* what if the extension is a pattern ?? */
768 *r++ = *p++; /* put in the closing null */
769 err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
772 regerror(err1,&preg,errmess,sizeof(errmess));
774 ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
778 err1 = regexec(&preg, exten, 0, 0, 0);
782 /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
783 err1,exten, pattern, reg1); */
784 return 0; /* no match */
786 /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
793 if ( strcmp(exten,pattern) == 0 ) {
801 static void check_expr2_input(pval *expr, char *str)
803 int spaces = strspn(str,"\t \n");
804 if ( !strncmp(str+spaces,"$[",2) ) {
805 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
806 expr->filename, expr->startline, expr->endline, str);
811 static void check_includes(pval *includes)
814 for (p4=includes->u1.list; p4; p4=p4->next) {
815 /* for each context pointed to, find it, then find a context/label that matches the
817 char *incl_context = p4->u1.str;
818 /* find a matching context name */
819 struct pval *that_other_context = find_context(incl_context);
820 if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
821 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n\
822 (You may ignore this warning if '%s' exists in extensions.conf, or is created by another module. I cannot check for those.)\n",
823 includes->filename, includes->startline, includes->endline, incl_context, incl_context);
830 static void check_timerange(pval *p)
837 times = ast_strdupa(p->u1.str);
839 /* Star is all times */
840 if (ast_strlen_zero(times) || !strcmp(times, "*")) {
843 /* Otherwise expect a range */
844 e = strchr(times, '-');
846 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",
847 p->filename, p->startline, p->endline, times);
853 while (*e && !isdigit(*e))
856 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
857 p->filename, p->startline, p->endline, p->u1.str);
860 if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
861 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
862 p->filename, p->startline, p->endline, times);
865 if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
866 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
867 p->filename, p->startline, p->endline, times);
872 if ((s1 < 0) || (s1 >= 24*30)) {
873 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
874 p->filename, p->startline, p->endline, times);
878 if ((e1 < 0) || (e1 >= 24*30)) {
879 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
880 p->filename, p->startline, p->endline, e);
886 static char *days[] =
897 /*! \brief get_dow: Get day of week */
898 static void check_dow(pval *DOW)
902 /* The following line is coincidence, really! */
905 dow = ast_strdupa(DOW->u1.str);
907 /* Check for all days */
908 if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
910 /* Get start and ending days */
911 c = strchr(dow, '-');
919 while ((s < 7) && strcasecmp(dow, days[s])) s++;
921 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",
922 DOW->filename, DOW->startline, DOW->endline, dow);
927 while ((e < 7) && strcasecmp(c, days[e])) e++;
929 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",
930 DOW->filename, DOW->startline, DOW->endline, c);
937 static void check_day(pval *DAY)
941 /* The following line is coincidence, really! */
944 day = ast_strdupa(DAY->u1.str);
946 /* Check for all days */
947 if (ast_strlen_zero(day) || !strcmp(day, "*")) {
950 /* Get start and ending days */
951 c = strchr(day, '-');
957 if (sscanf(day, "%d", &s) != 1) {
958 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
959 DAY->filename, DAY->startline, DAY->endline, day);
962 else if ((s < 1) || (s > 31)) {
963 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",
964 DAY->filename, DAY->startline, DAY->endline, day);
969 if (sscanf(c, "%d", &e) != 1) {
970 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
971 DAY->filename, DAY->startline, DAY->endline, c);
974 else if ((e < 1) || (e > 31)) {
975 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",
976 DAY->filename, DAY->startline, DAY->endline, day);
984 static char *months[] =
1000 static void check_month(pval *MON)
1004 /* The following line is coincidence, really! */
1007 mon = ast_strdupa(MON->u1.str);
1009 /* Check for all days */
1010 if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
1012 /* Get start and ending days */
1013 c = strchr(mon, '-');
1018 /* Find the start */
1020 while ((s < 12) && strcasecmp(mon, months[s])) s++;
1022 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1023 MON->filename, MON->startline, MON->endline, mon);
1028 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1030 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1031 MON->filename, MON->startline, MON->endline, c);
1038 static int check_break(pval *item)
1042 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1043 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1045 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
1046 || p->type == PV_WHILE || p->type == PV_FOR ) {
1051 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1052 item->filename, item->startline, item->endline);
1058 static int check_continue(pval *item)
1062 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1063 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1065 if( p->type == PV_WHILE || p->type == PV_FOR ) {
1070 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1071 item->filename, item->startline, item->endline);
1077 static struct pval *in_macro(pval *item)
1082 if( curr->type == PV_MACRO ) {
1090 static struct pval *in_context(pval *item)
1095 if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
1104 /* general purpose goto finder */
1106 static void check_label(pval *item)
1112 /* A label outside an extension just plain does not make sense! */
1117 if( curr->type == PV_MACRO || curr->type == PV_EXTENSION ) {
1125 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
1126 item->filename, item->startline, item->endline, item->u1.str);
1131 /* basically, ensure that a label is not repeated in a context. Period.
1132 The method: well, for each label, find the first label in the context
1133 with the same name. If it's not the current label, then throw an error. */
1136 /* printf("==== check_label: ====\n"); */
1137 if( !current_extension )
1138 curr = current_context;
1140 curr = current_extension;
1142 x = find_first_label_in_current_context((char *)item->u1.str, curr);
1143 /* 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); */
1144 if( x && x != item )
1146 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1147 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1150 /* printf("<<<<< check_label: ====\n"); */
1153 static pval *get_goto_target(pval *item)
1155 /* just one item-- the label should be in the current extension */
1156 pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1159 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1160 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1164 curr_cont = get_contxt(item);
1167 if (item->u1.list->next && !item->u1.list->next->next) {
1168 if (!strstr((item->u1.list)->u1.str,"${")
1169 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1170 struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1176 if (item->u1.list->next && item->u1.list->next->next) {
1178 pval *first = item->u1.list;
1179 pval *second = item->u1.list->next;
1180 pval *third = item->u1.list->next->next;
1182 if (!strstr((item->u1.list)->u1.str,"${")
1183 && !strstr(item->u1.list->next->u1.str,"${")
1184 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1185 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1189 struct pval *that_context = find_context(item->u1.list->u1.str);
1191 /* the target of the goto could be in an included context!! Fancy that!! */
1192 /* look for includes in the current context */
1194 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1195 if (p3->type == PV_INCLUDES) {
1197 for (p4=p3->u1.list; p4; p4=p4->next) {
1198 /* for each context pointed to, find it, then find a context/label that matches the
1200 char *incl_context = p4->u1.str;
1201 /* find a matching context name */
1202 struct pval *that_other_context = find_context(incl_context);
1203 if (that_other_context) {
1205 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1221 static void check_goto(pval *item)
1223 /* check for the target of the goto-- does it exist? */
1224 if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1225 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
1226 item->filename, item->startline, item->endline);
1230 /* just one item-- the label should be in the current extension */
1232 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1233 struct pval *z = get_extension_or_contxt(item);
1236 x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1237 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1238 (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1240 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
1241 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1249 if (item->u1.list->next && !item->u1.list->next->next) {
1251 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1252 (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1253 if (!strstr((item->u1.list)->u1.str,"${")
1254 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1255 struct pval *z = get_contxt(item);
1259 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1262 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",
1263 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1272 if (item->u1.list->next && item->u1.list->next->next) {
1274 pval *first = item->u1.list;
1275 pval *second = item->u1.list->next;
1276 pval *third = item->u1.list->next->next;
1278 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1279 (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1280 if (!strstr((item->u1.list)->u1.str,"${")
1281 && !strstr(item->u1.list->next->u1.str,"${")
1282 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1283 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1286 struct pval *found = 0;
1287 struct pval *that_context = find_context(item->u1.list->u1.str);
1289 /* the target of the goto could be in an included context!! Fancy that!! */
1290 /* look for includes in the current context */
1292 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1293 if (p3->type == PV_INCLUDES) {
1295 for (p4=p3->u1.list; p4; p4=p4->next) {
1296 /* for each context pointed to, find it, then find a context/label that matches the
1298 char *incl_context = p4->u1.str;
1299 /* find a matching context name */
1300 struct pval *that_other_context = find_context(incl_context);
1301 if (that_other_context) {
1303 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1313 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
1314 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1317 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1318 if( mac ) { /* yes! */
1319 struct pval *targ = in_context(found);
1322 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",
1323 item->filename, item->startline, item->endline);
1329 /* here is where code would go to check for target existence in extensions.conf files */
1331 struct pbx_find_info pfiq = {.stacklen = 0 };
1332 extern int localized_pbx_load_module(void);
1333 /* if this is a standalone, we will need to make sure the
1334 localized load of extensions.conf is done */
1335 if (!extensions_dot_conf_loaded) {
1336 localized_pbx_load_module();
1337 extensions_dot_conf_loaded++;
1340 pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
1341 atoi(third->u1.str) ? NULL : third->u1.str, NULL,
1342 atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
1344 if (pfiq.status != STATUS_SUCCESS) {
1345 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",
1346 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1350 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: Couldn't find goto target %s|%s|%s in the AEL code!\n",
1351 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1356 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1357 if( mac ) { /* yes! */
1358 struct pval *targ = in_context(x);
1361 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",
1362 item->filename, item->startline, item->endline);
1372 static void find_pval_goto_item(pval *item, int lev)
1376 ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
1380 switch ( item->type ) {
1382 /* fields: item->u1.str == name of macro
1383 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1384 item->u2.arglist->u1.str == argument
1385 item->u2.arglist->next == next arg
1387 item->u3.macro_statements == pval list of statements in macro body.
1390 /* printf("Descending into matching macro %s\n", match_context); */
1391 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1396 /* fields: item->u1.str == name of context
1397 item->u2.statements == pval list of statements in context body
1398 item->u3.abstract == int 1 if an abstract keyword were present
1403 /* fields: item->u1.str == value of case
1404 item->u2.statements == pval list of statements under the case
1406 find_pval_gotos(item->u2.statements,lev+1);
1410 /* fields: item->u1.str == value of case
1411 item->u2.statements == pval list of statements under the case
1413 find_pval_gotos(item->u2.statements,lev+1);
1418 item->u2.statements == pval list of statements under the case
1420 find_pval_gotos(item->u2.statements,lev+1);
1424 /* fields: item->u1.str == name of extension to catch
1425 item->u2.statements == pval list of statements in context body
1427 find_pval_gotos(item->u2.statements,lev+1);
1430 case PV_STATEMENTBLOCK:
1431 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1433 find_pval_gotos(item->u1.list,lev+1);
1437 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1438 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1440 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1444 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1446 for (p4=item->u1.list; p4; p4=p4->next) {
1447 /* for each context pointed to, find it, then find a context/label that matches the
1449 char *incl_context = p4->u1.str;
1450 /* find a matching context name */
1451 struct pval *that_context = find_context(incl_context);
1453 find_pval_gotos(that_context,lev+1); /* keep working up the includes */
1459 /* fields: item->u1.for_init == a string containing the initalizer
1460 item->u2.for_test == a string containing the loop test
1461 item->u3.for_inc == a string containing the loop increment
1463 item->u4.for_statements == a pval list of statements in the for ()
1465 find_pval_gotos(item->u4.for_statements,lev+1);
1469 /* fields: item->u1.str == the while conditional, as supplied by user
1471 item->u2.statements == a pval list of statements in the while ()
1473 find_pval_gotos(item->u2.statements,lev+1);
1477 /* fields: item->u1.str == the random number expression, as supplied by user
1479 item->u2.statements == a pval list of statements in the if ()
1480 item->u3.else_statements == a pval list of statements in the else
1482 fall thru to PV_IF */
1485 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1487 item->u2.statements == a pval list of statements in the if ()
1488 item->u3.else_statements == a pval list of statements in the else
1490 fall thru to PV_IF*/
1492 /* fields: item->u1.str == the if conditional, as supplied by user
1494 item->u2.statements == a pval list of statements in the if ()
1495 item->u3.else_statements == a pval list of statements in the else
1498 find_pval_gotos(item->u2.statements,lev+1);
1500 if (item->u3.else_statements) {
1501 find_pval_gotos(item->u3.else_statements,lev+1);
1506 /* fields: item->u1.str == the switch expression
1508 item->u2.statements == a pval list of statements in the switch,
1509 (will be case statements, most likely!)
1511 find_pval_gotos(item->u3.else_statements,lev+1);
1515 /* fields: item->u1.str == the extension name, label, whatever it's called
1517 item->u2.statements == a pval list of statements in the extension
1518 item->u3.hints == a char * hint argument
1519 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1522 find_pval_gotos(item->u2.statements,lev+1);
1530 static void find_pval_gotos(pval *item,int lev)
1534 for (i=item; i; i=i->next) {
1536 find_pval_goto_item(i, lev);
1542 /* general purpose label finder */
1543 static struct pval *match_pval_item(pval *item)
1547 switch ( item->type ) {
1549 /* fields: item->u1.str == name of macro
1550 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1551 item->u2.arglist->u1.str == argument
1552 item->u2.arglist->next == next arg
1554 item->u3.macro_statements == pval list of statements in macro body.
1556 /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1557 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1559 /* printf("MACRO: match context is: %s\n", match_context); */
1561 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 */ {
1562 /* printf("Returning on matching macro %s\n", match_context); */
1567 if (!return_on_context_match) {
1568 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1569 if ((x=match_pval(item->u3.macro_statements))) {
1570 /* printf("Responded with pval match %x\n", x); */
1575 /* printf("Skipping context/macro %s\n", item->u1.str); */
1581 /* fields: item->u1.str == name of context
1582 item->u2.statements == pval list of statements in context body
1583 item->u3.abstract == int 1 if an abstract keyword were present
1585 /* printf(" matching in CONTEXT\n"); */
1586 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1587 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1588 /* printf("Returning on matching context %s\n", match_context); */
1589 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1593 if (!return_on_context_match ) {
1594 /* printf("Descending into matching context %s\n", match_context); */
1595 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1596 /* printf("CONTEXT: Responded with pval match %x\n", x); */
1601 /* printf("Skipping context/macro %s\n", item->u1.str); */
1606 /* fields: item->u1.str == value of case
1607 item->u2.statements == pval list of statements under the case
1609 /* printf(" matching in CASE\n"); */
1610 if ((x=match_pval(item->u2.statements))) {
1611 /* printf("CASE: Responded with pval match %x\n", x); */
1617 /* fields: item->u1.str == value of case
1618 item->u2.statements == pval list of statements under the case
1620 /* printf(" matching in PATTERN\n"); */
1621 if ((x=match_pval(item->u2.statements))) {
1622 /* printf("PATTERN: Responded with pval match %x\n", x); */
1629 item->u2.statements == pval list of statements under the case
1631 /* printf(" matching in DEFAULT\n"); */
1632 if ((x=match_pval(item->u2.statements))) {
1633 /* printf("DEFAULT: Responded with pval match %x\n", x); */
1639 /* fields: item->u1.str == name of extension to catch
1640 item->u2.statements == pval list of statements in context body
1642 /* printf(" matching in CATCH\n"); */
1643 if ((x=match_pval(item->u2.statements))) {
1644 /* printf("CATCH: Responded with pval match %x\n", x); */
1649 case PV_STATEMENTBLOCK:
1650 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1652 /* printf(" matching in STATEMENTBLOCK\n"); */
1653 if ((x=match_pval(item->u1.list))) {
1654 /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1660 /* fields: item->u1.str == label name
1662 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1663 item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1666 if (!strcmp(match_label, item->u1.str)) {
1668 last_matched_label = item;
1672 if (!strcmp(match_label, item->u1.str)) {
1673 /* printf("LABEL: Responded with pval match %x\n", x); */
1680 /* fields: item->u1.for_init == a string containing the initalizer
1681 item->u2.for_test == a string containing the loop test
1682 item->u3.for_inc == a string containing the loop increment
1684 item->u4.for_statements == a pval list of statements in the for ()
1686 /* printf(" matching in FOR\n"); */
1687 if ((x=match_pval(item->u4.for_statements))) {
1688 /* printf("FOR: Responded with pval match %x\n", x);*/
1694 /* fields: item->u1.str == the while conditional, as supplied by user
1696 item->u2.statements == a pval list of statements in the while ()
1698 /* printf(" matching in WHILE\n"); */
1699 if ((x=match_pval(item->u2.statements))) {
1700 /* printf("WHILE: Responded with pval match %x\n", x); */
1706 /* fields: item->u1.str == the random number expression, as supplied by user
1708 item->u2.statements == a pval list of statements in the if ()
1709 item->u3.else_statements == a pval list of statements in the else
1711 fall thru to PV_IF */
1714 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1716 item->u2.statements == a pval list of statements in the if ()
1717 item->u3.else_statements == a pval list of statements in the else
1719 fall thru to PV_IF*/
1721 /* fields: item->u1.str == the if conditional, 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
1727 /* printf(" matching in IF/IFTIME/RANDOM\n"); */
1728 if ((x=match_pval(item->u2.statements))) {
1731 if (item->u3.else_statements) {
1732 if ((x=match_pval(item->u3.else_statements))) {
1733 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1740 /* fields: item->u1.str == the switch expression
1742 item->u2.statements == a pval list of statements in the switch,
1743 (will be case statements, most likely!)
1745 /* printf(" matching in SWITCH\n"); */
1746 if ((x=match_pval(item->u2.statements))) {
1747 /* printf("SWITCH: Responded with pval match %x\n", x); */
1753 /* fields: item->u1.str == the extension name, label, whatever it's called
1755 item->u2.statements == a pval list of statements in the extension
1756 item->u3.hints == a char * hint argument
1757 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1759 /* printf(" matching in EXTENSION\n"); */
1760 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1761 /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1762 if (strcmp(match_label,"1") == 0) {
1763 if (item->u2.statements) {
1764 struct pval *p5 = item->u2.statements;
1765 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1776 if ((x=match_pval(item->u2.statements))) {
1777 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1781 /* printf("Skipping exten %s\n", item->u1.str); */
1785 /* printf(" matching in default = %d\n", item->type); */
1791 struct pval *match_pval(pval *item)
1795 for (i=item; i; i=i->next) {
1797 /* printf(" -- match pval: item %d\n", i->type); */
1799 if ((x = match_pval_item(i))) {
1800 /* printf("match_pval: returning x=%x\n", (int)x); */
1801 return x; /* cut the search short */
1808 int count_labels_in_current_context(char *label)
1812 return_on_context_match = 0;
1813 match_pval(current_context->u2.statements);
1819 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1821 /* printf(" --- Got args %s, %s\n", exten, label); */
1826 return_on_context_match = 0;
1827 match_context = "*";
1829 match_label = label;
1831 ret = match_pval(curr_cont);
1835 /* the target of the goto could be in an included context!! Fancy that!! */
1836 /* look for includes in the current context */
1837 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1838 if (p3->type == PV_INCLUDES) {
1840 for (p4=p3->u1.list; p4; p4=p4->next) {
1841 /* for each context pointed to, find it, then find a context/label that matches the
1843 char *incl_context = p4->u1.str;
1844 /* find a matching context name */
1845 struct pval *that_context = find_context(incl_context);
1848 x3 = find_first_label_in_current_context(label, that_context);
1859 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1861 /* printf(" --- Got args %s, %s\n", exten, label); */
1866 return_on_context_match = 0;
1867 match_context = "*";
1868 match_exten = exten;
1869 match_label = label;
1870 ret = match_pval(curr_cont->u2.statements);
1874 /* the target of the goto could be in an included context!! Fancy that!! */
1875 /* look for includes in the current context */
1876 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1877 if (p3->type == PV_INCLUDES) {
1879 for (p4=p3->u1.list; p4; p4=p4->next) {
1880 /* for each context pointed to, find it, then find a context/label that matches the
1882 char *incl_context = p4->u1.str;
1883 /* find a matching context name */
1884 struct pval *that_context = find_context(incl_context);
1887 x3 = find_label_in_current_context(exten, label, that_context);
1898 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1900 /* printf(" --- Got args %s\n", label); */
1902 return_on_context_match = 0;
1903 match_context = "*";
1905 match_label = label;
1906 return match_pval(curr_ext);
1909 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1911 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1913 return_on_context_match = 0;
1915 match_context = context;
1916 match_exten = exten;
1917 match_label = label;
1919 return match_pval(current_db);
1923 struct pval *find_macro(char *name)
1925 return_on_context_match = 1;
1927 match_context = name;
1928 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1930 return match_pval(current_db);
1933 struct pval *find_context(char *name)
1935 return_on_context_match = 1;
1937 match_context = name;
1938 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1940 return match_pval(current_db);
1943 int is_float(char *arg )
1946 for (s=arg; *s; s++) {
1947 if (*s != '.' && (*s < '0' || *s > '9'))
1952 int is_int(char *arg )
1955 for (s=arg; *s; s++) {
1956 if (*s < '0' || *s > '9')
1961 int is_empty(char *arg)
1968 if (*arg != ' ' && *arg != '\t')
1976 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1978 struct argchoice *ac;
1981 switch (should->dtype) {
1982 case ARGD_OPTIONSET:
1983 if ( strstr(is->u1.str,"${") )
1984 return 0; /* no checking anything if there's a var reference in there! */
1986 opcop = ast_strdupa(is->u1.str);
1988 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
1991 while (*p && *p != ')' )
1997 for (ac=app->opts; ac; ac=ac->next) {
1998 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2001 for (ac=app->opts; ac; ac=ac->next) {
2002 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2003 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2005 if (p && *p == 'j') {
2006 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
2007 is->filename, is->startline, is->endline, app->name);
2013 if (ac->name[1] == '(') {
2014 if (*(p+1) != '(') {
2015 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",
2016 is->filename, is->startline, is->endline, ac->name[0], app->name);
2023 for (q=opcop; *q; q++) {
2024 if ( *q != '+' && *q != '(' && *q != ')') {
2025 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",
2026 is->filename, is->startline, is->endline, *q, app->name);
2038 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2040 struct argchoice *ac;
2043 switch (should->dtype) {
2045 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2047 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2052 if (is_int(is->u1.str))
2059 if (is_float(is->u1.str))
2066 if( !is->u1.str || strlen(is->u1.str) == 0 )
2067 return 1; /* a null arg in the call will match an enum, I guess! */
2068 for (ac=should->choices; ac; ac=ac->next) {
2069 if (strcmp(ac->name,is->u1.str) == 0)
2075 case ARGD_OPTIONSET:
2076 opcop = ast_strdupa(is->u1.str);
2078 for (ac=app->opts; ac; ac=ac->next) {
2079 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2082 for (ac=app->opts; ac; ac=ac->next) {
2083 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2084 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2088 if (ac->name[1] == '(') {
2089 if (*(p+1) == '(') {
2091 while (*q && *q != ')') {
2103 return 1; /* matches anything */
2106 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2110 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2113 struct argdesc *ad = app->args;
2117 for (pa = arglist; pa; pa=pa->next) {
2119 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2120 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2124 /* find the first entry in the ad list that will match */
2126 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2129 z= option_matches( ad, pa, app);
2134 if (ad->type == ARGD_REQUIRED) {
2135 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2136 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2140 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2141 option_matches_j( ad, pa, app);
2147 /* any app nodes left, that are not optional? */
2148 for ( ; ad; ad=ad->next) {
2149 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2152 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2153 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2164 void check_switch_expr(pval *item, struct argapp *apps)
2167 /* get and clean the variable name */
2169 struct argapp *a,*a2;
2170 struct appsetvar *v,*v2;
2171 struct argchoice *c;
2175 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2178 buff1 = ast_strdupa(p);
2180 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2181 buff1[strlen(buff1)-1] = 0;
2182 /* buff1 now contains the variable name */
2184 for (a=apps; a; a=a->next) {
2185 for (v=a->setvars;v;v=v->next) {
2186 if (strcmp(v->name,buff1) == 0) {
2194 /* we have a match, to a variable that has a set of determined values */
2199 /* first of all, does this switch have a default case ? */
2200 for (t=item->u2.statements; t; t=t->next) {
2201 if (t->type == PV_DEFAULT) {
2205 if (t->type == PV_PATTERN) {
2209 if (def || pat) /* nothing to check. All cases accounted for! */
2211 for (c=v->vals; c; c=c->next) {
2213 for (t=item->u2.statements; t; t=t->next) {
2214 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2215 if (!strcmp(t->u1.str,c->name)) {
2222 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2223 item->filename, item->startline, item->endline, item->u1.str, c->name);
2227 /* next, is there an app call in the current exten, that would set this var? */
2229 t = current_extension->u2.statements;
2230 if ( t && t->type == PV_STATEMENTBLOCK )
2231 t = t->u1.statements;
2232 for (; t && t != item; t=t->next) {
2233 if (t->type == PV_APPLICATION_CALL) {
2234 /* find the application that matches the u1.str */
2235 for (a2=apps; a2; a2=a2->next) {
2236 if (strcasecmp(a2->name, t->u1.str)==0) {
2237 for (v2=a2->setvars; v2; v2=v2->next) {
2238 if (strcmp(v2->name, buff1) == 0) {
2239 /* found an app that sets the var */
2253 /* see if it sets the var */
2255 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",
2256 item->filename, item->startline, item->endline, item->u1.str);
2264 /* first of all, does this switch have a default case ? */
2265 for (t=item->u2.statements; t; t=t->next) {
2266 if (t->type == PV_DEFAULT) {
2272 if (def) /* nothing to check. All cases accounted for! */
2274 /* if no default, warn and insert a default case at the end */
2275 p2 = tl->next = calloc(1, sizeof(struct pval));
2277 p2->type = PV_DEFAULT;
2278 p2->startline = tl->startline;
2279 p2->endline = tl->endline;
2280 p2->startcol = tl->startcol;
2281 p2->endcol = tl->endcol;
2282 p2->filename = strdup(tl->filename);
2283 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2284 p2->filename, p2->startline, p2->endline);
2290 static void check_context_names(void)
2293 for (i=current_db; i; i=i->next) {
2294 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2295 for (j=i->next; j; j=j->next) {
2296 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2297 if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
2299 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",
2300 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
2309 static void check_abstract_reference(pval *abstract_context)
2312 /* find some context includes that reference this context */
2315 /* otherwise, print out a warning */
2316 for (i=current_db; i; i=i->next) {
2317 if (i->type == PV_CONTEXT) {
2318 for (j=i->u2. statements; j; j=j->next) {
2319 if ( j->type == PV_INCLUDES ) {
2321 for (p4=j->u1.list; p4; p4=p4->next) {
2322 /* for each context pointed to, find it, then find a context/label that matches the
2324 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2325 return; /* found a match! */
2331 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",
2332 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2337 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2341 struct argapp *app, *found;
2343 struct pval *macro_def;
2344 struct pval *app_def;
2349 switch (item->type) {
2351 /* fields: item->u1.str == string associated with this (word).
2352 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2356 /* fields: item->u1.str == name of macro
2357 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2358 item->u2.arglist->u1.str == argument
2359 item->u2.arglist->next == next arg
2361 item->u3.macro_statements == pval list of statements in macro body.
2363 in_abstract_context = 0;
2364 current_context = item;
2365 current_extension = 0;
2367 check_macro_returns(item);
2369 for (lp=item->u2.arglist; lp; lp=lp->next) {
2372 check_pval(item->u3.macro_statements, apps,in_globals);
2376 /* fields: item->u1.str == name of context
2377 item->u2.statements == pval list of statements in context body
2378 item->u3.abstract == int 1 if an abstract keyword were present
2380 current_context = item;
2381 current_extension = 0;
2382 if ( item->u3.abstract ) {
2383 in_abstract_context = 1;
2384 check_abstract_reference(item);
2386 in_abstract_context = 0;
2387 check_pval(item->u2.statements, apps,in_globals);
2391 /* fields: item->u1.str == name of macro to call
2392 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2393 item->u2.arglist->u1.str == argument
2394 item->u2.arglist->next == next arg
2397 /* if this is a standalone, we will need to make sure the
2398 localized load of extensions.conf is done */
2399 if (!extensions_dot_conf_loaded) {
2400 localized_pbx_load_module();
2401 extensions_dot_conf_loaded++;
2404 macro_def = find_macro(item->u1.str);
2407 struct pbx_find_info pfiq = {.stacklen = 0 };
2408 struct pbx_find_info pfiq2 = {.stacklen = 0 };
2410 /* look for the macro in the extensions.conf world */
2411 pbx_find_extension(NULL, NULL, &pfiq, item->u1.str, "s", 1, NULL, NULL, E_MATCH);
2413 if (pfiq.status != STATUS_SUCCESS) {
2415 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2417 /* look for the macro in the extensions.conf world */
2418 pbx_find_extension(NULL, NULL, &pfiq2, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2420 if (pfiq2.status == STATUS_SUCCESS) {
2421 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",
2422 item->filename, item->startline, item->endline, item->u1.str, item->u1.str);
2425 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (Not even in the extensions.conf stuff!)\n",
2426 item->filename, item->startline, item->endline, item->u1.str);
2431 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s cannot be found in the AEL code!\n",
2432 item->filename, item->startline, item->endline, item->u1.str);
2436 #ifdef THIS_IS_1DOT4
2438 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2440 /* look for the macro in the extensions.conf world */
2441 pbx_find_extension(NULL, NULL, &pfiq, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2443 if (pfiq.status != STATUS_SUCCESS) {
2444 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",
2445 item->filename, item->startline, item->endline, item->u1.str);
2451 } else if (macro_def->type != PV_MACRO) {
2452 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2453 item->filename, item->startline, item->endline, item->u1.str);
2456 /* macro_def is a MACRO, so do the args match in number? */
2460 for (lp=item->u2.arglist; lp; lp=lp->next) {
2463 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2466 if (hereargs != thereargs ) {
2467 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",
2468 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2474 case PV_APPLICATION_CALL:
2475 /* fields: item->u1.str == name of application to call
2476 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2477 item->u2.arglist->u1.str == argument
2478 item->u2.arglist->next == next arg
2480 /* Need to check to see if the application is available! */
2481 app_def = find_context(item->u1.str);
2482 if (app_def && app_def->type == PV_MACRO) {
2483 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2484 item->filename, item->startline, item->endline, item->u1.str);
2487 if (strcasecmp(item->u1.str,"GotoIf") == 0
2488 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2489 || strcasecmp(item->u1.str,"while") == 0
2490 || strcasecmp(item->u1.str,"endwhile") == 0
2491 || strcasecmp(item->u1.str,"random") == 0
2492 || strcasecmp(item->u1.str,"gosub") == 0
2493 || strcasecmp(item->u1.str,"return") == 0
2494 || strcasecmp(item->u1.str,"gosubif") == 0
2495 || strcasecmp(item->u1.str,"continuewhile") == 0
2496 || strcasecmp(item->u1.str,"endwhile") == 0
2497 || strcasecmp(item->u1.str,"execif") == 0
2498 || strcasecmp(item->u1.str,"execiftime") == 0
2499 || strcasecmp(item->u1.str,"exitwhile") == 0
2500 || strcasecmp(item->u1.str,"goto") == 0
2501 || strcasecmp(item->u1.str,"macro") == 0
2502 || strcasecmp(item->u1.str,"macroexclusive") == 0
2503 || strcasecmp(item->u1.str,"macroif") == 0
2504 || strcasecmp(item->u1.str,"stackpop") == 0
2505 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2506 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",
2507 item->filename, item->startline, item->endline, item->u1.str);
2510 if (strcasecmp(item->u1.str,"macroexit") == 0) {
2511 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
2512 item->filename, item->startline, item->endline);
2513 item->type = PV_RETURN;
2520 for (app=apps; app; app=app->next) {
2521 if (strcasecmp(app->name, item->u1.str) == 0) {
2527 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2528 item->filename, item->startline, item->endline, item->u1.str);
2531 check_app_args(item, item->u2.arglist, app);
2536 /* fields: item->u1.str == value of case
2537 item->u2.statements == pval list of statements under the case
2539 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2540 /* find the last statement */
2541 check_pval(item->u2.statements, apps,in_globals);
2545 /* fields: item->u1.str == value of case
2546 item->u2.statements == pval list of statements under the case
2548 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2549 /* find the last statement */
2551 check_pval(item->u2.statements, apps,in_globals);
2556 item->u2.statements == pval list of statements under the case
2559 check_pval(item->u2.statements, apps,in_globals);
2563 /* fields: item->u1.str == name of extension to catch
2564 item->u2.statements == pval list of statements in context body
2566 check_pval(item->u2.statements, apps,in_globals);
2570 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2572 check_pval(item->u1.list, apps,in_globals);
2576 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2578 check_pval(item->u1.list, apps,in_globals);
2582 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2584 check_pval(item->u1.list, apps,in_globals);
2585 check_includes(item);
2586 for (lp=item->u1.list; lp; lp=lp->next){
2587 char *incl_context = lp->u1.str;
2588 struct pval *that_context = find_context(incl_context);
2590 if ( lp->u2.arglist ) {
2591 check_timerange(lp->u2.arglist);
2592 check_dow(lp->u2.arglist->next);
2593 check_day(lp->u2.arglist->next->next);
2594 check_month(lp->u2.arglist->next->next->next);
2598 find_pval_gotos(that_context->u2.statements,0);
2604 case PV_STATEMENTBLOCK:
2605 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2607 check_pval(item->u1.list, apps,in_globals);
2611 /* fields: item->u1.str == variable name
2612 item->u2.val == variable value to assign
2614 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2615 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2616 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);
2617 ast_expr_register_extra_error_info(errmsg);
2618 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2619 ast_expr_clear_extra_error_info();
2620 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2621 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2622 item->filename, item->startline, item->endline, item->u2.val);
2625 check_expr2_input(item,item->u2.val);
2629 case PV_LOCALVARDEC:
2630 /* fields: item->u1.str == variable name
2631 item->u2.val == variable value to assign
2633 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2634 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);
2635 ast_expr_register_extra_error_info(errmsg);
2636 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2637 ast_expr_clear_extra_error_info();
2638 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2639 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2640 item->filename, item->startline, item->endline, item->u2.val);
2643 check_expr2_input(item,item->u2.val);
2647 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2648 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2650 /* don't check goto's in abstract contexts */
2651 if ( in_abstract_context )
2658 /* fields: item->u1.str == label name
2660 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2661 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2662 item->filename, item->startline, item->endline, item->u1.str);
2670 /* fields: item->u1.for_init == a string containing the initalizer
2671 item->u2.for_test == a string containing the loop test
2672 item->u3.for_inc == a string containing the loop increment
2674 item->u4.for_statements == a pval list of statements in the for ()
2676 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);
2677 ast_expr_register_extra_error_info(errmsg);
2679 strp = strchr(item->u1.for_init, '=');
2681 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2683 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
2684 strp = strchr(item->u3.for_inc, '=');
2686 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2688 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2689 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2690 item->filename, item->startline, item->endline, item->u2.for_test);
2693 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2694 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2695 item->filename, item->startline, item->endline, item->u3.for_inc);
2698 check_expr2_input(item,item->u2.for_test);
2699 check_expr2_input(item,item->u3.for_inc);
2701 ast_expr_clear_extra_error_info();
2702 check_pval(item->u4.for_statements, apps,in_globals);
2706 /* fields: item->u1.str == the while conditional, as supplied by user
2708 item->u2.statements == a pval list of statements in the while ()
2710 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);
2711 ast_expr_register_extra_error_info(errmsg);
2712 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2713 ast_expr_clear_extra_error_info();
2714 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2715 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2716 item->filename, item->startline, item->endline, item->u1.str);
2719 check_expr2_input(item,item->u1.str);
2720 check_pval(item->u2.statements, apps,in_globals);
2737 check_continue(item);
2741 /* fields: item->u1.str == the random number expression, as supplied by user
2743 item->u2.statements == a pval list of statements in the if ()
2744 item->u3.else_statements == a pval list of statements in the else
2747 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);
2748 ast_expr_register_extra_error_info(errmsg);
2749 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2750 ast_expr_clear_extra_error_info();
2751 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2752 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2753 item->filename, item->startline, item->endline, item->u1.str);
2756 check_expr2_input(item,item->u1.str);
2757 check_pval(item->u2.statements, apps,in_globals);
2758 if (item->u3.else_statements) {
2759 check_pval(item->u3.else_statements, apps,in_globals);
2764 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2766 item->u2.statements == a pval list of statements in the if ()
2767 item->u3.else_statements == a pval list of statements in the else
2770 if ( item->u2.arglist ) {
2771 check_timerange(item->u1.list);
2772 check_dow(item->u1.list->next);
2773 check_day(item->u1.list->next->next);
2774 check_month(item->u1.list->next->next->next);
2777 check_pval(item->u2.statements, apps,in_globals);
2778 if (item->u3.else_statements) {
2779 check_pval(item->u3.else_statements, apps,in_globals);
2784 /* fields: item->u1.str == the if conditional, as supplied by user
2786 item->u2.statements == a pval list of statements in the if ()
2787 item->u3.else_statements == a pval list of statements in the else
2790 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);
2791 ast_expr_register_extra_error_info(errmsg);
2792 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2793 ast_expr_clear_extra_error_info();
2794 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2795 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2796 item->filename, item->startline, item->endline, item->u1.str);
2799 check_expr2_input(item,item->u1.str);
2800 check_pval(item->u2.statements, apps,in_globals);
2801 if (item->u3.else_statements) {
2802 check_pval(item->u3.else_statements, apps,in_globals);
2807 /* fields: item->u1.str == the switch expression
2809 item->u2.statements == a pval list of statements in the switch,
2810 (will be case statements, most likely!)
2812 /* we can check the switch expression, see if it matches any of the app variables...
2813 if it does, then, are all the possible cases accounted for? */
2814 check_switch_expr(item, apps);
2815 check_pval(item->u2.statements, apps,in_globals);
2819 /* fields: item->u1.str == the extension name, label, whatever it's called
2821 item->u2.statements == a pval list of statements in the extension
2822 item->u3.hints == a char * hint argument
2823 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2825 current_extension = item ;
2827 check_pval(item->u2.statements, apps,in_globals);
2831 /* fields: item->u1.str == the ignorepat data
2836 /* fields: item->u1.statements == pval list of statements, usually vardecs
2838 in_abstract_context = 0;
2839 check_pval(item->u1.statements, apps, 1);
2846 void check_pval(pval *item, struct argapp *apps, int in_globals)
2851 1. Do goto's point to actual labels?
2852 2. Do macro calls reference a macro?
2853 3. Does the number of macro args match the definition?
2854 4. Is a macro call missing its & at the front?
2855 5. Application calls-- we could check syntax for existing applications,
2856 but I need some some sort of universal description bnf for a general
2857 sort of method for checking arguments, in number, maybe even type, at least.
2858 Don't want to hand code checks for hundreds of applications.
2861 for (i=item; i; i=i->next) {
2862 check_pval_item(i,apps,in_globals);
2866 void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2873 struct argapp *apps=0;
2876 return; /* don't check an empty tree */
2878 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2879 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2881 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2884 errs = warns = notes = 0;
2886 check_context_names();
2887 check_pval(item, apps, 0);
2890 argdesc_destroy(apps); /* taketh away */
2899 /* =============================================================================================== */
2900 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2901 /* =============================================================================================== */
2903 static int control_statement_count = 0;
2905 struct ael_priority *new_prio(void)
2907 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2911 struct ael_extension *new_exten(void)
2913 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2917 void linkprio(struct ael_extension *exten, struct ael_priority *prio)
2919 if (!exten->plist) {
2920 exten->plist = prio;
2921 exten->plist_last = prio;
2923 exten->plist_last->next = prio;
2924 exten->plist_last = prio;
2927 prio->exten = exten; /* don't override the switch value */
2930 void destroy_extensions(struct ael_extension *exten)
2932 struct ael_extension *ne, *nen;
2933 for (ne=exten; ne; ne=nen) {
2934 struct ael_priority *pe, *pen;
2939 /* cidmatch fields are allocated with name, and freed when
2940 the name field is freed. Don't do a free for this field,
2941 unless you LIKE to see a crash! */
2946 for (pe=ne->plist; pe; pe=pen) {
2959 nen = ne->next_exten;
2965 ne->loop_continue = 0;
2970 static int label_inside_case(pval *label)
2974 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
2975 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
2984 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
2986 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2987 exten->next_exten = add;
2990 static void remove_spaces_before_equals(char *str)
2993 while( str && *str && *str != '=' )
2995 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
3009 /* =============================================================================================== */
3010 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
3011 /* =============================================================================================== */
3013 static void gen_match_to_pattern(char *pattern, char *result)
3015 /* the result will be a string that will be matched by pattern */
3016 char *p=pattern, *t=result;
3018 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
3020 else if (*p == '[') {
3026 *t++=*(p+1); /* use the first char in the set */
3033 *t++ = 0; /* cap it off */
3036 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
3039 struct ael_priority *pr;
3040 struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
3041 struct ael_priority *while_test, *while_loop, *while_end;
3042 struct ael_priority *switch_test, *switch_end, *fall_thru, *switch_empty;
3043 struct ael_priority *if_test, *if_end, *if_skip, *if_false;
3044 #ifdef OLD_RAND_ACTION
3045 struct ael_priority *rand_test, *rand_end, *rand_skip;
3050 char new_label[2000];
3052 int local_control_statement_count;
3054 struct ael_priority *loop_break_save;
3055 struct ael_priority *loop_continue_save;
3056 struct ael_extension *switch_case,*switch_null;
3058 for (p=statement; p; p=p->next) {
3062 pr->type = AEL_APPCALL;
3063 snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
3064 pr->app = strdup("Set");
3065 remove_spaces_before_equals(buf1);
3066 pr->appargs = strdup(buf1);
3068 linkprio(exten, pr);
3071 case PV_LOCALVARDEC:
3073 pr->type = AEL_APPCALL;
3074 snprintf(buf1,sizeof(buf1),"LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
3075 pr->app = strdup("Set");
3076 remove_spaces_before_equals(buf1);
3077 pr->appargs = strdup(buf1);
3079 linkprio(exten, pr);
3084 pr->type = AEL_APPCALL;
3085 p->u2.goto_target = get_goto_target(p);
3086 if( p->u2.goto_target ) {
3087 p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
3090 if (!p->u1.list->next) /* just one */ {
3091 pr->app = strdup("Goto");
3093 pr->appargs = strdup(p->u1.list->u1.str);
3094 else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
3095 snprintf(buf1,sizeof(buf1),"%s,%s", mother_exten->name, p->u1.list->u1.str);
3096 pr->appargs = strdup(buf1);
3099 } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
3100 snprintf(buf1,sizeof(buf1),"%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
3101 pr->app = strdup("Goto");
3102 pr->appargs = strdup(buf1);
3103 } else if (p->u1.list->next && p->u1.list->next->next) {
3104 snprintf(buf1,sizeof(buf1),"%s,%s,%s", p->u1.list->u1.str,
3105 p->u1.list->next->u1.str,
3106 p->u1.list->next->next->u1.str);
3107 pr->app = strdup("Goto");
3108 pr->appargs = strdup(buf1);
3111 linkprio(exten, pr);
3116 pr->type = AEL_LABEL;
3118 p->u3.compiled_label = exten;
3119 linkprio(exten, pr);
3123 control_statement_count++;
3124 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3125 loop_continue_save = exten->loop_continue;
3126 snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
3127 for_init = new_prio();
3128 for_inc = new_prio();
3129 for_test = new_prio();
3130 for_loop = new_prio();
3131 for_end = new_prio();
3132 for_init->type = AEL_APPCALL;
3133 for_inc->type = AEL_APPCALL;
3134 for_test->type = AEL_FOR_CONTROL;
3135 for_test->goto_false = for_end;
3136 for_loop->type = AEL_CONTROL1; /* simple goto */
3137 for_end->type = AEL_APPCALL;
3138 for_init->app = strdup("Set");
3140 strcpy(buf2,p->u1.for_init);
3141 remove_spaces_before_equals(buf2);
3142 strp = strchr(buf2, '=');
3144 strp2 = strchr(p->u1.for_init, '=');
3147 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3149 for_init->appargs = strdup(buf2);
3151 strp2 = p->u1.for_init;
3152 while (*strp2 && isspace(*strp2))
3154 if (*strp2 == '&') { /* itsa macro call */
3155 char *strp3 = strp2+1;
3156 while (*strp3 && isspace(*strp3))
3158 strcpy(buf2, strp3);
3159 strp3 = strchr(buf2,'(');
3163 while ((strp3=strchr(buf2,','))) {
3166 strp3 = strrchr(buf2, ')');
3168 *strp3 = 0; /* remove the closing paren */
3170 for_init->appargs = strdup(buf2);
3171 free(for_init->app);
3172 for_init->app = strdup("Macro");
3173 } else { /* must be a regular app call */
3175 strcpy(buf2, strp2);
3176 strp3 = strchr(buf2,'(');
3179 free(for_init->app);
3180 for_init->app = strdup(buf2);
3181 for_init->appargs = strdup(strp3+1);
3182 strp3 = strrchr(for_init->appargs, ')');
3184 *strp3 = 0; /* remove the closing paren */