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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
* Copyright (C) 2014-2018 Tobias Brunner
*
* Copyright (C) secunet Security Networks AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <library.h>
#include <settings/settings_types.h>
/**
* Defined in libstrongswan but not part of the public API
*/
bool settings_parser_parse_file(void *this, char *name);
/**
* Produce indentation for the given level
*/
static void get_indent(char indent[BUF_LEN], int level)
{
int i;
for (i = 0; i < level * 2 && i < BUF_LEN - 2; i += 2)
{
indent[i ] = ' ';
indent[i+1] = ' ';
}
indent[i] = '\0';
}
/**
* Recursively print the section and all subsections/settings
*/
static void print_section(section_t *section, int level)
{
section_t *sub;
section_ref_t *ref;
kv_t *kv;
char indent[BUF_LEN];
int i, j;
get_indent(indent, level);
for (i = 0; i < array_count(section->kv_order); i++)
{
array_get(section->kv_order, i, &kv);
printf("%s%s = %s\n", indent, kv->key, kv->value);
}
for (i = 0; i < array_count(section->sections_order); i++)
{
array_get(section->sections_order, i, &sub);
printf("%s%s", indent, sub->name);
if (array_count(sub->references))
{
for (j = 0; j < array_count(sub->references); j++)
{
array_get(sub->references, j, &ref);
printf("%s%s", j == 0 ? " : " : ", ", ref->name);
}
}
printf(" {\n");
print_section(sub, level + 1);
printf("%s}\n", indent);
}
}
/**
* Recursively print a given section and all subsections/settings
*/
static void print_settings_section(settings_t *settings, char *section,
int level)
{
enumerator_t *enumerator;
char indent[BUF_LEN], buf[BUF_LEN], *key, *value;
get_indent(indent, level);
enumerator = settings->create_key_value_enumerator(settings, section);
while (enumerator->enumerate(enumerator, &key, &value))
{
printf("%s%s = %s\n", indent, key, value);
}
enumerator->destroy(enumerator);
enumerator = settings->create_section_enumerator(settings, section);
while (enumerator->enumerate(enumerator, &key))
{
printf("%s%s {\n", indent, key);
snprintf(buf, sizeof(buf), "%s%s%s", section,
strlen(section) ? "." : "", key);
print_settings_section(settings, buf, level + 1);
printf("%s}\n", indent);
}
enumerator->destroy(enumerator);
}
static void usage(FILE *out, char *name)
{
fprintf(out, "Test strongswan.conf parser\n\n");
fprintf(out, "%s [OPTIONS]\n\n", name);
fprintf(out, "Options:\n");
fprintf(out, " -h, --help print this help.\n");
fprintf(out, " -d, --debug enables debugging of the parser.\n");
fprintf(out, " -r, --resolve displays the settings with references/redefines resolved.\n");
fprintf(out, " -f, --file=FILE config file to load (default STDIN).\n");
fprintf(out, "\n");
}
int main(int argc, char *argv[])
{
char *file = NULL;
bool resolve = FALSE;
while (true)
{
struct option long_opts[] = {
{"help", no_argument, NULL, 'h' },
{"debug", no_argument, NULL, 'd' },
{"file", required_argument, NULL, 'f' },
{"resolve", no_argument, NULL, 'r' },
{0,0,0,0 },
};
switch (getopt_long(argc, argv, "hdf:r", long_opts, NULL))
{
case EOF:
break;
case 'h':
usage(stdout, argv[0]);
return 0;
case 'd':
setenv("DEBUG_SETTINGS_PARSER", "1", TRUE);
continue;
case 'f':
file = optarg;
continue;
case 'r':
resolve = TRUE;
continue;
default:
usage(stderr, argv[0]);
return 1;
}
break;
}
/* don't load strongswan.conf */
library_init("", "settings-test");
atexit(library_deinit);
dbg_default_set_level(3);
if (file)
{
if (resolve)
{
settings_t *settings = settings_create(file);
print_settings_section(settings, "", 0);
settings->destroy(settings);
}
else
{
section_t *root = settings_section_create(strdup("root"));
settings_parser_parse_file(root, file);
print_section(root, 0);
settings_section_destroy(root, NULL);
}
}
else
{
usage(stderr, argv[0]);
}
return 0;
}
|