aboutsummaryrefslogtreecommitdiff
path: root/src/util/configuration_helper.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-09-05 16:58:55 +0200
committerChristian Grothoff <christian@grothoff.org>2021-09-05 16:59:04 +0200
commitb4e034d9aa471c6244718914e08be3ee414b18e6 (patch)
treed92d932db4a769eeb5780a91abcc8601ff31a503 /src/util/configuration_helper.c
parent00c03c7bb03f7ecae5b082eadf05a9bb29b0b957 (diff)
downloadgnunet-b4e034d9aa471c6244718914e08be3ee414b18e6.tar.gz
gnunet-b4e034d9aa471c6244718914e08be3ee414b18e6.zip
refactor gnunet-config to make preload hacks obsolete
Diffstat (limited to 'src/util/configuration_helper.c')
-rw-r--r--src/util/configuration_helper.c316
1 files changed, 316 insertions, 0 deletions
diff --git a/src/util/configuration_helper.c b/src/util/configuration_helper.c
new file mode 100644
index 000000000..eb8b543d1
--- /dev/null
+++ b/src/util/configuration_helper.c
@@ -0,0 +1,316 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2006, 2007, 2008, 2009, 2013, 2020, 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file src/util/configuration_helper.c
22 * @brief helper logic for gnunet-config
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27
28/**
29 * Print each option in a given section as a filename.
30 *
31 * @param cls closure
32 * @param section name of the section
33 * @param option name of the option
34 * @param value value of the option
35 */
36static void
37print_filename_option (void *cls,
38 const char *section,
39 const char *option,
40 const char *value)
41{
42 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
43
44 char *value_fn;
45 char *fn;
46
47 GNUNET_assert (GNUNET_OK ==
48 GNUNET_CONFIGURATION_get_value_filename (cfg,
49 section,
50 option,
51 &value_fn));
52 fn = GNUNET_STRINGS_filename_expand (value_fn);
53 if (NULL == fn)
54 fn = value_fn;
55 else
56 GNUNET_free (value_fn);
57 fprintf (stdout,
58 "%s = %s\n",
59 option,
60 fn);
61 GNUNET_free (fn);
62}
63
64
65/**
66 * Print each option in a given section.
67 *
68 * @param cls closure
69 * @param section name of the section
70 * @param option name of the option
71 * @param value value of the option
72 */
73static void
74print_option (void *cls,
75 const char *section,
76 const char *option,
77 const char *value)
78{
79 (void) cls;
80 (void) section;
81
82 fprintf (stdout,
83 "%s = %s\n",
84 option,
85 value);
86}
87
88
89/**
90 * Print out given section name.
91 *
92 * @param cls unused
93 * @param section a section in the configuration file
94 */
95static void
96print_section_name (void *cls,
97 const char *section)
98{
99 (void) cls;
100 fprintf (stdout,
101 "%s\n",
102 section);
103}
104
105
106void
107GNUNET_CONFIGURATION_config_tool_run (
108 void *cls,
109 char *const *args,
110 const char *cfgfile,
111 const struct GNUNET_CONFIGURATION_Handle *cfg)
112{
113 struct GNUNET_CONFIGURATION_ConfigSettings *cs = cls;
114 struct GNUNET_CONFIGURATION_Handle *out = NULL;
115 struct GNUNET_CONFIGURATION_Handle *ncfg = NULL;
116
117 (void) args;
118 if (NULL != cs->backend_check)
119 {
120 char *name;
121
122 GNUNET_asprintf (&name,
123 "libgnunet_plugin_%s",
124 cs->backend_check);
125 cs->global_ret = (GNUNET_OK ==
126 GNUNET_PLUGIN_test (name)) ? 0 : 77;
127 GNUNET_free (name);
128 return;
129 }
130
131 if (cs->diagnostics)
132 {
133 /* Re-parse the configuration with diagnostics enabled. */
134 ncfg = GNUNET_CONFIGURATION_create ();
135 GNUNET_CONFIGURATION_enable_diagnostics (ncfg);
136 GNUNET_CONFIGURATION_load (ncfg,
137 cfgfile);
138 cfg = ncfg;
139 }
140
141 if (cs->full)
142 cs->rewrite = GNUNET_YES;
143 if (cs->list_sections)
144 {
145 fprintf (stderr,
146 _ ("The following sections are available:\n"));
147 GNUNET_CONFIGURATION_iterate_sections (cfg,
148 &print_section_name,
149 NULL);
150 return;
151 }
152 if ( (! cs->rewrite) &&
153 (NULL == cs->section) )
154 {
155 char *serialization;
156
157 if (! cs->diagnostics)
158 {
159 fprintf (stderr,
160 _ ("%s, %s or %s argument is required\n"),
161 "--section",
162 "--list-sections",
163 "--diagnostics");
164 cs->global_ret = EXIT_INVALIDARGUMENT;
165 return;
166 }
167 serialization = GNUNET_CONFIGURATION_serialize_diagnostics (cfg);
168 fprintf (stdout,
169 "%s",
170 serialization);
171 GNUNET_free (serialization);
172 }
173 else if ( (NULL != cs->section) &&
174 (NULL == cs->value) )
175 {
176 if (NULL == cs->option)
177 {
178 GNUNET_CONFIGURATION_iterate_section_values (
179 cfg,
180 cs->section,
181 cs->is_filename
182 ? &print_filename_option
183 : &print_option,
184 (void *) cfg);
185 }
186 else
187 {
188 char *value;
189
190 if (cs->is_filename)
191 {
192 if (GNUNET_OK !=
193 GNUNET_CONFIGURATION_get_value_filename (cfg,
194 cs->section,
195 cs->option,
196 &value))
197 {
198 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
199 cs->section,
200 cs->option);
201 cs->global_ret = EXIT_NOTCONFIGURED;
202 return;
203 }
204 }
205 else
206 {
207 if (GNUNET_OK !=
208 GNUNET_CONFIGURATION_get_value_string (cfg,
209 cs->section,
210 cs->option,
211 &value))
212 {
213 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
214 cs->section,
215 cs->option);
216 cs->global_ret = EXIT_NOTCONFIGURED;
217 return;
218 }
219 }
220 fprintf (stdout,
221 "%s\n",
222 value);
223 GNUNET_free (value);
224 }
225 }
226 else if (NULL != cs->section)
227 {
228 if (NULL == cs->option)
229 {
230 fprintf (stderr,
231 _ ("--option argument required to set value\n"));
232 cs->global_ret = EXIT_INVALIDARGUMENT;
233 return;
234 }
235 out = GNUNET_CONFIGURATION_dup (cfg);
236 GNUNET_CONFIGURATION_set_value_string (out,
237 cs->section,
238 cs->option,
239 cs->value);
240 cs->rewrite = GNUNET_YES;
241 }
242 if (cs->rewrite)
243 {
244 char *cfg_fn = NULL;
245
246 if (NULL == out)
247 out = GNUNET_CONFIGURATION_dup (cfg);
248
249 if (NULL == cfgfile)
250 {
251 const char *xdg = getenv ("XDG_CONFIG_HOME");
252
253 if (NULL != xdg)
254 GNUNET_asprintf (&cfg_fn,
255 "%s%s%s",
256 xdg,
257 DIR_SEPARATOR_STR,
258 GNUNET_OS_project_data_get ()->config_file);
259 else
260 cfg_fn = GNUNET_strdup (
261 GNUNET_OS_project_data_get ()->user_config_file);
262 cfgfile = cfg_fn;
263 }
264
265 if (! cs->full)
266 {
267 struct GNUNET_CONFIGURATION_Handle *def;
268
269 def = GNUNET_CONFIGURATION_create ();
270 if (GNUNET_OK !=
271 GNUNET_CONFIGURATION_load (def,
272 NULL))
273 {
274 fprintf (stderr,
275 _ ("failed to load configuration defaults"));
276 cs->global_ret = 1;
277 GNUNET_CONFIGURATION_destroy (def);
278 GNUNET_CONFIGURATION_destroy (out);
279 GNUNET_free (cfg_fn);
280 return;
281 }
282 if (GNUNET_OK !=
283 GNUNET_CONFIGURATION_write_diffs (def,
284 out,
285 cfgfile))
286 cs->global_ret = 2;
287 GNUNET_CONFIGURATION_destroy (def);
288 }
289 else
290 {
291 if (GNUNET_OK !=
292 GNUNET_CONFIGURATION_write (out,
293 cfgfile))
294 cs->global_ret = 2;
295 }
296 GNUNET_free (cfg_fn);
297 }
298 if (NULL != out)
299 GNUNET_CONFIGURATION_destroy (out);
300 if (NULL != ncfg)
301 GNUNET_CONFIGURATION_destroy (ncfg);
302}
303
304
305void
306GNUNET_CONFIGURATION_config_settings_free (
307 struct GNUNET_CONFIGURATION_ConfigSettings *cs)
308{
309 GNUNET_free (cs->option);
310 GNUNET_free (cs->section);
311 GNUNET_free (cs->value);
312 GNUNET_free (cs->backend_check);
313}
314
315
316/* end of configuration_helper.c */