diff options
Diffstat (limited to 'gnu/usr.bin/cvs/lib/getopt.c')
-rw-r--r-- | gnu/usr.bin/cvs/lib/getopt.c | 535 |
1 files changed, 345 insertions, 190 deletions
diff --git a/gnu/usr.bin/cvs/lib/getopt.c b/gnu/usr.bin/cvs/lib/getopt.c index c322fc2..446a8e4 100644 --- a/gnu/usr.bin/cvs/lib/getopt.c +++ b/gnu/usr.bin/cvs/lib/getopt.c @@ -1,10 +1,15 @@ /* Getopt for GNU. - Copyright (C) 1987-1992 Free Software Foundation, Inc. + NOTE: getopt is now part of the C library, so if you don't know what + "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu + before changing it! - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94 + Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -13,73 +18,73 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ -#if !__STDC__ +/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. + Ditto for AIX 3.2 and <stdlib.h>. */ +#ifndef _NO_PROTO +#define _NO_PROTO +#endif + +#ifdef HAVE_CONFIG_H +#if defined (emacs) || defined (CONFIG_BROKETS) +/* We use <config.h> instead of "config.h" so that a compilation + using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h + (which it would do because it found this file in $srcdir). */ +#include <config.h> +#else +#include "config.h" +#endif +#endif + +#ifndef __STDC__ +/* This is a separate conditional since some stdc systems + reject `defined (const)'. */ +#ifndef const #define const #endif +#endif + +#include <stdio.h> + +/* Comment out all this code if we are using the GNU C Library, and are not + actually compiling the library itself. This code is part of the GNU C + Library, but also included in many other GNU distributions. Compiling + and linking in this code is a waste when using the GNU C library + (especially if it is a shared library). Rather than having every GNU + program understand `configure --with-gnu-libc' and omit the object files, + it is simpler to just do this in the source for each such file. */ + +#if defined (_LIBC) || !defined (__GNU_LIBRARY__) + + +/* This needs to come after some library #include + to get __GNU_LIBRARY__ defined. */ +#ifdef __GNU_LIBRARY__ +/* Don't include stdlib.h for non-GNU C libraries because some of them + contain conflicting prototypes for getopt. */ +#include <stdlib.h> +#endif /* GNU C library. */ /* This version of `getopt' appears to the caller like standard Unix `getopt' but it behaves differently for the user, since it allows the user to intersperse the options with the other arguments. - As `getopt' works, it permutes the elements of `argv' so that, + As `getopt' works, it permutes the elements of ARGV so that, when it is done, all the options precede everything else. Thus all application programs are extended to handle flexible argument order. - Setting the environment variable _POSIX_OPTION_ORDER disables permutation. + Setting the environment variable POSIXLY_CORRECT disables permutation. Then the behavior is completely standard. GNU application programs can use a third alternative mode in which they can distinguish the relative order of options and other arguments. */ #ifndef lint -static char rcsid[] = "@(#)getopt.c 1.7 92/03/31"; -#endif - -#include <stdio.h> - -#if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__) -#include <stdlib.h> -#else /* STDC_HEADERS or __GNU_LIBRARY__ */ -char *getenv (); -char *malloc (); -#endif /* STDC_HEADERS or __GNU_LIBRARY__ */ - -/* AIX requires this to be the first thing in the file. */ -#ifdef __GNUC__ -#if !defined(bsdi) && !defined(__386BSD__) -#define alloca __builtin_alloca +static char rcsid[] = "$CVSid: @(#)getopt.c 1.10 94/09/21 $"; #endif -#else /* not __GNUC__ */ -#ifdef sparc -#include <alloca.h> -#else -#ifdef _AIX - #pragma alloca -#else -char *alloca (); -#endif -#endif /* sparc */ -#endif /* not __GNUC__ */ -#if defined(USG) || defined(STDC_HEADERS) || defined(__GNU_LIBRARY__) -#include <string.h> -#ifndef bcopy -#define bcopy(s, d, n) memcpy ((d), (s), (n)) -#endif -#ifndef index -#define index strchr -#endif -#else /* USG or STDC_HEADERS or __GNU_LIBRARY__ */ -#ifdef VMS -#include <string.h> -#else /* VMS */ -#include <strings.h> -#endif /* VMS */ -/* Declaring bcopy causes errors on systems whose declarations are different. - If the declaration is omitted, everything works fine. */ -#endif /* USG or STDC_HEADERS or __GNU_LIBRARY__ */ +#include "getopt.h" /* For communication from `getopt' to the caller. When `getopt' finds an option that takes an argument, @@ -87,7 +92,7 @@ char *alloca (); Also, when `ordering' is RETURN_IN_ORDER, each non-option ARGV-element is returned here. */ -char *optarg = 0; +char *optarg = NULL; /* Index in ARGV of the next element to be scanned. This is used for communication to and from the caller @@ -101,6 +106,7 @@ char *optarg = 0; Otherwise, `optind' communicates from one call to the next how much of ARGV has been scanned so far. */ +/* XXX 1003.2 says this must be 1 before any call. */ int optind = 0; /* The next char to be scanned in the option-element @@ -117,17 +123,23 @@ static char *nextchar; int opterr = 1; +/* Set to an option character which was unrecognized. + This must be initialized on some systems to avoid linking in the + system's own getopt implementation. */ + +int optopt = '?'; + /* Describe how to deal with options that follow non-option ARGV-elements. If the caller did not specify anything, the default is REQUIRE_ORDER if the environment variable - _POSIX_OPTION_ORDER is defined, PERMUTE otherwise. + POSIXLY_CORRECT is defined, PERMUTE otherwise. REQUIRE_ORDER means don't recognize them as options; stop option processing when the first non-option is seen. This is what Unix does. This mode of operation is selected by either setting the environment - variable POSIX_ME_HARDER, or using `+' as the first character + variable POSIXLY_CORRECT, or using `+' as the first character of the list of option characters. PERMUTE is the default. We permute the contents of ARGV as we scan, @@ -151,28 +163,50 @@ static enum REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering; -/* Describe the long-named options requested by the application. - _GETOPT_LONG_OPTIONS is a vector of `struct option' terminated by an - element containing a name which is zero. - The field `has_arg' is 1 if the option takes an argument, - 2 if it takes an optional argument. */ - -struct option -{ - char *name; - int has_arg; - int *flag; - int val; -}; +/* Value of POSIXLY_CORRECT environment variable. */ +static char *posixly_correct; + +#ifdef __GNU_LIBRARY__ +/* We want to avoid inclusion of string.h with non-GNU libraries + because there are many ways it can cause trouble. + On some systems, it contains special magic macros that don't work + in GCC. */ +#include <string.h> +#define my_index strchr +#else -const struct option *_getopt_long_options; +/* Avoid depending on library functions or files + whose names are inconsistent. */ -int _getopt_long_only = 0; +char *getenv (); -/* Index in _GETOPT_LONG_OPTIONS of the long-named option actually found. - Only valid when a long-named option was found. */ +static char * +my_index (str, chr) + const char *str; + int chr; +{ + while (*str) + { + if (*str == chr) + return (char *) str; + str++; + } + return 0; +} -int option_index; +/* If using GCC, we can safely declare strlen this way. + If not using GCC, it is ok not to declare it. */ +#ifdef __GNUC__ +/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. + That was relevant to code that was here before. */ +#ifndef __STDC__ +/* gcc with -traditional declares the built-in strlen to return int, + and has done so at least since version 2.4.5. -- rms. */ +extern int strlen (const char *); +#endif /* not __STDC__ */ +#endif /* __GNUC__ */ + +#endif /* not __GNU_LIBRARY__ */ /* Handle permutation of arguments. */ @@ -185,32 +219,104 @@ static int last_nonopt; /* Exchange two adjacent subsequences of ARGV. One subsequence is elements [first_nonopt,last_nonopt) - which contains all the non-options that have been skipped so far. + which contains all the non-options that have been skipped so far. The other is elements [last_nonopt,optind), which contains all - the options processed since those non-options were skipped. + the options processed since those non-options were skipped. `first_nonopt' and `last_nonopt' are relocated so that they describe - the new indices of the non-options in ARGV after they are moved. */ + the new indices of the non-options in ARGV after they are moved. */ static void exchange (argv) char **argv; { - int nonopts_size = (last_nonopt - first_nonopt) * sizeof (char *); - char **temp = (char **) alloca (nonopts_size); + int bottom = first_nonopt; + int middle = last_nonopt; + int top = optind; + char *tem; - /* Interchange the two blocks of data in ARGV. */ + /* Exchange the shorter segment with the far end of the longer segment. + That puts the shorter segment into the right place. + It leaves the longer segment in the right place overall, + but it consists of two parts that need to be swapped next. */ + + while (top > middle && middle > bottom) + { + if (top - middle > middle - bottom) + { + /* Bottom segment is the short one. */ + int len = middle - bottom; + register int i; + + /* Swap it with the top part of the top segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[top - (middle - bottom) + i]; + argv[top - (middle - bottom) + i] = tem; + } + /* Exclude the moved bottom segment from further swapping. */ + top -= len; + } + else + { + /* Top segment is the short one. */ + int len = top - middle; + register int i; - bcopy (&argv[first_nonopt], temp, nonopts_size); - bcopy (&argv[last_nonopt], &argv[first_nonopt], - (optind - last_nonopt) * sizeof (char *)); - bcopy (temp, &argv[first_nonopt + optind - last_nonopt], nonopts_size); + /* Swap it with the bottom part of the bottom segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[middle + i]; + argv[middle + i] = tem; + } + /* Exclude the moved top segment from further swapping. */ + bottom += len; + } + } /* Update records for the slots the non-options now occupy. */ first_nonopt += (optind - last_nonopt); last_nonopt = optind; } + +/* Initialize the internal data when the first call is made. */ + +static const char * +_getopt_initialize (optstring) + const char *optstring; +{ + /* Start processing options with ARGV-element 1 (since ARGV-element 0 + is the program name); the sequence of previously skipped + non-option ARGV-elements is empty. */ + + first_nonopt = last_nonopt = optind = 1; + + nextchar = NULL; + + posixly_correct = getenv ("POSIXLY_CORRECT"); + + /* Determine how to handle the ordering of options and nonoptions. */ + + if (optstring[0] == '-') + { + ordering = RETURN_IN_ORDER; + ++optstring; + } + else if (optstring[0] == '+') + { + ordering = REQUIRE_ORDER; + ++optstring; + } + else if (posixly_correct != NULL) + ordering = REQUIRE_ORDER; + else + ordering = PERMUTE; + + return optstring; +} /* Scan elements of ARGV (whose length is ARGC) for option characters given in OPTSTRING. @@ -245,78 +351,67 @@ exchange (argv) handling the non-option ARGV-elements. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. - Long-named options begin with `+' instead of `-'. + Long-named options begin with `--' instead of `-'. Their names may be abbreviated as long as the abbreviation is unique or is an exact match for some defined option. If they have an argument, it follows the option name in the same ARGV-element, separated from the option name by a `=', or else the in next ARGV-element. When `getopt' finds a long-named option, it returns 0 if that option's `flag' field is nonzero, the value of the option's `val' field - otherwise. */ + if the `flag' field is zero. + + The elements of ARGV aren't really const, because we permute them. + But we pretend they're const in the prototype to be compatible + with other systems. + + LONGOPTS is a vector of `struct option' terminated by an + element containing a name which is zero. + + LONGIND returns the index in LONGOPT of the long-named option found. + It is only valid when a long-named option has been found by the most + recent call. + + If LONG_ONLY is nonzero, '-' as well as '--' can introduce + long-named options. */ int -gnu_getopt (argc, argv, optstring) +_getopt_internal (argc, argv, optstring, longopts, longind, long_only) int argc; - char **argv; + char *const *argv; const char *optstring; + const struct option *longopts; + int *longind; + int long_only; { - optarg = 0; - - /* Initialize the internal data when the first call is made. - Start processing options with ARGV-element 1 (since ARGV-element 0 - is the program name); the sequence of previously skipped - non-option ARGV-elements is empty. */ + optarg = NULL; if (optind == 0) - { - first_nonopt = last_nonopt = optind = 1; + optstring = _getopt_initialize (optstring); - nextchar = 0; - - /* Determine how to handle the ordering of options and nonoptions. */ - - if (optstring[0] == '-') - { - ordering = RETURN_IN_ORDER; - ++optstring; - } - else if (optstring[0] == '+') - { - ordering = REQUIRE_ORDER; - ++optstring; - } - else if (getenv ("POSIX_ME_HARDER") != 0) - ordering = REQUIRE_ORDER; - else - ordering = PERMUTE; - } - - if (nextchar == 0 || *nextchar == 0) + if (nextchar == NULL || *nextchar == '\0') { + /* Advance to the next ARGV-element. */ + if (ordering == PERMUTE) { /* If we have just processed some options following some non-options, exchange them so that the options come first. */ if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange (argv); + exchange ((char **) argv); else if (last_nonopt != optind) first_nonopt = optind; - /* Now skip any additional non-options + /* Skip any additional non-options and extend the range of non-options previously skipped. */ while (optind < argc - && (argv[optind][0] != '-' - || argv[optind][1] == 0) - && (_getopt_long_options == 0 - || argv[optind][0] != '+' - || argv[optind][1] == 0)) + && (argv[optind][0] != '-' || argv[optind][1] == '\0')) optind++; last_nonopt = optind; } - /* Special ARGV-element `--' means premature end of options. + /* The special ARGV-element `--' means premature end of options. Skip it like a null option, then exchange with previous non-options as if it were an option, then skip everything else like a non-option. */ @@ -326,7 +421,7 @@ gnu_getopt (argc, argv, optstring) optind++; if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange (argv); + exchange ((char **) argv); else if (first_nonopt == last_nonopt) first_nonopt = optind; last_nonopt = argc; @@ -349,9 +444,7 @@ gnu_getopt (argc, argv, optstring) /* If we have come to a non-option and did not permute it, either stop the scan or describe it to the caller and pass it by. */ - if ((argv[optind][0] != '-' || argv[optind][1] == 0) - && (_getopt_long_options == 0 - || argv[optind][0] != '+' || argv[optind][1] == 0)) + if ((argv[optind][0] != '-' || argv[optind][1] == '\0')) { if (ordering == REQUIRE_ORDER) return EOF; @@ -360,32 +453,48 @@ gnu_getopt (argc, argv, optstring) } /* We have found another option-ARGV-element. - Start decoding its characters. */ + Skip the initial punctuation. */ - nextchar = argv[optind] + 1; + nextchar = (argv[optind] + 1 + + (longopts != NULL && argv[optind][1] == '-')); } - if (_getopt_long_options != 0 - && (argv[optind][0] == '+' - || (_getopt_long_only && argv[optind][0] == '-')) - ) + /* Decode the current option-ARGV-element. */ + + /* Check whether the ARGV-element is a long option. + + If long_only and the ARGV-element has the form "-f", where f is + a valid short option, don't consider it an abbreviated form of + a long option that starts with f. Otherwise there would be no + way to give the -f short option. + + On the other hand, if there's a long option "fubar" and + the ARGV-element is "-fu", do consider that an abbreviation of + the long option, just like "--fu", and not "-f" with arg "u". + + This distinction seems to be the most useful approach. */ + + if (longopts != NULL + && (argv[optind][1] == '-' + || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) { + char *nameend; const struct option *p; - char *s = nextchar; + const struct option *pfound = NULL; int exact = 0; int ambig = 0; - const struct option *pfound = 0; - int indfound = 0; + int indfound; + int option_index; - while (*s && *s != '=') - s++; + for (nameend = nextchar; *nameend && *nameend != '='; nameend++) + /* Do nothing. */ ; - /* Test all options for either exact match or abbreviated matches. */ - for (p = _getopt_long_options, option_index = 0; p->name; - p++, option_index++) - if (!strncmp (p->name, nextchar, s - nextchar)) + /* Test all long options for either exact match + or abbreviated matches. */ + for (p = longopts, option_index = 0; p->name; p++, option_index++) + if (!strncmp (p->name, nextchar, nameend - nextchar)) { - if (s - nextchar == strlen (p->name)) + if (nameend - nextchar == strlen (p->name)) { /* Exact match found. */ pfound = p; @@ -393,39 +502,52 @@ gnu_getopt (argc, argv, optstring) exact = 1; break; } - else if (pfound == 0) + else if (pfound == NULL) { /* First nonexact match found. */ pfound = p; indfound = option_index; } else - /* Second nonexact match found. */ + /* Second or later nonexact match found. */ ambig = 1; } if (ambig && !exact) { - fprintf (stderr, "%s: option `%s' is ambiguous\n", - argv[0], argv[optind]); + if (opterr) + fprintf (stderr, "%s: option `%s' is ambiguous\n", + argv[0], argv[optind]); nextchar += strlen (nextchar); optind++; return '?'; } - if (pfound != 0) + if (pfound != NULL) { option_index = indfound; optind++; - if (*s) + if (*nameend) { - if (pfound->has_arg > 0) - optarg = s + 1; + /* Don't test has_arg with >, because some C compilers don't + allow it to be used on enums. */ + if (pfound->has_arg) + optarg = nameend + 1; else { - fprintf (stderr, - "%s: option `%c%s' doesn't allow an argument\n", - argv[0], argv[optind - 1][0], pfound->name); + if (opterr) + { + if (argv[optind - 1][1] == '-') + /* --option */ + fprintf (stderr, + "%s: option `--%s' doesn't allow an argument\n", + argv[0], pfound->name); + else + /* +option or -option */ + fprintf (stderr, + "%s: option `%c%s' doesn't allow an argument\n", + argv[0], argv[optind - 1][0], pfound->name); + } nextchar += strlen (nextchar); return '?'; } @@ -436,13 +558,16 @@ gnu_getopt (argc, argv, optstring) optarg = argv[optind++]; else { - fprintf (stderr, "%s: option `%s' requires an argument\n", - argv[0], argv[optind - 1]); + if (opterr) + fprintf (stderr, "%s: option `%s' requires an argument\n", + argv[0], argv[optind - 1]); nextchar += strlen (nextchar); - return '?'; + return optstring[0] == ':' ? ':' : '?'; } } nextchar += strlen (nextchar); + if (longind != NULL) + *longind = option_index; if (pfound->flag) { *(pfound->flag) = pfound->val; @@ -450,43 +575,52 @@ gnu_getopt (argc, argv, optstring) } return pfound->val; } - /* Can't find it as a long option. If this is getopt_long_only, - and the option starts with '-' and is a valid short - option, then interpret it as a short option. Otherwise it's - an error. */ - if (_getopt_long_only == 0 || argv[optind][0] == '+' || - index (optstring, *nextchar) == 0) + + /* Can't find it as a long option. If this is not getopt_long_only, + or the option starts with '--' or is not a valid short + option, then it's an error. + Otherwise interpret it as a short option. */ + if (!long_only || argv[optind][1] == '-' + || my_index (optstring, *nextchar) == NULL) { - if (opterr != 0) - fprintf (stderr, "%s: unrecognized option `%c%s'\n", - argv[0], argv[optind][0], nextchar); - nextchar += strlen (nextchar); + if (opterr) + { + if (argv[optind][1] == '-') + /* --option */ + fprintf (stderr, "%s: unrecognized option `--%s'\n", + argv[0], nextchar); + else + /* +option or -option */ + fprintf (stderr, "%s: unrecognized option `%c%s'\n", + argv[0], argv[optind][0], nextchar); + } + nextchar = (char *) ""; optind++; return '?'; } } - /* Look at and handle the next option-character. */ + /* Look at and handle the next short option-character. */ { char c = *nextchar++; - char *temp = index (optstring, c); + char *temp = my_index (optstring, c); /* Increment `optind' when we start to process its last character. */ - if (*nextchar == 0) - optind++; + if (*nextchar == '\0') + ++optind; - if (temp == 0 || c == ':') + if (temp == NULL || c == ':') { - if (opterr != 0) + if (opterr) { - if (c < 040 || c >= 0177) - fprintf (stderr, "%s: unrecognized option, character code 0%o\n", - argv[0], c); + if (posixly_correct) + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c); else - fprintf (stderr, "%s: unrecognized option `-%c'\n", - argv[0], c); + fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c); } + optopt = c; return '?'; } if (temp[1] == ':') @@ -494,19 +628,19 @@ gnu_getopt (argc, argv, optstring) if (temp[2] == ':') { /* This is an option that accepts an argument optionally. */ - if (*nextchar != 0) + if (*nextchar != '\0') { optarg = nextchar; optind++; } else - optarg = 0; - nextchar = 0; + optarg = NULL; + nextchar = NULL; } else { /* This is an option that requires an argument. */ - if (*nextchar != 0) + if (*nextchar != '\0') { optarg = nextchar; /* If we end this ARGV-element by taking the rest as an arg, @@ -515,21 +649,42 @@ gnu_getopt (argc, argv, optstring) } else if (optind == argc) { - if (opterr != 0) - fprintf (stderr, "%s: option `-%c' requires an argument\n", - argv[0], c); - c = '?'; + if (opterr) + { + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, "%s: option requires an argument -- %c\n", + argv[0], c); + } + optopt = c; + if (optstring[0] == ':') + c = ':'; + else + c = '?'; } else /* We already incremented `optind' once; increment it again when taking next ARGV-elt as argument. */ optarg = argv[optind++]; - nextchar = 0; + nextchar = NULL; } } return c; } } + +int +getopt (argc, argv, optstring) + int argc; + char *const *argv; + const char *optstring; +{ + return _getopt_internal (argc, argv, optstring, + (const struct option *) 0, + (int *) 0, + 0); +} + +#endif /* _LIBC or not __GNU_LIBRARY__. */ #ifdef TEST @@ -548,7 +703,7 @@ main (argc, argv) { int this_option_optind = optind ? optind : 1; - c = gnu_getopt (argc, argv, "abc:d:0123456789"); + c = getopt (argc, argv, "abc:d:0123456789"); if (c == EOF) break; |