Add copyright information to LICENSE.
[radsecproxy.git] / catgconf.c
1 /* Copyright (c) 2006-2009, Stig Venaas, UNINETT AS.
2  * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3  * Copyright (c) 2010-2013, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include "debug.h"
11 #include "gconfig.h"
12
13 int listconfig(struct gconffile **cf, char *block, int compact) {
14     char *opt = NULL, *val = NULL;
15     int conftype;
16
17     for (;;) {
18         free(opt);
19         free(val);
20         if (!getconfigline(cf, block, &opt, &val, &conftype))
21             return -1;
22         if (!opt)
23             return 0;           /* Success.  */
24
25         if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
26             if (!pushgconfpaths(cf, val))
27                 debugx(1, DBG_ERR, "failed to include config file %s", val);
28             continue;
29         }
30
31         switch (conftype) {
32         case CONF_STR:
33             if (block)
34                 printf(compact ? "%s=%s;" : "\t%s=%s\n", opt, val);
35             else
36                 printf("%s=%s\n", opt, val);
37             break;
38         case CONF_CBK:
39             printf("%s %s {%s", opt, val, compact ? "" : "\n");
40             if (listconfig(cf, val, compact))
41                 return -1;
42             printf("}\n");
43             break;
44         default:
45             printf("Unsupported config type\n");
46             return -1;
47         }
48     }
49
50     return 0;                   /* Success.  */
51 }
52
53 int main(int argc, char **argv) {
54     int c, compact = 0;
55     struct gconffile *cfs;
56
57     debug_init("radsecproxy-conf");
58     debug_set_level(DBG_WARN);
59
60     while ((c = getopt(argc, argv, "c")) != -1) {
61         switch (c) {
62         case 'c':
63             compact = 1;
64             break;
65         default:
66             goto usage;
67         }
68     }
69     if (argc - optind != 1)
70         goto usage;
71
72     cfs = openconfigfile(argv[optind]);
73     return listconfig(&cfs, NULL, compact);
74
75 usage:
76     debug(DBG_ERR, "Usage:\n%s [ -c ] configfile", argv[0]);
77     exit(1);
78 }
79
80 /* Local Variables: */
81 /* c-file-style: "stroustrup" */
82 /* End: */