added catgconf utility
authorvenaas <venaas>
Fri, 9 May 2008 09:15:30 +0000 (09:15 +0000)
committervenaas <venaas@e88ac4ed-0b26-0410-9574-a7f39faa03bf>
Fri, 9 May 2008 09:15:30 +0000 (09:15 +0000)
git-svn-id: https://svn.testnett.uninett.no/radsecproxy/trunk@250 e88ac4ed-0b26-0410-9574-a7f39faa03bf

Makefile
catgconf.c [new file with mode: 0644]
gconfig.c
gconfig.h
radsecproxy.c

index 85aac0d..6eb07e2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,5 +6,9 @@ all: radsecproxy
 
 radsecproxy: $(OBJ)
        $(CC) $(CFLAGS) $(OBJ) $(LDFLAGS) -o radsecproxy
+
+catgconf: util.o debug.o gconfig.o catgconf.o
+       $(CC) $(CFLAGS) util.o debug.o gconfig.o catgconf.o -o catgconf
+
 clean:
-       rm -f $(OBJ) radsecproxy
+       rm -f $(OBJ) catgconf.o radsecproxy catgconf
diff --git a/catgconf.c b/catgconf.c
new file mode 100644 (file)
index 0000000..dc0f312
--- /dev/null
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "debug.h"
+#include "gconfig.h"
+
+void listconfig(struct gconffile **cf, char *block, int compact) {
+    char *opt, *val;
+    int conftype;
+
+    for (;;) {
+       getconfigline(cf, block, &opt, &val, &conftype);
+       if (!opt)
+           return;
+
+       if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
+           if (!pushgconffiles(cf, val))
+               debugx(1, DBG_ERR, "failed to include config file %s", val);
+           continue;
+       }
+
+       switch (conftype) {
+       case CONF_STR:
+           if (block)
+               printf(compact ? "%s=%s;" : "\t%s=%s\n", opt, val);
+           else
+               printf("%s=%s\n", opt, val);
+           break;
+       case CONF_CBK:
+           printf("%s %s {%s", opt, val, compact ? "" : "\n");
+           listconfig(cf, val, compact);
+           printf("}\n");
+           break;
+       default:
+           printf("Unsupported config type\n");
+       }
+    }
+}
+
+int main(int argc, char **argv) {
+    int c, compact = 0;
+    struct gconffile *cfs;
+
+    debug_init("catgconf");
+    debug_set_level(DBG_WARN);
+
+    while ((c = getopt(argc, argv, "c")) != -1) {
+       switch (c) {
+        case 'c':
+           compact = 1;
+            break;
+       default:
+           goto usage;
+       }
+    }
+    if (argc - optind != 1)
+        goto usage;
+
+    cfs = openconfigfile(argv[optind]);
+    listconfig(&cfs, NULL, compact);
+    return 0;
+
+usage:
+    debug(DBG_ERR, "Usage:\n%s [ -c ] configfile", argv[0]);
+    exit(1);
+}
index af73db1..3e40139 100644 (file)
--- a/gconfig.c
+++ b/gconfig.c
@@ -13,6 +13,7 @@
 #include <glob.h>
 #include <sys/types.h>
 #include <libgen.h>
+#include <errno.h>
 #include "debug.h"
 #include "util.h"
 #include "gconfig.h"
@@ -138,6 +139,15 @@ FILE *popgconffile(struct gconffile **cf) {
     return (*cf)[0].file;
 }
 
