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 #define AST_INCLUDE_GLOB 1
21 #ifdef AST_INCLUDE_GLOB
24 #include <asterisk/config.h>
25 #include <asterisk/config_pvt.h>
26 #include <asterisk/cli.h>
27 #include <asterisk/lock.h>
28 #include <asterisk/options.h>
29 #include <asterisk/logger.h>
30 #include <asterisk/utils.h>
34 #define MAX_NESTED_COMMENTS 128
35 #define COMMENT_START ";--"
36 #define COMMENT_END "--;"
37 #define COMMENT_META ';'
38 #define COMMENT_TAG '-'
40 static int ast_cust_config=0;
41 struct ast_config *(*global_load_func)(const char *dbname, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
43 static struct ast_config_map {
44 struct ast_config_map *next;
52 AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
53 static struct ast_config_reg *ast_cust_config_list;
54 static char *config_conf_file = "extconfig.conf";
56 void ast_destroy_realtime(struct ast_variable *v)
58 struct ast_variable *vn;
66 void ast_category_destroy(struct ast_category *cat)
68 ast_destroy_realtime(cat->root);
72 void ast_destroy(struct ast_config *ast)
74 struct ast_category *cat, *catn;
81 ast_destroy_realtime(cat->root);
89 int ast_true(const char *s)
93 /* Determine if this is a true value */
94 if (!strcasecmp(s, "yes") ||
95 !strcasecmp(s, "true") ||
96 !strcasecmp(s, "y") ||
97 !strcasecmp(s, "t") ||
98 !strcasecmp(s, "1") ||
104 int ast_false(const char *s)
108 /* Determine if this is a false value */
109 if (!strcasecmp(s, "no") ||
110 !strcasecmp(s, "false") ||
111 !strcasecmp(s, "n") ||
112 !strcasecmp(s, "f") ||
113 !strcasecmp(s, "0") ||
114 !strcasecmp(s, "off"))
119 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
121 struct ast_category *cat;
124 if (cat->name == category)
130 if (!strcasecmp(cat->name, category))
137 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
139 struct ast_variable *v;
141 v = ast_variable_browse(config, category);
143 if (value == v->name)
147 v = ast_variable_browse(config, category);
149 if (!strcasecmp(value, v->name))
154 struct ast_category *cat;
159 if (!strcasecmp(value, v->name))
169 int ast_category_exist(struct ast_config *config, char *category_name)
171 struct ast_category *category = NULL;
173 category = config->root;
176 if (!strcasecmp(category->name,category_name))
178 category = category->next;
185 static struct ast_config_reg *get_ast_cust_config_keyword(const char *name, char *database, int dbsiz, char *table, int tabsiz)
187 struct ast_config_reg *reg,*ret=NULL;
188 struct ast_config_map *map;
190 ast_mutex_lock(&ast_cust_config_lock);
193 if (!strcasecmp(name, map->name)) {
194 strncpy(database, map->database, dbsiz - 1);
196 strncpy(table, map->table, tabsiz - 1);
198 strncpy(table, name, tabsiz - 1);
204 for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
205 if (!strcmp(reg->name,map->driver))
209 ast_mutex_unlock(&ast_cust_config_lock);
213 void ast_config_destroy_all(void)
215 struct ast_config_reg *key;
216 ast_mutex_lock(&ast_cust_config_lock);
217 for (key=ast_cust_config_list;key;key=key->next) {
218 ast_config_deregister(key);
220 ast_cust_config_list = NULL;
221 ast_mutex_unlock(&ast_cust_config_lock);
224 static struct ast_config_reg *get_config_registrations(void)
226 return ast_cust_config_list;
230 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel);
232 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 )
237 struct ast_variable *v;
239 cur = ast_strip(buf);
240 if (!ast_strlen_zero(cur)) {
241 /* Actually parse the entry */
243 /* A category header */
244 c = strchr(cur, ']');
247 *_tmpc = malloc(sizeof(struct ast_category));
251 "Out of memory, line %d\n", lineno);
254 memset(*_tmpc, 0, sizeof(struct ast_category));
255 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
256 (*_tmpc)->root = NULL;
260 tmp->prev->next = *_tmpc;
266 "parse error: no closing ']', line %d of %s\n", lineno, configfile);
268 } else if (cur[0] == '#') {
272 while(*c && (*c > 32)) c++;
276 /* Find real argument */
277 while(*c && (*c < 33)) c++;
282 if (!strcasecmp(cur, "include")) {
285 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
286 /* Get rid of leading mess */
288 while (!ast_strlen_zero(cur)) {
289 c = cur + strlen(cur) - 1;
290 if ((*c == '>') || (*c == '<') || (*c == '\"'))
296 if((c = strchr(cur,':'))) {
302 if (includelevel < MAX_INCLUDE_LEVEL) {
304 ast_log(LOG_WARNING, "Including files with explicit config engine no longer permitted. Please use extconfig.conf to specify all mappings\n");
306 if (!__ast_load(cur, tmp, _tmpc, _last, includelevel + 1))
310 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
312 ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
313 /* Strip off leading and trailing "'s and <>'s */
316 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
318 /* Just a line (variable = value) */
321 "parse error: No category context for line %d of %s\n", lineno, configfile);
325 c = strchr(cur, '=');
335 v = ast_new_variable(ast_strip(cur), ast_strip(c));
340 /* Put and reset comments */
349 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
353 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
361 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
367 struct ast_variable *var;
368 struct ast_category *cat;
370 if (configfile[0] == '/') {
371 strncpy(fn, configfile, sizeof(fn)-1);
373 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
376 strncpy(date, ctime(&t), sizeof(date) - 1);
377 if ((f = fopen(fn, "w"))) {
378 if ((option_verbose > 1) && !option_debug)
379 ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
381 fprintf(f, ";! Automatically generated configuration file\n");
382 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
383 fprintf(f, ";! Generator: %s\n", generator);
384 fprintf(f, ";! Creation Date: %s", date);
388 /* Dump section with any appropriate comment */
389 fprintf(f, "[%s]\n", cat->name);
393 fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
395 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
396 if (var->blanklines) {
397 blanklines = var->blanklines;
407 /* Put an empty line */
414 printf("Unable to open for writing: %s\n", fn);
415 else if (option_verbose > 1)
416 printf( "Unable to write (%s)", strerror(errno));
424 struct ast_variable *ast_load_realtime(const char *family, ...)
426 struct ast_config_reg *reg;
429 struct ast_variable *res=NULL;
431 va_start(ap, family);
432 reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
433 if (reg && reg->realtime_func)
434 res = reg->realtime_func(db, table, ap);
439 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
441 struct ast_config_reg *reg;
444 struct ast_config *res=NULL;
446 va_start(ap, family);
447 reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
448 if (reg && reg->realtime_multi_func)
449 res = reg->realtime_multi_func(db, table, ap);
454 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
456 struct ast_config_reg *reg;
461 va_start(ap, lookup);
462 reg = get_ast_cust_config_keyword(family, db, sizeof(db), table, sizeof(table));
463 if (reg && reg->update_func)
464 res = reg->update_func(db, table, keyfield, lookup, ap);
469 static struct ast_config *__ast_load(const char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel)
478 int comment = 0, nest[MAX_NESTED_COMMENTS];
480 struct ast_config_reg *reg=NULL;
481 struct ast_config *(*load_func)(const char *database, const char *table, const char *, struct ast_config *,struct ast_category **,struct ast_variable **,int);
482 char *comment_p, *process_buf, *new_buf=NULL;
485 if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
486 if (global_load_func) {
487 load_func = global_load_func;
489 reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
490 if (reg && reg->static_func) {
491 load_func = reg->static_func;
493 reg = get_ast_cust_config_keyword(configfile, db, sizeof(db), table, sizeof(table));
494 if (reg && reg->static_func)
495 global_load_func = load_func = reg->static_func;
500 ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
501 if((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 #ifdef AST_INCLUDE_GLOB
515 globbuf.gl_offs = 0; /* initialize it to silence gcc */
517 glob_ret = glob(fn, GLOB_NOMAGIC, NULL, &globbuf);
519 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
521 if (glob_ret == GLOB_NOSPACE)
523 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
524 else if (glob_ret == GLOB_ABORTED)
526 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
528 /* loop over expanded files */
530 for (i=0; i<globbuf.gl_pathc; i++) {
531 strncpy(fn, globbuf.gl_pathv[i], sizeof(fn)-1);
533 if ((option_verbose > 1) && !option_debug) {
534 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
537 if ((f = fopen(fn, "r"))) {
539 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
540 else if (option_verbose > 2)
541 ast_verbose( "Found\n");
543 if((tmp = malloc(sizeof(struct ast_config)))) {
544 memset(tmp, 0, sizeof(struct ast_config));
551 if (fgets(buf, sizeof(buf), f)) {
557 while ((comment_p = strchr(new_buf, COMMENT_META))) {
558 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
559 /* Yuck, gotta memmove */
560 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
562 } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
563 /* Meta-Comment start detected ";--" */
564 if (comment < MAX_NESTED_COMMENTS) {
566 new_buf = comment_p + 3;
568 nest[comment-1] = lineno;
570 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
572 } else if ((comment_p >= new_buf + 2) &&
573 (*(comment_p - 1) == COMMENT_TAG) &&
574 (*(comment_p - 2) == COMMENT_TAG)) {
575 /* Meta-Comment end detected */
577 new_buf = comment_p + 1;
579 /* Back to non-comment now */
581 /* Actually have to move what's left over the top, then continue */
583 oldptr = process_buf + strlen(process_buf);
584 memmove(oldptr, new_buf, strlen(new_buf) + 1);
587 process_buf = new_buf;
591 /* If ; is found, and we are not nested in a comment,
592 we immediately stop all comment processing */
596 new_buf = comment_p + 1;
599 if (process_buf && cfg_process(tmp, _tmpc, _last, process_buf, lineno, fn, includelevel)) {
606 ast_log(LOG_WARNING, "Out of memory\n");
609 } else { /* can't open file */
611 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
612 else if (option_verbose > 2)
613 ast_verbose( "Not found (%s)\n", strerror(errno));
616 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
618 #ifdef AST_INCLUDE_GLOB
630 int ast_config_register(struct ast_config_reg *new)
632 struct ast_config_reg *ptr;
633 ast_mutex_lock(&ast_cust_config_lock);
634 if (!ast_cust_config_list) {
635 ast_cust_config_list = new;
637 for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
640 ast_mutex_unlock(&ast_cust_config_lock);
641 ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
645 int ast_config_deregister(struct ast_config_reg *del)
647 struct ast_config_reg *ptr=NULL,*last=NULL;
648 ast_mutex_lock(&ast_cust_config_lock);
649 for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
651 if (last && ptr->next) {
652 last->next = ptr->next;
653 } else if (last && ! ptr->next) {
655 } else if (!last && ptr->next) {
656 ast_cust_config_list = ptr->next;
657 } else if (!last && !ptr->next) {
658 ast_cust_config_list = NULL;
663 ast_mutex_unlock(&ast_cust_config_lock);
667 int ast_cust_config_active(void) {
668 return (ast_cust_config >0) ? 1 : 0;
671 struct ast_config *ast_load(char *configfile)
673 struct ast_category *tmpc=NULL;
674 struct ast_variable *last = NULL;
676 return __ast_load(configfile, NULL, &tmpc, &last, 0);
679 void ast_category_append(struct ast_config *config, struct ast_category *cat)
681 struct ast_category *prev = NULL;
685 while(prev->next) prev = prev->next;
691 char *ast_category_browse(struct ast_config *config, char *prev)
693 struct ast_category *cat;
696 return config->root->name;
702 if (cat->name == prev) {
704 return cat->next->name;
712 if (!strcasecmp(cat->name, prev)) {
714 return cat->next->name;
724 struct ast_config *ast_new_config(void)
726 struct ast_config *config;
727 config = malloc(sizeof(struct ast_config));
728 memset(config,0,sizeof(struct ast_config));
734 struct ast_category *ast_new_category(char *name)
736 struct ast_category *category;
737 category = malloc(sizeof(struct ast_category));
739 memset(category,0,sizeof(struct ast_category));
740 strncpy(category->name,name,sizeof(category->name) - 1);
746 struct ast_variable *ast_new_variable(char *name, char *value)
748 struct ast_variable *variable;
749 int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
750 variable = malloc(length);
752 memset(variable, 0, length);
753 variable->name = variable->stuff;
754 variable->value = variable->stuff + strlen(name) + 1;
756 strcpy(variable->name,name);
757 strcpy(variable->value,value);
762 int ast_cust_config_register(struct ast_config_reg *new)
764 ast_config_register(new);
765 read_ast_cust_config();
768 int ast_cust_config_deregister(struct ast_config_reg *new)
770 ast_config_deregister(new);
771 read_ast_cust_config();
775 static void clear_cust_keywords(void)
777 struct ast_config_map *map, *prev;
778 ast_mutex_lock(&ast_cust_config_lock);
786 ast_mutex_unlock(&ast_cust_config_lock);
789 static int config_command(int fd, int argc, char **argv)
791 struct ast_config_reg *key;
792 struct ast_config_map *map;
795 ast_mutex_lock(&ast_cust_config_lock);
796 for (key=get_config_registrations();key;key=key->next) {
797 ast_cli(fd,"\nConfig Engine: %s\n",key->name);
800 if (!strcasecmp(map->driver, key->name))
801 ast_cli(fd,"===> %s (db=%s, table=%s)\n",map->name, map->database, map->table ? map->table : map->name);
805 ast_mutex_unlock(&ast_cust_config_lock);
811 static struct ast_cli_entry config_command_struct = {
812 { "show","config","handles", NULL }, config_command,
813 "Show Config Handles", NULL };
815 int register_config_cli()
817 return ast_cli_register(&config_command_struct);
820 int read_ast_cust_config(void)
822 char *cfg = config_conf_file;
823 struct ast_config *config;
824 struct ast_variable *v;
825 struct ast_config_map *map;
827 char *driver, *table, *database, *stringp;
829 clear_cust_keywords();
830 config = ast_load(cfg);
832 for (v = ast_variable_browse(config,"settings");v;v=v->next) {
834 driver = strsep(&stringp, ",");
835 database = strsep(&stringp, ",");
836 table = strsep(&stringp, ",");
838 if (!strcmp(v->name,config_conf_file) || !strcmp(v->name,"asterisk.conf")) {
839 ast_log(LOG_WARNING, "Cannot bind asterisk.conf or extconfig.conf!\n");
840 } else if (driver && database) {
841 length = sizeof(struct ast_config_map);
842 length += strlen(v->name) + 1;
843 length += strlen(driver) + 1;
844 length += strlen(database) + 1;
846 length += strlen(table) + 1;
847 map = malloc(length);
849 memset(map, 0, length);
850 map->name = map->stuff;
851 strcpy(map->name, v->name);
852 map->driver = map->name + strlen(map->name) + 1;
853 strcpy(map->driver, driver);
854 map->database = map->driver + strlen(map->driver) + 1;
855 strcpy(map->database, database);
857 map->table = map->database + strlen(map->database) + 1;
858 strcpy(map->table, table);
862 if (option_verbose > 1)
863 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",map->name,map->driver, map->database, map->table ? map->table : map->name);