]>
Commit | Line | Data |
---|---|---|
5b64c040 TB |
1 | /* |
2 | * Copyright (C) 2014 Tobias Brunner | |
3 | * Hochschule fuer Technik Rapperswil | |
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 | /** | |
32 | * Recursively print the section and all subsections/settings | |
33 | */ | |
34 | static void print_section(section_t *section, int level) | |
35 | { | |
36 | section_t *sub; | |
37 | kv_t *kv; | |
38 | int i; | |
39 | char indent[256]; | |
40 | ||
41 | for (i = 0; i < level * 2 && i < sizeof(indent) - 2; i += 2) | |
42 | { | |
43 | indent[i ] = ' '; | |
44 | indent[i+1] = ' '; | |
45 | } | |
46 | indent[i] = '\0'; | |
47 | ||
48 | for (i = 0; i < array_count(section->kv_order); i++) | |
49 | { | |
50 | array_get(section->kv_order, i, &kv); | |
51 | printf("%s%s = %s\n", indent, kv->key, kv->value); | |
52 | } | |
53 | for (i = 0; i < array_count(section->sections_order); i++) | |
54 | { | |
55 | array_get(section->sections_order, i, &sub); | |
56 | printf("%s%s {\n", indent, sub->name); | |
57 | print_section(sub, level + 1); | |
58 | printf("%s}\n", indent); | |
59 | } | |
60 | } | |
61 | ||
62 | static void usage(FILE *out, char *name) | |
63 | { | |
64 | fprintf(out, "Test strongswan.conf parser\n\n"); | |
65 | fprintf(out, "%s [OPTIONS]\n\n", name); | |
66 | fprintf(out, "Options:\n"); | |
67 | fprintf(out, " -h, --help print this help.\n"); | |
68 | fprintf(out, " -d, --debug enables debugging of the parser.\n"); | |
69 | fprintf(out, " -f, --file=FILE config file to load (default STDIN).\n"); | |
70 | fprintf(out, "\n"); | |
71 | } | |
72 | ||
73 | int main(int argc, char *argv[]) | |
74 | { | |
75 | char *file = NULL; | |
76 | ||
77 | /* don't load strongswan.conf */ | |
78 | library_init("", "settings-test"); | |
79 | atexit(library_deinit); | |
80 | ||
81 | dbg_default_set_level(3); | |
82 | ||
83 | while (true) | |
84 | { | |
85 | struct option long_opts[] = { | |
86 | {"help", no_argument, NULL, 'h' }, | |
87 | {"debug", no_argument, NULL, 'd' }, | |
88 | {"file", required_argument, NULL, 'f' }, | |
89 | {0,0,0,0 }, | |
90 | }; | |
91 | switch (getopt_long(argc, argv, "hdf:", long_opts, NULL)) | |
92 | { | |
93 | case EOF: | |
94 | break; | |
95 | case 'h': | |
96 | usage(stdout, argv[0]); | |
97 | return 0; | |
98 | case 'd': | |
99 | setenv("DEBUG_SETTINGS_PARSER", "1", TRUE); | |
100 | continue; | |
101 | case 'f': | |
102 | file = optarg; | |
103 | continue; | |
104 | default: | |
105 | usage(stderr, argv[0]); | |
106 | return 1; | |
107 | } | |
108 | break; | |
109 | } | |
110 | ||
111 | if (file) | |
112 | { | |
113 | section_t *root = settings_section_create(strdup("root")); | |
114 | ||
115 | settings_parser_parse_file(root, file); | |
116 | ||
117 | print_section(root, 0); | |
118 | ||
119 | settings_section_destroy(root, NULL); | |
120 | } | |
121 | else | |
122 | { | |
123 | usage(stderr, argv[0]); | |
124 | } | |
125 | return 0; | |
126 | } |