2 * UCW Library -- Reading of configuration files
4 * (c) 2001 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2005 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "lib/chartype.h"
13 #include "lib/fastbuf.h"
14 #include "lib/mempool.h"
28 static struct cfitem *cfsection;
29 struct mempool *cfpool;
31 #ifndef DEFAULT_CONFIG
32 #define DEFAULT_CONFIG NULL
35 byte *cfdeffile = DEFAULT_CONFIG;
37 static void CONSTRUCTOR
40 cfpool = mp_new(4096);
46 return mp_alloc(cfpool, size);
50 cfg_malloc_zero(uns size)
52 return mp_alloc_zero(cfpool, size);
58 return mp_strdup(cfpool, s);
62 cfg_printf(char *fmt, ...)
66 byte *res = mp_vprintf(cfpool, fmt, args);
71 void cf_register(struct cfitem *items)
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;
79 int cf_item_count(void)
81 struct cfitem *sect, *item;
83 for (sect = cfsection; sect; sect = sect->var)
84 for (item = sect+1; item->type; item++)
89 struct cfitem *cf_get_item(byte *sect, byte *name)
91 struct cfitem *item, *section;
94 while(item && strcasecmp(item->name,sect))
96 if(!item) /* unknown section */
100 for(item++; item->type && strcasecmp(item->name,name); item++);
101 if (!item->type && section->type == CT_INCOMPLETE_SECTION)
104 return item; /* item->type == 0 if not found */
108 uns name; /* One-letter name of the unit */
109 uns num, den; /* Fraction */
112 static const struct unit units[] = {
117 { 'g', 1000000000, 1 },
120 { 'G', 1073741824, 1 },
125 static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg)
128 if (end == value || end[1] || *end >= '0' && *end <= '9')
129 *msg = "Invalid number";
131 for (const struct unit *u=units; u->name; u++)
134 *msg = "Invalid unit";
140 static char cf_rngerr[] = "Number out of range";
142 byte *cf_parse_int(byte *value, uns *varp)
145 const struct unit *u;
148 msg = "Missing number";
152 uns x = strtoul(value, &end, 0);
155 else if (u = cf_lookup_unit(value, end, &msg)) {
156 u64 y = (u64)x * u->num;
158 msg = "Number is not an integer";
171 byte *cf_parse_u64(byte *value, u64 *varp)
174 const struct unit *u;
177 msg = "Missing number";
181 u64 x = strtoull(value, &end, 0);
184 else if (u = cf_lookup_unit(value, end, &msg)) {
185 if (x > ~(u64)0 / u->num)
186 msg = "Number out of range";
190 msg = "Number is not an integer";
200 byte *cf_parse_double(byte *value, double *varp)
203 const struct unit *u;
206 msg = "Missing number";
210 double x = strtoul(value, &end, 0);
213 else if (u = cf_lookup_unit(value, end, &msg))
214 *varp = x * u->num / u->den;
222 cf_parse_ip(byte **p, u32 *varp)
227 return "Missing IP address";
229 if (**p == '0' && *(*p + 1) | 32 == 'X')
232 x = strtoul(*p + 2, (char **)p, 16);
233 if (errno == ERANGE || x > 0xffffffff)
237 for (uns i = 0; i < 4; i++)
249 uns y = strtoul(*p, (char **)p, 10);
250 if (errno == ERANGE || y > 255)
257 return "Invalid IP address";
260 byte *cf_set_item(byte *sect, byte *name, byte *value)
266 return "Empty section name";
267 item=cf_get_item(sect,name);
268 if(!item) /* ignore unknown section */
273 msg = cf_parse_int(value, (uns *) item->var);
276 *((byte **) item->var) = cfg_strdup(value);
279 msg = ((ci_func) item->var)(item, cfg_strdup(value));
282 msg = cf_parse_double(value, (double *) item->var);
285 msg = cf_parse_u64(value, (u64 *) item->var);
288 msg = "Unknown keyword";
294 static int cf_subread(byte *filename,int level)
298 byte def_section[BUFFER];
302 if(level>=MAX_LEVEL){
303 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
307 fd=open(filename,O_RDONLY, 0666);
309 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
320 if(!bgets(b,buf,BUFFER))
325 while(c>buf && Cspace(c[-1]))
328 while(*c && Cspace(*c))
334 strcpy(def_section,c+1);
335 c=strchr(def_section,']');
339 msg="Garbage after ]";
348 byte *sect,*name,*value;
351 while(*c && !Cspace(*c))
353 while(*c && Cspace(*c))
357 if(!strcasecmp(name,"include")){
358 if(!cf_subread(value,level+1)){
359 msg="Included from here";
372 msg=cf_set_item(sect,name,value);
378 } /* for every line */
381 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
386 void cf_read(byte *filename)
388 if(!cf_subread(filename,0))
389 die("Reading config file %s failed",filename);
393 static void cf_opt_S(void)
395 byte *sect,*name,*value;
398 byte arg[strlen(optarg)+1];
404 msg="Missing argument";
421 msg=cf_set_item(sect,name,value);
424 die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg);
428 int cf_getopt(int argc,char * const argv[],
429 const char *shortopts,const struct option *longopts,
433 static int other_options;
436 res=getopt_long(argc,argv,shortopts,longopts,longindex);
437 if(res == 'S' || res == 'C') {
439 die("The -S and -C options must precede all other arguments");
446 /* unhandled option or end of options */