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