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 * Treat option as a filename.
48 static int is_filename;
51 * Whether to show the sections.
53 static int list_sections;
56 * Return value from 'main'.
61 * Should we generate a configuration file that is clean and
62 * only contains the deltas to the defaults?
67 * Print each option in a given section.
70 * @param section name of the section
71 * @param option name of the option
72 * @param value value of the option
75 print_option (void *cls,
80 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
88 GNUNET_assert (GNUNET_OK ==
89 GNUNET_CONFIGURATION_get_value_filename (cfg,
93 fn = GNUNET_STRINGS_filename_expand (value_fn);
97 GNUNET_free (value_fn);
115 * Print out given section name.
118 * @param section a section in the configuration file
121 print_section_name (void *cls,
132 * Main function that will be run by the scheduler.
135 * @param args remaining command-line arguments
136 * @param cfgfile name of the configuration file used (for saving,
138 * @param cfg configuration
144 const struct GNUNET_CONFIGURATION_Handle *cfg)
146 struct GNUNET_CONFIGURATION_Handle *out = NULL;
147 struct GNUNET_CONFIGURATION_Handle *diff = NULL;
154 struct GNUNET_CONFIGURATION_Handle *def;
156 def = GNUNET_CONFIGURATION_create ();
158 GNUNET_CONFIGURATION_load (def, NULL))
161 _("failed to load configuration defaults"));
165 diff = GNUNET_CONFIGURATION_get_diff (def,
169 if ( ((! rewrite) && (NULL == section)) || list_sections)
174 _("%s or %s argument is required\n"),
182 _("The following sections are available:\n"));
183 GNUNET_CONFIGURATION_iterate_sections (cfg,
190 if ( (NULL != section) && (NULL == value) )
194 GNUNET_CONFIGURATION_iterate_section_values (cfg,
204 GNUNET_CONFIGURATION_get_value_filename (cfg,
209 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
218 GNUNET_CONFIGURATION_get_value_string (cfg, section,
221 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
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,
247 const char *xdg = getenv ("XDG_CONFIG_HOME");
249 GNUNET_asprintf (&cfg_fn,
253 GNUNET_OS_project_data_get ()->config_file);
255 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
258 if ( (NULL != diff) || (NULL != out) )
261 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
265 GNUNET_free_non_null (cfg_fn);
267 GNUNET_CONFIGURATION_destroy (out);
270 GNUNET_CONFIGURATION_destroy (diff);
275 * Program to manipulate configuration files.
277 * @param argc number of arguments from the command line
278 * @param argv command line arguments
279 * @return 0 ok, 1 on error
285 struct GNUNET_GETOPT_CommandLineOption options[] = {
286 GNUNET_GETOPT_option_flag ('f',
288 gettext_noop ("interpret option value as a filename (with $-expansion)"),
290 GNUNET_GETOPT_option_string ('s',
293 gettext_noop ("name of the section to access"),
295 GNUNET_GETOPT_option_string ('o',
298 gettext_noop ("name of the option to access"),
300 GNUNET_GETOPT_option_string ('V',
303 gettext_noop ("value to set"),
305 GNUNET_GETOPT_option_flag ('S',
307 gettext_noop ("print available configuration sections"),
309 GNUNET_GETOPT_option_flag ('w',
311 gettext_noop ("write configuration file that only contains delta to defaults"),
313 GNUNET_GETOPT_OPTION_END
316 GNUNET_STRINGS_get_utf8_args (argc, argv,
321 GNUNET_PROGRAM_run (argc,
323 "gnunet-config [OPTIONS]",
324 gettext_noop ("Manipulate GNUnet configuration files"),
326 &run, NULL)) ? 0 : ret;
327 GNUNET_free ((void*) argv);
331 /* end of gnunet-config.c */