Merged revisions 63254 via svnmerge from
[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, 
472         const char *value, const char *match, unsigned int object)
473 {
474         struct ast_variable *cur, *prev=NULL, *newer;
475
476         if (!(newer = ast_variable_new(variable, value)))
477                 return -1;
478         
479         newer->object = object;
480
481         for (cur = category->root; cur; prev = cur, cur = cur->next) {
482                 if (strcasecmp(cur->name, variable) ||
483                         (!ast_strlen_zero(match) && strcasecmp(cur->value, match)))
484                         continue;
485
486                 newer->next = cur->next;
487                 newer->object = cur->object || object;
488                 if (prev)
489                         prev->next = newer;
490                 else
491                         category->root = newer;
492                 if (category->last == cur)
493                         category->last = newer;
494
495                 cur->next = NULL;
496                 ast_variables_destroy(cur);
497
498                 return 0;
499         }
500
501         if (prev)
502                 prev->next = newer;
503         else
504                 category->root = newer;
505
506         return 0;
507 }
508
509 int ast_category_delete(struct ast_config *cfg, const char *category)
510 {
511         struct ast_category *prev=NULL, *cat;
512         cat = cfg->root;
513         while (cat) {
514                 if (cat->name == category) {
515                         ast_variables_destroy(cat->root);
516                         if (prev) {
517                                 prev->next = cat->next;
518                                 if (cat == cfg->last)
519                                         cfg->last = prev;
520                         } else {
521                                 cfg->root = cat->next;
522                                 if (cat == cfg->last)
523                                         cfg->last = NULL;
524                         }
525                         free(cat);
526                         return 0;
527                 }
528                 prev = cat;
529                 cat = cat->next;
530         }
531
532         prev = NULL;
533         cat = cfg->root;
534         while (cat) {
535                 if (!strcasecmp(cat->name, category)) {
536                         ast_variables_destroy(cat->root);
537                         if (prev) {
538                                 prev->next = cat->next;
539                                 if (cat == cfg->last)
540                                         cfg->last = prev;
541                         } else {
542                                 cfg->root = cat->next;
543                                 if (cat == cfg->last)
544                                         cfg->last = NULL;
545                         }
546                         free(cat);
547                         return 0;
548                 }
549                 prev = cat;
550                 cat = cat->next;
551         }
552         return -1;
553 }
554
555 void ast_config_destroy(struct ast_config *cfg)
556 {
557         struct ast_category *cat, *catn;
558
559         if (!cfg)
560                 return;
561
562         cat = cfg->root;
563         while (cat) {
564                 ast_variables_destroy(cat->root);
565                 catn = cat;
566                 cat = cat->next;
567                 free(catn);
568         }
569         free(cfg);
570 }
571
572 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
573 {
574         return cfg->current;
575 }
576
577 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
578 {
579         /* cast below is just to silence compiler warning about dropping "const" */
580         cfg->current = (struct ast_category *) cat;
581 }
582
583 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
584                                 char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
585 {
586         char *c;
587         char *cur = buf;
588         struct ast_variable *v;
589         char cmd[512], exec_file[512];
590         int object, do_exec, do_include;
591
592         /* Actually parse the entry */
593         if (cur[0] == '[') {
594                 struct ast_category *newcat = NULL;
595                 char *catname;
596
597                 /* A category header */
598                 c = strchr(cur, ']');
599                 if (!c) {
600                         ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
601                         return -1;
602                 }
603                 *c++ = '\0';
604                 cur++;
605                 if (*c++ != '(')
606                         c = NULL;
607                 catname = cur;
608                 if (!(*cat = newcat = ast_category_new(catname))) {
609                         return -1;
610                 }
611                 /* add comments */
612                 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
613                         newcat->precomments = ALLOC_COMMENT(*comment_buffer);
614                 }
615                 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
616                         newcat->sameline = ALLOC_COMMENT(*lline_buffer);
617                 }
618                 if ( withcomments )
619                         CB_RESET(comment_buffer, lline_buffer);
620                 
621                 /* If there are options or categories to inherit from, process them now */
622                 if (c) {
623                         if (!(cur = strchr(c, ')'))) {
624                                 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
625                                 return -1;
626                         }
627                         *cur = '\0';
628                         while ((cur = strsep(&c, ","))) {
629                                 if (!strcasecmp(cur, "!")) {
630                                         (*cat)->ignored = 1;
631                                 } else if (!strcasecmp(cur, "+")) {
632                                         *cat = category_get(cfg, catname, 1);
633                                         if (!(*cat)) {
634                                                 if (newcat)
635                                                         ast_category_destroy(newcat);
636                                                 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
637                                                 return -1;
638                                         }
639                                         if (newcat) {
640                                                 move_variables(newcat, *cat);
641                                                 ast_category_destroy(newcat);
642                                                 newcat = NULL;
643                                         }
644                                 } else {
645                                         struct ast_category *base;
646                                 
647                                         base = category_get(cfg, cur, 1);
648                                         if (!base) {
649                                                 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
650                                                 return -1;
651                                         }
652                                         inherit_category(*cat, base);
653                                 }
654                         }
655                 }
656                 if (newcat)
657                         ast_category_append(cfg, *cat);
658         } else if (cur[0] == '#') {
659                 /* A directive */
660                 cur++;
661                 c = cur;
662                 while (*c && (*c > 32)) c++;
663                 if (*c) {
664                         *c = '\0';
665                         /* Find real argument */
666                         c = ast_skip_blanks(c + 1);
667                         if (!(*c))
668                                 c = NULL;
669                 } else 
670                         c = NULL;
671                 do_include = !strcasecmp(cur, "include");
672                 if (!do_include)
673                         do_exec = !strcasecmp(cur, "exec");
674                 else
675                         do_exec = 0;
676                 if (do_exec && !ast_opt_exec_includes) {
677                         ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
678                         do_exec = 0;
679                 }
680                 if (do_include || do_exec) {
681                         if (c) {
682                                 /* Strip off leading and trailing "'s and <>'s */
683                                 while ((*c == '<') || (*c == '>') || (*c == '\"')) c++;
684                                 /* Get rid of leading mess */
685                                 cur = c;
686                                 while (!ast_strlen_zero(cur)) {
687                                         c = cur + strlen(cur) - 1;
688                                         if ((*c == '>') || (*c == '<') || (*c == '\"'))
689                                                 *c = '\0';
690                                         else
691                                                 break;
692                                 }
693                                 /* #exec </path/to/executable>
694                                    We create a tmp file, then we #include it, then we delete it. */
695                                 if (do_exec) { 
696                                         snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
697                                         snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
698                                         ast_safe_system(cmd);
699                                         cur = exec_file;
700                                 } else
701                                         exec_file[0] = '\0';
702                                 /* A #include */
703                                 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
704                                 if (!ast_strlen_zero(exec_file))
705                                         unlink(exec_file);
706                                 if (!do_include)
707                                         return 0;
708
709                         } else {
710                                 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
711                                                 do_exec ? "exec" : "include",
712                                                 do_exec ? "/path/to/executable" : "filename",
713                                                 lineno,
714                                                 configfile);
715                         }
716                 }
717                 else 
718                         ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
719         } else {
720                 /* Just a line (variable = value) */
721                 if (!(*cat)) {
722                         ast_log(LOG_WARNING,
723                                 "parse error: No category context for line %d of %s\n", lineno, configfile);
724                         return -1;
725                 }
726                 c = strchr(cur, '=');
727                 if (c) {
728                         *c = 0;
729                         c++;
730                         /* Ignore > in => */
731                         if (*c== '>') {
732                                 object = 1;
733                                 c++;
734                         } else
735                                 object = 0;
736                         if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
737                                 v->lineno = lineno;
738                                 v->object = object;
739                                 /* Put and reset comments */
740                                 v->blanklines = 0;
741                                 ast_variable_append(*cat, v);
742                                 /* add comments */
743                                 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
744                                         v->precomments = ALLOC_COMMENT(*comment_buffer);
745                                 }
746                                 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
747                                         v->sameline = ALLOC_COMMENT(*lline_buffer);
748                                 }
749                                 if ( withcomments )
750                                         CB_RESET(comment_buffer, lline_buffer);
751                                 
752                         } else {
753                                 return -1;
754                         }
755                 } else {
756                         ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
757                 }
758         }
759         return 0;
760 }
761
762 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
763 {
764         char fn[256];
765         char buf[8192];
766         char *new_buf, *comment_p, *process_buf;
767         FILE *f;
768         int lineno=0;
769         int comment = 0, nest[MAX_NESTED_COMMENTS];
770         struct ast_category *cat = NULL;
771         int count = 0;
772         struct stat statbuf;
773         /*! Growable string buffer */
774         char *comment_buffer=0;   /*!< this will be a comment collector.*/
775         int   comment_buffer_size=0;  /*!< the amount of storage so far alloc'd for the comment_buffer */
776
777         char *lline_buffer=0;    /*!< A buffer for stuff behind the ; */
778         int  lline_buffer_size=0;
779
780         
781         cat = ast_config_get_current_category(cfg);
782
783         if (filename[0] == '/') {
784                 ast_copy_string(fn, filename, sizeof(fn));
785         } else {
786                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
787         }
788
789         if (withcomments) {
790                 CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
791                 if (!lline_buffer || !comment_buffer) {
792                         ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
793                         return NULL;
794                 }
795         }
796 #ifdef AST_INCLUDE_GLOB
797         {
798                 int glob_ret;
799                 glob_t globbuf;
800                 globbuf.gl_offs = 0;    /* initialize it to silence gcc */
801 #ifdef SOLARIS
802                 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
803 #else
804                 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
805 #endif
806                 if (glob_ret == GLOB_NOSPACE)
807                         ast_log(LOG_WARNING,
808                                 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
809                 else if (glob_ret  == GLOB_ABORTED)
810                         ast_log(LOG_WARNING,
811                                 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
812                 else  {
813                         /* loop over expanded files */
814                         int i;
815                         for (i=0; i<globbuf.gl_pathc; i++) {
816                                 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
817 #endif
818         do {
819                 if (stat(fn, &statbuf))
820                         continue;
821
822                 if (!S_ISREG(statbuf.st_mode)) {
823                         ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
824                         continue;
825                 }
826                 if (option_verbose > 1) {
827                         ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
828                         fflush(stdout);
829                 }
830                 if (!(f = fopen(fn, "r"))) {
831                         if (option_debug)
832                                 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
833                         if (option_verbose > 1)
834                                 ast_verbose( "Not found (%s)\n", strerror(errno));
835                         continue;
836                 }
837                 count++;
838                 if (option_debug)
839                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
840                 if (option_verbose > 1)
841                         ast_verbose("Found\n");
842                 while (!feof(f)) {
843                         lineno++;
844                         if (fgets(buf, sizeof(buf), f)) {
845                                 if ( withcomments ) {
846                                         CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer);       /* add the current lline buffer to the comment buffer */
847                                         lline_buffer[0] = 0;        /* erase the lline buffer */
848                                 }
849                                 
850                                 new_buf = buf;
851                                 if (comment) 
852                                         process_buf = NULL;
853                                 else
854                                         process_buf = buf;
855                                 
856                                 while ((comment_p = strchr(new_buf, COMMENT_META))) {
857                                         if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
858                                                 /* Yuck, gotta memmove */
859                                                 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
860                                                 new_buf = comment_p;
861                                         } else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
862                                                 /* Meta-Comment start detected ";--" */
863                                                 if (comment < MAX_NESTED_COMMENTS) {
864                                                         *comment_p = '\0';
865                                                         new_buf = comment_p + 3;
866                                                         comment++;
867                                                         nest[comment-1] = lineno;
868                                                 } else {
869                                                         ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
870                                                 }
871                                         } else if ((comment_p >= new_buf + 2) &&
872                                                    (*(comment_p - 1) == COMMENT_TAG) &&
873                                                    (*(comment_p - 2) == COMMENT_TAG)) {
874                                                 /* Meta-Comment end detected */
875                                                 comment--;
876                                                 new_buf = comment_p + 1;
877                                                 if (!comment) {
878                                                         /* Back to non-comment now */
879                                                         if (process_buf) {
880                                                                 /* Actually have to move what's left over the top, then continue */
881                                                                 char *oldptr;
882                                                                 oldptr = process_buf + strlen(process_buf);
883                                                                 if ( withcomments ) {
884                                                                         CB_ADD(&comment_buffer, &comment_buffer_size, ";");
885                                                                         CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
886                                                                 }
887                                                                 
888                                                                 memmove(oldptr, new_buf, strlen(new_buf) + 1);
889                                                                 new_buf = oldptr;
890                                                         } else
891                                                                 process_buf = new_buf;
892                                                 }
893                                         } else {
894                                                 if (!comment) {
895                                                         /* If ; is found, and we are not nested in a comment, 
896                                                            we immediately stop all comment processing */
897                                                         if ( withcomments ) {
898                                                                 LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
899                                                         }
900                                                         *comment_p = '\0'; 
901                                                         new_buf = comment_p;
902                                                 } else
903                                                         new_buf = comment_p + 1;
904                                         }
905                                 }
906                                 if ( withcomments && comment && !process_buf )
907                                 {
908                                         CB_ADD(&comment_buffer, &comment_buffer_size, buf);  /* the whole line is a comment, store it */
909                                 }
910                                 
911                                 if (process_buf) {
912                                         char *buf = ast_strip(process_buf);
913                                         if (!ast_strlen_zero(buf)) {
914                                                 if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
915                                                         cfg = NULL;
916                                                         break;
917                                                 }
918                                         }
919                                 }
920                         }
921                 }
922                 fclose(f);              
923         } while (0);
924         if (comment) {
925                 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
926         }
927 #ifdef AST_INCLUDE_GLOB
928                                         if (!cfg)
929                                                 break;
930                                 }
931                                 globfree(&globbuf);
932                         }
933                 }
934 #endif
935
936         if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
937                 free(comment_buffer);
938                 free(lline_buffer);
939                 comment_buffer = NULL;
940                 lline_buffer = NULL;
941                 comment_buffer_size = 0;
942                 lline_buffer_size = 0;
943         }
944         
945         if (count == 0)
946                 return NULL;
947
948         return cfg;
949 }
950
951 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
952 {
953         FILE *f;
954         char fn[256];
955         char date[256]="";
956         time_t t;
957         struct ast_variable *var;
958         struct ast_category *cat;
959         struct ast_comment *cmt;
960         int blanklines = 0;
961
962         if (configfile[0] == '/') {
963                 ast_copy_string(fn, configfile, sizeof(fn));
964         } else {
965                 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
966         }
967         time(&t);
968         ast_copy_string(date, ctime(&t), sizeof(date));
969 #ifdef __CYGWIN__       
970         if ((f = fopen(fn, "w+"))) {
971 #else
972         if ((f = fopen(fn, "w"))) {
973 #endif      
974                 if (option_verbose > 1)
975                         ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
976                 fprintf(f, ";!\n");
977                 fprintf(f, ";! Automatically generated configuration file\n");
978                 if (strcmp(configfile, fn))
979                         fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
980                 else
981                         fprintf(f, ";! Filename: %s\n", configfile);
982                 fprintf(f, ";! Generator: %s\n", generator);
983                 fprintf(f, ";! Creation Date: %s", date);
984                 fprintf(f, ";!\n");
985                 cat = cfg->root;
986                 while (cat) {
987                         /* Dump section with any appropriate comment */
988                         for (cmt = cat->precomments; cmt; cmt=cmt->next)
989                         {
990                                 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
991                                         fprintf(f,"%s", cmt->cmt);
992                         }
993                         if (!cat->precomments)
994                                 fprintf(f,"\n");
995                         fprintf(f, "[%s]", cat->name);
996                         for (cmt = cat->sameline; cmt; cmt=cmt->next)
997                         {
998                                 fprintf(f,"%s", cmt->cmt);
999                         }
1000                         if (!cat->sameline)
1001                                 fprintf(f,"\n");
1002                         var = cat->root;
1003                         while (var) {
1004                                 for (cmt = var->precomments; cmt; cmt=cmt->next)
1005                                 {
1006                                         if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1007                                                 fprintf(f,"%s", cmt->cmt);
1008                                 }
1009                                 if (var->sameline) 
1010                                         fprintf(f, "%s %s %s  %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1011                                 else    
1012                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1013                                 if (var->blanklines) {
1014                                         blanklines = var->blanklines;
1015                                         while (blanklines--)
1016                                                 fprintf(f, "\n");
1017                                 }
1018                                         
1019                                 var = var->next;
1020                         }
1021 #if 0
1022                         /* Put an empty line */
1023                         fprintf(f, "\n");
1024 #endif
1025                         cat = cat->next;
1026                 }
1027                 if ((option_verbose > 1) && !option_debug)
1028                         ast_verbose("Saved\n");
1029         } else {
1030                 if (option_debug)
1031                         ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
1032                 if (option_verbose > 1)
1033                         ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
1034                 return -1;
1035         }
1036         fclose(f);
1037         return 0;
1038 }
1039
1040 static void clear_config_maps(void) 
1041 {
1042         struct ast_config_map *map;
1043
1044         ast_mutex_lock(&config_lock);
1045
1046         while (config_maps) {
1047                 map = config_maps;
1048                 config_maps = config_maps->next;
1049                 free(map);
1050         }
1051                 
1052         ast_mutex_unlock(&config_lock);
1053 }
1054
1055 static int append_mapping(char *name, char *driver, char *database, char *table)
1056 {
1057         struct ast_config_map *map;
1058         int length;
1059
1060         length = sizeof(*map);
1061         length += strlen(name) + 1;
1062         length += strlen(driver) + 1;
1063         length += strlen(database) + 1;
1064         if (table)
1065                 length += strlen(table) + 1;
1066
1067         if (!(map = ast_calloc(1, length)))
1068                 return -1;
1069
1070         map->name = map->stuff;
1071         strcpy(map->name, name);
1072         map->driver = map->name + strlen(map->name) + 1;
1073         strcpy(map->driver, driver);
1074         map->database = map->driver + strlen(map->driver) + 1;
1075         strcpy(map->database, database);
1076         if (table) {
1077                 map->table = map->database + strlen(map->database) + 1;
1078                 strcpy(map->table, table);
1079         }
1080         map->next = config_maps;
1081
1082         if (option_verbose > 1)
1083                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1084                             map->name, map->driver, map->database, map->table ? map->table : map->name);
1085
1086         config_maps = map;
1087         return 0;
1088 }
1089
1090 int read_config_maps(void) 
1091 {
1092         struct ast_config *config, *configtmp;
1093         struct ast_variable *v;
1094         char *driver, *table, *database, *stringp, *tmp;
1095
1096         clear_config_maps();
1097
1098         configtmp = ast_config_new();
1099         configtmp->max_include_level = 1;
1100         config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1101         if (!config) {
1102                 ast_config_destroy(configtmp);
1103                 return 0;
1104         }
1105
1106         for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1107                 stringp = v->value;
1108                 driver = strsep(&stringp, ",");
1109
1110                 if ((tmp = strchr(stringp, '\"')))
1111                         stringp = tmp;
1112
1113                 /* check if the database text starts with a double quote */
1114                 if (*stringp == '"') {
1115                         stringp++;
1116                         database = strsep(&stringp, "\"");
1117                         strsep(&stringp, ",");
1118                 } else {
1119                         /* apparently this text has no quotes */
1120                         database = strsep(&stringp, ",");
1121                 }
1122
1123                 table = strsep(&stringp, ",");
1124
1125                 if (!strcmp(v->name, extconfig_conf)) {
1126                         ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1127                         continue;
1128                 }
1129
1130                 if (!strcmp(v->name, "asterisk.conf")) {
1131                         ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1132                         continue;
1133                 }
1134
1135                 if (!strcmp(v->name, "logger.conf")) {
1136                         ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1137                         continue;
1138                 }
1139
1140                 if (!driver || !database)
1141                         continue;
1142                 if (!strcasecmp(v->name, "sipfriends")) {
1143                         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");
1144                         append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1145                         append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1146                 } else if (!strcasecmp(v->name, "iaxfriends")) {
1147                         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");
1148                         append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1149                         append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1150                 } else 
1151                         append_mapping(v->name, driver, database, table);
1152         }
1153                 
1154         ast_config_destroy(config);
1155         return 0;
1156 }
1157
1158 int ast_config_engine_register(struct ast_config_engine *new) 
1159 {
1160         struct ast_config_engine *ptr;
1161
1162         ast_mutex_lock(&config_lock);
1163
1164         if (!config_engine_list) {
1165                 config_engine_list = new;
1166         } else {
1167                 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1168                 ptr->next = new;
1169         }
1170
1171         ast_mutex_unlock(&config_lock);
1172         ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1173
1174         return 1;
1175 }
1176
1177 int ast_config_engine_deregister(struct ast_config_engine *del) 
1178 {
1179         struct ast_config_engine *ptr, *last=NULL;
1180
1181         ast_mutex_lock(&config_lock);
1182
1183         for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1184                 if (ptr == del) {
1185                         if (last)
1186                                 last->next = ptr->next;
1187                         else
1188                                 config_engine_list = ptr->next;
1189                         break;
1190                 }
1191                 last = ptr;
1192         }
1193
1194         ast_mutex_unlock(&config_lock);
1195
1196         return 0;
1197 }
1198
1199 /*! \brief Find realtime engine for realtime family */
1200 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz) 
1201 {
1202         struct ast_config_engine *eng, *ret = NULL;
1203         struct ast_config_map *map;
1204
1205         ast_mutex_lock(&config_lock);
1206
1207         for (map = config_maps; map; map = map->next) {
1208                 if (!strcasecmp(family, map->name)) {
1209                         if (database)
1210                                 ast_copy_string(database, map->database, dbsiz);
1211                         if (table)
1212                                 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1213                         break;
1214                 }
1215         }
1216
1217         /* Check if the required driver (engine) exist */
1218         if (map) {
1219                 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1220                         if (!strcasecmp(eng->name, map->driver))
1221                                 ret = eng;
1222                 }
1223         }
1224
1225         ast_mutex_unlock(&config_lock);
1226         
1227         /* if we found a mapping, but the engine is not available, then issue a warning */
1228         if (map && !ret)
1229                 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1230
1231         return ret;
1232 }
1233
1234 static struct ast_config_engine text_file_engine = {
1235         .name = "text",
1236         .load_func = config_text_file_load,
1237 };
1238
1239 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1240 {
1241         char db[256];
1242         char table[256];
1243         struct ast_config_engine *loader = &text_file_engine;
1244         struct ast_config *result; 
1245
1246         if (cfg->include_level == cfg->max_include_level) {
1247                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1248                 return NULL;
1249         }
1250
1251         cfg->include_level++;
1252
1253         if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1254                 struct ast_config_engine *eng;
1255
1256                 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1257
1258
1259                 if (eng && eng->load_func) {
1260                         loader = eng;
1261                 } else {
1262                         eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1263                         if (eng && eng->load_func)
1264                                 loader = eng;
1265                 }
1266         }
1267
1268         result = loader->load_func(db, table, filename, cfg, withcomments);
1269
1270         if (result)
1271                 result->include_level--;
1272         else
1273                 cfg->include_level--;
1274
1275         return result;
1276 }
1277
1278 struct ast_config *ast_config_load(const char *filename)
1279 {
1280         struct ast_config *cfg;
1281         struct ast_config *result;
1282
1283         cfg = ast_config_new();
1284         if (!cfg)
1285                 return NULL;
1286
1287         result = ast_config_internal_load(filename, cfg, 0);
1288         if (!result)
1289                 ast_config_destroy(cfg);
1290
1291         return result;
1292 }
1293
1294 struct ast_config *ast_config_load_with_comments(const char *filename)
1295 {
1296         struct ast_config *cfg;
1297         struct ast_config *result;
1298
1299         cfg = ast_config_new();
1300         if (!cfg)
1301                 return NULL;
1302
1303         result = ast_config_internal_load(filename, cfg, 1);
1304         if (!result)
1305                 ast_config_destroy(cfg);
1306
1307         return result;
1308 }
1309
1310 static struct ast_variable *ast_load_realtime_helper(const char *family, va_list ap)
1311 {
1312         struct ast_config_engine *eng;
1313         char db[256]="";
1314         char table[256]="";
1315         struct ast_variable *res=NULL;
1316
1317         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1318         if (eng && eng->realtime_func) 
1319                 res = eng->realtime_func(db, table, ap);
1320
1321         return res;
1322 }
1323
1324 struct ast_variable *ast_load_realtime_all(const char *family, ...)
1325 {
1326         struct ast_variable *res;
1327         va_list ap;
1328
1329         va_start(ap, family);
1330         res = ast_load_realtime_helper(family, ap);
1331         va_end(ap);
1332
1333         return res;
1334 }
1335
1336 struct ast_variable *ast_load_realtime(const char *family, ...)
1337 {
1338         struct ast_variable *res, *cur, *prev = NULL, *freeme = NULL;
1339         va_list ap;
1340
1341         va_start(ap, family);
1342         res = ast_load_realtime_helper(family, ap);
1343         va_end(ap);
1344
1345         /* Eliminate blank entries */
1346         for (cur = res; cur; cur = cur->next) {
1347                 if (freeme) {
1348                         free(freeme);
1349                         freeme = NULL;
1350                 }
1351
1352                 if (ast_strlen_zero(cur->value)) {
1353                         if (prev)
1354                                 prev->next = cur->next;
1355                         else
1356                                 res = cur->next;
1357                         freeme = cur;
1358                 } else {
1359                         prev = cur;
1360                 }
1361         }
1362         return res;
1363 }
1364
1365 /*! \brief Check if realtime engine is configured for family */
1366 int ast_check_realtime(const char *family)
1367 {
1368         struct ast_config_engine *eng;
1369
1370         eng = find_engine(family, NULL, 0, NULL, 0);
1371         if (eng)
1372                 return 1;
1373         return 0;
1374
1375 }
1376
1377 /*! \brief Check if there's any realtime engines loaded */
1378 int ast_realtime_enabled()
1379 {
1380         return config_maps ? 1 : 0;
1381 }
1382
1383 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1384 {
1385         struct ast_config_engine *eng;
1386         char db[256]="";
1387         char table[256]="";
1388         struct ast_config *res=NULL;
1389         va_list ap;
1390
1391         va_start(ap, family);
1392         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1393         if (eng && eng->realtime_multi_func) 
1394                 res = eng->realtime_multi_func(db, table, ap);
1395         va_end(ap);
1396
1397         return res;
1398 }
1399
1400 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1401 {
1402         struct ast_config_engine *eng;
1403         int res = -1;
1404         char db[256]="";
1405         char table[256]="";
1406         va_list ap;
1407
1408         va_start(ap, lookup);
1409         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1410         if (eng && eng->update_func) 
1411                 res = eng->update_func(db, table, keyfield, lookup, ap);
1412         va_end(ap);
1413
1414         return res;
1415 }
1416
1417 int ast_store_realtime(const char *family, ...) {
1418         struct ast_config_engine *eng;
1419         int res = -1;
1420         char db[256]="";
1421         char table[256]="";
1422         va_list ap;
1423
1424         va_start(ap, family);
1425         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1426         if (eng && eng->store_func) 
1427                 res = eng->store_func(db, table, ap);
1428         va_end(ap);
1429
1430         return res;
1431 }
1432
1433 int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...) {
1434         struct ast_config_engine *eng;
1435         int res = -1;
1436         char db[256]="";
1437         char table[256]="";
1438         va_list ap;
1439
1440         va_start(ap, lookup);
1441         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1442         if (eng && eng->destroy_func) 
1443                 res = eng->destroy_func(db, table, keyfield, lookup, ap);
1444         va_end(ap);
1445
1446         return res;
1447 }
1448
1449 static int config_command(int fd, int argc, char **argv) 
1450 {
1451         struct ast_config_engine *eng;
1452         struct ast_config_map *map;
1453         
1454         ast_mutex_lock(&config_lock);
1455
1456         ast_cli(fd, "\n\n");
1457         for (eng = config_engine_list; eng; eng = eng->next) {
1458                 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1459                 for (map = config_maps; map; map = map->next)
1460                         if (!strcasecmp(map->driver, eng->name)) {
1461                                 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1462                                         map->table ? map->table : map->name);
1463                         }
1464         }
1465         ast_cli(fd,"\n\n");
1466         
1467         ast_mutex_unlock(&config_lock);
1468
1469         return 0;
1470 }
1471
1472 static char show_config_help[] =
1473         "Usage: core show config mappings\n"
1474         "       Shows the filenames to config engines.\n";
1475
1476 static struct ast_cli_entry cli_config[] = {
1477         { { "core", "show", "config", "mappings", NULL },
1478         config_command, "Display config mappings (file names to config engines)",
1479         show_config_help },
1480 };
1481
1482 int register_config_cli() 
1483 {
1484         ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
1485         return 0;
1486 }