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);
61 void cf_register(struct cfitem *items)
63 if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION)
64 die("cf_register: Invalid section type");
65 items[0].var=cfsection;
69 int cf_item_count(void)
71 struct cfitem *sect, *item;
73 for (sect = cfsection; sect; sect = sect->var)
74 for (item = sect+1; item->type; item++)
79 struct cfitem *cf_get_item(byte *sect, byte *name)
81 struct cfitem *item, *section;
84 while(item && strcasecmp(item->name,sect))
86 if(!item) /* unknown section */
90 for(item++; item->type && strcasecmp(item->name,name); item++);
91 if (!item->type && section->type == CT_INCOMPLETE_SECTION)
94 return item; /* item->type == 0 if not found */
98 uns name; /* One-letter name of the unit */
99 uns num, den; /* Fraction */
102 static const struct unit units[] = {
107 { 'g', 1000000000, 1 },
110 { 'G', 1073741824, 1 },
115 static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg)
118 if (end == value || end[1] || *end >= '0' && *end <= '9')
119 *msg = "Invalid number";
121 for (const struct unit *u=units; u->name; u++)
124 *msg = "Invalid unit";
130 static char cf_rngerr[] = "Number out of range";
132 byte *cf_parse_int(byte *value, uns *varp)
135 const struct unit *u;
138 msg = "Missing number";
142 uns x = strtoul(value, &end, 0);
145 else if (u = cf_lookup_unit(value, end, &msg)) {
146 u64 y = (u64)x * u->num;
148 msg = "Number is not an integer";
161 byte *cf_parse_u64(byte *value, u64 *varp)
164 const struct unit *u;
167 msg = "Missing number";
171 u64 x = strtoull(value, &end, 0);
174 else if (u = cf_lookup_unit(value, end, &msg)) {
175 if (x > ~(u64)0 / u->num)
176 msg = "Number out of range";
180 msg = "Number is not an integer";
190 byte *cf_parse_double(byte *value, double *varp)
193 const struct unit *u;
196 msg = "Missing number";
200 double x = strtoul(value, &end, 0);
203 else if (u = cf_lookup_unit(value, end, &msg))
204 *varp = x * u->num / u->den;
211 byte *cf_set_item(byte *sect, byte *name, byte *value)
217 return "Empty section name";
218 item=cf_get_item(sect,name);
219 if(!item) /* ignore unknown section */
224 msg = cf_parse_int(value, (uns *) item->var);
227 *((byte **) item->var) = cfg_strdup(value);
230 msg = ((ci_func) item->var)(item, cfg_strdup(value));
233 msg = cf_parse_double(value, (double *) item->var);
236 msg = cf_parse_u64(value, (u64 *) item->var);
239 msg = "Unknown keyword";
245 static int cf_subread(byte *filename,int level)
249 byte def_section[BUFFER];
253 if(level>=MAX_LEVEL){
254 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
258 fd=open(filename,O_RDONLY, 0666);
260 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
271 if(!bgets(b,buf,BUFFER))
276 while(c>buf && Cspace(c[-1]))
279 while(*c && Cspace(*c))
285 strcpy(def_section,c+1);
286 c=strchr(def_section,']');
290 msg="Garbage after ]";
299 byte *sect,*name,*value;
302 while(*c && !Cspace(*c))
304 while(*c && Cspace(*c))
308 if(!strcasecmp(name,"include")){
309 if(!cf_subread(value,level+1)){
310 msg="Included from here";
323 msg=cf_set_item(sect,name,value);
329 } /* for every line */
332 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
337 void cf_read(byte *filename)
339 if(!cf_subread(filename,0))
340 die("Reading config file %s failed",filename);
344 static void cf_opt_S(void)
346 byte *sect,*name,*value;
349 byte arg[strlen(optarg)+1];
355 msg="Missing argument";
372 msg=cf_set_item(sect,name,value);
375 die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg);
379 int cf_getopt(int argc,char * const argv[],
380 const char *shortopts,const struct option *longopts,
384 static int other_options;
387 res=getopt_long(argc,argv,shortopts,longopts,longindex);
388 if(res == 'S' || res == 'C') {
390 die("The -S and -C options must precede all other arguments");
397 /* unhandled option or end of options */