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/pools.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);
49 byte *k = cfg_malloc(l + 1);
54 void cf_register(struct cfitem *items)
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;
62 int cf_item_count(void)
64 struct cfitem *sect, *item;
66 for (sect = cfsection; sect; sect = sect->var)
67 for (item = sect+1; item->type; item++)
72 struct cfitem *cf_get_item(byte *sect, byte *name)
74 struct cfitem *item, *section;
77 while(item && strcasecmp(item->name,sect))
79 if(!item) /* unknown section */
83 for(item++; item->type && strcasecmp(item->name,name); item++);
84 if (!item->type && section->type == CT_INCOMPLETE_SECTION)
87 return item; /* item->type == 0 if not found */
91 uns name; /* One-letter name of the unit */
92 uns num, den; /* Fraction */
95 static const struct unit units[] = {
98 { 'g', 1000000000, 1 },
101 { 'G', 1073741824, 1 },
106 static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg)
109 if (end == value || end[1] || *end >= '0' && *end <= '9')
110 *msg = "Invalid number";
112 for (const struct unit *u=units; u->name; u++)
115 *msg = "Invalid unit";
121 static char cf_rngerr[] = "Number out of range";
123 byte *cf_parse_int(byte *value, uns *varp)
126 const struct unit *u;
129 msg = "Missing number";
133 uns x = strtoul(value, &end, 0);
136 else if (u = cf_lookup_unit(value, end, &msg)) {
137 u64 y = (u64)x * u->num;
139 msg = "Number is not an integer";
152 byte *cf_parse_u64(byte *value, u64 *varp)
155 const struct unit *u;
158 msg = "Missing number";
162 u64 x = strtoull(value, &end, 0);
165 else if (u = cf_lookup_unit(value, end, &msg)) {
166 if (x > ~(u64)0 / u->num)
167 msg = "Number out of range";
171 msg = "Number is not an integer";
181 byte *cf_parse_double(byte *value, double *varp)
184 const struct unit *u;
187 msg = "Missing number";
191 double x = strtoul(value, &end, 0);
194 else if (u = cf_lookup_unit(value, end, &msg))
195 *varp = x * u->num / u->den;
202 byte *cf_set_item(byte *sect, byte *name, byte *value)
208 return "Empty section name";
209 item=cf_get_item(sect,name);
210 if(!item) /* ignore unknown section */
215 msg = cf_parse_int(value, (uns *) item->var);
218 *((byte **) item->var) = cfg_stralloc(value);
221 msg = ((ci_func) item->var)(item, cfg_stralloc(value));
224 msg = cf_parse_double(value, (double *) item->var);
227 msg = cf_parse_u64(value, (u64 *) item->var);
230 msg = "Unknown keyword";
236 static int cf_subread(byte *filename,int level)
240 byte def_section[BUFFER];
244 if(level>=MAX_LEVEL){
245 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
249 fd=open(filename,O_RDONLY, 0666);
251 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
262 if(!bgets(b,buf,BUFFER))
267 while(c>buf && Cspace(c[-1]))
270 while(*c && Cspace(*c))
276 strcpy(def_section,c+1);
277 c=strchr(def_section,']');
281 msg="Garbage after ]";
290 byte *sect,*name,*value;
293 while(*c && !Cspace(*c))
295 while(*c && Cspace(*c))
299 if(!strcasecmp(name,"include")){
300 if(!cf_subread(value,level+1)){
301 msg="Included from here";
314 msg=cf_set_item(sect,name,value);
320 } /* for every line */
323 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
328 void cf_read(byte *filename)
330 if(!cf_subread(filename,0))
331 die("Reading config file %s failed",filename);
335 int cf_getopt(int argc,char * const argv[],
336 const char *shortopts,const struct option *longopts,
342 res=getopt_long(argc,argv,shortopts,longopts,longindex);
344 byte *sect,*name,*value;
351 msg="Missing argument";
368 msg=cf_set_item(sect,name,value);
371 die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
376 /* unhandled option or end of options */