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,
79 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
83 GNUNET_assert (GNUNET_OK ==
84 GNUNET_CONFIGURATION_get_value_filename (cfg,
91 GNUNET_STRINGS_filename_expand (value_fn));
104 * Print out given section name.
107 * @param section a section in the configuration file
110 print_section_name (void *cls,
121 * Main function that will be run by the scheduler.
124 * @param args remaining command-line arguments
125 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
126 * @param cfg configuration
132 const struct GNUNET_CONFIGURATION_Handle *cfg)
134 struct GNUNET_CONFIGURATION_Handle *out = NULL;
135 struct GNUNET_CONFIGURATION_Handle *diff = NULL;
141 struct GNUNET_CONFIGURATION_Handle *def;
143 def = GNUNET_CONFIGURATION_create ();
145 GNUNET_CONFIGURATION_load (def, NULL))
148 _("failed to load configuration defaults"));
152 diff = GNUNET_CONFIGURATION_get_diff (def,
156 if ( ((! rewrite) && (NULL == section)) || list_sections)
161 _("--section argument is required\n"));
164 _("The following sections are available:\n"));
165 GNUNET_CONFIGURATION_iterate_sections (cfg,
172 if ( (NULL != section) && (NULL == value) )
176 GNUNET_CONFIGURATION_iterate_section_values (cfg,
186 GNUNET_CONFIGURATION_get_value_filename (cfg,
191 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
200 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
202 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
208 fprintf (stdout, "%s\n", value);
211 else if (NULL != section)
215 fprintf (stderr, _("--option argument required to set value\n"));
219 out = GNUNET_CONFIGURATION_dup (cfg);
220 GNUNET_CONFIGURATION_set_value_string (out,
225 if ( (NULL != diff) || (NULL != out) )
228 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
233 GNUNET_CONFIGURATION_destroy (out);
236 GNUNET_CONFIGURATION_destroy (diff);
241 * Program to manipulate configuration files.
243 * @param argc number of arguments from the command line
244 * @param argv command line arguments
245 * @return 0 ok, 1 on error
251 struct GNUNET_GETOPT_CommandLineOption options[] = {
252 GNUNET_GETOPT_option_flag ('f',
254 gettext_noop ("obtain option of value as a filename (with $-expansion)"),
256 GNUNET_GETOPT_option_string ('s',
259 gettext_noop ("name of the section to access"),
261 GNUNET_GETOPT_option_string ('o',
264 gettext_noop ("name of the option to access"),
266 GNUNET_GETOPT_option_string ('V',
269 gettext_noop ("value to set"),
271 GNUNET_GETOPT_option_flag ('S',
273 gettext_noop ("print available configuration sections"),
275 GNUNET_GETOPT_option_flag ('w',
277 gettext_noop ("write configuration file that only contains delta to defaults"),
279 GNUNET_GETOPT_OPTION_END
282 GNUNET_STRINGS_get_utf8_args (argc, argv,
287 GNUNET_PROGRAM_run (argc,
289 "gnunet-config [OPTIONS]",
290 gettext_noop ("Manipulate GNUnet configuration files"),
292 &run, NULL)) ? 0 : ret;
293 GNUNET_free ((void*) argv);
297 /* end of gnunet-config.c */