2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/gnunet-config.c
23 * @brief tool to access and manipulate GNUnet configuration files
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
46 * Backend to check if the respective plugin is
47 * loadable. NULL if no check is to be performed.
48 * The value is the "basename" of the plugin to load.
50 static char *backend_check;
53 * Treat option as a filename.
55 static int is_filename;
58 * Whether to show the sections.
60 static int list_sections;
63 * Return value from 'main'.
65 static int global_ret;
68 * Should we generate a configuration file that is clean and
69 * only contains the deltas to the defaults?
75 * Print each option in a given section.
78 * @param section name of the section
79 * @param option name of the option
80 * @param value value of the option
83 print_option (void *cls,
88 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
96 GNUNET_assert (GNUNET_OK ==
97 GNUNET_CONFIGURATION_get_value_filename (cfg,
101 fn = GNUNET_STRINGS_filename_expand (value_fn);
105 GNUNET_free (value_fn);
106 fprintf (stdout, "%s = %s\n", option, fn);
111 fprintf (stdout, "%s = %s\n", option, value);
117 * Print out given section name.
120 * @param section a section in the configuration file
123 print_section_name (void *cls, const char *section)
126 fprintf (stdout, "%s\n", section);
131 * Main function that will be run by the scheduler.
134 * @param args remaining command-line arguments
135 * @param cfgfile name of the configuration file used (for saving,
137 * @param cfg configuration
143 const struct GNUNET_CONFIGURATION_Handle *cfg)
145 struct GNUNET_CONFIGURATION_Handle *out = NULL;
146 struct GNUNET_CONFIGURATION_Handle *diff = NULL;
151 if (NULL != backend_check)
155 GNUNET_asprintf (&name, "libgnunet_plugin_%s", backend_check);
156 global_ret = (GNUNET_OK == GNUNET_PLUGIN_test (name)) ? 0 : 77;
162 struct GNUNET_CONFIGURATION_Handle *def;
164 def = GNUNET_CONFIGURATION_create ();
165 if (GNUNET_OK != GNUNET_CONFIGURATION_load (def, NULL))
167 fprintf (stderr, _ ("failed to load configuration defaults"));
171 diff = GNUNET_CONFIGURATION_get_diff (def, cfg);
174 if (((! rewrite) && (NULL == section)) || list_sections)
179 _ ("%s or %s argument is required\n"),
186 fprintf (stderr, _ ("The following sections are available:\n"));
187 GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL);
192 if ((NULL != section) && (NULL == value))
196 GNUNET_CONFIGURATION_iterate_section_values (cfg,
205 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
210 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option);
217 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
222 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option);
227 fprintf (stdout, "%s\n", value);
230 else if (NULL != section)
234 fprintf (stderr, _ ("--option argument required to set value\n"));
238 out = GNUNET_CONFIGURATION_dup (cfg);
239 GNUNET_CONFIGURATION_set_value_string (out, section, option, value);
244 const char *xdg = getenv ("XDG_CONFIG_HOME");
246 GNUNET_asprintf (&cfg_fn,
250 GNUNET_OS_project_data_get ()->config_file);
252 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
255 if ((NULL != diff) || (NULL != out))
258 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, cfgfile))
261 GNUNET_free_non_null (cfg_fn);
263 GNUNET_CONFIGURATION_destroy (out);
266 GNUNET_CONFIGURATION_destroy (diff);
271 * Program to manipulate configuration files.
273 * @param argc number of arguments from the command line
274 * @param argv command line arguments
275 * @return 0 ok, 1 on error
278 main (int argc, char *const *argv)
280 struct GNUNET_GETOPT_CommandLineOption options[] =
281 { GNUNET_GETOPT_option_flag (
284 gettext_noop ("interpret option value as a filename (with $-expansion)"),
286 GNUNET_GETOPT_option_exclusive (GNUNET_GETOPT_option_string (
291 "test if the current installation supports the specified BACKEND"),
293 GNUNET_GETOPT_option_string ('s',
297 "name of the section to access"),
299 GNUNET_GETOPT_option_string ('o',
302 gettext_noop ("name of the option to access"),
304 GNUNET_GETOPT_option_string ('V',
307 gettext_noop ("value to set"),
309 GNUNET_GETOPT_option_flag ('S',
312 "print available configuration sections"),
314 GNUNET_GETOPT_option_flag (
318 "write configuration file that only contains delta to defaults"),
320 GNUNET_GETOPT_OPTION_END };
323 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
327 GNUNET_PROGRAM_run (argc,
329 "gnunet-config [OPTIONS]",
330 gettext_noop ("Manipulate GNUnet configuration files"),
334 GNUNET_free ((void *) argv);
335 if (GNUNET_OK == ret)
341 /* end of gnunet-config.c */