security_events: Fix error caused by DTD validation error
[asterisk/asterisk.git] / res / ael / pval.c
index c1d20f6..70bd689 100644 (file)
  * 
  */
 
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
+
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
@@ -52,10 +56,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #endif
 #include "asterisk/utils.h"
 
+extern struct ast_flags ast_compat;
 extern int localized_pbx_load_module(void);
 
 static char expr_output[2096];
 #define AST_PBX_MAX_STACK  128
+#define BUF_SIZE 2000
 
 /* these functions are in ../ast_expr2.fl */
 
@@ -694,7 +700,7 @@ static int extension_matches(pval *here, const char *exten, const char *pattern)
        regex_t preg;
        
        /* simple case, they match exactly, the pattern and exten name */
-       if (!strcmp(pattern,exten) == 0)
+       if (strcmp(pattern,exten) == 0)
                return 1;
        
        if (pattern[0] == '_') {
@@ -744,6 +750,7 @@ static int extension_matches(pval *here, const char *exten, const char *pattern)
                                while ( *p && *p != ']' ) {
                                        *r++ = *p++;
                                }
+                               *r++ = ']';
                                if ( *p != ']') {
                                        ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
                                                        here->filename, here->startline, here->endline, pattern);
@@ -858,12 +865,12 @@ static void check_timerange(pval *p)
                                p->filename, p->startline, p->endline, p->u1.str);
                warns++;
        }
-       if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
+       if (sscanf(times, "%2d:%2d", &s1, &s2) != 2) {
                ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
                                p->filename, p->startline, p->endline, times);
                warns++;
        }
-       if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
+       if (sscanf(e, "%2d:%2d", &e1, &e2) != 2) {
                ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
                                p->filename, p->startline, p->endline, times);
                warns++;
@@ -955,7 +962,7 @@ static void check_day(pval *DAY)
                c++;
        }
        /* Find the start */
-       if (sscanf(day, "%d", &s) != 1) {
+       if (sscanf(day, "%2d", &s) != 1) {
                ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
                                DAY->filename, DAY->startline, DAY->endline, day);
                warns++;
@@ -967,7 +974,7 @@ static void check_day(pval *DAY)
        }
        s--;
        if (c) {
-               if (sscanf(c, "%d", &e) != 1) {
+               if (sscanf(c, "%2d", &e) != 1) {
                        ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
                                        DAY->filename, DAY->startline, DAY->endline, c);
                        warns++;
@@ -1157,7 +1164,11 @@ static pval *get_goto_target(pval *item)
        pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
        pval *curr_cont;
        
-       if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
+       if (!item->u1.list) {
+               return NULL;
+       }
+
+       if (!item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
                struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
                        return x;
        }
@@ -1216,21 +1227,24 @@ static pval *get_goto_target(pval *item)
                        return x;
                }
        }
-       return 0;
+       return NULL;
 }
 
 static void check_goto(pval *item)
 {
+       if (!item->u1.list) {
+               return;
+       }
+
        /* check for the target of the goto-- does it exist? */
        if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
                ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  empty label reference found!\n",
                                item->filename, item->startline, item->endline);
                errs++;
        }
-       
+
        /* just one item-- the label should be in the current extension */
