Merge branch 'master' of ssh://gnunet.org/gnunet
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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   (void) cls;
81   (void) section;
82   fprintf (stdout,
83            "%s = %s\n",
84            option,
85            value);
86 }
87
88
89 /**
90  * Print out given section name.
91  *
92  * @param cls unused
93  * @param section a section in the configuration file
94  */
95 static void
96 print_section_name (void *cls,
97                     const char *section)
98 {
99   (void) cls;
100   fprintf (stdout,
101            "%s\n",
102            section);
103 }
104
105
106 /**
107  * Main function that will be run by the scheduler.
108  *
109  * @param cls closure
110  * @param args remaining command-line arguments
111  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
112  * @param cfg configuration
113  */
114 static void
115 run (void *cls,
116      char *const *args,
117      const char *cfgfile,
118      const struct GNUNET_CONFIGURATION_Handle *cfg)
119 {
120   struct GNUNET_CONFIGURATION_Handle *out = NULL;
121   struct GNUNET_CONFIGURATION_Handle *diff = NULL;
122
123   (void) cls;
124   (void) args;
125   if (rewrite)
126   {
127     struct GNUNET_CONFIGURATION_Handle *def;
128
129     def = GNUNET_CONFIGURATION_create ();
130     if (GNUNET_OK !=
131         GNUNET_CONFIGURATION_load (def, NULL))
132     {
133       fprintf (stderr,
134                _("failed to load configuration defaults"));
135       ret = 1;
136       return;
137     }
138     diff = GNUNET_CONFIGURATION_get_diff (def,
139                                           cfg);
140     cfg = diff;
141   }
142   if ( ((! rewrite) && (NULL == section)) || list_sections)
143   {
144     if (! list_sections)
145     {
146       fprintf (stderr,
147                _("--section argument is required\n"));
148     }
149     fprintf (stderr,
150              _("The following sections are available:\n"));
151     GNUNET_CONFIGURATION_iterate_sections (cfg,
152                                            &print_section_name,
153                                            NULL);
154     ret = 1;
155     goto cleanup;
156   }
157
158   if ( (NULL != section) && (NULL == value) )
159   {
160     if (NULL == option)
161     {
162       GNUNET_CONFIGURATION_iterate_section_values (cfg,
163                                                    section,
164                                                    &print_option,
165                                                    NULL);
166     }
167     else
168     {
169       if (is_filename)
170       {
171         if (GNUNET_OK !=
172             GNUNET_CONFIGURATION_get_value_filename (cfg,
173                                                      section,
174                                                      option,
175                                                      &value))
176         {
177           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
178                                      section, option);
179           ret = 3;
180           goto cleanup;
181         }
182       }
183       else
184       {
185         if (GNUNET_OK !=
186             GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
187         {
188           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
189                                      section, option);
190           ret = 3;
191           goto cleanup;
192         }
193       }
194       fprintf (stdout, "%s\n", value);
195     }
196   }
197   else if (NULL != section)
198   {
199     if (NULL == option)
200     {
201       fprintf (stderr, _("--option argument required to set value\n"));
202       ret = 1;
203       goto cleanup;
204     }
205     out = GNUNET_CONFIGURATION_dup (cfg);
206     GNUNET_CONFIGURATION_set_value_string (out,
207                                            section,
208                                            option,
209                                            value);
210   }
211   if ( (NULL != diff) || (NULL != out) )
212   {
213     if (GNUNET_OK !=
214         GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out,
215                                     cfgfile))
216       ret = 2;
217   }
218   if (NULL != out)
219     GNUNET_CONFIGURATION_destroy (out);
220  cleanup:
221   if (NULL != diff)
222     GNUNET_CONFIGURATION_destroy (diff);
223 }
224
225
226 /**
227  * Program to manipulate configuration files.
228  *
229  * @param argc number of arguments from the command line
230  * @param argv command line arguments
231  * @return 0 ok, 1 on error
232  */
233 int
234 main (int argc,
235       char *const *argv)
236 {
237   struct GNUNET_GETOPT_CommandLineOption options[] = {
238     GNUNET_GETOPT_option_flag ('f',
239                                "filename",
240                                gettext_noop ("obtain option of value as a filename (with $-expansion)"),
241                                &is_filename),
242     GNUNET_GETOPT_option_string ('s',
243                                  "section",
244                                  "SECTION",
245                                  gettext_noop ("name of the section to access"),
246                                  &section),
247     GNUNET_GETOPT_option_string ('o',
248                                  "option",
249                                  "OPTION",
250                                  gettext_noop ("name of the option to access"),
251                                  &option),
252     GNUNET_GETOPT_option_string ('V',
253                                  "value",
254                                  "VALUE",
255                                  gettext_noop ("value to set"),
256                                  &value),
257     GNUNET_GETOPT_option_flag ('S',
258                                "list-sections",
259                                gettext_noop ("print available configuration sections"),
260                                &list_sections),
261     GNUNET_GETOPT_option_flag ('w',
262                                "rewrite",
263                                gettext_noop ("write configuration file that only contains delta to defaults"),
264                                &rewrite),
265     GNUNET_GETOPT_OPTION_END
266   };
267   if (GNUNET_OK !=
268       GNUNET_STRINGS_get_utf8_args (argc, argv,
269                                     &argc, &argv))
270     return 2;
271
272   ret = (GNUNET_OK ==
273          GNUNET_PROGRAM_run (argc,
274                              argv,
275                              "gnunet-config [OPTIONS]",
276                              gettext_noop ("Manipulate GNUnet configuration files"),
277                              options,
278                              &run, NULL)) ? 0 : ret;
279   GNUNET_free ((void*) argv);
280   return ret;
281 }
282
283 /* end of gnunet-config.c */