2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Configuration File Parser
23 * \author Mark Spencer <markster@digium.com>
25 * Includes the Asterisk Realtime API - ARA
26 * See doc/realtime.txt and doc/extconfig.txt
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #define AST_INCLUDE_GLOB 1
41 #ifdef AST_INCLUDE_GLOB
42 #if defined(__Darwin__) || defined(__CYGWIN__)
43 #define GLOB_ABORTED GLOB_ABEND
48 #include "asterisk/config.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/options.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/utils.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/app.h"
57 #define MAX_NESTED_COMMENTS 128
58 #define COMMENT_START ";--"
59 #define COMMENT_END "--;"
60 #define COMMENT_META ';'
61 #define COMMENT_TAG '-'
63 static char *extconfig_conf = "extconfig.conf";
66 /*! \brief Structure to keep comments for rewriting configuration files */
67 /*! \brief Structure to keep comments for rewriting configuration files */
69 struct ast_comment *next;
75 static void CB_INIT(char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
77 if (!(*comment_buffer)) {
78 *comment_buffer = ast_malloc(CB_INCR);
79 if (!(*comment_buffer))
81 (*comment_buffer)[0] = 0;
82 *comment_buffer_size = CB_INCR;
83 *lline_buffer = ast_malloc(CB_INCR);
86 (*lline_buffer)[0] = 0;
87 *lline_buffer_size = CB_INCR;
89 (*comment_buffer)[0] = 0;
90 (*lline_buffer)[0] = 0;
94 static void CB_ADD(char **comment_buffer, int *comment_buffer_size, char *str)
96 int rem = *comment_buffer_size - strlen(*comment_buffer) - 1;
97 int siz = strlen(str);
99 *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + siz + 1);
100 if (!(*comment_buffer))
102 *comment_buffer_size += CB_INCR+siz+1;
104 strcat(*comment_buffer,str);
107 static void CB_ADD_LEN(char **comment_buffer, int *comment_buffer_size, char *str, int len)
109 int cbl = strlen(*comment_buffer) + 1;
110 int rem = *comment_buffer_size - cbl;
112 *comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + len + 1);
113 if (!(*comment_buffer))
115 *comment_buffer_size += CB_INCR+len+1;
117 strncat(*comment_buffer,str,len);
118 (*comment_buffer)[cbl+len-1] = 0;
121 static void LLB_ADD(char **lline_buffer, int *lline_buffer_size, char *str)
123 int rem = *lline_buffer_size - strlen(*lline_buffer) - 1;
124 int siz = strlen(str);
126 *lline_buffer = ast_realloc(*lline_buffer, *lline_buffer_size + CB_INCR + siz + 1);
127 if (!(*lline_buffer))
129 *lline_buffer_size += CB_INCR + siz + 1;
131 strcat(*lline_buffer,str);
134 static void CB_RESET(char **comment_buffer, char **lline_buffer)
136 (*comment_buffer)[0] = 0;
137 (*lline_buffer)[0] = 0;
141 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
143 struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
144 strcpy(x->cmt, buffer);
149 static struct ast_config_map {
150 struct ast_config_map *next;
156 } *config_maps = NULL;
158 AST_MUTEX_DEFINE_STATIC(config_lock);
159 static struct ast_config_engine *config_engine_list;
161 #define MAX_INCLUDE_LEVEL 10
163 struct ast_category {
165 int ignored; /*!< do not let user of the config see this category */
167 struct ast_comment *precomments;
168 struct ast_comment *sameline;
169 struct ast_variable *root;
170 struct ast_variable *last;
171 struct ast_category *next;
175 struct ast_category *root;
176 struct ast_category *last;
177 struct ast_category *current;
178 struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
180 int max_include_level;
183 struct ast_variable *ast_variable_new(const char *name, const char *value)
185 struct ast_variable *variable;
186 int name_len = strlen(name) + 1;
188 if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
189 variable->name = variable->stuff;
190 variable->value = variable->stuff + name_len;
191 strcpy(variable->name,name);
192 strcpy(variable->value,value);
198 void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
203 category->last->next = variable;
205 category->root = variable;
206 category->last = variable;
207 while (category->last->next)
208 category->last = category->last->next;
211 void ast_variables_destroy(struct ast_variable *v)
213 struct ast_variable *vn;
222 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
224 struct ast_category *cat = NULL;
226 if (category && config->last_browse && (config->last_browse->name == category))
227 cat = config->last_browse;
229 cat = ast_category_get(config, category);
231 return (cat) ? cat->root : NULL;
234 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
237 tmp = ast_variable_retrieve(cfg, cat, var);
239 tmp = ast_variable_retrieve(cfg, "general", var);
244 const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
246 struct ast_variable *v;
249 for (v = ast_variable_browse(config, category); v; v = v->next) {
250 if (!strcasecmp(variable, v->name))
254 struct ast_category *cat;
256 for (cat = config->root; cat; cat = cat->next)
257 for (v = cat->root; v; v = v->next)
258 if (!strcasecmp(variable, v->name))
265 static struct ast_variable *variable_clone(const struct ast_variable *old)
267 struct ast_variable *new = ast_variable_new(old->name, old->value);
270 new->lineno = old->lineno;
271 new->object = old->object;
272 new->blanklines = old->blanklines;
273 /* TODO: clone comments? */
279 static void move_variables(struct ast_category *old, struct ast_category *new)
281 struct ast_variable *var = old->root;
284 /* we can just move the entire list in a single op */
285 ast_variable_append(new, var);
288 struct ast_variable *next = var->next;
290 ast_variable_append(new, var);
296 struct ast_category *ast_category_new(const char *name)
298 struct ast_category *category;
300 if ((category = ast_calloc(1, sizeof(*category))))
301 ast_copy_string(category->name, name, sizeof(category->name));
305 static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
307 struct ast_category *cat;
309 /* try exact match first, then case-insensitive match */
310 for (cat = config->root; cat; cat = cat->next) {
311 if (cat->name == category_name && (ignored || !cat->ignored))
315 for (cat = config->root; cat; cat = cat->next) {
316 if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
323 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
325 return category_get(config, category_name, 0);
328 int ast_category_exist(const struct ast_config *config, const char *category_name)
330 return !!ast_category_get(config, category_name);
333 void ast_category_append(struct ast_config *config, struct ast_category *category)
336 config->last->next = category;
338 config->root = category;
339 category->include_level = config->include_level;
340 config->last = category;
341 config->current = category;
344 void ast_category_destroy(struct ast_category *cat)
346 ast_variables_destroy(cat->root);
350 static struct ast_category *next_available_category(struct ast_category *cat)
352 for (; cat && cat->ignored; cat = cat->next);
357 char *ast_category_browse(struct ast_config *config, const char *prev)
359 struct ast_category *cat = NULL;
361 if (prev && config->last_browse && (config->last_browse->name == prev))
362 cat = config->last_browse->next;
363 else if (!prev && config->root)
366 for (cat = config->root; cat; cat = cat->next) {
367 if (cat->name == prev) {
373 for (cat = config->root; cat; cat = cat->next) {
374 if (!strcasecmp(cat->name, prev)) {
383 cat = next_available_category(cat);
385 config->last_browse = cat;
386 return (cat) ? cat->name : NULL;
389 struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
391 struct ast_variable *v;
400 void ast_category_rename(struct ast_category *cat, const char *name)
402 ast_copy_string(cat->name, name, sizeof(cat->name));
405 static void inherit_category(struct ast_category *new, const struct ast_category *base)
407 struct ast_variable *var;
409 for (var = base->root; var; var = var->next)
410 ast_variable_append(new, variable_clone(var));
413 struct ast_config *ast_config_new(void)
415 struct ast_config *config;
417 if ((config = ast_calloc(1, sizeof(*config))))
418 config->max_include_level = MAX_INCLUDE_LEVEL;
422 int ast_variable_delete(struct ast_category *category, const char *variable, const char *match)
424 struct ast_variable *cur, *prev=NULL, *curn;
426 cur = category->root;
428 if (cur->name == variable) {
430 prev->next = cur->next;
431 if (cur == category->last)
432 category->last = prev;
434 category->root = cur->next;
435 if (cur == category->last)
436 category->last = NULL;
439 ast_variables_destroy(cur);
447 cur = category->root;
450 if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
452 prev->next = cur->next;
453 if (cur == category->last)
454 category->last = prev;
456 category->root = cur->next;
457 if (cur == category->last)
458 category->last = NULL;
461 ast_variables_destroy(cur);
471 int ast_variable_update(struct ast_category *category, const char *variable,
472 const char *value, const char *match, unsigned int object)
474 struct ast_variable *cur, *prev=NULL, *newer;
476 if (!(newer = ast_variable_new(variable, value)))
479 newer->object = object;
481 for (cur = category->root; cur; prev = cur, cur = cur->next) {
482 if (strcasecmp(cur->name, variable) ||
483 (!ast_strlen_zero(match) && strcasecmp(cur->value, match)))
486 newer->next = cur->next;
487 newer->object = cur->object || object;
491 category->root = newer;
492 if (category->last == cur)
493 category->last = newer;
496 ast_variables_destroy(cur);
504 category->root = newer;
509 int ast_category_delete(struct ast_config *cfg, const char *category)
511 struct ast_category *prev=NULL, *cat;
514 if (cat->name == category) {
515 ast_variables_destroy(cat->root);
517 prev->next = cat->next;
518 if (cat == cfg->last)
521 cfg->root = cat->next;
522 if (cat == cfg->last)
535 if (!strcasecmp(cat->name, category)) {
536 ast_variables_destroy(cat->root);
538 prev->next = cat->next;
539 if (cat == cfg->last)
542 cfg->root = cat->next;
543 if (cat == cfg->last)
555 void ast_config_destroy(struct ast_config *cfg)
557 struct ast_category *cat, *catn;
564 ast_variables_destroy(cat->root);
572 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
577 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
579 /* cast below is just to silence compiler warning about dropping "const" */
580 cfg->current = (struct ast_category *) cat;
583 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
584 char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
588 struct ast_variable *v;
589 char cmd[512], exec_file[512];
590 int object, do_exec, do_include;
592 /* Actually parse the entry */
594 struct ast_category *newcat = NULL;
597 /* A category header */
598 c = strchr(cur, ']');
600 ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
608 if (!(*cat = newcat = ast_category_new(catname))) {
612 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
613 newcat->precomments = ALLOC_COMMENT(*comment_buffer);
615 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
616 newcat->sameline = ALLOC_COMMENT(*lline_buffer);
619 CB_RESET(comment_buffer, lline_buffer);
621 /* If there are options or categories to inherit from, process them now */
623 if (!(cur = strchr(c, ')'))) {
624 ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
628 while ((cur = strsep(&c, ","))) {
629 if (!strcasecmp(cur, "!")) {
631 } else if (!strcasecmp(cur, "+")) {
632 *cat = category_get(cfg, catname, 1);
634 ast_config_destroy(cfg);
636 ast_category_destroy(newcat);
637 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
641 move_variables(newcat, *cat);
642 ast_category_destroy(newcat);
646 struct ast_category *base;
648 base = category_get(cfg, cur, 1);
650 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
653 inherit_category(*cat, base);
658 ast_category_append(cfg, *cat);
659 } else if (cur[0] == '#') {
663 while (*c && (*c > 32)) c++;
666 /* Find real argument */
667 c = ast_skip_blanks(c + 1);
672 do_include = !strcasecmp(cur, "include");
674 do_exec = !strcasecmp(cur, "exec");
677 if (do_exec && !ast_opt_exec_includes) {
678 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
681 if (do_include || do_exec) {
683 /* Strip off leading and trailing "'s and <>'s */
684 while ((*c == '<') || (*c == '>') || (*c == '\"')) c++;
685 /* Get rid of leading mess */
687 while (!ast_strlen_zero(cur)) {
688 c = cur + strlen(cur) - 1;
689 if ((*c == '>') || (*c == '<') || (*c == '\"'))
694 /* #exec </path/to/executable>
695 We create a tmp file, then we #include it, then we delete it. */
697 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
698 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
699 ast_safe_system(cmd);
704 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
705 if (!ast_strlen_zero(exec_file))
711 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
712 do_exec ? "exec" : "include",
713 do_exec ? "/path/to/executable" : "filename",
719 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
721 /* Just a line (variable = value) */
724 "parse error: No category context for line %d of %s\n", lineno, configfile);
727 c = strchr(cur, '=');
737 if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
740 /* Put and reset comments */
742 ast_variable_append(*cat, v);
744 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
745 v->precomments = ALLOC_COMMENT(*comment_buffer);
747 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
748 v->sameline = ALLOC_COMMENT(*lline_buffer);
751 CB_RESET(comment_buffer, lline_buffer);
757 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
763 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
767 char *new_buf, *comment_p, *process_buf;
770 int comment = 0, nest[MAX_NESTED_COMMENTS];
771 struct ast_category *cat = NULL;
774 /*! Growable string buffer */
775 char *comment_buffer=0; /*!< this will be a comment collector.*/
776 int comment_buffer_size=0; /*!< the amount of storage so far alloc'd for the comment_buffer */
778 char *lline_buffer=0; /*!< A buffer for stuff behind the ; */
779 int lline_buffer_size=0;
782 cat = ast_config_get_current_category(cfg);
784 if (filename[0] == '/') {
785 ast_copy_string(fn, filename, sizeof(fn));
787 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
791 CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
792 if (!lline_buffer || !comment_buffer) {
793 ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
797 #ifdef AST_INCLUDE_GLOB
801 globbuf.gl_offs = 0; /* initialize it to silence gcc */
803 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
805 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
807 if (glob_ret == GLOB_NOSPACE)
809 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
810 else if (glob_ret == GLOB_ABORTED)
812 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
814 /* loop over expanded files */
816 for (i=0; i<globbuf.gl_pathc; i++) {
817 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
820 if (stat(fn, &statbuf))
823 if (!S_ISREG(statbuf.st_mode)) {
824 ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
827 if (option_verbose > 1) {
828 ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
831 if (!(f = fopen(fn, "r"))) {
833 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
834 if (option_verbose > 1)
835 ast_verbose( "Not found (%s)\n", strerror(errno));
840 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
841 if (option_verbose > 1)
842 ast_verbose("Found\n");
845 if (fgets(buf, sizeof(buf), f)) {
846 if ( withcomments ) {
847 CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer); /* add the current lline buffer to the comment buffer */
848 lline_buffer[0] = 0; /* erase the lline buffer */
857 while ((comment_p = strchr(new_buf, COMMENT_META))) {
858 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
859 /* Yuck, gotta memmove */
860 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
862 } else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
863 /* Meta-Comment start detected ";--" */
864 if (comment < MAX_NESTED_COMMENTS) {
866 new_buf = comment_p + 3;
868 nest[comment-1] = lineno;
870 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
872 } else if ((comment_p >= new_buf + 2) &&
873 (*(comment_p - 1) == COMMENT_TAG) &&
874 (*(comment_p - 2) == COMMENT_TAG)) {
875 /* Meta-Comment end detected */
877 new_buf = comment_p + 1;
879 /* Back to non-comment now */
881 /* Actually have to move what's left over the top, then continue */
883 oldptr = process_buf + strlen(process_buf);
884 if ( withcomments ) {
885 CB_ADD(&comment_buffer, &comment_buffer_size, ";");
886 CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
889 memmove(oldptr, new_buf, strlen(new_buf) + 1);
892 process_buf = new_buf;
896 /* If ; is found, and we are not nested in a comment,
897 we immediately stop all comment processing */
898 if ( withcomments ) {
899 LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
904 new_buf = comment_p + 1;
907 if ( withcomments && comment && !process_buf )
909 CB_ADD(&comment_buffer, &comment_buffer_size, buf); /* the whole line is a comment, store it */
913 char *buf = ast_strip(process_buf);
914 if (!ast_strlen_zero(buf)) {
915 if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
926 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
928 #ifdef AST_INCLUDE_GLOB
937 if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
938 free(comment_buffer);
940 comment_buffer = NULL;
942 comment_buffer_size = 0;
943 lline_buffer_size = 0;
952 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
958 struct ast_variable *var;
959 struct ast_category *cat;
960 struct ast_comment *cmt;
963 if (configfile[0] == '/') {
964 ast_copy_string(fn, configfile, sizeof(fn));
966 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
969 ast_copy_string(date, ctime(&t), sizeof(date));
971 if ((f = fopen(fn, "w+"))) {
973 if ((f = fopen(fn, "w"))) {
975 if (option_verbose > 1)
976 ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
978 fprintf(f, ";! Automatically generated configuration file\n");
979 if (strcmp(configfile, fn))
980 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
982 fprintf(f, ";! Filename: %s\n", configfile);
983 fprintf(f, ";! Generator: %s\n", generator);
984 fprintf(f, ";! Creation Date: %s", date);
988 /* Dump section with any appropriate comment */
989 for (cmt = cat->precomments; cmt; cmt=cmt->next)
991 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
992 fprintf(f,"%s", cmt->cmt);
994 if (!cat->precomments)
996 fprintf(f, "[%s]", cat->name);
997 for (cmt = cat->sameline; cmt; cmt=cmt->next)
999 fprintf(f,"%s", cmt->cmt);
1005 for (cmt = var->precomments; cmt; cmt=cmt->next)
1007 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1008 fprintf(f,"%s", cmt->cmt);
1011 fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1013 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1014 if (var->blanklines) {
1015 blanklines = var->blanklines;
1016 while (blanklines--)
1023 /* Put an empty line */
1028 if ((option_verbose > 1) && !option_debug)
1029 ast_verbose("Saved\n");
1032 ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
1033 if (option_verbose > 1)
1034 ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
1041 static void clear_config_maps(void)
1043 struct ast_config_map *map;
1045 ast_mutex_lock(&config_lock);
1047 while (config_maps) {
1049 config_maps = config_maps->next;
1053 ast_mutex_unlock(&config_lock);
1056 static int append_mapping(char *name, char *driver, char *database, char *table)
1058 struct ast_config_map *map;
1061 length = sizeof(*map);
1062 length += strlen(name) + 1;
1063 length += strlen(driver) + 1;
1064 length += strlen(database) + 1;
1066 length += strlen(table) + 1;
1068 if (!(map = ast_calloc(1, length)))
1071 map->name = map->stuff;
1072 strcpy(map->name, name);
1073 map->driver = map->name + strlen(map->name) + 1;
1074 strcpy(map->driver, driver);
1075 map->database = map->driver + strlen(map->driver) + 1;
1076 strcpy(map->database, database);
1078 map->table = map->database + strlen(map->database) + 1;
1079 strcpy(map->table, table);
1081 map->next = config_maps;
1083 if (option_verbose > 1)
1084 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1085 map->name, map->driver, map->database, map->table ? map->table : map->name);
1091 int read_config_maps(void)
1093 struct ast_config *config, *configtmp;
1094 struct ast_variable *v;
1095 char *driver, *table, *database, *stringp, *tmp;
1097 clear_config_maps();
1099 configtmp = ast_config_new();
1100 configtmp->max_include_level = 1;
1101 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1103 ast_config_destroy(configtmp);
1107 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1109 driver = strsep(&stringp, ",");
1111 if ((tmp = strchr(stringp, '\"')))
1114 /* check if the database text starts with a double quote */
1115 if (*stringp == '"') {
1117 database = strsep(&stringp, "\"");
1118 strsep(&stringp, ",");
1120 /* apparently this text has no quotes */
1121 database = strsep(&stringp, ",");
1124 table = strsep(&stringp, ",");
1126 if (!strcmp(v->name, extconfig_conf)) {
1127 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1131 if (!strcmp(v->name, "asterisk.conf")) {
1132 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1136 if (!strcmp(v->name, "logger.conf")) {
1137 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1141 if (!driver || !database)
1143 if (!strcasecmp(v->name, "sipfriends")) {
1144 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");
1145 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1146 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1147 } else if (!strcasecmp(v->name, "iaxfriends")) {
1148 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");
1149 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1150 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1152 append_mapping(v->name, driver, database, table);
1155 ast_config_destroy(config);
1159 int ast_config_engine_register(struct ast_config_engine *new)
1161 struct ast_config_engine *ptr;
1163 ast_mutex_lock(&config_lock);
1165 if (!config_engine_list) {
1166 config_engine_list = new;
1168 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1172 ast_mutex_unlock(&config_lock);
1173 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1178 int ast_config_engine_deregister(struct ast_config_engine *del)
1180 struct ast_config_engine *ptr, *last=NULL;
1182 ast_mutex_lock(&config_lock);
1184 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1187 last->next = ptr->next;
1189 config_engine_list = ptr->next;
1195 ast_mutex_unlock(&config_lock);
1200 /*! \brief Find realtime engine for realtime family */
1201 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1203 struct ast_config_engine *eng, *ret = NULL;
1204 struct ast_config_map *map;
1206 ast_mutex_lock(&config_lock);
1208 for (map = config_maps; map; map = map->next) {
1209 if (!strcasecmp(family, map->name)) {
1211 ast_copy_string(database, map->database, dbsiz);
1213 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1218 /* Check if the required driver (engine) exist */
1220 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1221 if (!strcasecmp(eng->name, map->driver))
1226 ast_mutex_unlock(&config_lock);
1228 /* if we found a mapping, but the engine is not available, then issue a warning */
1230 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1235 static struct ast_config_engine text_file_engine = {
1237 .load_func = config_text_file_load,
1240 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1244 struct ast_config_engine *loader = &text_file_engine;
1245 struct ast_config *result;
1247 if (cfg->include_level == cfg->max_include_level) {
1248 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1252 cfg->include_level++;
1254 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1255 struct ast_config_engine *eng;
1257 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1260 if (eng && eng->load_func) {
1263 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1264 if (eng && eng->load_func)
1269 result = loader->load_func(db, table, filename, cfg, withcomments);
1272 result->include_level--;
1274 cfg->include_level--;
1279 struct ast_config *ast_config_load(const char *filename)
1281 struct ast_config *cfg;
1282 struct ast_config *result;
1284 cfg = ast_config_new();
1288 result = ast_config_internal_load(filename, cfg, 0);
1290 ast_config_destroy(cfg);
1295 struct ast_config *ast_config_load_with_comments(const char *filename)
1297 struct ast_config *cfg;
1298 struct ast_config *result;
1300 cfg = ast_config_new();
1304 result = ast_config_internal_load(filename, cfg, 1);
1306 ast_config_destroy(cfg);
1311 static struct ast_variable *ast_load_realtime_helper(const char *family, va_list ap)
1313 struct ast_config_engine *eng;
1316 struct ast_variable *res=NULL;
1318 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1319 if (eng && eng->realtime_func)
1320 res = eng->realtime_func(db, table, ap);
1325 struct ast_variable *ast_load_realtime_all(const char *family, ...)
1327 struct ast_variable *res;
1330 va_start(ap, family);
1331 res = ast_load_realtime_helper(family, ap);
1337 struct ast_variable *ast_load_realtime(const char *family, ...)
1339 struct ast_variable *res, *cur, *prev = NULL, *freeme = NULL;
1342 va_start(ap, family);
1343 res = ast_load_realtime_helper(family, ap);
1346 /* Eliminate blank entries */
1347 for (cur = res; cur; cur = cur->next) {
1353 if (ast_strlen_zero(cur->value)) {
1355 prev->next = cur->next;
1366 /*! \brief Check if realtime engine is configured for family */
1367 int ast_check_realtime(const char *family)
1369 struct ast_config_engine *eng;
1371 eng = find_engine(family, NULL, 0, NULL, 0);
1378 /*! \brief Check if there's any realtime engines loaded */
1379 int ast_realtime_enabled()
1381 return config_maps ? 1 : 0;
1384 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1386 struct ast_config_engine *eng;
1389 struct ast_config *res=NULL;
1392 va_start(ap, family);
1393 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1394 if (eng && eng->realtime_multi_func)
1395 res = eng->realtime_multi_func(db, table, ap);
1401 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1403 struct ast_config_engine *eng;
1409 va_start(ap, lookup);
1410 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1411 if (eng && eng->update_func)
1412 res = eng->update_func(db, table, keyfield, lookup, ap);
1418 int ast_store_realtime(const char *family, ...) {
1419 struct ast_config_engine *eng;
1425 va_start(ap, family);
1426 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1427 if (eng && eng->store_func)
1428 res = eng->store_func(db, table, ap);
1434 int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...) {
1435 struct ast_config_engine *eng;
1441 va_start(ap, lookup);
1442 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1443 if (eng && eng->destroy_func)
1444 res = eng->destroy_func(db, table, keyfield, lookup, ap);
1450 static int config_command(int fd, int argc, char **argv)
1452 struct ast_config_engine *eng;
1453 struct ast_config_map *map;
1455 ast_mutex_lock(&config_lock);
1457 ast_cli(fd, "\n\n");
1458 for (eng = config_engine_list; eng; eng = eng->next) {
1459 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1460 for (map = config_maps; map; map = map->next)
1461 if (!strcasecmp(map->driver, eng->name)) {
1462 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1463 map->table ? map->table : map->name);
1468 ast_mutex_unlock(&config_lock);
1473 static char show_config_help[] =
1474 "Usage: core show config mappings\n"
1475 " Shows the filenames to config engines.\n";
1477 static struct ast_cli_entry cli_config[] = {
1478 { { "core", "show", "config", "mappings", NULL },
1479 config_command, "Display config mappings (file names to config engines)",
1483 int register_config_cli()
1485 ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));