2 * Sherlock Library -- Reading of configuration files
4 * (c) 2001 Robert Spalek <robert@ucw.cz>
5 * (c) 2003 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 byte *cfdeffile = DEFAULT_CONFIG;
33 static void CONSTRUCTOR
36 cfpool = mp_new(4096);
42 return mp_alloc(cfpool, size);
48 return mp_strdup(cfpool, s);
51 void cf_register(struct cfitem *items)
53 if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION)
54 die("cf_register: Invalid section type");
55 items[0].var=cfsection;
59 int cf_item_count(void)
61 struct cfitem *sect, *item;
63 for (sect = cfsection; sect; sect = sect->var)
64 for (item = sect+1; item->type; item++)
69 struct cfitem *cf_get_item(byte *sect, byte *name)
71 struct cfitem *item, *section;
74 while(item && strcasecmp(item->name,sect))
76 if(!item) /* unknown section */
80 for(item++; item->type && strcasecmp(item->name,name); item++);
81 if (!item->type && section->type == CT_INCOMPLETE_SECTION)
84 return item; /* item->type == 0 if not found */
88 uns name; /* One-letter name of the unit */
89 uns num, den; /* Fraction */
92 static const struct unit units[] = {
97 { 'g', 1000000000, 1 },
100 { 'G', 1073741824, 1 },
105 static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg)
108 if (end == value || end[1] || *end >= '0' && *end <= '9')
109 *msg = "Invalid number";
111 for (const struct unit *u=units; u->name; u++)
114 *msg = "Invalid unit";
120 static char cf_rngerr[] = "Number out of range";
122 byte *cf_parse_int(byte *value, uns *varp)
125 const struct unit *u;
128 msg = "Missing number";
132 uns x = strtoul(value, &end, 0);
135 else if (u = cf_lookup_unit(value, end, &msg)) {
136 u64 y = (u64)x * u->num;
138 msg = "Number is not an integer";
151 byte *cf_parse_u64(byte *value, u64 *varp)
154 const struct unit *u;
157 msg = "Missing number";
161 u64 x = strtoull(value, &end, 0);
164 else if (u = cf_lookup_unit(value, end, &msg)) {
165 if (x > ~(u64)0 / u->num)
166 msg = "Number out of range";
170 msg = "Number is not an integer";
180 byte *cf_parse_double(byte *value, double *varp)
183 const struct unit *u;
186 msg = "Missing number";
190 double x = strtoul(value, &end, 0);
193 else if (u = cf_lookup_unit(value, end, &msg))
194 *varp = x * u->num / u->den;
201 byte *cf_set_item(byte *sect, byte *name, byte *value)
207 return "Empty section name";
208 item=cf_get_item(sect,name);
209 if(!item) /* ignore unknown section */
214 msg = cf_parse_int(value, (uns *) item->var);
217 *((byte **) item->var) = cfg_strdup(value);
220 msg = ((ci_func) item->var)(item, cfg_strdup(value));
223 msg = cf_parse_double(value, (double *) item->var);
226 msg = cf_parse_u64(value, (u64 *) item->var);
229 msg = "Unknown keyword";
235 static int cf_subread(byte *filename,int level)
239 byte def_section[BUFFER];
243 if(level>=MAX_LEVEL){
244 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
248 fd=open(filename,O_RDONLY, 0666);
250 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
261 if(!bgets(b,buf,BUFFER))
266 while(c>buf && Cspace(c[-1]))
269 while(*c && Cspace(*c))
275 strcpy(def_section,c+1);
276 c=strchr(def_section,']');
280 msg="Garbage after ]";
289 byte *sect,*name,*value;
292 while(*c && !Cspace(*c))
294 while(*c && Cspace(*c))
298 if(!strcasecmp(name,"include")){
299 if(!cf_subread(value,level+1)){
300 msg="Included from here";
313 msg=cf_set_item(sect,name,value);
319 } /* for every line */
322 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
327 void cf_read(byte *filename)
329 if(!cf_subread(filename,0))
330 die("Reading config file %s failed",filename);
334 static void cf_opt_S(void)
336 byte *sect,*name,*value;
339 byte arg[strlen(optarg)+1];
345 msg="Missing argument";
362 msg=cf_set_item(sect,name,value);
365 die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg);
369 int cf_getopt(int argc,char * const argv[],
370 const char *shortopts,const struct option *longopts,
374 static int other_options;
377 res=getopt_long(argc,argv,shortopts,longopts,longindex);
378 if(res == 'S' || res == 'C') {
380 die("The -S and -C options must precede all other arguments");
387 /* unhandled option or end of options */