diff options
Diffstat (limited to 'src/util/gnunet-config.c')
-rw-r--r-- | src/util/gnunet-config.c | 342 |
1 files changed, 171 insertions, 171 deletions
diff --git a/src/util/gnunet-config.c b/src/util/gnunet-config.c index 3b9b64d4e..c67237a27 100644 --- a/src/util/gnunet-config.c +++ b/src/util/gnunet-config.c | |||
@@ -16,7 +16,7 @@ | |||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file util/gnunet-config.c | 22 | * @file util/gnunet-config.c |
@@ -80,36 +80,36 @@ static int rewrite; | |||
80 | * @param value value of the option | 80 | * @param value value of the option |
81 | */ | 81 | */ |
82 | static void | 82 | static void |
83 | print_option (void *cls, | 83 | print_option(void *cls, |
84 | const char *section, | 84 | const char *section, |
85 | const char *option, | 85 | const char *option, |
86 | const char *value) | 86 | const char *value) |
87 | { | 87 | { |
88 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 88 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; |
89 | 89 | ||
90 | (void) section; | 90 | (void)section; |
91 | if (is_filename) | 91 | if (is_filename) |
92 | { | 92 | { |
93 | char *value_fn; | 93 | char *value_fn; |
94 | char *fn; | 94 | char *fn; |
95 | 95 | ||
96 | GNUNET_assert (GNUNET_OK == | 96 | GNUNET_assert(GNUNET_OK == |
97 | GNUNET_CONFIGURATION_get_value_filename (cfg, | 97 | GNUNET_CONFIGURATION_get_value_filename(cfg, |
98 | section, | 98 | section, |
99 | option, | 99 | option, |
100 | &value_fn)); | 100 | &value_fn)); |
101 | fn = GNUNET_STRINGS_filename_expand (value_fn); | 101 | fn = GNUNET_STRINGS_filename_expand(value_fn); |
102 | if (NULL == fn) | 102 | if (NULL == fn) |
103 | fn = value_fn; | 103 | fn = value_fn; |
104 | else | 104 | else |
105 | GNUNET_free (value_fn); | 105 | GNUNET_free(value_fn); |
106 | fprintf (stdout, "%s = %s\n", option, fn); | 106 | fprintf(stdout, "%s = %s\n", option, fn); |
107 | GNUNET_free (fn); | 107 | GNUNET_free(fn); |
108 | } | 108 | } |
109 | else | 109 | else |
110 | { | 110 | { |
111 | fprintf (stdout, "%s = %s\n", option, value); | 111 | fprintf(stdout, "%s = %s\n", option, value); |
112 | } | 112 | } |
113 | } | 113 | } |
114 | 114 | ||
115 | 115 | ||
@@ -120,10 +120,10 @@ print_option (void *cls, | |||
120 | * @param section a section in the configuration file | 120 | * @param section a section in the configuration file |
121 | */ | 121 | */ |
122 | static void | 122 | static void |
123 | print_section_name (void *cls, const char *section) | 123 | print_section_name(void *cls, const char *section) |
124 | { | 124 | { |
125 | (void) cls; | 125 | (void)cls; |
126 | fprintf (stdout, "%s\n", section); | 126 | fprintf(stdout, "%s\n", section); |
127 | } | 127 | } |
128 | 128 | ||
129 | 129 | ||
@@ -137,133 +137,133 @@ print_section_name (void *cls, const char *section) | |||
137 | * @param cfg configuration | 137 | * @param cfg configuration |
138 | */ | 138 | */ |
139 | static void | 139 | static void |
140 | run (void *cls, | 140 | run(void *cls, |
141 | char *const *args, | 141 | char *const *args, |
142 | const char *cfgfile, | 142 | const char *cfgfile, |
143 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 143 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
144 | { | 144 | { |
145 | struct GNUNET_CONFIGURATION_Handle *out = NULL; | 145 | struct GNUNET_CONFIGURATION_Handle *out = NULL; |
146 | struct GNUNET_CONFIGURATION_Handle *diff = NULL; | 146 | struct GNUNET_CONFIGURATION_Handle *diff = NULL; |
147 | char *cfg_fn; | 147 | char *cfg_fn; |
148 | 148 | ||
149 | (void) cls; | 149 | (void)cls; |
150 | (void) args; | 150 | (void)args; |
151 | if (NULL != backend_check) | 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 | } | ||
160 | if (rewrite) | ||
161 | { | ||
162 | struct GNUNET_CONFIGURATION_Handle *def; | ||
163 | |||
164 | def = GNUNET_CONFIGURATION_create (); | ||
165 | if (GNUNET_OK != GNUNET_CONFIGURATION_load (def, NULL)) | ||
166 | { | 152 | { |
167 | fprintf (stderr, _ ("failed to load configuration defaults")); | 153 | char *name; |
168 | global_ret = 1; | 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); | ||
169 | return; | 158 | return; |
170 | } | 159 | } |
171 | diff = GNUNET_CONFIGURATION_get_diff (def, cfg); | 160 | if (rewrite) |
172 | cfg = diff; | ||
173 | } | ||
174 | if (((! rewrite) && (NULL == section)) || list_sections) | ||
175 | { | ||
176 | if (! list_sections) | ||
177 | { | 161 | { |
178 | fprintf (stderr, | 162 | struct GNUNET_CONFIGURATION_Handle *def; |
179 | _ ("%s or %s argument is required\n"), | 163 | |
180 | "--section", | 164 | def = GNUNET_CONFIGURATION_create(); |
181 | "--list-sections"); | 165 | if (GNUNET_OK != GNUNET_CONFIGURATION_load(def, NULL)) |
182 | global_ret = 1; | 166 | { |
167 | fprintf(stderr, _("failed to load configuration defaults")); | ||
168 | global_ret = 1; | ||
169 | return; | ||
170 | } | ||
171 | diff = GNUNET_CONFIGURATION_get_diff(def, cfg); | ||
172 | cfg = diff; | ||
183 | } | 173 | } |
184 | else | 174 | if (((!rewrite) && (NULL == section)) || list_sections) |
185 | { | 175 | { |
186 | fprintf (stderr, _ ("The following sections are available:\n")); | 176 | if (!list_sections) |
187 | GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL); | 177 | { |
178 | fprintf(stderr, | ||
179 | _("%s or %s argument is required\n"), | ||
180 | "--section", | ||
181 | "--list-sections"); | ||
182 | global_ret = 1; | ||
183 | } | ||
184 | else | ||
185 | { | ||
186 | fprintf(stderr, _("The following sections are available:\n")); | ||
187 | GNUNET_CONFIGURATION_iterate_sections(cfg, &print_section_name, NULL); | ||
188 | } | ||
189 | goto cleanup; | ||
188 | } | 190 | } |
189 | goto cleanup; | ||
190 | } | ||
191 | 191 | ||
192 | if ((NULL != section) && (NULL == value)) | 192 | if ((NULL != section) && (NULL == value)) |
193 | { | ||
194 | if (NULL == option) | ||
195 | { | ||
196 | GNUNET_CONFIGURATION_iterate_section_values (cfg, | ||
197 | section, | ||
198 | &print_option, | ||
199 | (void *) cfg); | ||
200 | } | ||
201 | else | ||
202 | { | 193 | { |
203 | if (is_filename) | 194 | if (NULL == option) |
204 | { | ||
205 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
206 | section, | ||
207 | option, | ||
208 | &value)) | ||
209 | { | 195 | { |
210 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); | 196 | GNUNET_CONFIGURATION_iterate_section_values(cfg, |
211 | global_ret = 3; | 197 | section, |
212 | goto cleanup; | 198 | &print_option, |
199 | (void *)cfg); | ||
213 | } | 200 | } |
214 | } | ||
215 | else | 201 | else |
216 | { | ||
217 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, | ||
218 | section, | ||
219 | option, | ||
220 | &value)) | ||
221 | { | 202 | { |
222 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); | 203 | if (is_filename) |
223 | global_ret = 3; | 204 | { |
224 | goto cleanup; | 205 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, |
206 | section, | ||
207 | option, | ||
208 | &value)) | ||
209 | { | ||
210 | GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, option); | ||
211 | global_ret = 3; | ||
212 | goto cleanup; | ||
213 | } | ||
214 | } | ||
215 | else | ||
216 | { | ||
217 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, | ||
218 | section, | ||
219 | option, | ||
220 | &value)) | ||
221 | { | ||
222 | GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, option); | ||
223 | global_ret = 3; | ||
224 | goto cleanup; | ||
225 | } | ||
226 | } | ||
227 | fprintf(stdout, "%s\n", value); | ||
225 | } | 228 | } |
226 | } | ||
227 | fprintf (stdout, "%s\n", value); | ||
228 | } | 229 | } |
229 | } | ||
230 | else if (NULL != section) | 230 | else if (NULL != section) |
231 | { | ||
232 | if (NULL == option) | ||
233 | { | 231 | { |
234 | fprintf (stderr, _ ("--option argument required to set value\n")); | 232 | if (NULL == option) |
235 | global_ret = 1; | 233 | { |
236 | goto cleanup; | 234 | fprintf(stderr, _("--option argument required to set value\n")); |
235 | global_ret = 1; | ||
236 | goto cleanup; | ||
237 | } | ||
238 | out = GNUNET_CONFIGURATION_dup(cfg); | ||
239 | GNUNET_CONFIGURATION_set_value_string(out, section, option, value); | ||
237 | } | 240 | } |
238 | out = GNUNET_CONFIGURATION_dup (cfg); | ||
239 | GNUNET_CONFIGURATION_set_value_string (out, section, option, value); | ||
240 | } | ||
241 | cfg_fn = NULL; | 241 | cfg_fn = NULL; |
242 | if (NULL == cfgfile) | 242 | if (NULL == cfgfile) |
243 | { | 243 | { |
244 | const char *xdg = getenv ("XDG_CONFIG_HOME"); | 244 | const char *xdg = getenv("XDG_CONFIG_HOME"); |
245 | if (NULL != xdg) | 245 | if (NULL != xdg) |
246 | GNUNET_asprintf (&cfg_fn, | 246 | GNUNET_asprintf(&cfg_fn, |
247 | "%s%s%s", | 247 | "%s%s%s", |
248 | xdg, | 248 | xdg, |
249 | DIR_SEPARATOR_STR, | 249 | DIR_SEPARATOR_STR, |
250 | GNUNET_OS_project_data_get ()->config_file); | 250 | GNUNET_OS_project_data_get()->config_file); |
251 | else | 251 | else |
252 | 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); |
253 | cfgfile = cfg_fn; | 253 | cfgfile = cfg_fn; |
254 | } | 254 | } |
255 | if ((NULL != diff) || (NULL != out)) | 255 | if ((NULL != diff) || (NULL != out)) |
256 | { | 256 | { |
257 | if (GNUNET_OK != | 257 | if (GNUNET_OK != |
258 | GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, cfgfile)) | 258 | GNUNET_CONFIGURATION_write((NULL == out) ? diff : out, cfgfile)) |
259 | global_ret = 2; | 259 | global_ret = 2; |
260 | } | 260 | } |
261 | GNUNET_free_non_null (cfg_fn); | 261 | GNUNET_free_non_null(cfg_fn); |
262 | if (NULL != out) | 262 | if (NULL != out) |
263 | GNUNET_CONFIGURATION_destroy (out); | 263 | GNUNET_CONFIGURATION_destroy(out); |
264 | cleanup: | 264 | cleanup: |
265 | if (NULL != diff) | 265 | if (NULL != diff) |
266 | GNUNET_CONFIGURATION_destroy (diff); | 266 | GNUNET_CONFIGURATION_destroy(diff); |
267 | } | 267 | } |
268 | 268 | ||
269 | 269 | ||
@@ -275,63 +275,63 @@ cleanup: | |||
275 | * @return 0 ok, 1 on error | 275 | * @return 0 ok, 1 on error |
276 | */ | 276 | */ |
277 | int | 277 | int |
278 | main (int argc, char *const *argv) | 278 | main(int argc, char *const *argv) |
279 | { | 279 | { |
280 | struct GNUNET_GETOPT_CommandLineOption options[] = | 280 | struct GNUNET_GETOPT_CommandLineOption options[] = |
281 | {GNUNET_GETOPT_option_flag ( | 281 | { GNUNET_GETOPT_option_flag( |
282 | 'f', | 282 | 'f', |
283 | "filename", | 283 | "filename", |
284 | gettext_noop ("interpret option value as a filename (with $-expansion)"), | 284 | gettext_noop("interpret option value as a filename (with $-expansion)"), |
285 | &is_filename), | 285 | &is_filename), |
286 | GNUNET_GETOPT_option_exclusive (GNUNET_GETOPT_option_string ( | 286 | GNUNET_GETOPT_option_exclusive(GNUNET_GETOPT_option_string( |
287 | 'b', | 287 | 'b', |
288 | "supported-backend", | 288 | "supported-backend", |
289 | "BACKEND", | 289 | "BACKEND", |
290 | gettext_noop ( | 290 | gettext_noop( |
291 | "test if the current installation supports the specified BACKEND"), | 291 | "test if the current installation supports the specified BACKEND"), |
292 | &backend_check)), | 292 | &backend_check)), |
293 | GNUNET_GETOPT_option_string ('s', | 293 | GNUNET_GETOPT_option_string('s', |
294 | "section", | 294 | "section", |
295 | "SECTION", | 295 | "SECTION", |
296 | gettext_noop ( | 296 | gettext_noop( |
297 | "name of the section to access"), | 297 | "name of the section to access"), |
298 | §ion), | 298 | §ion), |
299 | GNUNET_GETOPT_option_string ('o', | 299 | GNUNET_GETOPT_option_string('o', |
300 | "option", | 300 | "option", |
301 | "OPTION", | 301 | "OPTION", |
302 | gettext_noop ("name of the option to access"), | 302 | gettext_noop("name of the option to access"), |
303 | &option), | 303 | &option), |
304 | GNUNET_GETOPT_option_string ('V', | 304 | GNUNET_GETOPT_option_string('V', |
305 | "value", | 305 | "value", |
306 | "VALUE", | 306 | "VALUE", |
307 | gettext_noop ("value to set"), | 307 | gettext_noop("value to set"), |
308 | &value), | 308 | &value), |
309 | GNUNET_GETOPT_option_flag ('S', | 309 | GNUNET_GETOPT_option_flag('S', |
310 | "list-sections", | 310 | "list-sections", |
311 | gettext_noop ( | 311 | gettext_noop( |
312 | "print available configuration sections"), | 312 | "print available configuration sections"), |
313 | &list_sections), | 313 | &list_sections), |
314 | GNUNET_GETOPT_option_flag ( | 314 | GNUNET_GETOPT_option_flag( |
315 | 'w', | 315 | 'w', |
316 | "rewrite", | 316 | "rewrite", |
317 | gettext_noop ( | 317 | gettext_noop( |
318 | "write configuration file that only contains delta to defaults"), | 318 | "write configuration file that only contains delta to defaults"), |
319 | &rewrite), | 319 | &rewrite), |
320 | GNUNET_GETOPT_OPTION_END}; | 320 | GNUNET_GETOPT_OPTION_END }; |
321 | int ret; | 321 | int ret; |
322 | 322 | ||
323 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 323 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) |
324 | return 2; | 324 | return 2; |
325 | 325 | ||
326 | ret = | 326 | ret = |
327 | GNUNET_PROGRAM_run (argc, | 327 | GNUNET_PROGRAM_run(argc, |
328 | argv, | 328 | argv, |
329 | "gnunet-config [OPTIONS]", | 329 | "gnunet-config [OPTIONS]", |
330 | gettext_noop ("Manipulate GNUnet configuration files"), | 330 | gettext_noop("Manipulate GNUnet configuration files"), |
331 | options, | 331 | options, |
332 | &run, | 332 | &run, |
333 | NULL); | 333 | NULL); |
334 | GNUNET_free ((void *) argv); | 334 | GNUNET_free((void *)argv); |
335 | if (GNUNET_OK == ret) | 335 | if (GNUNET_OK == ret) |
336 | return global_ret; | 336 | return global_ret; |
337 | return ret; | 337 | return ret; |