formatting fixes (bug #4736)
[asterisk/asterisk.git] / config.c
index a0e2903..8b9e441 100755 (executable)
--- a/config.c
+++ b/config.c
@@ -3,9 +3,9 @@
  *
  * Configuration File Parser
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
 #include <string.h>
 #include <errno.h>
 #include <time.h>
-#include <asterisk/config.h>
-#include <asterisk/config_pvt.h>
-#include <asterisk/cli.h>
-#include <asterisk/lock.h>
-#include <asterisk/options.h>
-#include <asterisk/logger.h>
-#include <asterisk/utils.h>
+#define AST_INCLUDE_GLOB 1
+#ifdef AST_INCLUDE_GLOB
+#ifdef __Darwin__
+#define GLOB_ABORTED GLOB_ABEND
+#endif
+# include <glob.h>
+#endif
+
 #include "asterisk.h"
-#include "astconf.h"
 
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-static int ast_cust_config=0;
-struct ast_config *(*global_load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
-#ifdef PRESERVE_COMMENTS                                                                                                                                     
-,struct ast_comment_struct *
-#endif
-);
+#include "asterisk/config.h"
+#include "asterisk/cli.h"
+#include "asterisk/lock.h"
+#include "asterisk/options.h"
+#include "asterisk/logger.h"
+#include "asterisk/utils.h"
+#include "asterisk/channel.h"
+#include "asterisk/app.h"
 
-AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
-static struct ast_config_reg *ast_cust_config_list;
-static char *config_conf_file = "extconfig.conf";
+#define MAX_NESTED_COMMENTS 128
+#define COMMENT_START ";--"
+#define COMMENT_END "--;"
+#define COMMENT_META ';'
+#define COMMENT_TAG '-'
 
-static char *strip(char *buf)
-{
-       char *start;
-       /* Strip off trailing whitespace, returns, etc */
-       while (!ast_strlen_zero(buf) && (buf[strlen(buf)-1]<33))
-               buf[strlen(buf)-1] = '\0';
-       start = buf;
-       /* Strip off leading whitespace, returns, etc */
-       while (*start && (*start < 33))
-               *start++ = '\0';
-       return start;
-}
+static char *extconfig_conf = "extconfig.conf";
 
-#ifdef PRESERVE_COMMENTS
-static void free_comments(struct ast_comment *com)
-{
-       struct ast_comment *l;
-       while (com) {
-               l = com;
-               com = com->next;
-               free(l);
-       }
-}
-#endif
+static struct ast_config_map {
+       struct ast_config_map *next;
+       char *name;
+       char *driver;
+       char *database;
+       char *table;
+       char stuff[0];
+} *config_maps = NULL;
 
-void ast_destroy(struct ast_config *ast)
-{
-       struct ast_category *cat, *catn;
-       struct ast_variable *v, *vn;
+AST_MUTEX_DEFINE_STATIC(config_lock);
+static struct ast_config_engine *config_engine_list;
 
-       if (!ast)
-               return;
+#define MAX_INCLUDE_LEVEL 10
 
-       cat = ast->root;
-       while(cat) {
-               v = cat->root;
-               while(v) {
-                       vn = v;
-                       free(v->name);
-                       free(v->value);
-#ifdef PRESERVE_COMMENTS
-                       free_comments(v->precomments);
-                       free_comments(v->sameline);
-#endif                 
-                       v = v->next;
-                       free(vn);
-               }
-               catn = cat;
-#ifdef PRESERVE_COMMENTS
-               free_comments(cat->precomments);
-               free_comments(cat->sameline);
-#endif         
-               cat = cat->next;
-               free(catn);
+struct ast_comment {
+       struct ast_comment *next;
+       char cmt[0];
+};
+
+struct ast_category {
+       char name[80];
+       int ignored;                    /* do not let user of the config see this category */
+       struct ast_variable *root;
+       struct ast_variable *last;
+       struct ast_category *next;
+};
+
+struct ast_config {
+       struct ast_category *root;
+       struct ast_category *last;
+       struct ast_category *current;
+       struct ast_category *last_browse;               /* used to cache the last category supplied via category_browse */
+       int include_level;
+       int max_include_level;
+};
+
+struct ast_variable *ast_variable_new(const char *name, const char *value) 
+{
+       struct ast_variable *variable;
+
+       int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
+       variable = malloc(length);
+       if (variable) {
+               memset(variable, 0, length);
+               variable->name = variable->stuff;
+               variable->value = variable->stuff + strlen(name) + 1;           
+               strcpy(variable->name,name);
+               strcpy(variable->value,value);
        }
-#ifdef PRESERVE_COMMENTS
-       free_comments(ast->trailingcomments);
-#endif 
-       free(ast);
+
+       return variable;
 }
 
-int ast_true(char *s)
+void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
 {
-       if (!s)
-               return 0;
-       /* Determine if this is a true value */
-       if (!strcasecmp(s, "yes") ||
-           !strcasecmp(s, "true") ||
-               !strcasecmp(s, "y") ||
-               !strcasecmp(s, "t") ||
-               !strcasecmp(s, "1") ||
-               !strcasecmp(s, "on"))
-                       return -1;
-       return 0;
+       if (category->last)
+               category->last->next = variable;
+       else
+               category->root = variable;
+       category->last = variable;
 }
 
-int ast_false(char *s)
+void ast_variables_destroy(struct ast_variable *v)
 {
-       if (!s)
-               return 0;
-       /* Determine if this is a false value */
-       if (!strcasecmp(s, "no") ||
-           !strcasecmp(s, "false") ||
-               !strcasecmp(s, "n") ||
-               !strcasecmp(s, "f") ||
-               !strcasecmp(s, "0") ||
-               !strcasecmp(s, "off"))
-                       return -1;
-       return 0;
+       struct ast_variable *vn;
+
+       while(v) {
+               vn = v;
+               v = v->next;
+               free(vn);
+       }
 }
 
-struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
+struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
 {
-       struct ast_category *cat;
-       cat = config->root;
-       while(cat) {
-               if (cat->name == category)
-                       return cat->root;
-               cat = cat->next;
-       }
-       cat = config->root;
-       while(cat) {
-               if (!strcasecmp(cat->name, category))
-                       return cat->root;
-               cat = cat->next;
-       }
-       return NULL;
+       struct ast_category *cat = NULL;
+
+       if (category && config->last_browse && (config->last_browse->name == category))
+               cat = config->last_browse;
+       else
+               cat = ast_category_get(config, category);
+
+       if (cat)
+               return cat->root;
+       else
+               return NULL;
 }
 
-char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
+char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
 {
        struct ast_variable *v;
+
        if (category) {
-               v = ast_variable_browse(config, category);
-               while (v) {
-                       if (value == v->name)
+               for (v = ast_variable_browse(config, category); v; v = v->next)
+                       if (variable == v->name)
                                return v->value;
-                       v=v->next;
-               }
-               v = ast_variable_browse(config, category);
-               while (v) {
-                       if (!strcasecmp(value, v->name))
+               for (v = ast_variable_browse(config, category); v; v = v->next)
+                       if (!strcasecmp(variable, v->name))
                                return v->value;
-                       v=v->next;
-               }
        } else {
                struct ast_category *cat;
-               cat = config->root;
-               while(cat) {
-                       v = cat->root;
-                       while (v) {
-                               if (!strcasecmp(value, v->name))
+
+               for (cat = config->root; cat; cat = cat->next)
+                       for (v = cat->root; v; v = v->next)
+                               if (!strcasecmp(variable, v->name))
                                        return v->value;
-                               v=v->next;
-                       }
-                       cat = cat->next;
-               }
        }
+
        return NULL;
 }
 
-#ifdef PRESERVE_COMMENTS
-int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
+static struct ast_variable *variable_clone(const struct ast_variable *old)
 {
-       struct ast_variable *v, *pv, *bv, *bpv;
-       struct ast_category *cat;
-       cat = cfg->root;
-       while(cat) {
-               if (cat->name == category) {
-                       break;
-               }
-               cat = cat->next;
-       }
-       if (!cat) {
-               cat = cfg->root;
-               while(cat) {
-                       if (!strcasecmp(cat->name, category)) {
-                               break;
-                       }
-                       cat = cat->next;
-               }
-       }
-       if (!cat)
-               return -1;
-       v = cat->root;
-       pv = NULL;
-       while (v) {
-               if ((variable == v->name) && (!value || !strcmp(v->value, value)))
-                       break;
-               pv = v;
-               v=v->next;
+       struct ast_variable *new = ast_variable_new(old->name, old->value);
+
+       if (new) {
+               new->lineno = old->lineno;
+               new->object = old->object;
+               new->blanklines = old->blanklines;
+               /* TODO: clone comments? */
        }
-       if (!v) {
-               /* Get the last one that looks like it */
-               bv = NULL;
-               bpv = NULL;
-               v = cat->root;
-               pv = NULL;
-               while (v) {
-                       if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
-                               bv = v;
-                               bpv = pv;
-                       }
-                       pv = v;
-                       v=v->next;
-               }
-               v = bv;
+
+       return new;
+}
+static void move_variables(struct ast_category *old, struct ast_category *new)
+{
+       struct ast_variable *var;
+       struct ast_variable *next;
+
+       next = old->root;
+       old->root = NULL;
+       for (var = next; var; var = next) {
+               next = var->next;
+               var->next = NULL;
+               ast_variable_append(new, var);
        }
+}
 
-       if (v) {
-               /* Unlink from original position */
-               if (pv) 
-                       pv->next = v->next;
-               else
-                       cat->root = v->next;
-               v->next = NULL;
-               free(v->name);
-               if (v->value)
-                       free(v->value);
-               free_comments(v->sameline);
-               free_comments(v->precomments);
-               return 0;
+struct ast_category *ast_category_new(const char *name) 
+{
+       struct ast_category *category;
+
+       category = malloc(sizeof(struct ast_category));
+       if (category) {
+               memset(category, 0, sizeof(struct ast_category));
+               ast_copy_string(category->name, name, sizeof(category->name));
        }
-       return -1;
+
+       return category;
 }
 
-int ast_category_delete(struct ast_config *cfg, char *category)
+static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
 {
-       struct ast_variable *v, *pv;
-       struct ast_category *cat, *cprev;
-       cat = cfg->root;
-       cprev = NULL;
-       while(cat) {
-               if (cat->name == category) {
-                       break;
-               }
-               cprev = cat;
-               cat = cat->next;
+       struct ast_category *cat;
+
+       for (cat = config->root; cat; cat = cat->next) {
+               if (cat->name == category_name && (ignored || !cat->ignored))
+                       return cat;
        }
-       if (!cat) {
-               cat = cfg->root;
-               cprev = NULL;
-               while(cat) {
-                       if (!strcasecmp(cat->name, category)) {
-                               break;
-                       }
-                       cprev = cat;
-                       cat = cat->next;
-               }
+
+       for (cat = config->root; cat; cat = cat->next) {
+               if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
+                       return cat;
        }
-       if (!cat)
-               return -1;
-       /* Unlink it */
-       if (cprev)
-               cprev->next = cat->next;
+
+       return NULL;
+}
+
+struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
+{
+       return category_get(config, category_name, 0);
+}
+
+int ast_category_exist(const struct ast_config *config, const char *category_name)
+{
+       return !!ast_category_get(config, category_name);
+}
+
+void ast_category_append(struct ast_config *config, struct ast_category *category)
+{
+       if (config->last)
+               config->last->next = category;
        else
-               cfg->root = cat->next;
-       v = cat->root;
-       while (v) {
-               pv = v;
-               v=v->next;
-               if (pv->value)
-                       free(pv->value);
-               if (pv->name)
-                       free(pv->name);
-               free_comments(pv->sameline);
-               free_comments(pv->precomments);
-               free(pv);
-       }
-       free_comments(cat->sameline);
-       free_comments(cat->precomments);
+               config->root = category;
+       config->last = category;
+       config->current = category;
+}
+
+void ast_category_destroy(struct ast_category *cat)
+{
+       ast_variables_destroy(cat->root);
        free(cat);
-       return 0;
 }
 
-struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
+static struct ast_category *next_available_category(struct ast_category *cat)
 {
-       struct ast_variable *v, *pv=NULL, *bv, *bpv;
-       struct ast_category *cat, *pcat;
-       cat = config->root;
-       if (!newcat) {
-               while(cat) {
-                       if (cat->name == category) {
+       for (; cat && cat->ignored; cat = cat->next);
+
+       return cat;
+}
+
+char *ast_category_browse(struct ast_config *config, const char *prev)
+{      
+       struct ast_category *cat = NULL;
+
+       if (prev && config->last_browse && (config->last_browse->name == prev))
+               cat = config->last_browse->next;
+       else if (!prev && config->root)
+                       cat = config->root;
+       else if (prev) {
+               for (cat = config->root; cat; cat = cat->next) {
+                       if (cat->name == prev) {
+                               cat = cat->next;
                                break;
                        }
-                       cat = cat->next;
                }
                if (!cat) {
-                       cat = config->root;
-                       while(cat) {
-                               if (!strcasecmp(cat->name, category)) {
+                       for (cat = config->root; cat; cat = cat->next) {
+                               if (!strcasecmp(cat->name, prev)) {
+                                       cat = cat->next;
                                        break;
                                }
-                               cat = cat->next;
                        }
                }
        }
-       if (!cat) {
-               cat = malloc(sizeof(struct ast_category));
-               if (!cat)
-                       return NULL;
-               memset(cat, 0, sizeof(struct ast_category));
-               strncpy(cat->name, category, sizeof(cat->name) - 1);
-               if (config->root) {
-                       /* Put us at the end */
-                       pcat = config->root;
-                       while(pcat->next)
-                               pcat = pcat->next;
-                       pcat->next = cat;
-               } else {
-                       /* We're the first one */
-                       config->root = cat;
-               }
-                       
-       }
-       if (!newvar) {
-               v = cat->root;
-               pv = NULL;
-               while (v) {
-                       if (variable == v->name)
-                               break;
-                       pv = v;
-                       v=v->next;
-               }
-               if (!v) {
-                       /* Get the last one that looks like it */
-                       bv = NULL;
-                       bpv = NULL;
-                       v = cat->root;
-                       pv = NULL;
-                       while (v) {
-                               if (!strcasecmp(variable, v->name)) {
-                                       bv = v;
-                                       bpv = pv;
-                               }
-                               pv = v;
-                               v=v->next;
-                       }
-                       v = bv;
-               }
-       } else v = NULL;
-       if (v && move) {
-               /* Unlink from original position */
-               if (pv) 
-                       pv->next = v->next;
-               else
-                       cat->root = v->next;
-               v->next = NULL;
-       }
-       if (!v) {
-               v = malloc(sizeof(struct ast_variable));
-               if (!v)
-                       return NULL;
-               memset(v, 0, sizeof(struct ast_variable));
-               v->name = strdup(variable);
-               move = 1;
-       }
-       if (v->value)
-               free(v->value);
-       if (value)
-               v->value = strdup(value);
+       
+       if (cat)
+               cat = next_available_category(cat);
+
+       config->last_browse = cat;
+       if (cat)
+               return cat->name;
        else
-               v->value = strdup("");
-       if (move) {
-               if (cat->root) {
-                       pv = cat->root;
-                       while (pv->next) 
-                               pv = pv->next;
-                       pv->next = v;
-               } else {
-                       cat->root = v;
-               }
-       }
+               return NULL;
+}
+
+struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
+{
+       struct ast_variable *v;
+
+       v = cat->root;
+       cat->root = NULL;
+
        return v;
 }
-#endif         
 
-int ast_category_exist(struct ast_config *config, char *category_name)
+void ast_category_rename(struct ast_category *cat, const char *name)
 {
-       struct ast_category *category = NULL;
+       ast_copy_string(cat->name, name, sizeof(cat->name));
+}
 
-       category = config->root;
+static void inherit_category(struct ast_category *new, const struct ast_category *base)
+{
+       struct ast_variable *var;
 
-       while(category) {
-               if (!strcasecmp(category->name,category_name)) 
-                       return 1;
-               category = category->next;
-       } 
+       for (var = base->root; var; var = var->next) {
+               struct ast_variable *v;
+               
+               v = variable_clone(var);
+               if (v)
+                       ast_variable_append(new, v);
+       }
+}
 
-       return 0;
+struct ast_config *ast_config_new(void) 
+{
+       struct ast_config *config;
+
+       config = malloc(sizeof(*config));
+       if (config) {
+               memset(config, 0, sizeof(*config));
+               config->max_include_level = MAX_INCLUDE_LEVEL;
+       }
+
+       return config;
 }
 
-#ifdef PRESERVE_COMMENTS
-static struct ast_comment *build_comment(char *cmt)
+void ast_config_destroy(struct ast_config *cfg)
 {
-       struct ast_comment *c;
-       int len = strlen(cmt) + 1;
-       c = malloc(sizeof(struct ast_comment) + len);
-       if (c) {
-               /* Memset the header */
-               memset(c, 0, sizeof(struct ast_comment));
-               /* Copy the rest */
-               strcpy(c->cmt, cmt);
+       struct ast_category *cat, *catn;
+
+       if (!cfg)
+               return;
+
+       cat = cfg->root;
+       while(cat) {
+               ast_variables_destroy(cat->root);
+               catn = cat;
+               cat = cat->next;
+               free(catn);
        }
-       return c;
+       free(cfg);
 }
-#endif
 
-static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
-#ifdef PRESERVE_COMMENTS
-, struct ast_comment_struct *acs
-#endif
-);
+struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
+{
+       return cfg->current;
+}
 
-static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel 
-#ifdef PRESERVE_COMMENTS
-,struct ast_comment_struct *acs
-#endif
-)
+void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
+{
+       /* cast below is just to silence compiler warning about dropping "const" */
+       cfg->current = (struct ast_category *) cat;
+}
+
+static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
 {
        char *c;
-       char *cur;
-       char *arg=NULL;
-       struct ast_config_reg *reg=NULL;
-       struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
-#ifdef PRESERVE_COMMENTS
-,struct ast_comment_struct *
-#endif
-       );
+       char *cur = buf;
        struct ast_variable *v;
-#ifdef PRESERVE_COMMENTS
-       struct ast_comment *com = NULL;
-#endif 
-       int object;
-       /* Strip off lines using ; as comment */
-       c = strchr(buf, ';');
-       if (c) {
-               *c = '\0';
-#ifdef PRESERVE_COMMENTS
-               c++;
-               if (*c != '!')
-                       com = build_comment(c);
-#endif                 
-       }
-       cur = strip(buf);
-       if (!ast_strlen_zero(cur)) {
-               /* Actually parse the entry */
-               if (cur[0] == '[') {
-                       /* A category header */
-                       c = strchr(cur, ']');
-                       if (c) {
-                               *c = 0;
-                               *_tmpc = malloc(sizeof(struct ast_category));
-                               if (!*_tmpc) {
-                                       ast_destroy(tmp);
-                                       ast_log(LOG_WARNING,
-                                               "Out of memory, line %d\n", lineno);
-                                       return -1;
-                               }
-                               memset(*_tmpc, 0, sizeof(struct ast_category));
-                               strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
-                               (*_tmpc)->root =  NULL;
-#ifdef PRESERVE_COMMENTS
-                               (*_tmpc)->precomments = acs->root;
-                               (*_tmpc)->sameline = com;
-#endif                         
-                               if (!tmp->prev)
-                                       tmp->root = *_tmpc;
-                               else
-                                       tmp->prev->next = *_tmpc;
-
-                               tmp->prev = *_tmpc;
-#ifdef PRESERVE_COMMENTS
-                               acs->root = NULL;
-                               acs->prev = NULL;
-#endif                         
-                               *_last =  NULL;
-                       } else {
-                               ast_log(LOG_WARNING, 
-                                       "parse error: no closing ']', line %d of %s\n", lineno, configfile);
-                       }
-               } else if (cur[0] == '#') {
-                       /* A directive */
-                       cur++;
-                       c = cur;
-                       while(*c && (*c > 32)) c++;
-                       if (*c) {
-                               *c = '\0';
-                               c++;
-                               /* Find real argument */
-                               while(*c  && (*c < 33)) c++;
-                               if (!*c)
-                                       c = NULL;
-                       } else 
-                               c = NULL;
-                       if (!strcasecmp(cur, "include")) {
-                               /* A #include */
-                               if (c) {
-                                       while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
-                                       /* Get rid of leading mess */
-                                       cur = c;
-                                       while (!ast_strlen_zero(cur)) {
-                                               c = cur + strlen(cur) - 1;
-                                               if ((*c == '>') || (*c == '<') || (*c == '\"'))
-                                                       *c = '\0';
-                                               else
-                                                       break;
+       char cmd[512], exec_file[512];
+       int object, do_exec, do_include;
+
+       /* Actually parse the entry */
+       if (cur[0] == '[') {
+               struct ast_category *newcat = NULL;
+               char *catname;
+
+               /* A category header */
+               c = strchr(cur, ']');
+               if (!c) {
+                       ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
+                       return -1;
+               }
+               *c++ = '\0';
+               cur++;
+               if (*c++ != '(')
+                       c = NULL;
+               catname = cur;
+               *cat = newcat = ast_category_new(catname);
+               if (!newcat) {
+                       ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
+                       return -1;
+               }
+               /* If there are options or categories to inherit from, process them now */
+               if (c) {
+                       if (!(cur = strchr(c, ')'))) {
+                               ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
+                               return -1;
+                       }
+                       *cur = '\0';
+                       while ((cur = strsep(&c, ","))) {
+                               if (!strcasecmp(cur, "!")) {
+                                       (*cat)->ignored = 1;
+                               } else if (!strcasecmp(cur, "+")) {
+                                       *cat = category_get(cfg, catname, 1);
+                                       if (!*cat) {
+                                               ast_destroy(cfg);
+                                               if (newcat)
+                                                       ast_category_destroy(newcat);
+                                               ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
+                                               return -1;
                                        }
-                                       
-                                       if((c = strchr(cur,':'))) {
-                                               *c = '\0';
-                                               c++;
-                                               arg = c;
+                                       if (newcat) {
+                                               move_variables(newcat, *cat);
+                                               ast_category_destroy(newcat);
+                                               newcat = NULL;
                                        }
-                                       
-                                       if (includelevel < MAX_INCLUDE_LEVEL) {
-                                               if(arg && cur) {
-                                                       load_func = NULL;
-                                                       if(ast_cust_config_list)
-                                                               reg = get_ast_cust_config(cur);
-                                                       if(reg && reg->func)
-                                                               load_func = reg->func;
-                                                       if(load_func) { 
-                                                               ast_log(LOG_NOTICE,"External Include '%s' via '%s' config engine\n",arg,cur);
-                                                               load_func(arg,tmp, _tmpc, _last, includelevel
-#ifdef PRESERVE_COMMENTS
-                                                                                 ,&acs
-#endif
-                                                                                 );
-                                                       }
-                                                       else 
-                                                               ast_log(LOG_WARNING,"Cant Find Registered Config Engine [%s] for [%s]\n",cur,arg);
-                                               }
-                                               else {
-                                                       __ast_load(cur, tmp, _tmpc, _last, includelevel + 1
-#ifdef PRESERVE_COMMENTS
-                                                                          ,acs
-#endif
-                                                                          );
-                                               }
-                                       } else
-                                               ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
-                               } else
-                                       ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
-                               /* Strip off leading and trailing "'s and <>'s */
-                       }
-                       else 
-                               ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
-               } else {
-                       /* Just a line (variable = value) */
-                       if (!*_tmpc) {
-                               ast_log(LOG_WARNING,
-                                       "parse error: No category context for line %d of %s\n", lineno, configfile);
-                               ast_destroy(tmp);
-                               return -1;
-                       }
-                       c = strchr(cur, '=');
+                               } else {
+                                       struct ast_category *base;
+                               
+                                       base = category_get(cfg, cur, 1);
+                                       if (!base) {
+                                               ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
+                                               return -1;
+                                       }
+                                       inherit_category(*cat, base);
+                               }
+                       }
+               }
+               if (newcat)
+                       ast_category_append(cfg, *cat);
+       } else if (cur[0] == '#') {
+               /* A directive */
+               cur++;
+               c = cur;
+               while(*c && (*c > 32)) c++;
+               if (*c) {
+                       *c = '\0';
+                       c++;
+                       /* Find real argument */
+                       while(*c  && (*c < 33)) c++;
+                       if (!*c)
+                               c = NULL;
+               } else 
+                       c = NULL;
+               do_include = !strcasecmp(cur, "include");
+               if(!do_include)
+                       do_exec = !strcasecmp(cur, "exec");
+               else
+                       do_exec = 0;
+               if (do_exec && !option_exec_includes) {
+                       ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
+                       do_exec = 0;
+               }
+               if (do_include || do_exec) {
                        if (c) {
-                               *c = 0;
-                               c++;
-                               /* Ignore > in => */
-                               if (*c== '>') {
-                                       object = 1;
-                                       c++;
-                               } else
-                                       object = 0;
-                               v = malloc(sizeof(struct ast_variable));
-                               if (v) {
-                                       memset(v, 0, sizeof(struct ast_variable));
-                                       v->next = NULL;
-                                       v->name = strdup(strip(cur));
-                                       v->value = strdup(strip(c));
-                                       v->lineno = lineno;
-                                       v->object = object;
-                                       /* Put and reset comments */
-#ifdef PRESERVE_COMMENTS
-                                       v->precomments = acs->root;
-                                       v->sameline = com;
-                                       acs->prev = NULL;
-                                       acs->root = NULL;
-#endif                                 
-                                       v->blanklines = 0;
-                                       if (*_last)
-                                               (*_last)->next = v;
+                               /* Strip off leading and trailing "'s and <>'s */
+                               while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
+                               /* Get rid of leading mess */
+                               cur = c;
+                               while (!ast_strlen_zero(cur)) {
+                                       c = cur + strlen(cur) - 1;
+                                       if ((*c == '>') || (*c == '<') || (*c == '\"'))
+                                               *c = '\0';
                                        else
-                                               (*_tmpc)->root = v;
-                                       *_last = v;
-                               } else {
-                                       ast_destroy(tmp);
-                                       ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
-                                       return -1;
+                                               break;
                                }
+                               /* #exec </path/to/executable>
+                                  We create a tmp file, then we #include it, then we delete it. */
+                               if (do_exec) { 
+                                       snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%ld.%ld", time(NULL), (long)pthread_self());
+                                       snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
+                                       ast_safe_system(cmd);
+                                       cur = exec_file;
+                               } else
+                                       exec_file[0] = '\0';
+                               /* A #include */
+                               do_include = ast_config_internal_load(cur, cfg) ? 1 : 0;
+                               if(!ast_strlen_zero(exec_file))
+                                       unlink(exec_file);
+                               if(!do_include)
+                                       return 0;
+
                        } else {
-                               ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
+                               ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
+                                               do_exec ? "exec" : "include",
+                                               do_exec ? "/path/to/executable" : "filename",
+                                               lineno,
+                                               configfile);
                        }
-                                                                                                               
                }
+               else 
+                       ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
        } else {
-               /* store any comments if there are any */
-#ifdef PRESERVE_COMMENTS
-               if (com) {
-                       if (acs->prev)
-                               acs->prev->next = com;
-                       else
-                               acs->root = com;
-                       acs->prev = com;
+               /* Just a line (variable = value) */
+               if (!*cat) {
+                       ast_log(LOG_WARNING,
+                               "parse error: No category context for line %d of %s\n", lineno, configfile);
+                       return -1;
+               }
+               c = strchr(cur, '=');
+               if (c) {
+                       *c = 0;
+                       c++;
+                       /* Ignore > in => */
+                       if (*c== '>') {
+                               object = 1;
+                               c++;
+                       } else
+                               object = 0;
+                       v = ast_variable_new(ast_strip(cur), ast_strip(c));
+                       if (v) {
+                               v->lineno = lineno;
+                               v->object = object;
+                               /* Put and reset comments */
+                               v->blanklines = 0;
+                               ast_variable_append(*cat, v);
+                       } else {
+                               ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
+                               return -1;
+                       }
                } else {
-                       if (*_last) 
-                               (*_last)->blanklines++;
+                       ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
                }
-#endif
+
        }
        return 0;
 }
 
-#ifdef PRESERVE_COMMENTS
-static void dump_comments(FILE *f, struct ast_comment *comment)
+static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg)
 {
-       while (comment) {
-               fprintf(f, ";%s", comment->cmt);
-               comment = comment->next;
+       char fn[256];
+       char buf[8192];
+       char *new_buf, *comment_p, *process_buf;
+       FILE *f;
+       int lineno=0;
+       int comment = 0, nest[MAX_NESTED_COMMENTS];
+       struct ast_category *cat = NULL;
+       int count = 0;
+       
+       cat = ast_config_get_current_category(cfg);
+
+       if (filename[0] == '/') {
+               ast_copy_string(fn, filename, sizeof(fn));
+       } else {
+               snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
        }
-}
+
+#ifdef AST_INCLUDE_GLOB
+       {
+               int glob_ret;
+               glob_t globbuf;
+               globbuf.gl_offs = 0;    /* initialize it to silence gcc */
+#ifdef SOLARIS
+               glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
+#else
+               glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
+#endif
+               if (glob_ret == GLOB_NOSPACE)
+                       ast_log(LOG_WARNING,
+                               "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
+               else if (glob_ret  == GLOB_ABORTED)
+                       ast_log(LOG_WARNING,
+                               "Glob Expansion of pattern '%s' failed: Read error\n", fn);
+               else  {
+                       /* loop over expanded files */
+                       int i;
+                       for (i=0; i<globbuf.gl_pathc; i++) {
+                               ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
+#endif
+       if ((option_verbose > 1) && !option_debug) {
+               ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
+               fflush(stdout);
+       }
+       if ((f = fopen(fn, "r"))) {
+               count++;
+               if (option_debug)
+                       ast_log(LOG_DEBUG, "Parsing %s\n", fn);
+               else if (option_verbose > 1)
+                       ast_verbose("Found\n");
+               while(!feof(f)) {
+                       lineno++;
+                       if (fgets(buf, sizeof(buf), f)) {
+                               new_buf = buf;
+                               if (comment)
+                                       process_buf = NULL;
+                               else
+                                       process_buf = buf;
+                               while ((comment_p = strchr(new_buf, COMMENT_META))) {
+                                       if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
+                                               /* Yuck, gotta memmove */
+                                               memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
+                                               new_buf = comment_p;
+                                       } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
+                                               /* Meta-Comment start detected ";--" */
+                                               if (comment < MAX_NESTED_COMMENTS) {
+                                                       *comment_p = '\0';
+                                                       new_buf = comment_p + 3;
+                                                       comment++;
+                                                       nest[comment-1] = lineno;
+                                               } else {
+                                                       ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
+                                               }
+                                       } else if ((comment_p >= new_buf + 2) &&
+                                                  (*(comment_p - 1) == COMMENT_TAG) &&
+                                                  (*(comment_p - 2) == COMMENT_TAG)) {
+                                               /* Meta-Comment end detected */
+                                               comment--;
+                                               new_buf = comment_p + 1;
+                                               if (!comment) {
+                                                       /* Back to non-comment now */
+                                                       if (process_buf) {
+                                                               /* Actually have to move what's left over the top, then continue */
+                                                               char *oldptr;
+                                                               oldptr = process_buf + strlen(process_buf);
+                                                               memmove(oldptr, new_buf, strlen(new_buf) + 1);
+                                                               new_buf = oldptr;
+                                                       } else
+                                                               process_buf = new_buf;
+                                               }
+                                       } else {
+                                               if (!comment) {
+                                                       /* If ; is found, and we are not nested in a comment, 
+                                                          we immediately stop all comment processing */
+                                                       *comment_p = '\0'; 
+                                                       new_buf = comment_p;
+                                               } else
+                                                       new_buf = comment_p + 1;
+                                       }
+                               }
+                               if (process_buf) {
+                                       char *buf = ast_strip(process_buf);
+                                       if (!ast_strlen_zero(buf))
+                                               if (process_text_line(cfg, &cat, buf, lineno, filename)) {
+                                                       cfg = NULL;
+                                                       break;
+                                               }
+                               }
+                       }
+               }
+               fclose(f);              
+       } else { /* can't open file */
+               if (option_debug)
+                       ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
+               else if (option_verbose > 1)
+                       ast_verbose( "Not found (%s)\n", strerror(errno));
+       }
+       if (comment) {
+               ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
+       }
+#ifdef AST_INCLUDE_GLOB
+                                       if (!cfg)
+                                               break;
+                               }
+                               globfree(&globbuf);
+                       }
+               }
 #endif
+       if (count == 0)
+               return NULL;
+
+       return cfg;
+}
 
-int ast_save(char *configfile, struct ast_config *cfg, char *generator)
+int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
 {
        FILE *f;
        char fn[256];
@@ -653,13 +661,14 @@ int ast_save(char *configfile, struct ast_config *cfg, char *generator)
        struct ast_variable *var;
        struct ast_category *cat;
        int blanklines = 0;
+
        if (configfile[0] == '/') {
-               strncpy(fn, configfile, sizeof(fn)-1);
+               ast_copy_string(fn, configfile, sizeof(fn));
        } else {
-               snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
+               snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
        }
        time(&t);
-       strncpy(date, ctime(&t), sizeof(date) - 1);
+       ast_copy_string(date, ctime(&t), sizeof(date));
        if ((f = fopen(fn, "w"))) {
                if ((option_verbose > 1) && !option_debug)
                        ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
@@ -671,32 +680,18 @@ int ast_save(char *configfile, struct ast_config *cfg, char *generator)
                fprintf(f, ";!\n");
                cat = cfg->root;
                while(cat) {
-#ifdef PRESERVE_COMMENTS
-                       /* Dump any precomments */
-                       dump_comments(f, cat->precomments);
-#endif
                        /* Dump section with any appropriate comment */
-#ifdef PRESERVE_COMMENTS
-                       if (cat->sameline) 
-                               fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->cmt);
-                       else
-#endif
-                               fprintf(f, "[%s]\n", cat->name);
+                       fprintf(f, "[%s]\n", cat->name);
                        var = cat->root;
                        while(var) {
-#ifdef PRESERVE_COMMENTS
-                               dump_comments(f, var->precomments);
-#endif                         
                                if (var->sameline) 
                                        fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
                                else    
                                        fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
                                if (var->blanklines) {
                                        blanklines = var->blanklines;
-                                       while (blanklines) {
+                                       while (blanklines--)
                                                fprintf(f, "\n");
-                                               blanklines--;
-                                       }
                                }
                                        
                                var = var->next;
@@ -707,9 +702,6 @@ int ast_save(char *configfile, struct ast_config *cfg, char *generator)
 #endif
                        cat = cat->next;
                }
-#ifdef PRESERVE_COMMENTS
-               dump_comments(f, cfg->trailingcomments);
-#endif         
        } else {
                if (option_debug)
                        printf("Unable to open for writing: %s\n", fn);
@@ -721,382 +713,324 @@ int ast_save(char *configfile, struct ast_config *cfg, char *generator)
        return 0;
 }
 
-static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
-#ifdef PRESERVE_COMMENTS
-, struct ast_comment_struct *acs
-#endif
-)
+static void clear_config_maps(void) 
 {
-       char fn[256];
-       char buf[8192];
-       FILE *f;
-       int lineno=0;
-       int master=0;
-       struct ast_config_reg *reg=NULL;
-       struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
-#ifdef PRESERVE_COMMENTS
-,struct ast_comment_struct *
-#endif
-);
+       struct ast_config_map *map;
 
-       load_func=NULL;
-       if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
-               if (global_load_func) {
-                       load_func = global_load_func;
-               } else {
-                       reg = get_ast_cust_config_keyword(configfile);
-                       if (reg && reg->func) {
-                               load_func = reg->func;
-                       } else {
-                               reg = get_ast_cust_config_keyword("global");
-                               if (reg && reg->func)
-                                       global_load_func = load_func = reg->func;
-                       }
-               }
+       ast_mutex_lock(&config_lock);
 
-               if (load_func) {
-                       ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
-                       tmp = load_func(configfile,tmp, _tmpc, _last, includelevel
-#ifdef PRESERVE_COMMENTS
-,&acs
-#endif
-);
-           
-                       if (tmp)
-                               return tmp;
-               }
+       while (config_maps) {
+               map = config_maps;
+               config_maps = config_maps->next;
+               free(map);
        }
+               
+       ast_mutex_unlock(&config_lock);
+}
 
-       if (configfile[0] == '/') {
-               strncpy(fn, configfile, sizeof(fn)-1);
-       } else {
-               snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
+static int append_mapping(char *name, char *driver, char *database, char *table)
+{
+       struct ast_config_map *map;
+       int length;
+
+       length = sizeof(*map);
+       length += strlen(name) + 1;
+       length += strlen(driver) + 1;
+       length += strlen(database) + 1;
+       if (table)
+               length += strlen(table) + 1;
+       map = malloc(length);
+
+       if (!map)
+               return -1;
+
+       memset(map, 0, length);
+       map->name = map->stuff;
+       strcpy(map->name, name);
+       map->driver = map->name + strlen(map->name) + 1;
+       strcpy(map->driver, driver);
+       map->database = map->driver + strlen(map->driver) + 1;
+       strcpy(map->database, database);
+       if (table) {
+               map->table = map->database + strlen(map->database) + 1;
+               strcpy(map->table, table);
        }
-       if ((option_verbose > 1) && !option_debug) {
-               ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
-               fflush(stdout);
+       map->next = config_maps;
+
+       if (option_verbose > 1)
+               ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
+                           map->name, map->driver, map->database, map->table ? map->table : map->name);
+
+       config_maps = map;
+       return 0;
+}
+
+void read_config_maps(void) 
+{
+       struct ast_config *config, *configtmp;
+       struct ast_variable *v;
+       char *driver, *table, *database, *stringp;
+
+       clear_config_maps();
+
+       configtmp = ast_config_new();
+       configtmp->max_include_level = 1;
+       config = ast_config_internal_load(extconfig_conf, configtmp);
+       if (!config) {
+               ast_config_destroy(configtmp);
+               return;
        }
-       if ((f = fopen(fn, "r"))) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "Parsing %s\n", fn);
-               else if (option_verbose > 1)
-                       ast_verbose( "Found\n");
-               if (!tmp) {
-                       tmp = malloc(sizeof(struct ast_config));
-                       if (tmp)
-                               memset(tmp, 0, sizeof(struct ast_config));
 
-                       master = 1;
-               }
-               if (!tmp) {
-                       ast_log(LOG_WARNING, "Out of memory\n");
-                       fclose(f);
-                       return NULL;
+       for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
+               stringp = v->value;
+               driver = strsep(&stringp, ",");
+               database = strsep(&stringp, ",");
+               table = strsep(&stringp, ",");
+                       
+               if (!strcmp(v->name, extconfig_conf)) {
+                       ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
+                       continue;
                }
-               while(!feof(f)) {
-                       lineno++;
-                       if (fgets(buf, sizeof(buf), f)) {
-                               if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
-#ifdef PRESERVE_COMMENTS
-                               , acs
-#endif
-                               )) {
-                                       fclose(f);
-                                       return NULL;
-                               }
-                       }
+
+               if (!strcmp(v->name, "asterisk.conf")) {
+                       ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
+                       continue;
                }
-               fclose(f);              
-       } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
-               else if (option_verbose > 1)
-                       ast_verbose( "Not found (%s)\n", strerror(errno));
-       }
-#ifdef PRESERVE_COMMENTS
-       if (master) {
-               /* Keep trailing comments */
-               tmp->trailingcomments = acs->root;
-               acs->root = NULL;
-               acs->prev = NULL;
-       }
-#endif
-       return tmp;
-}
 
-struct ast_config_reg *get_ast_cust_config_keyword(char *name) 
-{
-       struct ast_config_reg *reg,*ret=NULL;
-       int x=0;
-       ast_mutex_lock(&ast_cust_config_lock);
-       for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
-               for (x=0;x<reg->keycount && !ret ;x++) {
-                       if (!strcmp(reg->keywords[x],name))
-                               ret=reg;
+               if (!strcmp(v->name, "logger.conf")) {
+                       ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
+                       continue;
                }
-       }
-       ast_mutex_unlock(&ast_cust_config_lock);
-       return ret;
-}
 
-struct ast_config_reg *get_ast_cust_config(char *name) 
-{
-       struct ast_config_reg *ptr=NULL;
-       ast_mutex_lock(&ast_cust_config_lock);
-       for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
-               if (!strcmp(name,ptr->name))
-                       break;
+               if (!driver || !database)
+                       continue;
+               if (!strcasecmp(v->name, "sipfriends")) {
+                       ast_log(LOG_WARNING, "The 'sipfriends' table is obsolete, update your config to use sipusers and sippeers, though they can point to the same table.\n");
+                       append_mapping("sipusers", driver, database, table ? table : "sipfriends");
+                       append_mapping("sippeers", driver, database, table ? table : "sipfriends");
+               } else if (!strcasecmp(v->name, "iaxfriends")) {
+                       ast_log(LOG_WARNING, "The 'iaxfriends' table is obsolete, update your config to use iaxusers and iaxpeers, though they can point to the same table.\n");
+                       append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
+                       append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
+               } else 
+                       append_mapping(v->name, driver, database, table);
        }
-       ast_mutex_unlock(&ast_cust_config_lock);
-       return ptr;
+               
+       ast_config_destroy(config);
 }
 
-void ast_config_destroy_all(void) 
+int ast_config_engine_register(struct ast_config_engine *new) 
 {
-       struct ast_config_reg *key;
-       ast_mutex_lock(&ast_cust_config_lock);
-       for (key=ast_cust_config_list;key;key=key->next) {
-               ast_config_deregister(key);
-       }
-       ast_cust_config_list = NULL;
-       ast_mutex_unlock(&ast_cust_config_lock);
-}
+       struct ast_config_engine *ptr;
 
-struct ast_config_reg *get_config_registrations(void) 
-{
-       return ast_cust_config_list;
-}
+       ast_mutex_lock(&config_lock);
 
-int ast_config_register(struct ast_config_reg *new) 
-{
-       struct ast_config_reg *ptr;
-       ast_mutex_lock(&ast_cust_config_lock);
-       new->keycount = 0;
-       if (!ast_cust_config_list) {
-               ast_cust_config_list = new;
+       if (!config_engine_list) {
+               config_engine_list = new;
        } else {
-               for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
-                       ptr->next = new;
+               for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
+               ptr->next = new;
        }
-       ast_mutex_unlock(&ast_cust_config_lock);
-       ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
+
+       ast_mutex_unlock(&config_lock);
+       ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
+
        return 1;
 }
 
-int ast_config_deregister(struct ast_config_reg *del) 
+int ast_config_engine_deregister(struct ast_config_engine *del) 
 {
-       struct ast_config_reg *ptr=NULL,*last=NULL;
-       ast_mutex_lock(&ast_cust_config_lock);
-       for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
+       struct ast_config_engine *ptr, *last=NULL;
+
+       ast_mutex_lock(&config_lock);
+
+       for (ptr = config_engine_list; ptr; ptr=ptr->next) {
                if (ptr == del) {
-                       if (last && ptr->next) {
+                       if (last)
                                last->next = ptr->next;
-                       } else if (last && ! ptr->next) {
-                               last->next = NULL;
-                       } else if (!last && ptr->next) {
-                               ast_cust_config_list = ptr->next;
-                       } else if (!last && !ptr->next) {
-                               ast_cust_config_list = NULL;
-                       }
+                       else
+                               config_engine_list = ptr->next;
+                       break;
                }
                last = ptr;
        }
-       ast_mutex_unlock(&ast_cust_config_lock);
-       return 0;
-}
 
-int ast_cust_config_active(void) {
-       return (ast_cust_config >0) ? 1 : 0;
+       ast_mutex_unlock(&config_lock);
+
+       return 0;
 }
 
-struct ast_config *ast_load(char *configfile)
+static struct ast_config_engine *find_engine(const char *filename, char *database, int dbsiz, char *table, int tabsiz) 
 {
-       struct ast_category *tmpc=NULL;
-       struct ast_variable *last = NULL;
-
+       struct ast_config_engine *eng, *ret = NULL;
+       struct ast_config_map *map;
 
-#ifdef PRESERVE_COMMENTS
-       struct ast_comment_struct acs = { NULL, NULL };
-#endif 
+       ast_mutex_lock(&config_lock);
 
-
-       return __ast_load(configfile, NULL, &tmpc, &last, 0 
-#ifdef PRESERVE_COMMENTS
-       ,&acs
-#endif
-       );
-}
-
-char *ast_category_browse(struct ast_config *config, char *prev)
-{      
-       struct ast_category *cat;
-       if (!prev) {
-               if (config->root)
-                       return config->root->name;
-               else
-                       return NULL;
-       }
-       cat = config->root;
-       while(cat) {
-               if (cat->name == prev) {
-                       if (cat->next)
-                               return cat->next->name;
-                       else
-                               return NULL;
+       for (map = config_maps; map; map = map->next) {
+               if (!strcasecmp(filename, map->name)) {
+                       ast_copy_string(database, map->database, dbsiz);
+                       ast_copy_string(table, map->table ? map->table : filename, tabsiz);
+                       break;
                }
-               cat = cat->next;
        }
-       cat = config->root;
-       while(cat) {
-               if (!strcasecmp(cat->name, prev)) {
-                       if (cat->next)
-                               return cat->next->name;
-                       else
-                               return NULL;
+       if (map) {
+               for (eng = config_engine_list; !ret && eng; eng = eng->next) {
+                       if (!strcasecmp(eng->name, map->driver))
+                               ret = eng;
                }
-               cat = cat->next;
        }
-       return NULL;
-}
 
+       ast_mutex_unlock(&config_lock);
+       
+       /* if we found a mapping, but the engine is not available, then issue a warning */
+       if (map && !ret)
+               ast_log(LOG_WARNING, "Mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
 
-struct ast_config *ast_new_config(void) 
-{
-       struct ast_config *config;
-       config = malloc(sizeof(struct ast_config));
-       memset(config,0,sizeof(struct ast_config));
-       return config;
+       return ret;
 }
 
+static struct ast_config_engine text_file_engine = {
+       .name = "text",
+       .load_func = config_text_file_load,
+};
 
-
-struct ast_category *ast_new_category(char *name) 
+struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg)
 {
-       struct ast_category *category;
-       category = malloc(sizeof(struct ast_category));
-       if (category) {
-               memset(category,0,sizeof(struct ast_category));
-               strncpy(category->name,name,sizeof(category->name) - 1);
+       char db[256];
+       char table[256];
+       struct ast_config_engine *loader = &text_file_engine;
+       struct ast_config *result;
+
+       if (cfg->include_level == cfg->max_include_level) {
+               ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
+               return NULL;
        }
-       return category;
-}
 
+       cfg->include_level++;
 
-struct ast_variable *ast_new_variable(char *name, char *value) 
-{
-       struct ast_variable *variable;
-       variable = malloc(sizeof(struct ast_variable));
-       if (variable) {
-               memset(variable,0,sizeof(struct ast_variable));
-               variable->object=0;
-               variable->name = malloc(strlen(name)+1);
-               if (variable->name) {
-                       strcpy(variable->name,name);
-                       variable->value = malloc(strlen(value)+1);
-                       if (variable->value) {
-                               strcpy(variable->value,value);
-                       } else {
-                               free(variable->name);
-                               variable->name = NULL;
-                       }
+       if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
+               struct ast_config_engine *eng;
+
+               eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
+
+
+               if (eng && eng->load_func) {
+                       loader = eng;
+               } else {
+                       eng = find_engine("global", db, sizeof(db), table, sizeof(table));
+                       if (eng && eng->load_func)
+                               loader = eng;
                }
        }
-       if (!variable->value) {
-               free(variable);
-               variable = NULL;
-       }
-               
-       return variable;
+
+       result = loader->load_func(db, table, filename, cfg);
+
+       if (result)
+               result->include_level--;
+
+       return result;
 }
 
-int ast_cust_config_register(struct ast_config_reg *new) 
+struct ast_config *ast_config_load(const char *filename)
 {
-       ast_config_register(new);
-       read_ast_cust_config();
-       return 1;
+       struct ast_config *cfg;
+       struct ast_config *result;
+
+       cfg = ast_config_new();
+       if (!cfg)
+               return NULL;
+
+       result = ast_config_internal_load(filename, cfg);
+       if (!result)
+               ast_config_destroy(cfg);
+
+       return result;
 }
-int ast_cust_config_deregister(struct ast_config_reg *new) 
+
+struct ast_variable *ast_load_realtime(const char *family, ...)
 {
-       ast_config_deregister(new);
-       read_ast_cust_config();
-       return 1;
+       struct ast_config_engine *eng;
+       char db[256]="";
+       char table[256]="";
+       struct ast_variable *res=NULL;
+       va_list ap;
+
+       va_start(ap, family);
+       eng = find_engine(family, db, sizeof(db), table, sizeof(table));
+       if (eng && eng->realtime_func) 
+               res = eng->realtime_func(db, table, ap);
+       va_end(ap);
+
+       return res;
 }
 
-static void clear_cust_keywords(void) 
+struct ast_config *ast_load_realtime_multientry(const char *family, ...)
 {
-       struct ast_config_reg *key;
-       int x;
-       ast_mutex_lock(&ast_cust_config_lock);
-       for (key=get_config_registrations();key;key=key->next) {
-               for (x=0;x<key->keycount;x++) {
-                       key->keywords[x][0] = '\0';
-               }
-               key->keycount=0;
-       }
-       ast_mutex_unlock(&ast_cust_config_lock);
+       struct ast_config_engine *eng;
+       char db[256]="";
+       char table[256]="";
+       struct ast_config *res=NULL;
+       va_list ap;
+
+       va_start(ap, family);
+       eng = find_engine(family, db, sizeof(db), table, sizeof(table));
+       if (eng && eng->realtime_multi_func) 
+               res = eng->realtime_multi_func(db, table, ap);
+       va_end(ap);
+
+       return res;
+}
+
+int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
+{
+       struct ast_config_engine *eng;
+       int res = -1;
+       char db[256]="";
+       char table[256]="";
+       va_list ap;
+
+       va_start(ap, lookup);
+       eng = find_engine(family, db, sizeof(db), table, sizeof(table));
+       if (eng && eng->update_func) 
+               res = eng->update_func(db, table, keyfield, lookup, ap);
+       va_end(ap);
+
+       return res;
 }
 
 static int config_command(int fd, int argc, char **argv) 
 {
-       struct ast_config_reg *key;
-       int x;
+       struct ast_config_engine *eng;
+       struct ast_config_map *map;
        
-       ast_cli(fd,"\n\n");
-       ast_mutex_lock(&ast_cust_config_lock);
-       for (key=get_config_registrations();key;key=key->next) {
-               ast_cli(fd,"\nConfig Engine: %s\n",key->name);
-               for (x=0;x<key->keycount;x++)
-                       ast_cli(fd,"===>%s\n",key->keywords[x]);
+       ast_mutex_lock(&config_lock);
+
+       ast_cli(fd, "\n\n");
+       for (eng = config_engine_list; eng; eng = eng->next) {
+               ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
+               for (map = config_maps; map; map = map->next)
+                       if (!strcasecmp(map->driver, eng->name)) {
+                               ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
+                                       map->table ? map->table : map->name);
+                       }
        }
-       ast_mutex_unlock(&ast_cust_config_lock);
        ast_cli(fd,"\n\n");
        
+       ast_mutex_unlock(&config_lock);
+
        return 0;
 }
 
+static char show_config_help[] =
+       "Usage: show config mappings\n"
+       "       Shows the filenames to config engines.\n";
+
 static struct ast_cli_entry config_command_struct = {
-  { "show","config","handles", NULL }, config_command,
-  "Show Config Handles", NULL };
+       { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
+};
 
 int register_config_cli() 
 {
        return ast_cli_register(&config_command_struct);
 }
-
-int read_ast_cust_config(void) 
-{
-       char *cfg = config_conf_file;
-       struct ast_config *config;
-       struct ast_variable *v;
-       struct ast_config_reg *ptr;
-       struct ast_config_reg *test = NULL;
-
-       clear_cust_keywords();
-       config = ast_load(cfg);
-       if (config) {
-               for (v = ast_variable_browse(config,"settings");v;v=v->next) {
-                       
-                       ptr = get_ast_cust_config(v->value);
-                       if (ptr) {
-                               if (ptr->keycount >= CONFIG_KEYWORD_ARRAYLEN) {
-                                       ast_log(LOG_WARNING,"Max Number of Bindings exceeded for %s->%s %d/%d\n",v->name,v->value,ptr->keycount,CONFIG_KEYWORD_ARRAYLEN);
-                               } else {
-                                       if (strcmp(v->name,config_conf_file) && strcmp(v->name,"asterisk.conf")) {
-                                               if (!(test = get_ast_cust_config_keyword(v->name))) {
-                                                       ast_log(LOG_NOTICE,"Binding: %s to %s\n",v->name,v->value);
-                                                       strncpy(ptr->keywords[ptr->keycount],v->name,sizeof(ptr->keywords[ptr->keycount]) - 1);
-                                                       ptr->keywords[ptr->keycount][sizeof(ptr->keywords[ptr->keycount])-1] = '\0';
-                                                       ptr->keycount++;
-                                               }
-                                       } else {
-                                               ast_log(LOG_WARNING,"Cannot bind %s, Permission Denied\n",v->name);
-                                       }
-                               }
-                       }
-               }
-               
-               ast_destroy(config);
-       }
-
-       return 0;
-}