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 _("--section argument is required\n"));
173 _("The following sections are available:\n"));
174 GNUNET_CONFIGURATION_iterate_sections (cfg,
181 if ( (NULL != section) && (NULL == value) )
185 GNUNET_CONFIGURATION_iterate_section_values (cfg,
195 GNUNET_CONFIGURATION_get_value_filename (cfg,
200 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
209 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
211 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
217 fprintf (stdout, "%s\n", value);
220 else if (NULL != section)
224 fprintf (stderr, _("--option argument required to set value\n"));
228 out = GNUNET_CONFIGURATION_dup (cfg);
229 GNUNET_CONFIGURATION_set_value_string (out,
234 if ( (NULL != diff) || (NULL != out) )
237 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
242 GNUNET_CONFIGURATION_destroy (out);
245 GNUNET_CONFIGURATION_destroy (diff);
250 * Program to manipulate configuration files.
252 * @param argc number of arguments from the command line
253 * @param argv command line arguments
254 * @return 0 ok, 1 on error
260 struct GNUNET_GETOPT_CommandLineOption options[] = {
261 GNUNET_GETOPT_option_flag ('f',
263 gettext_noop ("obtain option of value as a filename (with $-expansion)"),
265 GNUNET_GETOPT_option_string ('s',
268 gettext_noop ("name of the section to access"),
270 GNUNET_GETOPT_option_string ('o',
273 gettext_noop ("name of the option to access"),
275 GNUNET_GETOPT_option_string ('V',
278 gettext_noop ("value to set"),
280 GNUNET_GETOPT_option_flag ('S',
282 gettext_noop ("print available configuration sections"),
284 GNUNET_GETOPT_option_flag ('w',
286 gettext_noop ("write configuration file that only contains delta to defaults"),
288 GNUNET_GETOPT_OPTION_END
291 GNUNET_STRINGS_get_utf8_args (argc, argv,
296 GNUNET_PROGRAM_run (argc,
298 "gnunet-config [OPTIONS]",
299 gettext_noop ("Manipulate GNUnet configuration files"),
301 &run, NULL)) ? 0 : ret;
302 GNUNET_free ((void*) argv);
306 /* end of gnunet-config.c */