-       
-       if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
+       if (!item->u1.list->next && !strstr(item->u1.list->u1.str,"${")) {
                struct pval *z = get_extension_or_contxt(item);
                struct pval *x = 0;
                if (z)
@@ -1373,8 +1387,9 @@ static void check_goto(pval *item)
 static void find_pval_goto_item(pval *item, int lev)
 {
        struct pval *p4;
+       
        if (lev>100) {
-               ast_log(LOG_ERROR,"find_pval_goto in infinite loop!\n\n");
+               ast_log(LOG_ERROR,"find_pval_goto in infinite loop! item_type: %d\n\n", item->type);
                return;
        }
        
@@ -1388,7 +1403,7 @@ static void find_pval_goto_item(pval *item, int lev)
                                   item->u3.macro_statements == pval list of statements in macro body.
                */
                        
-               /* printf("Descending into matching macro %s\n", match_context); */
+               /* printf("Descending into macro %s at line %d\n", item->u1.str, item->startline); */
                find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
                
                break;
@@ -1404,6 +1419,7 @@ static void find_pval_goto_item(pval *item, int lev)
                /* fields: item->u1.str     == value of case
                           item->u2.statements == pval list of statements under the case
                */
+               /* printf("Descending into Case of %s\n", item->u1.str); */
                find_pval_gotos(item->u2.statements,lev+1);
                break;
                        
@@ -1411,6 +1427,7 @@ static void find_pval_goto_item(pval *item, int lev)
                /* fields: item->u1.str     == value of case
                           item->u2.statements == pval list of statements under the case
                */
+               /* printf("Descending into Pattern of %s\n", item->u1.str); */
                find_pval_gotos(item->u2.statements,lev+1);
                break;
                        
@@ -1418,6 +1435,7 @@ static void find_pval_goto_item(pval *item, int lev)
                /* fields: 
                           item->u2.statements == pval list of statements under the case
                */
+               /* printf("Descending into default\n"); */
                find_pval_gotos(item->u2.statements,lev+1);
                break;
                        
@@ -1425,12 +1443,14 @@ static void find_pval_goto_item(pval *item, int lev)
                /* fields: item->u1.str     == name of extension to catch
                           item->u2.statements == pval list of statements in context body
                */
+               /* printf("Descending into catch of %s\n", item->u1.str); */
                find_pval_gotos(item->u2.statements,lev+1);
                break;
                        
        case PV_STATEMENTBLOCK:
                /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
                */
+               /* printf("Descending into statement block\n"); */
                find_pval_gotos(item->u1.list,lev+1);
                break;
                        
@@ -1450,8 +1470,9 @@ static void find_pval_goto_item(pval *item, int lev)
                        char *incl_context = p4->u1.str;
                        /* find a matching context name */
                        struct pval *that_context = find_context(incl_context);
-                       if (that_context) {
-                               find_pval_gotos(that_context,lev+1); /* keep working up the includes */
+                       if (that_context && that_context->u2.statements) {
+                               /* printf("Descending into include of '%s' at line %d; that_context=%s, that_context type=%d\n", incl_context, item->startline, that_context->u1.str, that_context->type); */
+                               find_pval_gotos(that_context->u2.statements,lev+1); /* keep working up the includes */
                        }
                }
                break;
@@ -1463,6 +1484,7 @@ static void find_pval_goto_item(pval *item, int lev)
 
                                   item->u4.for_statements == a pval list of statements in the for ()
                */
+               /* printf("Descending into for at line %d\n", item->startline); */
                find_pval_gotos(item->u4.for_statements,lev+1);
                break;
                        
@@ -1471,6 +1493,7 @@ static void find_pval_goto_item(pval *item, int lev)
 
                                   item->u2.statements == a pval list of statements in the while ()
                */
+               /* printf("Descending into while at line %d\n", item->startline); */
                find_pval_gotos(item->u2.statements,lev+1);
                break;
                        
@@ -1496,9 +1519,11 @@ static void find_pval_goto_item(pval *item, int lev)
                                   item->u3.else_statements == a pval list of statements in the else
                                                                                           (could be zero)
                */
+               /* printf("Descending into random/iftime/if at line %d\n", item->startline); */
                find_pval_gotos(item->u2.statements,lev+1);
 
                if (item->u3.else_statements) {
+                       /* printf("Descending into random/iftime/if's ELSE at line %d\n", item->startline); */
                        find_pval_gotos(item->u3.else_statements,lev+1);
                }
                break;
@@ -1509,6 +1534,7 @@ static void find_pval_goto_item(pval *item, int lev)
                                   item->u2.statements == a pval list of statements in the switch, 
                                                                                        (will be case statements, most likely!)
                */
+               /* printf("Descending into switch at line %d\n", item->startline); */
                find_pval_gotos(item->u3.else_statements,lev+1);
                break;
                        
@@ -1520,6 +1546,7 @@ static void find_pval_goto_item(pval *item, int lev)
                                   item->u4.regexten   == an int boolean. non-zero says that regexten was specified
                */
 
+               /* printf("Descending into extension %s at line %d\n", item->u1.str, item->startline); */
                find_pval_gotos(item->u2.statements,lev+1);
                break;
 
@@ -1531,9 +1558,9 @@ static void find_pval_goto_item(pval *item, int lev)
 static void find_pval_gotos(pval *item,int lev)
 {
        pval *i;
-
+       
        for (i=item; i; i=i->next) {
-               
+               /* printf("About to call pval_goto_item, itemcount=%d, itemtype=%d\n", item_count, i->type); */
                find_pval_goto_item(i, lev);
        }
 }
@@ -2491,7 +2518,6 @@ void check_pval_item(pval *item, struct argapp *apps, int in_globals)
                        || strcasecmp(item->u1.str,"endwhile") == 0
                        || strcasecmp(item->u1.str,"random") == 0
                        || strcasecmp(item->u1.str,"gosub") == 0
-                       || strcasecmp(item->u1.str,"return") == 0
                        || strcasecmp(item->u1.str,"gosubif") == 0
                        || strcasecmp(item->u1.str,"continuewhile") == 0
                        || strcasecmp(item->u1.str,"endwhile") == 0
@@ -2876,7 +2902,7 @@ void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_not
        if (!item)
                return; /* don't check an empty tree */
 #ifdef AAL_ARGCHECK
-       rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
+       rfilename = ast_alloca(10 + strlen(ast_config_AST_VAR_DIR));
        sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
        
        apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
@@ -3063,7 +3089,264 @@ static void gen_match_to_pattern(char *pattern, char *result)
        *t++ = 0; /* cap it off */
 }
 
-static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
+/* ==== a set of routines to search for a switch statement contained in the pval description */
+
+int find_switch_item(pval *item);
+int contains_switch(pval *item);
+
+
+int find_switch_item(pval *item)
+{
+       switch ( item->type ) {
+       case PV_LOCALVARDEC:
+               /* fields: item->u1.str == string associated with this (word). */
+               break;
+               
+       case PV_WORD:
+               /* fields: item->u1.str == string associated with this (word). */
+               break;
+               
+       case PV_MACRO:
+               /* fields: item->u1.str     == name of macro
+                          item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
+                                  item->u2.arglist->u1.str  == argument
+                                  item->u2.arglist->next   == next arg
+
+                                  item->u3.macro_statements == pval list of statements in macro body.
+               */
+               /* had better not see this */
+               if (contains_switch(item->u3.macro_statements))
+                       return 1;
+               break;
+                       
+       case PV_CONTEXT:
+               /* fields: item->u1.str     == name of context
+                          item->u2.statements == pval list of statements in context body
+                                  item->u3.abstract == int 1 if an abstract keyword were present
+               */
+               /* had better not see this */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               break;
+                       
+       case PV_MACRO_CALL:
+               /* fields: item->u1.str     == name of macro to call
+                          item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
+                                  item->u2.arglist->u1.str  == argument
+                                  item->u2.arglist->next   == next arg
+               */
+               break;
+                       
+       case PV_APPLICATION_CALL:
+               /* fields: item->u1.str     == name of application to call
+                          item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
+                                  item->u2.arglist->u1.str  == argument
+                                  item->u2.arglist->next   == next arg
+               */
+               break;
+                       
+       case PV_CASE:
+               /* fields: item->u1.str     == value of case
+                          item->u2.statements == pval list of statements under the case
+               */
+               /* had better not see this */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               break;
+                       
+       case PV_PATTERN:
+               /* fields: item->u1.str     == value of case
+                          item->u2.statements == pval list of statements under the case
+               */
+               /* had better not see this */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               break;
+                       
+       case PV_DEFAULT:
+               /* fields: 
+                          item->u2.statements == pval list of statements under the case
+               */
+               /* had better not see this */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               break;
+                       
+       case PV_CATCH:
+               /* fields: item->u1.str     == name of extension to catch
+                          item->u2.statements == pval list of statements in context body
+               */
+               /* had better not see this */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               break;
+                       
+       case PV_SWITCHES:
+               /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
+               */
+               break;
+                       
+       case PV_ESWITCHES:
+               /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
+               */
+               break;
+                       
+       case PV_INCLUDES:
+               /* fields: item->u1.list     == pval list of PV_WORD elements, one per entry in the list
+                          item->u2.arglist  == pval list of 4 PV_WORD elements for time values
+               */
+               break;
+                       
+       case PV_STATEMENTBLOCK:
+               /* fields: item->u1.list     == pval list of statements in block, one per entry in the list
+               */
+               if (contains_switch(item->u1.list) )
+                       return 1;
+               break;
+                       
+       case PV_VARDEC:
+               /* fields: item->u1.str     == variable name
+                          item->u2.val     == variable value to assign
+               */
+               break;
+                       
+       case PV_GOTO:
+               /* fields: item->u1.list     == pval list of PV_WORD target names, up to 3, in order as given by user.
+                          item->u1.list->u1.str  == where the data on a PV_WORD will always be.
+               */
+               break;
+                       
+       case PV_LABEL:
+               /* fields: item->u1.str     == label name
+               */
+               break;
+                       
+       case PV_FOR:
+               /* fields: item->u1.for_init     == a string containing the initalizer
+                          item->u2.for_test     == a string containing the loop test
+                          item->u3.for_inc      == a string containing the loop increment
+
+                                  item->u4.for_statements == a pval list of statements in the for ()
+               */
+               if (contains_switch(item->u4.for_statements))
+                       return 1;
+               break;
+                       
+       case PV_WHILE:
+               /* fields: item->u1.str        == the while conditional, as supplied by user
+
+                                  item->u2.statements == a pval list of statements in the while ()
+               */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               break;
+                       
+       case PV_BREAK:
+               /* fields: none
+               */
+               break;
+                       
+       case PV_RETURN:
+               /* fields: none
+               */
+               break;
+                       
+       case PV_CONTINUE:
+               /* fields: none
+               */
+               break;
+                       
+       case PV_IFTIME:
+               /* fields: item->u1.list        == there are 4 linked PV_WORDs here.
+
+                                  item->u2.statements == a pval list of statements in the if ()
+                                  item->u3.else_statements == a pval list of statements in the else
+                                                                                          (could be zero)
+               */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               if ( item->u3.else_statements ) {
+                       if (contains_switch(item->u3.else_statements))
+                               return 1;
+               }
+               break;
+                       
+       case PV_RANDOM:
+               /* fields: item->u1.str        == the random number expression, as supplied by user
+
+                                  item->u2.statements == a pval list of statements in the if ()
+                                  item->u3.else_statements == a pval list of statements in the else
+                                                                                          (could be zero)
+               */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               if ( item->u3.else_statements ) {
+                       if (contains_switch(item->u3.else_statements))
+                               return 1;
+               }
+               break;
+                       
+       case PV_IF:
+               /* fields: item->u1.str        == the if conditional, as supplied by user
+
+                                  item->u2.statements == a pval list of statements in the if ()
+                                  item->u3.else_statements == a pval list of statements in the else
+                                                                                          (could be zero)
+               */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               if ( item->u3.else_statements ) {
+                       if (contains_switch(item->u3.else_statements))
+                               return 1;
+               }
+               break;
+                       
+       case PV_SWITCH:
+               /* fields: item->u1.str        == the switch expression
+
+                                  item->u2.statements == a pval list of statements in the switch, 
+                                                                                       (will be case statements, most likely!)
+               */
+               return 1; /* JACKPOT */
+               break;
+                       
+       case PV_EXTENSION:
+               /* fields: item->u1.str        == the extension name, label, whatever it's called
+
+                                  item->u2.statements == a pval list of statements in the extension
+                                  item->u3.hints      == a char * hint argument
+                                  item->u4.regexten   == an int boolean. non-zero says that regexten was specified
+               */
+               if (contains_switch(item->u2.statements))
+                       return 1;
+               break;
+                       
+       case PV_IGNOREPAT:
+               /* fields: item->u1.str        == the ignorepat data
+               */
+               break;
+                       
+       case PV_GLOBALS:
+               /* fields: item->u1.statements     == pval list of statements, usually vardecs
+               */
+               break;
+       }
+       return 0;
+}
+
+int contains_switch(pval *item)
+{
+       pval *i;
+       
+       for (i=item; i; i=i->next) {
+               if (find_switch_item(i))
+                       return 1;
+       }
+       return 0;
+}
+
+
+static int gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
 {
        pval *p,*p2,*p3;
        struct ael_priority *pr;
@@ -3074,24 +3357,108 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
 #ifdef OLD_RAND_ACTION
        struct ael_priority *rand_test, *rand_end, *rand_skip;
 #endif
-       char buf1[2000];
-       char buf2[2000];
+       char *buf1;
+       char *buf2;
+       char *new_label;
        char *strp, *strp2;
-       char new_label[2000];
        int default_exists;
        int local_control_statement_count;
        int first;
        struct ael_priority *loop_break_save;
        struct ael_priority *loop_continue_save;
        struct ael_extension *switch_case,*switch_null;
+
+       if (!(buf1 = malloc(BUF_SIZE))) {
+               return -1;
+       }
+       if (!(buf2 = malloc(BUF_SIZE))) {
+               return -1;
+       }
+       if (!(new_label = malloc(BUF_SIZE))) {
+               return -1;
+       }
        
+       if ((mother_exten && !mother_exten->checked_switch) || (exten && !exten->checked_switch)) {
+               if (contains_switch(statement)) { /* only run contains_switch if you haven't checked before */
+                       if (mother_exten) {
+                               if (!mother_exten->has_switch) {
+                                       for (first = 1; first >= 0; first--) {
+                                               switch_set = new_prio();
+                                               switch_set->type = AEL_APPCALL;
+                                               if (!ast_compat_app_set) {
+                                                       switch_set->app = strdup("MSet");
+                                               } else {
+                                                       switch_set->app = strdup("Set");
+                                               }
+                                               /* Are we likely inside a gosub subroutine? */
+                                               if (!strcmp(mother_exten->name, "~~s~~") && first) {
+                                                       /* If we're not actually within a gosub, this will fail, but the
+                                                        * second time through, it will get set.  If we are within gosub,
+                                                        * the second time through is redundant, but acceptable. */
+                                                       switch_set->appargs = strdup("LOCAL(~~EXTEN~~)=${EXTEN}");
+                                               } else {
+                                                       switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
+                                                       first = 0;
+                                               }
+                                               linkprio(exten, switch_set, mother_exten);
+                                               mother_exten->has_switch = 1;
+                                               mother_exten->checked_switch = 1;
+                                               if (exten) {
+                                                       exten->has_switch = 1;
+                                                       exten->checked_switch = 1;
+                                               }
+                                       }
+                               }
+                       } else if (exten) {
+                               if (!exten->has_switch) {
+                                       for (first = 1; first >= 0; first--) {
+                                               switch_set = new_prio();
+                                               switch_set->type = AEL_APPCALL;
+                                               if (!ast_compat_app_set) {
+                                                       switch_set->app = strdup("MSet");
+                                               } else {
+                                                       switch_set->app = strdup("Set");
+                                               }
+                                               /* Are we likely inside a gosub subroutine? */
+                                               if (!strcmp(exten->name, "~~s~~")) {
+                                                       /* If we're not actually within a gosub, this will fail, but the
+                                                        * second time through, it will get set.  If we are within gosub,
+                                                        * the second time through is redundant, but acceptable. */
+                                                       switch_set->appargs = strdup("LOCAL(~~EXTEN~~)=${EXTEN}");
+                                               } else {
+                                                       switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
+                                                       first = 0;
+                                               }
+                                               linkprio(exten, switch_set, mother_exten);
+                                               exten->has_switch = 1;
+                                               exten->checked_switch = 1;
+                                               if (mother_exten) {
+                                                       mother_exten->has_switch = 1;
+                                                       mother_exten->checked_switch = 1;
+                                               }
+                                       }
+                               }
+                       }
+               } else {
+                       if (mother_exten) {
+                               mother_exten->checked_switch = 1;
+                       }
+                       if (exten) {
+                               exten->checked_switch = 1;
+                       }
+               }
+       }
        for (p=statement; p; p=p->next) {
                switch (p->type) {
                case PV_VARDEC:
                        pr = new_prio();
                        pr->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
-                       pr->app = strdup("Set");
+                       snprintf(buf1, BUF_SIZE, "%s=$[%s]", p->u1.str, p->u2.val);
+                       if (!ast_compat_app_set) {
+                               pr->app = strdup("MSet");
+                       } else {
+                               pr->app = strdup("Set");
+                       }
                        remove_spaces_before_equals(buf1);
                        pr->appargs = strdup(buf1);
                        pr->origin = p;
@@ -3101,20 +3468,24 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                case PV_LOCALVARDEC:
                        pr = new_prio();
                        pr->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
-                       pr->app = strdup("Set");
+                       snprintf(buf1, BUF_SIZE, "LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
+                       if (!ast_compat_app_set) {
+                               pr->app = strdup("MSet");
+                       } else {
+                               pr->app = strdup("Set");
+                       }
                        remove_spaces_before_equals(buf1);
                        pr->appargs = strdup(buf1);
                        pr->origin = p;
                        linkprio(exten, pr, mother_exten);
                        break;
-
+                       
                case PV_GOTO:
                        pr = new_prio();
                        pr->type = AEL_APPCALL;
                        p->u2.goto_target = get_goto_target(p);
                        if( p->u2.goto_target ) {
-                               p->u3.goto_target_in_case = p->u2.goto_target->u2.label_in_case = label_inside_case(p->u2.goto_target);
+                               p->u3.goto_target_in_case = label_inside_case(p->u2.goto_target);
                        }
                        
                        if (!p->u1.list->next) /* just one */ {
@@ -3122,16 +3493,16 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                if (!mother_exten)
                                        pr->appargs = strdup(p->u1.list->u1.str);
                                else {  /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */ 
-                                       snprintf(buf1,sizeof(buf1),"%s,%s", mother_exten->name, p->u1.list->u1.str);
+                                       snprintf(buf1, BUF_SIZE, "%s,%s", mother_exten->name, p->u1.list->u1.str);
                                        pr->appargs = strdup(buf1);
                                }
                                
                        } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
-                               snprintf(buf1,sizeof(buf1),"%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
+                               snprintf(buf1, BUF_SIZE, "%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
                                pr->app = strdup("Goto");
                                pr->appargs = strdup(buf1);
                        } else if (p->u1.list->next && p->u1.list->next->next) {
-                               snprintf(buf1,sizeof(buf1),"%s,%s,%s", p->u1.list->u1.str, 
+                               snprintf(buf1, BUF_SIZE, "%s,%s,%s", p->u1.list->u1.str, 
                                                p->u1.list->next->u1.str,
                                                p->u1.list->next->next->u1.str);
                                pr->app = strdup("Goto");
@@ -3153,7 +3524,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        control_statement_count++;
                        loop_break_save = exten->loop_break; /* save them, then restore before leaving */
                        loop_continue_save = exten->loop_continue;
-                       snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "for_%s_%d", label, control_statement_count);
                        for_init = new_prio();
                        for_inc = new_prio();
                        for_test = new_prio();
@@ -3165,7 +3536,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        for_test->goto_false = for_end;
                        for_loop->type = AEL_CONTROL1; /* simple goto */
                        for_end->type = AEL_APPCALL;
-                       for_init->app = strdup("Set");
+                       if (!ast_compat_app_set) {
+                               for_init->app = strdup("MSet");
+                       } else {
+                               for_init->app = strdup("Set");
+                       }
                        
                        strcpy(buf2,p->u1.for_init);
                        remove_spaces_before_equals(buf2);
@@ -3174,7 +3549,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                strp2 = strchr(p->u1.for_init, '=');
                                *(strp+1) = 0;
                                strcat(buf2,"$[");
-                               strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
+                               strncat(buf2,strp2+1, BUF_SIZE-strlen(strp2+1)-2);
                                strcat(buf2,"]");
                                for_init->appargs = strdup(buf2);
                        } else {
@@ -3223,10 +3598,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                strp2 = strchr(p->u3.for_inc, '=');
                                *(strp+1) = 0;
                                strcat(buf2,"$[");
-                               strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
+                               strncat(buf2,strp2+1, BUF_SIZE-strlen(strp2+1)-2);
                                strcat(buf2,"]");
                                for_inc->appargs = strdup(buf2);
-                               for_inc->app = strdup("Set");
+                               if (!ast_compat_app_set) {
+                                       for_inc->app = strdup("MSet");
+                               } else {
+                                       for_inc->app = strdup("Set");
+                               }
                        } else {
                                strp2 = p->u3.for_inc;
                                while (*strp2 && isspace(*strp2))
@@ -3261,11 +3640,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        }
                                }
                        }
-                       snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
+                       snprintf(buf1, BUF_SIZE, "$[%s]",p->u2.for_test);
                        for_test->app = 0;
                        for_test->appargs = strdup(buf1);
                        for_loop->goto_true = for_test;
-                       snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish for_%s_%d", label, control_statement_count);
                        for_end->app = strdup("NoOp");
                        for_end->appargs = strdup(buf1);
                        /* link & load! */
@@ -3276,7 +3655,9 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        exten->loop_break = for_end;
                        exten->loop_continue = for_inc;
                        
-                       gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context); /* this will link in all the statements here */
+                       if (gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                               return -1;
+                       }
                        
                        linkprio(exten, for_inc, mother_exten);
                        linkprio(exten, for_loop, mother_exten);
@@ -3292,7 +3673,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        control_statement_count++;
                        loop_break_save = exten->loop_break; /* save them, then restore before leaving */
                        loop_continue_save = exten->loop_continue;
-                       snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "while_%s_%d", label, control_statement_count);
                        while_test = new_prio();
                        while_loop = new_prio();
                        while_end = new_prio();
@@ -3300,11 +3681,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        while_test->goto_false = while_end;
                        while_loop->type = AEL_CONTROL1; /* simple goto */
                        while_end->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
+                       snprintf(buf1, BUF_SIZE, "$[%s]",p->u1.str);
                        while_test->app = 0;
                        while_test->appargs = strdup(buf1);
                        while_loop->goto_true = while_test;
-                       snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish while_%s_%d", label, control_statement_count);
                        while_end->app = strdup("NoOp");
                        while_end->appargs = strdup(buf1);
 
@@ -3314,7 +3695,9 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        exten->loop_break = while_end;
                        exten->loop_continue = while_test;
                        
-                       gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the while body statements here */
+                       if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the while body statements here */
+                               return -1;
+                       }
 
                        linkprio(exten, while_loop, mother_exten);
                        linkprio(exten, while_end, mother_exten);
@@ -3330,30 +3713,15 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        local_control_statement_count = control_statement_count;
                        loop_break_save = exten->loop_break; /* save them, then restore before leaving */
                        loop_continue_save = exten->loop_continue;
-                       snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
-                       if ((mother_exten && !mother_exten->has_switch)) {
-                               switch_set = new_prio();
-                               switch_set->type = AEL_APPCALL;
-                               switch_set->app = strdup("Set");
-                               switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
-                               linkprio(exten, switch_set, mother_exten);
-                               mother_exten->has_switch = 1;
-                       } else if ((exten && !exten->has_switch)) {
-                               switch_set = new_prio();
-                               switch_set->type = AEL_APPCALL;
-                               switch_set->app = strdup("Set");
-                               switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
-                               linkprio(exten, switch_set, exten);
-                               exten->has_switch = 1;
-                       }
+                       snprintf(new_label, BUF_SIZE, "sw_%s_%d", label, control_statement_count);
                        switch_test = new_prio();
                        switch_end = new_prio();
                        switch_test->type = AEL_APPCALL;
                        switch_end->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",control_statement_count, p->u1.str);
+                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", control_statement_count, p->u1.str);
                        switch_test->app = strdup("Goto");
                        switch_test->appargs = strdup(buf1);
-                       snprintf(buf1,sizeof(buf1),"Finish switch-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish switch_%s_%d", label, control_statement_count);
                        switch_end->app = strdup("NoOp");
                        switch_end->appargs = strdup(buf1);
                        switch_end->origin = p;
@@ -3371,6 +3739,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                if (p2->type == PV_CASE) {
                                        /* ok, generate a extension and link it in */
                                        switch_case = new_exten();
+                                       if (mother_exten && mother_exten->checked_switch) {
+                                               switch_case->has_switch = mother_exten->has_switch;
+                                               switch_case->checked_switch = mother_exten->checked_switch;
+                                       }
+                                       if (exten && exten->checked_switch) {
+                                               switch_case->has_switch = exten->has_switch;
+                                               switch_case->checked_switch = exten->checked_switch;
+                                       }
                                        switch_case->context = this_context;
                                        switch_case->is_switch = 1;
                                        /* the break/continue locations are inherited from parent */
@@ -3378,11 +3754,13 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        switch_case->loop_continue = exten->loop_continue;
                                        
                                        linkexten(exten,switch_case);
-                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s", local_control_statement_count, p2->u1.str);
+                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s", local_control_statement_count, p2->u1.str);
                                        switch_case->name = strdup(buf1);
-                                       snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, p2->u1.str, local_control_statement_count);
+                                       snprintf(new_label, BUF_SIZE, "sw_%s_%s_%d", label, p2->u1.str, local_control_statement_count);
                                        
-                                       gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the case body statements here */
+                                       if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the case body statements here */
+                                               return -1;
+                                       }
 
                                        /* here is where we write code to "fall thru" to the next case... if there is one... */
                                        for (p3=p2->u2.statements; p3; p3=p3->next) {
@@ -3396,7 +3774,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, p2->next->u1.str);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_PATTERN) {
@@ -3404,14 +3782,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
                                                        gen_match_to_pattern(p2->next->u1.str, buf2);
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10", local_control_statement_count, buf2);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_DEFAULT) {
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-.,10",local_control_statement_count);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (!p2->next) {
@@ -3427,7 +3805,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                struct ael_priority *np2 = new_prio();
                                                np2->type = AEL_APPCALL;
                                                np2->app = strdup("NoOp");
-                                               snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
+                                               snprintf(buf, BUF_SIZE, "End of Extension %s", switch_case->name);
                                                np2->appargs = strdup(buf);
                                                linkprio(switch_case, np2, mother_exten);
                                                switch_case-> return_target = np2;
@@ -3435,18 +3813,28 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                } else if (p2->type == PV_PATTERN) {
                                        /* ok, generate a extension and link it in */
                                        switch_case = new_exten();
+                                       if (mother_exten && mother_exten->checked_switch) {
+                                               switch_case->has_switch = mother_exten->has_switch;
+                                               switch_case->checked_switch = mother_exten->checked_switch;
+                                       }
+                                       if (exten && exten->checked_switch) {
+                                               switch_case->has_switch = exten->has_switch;
+                                               switch_case->checked_switch = exten->checked_switch;
+                                       }
                                        switch_case->context = this_context;
                                        switch_case->is_switch = 1;
                                        /* the break/continue locations are inherited from parent */
                                        switch_case->loop_break = exten->loop_break;
                                        switch_case->loop_continue = exten->loop_continue;
-                                       
+
                                        linkexten(exten,switch_case);
-                                       snprintf(buf1,sizeof(buf1),"_sw-%d-%s", local_control_statement_count, p2->u1.str);
+                                       snprintf(buf1, BUF_SIZE, "_sw_%d_%s", local_control_statement_count, p2->u1.str);
                                        switch_case->name = strdup(buf1);
-                                       snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, p2->u1.str, local_control_statement_count);
-                                       
-                                       gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the while body statements here */
+                                       snprintf(new_label, BUF_SIZE, "sw_%s_%s_%d", label, p2->u1.str, local_control_statement_count);
+
+                                       if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the while body statements here */
+                                               return -1;
+                                       }
                                        /* here is where we write code to "fall thru" to the next case... if there is one... */
                                        for (p3=p2->u2.statements; p3; p3=p3->next) {
                                                if (!p3->next)
@@ -3459,7 +3847,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, p2->next->u1.str);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_PATTERN) {
@@ -3467,14 +3855,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
                                                        gen_match_to_pattern(p2->next->u1.str, buf2);
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, buf2);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_DEFAULT) {
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-.,10",local_control_statement_count);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (!p2->next) {
@@ -3498,6 +3886,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                } else if (p2->type == PV_DEFAULT) {
                                        /* ok, generate a extension and link it in */
                                        switch_case = new_exten();
+                                       if (mother_exten && mother_exten->checked_switch) {
+                                               switch_case->has_switch = mother_exten->has_switch;
+                                               switch_case->checked_switch = mother_exten->checked_switch;
+                                       }
+                                       if (exten && exten->checked_switch) {
+                                               switch_case->has_switch = exten->has_switch;
+                                               switch_case->checked_switch = exten->checked_switch;
+                                       }
                                        switch_case->context = this_context;
                                        switch_case->is_switch = 1;
                                        
@@ -3507,14 +3903,22 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
 
                                        default_exists++;
                                        switch_null = new_exten();
+                                       if (mother_exten && mother_exten->checked_switch) {
+                                               switch_null->has_switch = mother_exten->has_switch;
+                                               switch_null->checked_switch = mother_exten->checked_switch;
+                                       }
+                                       if (exten && exten->checked_switch) {
+                                               switch_null->has_switch = exten->has_switch;
+                                               switch_null->checked_switch = exten->checked_switch;
+                                       }
                                        switch_null->context = this_context;
                                        switch_null->is_switch = 1;
                                        switch_empty = new_prio();
-                                       snprintf(buf1,sizeof(buf1),"sw-%d-.|10",local_control_statement_count);
+                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                        switch_empty->app = strdup("Goto");
                                        switch_empty->appargs = strdup(buf1);
                                        linkprio(switch_null, switch_empty, mother_exten);
-                                       snprintf(buf1,sizeof(buf1),"sw-%d-", local_control_statement_count);
+                                       snprintf(buf1, BUF_SIZE, "sw_%d_", local_control_statement_count);
                                        switch_null->name = strdup(buf1);
                                        switch_null->loop_break = exten->loop_break;
                                        switch_null->loop_continue = exten->loop_continue;
@@ -3524,12 +3928,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        switch_case->loop_break = exten->loop_break;
                                        switch_case->loop_continue = exten->loop_continue;
                                        linkexten(exten,switch_case);
-                                       snprintf(buf1,sizeof(buf1),"_sw-%d-.", local_control_statement_count);
+                                       snprintf(buf1, BUF_SIZE, "_sw_%d_.", local_control_statement_count);
                                        switch_case->name = strdup(buf1);
                                        
-                                       snprintf(new_label,sizeof(new_label),"sw-%s-default-%d", label, local_control_statement_count);
+                                       snprintf(new_label, BUF_SIZE, "sw_%s_default_%d", label, local_control_statement_count);
                                        
-                                       gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the default:  body statements here */
+                                       if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the default:  body statements here */
+                                               return -1;
+                                       }
                                        
                                        /* here is where we write code to "fall thru" to the next case... if there is one... */
                                        for (p3=p2->u2.statements; p3; p3=p3->next) {
@@ -3543,7 +3949,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, p2->next->u1.str);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_PATTERN) {
@@ -3551,14 +3957,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
                                                        gen_match_to_pattern(p2->next->u1.str, buf2);
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, buf2);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_DEFAULT) {
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-.,10",local_control_statement_count);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (!p2->next) {
@@ -3593,7 +3999,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                case PV_MACRO_CALL:
                        pr = new_prio();
                        pr->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"%s,s,1", p->u1.str);
