aboutsummaryrefslogtreecommitdiff
path: root/src/util/configuration_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/configuration_helper.c')
-rw-r--r--src/util/configuration_helper.c302
1 files changed, 0 insertions, 302 deletions
diff --git a/src/util/configuration_helper.c b/src/util/configuration_helper.c
deleted file mode 100644
index 8f995ec03..000000000
--- a/src/util/configuration_helper.c
+++ /dev/null
@@ -1,302 +0,0 @@
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 (cs->diagnostics)
119 {
120 /* Re-parse the configuration with diagnostics enabled. */
121 ncfg = GNUNET_CONFIGURATION_create ();
122 GNUNET_CONFIGURATION_enable_diagnostics (ncfg);
123 GNUNET_CONFIGURATION_load (ncfg,
124 cfgfile);
125 cfg = ncfg;
126 }
127
128 if (cs->full)
129 cs->rewrite = GNUNET_YES;
130 if (cs->list_sections)
131 {
132 fprintf (stderr,
133 _ ("The following sections are available:\n"));
134 GNUNET_CONFIGURATION_iterate_sections (cfg,
135 &print_section_name,
136 NULL);
137 return;
138 }
139 if ( (! cs->rewrite) &&
140 (NULL == cs->section) )
141 {
142 char *serialization;
143
144 if (! cs->diagnostics)
145 {
146 fprintf (stderr,
147 _ ("%s, %s or %s argument is required\n"),
148 "--section",
149 "--list-sections",
150 "--diagnostics");
151 cs->global_ret = EXIT_INVALIDARGUMENT;
152 return;
153 }
154 serialization = GNUNET_CONFIGURATION_serialize_diagnostics (cfg);
155 fprintf (stdout,
156 "%s",
157 serialization);
158 GNUNET_free (serialization);
159 }
160 else if ( (NULL != cs->section) &&
161 (NULL == cs->value) )
162 {
163 if (NULL == cs->option)
164 {
165 GNUNET_CONFIGURATION_iterate_section_values (
166 cfg,
167 cs->section,
168 cs->is_filename
169 ? &print_filename_option
170 : &print_option,
171 (void *) cfg);
172 }
173 else
174 {
175 char *value;
176
177 if (cs->is_filename)
178 {
179 if (GNUNET_OK !=
180 GNUNET_CONFIGURATION_get_value_filename (cfg,
181 cs->section,
182 cs->option,
183 &value))
184 {
185 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
186 cs->section,
187 cs->option);
188 cs->global_ret = EXIT_NOTCONFIGURED;
189 return;
190 }
191 }
192 else
193 {
194 if (GNUNET_OK !=
195 GNUNET_CONFIGURATION_get_value_string (cfg,
196 cs->section,
197 cs->option,
198 &value))
199 {
200 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
201 cs->section,
202 cs->option);
203 cs->global_ret = EXIT_NOTCONFIGURED;
204 return;
205 }
206 }
207 fprintf (stdout,
208 "%s\n",
209 value);
210 GNUNET_free (value);
211 }
212 }
213 else if (NULL != cs->section)
214 {
215 if (NULL == cs->option)
216 {
217 fprintf (stderr,
218 _ ("--option argument required to set value\n"));
219 cs->global_ret = EXIT_INVALIDARGUMENT;
220 return;
221 }
222 out = GNUNET_CONFIGURATION_dup (cfg);
223 GNUNET_CONFIGURATION_set_value_string (out,
224 cs->section,
225 cs->option,
226 cs->value);
227 cs->rewrite = GNUNET_YES;
228 }
229 if (cs->rewrite)
230 {
231 char *cfg_fn = NULL;
232
233 if (NULL == out)
234 out = GNUNET_CONFIGURATION_dup (cfg);
235
236 if (NULL == cfgfile)
237 {
238 const char *xdg = getenv ("XDG_CONFIG_HOME");
239
240 if (NULL != xdg)
241 GNUNET_asprintf (&cfg_fn,
242 "%s%s%s",
243 xdg,
244 DIR_SEPARATOR_STR,
245 GNUNET_OS_project_data_get ()->config_file);
246 else
247 cfg_fn = GNUNET_strdup (
248 GNUNET_OS_project_data_get ()->user_config_file);
249 cfgfile = cfg_fn;
250 }
251
252 if (! cs->full)
253 {
254 struct GNUNET_CONFIGURATION_Handle *def;
255
256 def = GNUNET_CONFIGURATION_create ();
257 if (GNUNET_OK !=
258 GNUNET_CONFIGURATION_load (def,
259 NULL))
260 {
261 fprintf (stderr,
262 _ ("failed to load configuration defaults"));
263 cs->global_ret = 1;
264 GNUNET_CONFIGURATION_destroy (def);
265 GNUNET_CONFIGURATION_destroy (out);
266 GNUNET_free (cfg_fn);
267 return;
268 }
269 if (GNUNET_OK !=
270 GNUNET_CONFIGURATION_write_diffs (def,
271 out,
272 cfgfile))
273 cs->global_ret = 2;
274 GNUNET_CONFIGURATION_destroy (def);
275 }
276 else
277 {
278 if (GNUNET_OK !=
279 GNUNET_CONFIGURATION_write (out,
280 cfgfile))
281 cs->global_ret = 2;
282 }
283 GNUNET_free (cfg_fn);
284 }
285 if (NULL != out)
286 GNUNET_CONFIGURATION_destroy (out);
287 if (NULL != ncfg)
288 GNUNET_CONFIGURATION_destroy (ncfg);
289}
290
291
292void
293GNUNET_CONFIGURATION_config_settings_free (
294 struct GNUNET_CONFIGURATION_ConfigSettings *cs)
295{
296 GNUNET_free (cs->option);
297 GNUNET_free (cs->section);
298 GNUNET_free (cs->value);
299}
300
301
302/* end of configuration_helper.c */