aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-config.c')
-rw-r--r--src/util/gnunet-config.c318
1 files changed, 28 insertions, 290 deletions
diff --git a/src/util/gnunet-config.c b/src/util/gnunet-config.c
index 11682daea..a195d6014 100644
--- a/src/util/gnunet-config.c
+++ b/src/util/gnunet-config.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V. 3 Copyright (C) 2012-2021 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -28,51 +28,12 @@
28 28
29 29
30/** 30/**
31 * Name of the section
32 */
33static char *section;
34
35/**
36 * Name of the option
37 */
38static char *option;
39
40/**
41 * Value to set
42 */
43static char *value;
44
45/**
46 * Backend to check if the respective plugin is 31 * Backend to check if the respective plugin is
47 * loadable. NULL if no check is to be performed. 32 * loadable. NULL if no check is to be performed.
48 * The value is the "basename" of the plugin to load. 33 * The value is the "basename" of the plugin to load.
49 */ 34 */
50static char *backend_check; 35static char *backend_check;
51 36
52/**
53 * Treat option as a filename.
54 */
55static int is_filename;
56
57/**
58 * Whether to show the sections.
59 */
60static int list_sections;
61
62/**
63 * Return value from 'main'.
64 */
65static int global_ret;
66
67/**
68 * Should we write out the configuration file, even if no value was changed?
69 */
70static int rewrite;
71
72/**
73 * Should the generated configuration file contain the whole configuration?
74 */
75static int full;
76 37
77/** 38/**
78 * If printing the value of CFLAGS has been requested. 39 * If printing the value of CFLAGS has been requested.
@@ -92,64 +53,11 @@ static int prefix;
92 53
93/** 54/**
94 * Print each option in a given section. 55 * Print each option in a given section.
56 * Main task to run to perform operations typical for
57 * gnunet-config as per the configuration settings
58 * given in @a cls.
95 * 59 *
96 * @param cls closure 60 * @param cls closure with the `struct GNUNET_CONFIGURATION_ConfigSettings`
97 * @param section name of the section
98 * @param option name of the option
99 * @param value value of the option
100 */
101static void
102print_option (void *cls,
103 const char *section,
104 const char *option,
105 const char *value)
106{
107 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
108
109 (void) section;
110 if (is_filename)
111 {
112 char *value_fn;
113 char *fn;
114
115 GNUNET_assert (GNUNET_OK ==
116 GNUNET_CONFIGURATION_get_value_filename (cfg,
117 section,
118 option,
119 &value_fn));
120 fn = GNUNET_STRINGS_filename_expand (value_fn);
121 if (NULL == fn)
122 fn = value_fn;
123 else
124 GNUNET_free (value_fn);
125 fprintf (stdout, "%s = %s\n", option, fn);
126 GNUNET_free (fn);
127 }
128 else
129 {
130 fprintf (stdout, "%s = %s\n", option, value);
131 }
132}
133
134
135/**
136 * Print out given section name.
137 *
138 * @param cls unused
139 * @param section a section in the configuration file
140 */
141static void
142print_section_name (void *cls, const char *section)
143{
144 (void) cls;
145 fprintf (stdout, "%s\n", section);
146}
147
148
149/**
150 * Main function that will be run by the scheduler.
151 *
152 * @param cls closure
153 * @param args remaining command-line arguments 61 * @param args remaining command-line arguments
154 * @param cfgfile name of the configuration file used (for saving, 62 * @param cfgfile name of the configuration file used (for saving,
155 * can be NULL!) 63 * can be NULL!)
@@ -161,10 +69,8 @@ run (void *cls,
161 const char *cfgfile, 69 const char *cfgfile,
162 const struct GNUNET_CONFIGURATION_Handle *cfg) 70 const struct GNUNET_CONFIGURATION_Handle *cfg)
163{ 71{
164 struct GNUNET_CONFIGURATION_Handle *out = NULL; 72 struct GNUNET_CONFIGURATION_ConfigSettings *cs = cls;
165 73
166 (void) cls;
167 (void) args;
168 if (1 == cflags || 1 == libs || 1 == prefix) 74 if (1 == cflags || 1 == libs || 1 == prefix)
169 { 75 {
170 char *prefixdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX); 76 char *prefixdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
@@ -192,153 +98,15 @@ run (void *cls,
192 GNUNET_asprintf (&name, 98 GNUNET_asprintf (&name,
193 "libgnunet_plugin_%s", 99 "libgnunet_plugin_%s",
194 backend_check); 100 backend_check);
195 global_ret = (GNUNET_OK == 101 cs->global_ret = (GNUNET_OK ==
196 GNUNET_PLUGIN_test (name)) ? 0 : 77; 102 GNUNET_PLUGIN_test (name)) ? 0 : 77;
197 GNUNET_free (name); 103 GNUNET_free (name);
198 return; 104 return;
199 } 105 }
200 if (full) 106 GNUNET_CONFIGURATION_config_tool_run (cs,
201 rewrite = GNUNET_YES; 107 args,
202 if (list_sections) 108 cfgfile,
203 { 109 cfg);
204 fprintf (stderr,
205 _ ("The following sections are available:\n"));
206 GNUNET_CONFIGURATION_iterate_sections (cfg,
207 &print_section_name,
208 NULL);
209 return;
210 }
211 if ( (! rewrite) &&
212 (NULL == section) )
213 {
214 fprintf (stderr,
215 _ ("%s or %s argument is required\n"),
216 "--section",
217 "--list-sections");
218 global_ret = 1;
219 return;
220 }
221
222 if ( (NULL != section) &&
223 (NULL == value) )
224 {
225 if (NULL == option)
226 {
227 GNUNET_CONFIGURATION_iterate_section_values (cfg,
228 section,
229 &print_option,
230 (void *) cfg);
231 }
232 else
233 {
234 if (is_filename)
235 {
236 if (GNUNET_OK !=
237 GNUNET_CONFIGURATION_get_value_filename (cfg,
238 section,
239 option,
240 &value))
241 {
242 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
243 section,
244 option);
245 global_ret = 3;
246 return;
247 }
248 }
249 else
250 {
251 if (GNUNET_OK !=
252 GNUNET_CONFIGURATION_get_value_string (cfg,
253 section,
254 option,
255 &value))
256 {
257 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
258 section,
259 option);
260 global_ret = 3;
261 return;
262 }
263 }
264 fprintf (stdout,
265 "%s\n",
266 value);
267 }
268 }
269 else if (NULL != section)
270 {
271 if (NULL == option)
272 {
273 fprintf (stderr,
274 _ ("--option argument required to set value\n"));
275 global_ret = 1;
276 return;
277 }
278 out = GNUNET_CONFIGURATION_dup (cfg);
279 GNUNET_CONFIGURATION_set_value_string (out,
280 section,
281 option,
282 value);
283 rewrite = GNUNET_YES;
284 }
285 if (rewrite)
286 {
287 char *cfg_fn = NULL;
288
289 if (NULL == out)
290 out = GNUNET_CONFIGURATION_dup (cfg);
291
292 if (NULL == cfgfile)
293 {
294 const char *xdg = getenv ("XDG_CONFIG_HOME");
295 if (NULL != xdg)
296 GNUNET_asprintf (&cfg_fn,
297 "%s%s%s",
298 xdg,
299 DIR_SEPARATOR_STR,
300 GNUNET_OS_project_data_get ()->config_file);
301 else
302 cfg_fn = GNUNET_strdup (
303 GNUNET_OS_project_data_get ()->user_config_file);
304 cfgfile = cfg_fn;
305 }
306
307 if (! full)
308 {
309 struct GNUNET_CONFIGURATION_Handle *def;
310
311 def = GNUNET_CONFIGURATION_create ();
312 if (GNUNET_OK !=
313 GNUNET_CONFIGURATION_load (def,
314 NULL))
315 {
316 fprintf (stderr,
317 _ ("failed to load configuration defaults"));
318 global_ret = 1;
319 GNUNET_CONFIGURATION_destroy (def);
320 GNUNET_CONFIGURATION_destroy (out);
321 GNUNET_free (cfg_fn);
322 return;
323 }
324 if (GNUNET_OK !=
325 GNUNET_CONFIGURATION_write_diffs (def,
326 out,
327 cfgfile))
328 global_ret = 2;
329 GNUNET_CONFIGURATION_destroy (def);
330 }
331 else
332 {
333 if (GNUNET_OK !=
334 GNUNET_CONFIGURATION_write (out,
335 cfgfile))
336 global_ret = 2;
337 }
338 GNUNET_free (cfg_fn);
339 }
340 if (NULL != out)
341 GNUNET_CONFIGURATION_destroy (out);
342} 110}
343 111
344 112
@@ -350,8 +118,13 @@ run (void *cls,
350 * @return 0 ok, 1 on error 118 * @return 0 ok, 1 on error
351 */ 119 */
352int 120int
353main (int argc, char *const *argv) 121main (int argc,
122 char *const *argv)
354{ 123{
124 struct GNUNET_CONFIGURATION_ConfigSettings cs = {
125 .api_version = GNUNET_UTIL_VERSION,
126 .global_ret = EXIT_SUCCESS
127 };
355 struct GNUNET_GETOPT_CommandLineOption options[] = { 128 struct GNUNET_GETOPT_CommandLineOption options[] = {
356 GNUNET_GETOPT_option_exclusive ( 129 GNUNET_GETOPT_option_exclusive (
357 GNUNET_GETOPT_option_string ( 130 GNUNET_GETOPT_option_string (
@@ -361,44 +134,6 @@ main (int argc, char *const *argv)
361 gettext_noop ( 134 gettext_noop (
362 "test if the current installation supports the specified BACKEND"), 135 "test if the current installation supports the specified BACKEND"),
363 &backend_check)), 136 &backend_check)),
364 GNUNET_GETOPT_option_flag (
365 'F',
366 "full",
367 gettext_noop (
368 "write the full configuration file, including default values"),
369 &full),
370 GNUNET_GETOPT_option_flag (
371 'f',
372 "filename",
373 gettext_noop ("interpret option value as a filename (with $-expansion)"),
374 &is_filename),
375 GNUNET_GETOPT_option_string ('o',
376 "option",
377 "OPTION",
378 gettext_noop ("name of the option to access"),
379 &option),
380 GNUNET_GETOPT_option_flag (
381 'r',
382 "rewrite",
383 gettext_noop (
384 "rewrite the configuration file, even if nothing changed"),
385 &rewrite),
386 GNUNET_GETOPT_option_flag ('S',
387 "list-sections",
388 gettext_noop (
389 "print available configuration sections"),
390 &list_sections),
391 GNUNET_GETOPT_option_string ('s',
392 "section",
393 "SECTION",
394 gettext_noop (
395 "name of the section to access"),
396 &section),
397 GNUNET_GETOPT_option_string ('V',
398 "value",
399 "VALUE",
400 gettext_noop ("value to set"),
401 &value),
402 GNUNET_GETOPT_option_flag ('C', 137 GNUNET_GETOPT_option_flag ('C',
403 "cflags", 138 "cflags",
404 gettext_noop ( 139 gettext_noop (
@@ -414,15 +149,15 @@ main (int argc, char *const *argv)
414 gettext_noop ( 149 gettext_noop (
415 "Provide the path under which GNUnet was installed"), 150 "Provide the path under which GNUnet was installed"),
416 &prefix), 151 &prefix),
152 GNUNET_CONFIGURATION_CONFIG_OPTIONS (&cs),
417 GNUNET_GETOPT_OPTION_END 153 GNUNET_GETOPT_OPTION_END
418 }; 154 };
419 int ret; 155 enum GNUNET_GenericReturnValue ret;
420 156
421 if (GNUNET_OK != 157 if (GNUNET_OK !=
422 GNUNET_STRINGS_get_utf8_args (argc, argv, 158 GNUNET_STRINGS_get_utf8_args (argc, argv,
423 &argc, &argv)) 159 &argc, &argv))
424 return 2; 160 return EXIT_FAILURE;
425
426 ret = 161 ret =
427 GNUNET_PROGRAM_run (argc, 162 GNUNET_PROGRAM_run (argc,
428 argv, 163 argv,
@@ -430,11 +165,14 @@ main (int argc, char *const *argv)
430 gettext_noop ("Manipulate GNUnet configuration files"), 165 gettext_noop ("Manipulate GNUnet configuration files"),
431 options, 166 options,
432 &run, 167 &run,
433 NULL); 168 &cs);
434 GNUNET_free_nz ((void *) argv); 169 GNUNET_free_nz ((void *) argv);
435 if (GNUNET_OK == ret) 170 GNUNET_CONFIGURATION_config_settings_free (&cs);
436 return global_ret; 171 if (GNUNET_NO == ret)
437 return ret; 172 return 0;
173 if (GNUNET_SYSERR == ret)
174 return EXIT_INVALIDARGUMENT;
175 return cs.global_ret;
438} 176}
439 177
440 178