2 * Robert Spalek, (c) 2001, robert@ucw.cz
3 * $Id: conf.c,v 1.1 2001/01/07 21:21:53 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))
117 while(*c && isspace(*c))
123 strcpy(def_section,c+1);
124 c=strchr(def_section,']');
133 if(!cf_subread(c+1,level+1)){
139 byte *sect,*name,*value;
143 while(c && *c && isspace(*c))
146 msg="Missing argument";
160 msg=cf_set_item(sect,name,value);
165 } /* for every line */
168 log("%s, line %d: %s",msg);
173 int cf_read(byte *filename)
175 return cf_subread(filename,0);
178 void cf_read_err(byte *filename)
180 if(!cf_read(filename))
181 die("Reading config file %s failed",filename);
184 void cf_register_opts(byte *so,struct option *lo)
189 if(shortlen+l>=MAX_SHORT_OPTS)
190 die("Too many short options %d",shortlen+l);
191 strcat(shortopts,so);
196 die("Too many long options %d",l);
201 int cf_getopt(int argc,char * const argv[], int *longindex)
206 res=getopt_long(argc,argv,shortopts,longopts,longindex);
208 byte *sect,*name,*value;
216 msg="Missing argument";
231 msg=cf_set_item(sect,name,value);
234 die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
239 }else{ /* unhandled option */