]> mj.ucw.cz Git - libucw.git/blob - lib/conf.c
avoid compiler warnings with debugging turned off
[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 *cf_set_item(byte *sect, byte *name, byte *value)
222 {
223         struct cfitem *item;
224         byte *msg=NULL;
225
226         if (!*sect)
227                 return "Empty section name";
228         item=cf_get_item(sect,name);
229         if(!item)       /* ignore unknown section */
230                 return NULL;
231
232         switch(item->type){
233                 case CT_INT:
234                         msg = cf_parse_int(value, (uns *) item->var);
235                         break;
236                 case CT_STRING:
237                         *((byte **) item->var) = cfg_strdup(value);
238                         break;
239                 case CT_FUNCTION:
240                         msg = ((ci_func) item->var)(item, cfg_strdup(value));
241                         break;
242                 case CT_DOUBLE:
243                         msg = cf_parse_double(value, (double *) item->var);
244                         break;
245                 case CT_U64:
246                         msg = cf_parse_u64(value, (u64 *) item->var);
247                         break;
248                 default:
249                         msg = "Unknown keyword";
250         }
251
252         return msg;
253 }
254
255 static int cf_subread(byte *filename,int level)
256 {
257         int fd;
258         struct fastbuf *b;
259         byte def_section[BUFFER];
260         int line;
261         byte *msg=NULL;
262
263         if(level>=MAX_LEVEL){
264                 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
265                 return 0;
266         }
267                 
268         fd=open(filename,O_RDONLY, 0666);
269         if(fd<0){
270                 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
271                 return 0;
272         }
273         b=bfdopen(fd,4096);
274
275         def_section[0]=0;
276         line=0;
277         while(1){
278                 byte buf[BUFFER];
279                 byte *c;
280
281                 if(!bgets(b,buf,BUFFER))
282                         break;
283                 line++;
284
285                 c=buf+strlen(buf);
286                 while(c>buf && Cspace(c[-1]))
287                         *--c=0;
288                 c=buf;
289                 while(*c && Cspace(*c))
290                         c++;
291                 if(!*c || *c=='#')
292                         continue;
293
294                 if(*c=='['){
295                         strcpy(def_section,c+1);
296                         c=strchr(def_section,']');
297                         if(c){
298                                 *c=0;
299                                 if(c[1]){
300                                         msg="Garbage after ]";
301                                         break;
302                                 }
303                         }else{
304                                 msg="Missing ]";
305                                 break;
306                         }
307
308                 }else{
309                         byte *sect,*name,*value;
310
311                         name=c;
312                         while(*c && !Cspace(*c))
313                                 c++;
314                         while(*c && Cspace(*c))
315                                 *c++=0;
316                         value=c;
317
318                         if(!strcasecmp(name,"include")){
319                                 if(!cf_subread(value,level+1)){
320                                         msg="Included from here";
321                                         break;
322                                 }
323                         }else{
324                                 c=strchr(name,'.');
325                                 if(!c)
326                                         sect=def_section;
327                                 else{
328                                         sect=name;
329                                         *c++=0;
330                                         name=c;
331                                 }
332
333                                 msg=cf_set_item(sect,name,value);
334                         }
335                         if(msg)
336                                 break;
337                 }
338
339         }       /* for every line */
340
341         if(msg)
342                 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
343         bclose(b);
344         return !msg;
345 }
346
347 void cf_read(byte *filename)
348 {
349         if(!cf_subread(filename,0))
350                 die("Reading config file %s failed",filename);
351         cfdeffile = NULL;
352 }
353
354 static void cf_opt_S(void)
355 {
356         byte *sect,*name,*value;
357         byte *c;
358         byte *msg=NULL;
359         byte arg[strlen(optarg)+1];
360
361         strcpy(arg, optarg);
362         name = arg;
363         c=strchr(name,'=');
364         if(!c){
365                 msg="Missing argument";
366                 sect=value="";
367         }else{
368                 *c++=0;
369                 value=c;
370
371                 c=strchr(name,'.');
372                 if(!c)
373                         sect="";
374                 else{
375                         sect=name;
376                         *c++=0;
377                         name=c;
378                 }
379
380                 if (cfdeffile)
381                         cf_read(cfdeffile);
382                 msg=cf_set_item(sect,name,value);
383         }
384         if(msg)
385                 die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg);
386
387 }
388
389 int cf_getopt(int argc,char * const argv[],
390                 const char *shortopts,const struct option *longopts,
391                 int *longindex)
392 {
393         int res;
394         static int other_options;
395
396         do{
397                 res=getopt_long(argc,argv,shortopts,longopts,longindex);
398                 if(res == 'S' || res == 'C') {
399                         if(other_options)
400                                 die("The -S and -C options must precede all other arguments");
401                 }
402                 if(res=='S'){
403                         cf_opt_S();
404                 }else if(res=='C'){
405                         cf_read(optarg);
406                 }else{
407                         /* unhandled option or end of options */
408                         if(cfdeffile)
409                                 cf_read(cfdeffile);
410                         other_options++;
411                         return res;
412                 }
413         }while(1);
414 }