Add features (incomplete, highly experimental), fix DundiLookup app, debug improvemen...
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999-2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <time.h>
20 #include <asterisk/config.h>
21 #include <asterisk/config_pvt.h>
22 #include <asterisk/cli.h>
23 #include <asterisk/lock.h>
24 #include <asterisk/options.h>
25 #include <asterisk/logger.h>
26 #include <asterisk/utils.h>
27 #include "asterisk.h"
28 #include "astconf.h"
29
30
31 static int ast_cust_config=0;
32 struct ast_config *(*global_load_func)(const char *dbname, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
33
34 static struct ast_config_map {
35         struct ast_config_map *next;
36         char *name;
37         char *driver;
38         char *database;
39         char *table;
40         char stuff[0];
41 } *maps = NULL;
42
43 AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
44 static struct ast_config_reg *ast_cust_config_list;
45 static char *config_conf_file = "extconfig.conf";
46
47 void ast_destroy_realtime(struct ast_variable *v)
48 {
49         struct ast_variable *vn;
50         while(v) {
51                 vn = v;
52                 v = v->next;
53                 free(vn);
54         }
55 }
56
57 void ast_destroy(struct ast_config *ast)
58 {
59         struct ast_category *cat, *catn;
60
61         if (!ast)
62                 return;
63
64         cat = ast->root;
65         while(cat) {
66                 ast_destroy_realtime(cat->root);
67                 catn = cat;
68                 cat = cat->next;
69                 free(catn);
70         }
71         free(ast);
72 }
73
74 int ast_true(const char *s)
75 {
76         if (!s)
77                 return 0;
78         /* Determine if this is a true value */
79         if (!strcasecmp(s, "yes") ||
80             !strcasecmp(s, "true") ||
81                 !strcasecmp(s, "y") ||
82                 !strcasecmp(s, "t") ||
83                 !strcasecmp(s, "1") ||
84                 !strcasecmp(s, "on"))
85                         return -1;
86         return 0;
87 }
88
89 int ast_false(const char *s)
90 {
91         if (!s)
92                 return 0;
93         /* Determine if this is a false value */
94         if (!strcasecmp(s, "no") ||
95             !strcasecmp(s, "false") ||
96                 !strcasecmp(s, "n") ||
97                 !strcasecmp(s, "f") ||
98                 !strcasecmp(s, "0") ||
99                 !strcasecmp(s, "off"))
100                         return -1;
101         return 0;
102 }
103
104 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
105 {
106         struct ast_category *cat;
107         cat = config->root;
108         while(cat) {
109                 if (cat->name == category)
110                         return cat->root;
111                 cat = cat->next;
112         }
113         cat = config->root;
114         while(cat) {
115                 if (!strcasecmp(cat->name, category))
116                         return cat->root;
117                 cat = cat->next;
118         }
119         return NULL;
120 }
121
122 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
123 {
124         struct ast_variable *v;
125         if (category) {
126                 v = ast_variable_browse(config, category);
127                 while (v) {
128                         if (value == v->name)
129                                 return v->value;
130                         v=v->next;
131                 }
132                 v = ast_variable_browse(config, category);
133                 while (v) {
134                         if (!strcasecmp(value, v->name))
135                                 return v->value;
136                         v=v->next;
137                 }
138         } else {
139                 struct ast_category *cat;
140                 cat = config->root;
141                 while(cat) {
142                         v = cat->root;
143                         while (v) {
144                                 if (!strcasecmp(value, v->name))
145                                         return v->value;
146                                 v=v->next;
147                         }
148                         cat = cat->next;
149                 }
150         }
151         return NULL;
152 }
153
154 int ast_category_exist(struct ast_config *config, char *category_name)
155 {
156         struct ast_category *category = NULL;
157
158         category = config->root;
159
160         while(category) {
161                 if (!strcasecmp(category->name,category_name)) 
162                         return 1;
163                 category = category->next;
164         } 
165
166         return 0;
167 }
168
169
170 static struct ast_config_reg *get_ast_cust_config_keyword(const char *name, char *database, int dbsiz, char *table, int tabsiz) 
171 {
172         struct ast_config_reg *reg,*ret=NULL;
173         struct ast_config_map *map;
174
175         ast_mutex_lock(&ast_cust_config_lock);
176         map = maps;
177         while(map) {
178                 if (!strcasecmp(name, map->name)) {
179                         strncpy(database, map->database, dbsiz - 1);
180                         if (map->table)
181                                 strncpy(table, map->table, tabsiz - 1);
182                         else
183                                 strncpy(table, name, tabsiz - 1);
184                         break;
185                 }
186                 map = map->next;
187         }
188         if (map) {
189                 for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
190                         if (!strcmp(reg->name,map->driver))
191                                 ret=reg;
192                 }
193         }
194         ast_mutex_unlock(&ast_cust_config_lock);
195         return ret;
196 }
197
198 void ast_config_destroy_all(void) 
199 {
200         struct ast_config_reg *key;
201         ast_mutex_lock(&ast_cust_config_lock);
202         for (key=ast_cust_config_list;key;key=key->next) {
203                 ast_config_deregister(key);
204         }
205         ast_cust_config_list = NULL;
206         ast_mutex_unlock(&ast_cust_config_lock);
207 }
208
209 static struct ast_config_reg *get_config_registrations(void) 
210 {
211         return ast_cust_config_list;
212 }
213
214
215 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel);
216
217 static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, const char *configfile, int includelevel )
218 {
219         char *c;
220         char *cur;
221         char *arg=NULL;
222         struct ast_variable *v;
223         int object;
224         /* Strip off lines using ; as comment */
225         c = strchr(buf, ';');
226         while (c) {
227                 if ((c == buf) || (*(c-1) != '\\')) {
228                         *c = '\0';
229                 } else {
230                         *(c-1) = ';';
231                         memmove(c, c + 1, strlen(c + 1));
232                 }
233                 c = strchr(c + 1, ';');
234         }
235         cur = ast_strip(buf);
236         if (!ast_strlen_zero(cur)) {
237                 /* Actually parse the entry */
238                 if (cur[0] == '[') {
239                         /* A category header */
240                         c = strchr(cur, ']');
241                         if (c) {
242                                 *c = 0;
243                                 *_tmpc = malloc(sizeof(struct ast_category));
244                                 if (!*_tmpc) {
245                                         ast_destroy(tmp);
246                                         ast_log(LOG_WARNING,
247                                                 "Out of memory, line %d\n", lineno);
248                                         return -1;
249                                 }
250                                 memset(*_tmpc, 0, sizeof(struct ast_category));
251                                 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
252                                 (*_tmpc)->root =  NULL;
253                                 if (!tmp->prev)
254                                         tmp->root = *_tmpc;
255                                 else
256                                         tmp->prev->next = *_tmpc;
257
258                                 tmp->prev = *_tmpc;
259                                 *_last =  NULL;
260                         } else {
261                                 ast_log(LOG_WARNING, 
262                                         "parse error: no closing ']', line %d of %s\n", lineno, configfile);
263                         }
264                 } else if (cur[0] == '#') {
265                         /* A directive */
266                         cur++;
267                         c = cur;
268                         while(*c && (*c > 32)) c++;
269                         if (*c) {
270                                 *c = '\0';
271                                 c++;
272                                 /* Find real argument */
273                                 while(*c  && (*c < 33)) c++;
274                                 if (!*c)
275                                         c = NULL;
276                         } else 
277                                 c = NULL;
278                         if (!strcasecmp(cur, "include")) {
279                                 /* A #include */
280                                 if (c) {
281                                         while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
282                                         /* Get rid of leading mess */
283                                         cur = c;
284                                         while (!ast_strlen_zero(cur)) {
285                                                 c = cur + strlen(cur) - 1;
286                                                 if ((*c == '>') || (*c == '<') || (*c == '\"'))
287                                                         *c = '\0';
288                                                 else
289                                                         break;
290                                         }
291                                         
292                                         if((c = strchr(cur,':'))) {
293                                                 *c = '\0';
294                                                 c++;
295                                                 arg = c;
296                                         }
297                                         
298                                         if (includelevel < MAX_INCLUDE_LEVEL) {
299                                                 if(arg && cur) {
300                                                         ast_log(LOG_WARNING, "Including files with explicit config engine no longer permitted.  Please use extconfig.conf to specify all mappings\n");
301                                                 } else {
302                                                         __ast_load(cur, tmp, _tmpc, _last, includelevel + 1);
303                                                 }
304                                         } else
305                                                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
306                                 } else
307                                         ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
308                                 /* Strip off leading and trailing "'s and <>'s */
309                         }
310                         else 
311                                 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
312                 } else {
313                         /* Just a line (variable = value) */
314                         if (!*_tmpc) {
315                                 ast_log(LOG_WARNING,
316                                         "parse error: No category context for line %d of %s\n", lineno, configfile);
317                                 ast_destroy(tmp);
318                                 return -1;
319                         }
320                         c = strchr(cur, '=');
321                         if (c) {
322                                 *c = 0;
323                                 c++;
324                                 /* Ignore > in => */
325                                 if (*c== '>') {
326                                         object = 1;
327                                         c++;
328                                 } else
329                                         object = 0;
330                                 v = ast_new_variable(ast_strip(cur), ast_strip(c));
331                                 if (v) {
332                                         v->next = NULL;
333                                         v->lineno = lineno;
334                                         v->object = object;
335                                         /* Put and reset comments */
336                                         v->blanklines = 0;
337                                         if (*_last)
338                                                 (*_last)->next = v;
339                                         else
340                                                 (*_tmpc)->root = v;
341                                         *_last = v;
342                                 } else {
343                                         ast_destroy(tmp);
344                                         ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
345                                         return -1;
346                                 }
347                         } else {
348                                 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
349                         }
350                                                                                                                 
351                 }
352         }
353         return 0;
354 }
355
356 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
357 {
358         FILE *f;
359         char fn[256];
360         char date[256]="";
361         time_t t;
362         struct ast_variable *var;
363         struct ast_category *cat;
364         int blanklines = 0;
365         if (configfile[0] == '/') {
366                 strncpy(fn, configfile, sizeof(fn)-1);
367         } else {
368                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
369         }
370         time(&t);
371         strncpy(date, ctime(&t), sizeof(date) - 1);
372         if ((f = fopen(fn, "w"))) {
373                 if ((option_verbose > 1) && !option_debug)
374                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
375                 fprintf(f, ";!\n");
376                 fprintf(f, ";! Automatically generated configuration file\n");
377                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
378                 fprintf(f, ";! Generator: %s\n", generator);
379                 fprintf(f, ";! Creation Date: %s", date);
380                 fprintf(f, ";!\n");
381                 cat = cfg->root;
382                 while(cat) {
383                         /* Dump section with any appropriate comment */
384                         fprintf(f, "[%s]\n", cat->name);
385                         var = cat->root;
386                         while(var) {
387                                 if (var->sameline) 
388                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
389                                 else    
390                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
391                                 if (var->blanklines) {
392                                         blanklines = var->blanklines;
393                                         while (blanklines) {
394                                                 fprintf(f, "\n");
395                                                 blanklines--;
396                                         }
397                                 }
398                                         
399                                 var = var->next;
400                         }
401 #if 0
402                         /* Put an empty line */
403                         fprintf(f, "\n");
404 #endif
405                         cat = cat->next;
406                 }
407         } else {
408                 if (option_debug)
409                         printf("Unable to open for writing: %s\n", fn);
410                 else if (option_verbose > 1)
411                         printf( "Unable to write (%s)", strerror(errno));
412                 return -1;
413         }
414         fclose(f);
415         return 0;
416 }
417
418
419 struct ast_variable *ast_load_realtime(const char *family, ...)
420 {
421         struct ast_config_reg *reg;
422         char db[256]="";
423         char table[256]="";
424         struct ast_variable *res=NULL;
425         va_list ap;
426         va_start(ap, family);
427         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
428         if (reg && reg->realtime_func) 
429                 res = reg->realtime_func(db, table, ap);
430         va_end(ap);
431         return res;
432 }
433
434 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
435 {
436         struct ast_config_reg *reg;
437         char db[256]="";
438         char table[256]="";
439         struct ast_config *res=NULL;
440         va_list ap;
441         va_start(ap, family);
442         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
443         if (reg && reg->realtime_multi_func) 
444                 res = reg->realtime_multi_func(db, table, ap);
445         va_end(ap);
446         return res;
447 }
448
449 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
450 {
451         struct ast_config_reg *reg;
452         int res = -1;
453         char db[256]="";
454         char table[256]="";
455         va_list ap;
456         va_start(ap, lookup);
457         reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
458         if (reg && reg->update_func) 
459                 res = reg->update_func(db, table, keyfield, lookup, ap);
460         va_end(ap);
461         return res;
462 }
463
464 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel)
465 {
466         char fn[256];
467         char buf[8192];
468         char db[256];
469         char table[256];
470         FILE *f;
471         int lineno=0;
472         int master=0;
473         struct ast_config_reg *reg=NULL;
474         struct ast_config *(*load_func)(const char *database, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
475
476         load_func=NULL;
477         if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
478                 if (global_load_func) {
479                         load_func = global_load_func;
480                 } else {
481                         reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
482                         if (reg && reg->static_func) {
483                                 load_func = reg->static_func;
484                         } else {
485                                 reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
486                                 if (reg && reg->static_func)
487                                         global_load_func = load_func = reg->static_func;
488                         }
489                 }
490
491                 if (load_func) {
492                         ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
493                         tmp = load_func(db, table, configfile,tmp, _tmpc, _last, includelevel);
494             
495                         if (tmp)
496                                 return tmp;
497                 }
498         }
499
500         if (configfile[0] == '/') {
501                 strncpy(fn, configfile, sizeof(fn)-1);
502         } else {
503                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
504         }
505         if ((option_verbose > 1) && !option_debug) {
506                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
507                 fflush(stdout);
508         }
509         if ((f = fopen(fn, "r"))) {
510                 if (option_debug)
511                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
512                 else if (option_verbose > 1)
513                         ast_verbose( "Found\n");
514                 if (!tmp) {
515                         tmp = malloc(sizeof(struct ast_config));
516                         if (tmp)
517                                 memset(tmp, 0, sizeof(struct ast_config));
518
519                         master = 1;
520                 }
521                 if (!tmp) {
522                         ast_log(LOG_WARNING, "Out of memory\n");
523                         fclose(f);
524                         return NULL;
525                 }
526                 while(!feof(f)) {
527                         lineno++;
528                         if (fgets(buf, sizeof(buf), f)) {
529                                 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel)) {
530                                         fclose(f);
531                                         return NULL;
532                                 }
533                         }
534                 }
535                 fclose(f);              
536         } else {
537                 if (option_debug)
538                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
539                 else if (option_verbose > 1)
540                         ast_verbose( "Not found (%s)\n", strerror(errno));
541         }
542         return tmp;
543 }
544
545 int ast_config_register(struct ast_config_reg *new) 
546 {
547         struct ast_config_reg *ptr;
548         ast_mutex_lock(&ast_cust_config_lock);
549         if (!ast_cust_config_list) {
550                 ast_cust_config_list = new;
551         } else {
552                 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
553                         ptr->next = new;
554         }
555         ast_mutex_unlock(&ast_cust_config_lock);
556         ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
557         return 1;
558 }
559
560 int ast_config_deregister(struct ast_config_reg *del) 
561 {
562         struct ast_config_reg *ptr=NULL,*last=NULL;
563         ast_mutex_lock(&ast_cust_config_lock);
564         for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
565                 if (ptr == del) {
566                         if (last && ptr->next) {
567                                 last->next = ptr->next;
568                         } else if (last && ! ptr->next) {
569                                 last->next = NULL;
570                         } else if (!last && ptr->next) {
571                                 ast_cust_config_list = ptr->next;
572                         } else if (!last && !ptr->next) {
573                                 ast_cust_config_list = NULL;
574                         }
575                 }
576                 last = ptr;
577         }
578         ast_mutex_unlock(&ast_cust_config_lock);
579         return 0;
580 }
581
582 int ast_cust_config_active(void) {
583         return (ast_cust_config >0) ? 1 : 0;
584 }
585
586 struct ast_config *ast_load(char *configfile)
587 {
588         struct ast_category *tmpc=NULL;
589         struct ast_variable *last = NULL;
590
591         return __ast_load(configfile, NULL, &tmpc, &last, 0);
592 }
593
594 char *ast_category_browse(struct ast_config *config, char *prev)
595 {       
596         struct ast_category *cat;
597         if (!prev) {
598                 if (config->root)
599                         return config->root->name;
600                 else
601                         return NULL;
602         }
603         cat = config->root;
604         while(cat) {
605                 if (cat->name == prev) {
606                         if (cat->next)
607                                 return cat->next->name;
608                         else
609                                 return NULL;
610                 }
611                 cat = cat->next;
612         }
613         cat = config->root;
614         while(cat) {
615                 if (!strcasecmp(cat->name, prev)) {
616                         if (cat->next)
617                                 return cat->next->name;
618                         else
619                                 return NULL;
620                 }
621                 cat = cat->next;
622         }
623         return NULL;
624 }
625
626
627 struct ast_config *ast_new_config(void) 
628 {
629         struct ast_config *config;
630         config = malloc(sizeof(struct ast_config));
631         memset(config,0,sizeof(struct ast_config));
632         return config;
633 }
634
635
636
637 struct ast_category *ast_new_category(char *name) 
638 {
639         struct ast_category *category;
640         category = malloc(sizeof(struct ast_category));
641         if (category) {
642                 memset(category,0,sizeof(struct ast_category));
643                 strncpy(category->name,name,sizeof(category->name) - 1);
644         }
645         return category;
646 }
647
648
649 struct ast_variable *ast_new_variable(char *name, char *value) 
650 {
651         struct ast_variable *variable;
652         int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
653         variable = malloc(length);
654         if (variable) {
655                 memset(variable, 0, length);
656                 variable->name = variable->stuff;
657                 variable->value = variable->stuff + strlen(name) + 1;           
658                 variable->object=0;
659                 strcpy(variable->name,name);
660                 strcpy(variable->value,value);
661         }
662         return variable;
663 }
664
665 int ast_cust_config_register(struct ast_config_reg *new) 
666 {
667         ast_config_register(new);
668         read_ast_cust_config();
669         return 1;
670 }
671 int ast_cust_config_deregister(struct ast_config_reg *new) 
672 {
673         ast_config_deregister(new);
674         read_ast_cust_config();
675         return 1;
676 }
677
678 static void clear_cust_keywords(void) 
679 {
680         struct ast_config_map *map, *prev;
681         ast_mutex_lock(&ast_cust_config_lock);
682         map = maps;
683         while(map) {
684                 prev = map;
685                 map = map->next;
686                 free(prev);
687         }
688         maps = NULL;
689         ast_mutex_unlock(&ast_cust_config_lock);
690 }
691
692 static int config_command(int fd, int argc, char **argv) 
693 {
694         struct ast_config_reg *key;
695         struct ast_config_map *map;
696         
697         ast_cli(fd,"\n\n");
698         ast_mutex_lock(&ast_cust_config_lock);
699         for (key=get_config_registrations();key;key=key->next) {
700                 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
701                 map = maps;
702                 while(map) {
703                         if (!strcasecmp(map->driver, key->name))
704                                 ast_cli(fd,"===> %s (db=%s, table=%s)\n",map->name, map->database, map->table ? map->table : map->name);
705                         map = map->next;
706                 }
707         }
708         ast_mutex_unlock(&ast_cust_config_lock);
709         ast_cli(fd,"\n\n");
710         
711         return 0;
712 }
713
714 static struct ast_cli_entry config_command_struct = {
715   { "show","config","handles", NULL }, config_command,
716   "Show Config Handles", NULL };
717
718 int register_config_cli() 
719 {
720         return ast_cli_register(&config_command_struct);
721 }
722
723 int read_ast_cust_config(void) 
724 {
725         char *cfg = config_conf_file;
726         struct ast_config *config;
727         struct ast_variable *v;
728         struct ast_config_map *map;
729         int length;
730         char *driver, *table, *database, *stringp;
731
732         clear_cust_keywords();
733         config = ast_load(cfg);
734         if (config) {
735                 for (v = ast_variable_browse(config,"settings");v;v=v->next) {
736                         stringp = v->value;
737                         driver = strsep(&stringp, ",");
738                         database = strsep(&stringp, ",");
739                         table = strsep(&stringp, ",");
740                         
741                         if (!strcmp(v->name,config_conf_file) || !strcmp(v->name,"asterisk.conf")) {
742                                 ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
743                         } else if (driver && database) {
744                                 length = sizeof(struct ast_config_map);
745                                 length += strlen(v->name) + 1;
746                                 length += strlen(driver) + 1;
747                                 length += strlen(database) + 1;
748                                 if (table)
749                                         length += strlen(table) + 1;
750                                 map = malloc(length);
751                                 if (map) {
752                                         memset(map, 0, length);
753                                         map->name = map->stuff;
754                                         strcpy(map->name, v->name);
755                                         map->driver = map->name + strlen(map->name) + 1;
756                                         strcpy(map->driver, driver);
757                                         map->database = map->driver + strlen(map->driver) + 1;
758                                         strcpy(map->database, database);
759                                         if (table) {
760                                                 map->table = map->database + strlen(map->database) + 1;
761                                                 strcpy(map->table, table);
762                                         } else
763                                                 map->table = NULL;
764                                         map->next = maps;
765                                         if (option_verbose > 1)
766                                                 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",map->name,map->driver, map->database, map->table ? map->table : map->name);
767                                         maps = map;
768                                 }
769                         }
770                 }
771                 
772                 ast_destroy(config);
773         }
774
775         return 0;
776 }