-/* Reading conf files
- * Robert Spalek, (c) 2001, robert@ucw.cz
- * $Id: conf.c,v 1.2 2001/01/08 10:26:37 robert Exp $
+/*
+ * Sherlock Library -- Reading configuration files
+ *
+ * (c) 2001 Robert Spalek <robert@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
*/
+#include "lib/lib.h"
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
-#include "lib/lib.h"
+#include "lib/chartype.h"
#include "lib/fastbuf.h"
+#include "lib/pools.h"
#include "lib/conf.h"
#define BUFFER 1024
#define MAX_LEVEL 8
-#define MAX_SECTIONS 64
+static struct cfitem *cfsection;
+static struct mempool *cfpool;
-static struct {
- byte *section;
- struct cfitem *items;
-} cfsection[MAX_SECTIONS];
-static int cfsections;
+byte *cfdeffile = DEFAULT_CONFIG;
-#define MAX_SHORT_OPTS 128
-#define MAX_LONG_OPTS 64
+static void CONSTRUCTOR
+conf_init(void)
+{
+ cfpool = mp_new(4096);
+}
+
+void *
+cfg_malloc(uns size)
+{
+ return mp_alloc(cfpool, size);
+}
-static byte shortopts[MAX_SHORT_OPTS] = "S:C:";
-static int shortlen=4;
+byte *
+cfg_stralloc(byte *s)
+{
+ uns l = strlen(s);
+ byte *k = cfg_malloc(l + 1);
+ strcpy(k, s);
+ return k;
+}
-static struct option longopts[MAX_LONG_OPTS] =
+void cf_register(struct cfitem *items)
{
- {"set", 1, 0, 'S'},
- {"config", 1, 0, 'C'}
-};
-static int longlen=2;
+ if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION)
+ die("cf_register: Invalid section type");
+ items[0].var=cfsection;
+ cfsection=items;
+}
-void cf_register(byte *section,struct cfitem *items)
+int cf_item_count(void)
{
- if(cfsections>=MAX_SECTIONS)
- die("too many modules %d",cfsections);
- cfsection[cfsections].section=section;
- cfsection[cfsections].items=items;
- cfsections++;
+ struct cfitem *sect, *item;
+ int count = 0;
+ for (sect = cfsection; sect; sect = sect->var)
+ for (item = sect+1; item->type; item++)
+ count++;
+ return count;
}
-static byte *cf_set_item(byte *sect, byte *name, byte *value)
+struct cfitem *cf_get_item(byte *sect, byte *name)
+{
+ struct cfitem *item, *section;
+
+ item=cfsection;
+ while(item && strcasecmp(item->name,sect))
+ item=item->var;
+ if(!item) /* unknown section */
+ return NULL;
+ section = item;
+
+ for(item++; item->type && strcasecmp(item->name,name); item++);
+ if (!item->type && section->type == CT_INCOMPLETE_SECTION)
+ return NULL;
+
+ return item; /* item->type == 0 if not found */
+}
+
+byte *cf_set_item(byte *sect, byte *name, byte *value)
{
- int idsect;
struct cfitem *item;
byte *msg=NULL;
- idsect=0;
- while(idsect<cfsections && strcasecmp(sect,cfsection[idsect].section))
- idsect++;
- if(idsect>=cfsections) /* ignore unknown section */
+ if (!*sect)
+ return "Empty section name";
+ item=cf_get_item(sect,name);
+ if(!item) /* ignore unknown section */
return NULL;
- item=cfsection[idsect].items;
- while(item->type && strcasecmp(name,item->name))
- item++;
switch(item->type){
- case ct_int:
+ case CT_INT:
{
char *end;
- *((uns *) item->var) = strtoul(value, &end, 0);
- if (end && *end)
- msg = "Invalid number";
+ if(!*value)
+ msg="Missing number";
+ else{
+ *((uns *) item->var) = strtoul(value, &end, 0);
+ if (end && *end)
+ msg = "Invalid number";
+ }
break;
}
- case ct_string:
- *((byte **) item->var) = stralloc(value);
+ case CT_STRING:
+ *((byte **) item->var) = cfg_stralloc(value);
break;
- case ct_function:
- msg = ((ci_func) item->var)(item, value);
+ case CT_FUNCTION:
+ msg = ((ci_func) item->var)(item, cfg_stralloc(value));
break;
default:
msg = "Unknown keyword";
static int cf_subread(byte *filename,int level)
{
+ int fd;
struct fastbuf *b;
byte def_section[BUFFER];
int line;
byte *msg=NULL;
if(level>=MAX_LEVEL){
- log("Too many nested files %d",level);
+ log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
return 0;
}
- b=bopen(filename,O_RDONLY,4096);
- if(!b){
- log("Cannot open file %s",filename);
+ fd=open(filename,O_RDONLY, 0666);
+ if(fd<0){
+ log(L_ERROR,"Cannot open configuration file %s: %m",filename);
return 0;
}
+ b=bfdopen(fd,4096);
def_section[0]=0;
line=0;
break;
line++;
+ c=buf+strlen(buf);
+ while(c>buf && Cspace(c[-1]))
+ *--c=0;
c=buf;
- while(*c && isspace(*c))
+ while(*c && Cspace(*c))
c++;
if(!*c || *c=='#')
continue;
if(*c=='['){
strcpy(def_section,c+1);
c=strchr(def_section,']');
- if(c)
+ if(c){
*c=0;
- else{
+ if(c[1]){
+ msg="Garbage after ]";
+ break;
+ }
+ }else{
msg="Missing ]";
break;
}
- }else if(*c=='<'){
- if(!cf_subread(c+1,level+1)){
- msg="";
- break;
- }
-
}else{
byte *sect,*name,*value;
name=c;
- c=strpbrk(c," \t");
- while(c && *c && isspace(*c))
+ while(*c && !Cspace(*c))
+ c++;
+ while(*c && Cspace(*c))
*c++=0;
- if(!c || !*c){
- msg="Missing argument";
- break;
- }
value=c;
- c=strchr(name,'.');
- if(!c)
- sect=def_section;
- else{
- sect=name;
- *c++=0;
- name=c;
- }
+ if(!strcasecmp(name,"include")){
+ if(!cf_subread(value,level+1)){
+ msg="Included from here";
+ break;
+ }
+ }else{
+ c=strchr(name,'.');
+ if(!c)
+ sect=def_section;
+ else{
+ sect=name;
+ *c++=0;
+ name=c;
+ }
- msg=cf_set_item(sect,name,value);
+ msg=cf_set_item(sect,name,value);
+ }
if(msg)
break;
}
} /* for every line */
if(msg)
- log("%s, line %d: %s",filename,line,msg);
+ log(L_ERROR,"%s, line %d: %s",filename,line,msg);
bclose(b);
return !msg;
}
-int cf_read(byte *filename)
-{
- return cf_subread(filename,0);
-}
-
-void cf_read_err(byte *filename)
+void cf_read(byte *filename)
{
- if(!cf_read(filename))
+ if(!cf_subread(filename,0))
die("Reading config file %s failed",filename);
+ cfdeffile = NULL;
}
-void cf_register_opts(byte *so,struct option *lo)
-{
- int l;
-
- l=strlen(so);
- if(shortlen+l>=MAX_SHORT_OPTS)
- die("Too many short options %d",shortlen+l);
- strcat(shortopts,so);
-
- l=longlen;
- while(lo->name){
- if(l>=MAX_LONG_OPTS)
- die("Too many long options %d",l);
- longopts[l++]=*lo++;
- }
-}
-
-int cf_getopt(int argc,char * const argv[], int *longindex)
+int cf_getopt(int argc,char * const argv[],
+ const char *shortopts,const struct option *longopts,
+ int *longindex)
{
int res;
byte *c;
byte *msg=NULL;
-
name=optarg;
c=strchr(name,'=');
if(!c){
name=c;
}
+ if (cfdeffile)
+ cf_read(cfdeffile);
msg=cf_set_item(sect,name,value);
}
if(msg)
die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg);
}else if(res=='C'){
- cf_read_err(optarg);
-
- }else{ /* unhandled option */
+ cf_read(optarg);
+ }else{
+ /* unhandled option or end of options */
+ if(cfdeffile)
+ cf_read(cfdeffile);
return res;
}
-
}while(1);
}
-