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