2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Digium, Inc.
6 * Steve Murphy <murf@parsetree.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
25 #include <sys/types.h>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/pbx.h"
39 #include "asterisk/config.h"
40 #include "asterisk/module.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/cli.h"
43 #include "asterisk/app.h"
44 #include "asterisk/callerid.h"
45 #include "asterisk/ael_structs.h"
47 #include "asterisk/argdesc.h"
50 static char expr_output[2096];
52 /* these functions are in ../ast_expr2.fl */
55 #ifdef __AST_DEBUG_MALLOC
56 static void FREE(void *ptr)
64 #define DEBUG_READ (1 << 0)
65 #define DEBUG_TOKENS (1 << 1)
66 #define DEBUG_MACROS (1 << 2)
67 #define DEBUG_CONTEXTS (1 << 3)
69 static int aeldebug = 0;
71 static char *dtext = "Asterisk Extension Language Compiler v2";
72 static char *config = "extensions.ael";
73 static char *registrar = "pbx_ael";
75 static int errs, warns, notes;
78 /* for the time being, short circuit all the AAL related structures
79 without permanently removing the code; after/during the AAL
80 development, this code can be properly re-instated
83 /* null definitions for structs passed down the infrastructure */
92 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
93 int option_matches( struct argdesc *should, pval *is, struct argapp *app);
94 int ael_is_funcname(char *name);
97 int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
98 void check_pval(pval *item, struct argapp *apps);
99 void check_pval_item(pval *item, struct argapp *apps);
100 void check_switch_expr(pval *item, struct argapp *apps);
101 void ast_expr_register_extra_error_info(char *errmsg);
102 void ast_expr_clear_extra_error_info(void);
103 int ast_expr(char *expr, char *buf, int length);
104 struct pval *find_macro(char *name);
105 struct pval *find_context(char *name);
106 struct pval *find_context(char *name);
107 struct pval *find_macro(char *name);
108 struct ael_priority *new_prio(void);
109 struct ael_extension *new_exten(void);
110 void linkprio(struct ael_extension *exten, struct ael_priority *prio);
111 void destroy_extensions(struct ael_extension *exten);
112 void linkexten(struct ael_extension *exten, struct ael_extension *add);
113 void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten );
114 void set_priorities(struct ael_extension *exten);
115 void add_extensions(struct ael_extension *exten, struct ast_context *context);
116 void ast_compile_ael2(struct ast_context **local_contexts, struct pval *root);
117 void destroy_pval(pval *item);
118 void destroy_pval_item(pval *item);
119 int is_float(char *arg );
120 int is_int(char *arg );
121 int is_empty(char *arg);
122 static pval *current_db;
123 static pval *current_context;
124 static pval *current_extension;
125 static const char *description(void);
126 static const char *key(void);
128 static const char *match_context;
129 static const char *match_exten;
130 static const char *match_label;
131 static int in_abstract_context;
132 static int count_labels; /* true, put matcher in label counting mode */
133 static int label_count; /* labels are only meant to be counted in a context or exten */
134 static int return_on_context_match;
135 static pval *last_matched_label;
136 struct pval *match_pval(pval *item);
137 static void check_timerange(pval *p);
138 static void check_dow(pval *DOW);
139 static void check_day(pval *DAY);
140 static void check_month(pval *MON);
141 static void check_expr2_input(pval *expr, char *str);
142 static int extension_matches(pval *here, const char *exten, const char *pattern);
143 static void check_goto(pval *item);
144 static void find_pval_goto_item(pval *item, int lev);
145 static void find_pval_gotos(pval *item, int lev);
147 static struct pval *find_label_in_current_context(char *exten, char *label);
148 static void print_pval_list(FILE *fin, pval *item, int depth);
150 static struct pval *find_label_in_current_extension(const char *label);
151 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
153 /* PRETTY PRINTER FOR AEL: ============================================================================= */
155 static void print_pval(FILE *fin, pval *item, int depth)
160 for (i=0; i<depth; i++) {
161 fprintf(fin, "\t"); /* depth == indentation */
164 switch ( item->type ) {
166 fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
170 fprintf(fin,"macro %s(", item->u1.str);
171 for (lp=item->u2.arglist; lp; lp=lp->next) {
172 if (lp != item->u2.arglist )
174 fprintf(fin,"%s", lp->u1.str);
176 fprintf(fin,") {\n");
177 print_pval_list(fin,item->u3.macro_statements,depth+1);
178 for (i=0; i<depth; i++) {
179 fprintf(fin,"\t"); /* depth == indentation */
181 fprintf(fin,"};\n\n");
185 if ( item->u3.abstract )
186 fprintf(fin,"abstract context %s {\n", item->u1.str);
188 fprintf(fin,"context %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 */
193 fprintf(fin,"};\n\n");
197 fprintf(fin,"&%s(", item->u1.str);
198 for (lp=item->u2.arglist; lp; lp=lp->next) {
199 if ( lp != item->u2.arglist )
201 fprintf(fin,"%s", lp->u1.str);
206 case PV_APPLICATION_CALL:
207 fprintf(fin,"%s(", item->u1.str);
208 for (lp=item->u2.arglist; lp; lp=lp->next) {
209 if ( lp != item->u2.arglist )
211 fprintf(fin,"%s", lp->u1.str);
217 fprintf(fin,"case %s:\n", item->u1.str);
218 print_pval_list(fin,item->u2.statements, depth+1);
222 fprintf(fin,"pattern %s:\n", item->u1.str);
223 print_pval_list(fin,item->u2.statements, depth+1);
227 fprintf(fin,"default:\n");
228 print_pval_list(fin,item->u2.statements, depth+1);
232 fprintf(fin,"catch %s {\n", item->u1.str);
233 print_pval_list(fin,item->u2.statements, depth+1);
234 for (i=0; i<depth; i++) {
235 fprintf(fin,"\t"); /* depth == indentation */
241 fprintf(fin,"switches {\n");
242 print_pval_list(fin,item->u1.list,depth+1);
243 for (i=0; i<depth; i++) {
244 fprintf(fin,"\t"); /* depth == indentation */
250 fprintf(fin,"eswitches {\n");
251 print_pval_list(fin,item->u1.list,depth+1);
252 for (i=0; i<depth; i++) {
253 fprintf(fin,"\t"); /* depth == indentation */
259 fprintf(fin,"includes {\n");
260 for (lp=item->u1.list; lp; lp=lp->next) {
261 for (i=0; i<depth+1; i++) {
262 fprintf(fin,"\t"); /* depth == indentation */
264 fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
265 if ( lp->u2.arglist )
266 fprintf(fin,"|%s|%s|%s|%s",
267 lp->u2.arglist->u1.str,
268 lp->u2.arglist->next->u1.str,
269 lp->u2.arglist->next->next->u1.str,
270 lp->u2.arglist->next->next->next->u1.str
272 fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
275 print_pval_list(fin,item->u1.list,depth+1);
276 for (i=0; i<depth; i++) {
277 fprintf(fin,"\t"); /* depth == indentation */
282 case PV_STATEMENTBLOCK:
284 print_pval_list(fin,item->u1.list, depth+1);
285 for (i=0; i<depth; i++) {
286 fprintf(fin,"\t"); /* depth == indentation */
292 fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
296 fprintf(fin,"goto %s", item->u1.list->u1.str);
297 if ( item->u1.list->next )
298 fprintf(fin,"|%s", item->u1.list->next->u1.str);
299 if ( item->u1.list->next && item->u1.list->next->next )
300 fprintf(fin,"|%s", item->u1.list->next->next->u1.str);
305 fprintf(fin,"%s:\n", item->u1.str);
309 fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
310 print_pval_list(fin,item->u4.for_statements,depth+1);
314 fprintf(fin,"while (%s)\n", item->u1.str);
315 print_pval_list(fin,item->u2.statements,depth+1);
319 fprintf(fin,"break;\n");
323 fprintf(fin,"return;\n");
327 fprintf(fin,"continue;\n");
333 if ( item->type == PV_IFTIME ) {
335 fprintf(fin,"ifTime ( %s|%s|%s|%s )\n",
336 item->u1.list->u1.str,
337 item->u1.list->next->u1.str,
338 item->u1.list->next->next->u1.str,
339 item->u1.list->next->next->next->u1.str
341 } else if ( item->type == PV_RANDOM ) {
342 fprintf(fin,"random ( %s )\n", item->u1.str );
344 fprintf(fin,"if ( %s )\n", item->u1.str);
345 if ( item->u2.statements && item->u2.statements->next ) {
346 for (i=0; i<depth; i++) {
347 fprintf(fin,"\t"); /* depth == indentation */
350 print_pval_list(fin,item->u2.statements,depth+1);
351 for (i=0; i<depth; i++) {
352 fprintf(fin,"\t"); /* depth == indentation */
354 if ( item->u3.else_statements )
358 } else if (item->u2.statements ) {
359 print_pval_list(fin,item->u2.statements,depth+1);
361 if (item->u3.else_statements )
362 fprintf(fin, " {} ");
364 fprintf(fin, " {}; ");
366 if ( item->u3.else_statements ) {
367 for (i=0; i<depth; i++) {
368 fprintf(fin,"\t"); /* depth == indentation */
370 fprintf(fin,"else\n");
371 print_pval_list(fin,item->u3.else_statements, depth);
376 fprintf(fin,"switch( %s ) {\n", item->u1.str);
377 print_pval_list(fin,item->u2.statements,depth+1);
378 for (i=0; i<depth; i++) {
379 fprintf(fin,"\t"); /* depth == indentation */
385 if ( item->u4.regexten )
386 fprintf(fin, "regexten ");
387 if ( item->u3.hints )
388 fprintf(fin,"hints(%s) ", item->u3.hints);
390 fprintf(fin,"%s => \n", item->u1.str);
391 print_pval_list(fin,item->u2.statements,depth+1);
395 fprintf(fin,"ignorepat => %s\n", item->u1.str);
399 fprintf(fin,"globals {\n");
400 print_pval_list(fin,item->u1.statements,depth+1);
401 for (i=0; i<depth; i++) {
402 fprintf(fin,"\t"); /* depth == indentation */
409 static void print_pval_list(FILE *fin, pval *item, int depth)
413 for (i=item; i; i=i->next) {
414 print_pval(fin, i, depth);
419 static void ael2_print(char *fname, pval *tree)
421 FILE *fin = fopen(fname,"w");
423 ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
426 print_pval_list(fin, tree, 0);
432 /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL: ============================================================================= */
434 void traverse_pval_template(pval *item, int depth);
435 void traverse_pval_item_template(pval *item, int depth);
438 void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
439 but you may not need it */
443 switch ( item->type ) {
445 /* fields: item->u1.str == string associated with this (word). */
449 /* fields: item->u1.str == name of macro
450 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
451 item->u2.arglist->u1.str == argument
452 item->u2.arglist->next == next arg
454 item->u3.macro_statements == pval list of statements in macro body.
456 for (lp=item->u2.arglist; lp; lp=lp->next) {
459 traverse_pval_item_template(item->u3.macro_statements,depth+1);
463 /* fields: item->u1.str == name of context
464 item->u2.statements == pval list of statements in context body
465 item->u3.abstract == int 1 if an abstract keyword were present
467 traverse_pval_item_template(item->u2.statements,depth+1);
471 /* fields: item->u1.str == name of macro to call
472 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
473 item->u2.arglist->u1.str == argument
474 item->u2.arglist->next == next arg
476 for (lp=item->u2.arglist; lp; lp=lp->next) {
480 case PV_APPLICATION_CALL:
481 /* fields: item->u1.str == name of application to call
482 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
483 item->u2.arglist->u1.str == argument
484 item->u2.arglist->next == next arg
486 for (lp=item->u2.arglist; lp; lp=lp->next) {
491 /* fields: item->u1.str == value of case
492 item->u2.statements == pval list of statements under the case
494 traverse_pval_item_template(item->u2.statements,depth+1);
498 /* fields: item->u1.str == value of case
499 item->u2.statements == pval list of statements under the case
501 traverse_pval_item_template(item->u2.statements,depth+1);
506 item->u2.statements == pval list of statements under the case
508 traverse_pval_item_template(item->u2.statements,depth+1);
512 /* fields: item->u1.str == name of extension to catch
513 item->u2.statements == pval list of statements in context body
515 traverse_pval_item_template(item->u2.statements,depth+1);
519 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
521 traverse_pval_item_template(item->u1.list,depth+1);
525 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
527 traverse_pval_item_template(item->u1.list,depth+1);
531 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
532 item->u2.arglist == pval list of 4 PV_WORD elements for time values
534 traverse_pval_item_template(item->u1.list,depth+1);
535 traverse_pval_item_template(item->u2.arglist,depth+1);
538 case PV_STATEMENTBLOCK:
539 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
541 traverse_pval_item_template(item->u1.list,depth+1);
545 /* fields: item->u1.str == variable name
546 item->u2.val == variable value to assign
551 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
552 item->u1.list->u1.str == where the data on a PV_WORD will always be.
555 if ( item->u1.list->next )
557 if ( item->u1.list->next && item->u1.list->next->next )
563 /* fields: item->u1.str == label name
568 /* fields: item->u1.for_init == a string containing the initalizer
569 item->u2.for_test == a string containing the loop test
570 item->u3.for_inc == a string containing the loop increment
572 item->u4.for_statements == a pval list of statements in the for ()
574 traverse_pval_item_template(item->u4.for_statements,depth+1);
578 /* fields: item->u1.str == the while conditional, as supplied by user
580 item->u2.statements == a pval list of statements in the while ()
582 traverse_pval_item_template(item->u2.statements,depth+1);
601 /* fields: item->u1.list == there are 4 linked PV_WORDs here.
603 item->u2.statements == a pval list of statements in the if ()
604 item->u3.else_statements == a pval list of statements in the else
607 traverse_pval_item_template(item->u2.statements,depth+1);
608 if ( item->u3.else_statements ) {
609 traverse_pval_item_template(item->u3.else_statements,depth+1);
614 /* fields: item->u1.str == the random number expression, as supplied by user
616 item->u2.statements == a pval list of statements in the if ()
617 item->u3.else_statements == a pval list of statements in the else
620 traverse_pval_item_template(item->u2.statements,depth+1);
621 if ( item->u3.else_statements ) {
622 traverse_pval_item_template(item->u3.else_statements,depth+1);
627 /* fields: item->u1.str == the if conditional, as supplied by user
629 item->u2.statements == a pval list of statements in the if ()
630 item->u3.else_statements == a pval list of statements in the else
633 traverse_pval_item_template(item->u2.statements,depth+1);
634 if ( item->u3.else_statements ) {
635 traverse_pval_item_template(item->u3.else_statements,depth+1);
640 /* fields: item->u1.str == the switch expression
642 item->u2.statements == a pval list of statements in the switch,
643 (will be case statements, most likely!)
645 traverse_pval_item_template(item->u2.statements,depth+1);
649 /* fields: item->u1.str == the extension name, label, whatever it's called
651 item->u2.statements == a pval list of statements in the extension
652 item->u3.hints == a char * hint argument
653 item->u4.regexten == an int boolean. non-zero says that regexten was specified
655 traverse_pval_item_template(item->u2.statements,depth+1);
659 /* fields: item->u1.str == the ignorepat data
664 /* fields: item->u1.statements == pval list of statements, usually vardecs
666 traverse_pval_item_template(item->u1.statements,depth+1);
671 void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
672 but you may not need it */
676 for (i=item; i; i=i->next) {
677 traverse_pval_item_template(i, depth);
682 /* SEMANTIC CHECKING FOR AEL: ============================================================================= */
684 /* (not all that is syntactically legal is good! */
688 static int extension_matches(pval *here, const char *exten, const char *pattern)
693 /* simple case, they match exactly, the pattern and exten name */
694 if( !strcmp(pattern,exten) == 0 )
697 if ( pattern[0] == '_' ) {
702 if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
703 ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
707 /* form a regular expression from the pattern, and then match it against exten */
708 *r++ = '^'; /* what if the extension is a pattern ?? */
709 *r++ = '_'; /* what if the extension is a pattern ?? */
711 for (p=pattern+1; *p; p++) {
741 while ( *p && *p != ']' ) {
745 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
746 here->filename, here->startline, here->endline, pattern);
765 *r++ = '$'; /* what if the extension is a pattern ?? */
766 *r++ = *p++; /* put in the closing null */
767 err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
770 regerror(err1,&preg,errmess,sizeof(errmess));
772 ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
776 err1 = regexec(&preg, exten, 0, 0, 0);
780 /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
781 err1,exten, pattern, reg1); */
782 return 0; /* no match */
784 /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
791 if ( strcmp(exten,pattern) == 0 ) {
799 static void check_expr2_input(pval *expr, char *str)
801 int spaces = strspn(str,"\t \n");
802 if ( !strncmp(str+spaces,"$[",2) ) {
803 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
804 expr->filename, expr->startline, expr->endline, str);
809 static void check_timerange(pval *p)
817 strncpy(times, p->u1.str, sizeof(times));
818 /* Star is all times */
819 if (ast_strlen_zero(times) || !strcmp(times, "*")) {
822 /* Otherwise expect a range */
823 e = strchr(times, '-');
825 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",
826 p->filename, p->startline, p->endline, times);
832 while (*e && !isdigit(*e))
835 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
836 p->filename, p->startline, p->endline, p->u1.str);
839 if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
840 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
841 p->filename, p->startline, p->endline, times);
844 if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
845 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
846 p->filename, p->startline, p->endline, times);
851 if ((s1 < 0) || (s1 >= 24*30)) {
852 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
853 p->filename, p->startline, p->endline, times);
857 if ((e1 < 0) || (e1 >= 24*30)) {
858 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
859 p->filename, p->startline, p->endline, e);
865 static char *days[] =
876 /*! \brief get_dow: Get day of week */
877 static void check_dow(pval *DOW)
881 /* The following line is coincidence, really! */
884 strncpy(dow,DOW->u1.str,sizeof(dow));
886 /* Check for all days */
887 if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
889 /* Get start and ending days */
890 c = strchr(dow, '-');
898 while ((s < 7) && strcasecmp(dow, days[s])) s++;
900 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",
901 DOW->filename, DOW->startline, DOW->endline, dow);
906 while ((e < 7) && strcasecmp(c, days[e])) e++;
908 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",
909 DOW->filename, DOW->startline, DOW->endline, c);
916 static void check_day(pval *DAY)
920 /* The following line is coincidence, really! */
923 strncpy(day,DAY->u1.str,sizeof(day));
924 /* Check for all days */
925 if (ast_strlen_zero(day) || !strcmp(day, "*")) {
928 /* Get start and ending days */
929 c = strchr(day, '-');
935 if (sscanf(day, "%d", &s) != 1) {
936 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
937 DAY->filename, DAY->startline, DAY->endline, day);
940 else if ((s < 1) || (s > 31)) {
941 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",
942 DAY->filename, DAY->startline, DAY->endline, day);
947 if (sscanf(c, "%d", &e) != 1) {
948 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
949 DAY->filename, DAY->startline, DAY->endline, c);
952 else if ((e < 1) || (e > 31)) {
953 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",
954 DAY->filename, DAY->startline, DAY->endline, day);
962 static char *months[] =
978 static void check_month(pval *MON)
982 /* The following line is coincidence, really! */
985 strncpy(mon,MON->u1.str,sizeof(mon));
986 /* Check for all days */
987 if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
989 /* Get start and ending days */
990 c = strchr(mon, '-');
997 while ((s < 12) && strcasecmp(mon, months[s])) s++;
999 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1000 MON->filename, MON->startline, MON->endline, mon);
1005 while ((e < 12) && strcasecmp(mon, months[e])) e++;
1007 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
1008 MON->filename, MON->startline, MON->endline, c);
1015 /* general purpose goto finder */
1018 static void check_goto(pval *item)
1020 /* check for the target of the goto-- does it exist? */
1021 if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
1022 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
1023 item->filename, item->startline, item->endline);
1027 /* just one item-- the label should be in the current extension */
1029 if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
1030 struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str));
1031 /* printf("Calling find_label_in_current_extension with args %s\n",
1032 (char*)((item->u1.list)->u1.str)); */
1034 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
1035 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1043 if (item->u1.list->next && !item->u1.list->next->next) {
1045 /* printf("Calling find_label_in_current_context with args %s, %s\n",
1046 (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
1047 if (!strstr((item->u1.list)->u1.str,"${")
1048 && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
1049 struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str);
1051 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",
1052 item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
1061 if (item->u1.list->next && item->u1.list->next->next) {
1063 pval *first = item->u1.list;
1064 pval *second = item->u1.list->next;
1065 pval *third = item->u1.list->next->next;
1067 /* printf("Calling find_label_in_current_context with args %s, %s, %s\n",
1068 (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
1069 if (!strstr((item->u1.list)->u1.str,"${")
1070 && !strstr(item->u1.list->next->u1.str,"${")
1071 && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
1072 struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
1075 struct pval *found = 0;
1076 struct pval *that_context = find_context(item->u1.list->u1.str);
1078 /* the target of the goto could be in an included context!! Fancy that!! */
1079 /* look for includes in the current context */
1081 for (p3=that_context->u2.statements; p3; p3=p3->next) {
1082 if (p3->type == PV_INCLUDES) {
1084 for (p4=p3->u1.list; p4; p4=p4->next) {
1085 /* for each context pointed to, find it, then find a context/label that matches the
1087 char *incl_context = p4->u1.str;
1088 /* find a matching context name */
1089 struct pval *that_other_context = find_context(incl_context);
1090 if (that_other_context) {
1091 struct pval *context_save = current_context;
1093 current_context = that_other_context;
1094 x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str);
1095 current_context = context_save;
1105 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
1106 item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
1110 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no context %s could be found that matches the goto target!\n",
1111 item->filename, item->startline, item->endline, item->u1.list->u1.str);
1120 static void find_pval_goto_item(pval *item, int lev)
1124 ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
1128 switch ( item->type ) {
1130 /* fields: item->u1.str == name of macro
1131 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1132 item->u2.arglist->u1.str == argument
1133 item->u2.arglist->next == next arg
1135 item->u3.macro_statements == pval list of statements in macro body.
1138 /* printf("Descending into matching macro %s\n", match_context); */
1139 find_pval_gotos(item->u2.statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
1144 /* fields: item->u1.str == name of context
1145 item->u2.statements == pval list of statements in context body
1146 item->u3.abstract == int 1 if an abstract keyword were present
1151 /* fields: item->u1.str == value of case
1152 item->u2.statements == pval list of statements under the case
1154 find_pval_gotos(item->u2.statements,lev+1);
1158 /* fields: item->u1.str == value of case
1159 item->u2.statements == pval list of statements under the case
1161 find_pval_gotos(item->u2.statements,lev+1);
1166 item->u2.statements == pval list of statements under the case
1168 find_pval_gotos(item->u2.statements,lev+1);
1172 /* fields: item->u1.str == name of extension to catch
1173 item->u2.statements == pval list of statements in context body
1175 find_pval_gotos(item->u2.statements,lev+1);
1178 case PV_STATEMENTBLOCK:
1179 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1181 find_pval_gotos(item->u1.list,lev+1);
1185 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
1186 item->u1.list->u1.str == where the data on a PV_WORD will always be.
1188 check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
1192 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
1194 for (p4=item->u1.list; p4; p4=p4->next) {
1195 /* for each context pointed to, find it, then find a context/label that matches the
1197 char *incl_context = p4->u1.str;
1198 /* find a matching context name */
1199 struct pval *that_context = find_context(incl_context);
1201 find_pval_gotos(that_context,lev+1); /* keep working up the includes */
1207 /* fields: item->u1.for_init == a string containing the initalizer
1208 item->u2.for_test == a string containing the loop test
1209 item->u3.for_inc == a string containing the loop increment
1211 item->u4.for_statements == a pval list of statements in the for ()
1213 find_pval_gotos(item->u4.for_statements,lev+1);
1217 /* fields: item->u1.str == the while conditional, as supplied by user
1219 item->u2.statements == a pval list of statements in the while ()
1221 find_pval_gotos(item->u2.statements,lev+1);
1225 /* fields: item->u1.str == the random number expression, as supplied by user
1227 item->u2.statements == a pval list of statements in the if ()
1228 item->u3.else_statements == a pval list of statements in the else
1230 fall thru to PV_IF */
1233 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1235 item->u2.statements == a pval list of statements in the if ()
1236 item->u3.else_statements == a pval list of statements in the else
1238 fall thru to PV_IF*/
1240 /* fields: item->u1.str == the if conditional, as supplied by user
1242 item->u2.statements == a pval list of statements in the if ()
1243 item->u3.else_statements == a pval list of statements in the else
1246 find_pval_gotos(item->u2.statements,lev+1);
1248 if (item->u3.else_statements) {
1249 find_pval_gotos(item->u3.else_statements,lev+1);
1254 /* fields: item->u1.str == the switch expression
1256 item->u2.statements == a pval list of statements in the switch,
1257 (will be case statements, most likely!)
1259 find_pval_gotos(item->u3.else_statements,lev+1);
1263 /* fields: item->u1.str == the extension name, label, whatever it's called
1265 item->u2.statements == a pval list of statements in the extension
1266 item->u3.hints == a char * hint argument
1267 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1270 find_pval_gotos(item->u2.statements,lev+1);
1278 static void find_pval_gotos(pval *item,int lev)
1282 for (i=item; i; i=i->next) {
1284 find_pval_goto_item(i, lev);
1290 /* general purpose label finder */
1291 static struct pval *match_pval_item(pval *item)
1295 switch ( item->type ) {
1297 /* fields: item->u1.str == name of macro
1298 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
1299 item->u2.arglist->u1.str == argument
1300 item->u2.arglist->next == next arg
1302 item->u3.macro_statements == pval list of statements in macro body.
1304 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1305 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1306 /* printf("Returning on matching macro %s\n", match_context); */
1311 if (!return_on_context_match) {
1312 /* printf("Descending into matching macro %s\n", match_context); */
1313 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1318 /* printf("Skipping context/macro %s\n", item->u1.str); */
1324 /* fields: item->u1.str == name of context
1325 item->u2.statements == pval list of statements in context body
1326 item->u3.abstract == int 1 if an abstract keyword were present
1328 if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
1329 if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
1330 /* printf("Returning on matching context %s\n", match_context); */
1335 if (!return_on_context_match ) {
1336 /* printf("Descending into matching context %s\n", match_context); */
1337 if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
1342 /* printf("Skipping context/macro %s\n", item->u1.str); */
1347 /* fields: item->u1.str == value of case
1348 item->u2.statements == pval list of statements under the case
1350 if ((x=match_pval(item->u2.statements))) {
1356 /* fields: item->u1.str == value of case
1357 item->u2.statements == pval list of statements under the case
1359 if ((x=match_pval(item->u2.statements))) {
1366 item->u2.statements == pval list of statements under the case
1368 if ((x=match_pval(item->u2.statements))) {
1374 /* fields: item->u1.str == name of extension to catch
1375 item->u2.statements == pval list of statements in context body
1377 if ((x=match_pval(item->u2.statements))) {
1382 case PV_STATEMENTBLOCK:
1383 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
1385 if ((x=match_pval(item->u1.list))) {
1391 /* fields: item->u1.str == label name
1393 /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
1394 item->u1.str, current_context->u1.str, current_extension->u1.str); */
1397 if (!strcmp(match_label, item->u1.str)) {
1399 last_matched_label = item;
1403 if (!strcmp(match_label, item->u1.str)) {
1410 /* fields: item->u1.for_init == a string containing the initalizer
1411 item->u2.for_test == a string containing the loop test
1412 item->u3.for_inc == a string containing the loop increment
1414 item->u4.for_statements == a pval list of statements in the for ()
1416 if ((x=match_pval(item->u4.for_statements))) {
1422 /* fields: item->u1.str == the while conditional, as supplied by user
1424 item->u2.statements == a pval list of statements in the while ()
1426 if ((x=match_pval(item->u2.statements))) {
1432 /* fields: item->u1.str == the random number expression, as supplied by user
1434 item->u2.statements == a pval list of statements in the if ()
1435 item->u3.else_statements == a pval list of statements in the else
1437 fall thru to PV_IF */
1440 /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
1442 item->u2.statements == a pval list of statements in the if ()
1443 item->u3.else_statements == a pval list of statements in the else
1445 fall thru to PV_IF*/
1447 /* fields: item->u1.str == the if conditional, as supplied by user
1449 item->u2.statements == a pval list of statements in the if ()
1450 item->u3.else_statements == a pval list of statements in the else
1453 if ((x=match_pval(item->u2.statements))) {
1456 if (item->u3.else_statements) {
1457 if ((x=match_pval(item->u3.else_statements))) {
1464 /* fields: item->u1.str == the switch expression
1466 item->u2.statements == a pval list of statements in the switch,
1467 (will be case statements, most likely!)
1469 if ((x=match_pval(item->u3.else_statements))) {
1475 /* fields: item->u1.str == the extension name, label, whatever it's called
1477 item->u2.statements == a pval list of statements in the extension
1478 item->u3.hints == a char * hint argument
1479 item->u4.regexten == an int boolean. non-zero says that regexten was specified
1481 if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
1482 /* printf("Descending into matching exten %s\n", match_exten); */
1483 if (strcmp(match_label,"1") == 0) {
1484 if (item->u2.statements) {
1485 struct pval *p5 = item->u2.statements;
1486 while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
1497 if ((x=match_pval(item->u2.statements))) {
1501 /* printf("Skipping exten %s\n", item->u1.str); */
1510 struct pval *match_pval(pval *item)
1514 for (i=item; i; i=i->next) {
1517 if ((x = match_pval_item(i)))
1518 return x; /* cut the search short */
1524 int count_labels_in_current_context(char *label)
1528 return_on_context_match = 0;
1529 match_pval(current_context->u2.statements);
1535 struct pval *find_label_in_current_context(char *exten, char *label)
1537 /* printf(" --- Got args %s, %s\n", exten, label); */
1542 return_on_context_match = 0;
1543 match_context = "*";
1544 match_exten = exten;
1545 match_label = label;
1546 ret = match_pval(current_context->u2.statements);
1550 /* the target of the goto could be in an included context!! Fancy that!! */
1551 /* look for includes in the current context */
1552 for (p3=current_context->u2.statements; p3; p3=p3->next) {
1553 if (p3->type == PV_INCLUDES) {
1555 for (p4=p3->u1.list; p4; p4=p4->next) {
1556 /* for each context pointed to, find it, then find a context/label that matches the
1558 char *incl_context = p4->u1.str;
1559 /* find a matching context name */
1560 struct pval *that_context = find_context(incl_context);
1562 struct pval *context_save = current_context;
1564 current_context = that_context;
1565 x3 = find_label_in_current_context(exten, label);
1566 current_context = context_save;
1577 static struct pval *find_label_in_current_extension(const char *label)
1579 /* printf(" --- Got args %s\n", label); */
1581 return_on_context_match = 0;
1582 match_context = "*";
1584 match_label = label;
1585 if (! current_extension) /* macros have no current extension, the whole thing is one extension... */
1586 return match_pval(current_context->u3.macro_statements);
1587 return match_pval(current_extension->u2.statements);
1590 static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
1592 /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
1594 return_on_context_match = 0;
1596 match_context = context;
1597 match_exten = exten;
1598 match_label = label;
1600 return match_pval(current_db);
1604 struct pval *find_macro(char *name)
1606 return_on_context_match = 1;
1608 match_context = name;
1609 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1611 return match_pval(current_db);
1614 struct pval *find_context(char *name)
1616 return_on_context_match = 1;
1618 match_context = name;
1619 match_exten = "*"; /* don't really need to set these, shouldn't be reached */
1621 return match_pval(current_db);
1624 int is_float(char *arg )
1627 for (s=arg; *s; s++) {
1628 if (*s != '.' && (*s < '0' || *s > '9'))
1633 int is_int(char *arg )
1636 for (s=arg; *s; s++) {
1637 if (*s < '0' || *s > '9')
1642 int is_empty(char *arg)
1649 if (*arg != ' ' && *arg != '\t')
1657 int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
1659 struct argchoice *ac;
1660 char opcop[400],*q,*p;
1662 switch (should->dtype) {
1663 case ARGD_OPTIONSET:
1664 if ( strstr(is->u1.str,"${") )
1665 return 0; /* no checking anything if there's a var reference in there! */
1667 strncpy(opcop,is->u1.str,sizeof(opcop));
1669 for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
1672 while (*p && *p != ')' )
1678 for (ac=app->opts; ac; ac=ac->next) {
1679 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
1682 for (ac=app->opts; ac; ac=ac->next) {
1683 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
1684 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
1686 if (p && *p == 'j') {
1687 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
1688 is->filename, is->startline, is->endline, app->name);
1694 if (ac->name[1] == '(') {
1695 if (*(p+1) != '(') {
1696 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",
1697 is->filename, is->startline, is->endline, ac->name[0], app->name);
1704 for (q=opcop; *q; q++) {
1705 if ( *q != '+' && *q != '(' && *q != ')') {
1706 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",
1707 is->filename, is->startline, is->endline, *q, app->name);
1719 int option_matches( struct argdesc *should, pval *is, struct argapp *app)
1721 struct argchoice *ac;
1724 switch (should->dtype) {
1726 if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
1728 if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
1733 if (is_int(is->u1.str))
1740 if (is_float(is->u1.str))
1747 if( !is->u1.str || strlen(is->u1.str) == 0 )
1748 return 1; /* a null arg in the call will match an enum, I guess! */
1749 for (ac=should->choices; ac; ac=ac->next) {
1750 if (strcmp(ac->name,is->u1.str) == 0)
1756 case ARGD_OPTIONSET:
1757 strncpy(opcop,is->u1.str,sizeof(opcop));
1759 for (ac=app->opts; ac; ac=ac->next) {
1760 if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
1763 for (ac=app->opts; ac; ac=ac->next) {
1764 if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
1765 char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
1769 if (ac->name[1] == '(') {
1770 if (*(p+1) == '(') {
1772 while (*q && *q != ')') {
1784 return 1; /* matches anything */
1787 return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
1791 int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
1794 struct argdesc *ad = app->args;
1798 for (pa = arglist; pa; pa=pa->next) {
1800 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
1801 arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
1805 /* find the first entry in the ad list that will match */
1807 if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
1810 z= option_matches( ad, pa, app);
1815 if (ad->type == ARGD_REQUIRED) {
1816 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
1817 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
1821 } else if (z && ad->dtype == ARGD_OPTIONSET) {
1822 option_matches_j( ad, pa, app);
1828 /* any app nodes left, that are not optional? */
1829 for ( ; ad; ad=ad->next) {
1830 if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
1833 ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
1834 arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
1845 void check_switch_expr(pval *item, struct argapp *apps)
1848 /* get and clean the variable name */
1849 char buff1[1024],*p;
1850 struct argapp *a,*a2;
1851 struct appsetvar *v,*v2;
1852 struct argchoice *c;
1856 while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
1859 strncpy(buff1,p,sizeof(buff1));
1860 while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
1861 buff1[strlen(buff1)-1] = 0;
1862 /* buff1 now contains the variable name */
1864 for (a=apps; a; a=a->next) {
1865 for (v=a->setvars;v;v=v->next) {
1866 if (strcmp(v->name,buff1) == 0) {
1874 /* we have a match, to a variable that has a set of determined values */
1879 /* first of all, does this switch have a default case ? */
1880 for (t=item->u2.statements; t; t=t->next) {
1881 if (t->type == PV_DEFAULT) {
1885 if (t->type == PV_PATTERN) {
1889 if (def || pat) /* nothing to check. All cases accounted for! */
1891 for (c=v->vals; c; c=c->next) {
1893 for (t=item->u2.statements; t; t=t->next) {
1894 if (t->type == PV_CASE || t->type == PV_PATTERN) {
1895 if (!strcmp(t->u1.str,c->name)) {
1902 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
1903 item->filename, item->startline, item->endline, item->u1.str, c->name);
1907 /* next, is there an app call in the current exten, that would set this var? */
1909 t = current_extension->u2.statements;
1910 if ( t && t->type == PV_STATEMENTBLOCK )
1911 t = t->u1.statements;
1912 for (; t && t != item; t=t->next) {
1913 if (t->type == PV_APPLICATION_CALL) {
1914 /* find the application that matches the u1.str */
1915 for (a2=apps; a2; a2=a2->next) {
1916 if (strcasecmp(a2->name, t->u1.str)==0) {
1917 for (v2=a2->setvars; v2; v2=v2->next) {
1918 if (strcmp(v2->name, buff1) == 0) {
1919 /* found an app that sets the var */
1933 /* see if it sets the var */
1935 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",
1936 item->filename, item->startline, item->endline, item->u1.str);
1943 static void check_context_names(void)
1946 for (i=current_db; i; i=i->next) {
1947 if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
1948 for (j=i->next; j; j=j->next) {
1949 if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
1950 if ( !strcmp(i->u1.str, j->u1.str) )
1952 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d!\n",
1953 i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
1962 static void check_abstract_reference(pval *abstract_context)
1965 /* find some context includes that reference this context */
1968 /* otherwise, print out a warning */
1969 for (i=current_db; i; i=i->next) {
1970 if (i->type == PV_CONTEXT) {
1971 for (j=i->u2. statements; j; j=j->next) {
1972 if ( j->type == PV_INCLUDES ) {
1974 for (p4=j->u1.list; p4; p4=p4->next) {
1975 /* for each context pointed to, find it, then find a context/label that matches the
1977 if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
1978 return; /* found a match! */
1984 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",
1985 abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
1990 void check_pval_item(pval *item, struct argapp *apps)
1994 struct argapp *app, *found;
1996 struct pval *macro_def;
1997 struct pval *app_def;
2002 switch (item->type) {
2004 /* fields: item->u1.str == string associated with this (word).
2005 item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
2009 /* fields: item->u1.str == name of macro
2010 item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
2011 item->u2.arglist->u1.str == argument
2012 item->u2.arglist->next == next arg
2014 item->u3.macro_statements == pval list of statements in macro body.
2016 in_abstract_context = 0;
2017 current_context = item;
2018 current_extension = 0;
2019 for (lp=item->u2.arglist; lp; lp=lp->next) {
2022 check_pval(item->u3.macro_statements, apps);
2026 /* fields: item->u1.str == name of context
2027 item->u2.statements == pval list of statements in context body
2028 item->u3.abstract == int 1 if an abstract keyword were present
2030 current_context = item;
2031 current_extension = 0;
2032 if ( item->u3.abstract ) {
2033 in_abstract_context = 1;
2034 check_abstract_reference(item);
2036 in_abstract_context = 0;
2037 check_pval(item->u2.statements, apps);
2041 /* fields: item->u1.str == name of macro to call
2042 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2043 item->u2.arglist->u1.str == argument
2044 item->u2.arglist->next == next arg
2046 macro_def = find_macro(item->u1.str);
2048 ast_log(LOG_ERROR, "Error: file %s, line %d-%d: macro call to non-existent %s !\n",
2049 item->filename, item->startline, item->endline, item->u1.str);
2051 } else if (macro_def->type != PV_MACRO) {
2052 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
2053 item->filename, item->startline, item->endline, item->u1.str);
2056 /* macro_def is a MACRO, so do the args match in number? */
2060 for (lp=item->u2.arglist; lp; lp=lp->next) {
2063 for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
2066 if (hereargs != thereargs ) {
2067 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",
2068 item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
2074 case PV_APPLICATION_CALL:
2075 /* fields: item->u1.str == name of application to call
2076 item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
2077 item->u2.arglist->u1.str == argument
2078 item->u2.arglist->next == next arg
2080 /* Need to check to see if the application is available! */
2081 app_def = find_context(item->u1.str);
2082 if (app_def && app_def->type == PV_MACRO) {
2083 ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
2084 item->filename, item->startline, item->endline, item->u1.str);
2087 if (strcasecmp(item->u1.str,"GotoIf") == 0
2088 || strcasecmp(item->u1.str,"GotoIfTime") == 0
2089 || strcasecmp(item->u1.str,"while") == 0
2090 || strcasecmp(item->u1.str,"endwhile") == 0
2091 || strcasecmp(item->u1.str,"random") == 0
2092 || strcasecmp(item->u1.str,"execIf") == 0 ) {
2093 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s needs to be re-written using AEL if, while, goto, etc. keywords instead!\n",
2094 item->filename, item->startline, item->endline, item->u1.str);
2099 for (app=apps; app; app=app->next) {
2100 if (strcasecmp(app->name, item->u1.str) == 0) {
2106 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
2107 item->filename, item->startline, item->endline, item->u1.str);
2110 check_app_args(item, item->u2.arglist, app);
2115 /* fields: item->u1.str == value of case
2116 item->u2.statements == pval list of statements under the case
2118 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2119 /* find the last statement */
2120 check_pval(item->u2.statements, apps);
2124 /* fields: item->u1.str == value of case
2125 item->u2.statements == pval list of statements under the case
2127 /* Make sure sequence of statements under case is terminated with goto, return, or break */
2128 /* find the last statement */
2130 check_pval(item->u2.statements, apps);
2135 item->u2.statements == pval list of statements under the case
2138 check_pval(item->u2.statements, apps);
2142 /* fields: item->u1.str == name of extension to catch
2143 item->u2.statements == pval list of statements in context body
2145 check_pval(item->u2.statements, apps);
2149 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2151 check_pval(item->u1.list, apps);
2155 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2157 check_pval(item->u1.list, apps);
2161 /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
2163 check_pval(item->u1.list, apps);
2164 for (lp=item->u1.list; lp; lp=lp->next){
2165 char *incl_context = lp->u1.str;
2166 struct pval *that_context = find_context(incl_context);
2168 if ( lp->u2.arglist ) {
2169 check_timerange(lp->u2.arglist);
2170 check_dow(lp->u2.arglist->next);
2171 check_day(lp->u2.arglist->next->next);
2172 check_month(lp->u2.arglist->next->next->next);
2176 find_pval_gotos(that_context->u2.statements,0);
2182 case PV_STATEMENTBLOCK:
2183 /* fields: item->u1.list == pval list of statements in block, one per entry in the list
2185 check_pval(item->u1.list, apps);
2189 /* fields: item->u1.str == variable name
2190 item->u2.val == variable value to assign
2192 /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
2193 snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.val);
2194 ast_expr_register_extra_error_info(errmsg);
2195 ast_expr(item->u2.val, expr_output, sizeof(expr_output));
2196 ast_expr_clear_extra_error_info();
2197 if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
2198 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2199 item->filename, item->startline, item->endline, item->u2.val);
2202 check_expr2_input(item,item->u2.val);
2206 /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
2207 item->u1.list->u1.str == where the data on a PV_WORD will always be.
2209 /* don't check goto's in abstract contexts */
2210 if ( in_abstract_context )
2217 /* fields: item->u1.str == label name
2219 if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
2220 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
2221 item->filename, item->startline, item->endline, item->u1.str);
2227 /* fields: item->u1.for_init == a string containing the initalizer
2228 item->u2.for_test == a string containing the loop test
2229 item->u3.for_inc == a string containing the loop increment
2231 item->u4.for_statements == a pval list of statements in the for ()
2233 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, for test expr '%s':", config, item->startline, item->startcol, item->endcol, item->u2.for_test);
2234 ast_expr_register_extra_error_info(errmsg);
2236 strp = strchr(item->u1.for_init, '=');
2238 ast_expr(strp+1, expr_output, sizeof(expr_output));
2240 ast_expr(item->u2.for_test, expr_output, sizeof(expr_output));
2241 strp = strchr(item->u3.for_inc, '=');
2243 ast_expr(strp+1, expr_output, sizeof(expr_output));
2245 if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
2246 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2247 item->filename, item->startline, item->endline, item->u2.for_test);
2250 if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
2251 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2252 item->filename, item->startline, item->endline, item->u3.for_inc);
2255 check_expr2_input(item,item->u2.for_test);
2256 check_expr2_input(item,item->u3.for_inc);
2258 ast_expr_clear_extra_error_info();
2259 check_pval(item->u4.for_statements, apps);
2263 /* fields: item->u1.str == the while conditional, as supplied by user
2265 item->u2.statements == a pval list of statements in the while ()
2267 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2268 ast_expr_register_extra_error_info(errmsg);
2269 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2270 ast_expr_clear_extra_error_info();
2271 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2272 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
2273 item->filename, item->startline, item->endline, item->u1.str);
2276 check_expr2_input(item,item->u1.str);
2277 check_pval(item->u2.statements, apps);
2296 /* fields: item->u1.str == the random number expression, as supplied by user
2298 item->u2.statements == a pval list of statements in the if ()
2299 item->u3.else_statements == a pval list of statements in the else
2302 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2303 ast_expr_register_extra_error_info(errmsg);
2304 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2305 ast_expr_clear_extra_error_info();
2306 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2307 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
2308 item->filename, item->startline, item->endline, item->u1.str);
2311 check_expr2_input(item,item->u1.str);
2312 check_pval(item->u2.statements, apps);
2313 if (item->u3.else_statements) {
2314 check_pval(item->u3.else_statements, apps);
2319 /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
2321 item->u2.statements == a pval list of statements in the if ()
2322 item->u3.else_statements == a pval list of statements in the else
2325 if ( item->u2.arglist ) {
2326 check_timerange(item->u1.list);
2327 check_dow(item->u1.list->next);
2328 check_day(item->u1.list->next->next);
2329 check_month(item->u1.list->next->next->next);
2332 check_pval(item->u2.statements, apps);
2333 if (item->u3.else_statements) {
2334 check_pval(item->u3.else_statements, apps);
2339 /* fields: item->u1.str == the if conditional, as supplied by user
2341 item->u2.statements == a pval list of statements in the if ()
2342 item->u3.else_statements == a pval list of statements in the else
2345 snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", config, item->startline, item->startcol, item->endcol, item->u1.str);
2346 ast_expr_register_extra_error_info(errmsg);
2347 ast_expr(item->u1.str, expr_output, sizeof(expr_output));
2348 ast_expr_clear_extra_error_info();
2349 if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
2350 ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
2351 item->filename, item->startline, item->endline, item->u1.str);
2354 check_expr2_input(item,item->u1.str);
2355 check_pval(item->u2.statements, apps);
2356 if (item->u3.else_statements) {
2357 check_pval(item->u3.else_statements, apps);
2362 /* fields: item->u1.str == the switch expression
2364 item->u2.statements == a pval list of statements in the switch,
2365 (will be case statements, most likely!)
2367 /* we can check the switch expression, see if it matches any of the app variables...
2368 if it does, then, are all the possible cases accounted for? */
2369 check_switch_expr(item, apps);
2370 check_pval(item->u2.statements, apps);
2374 /* fields: item->u1.str == the extension name, label, whatever it's called
2376 item->u2.statements == a pval list of statements in the extension
2377 item->u3.hints == a char * hint argument
2378 item->u4.regexten == an int boolean. non-zero says that regexten was specified
2380 current_extension = item ;
2382 check_pval(item->u2.statements, apps);
2386 /* fields: item->u1.str == the ignorepat data
2391 /* fields: item->u1.statements == pval list of statements, usually vardecs
2393 in_abstract_context = 0;
2394 check_pval(item->u1.statements, apps);
2401 void check_pval(pval *item, struct argapp *apps)
2406 1. Do goto's point to actual labels?
2407 2. Do macro calls reference a macro?
2408 3. Does the number of macro args match the definition?
2409 4. Is a macro call missing its & at the front?
2410 5. Application calls-- we could check syntax for existing applications,
2411 but I need some some sort of universal description bnf for a general
2412 sort of method for checking arguments, in number, maybe even type, at least.
2413 Don't want to hand code checks for hundreds of applications.
2416 for (i=item; i; i=i->next) {
2417 check_pval_item(i,apps);
2421 static void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
2428 struct argapp *apps=0;
2431 rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
2432 sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
2434 apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
2437 errs = warns = notes = 0;
2439 check_context_names();
2440 check_pval(item, apps);
2443 argdesc_destroy(apps); /* taketh away */
2452 /* =============================================================================================== */
2453 /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
2454 /* =============================================================================================== */
2456 static int control_statement_count = 0;
2458 struct ael_priority *new_prio(void)
2460 struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
2464 struct ael_extension *new_exten(void)
2466 struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
2470 void linkprio(struct ael_extension *exten, struct ael_priority *prio)
2472 if (!exten->plist) {
2473 exten->plist = prio;
2474 exten->plist_last = prio;
2476 exten->plist_last->next = prio;
2477 exten->plist_last = prio;
2481 void destroy_extensions(struct ael_extension *exten)
2483 struct ael_extension *ne, *nen;
2484 for (ne=exten; ne; ne=nen) {
2485 struct ael_priority *pe, *pen;
2493 for (pe=ne->plist; pe; pe=pen) {
2506 nen = ne->next_exten;
2512 ne->loop_continue = 0;
2517 void linkexten(struct ael_extension *exten, struct ael_extension *add)
2519 add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
2520 exten->next_exten = add;
2523 void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten )
2526 struct ael_priority *pr;
2527 struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
2528 struct ael_priority *while_test, *while_loop, *while_end;
2529 struct ael_priority *switch_test, *switch_end, *fall_thru;
2530 struct ael_priority *if_test, *if_end, *if_skip, *if_false;
2531 #ifdef OLD_RAND_ACTION
2532 struct ael_priority *rand_test, *rand_end, *rand_skip;
2537 char new_label[2000];
2539 struct ael_priority *loop_break_save;
2540 struct ael_priority *loop_continue_save;
2541 struct ael_extension *switch_case;
2543 for (p=statement; p; p=p->next) {
2547 pr->type = AEL_APPCALL;
2548 snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
2549 pr->app = strdup("Set");
2550 pr->appargs = strdup(buf1);
2552 linkprio(exten, pr);
2557 pr->type = AEL_APPCALL;
2558 if (!p->u1.list->next) /* just one */ {
2559 pr->app = strdup("Goto");
2561 pr->appargs = strdup(p->u1.list->u1.str);
2562 else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
2563 snprintf(buf1,sizeof(buf1),"%s|%s", mother_exten->name, p->u1.list->u1.str);
2564 pr->appargs = strdup(buf1);
2567 } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
2568 snprintf(buf1,sizeof(buf1),"%s|%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
2569 pr->app = strdup("Goto");
2570 pr->appargs = strdup(buf1);
2571 } else if (p->u1.list->next && p->u1.list->next->next) {
2572 snprintf(buf1,sizeof(buf1),"%s|%s|%s", p->u1.list->u1.str,
2573 p->u1.list->next->u1.str,
2574 p->u1.list->next->next->u1.str);
2575 pr->app = strdup("Goto");
2576 pr->appargs = strdup(buf1);
2579 linkprio(exten, pr);
2584 pr->type = AEL_LABEL;
2586 linkprio(exten, pr);
2590 control_statement_count++;
2591 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
2592 loop_continue_save = exten->loop_continue;
2593 snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
2594 for_init = new_prio();
2595 for_inc = new_prio();
2596 for_test = new_prio();
2597 for_loop = new_prio();
2598 for_end = new_prio();
2599 for_init->type = AEL_APPCALL;
2600 for_inc->type = AEL_APPCALL;
2601 for_test->type = AEL_FOR_CONTROL;
2602 for_test->goto_false = for_end;
2603 for_loop->type = AEL_CONTROL1; /* simple goto */
2604 for_end->type = AEL_APPCALL;
2605 for_init->app = strdup("Set");
2607 strcpy(buf2,p->u1.for_init);
2608 strp = strchr(buf2, '=');
2609 strp2 = strchr(p->u1.for_init, '=');
2613 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
2615 for_init->appargs = strdup(buf2);
2617 for_init->appargs = strdup(p->u1.for_init);
2619 for_inc->app = strdup("Set");
2621 strcpy(buf2,p->u3.for_inc);
2622 strp = strchr(buf2, '=');
2623 strp2 = strchr(p->u3.for_inc, '=');
2627 strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
2629 for_inc->appargs = strdup(buf2);
2631 for_inc->appargs = strdup(p->u3.for_inc);
2632 snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
2634 for_test->appargs = strdup(buf1);
2635 for_loop->goto_true = for_test;
2636 snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
2637 for_end->app = strdup("NoOp");
2638 for_end->appargs = strdup(buf1);
2640 linkprio(exten, for_init);
2641 linkprio(exten, for_test);
2643 /* now, put the body of the for loop here */
2644 exten->loop_break = for_end;
2645 exten->loop_continue = for_test;
2647 gen_prios(exten, new_label, p->u4.for_statements, mother_exten); /* this will link in all the statements here */
2649 linkprio(exten, for_inc);
2650 linkprio(exten, for_loop);
2651 linkprio(exten, for_end);
2654 exten->loop_break = loop_break_save;
2655 exten->loop_continue = loop_continue_save;
2656 for_loop->origin = p;
2660 control_statement_count++;
2661 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
2662 loop_continue_save = exten->loop_continue;
2663 snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
2664 while_test = new_prio();
2665 while_loop = new_prio();
2666 while_end = new_prio();
2667 while_test->type = AEL_FOR_CONTROL;
2668 while_test->goto_false = while_end;
2669 while_loop->type = AEL_CONTROL1; /* simple goto */
2670 while_end->type = AEL_APPCALL;
2671 snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
2672 while_test->app = 0;
2673 while_test->appargs = strdup(buf1);
2674 while_loop->goto_true = while_test;
2675 snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
2676 while_end->app = strdup("NoOp");
2677 while_end->appargs = strdup(buf1);
2679 linkprio(exten, while_test);
2681 /* now, put the body of the for loop here */
2682 exten->loop_break = while_end;
2683 exten->loop_continue = while_test;
2685 gen_prios(exten, new_label, p->u2.statements, mother_exten); /* this will link in all the while body statements here */
2687 linkprio(exten, while_loop);
2688 linkprio(exten, while_end);
2691 exten->loop_break = loop_break_save;
2692 exten->loop_continue = loop_continue_save;
2693 while_loop->origin = p;
2697 control_statement_count++;
2698 loop_break_save = exten->loop_break; /* save them, then restore before leaving */
2699 loop_continue_save = exten->loop_continue;
2700 snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
2702 switch_test = new_prio();
2703 switch_end = new_prio();
2704 switch_test->type = AEL_APPCALL;
2705 switch_end->type = AEL_APPCALL;
2706 snprintf(buf1,sizeof(buf1),"sw-%d-%s|1",control_statement_count, p->u1.str);
2707 switch_test->app = strdup("Goto");
2708 switch_test->appargs = strdup(buf1);
2709 snprintf(buf1,sizeof(buf1),"Finish switch-%s-%d", label, control_statement_count);
2710 switch_end->app = strdup("NoOp");
2711 switch_end->appargs = strdup(buf1);
2712 switch_end->origin = p;
2713 switch_end->exten = exten;
2715 linkprio(exten, switch_test);
2716 linkprio(exten, switch_end);
2718 exten->loop_break = switch_end;
2719 exten->loop_continue = 0;
2722 for (p2=p->u2.statements; p2; p2=p2->next) {
2723 /* now, for each case/default put the body of the for loop here */
2724 if (p2->type == PV_CASE) {
2725 /* ok, generate a extension and link it in */
2726 switch_case = new_exten();
2727 /* the break/continue locations are inherited from parent */
2728 switch_case->loop_break = exten->loop_break;
2729 switch_case->loop_continue = exten->loop_continue;
2731 linkexten(exten,switch_case);
2732 snprintf(buf1,sizeof(buf1),"sw-%d-%s", control_statement_count, p2->u1.str);
2733 switch_case->name = strdup(buf1);
2734 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, p2->u1.str, control_statement_count);
2736 gen_prios(switch_case, new_label, p2->u2.statements, exten); /* this will link in all the case body statements here */
2738 /* here is where we write code to "fall thru" to the next case... if there is one... */
2739 for (p3=p2->u2.statements; p3; p3=p3->next) {
2743 /* p3 now points the last statement... */
2744 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN) ) {
2745 /* is there a following CASE/PATTERN/DEFAULT? */
2746 if (p2->next && p2->next->type == PV_CASE) {
2747 fall_thru = new_prio();
2748 fall_thru->type = AEL_APPCALL;
2749 fall_thru->app = strdup("Goto");
2750 snprintf(buf1,sizeof(buf1),"sw-%d-%s|1",control_statement_count, p2->next->u1.str);
2751 fall_thru->appargs = strdup(buf1);
2752 linkprio(switch_case, fall_thru);
2753 } else if (p2->next && p2->next->type == PV_PATTERN) {
2754 fall_thru = new_prio();
2755 fall_thru->type = AEL_APPCALL;
2756 fall_thru->app = strdup("Goto");
2757 snprintf(buf1,sizeof(buf1),"_sw-%d-%s|1",control_statement_count, p2->next->u1.str);
2758 fall_thru->appargs = strdup(buf1);
2759 linkprio(switch_case, fall_thru);
2760 } else if (p2->next && p2->next->type == PV_DEFAULT) {
2761 fall_thru = new_prio();
2762 fall_thru->type = AEL_APPCALL;
2763 fall_thru->app = strdup("Goto");
2764 snprintf(buf1,sizeof(buf1),"_sw-%d-.|1",control_statement_count);
2765 fall_thru->appargs = strdup(buf1);
2766 linkprio(switch_case, fall_thru);
2767 } else if (!p2->next) {
2768 fall_thru = new_prio();
2769 fall_thru->type = AEL_CONTROL1;
2770 fall_thru->goto_true = switch_end;
2771 fall_thru->app = strdup("Goto");
2772 linkprio(switch_case, fall_thru);
2775 if (switch_case->return_needed) {
2777 struct ael_priority *np2 = new_prio();
2778 np2->type = AEL_APPCALL;
2779 np2->app = strdup("NoOp");
2780 snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
2781 np2->appargs = strdup(buf);
2782 linkprio(switch_case, np2);
2783 switch_case-> return_target = np2;
2785 } else if (p2->type == PV_PATTERN) {
2786 /* ok, generate a extension and link it in */
2787 switch_case = new_exten();
2788 /* the break/continue locations are inherited from parent */
2789 switch_case->loop_break = exten->loop_break;
2790 switch_case->loop_continue = exten->loop_continue;
2792 linkexten(exten,switch_case);
2793 snprintf(buf1,sizeof(buf1),"_sw-%d-%s", control_statement_count, p2->u1.str);
2794 switch_case->name = strdup(buf1);
2795 snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, p2->u1.str, control_statement_count);
2797 gen_prios(switch_case, new_label, p2->u2.statements, exten); /* this will link in all the while body statements here */
2798 /* here is where we write code to "fall thru" to the next case... if there is one... */
2799 for (p3=p2->u2.statements; p3; p3=p3->next) {
2803 /* p3 now points the last statement... */
2804 if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
2805 /* is there a following CASE/PATTERN/DEFAULT? */
2806 if (p2->next && p2->next->type == PV_CASE) {
2807 fall_thru = new_prio();
2808 fall_thru->type = AEL_APPCALL;
2809 fall_thru->app = strdup("Goto");
2810 snprintf(buf1,sizeof(buf1),"sw-%d-%s|1",control_statement_count, p2->next->u1.str);
2811 fall_thru->appargs = strdup(buf1);
2812 linkprio(switch_case, fall_thru);
2813 } else if (p2->next && p2->next->type == PV_PATTERN) {
2814 fall_thru = new_prio();
2815 fall_thru->type = AEL_APPCALL;