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