+                       snprintf(buf1, BUF_SIZE, "%s,~~s~~,1", p->u1.str);
                        first = 1;
                        for (p2 = p->u2.arglist; p2; p2 = p2->next) {
                                if (first)
@@ -3657,11 +4063,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
 
                case PV_IFTIME:
                        control_statement_count++;
-                       snprintf(new_label,sizeof(new_label),"iftime-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "iftime_%s_%d", label, control_statement_count);
                        
                        if_test = new_prio();
                        if_test->type = AEL_IFTIME_CONTROL;
-                       snprintf(buf1,sizeof(buf1),"%s,%s,%s,%s",
+                       snprintf(buf1, BUF_SIZE, "%s,%s,%s,%s",
                                         p->u1.list->u1.str, 
                                         p->u1.list->next->u1.str, 
                                         p->u1.list->next->next->u1.str, 
@@ -3672,7 +4078,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
 
                        if_end = new_prio();
                        if_end->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"Finish iftime-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish iftime_%s_%d", label, control_statement_count);
                        if_end->app = strdup("NoOp");
                        if_end->appargs = strdup(buf1);
 
@@ -3702,12 +4108,15 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        
                        /* now, put the body of the if here */
                        
-                       gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
+                       if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                               return -1;
+                       }
                        
                        if (p->u3.else_statements) {
                                linkprio(exten, if_skip, mother_exten);
-                               gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
-
+                               if (gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                                       return -1;
+                               }
                        }
                        
                        linkprio(exten, if_end, mother_exten);
@@ -3717,19 +4126,19 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                case PV_RANDOM:
                case PV_IF:
                        control_statement_count++;
-                       snprintf(new_label,sizeof(new_label),"if-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "if_%s_%d", label, control_statement_count);
                        
                        if_test = new_prio();
                        if_end = new_prio();
                        if_test->type = AEL_IF_CONTROL;
                        if_end->type = AEL_APPCALL;
                        if ( p->type == PV_RANDOM )
-                               snprintf(buf1,sizeof(buf1),"$[${RAND(0,99)} < (%s)]",p->u1.str);
+                               snprintf(buf1, BUF_SIZE, "$[${RAND(0,99)} < (%s)]", p->u1.str);
                        else
-                               snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
+                               snprintf(buf1, BUF_SIZE, "$[%s]", p->u1.str);
                        if_test->app = 0;
                        if_test->appargs = strdup(buf1);
-                       snprintf(buf1,sizeof(buf1),"Finish if-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish if_%s_%d", label, control_statement_count);
                        if_end->app = strdup("NoOp");
                        if_end->appargs = strdup(buf1);
                        if_test->origin = p;
@@ -3749,12 +4158,15 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        
                        /* now, put the body of the if here */
                        
-                       gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
+                       if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                               return -1;
+                       }
                        
                        if (p->u3.else_statements) {
                                linkprio(exten, if_skip, mother_exten);
-                               gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
-
+                               if (gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                                       return -1;
+                               }
                        }
                        
                        linkprio(exten, if_end, mother_exten);
@@ -3762,7 +4174,9 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        break;
 
                case PV_STATEMENTBLOCK:
-                       gen_prios(exten, label, p->u1.list, mother_exten, this_context ); /* recurse into the block */
+                       if (gen_prios(exten, label, p->u1.list, mother_exten, this_context)) { /* recurse into the block */
+                               return -1;
+                       }
                        break;
 
                case PV_CATCH:
@@ -3770,12 +4184,23 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        /* generate an extension with name of catch, put all catch stats
                           into this exten! */
                        switch_case = new_exten();
+                       if (mother_exten && mother_exten->checked_switch) {
+                               switch_case->has_switch = mother_exten->has_switch;
+                               switch_case->checked_switch = mother_exten->checked_switch;
+                       }
+                       if (exten && exten->checked_switch) {
+                               switch_case->has_switch = exten->has_switch;
+                               switch_case->checked_switch = exten->checked_switch;
+                       }
+                       
                        switch_case->context = this_context;
                        linkexten(exten,switch_case);
                        switch_case->name = strdup(p->u1.str);
-                       snprintf(new_label,sizeof(new_label),"catch-%s-%d",p->u1.str, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "catch_%s_%d",p->u1.str, control_statement_count);
                        
-                       gen_prios(switch_case, new_label, p->u2.statements,mother_exten,this_context); /* this will link in all the catch body statements here */
+                       if (gen_prios(switch_case, new_label, p->u2.statements, mother_exten,this_context)) { /* this will link in all the catch body statements here */
+                               return -1;
+                       }
                        if (switch_case->return_needed) { /* returns now generate a Return() app call, no longer a goto to the end of the exten */
                                char buf[2000];
                                struct ael_priority *np2 = new_prio();
@@ -3792,6 +4217,10 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        break;
                }
        }
+       free(buf1);
+       free(buf2);
+       free(new_label);
+       return 0;
 }
 
 void set_priorities(struct ael_extension *exten)
