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.c1016
1 files changed, 0 insertions, 1016 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
deleted file mode 100644
index 96aee40e3..000000000
--- a/src/util/getopt_helpers.c
+++ /dev/null
@@ -1,1016 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2006, 2011, 2020 GNUnet e.V.
4
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
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file src/util/getopt_helpers.c
23 * @brief implements command line that sets option
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
29#define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__)
30
31
32/**
33 * Print out program version (implements --version).
34 *
35 * @param ctx command line processing context
36 * @param scls additional closure (points to version string)
37 * @param option name of the option
38 * @param value not used (NULL)
39 * @return #GNUNET_NO (do not continue, not an error)
40 */
41static enum GNUNET_GenericReturnValue
42print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
43 void *scls,
44 const char *option,
45 const char *value)
46{
47 const char *version = scls;
48
49 (void) option;
50 (void) value;
51 printf ("%s v%s\n", ctx->binaryName, version);
52 return GNUNET_NO;
53}
54
55
56struct GNUNET_GETOPT_CommandLineOption
57GNUNET_GETOPT_option_version (const char *version)
58{
59 struct GNUNET_GETOPT_CommandLineOption clo = {
60 .shortName = 'v',
61 .name = "version",
62 .description = gettext_noop (
63 "print the version number"),
64 .option_exclusive = 1,
65 .processor = &print_version,
66 .scls = (void *) version
67 };
68
69 return clo;
70}
71
72
73/**
74 * At what offset does the help text start?
75 */
76#define BORDER 29
77
78/**
79 * Print out details on command line options (implements --help).
80 *
81 * @param ctx command line processing context
82 * @param scls additional closure (points to about text)
83 * @param option name of the option
84 * @param value not used (NULL)
85 * @return #GNUNET_NO (do not continue, not an error)
86 */
87static enum GNUNET_GenericReturnValue
88format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
89 void *scls,
90 const char *option,
91 const char *value)
92{
93 const char *about = scls;
94 size_t slen;
95 unsigned int i;
96 int j;
97 size_t ml;
98 size_t p;
99 char *scp;
100 const char *trans;
101 const struct GNUNET_GETOPT_CommandLineOption *opt;
102 const struct GNUNET_OS_ProjectData *pd;
103
104 (void) option;
105 (void) value;
106 if (NULL != about)
107 {
108 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
109 printf (_ (
110 "Arguments mandatory for long options are also mandatory for short options.\n"));
111 }
112 i = 0;
113 opt = ctx->allOptions;
114 while (NULL != opt[i].description)
115 {
116 if (opt[i].shortName == '\0')
117 printf (" ");
118 else
119 printf (" -%c, ", opt[i].shortName);
120 printf ("--%s", opt[i].name);
121 slen = 8 + strlen (opt[i].name);
122 if (NULL != opt[i].argumentHelp)
123 {
124 printf ("=%s", opt[i].argumentHelp);
125 slen += 1 + strlen (opt[i].argumentHelp);
126 }
127 if (slen > BORDER)
128 {
129 printf ("\n%*s", BORDER, "");
130 slen = BORDER;
131 }
132 if (slen < BORDER)
133 {
134 printf ("%*s", (int) (BORDER - slen), "");
135 slen = BORDER;
136 }
137 if (0 < strlen (opt[i].description))
138 trans = gettext (opt[i].description);
139 else
140 trans = "";
141 ml = strlen (trans);
142 p = 0;
143OUTER:
144 while (ml - p > 78 - slen)
145 {
146 for (j = p + 78 - slen; j > (int) p; j--)
147 {
148 if (isspace ((unsigned char) trans[j]))
149 {
150 scp = GNUNET_malloc (j - p + 1);
151 GNUNET_memcpy (scp, &trans[p], j - p);
152 scp[j - p] = '\0';
153 printf ("%s\n%*s", scp, BORDER + 2, "");
154 GNUNET_free (scp);
155 p = j + 1;
156 slen = BORDER + 2;
157 goto OUTER;
158 }
159 }
160 /* could not find space to break line */
161 scp = GNUNET_malloc (78 - slen + 1);
162 GNUNET_memcpy (scp, &trans[p], 78 - slen);
163 scp[78 - slen] = '\0';
164 printf ("%s\n%*s", scp, BORDER + 2, "");
165 GNUNET_free (scp);
166 slen = BORDER + 2;
167 p = p + 78 - slen;
168 }
169 /* print rest */
170 if (p < ml)
171 printf ("%s\n", &trans[p]);
172 if (strlen (trans) == 0)
173 printf ("\n");
174 i++;
175 }
176 pd = GNUNET_OS_project_data_get ();
177 printf ("\n"
178 "Report bugs to %s.\n"
179 "Home page: %s\n",
180 pd->bug_email,
181 pd->homepage);
182
183 if (0 != pd->is_gnu)
184 printf ("General help using GNU software: http://www.gnu.org/gethelp/\n");
185
186 return GNUNET_NO;
187}
188
189
190struct GNUNET_GETOPT_CommandLineOption
191GNUNET_GETOPT_option_help (const char *about)
192{
193 struct GNUNET_GETOPT_CommandLineOption clo = {
194 .shortName = 'h',
195 .name = "help",
196 .description = gettext_noop (
197 "print this help"),
198 .option_exclusive = 1,
199 .processor = format_help,
200 .scls = (void *) about
201 };
202
203 return clo;
204}
205
206
207/**
208 * Set an option of type 'unsigned int' from the command line. Each
209 * time the option flag is given, the value is incremented by one.
210 * A pointer to this function should be passed as part of the
211 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
212 * of this type. It should be followed by a pointer to a value of
213 * type 'int'.
214 *
215 * @param ctx command line processing context
216 * @param scls additional closure (will point to the 'unsigned int')
217 * @param option name of the option
218 * @param value not used (NULL)
219 * @return #GNUNET_OK
220 */
221static enum GNUNET_GenericReturnValue
222increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
223 void *scls,
224 const char *option,
225 const char *value)
226{
227 unsigned int *val = scls;
228
229 (void) ctx;
230 (void) option;
231 (void) value;
232 (*val)++;
233 return GNUNET_OK;
234}
235
236
237struct GNUNET_GETOPT_CommandLineOption
238GNUNET_GETOPT_option_increment_uint (char shortName,
239 const char *name,
240 const char *description,
241 unsigned int *val)
242{
243 struct GNUNET_GETOPT_CommandLineOption clo = {
244 .shortName = shortName,
245 .name = name,
246 .description = description,
247 .processor = &increment_value,
248 .scls = (void *) val
249 };
250
251 return clo;
252}
253
254
255struct GNUNET_GETOPT_CommandLineOption
256GNUNET_GETOPT_option_verbose (unsigned int *level)
257{
258 struct GNUNET_GETOPT_CommandLineOption clo = {
259 .shortName = 'V',
260 .name = "verbose",
261 .description =
262 gettext_noop ("be verbose"),
263 .processor = &increment_value,
264 .scls = (void *) level
265 };
266
267 return clo;
268}
269
270
271/**
272 * Set an option of type 'int' from the command line to 1 if the
273 * given option is present.
274 * A pointer to this function should be passed as part of the
275 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
276 * of this type. It should be followed by a pointer to a value of
277 * type 'int'.
278 *
279 * @param ctx command line processing context
280 * @param scls additional closure (will point to the 'int')
281 * @param option name of the option
282 * @param value not used (NULL)
283 * @return #GNUNET_OK
284 */
285static enum GNUNET_GenericReturnValue
286set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
287 void *scls,
288 const char *option,
289 const char *value)
290{
291 int *val = scls;
292
293 (void) ctx;
294 (void) option;
295 (void) value;
296 *val = 1;
297 return GNUNET_OK;
298}
299
300
301struct GNUNET_GETOPT_CommandLineOption
302GNUNET_GETOPT_option_flag (char shortName,
303 const char *name,
304 const char *description,
305 int *val)
306{
307 struct GNUNET_GETOPT_CommandLineOption clo = {
308 .shortName = shortName,
309 .name = name,
310 .description = description,
311 .processor = &set_one,
312 .scls = (void *) val
313 };
314
315 return clo;
316}
317
318
319/**
320 * Set an option of type 'char *' from the command line.
321 * A pointer to this function should be passed as part of the
322 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
323 * of this type. It should be followed by a pointer to a value of
324 * type 'char *', which will be allocated with the requested string.
325 *
326 * @param ctx command line processing context
327 * @param scls additional closure (will point to the 'char *',
328 * which will be allocated)
329 * @param option name of the option
330 * @param value actual value of the option (a string)
331 * @return #GNUNET_OK
332 */
333static enum GNUNET_GenericReturnValue
334set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
335 void *scls,
336 const char *option,
337 const char *value)
338{
339 char **val = scls;
340
341 (void) ctx;
342 (void) option;
343 GNUNET_assert (NULL != value);
344 GNUNET_free (*val);
345 *val = GNUNET_strdup (value);
346 return GNUNET_OK;
347}
348
349
350struct GNUNET_GETOPT_CommandLineOption
351GNUNET_GETOPT_option_string (char shortName,
352 const char *name,
353 const char *argumentHelp,
354 const char *description,
355 char **str)
356{
357 struct GNUNET_GETOPT_CommandLineOption clo = {
358 .shortName = shortName,
359 .name = name,
360 .argumentHelp = argumentHelp,
361 .description = description,
362 .require_argument = 1,
363 .processor = &set_string,
364 .scls = (void *) str
365 };
366
367 return clo;
368}
369
370
371struct GNUNET_GETOPT_CommandLineOption
372GNUNET_GETOPT_option_loglevel (char **level)
373{
374 struct GNUNET_GETOPT_CommandLineOption clo = {
375 .shortName = 'L',
376 .name = "log",
377 .argumentHelp = "LOGLEVEL",
378 .description = gettext_noop ("configure logging to use LOGLEVEL"),
379 .require_argument = 1,
380 .processor = &set_string,
381 .scls = (void *) level
382 };
383
384 return clo;
385}
386
387
388/**
389 * Set an option of type 'char *' from the command line with
390 * filename expansion a la #GNUNET_STRINGS_filename_expand().
391 *
392 * @param ctx command line processing context
393 * @param scls additional closure (will point to the `char *`,
394 * which will be allocated)
395 * @param option name of the option
396 * @param value actual value of the option (a string)
397 * @return #GNUNET_OK
398 */
399static enum GNUNET_GenericReturnValue
400set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
401 void *scls,
402 const char *option,
403 const char *value)
404{
405 char **val = scls;
406
407 (void) ctx;
408 (void) option;
409 GNUNET_assert (NULL != value);
410 GNUNET_free (*val);
411 *val = GNUNET_STRINGS_filename_expand (value);
412 return GNUNET_OK;
413}
414
415
416struct GNUNET_GETOPT_CommandLineOption
417GNUNET_GETOPT_option_filename (char shortName,
418 const char *name,
419 const char *argumentHelp,
420 const char *description,
421 char **str)
422{
423 struct GNUNET_GETOPT_CommandLineOption clo = {
424 .shortName = shortName,
425 .name = name,
426 .argumentHelp = argumentHelp,
427 .description = description,
428 .require_argument = 1,
429 .processor = &set_filename,
430 .scls = (void *) str
431 };
432
433 return clo;
434}
435
436
437struct GNUNET_GETOPT_CommandLineOption
438GNUNET_GETOPT_option_logfile (char **logfn)
439{
440 struct GNUNET_GETOPT_CommandLineOption clo = {
441 .shortName = 'l',
442 .name = "logfile",
443 .argumentHelp = "FILENAME",
444 .description =
445 gettext_noop ("configure logging to write logs to FILENAME"),
446 .require_argument = 1,
447 .processor = &set_filename,
448 .scls = (void *) logfn
449 };
450
451 return clo;
452}
453
454
455struct GNUNET_GETOPT_CommandLineOption
456GNUNET_GETOPT_option_cfgfile (char **fn)
457{
458 struct GNUNET_GETOPT_CommandLineOption clo = {
459 .shortName = 'c',
460 .name = "config",
461 .argumentHelp = "FILENAME",
462 .description = gettext_noop ("use configuration file FILENAME"),
463 .require_argument = 1,
464 .processor = &set_filename,
465 .scls = (void *) fn
466 };
467
468 return clo;
469}
470
471
472/**
473 * Set an option of type 'unsigned long long' from the command line.
474 * A pointer to this function should be passed as part of the
475 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
476 * of this type. It should be followed by a pointer to a value of
477 * type 'unsigned long long'.
478 *
479 * @param ctx command line processing context
480 * @param scls additional closure (will point to the 'unsigned long long')
481 * @param option name of the option
482 * @param value actual value of the option as a string.
483 * @return #GNUNET_OK if parsing the value worked
484 */
485static enum GNUNET_GenericReturnValue
486set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
487 void *scls,
488 const char *option,
489 const char *value)
490{
491 unsigned long long *val = scls;
492 char dummy[2];
493
494 (void) ctx;
495 if (1 != sscanf (value, "%llu%1s", val, dummy))
496 {
497 fprintf (stderr,
498 _ ("You must pass a number to the `%s' option.\n"),
499 option);
500 return GNUNET_SYSERR;
501 }
502 return GNUNET_OK;
503}
504
505
506struct GNUNET_GETOPT_CommandLineOption
507GNUNET_GETOPT_option_ulong (char shortName,
508 const char *name,
509 const char *argumentHelp,
510 const char *description,
511 unsigned long long *val)
512{
513 struct GNUNET_GETOPT_CommandLineOption clo = {
514 .shortName = shortName,
515 .name = name,
516 .argumentHelp = argumentHelp,
517 .description = description,
518 .require_argument = 1,
519 .processor = &set_ulong,
520 .scls = (void *) val
521 };
522
523 return clo;
524}
525
526
527/**
528 * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
529 * A pointer to this function should be passed as part of the
530 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
531 * of this type. It should be followed by a pointer to a value of
532 * type 'struct GNUNET_TIME_Relative'.
533 *
534 * @param ctx command line processing context
535 * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
536 * @param option name of the option
537 * @param value actual value of the option as a string.
538 * @return #GNUNET_OK if parsing the value worked
539 */
540static enum GNUNET_GenericReturnValue
541set_timetravel_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
542 void *scls,
543 const char *option,
544 const char *value)
545{
546 long long delta;
547 long long minus;
548 struct GNUNET_TIME_Relative rt;
549
550 (void) scls;
551 (void) ctx;
552 while (isspace (value[0]))
553 value++;
554 minus = 1;
555 if (value[0] == '-')
556 {
557 minus = -1;
558 value++;
559 }
560 else if (value[0] == '+')
561 {
562 value++;
563 }
564 if (GNUNET_OK !=
565 GNUNET_STRINGS_fancy_time_to_relative (value,
566 &rt))
567 {
568 fprintf (stderr,
569 _ (
570 "You must pass a relative time (optionally with sign) to the `%s' option.\n"),
571 option);
572 return GNUNET_SYSERR;
573 }
574 if (rt.rel_value_us > LLONG_MAX)
575 {
576 fprintf (stderr,
577 _ ("Value given for time travel `%s' option is too big.\n"),
578 option);
579 return GNUNET_SYSERR;
580 }
581 delta = (long long) rt.rel_value_us;
582 delta *= minus;
583 GNUNET_TIME_set_offset (delta);
584 return GNUNET_OK;
585}
586
587
588struct GNUNET_GETOPT_CommandLineOption
589GNUNET_GETOPT_option_timetravel (char shortName,
590 const char *name)
591{
592 struct GNUNET_GETOPT_CommandLineOption clo = {
593 .shortName = shortName,
594 .name = name,
595 .argumentHelp = _ ("[+/-]MICROSECONDS"),
596 .description = _ (
597 "modify system time by given offset (for debugging/testing only)"),
598 .require_argument = 1,
599 .processor = &set_timetravel_time
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 */
619static enum GNUNET_GenericReturnValue
620set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
621 void *scls,
622 const char *option,
623 const char *value)
624{
625 struct GNUNET_TIME_Relative *val = scls;
626
627 (void) ctx;
628 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val))
629 {
630 fprintf (stderr,
631 _ ("You must pass relative time to the `%s' option.\n"),
632 option);
633 return GNUNET_SYSERR;
634 }
635 return GNUNET_OK;
636}
637
638
639struct GNUNET_GETOPT_CommandLineOption
640GNUNET_GETOPT_option_relative_time (char shortName,
641 const char *name,
642 const char *argumentHelp,
643 const char *description,
644 struct GNUNET_TIME_Relative *val)
645{
646 struct GNUNET_GETOPT_CommandLineOption clo = {
647 .shortName = shortName,
648 .name = name,
649 .argumentHelp = argumentHelp,
650 .description = description,
651 .require_argument = 1,
652 .processor = &set_relative_time,
653 .scls = (void *) val
654 };
655
656 return clo;
657}
658
659
660/**
661 * Set an option of type 'struct GNUNET_TIME_Absolute' from the command line.
662 * A pointer to this function should be passed as part of the
663 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
664 * of this type. It should be followed by a pointer to a value of
665 * type 'struct GNUNET_TIME_Absolute'.
666 *
667 * @param ctx command line processing context
668 * @param scls additional closure (will point to the `struct GNUNET_TIME_Absolute`)
669 * @param option name of the option
670 * @param value actual value of the option as a string.
671 * @return #GNUNET_OK if parsing the value worked
672 */
673static enum GNUNET_GenericReturnValue
674set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
675 void *scls,
676 const char *option,
677 const char *value)
678{
679 struct GNUNET_TIME_Absolute *val = scls;
680
681 (void) ctx;
682 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val))
683 {
684 fprintf (stderr,
685 _ ("You must pass absolute time to the `%s' option.\n"),
686 option);
687 return GNUNET_SYSERR;
688 }
689 return GNUNET_OK;
690}
691
692
693struct GNUNET_GETOPT_CommandLineOption
694GNUNET_GETOPT_option_absolute_time (char shortName,
695 const char *name,
696 const char *argumentHelp,
697 const char *description,
698 struct GNUNET_TIME_Absolute *val)
699{
700 struct GNUNET_GETOPT_CommandLineOption clo = {
701 .shortName = shortName,
702 .name = name,
703 .argumentHelp = argumentHelp,
704 .description = description,
705 .require_argument = 1,
706 .processor = &set_absolute_time,
707 .scls = (void *) val
708 };
709
710 return clo;
711}
712
713
714/**
715 * Set an option of type 'struct GNUNET_TIME_Timestamp' from the command line.
716 * A pointer to this function should be passed as part of the
717 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
718 * of this type. It should be followed by a pointer to a value of
719 * type 'struct GNUNET_TIME_Absolute'.
720 *
721 * @param ctx command line processing context
722 * @param scls additional closure (will point to the `struct GNUNET_TIME_Absolute`)
723 * @param option name of the option
724 * @param value actual value of the option as a string.
725 * @return #GNUNET_OK if parsing the value worked
726 */
727static enum GNUNET_GenericReturnValue
728set_timestamp (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
729 void *scls,
730 const char *option,
731 const char *value)
732{
733 struct GNUNET_TIME_Timestamp *t = scls;
734 struct GNUNET_TIME_Absolute abs;
735
736 (void) ctx;
737 if (GNUNET_OK !=
738 GNUNET_STRINGS_fancy_time_to_absolute (value,
739 &abs))
740 {
741 fprintf (stderr,
742 _ ("You must pass a timestamp to the `%s' option.\n"),
743 option);
744 return GNUNET_SYSERR;
745 }
746 if (0 != abs.abs_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us)
747 {
748 fprintf (stderr,
749 _ ("The maximum precision allowed for timestamps is seconds.\n"));
750 return GNUNET_SYSERR;
751 }
752 t->abs_time = abs;
753 return GNUNET_OK;
754}
755
756
757struct GNUNET_GETOPT_CommandLineOption
758GNUNET_GETOPT_option_timestamp (char shortName,
759 const char *name,
760 const char *argumentHelp,
761 const char *description,
762 struct GNUNET_TIME_Timestamp *val)
763{
764 struct GNUNET_GETOPT_CommandLineOption clo = {
765 .shortName = shortName,
766 .name = name,
767 .argumentHelp = argumentHelp,
768 .description = description,
769 .require_argument = 1,
770 .processor = &set_timestamp,
771 .scls = (void *) val
772 };
773
774 return clo;
775}
776
777
778/**
779 * Set an option of type 'unsigned int' from the command line.
780 * A pointer to this function should be passed as part of the
781 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
782 * of this type. It should be followed by a pointer to a value of
783 * type 'unsigned int'.
784 *
785 * @param ctx command line processing context
786 * @param scls additional closure (will point to the 'unsigned int')
787 * @param option name of the option
788 * @param value actual value of the option as a string.
789 * @return #GNUNET_OK if parsing the value worked
790 */
791static enum GNUNET_GenericReturnValue
792set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
793 void *scls,
794 const char *option,
795 const char *value)
796{
797 unsigned int *val = scls;
798 char dummy[2];
799
800 (void) ctx;
801 if ('-' == *value)
802 {
803 fprintf (stderr,
804 _ (
805 "Your input for the '%s' option has to be a non negative number\n"),
806 option);
807 return GNUNET_SYSERR;
808 }
809 if (1 != sscanf (value, "%u%1s", val, dummy))
810 {
811 fprintf (stderr,
812 _ ("You must pass a number to the `%s' option.\n"),
813 option);
814 return GNUNET_SYSERR;
815 }
816 return GNUNET_OK;
817}
818
819
820struct GNUNET_GETOPT_CommandLineOption
821GNUNET_GETOPT_option_uint (char shortName,
822 const char *name,
823 const char *argumentHelp,
824 const char *description,
825 unsigned int *val)
826{
827 struct GNUNET_GETOPT_CommandLineOption clo = {
828 .shortName = shortName,
829 .name = name,
830 .argumentHelp = argumentHelp,
831 .description = description,
832 .require_argument = 1,
833 .processor = &set_uint,
834 .scls = (void *) val
835 };
836
837 return clo;
838}
839
840
841/**
842 * Set an option of type 'uint16_t' from the command line.
843 * A pointer to this function should be passed as part of the
844 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
845 * of this type. It should be followed by a pointer to a value of
846 * type 'uint16_t'.
847 *
848 * @param ctx command line processing context
849 * @param scls additional closure (will point to the 'unsigned int')
850 * @param option name of the option
851 * @param value actual value of the option as a string.
852 * @return #GNUNET_OK if parsing the value worked
853 */
854static enum GNUNET_GenericReturnValue
855set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
856 void *scls,
857 const char *option,
858 const char *value)
859{
860 uint16_t *val = scls;
861 unsigned int v;
862 char dummy[2];
863
864 (void) ctx;
865 if (1 != sscanf (value, "%u%1s", &v, dummy))
866 {
867 fprintf (stderr,
868 _ ("You must pass a number to the `%s' option.\n"),
869 option);
870 return GNUNET_SYSERR;
871 }
872 if (v > UINT16_MAX)
873 {
874 fprintf (stderr,
875 _ ("You must pass a number below %u to the `%s' option.\n"),
876 (unsigned int) UINT16_MAX,
877 option);
878 return GNUNET_SYSERR;
879 }
880 *val = (uint16_t) v;
881 return GNUNET_OK;
882}
883
884
885struct GNUNET_GETOPT_CommandLineOption
886GNUNET_GETOPT_option_uint16 (char shortName,
887 const char *name,
888 const char *argumentHelp,
889 const char *description,
890 uint16_t *val)
891{
892 struct GNUNET_GETOPT_CommandLineOption clo = {
893 .shortName = shortName,
894 .name = name,
895 .argumentHelp = argumentHelp,
896 .description = description,
897 .require_argument = 1,
898 .processor = &set_uint16,
899 .scls = (void *) val
900 };
901
902 return clo;
903}
904
905
906/**
907 * Closure for #set_base32().
908 */
909struct Base32Context
910{
911 /**
912 * Value to initialize (already allocated)
913 */
914 void *val;
915
916 /**
917 * Number of bytes expected for @e val.
918 */
919 size_t val_size;
920};
921
922
923/**
924 * Set an option of type 'unsigned int' from the command line.
925 * A pointer to this function should be passed as part of the
926 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
927 * of this type. It should be followed by a pointer to a value of
928 * type 'unsigned int'.
929 *
930 * @param ctx command line processing context
931 * @param scls additional closure (will point to the 'unsigned int')
932 * @param option name of the option
933 * @param value actual value of the option as a string.
934 * @return #GNUNET_OK if parsing the value worked
935 */
936static enum GNUNET_GenericReturnValue
937set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
938 void *scls,
939 const char *option,
940 const char *value)
941{
942 struct Base32Context *bc = scls;
943
944 (void) ctx;
945 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (value,
946 strlen (value),
947 bc->val,
948 bc->val_size))
949 {
950 fprintf (
951 stderr,
952 _ (
953 "Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"),
954 option);
955 return GNUNET_SYSERR;
956 }
957 return GNUNET_OK;
958}
959
960
961/**
962 * Helper function to clean up after
963 * #GNUNET_GETOPT_option_base32_fixed_size.
964 *
965 * @param cls value to GNUNET_free()
966 */
967static void
968free_bc (void *cls)
969{
970 GNUNET_free (cls);
971}
972
973
974struct GNUNET_GETOPT_CommandLineOption
975GNUNET_GETOPT_option_base32_fixed_size (char shortName,
976 const char *name,
977 const char *argumentHelp,
978 const char *description,
979 void *val,
980 size_t val_size)
981{
982 struct Base32Context *bc = GNUNET_new (struct Base32Context);
983 struct GNUNET_GETOPT_CommandLineOption clo = {
984 .shortName = shortName,
985 .name = name,
986 .argumentHelp = argumentHelp,
987 .description = description,
988 .require_argument = 1,
989 .processor = &set_base32,
990 .cleaner = &free_bc,
991 .scls = (void *) bc
992 };
993
994 bc->val = val;
995 bc->val_size = val_size;
996 return clo;
997}
998
999
1000struct GNUNET_GETOPT_CommandLineOption
1001GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
1002{
1003 opt.option_mandatory = 1;
1004 return opt;
1005}
1006
1007
1008struct GNUNET_GETOPT_CommandLineOption
1009GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt)
1010{
1011 opt.option_exclusive = 1;
1012 return opt;
1013}
1014
1015
1016/* end of getopt_helpers.c */