Home | History | Annotate | Line # | Download | only in dist
      1  1.1  tsutsui /* Getopt for GNU.
      2  1.1  tsutsui    NOTE: getopt is now part of the C library, so if you don't know what
      3  1.1  tsutsui    "Keep this file name-space clean" means, talk to roland (at) gnu.ai.mit.edu
      4  1.1  tsutsui    before changing it!
      5  1.1  tsutsui 
      6  1.1  tsutsui    Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95
      7  1.1  tsutsui    	Free Software Foundation, Inc.
      8  1.1  tsutsui 
      9  1.1  tsutsui This file is part of the libiberty library.  This library is free
     10  1.1  tsutsui software; you can redistribute it and/or modify it under the
     11  1.1  tsutsui terms of the GNU General Public License as published by the
     12  1.1  tsutsui Free Software Foundation; either version 2, or (at your option)
     13  1.1  tsutsui any later version.
     14  1.1  tsutsui 
     15  1.1  tsutsui This library is distributed in the hope that it will be useful,
     16  1.1  tsutsui but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  1.1  tsutsui MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  1.1  tsutsui GNU General Public License for more details.
     19  1.1  tsutsui 
     20  1.1  tsutsui You should have received a copy of the GNU General Public License
     21  1.1  tsutsui along with GNU CC; see the file COPYING.  If not, write to
     22  1.1  tsutsui the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     23  1.1  tsutsui 
     24  1.1  tsutsui As a special exception, if you link this library with files
     25  1.1  tsutsui compiled with a GNU compiler to produce an executable, this does not cause
     26  1.1  tsutsui the resulting executable to be covered by the GNU General Public License.
     27  1.1  tsutsui This exception does not however invalidate any other reasons why
     28  1.1  tsutsui the executable file might be covered by the GNU General Public License. */
     29  1.1  tsutsui 
     30  1.1  tsutsui /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
     32  1.1  tsutsui    Ditto for AIX 3.2 and <stdlib.h>.  */
     33  1.1  tsutsui #ifndef _NO_PROTO
     34  1.1  tsutsui #define _NO_PROTO
     35  1.1  tsutsui #endif
     36  1.1  tsutsui 
     37  1.1  tsutsui #ifdef HAVE_CONFIG_H
     38  1.1  tsutsui #if defined (emacs) || defined (CONFIG_BROKETS)
     39  1.1  tsutsui /* We use <config.h> instead of "config.h" so that a compilation
     40  1.1  tsutsui    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
     41  1.1  tsutsui    (which it would do because it found this file in $srcdir).  */
     42  1.1  tsutsui #include <config.h>
     43  1.1  tsutsui #else
     44  1.1  tsutsui #include "config.h"
     45  1.1  tsutsui #endif
     46  1.1  tsutsui #endif
     47  1.1  tsutsui 
     48  1.1  tsutsui #ifndef __STDC__
     49  1.1  tsutsui /* This is a separate conditional since some stdc systems
     50  1.1  tsutsui    reject `defined (const)'.  */
     51  1.1  tsutsui #ifndef const
     52  1.1  tsutsui #define const
     53  1.1  tsutsui #endif
     54  1.1  tsutsui #endif
     55  1.1  tsutsui 
     56  1.1  tsutsui #include <stdio.h>
     57  1.1  tsutsui 
     58  1.1  tsutsui /* Comment out all this code if we are using the GNU C Library, and are not
     59  1.1  tsutsui    actually compiling the library itself.  This code is part of the GNU C
     60  1.1  tsutsui    Library, but also included in many other GNU distributions.  Compiling
     61  1.1  tsutsui    and linking in this code is a waste when using the GNU C library
     62  1.1  tsutsui    (especially if it is a shared library).  Rather than having every GNU
     63  1.1  tsutsui    program understand `configure --with-gnu-libc' and omit the object files,
     64  1.1  tsutsui    it is simpler to just do this in the source for each such file.  */
     65  1.1  tsutsui /* Many versions of the Linux C library include older, broken versions
     66  1.1  tsutsui    of these routines, which will break the linker's command-line
     67  1.1  tsutsui    parsing.  */
     68  1.1  tsutsui 
     69  1.1  tsutsui #if defined (_LIBC) || !defined (__GNU_LIBRARY__) || defined (__linux__)
     70  1.1  tsutsui 
     71  1.1  tsutsui 
     72  1.1  tsutsui /* This needs to come after some library #include
     73  1.2  tsutsui    to get __GNU_LIBRARY__ defined.  */
     74  1.1  tsutsui #if defined(HAVE_STDLIB_H)
     75  1.1  tsutsui /* Don't include stdlib.h for non-GNU C libraries because some of them
     76  1.1  tsutsui    contain conflicting prototypes for getopt.  */
     77  1.1  tsutsui #include <stdlib.h>
     78  1.1  tsutsui #endif	/* GNU C library.  */
     79  1.1  tsutsui 
     80  1.1  tsutsui /* This version of `getopt' appears to the caller like standard Unix `getopt'
     81  1.1  tsutsui    but it behaves differently for the user, since it allows the user
     82  1.1  tsutsui    to intersperse the options with the other arguments.
     83  1.1  tsutsui 
     84  1.1  tsutsui    As `getopt' works, it permutes the elements of ARGV so that,
     85  1.1  tsutsui    when it is done, all the options precede everything else.  Thus
     86  1.1  tsutsui    all application programs are extended to handle flexible argument order.
     87  1.1  tsutsui 
     88  1.1  tsutsui    Setting the environment variable POSIXLY_CORRECT disables permutation.
     89  1.1  tsutsui    Then the behavior is completely standard.
     90  1.1  tsutsui 
     91  1.1  tsutsui    GNU application programs can use a third alternative mode in which
     92  1.1  tsutsui    they can distinguish the relative order of options and other arguments.  */
     93  1.1  tsutsui 
     94  1.1  tsutsui #include "getopt.h"
     95  1.1  tsutsui 
     96  1.1  tsutsui /* For communication from `getopt' to the caller.
     97  1.1  tsutsui    When `getopt' finds an option that takes an argument,
     98  1.1  tsutsui    the argument value is returned here.
     99  1.1  tsutsui    Also, when `ordering' is RETURN_IN_ORDER,
    100  1.1  tsutsui    each non-option ARGV-element is returned here.  */
    101  1.1  tsutsui 
    102  1.1  tsutsui char *optarg = NULL;
    103  1.1  tsutsui 
    104  1.1  tsutsui /* Index in ARGV of the next element to be scanned.
    105  1.1  tsutsui    This is used for communication to and from the caller
    106  1.1  tsutsui    and for communication between successive calls to `getopt'.
    107  1.1  tsutsui 
    108  1.1  tsutsui    On entry to `getopt', zero means this is the first call; initialize.
    109  1.1  tsutsui 
    110  1.1  tsutsui    When `getopt' returns EOF, this is the index of the first of the
    111  1.1  tsutsui    non-option elements that the caller should itself scan.
    112  1.1  tsutsui 
    113  1.1  tsutsui    Otherwise, `optind' communicates from one call to the next
    114  1.1  tsutsui    how much of ARGV has been scanned so far.  */
    115  1.1  tsutsui 
    116  1.1  tsutsui /* XXX 1003.2 says this must be 1 before any call.  */
    117  1.1  tsutsui int optind = 0;
    118  1.1  tsutsui 
    119  1.1  tsutsui /* The next char to be scanned in the option-element
    120  1.1  tsutsui    in which the last option character we returned was found.
    121  1.1  tsutsui    This allows us to pick up the scan where we left off.
    122  1.1  tsutsui 
    123  1.1  tsutsui    If this is zero, or a null string, it means resume the scan
    124  1.1  tsutsui    by advancing to the next ARGV-element.  */
    125  1.1  tsutsui 
    126  1.1  tsutsui static char *nextchar;
    127  1.1  tsutsui 
    128  1.1  tsutsui /* Callers store zero here to inhibit the error message
    129  1.1  tsutsui    for unrecognized options.  */
    130  1.1  tsutsui 
    131  1.1  tsutsui int opterr = 1;
    132  1.1  tsutsui 
    133  1.1  tsutsui /* Set to an option character which was unrecognized.
    134  1.1  tsutsui    This must be initialized on some systems to avoid linking in the
    135  1.1  tsutsui    system's own getopt implementation.  */
    136  1.1  tsutsui 
    137  1.1  tsutsui int optopt = '?';
    138  1.1  tsutsui 
    139  1.1  tsutsui /* Describe how to deal with options that follow non-option ARGV-elements.
    140  1.1  tsutsui 
    141  1.1  tsutsui    If the caller did not specify anything,
    142  1.1  tsutsui    the default is REQUIRE_ORDER if the environment variable
    143  1.1  tsutsui    POSIXLY_CORRECT is defined, PERMUTE otherwise.
    144  1.1  tsutsui 
    145  1.1  tsutsui    REQUIRE_ORDER means don't recognize them as options;
    146  1.1  tsutsui    stop option processing when the first non-option is seen.
    147  1.1  tsutsui    This is what Unix does.
    148  1.1  tsutsui    This mode of operation is selected by either setting the environment
    149  1.1  tsutsui    variable POSIXLY_CORRECT, or using `+' as the first character
    150  1.1  tsutsui    of the list of option characters.
    151  1.1  tsutsui 
    152  1.1  tsutsui    PERMUTE is the default.  We permute the contents of ARGV as we scan,
    153  1.1  tsutsui    so that eventually all the non-options are at the end.  This allows options
    154  1.1  tsutsui    to be given in any order, even with programs that were not written to
    155  1.1  tsutsui    expect this.
    156  1.1  tsutsui 
    157  1.1  tsutsui    RETURN_IN_ORDER is an option available to programs that were written
    158  1.1  tsutsui    to expect options and other ARGV-elements in any order and that care about
    159  1.1  tsutsui    the ordering of the two.  We describe each non-option ARGV-element
    160  1.1  tsutsui    as if it were the argument of an option with character code 1.
    161  1.1  tsutsui    Using `-' as the first character of the list of option characters
    162  1.1  tsutsui    selects this mode of operation.
    163  1.1  tsutsui 
    164  1.1  tsutsui    The special argument `--' forces an end of option-scanning regardless
    165  1.1  tsutsui    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
    166  1.1  tsutsui    `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
    167  1.1  tsutsui 
    168  1.1  tsutsui static enum
    169  1.1  tsutsui {
    170  1.1  tsutsui   REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
    171  1.1  tsutsui } ordering;
    172  1.2  tsutsui 
    173  1.1  tsutsui #if defined(HAVE_STRING_H)
    175  1.1  tsutsui /* We want to avoid inclusion of string.h with non-GNU libraries
    176  1.1  tsutsui    because there are many ways it can cause trouble.
    177  1.1  tsutsui    On some systems, it contains special magic macros that don't work
    178  1.1  tsutsui    in GCC.  */
    179  1.1  tsutsui #include <string.h>
    180  1.1  tsutsui #define	my_index	strchr
    181  1.1  tsutsui #else
    182  1.1  tsutsui 
    183  1.1  tsutsui /* Avoid depending on library functions or files
    184  1.1  tsutsui    whose names are inconsistent.  */
    185  1.1  tsutsui 
    186  1.1  tsutsui char *getenv ();
    187  1.1  tsutsui 
    188  1.1  tsutsui static char *
    189  1.1  tsutsui my_index (str, chr)
    190  1.1  tsutsui      const char *str;
    191  1.1  tsutsui      int chr;
    192  1.1  tsutsui {
    193  1.1  tsutsui   while (*str)
    194  1.1  tsutsui     {
    195  1.1  tsutsui       if (*str == chr)
    196  1.1  tsutsui 	return (char *) str;
    197  1.1  tsutsui       str++;
    198  1.1  tsutsui     }
    199  1.1  tsutsui   return 0;
    200  1.1  tsutsui }
    201  1.1  tsutsui 
    202  1.1  tsutsui /* If using GCC, we can safely declare strlen this way.
    203  1.1  tsutsui    If not using GCC, it is ok not to declare it.  */
    204  1.1  tsutsui #ifdef __GNUC__
    205  1.1  tsutsui /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
    206  1.1  tsutsui    That was relevant to code that was here before.  */
    207  1.1  tsutsui #ifndef __STDC__
    208  1.1  tsutsui /* gcc with -traditional declares the built-in strlen to return int,
    209  1.1  tsutsui    and has done so at least since version 2.4.5. -- rms.  */
    210  1.1  tsutsui extern int strlen (const char *);
    211  1.1  tsutsui #endif /* not __STDC__ */
    212  1.1  tsutsui #endif /* __GNUC__ */
    213  1.1  tsutsui 
    214  1.1  tsutsui #endif /* not __GNU_LIBRARY__ */
    215  1.1  tsutsui 
    216  1.1  tsutsui /* Handle permutation of arguments.  */
    218  1.1  tsutsui 
    219  1.1  tsutsui /* Describe the part of ARGV that contains non-options that have
    220  1.1  tsutsui    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
    221  1.1  tsutsui    `last_nonopt' is the index after the last of them.  */
    222  1.1  tsutsui 
    223  1.1  tsutsui static int first_nonopt;
    224  1.1  tsutsui static int last_nonopt;
    225  1.1  tsutsui 
    226  1.1  tsutsui /* Exchange two adjacent subsequences of ARGV.
    227  1.1  tsutsui    One subsequence is elements [first_nonopt,last_nonopt)
    228  1.1  tsutsui    which contains all the non-options that have been skipped so far.
    229  1.1  tsutsui    The other is elements [last_nonopt,optind), which contains all
    230  1.1  tsutsui    the options processed since those non-options were skipped.
    231  1.1  tsutsui 
    232  1.1  tsutsui    `first_nonopt' and `last_nonopt' are relocated so that they describe
    233  1.3  tsutsui    the new indices of the non-options in ARGV after they are moved.  */
    234  1.1  tsutsui 
    235  1.1  tsutsui static void
    236  1.1  tsutsui exchange (char **argv)
    237  1.1  tsutsui {
    238  1.1  tsutsui   int bottom = first_nonopt;
    239  1.1  tsutsui   int middle = last_nonopt;
    240  1.1  tsutsui   int top = optind;
    241  1.1  tsutsui   char *tem;
    242  1.1  tsutsui 
    243  1.1  tsutsui   /* Exchange the shorter segment with the far end of the longer segment.
    244  1.1  tsutsui      That puts the shorter segment into the right place.
    245  1.1  tsutsui      It leaves the longer segment in the right place overall,
    246  1.1  tsutsui      but it consists of two parts that need to be swapped next.  */
    247  1.1  tsutsui 
    248  1.1  tsutsui   while (top > middle && middle > bottom)
    249  1.1  tsutsui     {
    250  1.1  tsutsui       if (top - middle > middle - bottom)
    251  1.1  tsutsui 	{
    252  1.1  tsutsui 	  /* Bottom segment is the short one.  */
    253  1.1  tsutsui 	  int len = middle - bottom;
    254  1.1  tsutsui 	  register int i;
    255  1.1  tsutsui 
    256  1.1  tsutsui 	  /* Swap it with the top part of the top segment.  */
    257  1.1  tsutsui 	  for (i = 0; i < len; i++)
    258  1.1  tsutsui 	    {
    259  1.1  tsutsui 	      tem = argv[bottom + i];
    260  1.1  tsutsui 	      argv[bottom + i] = argv[top - (middle - bottom) + i];
    261  1.1  tsutsui 	      argv[top - (middle - bottom) + i] = tem;
    262  1.1  tsutsui 	    }
    263  1.1  tsutsui 	  /* Exclude the moved bottom segment from further swapping.  */
    264  1.1  tsutsui 	  top -= len;
    265  1.1  tsutsui 	}
    266  1.1  tsutsui       else
    267  1.1  tsutsui 	{
    268  1.1  tsutsui 	  /* Top segment is the short one.  */
    269  1.1  tsutsui 	  int len = top - middle;
    270  1.1  tsutsui 	  register int i;
    271  1.1  tsutsui 
    272  1.1  tsutsui 	  /* Swap it with the bottom part of the bottom segment.  */
    273  1.1  tsutsui 	  for (i = 0; i < len; i++)
    274  1.1  tsutsui 	    {
    275  1.1  tsutsui 	      tem = argv[bottom + i];
    276  1.1  tsutsui 	      argv[bottom + i] = argv[middle + i];
    277  1.1  tsutsui 	      argv[middle + i] = tem;
    278  1.1  tsutsui 	    }
    279  1.1  tsutsui 	  /* Exclude the moved top segment from further swapping.  */
    280  1.1  tsutsui 	  bottom += len;
    281  1.1  tsutsui 	}
    282  1.1  tsutsui     }
    283  1.1  tsutsui 
    284  1.1  tsutsui   /* Update records for the slots the non-options now occupy.  */
    285  1.1  tsutsui 
    286  1.1  tsutsui   first_nonopt += (optind - last_nonopt);
    287  1.1  tsutsui   last_nonopt = optind;
    288  1.1  tsutsui }
    289  1.1  tsutsui 
    290  1.3  tsutsui /* Initialize the internal data when the first call is made.  */
    291  1.1  tsutsui 
    292  1.1  tsutsui static const char *
    293  1.1  tsutsui _getopt_initialize (const char *optstring)
    294  1.1  tsutsui {
    295  1.1  tsutsui   /* Start processing options with ARGV-element 1 (since ARGV-element 0
    296  1.1  tsutsui      is the program name); the sequence of previously skipped
    297  1.1  tsutsui      non-option ARGV-elements is empty.  */
    298  1.1  tsutsui 
    299  1.1  tsutsui   first_nonopt = last_nonopt = optind = 1;
    300  1.1  tsutsui 
    301  1.1  tsutsui   nextchar = NULL;
    302  1.1  tsutsui 
    303  1.1  tsutsui   /* Determine how to handle the ordering of options and nonoptions.  */
    304  1.1  tsutsui 
    305  1.1  tsutsui   if (optstring[0] == '-')
    306  1.1  tsutsui     {
    307  1.1  tsutsui       ordering = RETURN_IN_ORDER;
    308  1.1  tsutsui       ++optstring;
    309  1.1  tsutsui     }
    310  1.1  tsutsui   else if (optstring[0] == '+')
    311  1.1  tsutsui     {
    312  1.1  tsutsui       ordering = REQUIRE_ORDER;
    313  1.1  tsutsui       ++optstring;
    314  1.1  tsutsui     }
    315  1.1  tsutsui   else if (getenv ("POSIXLY_CORRECT") != NULL)
    316  1.1  tsutsui     ordering = REQUIRE_ORDER;
    317  1.1  tsutsui   else
    318  1.1  tsutsui     ordering = PERMUTE;
    319  1.1  tsutsui 
    320  1.1  tsutsui   return optstring;
    321  1.1  tsutsui }
    322  1.1  tsutsui 
    323  1.1  tsutsui /* Scan elements of ARGV (whose length is ARGC) for option characters
    325  1.1  tsutsui    given in OPTSTRING.
    326  1.1  tsutsui 
    327  1.1  tsutsui    If an element of ARGV starts with '-', and is not exactly "-" or "--",
    328  1.1  tsutsui    then it is an option element.  The characters of this element
    329  1.1  tsutsui    (aside from the initial '-') are option characters.  If `getopt'
    330  1.1  tsutsui    is called repeatedly, it returns successively each of the option characters
    331  1.1  tsutsui    from each of the option elements.
    332  1.1  tsutsui 
    333  1.1  tsutsui    If `getopt' finds another option character, it returns that character,
    334  1.1  tsutsui    updating `optind' and `nextchar' so that the next call to `getopt' can
    335  1.1  tsutsui    resume the scan with the following option character or ARGV-element.
    336  1.1  tsutsui 
    337  1.1  tsutsui    If there are no more option characters, `getopt' returns `EOF'.
    338  1.1  tsutsui    Then `optind' is the index in ARGV of the first ARGV-element
    339  1.1  tsutsui    that is not an option.  (The ARGV-elements have been permuted
    340  1.1  tsutsui    so that those that are not options now come last.)
    341  1.1  tsutsui 
    342  1.1  tsutsui    OPTSTRING is a string containing the legitimate option characters.
    343  1.1  tsutsui    If an option character is seen that is not listed in OPTSTRING,
    344  1.1  tsutsui    return '?' after printing an error message.  If you set `opterr' to
    345  1.1  tsutsui    zero, the error message is suppressed but we still return '?'.
    346  1.1  tsutsui 
    347  1.1  tsutsui    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
    348  1.1  tsutsui    so the following text in the same ARGV-element, or the text of the following
    349  1.1  tsutsui    ARGV-element, is returned in `optarg'.  Two colons mean an option that
    350  1.1  tsutsui    wants an optional arg; if there is text in the current ARGV-element,
    351  1.1  tsutsui    it is returned in `optarg', otherwise `optarg' is set to zero.
    352  1.1  tsutsui 
    353  1.1  tsutsui    If OPTSTRING starts with `-' or `+', it requests different methods of
    354  1.1  tsutsui    handling the non-option ARGV-elements.
    355  1.1  tsutsui    See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
    356  1.1  tsutsui 
    357  1.1  tsutsui    Long-named options begin with `--' instead of `-'.
    358  1.1  tsutsui    Their names may be abbreviated as long as the abbreviation is unique
    359  1.1  tsutsui    or is an exact match for some defined option.  If they have an
    360  1.1  tsutsui    argument, it follows the option name in the same ARGV-element, separated
    361  1.1  tsutsui    from the option name by a `=', or else the in next ARGV-element.
    362  1.1  tsutsui    When `getopt' finds a long-named option, it returns 0 if that option's
    363  1.1  tsutsui    `flag' field is nonzero, the value of the option's `val' field
    364  1.1  tsutsui    if the `flag' field is zero.
    365  1.1  tsutsui 
    366  1.1  tsutsui    The elements of ARGV aren't really const, because we permute them.
    367  1.1  tsutsui    But we pretend they're const in the prototype to be compatible
    368  1.1  tsutsui    with other systems.
    369  1.1  tsutsui 
    370  1.1  tsutsui    LONGOPTS is a vector of `struct option' terminated by an
    371  1.1  tsutsui    element containing a name which is zero.
    372  1.1  tsutsui 
    373  1.1  tsutsui    LONGIND returns the index in LONGOPT of the long-named option found.
    374  1.1  tsutsui    It is only valid when a long-named option has been found by the most
    375  1.1  tsutsui    recent call.
    376  1.1  tsutsui 
    377  1.1  tsutsui    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
    378  1.1  tsutsui    long-named options.  */
    379  1.1  tsutsui 
    380  1.1  tsutsui int
    381  1.1  tsutsui _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
    382  1.1  tsutsui      int argc;
    383  1.1  tsutsui      char *const *argv;
    384  1.1  tsutsui      const char *optstring;
    385  1.1  tsutsui      const struct option *longopts;
    386  1.1  tsutsui      int *longind;
    387  1.1  tsutsui      int long_only;
    388  1.1  tsutsui {
    389  1.1  tsutsui   optarg = NULL;
    390  1.1  tsutsui 
    391  1.1  tsutsui   if (optind == 0)
    392  1.1  tsutsui     optstring = _getopt_initialize (optstring);
    393  1.1  tsutsui 
    394  1.1  tsutsui   if (argc == 0)
    395  1.1  tsutsui     return EOF;
    396  1.1  tsutsui 
    397  1.1  tsutsui   if (nextchar == NULL || *nextchar == '\0')
    398  1.1  tsutsui     {
    399  1.1  tsutsui       /* Advance to the next ARGV-element.  */
    400  1.1  tsutsui 
    401  1.1  tsutsui       if (ordering == PERMUTE)
    402  1.1  tsutsui 	{
    403  1.1  tsutsui 	  /* If we have just processed some options following some non-options,
    404  1.1  tsutsui 	     exchange them so that the options come first.  */
    405  1.1  tsutsui 
    406  1.1  tsutsui 	  if (first_nonopt != last_nonopt && last_nonopt != optind)
    407  1.1  tsutsui 	    exchange ((char **) argv);
    408  1.1  tsutsui 	  else if (last_nonopt != optind)
    409  1.1  tsutsui 	    first_nonopt = optind;
    410  1.1  tsutsui 
    411  1.1  tsutsui 	  /* Skip any additional non-options
    412  1.1  tsutsui 	     and extend the range of non-options previously skipped.  */
    413  1.1  tsutsui 
    414  1.1  tsutsui 	  while (optind < argc
    415  1.1  tsutsui 		 && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
    416  1.1  tsutsui 	    optind++;
    417  1.1  tsutsui 	  last_nonopt = optind;
    418  1.1  tsutsui 	}
    419  1.1  tsutsui 
    420  1.1  tsutsui       /* The special ARGV-element `--' means premature end of options.
    421  1.1  tsutsui 	 Skip it like a null option,
    422  1.1  tsutsui 	 then exchange with previous non-options as if it were an option,
    423  1.1  tsutsui 	 then skip everything else like a non-option.  */
    424  1.1  tsutsui 
    425  1.1  tsutsui       if (optind != argc && !strcmp (argv[optind], "--"))
    426  1.1  tsutsui 	{
    427  1.1  tsutsui 	  optind++;
    428  1.1  tsutsui 
    429  1.1  tsutsui 	  if (first_nonopt != last_nonopt && last_nonopt != optind)
    430  1.1  tsutsui 	    exchange ((char **) argv);
    431  1.1  tsutsui 	  else if (first_nonopt == last_nonopt)
    432  1.1  tsutsui 	    first_nonopt = optind;
    433  1.1  tsutsui 	  last_nonopt = argc;
    434  1.1  tsutsui 
    435  1.1  tsutsui 	  optind = argc;
    436  1.1  tsutsui 	}
    437  1.1  tsutsui 
    438  1.1  tsutsui       /* If we have done all the ARGV-elements, stop the scan
    439  1.1  tsutsui 	 and back over any non-options that we skipped and permuted.  */
    440  1.1  tsutsui 
    441  1.1  tsutsui       if (optind == argc)
    442  1.1  tsutsui 	{
    443  1.1  tsutsui 	  /* Set the next-arg-index to point at the non-options
    444  1.1  tsutsui 	     that we previously skipped, so the caller will digest them.  */
    445  1.1  tsutsui 	  if (first_nonopt != last_nonopt)
    446  1.1  tsutsui 	    optind = first_nonopt;
    447  1.1  tsutsui 	  return EOF;
    448  1.1  tsutsui 	}
    449  1.1  tsutsui 
    450  1.1  tsutsui       /* If we have come to a non-option and did not permute it,
    451  1.1  tsutsui 	 either stop the scan or describe it to the caller and pass it by.  */
    452  1.1  tsutsui 
    453  1.1  tsutsui       if ((argv[optind][0] != '-' || argv[optind][1] == '\0'))
    454  1.1  tsutsui 	{
    455  1.1  tsutsui 	  if (ordering == REQUIRE_ORDER)
    456  1.1  tsutsui 	    return EOF;
    457  1.1  tsutsui 	  optarg = argv[optind++];
    458  1.1  tsutsui 	  return 1;
    459  1.1  tsutsui 	}
    460  1.1  tsutsui 
    461  1.1  tsutsui       /* We have found another option-ARGV-element.
    462  1.1  tsutsui 	 Skip the initial punctuation.  */
    463  1.1  tsutsui 
    464  1.1  tsutsui       nextchar = (argv[optind] + 1
    465  1.1  tsutsui 		  + (longopts != NULL && argv[optind][1] == '-'));
    466  1.1  tsutsui     }
    467  1.1  tsutsui 
    468  1.1  tsutsui   /* Decode the current option-ARGV-element.  */
    469  1.1  tsutsui 
    470  1.1  tsutsui   /* Check whether the ARGV-element is a long option.
    471  1.1  tsutsui 
    472  1.1  tsutsui      If long_only and the ARGV-element has the form "-f", where f is
    473  1.1  tsutsui      a valid short option, don't consider it an abbreviated form of
    474  1.1  tsutsui      a long option that starts with f.  Otherwise there would be no
    475  1.1  tsutsui      way to give the -f short option.
    476  1.1  tsutsui 
    477  1.1  tsutsui      On the other hand, if there's a long option "fubar" and
    478  1.1  tsutsui      the ARGV-element is "-fu", do consider that an abbreviation of
    479  1.1  tsutsui      the long option, just like "--fu", and not "-f" with arg "u".
    480  1.1  tsutsui 
    481  1.1  tsutsui      This distinction seems to be the most useful approach.  */
    482  1.1  tsutsui 
    483  1.1  tsutsui   if (longopts != NULL
    484  1.1  tsutsui       && (argv[optind][1] == '-'
    485  1.1  tsutsui 	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
    486  1.1  tsutsui     {
    487  1.1  tsutsui       char *nameend;
    488  1.1  tsutsui       const struct option *p;
    489  1.1  tsutsui       const struct option *pfound = NULL;
    490  1.1  tsutsui       int exact = 0;
    491  1.1  tsutsui       int ambig = 0;
    492  1.1  tsutsui       int indfound;
    493  1.1  tsutsui       int option_index;
    494  1.1  tsutsui 
    495  1.1  tsutsui       for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
    496  1.1  tsutsui 	/* Do nothing.  */ ;
    497  1.1  tsutsui 
    498  1.1  tsutsui       /* Test all long options for either exact match
    499  1.1  tsutsui 	 or abbreviated matches.  */
    500  1.1  tsutsui       for (p = longopts, option_index = 0; p->name; p++, option_index++)
    501  1.1  tsutsui 	if (!strncmp (p->name, nextchar, nameend - nextchar))
    502  1.1  tsutsui 	  {
    503  1.1  tsutsui 	    if (nameend - nextchar == strlen (p->name))
    504  1.1  tsutsui 	      {
    505  1.1  tsutsui 		/* Exact match found.  */
    506  1.1  tsutsui 		pfound = p;
    507  1.1  tsutsui 		indfound = option_index;
    508  1.1  tsutsui 		exact = 1;
    509  1.1  tsutsui 		break;
    510  1.1  tsutsui 	      }
    511  1.1  tsutsui 	    else if (pfound == NULL)
    512  1.1  tsutsui 	      {
    513  1.1  tsutsui 		/* First nonexact match found.  */
    514  1.1  tsutsui 		pfound = p;
    515  1.1  tsutsui 		indfound = option_index;
    516  1.1  tsutsui 	      }
    517  1.1  tsutsui 	    else
    518  1.1  tsutsui 	      /* Second or later nonexact match found.  */
    519  1.1  tsutsui 	      ambig = 1;
    520  1.1  tsutsui 	  }
    521  1.1  tsutsui 
    522  1.1  tsutsui       if (ambig && !exact)
    523  1.1  tsutsui 	{
    524  1.1  tsutsui 	  if (opterr)
    525  1.1  tsutsui 	    fprintf (stderr, "%s: option `%s' is ambiguous\n",
    526  1.1  tsutsui 		     argv[0], argv[optind]);
    527  1.1  tsutsui 	  nextchar += strlen (nextchar);
    528  1.1  tsutsui 	  optind++;
    529  1.1  tsutsui 	  return '?';
    530  1.1  tsutsui 	}
    531  1.1  tsutsui 
    532  1.1  tsutsui       if (pfound != NULL)
    533  1.1  tsutsui 	{
    534  1.1  tsutsui 	  option_index = indfound;
    535  1.1  tsutsui 	  optind++;
    536  1.1  tsutsui 	  if (*nameend)
    537  1.1  tsutsui 	    {
    538  1.1  tsutsui 	      /* Don't test has_arg with >, because some C compilers don't
    539  1.1  tsutsui 		 allow it to be used on enums.  */
    540  1.1  tsutsui 	      if (pfound->has_arg)
    541  1.1  tsutsui 		optarg = nameend + 1;
    542  1.1  tsutsui 	      else
    543  1.1  tsutsui 		{
    544  1.1  tsutsui 		  if (opterr)
    545  1.1  tsutsui 		    {
    546  1.1  tsutsui 		      if (argv[optind - 1][1] == '-')
    547  1.1  tsutsui 			/* --option */
    548  1.1  tsutsui 			fprintf (stderr,
    549  1.1  tsutsui 				 "%s: option `--%s' doesn't allow an argument\n",
    550  1.1  tsutsui 				 argv[0], pfound->name);
    551  1.1  tsutsui 		      else
    552  1.1  tsutsui 			/* +option or -option */
    553  1.1  tsutsui 			fprintf (stderr,
    554  1.1  tsutsui 			     "%s: option `%c%s' doesn't allow an argument\n",
    555  1.1  tsutsui 			     argv[0], argv[optind - 1][0], pfound->name);
    556  1.1  tsutsui 		    }
    557  1.1  tsutsui 		  nextchar += strlen (nextchar);
    558  1.1  tsutsui 		  return '?';
    559  1.1  tsutsui 		}
    560  1.1  tsutsui 	    }
    561  1.1  tsutsui 	  else if (pfound->has_arg == 1)
    562  1.1  tsutsui 	    {
    563  1.1  tsutsui 	      if (optind < argc)
    564  1.1  tsutsui 		optarg = argv[optind++];
    565  1.1  tsutsui 	      else
    566  1.1  tsutsui 		{
    567  1.1  tsutsui 		  if (opterr)
    568  1.1  tsutsui 		    fprintf (stderr, "%s: option `%s' requires an argument\n",
    569  1.1  tsutsui 			     argv[0], argv[optind - 1]);
    570  1.1  tsutsui 		  nextchar += strlen (nextchar);
    571  1.1  tsutsui 		  return optstring[0] == ':' ? ':' : '?';
    572  1.1  tsutsui 		}
    573  1.1  tsutsui 	    }
    574  1.1  tsutsui 	  nextchar += strlen (nextchar);
    575  1.1  tsutsui 	  if (longind != NULL)
    576  1.1  tsutsui 	    *longind = option_index;
    577  1.1  tsutsui 	  if (pfound->flag)
    578  1.1  tsutsui 	    {
    579  1.1  tsutsui 	      *(pfound->flag) = pfound->val;
    580  1.1  tsutsui 	      return 0;
    581  1.1  tsutsui 	    }
    582  1.1  tsutsui 	  return pfound->val;
    583  1.1  tsutsui 	}
    584  1.1  tsutsui 
    585  1.1  tsutsui       /* Can't find it as a long option.  If this is not getopt_long_only,
    586  1.1  tsutsui 	 or the option starts with '--' or is not a valid short
    587  1.1  tsutsui 	 option, then it's an error.
    588  1.1  tsutsui 	 Otherwise interpret it as a short option.  */
    589  1.1  tsutsui       if (!long_only || argv[optind][1] == '-'
    590  1.1  tsutsui 	  || my_index (optstring, *nextchar) == NULL)
    591  1.1  tsutsui 	{
    592  1.1  tsutsui 	  if (opterr)
    593  1.1  tsutsui 	    {
    594  1.1  tsutsui 	      if (argv[optind][1] == '-')
    595  1.1  tsutsui 		/* --option */
    596  1.1  tsutsui 		fprintf (stderr, "%s: unrecognized option `--%s'\n",
    597  1.1  tsutsui 			 argv[0], nextchar);
    598  1.1  tsutsui 	      else
    599  1.1  tsutsui 		/* +option or -option */
    600  1.1  tsutsui 		fprintf (stderr, "%s: unrecognized option `%c%s'\n",
    601  1.1  tsutsui 			 argv[0], argv[optind][0], nextchar);
    602  1.1  tsutsui 	    }
    603  1.1  tsutsui 	  nextchar = (char *) "";
    604  1.1  tsutsui 	  optind++;
    605  1.1  tsutsui 	  return '?';
    606  1.1  tsutsui 	}
    607  1.1  tsutsui     }
    608  1.1  tsutsui 
    609  1.1  tsutsui   /* Look at and handle the next short option-character.  */
    610  1.1  tsutsui 
    611  1.1  tsutsui   {
    612  1.1  tsutsui     char c = *nextchar++;
    613  1.1  tsutsui     char *temp = my_index (optstring, c);
    614  1.1  tsutsui 
    615  1.1  tsutsui     /* Increment `optind' when we start to process its last character.  */
    616  1.1  tsutsui     if (*nextchar == '\0')
    617  1.1  tsutsui       ++optind;
    618  1.1  tsutsui 
    619  1.1  tsutsui     if (temp == NULL || c == ':')
    620  1.1  tsutsui       {
    621  1.1  tsutsui 	if (opterr)
    622  1.1  tsutsui 	  {
    623  1.1  tsutsui 	    /* 1003.2 specifies the format of this message.  */
    624  1.1  tsutsui 	    fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
    625  1.1  tsutsui 	  }
    626  1.1  tsutsui 	optopt = c;
    627  1.1  tsutsui 	return '?';
    628  1.1  tsutsui       }
    629  1.1  tsutsui     if (temp[1] == ':')
    630  1.1  tsutsui       {
    631  1.1  tsutsui 	if (temp[2] == ':')
    632  1.1  tsutsui 	  {
    633  1.1  tsutsui 	    /* This is an option that accepts an argument optionally.  */
    634  1.1  tsutsui 	    if (*nextchar != '\0')
    635  1.1  tsutsui 	      {
    636  1.1  tsutsui 		optarg = nextchar;
    637  1.1  tsutsui 		optind++;
    638  1.1  tsutsui 	      }
    639  1.1  tsutsui 	    else
    640  1.1  tsutsui 	      optarg = NULL;
    641  1.1  tsutsui 	    nextchar = NULL;
    642  1.1  tsutsui 	  }
    643  1.1  tsutsui 	else
    644  1.1  tsutsui 	  {
    645  1.1  tsutsui 	    /* This is an option that requires an argument.  */
    646  1.1  tsutsui 	    if (*nextchar != '\0')
    647  1.1  tsutsui 	      {
    648  1.1  tsutsui 		optarg = nextchar;
    649  1.1  tsutsui 		/* If we end this ARGV-element by taking the rest as an arg,
    650  1.1  tsutsui 		   we must advance to the next element now.  */
    651  1.1  tsutsui 		optind++;
    652  1.1  tsutsui 	      }
    653  1.1  tsutsui 	    else if (optind == argc)
    654  1.1  tsutsui 	      {
    655  1.1  tsutsui 		if (opterr)
    656  1.1  tsutsui 		  {
    657  1.1  tsutsui 		    /* 1003.2 specifies the format of this message.  */
    658  1.1  tsutsui 		    fprintf (stderr, "%s: option requires an argument -- %c\n",
    659  1.1  tsutsui 			     argv[0], c);
    660  1.1  tsutsui 		  }
    661  1.1  tsutsui 		optopt = c;
    662  1.1  tsutsui 		if (optstring[0] == ':')
    663  1.1  tsutsui 		  c = ':';
    664  1.1  tsutsui 		else
    665  1.1  tsutsui 		  c = '?';
    666  1.1  tsutsui 	      }
    667  1.1  tsutsui 	    else
    668  1.1  tsutsui 	      /* We already incremented `optind' once;
    669  1.1  tsutsui 		 increment it again when taking next ARGV-elt as argument.  */
    670  1.1  tsutsui 	      optarg = argv[optind++];
    671  1.1  tsutsui 	    nextchar = NULL;
    672  1.1  tsutsui 	  }
    673  1.1  tsutsui       }
    674  1.1  tsutsui     return c;
    675  1.1  tsutsui   }
    676  1.1  tsutsui }
    677  1.1  tsutsui 
    678  1.1  tsutsui #endif	/* _LIBC or not __GNU_LIBRARY__.  */
    679  1.1  tsutsui 
    680  1.1  tsutsui #ifdef TEST
    682  1.1  tsutsui 
    683  1.1  tsutsui /* Compile with -DTEST to make an executable for use in testing
    684  1.1  tsutsui    the above definition of `getopt'.  */
    685  1.1  tsutsui 
    686  1.1  tsutsui int
    687  1.1  tsutsui main (argc, argv)
    688  1.1  tsutsui      int argc;
    689  1.1  tsutsui      char **argv;
    690  1.1  tsutsui {
    691  1.1  tsutsui   int c;
    692  1.1  tsutsui   int digit_optind = 0;
    693  1.1  tsutsui 
    694  1.1  tsutsui   while (1)
    695  1.1  tsutsui     {
    696  1.1  tsutsui       int this_option_optind = optind ? optind : 1;
    697  1.1  tsutsui 
    698  1.1  tsutsui       c = getopt (argc, argv, "abc:d:0123456789");
    699  1.1  tsutsui       if (c == EOF)
    700  1.1  tsutsui 	break;
    701  1.1  tsutsui 
    702  1.1  tsutsui       switch (c)
    703  1.1  tsutsui 	{
    704  1.1  tsutsui 	case '0':
    705  1.1  tsutsui 	case '1':
    706  1.1  tsutsui 	case '2':
    707  1.1  tsutsui 	case '3':
    708  1.1  tsutsui 	case '4':
    709  1.1  tsutsui 	case '5':
    710  1.1  tsutsui 	case '6':
    711  1.1  tsutsui 	case '7':
    712  1.1  tsutsui 	case '8':
    713  1.1  tsutsui 	case '9':
    714  1.1  tsutsui 	  if (digit_optind != 0 && digit_optind != this_option_optind)
    715  1.1  tsutsui 	    printf ("digits occur in two different argv-elements.\n");
    716  1.1  tsutsui 	  digit_optind = this_option_optind;
    717  1.1  tsutsui 	  printf ("option %c\n", c);
    718  1.1  tsutsui 	  break;
    719  1.1  tsutsui 
    720  1.1  tsutsui 	case 'a':
    721  1.1  tsutsui 	  printf ("option a\n");
    722  1.1  tsutsui 	  break;
    723  1.1  tsutsui 
    724  1.1  tsutsui 	case 'b':
    725  1.1  tsutsui 	  printf ("option b\n");
    726  1.1  tsutsui 	  break;
    727  1.1  tsutsui 
    728  1.1  tsutsui 	case 'c':
    729  1.1  tsutsui 	  printf ("option c with value `%s'\n", optarg);
    730  1.1  tsutsui 	  break;
    731  1.1  tsutsui 
    732  1.1  tsutsui 	case '?':
    733  1.1  tsutsui 	  break;
    734  1.1  tsutsui 
    735  1.1  tsutsui 	default:
    736  1.1  tsutsui 	  printf ("?? getopt returned character code 0%o ??\n", c);
    737  1.1  tsutsui 	}
    738  1.1  tsutsui     }
    739  1.1  tsutsui 
    740  1.1  tsutsui   if (optind < argc)
    741  1.1  tsutsui     {
    742  1.1  tsutsui       printf ("non-option ARGV-elements: ");
    743  1.1  tsutsui       while (optind < argc)
    744  1.1  tsutsui 	printf ("%s ", argv[optind++]);
    745  1.1  tsutsui       printf ("\n");
    746  1.1  tsutsui     }
    747               
    748                 exit (0);
    749               }
    750               
    751               #endif /* TEST */
    752