Adding support for "urgent" voicemail messages. Messages which are
[asterisk/asterisk.git] / pbx / pbx_ael.c
old mode 100755 (executable)
new mode 100644 (file)
index 51328d0..22ec241
@@ -1,9 +1,9 @@
-/*
+/* 
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2005, Digium, Inc.
+ * Copyright (C) 2006, Digium, Inc.
  *
- * Mark Spencer <markster@digium.com>
+ * Steve Murphy <murf@parsetree.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
 
 /*! \file
  *
- * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions
+ * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
  * 
  */
 
-#include <sys/types.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <errno.h>
+/*** MODULEINFO
+       <depend>res_ael_share</depend>
+ ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include <ctype.h>
+#include <regex.h>
+#include <sys/stat.h>
+
 #include "asterisk/pbx.h"
 #include "asterisk/config.h"
 #include "asterisk/module.h"
 #include "asterisk/logger.h"
 #include "asterisk/cli.h"
+#include "asterisk/app.h"
 #include "asterisk/callerid.h"
-
-struct stringlink {
-       struct stringlink *next;
-       char data[0];
-};
-
-#define FILLIN_BREAK           1
-#define FILLIN_CONTINUE                2
-
-struct fillin {
-       struct fillin *next;
-       char exten[AST_MAX_EXTENSION];
-       int priority;
-       int type;
-};
-
-#ifdef __AST_DEBUG_MALLOC
-static void FREE(void *ptr)
-{
-       free(ptr);
-}
-#else
-#define FREE free
+#include "asterisk/hashtab.h"
+#include "asterisk/ael_structs.h"
+#include "asterisk/pval.h"
+#ifdef AAL_ARGCHECK
+#include "asterisk/argdesc.h"
 #endif
 
+/* these functions are in ../ast_expr2.fl */
+
 #define DEBUG_READ   (1 << 0)
 #define DEBUG_TOKENS (1 << 1)
 #define DEBUG_MACROS (1 << 2)
 #define DEBUG_CONTEXTS (1 << 3)
 
-static int aeldebug = 0;
-
-static char *dtext = "Asterisk Extension Language Compiler";
 static char *config = "extensions.ael";
 static char *registrar = "pbx_ael";
+static int pbx_load_module(void);
 
-static char *__grab_token(char *src, const char *filename, int lineno, int link)
-{
-       char *c;
-       char *b;
-       char *a;
-       int level = 0;
-       char *ret;
-#if 0
-       if (aeldebug || DEBUG_TOKENS) 
-               ast_verbose("Searching for token in '%s'!\n", src);
-#endif
-       c = src;
-       while(*c) {
-               if ((*c == '\\')) {
-                       c++;
-                       if (!*c)
-                               c--;
-               } else {
-                       if ((*c == '{') || (*c == '(')) {
-                               level++;
-                       } else if ((*c == '}') || (*c == ')')) {
-                               if (level)
-                                       level--;
-                               else
-                                       ast_log(LOG_WARNING, "Syntax error at line %d of '%s', too many closing braces!\n", lineno, filename);
-                       } else if ((*c == ';') && !level) {
-                               /* Got a token! */
-                               *c = '\0';
-                               b = c;
-                               b--;
-                               c++;
-                               while((b > src) && (*b < 33)) { 
-                                       *b = '\0'; 
-                                       b--; 
-                               }
-                               a = ast_skip_blanks(src);
-                               if (link) {
-                                       ret = malloc(strlen(a) + sizeof(struct stringlink) + 1);
-                                       if (ret)
-                                               strcpy(ret + sizeof(struct stringlink), a);
-                               } else
-                                       ret = strdup(a);
-                               /* Save remainder */
-                               memmove(src, c, strlen(c) + 1);
-                               return ret;
-                       }
-               }
-               c++;
-       }
-       return NULL;            
-}
-
-static char *grab_token(char *src, const char *filename, int lineno)
-{
-       return __grab_token(src, filename, lineno, 0);
-}
-
-static struct stringlink *arg_parse(char *args, const char *filename, int lineno)
-{
-       struct stringlink *cur, *prev=NULL, *root=NULL;
-       if (args) {
-               if (aeldebug & DEBUG_TOKENS) 
-                       ast_verbose("Parsing args '%s'!\n", args);
-               if (args[0] == '{') {
-                       /* Strip mandatory '}' from end */
-                       args[strlen(args) - 1] = '\0';
-                       while ((cur = (struct stringlink *)__grab_token(args + 1, filename, lineno, 1))) {
-                               cur->next = NULL;
-                               if (prev)
-                                       prev->next = cur;
-                               else
-                                       root = cur;
-                               prev = cur;
-                       }
-               } else if (*args) {
-                       root = malloc(sizeof(struct stringlink) + strlen(args) + 1);
-                       if (root) {
-                               strcpy(root->data, args);
-                               root->next = NULL;
-                       }
-               }
-       }
-       return root;
-}
-
-static char *grab_else(char *args, const char *filename, int lineno)
-{
-       char *ret = NULL;
-       int level=0;
-       char *c;
-       if (args) {
-               if (args[0] == '{') {
-                       c = args;
-                       while(*c) {
-                               if (*c == '{')
-                                       level++;
-                               else if (*c == '}') {
-                                       level--;
-                                       if (!level) {
-                                               c++;
-                                               while(*c && (*c < 33)) { *c = '\0'; c++; };
-                                               if (!strncasecmp(c, "else", 4) && 
-                                                       ((c[4] == '{') || (c[4] < 33))) {
-                                                               /* Ladies and gentlemen, we have an else clause */
-                                                       *c = '\0';
-                                                       c += 4;
-                                                       c = ast_skip_blanks(c);
-                                                       ret = c;
-                                                       if (aeldebug & DEBUG_TOKENS)
-                                                               ast_verbose("Returning else clause '%s'\n", c);
-                                               }
-                                               break;
-                                       }
-                               }
-                               c++;
-                       }
-               }
-       }
-       return ret;
-}
-
-static struct stringlink *param_parse(char *parms, const char *macro, const char *filename, int lineno)
-{
-       char *s, *e;
-       struct stringlink *root = NULL, *prev=NULL, *cur;
-       if (!parms || !*parms)
-               return NULL;
-       if (*parms != '(') {
-               ast_log(LOG_NOTICE, "Syntax error in parameter list for macro '%s' at about line %d of %s: Expecting '(' but got '%c'\n", macro, lineno, filename, *parms);
-               return NULL;
-       }
-       s = parms + 1;
-       while(*s) {
-               s = ast_skip_blanks(s);
-               e = s;
-               while(*e &&  (*e != ')') && (*e != ',')) {
-                       if (*e < 33)
-                               *e = '\0';
-                       e++;
-               }
-               if (*e) {
-                       /* Strip token */
-                       *e = '\0';
-                       e++;
-                       /* Skip over whitespace */
-                       e = ast_skip_blanks(e);
-                       /* Link */
-                       cur = malloc(strlen(s) + sizeof(struct stringlink) + 1);
-                       if (cur) {
-                               cur->next = NULL;
-                               strcpy(cur->data, s);
-                               if (prev)
-                                       prev->next = cur;
-                               else
-                                       root = cur;
-                               prev = cur;
-                       }
-                       s = e;
-               }
-       }
-       return root;
-}
-
-static void arg_free(struct stringlink *cur)
-{
-       struct stringlink *last;
-       while(cur) {
-               last = cur;
-               cur = cur->next;
-               free(last);
-       }
-}
-
-static void handle_globals(struct stringlink *vars)
-{
-       while(vars) {
-               pbx_builtin_setvar(NULL, vars->data);
-               vars = vars->next;
-       }
-}
-
-static struct stringlink *split_token(char *token, const char *filename, int lineno)
-{
-       char *args, *p;
-       struct stringlink *argv;
-       args = token;
-       while (*args && (*args > 32) && (*args != '{') && (*args != '(')) args++;
-       if (*args) {
-               p = args;
-               args = ast_skip_blanks(args);
-               if (*args != '(') {
-                       *p = '\0';
-               } else {
-                       while (*args && (*args != ')')) args++;
-                       if (*args == ')') {
-                               args++;
-                               args = ast_skip_blanks(args);
-                       }
-               }
-               if (!*args)
-                       args = NULL;
-       } else args = NULL;
-       argv = arg_parse(args, filename, lineno);
-       if (args)
-               *args = '\0';
-       return argv;
-}
+#ifndef AAL_ARGCHECK
+/* for the time being, short circuit all the AAL related structures
+   without permanently removing the code; after/during the AAL 
+   development, this code can be properly re-instated 
+*/
 
