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