radsecproxy-1.6.5.
[radsecproxy.git] / catgconf.c
1 /*
2  * Copyright (C) 2008 Stig Venaas <venaas@uninett.no>
3  * Copyright (C) 2010,2011,2012 NORDUnet A/S
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include "debug.h"
15 #include "gconfig.h"
16
17 int listconfig(struct gconffile **cf, char *block, int compact) {
18     char *opt = NULL, *val = NULL;
19     int conftype;
20
21     for (;;) {
22         free(opt);
23         free(val);
24         if (!getconfigline(cf, block, &opt, &val, &conftype))
25             return -1;
26         if (!opt)
27             return 0;           /* Success.  */
28
29         if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
30             if (!pushgconfpaths(cf, val))
31                 debugx(1, DBG_ERR, "failed to include config file %s", val);
32             continue;
33         }
34
35         switch (conftype) {
36         case CONF_STR:
37             if (block)
38                 printf(compact ? "%s=%s;" : "\t%s=%s\n", opt, val);
39             else
40                 printf("%s=%s\n", opt, val);
41             break;
42         case CONF_CBK:
43             printf("%s %s {%s", opt, val, compact ? "" : "\n");
44             if (listconfig(cf, val, compact))
45                 return -1;
46             printf("}\n");
47             break;
48         default:
49             printf("Unsupported config type\n");
50             return -1;
51         }
52     }
53
54     return 0;                   /* Success.  */
55 }
56
57 int main(int argc, char **argv) {
58     int c, compact = 0;
59     struct gconffile *cfs;
60
61     debug_init("radsecproxy-conf");
62     debug_set_level(DBG_WARN);
63
64     while ((c = getopt(argc, argv, "c")) != -1) {
65         switch (c) {
66         case 'c':
67             compact = 1;
68             break;
69         default:
70             goto usage;
71         }
72     }
73     if (argc - optind != 1)
74         goto usage;
75
76     cfs = openconfigfile(argv[optind]);
77     return listconfig(&cfs, NULL, compact);
78
79 usage:
80     debug(DBG_ERR, "Usage:\n%s [ -c ] configfile", argv[0]);
81     exit(1);
82 }
83
84 /* Local Variables: */
85 /* c-file-style: "stroustrup" */
86 /* End: */