- ttl is deprecated, don't warn
[oweals/gnunet.git] / src / util / gnunet-config.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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  * 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 /**
62  * Print each option in a given section.
63  *
64  * @param cls closure
65  * @param section name of the section
66  * @param option name of the option
67  * @param value value of the option
68  */
69 static void
70 print_option (void *cls, const char *section,
71               const char *option,
72               const char *value)
73 {
74   fprintf (stdout,
75            "%s = %s\n", option, value);
76 }
77
78
79 /**
80  * Print out given section name.
81  *
82  * @param cls unused
83  * @param section a section in the configuration file
84  */
85 static void
86 print_section_name (void *cls,
87                     const char *section)
88 {
89   fprintf (stdout, "%s\n", section);
90 }
91
92
93 /**
94  * Main function that will be run by the scheduler.
95  *
96  * @param cls closure
97  * @param args remaining command-line arguments
98  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
99  * @param cfg configuration
100  */
101 static void
102 run (void *cls, char *const *args, const char *cfgfile,
103      const struct GNUNET_CONFIGURATION_Handle *cfg)
104 {
105   struct GNUNET_CONFIGURATION_Handle *out;
106
107   if (NULL == section || list_sections)
108   {
109     if (! list_sections)
110     {
111       fprintf (stderr, _("--section argument is required\n"));
112     }
113     fprintf (stderr, _("The following sections are available:\n"));
114     GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL);
115     ret = 1;
116     return;
117   }
118
119   if (NULL == value)
120   {
121     if (NULL == option)
122     {
123       GNUNET_CONFIGURATION_iterate_section_values (cfg, section,
124                                                    &print_option, NULL);
125     }
126     else
127     {
128       if (is_filename)
129       {
130         if (GNUNET_OK !=
131             GNUNET_CONFIGURATION_get_value_filename (cfg, section, option, &value))
132         {
133           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
134                                      section, option);
135           ret = 3;
136           return;
137         }
138       }
139       else
140       {
141         if (GNUNET_OK !=
142             GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &value))
143         {
144           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
145                                      section, option);
146           ret = 3;
147           return;
148         }
149       }
150       fprintf (stdout, "%s\n", value);
151     }
152   }
153   else
154   {
155     if (NULL == option)
156     {
157       fprintf (stderr, _("--option argument required to set value\n"));
158       ret = 1;
159       return;
160     }
161     out = GNUNET_CONFIGURATION_dup (cfg);
162     GNUNET_CONFIGURATION_set_value_string (out, section, option, value);
163     if (GNUNET_OK !=
164         GNUNET_CONFIGURATION_write (out, cfgfile))
165       ret = 2;
166     GNUNET_CONFIGURATION_destroy (out);
167     return;
168   }
169 }
170
171
172 /**
173  * Program to manipulate configuration files.
174  *
175  * @param argc number of arguments from the command line
176  * @param argv command line arguments
177  * @return 0 ok, 1 on error
178  */
179 int
180 main (int argc, char *const *argv)
181 {
182   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
183     { 'f', "filename", NULL,
184       gettext_noop ("obtain option of value as a filename (with $-expansion)"),
185       0, &GNUNET_GETOPT_set_one, &is_filename },
186     { 's', "section", "SECTION",
187       gettext_noop ("name of the section to access"),
188       1, &GNUNET_GETOPT_set_string, &section },
189     { 'o', "option", "OPTION",
190       gettext_noop ("name of the option to access"),
191       1, &GNUNET_GETOPT_set_string, &option },
192     { 'V', "value", "VALUE",
193       gettext_noop ("value to set"),
194       1, &GNUNET_GETOPT_set_string, &value },
195     { 'S', "list-sections", NULL,
196       gettext_noop ("print available configuration sections"),
197       0, &GNUNET_GETOPT_set_one, &list_sections },
198     GNUNET_GETOPT_OPTION_END
199   };
200   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
201     return 2;
202
203   ret = (GNUNET_OK ==
204          GNUNET_PROGRAM_run (argc, argv, "gnunet-config [OPTIONS]",
205                              gettext_noop ("Manipulate GNUnet configuration files"),
206                              options, &run, NULL)) ? 0 : ret;
207   GNUNET_free ((void*) argv);
208   return ret;
209 }
210
211 /* end of gnunet-config.c */