capitalize dialplan function
[asterisk/asterisk.git] / 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 /*
20  *
21  * Configuration File Parser
22  * 
23  */
24
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <time.h>
31 #define AST_INCLUDE_GLOB 1
32 #ifdef AST_INCLUDE_GLOB
33 #ifdef __Darwin__
34 #define GLOB_ABORTED GLOB_ABEND
35 #endif
36 # include <glob.h>
37 #endif
38
39 #include "asterisk.h"
40
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42
43 #include "asterisk/config.h"
44 #include "asterisk/cli.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/options.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/app.h"
51
52 #define MAX_NESTED_COMMENTS 128
53 #define COMMENT_START ";--"
54 #define COMMENT_END "--;"
55 #define COMMENT_META ';'
56 #define COMMENT_TAG '-'
57
58 static char *extconfig_conf = "extconfig.conf";
59
60 static struct ast_config_map {
61         struct ast_config_map *next;
62         char *name;
63         char *driver;
64         char *database;
65         char *table;
66         char stuff[0];
67 } *config_maps = NULL;
68
69 AST_MUTEX_DEFINE_STATIC(config_lock);
70 static struct ast_config_engine *config_engine_list;
71
72 #define MAX_INCLUDE_LEVEL 10
73
74 struct ast_comment {
75         struct ast_comment *next;
76         char cmt[0];
77 };
78
79 struct ast_category {
80         char name[80];
81         int ignored;                    /* do not let user of the config see this category */
82         struct ast_variable *root;
83         struct ast_variable *last;
84         struct ast_category *next;
85 };
86
87 struct ast_config {
88         struct ast_category *root;
89         struct ast_category *last;
90         struct ast_category *current;
91         struct ast_category *last_browse;               /* used to cache the last category supplied via category_browse */
92         int include_level;
93         int max_include_level;
94 };
95
96 struct ast_variable *ast_variable_new(const char *name, const char *value) 
97 {
98         struct ast_variable *variable;
99
100         int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
101         variable = malloc(length);
102         if (variable) {
103                 memset(variable, 0, length);
104                 variable->name = variable->stuff;
105                 variable->value = variable->stuff + strlen(name) + 1;           
106                 strcpy(variable->name,name);
107                 strcpy(variable->value,value);
108         }
109
110         return variable;
111 }
112
113 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
114 {
115         if (category->last)
116                 category->last->next = variable;
117         else
118                 category->root = variable;
119         category->last = variable;
120 }
121
122 void ast_variables_destroy(struct ast_variable *v)
123 {
124         struct ast_variable *vn;
125
126         while(v) {
127                 vn = v;
128                 v = v->next;
129                 free(vn);
130         }
131 }
132
133 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
134 {
135         struct ast_category *cat = NULL;
136
137         if (category && config->last_browse && (config->last_browse->name == category))
138                 cat = config->last_browse;
139         else
140                 cat = ast_category_get(config, category);
141
142         if (cat)
143                 return cat->root;
144         else
145                 return NULL;
146 }
147
148 char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
149 {
150         struct ast_variable *v;
151
152         if (category) {
153                 for (v = ast_variable_browse(config, category); v; v = v->next)
154                         if (variable == v->name)
155                                 return v->value;
156                 for (v = ast_variable_browse(config, category); v; v = v->next)
157                         if (!strcasecmp(variable, v->name))
158                                 return v->value;
159         } else {
160                 struct ast_category *cat;
161
162                 for (cat = config->root; cat; cat = cat->next)
163                         for (v = cat->root; v; v = v->next)
164                                 if (!strcasecmp(variable, v->name))
165                                         return v->value;
166         }
167
168         return NULL;
169 }
170
171 static struct ast_variable *variable_clone(const struct ast_variable *old)
172 {
173         struct ast_variable *new = ast_variable_new(old->name, old->value);
174
175         if (new) {
176                 new->lineno = old->lineno;
177                 new->object = old->object;
178                 new->blanklines = old->blanklines;
179                 /* TODO: clone comments? */
180         }
181
182         return new;
183 }
184  
185 static void move_variables(struct ast_category *old, struct ast_category *new)
186 {
187         struct ast_variable *var;
188         struct ast_variable *next;
189
190         next = old->root;
191         old->root = NULL;
192         for (var = next; var; var = next) {
193                 next = var->next;
194                 var->next = NULL;
195                 ast_variable_append(new, var);
196         }
197 }
198
199 struct ast_category *ast_category_new(const char *name) 
200 {
201         struct ast_category *category;
202
203         category = malloc(sizeof(struct ast_category));
204         if (category) {
205                 memset(category, 0, sizeof(struct ast_category));
206                 ast_copy_string(category->name, name, sizeof(category->name));
207         }
208
209         return category;
210 }
211
212 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
213 {
214         struct ast_category *cat;
215
216         for (cat = config->root; cat; cat = cat->next) {
217                 if (cat->name == category_name && (ignored || !cat->ignored))
218                         return cat;
219         }
220
221         for (cat = config->root; cat; cat = cat->next) {
222                 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
223                         return cat;
224         }
225
226         return NULL;
227 }
228
229 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
230 {
231         return category_get(config, category_name, 0);
232 }
233
234 int ast_category_exist(const struct ast_config *config, const char *category_name)
235 {
236         return !!ast_category_get(config, category_name);
237 }
238
239 void ast_category_append(struct ast_config *config, struct ast_category *category)
240 {
241         if (config->last)
242                 config->last->next = category;
243         else
244                 config->root = category;
245         config->last = category;
246         config->current = category;
247 }
248
249 void ast_category_destroy(struct ast_category *cat)
250 {
251         ast_variables_destroy(cat->root);
252         free(cat);
253 }
254
255 static struct ast_category *next_available_category(struct ast_category *cat)
256 {
257         for (; cat && cat->ignored; cat = cat->next);
258
259         return cat;
260 }
261
262 char *ast_category_browse(struct ast_config *config, const char *prev)
263 {       
264         struct ast_category *cat = NULL;
265
266         if (prev && config->last_browse && (config->last_browse->name == prev))
267                 cat = config->last_browse->next;
268         else if (!prev && config->root)
269                         cat = config->root;
270         else if (prev) {
271                 for (cat = config->root; cat; cat = cat->next) {
272                         if (cat->name == prev) {
273                                 cat = cat->next;
274                                 break;
275                         }
276                 }
277                 if (!cat) {
278                         for (cat = config->root; cat; cat = cat->next) {
279                                 if (!strcasecmp(cat->name, prev)) {
280                                         cat = cat->next;
281                                         break;
282                                 }
283                         }
284                 }
285         }
286         
287         if (cat)
288                 cat = next_available_category(cat);
289
290         config->last_browse = cat;
291         if (cat)
292                 return cat->name;
293         else
294                 return NULL;
295 }
296
297 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
298 {
299         struct ast_variable *v;
300
301         v = cat->root;
302         cat->root = NULL;
303
304         return v;
305 }
306
307 void ast_category_rename(struct ast_category *cat, const char *name)
308 {
309         ast_copy_string(cat->name, name, sizeof(cat->name));
310 }
311
312 static void inherit_category(struct ast_category *new, const struct ast_category *base)
313 {
314         struct ast_variable *var;
315
316         for (var = base->root; var; var = var->next) {
317                 struct ast_variable *v;
318                 
319                 v = variable_clone(var);
320                 if (v)
321                         ast_variable_append(new, v);
322         }
323 }
324
325 struct ast_config *ast_config_new(void) 
326 {
327         struct ast_config *config;
328
329         config = malloc(sizeof(*config));
330         if (config) {
331                 memset(config, 0, sizeof(*config));
332                 config->max_include_level = MAX_INCLUDE_LEVEL;
333         }
334
335         return config;
336 }
337
338 void ast_config_destroy(struct ast_config *cfg)
339 {
340         struct ast_category *cat, *catn;
341
342         if (!cfg)
343                 return;
344
345         cat = cfg->root;
346         while(cat) {
347                 ast_variables_destroy(cat->root);
348                 catn = cat;
349                 cat = cat->next;
350                 free(catn);
351         }
352         free(cfg);
353 }
354
355 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
356 {
357         return cfg->current;
358 }
359
360 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
361 {
362         /* cast below is just to silence compiler warning about dropping "const" */
363         cfg->current = (struct ast_category *) cat;
364 }
365
366 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
367 {
368         char *c;
369         char *cur = buf;
370         struct ast_variable *v;
371         char cmd[512], exec_file[512];
372         int object, do_exec, do_include;
373
374         /* Actually parse the entry */
375         if (cur[0] == '[') {
376                 struct ast_category *newcat = NULL;
377                 char *catname;
378
379                 /* A category header */
380                 c = strchr(cur, ']');
381                 if (!c) {
382                         ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
383                         return -1;
384                 }
385                 *c++ = '\0';
386                 cur++;
387                 if (*c++ != '(')
388                         c = NULL;
389                 catname = cur;
390                 *cat = newcat = ast_category_new(catname);
391                 if (!newcat) {
392                         ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
393                         return -1;
394                 }
395                 /* If there are options or categories to inherit from, process them now */
396                 if (c) {
397                         if (!(cur = strchr(c, ')'))) {
398                                 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
399                                 return -1;
400                         }
401                         *cur = '\0';
402                         while ((cur = strsep(&c, ","))) {
403                                 if (!strcasecmp(cur, "!")) {
404                                         (*cat)->ignored = 1;
405                                 } else if (!strcasecmp(cur, "+")) {
406                                         *cat = category_get(cfg, catname, 1);
407                                         if (!*cat) {
408                                                 ast_config_destroy(cfg);
409                                                 if (newcat)
410                                                         ast_category_destroy(newcat);
411                                                 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
412                                                 return -1;
413                                         }
414                                         if (newcat) {
415                                                 move_variables(newcat, *cat);
416                                                 ast_category_destroy(newcat);
417                                                 newcat = NULL;
418                                         }
419                                 } else {
420                                         struct ast_category *base;
421                                 
422                                         base = category_get(cfg, cur, 1);
423                                         if (!base) {
424                                                 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
425                                                 return -1;
426                                         }
427                                         inherit_category(*cat, base);
428                                 }
429                         }
430                 }
431                 if (newcat)
432                         ast_category_append(cfg, *cat);
433         } else if (cur[0] == '#') {
434                 /* A directive */
435                 cur++;
436                 c = cur;
437                 while(*c && (*c > 32)) c++;
438                 if (*c) {
439                         *c = '\0';
440                         c++;
441                         /* Find real argument */
442                         while(*c  && (*c < 33)) c++;
443                         if (!*c)
444                                 c = NULL;
445                 } else 
446                         c = NULL;
447                 do_include = !strcasecmp(cur, "include");
448                 if(!do_include)
449                         do_exec = !strcasecmp(cur, "exec");
450                 else
451                         do_exec = 0;
452                 if (do_exec && !option_exec_includes) {
453                         ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
454                         do_exec = 0;
455                 }
456                 if (do_include || do_exec) {
457                         if (c) {
458                                 /* Strip off leading and trailing "'s and <>'s */
459                                 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
460                                 /* Get rid of leading mess */
461                                 cur = c;
462                                 while (!ast_strlen_zero(cur)) {
463                                         c = cur + strlen(cur) - 1;
464                                         if ((*c == '>') || (*c == '<') || (*c == '\"'))
465                                                 *c = '\0';
466                                         else
467                                                 break;
468                                 }
469                                 /* #exec </path/to/executable>
470                                    We create a tmp file, then we #include it, then we delete it. */
471                                 if (do_exec) { 
472                                         snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%ld.%ld", time(NULL), (long)pthread_self());
473                                         snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
474                                         ast_safe_system(cmd);
475                                         cur = exec_file;
476                                 } else
477                                         exec_file[0] = '\0';
478                                 /* A #include */
479                                 do_include = ast_config_internal_load(cur, cfg) ? 1 : 0;
480                                 if(!ast_strlen_zero(exec_file))
481                                         unlink(exec_file);
482                                 if(!do_include)
483                                         return 0;
484
485                         } else {
486                                 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n", 
487                                                 do_exec ? "exec" : "include",
488                                                 do_exec ? "/path/to/executable" : "filename",
489                                                 lineno,
490                                                 configfile);
491                         }
492                 }
493                 else 
494                         ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
495         } else {
496                 /* Just a line (variable = value) */
497                 if (!*cat) {
498                         ast_log(LOG_WARNING,
499                                 "parse error: No category context for line %d of %s\n", lineno, configfile);
500                         return -1;
501                 }
502                 c = strchr(cur, '=');
503                 if (c) {
504                         *c = 0;
505                         c++;
506                         /* Ignore > in => */
507                         if (*c== '>') {
508                                 object = 1;
509                                 c++;
510                         } else
511                                 object = 0;
512                         v = ast_variable_new(ast_strip(cur), ast_strip(c));
513                         if (v) {
514                                 v->lineno = lineno;
515                                 v->object = object;
516                                 /* Put and reset comments */
517                                 v->blanklines = 0;
518                                 ast_variable_append(*cat, v);
519                         } else {
520                                 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
521                                 return -1;
522                         }
523                 } else {
524                         ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
525                 }
526
527         }
528         return 0;
529 }
530
531 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg)
532 {
533         char fn[256];
534         char buf[8192];
535         char *new_buf, *comment_p, *process_buf;
536         FILE *f;
537         int lineno=0;
538         int comment = 0, nest[MAX_NESTED_COMMENTS];
539         struct ast_category *cat = NULL;
540         int count = 0;
541         
542         cat = ast_config_get_current_category(cfg);
543
544         if (filename[0] == '/') {
545                 ast_copy_string(fn, filename, sizeof(fn));
546         } else {
547                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
548         }
549
550 #ifdef AST_INCLUDE_GLOB
551         {
552                 int glob_ret;
553                 glob_t globbuf;
554                 globbuf.gl_offs = 0;    /* initialize it to silence gcc */
555 #ifdef SOLARIS
556                 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
557 #else
558                 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
559 #endif
560                 if (glob_ret == GLOB_NOSPACE)
561                         ast_log(LOG_WARNING,
562                                 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
563                 else if (glob_ret  == GLOB_ABORTED)
564                         ast_log(LOG_WARNING,
565                                 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
566                 else  {
567                         /* loop over expanded files */
568                         int i;
569                         for (i=0; i<globbuf.gl_pathc; i++) {
570                                 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
571 #endif
572         if ((option_verbose > 1) && !option_debug) {
573                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
574                 fflush(stdout);
575         }
576         if ((f = fopen(fn, "r"))) {
577                 count++;
578                 if (option_debug)
579                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
580                 else if (option_verbose > 1)
581                         ast_verbose("Found\n");
582                 while(!feof(f)) {
583                         lineno++;
584                         if (fgets(buf, sizeof(buf), f)) {
585                                 new_buf = buf;
586                                 if (comment)
587                                         process_buf = NULL;
588                                 else
589                                         process_buf = buf;
590                                 while ((comment_p = strchr(new_buf, COMMENT_META))) {
591                                         if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
592                                                 /* Yuck, gotta memmove */
593                                                 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
594                                                 new_buf = comment_p;
595                                         } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
596                                                 /* Meta-Comment start detected ";--" */
597                                                 if (comment < MAX_NESTED_COMMENTS) {
598                                                         *comment_p = '\0';
599                                                         new_buf = comment_p + 3;
600                                                         comment++;
601                                                         nest[comment-1] = lineno;
602                                                 } else {
603                                                         ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
604                                                 }
605                                         } else if ((comment_p >= new_buf + 2) &&
606                                                    (*(comment_p - 1) == COMMENT_TAG) &&
607                                                    (*(comment_p - 2) == COMMENT_TAG)) {
608                                                 /* Meta-Comment end detected */
609                                                 comment--;
610                                                 new_buf = comment_p + 1;
611                                                 if (!comment) {
612                                                         /* Back to non-comment now */
613                                                         if (process_buf) {
614                                                                 /* Actually have to move what's left over the top, then continue */
615                                                                 char *oldptr;
616                                                                 oldptr = process_buf + strlen(process_buf);
617                                                                 memmove(oldptr, new_buf, strlen(new_buf) + 1);
618                                                                 new_buf = oldptr;
619                                                         } else
620                                                                 process_buf = new_buf;
621                                                 }
622                                         } else {
623                                                 if (!comment) {
624                                                         /* If ; is found, and we are not nested in a comment, 
625                                                            we immediately stop all comment processing */
626                                                         *comment_p = '\0'; 
627                                                         new_buf = comment_p;
628                                                 } else
629                                                         new_buf = comment_p + 1;
630                                         }
631                                 }
632                                 if (process_buf) {
633                                         char *buf = ast_strip(process_buf);
634                                         if (!ast_strlen_zero(buf))
635                                                 if (process_text_line(cfg, &cat, buf, lineno, filename)) {
636                                                         cfg = NULL;
637                                                         break;
638                                                 }
639                                 }
640                         }
641                 }
642                 fclose(f);              
643         } else { /* can't open file */
644                 if (option_debug)
645                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
646                 else if (option_verbose > 1)
647                         ast_verbose( "Not found (%s)\n", strerror(errno));
648         }
649         if (comment) {
650                 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
651         }
652 #ifdef AST_INCLUDE_GLOB
653                                         if (!cfg)
654                                                 break;
655                                 }
656                                 globfree(&globbuf);
657                         }
658                 }
659 #endif
660         if (count == 0)
661                 return NULL;
662
663         return cfg;
664 }
665
666 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
667 {
668         FILE *f;
669         char fn[256];
670         char date[256]="";
671         time_t t;
672         struct ast_variable *var;
673         struct ast_category *cat;
674         int blanklines = 0;
675
676         if (configfile[0] == '/') {
677                 ast_copy_string(fn, configfile, sizeof(fn));
678         } else {
679                 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
680         }
681         time(&t);
682         ast_copy_string(date, ctime(&t), sizeof(date));
683         if ((f = fopen(fn, "w"))) {
684                 if ((option_verbose > 1) && !option_debug)
685                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
686                 fprintf(f, ";!\n");
687                 fprintf(f, ";! Automatically generated configuration file\n");
688                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
689                 fprintf(f, ";! Generator: %s\n", generator);
690                 fprintf(f, ";! Creation Date: %s", date);
691                 fprintf(f, ";!\n");
692                 cat = cfg->root;
693                 while(cat) {
694                         /* Dump section with any appropriate comment */
695                         fprintf(f, "[%s]\n", cat->name);
696                         var = cat->root;
697                         while(var) {
698                                 if (var->sameline) 
699                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
700                                 else    
701                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
702                                 if (var->blanklines) {
703                                         blanklines = var->blanklines;
704                                         while (blanklines--)
705                                                 fprintf(f, "\n");
706                                 }
707                                         
708                                 var = var->next;
709                         }
710 #if 0
711                         /* Put an empty line */
712                         fprintf(f, "\n");
713 #endif
714                         cat = cat->next;
715                 }
716         } else {
717                 if (option_debug)
718                         printf("Unable to open for writing: %s\n", fn);
719                 else if (option_verbose > 1)
720                         printf( "Unable to write (%s)", strerror(errno));
721                 return -1;
722         }
723         fclose(f);
724         return 0;
725 }
726
727 static void clear_config_maps(void) 
728 {
729         struct ast_config_map *map;
730
731         ast_mutex_lock(&config_lock);
732
733         while (config_maps) {
734                 map = config_maps;
735                 config_maps = config_maps->next;
736                 free(map);
737         }
738                 
739         ast_mutex_unlock(&config_lock);
740 }
741
742 static int append_mapping(char *name, char *driver, char *database, char *table)
743 {
744         struct ast_config_map *map;
745         int length;
746
747         length = sizeof(*map);
748         length += strlen(name) + 1;
749         length += strlen(driver) + 1;
750         length += strlen(database) + 1;
751         if (table)
752                 length += strlen(table) + 1;
753         map = malloc(length);
754
755         if (!map)
756                 return -1;
757
758         memset(map, 0, length);
759         map->name = map->stuff;
760         strcpy(map->name, name);
761         map->driver = map->name + strlen(map->name) + 1;
762         strcpy(map->driver, driver);
763         map->database = map->driver + strlen(map->driver) + 1;
764         strcpy(map->database, database);
765         if (table) {
766                 map->table = map->database + strlen(map->database) + 1;
767                 strcpy(map->table, table);
768         }
769         map->next = config_maps;
770
771         if (option_verbose > 1)
772                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
773                             map->name, map->driver, map->database, map->table ? map->table : map->name);
774
775         config_maps = map;
776         return 0;
777 }
778
779 void read_config_maps(void) 
780 {
781         struct ast_config *config, *configtmp;
782         struct ast_variable *v;
783         char *driver, *table, *database, *stringp;
784
785         clear_config_maps();
786
787         configtmp = ast_config_new();
788         configtmp->max_include_level = 1;
789         config = ast_config_internal_load(extconfig_conf, configtmp);
790         if (!config) {
791                 ast_config_destroy(configtmp);
792                 return;
793         }
794
795         for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
796                 stringp = v->value;
797                 driver = strsep(&stringp, ",");
798                 database = strsep(&stringp, ",");
799                 table = strsep(&stringp, ",");
800                         
801                 if (!strcmp(v->name, extconfig_conf)) {
802                         ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
803                         continue;
804                 }
805
806                 if (!strcmp(v->name, "asterisk.conf")) {
807                         ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
808                         continue;
809                 }
810
811                 if (!strcmp(v->name, "logger.conf")) {
812                         ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
813                         continue;
814                 }
815
816                 if (!driver || !database)
817                         continue;
818                 if (!strcasecmp(v->name, "sipfriends")) {
819                         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");
820                         append_mapping("sipusers", driver, database, table ? table : "sipfriends");
821                         append_mapping("sippeers", driver, database, table ? table : "sipfriends");
822                 } else if (!strcasecmp(v->name, "iaxfriends")) {
823                         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");
824                         append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
825                         append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
826                 } else 
827                         append_mapping(v->name, driver, database, table);
828         }
829                 
830         ast_config_destroy(config);
831 }
832
833 int ast_config_engine_register(struct ast_config_engine *new) 
834 {
835         struct ast_config_engine *ptr;
836
837         ast_mutex_lock(&config_lock);
838
839         if (!config_engine_list) {
840                 config_engine_list = new;
841         } else {
842                 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
843                 ptr->next = new;
844         }
845
846         ast_mutex_unlock(&config_lock);
847         ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
848
849         return 1;
850 }
851
852 int ast_config_engine_deregister(struct ast_config_engine *del) 
853 {
854         struct ast_config_engine *ptr, *last=NULL;
855
856         ast_mutex_lock(&config_lock);
857
858         for (ptr = config_engine_list; ptr; ptr=ptr->next) {
859                 if (ptr == del) {
860                         if (last)
861                                 last->next = ptr->next;
862                         else
863                                 config_engine_list = ptr->next;
864                         break;
865                 }
866                 last = ptr;
867         }
868
869         ast_mutex_unlock(&config_lock);
870
871         return 0;
872 }
873
874 /*--- find_engine: Find realtime engine for realtime family */
875 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz) 
876 {
877         struct ast_config_engine *eng, *ret = NULL;
878         struct ast_config_map *map;
879
880         ast_mutex_lock(&config_lock);
881
882         for (map = config_maps; map; map = map->next) {
883                 if (!strcasecmp(family, map->name)) {
884                         if (database)
885                                 ast_copy_string(database, map->database, dbsiz);
886                         if (table)
887                                 ast_copy_string(table, map->table ? map->table : family, tabsiz);
888                         break;
889                 }
890         }
891
892         /* Check if the required driver (engine) exist */
893         if (map) {
894                 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
895                         if (!strcasecmp(eng->name, map->driver))
896                                 ret = eng;
897                 }
898         }
899
900         ast_mutex_unlock(&config_lock);
901         
902         /* if we found a mapping, but the engine is not available, then issue a warning */
903         if (map && !ret)
904                 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
905
906         return ret;
907 }
908
909 static struct ast_config_engine text_file_engine = {
910         .name = "text",
911         .load_func = config_text_file_load,
912 };
913
914 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg)
915 {
916         char db[256];
917         char table[256];
918         struct ast_config_engine *loader = &text_file_engine;
919         struct ast_config *result;
920
921         if (cfg->include_level == cfg->max_include_level) {
922                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
923                 return NULL;
924         }
925
926         cfg->include_level++;
927
928         if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
929                 struct ast_config_engine *eng;
930
931                 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
932
933
934                 if (eng && eng->load_func) {
935                         loader = eng;
936                 } else {
937                         eng = find_engine("global", db, sizeof(db), table, sizeof(table));
938                         if (eng && eng->load_func)
939                                 loader = eng;
940                 }
941         }
942
943         result = loader->load_func(db, table, filename, cfg);
944
945         if (result)
946                 result->include_level--;
947
948         return result;
949 }
950
951 struct ast_config *ast_config_load(const char *filename)
952 {
953         struct ast_config *cfg;
954         struct ast_config *result;
955
956         cfg = ast_config_new();
957         if (!cfg)
958                 return NULL;
959
960         result = ast_config_internal_load(filename, cfg);
961         if (!result)
962                 ast_config_destroy(cfg);
963
964         return result;
965 }
966
967 struct ast_variable *ast_load_realtime(const char *family, ...)
968 {
969         struct ast_config_engine *eng;
970         char db[256]="";
971         char table[256]="";
972         struct ast_variable *res=NULL;
973         va_list ap;
974
975         va_start(ap, family);
976         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
977         if (eng && eng->realtime_func) 
978                 res = eng->realtime_func(db, table, ap);
979         va_end(ap);
980
981         return res;
982 }
983
984 /*--- ast_check_realtime: Check if realtime engine is configured for family */
985 int ast_check_realtime(const char *family)
986 {
987         struct ast_config_engine *eng;
988
989         eng = find_engine(family, NULL, 0, NULL, 0);
990         if (eng)
991                 return 1;
992         return 0;
993
994 }
995
996 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
997 {
998         struct ast_config_engine *eng;
999         char db[256]="";
1000         char table[256]="";
1001         struct ast_config *res=NULL;
1002         va_list ap;
1003
1004         va_start(ap, family);
1005         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1006         if (eng && eng->realtime_multi_func) 
1007                 res = eng->realtime_multi_func(db, table, ap);
1008         va_end(ap);
1009
1010         return res;
1011 }
1012
1013 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1014 {
1015         struct ast_config_engine *eng;
1016         int res = -1;
1017         char db[256]="";
1018         char table[256]="";
1019         va_list ap;
1020
1021         va_start(ap, lookup);
1022         eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1023         if (eng && eng->update_func) 
1024                 res = eng->update_func(db, table, keyfield, lookup, ap);
1025         va_end(ap);
1026
1027         return res;
1028 }
1029
1030 static int config_command(int fd, int argc, char **argv) 
1031 {
1032         struct ast_config_engine *eng;
1033         struct ast_config_map *map;
1034         
1035         ast_mutex_lock(&config_lock);
1036
1037         ast_cli(fd, "\n\n");
1038         for (eng = config_engine_list; eng; eng = eng->next) {
1039                 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1040                 for (map = config_maps; map; map = map->next)
1041                         if (!strcasecmp(map->driver, eng->name)) {
1042                                 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1043                                         map->table ? map->table : map->name);
1044                         }
1045         }
1046         ast_cli(fd,"\n\n");
1047         
1048         ast_mutex_unlock(&config_lock);
1049
1050         return 0;
1051 }
1052
1053 static char show_config_help[] =
1054         "Usage: show config mappings\n"
1055         "       Shows the filenames to config engines.\n";
1056
1057 static struct ast_cli_entry config_command_struct = {
1058         { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
1059 };
1060
1061 int register_config_cli() 
1062 {
1063         return ast_cli_register(&config_command_struct);
1064 }