]> mj.ucw.cz Git - libucw.git/blob - lib/conf.c
When pre-sorting a regular file, use lib/arraysort.h on an array of items
[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_u64(byte *value, u64 *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                 u64 x = strtoull(value, &end, 0);
163                 if (errno == ERANGE)
164                         msg = cf_rngerr;
165                 else if (u = cf_lookup_unit(value, end, &msg)) {
166                         if (x > ~(u64)0 / u->num)
167                                 msg = "Number out of range";
168                         else {
169                                 x *= u->num;
170                                 if (x % u->den)
171                                         msg = "Number is not an integer";
172                                 else
173                                         *varp = x / u->den;
174                         }
175                 } else
176                         *varp = x;
177         }
178         return msg;
179 }
180
181 byte *cf_parse_double(byte *value, double *varp)
182 {
183         char *msg = NULL;
184         const struct unit *u;
185
186         if (!*value)
187                 msg = "Missing number";
188         else {
189                 errno = 0;
190                 char *end;
191                 double x = strtoul(value, &end, 0);
192                 if (errno == ERANGE)
193                         msg = cf_rngerr;
194                 else if (u = cf_lookup_unit(value, end, &msg))
195                         *varp = x * u->num / u->den;
196                 else
197                         *varp = x;
198         }
199         return msg;
200 }
201
202 byte *cf_set_item(byte *sect, byte *name, byte *value)
203 {
204         struct cfitem *item;
205         byte *msg=NULL;
206
207         if (!*sect)
208                 return "Empty section name";
209         item=cf_get_item(sect,name);
210         if(!item)       /* ignore unknown section */
211                 return NULL;
212
213         switch(item->type){
214                 case CT_INT:
215                         msg = cf_parse_int(value, (uns *) item->var);
216                         break;
217                 case CT_STRING:
218                         *((byte **) item->var) = cfg_stralloc(value);
219                         break;
220                 case CT_FUNCTION:
221                         msg = ((ci_func) item->var)(item, cfg_stralloc(value));
222                         break;
223                 case CT_DOUBLE:
224                         msg = cf_parse_double(value, (double *) item->var);
225                         break;
226                 case CT_U64:
227                         msg = cf_parse_u64(value, (u64 *) item->var);
228                         break;
229                 default:
230                         msg = "Unknown keyword";
231         }
232
233         return msg;
234 }
235
236 static int cf_subread(byte *filename,int level)
237 {
238         int fd;
239         struct fastbuf *b;
240         byte def_section[BUFFER];
241         int line;
242         byte *msg=NULL;
243
244         if(level>=MAX_LEVEL){
245                 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
246                 return 0;
247         }
248                 
249         fd=open(filename,O_RDONLY, 0666);
250         if(fd<0){
251                 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
252                 return 0;
253         }
254         b=bfdopen(fd,4096);
255
256         def_section[0]=0;
257         line=0;
258         while(1){
259                 byte buf[BUFFER];
260                 byte *c;
261
262                 if(!bgets(b,buf,BUFFER))
263                         break;
264                 line++;
265
266                 c=buf+strlen(buf);
267                 while(c>buf && Cspace(c[-1]))
268                         *--c=0;
269                 c=buf;
270                 while(*c && Cspace(*c))
271                         c++;
272                 if(!*c || *c=='#')
273                         continue;
274
275                 if(*c=='['){
276                         strcpy(def_section,c+1);
277                         c=strchr(def_section,']');
278                         if(c){
279                                 *c=0;
280                                 if(c[1]){
281                                         msg="Garbage after ]";
282                                         break;
283                                 }
284                         }else{
285                                 msg="Missing ]";
286                                 break;
287                         }
288
289                 }else{
290                         byte *sect,*name,*value;
291
292                         name=c;
293                         while(*c && !Cspace(*c))
294                                 c++;
295                         while(*c && Cspace(*c))
296                                 *c++=0;
297                         value=c;
298
299                         if(!strcasecmp(name,"include")){
300                                 if(!cf_subread(value,level+1)){
301                                         msg="Included from here";
302                                         break;
303                                 }
304                         }else{
305                                 c=strchr(name,'.');
306                                 if(!c)
307                                         sect=def_section;
308                                 else{
309                                         sect=name;
310                                         *c++=0;
311                                         name=c;
312                                 }
313
314                                 msg=cf_set_item(sect,name,value);
315                         }
316                         if(msg)
317                                 break;
318                 }
319
320         }       /* for every line */
321
322         if(msg)
323                 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
324         bclose(b);
325         return !msg;
326 }
327
328 void cf_read(byte *filename)
329 {
330         if(!cf_subread(filename,0))
331                 die("Reading config file %s failed",filename);
332         cfdeffile = NULL;
333 }
334
335 int cf_getopt(int argc,char * const argv[],
336                 const char *shortopts,const struct option *longopts,
337                 int *longindex)
338 {
339         int res;
340
341         do{
342                 res=getopt_long(argc,argv,shortopts,longopts,longindex);
343                 if(res=='S'){
344                         byte *sect,*name,*value;
345                         byte *c;
346                         byte *msg=NULL;
347
348                         name=optarg;
349                         c=strchr(name,'=');
350                         if(!c){
351                                 msg="Missing argument";
352                                 sect=value="";
353                         }else{
354                                 *c++=0;
355                                 value=c;
356
357                                 c=strchr(name,'.');
358                                 if(!c)
359                                         sect="";
360                                 else{
361                                         sect=name;
362                                         *c++=0;
363                                         name=c;
364                                 }
365
366                                 if (cfdeffile)
367                                         cf_read(cfdeffile);
368                                 msg=cf_set_item(sect,name,value);
369                         }
370                         if(msg)
371                                 die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
372
373                 }else if(res=='C'){
374                         cf_read(optarg);
375                 }else{
376                         /* unhandled option or end of options */
377                         if(cfdeffile)
378                                 cf_read(cfdeffile);
379                         return res;
380                 }
381         }while(1);
382 }