diff options
Diffstat (limited to 'src/util/gnunet-config.c')
-rw-r--r-- | src/util/gnunet-config.c | 195 |
1 files changed, 102 insertions, 93 deletions
diff --git a/src/util/gnunet-config.c b/src/util/gnunet-config.c index f700428a2..3b9b64d4e 100644 --- a/src/util/gnunet-config.c +++ b/src/util/gnunet-config.c | |||
@@ -43,6 +43,13 @@ static char *option; | |||
43 | static char *value; | 43 | static char *value; |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * Backend to check if the respective plugin is | ||
47 | * loadable. NULL if no check is to be performed. | ||
48 | * The value is the "basename" of the plugin to load. | ||
49 | */ | ||
50 | static char *backend_check; | ||
51 | |||
52 | /** | ||
46 | * Treat option as a filename. | 53 | * Treat option as a filename. |
47 | */ | 54 | */ |
48 | static int is_filename; | 55 | static int is_filename; |
@@ -55,7 +62,7 @@ static int list_sections; | |||
55 | /** | 62 | /** |
56 | * Return value from 'main'. | 63 | * Return value from 'main'. |
57 | */ | 64 | */ |
58 | static int ret; | 65 | static int global_ret; |
59 | 66 | ||
60 | /** | 67 | /** |
61 | * Should we generate a configuration file that is clean and | 68 | * Should we generate a configuration file that is clean and |
@@ -63,6 +70,7 @@ static int ret; | |||
63 | */ | 70 | */ |
64 | static int rewrite; | 71 | static int rewrite; |
65 | 72 | ||
73 | |||
66 | /** | 74 | /** |
67 | * Print each option in a given section. | 75 | * Print each option in a given section. |
68 | * | 76 | * |
@@ -95,18 +103,12 @@ print_option (void *cls, | |||
95 | fn = value_fn; | 103 | fn = value_fn; |
96 | else | 104 | else |
97 | GNUNET_free (value_fn); | 105 | GNUNET_free (value_fn); |
98 | fprintf (stdout, | 106 | fprintf (stdout, "%s = %s\n", option, fn); |
99 | "%s = %s\n", | ||
100 | option, | ||
101 | fn); | ||
102 | GNUNET_free (fn); | 107 | GNUNET_free (fn); |
103 | } | 108 | } |
104 | else | 109 | else |
105 | { | 110 | { |
106 | fprintf (stdout, | 111 | fprintf (stdout, "%s = %s\n", option, value); |
107 | "%s = %s\n", | ||
108 | option, | ||
109 | value); | ||
110 | } | 112 | } |
111 | } | 113 | } |
112 | 114 | ||
@@ -118,13 +120,10 @@ print_option (void *cls, | |||
118 | * @param section a section in the configuration file | 120 | * @param section a section in the configuration file |
119 | */ | 121 | */ |
120 | static void | 122 | static void |
121 | print_section_name (void *cls, | 123 | print_section_name (void *cls, const char *section) |
122 | const char *section) | ||
123 | { | 124 | { |
124 | (void) cls; | 125 | (void) cls; |
125 | fprintf (stdout, | 126 | fprintf (stdout, "%s\n", section); |
126 | "%s\n", | ||
127 | section); | ||
128 | } | 127 | } |
129 | 128 | ||
130 | 129 | ||
@@ -149,45 +148,48 @@ run (void *cls, | |||
149 | 148 | ||
150 | (void) cls; | 149 | (void) cls; |
151 | (void) args; | 150 | (void) args; |
151 | if (NULL != backend_check) | ||
152 | { | ||
153 | char *name; | ||
154 | |||
155 | GNUNET_asprintf (&name, "libgnunet_plugin_%s", backend_check); | ||
156 | global_ret = (GNUNET_OK == GNUNET_PLUGIN_test (name)) ? 0 : 77; | ||
157 | GNUNET_free (name); | ||
158 | return; | ||
159 | } | ||
152 | if (rewrite) | 160 | if (rewrite) |
153 | { | 161 | { |
154 | struct GNUNET_CONFIGURATION_Handle *def; | 162 | struct GNUNET_CONFIGURATION_Handle *def; |
155 | 163 | ||
156 | def = GNUNET_CONFIGURATION_create (); | 164 | def = GNUNET_CONFIGURATION_create (); |
157 | if (GNUNET_OK != | 165 | if (GNUNET_OK != GNUNET_CONFIGURATION_load (def, NULL)) |
158 | GNUNET_CONFIGURATION_load (def, NULL)) | ||
159 | { | 166 | { |
160 | fprintf (stderr, | 167 | fprintf (stderr, _ ("failed to load configuration defaults")); |
161 | _("failed to load configuration defaults")); | 168 | global_ret = 1; |
162 | ret = 1; | ||
163 | return; | 169 | return; |
164 | } | 170 | } |
165 | diff = GNUNET_CONFIGURATION_get_diff (def, | 171 | diff = GNUNET_CONFIGURATION_get_diff (def, cfg); |
166 | cfg); | ||
167 | cfg = diff; | 172 | cfg = diff; |
168 | } | 173 | } |
169 | if ( ((! rewrite) && (NULL == section)) || list_sections) | 174 | if (((! rewrite) && (NULL == section)) || list_sections) |
170 | { | 175 | { |
171 | if (! list_sections) | 176 | if (! list_sections) |
172 | { | 177 | { |
173 | fprintf (stderr, | 178 | fprintf (stderr, |
174 | _("%s or %s argument is required\n"), | 179 | _ ("%s or %s argument is required\n"), |
175 | "--section", | 180 | "--section", |
176 | "--list-sections"); | 181 | "--list-sections"); |
177 | ret = 1; | 182 | global_ret = 1; |
178 | } | 183 | } |
179 | else | 184 | else |
180 | { | 185 | { |
181 | fprintf (stderr, | 186 | fprintf (stderr, _ ("The following sections are available:\n")); |
182 | _("The following sections are available:\n")); | 187 | GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL); |
183 | GNUNET_CONFIGURATION_iterate_sections (cfg, | ||
184 | &print_section_name, | ||
185 | NULL); | ||
186 | } | 188 | } |
187 | goto cleanup; | 189 | goto cleanup; |
188 | } | 190 | } |
189 | 191 | ||
190 | if ( (NULL != section) && (NULL == value) ) | 192 | if ((NULL != section) && (NULL == value)) |
191 | { | 193 | { |
192 | if (NULL == option) | 194 | if (NULL == option) |
193 | { | 195 | { |
@@ -200,27 +202,25 @@ run (void *cls, | |||
200 | { | 202 | { |
201 | if (is_filename) | 203 | if (is_filename) |
202 | { | 204 | { |
203 | if (GNUNET_OK != | 205 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, |
204 | GNUNET_CONFIGURATION_get_value_filename (cfg, | 206 | section, |
205 | section, | 207 | option, |
206 | option, | 208 | &value)) |
207 | &value)) | ||
208 | { | 209 | { |
209 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 210 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); |
210 | section, option); | 211 | global_ret = 3; |
211 | ret = 3; | ||
212 | goto cleanup; | 212 | goto cleanup; |
213 | } | 213 | } |
214 | } | 214 | } |
215 | else | 215 | else |
216 | { | 216 | { |
217 | if (GNUNET_OK != | 217 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, |
218 | GNUNET_CONFIGURATION_get_value_string (cfg, section, | 218 | section, |
219 | option, &value)) | 219 | option, |
220 | &value)) | ||
220 | { | 221 | { |
221 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | 222 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); |
222 | section, option); | 223 | global_ret = 3; |
223 | ret = 3; | ||
224 | goto cleanup; | 224 | goto cleanup; |
225 | } | 225 | } |
226 | } | 226 | } |
@@ -231,15 +231,12 @@ run (void *cls, | |||
231 | { | 231 | { |
232 | if (NULL == option) | 232 | if (NULL == option) |
233 | { | 233 | { |
234 | fprintf (stderr, _("--option argument required to set value\n")); | 234 | fprintf (stderr, _ ("--option argument required to set value\n")); |
235 | ret = 1; | 235 | global_ret = 1; |
236 | goto cleanup; | 236 | goto cleanup; |
237 | } | 237 | } |
238 | out = GNUNET_CONFIGURATION_dup (cfg); | 238 | out = GNUNET_CONFIGURATION_dup (cfg); |
239 | GNUNET_CONFIGURATION_set_value_string (out, | 239 | GNUNET_CONFIGURATION_set_value_string (out, section, option, value); |
240 | section, | ||
241 | option, | ||
242 | value); | ||
243 | } | 240 | } |
244 | cfg_fn = NULL; | 241 | cfg_fn = NULL; |
245 | if (NULL == cfgfile) | 242 | if (NULL == cfgfile) |
@@ -255,12 +252,11 @@ run (void *cls, | |||
255 | cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file); | 252 | cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file); |
256 | cfgfile = cfg_fn; | 253 | cfgfile = cfg_fn; |
257 | } | 254 | } |
258 | if ( (NULL != diff) || (NULL != out) ) | 255 | if ((NULL != diff) || (NULL != out)) |
259 | { | 256 | { |
260 | if (GNUNET_OK != | 257 | if (GNUNET_OK != |
261 | GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, | 258 | GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, cfgfile)) |
262 | cfgfile)) | 259 | global_ret = 2; |
263 | ret = 2; | ||
264 | } | 260 | } |
265 | GNUNET_free_non_null (cfg_fn); | 261 | GNUNET_free_non_null (cfg_fn); |
266 | if (NULL != out) | 262 | if (NULL != out) |
@@ -279,52 +275,65 @@ cleanup: | |||
279 | * @return 0 ok, 1 on error | 275 | * @return 0 ok, 1 on error |
280 | */ | 276 | */ |
281 | int | 277 | int |
282 | main (int argc, | 278 | main (int argc, char *const *argv) |
283 | char *const *argv) | ||
284 | { | 279 | { |
285 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 280 | struct GNUNET_GETOPT_CommandLineOption options[] = |
286 | GNUNET_GETOPT_option_flag ('f', | 281 | {GNUNET_GETOPT_option_flag ( |
287 | "filename", | 282 | 'f', |
288 | gettext_noop ("interpret option value as a filename (with $-expansion)"), | 283 | "filename", |
289 | &is_filename), | 284 | gettext_noop ("interpret option value as a filename (with $-expansion)"), |
290 | GNUNET_GETOPT_option_string ('s', | 285 | &is_filename), |
291 | "section", | 286 | GNUNET_GETOPT_option_exclusive (GNUNET_GETOPT_option_string ( |
292 | "SECTION", | 287 | 'b', |
293 | gettext_noop ("name of the section to access"), | 288 | "supported-backend", |
294 | §ion), | 289 | "BACKEND", |
295 | GNUNET_GETOPT_option_string ('o', | 290 | gettext_noop ( |
296 | "option", | 291 | "test if the current installation supports the specified BACKEND"), |
297 | "OPTION", | 292 | &backend_check)), |
298 | gettext_noop ("name of the option to access"), | 293 | GNUNET_GETOPT_option_string ('s', |
299 | &option), | 294 | "section", |
300 | GNUNET_GETOPT_option_string ('V', | 295 | "SECTION", |
301 | "value", | 296 | gettext_noop ( |
302 | "VALUE", | 297 | "name of the section to access"), |
303 | gettext_noop ("value to set"), | 298 | §ion), |
304 | &value), | 299 | GNUNET_GETOPT_option_string ('o', |
305 | GNUNET_GETOPT_option_flag ('S', | 300 | "option", |
306 | "list-sections", | 301 | "OPTION", |
307 | gettext_noop ("print available configuration sections"), | 302 | gettext_noop ("name of the option to access"), |
308 | &list_sections), | 303 | &option), |
309 | GNUNET_GETOPT_option_flag ('w', | 304 | GNUNET_GETOPT_option_string ('V', |
310 | "rewrite", | 305 | "value", |
311 | gettext_noop ("write configuration file that only contains delta to defaults"), | 306 | "VALUE", |
312 | &rewrite), | 307 | gettext_noop ("value to set"), |
313 | GNUNET_GETOPT_OPTION_END | 308 | &value), |
314 | }; | 309 | GNUNET_GETOPT_option_flag ('S', |
315 | if (GNUNET_OK != | 310 | "list-sections", |
316 | GNUNET_STRINGS_get_utf8_args (argc, argv, | 311 | gettext_noop ( |
317 | &argc, &argv)) | 312 | "print available configuration sections"), |
313 | &list_sections), | ||
314 | GNUNET_GETOPT_option_flag ( | ||
315 | 'w', | ||
316 | "rewrite", | ||
317 | gettext_noop ( | ||
318 | "write configuration file that only contains delta to defaults"), | ||
319 | &rewrite), | ||
320 | GNUNET_GETOPT_OPTION_END}; | ||
321 | int ret; | ||
322 | |||
323 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
318 | return 2; | 324 | return 2; |
319 | 325 | ||
320 | ret = (GNUNET_OK == | 326 | ret = |
321 | GNUNET_PROGRAM_run (argc, | 327 | GNUNET_PROGRAM_run (argc, |
322 | argv, | 328 | argv, |
323 | "gnunet-config [OPTIONS]", | 329 | "gnunet-config [OPTIONS]", |
324 | gettext_noop ("Manipulate GNUnet configuration files"), | 330 | gettext_noop ("Manipulate GNUnet configuration files"), |
325 | options, | 331 | options, |
326 | &run, NULL)) ? 0 : ret; | 332 | &run, |
327 | GNUNET_free ((void*) argv); | 333 | NULL); |
334 | GNUNET_free ((void *) argv); | ||
335 | if (GNUNET_OK == ret) | ||
336 | return global_ret; | ||
328 | return ret; | 337 | return ret; |
329 | } | 338 | } |
330 | 339 | ||