via 8118, a RealTime upgrade to make RT a complete storage abstraction. The store...
[asterisk/asterisk.git] / main / config.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Configuration File Parser
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * Includes the Asterisk Realtime API - ARA
26  * See doc/realtime.txt and doc/extconfig.txt
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <time.h>
39 #include <sys/stat.h>
40 #define AST_INCLUDE_GLOB 1
41 #ifdef AST_INCLUDE_GLOB
42 #if defined(__Darwin__) || defined(__CYGWIN__)
43 #define GLOB_ABORTED GLOB_ABEND
44 #endif
45 # include <glob.h>
46 #endif
47
48 #include "asterisk/config.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/options.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/utils.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/app.h"
56
57 #define MAX_NESTED_COMMENTS 128
58 #define COMMENT_START ";--"
59 #define COMMENT_END "--;"
60 #define COMMENT_META ';'
61 #define COMMENT_TAG '-'
62
63 static char *extconfig_conf = "extconfig.conf";
64
65
66 /*! \brief Structure to keep comments for rewriting configuration files */
67 /*! \brief Structure to keep comments for rewriting configuration files */
68 struct ast_comment {
69         struct ast_comment *next;
70         char cmt[0];
71 };
72
73 #define CB_INCR 250
74
75 static void CB_INIT(char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
76 {
77         if (!(*comment_buffer)) {
78                 *comment_buffer = ast_malloc(CB_INCR);
79                 if (!(*comment_buffer))
80                         return;
81                 (*comment_buffer)[0] = 0;
82                 *comment_buffer_size = CB_INCR;
83                 *lline_buffer = ast_malloc(CB_INCR);
84                 if (!(*lline_buffer))
85                         return;
86                 (*lline_buffer)[0] = 0;
87                 *lline_buffer_size = CB_INCR;
88         } else {
89                 (*comment_buffer)[0] = 0;
90                 (*lline_buffer)[0] = 0;
91         }
92 }
93
94 static void  CB_ADD(char **comment_buffer, int *comment_buffer_size, char *str)
95 {
96         int rem = *comment_buffer_size - strlen(*comment_buffer) - 1;
97         int siz = strlen(str);
98         if (rem < siz+1) {
99                 *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + siz + 1);
100                 if (!(*comment_buffer))
101                         return;
102                 *comment_buffer_size += CB_INCR+siz+1;
103         }
104         strcat(*comment_buffer,str);
105 }
106
107 static void  CB_ADD_LEN(char **comment_buffer, int *comment_buffer_size, char *str, int len)
108 {
109         int cbl = strlen(*comment_buffer) + 1;
110         int rem = *comment_buffer_size - cbl;
111         if (rem < len+1) {
112                 *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + len + 1);
113                 if (!(*comment_buffer))
114                         return;
115                 *comment_buffer_size += CB_INCR+len+1;
116         }
117         strncat(*comment_buffer,str,len);
118         (*comment_buffer)[cbl+len-1] = 0;
119 }
120
121 static void  LLB_ADD(char **lline_buffer, int *lline_buffer_size, char *str)
122 {
123         int rem = *lline_buffer_size - strlen(*lline_buffer) - 1;
124         int siz = strlen(str);
125         if (rem < siz+1) {
126                 *lline_buffer = ast_realloc(*lline_buffer, *lline_buffer_size + CB_INCR + siz + 1);
127                 if (!(*lline_buffer)) 
128                         return;
129                 *lline_buffer_size += CB_INCR + siz + 1;
130         }
131         strcat(*lline_buffer,str);
132 }
133
134 static void CB_RESET(char **comment_buffer, char **lline_buffer)  
135
136         (*comment_buffer)[0] = 0; 
137         (*lline_buffer)[0] = 0;
138 }
139
140
141 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
142
143         struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
144         strcpy(x->cmt, buffer);
145         return x;
146 }
147
148
149 static struct ast_config_map {
150         struct ast_config_map *next;
151         char *name;
152         char *driver;
153         char *database;
154         char *table;
155         char stuff[0];
156 } *config_maps = NULL;
157
158 AST_MUTEX_DEFINE_STATIC(config_lock);
159 static struct ast_config_engine *config_engine_list;
160
161 #define MAX_INCLUDE_LEVEL 10
162
163 struct ast_category {
164         char name[80];
165         int ignored;                    /*!< do not let user of the config see this category */
166         int include_level;      
167         struct ast_comment *precomments;
168         struct ast_comment *sameline;
169         struct ast_variable *root;
170         struct ast_variable *last;
171         struct ast_category *next;
172 };
173
174 struct ast_config {
175         struct ast_category *root;
176         struct ast_category *last;
177         struct ast_category *current;
178         struct ast_category *last_browse;               /*!< used to cache the last category supplied via category_browse */
179         int include_level;
180         int max_include_level;
181 };
182
183 struct ast_variable *ast_variable_new(const char *name, const char *value) 
184 {
185         struct ast_variable *variable;
186         int name_len = strlen(name) + 1;        
187
188         if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
189                 variable->name = variable->stuff;
190                 variable->value = variable->stuff + name_len;           
191                 strcpy(variable->name,name);
192                 strcpy(variable->value,value);
193         }
194
195         return variable;
196 }
197
198 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
199 {
200         if (!variable)
201                 return;
202         if (category->last)
203                 category->last->next = variable;
204         else
205                 category->root = variable;
206         category->last = variable;
207         while (category->last->next)
208                 category->last = category->last->next;
209 }
210
211 void ast_variables_destroy(struct ast_variable *v)
212 {
213         struct ast_variable *vn;
214
215         while (v) {
216                 vn = v;
217                 v = v->next;
218                 free(vn);
219         }
220 }
221
222 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
223 {
224         struct ast_category *cat = NULL;
225
226         if (category && config->last_browse && (config->last_browse->name == category))
227                 cat = config->last_browse;
228         else
229                 cat = ast_category_get(config, category);
230
231         return (cat) ? cat->root : NULL;
232 }
233
234 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
235 {
236         const char *tmp;
237         tmp = ast_variable_retrieve(cfg, cat, var);
238         if (!tmp)
239                 tmp = ast_variable_retrieve(cfg, "general", var);
240         return tmp;
241 }
242
243
244 const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
245 {
246         struct ast_variable *v;
247
248         if (category) {
249                 for (v = ast_variable_browse(config, category); v; v = v->next) {
250                         if (!strcasecmp(variable, v->name))
251                                 return v->value;
252                 }
253         } else {
254                 struct ast_category *cat;
255
256                 for (cat = config->root; cat; cat = cat->next)
257                         for (v = cat->root; v; v = v->next)
258                                 if (!strcasecmp(variable, v->name))
259                                         return v->value;
260         }
261
262         return NULL;
263 }
264
265 static struct ast_variable *variable_clone(const struct ast_variable *old)
266 {
267         struct ast_variable *new = ast_variable_new(old->name, old->value);
268
269         if (new) {
270                 new->lineno = old->lineno;
271                 new->object = old->object;
272                 new->blanklines = old->blanklines;
273                 /* TODO: clone comments? */
274         }
275
276         return new;
277 }
278  
279 static void move_variables(struct ast_category *old, struct ast_category *new)
280 {
281         struct ast_variable *var = old->root;
282         old->root = NULL;
283 #if 1
284         /* we can just move the entire list in a single op */
285         ast_variable_append(new, var);
286 #else
287         while (var) {
288                 struct ast_variable *next = var->next;
289                 var->next = NULL;
290                 ast_variable_append(new, var);
291                 var = next;
292         }
293 #endif
294 }
295
296 struct ast_category *ast_category_new(const char *name) 
297 {
298         struct ast_category *category;
299
300         if ((category = ast_calloc(1, sizeof(*category))))
301                 ast_copy_string(category->name, name, sizeof(category->name));
302         return category;
303 }
304
305 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
306 {
307         struct ast_category *cat;
308
309         /* try exact match first, then case-insensitive match */
310         for (cat = config->root; cat; cat = cat->next) {
311                 if (cat->name == category_name && (ignored || !cat->ignored))
312                         return cat;
313         }
314
315         for (cat = config->root; cat; cat = cat->next) {
316                 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
317                         return cat;
318         }
319
320         return NULL;
321 }
322
323 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
324 {
325         return category_get(config, category_name, 0);
326 }
327
328 int ast_category_exist(const struct ast_config *config, const char *category_name)
329 {
330         return !!ast_category_get(config, category_name);
331 }
332
333 void ast_category_append(struct ast_config *config, struct ast_category *category)
334 {
335         if (config->last)
336                 config->last->next = category;
337         else
338                 config->root = category;
339         category->include_level = config->include_level;
340         config->last = category;
341         config->current = category;
342 }
343
344 void ast_category_destroy(struct ast_category *cat)
345 {
346         ast_variables_destroy(cat->root);
347         free(cat);
348 }
349
350 static struct ast_category *next_available_category(struct ast_category *cat)
351 {
352         for (; cat && cat->ignored; cat = cat->next);
353
354         return cat;
355 }
356
357 char *ast_category_browse(struct ast_config *config, const char *prev)
358 {       
359         struct ast_category *cat = NULL;
360
361         if (prev && config->last_browse && (config->last_browse->name == prev))
362                 cat = config->last_browse->next;
363         else if (!prev && config->root)
364                 cat = config->root;
365         else if (prev) {
366                 for (cat = config->root; cat; cat = cat->next) {
367                         if (cat->name == prev) {
368                                 cat = cat->next;
369                                 break;
370                         }
371                 }
372                 if (!cat) {
373                         for (cat = config->root; cat; cat = cat->next) {
374                                 if (!strcasecmp(cat->name, prev)) {
375                                         cat = cat->next;
376                                         break;
377                                 }
378                         }
379                 }
380         }
381         
382         if (cat)
383                 cat = next_available_category(cat);
384
385         config->last_browse = cat;
386         return (cat) ? cat->name : NULL;
387 }
388
389 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
390 {
391         struct ast_variable *v;
392
393         v = cat->root;
394         cat->root = NULL;
395         cat->last = NULL;
396
397         return v;
398 }
399
400 void ast_category_rename(struct ast_category *cat, const char *name)
401 {
402         ast_copy_string(cat->name, name, sizeof(cat->name));
403 }
404
405 static void inherit_category(struct ast_category *new, const struct ast_category *base)
406 {
407         struct ast_variable *var;
408
409         for (var = base->root; var; var = var->next)
410                 ast_variable_append(new, variable_clone(var));
411 }
412
413 struct ast_config *ast_config_new(void) 
414 {
415         struct ast_config *config;
416
417         if ((config = ast_calloc(1, sizeof(*config))))
418                 config->max_include_level = MAX_INCLUDE_LEVEL;
419         return config;
420 }
421
422 int ast_variable_delete(struct ast_category *category, const char *variable, const char *match)
423 {
424         struct ast_variable *cur, *prev=NULL, *curn;
425         int res = -1;
426         cur = category->root;
427         while (cur) {
428                 if (cur->name == variable) {
429                         if (prev) {
430                                 prev->next = cur->next;
431                                 if (cur == category->last)
432                                         category->last = prev;
433                         } else {
434                                 category->root = cur->next;
435                                 if (cur == category->last)
436                                         category->last = NULL;
437                         }
438                         cur->next = NULL;
439                         ast_variables_destroy(cur);
440                         return 0;
441                 }
442                 prev = cur;
443                 cur = cur->next;
444         }
445
446         prev = NULL;
447         cur = category->root;
448         while (cur) {
449                 curn = cur->next;
450                 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
451                         if (prev) {
452                                 prev->next = cur->next;
453                                 if (cur == category->last)
454                                         category->last = prev;
455                         } else {
456                                 category->root = cur->next;
457                                 if (cur == category->last)
458                                         category->last = NULL;
459                         }
460                         cur->next = NULL;
461                         ast_variables_destroy(cur);
462                         res = 0;
463                 } else
464                         prev = cur;
465
466                 cur = curn;
467         }
468         return res;
469 }
470
471 int ast_variable_update(struct ast_category *category, const char *variable, const char *value, const char *match)
472 {
473         struct ast_variable *cur, *prev=NULL, *newer;
474         newer = ast_variable_new(variable, value);
475         if (!newer)
476                 return -1;
477         cur = category->root;
478         while (cur) {
479                 if (cur->name == variable) {
480                         newer->next = cur->next;
481                         newer->object = cur->object;
482                         if (prev)
483                                 prev->next = newer;
484                         else
485                                 category->root = newer;
486                         if (category->last == cur)
487                                 category->last = newer;
488                         cur->next = NULL;
489                         ast_variables_destroy(cur);
490                         return 0;
491                 }
492                 prev = cur;
493                 cur = cur->next;
494         }
495
496         prev = NULL;
497         cur = category->root;
498         while (cur) {
499                 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
500                         newer->next = cur->next;
501                         newer->object = cur->object;
502                         if (prev)
503                                 prev->next = newer;
504                         else
505                                 category->root = newer;
506                         if (category->last == cur)
507                                 category->last = newer;
508                         cur->next = NULL;
509                         ast_variables_destroy(cur);
510                         return 0;
511                 }
512                 prev = cur;
513                 cur = cur->next;
514         }
515         if (prev)
516                 prev->next = newer;
517         else
518                 category->root = newer;
519         return 0;
520 }
521
522 int ast_category_delete(struct ast_config *cfg, const char *category)
523 {
524         struct ast_category *prev=NULL, *cat;
525         cat = cfg->root;
526         while (cat) {
527                 if (cat->name == category) {
528                         ast_variables_destroy(cat->root);
529                         if (prev) {
530                                 prev->next = cat->next;
531                                 if (cat == cfg->last)
532                                         cfg->last = prev;
533                         } else {
534                                 cfg->root = cat->next;
535                                 if (cat == cfg->last)
536                                         cfg->last = NULL;
537                         }
538                         free(cat);
539                         return 0;
540                 }
541                 prev = cat;
542                 cat = cat->next;
543         }
544
545         prev = NULL;
546         cat = cfg->root;
547         while (cat) {
548                 if (!strcasecmp(cat->name, category)) {
549                         ast_variables_destroy(cat->root);
550                         if (prev) {
551                                 prev->next = cat->next;
552                                 if (cat == cfg->last)
553                                         cfg->last = prev;
554                         } else {
555                                 cfg->root = cat->next;
556                                 if (cat == cfg->last)
557                                         cfg->last = NULL;
558                         }
559                         free(cat);
560                         return 0;
561                 }
562                 prev = cat;
563                 cat = cat->next;
564         }
565         return -1;
566 }
567
568 void ast_config_destroy(struct ast_config *cfg)
569 {
570         struct ast_category *cat, *catn;
571
572         if (!cfg)
573                 return;
574
575         cat = cfg->root;
576         while (cat) {
577                 ast_variables_destroy(cat->root);
578                 catn = cat;
579                 cat = cat->next;
580                 free(catn);
581         }
582         free(cfg);
583 }
584
585 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
586 {
587         return cfg->current;
588 }
589
590 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
591 {
592         /* cast below is just to silence compiler warning about dropping "const" */
593         cfg->current = (struct ast_category *) cat;
594 }
595
596 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
597                                 char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
598 {
599         char *c;
600         char *cur = buf;
601         struct ast_variable *v;
602         char cmd[512], exec_file[512];
603         int object, do_exec, do_include;
604
605         /* Actually parse the entry */
606         if (cur[0] == '[') {
607                 struct ast_category *newcat = NULL;
608                 char *catname;
609
610                 /* A category header */
611                 c = strchr(cur, ']');
612                 if (!c) {
613                         ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
614                         return -1;
615                 }
616                 *c++ = '\0';
617                 cur++;
618                 if (*c++ != '(')
619                         c = NULL;
620                 catname = cur;
621                 if (!(*cat = newcat = ast_category_new(catname))) {
622                         return -1;
623                 }
624                 /* add comments */
625                 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
626                         newcat->precomments = ALLOC_COMMENT(*comment_buffer);
627                 }
628                 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
629                         newcat->sameline = ALLOC_COMMENT(*lline_buffer);
630                 }
631                 if ( withcomments )
632                         CB_RESET(comment_buffer, lline_buffer);
633                 
634                 /* If there are options or categories to inherit from, process them now */
635                 if (c) {
636                         if (!(cur = strchr(c, ')'))) {
637                                 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
638                                 return -1;
639                         }
640                         *cur = '\0';
641                         while ((cur = strsep(&c, ","))) {
642                                 if (!strcasecmp(cur, "!")) {
643                                         (*cat)->ignored = 1;
644                                 } else if (!strcasecmp(cur, "+")) {
645                                         *cat = category_get(cfg, catname, 1);
646                                         if (!(*cat)) {
647                                                 ast_config_destroy(cfg);
648                                                 if (newcat)
649                                                         ast_category_destroy(newcat);
650                                                 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
651                                                 return -1;
652                                         }
653                                         if (newcat) {
654                                                 move_variables(newcat, *cat);
655                                                 ast_category_destroy(newcat);
656                                                 newcat = NULL;
657                                         }
658                                 } else {
659                                         struct ast_category *base;
660                                 
661                                         base = category_get(cfg, cur, 1);
662                                         if (!base) {
663                                                 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
664                                                 return -1;
665                                         }
666                                         inherit_category(*cat, base);
667                                 }
668                         }
669                 }
670                 if (newcat)
671                         ast_category_append(cfg, *cat);
672         } else if (cur[0] == '#') {
673                 /* A directive */
674                 cur++;
675                 c = cur;
676                 while (*c && (*c > 32)) c++;
677                 if (*c) {
678                         *c = '\0';
679                         /* Find real argument */
680                         c = ast_skip_blanks(c + 1);
681                         if (!(*c))
682                                 c = NULL;
683                 } else 
684                         c = NULL;
685                 do_include = !strcasecmp(cur, "include");
686                 if (!do_include)
687                         do_exec = !strcasecmp(cur, "exec");
688                 else
689                         do_exec = 0;
690                 if (do_exec && !ast_opt_exec_includes) {
691                         ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
692                         do_exec = 0;
693                 }
694                 if (do_include || do_exec) {
695                         if (c) {
696                                 /* Strip off leading and trailing "'s and <>'s */
697                                 while ((*c == '<') || (*c == '>') || (*c == '\"')) c++;
698                                 /* Get rid of leading mess */
699                                 cur = c;
700                                 while (!ast_strlen_zero(cur)) {
701                                         c = cur + strlen(cur) - 1;
702                                         if ((*c == '>') || (*c == '<') || (*c == '\"'))
703                                                 *c = '\0';
704                                         else
705                                                 break;
706                                 }
707                                 /* #exec </path/to/executable>
708                                    We create a tmp file, then we #include it, then we delete it. */
709                                 if (do_exec) { 
710                                         snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
711                                         snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
712                                         ast_safe_system(cmd);
713                                         cur = exec_file;
714                                 } else
715                                         exec_file[0] = '\0';
716                                 /* A #include */
717                                 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
718                                 if (!ast_strlen_zero(exec_file))
719                                         unlink(exec_file);
720                                 if (!do_include)
721                                         return 0;
722
723                         } else {
724                                 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
725                                                 do_exec ? "exec" : "include",
726                                                 do_exec ? "/path/to/executable" : "filename",
727                                                 lineno,
728                                                 configfile);
729                         }
730                 }
731                 else 
732                         ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
733         } else {
734                 /* Just a line (variable = value) */
735                 if (!(*cat)) {
736                         ast_log(LOG_WARNING,
737                                 "parse error: No category context for line %d of %s\n", lineno, configfile);
738                         return -1;
739                 }
740                 c = strchr(cur, '=');
741                 if (c) {
742                         *c = 0;
743                         c++;
744                         /* Ignore > in => */
745                         if (*c== '>') {
746                                 object = 1;
747                                 c++;
748                         } else
749                                 object = 0;
750                         if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
751                                 v->lineno = lineno;
752                                 v->object = object;
753                                 /* Put and reset comments */
754                                 v->blanklines = 0;
755                                 ast_variable_append(*cat, v);
756                                 /* add comments */
757                                 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
758                                         v->precomments = ALLOC_COMMENT(*comment_buffer);
759                                 }
760                                 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
761                                         v->sameline = ALLOC_COMMENT(*lline_buffer);
762                                 }
763                                 if ( withcomments )
764                                         CB_RESET(comment_buffer, lline_buffer);
765                                 
766                         } else {
767                                 return -1;
768                         }
769                 } else {
770                         ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
771                 }
772         }
773         return 0;
774 }
775
776 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
777 {
778         char fn[256];
779         char buf[8192];
780         char *new_buf, *comment_p, *process_buf;
781         FILE *f;
782         int lineno=0;
783         int comment = 0, nest[MAX_NESTED_COMMENTS];
784         struct ast_category *cat = NULL;
785         int count = 0;
786         struct stat statbuf;
787         /*! Growable string buffer */
788         char *comment_buffer=0;   /*!< this will be a comment collector.*/
789         int   comment_buffer_size=0;  /*!< the amount of storage so far alloc'd for the comment_buffer */
790
791         char *lline_buffer=0;    /*!< A buffer for stuff behind the ; */
792         int  lline_buffer_size=0;
793
794         
795         cat = ast_config_get_current_category(cfg);
796
797         if (filename[0] == '/') {
798                 ast_copy_string(fn, filename, sizeof(fn));
799         } else {
800                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
801         }
802
803         if (withcomments) {
804                 CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
805                 if (!lline_buffer || !comment_buffer) {
806                         ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
807                         return NULL;
808                 }
809         }
810 #ifdef AST_INCLUDE_GLOB
811         {
812                 int glob_ret;
813                 glob_t globbuf;
814                 globbuf.gl_offs = 0;    /* initialize it to silence gcc */
815 #ifdef SOLARIS
816                 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
817 #else
818                 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
819 #endif
820                 if (glob_ret == GLOB_NOSPACE)
821                         ast_log(LOG_WARNING,
822                                 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
823                 else if (glob_ret  == GLOB_ABORTED)
824                         ast_log(LOG_WARNING,
825                                 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
826                 else  {
827                         /* loop over expanded files */
828                         int i;
829                         for (i=0; i<globbuf.gl_pathc; i++) {
830                                 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
831 #endif
832         do {
833                 if (stat(fn, &statbuf))
834                         continue;
835
836                 if (!S_ISREG(statbuf.st_mode)) {
837                         ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
838                         continue;
839                 }
840                 if (option_verbose > 1) {
841                         ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
842                         fflush(stdout);
843                 }
844                 if (!(f = fopen(fn, "r"))) {
845                         if (option_debug)
846                                 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
847                         if (option_verbose > 1)
848                                 ast_verbose( "Not found (%s)\n", strerror(errno));
849                         continue;
850                 }
851                 count++;
852                 if (option_debug)
853                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
854                 if (option_verbose > 1)
855                         ast_verbose("Found\n");
856                 while (!feof(f)) {
857                         lineno++;
858                         if (fgets(buf, sizeof(buf), f)) {
859                                 if ( withcomments ) {
860                                         CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer);       /* add the current lline buffer to the comment buffer */
861                                         lline_buffer[0] = 0;        /* erase the lline buffer */
862                                 }
863                                 
864                                 new_buf = buf;
865                                 if (comment) 
866                                         process_buf = NULL;
867                                 else
868                                         process_buf = buf;
869                                 
870                                 while ((comment_p = strchr(new_buf, COMMENT_META))) {
871                                         if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
872                                                 /* Yuck, gotta memmove */
873                                                 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
874                                                 new_buf = comment_p;
875                                         } else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
876                                                 /* Meta-Comment start detected ";--" */
877                                                 if (comment < MAX_NESTED_COMMENTS) {
878                                                         *comment_p = '\0';
879                                                         new_buf = comment_p + 3;
880                                                         comment++;
881                                                         nest[comment-1] = lineno;
882                                                 } else {
883                                                         ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
884                                                 }
885                                         } else if ((comment_p >= new_buf + 2) &&
886                                                    (*(comment_p - 1) == COMMENT_TAG) &&
887                                                    (*(comment_p - 2) == COMMENT_TAG)) {
888                                                 /* Meta-Comment end detected */
889                                                 comment--;
890                                                 new_buf = comment_p + 1;
891                                                 if (!comment) {
892                                                         /* Back to non-comment now */
893                                                         if (process_buf) {
894                                                                 /* Actually have to move what's left over the top, then continue */
895                                                                 char *oldptr;
896                                                                 oldptr = process_buf + strlen(process_buf);
897                                                                 if ( withcomments ) {
898                                                                         CB_ADD(&comment_buffer, &comment_buffer_size, ";");
899                                                                         CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
900                                                                 }
901                                                                 
902                                                                 memmove(oldptr, new_buf, strlen(new_buf) + 1);
903                                                                 new_buf = oldptr;
904                                                         } else
905                                                                 process_buf = new_buf;
906                                                 }
907                                         } else {
908                                                 if (!comment) {
909                                                         /* If ; is found, and we are not nested in a comment, 
910                                                            we immediately stop all comment processing */
911                                                         if ( withcomments ) {
912                                                                 LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
913                                                         }
914                                                         *comment_p = '\0'; 
915                                                         new_buf = comment_p;
916                                                 } else
917                                                         new_buf = comment_p + 1;
918                                         }
919                                 }
920                                 if ( withcomments && comment && !process_buf )
921                                 {
922                                         CB_ADD(&comment_buffer, &comment_buffer_size, buf);  /* the whole line is a comment, store it */
923                                 }
924                                 
925                                 if (process_buf) {
926                                         char *buf = ast_strip(process_buf);
927                                         if (!ast_strlen_zero(buf)) {
928                                                 if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
929                                                         cfg = NULL;
930                                                         break;
931                                                 }
932                                         }
933                                 }
934                         }
935                 }
936                 fclose(f);              
937         } while (0);
938         if (comment) {
939                 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
940         }
941 #ifdef AST_INCLUDE_GLOB
942                                         if (!cfg)
943                                                 break;
944                                 }
945                                 globfree(&globbuf);
946                         }
947                 }
948 #endif
949
950         if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
951                 free(comment_buffer);
952                 free(lline_buffer);
953                 comment_buffer = NULL;
954                 lline_buffer = NULL;
955                 comment_buffer_size = 0;
956                 lline_buffer_size = 0;
957         }
958         
959         if (count == 0)
960                 return NULL;
961
962         return cfg;
963 }
964
965 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
966 {
967         FILE *f;
968         char fn[256];
969         char date[256]="";
970         time_t t;
971         struct ast_variable *var;
972         struct ast_category *cat;
973         struct ast_comment *cmt;
974         int blanklines = 0;
975
976         if (configfile[0] == '/') {
977                 ast_copy_string(fn, configfile, sizeof(fn));
978         } else {
979                 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
980         }
981         time(&t);
982         ast_copy_string(date, ctime(&t), sizeof(date));
983 #ifdef __CYGWIN__       
984         if ((f = fopen(fn, "w+"))) {
985 #else
986         if ((f = fopen(fn, "w"))) {
987 #endif      
988                 if (option_verbose > 1)
989                         ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
990                 fprintf(f, ";!\n");
991                 fprintf(f, ";! Automatically generated configuration file\n");
992                 if (strcmp(configfile, fn))
993                         fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
994                 else
995                         fprintf(f, ";! Filename: %s\n", configfile);
996                 fprintf(f, ";! Generator: %s\n", generator);
997                 fprintf(f, ";! Creation Date: %s", date);
998                 fprintf(f, ";!\n");
999                 cat = cfg->root;
1000                 while (cat) {
1001                         /* Dump section with any appropriate comment */
1002                         for (cmt = cat->precomments; cmt; cmt=cmt->next)
1003                         {
1004                                 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1005                                         fprintf(f,"%s", cmt->cmt);
1006                         }
1007                         if (!cat->precomments)
1008                                 fprintf(f,"\n");
1009                         fprintf(f, "[%s]", cat->name);
1010                         for (cmt = cat->sameline; cmt; cmt=cmt->next)
1011                         {
1012                                 fprintf(f,"%s", cmt->cmt);
1013                         }
1014                         if (!cat->sameline)
1015                                 fprintf(f,"\n");
1016                         var = cat->root;
1017                         while (var) {
1018                                 for (cmt = var->precomments; cmt; cmt=cmt->next)
1019                                 {
1020                                         if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1021                                                 fprintf(f,"%s", cmt->cmt);
1022                                 }
1023                                 if (var->sameline) 
1024                                         fprintf(f, "%s %s %s  %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1025                                 else    
1026                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1027                                 if (var->blanklines) {
1028                                         blanklines = var->blanklines;
1029                                         while (blanklines--)
1030                                                 fprintf(f, "\n");
1031                                 }
1032                                         
1033                                 var = var->next;
1034                         }
1035 #if 0
1036                         /* Put an empty line */
1037                         fprintf(f, "\n");
1038 #endif
1039                         cat = cat->next;
1040                 }
1041                 if ((option_verbose > 1) && !option_debug)
1042                         ast_verbose("Saved\n");
1043         } else {
1044                 if (option_debug)
1045                         ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
1046                 if (option_verbose > 1)
1047                         ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
1048                 return -1;
1049         }
1050         fclose(f);
1051         return 0;
1052 }
1053
1054 static void clear_config_maps(void) 
1055 {
1056         struct ast_config_map *map;
1057
1058         ast_mutex_lock(&config_lock);
1059
1060         while (config_maps) {
1061                 map = config_maps;
1062                 config_maps = config_maps->next;
1063                 free(map);
1064         }
1065                 
1066         ast_mutex_unlock(&config_lock);
1067 }
1068
1069 static int append_mapping(char *name, char *driver, char *database, char *table)
1070 {
1071         struct ast_config_map *map;
1072         int length;
1073
1074         length = sizeof(*map);
1075         length += strlen(name) + 1;
1076         length += strlen(driver) + 1;
1077         length += strlen(database) + 1;
1078         if (table)
1079                 length += strlen(table) + 1;
1080
1081         if (!(map = ast_calloc(1, length)))
1082                 return -1;
1083
1084         map->name = map->stuff;
1085         strcpy(map->name, name);
1086         map->driver = map->name + strlen(map->name) + 1;
1087         strcpy(map->driver, driver);
1088         map->database = map->driver + strlen(map->driver) + 1;
1089         strcpy(map->database, database);
1090         if (table) {
1091                 map->table = map->database + strlen(map->database) + 1;
1092                 strcpy(map->table, table);
1093         }
1094         map->next = config_maps;
1095
1096         if (option_verbose > 1)
1097                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1098                             map->name, map->driver, map->database, map->table ? map->table : map->name);
1099
1100         config_maps = map;
1101         return 0;
1102 }
1103
1104 int read_config_maps(void) 
1105 {
1106         struct ast_config *config, *configtmp;
1107         struct ast_variable *v;
1108         char *driver, *table, *database, *stringp, *tmp;
1109
1110         clear_config_maps();
1111
1112         configtmp = ast_config_new();
1113         configtmp->max_include_level = 1;
1114         config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1115         if (!config) {
1116                 ast_config_destroy(configtmp);
1117                 return 0;
1118         }
1119
1120         for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1121                 stringp = v->value;
1122                 driver = strsep(&stringp, ",");
1123
1124                 if ((tmp = strchr(stringp, '\"')))
1125                         stringp = tmp;
1126
1127                 /* check if the database text starts with a double quote */
1128                 if (*stringp == '"') {
1129                         stringp++;
1130                         database = strsep(&stringp, "\"");
1131                         strsep(&stringp, ",");
1132                 } else {
1133                         /* apparently this text has no quotes */
1134                         database = strsep(&stringp, ",");
1135                 }
1136
1137                 table = strsep(&stringp, ",");
1138
1139                 if (!strcmp(v->name, extconfig_conf)) {
1140                         ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1141                         continue;
1142                 }
1143
1144                 if (!strcmp(v->name, "asterisk.conf")) {
1145                         ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1146                         continue;
1147                 }
1148
1149                 if (!strcmp(v->name, "logger.conf")) {
1150                         ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1151                         continue;
1152                 }
1153
1154                 if (!driver || !database)
1155                         continue;
1156                 if (!strcasecmp(v->name, "sipfriends")) {
1157                         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");
1158                         append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1159                         append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1160                 } else if (!strcasecmp(v->name, "iaxfriends")) {
1161                         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");
1162                         append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1163                         append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1164                 } else 
1165                         append_mapping(v->name, driver, database, table);
1166         }
1167                 
1168         ast_config_destroy(config);
1169         return 0;
1170 }
1171
1172 int ast_config_engine_register(struct ast_config_engine *new) 
1173 {
1174         struct ast_config_engine *ptr;
1175
1176         ast_mutex_lock(&config_lock);
1177
1178         if (!config_engine_list) {
1179                 config_engine_list = new;
1180         } else {
1181                 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1182                 ptr->next = new;
1183         }
1184
1185         ast_mutex_unlock(&config_lock);
1186         ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1187
1188         return 1;
1189 }
1190
1191 int ast_config_engine_deregister(struct ast_config_engine *del) 
1192 {
1193         struct ast_config_engine *ptr, *last=NULL;
1194
1195         ast_mutex_lock(&config_lock);
1196
1197         for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1198                 if (ptr == del) {
1199                         if (last)
1200                                 last->next = ptr->next;
1201                         else
1202                                 config_engine_list = ptr->next;
1203                         break;
1204                 }
1205                 last = ptr;
1206         }
1207
1208         ast_mutex_unlock(&config_lock);
1209
1210         return 0;
1211 }
1212
1213 /*! \brief Find realtime engine for realtime family */
1214 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz) 
1215 {
1216         struct ast_config_engine *eng, *ret = NULL;
1217         struct ast_config_map *map;
1218
1219         ast_mutex_lock(&config_lock);
1220
1221         for (map = config_maps; map; map = map->next) {
1222                 if (!strcasecmp(family, map->name)) {
1223                         if (database)
1224                                 ast_copy_string(database, map->database, dbsiz);
1225                         if (table)
1226                                 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1227                         break;
1228                 }
1229         }
1230
1231         /* Check if the required driver (engine) exist */
1232         if (map) {
1233                 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1234                         if (!strcasecmp(eng->name, map->driver))
1235                                 ret = eng;
1236                 }
1237         }
1238
1239         ast_mutex_unlock(&config_lock);
1240         
1241         /* if we found a mapping, but the engine is not available, then issue a warning */
1242         if (map && !ret)
1243                 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1244
1245         return ret;
1246 }
1247
1248 static struct ast_config_engine text_file_engine = {
1249         .name = "text",
1250         .load_func = config_text_file_load,
1251 };
1252
1253 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1254 {
1255         char db[256];
1256         char table[256];
1257         struct ast_config_engine *loader = &text_file_engine;
1258         struct ast_config *result; 
1259
1260         if (cfg->include_level == cfg->max_include_level) {
1261                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1262                 return NULL;
1263         }
1264
1265         cfg->include_level++;
1266
1267         if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1268                 struct ast_config_engine *eng;
1269
1270                 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1271
1272
1273                 if (eng && eng->load_func) {
1274                         loader = eng;
1275                 } else {
1276                         eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1277                         if (eng && eng->load_func)
1278                                 loader = eng;
1279                 }
1280         }
1281
1282         result = loader->load_func(db, table, filename, cfg, withcomments);
1283
1284         if (result)
1285                 result->include_level--;
1286
1287         return result;
1288 }
1289
1290 struct ast_config *ast_config_load(const char *filename)
1291 {
1292         struct ast_config *cfg;
1293         struct ast_config *result;
1294
1295         cfg = ast_config_new();
1296         if (!cfg)
1297                 return NULL;
1298
1299         result = ast_config_internal_load(filename, cfg, 0);
1300         if (!result)
1301                 ast_config_destroy(cfg);
1302
1303         return result;
1304 }
1305
1306 struct ast_config *ast_config_load_with_comments(const char *filename)
1307 {
1308         struct ast_config *cfg;
1309         struct ast_config *result;
1310
1311         cfg = ast_config_new();
1312         if (!cfg)
1313                 return NULL;
1314
1315         result = ast_config_internal_load(filename, cfg, 1);
1316         if (!result)
1317                 ast_config_destroy(cfg);
1318
1319         return result;
1320 }
1321
1322 static struct ast_variable *ast_load_realtime_helper(const char *family, va_list ap)
1323 {
1324         struct ast_config_engine *eng;
1325         char db[256]="";
1326         char table[256]="";
1327         struct ast_variable *res=NULL;
1328
1329         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1330         if (eng && eng->realtime_func) 
1331                 res = eng->realtime_func(db, table, ap);
1332
1333         return res;
1334 }
1335
1336 struct ast_variable *ast_load_realtime_all(const char *family, ...)
1337 {
1338         struct ast_variable *res;
1339         va_list ap;
1340
1341         va_start(ap, family);
1342         res = ast_load_realtime_helper(family, ap);
1343         va_end(ap);
1344
1345         return res;
1346 }
1347
1348 struct ast_variable *ast_load_realtime(const char *family, ...)
1349 {
1350         struct ast_variable *res, *cur, *prev = NULL, *freeme = NULL;
1351         va_list ap;
1352
1353         va_start(ap, family);
1354         res = ast_load_realtime_helper(family, ap);
1355         va_end(ap);
1356
1357         /* Eliminate blank entries */
1358         for (cur = res; cur; cur = cur->next) {
1359                 if (freeme) {
1360                         free(freeme);
1361                         freeme = NULL;
1362                 }
1363
1364                 if (ast_strlen_zero(cur->value)) {
1365                         if (prev)
1366                                 prev->next = cur->next;
1367                         else
1368                                 res = cur->next;
1369                         freeme = cur;
1370                 } else {
1371                         prev = cur;
1372                 }
1373         }
1374         return res;
1375 }
1376
1377 /*! \brief Check if realtime engine is configured for family */
1378 int ast_check_realtime(const char *family)
1379 {
1380         struct ast_config_engine *eng;
1381
1382         eng = find_engine(family, NULL, 0, NULL, 0);
1383         if (eng)
1384                 return 1;
1385         return 0;
1386
1387 }
1388
1389 /*! \brief Check if there's any realtime engines loaded */
1390 int ast_realtime_enabled()
1391 {
1392         return config_maps ? 1 : 0;
1393 }
1394
1395 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1396 {
1397         struct ast_config_engine *eng;
1398         char db[256]="";
1399         char table[256]="";
1400         struct ast_config *res=NULL;
1401         va_list ap;
1402
1403         va_start(ap, family);
1404         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1405         if (eng && eng->realtime_multi_func) 
1406                 res = eng->realtime_multi_func(db, table, ap);
1407         va_end(ap);
1408
1409         return res;
1410 }
1411
1412 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1413 {
1414         struct ast_config_engine *eng;
1415         int res = -1;
1416         char db[256]="";
1417         char table[256]="";
1418         va_list ap;
1419
1420         va_start(ap, lookup);
1421         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1422         if (eng && eng->update_func) 
1423                 res = eng->update_func(db, table, keyfield, lookup, ap);
1424         va_end(ap);
1425
1426         return res;
1427 }
1428
1429 int ast_store_realtime(const char *family, ...) {
1430         struct ast_config_engine *eng;
1431         int res = -1;
1432         char db[256]="";
1433         char table[256]="";
1434         va_list ap;
1435
1436         va_start(ap, family);
1437         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1438         if (eng && eng->store_func) 
1439                 res = eng->store_func(db, table, ap);
1440         va_end(ap);
1441
1442         return res;
1443 }
1444
1445 int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...) {
1446         struct ast_config_engine *eng;
1447         int res = -1;
1448         char db[256]="";
1449         char table[256]="";
1450         va_list ap;
1451
1452         va_start(ap, lookup);
1453         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1454         if (eng && eng->destroy_func) 
1455                 res = eng->destroy_func(db, table, keyfield, lookup, ap);
1456         va_end(ap);
1457
1458         return res;
1459 }
1460
1461 static int config_command(int fd, int argc, char **argv) 
1462 {
1463         struct ast_config_engine *eng;
1464         struct ast_config_map *map;
1465         
1466         ast_mutex_lock(&config_lock);
1467
1468         ast_cli(fd, "\n\n");
1469         for (eng = config_engine_list; eng; eng = eng->next) {
1470                 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1471                 for (map = config_maps; map; map = map->next)
1472                         if (!strcasecmp(map->driver, eng->name)) {
1473                                 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1474                                         map->table ? map->table : map->name);
1475                         }
1476         }
1477         ast_cli(fd,"\n\n");
1478         
1479         ast_mutex_unlock(&config_lock);
1480
1481         return 0;
1482 }
1483
1484 static char show_config_help[] =
1485         "Usage: core show config mappings\n"
1486         "       Shows the filenames to config engines.\n";
1487
1488 static struct ast_cli_entry cli_config[] = {
1489         { { "core", "show", "config", "mappings", NULL },
1490         config_command, "Display config mappings (file names to config engines)",
1491         show_config_help },
1492 };
1493
1494 int register_config_cli() 
1495 {
1496         ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
1497         return 0;
1498 }