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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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, const char *section,
80 "%s = %s\n", option, value);
85 * Print out given section name.
88 * @param section a section in the configuration file
91 print_section_name (void *cls,
94 fprintf (stdout, "%s\n", section);
99 * Main function that will be run by the scheduler.
102 * @param args remaining command-line arguments
103 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
104 * @param cfg configuration
110 const struct GNUNET_CONFIGURATION_Handle *cfg)
112 struct GNUNET_CONFIGURATION_Handle *out = NULL;
113 struct GNUNET_CONFIGURATION_Handle *diff = NULL;
117 struct GNUNET_CONFIGURATION_Handle *def;
119 def = GNUNET_CONFIGURATION_create ();
121 GNUNET_CONFIGURATION_load (def, NULL))
124 _("failed to load configuration defaults"));
128 diff = GNUNET_CONFIGURATION_get_diff (def,
132 if ( ((! rewrite) && (NULL == section)) || list_sections)
137 _("--section argument is required\n"));
140 _("The following sections are available:\n"));
141 GNUNET_CONFIGURATION_iterate_sections (cfg,
148 if ( (NULL != section) && (NULL == value) )
152 GNUNET_CONFIGURATION_iterate_section_values (cfg,
162 GNUNET_CONFIGURATION_get_value_filename (cfg,
167 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
176 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
178 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
184 fprintf (stdout, "%s\n", value);
187 else if (NULL != section)
191 fprintf (stderr, _("--option argument required to set value\n"));
195 out = GNUNET_CONFIGURATION_dup (cfg);
196 GNUNET_CONFIGURATION_set_value_string (out,
201 if ( (NULL != diff) || (NULL != out) )
204 GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
209 GNUNET_CONFIGURATION_destroy (out);
212 GNUNET_CONFIGURATION_destroy (diff);
217 * Program to manipulate configuration files.
219 * @param argc number of arguments from the command line
220 * @param argv command line arguments
221 * @return 0 ok, 1 on error
224 main (int argc, char *const *argv)
226 struct GNUNET_GETOPT_CommandLineOption options[] = {
227 GNUNET_GETOPT_OPTION_SET_ONE ('f',
229 gettext_noop ("obtain option of value as a filename (with $-expansion)"),
231 GNUNET_GETOPT_OPTION_STRING ('s',
234 gettext_noop ("name of the section to access"),
236 GNUNET_GETOPT_OPTION_STRING ('o',
239 gettext_noop ("name of the option to access"),
241 GNUNET_GETOPT_OPTION_STRING ('V',
244 gettext_noop ("value to set"),
246 GNUNET_GETOPT_OPTION_SET_ONE ('S',
248 gettext_noop ("print available configuration sections"),
250 GNUNET_GETOPT_OPTION_SET_ONE ('w',
252 gettext_noop ("write configuration file that only contains delta to defaults"),
254 GNUNET_GETOPT_OPTION_END
257 GNUNET_STRINGS_get_utf8_args (argc, argv,
262 GNUNET_PROGRAM_run (argc,
264 "gnunet-config [OPTIONS]",
265 gettext_noop ("Manipulate GNUnet configuration files"),
267 &run, NULL)) ? 0 : ret;
268 GNUNET_free ((void*) argv);
272 /* end of gnunet-config.c */