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 includes->filename, includes->startline, includes->endline, incl_context);
829 static void check_timerange(pval *p)
836 times = ast_strdupa(p->u1.str);
838 /* Star is all times */
839 if (ast_strlen_zero(times) || !strcmp(times, "*")) {
842 /* Otherwise expect a range */
843 e = strchr(times, '-');
845 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",
846 p->filename, p->startline, p->endline, times);
852 while (*e && !isdigit(*e))
855 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
856 p->filename, p->startline, p->endline, p->u1.str);
859 if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
860 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
861 p->filename, p->startline, p->endline, times);
864 if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
865 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
866 p->filename, p->startline, p->endline, times);
871 if ((s1 < 0) || (s1 >= 24*30)) {
872 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
873 p->filename, p->startline, p->endline, times);
877 if ((e1 < 0) || (e1 >= 24*30)) {
878 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
879 p->filename, p->startline, p->endline, e);
885 static char *days[] =
896 /*! \brief get_dow: Get day of week */
897 static void check_dow(pval *DOW)
901 /* The following line is coincidence, really! */
904 dow = ast_strdupa(DOW->u1.str);
906 /* Check for all days */
907 if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
909 /* Get start and ending days */
910 c = strchr(dow, '-');
918 while ((s < 7) && strcasecmp(dow, days[s])) s++;
920 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",
921 DOW->filename, DOW->startline, DOW->endline, dow);
926 while ((e < 7) && strcasecmp(c, days[e])) e++;
928 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",
929 DOW->filename, DOW->startline, DOW->endline, c);
936 static void check_day(pval *DAY)
940 /* The following line is coincidence, really! */
943 day = ast_strdupa(DAY->u1.str);
945 /* Check for all days */
946 if (ast_strlen_zero(day) || !strcmp(day, "*")) {
949 /* Get start and ending days */
950 c = strchr(day, '-');
956 if (sscanf(day, "%d", &s) != 1) {
957 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
958 DAY->filename, DAY->startline, DAY->endline, day);
961 else if ((s < 1) || (s > 31)) {
962 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",
963 DAY->filename, DAY->startline, DAY->endline, day);
968 if (sscanf(c, "%d", &e) != 1) {
969 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
970 DAY->filename, DAY->startline, DAY->endline, c);
973 else if ((e < 1) || (e > 31)) {
974 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",
975 DAY->filename, DAY->startline, DAY->endline, day);
983 static char *months[] =
999 static void check_month(pval *MON)
1003 /* The following line is coincidence, really! */
1006 mon = ast_strdupa(MON->u1.str);
1008 /* Check for all days */
1009 if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
1011 /* Get start and ending days */
1012 c = strchr(mon, '-');
1017 /* Find the start */
1019 while ((s < 12) && strcasecmp(mon, months[s])) s++;
1021 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1022 MON->filename, MON->startline, MON->endline, mon);
1027 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1029 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1030 MON->filename, MON->startline, MON->endline, c);
1037 static int check_break(pval *item)
1041 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1042 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1044 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
1045 || p->type == PV_WHILE || p->type == PV_FOR ) {
1050 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
1051 item->filename, item->startline, item->endline);
1057 static int check_continue(pval *item)
1061 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
1062 /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
1064 if( p->type == PV_WHILE || p->type == PV_FOR ) {
1069 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
1070 item->filename, item->startline, item->endline);
1076 static struct pval *in_macro(pval *item)
1081 if( curr->type == PV_MACRO ) {
1089 static struct pval *in_context(pval *item)
1094 if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
1103 /* general purpose goto finder */
1105 static void check_label(pval *item)
1111 /* A label outside an extension just plain does not make sense! */
1116 if( curr->type == PV_MACRO || curr->type == PV_EXTENSION ) {
1124 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
1125 item->filename, item->startline, item->endline, item->u1.str);
1130 /* basically, ensure that a label is not repeated in a context. Period.
1131 The method: well, for each label, find the first label in the context
1132 with the same name. If it's not the current label, then throw an error. */
1135 /* printf("==== check_label: ====\n"); */
1136 if( !current_extension )
1137 curr = current_context;
1139 curr = current_extension;
1141 x = find_first_label_in_current_context((char *)item->u1.str, curr);
1142 /* 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); */
1143 if( x && x != item )
1145 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
1146 item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
1149 /* printf("<<<<< check_label: ====\n"); */
1152 static pval *get_goto_target(pval *item)
1154 /* just one item-- the label should be in the current extension */
1155 pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
1158 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1159 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
1163 curr_cont = get_contxt(item);
1166 if (item->u1.list->next && !item->u1.list->next->next) {
1167 if (!strstr((item->u1.list)->u1.str,"${")
1168 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1169 struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
1175 if (item->u1.list->next && item->u1.list->next->next) {
1177 pval *first = item->u1.list;
1178 pval *second = item->u1.list->next;
1179 pval *third = item->u1.list->next->next;
1181 if (!strstr((item->u1.list)->u1.str,"${")
1182 && !strstr(item->u1.list->next->u1.str,"${")
1183 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1184 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1188 struct pval *that_context = find_context(item->u1.list->u1.str);
1190 /* the target of the goto could be in an included context!! Fancy that!! */
1191 /* look for includes in the current context */
1193 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1194 if (p3->type == PV_INCLUDES) {
1196 for (p4=p3->u1.list; p4; p4=p4->next) {
1197 /* for each context pointed to, find it, then find a context/label that matches the
1199 char *incl_context = p4->u1.str;
1200 /* find a matching context name */
1201 struct pval *that_other_context = find_context(incl_context);
1202 if (that_other_context) {
1204 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1220 static void check_goto(pval *item)
1222 /* check for the target of the goto-- does it exist? */
1223 if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1224 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
1225 item->filename, item->startline, item->endline);
1229 /* just one item-- the label should be in the current extension */
1231 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1232 struct pval *z = get_extension_or_contxt(item);
1235 x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
1236 /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
1237 (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
1239 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
1240 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1248 if (item->u1.list->next && !item->u1.list->next->next) {
1250 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1251 (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1252 if (!strstr((item->u1.list)->u1.str,"${")
1253 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1254 struct pval *z = get_contxt(item);
1258 x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
1261 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",
1262 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1271 if (item->u1.list->next && item->u1.list->next->next) {
1273 pval *first = item->u1.list;
1274 pval *second = item->u1.list->next;
1275 pval *third = item->u1.list->next->next;
1277 /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
1278 (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1279 if (!strstr((item->u1.list)->u1.str,"${")
1280 && !strstr(item->u1.list->next->u1.str,"${")
1281 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1282 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1285 struct pval *found = 0;
1286 struct pval *that_context = find_context(item->u1.list->u1.str);
1288 /* the target of the goto could be in an included context!! Fancy that!! */
1289 /* look for includes in the current context */
1291 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1292 if (p3->type == PV_INCLUDES) {
1294 for (p4=p3->u1.list; p4; p4=p4->next) {
1295 /* for each context pointed to, find it, then find a context/label that matches the
1297 char *incl_context = p4->u1.str;
1298 /* find a matching context name */
1299 struct pval *that_other_context = find_context(incl_context);
1300 if (that_other_context) {
1302 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
1312 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
1313 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1316 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1317 if( mac ) { /* yes! */
1318 struct pval *targ = in_context(found);
1321 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",
1322 item->filename, item->startline, item->endline);
1328 /* here is where code would go to check for target existence in extensions.conf files */
1330 struct pbx_find_info pfiq = {.stacklen = 0 };
1331 extern int localized_pbx_load_module(void);
1332 /* if this is a standalone, we will need to make sure the
1333 localized load of extensions.conf is done */
1334 if (!extensions_dot_conf_loaded) {
1335 localized_pbx_load_module();
1336 extensions_dot_conf_loaded++;
1339 pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
1340 atoi(third->u1.str) ? NULL : third->u1.str, NULL,
1341 atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
1343 if (pfiq.status != STATUS_SUCCESS) {
1344 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",
1345 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1349 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: Couldn't find goto target %s|%s|%s in the AEL code!\n",
1350 item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
1355 struct pval *mac = in_macro(item); /* is this goto inside a macro? */
1356 if( mac ) { /* yes! */
1357 struct pval *targ = in_context(x);
1360 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",
1361 item->filename, item->startline, item->endline);
1371 static void find_pval_goto_item(pval *item, int lev)
1375 ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
1379 switch ( item->type ) {
1381 /* fields: item->u1.str == name of macro
1382 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1383 item->u2.arglist->u1.str == argument
1384 item->u2.arglist->next == next arg
1386 item->u3.macro_statements == pval list of statements in macro body.
1389 /* printf("Descending into matching macro %s\n", match_context); */
1390 find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1395 /* fields: item->u1.str == name of context
1396 item->u2.statements == pval list of statements in context body
1397 item->u3.abstract == int 1 if an abstract keyword were present
1402 /* fields: item->u1.str == value of case
1403 item->u2.statements == pval list of statements under the case
1405 find_pval_gotos(item->u2.statements,lev+1);
1409 /* fields: item->u1.str == value of case
1410 item->u2.statements == pval list of statements under the case
1412 find_pval_gotos(item->u2.statements,lev+1);
1417 item->u2.statements == pval list of statements under the case
1419 find_pval_gotos(item->u2.statements,lev+1);
1423 /* fields: item->u1.str == name of extension to catch
1424 item->u2.statements == pval list of statements in context body
1426 find_pval_gotos(item->u2.statements,lev+1);
1429 case PV_STATEMENTBLOCK:
1430 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1432 find_pval_gotos(item->u1.list,lev+1);
1436 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1437 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1439 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1443 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1445 for (p4=item->u1.list; p4; p4=p4->next) {
1446 /* for each context pointed to, find it, then find a context/label that matches the
1448 char *incl_context = p4->u1.str;
1449 /* find a matching context name */
1450 struct pval *that_context = find_context(incl_context);
1452 find_pval_gotos(that_context,lev+1); /* keep working up the includes */
1458 /* fields: item->u1.for_init == a string containing the initalizer
1459 item->u2.for_test == a string containing the loop test
1460 item->u3.for_inc == a string containing the loop increment
1462 item->u4.for_statements == a pval list of statements in the for ()
1464 find_pval_gotos(item->u4.for_statements,lev+1);
1468 /* fields: item->u1.str == the while conditional, as supplied by user
1470 item->u2.statements == a pval list of statements in the while ()
1472 find_pval_gotos(item->u2.statements,lev+1);
1476 /* fields: item->u1.str == the random number expression, as supplied by user
1478 item->u2.statements == a pval list of statements in the if ()
1479 item->u3.else_statements == a pval list of statements in the else
1481 fall thru to PV_IF */
1484 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1486 item->u2.statements == a pval list of statements in the if ()
1487 item->u3.else_statements == a pval list of statements in the else
1489 fall thru to PV_IF*/
1491 /* fields: item->u1.str == the if conditional, as supplied by user
1493 item->u2.statements == a pval list of statements in the if ()
1494 item->u3.else_statements == a pval list of statements in the else
1497 find_pval_gotos(item->u2.statements,lev+1);
1499 if (item->u3.else_statements) {
1500 find_pval_gotos(item->u3.else_statements,lev+1);
1505 /* fields: item->u1.str == the switch expression
1507 item->u2.statements == a pval list of statements in the switch,
1508 (will be case statements, most likely!)
1510 find_pval_gotos(item->u3.else_statements,lev+1);
1514 /* fields: item->u1.str == the extension name, label, whatever it's called
1516 item->u2.statements == a pval list of statements in the extension
1517 item->u3.hints == a char * hint argument
1518 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1521 find_pval_gotos(item->u2.statements,lev+1);
1529 static void find_pval_gotos(pval *item,int lev)
1533 for (i=item; i; i=i->next) {
1535 find_pval_goto_item(i, lev);
1541 /* general purpose label finder */
1542 static struct pval *match_pval_item(pval *item)
1546 switch ( item->type ) {
1548 /* fields: item->u1.str == name of macro
1549 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1550 item->u2.arglist->u1.str == argument
1551 item->u2.arglist->next == next arg
1553 item->u3.macro_statements == pval list of statements in macro body.
1555 /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
1556 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1558 /* printf("MACRO: match context is: %s\n", match_context); */
1560 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 */ {
1561 /* printf("Returning on matching macro %s\n", match_context); */
1566 if (!return_on_context_match) {
1567 /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
1568 if ((x=match_pval(item->u3.macro_statements))) {
1569 /* printf("Responded with pval match %x\n", x); */
1574 /* printf("Skipping context/macro %s\n", item->u1.str); */
1580 /* fields: item->u1.str == name of context
1581 item->u2.statements == pval list of statements in context body
1582 item->u3.abstract == int 1 if an abstract keyword were present
1584 /* printf(" matching in CONTEXT\n"); */
1585 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1586 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1587 /* printf("Returning on matching context %s\n", match_context); */
1588 /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
1592 if (!return_on_context_match ) {
1593 /* printf("Descending into matching context %s\n", match_context); */
1594 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1595 /* printf("CONTEXT: Responded with pval match %x\n", x); */
1600 /* printf("Skipping context/macro %s\n", item->u1.str); */
1605 /* fields: item->u1.str == value of case
1606 item->u2.statements == pval list of statements under the case
1608 /* printf(" matching in CASE\n"); */
1609 if ((x=match_pval(item->u2.statements))) {
1610 /* printf("CASE: Responded with pval match %x\n", x); */
1616 /* fields: item->u1.str == value of case
1617 item->u2.statements == pval list of statements under the case
1619 /* printf(" matching in PATTERN\n"); */
1620 if ((x=match_pval(item->u2.statements))) {
1621 /* printf("PATTERN: Responded with pval match %x\n", x); */
1628 item->u2.statements == pval list of statements under the case
1630 /* printf(" matching in DEFAULT\n"); */
1631 if ((x=match_pval(item->u2.statements))) {
1632 /* printf("DEFAULT: Responded with pval match %x\n", x); */
1638 /* fields: item->u1.str == name of extension to catch
1639 item->u2.statements == pval list of statements in context body
1641 /* printf(" matching in CATCH\n"); */
1642 if ((x=match_pval(item->u2.statements))) {
1643 /* printf("CATCH: Responded with pval match %x\n", x); */
1648 case PV_STATEMENTBLOCK:
1649 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1651 /* printf(" matching in STATEMENTBLOCK\n"); */
1652 if ((x=match_pval(item->u1.list))) {
1653 /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
1659 /* fields: item->u1.str == label name
1661 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1662 item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
1665 if (!strcmp(match_label, item->u1.str)) {
1667 last_matched_label = item;
1671 if (!strcmp(match_label, item->u1.str)) {
1672 /* printf("LABEL: Responded with pval match %x\n", x); */
1679 /* fields: item->u1.for_init == a string containing the initalizer
1680 item->u2.for_test == a string containing the loop test
1681 item->u3.for_inc == a string containing the loop increment
1683 item->u4.for_statements == a pval list of statements in the for ()
1685 /* printf(" matching in FOR\n"); */
1686 if ((x=match_pval(item->u4.for_statements))) {
1687 /* printf("FOR: Responded with pval match %x\n", x);*/
1693 /* fields: item->u1.str == the while conditional, as supplied by user
1695 item->u2.statements == a pval list of statements in the while ()
1697 /* printf(" matching in WHILE\n"); */
1698 if ((x=match_pval(item->u2.statements))) {
1699 /* printf("WHILE: Responded with pval match %x\n", x); */
1705 /* fields: item->u1.str == the random number expression, as supplied by user
1707 item->u2.statements == a pval list of statements in the if ()
1708 item->u3.else_statements == a pval list of statements in the else
1710 fall thru to PV_IF */
1713 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1715 item->u2.statements == a pval list of statements in the if ()
1716 item->u3.else_statements == a pval list of statements in the else
1718 fall thru to PV_IF*/
1720 /* fields: item->u1.str == the if conditional, as supplied by user
1722 item->u2.statements == a pval list of statements in the if ()
1723 item->u3.else_statements == a pval list of statements in the else
1726 /* printf(" matching in IF/IFTIME/RANDOM\n"); */
1727 if ((x=match_pval(item->u2.statements))) {
1730 if (item->u3.else_statements) {
1731 if ((x=match_pval(item->u3.else_statements))) {
1732 /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
1739 /* fields: item->u1.str == the switch expression
1741 item->u2.statements == a pval list of statements in the switch,
1742 (will be case statements, most likely!)
1744 /* printf(" matching in SWITCH\n"); */
1745 if ((x=match_pval(item->u2.statements))) {
1746 /* printf("SWITCH: Responded with pval match %x\n", x); */
1752 /* fields: item->u1.str == the extension name, label, whatever it's called
1754 item->u2.statements == a pval list of statements in the extension
1755 item->u3.hints == a char * hint argument
1756 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1758 /* printf(" matching in EXTENSION\n"); */
1759 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1760 /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
1761 if (strcmp(match_label,"1") == 0) {
1762 if (item->u2.statements) {
1763 struct pval *p5 = item->u2.statements;
1764 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1775 if ((x=match_pval(item->u2.statements))) {
1776 /* printf("EXTENSION: Responded with pval match %x\n", x); */
1780 /* printf("Skipping exten %s\n", item->u1.str); */
1784 /* printf(" matching in default = %d\n", item->type); */
1790 struct pval *match_pval(pval *item)
1794 for (i=item; i; i=i->next) {
1796 /* printf(" -- match pval: item %d\n", i->type); */
1798 if ((x = match_pval_item(i))) {
1799 /* printf("match_pval: returning x=%x\n", (int)x); */
1800 return x; /* cut the search short */
1807 int count_labels_in_current_context(char *label)
1811 return_on_context_match = 0;
1812 match_pval(current_context->u2.statements);
1818 struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
1820 /* printf(" --- Got args %s, %s\n", exten, label); */
1825 return_on_context_match = 0;
1826 match_context = "*";
1828 match_label = label;
1830 ret = match_pval(curr_cont);
1834 /* the target of the goto could be in an included context!! Fancy that!! */
1835 /* look for includes in the current context */
1836 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1837 if (p3->type == PV_INCLUDES) {
1839 for (p4=p3->u1.list; p4; p4=p4->next) {
1840 /* for each context pointed to, find it, then find a context/label that matches the
1842 char *incl_context = p4->u1.str;
1843 /* find a matching context name */
1844 struct pval *that_context = find_context(incl_context);
1847 x3 = find_first_label_in_current_context(label, that_context);
1858 struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
1860 /* printf(" --- Got args %s, %s\n", exten, label); */
1865 return_on_context_match = 0;
1866 match_context = "*";
1867 match_exten = exten;
1868 match_label = label;
1869 ret = match_pval(curr_cont->u2.statements);
1873 /* the target of the goto could be in an included context!! Fancy that!! */
1874 /* look for includes in the current context */
1875 for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
1876 if (p3->type == PV_INCLUDES) {
1878 for (p4=p3->u1.list; p4; p4=p4->next) {
1879 /* for each context pointed to, find it, then find a context/label that matches the
1881 char *incl_context = p4->u1.str;
1882 /* find a matching context name */
1883 struct pval *that_context = find_context(incl_context);
1886 x3 = find_label_in_current_context(exten, label, that_context);
1897 static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
1899 /* printf(" --- Got args %s\n", label); */
1901 return_on_context_match = 0;
1902 match_context = "*";
1904 match_label = label;
1905 return match_pval(curr_ext);
1908 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1910 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1912 return_on_context_match = 0;
1914 match_context = context;
1915 match_exten = exten;
1916 match_label = label;
1918 return match_pval(current_db);
1922 struct pval *find_macro(char *name)
1924 return_on_context_match = 1;
1926 match_context = name;
1927 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1929 return match_pval(current_db);
1932 struct pval *find_context(char *name)
1934 return_on_context_match = 1;
1936 match_context = name;
1937 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1939 return match_pval(current_db);
1942 int is_float(char *arg )
1945 for (s=arg; *s; s++) {
1946 if (*s != '.' && (*s < '0' || *s > '9'))
1951 int is_int(char *arg )
1954 for (s=arg; *s; s++) {
1955 if (*s < '0' || *s > '9')
1960 int is_empty(char *arg)
1967 if (*arg != ' ' && *arg != '\t')
1975 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1977 struct argchoice *ac;
1980 switch (should->dtype) {
1981 case ARGD_OPTIONSET:
1982 if ( strstr(is->u1.str,"${") )
1983 return 0; /* no checking anything if there's a var reference in there! */
1985 opcop = ast_strdupa(is->u1.str);
1987 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
1990 while (*p && *p != ')' )
1996 for (ac=app->opts; ac; ac=ac->next) {
1997 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2000 for (ac=app->opts; ac; ac=ac->next) {
2001 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2002 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2004 if (p && *p == 'j') {
2005 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
2006 is->filename, is->startline, is->endline, app->name);
2012 if (ac->name[1] == '(') {
2013 if (*(p+1) != '(') {
2014 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",
2015 is->filename, is->startline, is->endline, ac->name[0], app->name);
2022 for (q=opcop; *q; q++) {
2023 if ( *q != '+' && *q != '(' && *q != ')') {
2024 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",
2025 is->filename, is->startline, is->endline, *q, app->name);
2037 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
2039 struct argchoice *ac;
2042 switch (should->dtype) {
2044 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
2046 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
2051 if (is_int(is->u1.str))
2058 if (is_float(is->u1.str))
2065 if( !is->u1.str || strlen(is->u1.str) == 0 )
2066 return 1; /* a null arg in the call will match an enum, I guess! */
2067 for (ac=should->choices; ac; ac=ac->next) {
2068 if (strcmp(ac->name,is->u1.str) == 0)
2074 case ARGD_OPTIONSET:
2075 opcop = ast_strdupa(is->u1.str);
2077 for (ac=app->opts; ac; ac=ac->next) {
2078 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
2081 for (ac=app->opts; ac; ac=ac->next) {
2082 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
2083 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
2087 if (ac->name[1] == '(') {
2088 if (*(p+1) == '(') {
2090 while (*q && *q != ')') {
2102 return 1; /* matches anything */
2105 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
2109 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
2112 struct argdesc *ad = app->args;
2116 for (pa = arglist; pa; pa=pa->next) {
2118 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
2119 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
2123 /* find the first entry in the ad list that will match */
2125 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
2128 z= option_matches( ad, pa, app);
2133 if (ad->type == ARGD_REQUIRED) {
2134 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2135 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2139 } else if (z && ad->dtype == ARGD_OPTIONSET) {
2140 option_matches_j( ad, pa, app);
2146 /* any app nodes left, that are not optional? */
2147 for ( ; ad; ad=ad->next) {
2148 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
2151 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
2152 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
2163 void check_switch_expr(pval *item, struct argapp *apps)
2166 /* get and clean the variable name */
2168 struct argapp *a,*a2;
2169 struct appsetvar *v,*v2;
2170 struct argchoice *c;
2174 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
2177 buff1 = ast_strdupa(p);
2179 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
2180 buff1[strlen(buff1)-1] = 0;
2181 /* buff1 now contains the variable name */
2183 for (a=apps; a; a=a->next) {
2184 for (v=a->setvars;v;v=v->next) {
2185 if (strcmp(v->name,buff1) == 0) {
2193 /* we have a match, to a variable that has a set of determined values */
2198 /* first of all, does this switch have a default case ? */
2199 for (t=item->u2.statements; t; t=t->next) {
2200 if (t->type == PV_DEFAULT) {
2204 if (t->type == PV_PATTERN) {
2208 if (def || pat) /* nothing to check. All cases accounted for! */
2210 for (c=v->vals; c; c=c->next) {
2212 for (t=item->u2.statements; t; t=t->next) {
2213 if (t->type == PV_CASE || t->type == PV_PATTERN) {
2214 if (!strcmp(t->u1.str,c->name)) {
2221 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
2222 item->filename, item->startline, item->endline, item->u1.str, c->name);
2226 /* next, is there an app call in the current exten, that would set this var? */
2228 t = current_extension->u2.statements;
2229 if ( t && t->type == PV_STATEMENTBLOCK )
2230 t = t->u1.statements;
2231 for (; t && t != item; t=t->next) {
2232 if (t->type == PV_APPLICATION_CALL) {
2233 /* find the application that matches the u1.str */
2234 for (a2=apps; a2; a2=a2->next) {
2235 if (strcasecmp(a2->name, t->u1.str)==0) {
2236 for (v2=a2->setvars; v2; v2=v2->next) {
2237 if (strcmp(v2->name, buff1) == 0) {
2238 /* found an app that sets the var */
2252 /* see if it sets the var */
2254 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",
2255 item->filename, item->startline, item->endline, item->u1.str);
2263 /* first of all, does this switch have a default case ? */
2264 for (t=item->u2.statements; t; t=t->next) {
2265 if (t->type == PV_DEFAULT) {
2271 if (def) /* nothing to check. All cases accounted for! */
2273 /* if no default, warn and insert a default case at the end */
2274 p2 = tl->next = calloc(1, sizeof(struct pval));
2276 p2->type = PV_DEFAULT;
2277 p2->startline = tl->startline;
2278 p2->endline = tl->endline;
2279 p2->startcol = tl->startcol;
2280 p2->endcol = tl->endcol;
2281 p2->filename = strdup(tl->filename);
2282 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
2283 p2->filename, p2->startline, p2->endline);
2289 static void check_context_names(void)
2292 for (i=current_db; i; i=i->next) {
2293 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
2294 for (j=i->next; j; j=j->next) {
2295 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
2296 if ( !strcmp(i->u1.str, j->u1.str) )
2298 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d!\n",
2299 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
2308 static void check_abstract_reference(pval *abstract_context)
2311 /* find some context includes that reference this context */
2314 /* otherwise, print out a warning */
2315 for (i=current_db; i; i=i->next) {
2316 if (i->type == PV_CONTEXT) {
2317 for (j=i->u2. statements; j; j=j->next) {
2318 if ( j->type == PV_INCLUDES ) {
2320 for (p4=j->u1.list; p4; p4=p4->next) {
2321 /* for each context pointed to, find it, then find a context/label that matches the
2323 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
2324 return; /* found a match! */
2330 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",
2331 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
2336 void check_pval_item(pval *item, struct argapp *apps, int in_globals)
2340 struct argapp *app, *found;
2342 struct pval *macro_def;
2343 struct pval *app_def;
2348 switch (item->type) {
2350 /* fields: item->u1.str == string associated with this (word).
2351 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2355 /* fields: item->u1.str == name of macro
2356 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2357 item->u2.arglist->u1.str == argument
2358 item->u2.arglist->next == next arg
2360 item->u3.macro_statements == pval list of statements in macro body.
2362 in_abstract_context = 0;
2363 current_context = item;
2364 current_extension = 0;
2366 check_macro_returns(item);
2368 for (lp=item->u2.arglist; lp; lp=lp->next) {
2371 check_pval(item->u3.macro_statements, apps,in_globals);
2375 /* fields: item->u1.str == name of context
2376 item->u2.statements == pval list of statements in context body
2377 item->u3.abstract == int 1 if an abstract keyword were present
2379 current_context = item;
2380 current_extension = 0;
2381 if ( item->u3.abstract ) {
2382 in_abstract_context = 1;
2383 check_abstract_reference(item);
2385 in_abstract_context = 0;
2386 check_pval(item->u2.statements, apps,in_globals);
2390 /* fields: item->u1.str == name of macro to call
2391 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2392 item->u2.arglist->u1.str == argument
2393 item->u2.arglist->next == next arg
2396 /* if this is a standalone, we will need to make sure the
2397 localized load of extensions.conf is done */
2398 if (!extensions_dot_conf_loaded) {
2399 localized_pbx_load_module();
2400 extensions_dot_conf_loaded++;
2403 macro_def = find_macro(item->u1.str);
2406 struct pbx_find_info pfiq = {.stacklen = 0 };
2407 struct pbx_find_info pfiq2 = {.stacklen = 0 };
2409 /* look for the macro in the extensions.conf world */
2410 pbx_find_extension(NULL, NULL, &pfiq, item->u1.str, "s", 1, NULL, NULL, E_MATCH);
2412 if (pfiq.status != STATUS_SUCCESS) {
2414 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2416 /* look for the macro in the extensions.conf world */
2417 pbx_find_extension(NULL, NULL, &pfiq2, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2419 if (pfiq2.status == STATUS_SUCCESS) {
2420 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",
2421 item->filename, item->startline, item->endline, item->u1.str, item->u1.str);
2424 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (Not even in the extensions.conf stuff!)\n",
2425 item->filename, item->startline, item->endline, item->u1.str);
2430 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s cannot be found in the AEL code!\n",
2431 item->filename, item->startline, item->endline, item->u1.str);
2435 #ifdef THIS_IS_1DOT4
2437 snprintf(namebuf2, 256, "macro-%s", item->u1.str);
2439 /* look for the macro in the extensions.conf world */
2440 pbx_find_extension(NULL, NULL, &pfiq, namebuf2, "s", 1, NULL, NULL, E_MATCH);
2442 if (pfiq.status != STATUS_SUCCESS) {
2443 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",
2444 item->filename, item->startline, item->endline, item->u1.str);
2450 } else if (macro_def->type != PV_MACRO) {
2451 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2452 item->filename, item->startline, item->endline, item->u1.str);
2455 /* macro_def is a MACRO, so do the args match in number? */
2459 for (lp=item->u2.arglist; lp; lp=lp->next) {
2462 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2465 if (hereargs != thereargs ) {
2466 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",
2467 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2473 case PV_APPLICATION_CALL:
2474 /* fields: item->u1.str == name of application to call
2475 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2476 item->u2.arglist->u1.str == argument
2477 item->u2.arglist->next == next arg
2479 /* Need to check to see if the application is available! */
2480 app_def = find_context(item->u1.str);
2481 if (app_def && app_def->type == PV_MACRO) {
2482 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2483 item->filename, item->startline, item->endline, item->u1.str);
2486 if (strcasecmp(item->u1.str,"GotoIf") == 0
2487 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2488 || strcasecmp(item->u1.str,"while") == 0
2489 || strcasecmp(item->u1.str,"endwhile") == 0
2490 || strcasecmp(item->u1.str,"random") == 0
2491 || strcasecmp(item->u1.str,"gosub") == 0
2492 || strcasecmp(item->u1.str,"return") == 0
2493 || strcasecmp(item->u1.str,"gosubif") == 0
2494 || strcasecmp(item->u1.str,"continuewhile") == 0
2495 || strcasecmp(item->u1.str,"endwhile") == 0
2496 || strcasecmp(item->u1.str,"execif") == 0
2497 || strcasecmp(item->u1.str,"execiftime") == 0
2498 || strcasecmp(item->u1.str,"exitwhile") == 0
2499 || strcasecmp(item->u1.str,"goto") == 0
2500 || strcasecmp(item->u1.str,"macro") == 0
2501 || strcasecmp(item->u1.str,"macroexclusive") == 0
2502 || strcasecmp(item->u1.str,"macroif") == 0
2503 || strcasecmp(item->u1.str,"stackpop") == 0
2504 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2505 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",
2506 item->filename, item->startline, item->endline, item->u1.str);
2509 if (strcasecmp(item->u1.str,"macroexit") == 0) {
2510 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
2511 item->filename, item->startline, item->endline);
2512 item->type = PV_RETURN;
2519 for (app=apps; app; app=app->next) {
2520 if (strcasecmp(app->name, item->u1.str) == 0) {
2526 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2527 item->filename, item->startline, item->endline, item->u1.str);
2530 check_app_args(item, item->u2.arglist, app);
2535 /* fields: item->u1.str == value of case
2536 item->u2.statements == pval list of statements under the case
2538 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2539 /* find the last statement */
2540 check_pval(item->u2.statements, apps,in_globals);
2544 /* fields: item->u1.str == value of case
2545 item->u2.statements == pval list of statements under the case
2547 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2548 /* find the last statement */
2550 check_pval(item->u2.statements, apps,in_globals);
2555 item->u2.statements == pval list of statements under the case
2558 check_pval(item->u2.statements, apps,in_globals);
2562 /* fields: item->u1.str == name of extension to catch
2563 item->u2.statements == pval list of statements in context body
2565 check_pval(item->u2.statements, apps,in_globals);
2569 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2571 check_pval(item->u1.list, apps,in_globals);
2575 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2577 check_pval(item->u1.list, apps,in_globals);
2581 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2583 check_pval(item->u1.list, apps,in_globals);
2584 check_includes(item);
2585 for (lp=item->u1.list; lp; lp=lp->next){
2586 char *incl_context = lp->u1.str;
2587 struct pval *that_context = find_context(incl_context);
2589 if ( lp->u2.arglist ) {
2590 check_timerange(lp->u2.arglist);
2591 check_dow(lp->u2.arglist->next);
2592 check_day(lp->u2.arglist->next->next);
2593 check_month(lp->u2.arglist->next->next->next);
2597 find_pval_gotos(that_context->u2.statements,0);
2603 case PV_STATEMENTBLOCK:
2604 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2606 check_pval(item->u1.list, apps,in_globals);
2610 /* fields: item->u1.str == variable name
2611 item->u2.val == variable value to assign
2613 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2614 if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
2615 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);
2616 ast_expr_register_extra_error_info(errmsg);
2617 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2618 ast_expr_clear_extra_error_info();
2619 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2620 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2621 item->filename, item->startline, item->endline, item->u2.val);
2624 check_expr2_input(item,item->u2.val);
2628 case PV_LOCALVARDEC:
2629 /* fields: item->u1.str == variable name
2630 item->u2.val == variable value to assign
2632 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2633 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);
2634 ast_expr_register_extra_error_info(errmsg);
2635 ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
2636 ast_expr_clear_extra_error_info();
2637 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2638 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2639 item->filename, item->startline, item->endline, item->u2.val);
2642 check_expr2_input(item,item->u2.val);
2646 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2647 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2649 /* don't check goto's in abstract contexts */
2650 if ( in_abstract_context )
2657 /* fields: item->u1.str == label name
2659 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2660 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2661 item->filename, item->startline, item->endline, item->u1.str);
2669 /* fields: item->u1.for_init == a string containing the initalizer
2670 item->u2.for_test == a string containing the loop test
2671 item->u3.for_inc == a string containing the loop increment
2673 item->u4.for_statements == a pval list of statements in the for ()
2675 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);
2676 ast_expr_register_extra_error_info(errmsg);
2678 strp = strchr(item->u1.for_init, '=');
2680 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2682 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
2683 strp = strchr(item->u3.for_inc, '=');
2685 ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
2687 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2688 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2689 item->filename, item->startline, item->endline, item->u2.for_test);
2692 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2693 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2694 item->filename, item->startline, item->endline, item->u3.for_inc);
2697 check_expr2_input(item,item->u2.for_test);
2698 check_expr2_input(item,item->u3.for_inc);
2700 ast_expr_clear_extra_error_info();
2701 check_pval(item->u4.for_statements, apps,in_globals);
2705 /* fields: item->u1.str == the while conditional, as supplied by user
2707 item->u2.statements == a pval list of statements in the while ()
2709 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);
2710 ast_expr_register_extra_error_info(errmsg);
2711 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2712 ast_expr_clear_extra_error_info();
2713 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2714 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2715 item->filename, item->startline, item->endline, item->u1.str);
2718 check_expr2_input(item,item->u1.str);
2719 check_pval(item->u2.statements, apps,in_globals);
2736 check_continue(item);
2740 /* fields: item->u1.str == the random number expression, as supplied by user
2742 item->u2.statements == a pval list of statements in the if ()
2743 item->u3.else_statements == a pval list of statements in the else
2746 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);
2747 ast_expr_register_extra_error_info(errmsg);
2748 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2749 ast_expr_clear_extra_error_info();
2750 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2751 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2752 item->filename, item->startline, item->endline, item->u1.str);
2755 check_expr2_input(item,item->u1.str);
2756 check_pval(item->u2.statements, apps,in_globals);
2757 if (item->u3.else_statements) {
2758 check_pval(item->u3.else_statements, apps,in_globals);
2763 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2765 item->u2.statements == a pval list of statements in the if ()
2766 item->u3.else_statements == a pval list of statements in the else
2769 if ( item->u2.arglist ) {
2770 check_timerange(item->u1.list);
2771 check_dow(item->u1.list->next);
2772 check_day(item->u1.list->next->next);
2773 check_month(item->u1.list->next->next->next);
2776 check_pval(item->u2.statements, apps,in_globals);
2777 if (item->u3.else_statements) {
2778 check_pval(item->u3.else_statements, apps,in_globals);
2783 /* fields: item->u1.str == the if conditional, as supplied by user
2785 item->u2.statements == a pval list of statements in the if ()
2786 item->u3.else_statements == a pval list of statements in the else
2789 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);
2790 ast_expr_register_extra_error_info(errmsg);
2791 ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
2792 ast_expr_clear_extra_error_info();
2793 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2794 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2795 item->filename, item->startline, item->endline, item->u1.str);
2798 check_expr2_input(item,item->u1.str);
2799 check_pval(item->u2.statements, apps,in_globals);
2800 if (item->u3.else_statements) {
2801 check_pval(item->u3.else_statements, apps,in_globals);
2806 /* fields: item->u1.str == the switch expression
2808 item->u2.statements == a pval list of statements in the switch,
2809 (will be case statements, most likely!)
2811 /* we can check the switch expression, see if it matches any of the app variables...
2812 if it does, then, are all the possible cases accounted for? */
2813 check_switch_expr(item, apps);
2814 check_pval(item->u2.statements, apps,in_globals);
2818 /* fields: item->u1.str == the extension name, label, whatever it's called
2820 item->u2.statements == a pval list of statements in the extension
2821 item->u3.hints == a char * hint argument
2822 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2824 current_extension = item ;
2826 check_pval(item->u2.statements, apps,in_globals);
2830 /* fields: item->u1.str == the ignorepat data
2835 /* fields: item->u1.statements == pval list of statements, usually vardecs
2837 in_abstract_context = 0;
2838 check_pval(item->u1.statements, apps, 1);
2845 void check_pval(pval *item, struct argapp *apps, int in_globals)
2850 1. Do goto's point to actual labels?
2851 2. Do macro calls reference a macro?
2852 3. Does the number of macro args match the definition?
2853 4. Is a macro call missing its & at the front?
2854 5. Application calls-- we could check syntax for existing applications,
2855 but I need some some sort of universal description bnf for a general
2856 sort of method for checking arguments, in number, maybe even type, at least.
2857 Don't want to hand code checks for hundreds of applications.
2860 for (i=item; i; i=i->next) {
2861 check_pval_item(i,apps,in_globals);
2865 void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2872 struct argapp *apps=0;
2875 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2876 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2878 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2881 errs = warns = notes = 0;
2883 check_context_names();
2884 check_pval(item, apps, 0);
2887 argdesc_destroy(apps); /* taketh away */
2896 /* =============================================================================================== */
2897 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2898 /* =============================================================================================== */
2900 static int control_statement_count = 0;
2902 struct ael_priority *new_prio(void)
2904 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2908 struct ael_extension *new_exten(void)
2910 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2914 void linkprio(struct ael_extension *exten, struct ael_priority *prio)
2916 if (!exten->plist) {
2917 exten->plist = prio;
2918 exten->plist_last = prio;
2920 exten->plist_last->next = prio;
2921 exten->plist_last = prio;
2924 prio->exten = exten; /* don't override the switch value */
2927 void destroy_extensions(struct ael_extension *exten)
2929 struct ael_extension *ne, *nen;
2930 for (ne=exten; ne; ne=nen) {
2931 struct ael_priority *pe, *pen;
2936 /* cidmatch fields are allocated with name, and freed when
2937 the name field is freed. Don't do a free for this field,
2938 unless you LIKE to see a crash! */
2943 for (pe=ne->plist; pe; pe=pen) {
2956 nen = ne->next_exten;
2962 ne->loop_continue = 0;
2967 static int label_inside_case(pval *label)
2971 while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
2972 if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
2981 static void linkexten(struct ael_extension *exten, struct ael_extension *add)
2983 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2984 exten->next_exten = add;
2987 static void remove_spaces_before_equals(char *str)
2990 while( str && *str && *str != '=' )
2992 if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
3006 /* =============================================================================================== */
3007 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
3008 /* =============================================================================================== */
3010 static void gen_match_to_pattern(char *pattern, char *result)
3012 /* the result will be a string that will be matched by pattern */
3013 char *p=pattern, *t=result;
3015 if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
3017 else if (*p == '[') {
3023 *t++=*(p+1); /* use the first char in the set */
3030 *t++ = 0; /* cap it off */
3033 static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
3036 struct ael_priority *pr;
3037 struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
3038 struct ael_priority *while_test, *while_loop, *while_end;
3039 struct ael_priority *switch_test, *switch_end, *fall_thru;
3040 struct ael_priority *if_test, *if_end, *if_skip, *if_false;
3041 #ifdef OLD_RAND_ACTION
3042 struct ael_priority *rand_test, *rand_end, *rand_skip;
3047 char new_label[2000];
3049 int local_control_statement_count;
3051 struct ael_priority *loop_break_save;
3052 struct ael_priority *loop_continue_save;
3053 struct ael_extension *switch_case;
3055 for (p=statement; p; p=p->next) {
3059 pr->type = AEL_APPCALL;
3060 snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
3061 pr->app = strdup("Set");
3062 remove_spaces_before_equals(buf1);
3063 pr->appargs = strdup(buf1);
3065 linkprio(exten, pr);
3068 case PV_LOCALVARDEC:
3070 pr->type = AEL_APPCALL;
3071 snprintf(buf1,sizeof(buf1),"LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
3072 pr->app = strdup("Set");
3073 remove_spaces_before_equals(buf1);
3074 pr->appargs = strdup(buf1);
3076 linkprio(exten, pr);
3081 pr->type = AEL_APPCALL;
3082 p->u2.goto_target = get_goto_target(p);
3083 if( p->u2.goto_target ) {
3084 p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
3087 if (!p->u1.list->next) /* just one */ {
3088 pr->app = strdup("Goto");
3090 pr->appargs = strdup(p->u1.list->u1.str);
3091 else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
3092 snprintf(buf1,sizeof(buf1),"%s,%s", mother_exten->name, p->u1.list->u1.str);
3093 pr->appargs = strdup(buf1);
3096 } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
3097 snprintf(buf1,sizeof(buf1),"%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
3098 pr->app = strdup("Goto");
3099 pr->appargs = strdup(buf1);
3100 } else if (p->u1.list->next && p->u1.list->next->next) {
3101 snprintf(buf1,sizeof(buf1),"%s,%s,%s", p->u1.list->u1.str,
3102 p->u1.list->next->u1.str,
3103 p->u1.list->next->next->u1.str);
3104 pr->app = strdup("Goto");
3105 pr->appargs = strdup(buf1);
3108 linkprio(exten, pr);
3113 pr->type = AEL_LABEL;
3115 p->u3.compiled_label = exten;
3116 linkprio(exten, pr);
3120 control_statement_count++;
3121 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
3122 loop_continue_save = exten->loop_continue;
3123 snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
3124 for_init = new_prio();
3125 for_inc = new_prio();
3126 for_test = new_prio();
3127 for_loop = new_prio();
3128 for_end = new_prio();
3129 for_init->type = AEL_APPCALL;
3130 for_inc->type = AEL_APPCALL;
3131 for_test->type = AEL_FOR_CONTROL;
3132 for_test->goto_false = for_end;
3133 for_loop->type = AEL_CONTROL1; /* simple goto */
3134 for_end->type = AEL_APPCALL;
3135 for_init->app = strdup("Set");
3137 strcpy(buf2,p->u1.for_init);
3138 remove_spaces_before_equals(buf2);
3139 strp = strchr(buf2, '=');
3140 strp2 = strchr(p->u1.for_init, '=');
3144 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3146 for_init->appargs = strdup(buf2);
3148 for_init->appargs = strdup(p->u1.for_init);
3151 strcpy(buf2,p->u3.for_inc);
3152 remove_spaces_before_equals(buf2);
3153 strp = strchr(buf2, '=');
3154 if (strp) { /* there's an = in this part; that means an assignment. set it up */
3155 strp2 = strchr(p->u3.for_inc, '=');
3158 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
3160 for_inc->appargs = strdup(buf2);
3161 for_inc->app = strdup("Set");
3163 strp2 = p->u3.for_inc;
3164 while (*strp2 && isspace(*strp2))
3166 if (*strp2 == '&') { /* itsa macro call */
3167 char *strp3 = strp2+1;
3168 while (*strp3 && isspace(*strp3))
3170 strcpy(buf2, strp3);
3171 strp3 = strchr(buf2,'(');
3175 strp3 = strrchr(buf2, ')');
3177 *strp3 = 0; /* remove the closing paren */
3179 for_inc->appargs = strdup(buf2);
3181 for_inc->app = strdup("Macro");
3182 } else { /* must be a regular app call */
3184 strcpy(buf2, strp2);
3185 strp3 = strchr(buf2,'(');
3188 for_inc->app = strdup(buf2);