aboutsummaryrefslogtreecommitdiff
path: root/src/util/getopt_helpers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/getopt_helpers.c')
-rw-r--r--src/util/getopt_helpers.c249
1 files changed, 80 insertions, 169 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index 592875531..917aa440b 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -38,7 +38,7 @@
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 */
41static int 41static enum GNUNET_GenericReturnValue
42print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 42print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
43 void *scls, 43 void *scls,
44 const char *option, 44 const char *option,
@@ -53,12 +53,6 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
53} 53}
54 54
55 55
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 */
62struct GNUNET_GETOPT_CommandLineOption 56struct GNUNET_GETOPT_CommandLineOption
63GNUNET_GETOPT_option_version (const char *version) 57GNUNET_GETOPT_option_version (const char *version)
64{ 58{
@@ -90,7 +84,7 @@ GNUNET_GETOPT_option_version (const char *version)
90 * @param value not used (NULL) 84 * @param value not used (NULL)
91 * @return #GNUNET_NO (do not continue, not an error) 85 * @return #GNUNET_NO (do not continue, not an error)
92 */ 86 */
93static int 87static enum GNUNET_GenericReturnValue
94format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 88format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
95 void *scls, 89 void *scls,
96 const char *option, 90 const char *option,
@@ -192,12 +186,6 @@ OUTER:
192} 186}
193 187
194 188
195/**
196 * Defining the option to print the command line
197 * help text (-h option).
198 *
199 * @param about string with brief description of the application
200 */
201struct GNUNET_GETOPT_CommandLineOption 189struct GNUNET_GETOPT_CommandLineOption
202GNUNET_GETOPT_option_help (const char *about) 190GNUNET_GETOPT_option_help (const char *about)
203{ 191{
@@ -229,7 +217,7 @@ GNUNET_GETOPT_option_help (const char *about)
229 * @param value not used (NULL) 217 * @param value not used (NULL)
230 * @return #GNUNET_OK 218 * @return #GNUNET_OK
231 */ 219 */
232static int 220static enum GNUNET_GenericReturnValue
233increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 221increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
234 void *scls, 222 void *scls,
235 const char *option, 223 const char *option,
@@ -245,15 +233,6 @@ increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
245} 233}
246 234
247 235
248/**
249 * Increment @a val each time the option flag is given by one.
250 *
251 * @param shortName short name of the option
252 * @param name long name of the option
253 * @param argumentHelp help text for the option argument
254 * @param description long help text for the option
255 * @param[out] val increment by 1 each time the option is present
256 */
257struct GNUNET_GETOPT_CommandLineOption 236struct GNUNET_GETOPT_CommandLineOption
258GNUNET_GETOPT_option_increment_uint (char shortName, 237GNUNET_GETOPT_option_increment_uint (char shortName,
259 const char *name, 238 const char *name,
@@ -272,12 +251,6 @@ GNUNET_GETOPT_option_increment_uint (char shortName,
272} 251}
273 252
274 253
275/**
276 * Define the '-V' verbosity option. Using the option more
277 * than once increments @a level each time.
278 *
279 * @param[out] level set to the verbosity level
280 */
281struct GNUNET_GETOPT_CommandLineOption 254struct GNUNET_GETOPT_CommandLineOption
282GNUNET_GETOPT_option_verbose (unsigned int *level) 255GNUNET_GETOPT_option_verbose (unsigned int *level)
283{ 256{
@@ -308,7 +281,7 @@ GNUNET_GETOPT_option_verbose (unsigned int *level)
308 * @param value not used (NULL) 281 * @param value not used (NULL)
309 * @return #GNUNET_OK 282 * @return #GNUNET_OK
310 */ 283 */
311static int 284static enum GNUNET_GenericReturnValue
312set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 285set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
313 void *scls, 286 void *scls,
314 const char *option, 287 const char *option,
@@ -324,16 +297,6 @@ set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
324} 297}
325 298
326 299
327/**
328 * Allow user to specify a flag (which internally means setting
329 * an integer to 1/#GNUNET_YES/#GNUNET_OK.
330 *
331 * @param shortName short name of the option
332 * @param name long name of the option
333 * @param argumentHelp help text for the option argument
334 * @param description long help text for the option
335 * @param[out] val set to 1 if the option is present
336 */
337struct GNUNET_GETOPT_CommandLineOption 300struct GNUNET_GETOPT_CommandLineOption
338GNUNET_GETOPT_option_flag (char shortName, 301GNUNET_GETOPT_option_flag (char shortName,
339 const char *name, 302 const char *name,
@@ -366,7 +329,7 @@ GNUNET_GETOPT_option_flag (char shortName,
366 * @param value actual value of the option (a string) 329 * @param value actual value of the option (a string)
367 * @return #GNUNET_OK 330 * @return #GNUNET_OK
368 */ 331 */
369static int 332static enum GNUNET_GenericReturnValue
370set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 333set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
371 void *scls, 334 void *scls,
372 const char *option, 335 const char *option,
@@ -383,15 +346,6 @@ set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
383} 346}
384 347
385 348
386/**
387 * Allow user to specify a string.
388 *
389 * @param shortName short name of the option
390 * @param name long name of the option
391 * @param argumentHelp help text for the option argument
392 * @param description long help text for the option
393 * @param[out] str set to the string
394 */
395struct GNUNET_GETOPT_CommandLineOption 349struct GNUNET_GETOPT_CommandLineOption
396GNUNET_GETOPT_option_string (char shortName, 350GNUNET_GETOPT_option_string (char shortName,
397 const char *name, 351 const char *name,
@@ -413,12 +367,6 @@ GNUNET_GETOPT_option_string (char shortName,
413} 367}
414 368
415 369
416/**
417 * Define the '-L' log level option. Note that we do not check
418 * that the log level is valid here.
419 *
420 * @param[out] level set to the log level
421 */
422struct GNUNET_GETOPT_CommandLineOption 370struct GNUNET_GETOPT_CommandLineOption
423GNUNET_GETOPT_option_loglevel (char **level) 371GNUNET_GETOPT_option_loglevel (char **level)
424{ 372{
@@ -447,7 +395,7 @@ GNUNET_GETOPT_option_loglevel (char **level)
447 * @param value actual value of the option (a string) 395 * @param value actual value of the option (a string)
448 * @return #GNUNET_OK 396 * @return #GNUNET_OK
449 */ 397 */
450static int 398static enum GNUNET_GenericReturnValue
451set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 399set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
452 void *scls, 400 void *scls,
453 const char *option, 401 const char *option,
@@ -464,15 +412,6 @@ set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
464} 412}
465 413
466 414
467/**
468 * Allow user to specify a filename (automatically path expanded).
469 *
470 * @param shortName short name of the option
471 * @param name long name of the option
472 * @param argumentHelp help text for the option argument
473 * @param description long help text for the option
474 * @param[out] str set to the string
475 */
476struct GNUNET_GETOPT_CommandLineOption 415struct GNUNET_GETOPT_CommandLineOption
477GNUNET_GETOPT_option_filename (char shortName, 416GNUNET_GETOPT_option_filename (char shortName,
478 const char *name, 417 const char *name,
@@ -494,11 +433,6 @@ GNUNET_GETOPT_option_filename (char shortName,
494} 433}
495 434
496 435
497/**
498 * Allow user to specify log file name (-l option)
499 *
500 * @param[out] logfn set to the name of the logfile
501 */
502struct GNUNET_GETOPT_CommandLineOption 436struct GNUNET_GETOPT_CommandLineOption
503GNUNET_GETOPT_option_logfile (char **logfn) 437GNUNET_GETOPT_option_logfile (char **logfn)
504{ 438{
@@ -517,11 +451,6 @@ GNUNET_GETOPT_option_logfile (char **logfn)
517} 451}
518 452
519 453
520/**
521 * Allow user to specify configuration file name (-c option)
522 *
523 * @param[out] fn set to the name of the configuration file
524 */
525struct GNUNET_GETOPT_CommandLineOption 454struct GNUNET_GETOPT_CommandLineOption
526GNUNET_GETOPT_option_cfgfile (char **fn) 455GNUNET_GETOPT_option_cfgfile (char **fn)
527{ 456{
@@ -552,7 +481,7 @@ GNUNET_GETOPT_option_cfgfile (char **fn)
552 * @param value actual value of the option as a string. 481 * @param value actual value of the option as a string.
553 * @return #GNUNET_OK if parsing the value worked 482 * @return #GNUNET_OK if parsing the value worked
554 */ 483 */
555static int 484static enum GNUNET_GenericReturnValue
556set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 485set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
557 void *scls, 486 void *scls,
558 const char *option, 487 const char *option,
@@ -573,15 +502,6 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
573} 502}
574 503
575 504
576/**
577 * Allow user to specify an `unsigned long long`
578 *
579 * @param shortName short name of the option
580 * @param name long name of the option
581 * @param argumentHelp help text for the option argument
582 * @param description long help text for the option
583 * @param[out] val set to the value specified at the command line
584 */
585struct GNUNET_GETOPT_CommandLineOption 505struct GNUNET_GETOPT_CommandLineOption
586GNUNET_GETOPT_option_ulong (char shortName, 506GNUNET_GETOPT_option_ulong (char shortName,
587 const char *name, 507 const char *name,
@@ -616,7 +536,7 @@ GNUNET_GETOPT_option_ulong (char shortName,
616 * @param value actual value of the option as a string. 536 * @param value actual value of the option as a string.
617 * @return #GNUNET_OK if parsing the value worked 537 * @return #GNUNET_OK if parsing the value worked
618 */ 538 */
619static int 539static enum GNUNET_GenericReturnValue
620set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 540set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
621 void *scls, 541 void *scls,
622 const char *option, 542 const char *option,
@@ -664,15 +584,6 @@ set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
664} 584}
665 585
666 586
667/**
668 * Allow user to specify a `long long` with an offset to add to the current
669 * system time to construct the time seen by the application. Used for
670 * debugging / testing.
671 *
672 * @param shortName short name of the option
673 * @param name long name of the option
674 * @param[out] val set to the time specified at the command line
675 */
676struct GNUNET_GETOPT_CommandLineOption 587struct GNUNET_GETOPT_CommandLineOption
677GNUNET_GETOPT_option_timetravel (char shortName, 588GNUNET_GETOPT_option_timetravel (char shortName,
678 const char *name) 589 const char *name)
@@ -684,8 +595,7 @@ GNUNET_GETOPT_option_timetravel (char shortName,
684 .description = _ ( 595 .description = _ (
685 "modify system time by given offset (for debugging/testing only)"), 596 "modify system time by given offset (for debugging/testing only)"),
686 .require_argument = 1, 597 .require_argument = 1,
687 .processor = 598 .processor = &set_timetravel_time
688 &set_timetravel_time
689 }; 599 };
690 600
691 return clo; 601 return clo;
@@ -705,7 +615,7 @@ GNUNET_GETOPT_option_timetravel (char shortName,
705 * @param value actual value of the option as a string. 615 * @param value actual value of the option as a string.
706 * @return #GNUNET_OK if parsing the value worked 616 * @return #GNUNET_OK if parsing the value worked
707 */ 617 */
708static int 618static enum GNUNET_GenericReturnValue
709set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 619set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
710 void *scls, 620 void *scls,
711 const char *option, 621 const char *option,
@@ -725,16 +635,6 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
725} 635}
726 636
727 637
728/**
729 * Allow user to specify a `struct GNUNET_TIME_Relative`
730 * (using human-readable "fancy" time).
731 *
732 * @param shortName short name of the option
733 * @param name long name of the option
734 * @param argumentHelp help text for the option argument
735 * @param description long help text for the option
736 * @param[out] val set to the time specified at the command line
737 */
738struct GNUNET_GETOPT_CommandLineOption 638struct GNUNET_GETOPT_CommandLineOption
739GNUNET_GETOPT_option_relative_time (char shortName, 639GNUNET_GETOPT_option_relative_time (char shortName,
740 const char *name, 640 const char *name,
@@ -748,8 +648,7 @@ GNUNET_GETOPT_option_relative_time (char shortName,
748 .argumentHelp = argumentHelp, 648 .argumentHelp = argumentHelp,
749 .description = description, 649 .description = description,
750 .require_argument = 1, 650 .require_argument = 1,
751 .processor = 651 .processor = &set_relative_time,
752 &set_relative_time,
753 .scls = (void *) val 652 .scls = (void *) val
754 }; 653 };
755 654
@@ -770,7 +669,7 @@ GNUNET_GETOPT_option_relative_time (char shortName,
770 * @param value actual value of the option as a string. 669 * @param value actual value of the option as a string.
771 * @return #GNUNET_OK if parsing the value worked 670 * @return #GNUNET_OK if parsing the value worked
772 */ 671 */
773static int 672static enum GNUNET_GenericReturnValue
774set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 673set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
775 void *scls, 674 void *scls,
776 const char *option, 675 const char *option,
@@ -790,16 +689,6 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
790} 689}
791 690
792 691
793/**
794 * Allow user to specify a `struct GNUNET_TIME_Absolute`
795 * (using human-readable "fancy" time).
796 *
797 * @param shortName short name of the option
798 * @param name long name of the option
799 * @param argumentHelp help text for the option argument
800 * @param description long help text for the option
801 * @param[out] val set to the time specified at the command line
802 */
803struct GNUNET_GETOPT_CommandLineOption 692struct GNUNET_GETOPT_CommandLineOption
804GNUNET_GETOPT_option_absolute_time (char shortName, 693GNUNET_GETOPT_option_absolute_time (char shortName,
805 const char *name, 694 const char *name,
@@ -813,8 +702,71 @@ GNUNET_GETOPT_option_absolute_time (char shortName,
813 .argumentHelp = argumentHelp, 702 .argumentHelp = argumentHelp,
814 .description = description, 703 .description = description,
815 .require_argument = 1, 704 .require_argument = 1,
816 .processor = 705 .processor = &set_absolute_time,
817 &set_absolute_time, 706 .scls = (void *) val
707 };
708
709 return clo;
710}
711
712
713/**
714 * Set an option of type 'struct GNUNET_TIME_Timestamp' from the command line.
715 * A pointer to this function should be passed as part of the
716 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
717 * of this type. It should be followed by a pointer to a value of
718 * type 'struct GNUNET_TIME_Absolute'.
719 *
720 * @param ctx command line processing context
721 * @param scls additional closure (will point to the `struct GNUNET_TIME_Absolute`)
722 * @param option name of the option
723 * @param value actual value of the option as a string.
724 * @return #GNUNET_OK if parsing the value worked
725 */
726static enum GNUNET_GenericReturnValue
727set_timestamp (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
728 void *scls,
729 const char *option,
730 const char *value)
731{
732 struct GNUNET_TIME_Timestamp *t = scls;
733 struct GNUNET_TIME_Absolute abs;
734
735 (void) ctx;
736 if (GNUNET_OK !=
737 GNUNET_STRINGS_fancy_time_to_absolute (value,
738 &abs))
739 {
740 fprintf (stderr,
741 _ ("You must pass a timestamp to the `%s' option.\n"),
742 option);
743 return GNUNET_SYSERR;
744 }
745 if (0 != abs.abs_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us)
746 {
747 fprintf (stderr,
748 _ ("The maximum precision allowed for timestamps is seconds.\n"));
749 return GNUNET_SYSERR;
750 }
751 t->abs_time = abs;
752 return GNUNET_OK;
753}
754
755
756struct GNUNET_GETOPT_CommandLineOption
757GNUNET_GETOPT_option_timestamp (char shortName,
758 const char *name,
759 const char *argumentHelp,
760 const char *description,
761 struct GNUNET_TIME_Timestamp *val)
762{
763 struct GNUNET_GETOPT_CommandLineOption clo = {
764 .shortName = shortName,
765 .name = name,
766 .argumentHelp = argumentHelp,
767 .description = description,
768 .require_argument = 1,
769 .processor = &set_timestamp,
818 .scls = (void *) val 770 .scls = (void *) val
819 }; 771 };
820 772
@@ -835,7 +787,7 @@ GNUNET_GETOPT_option_absolute_time (char shortName,
835 * @param value actual value of the option as a string. 787 * @param value actual value of the option as a string.
836 * @return #GNUNET_OK if parsing the value worked 788 * @return #GNUNET_OK if parsing the value worked
837 */ 789 */
838static int 790static enum GNUNET_GenericReturnValue
839set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 791set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
840 void *scls, 792 void *scls,
841 const char *option, 793 const char *option,
@@ -864,15 +816,6 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
864} 816}
865 817
866 818
867/**
868 * Allow user to specify an unsigned integer.
869 *
870 * @param shortName short name of the option
871 * @param name long name of the option
872 * @param argumentHelp help text for the option argument
873 * @param description long help text for the option
874 * @param[out] val set to the value specified at the command line
875 */
876struct GNUNET_GETOPT_CommandLineOption 819struct GNUNET_GETOPT_CommandLineOption
877GNUNET_GETOPT_option_uint (char shortName, 820GNUNET_GETOPT_option_uint (char shortName,
878 const char *name, 821 const char *name,
@@ -907,7 +850,7 @@ GNUNET_GETOPT_option_uint (char shortName,
907 * @param value actual value of the option as a string. 850 * @param value actual value of the option as a string.
908 * @return #GNUNET_OK if parsing the value worked 851 * @return #GNUNET_OK if parsing the value worked
909 */ 852 */
910static int 853static enum GNUNET_GenericReturnValue
911set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 854set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
912 void *scls, 855 void *scls,
913 const char *option, 856 const char *option,
@@ -938,15 +881,6 @@ set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
938} 881}
939 882
940 883
941/**
942 * Allow user to specify an uint16_t.
943 *
944 * @param shortName short name of the option
945 * @param name long name of the option
946 * @param argumentHelp help text for the option argument
947 * @param description long help text for the option
948 * @param[out] val set to the value specified at the command line
949 */
950struct GNUNET_GETOPT_CommandLineOption 884struct GNUNET_GETOPT_CommandLineOption
951GNUNET_GETOPT_option_uint16 (char shortName, 885GNUNET_GETOPT_option_uint16 (char shortName,
952 const char *name, 886 const char *name,
@@ -998,7 +932,7 @@ struct Base32Context
998 * @param value actual value of the option as a string. 932 * @param value actual value of the option as a string.
999 * @return #GNUNET_OK if parsing the value worked 933 * @return #GNUNET_OK if parsing the value worked
1000 */ 934 */
1001static int 935static enum GNUNET_GenericReturnValue
1002set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 936set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
1003 void *scls, 937 void *scls,
1004 const char *option, 938 const char *option,
@@ -1036,17 +970,6 @@ free_bc (void *cls)
1036} 970}
1037 971
1038 972
1039/**
1040 * Allow user to specify a binary value using Crockford
1041 * Base32 encoding.
1042 *
1043 * @param shortName short name of the option
1044 * @param name long name of the option
1045 * @param argumentHelp help text for the option argument
1046 * @param description long help text for the option
1047 * @param[out] val binary value decoded from Crockford Base32-encoded argument
1048 * @param val_size size of @a val in bytes
1049 */
1050struct GNUNET_GETOPT_CommandLineOption 973struct GNUNET_GETOPT_CommandLineOption
1051GNUNET_GETOPT_option_base32_fixed_size (char shortName, 974GNUNET_GETOPT_option_base32_fixed_size (char shortName,
1052 const char *name, 975 const char *name,
@@ -1073,12 +996,6 @@ GNUNET_GETOPT_option_base32_fixed_size (char shortName,
1073} 996}
1074 997
1075 998
1076/**
1077 * Make the given option mandatory.
1078 *
1079 * @param opt option to modify
1080 * @return @a opt with the mandatory flag set.
1081 */
1082struct GNUNET_GETOPT_CommandLineOption 999struct GNUNET_GETOPT_CommandLineOption
1083GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt) 1000GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
1084{ 1001{
@@ -1087,12 +1004,6 @@ GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
1087} 1004}
1088 1005
1089 1006
1090/**
1091 * Make the given option mutually exclusive with other options.
1092 *
1093 * @param opt option to modify
1094 * @return @a opt with the exclusive flag set.
1095 */
1096struct GNUNET_GETOPT_CommandLineOption 1007struct GNUNET_GETOPT_CommandLineOption
1097GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt) 1008GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt)
1098{ 1009{