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