2 * Asterisk -- A telephony toolkit for Linux.
4 * Configuration File Parser
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
19 #include <asterisk/config.h>
20 #include <asterisk/options.h>
21 #include <asterisk/logger.h>
24 #define MAX_INCLUDE_LEVEL 10
28 struct ast_variable *root;
29 struct ast_category *next;
33 /* Maybe this structure isn't necessary but we'll keep it
35 struct ast_category *root;
38 static char *strip(char *buf)
41 /* Strip off trailing whitespace, returns, etc */
42 while(strlen(buf) && (buf[strlen(buf)-1]<33))
43 buf[strlen(buf)-1] = '\0';
45 /* Strip off leading whitespace, returns, etc */
46 while(*start && (*start < 33))
51 void ast_destroy(struct ast_config *ast)
53 struct ast_category *cat, *catn;
54 struct ast_variable *v, *vn;
80 /* Determine if this is a true value */
81 if (!strcasecmp(s, "yes") ||
82 !strcasecmp(s, "true") ||
83 !strcasecmp(s, "y") ||
84 !strcasecmp(s, "t") ||
90 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
92 struct ast_category *cat;
95 if (cat->name == category)
101 if (!strcasecmp(cat->name, category))
108 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
110 struct ast_variable *v;
112 v = ast_variable_browse(config, category);
114 if (value == v->name)
118 v = ast_variable_browse(config, category);
120 if (!strcasecmp(value, v->name))
125 struct ast_category *cat;
130 if (!strcasecmp(value, v->name))
140 int ast_category_exist(struct ast_config *config, char *category_name)
142 struct ast_category *category = NULL;
144 category = config->root;
147 if (!strcasecmp(category->name,category_name))
149 category = category->next;
155 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel);
156 static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel)
160 struct ast_variable *v;
161 /* Strip off lines using ; as comment */
162 c = strchr(buf, ';');
167 /* Actually parse the entry */
169 /* A category header */
170 c = strchr(cur, ']');
173 *_tmpc = malloc(sizeof(struct ast_category));
177 "Out of memory, line %d\n", lineno);
180 memset(*_tmpc, 0, sizeof(struct ast_category));
181 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
182 (*_tmpc)->root = NULL;
183 (*_tmpc)->next = tmp->root;
188 "parse error: no closing ']', line %d of %s\n", lineno, configfile);
190 } else if (cur[0] == '#') {
194 while(*c && (*c > 32)) c++;
198 /* Find real argument */
199 while(*c && (*c < 33)) c++;
204 if (!strcasecmp(cur, "include")) {
207 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
208 /* Get rid of leading mess */
211 c = cur + strlen(cur) - 1;
212 if ((*c == '>') || (*c == '<') || (*c == '\"'))
217 if (includelevel < MAX_INCLUDE_LEVEL) {
218 __ast_load(cur, tmp, _tmpc, _last, includelevel + 1);
220 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
222 ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
223 /* Strip off leading and trailing "'s and <>'s */
225 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
227 /* Just a line (variable = value) */
230 "parse error: No category context for line %d of %s\n", lineno, configfile);
232 c = strchr(cur, '=');
239 v = malloc(sizeof(struct ast_variable));
241 memset(v, 0, sizeof(struct ast_variable));
243 v->name = strdup(strip(cur));
244 v->value = strdup(strip(c));
253 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
257 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
265 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel)
272 if (configfile[0] == '/') {
273 strncpy(fn, configfile, sizeof(fn)-1);
275 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
277 if ((option_verbose > 1) && !option_debug) {
278 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
281 if ((f = fopen(fn, "r"))) {
283 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
284 else if (option_verbose > 1)
285 ast_verbose( "Found\n");
287 tmp = malloc(sizeof(struct ast_config));
289 memset(tmp, 0, sizeof(struct ast_config));
292 ast_log(LOG_WARNING, "Out of memory\n");
297 fgets(buf, sizeof(buf), f);
300 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel)) {
309 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
310 else if (option_verbose > 1)
311 ast_verbose( "Not found (%s)", strerror(errno));
316 struct ast_config *ast_load(char *configfile)
318 struct ast_category *tmpc=NULL;
319 struct ast_variable *last = NULL;
320 return __ast_load(configfile, NULL, &tmpc, &last, 0);
323 char *ast_category_browse(struct ast_config *config, char *prev)
325 struct ast_category *cat;
328 return config->root->name;
334 if (cat->name == prev) {
336 return cat->next->name;
344 if (!strcasecmp(cat->name, prev)) {
346 return cat->next->name;