reducing error messages about missing configuration options by introducing new helper...
[oweals/gnunet.git] / src / util / gnunet-config.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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  * Return value from 'main'.
52  */
53 static int ret;
54
55
56 /**
57  * Print each option in a given section.
58  *
59  * @param cls closure
60  * @param section name of the section
61  * @param option name of the option
62  * @param value value of the option
63  */
64 static void 
65 print_option (void *cls, const char *section,
66               const char *option,
67               const char *value)
68 {
69   fprintf (stdout, 
70            "%s = %s\n", option, value);
71 }
72
73
74 /**
75  * Main function that will be run by the scheduler.
76  *
77  * @param cls closure
78  * @param args remaining command-line arguments
79  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
80  * @param cfg configuration
81  */
82 static void
83 run (void *cls, char *const *args, const char *cfgfile,
84      const struct GNUNET_CONFIGURATION_Handle *cfg)
85 {
86   struct GNUNET_CONFIGURATION_Handle *out;
87
88   if (NULL == section)
89   {
90     fprintf (stderr, _("--section argument is required\n"));
91     ret = 1;
92     return;
93   }
94
95   if (NULL == value)
96   {
97     if (NULL == option)
98     {
99       GNUNET_CONFIGURATION_iterate_section_values (cfg, section,
100                                                    &print_option, NULL);
101     }
102     else
103     {
104       if (is_filename)
105       {
106         if (GNUNET_OK !=
107             GNUNET_CONFIGURATION_get_value_filename (cfg, section, option, &value))
108         {
109           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
110                                      section, option);
111           ret = 3;
112           return;
113         }
114       }
115       else
116       {
117         if (GNUNET_OK !=
118             GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
119         {
120           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
121                                      section, option);
122           ret = 3;
123           return;
124         }
125       }
126       fprintf (stdout, "%s\n", value);
127     }
128   }
129   else
130   {
131     if (NULL == option)
132     {
133       fprintf (stderr, _("--option argument required to set value\n"));
134       ret = 1;
135       return;
136     }
137     out = GNUNET_CONFIGURATION_dup (cfg);
138     GNUNET_CONFIGURATION_set_value_string (out, section, option, value);
139     if (GNUNET_OK != 
140         GNUNET_CONFIGURATION_write (out, cfgfile))
141       ret = 2;
142     GNUNET_CONFIGURATION_destroy (out);
143     return;    
144   }
145 }
146
147
148 /**
149  * Program to manipulate configuration files.
150  *
151  * @param argc number of arguments from the command line
152  * @param argv command line arguments
153  * @return 0 ok, 1 on error
154  */
155 int
156 main (int argc, char *const *argv)
157 {
158   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
159     { 'f', "filename", NULL,
160       gettext_noop ("obtain option of value as a filename (with $-expansion)"),
161       0, &GNUNET_GETOPT_set_one, &is_filename },
162     { 's', "section", "SECTION",
163       gettext_noop ("name of the section to access"),
164       1, &GNUNET_GETOPT_set_string, &section },
165     { 'o', "option", "OPTION",
166       gettext_noop ("name of the option to access"),
167       1, &GNUNET_GETOPT_set_string, &option },
168     { 'V', "value", "VALUE",
169       gettext_noop ("value to set"),
170       1, &GNUNET_GETOPT_set_string, &value },
171     GNUNET_GETOPT_OPTION_END
172   };
173
174   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
175     return 2;
176
177   return (GNUNET_OK ==
178           GNUNET_PROGRAM_run (argc, argv, "gnunet-config [OPTIONS]",
179                               gettext_noop ("Manipulate GNUnet configuration files"),
180                               options, &run, NULL)) ? 0 : ret;
181 }
182
183 /* end of gnunet-config.c */