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