]> mj.ucw.cz Git - libucw.git/blob - lib/conf.c
Two improvements to the configuration language:
[libucw.git] / lib / conf.c
1 /*
2  *      Sherlock Library -- Reading of configuration files
3  *
4  *      (c) 2001 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "lib/lib.h"
12 #include "lib/chartype.h"
13 #include "lib/fastbuf.h"
14 #include "lib/pools.h"
15
16 #include "lib/conf.h"
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <errno.h>
24
25 #define BUFFER          1024
26 #define MAX_LEVEL       8
27
28 static struct cfitem *cfsection;
29 struct mempool *cfpool;
30
31 byte *cfdeffile = DEFAULT_CONFIG;
32
33 static void CONSTRUCTOR
34 conf_init(void)
35 {
36         cfpool = mp_new(4096);
37 }
38
39 void *
40 cfg_malloc(uns size)
41 {
42         return mp_alloc(cfpool, size);
43 }
44
45 byte *
46 cfg_stralloc(byte *s)
47 {
48         uns l = strlen(s);
49         byte *k = cfg_malloc(l + 1);
50         strcpy(k, s);
51         return k;
52 }
53
54 void cf_register(struct cfitem *items)
55 {
56         if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION)
57                 die("cf_register: Invalid section type");
58         items[0].var=cfsection;
59         cfsection=items;
60 }
61
62 int cf_item_count(void)
63 {
64         struct cfitem *sect, *item;
65         int count = 0;
66         for (sect = cfsection; sect; sect = sect->var)
67                 for (item = sect+1; item->type; item++)
68                         count++;
69         return count;
70 }
71
72 struct cfitem *cf_get_item(byte *sect, byte *name)
73 {
74         struct cfitem *item, *section;
75
76         item=cfsection;
77         while(item && strcasecmp(item->name,sect))
78                 item=item->var;
79         if(!item)       /* unknown section */
80                 return NULL;
81         section = item;
82
83         for(item++; item->type && strcasecmp(item->name,name); item++);
84         if (!item->type && section->type == CT_INCOMPLETE_SECTION)
85                 return NULL;
86
87         return item;    /* item->type == 0 if not found */
88 }
89
90 struct unit {
91         uns name;                       /* One-letter name of the unit */
92         uns num, den;                   /* Fraction */
93 };
94
95 static const struct unit units[] = {
96         { 'k', 1000, 1 },
97         { 'm', 1000000, 1 },
98         { 'g', 1000000000, 1 },
99         { 'K', 1024, 1 },
100         { 'M', 1048576, 1 },
101         { 'G', 1073741824, 1 },
102         { '%', 1, 100 },
103         { 0, 0, 0 }
104 };
105
106 static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg)
107 {
108         if (end && *end) {
109                 if (end == value || end[1] || *end >= '0' && *end <= '9')
110                         *msg = "Invalid number";
111                 else {
112                         for (const struct unit *u=units; u->name; u++)
113                                 if (u->name == *end)
114                                         return u;
115                         *msg = "Invalid unit";
116                 }
117         }
118         return NULL;
119 }
120
121 static char cf_rngerr[] = "Number out of range";
122
123 byte *cf_parse_int(byte *value, uns *varp)
124 {
125         char *msg = NULL;
126         const struct unit *u;
127
128         if (!*value)
129                 msg = "Missing number";
130         else {
131                 errno = 0;
132                 char *end;
133                 uns x = strtoul(value, &end, 0);
134                 if (errno == ERANGE)
135                         msg = cf_rngerr;
136                 else if (u = cf_lookup_unit(value, end, &msg)) {
137                         u64 y = (u64)x * u->num;
138                         if (y % u->den)
139                                 msg = "Number is not an integer";
140                         else {
141                                 y /= u->den;
142                                 if (y > 0xffffffff)
143                                         msg = cf_rngerr;
144                                 *varp = y;
145                         }
146                 } else
147                         *varp = x;
148         }
149         return msg;
150 }
151
152 byte *cf_parse_double(byte *value, double *varp)
153 {
154         char *msg = NULL;
155         const struct unit *u;
156
157         if (!*value)
158                 msg = "Missing number";
159         else {
160                 errno = 0;
161                 char *end;
162                 double x = strtoul(value, &end, 0);
163                 if (errno == ERANGE)
164                         msg = cf_rngerr;
165                 else if (u = cf_lookup_unit(value, end, &msg))
166                         *varp = x * u->num / u->den;
167                 else
168                         *varp = x;
169         }
170         return msg;
171 }
172
173 byte *cf_set_item(byte *sect, byte *name, byte *value)
174 {
175         struct cfitem *item;
176         byte *msg=NULL;
177
178         if (!*sect)
179                 return "Empty section name";
180         item=cf_get_item(sect,name);
181         if(!item)       /* ignore unknown section */
182                 return NULL;
183
184         switch(item->type){
185                 case CT_INT:
186                         msg = cf_parse_int(value, (uns *) item->var);
187                         break;
188                 case CT_STRING:
189                         *((byte **) item->var) = cfg_stralloc(value);
190                         break;
191                 case CT_FUNCTION:
192                         msg = ((ci_func) item->var)(item, cfg_stralloc(value));
193                         break;
194                 case CT_DOUBLE:
195                         msg = cf_parse_double(value, (double *) item->var);
196                         break;
197                 default:
198                         msg = "Unknown keyword";
199         }
200
201         return msg;
202 }
203
204 static int cf_subread(byte *filename,int level)
205 {
206         int fd;
207         struct fastbuf *b;
208         byte def_section[BUFFER];
209         int line;
210         byte *msg=NULL;
211
212         if(level>=MAX_LEVEL){
213                 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
214                 return 0;
215         }
216                 
217         fd=open(filename,O_RDONLY, 0666);
218         if(fd<0){
219                 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
220                 return 0;
221         }
222         b=bfdopen(fd,4096);
223
224         def_section[0]=0;
225         line=0;
226         while(1){
227                 byte buf[BUFFER];
228                 byte *c;
229
230                 if(!bgets(b,buf,BUFFER))
231                         break;
232                 line++;
233
234                 c=buf+strlen(buf);
235                 while(c>buf && Cspace(c[-1]))
236                         *--c=0;
237                 c=buf;
238                 while(*c && Cspace(*c))
239                         c++;
240                 if(!*c || *c=='#')
241                         continue;
242
243                 if(*c=='['){
244                         strcpy(def_section,c+1);
245                         c=strchr(def_section,']');
246                         if(c){
247                                 *c=0;
248                                 if(c[1]){
249                                         msg="Garbage after ]";
250                                         break;
251                                 }
252                         }else{
253                                 msg="Missing ]";
254                                 break;
255                         }
256
257                 }else{
258                         byte *sect,*name,*value;
259
260                         name=c;
261                         while(*c && !Cspace(*c))
262                                 c++;
263                         while(*c && Cspace(*c))
264                                 *c++=0;
265                         value=c;
266
267                         if(!strcasecmp(name,"include")){
268                                 if(!cf_subread(value,level+1)){
269                                         msg="Included from here";
270                                         break;
271                                 }
272                         }else{
273                                 c=strchr(name,'.');
274                                 if(!c)
275                                         sect=def_section;
276                                 else{
277                                         sect=name;
278                                         *c++=0;
279                                         name=c;
280                                 }
281
282                                 msg=cf_set_item(sect,name,value);
283                         }
284                         if(msg)
285                                 break;
286                 }
287
288         }       /* for every line */
289
290         if(msg)
291                 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
292         bclose(b);
293         return !msg;
294 }
295
296 void cf_read(byte *filename)
297 {
298         if(!cf_subread(filename,0))
299                 die("Reading config file %s failed",filename);
300         cfdeffile = NULL;
301 }
302
303 int cf_getopt(int argc,char * const argv[],
304                 const char *shortopts,const struct option *longopts,
305                 int *longindex)
306 {
307         int res;
308
309         do{
310                 res=getopt_long(argc,argv,shortopts,longopts,longindex);
311                 if(res=='S'){
312                         byte *sect,*name,*value;
313                         byte *c;
314                         byte *msg=NULL;
315
316                         name=optarg;
317                         c=strchr(name,'=');
318                         if(!c){
319                                 msg="Missing argument";
320                                 sect=value="";
321                         }else{
322                                 *c++=0;
323                                 value=c;
324
325                                 c=strchr(name,'.');
326                                 if(!c)
327                                         sect="";
328                                 else{
329                                         sect=name;
330                                         *c++=0;
331                                         name=c;
332                                 }
333
334                                 if (cfdeffile)
335                                         cf_read(cfdeffile);
336                                 msg=cf_set_item(sect,name,value);
337                         }
338                         if(msg)
339                                 die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
340
341                 }else if(res=='C'){
342                         cf_read(optarg);
343                 }else{
344                         /* unhandled option or end of options */
345                         if(cfdeffile)
346                                 cf_read(cfdeffile);
347                         return res;
348                 }
349         }while(1);
350 }