avoid failing hard if 'gnunetcheck' db does not exist
[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, can be NULL!)
137  * @param cfg configuration
138  */
139 static void
140 run (void *cls,
141      char *const *args,
142      const char *cfgfile,
143      const struct GNUNET_CONFIGURATION_Handle *cfg)
144 {
145   struct GNUNET_CONFIGURATION_Handle *out = NULL;
146   struct GNUNET_CONFIGURATION_Handle *diff = NULL;
147   char *cfg_fn;
148
149   (void) cls;
150   (void) args;
151   if (rewrite)
152   {
153     struct GNUNET_CONFIGURATION_Handle *def;
154
155     def = GNUNET_CONFIGURATION_create ();
156     if (GNUNET_OK !=
157         GNUNET_CONFIGURATION_load (def, NULL))
158     {
159       fprintf (stderr,
160                _("failed to load configuration defaults"));
161       ret = 1;
162       return;
163     }
164     diff = GNUNET_CONFIGURATION_get_diff (def,
165                                           cfg);
166     cfg = diff;
167   }
168   if ( ((! rewrite) && (NULL == section)) || list_sections)
169   {
170     if (! list_sections)
171     {
172       fprintf (stderr,
173                _("%s or %s argument is required\n"),
174                "--section",
175                "--list-sections");
176       ret = 1;
177     }
178     else
179     {
180       fprintf (stderr,
181                _("The following sections are available:\n"));
182       GNUNET_CONFIGURATION_iterate_sections (cfg,
183                                              &print_section_name,
184                                              NULL);
185     }
186     goto cleanup;
187   }
188
189   if ( (NULL != section) && (NULL == value) )
190   {
191     if (NULL == option)
192     {
193       GNUNET_CONFIGURATION_iterate_section_values (cfg,
194                                                    section,
195                                                   &print_option,
196                                                    (void *) cfg);
197     }
198     else
199     {
200       if (is_filename)
201       {
202         if (GNUNET_OK !=
203             GNUNET_CONFIGURATION_get_value_filename (cfg,
204                                                      section,
205                                                      option,
206                                                      &value))
207         {
208           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
209                                      section, option);
210           ret = 3;
211           goto cleanup;
212         }
213       }
214       else
215       {
216         if (GNUNET_OK !=
217             GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
218         {
219           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
220                                      section, option);
221           ret = 3;
222           goto cleanup;
223         }
224       }
225       fprintf (stdout, "%s\n", value);
226     }
227   }
228   else if (NULL != section)
229   {
230     if (NULL == option)
231     {
232       fprintf (stderr, _("--option argument required to set value\n"));
233       ret = 1;
234       goto cleanup;
235     }
236     out = GNUNET_CONFIGURATION_dup (cfg);
237     GNUNET_CONFIGURATION_set_value_string (out,
238                                            section,
239                                            option,
240                                            value);
241   }
242   cfg_fn = NULL;
243   if (NULL == cfgfile)
244   {
245     const char *xdg = getenv ("XDG_CONFIG_HOME");
246     if (NULL != xdg)
247       GNUNET_asprintf (&cfg_fn,
248                        "%s%s%s",
249                        xdg,
250                        DIR_SEPARATOR_STR,
251                        GNUNET_OS_project_data_get ()->config_file);
252     else
253       cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
254     cfgfile = cfg_fn;
255   }
256   if ( (NULL != diff) || (NULL != out) )
257   {
258     if (GNUNET_OK !=
259         GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
260                                     cfgfile))
261       ret = 2;
262   }
263   GNUNET_free_non_null (cfg_fn);
264   if (NULL != out)
265     GNUNET_CONFIGURATION_destroy (out);
266  cleanup:
267   if (NULL != diff)
268     GNUNET_CONFIGURATION_destroy (diff);
269 }
270
271
272 /**
273  * Program to manipulate configuration files.
274  *
275  * @param argc number of arguments from the command line
276  * @param argv command line arguments
277  * @return 0 ok, 1 on error
278  */
279 int
280 main (int argc,
281       char *const *argv)
282 {
283   struct GNUNET_GETOPT_CommandLineOption options[] = {
284     GNUNET_GETOPT_option_flag ('f',
285                                "filename",
286                                gettext_noop ("obtain option of value as a filename (with $-expansion)"),
287                                &is_filename),
288     GNUNET_GETOPT_option_string ('s',
289                                  "section",
290                                  "SECTION",
291                                  gettext_noop ("name of the section to access"),
292                                  &section),
293     GNUNET_GETOPT_option_string ('o',
294                                  "option",
295                                  "OPTION",
296                                  gettext_noop ("name of the option to access"),
297                                  &option),
298     GNUNET_GETOPT_option_string ('V',
299                                  "value",
300                                  "VALUE",
301                                  gettext_noop ("value to set"),
302                                  &value),
303     GNUNET_GETOPT_option_flag ('S',
304                                "list-sections",
305                                gettext_noop ("print available configuration sections"),
306                                &list_sections),
307     GNUNET_GETOPT_option_flag ('w',
308                                "rewrite",
309                                gettext_noop ("write configuration file that only contains delta to defaults"),
310                                &rewrite),
311     GNUNET_GETOPT_OPTION_END
312   };
313   if (GNUNET_OK !=
314       GNUNET_STRINGS_get_utf8_args (argc, argv,
315                                     &argc, &argv))
316     return 2;
317
318   ret = (GNUNET_OK ==
319          GNUNET_PROGRAM_run (argc,
320                              argv,
321                              "gnunet-config [OPTIONS]",
322                              gettext_noop ("Manipulate GNUnet configuration files"),
323                              options,
324                              &run, NULL)) ? 0 : ret;
325   GNUNET_free ((void*) argv);
326   return ret;
327 }
328
329 /* end of gnunet-config.c */