@@ -4005,8 +4434,22 @@ static void fix_gotos_in_extensions(struct ael_extension *exten)
        }
 }
 
+static int context_used(struct ael_extension *exten_list, struct ast_context *context)
+{
+       struct ael_extension *exten;
+       /* Check the simple elements first */
+       if (ast_walk_context_extensions(context, NULL) || ast_walk_context_includes(context, NULL) || ast_walk_context_ignorepats(context, NULL) || ast_walk_context_switches(context, NULL)) {
+               return 1;
+       }
+       for (exten = exten_list; exten; exten = exten->next_exten) {
+               if (exten->context == context) {
+                       return 1;
+               }
+       }
+       return 0;
+}
 
-void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root)
+int ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root)
 {
        pval *p,*p2;
        struct ast_context *context;
@@ -4041,13 +4484,17 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                        
                        exten = new_exten();
                        exten->context = context;
-                       exten->name = strdup("s");
+                       exten->name = strdup("~~s~~");
                        argc = 1;
                        for (lp=p->u2.arglist; lp; lp=lp->next) {
                                /* for each arg, set up a "Set" command */
                                struct ael_priority *np2 = new_prio();
                                np2->type = AEL_APPCALL;
-                               np2->app = strdup("Set");
+                               if (!ast_compat_app_set) {
+                                       np2->app = strdup("MSet");
+                               } else {
+                                       np2->app = strdup("Set");
+                               }
                                snprintf(buf,sizeof(buf),"LOCAL(%s)=${ARG%d}", lp->u1.str, argc++);
                                remove_spaces_before_equals(buf);
                                np2->appargs = strdup(buf);
@@ -4055,7 +4502,9 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                        }
                        
                        /* CONTAINS APPCALLS, CATCH, just like extensions... */
-                       gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context );
+                       if (gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context)) {
+                               return -1;
+                       }
                        if (exten->return_needed) {  /* most likely, this will go away */
                                struct ael_priority *np2 = new_prio();
                                np2->type = AEL_APPCALL;
@@ -4097,7 +4546,9 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                                        if ( p2->u3.hints )
                                                exten->hints = strdup(p2->u3.hints);
                                        exten->regexten = p2->u4.regexten;
-                                       gen_prios(exten, p->u1.str, p2->u2.statements, 0, context );
+                                       if (gen_prios(exten, p->u1.str, p2->u2.statements, 0, context)) {
+                                               return -1;
+                                       }
                                        if (exten->return_needed) { /* returns don't generate a goto eoe (end of extension) any more, just a Return() app call) */
                                                struct ael_priority *np2 = new_prio();
                                                np2->type = AEL_APPCALL;
@@ -4108,12 +4559,6 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                                                exten-> return_target = np2;
                                        }
                                        /* is the last priority in the extension a label? Then add a trailing no-op */
-                                       if( !exten->plist_last )
-                                       {
-                                               ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Empty Extension!\n",
-                                                               p2->filename, p2->startline, p2->endline);
-                                       }
-                                       
                                        if ( exten->plist_last && exten->plist_last->type == AEL_LABEL ) {
                                                struct ael_priority *np2 = new_prio();
                                                np2->type = AEL_APPCALL;
@@ -4184,6 +4629,71 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                        
                }
        }
