Version 0.1.7 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 struct ast_config *ast_load(char *configfile)
127 {
128         char fn[256];
129         char buf[256];
130         struct ast_config *tmp=NULL;
131         struct ast_category *tmpc=NULL;
132         struct ast_variable *v, *last=NULL;
133         FILE *f;
134         char *c, *cur;
135         int lineno=0;
136         if (configfile[0] == '/') {
137                 strncpy(fn, configfile, sizeof(fn));
138         } else {
139                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
140         }
141         if ((option_verbose > 1) && !option_debug) {
142                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
143                 fflush(stdout);
144         }
145         if ((f = fopen(fn, "r"))) {
146                 if (option_debug)
147                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
148                 else if (option_verbose > 1)
149                         ast_verbose( "Found\n");
150                 tmp = malloc(sizeof(struct ast_config));
151                 if (!tmp) {
152                         ast_log(LOG_WARNING, "Out of memory\n");
153                         fclose(f);
154                         return NULL;
155                 }
156                 tmp->root = NULL;
157                 while(!feof(f)) {
158                         fgets(buf, sizeof(buf), f);
159                         lineno++;
160                         if (!feof(f)) {
161                                 /* Strip off lines using ; as comment */
162                                 c = strchr(buf, ';');
163                                 if (c)
164                                         *c = '\0';
165                                 cur = strip(buf);
166                                 if (strlen(cur)) {
167                                         /* Actually parse the entry */
168                                         if (cur[0] == '[') {
169                                                 /* A category header */
170                                                 /* XXX Don't let them use the same category twice XXX */
171                                                 c = strchr(cur, ']');
172                                                 if (c) {
173                                                         *c = 0;
174                                                         tmpc = malloc(sizeof(struct ast_category));
175                                                         if (!tmpc) {
176                                                                 ast_destroy(tmp);
177                                                                 ast_log(LOG_WARNING,
178                                                                         "Out of memory, line %d\n", lineno);
179                                                                 fclose(f);
180                                                                 return NULL;
181                                                         }
182                                                         strncpy(tmpc->name, cur+1, sizeof(tmpc->name));
183                                                         tmpc->root =  NULL;
184                                                         tmpc->next = tmp->root;
185                                                         tmp->root = tmpc;
186                                                         last =  NULL;
187                                                 } else {
188                                                         ast_log(LOG_WARNING, 
189                                                                 "parse error: no closing ']', line %d\n", lineno);
190                                                         ast_destroy(tmp);
191                                                         fclose(f);
192                                                         return NULL;
193                                                 }
194                                         } else {
195                                                 /* Just a line (variable = value) */
196                                                 if (!tmpc) {
197                                                         ast_log(LOG_WARNING,
198                                                                 "parse error: No category context for line %d\n", lineno);
199                                                         ast_destroy(tmp);
200                                                         fclose(f);
201                                                         return NULL;
202                                                 }
203                                                 c = strchr(cur, '=');
204                                                 if (c) {
205                                                         *c = 0;
206                                                         c++;
207                                                         v = malloc(sizeof(struct ast_variable));
208                                                         if (v) {
209                                                                 v->next = NULL;
210                                                                 v->name = strdup(strip(cur));
211                                                                 v->value = strdup(strip(c));
212                                                                 if (last)  
213                                                                         last->next = v;
214                                                                 else
215                                                                         tmpc->root = v;
216                                                                 last = v;
217                                                         } else {
218                                                                 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
219                                                                 fclose(f);
220                                                                 ast_destroy(tmp);
221                                                         }
222                                                 } else {
223                                                         ast_log(LOG_WARNING, "No = in line %d\n", lineno);
224                                                         fclose(f);
225                                                         ast_destroy(tmp);
226                                                 }
227                                                                                                                 
228                                         }
229                                 }
230                         }
231                 }
232                 fclose(f);              
233         } else {
234                 if (option_debug)
235                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
236                 else if (option_verbose > 1)
237                         ast_verbose( "Not found (%s)", strerror(errno));
238         }
239         return tmp;
240 }
241
242 char *ast_category_browse(struct ast_config *config, char *prev)
243 {       
244         struct ast_category *cat;
245         if (!prev) {
246                 if (config->root)
247                         return config->root->name;
248                 else
249                         return NULL;
250         }
251         cat = config->root;
252         while(cat) {
253                 if (!strcasecmp(cat->name, prev)) {
254                         if (cat->next)
255                                 return cat->next->name;
256                         else
257                                 return NULL;
258                 }
259                 cat = cat->next;
260         }
261         return NULL;
262 }