]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - scripts/settings-test.c
2 * Copyright (C) 2014-2018 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
24 #include <settings/settings_types.h>
27 * Defined in libstrongswan but not part of the public API
29 bool settings_parser_parse_file(void *this, char *name
);
32 * Produce indentation for the given level
34 static void get_indent(char indent
[BUF_LEN
], int level
)
38 for (i
= 0; i
< level
* 2 && i
< BUF_LEN
- 2; i
+= 2)
47 * Recursively print the section and all subsections/settings
49 static void print_section(section_t
*section
, int level
)
57 get_indent(indent
, level
);
59 for (i
= 0; i
< array_count(section
->kv_order
); i
++)
61 array_get(section
->kv_order
, i
, &kv
);
62 printf("%s%s = %s\n", indent
, kv
->key
, kv
->value
);
64 for (i
= 0; i
< array_count(section
->sections_order
); i
++)
66 array_get(section
->sections_order
, i
, &sub
);
67 printf("%s%s", indent
, sub
->name
);
68 if (array_count(sub
->references
))
70 for (j
= 0; j
< array_count(sub
->references
); j
++)
72 array_get(sub
->references
, j
, &ref
);
73 printf("%s%s", j
== 0 ? " : " : ", ", ref
->name
);
76 printf(" {\n", indent
);
77 print_section(sub
, level
+ 1);
78 printf("%s}\n", indent
);
83 * Recursively print a given section and all subsections/settings
84 * FIXME: Doesn't work properly if any of the keys contain dots
86 static void print_settings_section(settings_t
*settings
, char *section
,
89 enumerator_t
*enumerator
;
90 char indent
[BUF_LEN
], buf
[BUF_LEN
], *key
, *value
;
92 get_indent(indent
, level
);
94 enumerator
= settings
->create_key_value_enumerator(settings
, section
);
95 while (enumerator
->enumerate(enumerator
, &key
, &value
))
97 printf("%s%s = %s\n", indent
, key
, value
);
100 enumerator
->destroy(enumerator
);
102 enumerator
= settings
->create_section_enumerator(settings
, section
);
103 while (enumerator
->enumerate(enumerator
, &key
))
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
);
111 enumerator
->destroy(enumerator
);
114 static void usage(FILE *out
, char *name
)
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");
121 fprintf(out
, " -r, --resolve displays the settings with references/redefines resolved.\n");
122 fprintf(out
, " -f, --file=FILE config file to load (default STDIN).\n");
126 int main(int argc
, char *argv
[])
129 bool resolve
= FALSE
;
133 struct option long_opts
[] = {
134 {"help", no_argument
, NULL
, 'h' },
135 {"debug", no_argument
, NULL
, 'd' },
136 {"file", required_argument
, NULL
, 'f' },
137 {"resolve", no_argument
, NULL
, 'r' },
140 switch (getopt_long(argc
, argv
, "hdf:r", long_opts
, NULL
))
145 usage(stdout
, argv
[0]);
148 setenv("DEBUG_SETTINGS_PARSER", "1", TRUE
);
157 usage(stderr
, argv
[0]);
163 /* don't load strongswan.conf */
164 library_init("", "settings-test");
165 atexit(library_deinit
);
167 dbg_default_set_level(3);
173 settings_t
*settings
= settings_create(file
);
175 print_settings_section(settings
, "", 0);
177 settings
->destroy(settings
);
181 section_t
*root
= settings_section_create(strdup("root"));
183 settings_parser_parse_file(root
, file
);
185 print_section(root
, 0);
187 settings_section_destroy(root
, NULL
);
192 usage(stderr
, argv
[0]);