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;
221 byte *cf_set_item(byte *sect, byte *name, byte *value)
227 return "Empty section name";
228 item=cf_get_item(sect,name);
229 if(!item) /* ignore unknown section */
234 msg = cf_parse_int(value, (uns *) item->var);
237 *((byte **) item->var) = cfg_strdup(value);
240 msg = ((ci_func) item->var)(item, cfg_strdup(value));
243 msg = cf_parse_double(value, (double *) item->var);
246 msg = cf_parse_u64(value, (u64 *) item->var);
249 msg = "Unknown keyword";
255 static int cf_subread(byte *filename,int level)
259 byte def_section[BUFFER];
263 if(level>=MAX_LEVEL){
264 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
268 fd=open(filename,O_RDONLY, 0666);
270 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
281 if(!bgets(b,buf,BUFFER))
286 while(c>buf && Cspace(c[-1]))
289 while(*c && Cspace(*c))
295 strcpy(def_section,c+1);
296 c=strchr(def_section,']');
300 msg="Garbage after ]";
309 byte *sect,*name,*value;
312 while(*c && !Cspace(*c))
314 while(*c && Cspace(*c))
318 if(!strcasecmp(name,"include")){
319 if(!cf_subread(value,level+1)){
320 msg="Included from here";
333 msg=cf_set_item(sect,name,value);
339 } /* for every line */
342 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
347 void cf_read(byte *filename)
349 if(!cf_subread(filename,0))
350 die("Reading config file %s failed",filename);
354 static void cf_opt_S(void)
356 byte *sect,*name,*value;
359 byte arg[strlen(optarg)+1];
365 msg="Missing argument";
382 msg=cf_set_item(sect,name,value);
385 die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg);
389 int cf_getopt(int argc,char * const argv[],
390 const char *shortopts,const struct option *longopts,
394 static int other_options;
397 res=getopt_long(argc,argv,shortopts,longopts,longindex);
398 if(res == 'S' || res == 'C') {
400 die("The -S and -C options must precede all other arguments");
407 /* unhandled option or end of options */