tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / gnunet-config.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file util/gnunet-config.c
23  * @brief tool to access and manipulate GNUnet configuration files
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29
30 /**
31  * Name of the section
32  */
33 static char *section;
34
35 /**
36  * Name of the option
37  */
38 static char *option;
39
40 /**
41  * Value to set
42  */
43 static char *value;
44
45 /**
46  * Treat option as a filename.
47  */
48 static int is_filename;
49
50 /**
51  * Whether to show the sections.
52  */
53 static int list_sections;
54
55 /**
56  * Return value from 'main'.
57  */
58 static int ret;
59
60 /**
61  * Should we generate a configuration file that is clean and
62  * only contains the deltas to the defaults?
63  */
64 static int rewrite;
65
66 /**
67  * Print each option in a given section.
68  *
69  * @param cls closure
70  * @param section name of the section
71  * @param option name of the option
72  * @param value value of the option
73  */
74 static void
75 print_option (void *cls,
76               const char *section,
77               const char *option,
78               const char *value)
79 {
80   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
81
82   (void) section;
83   if (is_filename)
84   {
85     char *value_fn;
86     char *fn;
87
88     GNUNET_assert (GNUNET_OK ==
89                    GNUNET_CONFIGURATION_get_value_filename (cfg,
90                                                             section,
91                                                             option,
92                                                             &value_fn));
93     fn = GNUNET_STRINGS_filename_expand (value_fn);
94     if (NULL == fn)
95       fn = value_fn;
96     else
97       GNUNET_free (value_fn);
98     fprintf (stdout,
99              "%s = %s\n",
100              option,
101              fn);
102     GNUNET_free (fn);
103   }
104   else
105   {
106     fprintf (stdout,
107              "%s = %s\n",
108              option,
109              value);
110   }
111 }
112
113
114 /**
115  * Print out given section name.
116  *
117  * @param cls unused
118  * @param section a section in the configuration file
119  */
120 static void
121 print_section_name (void *cls,
122                     const char *section)
123 {
124   (void) cls;
125   fprintf (stdout,
126            "%s\n",
127            section);
128 }
129
130
131 /**
132  * Main function that will be run by the scheduler.
133  *
134  * @param cls closure
135  * @param args remaining command-line arguments
136  * @param cfgfile name of the configuration file used (for saving,
137  *                                                     can be NULL!)
138  * @param cfg configuration
139  */
140 static void
141 run (void *cls,
142      char *const *args,
143      const char *cfgfile,
144      const struct GNUNET_CONFIGURATION_Handle *cfg)
145 {
146   struct GNUNET_CONFIGURATION_Handle *out = NULL;
147   struct GNUNET_CONFIGURATION_Handle *diff = NULL;
148   char *cfg_fn;
149
150   (void) cls;
151   (void) args;
152   if (rewrite)
153   {
154     struct GNUNET_CONFIGURATION_Handle *def;
155
156     def = GNUNET_CONFIGURATION_create ();
157     if (GNUNET_OK !=
158         GNUNET_CONFIGURATION_load (def, NULL))
159     {
160       fprintf (stderr,
161                _("failed to load configuration defaults"));
162       ret = 1;
163       return;
164     }
165     diff = GNUNET_CONFIGURATION_get_diff (def,
166                                           cfg);
167     cfg = diff;
168   }
169   if ( ((! rewrite) && (NULL == section)) || list_sections)
170   {
171     if (! list_sections)
172     {
173       fprintf (stderr,
174                _("%s or %s argument is required\n"),
175                "--section",
176                "--list-sections");
177       ret = 1;
178     }
179     else
180     {
181       fprintf (stderr,
182                _("The following sections are available:\n"));
183       GNUNET_CONFIGURATION_iterate_sections (cfg,
184                                              &print_section_name,
185                                              NULL);
186     }
187     goto cleanup;
188   }
189
190   if ( (NULL != section) && (NULL == value) )
191   {
192     if (NULL == option)
193     {
194       GNUNET_CONFIGURATION_iterate_section_values (cfg,
195                                                    section,
196                                                    &print_option,
197                                                    (void *) cfg);
198     }
199     else
200     {
201       if (is_filename)
202       {
203         if (GNUNET_OK !=
204             GNUNET_CONFIGURATION_get_value_filename (cfg,
205                                                      section,
206                                                      option,
207                                                      &value))
208         {
209           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
210                                      section, option);
211           ret = 3;
212           goto cleanup;
213         }
214       }
215       else
216       {
217         if (GNUNET_OK !=
218             GNUNET_CONFIGURATION_get_value_string (cfg, section,
219                                                    option, &value))
220         {
221           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
222                                      section, option);
223           ret = 3;
224           goto cleanup;
225         }
226       }
227       fprintf (stdout, "%s\n", value);
228     }
229   }
230   else if (NULL != section)
231   {
232     if (NULL == option)
233     {
234       fprintf (stderr, _("--option argument required to set value\n"));
235       ret = 1;
236       goto cleanup;
237     }
238     out = GNUNET_CONFIGURATION_dup (cfg);
239     GNUNET_CONFIGURATION_set_value_string (out,
240                                            section,
241                                            option,
242                                            value);
243   }
244   cfg_fn = NULL;
245   if (NULL == cfgfile)
246   {
247     const char *xdg = getenv ("XDG_CONFIG_HOME");
248     if (NULL != xdg)
249       GNUNET_asprintf (&cfg_fn,
250                        "%s%s%s",
251                        xdg,
252                        DIR_SEPARATOR_STR,
253                        GNUNET_OS_project_data_get ()->config_file);
254     else
255       cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
256     cfgfile = cfg_fn;
257   }
258   if ( (NULL != diff) || (NULL != out) )
259   {
260     if (GNUNET_OK !=
261         GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
262                                     cfgfile))
263       ret = 2;
264   }
265   GNUNET_free_non_null (cfg_fn);
266   if (NULL != out)
267     GNUNET_CONFIGURATION_destroy (out);
268 cleanup:
269   if (NULL != diff)
270     GNUNET_CONFIGURATION_destroy (diff);
271 }
272
273
274 /**
275  * Program to manipulate configuration files.
276  *
277  * @param argc number of arguments from the command line
278  * @param argv command line arguments
279  * @return 0 ok, 1 on error
280  */
281 int
282 main (int argc,
283       char *const *argv)
284 {
285   struct GNUNET_GETOPT_CommandLineOption options[] = {
286     GNUNET_GETOPT_option_flag ('f',
287       "filename",
288       gettext_noop ("interpret option value as a filename (with $-expansion)"),
289       &is_filename),
290     GNUNET_GETOPT_option_string ('s',
291                                 "section",
292                                 "SECTION",
293                                 gettext_noop ("name of the section to access"),
294                                 &section),
295     GNUNET_GETOPT_option_string ('o',
296                                  "option",
297                                  "OPTION",
298                                  gettext_noop ("name of the option to access"),
299                                  &option),
300     GNUNET_GETOPT_option_string ('V',
301                                  "value",
302                                  "VALUE",
303                                  gettext_noop ("value to set"),
304                                  &value),
305     GNUNET_GETOPT_option_flag ('S',
306                        "list-sections",
307                        gettext_noop ("print available configuration sections"),
308                        &list_sections),
309     GNUNET_GETOPT_option_flag ('w',
310                                "rewrite",
311                                gettext_noop ("write configuration file that only contains delta to defaults"),
312                                &rewrite),
313     GNUNET_GETOPT_OPTION_END
314   };
315   if (GNUNET_OK !=
316       GNUNET_STRINGS_get_utf8_args (argc, argv,
317                                     &argc, &argv))
318     return 2;
319
320   ret = (GNUNET_OK ==
321          GNUNET_PROGRAM_run (argc,
322                         argv,
323                         "gnunet-config [OPTIONS]",
324                         gettext_noop ("Manipulate GNUnet configuration files"),
325                         options,
326                         &run, NULL)) ? 0 : ret;
327   GNUNET_free ((void*) argv);
328   return ret;
329 }
330
331 /* end of gnunet-config.c */