-static int matches_keyword(const char *data, const char *keyword)
-{
-       char c;
-       if (!strncasecmp(data, keyword, strlen(keyword))) {
-               c = data[strlen(keyword)];
-               if ((c < 33) || (c == '(') || (c == '{'))
-                       return 1;
-       }
-       return 0;
-}
+#endif
 
-static struct stringlink *split_params(char *token, const char *filename, int lineno)
-{
-       char *params;
-       struct stringlink *paramv;
-       params = token;
-       while(*params && (*params > 32) && (*params != '(')) params++;
-       if (*params) {
-               if (*params != '(') {
-                       *params = '\0';
-                       params++;
-                       params = ast_skip_blanks(params);
-               }
-               if (!*params)
-                       params = NULL;
-       } else params = NULL;
-       paramv = param_parse(params, token, filename, lineno);
-       if (params)
-               *params = '\0';
-       return paramv;
-}
+#ifdef AAL_ARGCHECK
+int option_matches_j( struct argdesc *should, pval *is, struct argapp *app);
+int option_matches( struct argdesc *should, pval *is, struct argapp *app);
+int ael_is_funcname(char *name);
+#endif
 
-static const char *get_case(char *s, char **restout, int *pattern)
-{
-       char *newcase=NULL;
-       char *rest=NULL;
-       if (!strncasecmp(s, "case", 4) && s[4] && ((s[4] < 33) || (s[4] == ':'))) {
-               newcase = s + 4;
-               newcase = ast_skip_blanks(newcase);
-               rest = newcase;
-               *pattern = 0;
-       } else if (!strncasecmp(s, "pattern", 7) && s[7] && ((s[7] < 33) || (s[7] == ':'))) {
-               newcase = s + 8;
-               newcase = ast_skip_blanks(newcase);
-               rest = newcase;
-               *pattern = 1;
-       } else if (!strncasecmp(s, "default", 7) && ((s[7] < 33) || (s[7] == ':'))) {
-               newcase = ".";
-               rest = s + 7;
-               rest = ast_skip_blanks(rest);
-               *pattern = 1;
-       }
+int check_app_args(pval *appcall, pval *arglist, struct argapp *app);
+void check_pval(pval *item, struct argapp *apps, int in_globals);
+void check_pval_item(pval *item, struct argapp *apps, int in_globals);
+void check_switch_expr(pval *item, struct argapp *apps);
+void ast_expr_register_extra_error_info(char *errmsg);
+void ast_expr_clear_extra_error_info(void);
+struct pval *find_macro(char *name);
+struct pval *find_context(char *name);
+struct pval *find_context(char *name);
+struct pval *find_macro(char *name);
+struct ael_priority *new_prio(void);
+struct ael_extension *new_exten(void);
+void destroy_extensions(struct ael_extension *exten);
+void set_priorities(struct ael_extension *exten);
+void add_extensions(struct ael_extension *exten);
+void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root);
+void destroy_pval(pval *item);
+void destroy_pval_item(pval *item);
+int is_float(char *arg );
+int is_int(char *arg );
+int is_empty(char *arg);
+
+/* static void substitute_commas(char *str); */
 
-       if (rest) {
-               while (*rest && (*rest > 32) && (*rest != ':')) rest++;
-               if (*rest) {
-                       *rest = 0;
-                       rest++;
-                       while (*rest && ((*rest == ':') || (*rest < 33))) rest++;
-                       *restout = rest;
-               } else {
-                       *restout = "";
-               }
-       } else
-               *restout = s;
-       if (aeldebug & DEBUG_TOKENS)
-               ast_verbose("GETCASE: newcase is '%s', rest = '%s'\n", newcase, *restout);
-       return newcase;
-}
+static int aeldebug = 0;
 
-static void fillin_free(struct fillin *fillin)
-{
-       struct fillin *cur, *next;
-       cur =  fillin;
-       while(cur) {
-               next = cur->next;
-               free(cur);
-               cur = next;
-       }
-}
+/* interface stuff */
 
