diff options
Diffstat (limited to 'src/util/getopt_helpers.c')
-rw-r--r-- | src/util/getopt_helpers.c | 634 |
1 files changed, 593 insertions, 41 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index 4d7104503..76342a6c9 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c | |||
@@ -38,11 +38,11 @@ | |||
38 | * @param value not used (NULL) | 38 | * @param value not used (NULL) |
39 | * @return #GNUNET_NO (do not continue, not an error) | 39 | * @return #GNUNET_NO (do not continue, not an error) |
40 | */ | 40 | */ |
41 | int | 41 | static int |
42 | GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 42 | print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
43 | void *scls, | 43 | void *scls, |
44 | const char *option, | 44 | const char *option, |
45 | const char *value) | 45 | const char *value) |
46 | { | 46 | { |
47 | const char *version = scls; | 47 | const char *version = scls; |
48 | 48 | ||
@@ -54,6 +54,26 @@ GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext * | |||
54 | 54 | ||
55 | 55 | ||
56 | /** | 56 | /** |
57 | * Define the option to print the version of | ||
58 | * the application (-v option) | ||
59 | * | ||
60 | * @param version string with the version number | ||
61 | */ | ||
62 | struct GNUNET_GETOPT_CommandLineOption | ||
63 | GNUNET_GETOPT_OPTION_VERSION (const char *version) | ||
64 | { | ||
65 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
66 | .shortName = 'v', | ||
67 | .name = "version", | ||
68 | .description = gettext_noop("print the version number"), | ||
69 | .processor = &print_version, | ||
70 | .scls = (void *) version | ||
71 | }; | ||
72 | return clo; | ||
73 | } | ||
74 | |||
75 | |||
76 | /** | ||
57 | * At what offset does the help text start? | 77 | * At what offset does the help text start? |
58 | */ | 78 | */ |
59 | #define BORDER 29 | 79 | #define BORDER 29 |
@@ -67,11 +87,11 @@ GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext * | |||
67 | * @param value not used (NULL) | 87 | * @param value not used (NULL) |
68 | * @return #GNUNET_NO (do not continue, not an error) | 88 | * @return #GNUNET_NO (do not continue, not an error) |
69 | */ | 89 | */ |
70 | int | 90 | static int |
71 | GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 91 | format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
72 | void *scls, | 92 | void *scls, |
73 | const char *option, | 93 | const char *option, |
74 | const char *value) | 94 | const char *value) |
75 | { | 95 | { |
76 | const char *about = scls; | 96 | const char *about = scls; |
77 | size_t slen; | 97 | size_t slen; |
@@ -165,6 +185,27 @@ OUTER: | |||
165 | 185 | ||
166 | 186 | ||
167 | /** | 187 | /** |
188 | * Defining the option to print the command line | ||
189 | * help text (-h option). | ||
190 | * | ||
191 | * @param about string with brief description of the application | ||
192 | */ | ||
193 | struct GNUNET_GETOPT_CommandLineOption | ||
194 | GNUNET_GETOPT_OPTION_HELP (const char *about) | ||
195 | { | ||
196 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
197 | .shortName = 'h', | ||
198 | .name = "help", | ||
199 | .description = gettext_noop("print this help"), | ||
200 | .processor = format_help, | ||
201 | .scls = (void *) about | ||
202 | }; | ||
203 | |||
204 | return clo; | ||
205 | } | ||
206 | |||
207 | |||
208 | /** | ||
168 | * Set an option of type 'unsigned int' from the command line. Each | 209 | * Set an option of type 'unsigned int' from the command line. Each |
169 | * time the option flag is given, the value is incremented by one. | 210 | * time the option flag is given, the value is incremented by one. |
170 | * A pointer to this function should be passed as part of the | 211 | * A pointer to this function should be passed as part of the |
@@ -173,17 +214,18 @@ OUTER: | |||
173 | * type 'int'. | 214 | * type 'int'. |
174 | * | 215 | * |
175 | * @param ctx command line processing context | 216 | * @param ctx command line processing context |
176 | * @param scls additional closure (will point to the 'int') | 217 | * @param scls additional closure (will point to the 'unsigned int') |
177 | * @param option name of the option | 218 | * @param option name of the option |
178 | * @param value not used (NULL) | 219 | * @param value not used (NULL) |
179 | * @return #GNUNET_OK | 220 | * @return #GNUNET_OK |
180 | */ | 221 | */ |
181 | int | 222 | static int |
182 | GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext | 223 | increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
183 | *ctx, void *scls, const char *option, | 224 | void *scls, |
184 | const char *value) | 225 | const char *option, |
226 | const char *value) | ||
185 | { | 227 | { |
186 | int *val = scls; | 228 | unsigned int *val = scls; |
187 | 229 | ||
188 | (*val)++; | 230 | (*val)++; |
189 | return GNUNET_OK; | 231 | return GNUNET_OK; |
@@ -191,6 +233,54 @@ GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext | |||
191 | 233 | ||
192 | 234 | ||
193 | /** | 235 | /** |
236 | * Increment @a val each time the option flag is given by one. | ||
237 | * | ||
238 | * @param shortName short name of the option | ||
239 | * @param name long name of the option | ||
240 | * @param argumentHelp help text for the option argument | ||
241 | * @param description long help text for the option | ||
242 | * @param[out] val increment by 1 each time the option is present | ||
243 | */ | ||
244 | struct GNUNET_GETOPT_CommandLineOption | ||
245 | GNUNET_GETOPT_OPTION_INCREMENT_VALUE (char shortName, | ||
246 | const char *name, | ||
247 | const char *description, | ||
248 | unsigned int *val) | ||
249 | { | ||
250 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
251 | .shortName = shortName, | ||
252 | .name = name, | ||
253 | .description = description, | ||
254 | .processor = &increment_value, | ||
255 | .scls = (void *) val | ||
256 | }; | ||
257 | |||
258 | return clo; | ||
259 | } | ||
260 | |||
261 | |||
262 | /** | ||
263 | * Define the '-V' verbosity option. Using the option more | ||
264 | * than once increments @a level each time. | ||
265 | * | ||
266 | * @param[out] level set to the verbosity level | ||
267 | */ | ||
268 | struct GNUNET_GETOPT_CommandLineOption | ||
269 | GNUNET_GETOPT_OPTION_VERBOSE (unsigned int *level) | ||
270 | { | ||
271 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
272 | .shortName = 'V', | ||
273 | .name = "verbose", | ||
274 | .description = gettext_noop("be verbose"), | ||
275 | .processor = &increment_value, | ||
276 | .scls = (void *) level | ||
277 | }; | ||
278 | |||
279 | return clo; | ||
280 | } | ||
281 | |||
282 | |||
283 | /** | ||
194 | * Set an option of type 'int' from the command line to 1 if the | 284 | * Set an option of type 'int' from the command line to 1 if the |
195 | * given option is present. | 285 | * given option is present. |
196 | * A pointer to this function should be passed as part of the | 286 | * A pointer to this function should be passed as part of the |
@@ -204,9 +294,11 @@ GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext | |||
204 | * @param value not used (NULL) | 294 | * @param value not used (NULL) |
205 | * @return #GNUNET_OK | 295 | * @return #GNUNET_OK |
206 | */ | 296 | */ |
207 | int | 297 | static int |
208 | GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 298 | set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
209 | void *scls, const char *option, const char *value) | 299 | void *scls, |
300 | const char *option, | ||
301 | const char *value) | ||
210 | { | 302 | { |
211 | int *val = scls; | 303 | int *val = scls; |
212 | 304 | ||
@@ -216,6 +308,34 @@ GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
216 | 308 | ||
217 | 309 | ||
218 | /** | 310 | /** |
311 | * Allow user to specify a flag (which internally means setting | ||
312 | * an integer to 1/#GNUNET_YES/#GNUNET_OK. | ||
313 | * | ||
314 | * @param shortName short name of the option | ||
315 | * @param name long name of the option | ||
316 | * @param argumentHelp help text for the option argument | ||
317 | * @param description long help text for the option | ||
318 | * @param[out] val set to 1 if the option is present | ||
319 | */ | ||
320 | struct GNUNET_GETOPT_CommandLineOption | ||
321 | GNUNET_GETOPT_OPTION_SET_ONE (char shortName, | ||
322 | const char *name, | ||
323 | const char *description, | ||
324 | int *val) | ||
325 | { | ||
326 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
327 | .shortName = shortName, | ||
328 | .name = name, | ||
329 | .description = description, | ||
330 | .processor = &set_one, | ||
331 | .scls = (void *) val | ||
332 | }; | ||
333 | |||
334 | return clo; | ||
335 | } | ||
336 | |||
337 | |||
338 | /** | ||
219 | * Set an option of type 'char *' from the command line. | 339 | * Set an option of type 'char *' from the command line. |
220 | * A pointer to this function should be passed as part of the | 340 | * A pointer to this function should be passed as part of the |
221 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | 341 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options |
@@ -229,31 +349,174 @@ GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
229 | * @param value actual value of the option (a string) | 349 | * @param value actual value of the option (a string) |
230 | * @return #GNUNET_OK | 350 | * @return #GNUNET_OK |
231 | */ | 351 | */ |
232 | int | 352 | static int |
233 | GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 353 | set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
234 | void *scls, const char *option, const char *value) | 354 | void *scls, |
355 | const char *option, | ||
356 | const char *value) | ||
235 | { | 357 | { |
236 | char **val = scls; | 358 | char **val = scls; |
237 | 359 | ||
238 | GNUNET_assert (value != NULL); | 360 | GNUNET_assert (NULL != value); |
239 | GNUNET_free_non_null (*val); | 361 | GNUNET_free_non_null (*val); |
240 | *val = GNUNET_strdup (value); | 362 | *val = GNUNET_strdup (value); |
241 | return GNUNET_OK; | 363 | return GNUNET_OK; |
242 | } | 364 | } |
243 | 365 | ||
244 | 366 | ||
245 | int | 367 | /** |
246 | GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 368 | * Allow user to specify a string. |
247 | void *scls, const char *option, const char *value) | 369 | * |
370 | * @param shortName short name of the option | ||
371 | * @param name long name of the option | ||
372 | * @param argumentHelp help text for the option argument | ||
373 | * @param description long help text for the option | ||
374 | * @param[out] str set to the string | ||
375 | */ | ||
376 | struct GNUNET_GETOPT_CommandLineOption | ||
377 | GNUNET_GETOPT_OPTION_STRING (char shortName, | ||
378 | const char *name, | ||
379 | const char *argumentHelp, | ||
380 | const char *description, | ||
381 | char **str) | ||
382 | { | ||
383 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
384 | .shortName = shortName, | ||
385 | .name = name, | ||
386 | .argumentHelp = argumentHelp, | ||
387 | .description = description, | ||
388 | .require_argument = 1, | ||
389 | .processor = &set_string, | ||
390 | .scls = (void *) str | ||
391 | }; | ||
392 | |||
393 | return clo; | ||
394 | } | ||
395 | |||
396 | |||
397 | /** | ||
398 | * Define the '-L' log level option. Note that we do not check | ||
399 | * that the log level is valid here. | ||
400 | * | ||
401 | * @param[out] level set to the log level | ||
402 | */ | ||
403 | struct GNUNET_GETOPT_CommandLineOption | ||
404 | GNUNET_GETOPT_OPTION_LOGLEVEL (char **level) | ||
405 | { | ||
406 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
407 | .shortName = 'L', | ||
408 | .name = "log", | ||
409 | .argumentHelp = "LOGLEVEL", | ||
410 | .description = gettext_noop("configure logging to use LOGLEVEL"), | ||
411 | .require_argument = 1, | ||
412 | .processor = &set_string, | ||
413 | .scls = (void *) level | ||
414 | }; | ||
415 | |||
416 | return clo; | ||
417 | } | ||
418 | |||
419 | |||
420 | /** | ||
421 | * Set an option of type 'char *' from the command line with | ||
422 | * filename expansion a la #GNUNET_STRINGS_filename_expand(). | ||
423 | * | ||
424 | * @param ctx command line processing context | ||
425 | * @param scls additional closure (will point to the `char *`, | ||
426 | * which will be allocated) | ||
427 | * @param option name of the option | ||
428 | * @param value actual value of the option (a string) | ||
429 | * @return #GNUNET_OK | ||
430 | */ | ||
431 | static int | ||
432 | set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
433 | void *scls, | ||
434 | const char *option, | ||
435 | const char *value) | ||
248 | { | 436 | { |
249 | char **val = scls; | 437 | char **val = scls; |
250 | 438 | ||
251 | GNUNET_assert (value != NULL); | 439 | GNUNET_assert (NULL != value); |
252 | GNUNET_free_non_null (*val); | 440 | GNUNET_free_non_null (*val); |
253 | *val = GNUNET_STRINGS_filename_expand (value); | 441 | *val = GNUNET_STRINGS_filename_expand (value); |
254 | return GNUNET_OK; | 442 | return GNUNET_OK; |
255 | } | 443 | } |
256 | 444 | ||
445 | |||
446 | /** | ||
447 | * Allow user to specify a filename (automatically path expanded). | ||
448 | * | ||
449 | * @param shortName short name of the option | ||
450 | * @param name long name of the option | ||
451 | * @param argumentHelp help text for the option argument | ||
452 | * @param description long help text for the option | ||
453 | * @param[out] str set to the string | ||
454 | */ | ||
455 | struct GNUNET_GETOPT_CommandLineOption | ||
456 | GNUNET_GETOPT_OPTION_FILENAME (char shortName, | ||
457 | const char *name, | ||
458 | const char *argumentHelp, | ||
459 | const char *description, | ||
460 | char **str) | ||
461 | { | ||
462 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
463 | .shortName = shortName, | ||
464 | .name = name, | ||
465 | .argumentHelp = argumentHelp, | ||
466 | .description = description, | ||
467 | .require_argument = 1, | ||
468 | .processor = &set_filename, | ||
469 | .scls = (void *) str | ||
470 | }; | ||
471 | |||
472 | return clo; | ||
473 | } | ||
474 | |||
475 | |||
476 | /** | ||
477 | * Allow user to specify log file name (-l option) | ||
478 | * | ||
479 | * @param[out] logfn set to the name of the logfile | ||
480 | */ | ||
481 | struct GNUNET_GETOPT_CommandLineOption | ||
482 | GNUNET_GETOPT_OPTION_LOGFILE (char **logfn) | ||
483 | { | ||
484 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
485 | .shortName = 'l', | ||
486 | .name = "logfile", | ||
487 | .argumentHelp = "FILENAME", | ||
488 | .description = gettext_noop ("configure logging to write logs to FILENAME"), | ||
489 | .require_argument = 1, | ||
490 | .processor = &set_filename, | ||
491 | .scls = (void *) logfn | ||
492 | }; | ||
493 | |||
494 | return clo; | ||
495 | } | ||
496 | |||
497 | |||
498 | /** | ||
499 | * Allow user to specify configuration file name (-c option) | ||
500 | * | ||
501 | * @param[out] fn set to the name of the configuration file | ||
502 | */ | ||
503 | struct GNUNET_GETOPT_CommandLineOption | ||
504 | GNUNET_GETOPT_OPTION_CFG_FILE (char **fn) | ||
505 | { | ||
506 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
507 | .shortName = 'c', | ||
508 | .name = "config", | ||
509 | .argumentHelp = "FILENAME", | ||
510 | .description = gettext_noop("use configuration file FILENAME"), | ||
511 | .require_argument = 1, | ||
512 | .processor = &set_filename, | ||
513 | .scls = (void *) fn | ||
514 | }; | ||
515 | |||
516 | return clo; | ||
517 | } | ||
518 | |||
519 | |||
257 | /** | 520 | /** |
258 | * Set an option of type 'unsigned long long' from the command line. | 521 | * Set an option of type 'unsigned long long' from the command line. |
259 | * A pointer to this function should be passed as part of the | 522 | * A pointer to this function should be passed as part of the |
@@ -267,15 +530,21 @@ GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ct | |||
267 | * @param value actual value of the option as a string. | 530 | * @param value actual value of the option as a string. |
268 | * @return #GNUNET_OK if parsing the value worked | 531 | * @return #GNUNET_OK if parsing the value worked |
269 | */ | 532 | */ |
270 | int | 533 | static int |
271 | GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 534 | set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
272 | void *scls, const char *option, const char *value) | 535 | void *scls, |
536 | const char *option, | ||
537 | const char *value) | ||
273 | { | 538 | { |
274 | unsigned long long *val = scls; | 539 | unsigned long long *val = scls; |
275 | 540 | ||
276 | if (1 != SSCANF (value, "%llu", val)) | 541 | if (1 != SSCANF (value, |
542 | "%llu", | ||
543 | val)) | ||
277 | { | 544 | { |
278 | FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option); | 545 | FPRINTF (stderr, |
546 | _("You must pass a number to the `%s' option.\n"), | ||
547 | option); | ||
279 | return GNUNET_SYSERR; | 548 | return GNUNET_SYSERR; |
280 | } | 549 | } |
281 | return GNUNET_OK; | 550 | return GNUNET_OK; |
@@ -283,6 +552,36 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
283 | 552 | ||
284 | 553 | ||
285 | /** | 554 | /** |
555 | * Allow user to specify an `unsigned long long` | ||
556 | * | ||
557 | * @param shortName short name of the option | ||
558 | * @param name long name of the option | ||
559 | * @param argumentHelp help text for the option argument | ||
560 | * @param description long help text for the option | ||
561 | * @param[out] val set to the value specified at the command line | ||
562 | */ | ||
563 | struct GNUNET_GETOPT_CommandLineOption | ||
564 | GNUNET_GETOPT_OPTION_SET_ULONG (char shortName, | ||
565 | const char *name, | ||
566 | const char *argumentHelp, | ||
567 | const char *description, | ||
568 | unsigned long long *val) | ||
569 | { | ||
570 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
571 | .shortName = shortName, | ||
572 | .name = name, | ||
573 | .argumentHelp = argumentHelp, | ||
574 | .description = description, | ||
575 | .require_argument = 1, | ||
576 | .processor = &set_ulong, | ||
577 | .scls = (void *) val | ||
578 | }; | ||
579 | |||
580 | return clo; | ||
581 | } | ||
582 | |||
583 | |||
584 | /** | ||
286 | * Set an option of type 'struct GNUNET_TIME_Relative' from the command line. | 585 | * Set an option of type 'struct GNUNET_TIME_Relative' from the command line. |
287 | * A pointer to this function should be passed as part of the | 586 | * A pointer to this function should be passed as part of the |
288 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | 587 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options |
@@ -295,9 +594,11 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
295 | * @param value actual value of the option as a string. | 594 | * @param value actual value of the option as a string. |
296 | * @return #GNUNET_OK if parsing the value worked | 595 | * @return #GNUNET_OK if parsing the value worked |
297 | */ | 596 | */ |
298 | int | 597 | static int |
299 | GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 598 | set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
300 | void *scls, const char *option, const char *value) | 599 | void *scls, |
600 | const char *option, | ||
601 | const char *value) | ||
301 | { | 602 | { |
302 | struct GNUNET_TIME_Relative *val = scls; | 603 | struct GNUNET_TIME_Relative *val = scls; |
303 | 604 | ||
@@ -305,7 +606,74 @@ GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContex | |||
305 | GNUNET_STRINGS_fancy_time_to_relative (value, | 606 | GNUNET_STRINGS_fancy_time_to_relative (value, |
306 | val)) | 607 | val)) |
307 | { | 608 | { |
308 | FPRINTF (stderr, _("You must pass relative time to the `%s' option.\n"), option); | 609 | FPRINTF (stderr, |
610 | _("You must pass relative time to the `%s' option.\n"), | ||
611 | option); | ||
612 | return GNUNET_SYSERR; | ||
613 | } | ||
614 | return GNUNET_OK; | ||
615 | } | ||
616 | |||
617 | |||
618 | /** | ||
619 | * Allow user to specify a `struct GNUNET_TIME_Relative` | ||
620 | * (using human-readable "fancy" time). | ||
621 | * | ||
622 | * @param shortName short name of the option | ||
623 | * @param name long name of the option | ||
624 | * @param argumentHelp help text for the option argument | ||
625 | * @param description long help text for the option | ||
626 | * @param[out] val set to the time specified at the command line | ||
627 | */ | ||
628 | struct GNUNET_GETOPT_CommandLineOption | ||
629 | GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME (char shortName, | ||
630 | const char *name, | ||
631 | const char *argumentHelp, | ||
632 | const char *description, | ||
633 | struct GNUNET_TIME_Relative *val) | ||
634 | { | ||
635 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
636 | .shortName = shortName, | ||
637 | .name = name, | ||
638 | .argumentHelp = argumentHelp, | ||
639 | .description = description, | ||
640 | .require_argument = 1, | ||
641 | .processor = &set_relative_time, | ||
642 | .scls = (void *) val | ||
643 | }; | ||
644 | |||
645 | return clo; | ||
646 | } | ||
647 | |||
648 | |||
649 | /** | ||
650 | * Set an option of type 'struct GNUNET_TIME_Absolute' from the command line. | ||
651 | * A pointer to this function should be passed as part of the | ||
652 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | ||
653 | * of this type. It should be followed by a pointer to a value of | ||
654 | * type 'struct GNUNET_TIME_Absolute'. | ||
655 | * | ||
656 | * @param ctx command line processing context | ||
657 | * @param scls additional closure (will point to the `struct GNUNET_TIME_Absolute`) | ||
658 | * @param option name of the option | ||
659 | * @param value actual value of the option as a string. | ||
660 | * @return #GNUNET_OK if parsing the value worked | ||
661 | */ | ||
662 | static int | ||
663 | set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
664 | void *scls, | ||
665 | const char *option, | ||
666 | const char *value) | ||
667 | { | ||
668 | struct GNUNET_TIME_Absolute *val = scls; | ||
669 | |||
670 | if (GNUNET_OK != | ||
671 | GNUNET_STRINGS_fancy_time_to_absolute (value, | ||
672 | val)) | ||
673 | { | ||
674 | FPRINTF (stderr, | ||
675 | _("You must pass absolute time to the `%s' option.\n"), | ||
676 | option); | ||
309 | return GNUNET_SYSERR; | 677 | return GNUNET_SYSERR; |
310 | } | 678 | } |
311 | return GNUNET_OK; | 679 | return GNUNET_OK; |
@@ -313,6 +681,37 @@ GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContex | |||
313 | 681 | ||
314 | 682 | ||
315 | /** | 683 | /** |
684 | * Allow user to specify a `struct GNUNET_TIME_Absolute` | ||
685 | * (using human-readable "fancy" time). | ||
686 | * | ||
687 | * @param shortName short name of the option | ||
688 | * @param name long name of the option | ||
689 | * @param argumentHelp help text for the option argument | ||
690 | * @param description long help text for the option | ||
691 | * @param[out] val set to the time specified at the command line | ||
692 | */ | ||
693 | struct GNUNET_GETOPT_CommandLineOption | ||
694 | GNUNET_GETOPT_OPTION_SET_ABSOLUTE_TIME (char shortName, | ||
695 | const char *name, | ||
696 | const char *argumentHelp, | ||
697 | const char *description, | ||
698 | struct GNUNET_TIME_Absolute *val) | ||
699 | { | ||
700 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
701 | .shortName = shortName, | ||
702 | .name = name, | ||
703 | .argumentHelp = argumentHelp, | ||
704 | .description = description, | ||
705 | .require_argument = 1, | ||
706 | .processor = &set_absolute_time, | ||
707 | .scls = (void *) val | ||
708 | }; | ||
709 | |||
710 | return clo; | ||
711 | } | ||
712 | |||
713 | |||
714 | /** | ||
316 | * Set an option of type 'unsigned int' from the command line. | 715 | * Set an option of type 'unsigned int' from the command line. |
317 | * A pointer to this function should be passed as part of the | 716 | * A pointer to this function should be passed as part of the |
318 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | 717 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options |
@@ -325,19 +724,172 @@ GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContex | |||
325 | * @param value actual value of the option as a string. | 724 | * @param value actual value of the option as a string. |
326 | * @return #GNUNET_OK if parsing the value worked | 725 | * @return #GNUNET_OK if parsing the value worked |
327 | */ | 726 | */ |
328 | int | 727 | static int |
329 | GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 728 | set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
330 | void *scls, const char *option, const char *value) | 729 | void *scls, |
730 | const char *option, | ||
731 | const char *value) | ||
331 | { | 732 | { |
332 | unsigned int *val = scls; | 733 | unsigned int *val = scls; |
333 | 734 | ||
334 | if (1 != SSCANF (value, "%u", val)) | 735 | if (1 != SSCANF (value, |
736 | "%u", | ||
737 | val)) | ||
335 | { | 738 | { |
336 | FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option); | 739 | FPRINTF (stderr, |
740 | _("You must pass a number to the `%s' option.\n"), | ||
741 | option); | ||
337 | return GNUNET_SYSERR; | 742 | return GNUNET_SYSERR; |
338 | } | 743 | } |
339 | return GNUNET_OK; | 744 | return GNUNET_OK; |
340 | } | 745 | } |
341 | 746 | ||
342 | 747 | ||
748 | /** | ||
749 | * Allow user to specify an unsigned integer. | ||
750 | * | ||
751 | * @param shortName short name of the option | ||
752 | * @param name long name of the option | ||
753 | * @param argumentHelp help text for the option argument | ||
754 | * @param description long help text for the option | ||
755 | * @param[out] val set to the value specified at the command line | ||
756 | */ | ||
757 | struct GNUNET_GETOPT_CommandLineOption | ||
758 | GNUNET_GETOPT_OPTION_SET_UINT (char shortName, | ||
759 | const char *name, | ||
760 | const char *argumentHelp, | ||
761 | const char *description, | ||
762 | unsigned int *val) | ||
763 | { | ||
764 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
765 | .shortName = shortName, | ||
766 | .name = name, | ||
767 | .argumentHelp = argumentHelp, | ||
768 | .description = description, | ||
769 | .require_argument = 1, | ||
770 | .processor = &set_uint, | ||
771 | .scls = (void *) val | ||
772 | }; | ||
773 | |||
774 | return clo; | ||
775 | } | ||
776 | |||
777 | |||
778 | /** | ||
779 | * Closure for #set_base32(). | ||
780 | */ | ||
781 | struct Base32Context | ||
782 | { | ||
783 | /** | ||
784 | * Value to initialize (already allocated) | ||
785 | */ | ||
786 | void *val; | ||
787 | |||
788 | /** | ||
789 | * Number of bytes expected for @e val. | ||
790 | */ | ||
791 | size_t val_size; | ||
792 | }; | ||
793 | |||
794 | |||
795 | /** | ||
796 | * Set an option of type 'unsigned int' from the command line. | ||
797 | * A pointer to this function should be passed as part of the | ||
798 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | ||
799 | * of this type. It should be followed by a pointer to a value of | ||
800 | * type 'unsigned int'. | ||
801 | * | ||
802 | * @param ctx command line processing context | ||
803 | * @param scls additional closure (will point to the 'unsigned int') | ||
804 | * @param option name of the option | ||
805 | * @param value actual value of the option as a string. | ||
806 | * @return #GNUNET_OK if parsing the value worked | ||
807 | */ | ||
808 | static int | ||
809 | set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
810 | void *scls, | ||
811 | const char *option, | ||
812 | const char *value) | ||
813 | { | ||
814 | struct Base32Context *bc = scls; | ||
815 | |||
816 | if (GNUNET_OK != | ||
817 | GNUNET_STRINGS_string_to_data (value, | ||
818 | strlen (value), | ||
819 | bc->val, | ||
820 | bc->val_size)) | ||
821 | { | ||
822 | fprintf (stderr, | ||
823 | _("Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), | ||
824 | option); | ||
825 | return GNUNET_SYSERR; | ||
826 | } | ||
827 | return GNUNET_OK; | ||
828 | } | ||
829 | |||
830 | |||
831 | /** | ||
832 | * Helper function to clean up after | ||
833 | * #GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE. | ||
834 | * | ||
835 | * @param cls value to GNUNET_free() | ||
836 | */ | ||
837 | static void | ||
838 | free_bc (void *cls) | ||
839 | { | ||
840 | GNUNET_free (cls); | ||
841 | } | ||
842 | |||
843 | |||
844 | /** | ||
845 | * Allow user to specify a binary value using Crockford | ||
846 | * Base32 encoding. | ||
847 | * | ||
848 | * @param shortName short name of the option | ||
849 | * @param name long name of the option | ||
850 | * @param argumentHelp help text for the option argument | ||
851 | * @param description long help text for the option | ||
852 | * @param[out] val binary value decoded from Crockford Base32-encoded argument | ||
853 | * @param val_size size of @a val in bytes | ||
854 | */ | ||
855 | struct GNUNET_GETOPT_CommandLineOption | ||
856 | GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE (char shortName, | ||
857 | const char *name, | ||
858 | const char *argumentHelp, | ||
859 | const char *description, | ||
860 | void *val, | ||
861 | size_t val_size) | ||
862 | { | ||
863 | struct Base32Context *bc = GNUNET_new (struct Base32Context); | ||
864 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
865 | .shortName = shortName, | ||
866 | .name = name, | ||
867 | .argumentHelp = argumentHelp, | ||
868 | .description = description, | ||
869 | .require_argument = 1, | ||
870 | .processor = &set_base32, | ||
871 | .cleaner = &free_bc, | ||
872 | .scls = (void *) bc | ||
873 | }; | ||
874 | |||
875 | bc->val = val; | ||
876 | bc->val_size = val_size; | ||
877 | return clo; | ||
878 | } | ||
879 | |||
880 | |||
881 | /** | ||
882 | * Make the given option mandatory. | ||
883 | * | ||
884 | * @param opt option to modify | ||
885 | * @return @a opt with the mandatory flag set. | ||
886 | */ | ||
887 | struct GNUNET_GETOPT_CommandLineOption | ||
888 | GNUNET_GETOPT_OPTION_MANDATORY (struct GNUNET_GETOPT_CommandLineOption opt) | ||
889 | { | ||
890 | opt.option_mandatory = 1; | ||
891 | return opt; | ||
892 | } | ||
893 | |||
894 | |||
343 | /* end of getopt_helpers.c */ | 895 | /* end of getopt_helpers.c */ |