da893398aad33e54538e813a9406a080f99b8c1d
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <time.h>
20 #define AST_INCLUDE_GLOB 1
21 #ifdef AST_INCLUDE_GLOB
22 #ifdef __OSX__
23 #define GLOB_ABORTED GLOB_ABEND
24 #endif
25 # include <glob.h>
26 #endif
27 #include <asterisk/config.h>
28 #include <asterisk/cli.h>
29 #include <asterisk/lock.h>
30 #include <asterisk/options.h>
31 #include <asterisk/logger.h>
32 #include <asterisk/utils.h>
33 #include "asterisk.h"
34 #include "astconf.h"
35
36 #define MAX_NESTED_COMMENTS 128
37 #define COMMENT_START ";--"
38 #define COMMENT_END "--;"
39 #define COMMENT_META ';'
40 #define COMMENT_TAG '-'
41
42 static char *extconfig_conf = "extconfig.conf";
43
44 static struct ast_config_map {
45         struct ast_config_map *next;
46         char *name;
47         char *driver;
48         char *database;
49         char *table;
50         char stuff[0];
51 } *config_maps = NULL;
52
53 AST_MUTEX_DEFINE_STATIC(config_lock);
54 static struct ast_config_engine *config_engine_list;
55
56 #define MAX_INCLUDE_LEVEL 10
57
58 struct ast_comment {
59         struct ast_comment *next;
60         char cmt[0];
61 };
62
63 struct ast_category {
64         char name[80];
65         int ignored;                    /* do not let user of the config see this category */
66         struct ast_variable *root;
67         struct ast_variable *last;
68         struct ast_category *next;
69 };
70
71 struct ast_config {
72         struct ast_category *root;
73         struct ast_category *last;
74         struct ast_category *current;
75         struct ast_category *last_browse;               /* used to cache the last category supplied via category_browse */
76         int include_level;
77         int max_include_level;
78 };
79
80 int ast_true(const char *s)
81 {
82         if (!s)
83                 return 0;
84         /* Determine if this is a true value */
85         if (!strcasecmp(s, "yes") ||
86             !strcasecmp(s, "true") ||
87             !strcasecmp(s, "y") ||
88             !strcasecmp(s, "t") ||
89             !strcasecmp(s, "1") ||
90             !strcasecmp(s, "on"))
91                 return -1;
92         return 0;
93 }
94
95 int ast_false(const char *s)
96 {
97         if (!s)
98                 return 0;
99         /* Determine if this is a false value */
100         if (!strcasecmp(s, "no") ||
101             !strcasecmp(s, "false") ||
102             !strcasecmp(s, "n") ||
103             !strcasecmp(s, "f") ||
104             !strcasecmp(s, "0") ||
105             !strcasecmp(s, "off"))
106                 return -1;
107         return 0;
108 }
109
110 struct ast_variable *ast_variable_new(const char *name, const char *value) 
111 {
112         struct ast_variable *variable;
113
114         int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
115         variable = malloc(length);
116         if (variable) {
117                 memset(variable, 0, length);
118                 variable->name = variable->stuff;
119                 variable->value = variable->stuff + strlen(name) + 1;           
120                 strcpy(variable->name,name);
121                 strcpy(variable->value,value);
122         }
123
124         return variable;
125 }
126
127 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
128 {
129         if (category->last)
130                 category->last->next = variable;
131         else
132                 category->root = variable;
133         category->last = variable;
134 }
135
136 void ast_variables_destroy(struct ast_variable *v)
137 {
138         struct ast_variable *vn;
139
140         while(v) {
141                 vn = v;
142                 v = v->next;
143                 free(vn);
144         }
145 }
146
147 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
148 {
149         struct ast_category *cat = NULL;
150
151         if (category && config->last_browse && (config->last_browse->name == category))
152                 cat = config->last_browse;
153         else
154                 cat = ast_category_get(config, category);
155
156         if (cat)
157                 return cat->root;
158         else
159                 return NULL;
160 }
161
162 char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
163 {
164         struct ast_variable *v;
165
166         if (category) {
167                 for (v = ast_variable_browse(config, category); v; v = v->next)
168                         if (variable == v->name)
169                                 return v->value;
170                 for (v = ast_variable_browse(config, category); v; v = v->next)
171                         if (!strcasecmp(variable, v->name))
172                                 return v->value;
173         } else {
174                 struct ast_category *cat;
175
176                 for (cat = config->root; cat; cat = cat->next)
177                         for (v = cat->root; v; v = v->next)
178                                 if (!strcasecmp(variable, v->name))
179                                         return v->value;
180         }
181
182         return NULL;
183 }
184
185 static struct ast_variable *variable_clone(const struct ast_variable *old)
186 {
187         struct ast_variable *new = ast_variable_new(old->name, old->value);
188
189         if (new) {
190                 new->lineno = old->lineno;
191                 new->object = old->object;
192                 new->blanklines = old->blanklines;
193                 /* TODO: clone comments? */
194         }
195
196         return new;
197 }
198  
199 static void move_variables(struct ast_category *old, struct ast_category *new)
200 {
201         struct ast_variable *var;
202         struct ast_variable *next;
203
204         next = old->root;
205         old->root = NULL;
206         for (var = next; var; var = next) {
207                 next = var->next;
208                 var->next = NULL;
209                 ast_variable_append(new, var);
210         }
211 }
212
213 struct ast_category *ast_category_new(const char *name) 
214 {
215         struct ast_category *category;
216
217         category = malloc(sizeof(struct ast_category));
218         if (category) {
219                 memset(category, 0, sizeof(struct ast_category));
220                 strncpy(category->name, name, sizeof(category->name) - 1);
221         }
222
223         return category;
224 }
225
226 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
227 {
228         struct ast_category *cat;
229
230         for (cat = config->root; cat; cat = cat->next) {
231                 if (cat->name == category_name && (ignored || !cat->ignored))
232                         return cat;
233         }
234
235         for (cat = config->root; cat; cat = cat->next) {
236                 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
237                         return cat;
238         }
239
240         return NULL;
241 }
242
243 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
244 {
245         return category_get(config, category_name, 0);
246 }
247
248 int ast_category_exist(const struct ast_config *config, const char *category_name)
249 {
250         return !!ast_category_get(config, category_name);
251 }
252
253 void ast_category_append(struct ast_config *config, struct ast_category *category)
254 {
255         if (config->last)
256                 config->last->next = category;
257         else
258                 config->root = category;
259         config->last = category;
260         config->current = category;
261 }
262
263 void ast_category_destroy(struct ast_category *cat)
264 {
265         ast_variables_destroy(cat->root);
266         free(cat);
267 }
268
269 static struct ast_category *next_available_category(struct ast_category *cat)
270 {
271         for (; cat && cat->ignored; cat = cat->next);
272
273         return cat;
274 }
275
276 char *ast_category_browse(struct ast_config *config, const char *prev)
277 {       
278         struct ast_category *cat = NULL;
279
280         if (prev && config->last_browse && (config->last_browse->name == prev))
281                 cat = config->last_browse->next;
282         else if (!prev && config->root)
283                         cat = config->root;
284         else if (prev) {
285                 for (cat = config->root; cat; cat = cat->next) {
286                         if (cat->name == prev) {
287                                 cat = cat->next;
288                                 break;
289                         }
290                 }
291                 if (!cat) {
292                         for (cat = config->root; cat; cat = cat->next) {
293                                 if (!strcasecmp(cat->name, prev)) {
294                                         cat = cat->next;
295                                         break;
296                                 }
297                         }
298                 }
299         }
300         
301         if (cat)
302                 cat = next_available_category(cat);
303
304         config->last_browse = cat;
305         if (cat)
306                 return cat->name;
307         else
308                 return NULL;
309 }
310
311 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
312 {
313         struct ast_variable *v;
314
315         v = cat->root;
316         cat->root = NULL;
317
318         return v;
319 }
320
321 void ast_category_rename(struct ast_category *cat, const char *name)
322 {
323         strncpy(cat->name, name, sizeof(cat->name) - 1);
324 }
325
326 static void inherit_category(struct ast_category *new, const struct ast_category *base)
327 {
328         struct ast_variable *var;
329
330         for (var = base->root; var; var = var->next) {
331                 struct ast_variable *v;
332                 
333                 v = variable_clone(var);
334                 if (v)
335                         ast_variable_append(new, v);
336         }
337 }
338
339 struct ast_config *ast_config_new(void) 
340 {
341         struct ast_config *config;
342
343         config = malloc(sizeof(*config));
344         if (config) {
345                 memset(config, 0, sizeof(*config));
346                 config->max_include_level = MAX_INCLUDE_LEVEL;
347         }
348
349         return config;
350 }
351
352 void ast_config_destroy(struct ast_config *cfg)
353 {
354         struct ast_category *cat, *catn;
355
356         if (!cfg)
357                 return;
358
359         cat = cfg->root;
360         while(cat) {
361                 ast_variables_destroy(cat->root);
362                 catn = cat;
363                 cat = cat->next;
364                 free(catn);
365         }
366         free(cfg);
367 }
368
369 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
370 {
371         return cfg->current;
372 }
373
374 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
375 {
376         /* cast below is just to silence compiler warning about dropping "const" */
377         cfg->current = (struct ast_category *) cat;
378 }
379
380 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
381 {
382         char *c;
383         char *cur = buf;
384         struct ast_variable *v;
385         int object;
386
387         /* Actually parse the entry */
388         if (cur[0] == '[') {
389                 struct ast_category *newcat = NULL;
390                 char *catname;
391
392                 /* A category header */
393                 c = strchr(cur, ']');
394                 if (!c) {
395                         ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
396                         return -1;
397                 }
398                 *c++ = '\0';
399                 cur++;
400                 if (*c++ != '(')
401                         c = NULL;
402                 catname = cur;
403                 *cat = newcat = ast_category_new(catname);
404                 if (!newcat) {
405                         ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
406                         return -1;
407                 }
408                 /* If there are options or categories to inherit from, process them now */
409                 if (c) {
410                         if (!(cur = strchr(c, ')'))) {
411                                 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
412                                 return -1;
413                         }
414                         *cur = '\0';
415                         while ((cur = strsep(&c, ","))) {
416                                 if (!strcasecmp(cur, "!")) {
417                                         (*cat)->ignored = 1;
418                                 } else if (!strcasecmp(cur, "+")) {
419                                         *cat = category_get(cfg, catname, 1);
420                                         if (!*cat) {
421                                                 ast_destroy(cfg);
422                                                 if (newcat)
423                                                         ast_category_destroy(newcat);
424                                                 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
425                                                 return -1;
426                                         }
427                                         if (newcat) {
428                                                 move_variables(newcat, *cat);
429                                                 ast_category_destroy(newcat);
430                                                 newcat = NULL;
431                                         }
432                                 } else {
433                                         struct ast_category *base;
434                                 
435                                         base = category_get(cfg, cur, 1);
436                                         if (!base) {
437                                                 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
438                                                 return -1;
439                                         }
440                                         inherit_category(*cat, base);
441                                 }
442                         }
443                 }
444                 if (newcat)
445                         ast_category_append(cfg, *cat);
446         } else if (cur[0] == '#') {
447                 /* A directive */
448                 cur++;
449                 c = cur;
450                 while(*c && (*c > 32)) c++;
451                 if (*c) {
452                         *c = '\0';
453                         c++;
454                         /* Find real argument */
455                         while(*c  && (*c < 33)) c++;
456                         if (!*c)
457                                 c = NULL;
458                 } else 
459                         c = NULL;
460                 if (!strcasecmp(cur, "include")) {
461                         /* A #include */
462                         if (c) {
463                                 /* Strip off leading and trailing "'s and <>'s */
464                                 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
465                                 /* Get rid of leading mess */
466                                 cur = c;
467                                 while (!ast_strlen_zero(cur)) {
468                                         c = cur + strlen(cur) - 1;
469                                         if ((*c == '>') || (*c == '<') || (*c == '\"'))
470                                                 *c = '\0';
471                                         else
472                                                 break;
473                                 }
474
475                                 if (!ast_config_internal_load(cur, cfg))
476                                         return -1;
477                         } else
478                                 ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
479                 }
480                 else 
481                         ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
482         } else {
483                 /* Just a line (variable = value) */
484                 if (!*cat) {
485                         ast_log(LOG_WARNING,
486                                 "parse error: No category context for line %d of %s\n", lineno, configfile);
487                         return -1;
488                 }
489                 c = strchr(cur, '=');
490                 if (c) {
491                         *c = 0;
492                         c++;
493                         /* Ignore > in => */
494                         if (*c== '>') {
495                                 object = 1;
496                                 c++;
497                         } else
498                                 object = 0;
499                         v = ast_variable_new(ast_strip(cur), ast_strip(c));
500                         if (v) {
501                                 v->lineno = lineno;
502                                 v->object = object;
503                                 /* Put and reset comments */
504                                 v->blanklines = 0;
505                                 ast_variable_append(*cat, v);
506                         } else {
507                                 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
508                                 return -1;
509                         }
510                 } else {
511                         ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
512                 }
513
514         }
515         return 0;
516 }
517
518 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg)
519 {
520         char fn[256];
521         char buf[8192];
522         char *new_buf, *comment_p, *process_buf;
523         FILE *f;
524         int lineno=0;
525         int comment = 0, nest[MAX_NESTED_COMMENTS];
526         struct ast_category *cat = NULL;
527         
528         cat = ast_config_get_current_category(cfg);
529
530         if (filename[0] == '/') {
531                 strncpy(fn, filename, sizeof(fn)-1);
532         } else {
533                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
534         }
535
536 #ifdef AST_INCLUDE_GLOB
537         {
538                 int glob_ret;
539                 glob_t globbuf;
540                 globbuf.gl_offs = 0;    /* initialize it to silence gcc */
541 #ifdef SOLARIS
542                 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
543 #else
544                 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
545 #endif
546                 if (glob_ret == GLOB_NOSPACE)
547                         ast_log(LOG_WARNING,
548                                 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
549                 else if (glob_ret  == GLOB_ABORTED)
550                         ast_log(LOG_WARNING,
551                                 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
552                 else  {
553                         /* loop over expanded files */
554                         int i;
555                         for (i=0; i<globbuf.gl_pathc; i++) {
556                                 strncpy(fn, globbuf.gl_pathv[i], sizeof(fn)-1);
557 #endif
558         if ((option_verbose > 1) && !option_debug) {
559                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
560                 fflush(stdout);
561         }
562         if ((f = fopen(fn, "r"))) {
563                 if (option_debug)
564                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
565                 else if (option_verbose > 1)
566                         ast_verbose("Found\n");
567                 while(!feof(f)) {
568                         lineno++;
569                         if (fgets(buf, sizeof(buf), f)) {
570                                 new_buf = buf;
571                                 if (comment)
572                                         process_buf = NULL;
573                                 else
574                                         process_buf = buf;
575                                 while ((comment_p = strchr(new_buf, COMMENT_META))) {
576                                         if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
577                                                 /* Yuck, gotta memmove */
578                                                 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
579                                                 new_buf = comment_p;
580                                         } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
581                                                 /* Meta-Comment start detected ";--" */
582                                                 if (comment < MAX_NESTED_COMMENTS) {
583                                                         *comment_p = '\0';
584                                                         new_buf = comment_p + 3;
585                                                         comment++;
586                                                         nest[comment-1] = lineno;
587                                                 } else {
588                                                         ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
589                                                 }
590                                         } else if ((comment_p >= new_buf + 2) &&
591                                                    (*(comment_p - 1) == COMMENT_TAG) &&
592                                                    (*(comment_p - 2) == COMMENT_TAG)) {
593                                                 /* Meta-Comment end detected */
594                                                 comment--;
595                                                 new_buf = comment_p + 1;
596                                                 if (!comment) {
597                                                         /* Back to non-comment now */
598                                                         if (process_buf) {
599                                                                 /* Actually have to move what's left over the top, then continue */
600                                                                 char *oldptr;
601                                                                 oldptr = process_buf + strlen(process_buf);
602                                                                 memmove(oldptr, new_buf, strlen(new_buf) + 1);
603                                                                 new_buf = oldptr;
604                                                         } else
605                                                                 process_buf = new_buf;
606                                                 }
607                                         } else {
608                                                 if (!comment) {
609                                                         /* If ; is found, and we are not nested in a comment, 
610                                                            we immediately stop all comment processing */
611                                                         *comment_p = '\0'; 
612                                                         new_buf = comment_p;
613                                                 } else
614                                                         new_buf = comment_p + 1;
615                                         }
616                                 }
617                                 if (process_buf) {
618                                         char *buf = ast_strip(process_buf);
619                                         if (!ast_strlen_zero(buf))
620                                                 if (process_text_line(cfg, &cat, buf, lineno, filename)) {
621                                                         cfg = NULL;
622                                                         break;
623                                                 }
624                                 }
625                         }
626                 }
627                 fclose(f);              
628         } else { /* can't open file */
629                 if (option_debug)
630                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
631                 else if (option_verbose > 2)
632                         ast_verbose( "Not found (%s)\n", strerror(errno));
633         }
634         if (comment) {
635                 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
636         }
637 #ifdef AST_INCLUDE_GLOB
638                                         if (!cfg)
639                                                 break;
640                                 }
641                                 globfree(&globbuf);
642                         }
643                 }
644 #endif
645
646         return cfg;
647 }
648
649 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
650 {
651         FILE *f;
652         char fn[256];
653         char date[256]="";
654         time_t t;
655         struct ast_variable *var;
656         struct ast_category *cat;
657         int blanklines = 0;
658
659         if (configfile[0] == '/') {
660                 strncpy(fn, configfile, sizeof(fn)-1);
661         } else {
662                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
663         }
664         time(&t);
665         strncpy(date, ctime(&t), sizeof(date) - 1);
666         if ((f = fopen(fn, "w"))) {
667                 if ((option_verbose > 1) && !option_debug)
668                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
669                 fprintf(f, ";!\n");
670                 fprintf(f, ";! Automatically generated configuration file\n");
671                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
672                 fprintf(f, ";! Generator: %s\n", generator);
673                 fprintf(f, ";! Creation Date: %s", date);
674                 fprintf(f, ";!\n");
675                 cat = cfg->root;
676                 while(cat) {
677                         /* Dump section with any appropriate comment */
678                         fprintf(f, "[%s]\n", cat->name);
679                         var = cat->root;
680                         while(var) {
681                                 if (var->sameline) 
682                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
683                                 else    
684                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
685                                 if (var->blanklines) {
686                                         blanklines = var->blanklines;
687                                         while (blanklines--)
688                                                 fprintf(f, "\n");
689                                 }
690                                         
691                                 var = var->next;
692                         }
693 #if 0
694                         /* Put an empty line */
695                         fprintf(f, "\n");
696 #endif
697                         cat = cat->next;
698                 }
699         } else {
700                 if (option_debug)
701                         printf("Unable to open for writing: %s\n", fn);
702                 else if (option_verbose > 1)
703                         printf( "Unable to write (%s)", strerror(errno));
704                 return -1;
705         }
706         fclose(f);
707         return 0;
708 }
709
710 static void clear_config_maps(void) 
711 {
712         struct ast_config_map *map;
713
714         ast_mutex_lock(&config_lock);
715
716         while (config_maps) {
717                 map = config_maps;
718                 config_maps = config_maps->next;
719                 free(map);
720         }
721                 
722         ast_mutex_unlock(&config_lock);
723 }
724
725 void read_config_maps(void) 
726 {
727         struct ast_config *config;
728         struct ast_variable *v;
729         struct ast_config_map *map;
730         int length;
731         char *driver, *table, *database, *stringp;
732
733         clear_config_maps();
734
735         config = ast_config_new();
736         config->max_include_level = 1;
737         config = ast_config_internal_load(extconfig_conf, config);
738         if (!config)
739                 return;
740
741         for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
742                 stringp = v->value;
743                 driver = strsep(&stringp, ",");
744                 database = strsep(&stringp, ",");
745                 table = strsep(&stringp, ",");
746                         
747                 if (!strcmp(v->name, extconfig_conf) || !strcmp(v->name, "asterisk.conf")) {
748                         ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
749                         continue;
750                 }
751
752                 if (!driver || !database)
753                         continue;
754
755                 length = sizeof(*map);
756                 length += strlen(v->name) + 1;
757                 length += strlen(driver) + 1;
758                 length += strlen(database) + 1;
759                 if (table)
760                         length += strlen(table) + 1;
761                 map = malloc(length);
762
763                 if (!map)
764                         continue;
765
766                 memset(map, 0, length);
767                 map->name = map->stuff;
768                 strcpy(map->name, v->name);
769                 map->driver = map->name + strlen(map->name) + 1;
770                 strcpy(map->driver, driver);
771                 map->database = map->driver + strlen(map->driver) + 1;
772                 strcpy(map->database, database);
773                 if (table) {
774                         map->table = map->database + strlen(map->database) + 1;
775                         strcpy(map->table, table);
776                 }
777                 map->next = config_maps;
778
779                 if (option_verbose > 1)
780                         ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
781                                     map->name, map->driver, map->database, map->table ? map->table : map->name);
782
783                 config_maps = map;
784         }
785                 
786         ast_config_destroy(config);
787 }
788
789 int ast_config_engine_register(struct ast_config_engine *new) 
790 {
791         struct ast_config_engine *ptr;
792
793         ast_mutex_lock(&config_lock);
794
795         if (!config_engine_list) {
796                 config_engine_list = new;
797         } else {
798                 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
799                 ptr->next = new;
800         }
801
802         ast_mutex_unlock(&config_lock);
803         ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
804
805         return 1;
806 }
807
808 int ast_config_engine_deregister(struct ast_config_engine *del) 
809 {
810         struct ast_config_engine *ptr, *last=NULL;
811
812         ast_mutex_lock(&config_lock);
813
814         for (ptr = config_engine_list; ptr; ptr=ptr->next) {
815                 if (ptr == del) {
816                         if (last)
817                                 last->next = ptr->next;
818                         else
819                                 config_engine_list = ptr->next;
820                         break;
821                 }
822                 last = ptr;
823         }
824
825         ast_mutex_unlock(&config_lock);
826
827         return 0;
828 }
829
830 static struct ast_config_engine *find_engine(const char *filename, char *database, int dbsiz, char *table, int tabsiz) 
831 {
832         struct ast_config_engine *eng, *ret=NULL;
833         struct ast_config_map *map;
834
835         ast_mutex_lock(&config_lock);
836
837         map = config_maps;
838         while (map) {
839                 if (!strcasecmp(filename, map->name)) {
840                         strncpy(database, map->database, dbsiz-1);
841                         if (map->table)
842                                 strncpy(table, map->table, tabsiz-1);
843                         else
844                                 strncpy(table, filename, tabsiz-1);
845                         break;
846                 }
847                 map = map->next;
848         }
849         if (map) {
850                 for (eng = config_engine_list; eng; eng = eng->next) {
851                         if (!strcmp(eng->name, map->driver)) {
852                                 ret = eng;
853                                 break;
854                         }
855                 }
856         }
857         ast_mutex_unlock(&config_lock);
858
859         return ret;
860 }
861
862 static struct ast_config_engine text_file_engine = {
863         .name = "text",
864         .load_func = config_text_file_load,
865 };
866
867 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg)
868 {
869         char db[256];
870         char table[256];
871         struct ast_config_engine *loader = &text_file_engine;
872         struct ast_config *result;
873
874         if (cfg->include_level == cfg->max_include_level) {
875                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
876                 return NULL;
877         }
878
879         cfg->include_level++;
880
881         if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
882                 struct ast_config_engine *eng;
883
884                 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
885                 if (eng && eng->load_func) {
886                         loader = eng;
887                 } else {
888                         eng = find_engine("global", db, sizeof(db), table, sizeof(table));
889                         if (eng && eng->load_func)
890                                 loader = eng;
891                 }
892         }
893
894         result = loader->load_func(db, table, filename, cfg);
895         if (result)
896                 result->include_level--;
897
898         return result;
899 }
900
901 struct ast_config *ast_config_load(const char *filename)
902 {
903         struct ast_config *cfg;
904         struct ast_config *result;
905
906         cfg = ast_config_new();
907         if (!cfg)
908                 return NULL;
909
910         result = ast_config_internal_load(filename, cfg);
911         if (!result)
912                 ast_config_destroy(cfg);
913
914         return result;
915 }
916
917 struct ast_variable *ast_load_realtime(const char *family, ...)
918 {
919         struct ast_config_engine *eng;
920         char db[256]="";
921         char table[256]="";
922         struct ast_variable *res=NULL;
923         va_list ap;
924
925         va_start(ap, family);
926         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
927         if (eng && eng->realtime_func) 
928                 res = eng->realtime_func(db, table, ap);
929         va_end(ap);
930
931         return res;
932 }
933
934 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
935 {
936         struct ast_config_engine *eng;
937         char db[256]="";
938         char table[256]="";
939         struct ast_config *res=NULL;
940         va_list ap;
941
942         va_start(ap, family);
943         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
944         if (eng && eng->realtime_multi_func) 
945                 res = eng->realtime_multi_func(db, table, ap);
946         va_end(ap);
947
948         return res;
949 }
950
951 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
952 {
953         struct ast_config_engine *eng;
954         int res = -1;
955         char db[256]="";
956         char table[256]="";
957         va_list ap;
958
959         va_start(ap, lookup);
960         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
961         if (eng && eng->update_func) 
962                 res = eng->update_func(db, table, keyfield, lookup, ap);
963         va_end(ap);
964
965         return res;
966 }
967
968 static int config_command(int fd, int argc, char **argv) 
969 {
970         struct ast_config_engine *eng;
971         struct ast_config_map *map;
972         
973         ast_mutex_lock(&config_lock);
974
975         ast_cli(fd, "\n\n");
976         for (eng = config_engine_list; eng; eng = eng->next) {
977                 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
978                 for (map = config_maps; map; map = map->next)
979                         if (!strcasecmp(map->driver, eng->name)) {
980                                 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
981                                         map->table ? map->table : map->name);
982                                 break;
983                         }
984         }
985         ast_cli(fd,"\n\n");
986         
987         ast_mutex_unlock(&config_lock);
988
989         return 0;
990 }
991
992 static struct ast_cli_entry config_command_struct = {
993         { "show", "config", "mappings" }, config_command, "Show Config mappings (file names to config engines)"
994 };
995
996 int register_config_cli() 
997 {
998         return ast_cli_register(&config_command_struct);
999 }