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