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