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);
635 ast_category_destroy(newcat);
636 ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
640 move_variables(newcat, *cat);
641 ast_category_destroy(newcat);
645 struct ast_category *base;
647 base = category_get(cfg, cur, 1);
649 ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
652 inherit_category(*cat, base);
657 ast_category_append(cfg, *cat);
658 } else if (cur[0] == '#') {
662 while (*c && (*c > 32)) c++;
665 /* Find real argument */
666 c = ast_skip_blanks(c + 1);
671 do_include = !strcasecmp(cur, "include");
673 do_exec = !strcasecmp(cur, "exec");
676 if (do_exec && !ast_opt_exec_includes) {
677 ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
680 if (do_include || do_exec) {
682 /* Strip off leading and trailing "'s and <>'s */
683 while ((*c == '<') || (*c == '>') || (*c == '\"')) c++;
684 /* Get rid of leading mess */
686 while (!ast_strlen_zero(cur)) {
687 c = cur + strlen(cur) - 1;
688 if ((*c == '>') || (*c == '<') || (*c == '\"'))
693 /* #exec </path/to/executable>
694 We create a tmp file, then we #include it, then we delete it. */
696 snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
697 snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
698 ast_safe_system(cmd);
703 do_include = ast_config_internal_load(cur, cfg, withcomments) ? 1 : 0;
704 if (!ast_strlen_zero(exec_file))
710 ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
711 do_exec ? "exec" : "include",
712 do_exec ? "/path/to/executable" : "filename",
718 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
720 /* Just a line (variable = value) */
723 "parse error: No category context for line %d of %s\n", lineno, configfile);
726 c = strchr(cur, '=');
736 if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
739 /* Put and reset comments */
741 ast_variable_append(*cat, v);
743 if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
744 v->precomments = ALLOC_COMMENT(*comment_buffer);
746 if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
747 v->sameline = ALLOC_COMMENT(*lline_buffer);
750 CB_RESET(comment_buffer, lline_buffer);
756 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
762 static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments)
766 char *new_buf, *comment_p, *process_buf;
769 int comment = 0, nest[MAX_NESTED_COMMENTS];
770 struct ast_category *cat = NULL;
773 /*! Growable string buffer */
774 char *comment_buffer=0; /*!< this will be a comment collector.*/
775 int comment_buffer_size=0; /*!< the amount of storage so far alloc'd for the comment_buffer */
777 char *lline_buffer=0; /*!< A buffer for stuff behind the ; */
778 int lline_buffer_size=0;
781 cat = ast_config_get_current_category(cfg);
783 if (filename[0] == '/') {
784 ast_copy_string(fn, filename, sizeof(fn));
786 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
790 CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
791 if (!lline_buffer || !comment_buffer) {
792 ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
796 #ifdef AST_INCLUDE_GLOB
800 globbuf.gl_offs = 0; /* initialize it to silence gcc */
802 glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
804 glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
806 if (glob_ret == GLOB_NOSPACE)
808 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
809 else if (glob_ret == GLOB_ABORTED)
811 "Glob Expansion of pattern '%s' failed: Read error\n", fn);
813 /* loop over expanded files */
815 for (i=0; i<globbuf.gl_pathc; i++) {
816 ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
819 if (stat(fn, &statbuf))
822 if (!S_ISREG(statbuf.st_mode)) {
823 ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
826 if (option_verbose > 1) {
827 ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
830 if (!(f = fopen(fn, "r"))) {
832 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
833 if (option_verbose > 1)
834 ast_verbose( "Not found (%s)\n", strerror(errno));
839 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
840 if (option_verbose > 1)
841 ast_verbose("Found\n");
844 if (fgets(buf, sizeof(buf), f)) {
845 if ( withcomments ) {
846 CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer); /* add the current lline buffer to the comment buffer */
847 lline_buffer[0] = 0; /* erase the lline buffer */
856 while ((comment_p = strchr(new_buf, COMMENT_META))) {
857 if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
858 /* Yuck, gotta memmove */
859 memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
861 } else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
862 /* Meta-Comment start detected ";--" */
863 if (comment < MAX_NESTED_COMMENTS) {
865 new_buf = comment_p + 3;
867 nest[comment-1] = lineno;
869 ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
871 } else if ((comment_p >= new_buf + 2) &&
872 (*(comment_p - 1) == COMMENT_TAG) &&
873 (*(comment_p - 2) == COMMENT_TAG)) {
874 /* Meta-Comment end detected */
876 new_buf = comment_p + 1;
878 /* Back to non-comment now */
880 /* Actually have to move what's left over the top, then continue */
882 oldptr = process_buf + strlen(process_buf);
883 if ( withcomments ) {
884 CB_ADD(&comment_buffer, &comment_buffer_size, ";");
885 CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
888 memmove(oldptr, new_buf, strlen(new_buf) + 1);
891 process_buf = new_buf;
895 /* If ; is found, and we are not nested in a comment,
896 we immediately stop all comment processing */
897 if ( withcomments ) {
898 LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
903 new_buf = comment_p + 1;
906 if ( withcomments && comment && !process_buf )
908 CB_ADD(&comment_buffer, &comment_buffer_size, buf); /* the whole line is a comment, store it */
912 char *buf = ast_strip(process_buf);
913 if (!ast_strlen_zero(buf)) {
914 if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
925 ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment]);
927 #ifdef AST_INCLUDE_GLOB
936 if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
937 free(comment_buffer);
939 comment_buffer = NULL;
941 comment_buffer_size = 0;
942 lline_buffer_size = 0;
951 int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
957 struct ast_variable *var;
958 struct ast_category *cat;
959 struct ast_comment *cmt;
962 if (configfile[0] == '/') {
963 ast_copy_string(fn, configfile, sizeof(fn));
965 snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
968 ast_copy_string(date, ctime(&t), sizeof(date));
970 if ((f = fopen(fn, "w+"))) {
972 if ((f = fopen(fn, "w"))) {
974 if (option_verbose > 1)
975 ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
977 fprintf(f, ";! Automatically generated configuration file\n");
978 if (strcmp(configfile, fn))
979 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
981 fprintf(f, ";! Filename: %s\n", configfile);
982 fprintf(f, ";! Generator: %s\n", generator);
983 fprintf(f, ";! Creation Date: %s", date);
987 /* Dump section with any appropriate comment */
988 for (cmt = cat->precomments; cmt; cmt=cmt->next)
990 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
991 fprintf(f,"%s", cmt->cmt);
993 if (!cat->precomments)
995 fprintf(f, "[%s]", cat->name);
996 for (cmt = cat->sameline; cmt; cmt=cmt->next)
998 fprintf(f,"%s", cmt->cmt);
1004 for (cmt = var->precomments; cmt; cmt=cmt->next)
1006 if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
1007 fprintf(f,"%s", cmt->cmt);
1010 fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
1012 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
1013 if (var->blanklines) {
1014 blanklines = var->blanklines;
1015 while (blanklines--)
1022 /* Put an empty line */
1027 if ((option_verbose > 1) && !option_debug)
1028 ast_verbose("Saved\n");
1031 ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
1032 if (option_verbose > 1)
1033 ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
1040 static void clear_config_maps(void)
1042 struct ast_config_map *map;
1044 ast_mutex_lock(&config_lock);
1046 while (config_maps) {
1048 config_maps = config_maps->next;
1052 ast_mutex_unlock(&config_lock);
1055 static int append_mapping(char *name, char *driver, char *database, char *table)
1057 struct ast_config_map *map;
1060 length = sizeof(*map);
1061 length += strlen(name) + 1;
1062 length += strlen(driver) + 1;
1063 length += strlen(database) + 1;
1065 length += strlen(table) + 1;
1067 if (!(map = ast_calloc(1, length)))
1070 map->name = map->stuff;
1071 strcpy(map->name, name);
1072 map->driver = map->name + strlen(map->name) + 1;
1073 strcpy(map->driver, driver);
1074 map->database = map->driver + strlen(map->driver) + 1;
1075 strcpy(map->database, database);
1077 map->table = map->database + strlen(map->database) + 1;
1078 strcpy(map->table, table);
1080 map->next = config_maps;
1082 if (option_verbose > 1)
1083 ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
1084 map->name, map->driver, map->database, map->table ? map->table : map->name);
1090 int read_config_maps(void)
1092 struct ast_config *config, *configtmp;
1093 struct ast_variable *v;
1094 char *driver, *table, *database, *stringp, *tmp;
1096 clear_config_maps();
1098 configtmp = ast_config_new();
1099 configtmp->max_include_level = 1;
1100 config = ast_config_internal_load(extconfig_conf, configtmp, 0);
1102 ast_config_destroy(configtmp);
1106 for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
1108 driver = strsep(&stringp, ",");
1110 if ((tmp = strchr(stringp, '\"')))
1113 /* check if the database text starts with a double quote */
1114 if (*stringp == '"') {
1116 database = strsep(&stringp, "\"");
1117 strsep(&stringp, ",");
1119 /* apparently this text has no quotes */
1120 database = strsep(&stringp, ",");
1123 table = strsep(&stringp, ",");
1125 if (!strcmp(v->name, extconfig_conf)) {
1126 ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
1130 if (!strcmp(v->name, "asterisk.conf")) {
1131 ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
1135 if (!strcmp(v->name, "logger.conf")) {
1136 ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
1140 if (!driver || !database)
1142 if (!strcasecmp(v->name, "sipfriends")) {
1143 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");
1144 append_mapping("sipusers", driver, database, table ? table : "sipfriends");
1145 append_mapping("sippeers", driver, database, table ? table : "sipfriends");
1146 } else if (!strcasecmp(v->name, "iaxfriends")) {
1147 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");
1148 append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
1149 append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
1151 append_mapping(v->name, driver, database, table);
1154 ast_config_destroy(config);
1158 int ast_config_engine_register(struct ast_config_engine *new)
1160 struct ast_config_engine *ptr;
1162 ast_mutex_lock(&config_lock);
1164 if (!config_engine_list) {
1165 config_engine_list = new;
1167 for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
1171 ast_mutex_unlock(&config_lock);
1172 ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
1177 int ast_config_engine_deregister(struct ast_config_engine *del)
1179 struct ast_config_engine *ptr, *last=NULL;
1181 ast_mutex_lock(&config_lock);
1183 for (ptr = config_engine_list; ptr; ptr=ptr->next) {
1186 last->next = ptr->next;
1188 config_engine_list = ptr->next;
1194 ast_mutex_unlock(&config_lock);
1199 /*! \brief Find realtime engine for realtime family */
1200 static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
1202 struct ast_config_engine *eng, *ret = NULL;
1203 struct ast_config_map *map;
1205 ast_mutex_lock(&config_lock);
1207 for (map = config_maps; map; map = map->next) {
1208 if (!strcasecmp(family, map->name)) {
1210 ast_copy_string(database, map->database, dbsiz);
1212 ast_copy_string(table, map->table ? map->table : family, tabsiz);
1217 /* Check if the required driver (engine) exist */
1219 for (eng = config_engine_list; !ret && eng; eng = eng->next) {
1220 if (!strcasecmp(eng->name, map->driver))
1225 ast_mutex_unlock(&config_lock);
1227 /* if we found a mapping, but the engine is not available, then issue a warning */
1229 ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
1234 static struct ast_config_engine text_file_engine = {
1236 .load_func = config_text_file_load,
1239 struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, int withcomments)
1243 struct ast_config_engine *loader = &text_file_engine;
1244 struct ast_config *result;
1246 if (cfg->include_level == cfg->max_include_level) {
1247 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
1251 cfg->include_level++;
1253 if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
1254 struct ast_config_engine *eng;
1256 eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
1259 if (eng && eng->load_func) {
1262 eng = find_engine("global", db, sizeof(db), table, sizeof(table));
1263 if (eng && eng->load_func)
1268 result = loader->load_func(db, table, filename, cfg, withcomments);
1271 result->include_level--;
1273 cfg->include_level--;
1278 struct ast_config *ast_config_load(const char *filename)
1280 struct ast_config *cfg;
1281 struct ast_config *result;
1283 cfg = ast_config_new();
1287 result = ast_config_internal_load(filename, cfg, 0);
1289 ast_config_destroy(cfg);
1294 struct ast_config *ast_config_load_with_comments(const char *filename)
1296 struct ast_config *cfg;
1297 struct ast_config *result;
1299 cfg = ast_config_new();
1303 result = ast_config_internal_load(filename, cfg, 1);
1305 ast_config_destroy(cfg);
1310 static struct ast_variable *ast_load_realtime_helper(const char *family, va_list ap)
1312 struct ast_config_engine *eng;
1315 struct ast_variable *res=NULL;
1317 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1318 if (eng && eng->realtime_func)
1319 res = eng->realtime_func(db, table, ap);
1324 struct ast_variable *ast_load_realtime_all(const char *family, ...)
1326 struct ast_variable *res;
1329 va_start(ap, family);
1330 res = ast_load_realtime_helper(family, ap);
1336 struct ast_variable *ast_load_realtime(const char *family, ...)
1338 struct ast_variable *res, *cur, *prev = NULL, *freeme = NULL;
1341 va_start(ap, family);
1342 res = ast_load_realtime_helper(family, ap);
1345 /* Eliminate blank entries */
1346 for (cur = res; cur; cur = cur->next) {
1352 if (ast_strlen_zero(cur->value)) {
1354 prev->next = cur->next;
1365 /*! \brief Check if realtime engine is configured for family */
1366 int ast_check_realtime(const char *family)
1368 struct ast_config_engine *eng;
1370 eng = find_engine(family, NULL, 0, NULL, 0);
1377 /*! \brief Check if there's any realtime engines loaded */
1378 int ast_realtime_enabled()
1380 return config_maps ? 1 : 0;
1383 struct ast_config *ast_load_realtime_multientry(const char *family, ...)
1385 struct ast_config_engine *eng;
1388 struct ast_config *res=NULL;
1391 va_start(ap, family);
1392 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1393 if (eng && eng->realtime_multi_func)
1394 res = eng->realtime_multi_func(db, table, ap);
1400 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
1402 struct ast_config_engine *eng;
1408 va_start(ap, lookup);
1409 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1410 if (eng && eng->update_func)
1411 res = eng->update_func(db, table, keyfield, lookup, ap);
1417 int ast_store_realtime(const char *family, ...) {
1418 struct ast_config_engine *eng;
1424 va_start(ap, family);
1425 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1426 if (eng && eng->store_func)
1427 res = eng->store_func(db, table, ap);
1433 int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...) {
1434 struct ast_config_engine *eng;
1440 va_start(ap, lookup);
1441 eng = find_engine(family, db, sizeof(db), table, sizeof(table));
1442 if (eng && eng->destroy_func)
1443 res = eng->destroy_func(db, table, keyfield, lookup, ap);
1449 static int config_command(int fd, int argc, char **argv)
1451 struct ast_config_engine *eng;
1452 struct ast_config_map *map;
1454 ast_mutex_lock(&config_lock);
1456 ast_cli(fd, "\n\n");
1457 for (eng = config_engine_list; eng; eng = eng->next) {
1458 ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
1459 for (map = config_maps; map; map = map->next)
1460 if (!strcasecmp(map->driver, eng->name)) {
1461 ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
1462 map->table ? map->table : map->name);
1467 ast_mutex_unlock(&config_lock);
1472 static char show_config_help[] =
1473 "Usage: core show config mappings\n"
1474 " Shows the filenames to config engines.\n";
1476 static struct ast_cli_entry cli_config[] = {
1477 { { "core", "show", "config", "mappings", NULL },
1478 config_command, "Display config mappings (file names to config engines)",
1482 int register_config_cli()
1484 ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));