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'.
62 * Print each option in a given section.
65 * @param section name of the section
66 * @param option name of the option
67 * @param value value of the option
70 print_option (void *cls, const char *section,
75 "%s = %s\n", option, value);
80 * Print out given section name.
83 * @param section a section in the configuration file
86 print_section_name (void *cls,
89 fprintf (stdout, "%s\n", section);
94 * Main function that will be run by the scheduler.
97 * @param args remaining command-line arguments
98 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
99 * @param cfg configuration
102 run (void *cls, char *const *args, const char *cfgfile,
103 const struct GNUNET_CONFIGURATION_Handle *cfg)
105 struct GNUNET_CONFIGURATION_Handle *out;
107 if (NULL == section || list_sections)
111 fprintf (stderr, _("--section argument is required\n"));
113 fprintf (stderr, _("The following sections are available:\n"));
114 GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL);
123 GNUNET_CONFIGURATION_iterate_section_values (cfg, section,
124 &print_option, NULL);
131 GNUNET_CONFIGURATION_get_value_filename (cfg, section, option, &value))
133 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
142 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
144 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
150 fprintf (stdout, "%s\n", value);
157 fprintf (stderr, _("--option argument required to set value\n"));
161 out = GNUNET_CONFIGURATION_dup (cfg);
162 GNUNET_CONFIGURATION_set_value_string (out, section, option, value);
164 GNUNET_CONFIGURATION_write (out, cfgfile))
166 GNUNET_CONFIGURATION_destroy (out);
173 * Program to manipulate configuration files.
175 * @param argc number of arguments from the command line
176 * @param argv command line arguments
177 * @return 0 ok, 1 on error
180 main (int argc, char *const *argv)
182 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
183 { 'f', "filename", NULL,
184 gettext_noop ("obtain option of value as a filename (with $-expansion)"),
185 0, &GNUNET_GETOPT_set_one, &is_filename },
186 { 's', "section", "SECTION",
187 gettext_noop ("name of the section to access"),
188 1, &GNUNET_GETOPT_set_string, §ion },
189 { 'o', "option", "OPTION",
190 gettext_noop ("name of the option to access"),
191 1, &GNUNET_GETOPT_set_string, &option },
192 { 'V', "value", "VALUE",
193 gettext_noop ("value to set"),
194 1, &GNUNET_GETOPT_set_string, &value },
195 { 'S', "list-sections", NULL,
196 gettext_noop ("print available configuration sections"),
197 0, &GNUNET_GETOPT_set_one, &list_sections },
198 GNUNET_GETOPT_OPTION_END
200 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
204 GNUNET_PROGRAM_run (argc, argv, "gnunet-config [OPTIONS]",
205 gettext_noop ("Manipulate GNUnet configuration files"),
206 options, &run, NULL)) ? 0 : ret;
207 GNUNET_free ((void*) argv);
211 /* end of gnunet-config.c */