2 * Asterisk -- A telephony toolkit for Linux.
4 * Configuration File Parser
6 * Copyright (C) 1999-2004, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
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>
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);
34 static struct ast_config_map {
35 struct ast_config_map *next;
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";
47 void ast_destroy_realtime(struct ast_variable *v)
49 struct ast_variable *vn;
57 void ast_category_destroy(struct ast_category *cat)
59 ast_destroy_realtime(cat->root);
63 void ast_destroy(struct ast_config *ast)
65 struct ast_category *cat, *catn;
72 ast_destroy_realtime(cat->root);
80 int ast_true(const char *s)
84 /* Determine if this is a true value */
85 if (!strcasecmp(s, "yes") ||
86 !strcasecmp(s, "true") ||
87 !strcasecmp(s, "y") ||
88 !strcasecmp(s, "t") ||
89 !strcasecmp(s, "1") ||
95 int ast_false(const char *s)
99 /* Determine if this is a false value */
100 if (!strcasecmp(s, "no") ||
101 !strcasecmp(s, "false") ||
102 !strcasecmp(s, "n") ||
103 !strcasecmp(s, "f") ||
104 !strcasecmp(s, "0") ||
105 !strcasecmp(s, "off"))
110 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
112 struct ast_category *cat;
115 if (cat->name == category)
121 if (!strcasecmp(cat->name, category))
128 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
130 struct ast_variable *v;
132 v = ast_variable_browse(config, category);
134 if (value == v->name)
138 v = ast_variable_browse(config, category);
140 if (!strcasecmp(value, v->name))
145 struct ast_category *cat;
150 if (!strcasecmp(value, v->name))
160 int ast_category_exist(struct ast_config *config, char *category_name)
162 struct ast_category *category = NULL;
164 category = config->root;
167 if (!strcasecmp(category->name,category_name))
169 category = category->next;
176 static struct ast_config_reg *get_ast_cust_config_keyword(const char *name, char *database, int dbsiz, char *table, int tabsiz)
178 struct ast_config_reg *reg,*ret=NULL;
179 struct ast_config_map *map;
181 ast_mutex_lock(&ast_cust_config_lock);
184 if (!strcasecmp(name, map->name)) {
185 strncpy(database, map->database, dbsiz - 1);
187 strncpy(table, map->table, tabsiz - 1);
189 strncpy(table, name, tabsiz - 1);
195 for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
196 if (!strcmp(reg->name,map->driver))
200 ast_mutex_unlock(&ast_cust_config_lock);
204 void ast_config_destroy_all(void)
206 struct ast_config_reg *key;
207 ast_mutex_lock(&ast_cust_config_lock);
208 for (key=ast_cust_config_list;key;key=key->next) {
209 ast_config_deregister(key);
211 ast_cust_config_list = NULL;
212 ast_mutex_unlock(&ast_cust_config_lock);
215 static struct ast_config_reg *get_config_registrations(void)
217 return ast_cust_config_list;
221 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel);
223 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 )
228 struct ast_variable *v;
230 /* Strip off lines using ; as comment */
231 c = strchr(buf, ';');
233 if ((c == buf) || (*(c-1) != '\\')) {
237 memmove(c, c + 1, strlen(c + 1));
239 c = strchr(c + 1, ';');
241 cur = ast_strip(buf);
242 if (!ast_strlen_zero(cur)) {
243 /* Actually parse the entry */
245 /* A category header */
246 c = strchr(cur, ']');
249 *_tmpc = malloc(sizeof(struct ast_category));
253 "Out of memory, line %d\n", lineno);
256 memset(*_tmpc, 0, sizeof(struct ast_category));
257 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
258 (*_tmpc)->root = NULL;
262 tmp->prev->next = *_tmpc;
268 "parse error: no closing ']', line %d of %s\n", lineno, configfile);
270 } else if (cur[0] == '#') {
274 while(*c && (*c > 32)) c++;
278 /* Find real argument */
279 while(*c && (*c < 33)) c++;
284 if (!strcasecmp(cur, "include")) {
287 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
288 /* Get rid of leading mess */
290 while (!ast_strlen_zero(cur)) {
291 c = cur + strlen(cur) - 1;
292 if ((*c == '>') || (*c == '<') || (*c == '\"'))
298 if((c = strchr(cur,':'))) {
304 if (includelevel < MAX_INCLUDE_LEVEL) {
306 ast_log(LOG_WARNING, "Including files with explicit config engine no longer permitted. Please use extconfig.conf to specify all mappings\n");
308 __ast_load(cur, tmp, _tmpc, _last, includelevel + 1);
311 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
313 ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
314 /* Strip off leading and trailing "'s and <>'s */
317 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
319 /* Just a line (variable = value) */
322 "parse error: No category context for line %d of %s\n", lineno, configfile);
326 c = strchr(cur, '=');
336 v = ast_new_variable(ast_strip(cur), ast_strip(c));
341 /* Put and reset comments */
350 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
354 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
362 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
368 struct ast_variable *var;
369 struct ast_category *cat;
371 if (configfile[0] == '/') {
372 strncpy(fn, configfile, sizeof(fn)-1);
374 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
377 strncpy(date, ctime(&t), sizeof(date) - 1);
378 if ((f = fopen(fn, "w"))) {
379 if ((option_verbose > 1) && !option_debug)
380 ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
382 fprintf(f, ";! Automatically generated configuration file\n");
383 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
384 fprintf(f, ";! Generator: %s\n", generator);
385 fprintf(f, ";! Creation Date: %s", date);
389 /* Dump section with any appropriate comment */
390 fprintf(f, "[%s]\n", cat->name);
394 fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
396 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
397 if (var->blanklines) {
398 blanklines = var->blanklines;
408 /* Put an empty line */
415 printf("Unable to open for writing: %s\n", fn);
416 else if (option_verbose > 1)
417 printf( "Unable to write (%s)", strerror(errno));
425 struct ast_variable *ast_load_realtime(const char *family, ...)
427 struct ast_config_reg *reg;
430 struct ast_variable *res=NULL;
432 va_start(ap, family);
433 reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
434 if (reg && reg->realtime_func)
435 res = reg->realtime_func(db, table, ap);
440 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
442 struct ast_config_reg *reg;
445 struct ast_config *res=NULL;
447 va_start(ap, family);
448 reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
449 if (reg && reg->realtime_multi_func)
450 res = reg->realtime_multi_func(db, table, ap);
455 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
457 struct ast_config_reg *reg;
462 va_start(ap, lookup);
463 reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
464 if (reg && reg->update_func)
465 res = reg->update_func(db, table, keyfield, lookup, ap);
470 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel)
479 struct ast_config_reg *reg=NULL;
480 struct ast_config *(*load_func)(const char *database, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
483 if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
484 if (global_load_func) {
485 load_func = global_load_func;
487 reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
488 if (reg && reg->static_func) {
489 load_func = reg->static_func;
491 reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
492 if (reg && reg->static_func)
493 global_load_func = load_func = reg->static_func;
498 ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
499 tmp = load_func(db, table, configfile,tmp, _tmpc, _last, includelevel);
506 if (configfile[0] == '/') {
507 strncpy(fn, configfile, sizeof(fn)-1);
509 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
511 if ((option_verbose > 1) && !option_debug) {
512 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
515 if ((f = fopen(fn, "r"))) {
517 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
518 else if (option_verbose > 1)
519 ast_verbose( "Found\n");
521 tmp = malloc(sizeof(struct ast_config));
523 memset(tmp, 0, sizeof(struct ast_config));
528 ast_log(LOG_WARNING, "Out of memory\n");
534 if (fgets(buf, sizeof(buf), f)) {
535 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel)) {
544 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
545 else if (option_verbose > 1)
546 ast_verbose( "Not found (%s)\n", strerror(errno));
551 int ast_config_register(struct ast_config_reg *new)
553 struct ast_config_reg *ptr;
554 ast_mutex_lock(&ast_cust_config_lock);
555 if (!ast_cust_config_list) {
556 ast_cust_config_list = new;
558 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
561 ast_mutex_unlock(&ast_cust_config_lock);
562 ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
566 int ast_config_deregister(struct ast_config_reg *del)
568 struct ast_config_reg *ptr=NULL,*last=NULL;
569 ast_mutex_lock(&ast_cust_config_lock);
570 for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
572 if (last && ptr->next) {
573 last->next = ptr->next;
574 } else if (last && ! ptr->next) {
576 } else if (!last && ptr->next) {
577 ast_cust_config_list = ptr->next;
578 } else if (!last && !ptr->next) {
579 ast_cust_config_list = NULL;
584 ast_mutex_unlock(&ast_cust_config_lock);
588 int ast_cust_config_active(void) {
589 return (ast_cust_config >0) ? 1 : 0;
592 struct ast_config *ast_load(char *configfile)
594 struct ast_category *tmpc=NULL;
595 struct ast_variable *last = NULL;
597 return __ast_load(configfile, NULL, &tmpc, &last, 0);
600 void ast_category_append(struct ast_config *config, struct ast_category *cat)
602 struct ast_category *prev = NULL;
606 while(prev->next) prev = prev->next;
612 char *ast_category_browse(struct ast_config *config, char *prev)
614 struct ast_category *cat;
617 return config->root->name;
623 if (cat->name == prev) {
625 return cat->next->name;
633 if (!strcasecmp(cat->name, prev)) {
635 return cat->next->name;
645 struct ast_config *ast_new_config(void)
647 struct ast_config *config;
648 config = malloc(sizeof(struct ast_config));
649 memset(config,0,sizeof(struct ast_config));
655 struct ast_category *ast_new_category(char *name)
657 struct ast_category *category;
658 category = malloc(sizeof(struct ast_category));
660 memset(category,0,sizeof(struct ast_category));
661 strncpy(category->name,name,sizeof(category->name) - 1);
667 struct ast_variable *ast_new_variable(char *name, char *value)
669 struct ast_variable *variable;
670 int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
671 variable = malloc(length);
673 memset(variable, 0, length);
674 variable->name = variable->stuff;
675 variable->value = variable->stuff + strlen(name) + 1;
677 strcpy(variable->name,name);
678 strcpy(variable->value,value);
683 int ast_cust_config_register(struct ast_config_reg *new)
685 ast_config_register(new);
686 read_ast_cust_config();
689 int ast_cust_config_deregister(struct ast_config_reg *new)
691 ast_config_deregister(new);
692 read_ast_cust_config();
696 static void clear_cust_keywords(void)
698 struct ast_config_map *map, *prev;
699 ast_mutex_lock(&ast_cust_config_lock);
707 ast_mutex_unlock(&ast_cust_config_lock);
710 static int config_command(int fd, int argc, char **argv)
712 struct ast_config_reg *key;
713 struct ast_config_map *map;
716 ast_mutex_lock(&ast_cust_config_lock);
717 for (key=get_config_registrations();key;key=key->next) {
718 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
721 if (!strcasecmp(map->driver, key->name))
722 ast_cli(fd,"===> %s (db=%s, table=%s)\n",map->name, map->database, map->table ? map->table : map->name);
726 ast_mutex_unlock(&ast_cust_config_lock);
732 static struct ast_cli_entry config_command_struct = {
733 { "show","config","handles", NULL }, config_command,
734 "Show Config Handles", NULL };
736 int register_config_cli()
738 return ast_cli_register(&config_command_struct);
741 int read_ast_cust_config(void)
743 char *cfg = config_conf_file;
744 struct ast_config *config;
745 struct ast_variable *v;
746 struct ast_config_map *map;
748 char *driver, *table, *database, *stringp;
750 clear_cust_keywords();
751 config = ast_load(cfg);
753 for (v = ast_variable_browse(config,"settings");v;v=v->next) {
755 driver = strsep(&stringp, ",");
756 database = strsep(&stringp, ",");
757 table = strsep(&stringp, ",");
759 if (!strcmp(v->name,config_conf_file) || !strcmp(v->name,"asterisk.conf")) {
760 ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
761 } else if (driver && database) {
762 length = sizeof(struct ast_config_map);
763 length += strlen(v->name) + 1;
764 length += strlen(driver) + 1;
765 length += strlen(database) + 1;
767 length += strlen(table) + 1;
768 map = malloc(length);
770 memset(map, 0, length);
771 map->name = map->stuff;
772 strcpy(map->name, v->name);
773 map->driver = map->name + strlen(map->name) + 1;
774 strcpy(map->driver, driver);
775 map->database = map->driver + strlen(map->driver) + 1;
776 strcpy(map->database, database);
778 map->table = map->database + strlen(map->database) + 1;
779 strcpy(map->table, table);
783 if (option_verbose > 1)
784 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",map->name,map->driver, map->database, map->table ? map->table : map->name);