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++);
54 *((uns *) item->var) = strtoul(value, &end, 0);
56 msg = "Invalid number";
61 *((byte **) item->var) = stralloc(value);
64 msg = ((ci_func) item->var)(item, value);
67 msg = "Unknown keyword";
74 static int cf_subread(byte *filename,int level)
78 byte def_section[BUFFER];
83 log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
87 fd=sh_open(filename,O_RDONLY, 0666);
89 log(L_ERROR,"Cannot open configuration file %s: %m",filename);
100 if(!bgets(b,buf,BUFFER))
105 while(c>buf && Cspace(c[-1]))
108 while(*c && Cspace(*c))
114 strcpy(def_section,c+1);
115 c=strchr(def_section,']');
119 msg="Garbage after ]";
128 byte *sect,*name,*value;
131 while(*c && !Cspace(*c))
133 while(*c && Cspace(*c))
137 if(!strcasecmp(name,"include")){
138 if(!cf_subread(value,level+1)){
139 msg="Included from here";
152 msg=cf_set_item(sect,name,value);
158 } /* for every line */
161 log(L_ERROR,"%s, line %d: %s",filename,line,msg);
166 void cf_read(byte *filename)
168 if(!cf_subread(filename,0))
169 die("Reading config file %s failed",filename);
172 int cf_getopt(int argc,char * const argv[],
173 const char *shortopts,const struct option *longopts,
179 res=getopt_long(argc,argv,shortopts,longopts,longindex);
181 byte *sect,*name,*value;
188 msg="Missing argument";
203 msg=cf_set_item(sect,name,value);
206 die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
210 }else{ /* unhandled option */