-static void fillin_process(struct ast_context *con, struct fillin *fillin, const char *filename, int lineno, const char *breakexten, int breakprio, const char *contexten, int contprio)
-{
-       struct fillin *cur;
-       char *app;
-       char mdata[AST_MAX_EXTENSION + 20];
-       cur = fillin;
-       while(cur) {
-               if (cur->type == FILLIN_BREAK) {
-                       if (breakexten && breakprio) {
-                               app = "Goto";
-                               snprintf(mdata, sizeof(mdata), "%s|%d", breakexten, breakprio);
-                       } else {
-                               app = "NoOp";
-                               snprintf(mdata, sizeof(mdata), "Invalid break");
-                               ast_log(LOG_NOTICE, "Ignoring inappropriate break around line %d of %s\n", lineno, filename);
-                       }
-                       if (ast_add_extension2(con, 0, cur->exten, cur->priority, NULL, NULL, app, strdup(mdata), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of break '%s'\n", cur->priority, cur->exten);
-               } else if (cur->type == FILLIN_CONTINUE) {
-                       if (contexten && contprio) {
-                               app = "Goto";
-                               snprintf(mdata, sizeof(mdata), "%s|%d", contexten, contprio);
-                       } else {
-                               app = "NoOp";
-                               snprintf(mdata, sizeof(mdata), "Invalid continue");
-                               ast_log(LOG_NOTICE, "Ignoring inappropriate continue around line %d of %s\n", lineno, filename);
-                       }
-                       if (ast_add_extension2(con, 0, cur->exten, cur->priority, NULL, NULL, app, strdup(mdata), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of continue '%s'\n", cur->priority, cur->exten);
-               } else {
-                       ast_log(LOG_WARNING, "Whoa, unknown fillin type '%d'\n", cur->type);
-               }
-               cur = cur->next;
-       }
-}
+/* if all the below are static, who cares if they are present? */
 
-static int match_assignment(char *variable, char **value)
+static int pbx_load_module(void)
 {
-       char *c;
-       char *ws;
-       int inpar = 0;
-       c = variable;
+       int errs=0, sem_err=0, sem_warn=0, sem_note=0;
+       char *rfilename;
+       struct ast_context *local_contexts=NULL, *con;
+       struct ast_hashtab *local_table=NULL;
        
-       while (*c && (*c > 32)) {
-               if(*c == ')' && (inpar > 0)) {
-                       inpar--;
-               } else if(*c == '(' && (inpar >= 0)) {
-                       inpar++;
-               } else if(*c == '=' && (inpar == 0)) {
-                       break;
-               }
-               c++;
-       } 
-       ws = c;
-       c = ast_skip_blanks(c);
-       if (*c == '=') {
-               *ws = '\0';
-               *c = '\0';
-               c++;
-               c = ast_skip_blanks(c);
-               *value = c;
-               return 1;
-       }
-       return 0;
-}
+       struct pval *parse_tree;
 
-static int matches_label(char *data, char **rest)
-{
-       char last = 0;
-       char *start = data;
-       while (*data > 32) {
-               last = *data;
-               data++;
-       }
-       if (last != ':') {
-               data = ast_skip_blanks(data);
-               last = *data;
-               data++;
-       }
-       if (last == ':') {
-               *rest = data;
-               /* Go back and trim up the label */
-               while(*start && ((*start > 32) && (*start != ':'))) start++;
-               *start = '\0';
-               return 1;
+       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);
        }
-       return 0;
-}
-
-static char *argument_end(char *str)
-{
-       int level=0;
-       while(*++str) {
-               switch(*str) {
-               case '(':
-                       level++;
-                       break;
-               case ')':
-                       if(level)
-                               level--;
-                       else
-                               return str;
-                       break;
-               default:
-                       break;
-               }
+       if (access(rfilename,R_OK) != 0) {
+               ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename);
+               return AST_MODULE_LOAD_DECLINE;
        }
-       return NULL;
-}
-
-static int build_step(const char *what, const char *name, const char *filename, int lineno, struct ast_context *con, char *exten, int *pos, char *data, struct fillin **fillout, char **label);
-static int __build_step(const char *what, const char *name, const char *filename, int lineno, struct ast_context *con, char *exten, int *pos, char *data, struct fillin **fillout, char **label)
-{
-       char *app;
-       char *args;
-       char *c;
-       char *margs=NULL;
-       char *oargs;
-       char *rest;
-       const char *curcase, *newcase;
-       struct stringlink *swargs, *cur;
-       int cpos;
-       int mlen;
-       int pattern = 0;
-       struct fillin *fillin;
        
-       data = ast_skip_blanks(data);
-       if (matches_label(data, &c)) {
-               *label = data;
-               data = c;
-               data = ast_skip_blanks(data);
-       }
-       if (ast_strlen_zero(data))
-               return 0;
-       if (matches_keyword(data, "switch")) {
-               fillin = NULL;
-               /* Switch */
-               args = data + strlen("switch");
-               while ((*args < 33) && (*args != '(')) args++;
-               if ((*args == '(') && (c = argument_end(args))) {
-                       args++;
-                       *c = '\0';
-                       c++;
-                       if (aeldebug & DEBUG_TOKENS)
-                               ast_verbose("--SWITCH on : %s\n", args);
-                       mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
-                       margs = alloca(mlen);
-                       app = "Goto";
-                       sprintf(margs, "sw-%d-%s|1", *pos, args);
-                       ast_process_quotes_and_slashes(margs, ',', '|');
-                       oargs = args;
-                       args = margs;
-                       if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       else {
-                               *label = NULL;
-                               (*pos)++;
-                       }
-                       app = "NoOp";
-                       sprintf(margs, "Finish switch-%d", *pos - 1);
-                       if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       else {
-                               *label = NULL;
-                               (*pos)++;
-                       }
-                       c = ast_skip_blanks(c);
-                       if (aeldebug & DEBUG_TOKENS)
-                               ast_verbose("ARG Parsing '%s'\n", c);
-                       swargs = arg_parse(c, filename, lineno);
-                       cur = swargs;
-                       curcase = NULL;
-                       while(cur) {
-                               if ((newcase = get_case(cur->data, &rest, &pattern))) {
-                                       if (aeldebug & DEBUG_TOKENS)
-                                               ast_verbose("--NEWCASE: '%s'!\n", newcase);
-                                       if (curcase) {
-                                               /* Handle fall through */
-                                               char tmp[strlen(newcase) + strlen(name) + 40];
-                                               sprintf(tmp, "sw-%d-%s|%d", *pos - 2, newcase, 1);
-                                               ast_add_extension2(con, 0, margs, cpos, NULL, NULL, "Goto", strdup(tmp), FREE, registrar);
-                                       }
-                                       curcase = newcase;
-                                       cpos = 1;
-                                       if (pattern)
-                                               snprintf(margs, mlen, "_sw-%d-%s", *pos - 2, curcase);
-                                       else
-                                               snprintf(margs, mlen, "sw-%d-%s", *pos - 2, curcase);
-                                       if (!strcasecmp(rest, "break")) {
-                                               char tmp[strlen(exten) + 10];
-                                               sprintf(tmp, "%s|%d", exten, *pos - 1);
-                                               ast_add_extension2(con, 0, exten, cpos, *label, NULL, "Goto", strdup(tmp), FREE, registrar);
-                                               curcase = NULL;
-                                               *label = NULL;
-                                       } else
-                                               build_step("switch", margs, filename, lineno, con, margs, &cpos, rest, &fillin, label);
-                               } else if (curcase) {
-                                       if (aeldebug & DEBUG_TOKENS)
-                                               ast_verbose("Building statement from '%s'\n", rest);
-                                       if (!strcasecmp(rest, "break")) {
-                                               char tmp[strlen(exten) + 10];
-                                               sprintf(tmp, "%s|%d", exten, *pos - 1);
-                                               ast_add_extension2(con, 0, margs, cpos, *label, NULL, "Goto", strdup(tmp), FREE, registrar);
-                                               curcase = NULL;
-                                               *label = NULL;
-                                       } else
-                                               build_step("switch", margs, filename, lineno, con, margs, &cpos, rest, &fillin, label);
-                               } else 
-                                       ast_log(LOG_WARNING, "Unreachable code in switch at about line %d of %s\n", lineno, filename);
-                               if (aeldebug & DEBUG_TOKENS)
-                                       ast_verbose("--SWARG: %s\n", cur->data);
-                               cur = cur->next;
-                       }
-                       /* Can't do anything with these */
-                       fillin_process(con, fillin, filename, lineno, NULL, 0, NULL, 0);
-                       fillin_free(fillin);
-                       arg_free(swargs);
-               } else
-                       ast_log(LOG_WARNING, "Syntax error in switch declaration in %s around line %d!\n", filename, lineno); 
-                       
-       } else if (matches_keyword(data, "if")) {
-               /* If... */
-               args = data + strlen("if");
-               while ((*args < 33) && (*args != '(')) args++;
-               if ((*args == '(') && (c = argument_end(args))) {
-                       int ifblock;
-                       int ifstart;
-                       int elsestart;
-                       int ifend;
-                       int ifskip;
-                       char *elses;
-                       char *iflabel;
-                       args++;
-                       *c = '\0';
-                       c++;
-                       c = ast_skip_blanks(c);
-                       if (aeldebug & DEBUG_TOKENS)
-                               ast_verbose("--IF on : '%s' : '%s'\n", args, c);
-                       mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
-                       margs = alloca(mlen);
-                       /* Remember where the ifblock starts, and skip over */
-                       ifblock = (*pos)++;
-                       iflabel = *label;
-                       *label = NULL;
-                       /* Remember where the start of the ifblock is */
-                       ifstart = *pos;
-                       snprintf(margs, mlen, "if-%s-%d", name, ifblock);
-                       /* Now process the block of the if */
-                       if (aeldebug & DEBUG_TOKENS)
-                               ast_verbose("Searching for elses in '%s'\n", c);
-                       elses = grab_else(c, filename, lineno);
-                       build_step("if", margs, filename, lineno, con, exten, pos, c, fillout, label);
-                       if (elses) {
-                               /* Reserve a goto to exit the if */
-                               ifskip = *pos;
-                               (*pos)++;
-                               elsestart = *pos;
-                               build_step("else", margs, filename, lineno, con, exten, pos, elses, fillout, label);
-                       } else {
-                               elsestart = *pos;
-                               ifskip = 0;
-                       }
-                       ifend = *pos;
-                       (*pos)++;
-                       app = "NoOp";
-                       snprintf(margs, mlen, "Finish if-%s-%d", name, ifblock);
-                       if (ast_add_extension2(con, 0, exten, ifend, *label, NULL, app, strdup(margs), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       *label = NULL;
-                       app = "GotoIf";
-                       snprintf(margs, mlen, "$[ %s ]?%d:%d", args, ifstart, elsestart);
-                       if (ast_add_extension2(con, 0, exten, ifblock, iflabel, NULL, app, strdup(margs), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       if (ifskip) {
-                               /* Skip as appropriate around else clause */
-                               snprintf(margs, mlen, "%d", ifend);
-                               if (ast_add_extension2(con, 0, exten, ifskip, NULL, NULL, "Goto", strdup(margs), FREE, registrar))
-                                       ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       }
-               } else
-                       ast_log(LOG_WARNING, "Syntax error in if declaration in %s around line %d!\n", filename, lineno); 
-       } else if (matches_keyword(data, "while")) {
-               /* While... */
-               fillin = NULL;
-               args = data + strlen("while");
-               while ((*args < 33) && (*args != '(')) args++;
-               if ((*args == '(') && (c = argument_end(args))) {
-                       int whileblock;
-                       int whilestart;
-                       int whileend;
-                       char *whilelabel;
-                       args++;
-                       *c = '\0';
-                       c++;
-                       c = ast_skip_blanks(c);
-                       if (aeldebug & DEBUG_TOKENS)
-                               ast_verbose("--WHILE on : '%s' : '%s'\n", args, c);
-                       mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
-                       margs = alloca(mlen);
-                       /* Remember where to put the conditional, and keep its position */
-                       whilestart = (*pos);
-                       whilelabel = *label;
-                       *label = NULL;
-                       (*pos)++;
-                       /* Remember where the whileblock starts */
-                       whileblock = (*pos);
-                       snprintf(margs, mlen, "while-%s-%d", name, whilestart);
-                       build_step("while", margs, filename, lineno, con, exten, pos, c, &fillin, label);
-                       /* Close the loop */
-                       app = "Goto";
-                       snprintf(margs, mlen, "%d", whilestart);
-                       if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       *label = NULL;
-                       whileend = (*pos);
-                       /* Place trailer */
-                       app = "NoOp";
-                       snprintf(margs, mlen, "Finish while-%s-%d", name, whilestart);
-                       if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       *label = NULL;
-                       app = "GotoIf";
-                       snprintf(margs, mlen, "$[ %s ]?%d:%d", args, whileblock, whileend);
-                       if (ast_add_extension2(con, 0, exten, whilestart, whilelabel, NULL, app, strdup(margs), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                       fillin_process(con, fillin, filename, lineno, exten, whileend, exten, whilestart);
-                       fillin_free(fillin);
-               } else
-                       ast_log(LOG_WARNING, "Syntax error in while declaration in %s around line %d!\n", filename, lineno); 
-       } else if (matches_keyword(data, "jump")) {
-               char *p;
-               /* Jump... */
-               fillin = NULL;
-               args = data + strlen("jump");
-               args = ast_skip_blanks(args);
-               if (aeldebug & DEBUG_TOKENS)
-                       ast_verbose("--JUMP to : '%s'\n", args);
-               p = strchr(args, ',');
-               if (p) {
-                       *p = '\0';
-                       p++;
-               } else
-                       p = "1";
-               c = strchr(args, '@');
-               if (c) {
-                       *c = '\0';
-                       c++;
-               }
-               mlen = strlen(exten) + 128 + strlen(args) + strlen(name) + (c ? strlen(c) : 0);
-               margs = alloca(mlen);
-               if (c) 
-                       snprintf(margs, mlen, "%s|%s|%s", c,args, p);
-               else
-                       snprintf(margs, mlen, "%s|%s", args, p);
-               app = "Goto";
-               if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
-                       ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-               *label = NULL;
-       } else if (matches_keyword(data, "goto")) {
-               /* Jump... */
-               fillin = NULL;
-               args = data + strlen("goto");
-               args = ast_skip_blanks(args);
-               if (aeldebug & DEBUG_TOKENS)
-                       ast_verbose("--GOTO to : '%s'\n", args);
-               app = "Goto";
-               if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(args), FREE, registrar))
-                       ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-               *label = NULL;
-       } else if (matches_keyword(data, "for")) {
-               /* While... */
-               fillin = NULL;
-               args = data + strlen("for");
-               while ((*args < 33) && (*args != '(')) args++;
-               if ((*args == '(') && (c = argument_end(args))) {
-                       int forblock;
-                       int forprep;
-                       int forstart;
-                       int forend;
-                       struct stringlink *fields;
-                       char *tmp;
-                       char *forlabel = NULL;
-                       args++;
-                       *c = '\0';
-                       c++;
-                       c = ast_skip_blanks(c);
-                       /* Parse arguments first */
-                       tmp = alloca(strlen(args) + 10);
-                       if (tmp) {
-                               snprintf(tmp, strlen(args) + 10, "{%s;}", args);
-                               fields = arg_parse(tmp, filename, lineno);
-                       } else
-                               fields = NULL;
-                       if (fields && fields->next && fields->next->next) {
-                               if (aeldebug & DEBUG_TOKENS)
-                                       ast_verbose("--FOR ('%s' ; '%s' ; '%s') : '%s'\n", fields->data, fields->next->data, fields->next->next->data, c);
-                               mlen = strlen(exten) + 128 + strlen(args) + strlen(name);
-                               margs = alloca(mlen);
-                               forprep = *pos;
-                               snprintf(margs, mlen, "for-%s-%d", name, forprep);
-                               fillin = NULL;
-                               build_step("while", margs, filename, lineno, con, exten, pos, fields->data, &fillin, label);
-                               /* Remember where to put the conditional, and keep its position */
-                               forstart = (*pos);
-                               forlabel = *label;
-                               (*pos)++;
-                               *label = NULL;
-                               /* Remember where the whileblock starts */
-                               forblock = (*pos);
-                               build_step("for", margs, filename, lineno, con, exten, pos, fields->next->next->data, &fillin, label);
-                               build_step("for", margs, filename, lineno, con, exten, pos, c, &fillin, label);
-                               /* Close the loop */
-                               app = "Goto";
-                               snprintf(margs, mlen, "%d", forstart);
-                               if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
-                                       ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                               *label = NULL;
-                               forend = (*pos);
-                               /* Place trailer */
-                               app = "NoOp";
-                               snprintf(margs, mlen, "Finish for-%s-%d", name, forprep);
-                               if (ast_add_extension2(con, 0, exten, (*pos)++, *label, NULL, app, strdup(margs), FREE, registrar))
-                                       ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-                               *label = NULL;
-                               app = "GotoIf";
-                               snprintf(margs, mlen, "$[ %s ]?%d:%d", fields->next->data, forblock, forend);
-                               if (ast_add_extension2(con, 0, exten, forstart, forlabel, NULL, app, strdup(margs), FREE, registrar))
-                                       ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", forstart, what, name);
-                               fillin_process(con, fillin, filename, lineno, exten, forend, exten, forstart);
-                               fillin_free(fillin);
-                       } else
-                               ast_log(LOG_NOTICE, "Improper for declaration in %s around line %d!\n", filename, lineno); 
-                       arg_free(fields);
-               } else
-                       ast_log(LOG_WARNING, "Syntax error in for declaration in %s around line %d!\n", filename, lineno); 
-                       
-       } else if (!strcasecmp(data, "break") || !strcasecmp(data, "continue")) {
-               struct fillin *fi;
-               fi = malloc(sizeof(struct fillin));
-               if (fi) {
-                       memset(fi, 0, sizeof(struct fillin));
-                       if (!strcasecmp(data, "break"))
-                               fi->type = FILLIN_BREAK;
-                       else
-                               fi->type = FILLIN_CONTINUE;
-                       ast_copy_string(fi->exten, exten, sizeof(fi->exten));
-                       fi->priority = (*pos)++;
-                       fi->next = *fillout;
-                       *fillout = fi;
-               }
-       } else if (match_assignment(data, &rest)) {
-               if (aeldebug & DEBUG_TOKENS)
-                       ast_verbose("ASSIGN  '%s' = '%s'\n", data, rest);
-               mlen = strlen(rest) + strlen(data) + 20;
-               margs = alloca(mlen);
-               snprintf(margs, mlen, "%s=$[ %s ]", data, rest);
-               app = "Set";
-               if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(margs), FREE, registrar))
-                       ast_log(LOG_WARNING, "Unable to add assignment at priority '%d' of %s '%s'\n", *pos, what, name);
-               else {
-                       *label = NULL;
-                       (*pos)++;
-               }
+       parse_tree = ael2_parse(rfilename, &errs);
+       ast_log(LOG_NOTICE, "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_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename);
+               local_table = ast_hashtab_create(11, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
+               ast_compile_ael2(&local_contexts, local_table, parse_tree);
+               ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename);
+               
+               ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
+               local_table = NULL; /* it's the dialplan global now */
+               local_contexts = NULL;
+               ast_log(LOG_NOTICE, "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_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename);
        } else {
-               app = data;
-               args = app;
-               while (*args && (*args > 32) && (*args != '(')) args++;
-                       if (*args != '(') {
-                       while(*args && (*args != '(')) { *args = '\0'; args++; };
-               }
-               if (*args == '(') {
-                       *args = '\0';
-                       args++;
-                       /* Got arguments, trim trailing ')' */
-                       c = args + strlen(args) - 1;
-                       while((c >= args) && (*c < 33) && (*c != ')')) { *c = '\0'; c--; };
-                       if ((c >= args) && (*c == ')')) *c = '\0';
-               } else
-                       args = "";
-               ast_process_quotes_and_slashes(args, ',', '|');
-               if (app[0] == '&') {
-                       app++;
-                       margs = alloca(strlen(args) + strlen(app) + 10);
-                       sprintf(margs, "%s|%s", app, args);
-                       args = margs;
-                       app = "Macro";
-               }
-               if (aeldebug & DEBUG_TOKENS)
-                       ast_verbose("-- APP: '%s', ARGS: '%s'\n", app, args);
-               if (ast_add_extension2(con, 0, exten, *pos, *label, NULL, app, strdup(args), FREE, registrar))
-                       ast_log(LOG_WARNING, "Unable to add step at priority '%d' of %s '%s'\n", *pos, what, name);
-               else {
-                       (*pos)++;
-                       *label = NULL;
-               }
-       }
-       return 0;
-}
-
-static int build_step(const char *what, const char *name, const char *filename, int lineno, struct ast_context *con, char *exten, int *pos, char *data, struct fillin **fillout, char **label)
-{
-       struct stringlink *args, *cur;
-       int res=0;
-       struct fillin *fillin=NULL;
-       int dropfill = 0;
-       char *labelin = NULL;
-       if (!fillout) {
-               fillout = &fillin;
-               dropfill = 1;
+               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_DECLINE;
        }
-       if (!label) {
-               label = &labelin;
-       };
-       args = arg_parse(data, filename, lineno);
-       cur = args;
-       while(cur) {
-               res |= __build_step(what, name, filename, lineno, con, exten, pos, cur->data, fillout, label);
-               cur = cur->next;
-       }
-       arg_free(args);
-       if (dropfill) {
-               fillin_process(con, fillin, filename, lineno, NULL, 0, NULL, 0);
-               fillin_free(fillin);
-       }
-       return res;
-}
-
-static int parse_catch(char *data, char **catch, char **rest)
-{
-       /* Skip the word 'catch' */
-       data += 5;
-       data = ast_skip_blanks(data);
-       /* Here's the extension */
-       *catch = data;
-       if (!*data)
-               return 0;
-       while (*data && (*data > 32)) data++;
-       if (!*data)
-               return 0;
-       /* Trim any trailing spaces */
-       *data = '\0';
-       data++;
-       data = ast_skip_blanks(data);
-       if (!*data)
-               return 0;
-       *rest = data;
-       return 1;
-}
-
-static void handle_macro(struct ast_context **local_contexts, struct stringlink *vars, const char *filename, int lineno)
-{
-       struct stringlink *argv;
-       struct stringlink *paramv;
-       struct stringlink *cur;
-       struct ast_context *con;
-       struct fillin *fillin;
-       char *catch, *rest;
-       char name[256];
-       int pos;
-       int cpos;
-
-       if (aeldebug & DEBUG_MACROS)
-               ast_verbose("Root macro def is '%s'\n", vars->data);
-       argv = split_token(vars->data, filename, lineno);
-       paramv = split_params(vars->data, filename, lineno);
-       if (aeldebug & DEBUG_MACROS) 
-               ast_verbose("Found macro '%s'\n", vars->data);
-       snprintf(name, sizeof(name), "macro-%s", vars->data);
-       con = ast_context_create(local_contexts, name, registrar);
-       if (con) {
-               pos = 1;
-               cur = paramv;
-               while(cur) {
-                       if (aeldebug & DEBUG_MACROS)
-                               ast_verbose("  PARAM => '%s'\n", cur->data);
-                       snprintf(name, sizeof(name), "%s=${ARG%d}", cur->data, pos);
-                       if (ast_add_extension2(con, 0, "s", pos, NULL, NULL, "Set", strdup(name), FREE, registrar))
-                               ast_log(LOG_WARNING, "Unable to add step at priority '%d' of macro '%s'\n", pos, vars->data);
-                       else
-                               pos++;
-                       cur = cur->next;
-               }
-               cur = argv;
-               while(cur) {
-                       if (aeldebug & DEBUG_MACROS)
-                               ast_verbose("  STEP => '%s'\n", cur->data);
-                       if (matches_keyword(cur->data, "catch")) {
-                               if (aeldebug & DEBUG_MACROS)
-                                       ast_verbose("--CATCH: '%s'\n", cur->data);
-                               if (parse_catch(cur->data, &catch, &rest)) {
-                                       cpos = 1;
-                                       build_step("catch", catch, filename, lineno, con, catch, &cpos, rest, NULL, NULL);
-                               } else
-                                       ast_log(LOG_NOTICE, "Parse error for catch at about line %d of %s\n", lineno, filename);
-                       } else {
-                               fillin = NULL;
-                               build_step("macro", vars->data, filename, lineno, con, "s", &pos, cur->data, NULL, NULL);
-                       }
-                       cur = cur->next;
-               }
-       } else
-               ast_log(LOG_WARNING, "Unable to create context '%s'\n", name);
-       arg_free(paramv);
-       arg_free(argv);
-       if (vars->next)
-               ast_log(LOG_NOTICE, "Ignoring excess tokens in macro definition around line %d of %s!\n", lineno, filename);
+       destroy_pval(parse_tree); /* free up the memory */
+       
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
-static int matches_extension(char *exten, char **extout)
-{
-       char *c;
-       *extout = NULL;
-       c = exten;
-       while(*c && (*c > 32)) c++;
-       if (*c) {
-               *c = '\0';
-               c++;
-               c = ast_skip_blanks(c);
-               if (*c) {
-                       if (*c == '=') {
-                               *c = '\0';
-                               c++;
-                               if (*c == '>')
-                                       c++;
-                               c = ast_skip_blanks(c);
-                               *extout = c;
-                               return 1;
-                       }
-               }
+/* CLI interface */
+static char *handle_cli_ael_debug_multiple_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "ael debug [read|tokens|macros|contexts|off]";
+               e->usage =
+                       "Usage: ael debug [read|tokens|macros|contexts|off]\n"
+                       "       Enable AEL read, token, macro, or context debugging,\n"
+                       "       or disable all AEL debugging messages.  Note: this\n"
+                       "       currently does nothing.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
        }
-       return 0;
-}
 
-static void parse_keyword(char *s, char **o)
-{
-       char *c;
-       c = s;
-       while((*c) && (*c > 32)) c++;
-       if (*c) {
-               *c = '\0';
-               c++;
-               c = ast_skip_blanks(c);
-               *o = c;
-       } else
-               *o = NULL;
-}
-
-static void handle_context(struct ast_context **local_contexts, struct stringlink *vars, const char *filename, int lineno)
-{
-       struct stringlink *argv;
-       struct stringlink *cur2;
-       struct stringlink *argv2;
-       struct stringlink *cur;
-       struct ast_context *con;
-       char *rest;
-       char *c;
-       char name[256];
-       int pos;
-
-       if (aeldebug & DEBUG_CONTEXTS)
-               ast_verbose("Root context def is '%s'\n", vars->data);
-       argv = split_token(vars->data, filename, lineno);
-       if (aeldebug & DEBUG_CONTEXTS) 
-               ast_verbose("Found context '%s'\n", vars->data);
-       snprintf(name, sizeof(name), "%s", vars->data);
-       con = ast_context_create(local_contexts, name, registrar);
-       if (con) {
-               cur = argv;
-               while(cur) {
-                       if (matches_keyword(cur->data, "includes")) {
-                               if (aeldebug & DEBUG_CONTEXTS)
-                                       ast_verbose("--INCLUDES: '%s'\n", cur->data);
-                               parse_keyword(cur->data, &rest);
-                               if (rest) {
-                                       argv2 = arg_parse(rest, filename, lineno);
-                                       cur2 = argv2;
-                                       while(cur2) {
-                                               ast_context_add_include2(con, cur2->data, registrar);
-                                               cur2 = cur2->next;
-                                       }
-                                       arg_free(argv2);
-                               }
-                       } else if (matches_keyword(cur->data, "ignorepat")) {
-                               if (aeldebug & DEBUG_CONTEXTS)
-                                       ast_verbose("--IGNOREPAT: '%s'\n", cur->data);
-                               parse_keyword(cur->data, &rest);
-                               if (rest) {
-                                       argv2 = arg_parse(rest, filename, lineno);
-                                       cur2 = argv2;
-                                       while(cur2) {
-                                               ast_context_add_ignorepat2(con, cur2->data, registrar);
-                                               cur2 = cur2->next;
-                                       }
-                                       arg_free(argv2);
-                               }
-                       } else if (matches_keyword(cur->data, "switches") || matches_keyword(cur->data, "eswitches")) {
-                               if (aeldebug & DEBUG_CONTEXTS)
-                                       ast_verbose("--[E]SWITCH: '%s'\n", cur->data);
-                               parse_keyword(cur->data, &rest);
-                               if (rest) {
-                                       argv2 = arg_parse(rest, filename, lineno);
-                                       cur2 = argv2;
-                                       while(cur2) {
-                                               c = strchr(cur2->data, '/');
-                                               if (c) {
-                                                       *c = '\0';
-                                                       c++;
-                                               } else
-                                                       c = "";
-                                               ast_context_add_switch2(con, cur2->data, c, (cur->data[0] == 'e'), registrar);
-                                               cur2 = cur2->next;
-                                       }
-                                       arg_free(argv2);
-                               }
-                       } else if (matches_extension(cur->data, &rest)) {
-                               if (aeldebug & DEBUG_CONTEXTS)
-                                       ast_verbose("Extension: '%s' => '%s'\n", cur->data, rest);
-                               pos = 1;
-                               build_step("extension", cur->data, filename, lineno, con, cur->data, &pos, rest, NULL, NULL);
-                       }
-                       cur = cur->next;
-               }
-       } else
-                       ast_log(LOG_WARNING, "Unable to create context '%s'\n", name);
-       arg_free(argv);
-       if (vars->next)
-               ast_log(LOG_NOTICE, "Ignoring excess tokens in macro definition around line %d of %s!\n", lineno, filename);
-}
-
-static int handle_root_token(struct ast_context **local_contexts, char *token, int level, const char *filename, int lineno)
-{
-       struct stringlink *argv, *cur;
-       argv = split_token(token, filename, lineno);
-       if (aeldebug & DEBUG_TOKENS) {
-               ast_verbose("Found root token '%s' at level %d (%s:%d)!\n", token, level, filename, lineno);
-               cur = argv;
-               while(cur) {
-                       ast_verbose("   ARG => '%s'\n", cur->data);
-                       cur = cur->next;
-               }
-       }
-       if (!strcasecmp(token, "globals")) {
-               handle_globals(argv);
-       } else if (!strcasecmp(token, "macro")) {
-               handle_macro(local_contexts, argv, filename, lineno);
-       } else if (!strcasecmp(token, "context")) {
-               handle_context(local_contexts, argv, filename, lineno);
-       } else {
-               ast_log(LOG_NOTICE, "Unknown root token '%s'\n", token);
+       if (a->argc != 3)
+               return CLI_SHOWUSAGE;
+
+       if (!strcasecmp(a->argv[2], "read"))
+               aeldebug |= DEBUG_READ;
+       else if (!strcasecmp(a->argv[2], "tokens"))
+               aeldebug |= DEBUG_TOKENS;
+       else if (!strcasecmp(a->argv[2], "macros"))
+               aeldebug |= DEBUG_MACROS;
+       else if (!strcasecmp(a->argv[2], "contexts"))
+               aeldebug |= DEBUG_CONTEXTS;
+       else if (!strcasecmp(a->argv[2], "off"))
+               aeldebug = 0;
+       else
+               return CLI_SHOWUSAGE;
+
+       return CLI_SUCCESS;
+}
+
+static char *handle_cli_ael_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "ael set debug {read|tokens|macros|contexts|off}";
+               e->usage =
+                       "Usage: ael debug {read|tokens|macros|contexts|off}\n"
+                       "       Enable AEL read, token, macro, or context debugging,\n"
+                       "       or disable all AEL debugging messages.  Note: this\n"
+                       "       currently does nothing.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
        }
-       arg_free(argv);
-       return 0;
-}
 
-
-static int ast_ael_compile(struct ast_context **local_contexts, const char *filename)
-{
-       char *rfilename;
-       char *buf, *tbuf;
-       int bufsiz;
-       FILE *f;
-       char *c;
-       char *token;
-       int lineno=0;
-
-       if (filename[0] == '/')
-               rfilename = (char *)filename;
-       else {
-               rfilename = alloca(strlen(filename) + strlen(ast_config_AST_CONFIG_DIR) + 2);
-               sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, filename);
-       }
-       
-       f = fopen(rfilename, "r");
-       if (!f) {
-               ast_log(LOG_WARNING, "Unable to open '%s': %s\n", rfilename, strerror(errno));
-               return -1;
-       }
-       buf = malloc(4096);
-       if (!buf) {
-               ast_log(LOG_WARNING, "Out of memory!\n");
-               fclose(f);
-               return -1;
+       if (a->argc != e->args)
+               return CLI_SHOWUSAGE;
+
+       if (!strcasecmp(a->argv[3], "read"))
+               aeldebug |= DEBUG_READ;
+       else if (!strcasecmp(a->argv[3], "tokens"))
+               aeldebug |= DEBUG_TOKENS;
+       else if (!strcasecmp(a->argv[3], "macros"))
+               aeldebug |= DEBUG_MACROS;
+       else if (!strcasecmp(a->argv[3], "contexts"))
+               aeldebug |= DEBUG_CONTEXTS;
+       else if (!strcasecmp(a->argv[3], "off"))
+               aeldebug = 0;
+       else
+               return CLI_SHOWUSAGE;
+
+       return CLI_SUCCESS;
+}
+
+static char *handle_cli_ael_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "ael reload";
+               e->usage =
+                       "Usage: ael reload\n"
+                       "       Reloads AEL configuration.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
        }
-       buf[0] = 0;
-       bufsiz = 4096;
-       while(!feof(f)) {
-               if (bufsiz - strlen(buf) < 2048) {
-                       bufsiz += 4096;
-                       tbuf = realloc(buf, bufsiz);
-                       if (tbuf) {
-                               buf = tbuf;
-                       } else {
-                               free(buf);
-                               ast_log(LOG_WARNING, "Out of memory!\n");
-                               fclose(f);
-                       }
-               }
-               if (fgets(buf + strlen(buf), bufsiz - strlen(buf), f)) {
-                       lineno++;
-                       while(*buf && buf[strlen(buf) - 1] < 33)
-                               buf[strlen(buf) - 1] = '\0';
-                       c = strstr(buf, "//");
-                       if (c)
-                               *c = '\0';
-                       if (*buf) {
-                               if (aeldebug & DEBUG_READ)
-                                       ast_verbose("Newly composed line '%s'\n", buf);
-                               while((token = grab_token(buf, filename, lineno))) {
-                                       handle_root_token(local_contexts, token, 0, filename, lineno);
-                                       free(token);
-                               }
-                       }
-               }
-       };
-       free(buf);
-       fclose(f);
-       return 0;
-}
-
-static int pbx_load_module(void)
-{
-       struct ast_context *local_contexts=NULL, *con;
-       ast_ael_compile(&local_contexts, config);
-       ast_merge_contexts_and_delete(&local_contexts, registrar);
-       for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
-               ast_context_verify_includes(con);
-
-       return 0;
-}
-
-/* CLI interface */
-static int ael_debug_read(int fd, int argc, char *argv[])
-{
-       aeldebug |= DEBUG_READ;
-       return 0;
-}
-
-static int ael_debug_tokens(int fd, int argc, char *argv[])
-{
-       aeldebug |= DEBUG_TOKENS;
-       return 0;
-}
-
-static int ael_debug_macros(int fd, int argc, char *argv[])
-{
-       aeldebug |= DEBUG_MACROS;
-       return 0;
-}
 
-static int ael_debug_contexts(int fd, int argc, char *argv[])
-{
-       aeldebug |= DEBUG_CONTEXTS;
-       return 0;
-}
+       if (a->argc != 2)
+               return CLI_SHOWUSAGE;
 
-static int ael_no_debug(int fd, int argc, char *argv[])
-{
-       aeldebug = 0;
-       return 0;
-}
-
-static int ael_reload(int fd, int argc, char *argv[])
-{
-       ast_context_destroy(NULL, registrar);
-       return (pbx_load_module());
+       return (pbx_load_module() ? CLI_FAILURE : CLI_SUCCESS);
 }
 
-static struct ast_cli_entry  ael_cli[] = {
-       { { "ael", "reload", NULL }, ael_reload, "Reload AEL configuration"},
-       { { "ael", "debug", "read", NULL }, ael_debug_read, "Enable AEL read debug"},
-       { { "ael", "debug", "tokens", NULL }, ael_debug_tokens, "Enable AEL tokens debug"},
-       { { "ael", "debug", "macros", NULL }, ael_debug_macros, "Enable AEL macros debug"},
-       { { "ael", "debug", "contexts", NULL }, ael_debug_contexts, "Enable AEL contexts debug"},
-       { { "ael", "no", "debug", NULL }, ael_no_debug, "Disable AEL debug messages"},
+static struct ast_cli_entry cli_ael_debug_multiple_deprecated = AST_CLI_DEFINE(handle_cli_ael_debug_multiple_deprecated, "Enable AEL debugging flags");
+static struct ast_cli_entry cli_ael[] = {
+       AST_CLI_DEFINE(handle_cli_ael_reload,    "Reload AEL configuration"),
+       AST_CLI_DEFINE(handle_cli_ael_set_debug, "Enable AEL debugging flags", .deprecate_cmd = &cli_ael_debug_multiple_deprecated)
 };
 
-/*
- * Standard module functions ...
- */
-int unload_module(void)
+static int unload_module(void)
 {
        ast_context_destroy(NULL, registrar);
-       ast_cli_unregister_multiple(ael_cli, sizeof(ael_cli)/ sizeof(ael_cli[0]));
+       ast_cli_unregister_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
        return 0;
 }
 
-
-int load_module(void)
+static int load_module(void)
 {
-       ast_cli_register_multiple(ael_cli, sizeof(ael_cli)/ sizeof(ael_cli[0]));
+       ast_cli_register_multiple(cli_ael, sizeof(cli_ael) / sizeof(struct ast_cli_entry));
        return (pbx_load_module());
 }
 
-int reload(void)
+static int reload(void)
 {
-       unload_module();
-       return (load_module());
+       return pbx_load_module();
 }
 
-int usecount(void)
+#ifdef STANDALONE_AEL
+#define AST_MODULE "ael"
+int ael_external_load_module(void);
+int ael_external_load_module(void)
 {
-       return 0;
+        pbx_load_module();
+        return 1;
 }
+#endif
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Extension Language Compiler",
+               .load = load_module,
+               .unload = unload_module,
+               .reload = reload,
+              );
+
+#ifdef AAL_ARGCHECK
+static char *ael_funclist[] =
+{
+       "AGENT",
+       "ARRAY",
+       "BASE64_DECODE",
+       "BASE64_ENCODE",
+       "CALLERID",
+       "CDR",
+       "CHANNEL",
+       "CHECKSIPDOMAIN",
+       "CHECK_MD5",
+       "CURL",
+       "CUT",
+       "DB",
+       "DB_EXISTS",
+       "DUNDILOOKUP",
+       "ENUMLOOKUP",
+       "ENV",
+       "EVAL",
+       "EXISTS",
+       "FIELDQTY",
+       "FILTER",
+       "GROUP",
+       "GROUP_COUNT",
+       "GROUP_LIST",
+       "GROUP_MATCH_COUNT",
+       "IAXPEER",
+       "IF",
+       "IFTIME",
+       "ISNULL",
+       "KEYPADHASH",
+       "LANGUAGE",
+       "LEN",
+       "MATH",
+       "MD5",
+       "MUSICCLASS",
+       "QUEUEAGENTCOUNT",
+       "QUEUE_MEMBER_COUNT",
+       "QUEUE_MEMBER_LIST",
+       "QUOTE",
+       "RAND",
+       "REGEX",
+       "SET",
+       "SHA1",
+       "SIPCHANINFO",
+       "SIPPEER",
+       "SIP_HEADER",
+       "SORT",
+       "STAT",
+       "STRFTIME",
+       "STRPTIME",
+       "TIMEOUT",
+       "TXTCIDNAME",
+       "URIDECODE",
+       "URIENCODE",
+       "VMCOUNT"
+};
 
-char *description(void)
-{
-       return dtext;
-}
 
-char *key(void)
+int ael_is_funcname(char *name)
 {
-       return ASTERISK_GPL_KEY;
+       int s,t;
+       t = sizeof(ael_funclist)/sizeof(char*);
+       s = 0;
+       while ((s < t) && strcasecmp(name, ael_funclist[s])) 
+               s++;
+       if ( s < t )
+               return 1;
+       else
+               return 0;
 }
+#endif