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