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/>.
20 * @file util/gnunet-config.c
21 * @brief tool to access and manipulate GNUnet configuration files
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
44 * Treat option as a filename.
46 static int is_filename;
49 * Whether to show the sections.
51 static int list_sections;
54 * Return value from 'main'.
59 * Should we generate a configuration file that is clean and
60 * only contains the deltas to the defaults?
65 * Print each option in a given section.
68 * @param section name of the section
69 * @param option name of the option
70 * @param value value of the option
73 print_option (void *cls,
78 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
86 GNUNET_assert (GNUNET_OK ==
87 GNUNET_CONFIGURATION_get_value_filename (cfg,
91 fn = GNUNET_STRINGS_filename_expand (value_fn);
95 GNUNET_free (value_fn);
113 * Print out given section name.
116 * @param section a section in the configuration file
119 print_section_name (void *cls,
130 * Main function that will be run by the scheduler.
133 * @param args remaining command-line arguments
134 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
135 * @param cfg configuration
141 const struct GNUNET_CONFIGURATION_Handle *cfg)
143 struct GNUNET_CONFIGURATION_Handle *out = NULL;
144 struct GNUNET_CONFIGURATION_Handle *diff = NULL;
150 struct GNUNET_CONFIGURATION_Handle *def;
152 def = GNUNET_CONFIGURATION_create ();
154 GNUNET_CONFIGURATION_load (def, NULL))
157 _("failed to load configuration defaults"));
161 diff = GNUNET_CONFIGURATION_get_diff (def,
165 if ( ((! rewrite) && (NULL == section)) || list_sections)
170 _("%s or %s argument is required\n"),
178 _("The following sections are available:\n"));
179 GNUNET_CONFIGURATION_iterate_sections (cfg,
186 if ( (NULL != section) && (NULL == value) )
190 GNUNET_CONFIGURATION_iterate_section_values (cfg,
200 GNUNET_CONFIGURATION_get_value_filename (cfg,
205 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
214 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
216 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
222 fprintf (stdout, "%s\n", value);
225 else if (NULL != section)
229 fprintf (stderr, _("--option argument required to set value\n"));
233 out = GNUNET_CONFIGURATION_dup (cfg);
234 GNUNET_CONFIGURATION_set_value_string (out,
239 if ( (NULL != diff) || (NULL != out) )
242 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
247 GNUNET_CONFIGURATION_destroy (out);
250 GNUNET_CONFIGURATION_destroy (diff);
255 * Program to manipulate configuration files.
257 * @param argc number of arguments from the command line
258 * @param argv command line arguments
259 * @return 0 ok, 1 on error
265 struct GNUNET_GETOPT_CommandLineOption options[] = {
266 GNUNET_GETOPT_option_flag ('f',
268 gettext_noop ("obtain option of value as a filename (with $-expansion)"),
270 GNUNET_GETOPT_option_string ('s',
273 gettext_noop ("name of the section to access"),
275 GNUNET_GETOPT_option_string ('o',
278 gettext_noop ("name of the option to access"),
280 GNUNET_GETOPT_option_string ('V',
283 gettext_noop ("value to set"),
285 GNUNET_GETOPT_option_flag ('S',
287 gettext_noop ("print available configuration sections"),
289 GNUNET_GETOPT_option_flag ('w',
291 gettext_noop ("write configuration file that only contains delta to defaults"),
293 GNUNET_GETOPT_OPTION_END
296 GNUNET_STRINGS_get_utf8_args (argc, argv,
301 GNUNET_PROGRAM_run (argc,
303 "gnunet-config [OPTIONS]",
304 gettext_noop ("Manipulate GNUnet configuration files"),
306 &run, NULL)) ? 0 : ret;
307 GNUNET_free ((void*) argv);
311 /* end of gnunet-config.c */