]> mj.ucw.cz Git - libucw.git/blobdiff - lib/conf.c
Minor cleanup of KMP:
[libucw.git] / lib / conf.c
index effdbd9c81d2e0b1b1692debf8fe9dc91ede4259..2293cdae84a836ab7b22e0d6504e2003c80d228f 100644 (file)
@@ -1,8 +1,8 @@
 /*
- *     Sherlock Library -- Reading of configuration files
+ *     UCW Library -- Reading of configuration files
  *
  *     (c) 2001 Robert Spalek <robert@ucw.cz>
- *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *     (c) 2003--2005 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -11,7 +11,7 @@
 #include "lib/lib.h"
 #include "lib/chartype.h"
 #include "lib/fastbuf.h"
-#include "lib/pools.h"
+#include "lib/mempool.h"
 
 #include "lib/conf.h"
 
 static struct cfitem *cfsection;
 struct mempool *cfpool;
 
+#ifndef DEFAULT_CONFIG
+#define DEFAULT_CONFIG NULL
+#endif
+
 byte *cfdeffile = DEFAULT_CONFIG;
 
 static void CONSTRUCTOR
@@ -42,6 +46,12 @@ cfg_malloc(uns size)
        return mp_alloc(cfpool, size);
 }
 
+void *
+cfg_malloc_zero(uns size)
+{
+       return mp_alloc_zero(cfpool, size);
+}
+
 byte *
 cfg_strdup(byte *s)
 {
@@ -331,6 +341,41 @@ void cf_read(byte *filename)
        cfdeffile = NULL;
 }
 
+static void cf_opt_S(void)
+{
+       byte *sect,*name,*value;
+       byte *c;
+       byte *msg=NULL;
+       byte arg[strlen(optarg)+1];
+
+       strcpy(arg, optarg);
+       name = arg;
+       c=strchr(name,'=');
+       if(!c){
+               msg="Missing argument";
+               sect=value="";
+       }else{
+               *c++=0;
+               value=c;
+
+               c=strchr(name,'.');
+               if(!c)
+                       sect="";
+               else{
+                       sect=name;
+                       *c++=0;
+                       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: %s",sect,name,value,msg);
+
+}
+
 int cf_getopt(int argc,char * const argv[],
                const char *shortopts,const struct option *longopts,
                int *longindex)
@@ -345,35 +390,7 @@ int cf_getopt(int argc,char * const argv[],
                                die("The -S and -C options must precede all other arguments");
                }
                if(res=='S'){
-                       byte *sect,*name,*value;
-                       byte *c;
-                       byte *msg=NULL;
-
-                       name=optarg;
-                       c=strchr(name,'=');
-                       if(!c){
-                               msg="Missing argument";
-                               sect=value="";
-                       }else{
-                               *c++=0;
-                               value=c;
-
-                               c=strchr(name,'.');
-                               if(!c)
-                                       sect="";
-                               else{
-                                       sect=name;
-                                       *c++=0;
-                                       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: %s",sect,name,value,msg);
-
+                       cf_opt_S();
                }else if(res=='C'){
                        cf_read(optarg);
                }else{