added catgconf utility
[libradsec.git] / catgconf.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include "debug.h"
6 #include "gconfig.h"
7
8 void listconfig(struct gconffile **cf, char *block, int compact) {
9     char *opt, *val;
10     int conftype;
11
12     for (;;) {
13         getconfigline(cf, block, &opt, &val, &conftype);
14         if (!opt)
15             return;
16
17         if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
18             if (!pushgconffiles(cf, val))
19                 debugx(1, DBG_ERR, "failed to include config file %s", val);
20             continue;
21         }
22
23         switch (conftype) {
24         case CONF_STR:
25             if (block)
26                 printf(compact ? "%s=%s;" : "\t%s=%s\n", opt, val);
27             else
28                 printf("%s=%s\n", opt, val);
29             break;
30         case CONF_CBK:
31             printf("%s %s {%s", opt, val, compact ? "" : "\n");
32             listconfig(cf, val, compact);
33             printf("}\n");
34             break;
35         default:
36             printf("Unsupported config type\n");
37         }
38     }
39 }
40
41 int main(int argc, char **argv) {
42     int c, compact = 0;
43     struct gconffile *cfs;
44
45     debug_init("catgconf");
46     debug_set_level(DBG_WARN);
47
48     while ((c = getopt(argc, argv, "c")) != -1) {
49         switch (c) {
50         case 'c':
51             compact = 1;
52             break;
53         default:
54             goto usage;
55         }
56     }
57     if (argc - optind != 1)
58         goto usage;
59
60     cfs = openconfigfile(argv[optind]);
61     listconfig(&cfs, NULL, compact);
62     return 0;
63
64 usage:
65     debug(DBG_ERR, "Usage:\n%s [ -c ] configfile", argv[0]);
66     exit(1);
67 }