aboutsummaryrefslogtreecommitdiff
path: root/src/util/getopt.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.c
parent30eb0faa8d4894659cc90c76cc634148df86eab9 (diff)
downloadgnunet-164eed8f5171824cf27b318afeab12f8c6ba9262.tar.gz
gnunet-164eed8f5171824cf27b318afeab12f8c6ba9262.zip
fix test case, implement base32 argument parser logic
Diffstat (limited to 'src/util/getopt.c')
-rw-r--r--src/util/getopt.c56
1 files changed, 35 insertions, 21 deletions
diff --git a/src/util/getopt.c b/src/util/getopt.c
index ff62dba9b..85f67500c 100644
--- a/src/util/getopt.c
+++ b/src/util/getopt.c
@@ -26,7 +26,7 @@ USA.
26 26
27 27
28This code was heavily modified for GNUnet. 28This code was heavily modified for GNUnet.
29Copyright Copyright (C) 2006 Christian Grothoff 29Copyright Copyright (C) 2006, 2017 Christian Grothoff
30*/ 30*/
31 31
32/** 32/**
@@ -845,9 +845,13 @@ GN_getopt_internal (int argc, char *const *argv, const char *optstring,
845 } 845 }
846} 846}
847 847
848
848static int 849static int
849GNgetopt_long (int argc, char *const *argv, const char *options, 850GNgetopt_long (int argc,
850 const struct GNoption *long_options, int *opt_index) 851 char *const *argv,
852 const char *options,
853 const struct GNoption *long_options,
854 int *opt_index)
851{ 855{
852 return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0); 856 return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0);
853} 857}
@@ -867,12 +871,12 @@ GNgetopt_long (int argc, char *const *argv, const char *options,
867int 871int
868GNUNET_GETOPT_run (const char *binaryOptions, 872GNUNET_GETOPT_run (const char *binaryOptions,
869 const struct GNUNET_GETOPT_CommandLineOption *allOptions, 873 const struct GNUNET_GETOPT_CommandLineOption *allOptions,
870 unsigned int argc, char *const *argv) 874 unsigned int argc,
875 char *const *argv)
871{ 876{
872 struct GNoption *long_options; 877 struct GNoption *long_options;
873 struct GNUNET_GETOPT_CommandLineProcessorContext clpc; 878 struct GNUNET_GETOPT_CommandLineProcessorContext clpc;
874 int count; 879 int count;
875 int i;
876 char *shorts; 880 char *shorts;
877 int spos; 881 int spos;
878 int cont; 882 int cont;
@@ -885,13 +889,13 @@ GNUNET_GETOPT_run (const char *binaryOptions,
885 clpc.allOptions = allOptions; 889 clpc.allOptions = allOptions;
886 clpc.argv = argv; 890 clpc.argv = argv;
887 clpc.argc = argc; 891 clpc.argc = argc;
888 count = 0; 892 for (count = 0; NULL != allOptions[count].name; count++) ;
889 while (allOptions[count].name != NULL) 893
890 count++; 894 long_options = GNUNET_new_array (count + 1,
891 long_options = GNUNET_malloc (sizeof (struct GNoption) * (count + 1)); 895 struct GNoption);
892 shorts = GNUNET_malloc (count * 2 + 1); 896 shorts = GNUNET_malloc (count * 2 + 1);
893 spos = 0; 897 spos = 0;
894 for (i = 0; i < count; i++) 898 for (unsigned i = 0; i < count; i++)
895 { 899 {
896 long_options[i].name = allOptions[i].name; 900 long_options[i].name = allOptions[i].name;
897 long_options[i].has_arg = allOptions[i].require_argument; 901 long_options[i].has_arg = allOptions[i].require_argument;
@@ -907,13 +911,17 @@ GNUNET_GETOPT_run (const char *binaryOptions,
907 long_options[count].val = '\0'; 911 long_options[count].val = '\0';
908 shorts[spos] = '\0'; 912 shorts[spos] = '\0';
909 cont = GNUNET_OK; 913 cont = GNUNET_OK;
914
910 /* main getopt loop */ 915 /* main getopt loop */
911 while (cont == GNUNET_OK) 916 while (GNUNET_OK == cont)
912 { 917 {
913 int option_index = 0; 918 int option_index = 0;
919 unsigned int i;
914 920
915 c = GNgetopt_long (argc, argv, shorts, long_options, &option_index); 921 c = GNgetopt_long (argc, argv,
916 922 shorts,
923 long_options,
924 &option_index);
917 if (c == GNUNET_SYSERR) 925 if (c == GNUNET_SYSERR)
918 break; /* No more flags to process */ 926 break; /* No more flags to process */
919 927
@@ -922,25 +930,31 @@ GNUNET_GETOPT_run (const char *binaryOptions,
922 clpc.currentArgument = GNoptind - 1; 930 clpc.currentArgument = GNoptind - 1;
923 if ((char) c == allOptions[i].shortName) 931 if ((char) c == allOptions[i].shortName)
924 { 932 {
925 cont = 933 cont = allOptions[i].processor (&clpc,
926 allOptions[i].processor (&clpc, allOptions[i].scls, 934 allOptions[i].scls,
927 allOptions[i].name, GNoptarg); 935 allOptions[i].name,
936 GNoptarg);
928 break; 937 break;
929 } 938 }
930 } 939 }
931 if (i == count) 940 if (i == count)
932 { 941 {
933 FPRINTF (stderr, _("Use %s to get a list of options.\n"), "--help"); 942 FPRINTF (stderr,
943 _("Use %s to get a list of options.\n"),
944 "--help");
934 cont = GNUNET_SYSERR; 945 cont = GNUNET_SYSERR;
935 } 946 }
936 } 947 }
937
938 GNUNET_free (shorts); 948 GNUNET_free (shorts);
939 GNUNET_free (long_options); 949 GNUNET_free (long_options);
940 if (cont != GNUNET_OK) 950
941 { 951 /* call cleaners, if available */
952 for (count = 0; NULL != allOptions[count].name; count++)
953 if (NULL != allOptions[count].cleaner)
954 allOptions[count].cleaner (allOptions[count].scls);
955
956 if (GNUNET_OK != cont)
942 return cont; 957 return cont;
943 }
944 return GNoptind; 958 return GNoptind;
945} 959}
946 960