1*a9fa9459Szrj /* Getopt for GNU.
2*a9fa9459Szrj NOTE: getopt is now part of the C library, so if you don't know what
3*a9fa9459Szrj "Keep this file name-space clean" means, talk to drepper@gnu.org
4*a9fa9459Szrj before changing it!
5*a9fa9459Szrj
6*a9fa9459Szrj Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
7*a9fa9459Szrj 1996, 1997, 1998, 2005 Free Software Foundation, Inc.
8*a9fa9459Szrj
9*a9fa9459Szrj NOTE: This source is derived from an old version taken from the GNU C
10*a9fa9459Szrj Library (glibc).
11*a9fa9459Szrj
12*a9fa9459Szrj This program is free software; you can redistribute it and/or modify it
13*a9fa9459Szrj under the terms of the GNU General Public License as published by the
14*a9fa9459Szrj Free Software Foundation; either version 2, or (at your option) any
15*a9fa9459Szrj later version.
16*a9fa9459Szrj
17*a9fa9459Szrj This program is distributed in the hope that it will be useful,
18*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
19*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20*a9fa9459Szrj GNU General Public License for more details.
21*a9fa9459Szrj
22*a9fa9459Szrj You should have received a copy of the GNU General Public License
23*a9fa9459Szrj along with this program; if not, write to the Free Software
24*a9fa9459Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
25*a9fa9459Szrj USA. */
26*a9fa9459Szrj
27*a9fa9459Szrj /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
28*a9fa9459Szrj Ditto for AIX 3.2 and <stdlib.h>. */
29*a9fa9459Szrj #ifndef _NO_PROTO
30*a9fa9459Szrj # define _NO_PROTO
31*a9fa9459Szrj #endif
32*a9fa9459Szrj
33*a9fa9459Szrj #ifdef HAVE_CONFIG_H
34*a9fa9459Szrj # include <config.h>
35*a9fa9459Szrj #endif
36*a9fa9459Szrj
37*a9fa9459Szrj #if !defined __STDC__ || !__STDC__
38*a9fa9459Szrj /* This is a separate conditional since some stdc systems
39*a9fa9459Szrj reject `defined (const)'. */
40*a9fa9459Szrj # ifndef const
41*a9fa9459Szrj # define const
42*a9fa9459Szrj # endif
43*a9fa9459Szrj #endif
44*a9fa9459Szrj
45*a9fa9459Szrj #include "ansidecl.h"
46*a9fa9459Szrj #include <stdio.h>
47*a9fa9459Szrj
48*a9fa9459Szrj /* Comment out all this code if we are using the GNU C Library, and are not
49*a9fa9459Szrj actually compiling the library itself. This code is part of the GNU C
50*a9fa9459Szrj Library, but also included in many other GNU distributions. Compiling
51*a9fa9459Szrj and linking in this code is a waste when using the GNU C library
52*a9fa9459Szrj (especially if it is a shared library). Rather than having every GNU
53*a9fa9459Szrj program understand `configure --with-gnu-libc' and omit the object files,
54*a9fa9459Szrj it is simpler to just do this in the source for each such file. */
55*a9fa9459Szrj
56*a9fa9459Szrj #define GETOPT_INTERFACE_VERSION 2
57*a9fa9459Szrj #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
58*a9fa9459Szrj # include <gnu-versions.h>
59*a9fa9459Szrj # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
60*a9fa9459Szrj # define ELIDE_CODE
61*a9fa9459Szrj # endif
62*a9fa9459Szrj #endif
63*a9fa9459Szrj
64*a9fa9459Szrj #ifndef ELIDE_CODE
65*a9fa9459Szrj
66*a9fa9459Szrj
67*a9fa9459Szrj /* This needs to come after some library #include
68*a9fa9459Szrj to get __GNU_LIBRARY__ defined. */
69*a9fa9459Szrj #ifdef __GNU_LIBRARY__
70*a9fa9459Szrj /* Don't include stdlib.h for non-GNU C libraries because some of them
71*a9fa9459Szrj contain conflicting prototypes for getopt. */
72*a9fa9459Szrj # include <stdlib.h>
73*a9fa9459Szrj # include <unistd.h>
74*a9fa9459Szrj #endif /* GNU C library. */
75*a9fa9459Szrj
76*a9fa9459Szrj #ifdef VMS
77*a9fa9459Szrj # include <unixlib.h>
78*a9fa9459Szrj # if HAVE_STRING_H - 0
79*a9fa9459Szrj # include <string.h>
80*a9fa9459Szrj # endif
81*a9fa9459Szrj #endif
82*a9fa9459Szrj
83*a9fa9459Szrj #ifndef _
84*a9fa9459Szrj /* This is for other GNU distributions with internationalized messages.
85*a9fa9459Szrj When compiling libc, the _ macro is predefined. */
86*a9fa9459Szrj # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
87*a9fa9459Szrj # include <libintl.h>
88*a9fa9459Szrj # define _(msgid) gettext (msgid)
89*a9fa9459Szrj # else
90*a9fa9459Szrj # define _(msgid) (msgid)
91*a9fa9459Szrj # endif
92*a9fa9459Szrj #endif
93*a9fa9459Szrj
94*a9fa9459Szrj /* This version of `getopt' appears to the caller like standard Unix `getopt'
95*a9fa9459Szrj but it behaves differently for the user, since it allows the user
96*a9fa9459Szrj to intersperse the options with the other arguments.
97*a9fa9459Szrj
98*a9fa9459Szrj As `getopt' works, it permutes the elements of ARGV so that,
99*a9fa9459Szrj when it is done, all the options precede everything else. Thus
100*a9fa9459Szrj all application programs are extended to handle flexible argument order.
101*a9fa9459Szrj
102*a9fa9459Szrj Setting the environment variable POSIXLY_CORRECT disables permutation.
103*a9fa9459Szrj Then the behavior is completely standard.
104*a9fa9459Szrj
105*a9fa9459Szrj GNU application programs can use a third alternative mode in which
106*a9fa9459Szrj they can distinguish the relative order of options and other arguments. */
107*a9fa9459Szrj
108*a9fa9459Szrj #include "getopt.h"
109*a9fa9459Szrj
110*a9fa9459Szrj /* For communication from `getopt' to the caller.
111*a9fa9459Szrj When `getopt' finds an option that takes an argument,
112*a9fa9459Szrj the argument value is returned here.
113*a9fa9459Szrj Also, when `ordering' is RETURN_IN_ORDER,
114*a9fa9459Szrj each non-option ARGV-element is returned here. */
115*a9fa9459Szrj
116*a9fa9459Szrj char *optarg = NULL;
117*a9fa9459Szrj
118*a9fa9459Szrj /* Index in ARGV of the next element to be scanned.
119*a9fa9459Szrj This is used for communication to and from the caller
120*a9fa9459Szrj and for communication between successive calls to `getopt'.
121*a9fa9459Szrj
122*a9fa9459Szrj On entry to `getopt', zero means this is the first call; initialize.
123*a9fa9459Szrj
124*a9fa9459Szrj When `getopt' returns -1, this is the index of the first of the
125*a9fa9459Szrj non-option elements that the caller should itself scan.
126*a9fa9459Szrj
127*a9fa9459Szrj Otherwise, `optind' communicates from one call to the next
128*a9fa9459Szrj how much of ARGV has been scanned so far. */
129*a9fa9459Szrj
130*a9fa9459Szrj /* 1003.2 says this must be 1 before any call. */
131*a9fa9459Szrj int optind = 1;
132*a9fa9459Szrj
133*a9fa9459Szrj /* Formerly, initialization of getopt depended on optind==0, which
134*a9fa9459Szrj causes problems with re-calling getopt as programs generally don't
135*a9fa9459Szrj know that. */
136*a9fa9459Szrj
137*a9fa9459Szrj int __getopt_initialized = 0;
138*a9fa9459Szrj
139*a9fa9459Szrj /* The next char to be scanned in the option-element
140*a9fa9459Szrj in which the last option character we returned was found.
141*a9fa9459Szrj This allows us to pick up the scan where we left off.
142*a9fa9459Szrj
143*a9fa9459Szrj If this is zero, or a null string, it means resume the scan
144*a9fa9459Szrj by advancing to the next ARGV-element. */
145*a9fa9459Szrj
146*a9fa9459Szrj static char *nextchar;
147*a9fa9459Szrj
148*a9fa9459Szrj /* Callers store zero here to inhibit the error message
149*a9fa9459Szrj for unrecognized options. */
150*a9fa9459Szrj
151*a9fa9459Szrj int opterr = 1;
152*a9fa9459Szrj
153*a9fa9459Szrj /* Set to an option character which was unrecognized.
154*a9fa9459Szrj This must be initialized on some systems to avoid linking in the
155*a9fa9459Szrj system's own getopt implementation. */
156*a9fa9459Szrj
157*a9fa9459Szrj int optopt = '?';
158*a9fa9459Szrj
159*a9fa9459Szrj /* Describe how to deal with options that follow non-option ARGV-elements.
160*a9fa9459Szrj
161*a9fa9459Szrj If the caller did not specify anything,
162*a9fa9459Szrj the default is REQUIRE_ORDER if the environment variable
163*a9fa9459Szrj POSIXLY_CORRECT is defined, PERMUTE otherwise.
164*a9fa9459Szrj
165*a9fa9459Szrj REQUIRE_ORDER means don't recognize them as options;
166*a9fa9459Szrj stop option processing when the first non-option is seen.
167*a9fa9459Szrj This is what Unix does.
168*a9fa9459Szrj This mode of operation is selected by either setting the environment
169*a9fa9459Szrj variable POSIXLY_CORRECT, or using `+' as the first character
170*a9fa9459Szrj of the list of option characters.
171*a9fa9459Szrj
172*a9fa9459Szrj PERMUTE is the default. We permute the contents of ARGV as we scan,
173*a9fa9459Szrj so that eventually all the non-options are at the end. This allows options
174*a9fa9459Szrj to be given in any order, even with programs that were not written to
175*a9fa9459Szrj expect this.
176*a9fa9459Szrj
177*a9fa9459Szrj RETURN_IN_ORDER is an option available to programs that were written
178*a9fa9459Szrj to expect options and other ARGV-elements in any order and that care about
179*a9fa9459Szrj the ordering of the two. We describe each non-option ARGV-element
180*a9fa9459Szrj as if it were the argument of an option with character code 1.
181*a9fa9459Szrj Using `-' as the first character of the list of option characters
182*a9fa9459Szrj selects this mode of operation.
183*a9fa9459Szrj
184*a9fa9459Szrj The special argument `--' forces an end of option-scanning regardless
185*a9fa9459Szrj of the value of `ordering'. In the case of RETURN_IN_ORDER, only
186*a9fa9459Szrj `--' can cause `getopt' to return -1 with `optind' != ARGC. */
187*a9fa9459Szrj
188*a9fa9459Szrj static enum
189*a9fa9459Szrj {
190*a9fa9459Szrj REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
191*a9fa9459Szrj } ordering;
192*a9fa9459Szrj
193*a9fa9459Szrj /* Value of POSIXLY_CORRECT environment variable. */
194*a9fa9459Szrj static char *posixly_correct;
195*a9fa9459Szrj
196*a9fa9459Szrj #ifdef __GNU_LIBRARY__
197*a9fa9459Szrj /* We want to avoid inclusion of string.h with non-GNU libraries
198*a9fa9459Szrj because there are many ways it can cause trouble.
199*a9fa9459Szrj On some systems, it contains special magic macros that don't work
200*a9fa9459Szrj in GCC. */
201*a9fa9459Szrj # include <string.h>
202*a9fa9459Szrj # define my_index strchr
203*a9fa9459Szrj #else
204*a9fa9459Szrj
205*a9fa9459Szrj # if HAVE_STRING_H
206*a9fa9459Szrj # include <string.h>
207*a9fa9459Szrj # else
208*a9fa9459Szrj # if HAVE_STRINGS_H
209*a9fa9459Szrj # include <strings.h>
210*a9fa9459Szrj # endif
211*a9fa9459Szrj # endif
212*a9fa9459Szrj
213*a9fa9459Szrj /* Avoid depending on library functions or files
214*a9fa9459Szrj whose names are inconsistent. */
215*a9fa9459Szrj
216*a9fa9459Szrj #if HAVE_STDLIB_H && HAVE_DECL_GETENV
217*a9fa9459Szrj # include <stdlib.h>
218*a9fa9459Szrj #elif !defined(getenv)
219*a9fa9459Szrj # ifdef __cplusplus
220*a9fa9459Szrj extern "C" {
221*a9fa9459Szrj # endif /* __cplusplus */
222*a9fa9459Szrj extern char *getenv (const char *);
223*a9fa9459Szrj # ifdef __cplusplus
224*a9fa9459Szrj }
225*a9fa9459Szrj # endif /* __cplusplus */
226*a9fa9459Szrj #endif
227*a9fa9459Szrj
228*a9fa9459Szrj static char *
my_index(const char * str,int chr)229*a9fa9459Szrj my_index (const char *str, int chr)
230*a9fa9459Szrj {
231*a9fa9459Szrj while (*str)
232*a9fa9459Szrj {
233*a9fa9459Szrj if (*str == chr)
234*a9fa9459Szrj return (char *) str;
235*a9fa9459Szrj str++;
236*a9fa9459Szrj }
237*a9fa9459Szrj return 0;
238*a9fa9459Szrj }
239*a9fa9459Szrj
240*a9fa9459Szrj /* If using GCC, we can safely declare strlen this way.
241*a9fa9459Szrj If not using GCC, it is ok not to declare it. */
242*a9fa9459Szrj #ifdef __GNUC__
243*a9fa9459Szrj /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
244*a9fa9459Szrj That was relevant to code that was here before. */
245*a9fa9459Szrj # if (!defined __STDC__ || !__STDC__) && !defined strlen
246*a9fa9459Szrj /* gcc with -traditional declares the built-in strlen to return int,
247*a9fa9459Szrj and has done so at least since version 2.4.5. -- rms. */
248*a9fa9459Szrj extern int strlen (const char *);
249*a9fa9459Szrj # endif /* not __STDC__ */
250*a9fa9459Szrj #endif /* __GNUC__ */
251*a9fa9459Szrj
252*a9fa9459Szrj #endif /* not __GNU_LIBRARY__ */
253*a9fa9459Szrj
254*a9fa9459Szrj /* Handle permutation of arguments. */
255*a9fa9459Szrj
256*a9fa9459Szrj /* Describe the part of ARGV that contains non-options that have
257*a9fa9459Szrj been skipped. `first_nonopt' is the index in ARGV of the first of them;
258*a9fa9459Szrj `last_nonopt' is the index after the last of them. */
259*a9fa9459Szrj
260*a9fa9459Szrj static int first_nonopt;
261*a9fa9459Szrj static int last_nonopt;
262*a9fa9459Szrj
263*a9fa9459Szrj #ifdef _LIBC
264*a9fa9459Szrj /* Bash 2.0 gives us an environment variable containing flags
265*a9fa9459Szrj indicating ARGV elements that should not be considered arguments. */
266*a9fa9459Szrj
267*a9fa9459Szrj /* Defined in getopt_init.c */
268*a9fa9459Szrj extern char *__getopt_nonoption_flags;
269*a9fa9459Szrj
270*a9fa9459Szrj static int nonoption_flags_max_len;
271*a9fa9459Szrj static int nonoption_flags_len;
272*a9fa9459Szrj
273*a9fa9459Szrj static int original_argc;
274*a9fa9459Szrj static char *const *original_argv;
275*a9fa9459Szrj
276*a9fa9459Szrj /* Make sure the environment variable bash 2.0 puts in the environment
277*a9fa9459Szrj is valid for the getopt call we must make sure that the ARGV passed
278*a9fa9459Szrj to getopt is that one passed to the process. */
279*a9fa9459Szrj static void
280*a9fa9459Szrj __attribute__ ((unused))
store_args_and_env(int argc,char * const * argv)281*a9fa9459Szrj store_args_and_env (int argc, char *const *argv)
282*a9fa9459Szrj {
283*a9fa9459Szrj /* XXX This is no good solution. We should rather copy the args so
284*a9fa9459Szrj that we can compare them later. But we must not use malloc(3). */
285*a9fa9459Szrj original_argc = argc;
286*a9fa9459Szrj original_argv = argv;
287*a9fa9459Szrj }
288*a9fa9459Szrj # ifdef text_set_element
289*a9fa9459Szrj text_set_element (__libc_subinit, store_args_and_env);
290*a9fa9459Szrj # endif /* text_set_element */
291*a9fa9459Szrj
292*a9fa9459Szrj # define SWAP_FLAGS(ch1, ch2) \
293*a9fa9459Szrj if (nonoption_flags_len > 0) \
294*a9fa9459Szrj { \
295*a9fa9459Szrj char __tmp = __getopt_nonoption_flags[ch1]; \
296*a9fa9459Szrj __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
297*a9fa9459Szrj __getopt_nonoption_flags[ch2] = __tmp; \
298*a9fa9459Szrj }
299*a9fa9459Szrj #else /* !_LIBC */
300*a9fa9459Szrj # define SWAP_FLAGS(ch1, ch2)
301*a9fa9459Szrj #endif /* _LIBC */
302*a9fa9459Szrj
303*a9fa9459Szrj /* Exchange two adjacent subsequences of ARGV.
304*a9fa9459Szrj One subsequence is elements [first_nonopt,last_nonopt)
305*a9fa9459Szrj which contains all the non-options that have been skipped so far.
306*a9fa9459Szrj The other is elements [last_nonopt,optind), which contains all
307*a9fa9459Szrj the options processed since those non-options were skipped.
308*a9fa9459Szrj
309*a9fa9459Szrj `first_nonopt' and `last_nonopt' are relocated so that they describe
310*a9fa9459Szrj the new indices of the non-options in ARGV after they are moved. */
311*a9fa9459Szrj
312*a9fa9459Szrj #if defined __STDC__ && __STDC__
313*a9fa9459Szrj static void exchange (char **);
314*a9fa9459Szrj #endif
315*a9fa9459Szrj
316*a9fa9459Szrj static void
exchange(char ** argv)317*a9fa9459Szrj exchange (char **argv)
318*a9fa9459Szrj {
319*a9fa9459Szrj int bottom = first_nonopt;
320*a9fa9459Szrj int middle = last_nonopt;
321*a9fa9459Szrj int top = optind;
322*a9fa9459Szrj char *tem;
323*a9fa9459Szrj
324*a9fa9459Szrj /* Exchange the shorter segment with the far end of the longer segment.
325*a9fa9459Szrj That puts the shorter segment into the right place.
326*a9fa9459Szrj It leaves the longer segment in the right place overall,
327*a9fa9459Szrj but it consists of two parts that need to be swapped next. */
328*a9fa9459Szrj
329*a9fa9459Szrj #ifdef _LIBC
330*a9fa9459Szrj /* First make sure the handling of the `__getopt_nonoption_flags'
331*a9fa9459Szrj string can work normally. Our top argument must be in the range
332*a9fa9459Szrj of the string. */
333*a9fa9459Szrj if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
334*a9fa9459Szrj {
335*a9fa9459Szrj /* We must extend the array. The user plays games with us and
336*a9fa9459Szrj presents new arguments. */
337*a9fa9459Szrj char *new_str = (char *) malloc (top + 1);
338*a9fa9459Szrj if (new_str == NULL)
339*a9fa9459Szrj nonoption_flags_len = nonoption_flags_max_len = 0;
340*a9fa9459Szrj else
341*a9fa9459Szrj {
342*a9fa9459Szrj memset (mempcpy (new_str, __getopt_nonoption_flags,
343*a9fa9459Szrj nonoption_flags_max_len),
344*a9fa9459Szrj '\0', top + 1 - nonoption_flags_max_len);
345*a9fa9459Szrj nonoption_flags_max_len = top + 1;
346*a9fa9459Szrj __getopt_nonoption_flags = new_str;
347*a9fa9459Szrj }
348*a9fa9459Szrj }
349*a9fa9459Szrj #endif
350*a9fa9459Szrj
351*a9fa9459Szrj while (top > middle && middle > bottom)
352*a9fa9459Szrj {
353*a9fa9459Szrj if (top - middle > middle - bottom)
354*a9fa9459Szrj {
355*a9fa9459Szrj /* Bottom segment is the short one. */
356*a9fa9459Szrj int len = middle - bottom;
357*a9fa9459Szrj register int i;
358*a9fa9459Szrj
359*a9fa9459Szrj /* Swap it with the top part of the top segment. */
360*a9fa9459Szrj for (i = 0; i < len; i++)
361*a9fa9459Szrj {
362*a9fa9459Szrj tem = argv[bottom + i];
363*a9fa9459Szrj argv[bottom + i] = argv[top - (middle - bottom) + i];
364*a9fa9459Szrj argv[top - (middle - bottom) + i] = tem;
365*a9fa9459Szrj SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
366*a9fa9459Szrj }
367*a9fa9459Szrj /* Exclude the moved bottom segment from further swapping. */
368*a9fa9459Szrj top -= len;
369*a9fa9459Szrj }
370*a9fa9459Szrj else
371*a9fa9459Szrj {
372*a9fa9459Szrj /* Top segment is the short one. */
373*a9fa9459Szrj int len = top - middle;
374*a9fa9459Szrj register int i;
375*a9fa9459Szrj
376*a9fa9459Szrj /* Swap it with the bottom part of the bottom segment. */
377*a9fa9459Szrj for (i = 0; i < len; i++)
378*a9fa9459Szrj {
379*a9fa9459Szrj tem = argv[bottom + i];
380*a9fa9459Szrj argv[bottom + i] = argv[middle + i];
381*a9fa9459Szrj argv[middle + i] = tem;
382*a9fa9459Szrj SWAP_FLAGS (bottom + i, middle + i);
383*a9fa9459Szrj }
384*a9fa9459Szrj /* Exclude the moved top segment from further swapping. */
385*a9fa9459Szrj bottom += len;
386*a9fa9459Szrj }
387*a9fa9459Szrj }
388*a9fa9459Szrj
389*a9fa9459Szrj /* Update records for the slots the non-options now occupy. */
390*a9fa9459Szrj
391*a9fa9459Szrj first_nonopt += (optind - last_nonopt);
392*a9fa9459Szrj last_nonopt = optind;
393*a9fa9459Szrj }
394*a9fa9459Szrj
395*a9fa9459Szrj /* Initialize the internal data when the first call is made. */
396*a9fa9459Szrj
397*a9fa9459Szrj #if defined __STDC__ && __STDC__
398*a9fa9459Szrj static const char *_getopt_initialize (int, char *const *, const char *);
399*a9fa9459Szrj #endif
400*a9fa9459Szrj static const char *
_getopt_initialize(int argc ATTRIBUTE_UNUSED,char * const * argv ATTRIBUTE_UNUSED,const char * optstring)401*a9fa9459Szrj _getopt_initialize (int argc ATTRIBUTE_UNUSED,
402*a9fa9459Szrj char *const *argv ATTRIBUTE_UNUSED,
403*a9fa9459Szrj const char *optstring)
404*a9fa9459Szrj {
405*a9fa9459Szrj /* Start processing options with ARGV-element 1 (since ARGV-element 0
406*a9fa9459Szrj is the program name); the sequence of previously skipped
407*a9fa9459Szrj non-option ARGV-elements is empty. */
408*a9fa9459Szrj
409*a9fa9459Szrj first_nonopt = last_nonopt = optind;
410*a9fa9459Szrj
411*a9fa9459Szrj nextchar = NULL;
412*a9fa9459Szrj
413*a9fa9459Szrj posixly_correct = getenv ("POSIXLY_CORRECT");
414*a9fa9459Szrj
415*a9fa9459Szrj /* Determine how to handle the ordering of options and nonoptions. */
416*a9fa9459Szrj
417*a9fa9459Szrj if (optstring[0] == '-')
418*a9fa9459Szrj {
419*a9fa9459Szrj ordering = RETURN_IN_ORDER;
420*a9fa9459Szrj ++optstring;
421*a9fa9459Szrj }
422*a9fa9459Szrj else if (optstring[0] == '+')
423*a9fa9459Szrj {
424*a9fa9459Szrj ordering = REQUIRE_ORDER;
425*a9fa9459Szrj ++optstring;
426*a9fa9459Szrj }
427*a9fa9459Szrj else if (posixly_correct != NULL)
428*a9fa9459Szrj ordering = REQUIRE_ORDER;
429*a9fa9459Szrj else
430*a9fa9459Szrj ordering = PERMUTE;
431*a9fa9459Szrj
432*a9fa9459Szrj #ifdef _LIBC
433*a9fa9459Szrj if (posixly_correct == NULL
434*a9fa9459Szrj && argc == original_argc && argv == original_argv)
435*a9fa9459Szrj {
436*a9fa9459Szrj if (nonoption_flags_max_len == 0)
437*a9fa9459Szrj {
438*a9fa9459Szrj if (__getopt_nonoption_flags == NULL
439*a9fa9459Szrj || __getopt_nonoption_flags[0] == '\0')
440*a9fa9459Szrj nonoption_flags_max_len = -1;
441*a9fa9459Szrj else
442*a9fa9459Szrj {
443*a9fa9459Szrj const char *orig_str = __getopt_nonoption_flags;
444*a9fa9459Szrj int len = nonoption_flags_max_len = strlen (orig_str);
445*a9fa9459Szrj if (nonoption_flags_max_len < argc)
446*a9fa9459Szrj nonoption_flags_max_len = argc;
447*a9fa9459Szrj __getopt_nonoption_flags =
448*a9fa9459Szrj (char *) malloc (nonoption_flags_max_len);
449*a9fa9459Szrj if (__getopt_nonoption_flags == NULL)
450*a9fa9459Szrj nonoption_flags_max_len = -1;
451*a9fa9459Szrj else
452*a9fa9459Szrj memset (mempcpy (__getopt_nonoption_flags, orig_str, len),
453*a9fa9459Szrj '\0', nonoption_flags_max_len - len);
454*a9fa9459Szrj }
455*a9fa9459Szrj }
456*a9fa9459Szrj nonoption_flags_len = nonoption_flags_max_len;
457*a9fa9459Szrj }
458*a9fa9459Szrj else
459*a9fa9459Szrj nonoption_flags_len = 0;
460*a9fa9459Szrj #endif
461*a9fa9459Szrj
462*a9fa9459Szrj return optstring;
463*a9fa9459Szrj }
464*a9fa9459Szrj
465*a9fa9459Szrj /* Scan elements of ARGV (whose length is ARGC) for option characters
466*a9fa9459Szrj given in OPTSTRING.
467*a9fa9459Szrj
468*a9fa9459Szrj If an element of ARGV starts with '-', and is not exactly "-" or "--",
469*a9fa9459Szrj then it is an option element. The characters of this element
470*a9fa9459Szrj (aside from the initial '-') are option characters. If `getopt'
471*a9fa9459Szrj is called repeatedly, it returns successively each of the option characters
472*a9fa9459Szrj from each of the option elements.
473*a9fa9459Szrj
474*a9fa9459Szrj If `getopt' finds another option character, it returns that character,
475*a9fa9459Szrj updating `optind' and `nextchar' so that the next call to `getopt' can
476*a9fa9459Szrj resume the scan with the following option character or ARGV-element.
477*a9fa9459Szrj
478*a9fa9459Szrj If there are no more option characters, `getopt' returns -1.
479*a9fa9459Szrj Then `optind' is the index in ARGV of the first ARGV-element
480*a9fa9459Szrj that is not an option. (The ARGV-elements have been permuted
481*a9fa9459Szrj so that those that are not options now come last.)
482*a9fa9459Szrj
483*a9fa9459Szrj OPTSTRING is a string containing the legitimate option characters.
484*a9fa9459Szrj If an option character is seen that is not listed in OPTSTRING,
485*a9fa9459Szrj return '?' after printing an error message. If you set `opterr' to
486*a9fa9459Szrj zero, the error message is suppressed but we still return '?'.
487*a9fa9459Szrj
488*a9fa9459Szrj If a char in OPTSTRING is followed by a colon, that means it wants an arg,
489*a9fa9459Szrj so the following text in the same ARGV-element, or the text of the following
490*a9fa9459Szrj ARGV-element, is returned in `optarg'. Two colons mean an option that
491*a9fa9459Szrj wants an optional arg; if there is text in the current ARGV-element,
492*a9fa9459Szrj it is returned in `optarg', otherwise `optarg' is set to zero.
493*a9fa9459Szrj
494*a9fa9459Szrj If OPTSTRING starts with `-' or `+', it requests different methods of
495*a9fa9459Szrj handling the non-option ARGV-elements.
496*a9fa9459Szrj See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
497*a9fa9459Szrj
498*a9fa9459Szrj Long-named options begin with `--' instead of `-'.
499*a9fa9459Szrj Their names may be abbreviated as long as the abbreviation is unique
500*a9fa9459Szrj or is an exact match for some defined option. If they have an
501*a9fa9459Szrj argument, it follows the option name in the same ARGV-element, separated
502*a9fa9459Szrj from the option name by a `=', or else the in next ARGV-element.
503*a9fa9459Szrj When `getopt' finds a long-named option, it returns 0 if that option's
504*a9fa9459Szrj `flag' field is nonzero, the value of the option's `val' field
505*a9fa9459Szrj if the `flag' field is zero.
506*a9fa9459Szrj
507*a9fa9459Szrj The elements of ARGV aren't really const, because we permute them.
508*a9fa9459Szrj But we pretend they're const in the prototype to be compatible
509*a9fa9459Szrj with other systems.
510*a9fa9459Szrj
511*a9fa9459Szrj LONGOPTS is a vector of `struct option' terminated by an
512*a9fa9459Szrj element containing a name which is zero.
513*a9fa9459Szrj
514*a9fa9459Szrj LONGIND returns the index in LONGOPT of the long-named option found.
515*a9fa9459Szrj It is only valid when a long-named option has been found by the most
516*a9fa9459Szrj recent call.
517*a9fa9459Szrj
518*a9fa9459Szrj If LONG_ONLY is nonzero, '-' as well as '--' can introduce
519*a9fa9459Szrj long-named options. */
520*a9fa9459Szrj
521*a9fa9459Szrj int
_getopt_internal(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * longind,int long_only)522*a9fa9459Szrj _getopt_internal (int argc, char *const *argv, const char *optstring,
523*a9fa9459Szrj const struct option *longopts,
524*a9fa9459Szrj int *longind, int long_only)
525*a9fa9459Szrj {
526*a9fa9459Szrj optarg = NULL;
527*a9fa9459Szrj
528*a9fa9459Szrj if (optind == 0 || !__getopt_initialized)
529*a9fa9459Szrj {
530*a9fa9459Szrj if (optind == 0)
531*a9fa9459Szrj optind = 1; /* Don't scan ARGV[0], the program name. */
532*a9fa9459Szrj optstring = _getopt_initialize (argc, argv, optstring);
533*a9fa9459Szrj __getopt_initialized = 1;
534*a9fa9459Szrj }
535*a9fa9459Szrj
536*a9fa9459Szrj /* Test whether ARGV[optind] points to a non-option argument.
537*a9fa9459Szrj Either it does not have option syntax, or there is an environment flag
538*a9fa9459Szrj from the shell indicating it is not an option. The later information
539*a9fa9459Szrj is only used when the used in the GNU libc. */
540*a9fa9459Szrj #ifdef _LIBC
541*a9fa9459Szrj # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
542*a9fa9459Szrj || (optind < nonoption_flags_len \
543*a9fa9459Szrj && __getopt_nonoption_flags[optind] == '1'))
544*a9fa9459Szrj #else
545*a9fa9459Szrj # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
546*a9fa9459Szrj #endif
547*a9fa9459Szrj
548*a9fa9459Szrj if (nextchar == NULL || *nextchar == '\0')
549*a9fa9459Szrj {
550*a9fa9459Szrj /* Advance to the next ARGV-element. */
551*a9fa9459Szrj
552*a9fa9459Szrj /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
553*a9fa9459Szrj moved back by the user (who may also have changed the arguments). */
554*a9fa9459Szrj if (last_nonopt > optind)
555*a9fa9459Szrj last_nonopt = optind;
556*a9fa9459Szrj if (first_nonopt > optind)
557*a9fa9459Szrj first_nonopt = optind;
558*a9fa9459Szrj
559*a9fa9459Szrj if (ordering == PERMUTE)
560*a9fa9459Szrj {
561*a9fa9459Szrj /* If we have just processed some options following some non-options,
562*a9fa9459Szrj exchange them so that the options come first. */
563*a9fa9459Szrj
564*a9fa9459Szrj if (first_nonopt != last_nonopt && last_nonopt != optind)
565*a9fa9459Szrj exchange ((char **) argv);
566*a9fa9459Szrj else if (last_nonopt != optind)
567*a9fa9459Szrj first_nonopt = optind;
568*a9fa9459Szrj
569*a9fa9459Szrj /* Skip any additional non-options
570*a9fa9459Szrj and extend the range of non-options previously skipped. */
571*a9fa9459Szrj
572*a9fa9459Szrj while (optind < argc && NONOPTION_P)
573*a9fa9459Szrj optind++;
574*a9fa9459Szrj last_nonopt = optind;
575*a9fa9459Szrj }
576*a9fa9459Szrj
577*a9fa9459Szrj /* The special ARGV-element `--' means premature end of options.
578*a9fa9459Szrj Skip it like a null option,
579*a9fa9459Szrj then exchange with previous non-options as if it were an option,
580*a9fa9459Szrj then skip everything else like a non-option. */
581*a9fa9459Szrj
582*a9fa9459Szrj if (optind != argc && !strcmp (argv[optind], "--"))
583*a9fa9459Szrj {
584*a9fa9459Szrj optind++;
585*a9fa9459Szrj
586*a9fa9459Szrj if (first_nonopt != last_nonopt && last_nonopt != optind)
587*a9fa9459Szrj exchange ((char **) argv);
588*a9fa9459Szrj else if (first_nonopt == last_nonopt)
589*a9fa9459Szrj first_nonopt = optind;
590*a9fa9459Szrj last_nonopt = argc;
591*a9fa9459Szrj
592*a9fa9459Szrj optind = argc;
593*a9fa9459Szrj }
594*a9fa9459Szrj
595*a9fa9459Szrj /* If we have done all the ARGV-elements, stop the scan
596*a9fa9459Szrj and back over any non-options that we skipped and permuted. */
597*a9fa9459Szrj
598*a9fa9459Szrj if (optind == argc)
599*a9fa9459Szrj {
600*a9fa9459Szrj /* Set the next-arg-index to point at the non-options
601*a9fa9459Szrj that we previously skipped, so the caller will digest them. */
602*a9fa9459Szrj if (first_nonopt != last_nonopt)
603*a9fa9459Szrj optind = first_nonopt;
604*a9fa9459Szrj return -1;
605*a9fa9459Szrj }
606*a9fa9459Szrj
607*a9fa9459Szrj /* If we have come to a non-option and did not permute it,
608*a9fa9459Szrj either stop the scan or describe it to the caller and pass it by. */
609*a9fa9459Szrj
610*a9fa9459Szrj if (NONOPTION_P)
611*a9fa9459Szrj {
612*a9fa9459Szrj if (ordering == REQUIRE_ORDER)
613*a9fa9459Szrj return -1;
614*a9fa9459Szrj optarg = argv[optind++];
615*a9fa9459Szrj return 1;
616*a9fa9459Szrj }
617*a9fa9459Szrj
618*a9fa9459Szrj /* We have found another option-ARGV-element.
619*a9fa9459Szrj Skip the initial punctuation. */
620*a9fa9459Szrj
621*a9fa9459Szrj nextchar = (argv[optind] + 1
622*a9fa9459Szrj + (longopts != NULL && argv[optind][1] == '-'));
623*a9fa9459Szrj }
624*a9fa9459Szrj
625*a9fa9459Szrj /* Decode the current option-ARGV-element. */
626*a9fa9459Szrj
627*a9fa9459Szrj /* Check whether the ARGV-element is a long option.
628*a9fa9459Szrj
629*a9fa9459Szrj If long_only and the ARGV-element has the form "-f", where f is
630*a9fa9459Szrj a valid short option, don't consider it an abbreviated form of
631*a9fa9459Szrj a long option that starts with f. Otherwise there would be no
632*a9fa9459Szrj way to give the -f short option.
633*a9fa9459Szrj
634*a9fa9459Szrj On the other hand, if there's a long option "fubar" and
635*a9fa9459Szrj the ARGV-element is "-fu", do consider that an abbreviation of
636*a9fa9459Szrj the long option, just like "--fu", and not "-f" with arg "u".
637*a9fa9459Szrj
638*a9fa9459Szrj This distinction seems to be the most useful approach. */
639*a9fa9459Szrj
640*a9fa9459Szrj if (longopts != NULL
641*a9fa9459Szrj && (argv[optind][1] == '-'
642*a9fa9459Szrj || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
643*a9fa9459Szrj {
644*a9fa9459Szrj char *nameend;
645*a9fa9459Szrj const struct option *p;
646*a9fa9459Szrj const struct option *pfound = NULL;
647*a9fa9459Szrj int exact = 0;
648*a9fa9459Szrj int ambig = 0;
649*a9fa9459Szrj int indfound = -1;
650*a9fa9459Szrj int option_index;
651*a9fa9459Szrj
652*a9fa9459Szrj for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
653*a9fa9459Szrj /* Do nothing. */ ;
654*a9fa9459Szrj
655*a9fa9459Szrj /* Test all long options for either exact match
656*a9fa9459Szrj or abbreviated matches. */
657*a9fa9459Szrj for (p = longopts, option_index = 0; p->name; p++, option_index++)
658*a9fa9459Szrj if (!strncmp (p->name, nextchar, nameend - nextchar))
659*a9fa9459Szrj {
660*a9fa9459Szrj if ((unsigned int) (nameend - nextchar)
661*a9fa9459Szrj == (unsigned int) strlen (p->name))
662*a9fa9459Szrj {
663*a9fa9459Szrj /* Exact match found. */
664*a9fa9459Szrj pfound = p;
665*a9fa9459Szrj indfound = option_index;
666*a9fa9459Szrj exact = 1;
667*a9fa9459Szrj break;
668*a9fa9459Szrj }
669*a9fa9459Szrj else if (pfound == NULL)
670*a9fa9459Szrj {
671*a9fa9459Szrj /* First nonexact match found. */
672*a9fa9459Szrj pfound = p;
673*a9fa9459Szrj indfound = option_index;
674*a9fa9459Szrj }
675*a9fa9459Szrj else
676*a9fa9459Szrj /* Second or later nonexact match found. */
677*a9fa9459Szrj ambig = 1;
678*a9fa9459Szrj }
679*a9fa9459Szrj
680*a9fa9459Szrj if (ambig && !exact)
681*a9fa9459Szrj {
682*a9fa9459Szrj if (opterr)
683*a9fa9459Szrj fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
684*a9fa9459Szrj argv[0], argv[optind]);
685*a9fa9459Szrj nextchar += strlen (nextchar);
686*a9fa9459Szrj optind++;
687*a9fa9459Szrj optopt = 0;
688*a9fa9459Szrj return '?';
689*a9fa9459Szrj }
690*a9fa9459Szrj
691*a9fa9459Szrj if (pfound != NULL)
692*a9fa9459Szrj {
693*a9fa9459Szrj option_index = indfound;
694*a9fa9459Szrj optind++;
695*a9fa9459Szrj if (*nameend)
696*a9fa9459Szrj {
697*a9fa9459Szrj /* Don't test has_arg with >, because some C compilers don't
698*a9fa9459Szrj allow it to be used on enums. */
699*a9fa9459Szrj if (pfound->has_arg)
700*a9fa9459Szrj optarg = nameend + 1;
701*a9fa9459Szrj else
702*a9fa9459Szrj {
703*a9fa9459Szrj if (opterr)
704*a9fa9459Szrj {
705*a9fa9459Szrj if (argv[optind - 1][1] == '-')
706*a9fa9459Szrj /* --option */
707*a9fa9459Szrj fprintf (stderr,
708*a9fa9459Szrj _("%s: option `--%s' doesn't allow an argument\n"),
709*a9fa9459Szrj argv[0], pfound->name);
710*a9fa9459Szrj else
711*a9fa9459Szrj /* +option or -option */
712*a9fa9459Szrj fprintf (stderr,
713*a9fa9459Szrj _("%s: option `%c%s' doesn't allow an argument\n"),
714*a9fa9459Szrj argv[0], argv[optind - 1][0], pfound->name);
715*a9fa9459Szrj
716*a9fa9459Szrj nextchar += strlen (nextchar);
717*a9fa9459Szrj
718*a9fa9459Szrj optopt = pfound->val;
719*a9fa9459Szrj return '?';
720*a9fa9459Szrj }
721*a9fa9459Szrj }
722*a9fa9459Szrj }
723*a9fa9459Szrj else if (pfound->has_arg == 1)
724*a9fa9459Szrj {
725*a9fa9459Szrj if (optind < argc)
726*a9fa9459Szrj optarg = argv[optind++];
727*a9fa9459Szrj else
728*a9fa9459Szrj {
729*a9fa9459Szrj if (opterr)
730*a9fa9459Szrj fprintf (stderr,
731*a9fa9459Szrj _("%s: option `%s' requires an argument\n"),
732*a9fa9459Szrj argv[0], argv[optind - 1]);
733*a9fa9459Szrj nextchar += strlen (nextchar);
734*a9fa9459Szrj optopt = pfound->val;
735*a9fa9459Szrj return optstring[0] == ':' ? ':' : '?';
736*a9fa9459Szrj }
737*a9fa9459Szrj }
738*a9fa9459Szrj nextchar += strlen (nextchar);
739*a9fa9459Szrj if (longind != NULL)
740*a9fa9459Szrj *longind = option_index;
741*a9fa9459Szrj if (pfound->flag)
742*a9fa9459Szrj {
743*a9fa9459Szrj *(pfound->flag) = pfound->val;
744*a9fa9459Szrj return 0;
745*a9fa9459Szrj }
746*a9fa9459Szrj return pfound->val;
747*a9fa9459Szrj }
748*a9fa9459Szrj
749*a9fa9459Szrj /* Can't find it as a long option. If this is not getopt_long_only,
750*a9fa9459Szrj or the option starts with '--' or is not a valid short
751*a9fa9459Szrj option, then it's an error.
752*a9fa9459Szrj Otherwise interpret it as a short option. */
753*a9fa9459Szrj if (!long_only || argv[optind][1] == '-'
754*a9fa9459Szrj || my_index (optstring, *nextchar) == NULL)
755*a9fa9459Szrj {
756*a9fa9459Szrj if (opterr)
757*a9fa9459Szrj {
758*a9fa9459Szrj if (argv[optind][1] == '-')
759*a9fa9459Szrj /* --option */
760*a9fa9459Szrj fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
761*a9fa9459Szrj argv[0], nextchar);
762*a9fa9459Szrj else
763*a9fa9459Szrj /* +option or -option */
764*a9fa9459Szrj fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
765*a9fa9459Szrj argv[0], argv[optind][0], nextchar);
766*a9fa9459Szrj }
767*a9fa9459Szrj nextchar = (char *) "";
768*a9fa9459Szrj optind++;
769*a9fa9459Szrj optopt = 0;
770*a9fa9459Szrj return '?';
771*a9fa9459Szrj }
772*a9fa9459Szrj }
773*a9fa9459Szrj
774*a9fa9459Szrj /* Look at and handle the next short option-character. */
775*a9fa9459Szrj
776*a9fa9459Szrj {
777*a9fa9459Szrj char c = *nextchar++;
778*a9fa9459Szrj char *temp = my_index (optstring, c);
779*a9fa9459Szrj
780*a9fa9459Szrj /* Increment `optind' when we start to process its last character. */
781*a9fa9459Szrj if (*nextchar == '\0')
782*a9fa9459Szrj ++optind;
783*a9fa9459Szrj
784*a9fa9459Szrj if (temp == NULL || c == ':')
785*a9fa9459Szrj {
786*a9fa9459Szrj if (opterr)
787*a9fa9459Szrj {
788*a9fa9459Szrj if (posixly_correct)
789*a9fa9459Szrj /* 1003.2 specifies the format of this message. */
790*a9fa9459Szrj fprintf (stderr, _("%s: illegal option -- %c\n"),
791*a9fa9459Szrj argv[0], c);
792*a9fa9459Szrj else
793*a9fa9459Szrj fprintf (stderr, _("%s: invalid option -- %c\n"),
794*a9fa9459Szrj argv[0], c);
795*a9fa9459Szrj }
796*a9fa9459Szrj optopt = c;
797*a9fa9459Szrj return '?';
798*a9fa9459Szrj }
799*a9fa9459Szrj /* Convenience. Treat POSIX -W foo same as long option --foo */
800*a9fa9459Szrj if (temp[0] == 'W' && temp[1] == ';')
801*a9fa9459Szrj {
802*a9fa9459Szrj char *nameend;
803*a9fa9459Szrj const struct option *p;
804*a9fa9459Szrj const struct option *pfound = NULL;
805*a9fa9459Szrj int exact = 0;
806*a9fa9459Szrj int ambig = 0;
807*a9fa9459Szrj int indfound = 0;
808*a9fa9459Szrj int option_index;
809*a9fa9459Szrj
810*a9fa9459Szrj /* This is an option that requires an argument. */
811*a9fa9459Szrj if (*nextchar != '\0')
812*a9fa9459Szrj {
813*a9fa9459Szrj optarg = nextchar;
814*a9fa9459Szrj /* If we end this ARGV-element by taking the rest as an arg,
815*a9fa9459Szrj we must advance to the next element now. */
816*a9fa9459Szrj optind++;
817*a9fa9459Szrj }
818*a9fa9459Szrj else if (optind == argc)
819*a9fa9459Szrj {
820*a9fa9459Szrj if (opterr)
821*a9fa9459Szrj {
822*a9fa9459Szrj /* 1003.2 specifies the format of this message. */
823*a9fa9459Szrj fprintf (stderr, _("%s: option requires an argument -- %c\n"),
824*a9fa9459Szrj argv[0], c);
825*a9fa9459Szrj }
826*a9fa9459Szrj optopt = c;
827*a9fa9459Szrj if (optstring[0] == ':')
828*a9fa9459Szrj c = ':';
829*a9fa9459Szrj else
830*a9fa9459Szrj c = '?';
831*a9fa9459Szrj return c;
832*a9fa9459Szrj }
833*a9fa9459Szrj else
834*a9fa9459Szrj /* We already incremented `optind' once;
835*a9fa9459Szrj increment it again when taking next ARGV-elt as argument. */
836*a9fa9459Szrj optarg = argv[optind++];
837*a9fa9459Szrj
838*a9fa9459Szrj /* optarg is now the argument, see if it's in the
839*a9fa9459Szrj table of longopts. */
840*a9fa9459Szrj
841*a9fa9459Szrj for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
842*a9fa9459Szrj /* Do nothing. */ ;
843*a9fa9459Szrj
844*a9fa9459Szrj /* Test all long options for either exact match
845*a9fa9459Szrj or abbreviated matches. */
846*a9fa9459Szrj for (p = longopts, option_index = 0; p->name; p++, option_index++)
847*a9fa9459Szrj if (!strncmp (p->name, nextchar, nameend - nextchar))
848*a9fa9459Szrj {
849*a9fa9459Szrj if ((unsigned int) (nameend - nextchar) == strlen (p->name))
850*a9fa9459Szrj {
851*a9fa9459Szrj /* Exact match found. */
852*a9fa9459Szrj pfound = p;
853*a9fa9459Szrj indfound = option_index;
854*a9fa9459Szrj exact = 1;
855*a9fa9459Szrj break;
856*a9fa9459Szrj }
857*a9fa9459Szrj else if (pfound == NULL)
858*a9fa9459Szrj {
859*a9fa9459Szrj /* First nonexact match found. */
860*a9fa9459Szrj pfound = p;
861*a9fa9459Szrj indfound = option_index;
862*a9fa9459Szrj }
863*a9fa9459Szrj else
864*a9fa9459Szrj /* Second or later nonexact match found. */
865*a9fa9459Szrj ambig = 1;
866*a9fa9459Szrj }
867*a9fa9459Szrj if (ambig && !exact)
868*a9fa9459Szrj {
869*a9fa9459Szrj if (opterr)
870*a9fa9459Szrj fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
871*a9fa9459Szrj argv[0], argv[optind]);
872*a9fa9459Szrj nextchar += strlen (nextchar);
873*a9fa9459Szrj optind++;
874*a9fa9459Szrj return '?';
875*a9fa9459Szrj }
876*a9fa9459Szrj if (pfound != NULL)
877*a9fa9459Szrj {
878*a9fa9459Szrj option_index = indfound;
879*a9fa9459Szrj if (*nameend)
880*a9fa9459Szrj {
881*a9fa9459Szrj /* Don't test has_arg with >, because some C compilers don't
882*a9fa9459Szrj allow it to be used on enums. */
883*a9fa9459Szrj if (pfound->has_arg)
884*a9fa9459Szrj optarg = nameend + 1;
885*a9fa9459Szrj else
886*a9fa9459Szrj {
887*a9fa9459Szrj if (opterr)
888*a9fa9459Szrj fprintf (stderr, _("\
889*a9fa9459Szrj %s: option `-W %s' doesn't allow an argument\n"),
890*a9fa9459Szrj argv[0], pfound->name);
891*a9fa9459Szrj
892*a9fa9459Szrj nextchar += strlen (nextchar);
893*a9fa9459Szrj return '?';
894*a9fa9459Szrj }
895*a9fa9459Szrj }
896*a9fa9459Szrj else if (pfound->has_arg == 1)
897*a9fa9459Szrj {
898*a9fa9459Szrj if (optind < argc)
899*a9fa9459Szrj optarg = argv[optind++];
900*a9fa9459Szrj else
901*a9fa9459Szrj {
902*a9fa9459Szrj if (opterr)
903*a9fa9459Szrj fprintf (stderr,
904*a9fa9459Szrj _("%s: option `%s' requires an argument\n"),
905*a9fa9459Szrj argv[0], argv[optind - 1]);
906*a9fa9459Szrj nextchar += strlen (nextchar);
907*a9fa9459Szrj return optstring[0] == ':' ? ':' : '?';
908*a9fa9459Szrj }
909*a9fa9459Szrj }
910*a9fa9459Szrj nextchar += strlen (nextchar);
911*a9fa9459Szrj if (longind != NULL)
912*a9fa9459Szrj *longind = option_index;
913*a9fa9459Szrj if (pfound->flag)
914*a9fa9459Szrj {
915*a9fa9459Szrj *(pfound->flag) = pfound->val;
916*a9fa9459Szrj return 0;
917*a9fa9459Szrj }
918*a9fa9459Szrj return pfound->val;
919*a9fa9459Szrj }
920*a9fa9459Szrj nextchar = NULL;
921*a9fa9459Szrj return 'W'; /* Let the application handle it. */
922*a9fa9459Szrj }
923*a9fa9459Szrj if (temp[1] == ':')
924*a9fa9459Szrj {
925*a9fa9459Szrj if (temp[2] == ':')
926*a9fa9459Szrj {
927*a9fa9459Szrj /* This is an option that accepts an argument optionally. */
928*a9fa9459Szrj if (*nextchar != '\0')
929*a9fa9459Szrj {
930*a9fa9459Szrj optarg = nextchar;
931*a9fa9459Szrj optind++;
932*a9fa9459Szrj }
933*a9fa9459Szrj else
934*a9fa9459Szrj optarg = NULL;
935*a9fa9459Szrj nextchar = NULL;
936*a9fa9459Szrj }
937*a9fa9459Szrj else
938*a9fa9459Szrj {
939*a9fa9459Szrj /* This is an option that requires an argument. */
940*a9fa9459Szrj if (*nextchar != '\0')
941*a9fa9459Szrj {
942*a9fa9459Szrj optarg = nextchar;
943*a9fa9459Szrj /* If we end this ARGV-element by taking the rest as an arg,
944*a9fa9459Szrj we must advance to the next element now. */
945*a9fa9459Szrj optind++;
946*a9fa9459Szrj }
947*a9fa9459Szrj else if (optind == argc)
948*a9fa9459Szrj {
949*a9fa9459Szrj if (opterr)
950*a9fa9459Szrj {
951*a9fa9459Szrj /* 1003.2 specifies the format of this message. */
952*a9fa9459Szrj fprintf (stderr,
953*a9fa9459Szrj _("%s: option requires an argument -- %c\n"),
954*a9fa9459Szrj argv[0], c);
955*a9fa9459Szrj }
956*a9fa9459Szrj optopt = c;
957*a9fa9459Szrj if (optstring[0] == ':')
958*a9fa9459Szrj c = ':';
959*a9fa9459Szrj else
960*a9fa9459Szrj c = '?';
961*a9fa9459Szrj }
962*a9fa9459Szrj else
963*a9fa9459Szrj /* We already incremented `optind' once;
964*a9fa9459Szrj increment it again when taking next ARGV-elt as argument. */
965*a9fa9459Szrj optarg = argv[optind++];
966*a9fa9459Szrj nextchar = NULL;
967*a9fa9459Szrj }
968*a9fa9459Szrj }
969*a9fa9459Szrj return c;
970*a9fa9459Szrj }
971*a9fa9459Szrj }
972*a9fa9459Szrj
973*a9fa9459Szrj int
getopt(int argc,char * const * argv,const char * optstring)974*a9fa9459Szrj getopt (int argc, char *const *argv, const char *optstring)
975*a9fa9459Szrj {
976*a9fa9459Szrj return _getopt_internal (argc, argv, optstring,
977*a9fa9459Szrj (const struct option *) 0,
978*a9fa9459Szrj (int *) 0,
979*a9fa9459Szrj 0);
980*a9fa9459Szrj }
981*a9fa9459Szrj
982*a9fa9459Szrj #endif /* Not ELIDE_CODE. */
983*a9fa9459Szrj
984*a9fa9459Szrj #ifdef TEST
985*a9fa9459Szrj
986*a9fa9459Szrj /* Compile with -DTEST to make an executable for use in testing
987*a9fa9459Szrj the above definition of `getopt'. */
988*a9fa9459Szrj
989*a9fa9459Szrj int
main(int argc,char ** argv)990*a9fa9459Szrj main (int argc, char **argv)
991*a9fa9459Szrj {
992*a9fa9459Szrj int c;
993*a9fa9459Szrj int digit_optind = 0;
994*a9fa9459Szrj
995*a9fa9459Szrj while (1)
996*a9fa9459Szrj {
997*a9fa9459Szrj int this_option_optind = optind ? optind : 1;
998*a9fa9459Szrj
999*a9fa9459Szrj c = getopt (argc, argv, "abc:d:0123456789");
1000*a9fa9459Szrj if (c == -1)
1001*a9fa9459Szrj break;
1002*a9fa9459Szrj
1003*a9fa9459Szrj switch (c)
1004*a9fa9459Szrj {
1005*a9fa9459Szrj case '0':
1006*a9fa9459Szrj case '1':
1007*a9fa9459Szrj case '2':
1008*a9fa9459Szrj case '3':
1009*a9fa9459Szrj case '4':
1010*a9fa9459Szrj case '5':
1011*a9fa9459Szrj case '6':
1012*a9fa9459Szrj case '7':
1013*a9fa9459Szrj case '8':
1014*a9fa9459Szrj case '9':
1015*a9fa9459Szrj if (digit_optind != 0 && digit_optind != this_option_optind)
1016*a9fa9459Szrj printf ("digits occur in two different argv-elements.\n");
1017*a9fa9459Szrj digit_optind = this_option_optind;
1018*a9fa9459Szrj printf ("option %c\n", c);
1019*a9fa9459Szrj break;
1020*a9fa9459Szrj
1021*a9fa9459Szrj case 'a':
1022*a9fa9459Szrj printf ("option a\n");
1023*a9fa9459Szrj break;
1024*a9fa9459Szrj
1025*a9fa9459Szrj case 'b':
1026*a9fa9459Szrj printf ("option b\n");
1027*a9fa9459Szrj break;
1028*a9fa9459Szrj
1029*a9fa9459Szrj case 'c':
1030*a9fa9459Szrj printf ("option c with value `%s'\n", optarg);
1031*a9fa9459Szrj break;
1032*a9fa9459Szrj
1033*a9fa9459Szrj case '?':
1034*a9fa9459Szrj break;
1035*a9fa9459Szrj
1036*a9fa9459Szrj default:
1037*a9fa9459Szrj printf ("?? getopt returned character code 0%o ??\n", c);
1038*a9fa9459Szrj }
1039*a9fa9459Szrj }
1040*a9fa9459Szrj
1041*a9fa9459Szrj if (optind < argc)
1042*a9fa9459Szrj {
1043*a9fa9459Szrj printf ("non-option ARGV-elements: ");
1044*a9fa9459Szrj while (optind < argc)
1045*a9fa9459Szrj printf ("%s ", argv[optind++]);
1046*a9fa9459Szrj printf ("\n");
1047*a9fa9459Szrj }
1048*a9fa9459Szrj
1049*a9fa9459Szrj exit (0);
1050*a9fa9459Szrj }
1051*a9fa9459Szrj
1052*a9fa9459Szrj #endif /* TEST */
1053