2 * Sherlock Library -- Reading configuration files
4 * (c) 2001 Robert Spalek <robert@ucw.cz>
15 #include "lib/chartype.h"
16 #include "lib/fastbuf.h"
24 static struct cfitem *cfsection;
26 void cf_register(struct cfitem *items)
28 if(items[0].type!=CT_SECTION)
29 die("Invalid configuration section, first item must be of type CT_SECTION");
30 items[0].var=cfsection;
34 byte *cf_set_item(byte *sect, byte *name, byte *value)
40 while(item && strcasecmp(item->name,sect))
42 if(!item) /* ignore unknown section */
45 for(item++; item->type && strcasecmp(item->name,name); item++);
51 *((uns *) item->var) = strtoul(value, &end, 0);
53 msg = "Invalid number";
57 *((byte **) item->var) = stralloc(value);
60 msg = ((ci_func) item->var)(item, value);
63 msg = "Unknown keyword";
70 static int cf_subread(byte *filename,int level)
74 byte def_section[BUFFER];
79 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
83 fd=sh_open(filename,O_RDONLY, 0666);
85 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
96 if(!bgets(b,buf,BUFFER))
101 while(*c && Cspace(*c))
107 strcpy(def_section,c+1);
108 c=strchr(def_section,']');
117 if(!cf_subread(c+1,level+1)){
123 byte *sect,*name,*value;
127 while(c && *c && Cspace(*c))
130 msg="Missing argument";
144 msg=cf_set_item(sect,name,value);
149 } /* for every line */
152 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
157 void cf_read(byte *filename)
159 if(!cf_subread(filename,0))
160 die("Reading config file %s failed",filename);
163 int cf_getopt(int argc,char * const argv[],
164 const char *shortopts,const struct option *longopts,
170 res=getopt_long(argc,argv,shortopts,longopts,longindex);
172 byte *sect,*name,*value;
179 msg="Missing argument";
194 msg=cf_set_item(sect,name,value);
197 die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
201 }else{ /* unhandled option */