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