]> mj.ucw.cz Git - libucw.git/blob - lib/conf.c
significant simplifications of the interface
[libucw.git] / lib / conf.c
1 /*
2  *      UCW Library -- Reading of configuration files
3  *
4  *      (c) 2001 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2005 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/mempool.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 #ifndef DEFAULT_CONFIG
32 #define DEFAULT_CONFIG NULL
33 #endif
34
35 byte *cfdeffile = DEFAULT_CONFIG;
36
37 static void CONSTRUCTOR
38 conf_init(void)
39 {
40         cfpool = mp_new(4096);
41 }
42
43 void *
44 cfg_malloc(uns size)
45 {
46         return mp_alloc(cfpool, size);
47 }
48
49 void *
50 cfg_malloc_zero(uns size)
51 {
52         return mp_alloc_zero(cfpool, size);
53 }
54
55 byte *
56 cfg_strdup(byte *s)
57 {
58         return mp_strdup(cfpool, s);
59 }
60
61 byte *
62 cfg_printf(char *fmt, ...)
63 {
64         va_list args;
65         va_start(args, fmt);
66         byte *res = mp_vprintf(cfpool, fmt, args);
67         va_end(args);
68         return res;
69 }
70
71 void cf_register(struct cfitem *items)
72 {
73         if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION)
74                 die("cf_register: Invalid section type");
75         items[0].var=cfsection;
76         cfsection=items;
77 }
78
79 int cf_item_count(void)
80 {
81         struct cfitem *sect, *item;
82         int count = 0;
83         for (sect = cfsection; sect; sect = sect->var)
84                 for (item = sect+1; item->type; item++)
85                         count++;
86         return count;
87 }
88
89 struct cfitem *cf_get_item(byte *sect, byte *name)
90 {
91         struct cfitem *item, *section;
92
93         item=cfsection;
94         while(item && strcasecmp(item->name,sect))
95                 item=item->var;
96         if(!item)       /* unknown section */
97                 return NULL;
98         section = item;
99
100         for(item++; item->type && strcasecmp(item->name,name); item++);
101         if (!item->type && section->type == CT_INCOMPLETE_SECTION)
102                 return NULL;
103
104         return item;    /* item->type == 0 if not found */
105 }
106
107 struct unit {
108         uns name;                       /* One-letter name of the unit */
109         uns num, den;                   /* Fraction */
110 };
111
112 static const struct unit units[] = {
113         { 'd', 86400, 1 },
114         { 'h', 3600, 1 },
115         { 'k', 1000, 1 },
116         { 'm', 1000000, 1 },
117         { 'g', 1000000000, 1 },
118         { 'K', 1024, 1 },
119         { 'M', 1048576, 1 },
120         { 'G', 1073741824, 1 },
121         { '%', 1, 100 },
122         { 0, 0, 0 }
123 };
124
125 static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg)
126 {
127         if (end && *end) {
128                 if (end == value || end[1] || *end >= '0' && *end <= '9')
129                         *msg = "Invalid number";
130                 else {
131                         for (const struct unit *u=units; u->name; u++)
132                                 if (u->name == *end)
133                                         return u;
134                         *msg = "Invalid unit";
135                 }
136         }
137         return NULL;
138 }
139
140 static char cf_rngerr[] = "Number out of range";
141
142 byte *cf_parse_int(byte *value, uns *varp)
143 {
144         char *msg = NULL;
145         const struct unit *u;
146
147         if (!*value)
148                 msg = "Missing number";
149         else {
150                 errno = 0;
151                 char *end;
152                 uns x = strtoul(value, &end, 0);
153                 if (errno == ERANGE)
154                         msg = cf_rngerr;
155                 else if (u = cf_lookup_unit(value, end, &msg)) {
156                         u64 y = (u64)x * u->num;
157                         if (y % u->den)
158                                 msg = "Number is not an integer";
159                         else {
160                                 y /= u->den;
161                                 if (y > 0xffffffff)
162                                         msg = cf_rngerr;
163                                 *varp = y;
164                         }
165                 } else
166                         *varp = x;
167         }
168         return msg;
169 }
170
171 byte *cf_parse_u64(byte *value, u64 *varp)
172 {
173         char *msg = NULL;
174         const struct unit *u;
175
176         if (!*value)
177                 msg = "Missing number";
178         else {
179                 errno = 0;
180                 char *end;
181                 u64 x = strtoull(value, &end, 0);
182                 if (errno == ERANGE)
183                         msg = cf_rngerr;
184                 else if (u = cf_lookup_unit(value, end, &msg)) {
185                         if (x > ~(u64)0 / u->num)
186                                 msg = "Number out of range";
187                         else {
188                                 x *= u->num;
189                                 if (x % u->den)
190                                         msg = "Number is not an integer";
191                                 else
192                                         *varp = x / u->den;
193                         }
194                 } else
195                         *varp = x;
196         }
197         return msg;
198 }
199
200 byte *cf_parse_double(byte *value, double *varp)
201 {
202         char *msg = NULL;
203         const struct unit *u;
204
205         if (!*value)
206                 msg = "Missing number";
207         else {
208                 errno = 0;
209                 char *end;
210                 double x = strtoul(value, &end, 0);
211                 if (errno == ERANGE)
212                         msg = cf_rngerr;
213                 else if (u = cf_lookup_unit(value, end, &msg))
214                         *varp = x * u->num / u->den;
215                 else
216                         *varp = x;
217         }
218         return msg;
219 }
220
221 byte *
222 cf_parse_ip(byte **p, u32 *varp)
223 {
224   while (Cspace(**p))
225     (*p)++;
226   if (!**p)
227     return "Missing IP address";
228   uns x = 0;
229   if (**p == '0' && *(*p + 1) | 32 == 'X')
230     {
231       errno = 0;
232       x = strtoul(*p + 2, (char **)p, 16);
233       if (errno == ERANGE || x > 0xffffffff)
234         goto error;
235     }
236   else
237     for (uns i = 0; i < 4; i++)
238       {
239         if (i)
240           {
241             while (Cspace(**p))
242               (*p)++;
243             if (*(*p)++ != '.')
244               goto error;
245           }
246         while (Cspace(**p))
247           (*p)++;
248         errno = 0;
249         uns y = strtoul(*p, (char **)p, 10);
250         if (errno == ERANGE || y > 255)
251           goto error;
252         x = (x << 8) + y;
253       }
254   *varp = x;
255   return NULL;
256 error:
257   return "Invalid IP address";
258 }
259
260 byte *cf_set_item(byte *sect, byte *name, byte *value)
261 {
262         struct cfitem *item;
263         byte *msg=NULL;
264
265         if (!*sect)
266                 return "Empty section name";
267         item=cf_get_item(sect,name);
268         if(!item)       /* ignore unknown section */
269                 return NULL;
270
271         switch(item->type){
272                 case CT_INT:
273                         msg = cf_parse_int(value, (uns *) item->var);
274                         break;
275                 case CT_STRING:
276                         *((byte **) item->var) = cfg_strdup(value);
277                         break;
278                 case CT_FUNCTION:
279                         msg = ((ci_func) item->var)(item, cfg_strdup(value));
280                         break;
281                 case CT_DOUBLE:
282                         msg = cf_parse_double(value, (double *) item->var);
283                         break;
284                 case CT_U64:
285                         msg = cf_parse_u64(value, (u64 *) item->var);
286                         break;
287                 default:
288                         msg = "Unknown keyword";
289         }
290
291         return msg;
292 }
293
294 static int cf_subread(byte *filename,int level)
295 {
296         int fd;
297         struct fastbuf *b;
298         byte def_section[BUFFER];
299         int line;
300         byte *msg=NULL;
301
302         if(level>=MAX_LEVEL){
303                 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
304                 return 0;
305         }
306                 
307         fd=open(filename,O_RDONLY, 0666);
308         if(fd<0){
309                 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
310                 return 0;
311         }
312         b=bfdopen(fd,4096);
313
314         def_section[0]=0;
315         line=0;
316         while(1){
317                 byte buf[BUFFER];
318                 byte *c;
319
320                 if(!bgets(b,buf,BUFFER))
321                         break;
322                 line++;
323
324                 c=buf+strlen(buf);
325                 while(c>buf && Cspace(c[-1]))
326                         *--c=0;
327                 c=buf;
328                 while(*c && Cspace(*c))
329                         c++;
330                 if(!*c || *c=='#')
331                         continue;
332
333                 if(*c=='['){
334                         strcpy(def_section,c+1);
335                         c=strchr(def_section,']');
336                         if(c){
337                                 *c=0;
338                                 if(c[1]){
339                                         msg="Garbage after ]";
340                                         break;
341                                 }
342                         }else{
343                                 msg="Missing ]";
344                                 break;
345                         }
346
347                 }else{
348                         byte *sect,*name,*value;
349
350                         name=c;
351                         while(*c && !Cspace(*c))
352                                 c++;
353                         while(*c && Cspace(*c))
354                                 *c++=0;
355                         value=c;
356
357                         if(!strcasecmp(name,"include")){
358                                 if(!cf_subread(value,level+1)){
359                                         msg="Included from here";
360                                         break;
361                                 }
362                         }else{
363                                 c=strchr(name,'.');
364                                 if(!c)
365                                         sect=def_section;
366                                 else{
367                                         sect=name;
368                                         *c++=0;
369                                         name=c;
370                                 }
371
372                                 msg=cf_set_item(sect,name,value);
373                         }
374                         if(msg)
375                                 break;
376                 }
377
378         }       /* for every line */
379
380         if(msg)
381                 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
382         bclose(b);
383         return !msg;
384 }
385
386 void cf_read(byte *filename)
387 {
388         if(!cf_subread(filename,0))
389                 die("Reading config file %s failed",filename);
390         cfdeffile = NULL;
391 }
392
393 static void cf_opt_S(void)
394 {
395         byte *sect,*name,*value;
396         byte *c;
397         byte *msg=NULL;
398         byte arg[strlen(optarg)+1];
399
400         strcpy(arg, optarg);
401         name = arg;
402         c=strchr(name,'=');
403         if(!c){
404                 msg="Missing argument";
405                 sect=value="";
406         }else{
407                 *c++=0;
408                 value=c;
409
410                 c=strchr(name,'.');
411                 if(!c)
412                         sect="";
413                 else{
414                         sect=name;
415                         *c++=0;
416                         name=c;
417                 }
418
419                 if (cfdeffile)
420                         cf_read(cfdeffile);
421                 msg=cf_set_item(sect,name,value);
422         }
423         if(msg)
424                 die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg);
425
426 }
427
428 int cf_getopt(int argc,char * const argv[],
429                 const char *shortopts,const struct option *longopts,
430                 int *longindex)
431 {
432         int res;
433         static int other_options;
434
435         do{
436                 res=getopt_long(argc,argv,shortopts,longopts,longindex);
437                 if(res == 'S' || res == 'C') {
438                         if(other_options)
439                                 die("The -S and -C options must precede all other arguments");
440                 }
441                 if(res=='S'){
442                         cf_opt_S();
443                 }else if(res=='C'){
444                         cf_read(optarg);
445                 }else{
446                         /* unhandled option or end of options */
447                         if(cfdeffile)
448                                 cf_read(cfdeffile);
449                         other_options++;
450                         return res;
451                 }
452         }while(1);
453 }