diff options
-rw-r--r-- | src/include/gnunet_getopt_lib.h | 17 | ||||
-rw-r--r-- | src/util/getopt.c | 18 | ||||
-rw-r--r-- | src/util/getopt_helpers.c | 16 | ||||
-rw-r--r-- | src/util/test_getopt.c | 13 | ||||
-rw-r--r-- | src/util/test_program.c | 129 |
5 files changed, 132 insertions, 61 deletions
diff --git a/src/include/gnunet_getopt_lib.h b/src/include/gnunet_getopt_lib.h index bc2e079da..c2bd72340 100644 --- a/src/include/gnunet_getopt_lib.h +++ b/src/include/gnunet_getopt_lib.h | |||
@@ -131,6 +131,11 @@ struct GNUNET_GETOPT_CommandLineOption | |||
131 | int require_argument; | 131 | int require_argument; |
132 | 132 | ||
133 | /** | 133 | /** |
134 | * Is the presence of this option mandatory? | ||
135 | */ | ||
136 | int option_mandatory; | ||
137 | |||
138 | /** | ||
134 | * Handler for the option. | 139 | * Handler for the option. |
135 | */ | 140 | */ |
136 | GNUNET_GETOPT_CommandLineOptionProcessor processor; | 141 | GNUNET_GETOPT_CommandLineOptionProcessor processor; |
@@ -389,10 +394,20 @@ GNUNET_GETOPT_OPTION_CFG_FILE (char **fn); | |||
389 | 394 | ||
390 | 395 | ||
391 | /** | 396 | /** |
397 | * Make the given option mandatory. | ||
398 | * | ||
399 | * @param opt option to modify | ||
400 | * @return @a opt with the mandatory flag set. | ||
401 | */ | ||
402 | struct GNUNET_GETOPT_CommandLineOption | ||
403 | GNUNET_GETOPT_OPTION_MANDATORY (struct GNUNET_GETOPT_CommandLineOption opt); | ||
404 | |||
405 | |||
406 | /** | ||
392 | * Marker for the end of the list of options. | 407 | * Marker for the end of the list of options. |
393 | */ | 408 | */ |
394 | #define GNUNET_GETOPT_OPTION_END \ | 409 | #define GNUNET_GETOPT_OPTION_END \ |
395 | { '\0', NULL, NULL, NULL, 0, NULL, NULL, NULL } | 410 | { '\0', NULL, NULL, NULL, 0, 0, NULL, NULL, NULL } |
396 | 411 | ||
397 | 412 | ||
398 | /** | 413 | /** |
diff --git a/src/util/getopt.c b/src/util/getopt.c index 85f67500c..036e0f4be 100644 --- a/src/util/getopt.c +++ b/src/util/getopt.c | |||
@@ -881,6 +881,7 @@ GNUNET_GETOPT_run (const char *binaryOptions, | |||
881 | int spos; | 881 | int spos; |
882 | int cont; | 882 | int cont; |
883 | int c; | 883 | int c; |
884 | uint8_t *seen; | ||
884 | 885 | ||
885 | GNUNET_assert (argc > 0); | 886 | GNUNET_assert (argc > 0); |
886 | GNoptind = 0; | 887 | GNoptind = 0; |
@@ -893,6 +894,8 @@ GNUNET_GETOPT_run (const char *binaryOptions, | |||
893 | 894 | ||
894 | long_options = GNUNET_new_array (count + 1, | 895 | long_options = GNUNET_new_array (count + 1, |
895 | struct GNoption); | 896 | struct GNoption); |
897 | seen = GNUNET_new_array (count, | ||
898 | uint8_t); | ||
896 | shorts = GNUNET_malloc (count * 2 + 1); | 899 | shorts = GNUNET_malloc (count * 2 + 1); |
897 | spos = 0; | 900 | spos = 0; |
898 | for (unsigned i = 0; i < count; i++) | 901 | for (unsigned i = 0; i < count; i++) |
@@ -934,6 +937,7 @@ GNUNET_GETOPT_run (const char *binaryOptions, | |||
934 | allOptions[i].scls, | 937 | allOptions[i].scls, |
935 | allOptions[i].name, | 938 | allOptions[i].name, |
936 | GNoptarg); | 939 | GNoptarg); |
940 | seen[i] = 1; | ||
937 | break; | 941 | break; |
938 | } | 942 | } |
939 | } | 943 | } |
@@ -948,6 +952,20 @@ GNUNET_GETOPT_run (const char *binaryOptions, | |||
948 | GNUNET_free (shorts); | 952 | GNUNET_free (shorts); |
949 | GNUNET_free (long_options); | 953 | GNUNET_free (long_options); |
950 | 954 | ||
955 | if (GNUNET_YES == cont) | ||
956 | { | ||
957 | for (count = 0; NULL != allOptions[count].name; count++) | ||
958 | if ( (0 == seen[count]) && | ||
959 | (allOptions[count].option_mandatory) ) | ||
960 | { | ||
961 | FPRINTF (stderr, | ||
962 | _("Missing mandatory option `%s'.\n"), | ||
963 | allOptions[count].name); | ||
964 | cont = GNUNET_SYSERR; | ||
965 | } | ||
966 | } | ||
967 | GNUNET_free (seen); | ||
968 | |||
951 | /* call cleaners, if available */ | 969 | /* call cleaners, if available */ |
952 | for (count = 0; NULL != allOptions[count].name; count++) | 970 | for (count = 0; NULL != allOptions[count].name; count++) |
953 | if (NULL != allOptions[count].cleaner) | 971 | if (NULL != allOptions[count].cleaner) |
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index a94847a47..76342a6c9 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c | |||
@@ -357,7 +357,7 @@ set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
357 | { | 357 | { |
358 | char **val = scls; | 358 | char **val = scls; |
359 | 359 | ||
360 | GNUNET_assert (value != NULL); | 360 | GNUNET_assert (NULL != value); |
361 | GNUNET_free_non_null (*val); | 361 | GNUNET_free_non_null (*val); |
362 | *val = GNUNET_strdup (value); | 362 | *val = GNUNET_strdup (value); |
363 | return GNUNET_OK; | 363 | return GNUNET_OK; |
@@ -878,4 +878,18 @@ GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE (char shortName, | |||
878 | } | 878 | } |
879 | 879 | ||
880 | 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 | |||
881 | /* end of getopt_helpers.c */ | 895 | /* end of getopt_helpers.c */ |
diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c index faa6a07a1..13cedd7f5 100644 --- a/src/util/test_getopt.c +++ b/src/util/test_getopt.c | |||
@@ -136,13 +136,16 @@ testLogOpts () | |||
136 | GNUNET_GETOPT_OPTION_END | 136 | GNUNET_GETOPT_OPTION_END |
137 | }; | 137 | }; |
138 | 138 | ||
139 | if (5 != GNUNET_GETOPT_run ("test_getopt", logoptionlist, 5, myargv)) | 139 | if (5 != GNUNET_GETOPT_run ("test_getopt", |
140 | logoptionlist, | ||
141 | 5, myargv)) | ||
140 | { | 142 | { |
141 | GNUNET_break (0); | 143 | GNUNET_break (0); |
142 | return 1; | 144 | return 1; |
143 | } | 145 | } |
144 | GNUNET_assert (fn != NULL); | 146 | GNUNET_assert (NULL != fn); |
145 | if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename"))) | 147 | if ( (0 != strcmp (level, "WARNING")) || |
148 | (NULL == strstr (fn, "/filename")) ) | ||
146 | { | 149 | { |
147 | GNUNET_break (0); | 150 | GNUNET_break (0); |
148 | GNUNET_free (level); | 151 | GNUNET_free (level); |
@@ -212,7 +215,9 @@ main (int argc, char *argv[]) | |||
212 | { | 215 | { |
213 | int errCnt = 0; | 216 | int errCnt = 0; |
214 | 217 | ||
215 | GNUNET_log_setup ("test_getopt", "WARNING", NULL); | 218 | GNUNET_log_setup ("test_getopt", |
219 | "WARNING", | ||
220 | NULL); | ||
216 | /* suppress output from -h, -v options */ | 221 | /* suppress output from -h, -v options */ |
217 | #ifndef MINGW | 222 | #ifndef MINGW |
218 | GNUNET_break (0 == CLOSE (1)); | 223 | GNUNET_break (0 == CLOSE (1)); |
diff --git a/src/util/test_program.c b/src/util/test_program.c index 669cee7bd..d206952af 100644 --- a/src/util/test_program.c +++ b/src/util/test_program.c | |||
@@ -24,37 +24,19 @@ | |||
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_util_lib.h" | 25 | #include "gnunet_util_lib.h" |
26 | 26 | ||
27 | static int setme1, setme2; | 27 | |
28 | 28 | static int setme1; | |
29 | static struct GNUNET_GETOPT_CommandLineOption options1[] = { | 29 | |
30 | {'n', "name", NULL, "description", 0, &GNUNET_GETOPT_set_one, &setme1}, | 30 | static int setme2; |
31 | GNUNET_GETOPT_OPTION_END | 31 | |
32 | }; | ||
33 | |||
34 | static struct GNUNET_GETOPT_CommandLineOption options2[] = { | ||
35 | {'n', "name", NULL, "description", 0, &GNUNET_GETOPT_set_one, &setme1}, | ||
36 | {'N', "number", NULL, "description", 0, &GNUNET_GETOPT_set_one, &setme2}, | ||
37 | GNUNET_GETOPT_OPTION_END | ||
38 | }; | ||
39 | |||
40 | static struct GNUNET_GETOPT_CommandLineOption options3[] = { | ||
41 | {'N', "number", NULL, "description", 0, &GNUNET_GETOPT_set_one, &setme1}, | ||
42 | {'n', "name", NULL, "description", 0, &GNUNET_GETOPT_set_one, &setme2}, | ||
43 | GNUNET_GETOPT_OPTION_END | ||
44 | }; | ||
45 | |||
46 | static struct GNUNET_GETOPT_CommandLineOption options4[] = { | ||
47 | {'n', "name", NULL, "description", 0, &GNUNET_GETOPT_set_one, &setme1}, | ||
48 | {'n', "number", NULL, "description", 0, &GNUNET_GETOPT_set_one, &setme2}, | ||
49 | GNUNET_GETOPT_OPTION_END | ||
50 | }; | ||
51 | 32 | ||
52 | /** | 33 | /** |
53 | * Main function that will be run. | 34 | * Main function that will be run. |
54 | */ | 35 | */ |
55 | |||
56 | static void | 36 | static void |
57 | runner (void *cls, char *const *args, const char *cfgfile, | 37 | runner (void *cls, |
38 | char *const *args, | ||
39 | const char *cfgfile, | ||
58 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 40 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
59 | { | 41 | { |
60 | int *ok = cls; | 42 | int *ok = cls; |
@@ -62,21 +44,16 @@ runner (void *cls, char *const *args, const char *cfgfile, | |||
62 | GNUNET_assert (setme1 == 1); | 44 | GNUNET_assert (setme1 == 1); |
63 | GNUNET_assert (0 == strcmp (args[0], "extra")); | 45 | GNUNET_assert (0 == strcmp (args[0], "extra")); |
64 | GNUNET_assert (args[1] == NULL); | 46 | GNUNET_assert (args[1] == NULL); |
65 | GNUNET_assert (0 == strcmp (cfgfile, "test_program_data.conf")); | 47 | GNUNET_assert (NULL != strstr (cfgfile, "/test_program_data.conf")); |
66 | |||
67 | *ok = 0; | 48 | *ok = 0; |
68 | } | 49 | } |
69 | 50 | ||
70 | /** | 51 | |
71 | * Main method, starts scheduler with task1, | 52 | int |
72 | * checks that "ok" is correct at the end. | 53 | main (int argc, char *argv[]) |
73 | */ | ||
74 | static int | ||
75 | check () | ||
76 | { | 54 | { |
77 | int ok = 1; | 55 | int ok = 1; |
78 | 56 | char *const argvx[] = { | |
79 | char *const argv[] = { | ||
80 | "test_program", | 57 | "test_program", |
81 | "-c", | 58 | "-c", |
82 | "test_program_data.conf", | 59 | "test_program_data.conf", |
@@ -86,33 +63,75 @@ check () | |||
86 | "extra", | 63 | "extra", |
87 | NULL | 64 | NULL |
88 | }; | 65 | }; |
66 | struct GNUNET_GETOPT_CommandLineOption options1[] = { | ||
67 | GNUNET_GETOPT_OPTION_SET_ONE ('n', | ||
68 | "name", | ||
69 | "description", | ||
70 | &setme1), | ||
71 | GNUNET_GETOPT_OPTION_END | ||
72 | }; | ||
73 | struct GNUNET_GETOPT_CommandLineOption options2[] = { | ||
74 | GNUNET_GETOPT_OPTION_SET_ONE ('n', | ||
75 | "name", | ||
76 | "description", | ||
77 | &setme1), | ||
78 | GNUNET_GETOPT_OPTION_SET_ONE ('N', | ||
79 | "number", | ||
80 | "description", | ||
81 | &setme2), | ||
82 | GNUNET_GETOPT_OPTION_END | ||
83 | }; | ||
84 | struct GNUNET_GETOPT_CommandLineOption options3[] = { | ||
85 | GNUNET_GETOPT_OPTION_SET_ONE ('N', | ||
86 | "number", | ||
87 | "description", | ||
88 | &setme1), | ||
89 | GNUNET_GETOPT_OPTION_SET_ONE ('n', | ||
90 | "name", | ||
91 | "description", | ||
92 | &setme2), | ||
93 | GNUNET_GETOPT_OPTION_END | ||
94 | }; | ||
95 | struct GNUNET_GETOPT_CommandLineOption options4[] = { | ||
96 | GNUNET_GETOPT_OPTION_SET_ONE ('n', | ||
97 | "name", | ||
98 | "description", | ||
99 | &setme1), | ||
100 | GNUNET_GETOPT_OPTION_SET_ONE ('n', | ||
101 | "name", | ||
102 | "description", | ||
103 | &setme2), | ||
104 | GNUNET_GETOPT_OPTION_END | ||
105 | }; | ||
89 | 106 | ||
107 | |||
108 | GNUNET_log_setup ("test_program", | ||
109 | "WARNING", | ||
110 | NULL); | ||
90 | GNUNET_assert (GNUNET_OK == | 111 | GNUNET_assert (GNUNET_OK == |
91 | GNUNET_PROGRAM_run (7, argv, "test_program", "A test", | 112 | GNUNET_PROGRAM_run (7, argvx, |
92 | options1, &runner, &ok)); | 113 | "test_program", |
114 | "A test", | ||
115 | options1, | ||
116 | &runner, &ok)); | ||
93 | 117 | ||
94 | GNUNET_assert (GNUNET_OK == | 118 | GNUNET_assert (GNUNET_OK == |
95 | GNUNET_PROGRAM_run (7, argv, "test_program", "A test", | 119 | GNUNET_PROGRAM_run (7, argvx, |
96 | options2, &runner, &ok)); | 120 | "test_program", "A test", |
121 | options2, | ||
122 | &runner, &ok)); | ||
97 | GNUNET_assert (GNUNET_OK == | 123 | GNUNET_assert (GNUNET_OK == |
98 | GNUNET_PROGRAM_run (7, argv, "test_program", "A test", | 124 | GNUNET_PROGRAM_run (7, argvx, |
99 | options3, &runner, &ok)); | 125 | "test_program", "A test", |
126 | options3, | ||
127 | &runner, &ok)); | ||
100 | GNUNET_assert (GNUNET_OK == | 128 | GNUNET_assert (GNUNET_OK == |
101 | GNUNET_PROGRAM_run (7, argv, "test_program", "A test", | 129 | GNUNET_PROGRAM_run (7, argvx, |
102 | options4, &runner, &ok)); | 130 | "test_program", "A test", |
131 | options4, | ||
132 | &runner, &ok)); | ||
103 | 133 | ||
104 | return ok; | 134 | return ok; |
105 | } | 135 | } |
106 | 136 | ||
107 | int | ||
108 | main (int argc, char *argv[]) | ||
109 | { | ||
110 | int ret = 0; | ||
111 | |||
112 | GNUNET_log_setup ("test_program", "WARNING", NULL); | ||
113 | ret += check (); | ||
114 | |||
115 | return ret; | ||
116 | } | ||
117 | |||
118 | /* end of test_program.c */ | 137 | /* end of test_program.c */ |