2 NOTE: getopt is now part of the C library, so if you don't know what
3 "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97
7 Free Software Foundation, Inc.
9 NOTE: The canonical source of this file is maintained with the GNU C Library.
10 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by the
14 Free Software Foundation; either version 3, or (at your option) any
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 This code was heavily modified for GNUnet.
29 Copyright (C) 2006, 2017 Christian Grothoff
34 * @brief GNU style option parsing
36 * TODO: get rid of statics (make reentrant) and
37 * replace main GNU getopt parser with one that
38 * actually fits our API.
41 #include "gnunet_util_lib.h"
50 #define LOG(kind, ...) GNUNET_log_from(kind, "util-getopt", __VA_ARGS__)
52 #define LOG_STRERROR(kind, syscall) \
53 GNUNET_log_from_strerror(kind, "util-getopt", syscall)
56 /* This is for other GNU distributions with internationalized messages.
57 When compiling libc, the _ macro is predefined. */
60 #define _(msgid) gettext(msgid)
62 #define _(msgid) (msgid)
66 /* Describe the long-named options requested by the application.
67 The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
68 of `struct GNoption' terminated by an element containing a name which is
71 The field `has_arg' is:
72 no_argument (or 0) if the option does not take an argument,
73 required_argument (or 1) if the option requires an argument,
74 optional_argument (or 2) if the option takes an optional argument.
76 If the field `flag' is not NULL, it points to a variable that is set
77 to the value given in the field `val' when the option is found, but
78 left unchanged if the option is not found.
80 To have a long-named option do something other than set an `int' to
81 a compiled-in constant, such as set a value from `GNoptarg', set the
82 option's `flag' field to zero and its `val' field to a nonzero
83 value (the equivalent single-letter option character, if there is
84 one). For long options that have a zero `flag' field, `getopt'
85 returns the contents of the `val' field. */
89 /* has_arg can't be an enum because some compilers complain about
90 * type mismatches in all the code that assumes it is an int. */
97 /* This version of `getopt' appears to the caller like standard Unix `getopt'
98 but it behaves differently for the user, since it allows the user
99 to intersperse the options with the other arguments.
101 As `getopt' works, it permutes the elements of ARGV so that,
102 when it is done, all the options precede everything else. Thus
103 all application programs are extended to handle flexible argument order.
105 Setting the environment variable POSIXLY_CORRECT disables permutation.
106 Then the behavior is completely standard.
108 GNU application programs can use a third alternative mode in which
109 they can distinguish the relative order of options and other arguments. */
111 /* For communication from `getopt' to the caller.
112 When `getopt' finds an option that takes an argument,
113 the argument value is returned here.
114 Also, when `ordering' is RETURN_IN_ORDER,
115 each non-option ARGV-element is returned here. */
117 static char *GNoptarg = NULL;
119 /* Index in ARGV of the next element to be scanned.
120 This is used for communication to and from the caller
121 and for communication between successive calls to `getopt'.
123 On entry to `getopt', zero means this is the first call; initialize.
125 When `getopt' returns -1, this is the index of the first of the
126 non-option elements that the caller should itself scan.
128 Otherwise, `GNoptind' communicates from one call to the next
129 how much of ARGV has been scanned so far. */
131 /* 1003.2 says this must be 1 before any call. */
132 static int GNoptind = 1;
134 /* The next char to be scanned in the option-element
135 in which the last option character we returned was found.
136 This allows us to pick up the scan where we left off.
138 If this is zero, or a null string, it means resume the scan
139 by advancing to the next ARGV-element. */
141 static char *nextchar;
144 /* Describe how to deal with options that follow non-option ARGV-elements.
146 If the caller did not specify anything,
147 the default is REQUIRE_ORDER if the environment variable
148 POSIXLY_CORRECT is defined, PERMUTE otherwise.
150 REQUIRE_ORDER means don't recognize them as options;
151 stop option processing when the first non-option is seen.
152 This is what Unix does.
153 This mode of operation is selected by either setting the environment
154 variable POSIXLY_CORRECT, or using `+' as the first character
155 of the list of option characters.
157 PERMUTE is the default. We GNUNET_CRYPTO_random_permute the contents of ARGV as we scan,
158 so that eventually all the non-options are at the end. This allows options
159 to be given in any order, even with programs that were not written to
162 RETURN_IN_ORDER is an option available to programs that were written
163 to expect GNoptions and other ARGV-elements in any order and that care about
164 the ordering of the two. We describe each non-option ARGV-element
165 as if it were the argument of an option with character code 1.
166 Using `-' as the first character of the list of option characters
167 selects this mode of operation.
169 The special argument `--' forces an end of option-scanning regardless
170 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
171 `--' can cause `getopt' to return -1 with `GNoptind' != ARGC. */
173 static enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering;
175 /* Value of POSIXLY_CORRECT environment variable. */
176 static char *posixly_correct;
178 #ifdef __GNU_LIBRARY__
179 /* We want to avoid inclusion of string.h with non-GNU libraries
180 because there are many ways it can cause trouble.
181 On some systems, it contains special magic macros that don't work
184 #define my_index strchr
187 /* Avoid depending on library functions or files
188 whose names are inconsistent. */
194 my_index(const char *str, int chr)
205 /* If using GCC, we can safely declare strlen this way.
206 If not using GCC, it is ok not to declare it. */
208 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
209 That was relevant to code that was here before. */
210 #if !defined(__STDC__) || !__STDC__
211 /* gcc with -traditional declares the built-in strlen to return int,
212 and has done so at least since version 2.4.5. -- rms. */
214 strlen(const char *);
215 #endif /* not __STDC__ */
216 #endif /* __GNUC__ */
218 #endif /* not __GNU_LIBRARY__ */
220 /* Handle permutation of arguments. */
222 /* Describe the part of ARGV that contains non-options that have
223 been skipped. `first_nonopt' is the index in ARGV of the first of them;
224 `last_nonopt' is the index after the last of them. */
226 static int first_nonopt;
227 static int last_nonopt;
229 #define SWAP_FLAGS(ch1, ch2)
231 /* Exchange two adjacent subsequences of ARGV.
232 One subsequence is elements [first_nonopt,last_nonopt)
233 which contains all the non-options that have been skipped so far.
234 The other is elements [last_nonopt,GNoptind), which contains all
235 the options processed since those non-options were skipped.
237 `first_nonopt' and `last_nonopt' are relocated so that they describe
238 the new indices of the non-options in ARGV after they are moved. */
240 #if defined(__STDC__) && __STDC__
246 exchange(char **argv)
248 int bottom = first_nonopt;
249 int middle = last_nonopt;
253 /* Exchange the shorter segment with the far end of the longer segment.
254 * That puts the shorter segment into the right place.
255 * It leaves the longer segment in the right place overall,
256 * but it consists of two parts that need to be swapped next. */
258 while (top > middle && middle > bottom)
260 if (top - middle > middle - bottom)
262 /* Bottom segment is the short one. */
263 int len = middle - bottom;
266 /* Swap it with the top part of the top segment. */
267 for (i = 0; i < len; i++)
269 tem = argv[bottom + i];
270 argv[bottom + i] = argv[top - (middle - bottom) + i];
271 argv[top - (middle - bottom) + i] = tem;
272 SWAP_FLAGS(bottom + i, top - (middle - bottom) + i);
274 /* Exclude the moved bottom segment from further swapping. */
279 /* Top segment is the short one. */
280 int len = top - middle;
283 /* Swap it with the bottom part of the bottom segment. */
284 for (i = 0; i < len; i++)
286 tem = argv[bottom + i];
287 argv[bottom + i] = argv[middle + i];
288 argv[middle + i] = tem;
289 SWAP_FLAGS(bottom + i, middle + i);
291 /* Exclude the moved top segment from further swapping. */
296 /* Update records for the slots the non-options now occupy. */
298 first_nonopt += (GNoptind - last_nonopt);
299 last_nonopt = GNoptind;
302 /* Initialize the internal data when the first call is made. */
304 #if defined(__STDC__) && __STDC__
306 _getopt_initialize(int, char *const *, const char *);
309 _getopt_initialize(int argc, char *const *argv, const char *optstring)
311 /* Start processing options with ARGV-element 1 (since ARGV-element 0
312 * is the program name); the sequence of previously skipped
313 * non-option ARGV-elements is empty. */
315 first_nonopt = last_nonopt = GNoptind;
319 posixly_correct = getenv("POSIXLY_CORRECT");
321 /* Determine how to handle the ordering of options and nonoptions. */
323 if (optstring[0] == '-')
325 ordering = RETURN_IN_ORDER;
328 else if (optstring[0] == '+')
330 ordering = REQUIRE_ORDER;
333 else if (posixly_correct != NULL)
334 ordering = REQUIRE_ORDER;
341 /* Scan elements of ARGV (whose length is ARGC) for option characters
344 If an element of ARGV starts with '-', and is not exactly "-" or "--",
345 then it is an option element. The characters of this element
346 (aside from the initial '-') are option characters. If `getopt'
347 is called repeatedly, it returns successively each of the option characters
348 from each of the option elements.
350 If `getopt' finds another option character, it returns that character,
351 updating `GNoptind' and `nextchar' so that the next call to `getopt' can
352 resume the scan with the following option character or ARGV-element.
354 If there are no more option characters, `getopt' returns -1.
355 Then `GNoptind' is the index in ARGV of the first ARGV-element
356 that is not an option. (The ARGV-elements have been permuted
357 so that those that are not options now come last.)
359 OPTSTRING is a string containing the legitimate option characters.
360 If an option character is seen that is not listed in OPTSTRING,
361 return '?' after printing an error message. If you set `GNopterr' to
362 zero, the error message is suppressed but we still return '?'.
364 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
365 so the following text in the same ARGV-element, or the text of the following
366 ARGV-element, is returned in `GNoptarg'. Two colons mean an option that
367 wants an optional arg; if there is text in the current ARGV-element,
368 it is returned in `GNoptarg', otherwise `GNoptarg' is set to zero.
370 If OPTSTRING starts with `-' or `+', it requests different methods of
371 handling the non-option ARGV-elements.
372 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
374 Long-named options begin with `--' instead of `-'.
375 Their names may be abbreviated as long as the abbreviation is unique
376 or is an exact match for some defined option. If they have an
377 argument, it follows the option name in the same ARGV-element, separated
378 from the option name by a `=', or else the in next ARGV-element.
379 When `getopt' finds a long-named option, it returns 0 if that option's
380 `flag' field is nonzero, the value of the option's `val' field
381 if the `flag' field is zero.
383 The elements of ARGV aren't really const, because we GNUNET_CRYPTO_random_permute them.
384 But we pretend they're const in the prototype to be compatible
387 LONGOPTS is a vector of `struct GNoption' terminated by an
388 element containing a name which is zero.
390 LONGIND returns the index in LONGOPT of the long-named option found.
391 It is only valid when a long-named option has been found by the most
394 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
395 long-named options. */
398 GN_getopt_internal(int argc,
400 const char *optstring,
401 const struct GNoption *longopts,
405 static int __getopt_initialized = 0;
406 static int GNopterr = 1;
410 if (GNoptind == 0 || !__getopt_initialized)
413 GNoptind = 1; /* Don't scan ARGV[0], the program name. */
414 optstring = _getopt_initialize(argc, argv, optstring);
415 __getopt_initialized = 1;
418 /* Test whether ARGV[GNoptind] points to a non-option argument.
419 * Either it does not have option syntax, or there is an environment flag
420 * from the shell indicating it is not an option. The later information
421 * is only used when the used in the GNU libc. */
422 #define NONOPTION_P (argv[GNoptind][0] != '-' || argv[GNoptind][1] == '\0')
424 if (nextchar == NULL || *nextchar == '\0')
426 /* Advance to the next ARGV-element. */
428 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been
429 * moved back by the user (who may also have changed the arguments). */
430 if (last_nonopt > GNoptind)
431 last_nonopt = GNoptind;
432 if (first_nonopt > GNoptind)
433 first_nonopt = GNoptind;
435 if (ordering == PERMUTE)
437 /* If we have just processed some options following some non-options,
438 * exchange them so that the options come first. */
440 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
441 exchange((char **)argv);
442 else if (last_nonopt != GNoptind)
443 first_nonopt = GNoptind;
445 /* Skip any additional non-options
446 * and extend the range of non-options previously skipped. */
448 while (GNoptind < argc && NONOPTION_P)
450 last_nonopt = GNoptind;
453 /* The special ARGV-element `--' means premature end of options.
454 * Skip it like a null option,
455 * then exchange with previous non-options as if it were an option,
456 * then skip everything else like a non-option. */
457 if (GNoptind != argc && !strcmp(argv[GNoptind], "--"))
461 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
462 exchange((char **)argv);
463 else if (first_nonopt == last_nonopt)
464 first_nonopt = GNoptind;
470 /* If we have done all the ARGV-elements, stop the scan
471 * and back over any non-options that we skipped and permuted. */
473 if (GNoptind == argc)
475 /* Set the next-arg-index to point at the non-options
476 * that we previously skipped, so the caller will digest them. */
477 if (first_nonopt != last_nonopt)
478 GNoptind = first_nonopt;
482 /* If we have come to a non-option and did not permute it,
483 * either stop the scan or describe it to the caller and pass it by. */
487 if (ordering == REQUIRE_ORDER)
489 GNoptarg = argv[GNoptind++];
493 /* We have found another option-ARGV-element.
494 * Skip the initial punctuation. */
497 (argv[GNoptind] + 1 + (longopts != NULL && argv[GNoptind][1] == '-'));
500 /* Decode the current option-ARGV-element. */
502 /* Check whether the ARGV-element is a long option.
504 * If long_only and the ARGV-element has the form "-f", where f is
505 * a valid short option, don't consider it an abbreviated form of
506 * a long option that starts with f. Otherwise there would be no
507 * way to give the -f short option.
509 * On the other hand, if there's a long option "fubar" and
510 * the ARGV-element is "-fu", do consider that an abbreviation of
511 * the long option, just like "--fu", and not "-f" with arg "u".
513 * This distinction seems to be the most useful approach. */
515 if (longopts != NULL &&
516 (argv[GNoptind][1] == '-' ||
518 (argv[GNoptind][2] || !my_index(optstring, argv[GNoptind][1])))))
521 const struct GNoption *p;
522 const struct GNoption *pfound = NULL;
528 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
531 /* Test all long options for either exact match
532 * or abbreviated matches. */
533 for (p = longopts, option_index = 0; p->name; p++, option_index++)
534 if (!strncmp(p->name, nextchar, nameend - nextchar))
536 if ((unsigned int)(nameend - nextchar) ==
537 (unsigned int)strlen(p->name))
539 /* Exact match found. */
541 indfound = option_index;
545 else if (pfound == NULL)
547 /* First nonexact match found. */
549 indfound = option_index;
552 /* Second or later nonexact match found. */
560 _("%s: option `%s' is ambiguous\n"),
563 nextchar += strlen(nextchar);
570 option_index = indfound;
574 /* Don't test has_arg with >, because some C compilers don't
575 * allow it to be used on enums. */
577 GNoptarg = nameend + 1;
582 if (argv[GNoptind - 1][1] == '-')
585 _("%s: option `--%s' does not allow an argument\n"),
589 /* +option or -option */
591 _("%s: option `%c%s' does not allow an argument\n"),
593 argv[GNoptind - 1][0],
596 nextchar += strlen(nextchar);
600 else if (pfound->has_arg == 1)
604 GNoptarg = argv[GNoptind++];
611 _("%s: option `%s' requires an argument\n"),
615 nextchar += strlen(nextchar);
616 return (optstring[0] == ':') ? ':' : '?';
619 nextchar += strlen(nextchar);
621 *longind = option_index;
624 *(pfound->flag) = pfound->val;
630 /* Can't find it as a long option. If this is not getopt_long_only,
631 * or the option starts with '--' or is not a valid short
632 * option, then it's an error.
633 * Otherwise interpret it as a short option. */
634 if (!long_only || argv[GNoptind][1] == '-' ||
635 my_index(optstring, *nextchar) == NULL)
639 if (argv[GNoptind][1] == '-')
642 _("%s: unrecognized option `--%s'\n"),
646 /* +option or -option */
648 _("%s: unrecognized option `%c%s'\n"),
653 nextchar = (char *)"";
659 /* Look at and handle the next short option-character. */
662 char c = *nextchar++;
663 char *temp = my_index(optstring, c);
665 /* Increment `GNoptind' when we start to process its last character. */
666 if (*nextchar == '\0')
669 if (temp == NULL || c == ':')
674 /* 1003.2 specifies the format of this message. */
675 fprintf(stderr, _("%s: illegal option -- %c\n"), argv[0], c);
677 fprintf(stderr, _("%s: invalid option -- %c\n"), argv[0], c);
681 /* Convenience. Treat POSIX -W foo same as long option --foo */
682 if (temp[0] == 'W' && temp[1] == ';')
685 const struct GNoption *p;
686 const struct GNoption *pfound = NULL;
692 /* This is an option that requires an argument. */
693 if (*nextchar != '\0')
696 /* If we end this ARGV-element by taking the rest as an arg,
697 * we must advance to the next element now. */
700 else if (GNoptind == argc)
704 /* 1003.2 specifies the format of this message. */
706 _("%s: option requires an argument -- %c\n"),
710 if (optstring[0] == ':')
717 /* We already incremented `GNoptind' once;
718 * increment it again when taking next ARGV-elt as argument. */
719 GNoptarg = argv[GNoptind++];
721 /* GNoptarg is now the argument, see if it's in the
722 * table of longopts. */
724 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '=';
728 /* Test all long options for either exact match
729 * or abbreviated matches. */
730 if (longopts != NULL)
731 for (p = longopts, option_index = 0; p->name; p++, option_index++)
732 if (!strncmp(p->name, nextchar, nameend - nextchar))
734 if ((unsigned int)(nameend - nextchar) == strlen(p->name))
736 /* Exact match found. */
738 indfound = option_index;
742 else if (pfound == NULL)
744 /* First nonexact match found. */
746 indfound = option_index;
749 /* Second or later nonexact match found. */
756 _("%s: option `-W %s' is ambiguous\n"),
759 nextchar += strlen(nextchar);
765 option_index = indfound;
768 /* Don't test has_arg with >, because some C compilers don't
769 * allow it to be used on enums. */
771 GNoptarg = nameend + 1;
776 _("%s: option `-W %s' does not allow an argument\n"),
780 nextchar += strlen(nextchar);
784 else if (pfound->has_arg == 1)
787 GNoptarg = argv[GNoptind++];
792 _("%s: option `%s' requires an argument\n"),
795 nextchar += strlen(nextchar);
796 return optstring[0] == ':' ? ':' : '?';
799 nextchar += strlen(nextchar);
801 *longind = option_index;
804 *(pfound->flag) = pfound->val;
810 return 'W'; /* Let the application handle it. */
816 /* This is an option that accepts an argument optionally. */
817 if (*nextchar != '\0')
828 /* This is an option that requires an argument. */
829 if (*nextchar != '\0')
832 /* If we end this ARGV-element by taking the rest as an arg,
833 * we must advance to the next element now. */
836 else if (GNoptind == argc)
840 /* 1003.2 specifies the format of this message. */
842 _("%s: option requires an argument -- %c\n"),
846 if (optstring[0] == ':')
852 /* We already incremented `GNoptind' once;
853 * increment it again when taking next ARGV-elt as argument. */
854 GNoptarg = argv[GNoptind++];
864 GNgetopt_long(int argc,
867 const struct GNoption *long_options,
870 return GN_getopt_internal(argc, argv, options, long_options, opt_index, 0);
873 /* ******************** now the GNUnet specific modifications... ********************* */
876 * Parse the command line.
878 * @param binaryOptions Name of application with option summary
879 * @param allOptions defined options and handlers
880 * @param argc number of arguments
881 * @param argv actual arguments
882 * @return index into argv with first non-option
883 * argument, or #GNUNET_SYSERR on error
886 GNUNET_GETOPT_run(const char *binaryOptions,
887 const struct GNUNET_GETOPT_CommandLineOption *allOptions,
891 struct GNoption *long_options;
892 struct GNUNET_GETOPT_CommandLineProcessorContext clpc;
898 unsigned int optmatch = 0;
899 const char *have_exclusive = NULL;
901 GNUNET_assert(argc > 0);
903 clpc.binaryName = argv[0];
904 clpc.binaryOptions = binaryOptions;
905 clpc.allOptions = allOptions;
908 for (count = 0; NULL != allOptions[count].name; count++)
911 /* transform our option representation into the format
912 used by the GNU getopt copylib */
913 long_options = GNUNET_new_array(count + 1, struct GNoption);
914 seen = GNUNET_new_array(count, uint8_t);
915 shorts = GNUNET_malloc(count * 2 + 1);
917 for (unsigned i = 0; i < count; i++)
919 long_options[i].name = allOptions[i].name;
920 long_options[i].has_arg = allOptions[i].require_argument;
921 long_options[i].flag = NULL;
922 long_options[i].val = allOptions[i].shortName;
923 shorts[spos++] = allOptions[i].shortName;
924 if (allOptions[i].require_argument != 0)
925 shorts[spos++] = ':';
927 long_options[count].name = NULL;
928 long_options[count].has_arg = 0;
929 long_options[count].flag = NULL;
930 long_options[count].val = '\0';
934 /* main getopt loop */
937 int option_index = 0;
941 c = GNgetopt_long(argc, argv, shorts, long_options, &option_index);
942 if (c == GNUNET_SYSERR)
943 break; /* No more flags to process */
945 /* Check which of our program's options was given by the user */
946 for (i = 0; i < count; i++)
948 clpc.currentArgument = GNoptind - 1;
949 if ((char)c == allOptions[i].shortName)
952 if (allOptions[i].option_exclusive)
953 have_exclusive = allOptions[i].name;
954 if (GNUNET_OK == cont)
956 /* parse the option using the option-specific processor */
957 cont = allOptions[i].processor(&clpc,
968 fprintf(stderr, _("Use %s to get a list of options.\n"), "--help");
969 cont = GNUNET_SYSERR;
973 GNUNET_free(long_options);
975 /* check that if any option that was marked as exclusive
976 is the only option that was provided */
977 if ((NULL != have_exclusive) && (optmatch > 1))
980 _("Option `%s' can't be used with other options.\n"),
982 cont = GNUNET_SYSERR;
984 if (GNUNET_YES == cont)
986 /* check that all mandatory options are present */
987 for (count = 0; NULL != allOptions[count].name; count++)
989 if ((0 == seen[count]) && (allOptions[count].option_mandatory))
992 _("Missing mandatory option `%s'.\n"),
993 allOptions[count].name);
994 cont = GNUNET_SYSERR;
1000 /* call cleaners, if available */
1001 for (unsigned int i = 0; NULL != allOptions[i].name; i++)
1002 if (NULL != allOptions[i].cleaner)
1003 allOptions[i].cleaner(allOptions[i].scls);
1005 if (GNUNET_OK != cont)
1010 /* end of getopt.c */