+
+       /* Create default "h" bubble context */
+       if (ast_custom_function_find("DIALPLAN_EXISTS") && ast_custom_function_find("STACK_PEEK")) {
+               int i;
+               const char *h_context = "ael-builtin-h-bubble";
+               struct ael_priority *np;
+               struct {
+                       int priority;
+                       const char *app;
+                       const char *arg;
+               } steps[] = {
+                       /* Start high, to avoid conflict with existing h extensions */
+                       { 1, "Goto", "9991" },
+                       /* Save the context, because after the StackPop, it disappears */
+                       { 9991, "Set", "~~parentcxt~~=${STACK_PEEK(1,c,1)}" },
+                       /* If we're not in a Gosub frame, exit */
+                       { 9992, "GotoIf", "$[\"${~~parentcxt~~}\"=\"\"]?9996" },
+                       /* Check for an "h" extension in that context */
+                       { 9993, "GotoIf", "${DIALPLAN_EXISTS(${~~parentcxt~~},h,1)}?9994:9996" },
+                       /* Pop off the stack frame to prevent an infinite loop */
+                       { 9994, "StackPop", "" },
+                       /* Finally, go there. */
+                       { 9995, "Goto", "${~~parentcxt~~},h,1" },
+                       /* Just an empty priority for jumping out early */
+                       { 9996, "NoOp", "" }
+               };
+               context = ast_context_find_or_create(local_contexts, local_table, h_context, registrar);
+               if (context_used(exten_list, context)) {
+                       int found = 0;
+                       while (!found) {
+                               /* Pick a new context name that is not used. */
+                               char h_context_template[] = "/tmp/ael-builtin-h-bubble-XXXXXX";
+                               int fd = mkstemp(h_context_template);
+                               unlink(h_context_template);
+                               close(fd);
+                               context = ast_context_find_or_create(local_contexts, local_table, h_context_template + 5, registrar);
+                               found = !context_used(exten_list, context);
+                       }
+                       h_context = ast_get_context_name(context);
+               }
+               exten = new_exten();
+               exten->context = context;
+               exten->name = strdup("h");
+
+               for (i = 0; i < ARRAY_LEN(steps); i++) {
+                       np = new_prio();
+                       np->type = AEL_APPCALL;
+                       np->priority_num = steps[i].priority;
+                       np->app = strdup(steps[i].app);
+                       np->appargs = strdup(steps[i].arg);
+                       linkprio(exten, np, NULL);
+               }
+               attach_exten(&exten_list, exten);
+
+               /* Include the default "h" bubble context in each macro context */
+               for (exten = exten_list; exten; exten = exten->next_exten) {
+                       /* All macros contain a "~~s~~" extension, and it's the first created.  If
+                        * we perchance get a non-macro context, it's no big deal; the logic is
+                        * designed to exit out smoothly if not called from within a Gosub. */
+                       if (!strcmp(exten->name, "~~s~~")) {
+                               ast_context_add_include2(exten->context, h_context, registrar);
+                       }
+               }
+       }
+
        /* moved these from being done after a macro or extension were processed,
           to after all processing is done, for the sake of fixing gotos to labels inside cases... */
        /* I guess this would be considered 2nd pass of compiler now... */
