aboutsummaryrefslogtreecommitdiff
path: root/src/util/getopt_helpers.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-03-15 10:16:42 +0100
committerChristian Grothoff <christian@grothoff.org>2017-03-15 11:14:58 +0100
commit164eed8f5171824cf27b318afeab12f8c6ba9262 (patch)
treec84580c2569579cd554bf2f1426010eadaef0f90 /src/util/getopt_helpers.c
parent30eb0faa8d4894659cc90c76cc634148df86eab9 (diff)
downloadgnunet-164eed8f5171824cf27b318afeab12f8c6ba9262.tar.gz
gnunet-164eed8f5171824cf27b318afeab12f8c6ba9262.zip
fix test case, implement base32 argument parser logic
Diffstat (limited to 'src/util/getopt_helpers.c')
-rw-r--r--src/util/getopt_helpers.c105
1 files changed, 104 insertions, 1 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index 234f5371f..9f6f4c764 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -266,7 +266,7 @@ GNUNET_GETOPT_OPTION_INCREMENT_VALUE (char shortName,
266 * @param[out] level set to the verbosity level 266 * @param[out] level set to the verbosity level
267 */ 267 */
268struct GNUNET_GETOPT_CommandLineOption 268struct GNUNET_GETOPT_CommandLineOption
269GNUNET_GETOPT_OPTION_VERBOSE (int *level) 269GNUNET_GETOPT_OPTION_VERBOSE (unsigned int *level)
270{ 270{
271 struct GNUNET_GETOPT_CommandLineOption clo = { 271 struct GNUNET_GETOPT_CommandLineOption clo = {
272 .shortName = 'V', 272 .shortName = 'V',
@@ -710,4 +710,107 @@ GNUNET_GETOPT_OPTION_SET_UINT (char shortName,
710} 710}
711 711
712 712
713/**
714 * Closure for #set_base32().
715 */
716struct Base32Context
717{
718 /**
719 * Value to initialize (already allocated)
720 */
721 void *val;
722
723 /**
724 * Number of bytes expected for @e val.
725 */
726 size_t val_size;
727};
728
729
730/**
731 * Set an option of type 'unsigned int' from the command line.
732 * A pointer to this function should be passed as part of the
733 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
734 * of this type. It should be followed by a pointer to a value of
735 * type 'unsigned int'.
736 *
737 * @param ctx command line processing context
738 * @param scls additional closure (will point to the 'unsigned int')
739 * @param option name of the option
740 * @param value actual value of the option as a string.
741 * @return #GNUNET_OK if parsing the value worked
742 */
743static int
744set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
745 void *scls,
746 const char *option,
747 const char *value)
748{
749 struct Base32Context *bc = scls;
750
751 if (GNUNET_OK !=
752 GNUNET_STRINGS_string_to_data (value,
753 strlen (value),
754 bc->val,
755 bc->val_size))
756 {
757 fprintf (stderr,
758 _("Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"),
759 option);
760 return GNUNET_SYSERR;
761 }
762 return GNUNET_OK;
763}
764
765
766/**
767 * Helper function to clean up after
768 * #GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE.
769 *
770 * @param cls value to GNUNET_free()
771 */
772static void
773free_bc (void *cls)
774{
775 GNUNET_free (cls);
776}
777
778
779/**
780 * Allow user to specify a binary value using Crockford
781 * Base32 encoding.
782 *
783 * @param shortName short name of the option
784 * @param name long name of the option
785 * @param argumentHelp help text for the option argument
786 * @param description long help text for the option
787 * @param[out] val binary value decoded from Crockford Base32-encoded argument
788 * @param val_size size of @a val in bytes
789 */
790struct GNUNET_GETOPT_CommandLineOption
791GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE (char shortName,
792 const char *name,
793 const char *argumentHelp,
794 const char *description,
795 void *val,
796 size_t val_size)
797{
798 struct Base32Context *bc = GNUNET_new (struct Base32Context);
799 struct GNUNET_GETOPT_CommandLineOption clo = {
800 .shortName = shortName,
801 .name = name,
802 .argumentHelp = argumentHelp,
803 .description = description,
804 .require_argument = 1,
805 .processor = &set_base32,
806 .cleaner = &free_bc,
807 .scls = (void *) bc
808 };
809
810 bc->val = val;
811 bc->val_size = val_size;
812 return clo;
813}
814
815
713/* end of getopt_helpers.c */ 816/* end of getopt_helpers.c */