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