2 * Robert Spalek, (c) 2001, robert@ucw.cz
3 * $Id: conf.c,v 1.2 2001/01/08 10:26:37 robert Exp $
14 #include "lib/fastbuf.h"
21 #define MAX_SECTIONS 64
26 } cfsection[MAX_SECTIONS];
27 static int cfsections;
29 #define MAX_SHORT_OPTS 128
30 #define MAX_LONG_OPTS 64
32 static byte shortopts[MAX_SHORT_OPTS] = "S:C:";
33 static int shortlen=4;
35 static struct option longopts[MAX_LONG_OPTS] =
42 void cf_register(byte *section,struct cfitem *items)
44 if(cfsections>=MAX_SECTIONS)
45 die("too many modules %d",cfsections);
46 cfsection[cfsections].section=section;
47 cfsection[cfsections].items=items;
51 static byte *cf_set_item(byte *sect, byte *name, byte *value)
58 while(idsect<cfsections && strcasecmp(sect,cfsection[idsect].section))
60 if(idsect>=cfsections) /* ignore unknown section */
63 item=cfsection[idsect].items;
64 while(item->type && strcasecmp(name,item->name))
70 *((uns *) item->var) = strtoul(value, &end, 0);
72 msg = "Invalid number";
76 *((byte **) item->var) = stralloc(value);
79 msg = ((ci_func) item->var)(item, value);
82 msg = "Unknown keyword";
89 static int cf_subread(byte *filename,int level)
92 byte def_section[BUFFER];
97 log("Too many nested files %d",level);
101 b=bopen(filename,O_RDONLY,4096);
103 log("Cannot open file %s",filename);
113 if(!bgets(b,buf,BUFFER))
118 while(*c && isspace(*c))
124 strcpy(def_section,c+1);
125 c=strchr(def_section,']');
134 if(!cf_subread(c+1,level+1)){
140 byte *sect,*name,*value;
144 while(c && *c && isspace(*c))
147 msg="Missing argument";
161 msg=cf_set_item(sect,name,value);
166 } /* for every line */
169 log("%s, line %d: %s",filename,line,msg);
174 int cf_read(byte *filename)
176 return cf_subread(filename,0);
179 void cf_read_err(byte *filename)
181 if(!cf_read(filename))
182 die("Reading config file %s failed",filename);
185 void cf_register_opts(byte *so,struct option *lo)
190 if(shortlen+l>=MAX_SHORT_OPTS)
191 die("Too many short options %d",shortlen+l);
192 strcat(shortopts,so);
197 die("Too many long options %d",l);
202 int cf_getopt(int argc,char * const argv[], int *longindex)
207 res=getopt_long(argc,argv,shortopts,longopts,longindex);
209 byte *sect,*name,*value;
217 msg="Missing argument";
232 msg=cf_set_item(sect,name,value);
235 die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
240 }else{ /* unhandled option */