summaryrefslogtreecommitdiff
path: root/catgconf.c
blob: f3d7b9c3ded22d24fa05d3789a5cfa5251f7d83d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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 = NULL, *val = NULL;
    int conftype;

    for (;;) {
	free(opt);
	free(val);
	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);
}