diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-02-04 15:47:01 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-02-04 15:47:01 +0100 |
commit | e14544109bf1c473a0e32d6c3b0e84dec8989efc (patch) | |
tree | 6f7701cf0e75857951c34b6921ae28dbdc3a7740 /src/util | |
parent | e3dcec36a06dec9ca7b8f4f43ff247633167d3df (diff) | |
download | gnunet-e14544109bf1c473a0e32d6c3b0e84dec8989efc.tar.gz gnunet-e14544109bf1c473a0e32d6c3b0e84dec8989efc.zip |
add timetravel getopt helper
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/getopt_helpers.c | 294 |
1 files changed, 194 insertions, 100 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index 68da423b8..a2572ccab 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | Copyright (C) 2006, 2011 GNUnet e.V. | 3 | Copyright (C) 2006, 2011, 2020 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -62,13 +62,15 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
62 | struct GNUNET_GETOPT_CommandLineOption | 62 | struct GNUNET_GETOPT_CommandLineOption |
63 | GNUNET_GETOPT_option_version (const char *version) | 63 | GNUNET_GETOPT_option_version (const char *version) |
64 | { | 64 | { |
65 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'v', | 65 | struct GNUNET_GETOPT_CommandLineOption clo = { |
66 | .name = "version", | 66 | .shortName = 'v', |
67 | .description = gettext_noop ( | 67 | .name = "version", |
68 | "print the version number"), | 68 | .description = gettext_noop ( |
69 | .option_exclusive = 1, | 69 | "print the version number"), |
70 | .processor = &print_version, | 70 | .option_exclusive = 1, |
71 | .scls = (void *) version }; | 71 | .processor = &print_version, |
72 | .scls = (void *) version | ||
73 | }; | ||
72 | 74 | ||
73 | return clo; | 75 | return clo; |
74 | } | 76 | } |
@@ -199,13 +201,15 @@ OUTER: | |||
199 | struct GNUNET_GETOPT_CommandLineOption | 201 | struct GNUNET_GETOPT_CommandLineOption |
200 | GNUNET_GETOPT_option_help (const char *about) | 202 | GNUNET_GETOPT_option_help (const char *about) |
201 | { | 203 | { |
202 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'h', | 204 | struct GNUNET_GETOPT_CommandLineOption clo = { |
203 | .name = "help", | 205 | .shortName = 'h', |
204 | .description = gettext_noop ( | 206 | .name = "help", |
205 | "print this help"), | 207 | .description = gettext_noop ( |
206 | .option_exclusive = 1, | 208 | "print this help"), |
207 | .processor = format_help, | 209 | .option_exclusive = 1, |
208 | .scls = (void *) about }; | 210 | .processor = format_help, |
211 | .scls = (void *) about | ||
212 | }; | ||
209 | 213 | ||
210 | return clo; | 214 | return clo; |
211 | } | 215 | } |
@@ -256,11 +260,13 @@ GNUNET_GETOPT_option_increment_uint (char shortName, | |||
256 | const char *description, | 260 | const char *description, |
257 | unsigned int *val) | 261 | unsigned int *val) |
258 | { | 262 | { |
259 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 263 | struct GNUNET_GETOPT_CommandLineOption clo = { |
260 | .name = name, | 264 | .shortName = shortName, |
261 | .description = description, | 265 | .name = name, |
262 | .processor = &increment_value, | 266 | .description = description, |
263 | .scls = (void *) val }; | 267 | .processor = &increment_value, |
268 | .scls = (void *) val | ||
269 | }; | ||
264 | 270 | ||
265 | return clo; | 271 | return clo; |
266 | } | 272 | } |
@@ -275,12 +281,14 @@ GNUNET_GETOPT_option_increment_uint (char shortName, | |||
275 | struct GNUNET_GETOPT_CommandLineOption | 281 | struct GNUNET_GETOPT_CommandLineOption |
276 | GNUNET_GETOPT_option_verbose (unsigned int *level) | 282 | GNUNET_GETOPT_option_verbose (unsigned int *level) |
277 | { | 283 | { |
278 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'V', | 284 | struct GNUNET_GETOPT_CommandLineOption clo = { |
279 | .name = "verbose", | 285 | .shortName = 'V', |
280 | .description = | 286 | .name = "verbose", |
281 | gettext_noop ("be verbose"), | 287 | .description = |
282 | .processor = &increment_value, | 288 | gettext_noop ("be verbose"), |
283 | .scls = (void *) level }; | 289 | .processor = &increment_value, |
290 | .scls = (void *) level | ||
291 | }; | ||
284 | 292 | ||
285 | return clo; | 293 | return clo; |
286 | } | 294 | } |
@@ -332,11 +340,13 @@ GNUNET_GETOPT_option_flag (char shortName, | |||
332 | const char *description, | 340 | const char *description, |
333 | int *val) | 341 | int *val) |
334 | { | 342 | { |
335 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 343 | struct GNUNET_GETOPT_CommandLineOption clo = { |
336 | .name = name, | 344 | .shortName = shortName, |
337 | .description = description, | 345 | .name = name, |
338 | .processor = &set_one, | 346 | .description = description, |
339 | .scls = (void *) val }; | 347 | .processor = &set_one, |
348 | .scls = (void *) val | ||
349 | }; | ||
340 | 350 | ||
341 | return clo; | 351 | return clo; |
342 | } | 352 | } |
@@ -389,13 +399,15 @@ GNUNET_GETOPT_option_string (char shortName, | |||
389 | const char *description, | 399 | const char *description, |
390 | char **str) | 400 | char **str) |
391 | { | 401 | { |
392 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 402 | struct GNUNET_GETOPT_CommandLineOption clo = { |
393 | .name = name, | 403 | .shortName = shortName, |
394 | .argumentHelp = argumentHelp, | 404 | .name = name, |
395 | .description = description, | 405 | .argumentHelp = argumentHelp, |
396 | .require_argument = 1, | 406 | .description = description, |
397 | .processor = &set_string, | 407 | .require_argument = 1, |
398 | .scls = (void *) str }; | 408 | .processor = &set_string, |
409 | .scls = (void *) str | ||
410 | }; | ||
399 | 411 | ||
400 | return clo; | 412 | return clo; |
401 | } | 413 | } |
@@ -410,14 +422,15 @@ GNUNET_GETOPT_option_string (char shortName, | |||
410 | struct GNUNET_GETOPT_CommandLineOption | 422 | struct GNUNET_GETOPT_CommandLineOption |
411 | GNUNET_GETOPT_option_loglevel (char **level) | 423 | GNUNET_GETOPT_option_loglevel (char **level) |
412 | { | 424 | { |
413 | struct GNUNET_GETOPT_CommandLineOption clo = | 425 | struct GNUNET_GETOPT_CommandLineOption clo = { |
414 | { .shortName = 'L', | 426 | .shortName = 'L', |
415 | .name = "log", | 427 | .name = "log", |
416 | .argumentHelp = "LOGLEVEL", | 428 | .argumentHelp = "LOGLEVEL", |
417 | .description = gettext_noop ("configure logging to use LOGLEVEL"), | 429 | .description = gettext_noop ("configure logging to use LOGLEVEL"), |
418 | .require_argument = 1, | 430 | .require_argument = 1, |
419 | .processor = &set_string, | 431 | .processor = &set_string, |
420 | .scls = (void *) level }; | 432 | .scls = (void *) level |
433 | }; | ||
421 | 434 | ||
422 | return clo; | 435 | return clo; |
423 | } | 436 | } |
@@ -467,13 +480,15 @@ GNUNET_GETOPT_option_filename (char shortName, | |||
467 | const char *description, | 480 | const char *description, |
468 | char **str) | 481 | char **str) |
469 | { | 482 | { |
470 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 483 | struct GNUNET_GETOPT_CommandLineOption clo = { |
471 | .name = name, | 484 | .shortName = shortName, |
472 | .argumentHelp = argumentHelp, | 485 | .name = name, |
473 | .description = description, | 486 | .argumentHelp = argumentHelp, |
474 | .require_argument = 1, | 487 | .description = description, |
475 | .processor = &set_filename, | 488 | .require_argument = 1, |
476 | .scls = (void *) str }; | 489 | .processor = &set_filename, |
490 | .scls = (void *) str | ||
491 | }; | ||
477 | 492 | ||
478 | return clo; | 493 | return clo; |
479 | } | 494 | } |
@@ -487,15 +502,16 @@ GNUNET_GETOPT_option_filename (char shortName, | |||
487 | struct GNUNET_GETOPT_CommandLineOption | 502 | struct GNUNET_GETOPT_CommandLineOption |
488 | GNUNET_GETOPT_option_logfile (char **logfn) | 503 | GNUNET_GETOPT_option_logfile (char **logfn) |
489 | { | 504 | { |
490 | struct GNUNET_GETOPT_CommandLineOption clo = | 505 | struct GNUNET_GETOPT_CommandLineOption clo = { |
491 | { .shortName = 'l', | 506 | .shortName = 'l', |
492 | .name = "logfile", | 507 | .name = "logfile", |
493 | .argumentHelp = "FILENAME", | 508 | .argumentHelp = "FILENAME", |
494 | .description = | 509 | .description = |
495 | gettext_noop ("configure logging to write logs to FILENAME"), | 510 | gettext_noop ("configure logging to write logs to FILENAME"), |
496 | .require_argument = 1, | 511 | .require_argument = 1, |
497 | .processor = &set_filename, | 512 | .processor = &set_filename, |
498 | .scls = (void *) logfn }; | 513 | .scls = (void *) logfn |
514 | }; | ||
499 | 515 | ||
500 | return clo; | 516 | return clo; |
501 | } | 517 | } |
@@ -509,14 +525,15 @@ GNUNET_GETOPT_option_logfile (char **logfn) | |||
509 | struct GNUNET_GETOPT_CommandLineOption | 525 | struct GNUNET_GETOPT_CommandLineOption |
510 | GNUNET_GETOPT_option_cfgfile (char **fn) | 526 | GNUNET_GETOPT_option_cfgfile (char **fn) |
511 | { | 527 | { |
512 | struct GNUNET_GETOPT_CommandLineOption clo = | 528 | struct GNUNET_GETOPT_CommandLineOption clo = { |
513 | { .shortName = 'c', | 529 | .shortName = 'c', |
514 | .name = "config", | 530 | .name = "config", |
515 | .argumentHelp = "FILENAME", | 531 | .argumentHelp = "FILENAME", |
516 | .description = gettext_noop ("use configuration file FILENAME"), | 532 | .description = gettext_noop ("use configuration file FILENAME"), |
517 | .require_argument = 1, | 533 | .require_argument = 1, |
518 | .processor = &set_filename, | 534 | .processor = &set_filename, |
519 | .scls = (void *) fn }; | 535 | .scls = (void *) fn |
536 | }; | ||
520 | 537 | ||
521 | return clo; | 538 | return clo; |
522 | } | 539 | } |
@@ -572,13 +589,80 @@ GNUNET_GETOPT_option_ulong (char shortName, | |||
572 | const char *description, | 589 | const char *description, |
573 | unsigned long long *val) | 590 | unsigned long long *val) |
574 | { | 591 | { |
575 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 592 | struct GNUNET_GETOPT_CommandLineOption clo = { |
576 | .name = name, | 593 | .shortName = shortName, |
577 | .argumentHelp = argumentHelp, | 594 | .name = name, |
578 | .description = description, | 595 | .argumentHelp = argumentHelp, |
579 | .require_argument = 1, | 596 | .description = description, |
580 | .processor = &set_ulong, | 597 | .require_argument = 1, |
581 | .scls = (void *) val }; | 598 | .processor = &set_ulong, |
599 | .scls = (void *) val | ||
600 | }; | ||
601 | |||
602 | return clo; | ||
603 | } | ||
604 | |||
605 | |||
606 | /** | ||
607 | * Set an option of type 'struct GNUNET_TIME_Relative' from the command line. | ||
608 | * A pointer to this function should be passed as part of the | ||
609 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | ||
610 | * of this type. It should be followed by a pointer to a value of | ||
611 | * type 'struct GNUNET_TIME_Relative'. | ||
612 | * | ||
613 | * @param ctx command line processing context | ||
614 | * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative') | ||
615 | * @param option name of the option | ||
616 | * @param value actual value of the option as a string. | ||
617 | * @return #GNUNET_OK if parsing the value worked | ||
618 | */ | ||
619 | static int | ||
620 | set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
621 | void *scls, | ||
622 | const char *option, | ||
623 | const char *value) | ||
624 | { | ||
625 | long long delta; | ||
626 | |||
627 | (void) scls; | ||
628 | (void) ctx; | ||
629 | if (1 != sscanf (value, | ||
630 | "%lld", | ||
631 | &delta)) | ||
632 | { | ||
633 | fprintf (stderr, | ||
634 | _ ("You must pass a number to the `%s' option.\n"), | ||
635 | option); | ||
636 | return GNUNET_SYSERR; | ||
637 | } | ||
638 | GNUNET_TIME_set_offset (delta); | ||
639 | return GNUNET_OK; | ||
640 | } | ||
641 | |||
642 | |||
643 | /** | ||
644 | * Allow user to specify a `long long` with an offset to add to the current | ||
645 | * system time to construct the time seen by the application. Used for | ||
646 | * debugging / testing. | ||
647 | * | ||
648 | * @param shortName short name of the option | ||
649 | * @param name long name of the option | ||
650 | * @param[out] val set to the time specified at the command line | ||
651 | */ | ||
652 | struct GNUNET_GETOPT_CommandLineOption | ||
653 | GNUNET_GETOPT_option_timetravel (char shortName, | ||
654 | const char *name) | ||
655 | { | ||
656 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
657 | .shortName = shortName, | ||
658 | .name = name, | ||
659 | .argumentHelp = _ ("[+/-]MICROSECONDS"), | ||
660 | .description = _ ( | ||
661 | "modify system time by given offset (for debugging/testing only)"), | ||
662 | .require_argument = 1, | ||
663 | .processor = | ||
664 | &set_timetravel_time | ||
665 | }; | ||
582 | 666 | ||
583 | return clo; | 667 | return clo; |
584 | } | 668 | } |
@@ -634,14 +718,16 @@ GNUNET_GETOPT_option_relative_time (char shortName, | |||
634 | const char *description, | 718 | const char *description, |
635 | struct GNUNET_TIME_Relative *val) | 719 | struct GNUNET_TIME_Relative *val) |
636 | { | 720 | { |
637 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 721 | struct GNUNET_GETOPT_CommandLineOption clo = { |
638 | .name = name, | 722 | .shortName = shortName, |
639 | .argumentHelp = argumentHelp, | 723 | .name = name, |
640 | .description = description, | 724 | .argumentHelp = argumentHelp, |
641 | .require_argument = 1, | 725 | .description = description, |
642 | .processor = | 726 | .require_argument = 1, |
643 | &set_relative_time, | 727 | .processor = |
644 | .scls = (void *) val }; | 728 | &set_relative_time, |
729 | .scls = (void *) val | ||
730 | }; | ||
645 | 731 | ||
646 | return clo; | 732 | return clo; |
647 | } | 733 | } |
@@ -697,14 +783,16 @@ GNUNET_GETOPT_option_absolute_time (char shortName, | |||
697 | const char *description, | 783 | const char *description, |
698 | struct GNUNET_TIME_Absolute *val) | 784 | struct GNUNET_TIME_Absolute *val) |
699 | { | 785 | { |
700 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 786 | struct GNUNET_GETOPT_CommandLineOption clo = { |
701 | .name = name, | 787 | .shortName = shortName, |
702 | .argumentHelp = argumentHelp, | 788 | .name = name, |
703 | .description = description, | 789 | .argumentHelp = argumentHelp, |
704 | .require_argument = 1, | 790 | .description = description, |
705 | .processor = | 791 | .require_argument = 1, |
706 | &set_absolute_time, | 792 | .processor = |
707 | .scls = (void *) val }; | 793 | &set_absolute_time, |
794 | .scls = (void *) val | ||
795 | }; | ||
708 | 796 | ||
709 | return clo; | 797 | return clo; |
710 | } | 798 | } |
@@ -737,7 +825,7 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
737 | { | 825 | { |
738 | fprintf (stderr, | 826 | fprintf (stderr, |
739 | _ ( | 827 | _ ( |
740 | "Your input for the '%s' option has to be a non negative number \n"), | 828 | "Your input for the '%s' option has to be a non negative number\n"), |
741 | option); | 829 | option); |
742 | return GNUNET_SYSERR; | 830 | return GNUNET_SYSERR; |
743 | } | 831 | } |
@@ -768,13 +856,15 @@ GNUNET_GETOPT_option_uint (char shortName, | |||
768 | const char *description, | 856 | const char *description, |
769 | unsigned int *val) | 857 | unsigned int *val) |
770 | { | 858 | { |
771 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 859 | struct GNUNET_GETOPT_CommandLineOption clo = { |
772 | .name = name, | 860 | .shortName = shortName, |
773 | .argumentHelp = argumentHelp, | 861 | .name = name, |
774 | .description = description, | 862 | .argumentHelp = argumentHelp, |
775 | .require_argument = 1, | 863 | .description = description, |
776 | .processor = &set_uint, | 864 | .require_argument = 1, |
777 | .scls = (void *) val }; | 865 | .processor = &set_uint, |
866 | .scls = (void *) val | ||
867 | }; | ||
778 | 868 | ||
779 | return clo; | 869 | return clo; |
780 | } | 870 | } |
@@ -840,13 +930,15 @@ GNUNET_GETOPT_option_uint16 (char shortName, | |||
840 | const char *description, | 930 | const char *description, |
841 | uint16_t *val) | 931 | uint16_t *val) |
842 | { | 932 | { |
843 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 933 | struct GNUNET_GETOPT_CommandLineOption clo = { |
844 | .name = name, | 934 | .shortName = shortName, |
845 | .argumentHelp = argumentHelp, | 935 | .name = name, |
846 | .description = description, | 936 | .argumentHelp = argumentHelp, |
847 | .require_argument = 1, | 937 | .description = description, |
848 | .processor = &set_uint16, | 938 | .require_argument = 1, |
849 | .scls = (void *) val }; | 939 | .processor = &set_uint16, |
940 | .scls = (void *) val | ||
941 | }; | ||
850 | 942 | ||
851 | return clo; | 943 | return clo; |
852 | } | 944 | } |
@@ -940,14 +1032,16 @@ GNUNET_GETOPT_option_base32_fixed_size (char shortName, | |||
940 | size_t val_size) | 1032 | size_t val_size) |
941 | { | 1033 | { |
942 | struct Base32Context *bc = GNUNET_new (struct Base32Context); | 1034 | struct Base32Context *bc = GNUNET_new (struct Base32Context); |
943 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 1035 | struct GNUNET_GETOPT_CommandLineOption clo = { |
944 | .name = name, | 1036 | .shortName = shortName, |
945 | .argumentHelp = argumentHelp, | 1037 | .name = name, |
946 | .description = description, | 1038 | .argumentHelp = argumentHelp, |
947 | .require_argument = 1, | 1039 | .description = description, |
948 | .processor = &set_base32, | 1040 | .require_argument = 1, |
949 | .cleaner = &free_bc, | 1041 | .processor = &set_base32, |
950 | .scls = (void *) bc }; | 1042 | .cleaner = &free_bc, |
1043 | .scls = (void *) bc | ||
1044 | }; | ||
951 | 1045 | ||
952 | bc->val = val; | 1046 | bc->val = val; |
953 | bc->val_size = val_size; | 1047 | bc->val_size = val_size; |