Version 0.1.8 from FTP
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <asterisk/config.h>
20 #include <asterisk/options.h>
21 #include <asterisk/logger.h>
22 #include "asterisk.h"
23
24 struct ast_category {
25         char name[80];
26         struct ast_variable *root;
27         struct ast_category *next;
28 };
29
30 struct ast_config {
31         /* Maybe this structure isn't necessary but we'll keep it
32            for now */
33         struct ast_category *root;
34 };
35
36 static char *strip(char *buf)
37 {
38         char *start;
39         /* Strip off trailing whitespace, returns, etc */
40         while(strlen(buf) && (buf[strlen(buf)-1]<33))
41                 buf[strlen(buf)-1] = '\0';
42         start = buf;
43         /* Strip off leading whitespace, returns, etc */
44         while(*start && (*start < 33))
45                 *start++ = '\0';
46         return start;
47 }
48
49 void ast_destroy(struct ast_config *ast)
50 {
51         struct ast_category *cat, *catn;
52         struct ast_variable *v, *vn;
53
54         if (!ast)
55                 return;
56
57         cat = ast->root;
58         while(cat) {
59                 v = cat->root;
60                 while(v) {
61                         vn = v;
62                         free(v->name);
63                         free(v->value);
64                         v = v->next;
65                         free(vn);
66                 }
67                 catn = cat;
68                 cat = cat->next;
69                 free(catn);
70         }
71         free(ast);
72 }
73
74 int ast_true(char *s)
75 {
76         if (!s)
77                 return 0;
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") ||
83                 !strcasecmp(s, "1"))
84                         return -1;
85         return 0;
86 }
87
88 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
89 {
90         struct ast_category *cat;
91         cat = config->root;
92         while(cat) {
93                 if (!strcasecmp(cat->name, category))
94                         return cat->root;
95                 cat = cat->next;
96         }
97         return NULL;
98 }
99
100 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
101 {
102         struct ast_variable *v;
103         if (category) {
104                 v = ast_variable_browse(config, category);
105                 while (v) {
106                         if (!strcasecmp(value, v->name))
107                                 return v->value;
108                         v=v->next;
109                 }
110         } else {
111                 struct ast_category *cat;
112                 cat = config->root;
113                 while(cat) {
114                         v = cat->root;
115                         while (v) {
116                                 if (!strcasecmp(value, v->name))
117                                         return v->value;
118                                 v=v->next;
119                         }
120                         cat = cat->next;
121                 }
122         }
123         return NULL;
124 }
125
126 int ast_category_exist(struct ast_config *config, char *category_name)
127 {
128         struct ast_category *category = NULL;
129
130         category = config->root;
131
132         while(category) {
133                 if (!strcasecmp(category->name,category_name)) 
134                         return 1;
135                 category = category->next;
136         } 
137
138         return 0;
139 }
140
141 struct ast_config *ast_load(char *configfile)
142 {
143         char fn[256];
144         char buf[256];
145         struct ast_config *tmp=NULL;
146         struct ast_category *tmpc=NULL;
147         struct ast_variable *v, *last=NULL;
148         FILE *f;
149         char *c, *cur;
150         int lineno=0;
151
152         if (configfile[0] == '/') {
153                 strncpy(fn, configfile, sizeof(fn));
154         } else {
155                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
156         }
157         if ((option_verbose > 1) && !option_debug) {
158                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
159                 fflush(stdout);
160         }
161         if ((f = fopen(fn, "r"))) {
162                 if (option_debug)
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));
167                 if (!tmp) {
168                         ast_log(LOG_WARNING, "Out of memory\n");
169                         fclose(f);
170                         return NULL;
171                 }
172                 tmp->root = NULL;
173                 while(!feof(f)) {
174                         fgets(buf, sizeof(buf), f);
175                         lineno++;
176                         if (!feof(f)) {
177                                 /* Strip off lines using ; as comment */
178                                 c = strchr(buf, ';');
179                                 if (c)
180                                         *c = '\0';
181                                 cur = strip(buf);
182                                 if (strlen(cur)) {
183                                         /* Actually parse the entry */
184                                         if (cur[0] == '[') {
185                                                 /* A category header */
186                                                 c = strchr(cur, ']');
187                                                 if (c) {
188                                                         *c = 0;
189                                                         /* 
190                                                          * Check category duplicity before structure
191                                                          * allocation
192                                                          */
193                                                         if (ast_category_exist(tmp,cur+1)) {
194                                                                 ast_destroy(tmp);
195                                                                 ast_log(LOG_WARNING,
196                                                                         "Found duplicit category [%s] in "
197                                                                         "file %s line %d\n",
198                                                                         cur+1,configfile,lineno);
199                                                                 fclose(f);
200                                                                 return NULL; 
201                                                         }
202                                                 
203                                                         tmpc = malloc(sizeof(struct ast_category));
204                                                         if (!tmpc) {
205                                                                 ast_destroy(tmp);
206                                                                 ast_log(LOG_WARNING,
207                                                                         "Out of memory, line %d\n", lineno);
208                                                                 fclose(f);
209                                                                 return NULL;
210                                                         }
211                                                         strncpy(tmpc->name, cur+1, sizeof(tmpc->name));
212                                                         tmpc->root =  NULL;
213                                                         tmpc->next = tmp->root;
214                                                         tmp->root = tmpc;
215                                                         last =  NULL;
216                                                 } else {
217                                                         ast_log(LOG_WARNING, 
218                                                                 "parse error: no closing ']', line %d\n", lineno);
219                                                         ast_destroy(tmp);
220                                                         fclose(f);
221                                                         return NULL;
222                                                 }
223                                         } else {
224                                                 /* Just a line (variable = value) */
225                                                 if (!tmpc) {
226                                                         ast_log(LOG_WARNING,
227                                                                 "parse error: No category context for line %d\n", lineno);
228                                                         ast_destroy(tmp);
229                                                         fclose(f);
230                                                         return NULL;
231                                                 }
232                                                 c = strchr(cur, '=');
233                                                 if (c) {
234                                                         *c = 0;
235                                                         c++;
236                                                         /* Ignore > in => */
237                                                         if (*c== '>')
238                                                                 c++;
239                                                         v = malloc(sizeof(struct ast_variable));
240                                                         if (v) {
241                                                                 v->next = NULL;
242                                                                 v->name = strdup(strip(cur));
243                                                                 v->value = strdup(strip(c));
244                                                                 v->lineno = lineno;
245                                                                 if (last)  
246                                                                         last->next = v;
247                                                                 else
248                                                                         tmpc->root = v;
249                                                                 last = v;
250                                                         } else {
251                                                                 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
252                                                                 fclose(f);
253                                                                 ast_destroy(tmp);
254                                                         }
255                                                 } else {
256                                                         ast_log(LOG_WARNING, "No = in line %d\n", lineno);
257                                                         fclose(f);
258                                                         ast_destroy(tmp);
259                                                 }
260                                                                                                                 
261                                         }
262                                 }
263                         }
264                 }
265                 fclose(f);              
266         } else {
267                 if (option_debug)
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));
271         }
272         return tmp;
273 }
274
275 char *ast_category_browse(struct ast_config *config, char *prev)
276 {       
277         struct ast_category *cat;
278         if (!prev) {
279                 if (config->root)
280                         return config->root->name;
281                 else
282                         return NULL;
283         }
284         cat = config->root;
285         while(cat) {
286                 if (!strcasecmp(cat->name, prev)) {
287                         if (cat->next)
288                                 return cat->next->name;
289                         else
290                                 return NULL;
291                 }
292                 cat = cat->next;
293         }
294         return NULL;
295 }