+struct gconffile *openconfigfile(const char *file) {
+    struct gconffile *cf = NULL;
+
+    if (!pushgconffile(&cf, file))
+       debugx(1, DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
+    debug(DBG_DBG, "reading config file %s", file);
+    return cf;
+}
+
 /* Parses config with following syntax:
  * One of these:
  * option-name value
@@ -148,17 +158,19 @@ FILE *popgconffile(struct gconffile **cf) {
  *     ...
  * }
  */
-void getgenericconfig(struct gconffile **cf, char *block, ...) {
-    va_list ap;
-    char line[1024];
-    /* initialise lots of stuff to avoid stupid compiler warnings */
-    char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
-    uint8_t *bln;
-    int type = 0, tcount, conftype = 0, n;
-    void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
 
+void getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
+    char line[1024];
+    char *tokens[3], *s;
+    int tcount;
+    
+    *opt = NULL;
+    *val = NULL;
+    *conftype = 0;
+    
     if (!cf || !*cf || !(*cf)->file)
        return;
+
     for (;;) {
        if (!fgets(line, 1024, (*cf)->file)) {
            if (popgconffile(cf))
@@ -181,35 +193,51 @@ void getgenericconfig(struct gconffile **cf, char *block, ...) {
                return;
            debugx(1, DBG_ERR, "configuration error, found } with no matching {");
        }
-
-       switch (tcount) {
-       case 2:
-           opt = tokens[0];
-           val = tokens[1];
-           conftype = CONF_STR;
+       break;
+    }
+    
+    switch (tcount) {
+    case 2:
+       *opt = tokens[0];
+       *val = tokens[1];
+       *conftype = CONF_STR;
+       break;
+    case 3:
+       if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
+           *opt = tokens[0];
+           *val = tokens[2];
+           *conftype = CONF_STR;
            break;
-       case 3:
-           if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
-               opt = tokens[0];
-               val = tokens[2];
-               conftype = CONF_STR;
-               break;
-           }
-           if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
-               opt = tokens[0];
-               val = tokens[1];
-               conftype = CONF_CBK;
-               break;
-           }
-           /* fall through */
-       default:
-           if (block)
-               debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
-           debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
        }
+       if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
+           *opt = tokens[0];
+           *val = tokens[1];
+           *conftype = CONF_CBK;
+           break;
+       }
+       /* fall through */
+    default:
+       if (block)
+           debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
+       debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
+    }
 
-       if (!*val)
-           debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
+    if (!**val)
+       debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
+    return;
+}
+
+void getgenericconfig(struct gconffile **cf, char *block, ...) {
+    va_list ap;
+    char *opt, *val, *word, *optval, **str = NULL, ***mstr = NULL;
+    uint8_t *bln;
+    int type = 0, conftype = 0, n;
+    void (*cbk)(struct gconffile **, char *, char *, char *) = NULL;
+
+    for (;;) {
+       getconfigline(cf, block, &opt, &val, &conftype);
+       if (!opt)
+           return;
 
        if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
            if (!pushgconffiles(cf, val))
index be48165..af44dac 100644 (file)
--- a/gconfig.h
+++ b/gconfig.h
@@ -8,7 +8,9 @@ struct gconffile {
     FILE *file;
 };
 
+void getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype);
 void getgenericconfig(struct gconffile **cf, char *block, ...);
 FILE *pushgconffile(struct gconffile **cf, const char *path);
 FILE *pushgconffiles(struct gconffile **cf, const char *path);
 FILE *popgconffile(struct gconffile **cf);
+struct gconffile *openconfigfile(const char *file);
index bd8c272..14a23c5 100644 (file)
@@ -2754,15 +2754,6 @@ void addrealm(char *value, char **servers, char **accservers, char *message) {
     debug(DBG_DBG, "addrealm: added realm %s", value);
 }
 
-struct gconffile *openconfigfile(const char *file) {
-    struct gconffile *cf = NULL;
-
-    if (!pushgconffile(&cf, file))
-       debugx(1, DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
-    debug(DBG_DBG, "reading config file %s", file);
-    return cf;
-}
-
 int addmatchcertattr(struct clsrvconf *conf, char *matchcertattr) {
     char *v;
     regex_t **r;
@@ -3215,8 +3206,7 @@ void getargs(int argc, char **argv, uint8_t *foreground, uint8_t *pretend, uint8
        return;
 
  usage:
-    debug(DBG_ERR, "Usage:\n%s [ -c configfile ] [ -d debuglevel ] [ -f ] [ -p ] [ -v ]", argv[0]);
-    exit(1);
+    debugx(1, DBG_ERR, "Usage:\n%s [ -c configfile ] [ -d debuglevel ] [ -f ] [ -p ] [ -v ]", argv[0]);
 }
 
 #ifdef SYS_SOLARIS9