diff options
Diffstat (limited to 'src/util/getopt_helpers.c')
-rw-r--r-- | src/util/getopt_helpers.c | 440 |
1 files changed, 208 insertions, 232 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index 77032e501..05856beee 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c | |||
@@ -26,7 +26,7 @@ | |||
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | 28 | ||
29 | #define LOG(kind,...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) | 29 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__) |
30 | 30 | ||
31 | 31 | ||
32 | /** | 32 | /** |
@@ -48,9 +48,7 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
48 | 48 | ||
49 | (void) option; | 49 | (void) option; |
50 | (void) value; | 50 | (void) value; |
51 | printf ("%s v%s\n", | 51 | printf ("%s v%s\n", ctx->binaryName, version); |
52 | ctx->binaryName, | ||
53 | version); | ||
54 | return GNUNET_NO; | 52 | return GNUNET_NO; |
55 | } | 53 | } |
56 | 54 | ||
@@ -64,13 +62,13 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
64 | struct GNUNET_GETOPT_CommandLineOption | 62 | struct GNUNET_GETOPT_CommandLineOption |
65 | GNUNET_GETOPT_option_version (const char *version) | 63 | GNUNET_GETOPT_option_version (const char *version) |
66 | { | 64 | { |
67 | struct GNUNET_GETOPT_CommandLineOption clo = { | 65 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'v', |
68 | .shortName = 'v', | 66 | .name = "version", |
69 | .name = "version", | 67 | .description = gettext_noop ( |
70 | .description = gettext_noop("print the version number"), | 68 | "print the version number"), |
71 | .processor = &print_version, | 69 | .option_exclusive = 1, |
72 | .scls = (void *) version | 70 | .processor = &print_version, |
73 | }; | 71 | .scls = (void *) version}; |
74 | return clo; | 72 | return clo; |
75 | } | 73 | } |
76 | 74 | ||
@@ -110,10 +108,9 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
110 | (void) value; | 108 | (void) value; |
111 | if (NULL != about) | 109 | if (NULL != about) |
112 | { | 110 | { |
113 | printf ("%s\n%s\n", | 111 | printf ("%s\n%s\n", ctx->binaryOptions, gettext (about)); |
114 | ctx->binaryOptions, | 112 | printf (_ ( |
115 | gettext (about)); | 113 | "Arguments mandatory for long options are also mandatory for short options.\n")); |
116 | printf (_("Arguments mandatory for long options are also mandatory for short options.\n")); | ||
117 | } | 114 | } |
118 | i = 0; | 115 | i = 0; |
119 | opt = ctx->allOptions; | 116 | opt = ctx->allOptions; |
@@ -146,7 +143,7 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
146 | trans = ""; | 143 | trans = ""; |
147 | ml = strlen (trans); | 144 | ml = strlen (trans); |
148 | p = 0; | 145 | p = 0; |
149 | OUTER: | 146 | OUTER: |
150 | while (ml - p > 78 - slen) | 147 | while (ml - p > 78 - slen) |
151 | { | 148 | { |
152 | for (j = p + 78 - slen; j > (int) p; j--) | 149 | for (j = p + 78 - slen; j > (int) p; j--) |
@@ -198,13 +195,13 @@ OUTER: | |||
198 | struct GNUNET_GETOPT_CommandLineOption | 195 | struct GNUNET_GETOPT_CommandLineOption |
199 | GNUNET_GETOPT_option_help (const char *about) | 196 | GNUNET_GETOPT_option_help (const char *about) |
200 | { | 197 | { |
201 | struct GNUNET_GETOPT_CommandLineOption clo = { | 198 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'h', |
202 | .shortName = 'h', | 199 | .name = "help", |
203 | .name = "help", | 200 | .description = gettext_noop ( |
204 | .description = gettext_noop("print this help"), | 201 | "print this help"), |
205 | .processor = format_help, | 202 | .option_exclusive = 1, |
206 | .scls = (void *) about | 203 | .processor = format_help, |
207 | }; | 204 | .scls = (void *) about}; |
208 | 205 | ||
209 | return clo; | 206 | return clo; |
210 | } | 207 | } |
@@ -251,17 +248,15 @@ increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
251 | */ | 248 | */ |
252 | struct GNUNET_GETOPT_CommandLineOption | 249 | struct GNUNET_GETOPT_CommandLineOption |
253 | GNUNET_GETOPT_option_increment_uint (char shortName, | 250 | GNUNET_GETOPT_option_increment_uint (char shortName, |
254 | const char *name, | 251 | const char *name, |
255 | const char *description, | 252 | const char *description, |
256 | unsigned int *val) | 253 | unsigned int *val) |
257 | { | 254 | { |
258 | struct GNUNET_GETOPT_CommandLineOption clo = { | 255 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
259 | .shortName = shortName, | 256 | .name = name, |
260 | .name = name, | 257 | .description = description, |
261 | .description = description, | 258 | .processor = &increment_value, |
262 | .processor = &increment_value, | 259 | .scls = (void *) val}; |
263 | .scls = (void *) val | ||
264 | }; | ||
265 | 260 | ||
266 | return clo; | 261 | return clo; |
267 | } | 262 | } |
@@ -276,13 +271,12 @@ GNUNET_GETOPT_option_increment_uint (char shortName, | |||
276 | struct GNUNET_GETOPT_CommandLineOption | 271 | struct GNUNET_GETOPT_CommandLineOption |
277 | GNUNET_GETOPT_option_verbose (unsigned int *level) | 272 | GNUNET_GETOPT_option_verbose (unsigned int *level) |
278 | { | 273 | { |
279 | struct GNUNET_GETOPT_CommandLineOption clo = { | 274 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'V', |
280 | .shortName = 'V', | 275 | .name = "verbose", |
281 | .name = "verbose", | 276 | .description = |
282 | .description = gettext_noop("be verbose"), | 277 | gettext_noop ("be verbose"), |
283 | .processor = &increment_value, | 278 | .processor = &increment_value, |
284 | .scls = (void *) level | 279 | .scls = (void *) level}; |
285 | }; | ||
286 | 280 | ||
287 | return clo; | 281 | return clo; |
288 | } | 282 | } |
@@ -330,17 +324,15 @@ set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
330 | */ | 324 | */ |
331 | struct GNUNET_GETOPT_CommandLineOption | 325 | struct GNUNET_GETOPT_CommandLineOption |
332 | GNUNET_GETOPT_option_flag (char shortName, | 326 | GNUNET_GETOPT_option_flag (char shortName, |
333 | const char *name, | 327 | const char *name, |
334 | const char *description, | 328 | const char *description, |
335 | int *val) | 329 | int *val) |
336 | { | 330 | { |
337 | struct GNUNET_GETOPT_CommandLineOption clo = { | 331 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
338 | .shortName = shortName, | 332 | .name = name, |
339 | .name = name, | 333 | .description = description, |
340 | .description = description, | 334 | .processor = &set_one, |
341 | .processor = &set_one, | 335 | .scls = (void *) val}; |
342 | .scls = (void *) val | ||
343 | }; | ||
344 | 336 | ||
345 | return clo; | 337 | return clo; |
346 | } | 338 | } |
@@ -393,15 +385,13 @@ GNUNET_GETOPT_option_string (char shortName, | |||
393 | const char *description, | 385 | const char *description, |
394 | char **str) | 386 | char **str) |
395 | { | 387 | { |
396 | struct GNUNET_GETOPT_CommandLineOption clo = { | 388 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
397 | .shortName = shortName, | 389 | .name = name, |
398 | .name = name, | 390 | .argumentHelp = argumentHelp, |
399 | .argumentHelp = argumentHelp, | 391 | .description = description, |
400 | .description = description, | 392 | .require_argument = 1, |
401 | .require_argument = 1, | 393 | .processor = &set_string, |
402 | .processor = &set_string, | 394 | .scls = (void *) str}; |
403 | .scls = (void *) str | ||
404 | }; | ||
405 | 395 | ||
406 | return clo; | 396 | return clo; |
407 | } | 397 | } |
@@ -416,15 +406,14 @@ GNUNET_GETOPT_option_string (char shortName, | |||
416 | struct GNUNET_GETOPT_CommandLineOption | 406 | struct GNUNET_GETOPT_CommandLineOption |
417 | GNUNET_GETOPT_option_loglevel (char **level) | 407 | GNUNET_GETOPT_option_loglevel (char **level) |
418 | { | 408 | { |
419 | struct GNUNET_GETOPT_CommandLineOption clo = { | 409 | struct GNUNET_GETOPT_CommandLineOption clo = |
420 | .shortName = 'L', | 410 | {.shortName = 'L', |
421 | .name = "log", | 411 | .name = "log", |
422 | .argumentHelp = "LOGLEVEL", | 412 | .argumentHelp = "LOGLEVEL", |
423 | .description = gettext_noop("configure logging to use LOGLEVEL"), | 413 | .description = gettext_noop ("configure logging to use LOGLEVEL"), |
424 | .require_argument = 1, | 414 | .require_argument = 1, |
425 | .processor = &set_string, | 415 | .processor = &set_string, |
426 | .scls = (void *) level | 416 | .scls = (void *) level}; |
427 | }; | ||
428 | 417 | ||
429 | return clo; | 418 | return clo; |
430 | } | 419 | } |
@@ -469,20 +458,18 @@ set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
469 | */ | 458 | */ |
470 | struct GNUNET_GETOPT_CommandLineOption | 459 | struct GNUNET_GETOPT_CommandLineOption |
471 | GNUNET_GETOPT_option_filename (char shortName, | 460 | GNUNET_GETOPT_option_filename (char shortName, |
472 | const char *name, | 461 | const char *name, |
473 | const char *argumentHelp, | 462 | const char *argumentHelp, |
474 | const char *description, | 463 | const char *description, |
475 | char **str) | 464 | char **str) |
476 | { | 465 | { |
477 | struct GNUNET_GETOPT_CommandLineOption clo = { | 466 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
478 | .shortName = shortName, | 467 | .name = name, |
479 | .name = name, | 468 | .argumentHelp = argumentHelp, |
480 | .argumentHelp = argumentHelp, | 469 | .description = description, |
481 | .description = description, | 470 | .require_argument = 1, |
482 | .require_argument = 1, | 471 | .processor = &set_filename, |
483 | .processor = &set_filename, | 472 | .scls = (void *) str}; |
484 | .scls = (void *) str | ||
485 | }; | ||
486 | 473 | ||
487 | return clo; | 474 | return clo; |
488 | } | 475 | } |
@@ -496,15 +483,15 @@ GNUNET_GETOPT_option_filename (char shortName, | |||
496 | struct GNUNET_GETOPT_CommandLineOption | 483 | struct GNUNET_GETOPT_CommandLineOption |
497 | GNUNET_GETOPT_option_logfile (char **logfn) | 484 | GNUNET_GETOPT_option_logfile (char **logfn) |
498 | { | 485 | { |
499 | struct GNUNET_GETOPT_CommandLineOption clo = { | 486 | struct GNUNET_GETOPT_CommandLineOption clo = |
500 | .shortName = 'l', | 487 | {.shortName = 'l', |
501 | .name = "logfile", | 488 | .name = "logfile", |
502 | .argumentHelp = "FILENAME", | 489 | .argumentHelp = "FILENAME", |
503 | .description = gettext_noop ("configure logging to write logs to FILENAME"), | 490 | .description = |
504 | .require_argument = 1, | 491 | gettext_noop ("configure logging to write logs to FILENAME"), |
505 | .processor = &set_filename, | 492 | .require_argument = 1, |
506 | .scls = (void *) logfn | 493 | .processor = &set_filename, |
507 | }; | 494 | .scls = (void *) logfn}; |
508 | 495 | ||
509 | return clo; | 496 | return clo; |
510 | } | 497 | } |
@@ -518,15 +505,14 @@ GNUNET_GETOPT_option_logfile (char **logfn) | |||
518 | struct GNUNET_GETOPT_CommandLineOption | 505 | struct GNUNET_GETOPT_CommandLineOption |
519 | GNUNET_GETOPT_option_cfgfile (char **fn) | 506 | GNUNET_GETOPT_option_cfgfile (char **fn) |
520 | { | 507 | { |
521 | struct GNUNET_GETOPT_CommandLineOption clo = { | 508 | struct GNUNET_GETOPT_CommandLineOption clo = |
522 | .shortName = 'c', | 509 | {.shortName = 'c', |
523 | .name = "config", | 510 | .name = "config", |
524 | .argumentHelp = "FILENAME", | 511 | .argumentHelp = "FILENAME", |
525 | .description = gettext_noop("use configuration file FILENAME"), | 512 | .description = gettext_noop ("use configuration file FILENAME"), |
526 | .require_argument = 1, | 513 | .require_argument = 1, |
527 | .processor = &set_filename, | 514 | .processor = &set_filename, |
528 | .scls = (void *) fn | 515 | .scls = (void *) fn}; |
529 | }; | ||
530 | 516 | ||
531 | return clo; | 517 | return clo; |
532 | } | 518 | } |
@@ -555,13 +541,10 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
555 | char dummy[2]; | 541 | char dummy[2]; |
556 | 542 | ||
557 | (void) ctx; | 543 | (void) ctx; |
558 | if (1 != SSCANF (value, | 544 | if (1 != SSCANF (value, "%llu%1s", val, dummy)) |
559 | "%llu%1s", | ||
560 | val, | ||
561 | dummy)) | ||
562 | { | 545 | { |
563 | FPRINTF (stderr, | 546 | FPRINTF (stderr, |
564 | _("You must pass a number to the `%s' option.\n"), | 547 | _ ("You must pass a number to the `%s' option.\n"), |
565 | option); | 548 | option); |
566 | return GNUNET_SYSERR; | 549 | return GNUNET_SYSERR; |
567 | } | 550 | } |
@@ -580,20 +563,18 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
580 | */ | 563 | */ |
581 | struct GNUNET_GETOPT_CommandLineOption | 564 | struct GNUNET_GETOPT_CommandLineOption |
582 | GNUNET_GETOPT_option_ulong (char shortName, | 565 | GNUNET_GETOPT_option_ulong (char shortName, |
583 | const char *name, | 566 | const char *name, |
584 | const char *argumentHelp, | 567 | const char *argumentHelp, |
585 | const char *description, | 568 | const char *description, |
586 | unsigned long long *val) | 569 | unsigned long long *val) |
587 | { | 570 | { |
588 | struct GNUNET_GETOPT_CommandLineOption clo = { | 571 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
589 | .shortName = shortName, | 572 | .name = name, |
590 | .name = name, | 573 | .argumentHelp = argumentHelp, |
591 | .argumentHelp = argumentHelp, | 574 | .description = description, |
592 | .description = description, | 575 | .require_argument = 1, |
593 | .require_argument = 1, | 576 | .processor = &set_ulong, |
594 | .processor = &set_ulong, | 577 | .scls = (void *) val}; |
595 | .scls = (void *) val | ||
596 | }; | ||
597 | 578 | ||
598 | return clo; | 579 | return clo; |
599 | } | 580 | } |
@@ -619,14 +600,12 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
619 | const char *value) | 600 | const char *value) |
620 | { | 601 | { |
621 | struct GNUNET_TIME_Relative *val = scls; | 602 | struct GNUNET_TIME_Relative *val = scls; |
622 | 603 | ||
623 | (void) ctx; | 604 | (void) ctx; |
624 | if (GNUNET_OK != | 605 | if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val)) |
625 | GNUNET_STRINGS_fancy_time_to_relative (value, | ||
626 | val)) | ||
627 | { | 606 | { |
628 | FPRINTF (stderr, | 607 | FPRINTF (stderr, |
629 | _("You must pass relative time to the `%s' option.\n"), | 608 | _ ("You must pass relative time to the `%s' option.\n"), |
630 | option); | 609 | option); |
631 | return GNUNET_SYSERR; | 610 | return GNUNET_SYSERR; |
632 | } | 611 | } |
@@ -646,20 +625,18 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
646 | */ | 625 | */ |
647 | struct GNUNET_GETOPT_CommandLineOption | 626 | struct GNUNET_GETOPT_CommandLineOption |
648 | GNUNET_GETOPT_option_relative_time (char shortName, | 627 | GNUNET_GETOPT_option_relative_time (char shortName, |
649 | const char *name, | 628 | const char *name, |
650 | const char *argumentHelp, | 629 | const char *argumentHelp, |
651 | const char *description, | 630 | const char *description, |
652 | struct GNUNET_TIME_Relative *val) | 631 | struct GNUNET_TIME_Relative *val) |
653 | { | 632 | { |
654 | struct GNUNET_GETOPT_CommandLineOption clo = { | 633 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
655 | .shortName = shortName, | 634 | .name = name, |
656 | .name = name, | 635 | .argumentHelp = argumentHelp, |
657 | .argumentHelp = argumentHelp, | 636 | .description = description, |
658 | .description = description, | 637 | .require_argument = 1, |
659 | .require_argument = 1, | 638 | .processor = &set_relative_time, |
660 | .processor = &set_relative_time, | 639 | .scls = (void *) val}; |
661 | .scls = (void *) val | ||
662 | }; | ||
663 | 640 | ||
664 | return clo; | 641 | return clo; |
665 | } | 642 | } |
@@ -687,12 +664,10 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
687 | struct GNUNET_TIME_Absolute *val = scls; | 664 | struct GNUNET_TIME_Absolute *val = scls; |
688 | 665 | ||
689 | (void) ctx; | 666 | (void) ctx; |
690 | if (GNUNET_OK != | 667 | if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val)) |
691 | GNUNET_STRINGS_fancy_time_to_absolute (value, | ||
692 | val)) | ||
693 | { | 668 | { |
694 | FPRINTF (stderr, | 669 | FPRINTF (stderr, |
695 | _("You must pass absolute time to the `%s' option.\n"), | 670 | _ ("You must pass absolute time to the `%s' option.\n"), |
696 | option); | 671 | option); |
697 | return GNUNET_SYSERR; | 672 | return GNUNET_SYSERR; |
698 | } | 673 | } |
@@ -712,20 +687,18 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
712 | */ | 687 | */ |
713 | struct GNUNET_GETOPT_CommandLineOption | 688 | struct GNUNET_GETOPT_CommandLineOption |
714 | GNUNET_GETOPT_option_absolute_time (char shortName, | 689 | GNUNET_GETOPT_option_absolute_time (char shortName, |
715 | const char *name, | 690 | const char *name, |
716 | const char *argumentHelp, | 691 | const char *argumentHelp, |
717 | const char *description, | 692 | const char *description, |
718 | struct GNUNET_TIME_Absolute *val) | 693 | struct GNUNET_TIME_Absolute *val) |
719 | { | 694 | { |
720 | struct GNUNET_GETOPT_CommandLineOption clo = { | 695 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
721 | .shortName = shortName, | 696 | .name = name, |
722 | .name = name, | 697 | .argumentHelp = argumentHelp, |
723 | .argumentHelp = argumentHelp, | 698 | .description = description, |
724 | .description = description, | 699 | .require_argument = 1, |
725 | .require_argument = 1, | 700 | .processor = &set_absolute_time, |
726 | .processor = &set_absolute_time, | 701 | .scls = (void *) val}; |
727 | .scls = (void *) val | ||
728 | }; | ||
729 | 702 | ||
730 | return clo; | 703 | return clo; |
731 | } | 704 | } |
@@ -754,20 +727,18 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
754 | char dummy[2]; | 727 | char dummy[2]; |
755 | 728 | ||
756 | (void) ctx; | 729 | (void) ctx; |
757 | if('-' == *value) | 730 | if ('-' == *value) |
758 | { | 731 | { |
759 | FPRINTF (stderr, | 732 | FPRINTF (stderr, |
760 | _("Your input for the '%s' option has to be a non negative number \n"), | 733 | _ ( |
761 | option); | 734 | "Your input for the '%s' option has to be a non negative number \n"), |
762 | return GNUNET_SYSERR; | 735 | option); |
736 | return GNUNET_SYSERR; | ||
763 | } | 737 | } |
764 | if (1 != SSCANF (value, | 738 | if (1 != SSCANF (value, "%u%1s", val, dummy)) |
765 | "%u%1s", | ||
766 | val, | ||
767 | dummy)) | ||
768 | { | 739 | { |
769 | FPRINTF (stderr, | 740 | FPRINTF (stderr, |
770 | _("You must pass a number to the `%s' option.\n"), | 741 | _ ("You must pass a number to the `%s' option.\n"), |
771 | option); | 742 | option); |
772 | return GNUNET_SYSERR; | 743 | return GNUNET_SYSERR; |
773 | } | 744 | } |
@@ -786,26 +757,23 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
786 | */ | 757 | */ |
787 | struct GNUNET_GETOPT_CommandLineOption | 758 | struct GNUNET_GETOPT_CommandLineOption |
788 | GNUNET_GETOPT_option_uint (char shortName, | 759 | GNUNET_GETOPT_option_uint (char shortName, |
789 | const char *name, | 760 | const char *name, |
790 | const char *argumentHelp, | 761 | const char *argumentHelp, |
791 | const char *description, | 762 | const char *description, |
792 | unsigned int *val) | 763 | unsigned int *val) |
793 | { | 764 | { |
794 | struct GNUNET_GETOPT_CommandLineOption clo = { | 765 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
795 | .shortName = shortName, | 766 | .name = name, |
796 | .name = name, | 767 | .argumentHelp = argumentHelp, |
797 | .argumentHelp = argumentHelp, | 768 | .description = description, |
798 | .description = description, | 769 | .require_argument = 1, |
799 | .require_argument = 1, | 770 | .processor = &set_uint, |
800 | .processor = &set_uint, | 771 | .scls = (void *) val}; |
801 | .scls = (void *) val | ||
802 | }; | ||
803 | 772 | ||
804 | return clo; | 773 | return clo; |
805 | } | 774 | } |
806 | 775 | ||
807 | 776 | ||
808 | |||
809 | /** | 777 | /** |
810 | * Set an option of type 'uint16_t' from the command line. | 778 | * Set an option of type 'uint16_t' from the command line. |
811 | * A pointer to this function should be passed as part of the | 779 | * A pointer to this function should be passed as part of the |
@@ -821,30 +789,27 @@ GNUNET_GETOPT_option_uint (char shortName, | |||
821 | */ | 789 | */ |
822 | static int | 790 | static int |
823 | set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 791 | set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
824 | void *scls, | 792 | void *scls, |
825 | const char *option, | 793 | const char *option, |
826 | const char *value) | 794 | const char *value) |
827 | { | 795 | { |
828 | uint16_t *val = scls; | 796 | uint16_t *val = scls; |
829 | unsigned int v; | 797 | unsigned int v; |
830 | char dummy[2]; | 798 | char dummy[2]; |
831 | 799 | ||
832 | (void) ctx; | 800 | (void) ctx; |
833 | if (1 != SSCANF (value, | 801 | if (1 != SSCANF (value, "%u%1s", &v, dummy)) |
834 | "%u%1s", | ||
835 | &v, | ||
836 | dummy)) | ||
837 | { | 802 | { |
838 | FPRINTF (stderr, | 803 | FPRINTF (stderr, |
839 | _("You must pass a number to the `%s' option.\n"), | 804 | _ ("You must pass a number to the `%s' option.\n"), |
840 | option); | 805 | option); |
841 | return GNUNET_SYSERR; | 806 | return GNUNET_SYSERR; |
842 | } | 807 | } |
843 | if (v > UINT16_MAX) | 808 | if (v > UINT16_MAX) |
844 | { | 809 | { |
845 | FPRINTF (stderr, | 810 | FPRINTF (stderr, |
846 | _("You must pass a number below %u to the `%s' option.\n"), | 811 | _ ("You must pass a number below %u to the `%s' option.\n"), |
847 | (unsigned int) UINT16_MAX, | 812 | (unsigned int) UINT16_MAX, |
848 | option); | 813 | option); |
849 | return GNUNET_SYSERR; | 814 | return GNUNET_SYSERR; |
850 | } | 815 | } |
@@ -864,20 +829,18 @@ set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
864 | */ | 829 | */ |
865 | struct GNUNET_GETOPT_CommandLineOption | 830 | struct GNUNET_GETOPT_CommandLineOption |
866 | GNUNET_GETOPT_option_uint16 (char shortName, | 831 | GNUNET_GETOPT_option_uint16 (char shortName, |
867 | const char *name, | 832 | const char *name, |
868 | const char *argumentHelp, | 833 | const char *argumentHelp, |
869 | const char *description, | 834 | const char *description, |
870 | uint16_t *val) | 835 | uint16_t *val) |
871 | { | 836 | { |
872 | struct GNUNET_GETOPT_CommandLineOption clo = { | 837 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
873 | .shortName = shortName, | 838 | .name = name, |
874 | .name = name, | 839 | .argumentHelp = argumentHelp, |
875 | .argumentHelp = argumentHelp, | 840 | .description = description, |
876 | .description = description, | 841 | .require_argument = 1, |
877 | .require_argument = 1, | 842 | .processor = &set_uint16, |
878 | .processor = &set_uint16, | 843 | .scls = (void *) val}; |
879 | .scls = (void *) val | ||
880 | }; | ||
881 | 844 | ||
882 | return clo; | 845 | return clo; |
883 | } | 846 | } |
@@ -922,15 +885,16 @@ set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
922 | struct Base32Context *bc = scls; | 885 | struct Base32Context *bc = scls; |
923 | 886 | ||
924 | (void) ctx; | 887 | (void) ctx; |
925 | if (GNUNET_OK != | 888 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (value, |
926 | GNUNET_STRINGS_string_to_data (value, | 889 | strlen (value), |
927 | strlen (value), | 890 | bc->val, |
928 | bc->val, | 891 | bc->val_size)) |
929 | bc->val_size)) | ||
930 | { | 892 | { |
931 | fprintf (stderr, | 893 | fprintf ( |
932 | _("Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), | 894 | stderr, |
933 | option); | 895 | _ ( |
896 | "Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"), | ||
897 | option); | ||
934 | return GNUNET_SYSERR; | 898 | return GNUNET_SYSERR; |
935 | } | 899 | } |
936 | return GNUNET_OK; | 900 | return GNUNET_OK; |
@@ -963,23 +927,21 @@ free_bc (void *cls) | |||
963 | */ | 927 | */ |
964 | struct GNUNET_GETOPT_CommandLineOption | 928 | struct GNUNET_GETOPT_CommandLineOption |
965 | GNUNET_GETOPT_option_base32_fixed_size (char shortName, | 929 | GNUNET_GETOPT_option_base32_fixed_size (char shortName, |
966 | const char *name, | 930 | const char *name, |
967 | const char *argumentHelp, | 931 | const char *argumentHelp, |
968 | const char *description, | 932 | const char *description, |
969 | void *val, | 933 | void *val, |
970 | size_t val_size) | 934 | size_t val_size) |
971 | { | 935 | { |
972 | struct Base32Context *bc = GNUNET_new (struct Base32Context); | 936 | struct Base32Context *bc = GNUNET_new (struct Base32Context); |
973 | struct GNUNET_GETOPT_CommandLineOption clo = { | 937 | struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, |
974 | .shortName = shortName, | 938 | .name = name, |
975 | .name = name, | 939 | .argumentHelp = argumentHelp, |
976 | .argumentHelp = argumentHelp, | 940 | .description = description, |
977 | .description = description, | 941 | .require_argument = 1, |
978 | .require_argument = 1, | 942 | .processor = &set_base32, |
979 | .processor = &set_base32, | 943 | .cleaner = &free_bc, |
980 | .cleaner = &free_bc, | 944 | .scls = (void *) bc}; |
981 | .scls = (void *) bc | ||
982 | }; | ||
983 | 945 | ||
984 | bc->val = val; | 946 | bc->val = val; |
985 | bc->val_size = val_size; | 947 | bc->val_size = val_size; |
@@ -1001,4 +963,18 @@ GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt) | |||
1001 | } | 963 | } |
1002 | 964 | ||
1003 | 965 | ||
966 | /** | ||
967 | * Make the given option mutually exclusive with other options. | ||
968 | * | ||
969 | * @param opt option to modify | ||
970 | * @return @a opt with the exclusive flag set. | ||
971 | */ | ||
972 | struct GNUNET_GETOPT_CommandLineOption | ||
973 | GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt) | ||
974 | { | ||
975 | opt.option_exclusive = 1; | ||
976 | return opt; | ||
977 | } | ||
978 | |||
979 | |||
1004 | /* end of getopt_helpers.c */ | 980 | /* end of getopt_helpers.c */ |