@@ -4191,6 +4701,7 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
        add_extensions(exten_list);   /* actually makes calls to create priorities in ast_contexts -- feeds dialplan to asterisk */
        destroy_extensions(exten_list);  /* all that remains is an empty husk, discard of it as is proper */
        
+       return 0;
 }
 
 
@@ -5382,7 +5893,7 @@ pval* pvalGlobalsWalkStatements( pval *p, pval **next_statement )
 {
        if (!pvalCheckType(p, "pvalGlobalsWalkStatements", PV_GLOBALS))
                return 0;
-       if (!next_statement) {
+       if (!*next_statement) {
                *next_statement = p;
                return p;
        } else {
@@ -5403,7 +5914,7 @@ void pvalTopLevAddObject( pval *p, pval *contextOrObj )
 
 pval *pvalTopLevWalkObjects(pval *p, pval **next_obj )
 {
-       if (!next_obj) {
+       if (!*next_obj) {
                *next_obj = p;
                return p;
        } else {
@@ -5429,50 +5940,3 @@ pval * linku1(pval *head, pval *tail)
        return head;
 }
 
-#ifdef HERE_BY_MISTAKE_I_THINK
-static char *config = "extensions.ael";
-int do_pbx_load_module(void)
-{
-       int errs, sem_err, sem_warn, sem_note;
-       char *rfilename;
-       struct ast_context *local_contexts=NULL, *con;
-       struct pval *parse_tree;
-
-       ast_log(LOG_NOTICE, "Starting AEL load process.\n");
-       if (config[0] == '/')
-               rfilename = (char *)config;
-       else {
-               rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
-               sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config);
-       }
-       ast_log(LOG_NOTICE, "AEL load process: calculated config file name '%s'.\n", rfilename);
-
-       if (access(rfilename,R_OK) != 0) {
-               ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
-               return AST_MODULE_LOAD_DECLINE;
-       }
-       
-       parse_tree = ael2_parse(rfilename, &errs);
-       ast_log(LOG_DEBUG, "AEL load process: parsed config file name '%s'.\n", rfilename);
-       ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note);
-       if (errs == 0 && sem_err == 0) {
-               ast_log(LOG_DEBUG, "AEL load process: checked config file name '%s'.\n", rfilename);
-               ast_compile_ael2(&local_contexts, parse_tree);
-               ast_log(LOG_DEBUG, "AEL load process: compiled config file name '%s'.\n", rfilename);
-               
-               ast_merge_contexts_and_delete(&local_contexts, registrar);
-               ast_log(LOG_DEBUG, "AEL load process: merged config file name '%s'.\n", rfilename);
-               for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
-                       ast_context_verify_includes(con);
-               ast_log(LOG_DEBUG, "AEL load process: verified config file name '%s'.\n", rfilename);
-       } else {
-               ast_log(LOG_ERROR, "Sorry, but %d syntax errors and %d semantic errors were detected. It doesn't make sense to compile.\n", errs, sem_err);
-               destroy_pval(parse_tree); /* free up the memory */
-               return AST_MODULE_LOAD_FAILURE;
-       }
-       destroy_pval(parse_tree); /* free up the memory */
-       
-       return AST_MODULE_LOAD_SUCCESS;
-}
-#endif
-