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