2 * UCW 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 #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);
52 return mp_strdup(cfpool, s);
55 void cf_register(struct cfitem *items)
57 if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION)
58 die("cf_register: Invalid section type");
59 items[0].var=cfsection;
63 int cf_item_count(void)
65 struct cfitem *sect, *item;
67 for (sect = cfsection; sect; sect = sect->var)
68 for (item = sect+1; item->type; item++)
73 struct cfitem *cf_get_item(byte *sect, byte *name)
75 struct cfitem *item, *section;
78 while(item && strcasecmp(item->name,sect))
80 if(!item) /* unknown section */
84 for(item++; item->type && strcasecmp(item->name,name); item++);
85 if (!item->type && section->type == CT_INCOMPLETE_SECTION)
88 return item; /* item->type == 0 if not found */
92 uns name; /* One-letter name of the unit */
93 uns num, den; /* Fraction */
96 static const struct unit units[] = {
101 { 'g', 1000000000, 1 },
104 { 'G', 1073741824, 1 },
109 static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg)
112 if (end == value || end[1] || *end >= '0' && *end <= '9')
113 *msg = "Invalid number";
115 for (const struct unit *u=units; u->name; u++)
118 *msg = "Invalid unit";
124 static char cf_rngerr[] = "Number out of range";
126 byte *cf_parse_int(byte *value, uns *varp)
129 const struct unit *u;
132 msg = "Missing number";
136 uns x = strtoul(value, &end, 0);
139 else if (u = cf_lookup_unit(value, end, &msg)) {
140 u64 y = (u64)x * u->num;
142 msg = "Number is not an integer";
155 byte *cf_parse_u64(byte *value, u64 *varp)
158 const struct unit *u;
161 msg = "Missing number";
165 u64 x = strtoull(value, &end, 0);
168 else if (u = cf_lookup_unit(value, end, &msg)) {
169 if (x > ~(u64)0 / u->num)
170 msg = "Number out of range";
174 msg = "Number is not an integer";
184 byte *cf_parse_double(byte *value, double *varp)
187 const struct unit *u;
190 msg = "Missing number";
194 double x = strtoul(value, &end, 0);
197 else if (u = cf_lookup_unit(value, end, &msg))
198 *varp = x * u->num / u->den;
205 byte *cf_set_item(byte *sect, byte *name, byte *value)
211 return "Empty section name";
212 item=cf_get_item(sect,name);
213 if(!item) /* ignore unknown section */
218 msg = cf_parse_int(value, (uns *) item->var);
221 *((byte **) item->var) = cfg_strdup(value);
224 msg = ((ci_func) item->var)(item, cfg_strdup(value));
227 msg = cf_parse_double(value, (double *) item->var);
230 msg = cf_parse_u64(value, (u64 *) item->var);
233 msg = "Unknown keyword";
239 static int cf_subread(byte *filename,int level)
243 byte def_section[BUFFER];
247 if(level>=MAX_LEVEL){
248 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
252 fd=open(filename,O_RDONLY, 0666);
254 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
265 if(!bgets(b,buf,BUFFER))
270 while(c>buf && Cspace(c[-1]))
273 while(*c && Cspace(*c))
279 strcpy(def_section,c+1);
280 c=strchr(def_section,']');
284 msg="Garbage after ]";
293 byte *sect,*name,*value;
296 while(*c && !Cspace(*c))
298 while(*c && Cspace(*c))
302 if(!strcasecmp(name,"include")){
303 if(!cf_subread(value,level+1)){
304 msg="Included from here";
317 msg=cf_set_item(sect,name,value);
323 } /* for every line */
326 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
331 void cf_read(byte *filename)
333 if(!cf_subread(filename,0))
334 die("Reading config file %s failed",filename);
338 static void cf_opt_S(void)
340 byte *sect,*name,*value;
343 byte arg[strlen(optarg)+1];
349 msg="Missing argument";
366 msg=cf_set_item(sect,name,value);
369 die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg);
373 int cf_getopt(int argc,char * const argv[],
374 const char *shortopts,const struct option *longopts,
378 static int other_options;
381 res=getopt_long(argc,argv,shortopts,longopts,longindex);
382 if(res == 'S' || res == 'C') {
384 die("The -S and -C options must precede all other arguments");
391 /* unhandled option or end of options */