Merged revisions 61959 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                                                 ast_config_destroy(cfg);
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                         if (option_debug)
833                                 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
834                         if (option_verbose > 1)
835                                 ast_verbose( "Not found (%s)\n", strerror(errno));
836                         continue;
837                 }
838                 count++;
839                 if (option_debug)
840                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
841                 if (option_verbose > 1)
842                         ast_verbose("Found\n");
843                 while (!feof(f)) {
844                         lineno++;
845                         if (fgets(buf, sizeof(buf), f)) {
846                                 if ( withcomments ) {
847                                         CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer);       /* add the current lline buffer to the comment buffer */
848                                         lline_buffer[0] = 0;        /* erase the lline buffer */
849                                 }
850                                 
851                                 new_buf = buf;
852                                 if (comment) 
853                                         process_buf = NULL;
854                                 else
855                                         process_buf = buf;
856                                 
857                                 while ((comment_p = strchr(new_buf, COMMENT_META))) {
858                                         if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
859                                                 /* Yuck, gotta memmove */
860                                                 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
861                                                 new_buf = comment_p;
862                                         } else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
863                                                 /* Meta-Comment start detected ";--" */
864                                                 if (comment < MAX_NESTED_COMMENTS) {
865                                                         *comment_p = '\0';
866                                                         new_buf = comment_p + 3;
867                                                         comment++;
868                                                         nest[comment-1] = lineno;
869                                                 } else {
870                                                         ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
871                                                 }
872                                         } else if ((comment_p >= new_buf + 2) &&
873                                                    (*(comment_p - 1) == COMMENT_TAG) &&
874                                                    (*(comment_p - 2) == COMMENT_TAG)) {
875                                                 /* Meta-Comment end detected */
876                                                 comment--;
877                                                 new_buf = comment_p + 1;
878                                                 if (!comment) {
879                                                         /* Back to non-comment now */
880                                                         if (process_buf) {
881                                                                 /* Actually have to move what's left over the top, then continue */
882                                                                 char *oldptr;
883                                                                 oldptr = process_buf + strlen(process_buf);
884                                                                 if ( withcomments ) {
885                                                                         CB_ADD(&comment_buffer, &comment_buffer_size, ";");
886                                                                         CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
887                                                                 }
888                                                                 
889                                                                 memmove(oldptr, new_buf, strlen(new_buf) + 1);
890                                                                 new_buf = oldptr;
891                                                         } else
892                                                                 process_buf = new_buf;
893                                                 }
894                                         } else {
895                                                 if (!comment) {
896                                                         /* If ; is found, and we are not nested in a comment, 
897                                                            we immediately stop all comment processing */
898                                                         if ( withcomments ) {
899                                                                 LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
900                                                         }
901                                                         *comment_p = '\0'; 
902                                                         new_buf = comment_p;
903                                                 } else
904                                                         new_buf = comment_p + 1;
905                                         }
906                                 }
907                                 if ( withcomments && comment && !process_buf )
908                                 {
909                                         CB_ADD(&comment_buffer, &comment_buffer_size, buf);  /* the whole line is a comment, store it */
910                                 }
911                                 
912                                 if (process_buf) {
913                                         char *buf = ast_strip(process_buf);
914                                         if (!ast_strlen_zero(buf)) {
915                                                 if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
916                                                         cfg = NULL;
917                                                         break;
918                                                 }
919                                         }
920                                 }
921                         }
922                 }
923                 fclose(f);              
924         } while (0);
925         if (comment) {
926                 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
927         }
928 #ifdef AST_INCLUDE_GLOB
929                                         if (!cfg)
930                                                 break;
931                                 }
932                                 globfree(&globbuf);
933                         }
934                 }
935 #endif
936
937         if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
938                 free(comment_buffer);
939                 free(lline_buffer);
940                 comment_buffer = NULL;
941                 lline_buffer = NULL;
942                 comment_buffer_size = 0;
943                 lline_buffer_size = 0;
944         }
945         
946         if (count == 0)
947                 return NULL;
948
949         return cfg;
950 }
951
952 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
953 {
954         FILE *f;
955         char fn[256];
956         char date[256]="";
957         time_t t;
958         struct ast_variable *var;
959         struct ast_category *cat;
960         struct ast_comment *cmt;
961         int blanklines = 0;
962
963         if (configfile[0] == '/') {
964                 ast_copy_string(fn, configfile, sizeof(fn));
965         } else {
966                 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
967         }
968         time(&t);
969         ast_copy_string(date, ctime(&t), sizeof(date));
970 #ifdef __CYGWIN__       
971         if ((f = fopen(fn, "w+"))) {
972 #else
973         if ((f = fopen(fn, "w"))) {
974 #endif      
975                 if (option_verbose > 1)
976                         ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
977                 fprintf(f, ";!\n");
978                 fprintf(f, ";! Automatically generated configuration file\n");
979                 if (strcmp(configfile, fn))
980                         fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
981                 else
982                         fprintf(f, ";! Filename: %s\n", configfile);
983                 fprintf(f, ";! Generator: %s\n", generator);
984                 fprintf(f, ";! Creation Date: %s", date);
985                 fprintf(f, ";!\n");
986                 cat = cfg->root;
987                 while (cat) {
988                         /* Dump section with any appropriate comment */
989                         for (cmt = cat->precomments; cmt; cmt=cmt->next)
990                         {
991                                 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
992                                         fprintf(f,"%s", cmt->cmt);
993                         }
994                         if (!cat->precomments)
995                                 fprintf(f,"\n");
996                         fprintf(f, "[%s]", cat->name);
997                         for (cmt = cat->sameline; cmt; cmt=cmt->next)
998                         {
999                                 fprintf(f,"%s", cmt->cmt);
1000                         }
1001                         if (!cat->sameline)
1002                                 fprintf(f,"\n");
1003                         var = cat->root;
1004                         while (var) {
1005                                 for (cmt = var->precomments; cmt; cmt=cmt->next)
1006                                 {
1007                                         if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1008                                                 fprintf(f,"%s", cmt->cmt);
1009                                 }
1010                                 if (var->sameline) 
1011                                         fprintf(f, "%s %s %s  %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1012                                 else    
1013                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1014                                 if (var->blanklines) {
1015                                         blanklines = var->blanklines;
1016                                         while (blanklines--)
1017                                                 fprintf(f, "\n");
1018                                 }
1019                                         
1020                                 var = var->next;
1021                         }
1022 #if 0
1023                         /* Put an empty line */
1024                         fprintf(f, "\n");
1025 #endif
1026                         cat = cat->next;
1027                 }
1028                 if ((option_verbose > 1) && !option_debug)
1029                         ast_verbose("Saved\n");
1030         } else {
1031                 if (option_debug)
1032                         ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
1033                 if (option_verbose > 1)
1034                         ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
1035                 return -1;
1036         }
1037         fclose(f);
1038         return 0;
1039 }
1040
1041 static void clear_config_maps(void) 
1042 {
1043         struct ast_config_map *map;
1044
1045         ast_mutex_lock(&config_lock);
1046
1047         while (config_maps) {
1048                 map = config_maps;
1049                 config_maps = config_maps->next;
1050                 free(map);
1051         }
1052                 
1053         ast_mutex_unlock(&config_lock);
1054 }
1055
1056 static int append_mapping(char *name, char *driver, char *database, char *table)
1057 {
1058         struct ast_config_map *map;
1059         int length;
1060
1061         length = sizeof(*map);
1062         length += strlen(name) + 1;
1063         length += strlen(driver) + 1;
1064         length += strlen(database) + 1;
1065         if (table)
1066                 length += strlen(table) + 1;
1067
1068         if (!(map = ast_calloc(1, length)))
1069                 return -1;
1070
1071         map->name = map->stuff;
1072         strcpy(map->name, name);
1073         map->driver = map->name + strlen(map->name) + 1;
1074         strcpy(map->driver, driver);
1075         map->database = map->driver + strlen(map->driver) + 1;
1076         strcpy(map->database, database);
1077         if (table) {
1078                 map->table = map->database + strlen(map->database) + 1;
1079                 strcpy(map->table, table);
1080         }
1081         map->next = config_maps;
1082
1083         if (option_verbose > 1)
1084                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1085                             map->name, map->driver, map->database, map->table ? map->table : map->name);
1086
1087         config_maps = map;
1088         return 0;
1089 }
1090
1091 int read_config_maps(void) 
1092 {
1093         struct ast_config *config, *configtmp;
1094         struct ast_variable *v;
1095         char *driver, *table, *database, *stringp, *tmp;
1096
1097         clear_config_maps();
1098
1099         configtmp = ast_config_new();
1100         configtmp->max_include_level = 1;
1101         config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1102         if (!config) {
1103                 ast_config_destroy(configtmp);
1104                 return 0;
1105         }
1106
1107         for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1108                 stringp = v->value;
1109                 driver = strsep(&stringp, ",");
1110
1111                 if ((tmp = strchr(stringp, '\"')))
1112                         stringp = tmp;
1113
1114                 /* check if the database text starts with a double quote */
1115                 if (*stringp == '"') {
1116                         stringp++;
1117                         database = strsep(&stringp, "\"");
1118                         strsep(&stringp, ",");
1119                 } else {
1120                         /* apparently this text has no quotes */
1121                         database = strsep(&stringp, ",");
1122                 }
1123
1124                 table = strsep(&stringp, ",");
1125
1126                 if (!strcmp(v->name, extconfig_conf)) {
1127                         ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1128                         continue;
1129                 }
1130
1131                 if (!strcmp(v->name, "asterisk.conf")) {
1132                         ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1133                         continue;
1134                 }
1135
1136                 if (!strcmp(v->name, "logger.conf")) {
1137                         ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1138                         continue;
1139                 }
1140
1141                 if (!driver || !database)
1142                         continue;
1143                 if (!strcasecmp(v->name, "sipfriends")) {
1144                         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");
1145                         append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1146                         append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1147                 } else if (!strcasecmp(v->name, "iaxfriends")) {
1148                         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");
1149                         append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1150                         append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1151                 } else 
1152                         append_mapping(v->name, driver, database, table);
1153         }
1154                 
1155         ast_config_destroy(config);
1156         return 0;
1157 }
1158
1159 int ast_config_engine_register(struct ast_config_engine *new) 
1160 {
1161         struct ast_config_engine *ptr;
1162
1163         ast_mutex_lock(&config_lock);
1164
1165         if (!config_engine_list) {
1166                 config_engine_list = new;
1167         } else {
1168                 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1169                 ptr->next = new;
1170         }
1171
1172         ast_mutex_unlock(&config_lock);
1173         ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1174
1175         return 1;
1176 }
1177
1178 int ast_config_engine_deregister(struct ast_config_engine *del) 
1179 {
1180         struct ast_config_engine *ptr, *last=NULL;
1181
1182         ast_mutex_lock(&config_lock);
1183
1184         for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1185                 if (ptr == del) {
1186                         if (last)
1187                                 last->next = ptr->next;
1188                         else
1189                                 config_engine_list = ptr->next;
1190                         break;
1191                 }
1192                 last = ptr;
1193         }
1194
1195         ast_mutex_unlock(&config_lock);
1196
1197         return 0;
1198 }
1199
1200 /*! \brief Find realtime engine for realtime family */
1201 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz) 
1202 {
1203         struct ast_config_engine *eng, *ret = NULL;
1204         struct ast_config_map *map;
1205
1206         ast_mutex_lock(&config_lock);
1207
1208         for (map = config_maps; map; map = map->next) {
1209                 if (!strcasecmp(family, map->name)) {
1210                         if (database)
1211                                 ast_copy_string(database, map->database, dbsiz);
1212                         if (table)
1213                                 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1214                         break;
1215                 }
1216         }
1217
1218         /* Check if the required driver (engine) exist */
1219         if (map) {
1220                 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1221                         if (!strcasecmp(eng->name, map->driver))
1222                                 ret = eng;
1223                 }
1224         }
1225
1226         ast_mutex_unlock(&config_lock);
1227         
1228         /* if we found a mapping, but the engine is not available, then issue a warning */
1229         if (map && !ret)
1230                 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1231
1232         return ret;
1233 }
1234
1235 static struct ast_config_engine text_file_engine = {
1236         .name = "text",
1237         .load_func = config_text_file_load,
1238 };
1239
1240 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1241 {
1242         char db[256];
1243         char table[256];
1244         struct ast_config_engine *loader = &text_file_engine;
1245         struct ast_config *result; 
1246
1247         if (cfg->include_level == cfg->max_include_level) {
1248                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1249                 return NULL;
1250         }
1251
1252         cfg->include_level++;
1253
1254         if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1255                 struct ast_config_engine *eng;
1256
1257                 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1258
1259
1260                 if (eng && eng->load_func) {
1261                         loader = eng;
1262                 } else {
1263                         eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1264                         if (eng && eng->load_func)
1265                                 loader = eng;
1266                 }
1267         }
1268
1269         result = loader->load_func(db, table, filename, cfg, withcomments);
1270
1271         if (result)
1272                 result->include_level--;
1273         else
1274                 cfg->include_level--;
1275
1276         return result;
1277 }
1278
1279 struct ast_config *ast_config_load(const char *filename)
1280 {
1281         struct ast_config *cfg;
1282         struct ast_config *result;
1283
1284         cfg = ast_config_new();
1285         if (!cfg)
1286                 return NULL;
1287
1288         result = ast_config_internal_load(filename, cfg, 0);
1289         if (!result)
1290                 ast_config_destroy(cfg);
1291
1292         return result;
1293 }
1294
1295 struct ast_config *ast_config_load_with_comments(const char *filename)
1296 {
1297         struct ast_config *cfg;
1298         struct ast_config *result;
1299
1300         cfg = ast_config_new();
1301         if (!cfg)
1302                 return NULL;
1303
1304         result = ast_config_internal_load(filename, cfg, 1);
1305         if (!result)
1306                 ast_config_destroy(cfg);
1307
1308         return result;
1309 }
1310
1311 static struct ast_variable *ast_load_realtime_helper(const char *family, va_list ap)
1312 {
1313         struct ast_config_engine *eng;
1314         char db[256]="";
1315         char table[256]="";
1316         struct ast_variable *res=NULL;
1317
1318         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1319         if (eng && eng->realtime_func) 
1320                 res = eng->realtime_func(db, table, ap);
1321
1322         return res;
1323 }
1324
1325 struct ast_variable *ast_load_realtime_all(const char *family, ...)
1326 {
1327         struct ast_variable *res;
1328         va_list ap;
1329
1330         va_start(ap, family);
1331         res = ast_load_realtime_helper(family, ap);
1332         va_end(ap);
1333
1334         return res;
1335 }
1336
1337 struct ast_variable *ast_load_realtime(const char *family, ...)
1338 {
1339         struct ast_variable *res, *cur, *prev = NULL, *freeme = NULL;
1340         va_list ap;
1341
1342         va_start(ap, family);
1343         res = ast_load_realtime_helper(family, ap);
1344         va_end(ap);
1345
1346         /* Eliminate blank entries */
1347         for (cur = res; cur; cur = cur->next) {
1348                 if (freeme) {
1349                         free(freeme);
1350                         freeme = NULL;
1351                 }
1352
1353                 if (ast_strlen_zero(cur->value)) {
1354                         if (prev)
1355                                 prev->next = cur->next;
1356                         else
1357                                 res = cur->next;
1358                         freeme = cur;
1359                 } else {
1360                         prev = cur;
1361                 }
1362         }
1363         return res;
1364 }
1365
1366 /*! \brief Check if realtime engine is configured for family */
1367 int ast_check_realtime(const char *family)
1368 {
1369         struct ast_config_engine *eng;
1370
1371         eng = find_engine(family, NULL, 0, NULL, 0);
1372         if (eng)
1373                 return 1;
1374         return 0;
1375
1376 }
1377
1378 /*! \brief Check if there's any realtime engines loaded */
1379 int ast_realtime_enabled()
1380 {
1381         return config_maps ? 1 : 0;
1382 }
1383
1384 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1385 {
1386         struct ast_config_engine *eng;
1387         char db[256]="";
1388         char table[256]="";
1389         struct ast_config *res=NULL;
1390         va_list ap;
1391
1392         va_start(ap, family);
1393         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1394         if (eng && eng->realtime_multi_func) 
1395                 res = eng->realtime_multi_func(db, table, ap);
1396         va_end(ap);
1397
1398         return res;
1399 }
1400
1401 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1402 {
1403         struct ast_config_engine *eng;
1404         int res = -1;
1405         char db[256]="";
1406         char table[256]="";
1407         va_list ap;
1408
1409         va_start(ap, lookup);
1410         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1411         if (eng && eng->update_func) 
1412                 res = eng->update_func(db, table, keyfield, lookup, ap);
1413         va_end(ap);
1414
1415         return res;
1416 }
1417
1418 int ast_store_realtime(const char *family, ...) {
1419         struct ast_config_engine *eng;
1420         int res = -1;
1421         char db[256]="";
1422         char table[256]="";
1423         va_list ap;
1424
1425         va_start(ap, family);
1426         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1427         if (eng && eng->store_func) 
1428                 res = eng->store_func(db, table, ap);
1429         va_end(ap);
1430
1431         return res;
1432 }
1433
1434 int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...) {
1435         struct ast_config_engine *eng;
1436         int res = -1;
1437         char db[256]="";
1438         char table[256]="";
1439         va_list ap;
1440
1441         va_start(ap, lookup);
1442         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1443         if (eng && eng->destroy_func) 
1444                 res = eng->destroy_func(db, table, keyfield, lookup, ap);
1445         va_end(ap);
1446
1447         return res;
1448 }
1449
1450 static int config_command(int fd, int argc, char **argv) 
1451 {
1452         struct ast_config_engine *eng;
1453         struct ast_config_map *map;
1454         
1455         ast_mutex_lock(&config_lock);
1456
1457         ast_cli(fd, "\n\n");
1458         for (eng = config_engine_list; eng; eng = eng->next) {
1459                 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1460                 for (map = config_maps; map; map = map->next)
1461                         if (!strcasecmp(map->driver, eng->name)) {
1462                                 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1463                                         map->table ? map->table : map->name);
1464                         }
1465         }
1466         ast_cli(fd,"\n\n");
1467         
1468         ast_mutex_unlock(&config_lock);
1469
1470         return 0;
1471 }
1472
1473 static char show_config_help[] =
1474         "Usage: core show config mappings\n"
1475         "       Shows the filenames to config engines.\n";
1476
1477 static struct ast_cli_entry cli_config[] = {
1478         { { "core", "show", "config", "mappings", NULL },
1479         config_command, "Display config mappings (file names to config engines)",
1480         show_config_help },
1481 };
1482
1483 int register_config_cli() 
1484 {
1485         ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
1486         return 0;
1487 }