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 (!strcasecmp(cat->name, category))
100 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
102 struct ast_variable *v;
104 v = ast_variable_browse(config, category);
106 if (!strcasecmp(value, v->name))
111 struct ast_category *cat;
116 if (!strcasecmp(value, v->name))
126 int ast_category_exist(struct ast_config *config, char *category_name)
128 struct ast_category *category = NULL;
130 category = config->root;
133 if (!strcasecmp(category->name,category_name))
135 category = category->next;
141 struct ast_config *ast_load(char *configfile)
145 struct ast_config *tmp=NULL;
146 struct ast_category *tmpc=NULL;
147 struct ast_variable *v, *last=NULL;
152 if (configfile[0] == '/') {
153 strncpy(fn, configfile, sizeof(fn));
155 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
157 if ((option_verbose > 1) && !option_debug) {
158 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
161 if ((f = fopen(fn, "r"))) {
163 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
164 else if (option_verbose > 1)
165 ast_verbose( "Found\n");
166 tmp = malloc(sizeof(struct ast_config));
168 ast_log(LOG_WARNING, "Out of memory\n");
174 fgets(buf, sizeof(buf), f);
177 /* Strip off lines using ; as comment */
178 c = strchr(buf, ';');
183 /* Actually parse the entry */
185 /* A category header */
186 c = strchr(cur, ']');
190 * Check category duplicity before structure
193 if (ast_category_exist(tmp,cur+1)) {
196 "Found duplicit category [%s] in "
198 cur+1,configfile,lineno);
203 tmpc = malloc(sizeof(struct ast_category));
207 "Out of memory, line %d\n", lineno);
211 strncpy(tmpc->name, cur+1, sizeof(tmpc->name));
213 tmpc->next = tmp->root;
218 "parse error: no closing ']', line %d\n", lineno);
224 /* Just a line (variable = value) */
227 "parse error: No category context for line %d\n", lineno);
232 c = strchr(cur, '=');
239 v = malloc(sizeof(struct ast_variable));
242 v->name = strdup(strip(cur));
243 v->value = strdup(strip(c));
251 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
256 ast_log(LOG_WARNING, "No = in line %d\n", lineno);
268 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
269 else if (option_verbose > 1)
270 ast_verbose( "Not found (%s)", strerror(errno));
275 char *ast_category_browse(struct ast_config *config, char *prev)
277 struct ast_category *cat;
280 return config->root->name;
286 if (!strcasecmp(cat->name, prev)) {
288 return cat->next->name;