catgconf.c 1.94 KB
Newer Older
Linus Nordberg's avatar
Linus Nordberg committed
1 2 3 4 5 6 7 8 9
/*
 * Copyright (C) 2008 Stig Venaas <venaas@uninett.no>
 * Copyright (C) 2010,2011,2012 NORDUnet A/S
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

venaas's avatar
venaas committed
10 11 12 13 14 15 16
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "debug.h"
#include "gconfig.h"

17
int listconfig(struct gconffile **cf, char *block, int compact) {
18
    char *opt = NULL, *val = NULL;
venaas's avatar
venaas committed
19 20 21
    int conftype;

    for (;;) {
22 23
	free(opt);
	free(val);
24 25 26 27
	if (!getconfigline(cf, block, &opt, &val, &conftype))
            return -1;
        if (!opt)
            return 0;           /* Success.  */
venaas's avatar
venaas committed
28 29

	if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
30
	    if (!pushgconfpaths(cf, val))
venaas's avatar
venaas committed
31 32 33 34 35 36 37 38 39 40 41 42 43
		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");
44 45
	    if (listconfig(cf, val, compact))
                return -1;
venaas's avatar
venaas committed
46 47 48 49
	    printf("}\n");
	    break;
	default:
	    printf("Unsupported config type\n");
50
            return -1;
venaas's avatar
venaas committed
51 52
	}
    }
53 54

    return 0;                   /* Success.  */
venaas's avatar
venaas committed
55 56 57 58 59 60
}

int main(int argc, char **argv) {
    int c, compact = 0;
    struct gconffile *cfs;

Linus Nordberg's avatar
Linus Nordberg committed
61
    debug_init("radsecproxy-conf");
venaas's avatar
venaas committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    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]);
77
    return listconfig(&cfs, NULL, compact);
venaas's avatar
venaas committed
78 79 80 81 82

usage:
    debug(DBG_ERR, "Usage:\n%s [ -c ] configfile", argv[0]);
    exit(1);
}
83 84 85 86

/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */