Version 0.1.3 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         cat = ast->root;
55         while(cat) {
56                 v = cat->root;
57                 while(v) {
58                         vn = v;
59                         free(v->name);
60                         free(v->value);
61                         v = v->next;
62                         free(vn);
63                 }
64                 catn = cat;
65                 cat = cat->next;
66                 free(catn);
67         }
68         free(ast);
69 }
70
71 int ast_true(char *s)
72 {
73         /* Determine if this is a true value */
74         if (!strcasecmp(s, "yes") ||
75             !strcasecmp(s, "true") ||
76                 !strcasecmp(s, "y") ||
77                 !strcasecmp(s, "t") ||
78                 !strcasecmp(s, "1"))
79                         return -1;
80         return 0;
81 }
82
83 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
84 {
85         struct ast_category *cat;
86         cat = config->root;
87         while(cat) {
88                 if (!strcasecmp(cat->name, category))
89                         return cat->root;
90                 cat = cat->next;
91         }
92         return NULL;
93 }
94
95 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
96 {
97         struct ast_variable *v;
98         if (category) {
99                 v = ast_variable_browse(config, category);
100                 while (v) {
101                         if (!strcasecmp(value, v->name))
102                                 return v->value;
103                         v=v->next;
104                 }
105         } else {
106                 struct ast_category *cat;
107                 cat = config->root;
108                 while(cat) {
109                         v = cat->root;
110                         while (v) {
111                                 if (!strcasecmp(value, v->name))
112                                         return v->value;
113                                 v=v->next;
114                         }
115                         cat = cat->next;
116                 }
117         }
118         return NULL;
119 }
120
121 struct ast_config *ast_load(char *configfile)
122 {
123         char fn[256];
124         char buf[256];
125         struct ast_config *tmp=NULL;
126         struct ast_category *tmpc=NULL;
127         struct ast_variable *v, *last=NULL;
128         FILE *f;
129         char *c, *cur;
130         int lineno=0;
131         if (configfile[0] == '/') {
132                 strncpy(fn, configfile, sizeof(fn));
133         } else {
134                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
135         }
136         if ((option_verbose > 1) && !option_debug) {
137                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
138                 fflush(stdout);
139         }
140         if ((f = fopen(fn, "r"))) {
141                 if (option_debug)
142                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
143                 else if (option_verbose > 1)
144                         ast_verbose( "Found\n");
145                 tmp = malloc(sizeof(struct ast_config));
146                 if (!tmp) {
147                         ast_log(LOG_WARNING, "Out of memory\n");
148                         fclose(f);
149                         return NULL;
150                 }
151                 tmp->root = NULL;
152                 while(!feof(f)) {
153                         fgets(buf, sizeof(buf), f);
154                         lineno++;
155                         if (!feof(f)) {
156                                 /* Strip off lines using ; as comment */
157                                 c = strchr(buf, ';');
158                                 if (c)
159                                         *c = '\0';
160                                 cur = strip(buf);
161                                 if (strlen(cur)) {
162                                         /* Actually parse the entry */
163                                         if (cur[0] == '[') {
164                                                 /* A category header */
165                                                 /* XXX Don't let them use the same category twice XXX */
166                                                 c = strchr(cur, ']');
167                                                 if (c) {
168                                                         *c = 0;
169                                                         tmpc = malloc(sizeof(struct ast_category));
170                                                         if (!tmpc) {
171                                                                 ast_destroy(tmp);
172                                                                 ast_log(LOG_WARNING,
173                                                                         "Out of memory, line %d\n", lineno);
174                                                                 fclose(f);
175                                                                 return NULL;
176                                                         }
177                                                         strncpy(tmpc->name, cur+1, sizeof(tmpc->name));
178                                                         tmpc->root =  NULL;
179                                                         tmpc->next = tmp->root;
180                                                         tmp->root = tmpc;
181                                                         last =  NULL;
182                                                 } else {
183                                                         ast_log(LOG_WARNING, 
184                                                                 "parse error: no closing ']', line %d\n", lineno);
185                                                         ast_destroy(tmp);
186                                                         fclose(f);
187                                                         return NULL;
188                                                 }
189                                         } else {
190                                                 /* Just a line (variable = value) */
191                                                 if (!tmpc) {
192                                                         ast_log(LOG_WARNING,
193                                                                 "parse error: No category context for line %d\n", lineno);
194                                                         ast_destroy(tmp);
195                                                         fclose(f);
196                                                         return NULL;
197                                                 }
198                                                 c = strchr(cur, '=');
199                                                 if (c) {
200                                                         *c = 0;
201                                                         c++;
202                                                         v = malloc(sizeof(struct ast_variable));
203                                                         if (v) {
204                                                                 v->next = NULL;
205                                                                 v->name = strdup(strip(cur));
206                                                                 v->value = strdup(strip(c));
207                                                                 if (last)  
208                                                                         last->next = v;
209                                                                 else
210                                                                         tmpc->root = v;
211                                                                 last = v;
212                                                         } else {
213                                                                 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
214                                                                 fclose(f);
215                                                                 ast_destroy(tmp);
216                                                         }
217                                                 } else {
218                                                         ast_log(LOG_WARNING, "No = in line %d\n", lineno);
219                                                         fclose(f);
220                                                         ast_destroy(tmp);
221                                                 }
222                                                                                                                 
223                                         }
224                                 }
225                         }
226                 }
227                 fclose(f);              
228         } else {
229                 if (option_debug)
230                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
231                 else if (option_verbose > 1)
232                         ast_verbose( "Not found (%s)", strerror(errno));
233         }
234         return tmp;
235 }
236
237 char *ast_category_browse(struct ast_config *config, char *prev)
238 {       
239         struct ast_category *cat;
240         if (!prev) {
241                 if (config->root)
242                         return config->root->name;
243                 else
244                         return NULL;
245         }
246         cat = config->root;
247         while(cat) {
248                 if (!strcasecmp(cat->name, prev)) {
249                         if (cat->next)
250                                 return cat->next->name;
251                         else
252                                 return NULL;
253                 }
254                 cat = cat->next;
255         }
256         return NULL;
257 }