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, can be NULL!)
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;
153 struct GNUNET_CONFIGURATION_Handle *def;
155 def = GNUNET_CONFIGURATION_create ();
157 GNUNET_CONFIGURATION_load (def, NULL))
160 _("failed to load configuration defaults"));
164 diff = GNUNET_CONFIGURATION_get_diff (def,
168 if ( ((! rewrite) && (NULL == section)) || list_sections)
173 _("%s or %s argument is required\n"),
181 _("The following sections are available:\n"));
182 GNUNET_CONFIGURATION_iterate_sections (cfg,
189 if ( (NULL != section) && (NULL == value) )
193 GNUNET_CONFIGURATION_iterate_section_values (cfg,
203 GNUNET_CONFIGURATION_get_value_filename (cfg,
208 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
217 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
219 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
225 fprintf (stdout, "%s\n", value);
228 else if (NULL != section)
232 fprintf (stderr, _("--option argument required to set value\n"));
236 out = GNUNET_CONFIGURATION_dup (cfg);
237 GNUNET_CONFIGURATION_set_value_string (out,
245 const char *xdg = getenv ("XDG_CONFIG_HOME");
247 GNUNET_asprintf (&cfg_fn,
251 GNUNET_OS_project_data_get ()->config_file);
253 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
256 if ( (NULL != diff) || (NULL != out) )
259 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
263 GNUNET_free_non_null (cfg_fn);
265 GNUNET_CONFIGURATION_destroy (out);
268 GNUNET_CONFIGURATION_destroy (diff);
273 * Program to manipulate configuration files.
275 * @param argc number of arguments from the command line
276 * @param argv command line arguments
277 * @return 0 ok, 1 on error
283 struct GNUNET_GETOPT_CommandLineOption options[] = {
284 GNUNET_GETOPT_option_flag ('f',
286 gettext_noop ("obtain option of value as a filename (with $-expansion)"),
288 GNUNET_GETOPT_option_string ('s',
291 gettext_noop ("name of the section to access"),
293 GNUNET_GETOPT_option_string ('o',
296 gettext_noop ("name of the option to access"),
298 GNUNET_GETOPT_option_string ('V',
301 gettext_noop ("value to set"),
303 GNUNET_GETOPT_option_flag ('S',
305 gettext_noop ("print available configuration sections"),
307 GNUNET_GETOPT_option_flag ('w',
309 gettext_noop ("write configuration file that only contains delta to defaults"),
311 GNUNET_GETOPT_OPTION_END
314 GNUNET_STRINGS_get_utf8_args (argc, argv,
319 GNUNET_PROGRAM_run (argc,
321 "gnunet-config [OPTIONS]",
322 gettext_noop ("Manipulate GNUnet configuration files"),
324 &run, NULL)) ? 0 : ret;
325 GNUNET_free ((void*) argv);
329 /* end of gnunet-config.c */