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;
75 /* Determine if this is a true value */
76 if (!strcasecmp(s, "yes") ||
77 !strcasecmp(s, "true") ||
78 !strcasecmp(s, "y") ||
79 !strcasecmp(s, "t") ||
85 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
87 struct ast_category *cat;
90 if (!strcasecmp(cat->name, category))
97 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
99 struct ast_variable *v;
101 v = ast_variable_browse(config, category);
103 if (!strcasecmp(value, v->name))
108 struct ast_category *cat;
113 if (!strcasecmp(value, v->name))
123 struct ast_config *ast_load(char *configfile)
127 struct ast_config *tmp=NULL;
128 struct ast_category *tmpc=NULL;
129 struct ast_variable *v, *last=NULL;
133 if (configfile[0] == '/') {
134 strncpy(fn, configfile, sizeof(fn));
136 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
138 if ((option_verbose > 1) && !option_debug) {
139 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
142 if ((f = fopen(fn, "r"))) {
144 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
145 else if (option_verbose > 1)
146 ast_verbose( "Found\n");
147 tmp = malloc(sizeof(struct ast_config));
149 ast_log(LOG_WARNING, "Out of memory\n");
155 fgets(buf, sizeof(buf), f);
158 /* Strip off lines using ; as comment */
159 c = strchr(buf, ';');
164 /* Actually parse the entry */
166 /* A category header */
167 /* XXX Don't let them use the same category twice XXX */
168 c = strchr(cur, ']');
171 tmpc = malloc(sizeof(struct ast_category));
175 "Out of memory, line %d\n", lineno);
179 strncpy(tmpc->name, cur+1, sizeof(tmpc->name));
181 tmpc->next = tmp->root;
186 "parse error: no closing ']', line %d\n", lineno);
192 /* Just a line (variable = value) */
195 "parse error: No category context for line %d\n", lineno);
200 c = strchr(cur, '=');
204 v = malloc(sizeof(struct ast_variable));
207 v->name = strdup(strip(cur));
208 v->value = strdup(strip(c));
215 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
220 ast_log(LOG_WARNING, "No = in line %d\n", lineno);
232 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
233 else if (option_verbose > 1)
234 ast_verbose( "Not found (%s)", strerror(errno));
239 char *ast_category_browse(struct ast_config *config, char *prev)
241 struct ast_category *cat;
244 return config->root->name;
250 if (!strcasecmp(cat->name, prev)) {
252 return cat->next->name;