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>
26 struct ast_variable *root;
27 struct ast_category *next;
31 /* Maybe this structure isn't necessary but we'll keep it
33 struct ast_category *root;
36 static char *strip(char *buf)
39 /* Strip off trailing whitespace, returns, etc */
40 while(strlen(buf) && (buf[strlen(buf)-1]<33))
41 buf[strlen(buf)-1] = '\0';
43 /* Strip off leading whitespace, returns, etc */
44 while(*start && (*start < 33))
49 void ast_destroy(struct ast_config *ast)
51 struct ast_category *cat, *catn;
52 struct ast_variable *v, *vn;
78 /* Determine if this is a true value */
79 if (!strcasecmp(s, "yes") ||
80 !strcasecmp(s, "true") ||
81 !strcasecmp(s, "y") ||
82 !strcasecmp(s, "t") ||
88 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
90 struct ast_category *cat;
93 if (cat->name == category)
99 if (!strcasecmp(cat->name, category))
106 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
108 struct ast_variable *v;
110 v = ast_variable_browse(config, category);
112 if (value == v->name)
116 v = ast_variable_browse(config, category);
118 if (!strcasecmp(value, v->name))
123 struct ast_category *cat;
128 if (!strcasecmp(value, v->name))
138 int ast_category_exist(struct ast_config *config, char *category_name)
140 struct ast_category *category = NULL;
142 category = config->root;
145 if (!strcasecmp(category->name,category_name))
147 category = category->next;
153 struct ast_config *ast_load(char *configfile)
157 struct ast_config *tmp=NULL;
158 struct ast_category *tmpc=NULL;
159 struct ast_variable *v, *last=NULL;
164 if (configfile[0] == '/') {
165 strncpy(fn, configfile, sizeof(fn)-1);
167 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
169 if ((option_verbose > 1) && !option_debug) {
170 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
173 if ((f = fopen(fn, "r"))) {
175 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
176 else if (option_verbose > 1)
177 ast_verbose( "Found\n");
178 tmp = malloc(sizeof(struct ast_config));
180 ast_log(LOG_WARNING, "Out of memory\n");
186 fgets(buf, sizeof(buf), f);
189 /* Strip off lines using ; as comment */
190 c = strchr(buf, ';');
195 /* Actually parse the entry */
197 /* A category header */
198 c = strchr(cur, ']');
203 * Check category duplicity before structure
206 if (ast_category_exist(tmp,cur+1)) {
209 "Found duplicit category [%s] in "
211 cur+1,configfile,lineno);
216 tmpc = malloc(sizeof(struct ast_category));
220 "Out of memory, line %d\n", lineno);
224 strncpy(tmpc->name, cur+1, sizeof(tmpc->name)-1);
226 tmpc->next = tmp->root;
231 "parse error: no closing ']', line %d\n", lineno);
234 /* Just a line (variable = value) */
237 "parse error: No category context for line %d\n", lineno);
239 c = strchr(cur, '=');
246 v = malloc(sizeof(struct ast_variable));
249 v->name = strdup(strip(cur));
250 v->value = strdup(strip(c));
259 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
264 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
274 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
275 else if (option_verbose > 1)
276 ast_verbose( "Not found (%s)", strerror(errno));
281 char *ast_category_browse(struct ast_config *config, char *prev)
283 struct ast_category *cat;
286 return config->root->name;
292 if (cat->name == prev) {
294 return cat->next->name;
302 if (!strcasecmp(cat->name, prev)) {
304 return cat->next->name;