xref: /aosp_15_r20/external/edid-decode/vs/getopt.c (revision 193032a37cc83cffc1526215991f3c21671f4245)
1*193032a3SAndroid Build Coastguard Worker /**
2*193032a3SAndroid Build Coastguard Worker  * @file getopt.c
3*193032a3SAndroid Build Coastguard Worker  * @copy 2012 MinGW.org project
4*193032a3SAndroid Build Coastguard Worker  *
5*193032a3SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
6*193032a3SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
7*193032a3SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
8*193032a3SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*193032a3SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
10*193032a3SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
11*193032a3SAndroid Build Coastguard Worker  *
12*193032a3SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
13*193032a3SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
14*193032a3SAndroid Build Coastguard Worker  * Software.
15*193032a3SAndroid Build Coastguard Worker  *
16*193032a3SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*193032a3SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*193032a3SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*193032a3SAndroid Build Coastguard Worker  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*193032a3SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21*193032a3SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22*193032a3SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
23*193032a3SAndroid Build Coastguard Worker  */
24*193032a3SAndroid Build Coastguard Worker 
25*193032a3SAndroid Build Coastguard Worker /*
26*193032a3SAndroid Build Coastguard Worker  * Implementation of the `getopt', `getopt_long' and `getopt_long_only'
27*193032a3SAndroid Build Coastguard Worker  * APIs, for inclusion in the MinGW runtime library.
28*193032a3SAndroid Build Coastguard Worker  */
29*193032a3SAndroid Build Coastguard Worker 
30*193032a3SAndroid Build Coastguard Worker #include <stdio.h>
31*193032a3SAndroid Build Coastguard Worker #include <stdlib.h>
32*193032a3SAndroid Build Coastguard Worker #include <stdarg.h>
33*193032a3SAndroid Build Coastguard Worker #include <getopt.h>
34*193032a3SAndroid Build Coastguard Worker 
35*193032a3SAndroid Build Coastguard Worker /* Identify how to get the calling program name, for use in messages...
36*193032a3SAndroid Build Coastguard Worker  */
37*193032a3SAndroid Build Coastguard Worker #ifdef __CYGWIN__
38*193032a3SAndroid Build Coastguard Worker /*
39*193032a3SAndroid Build Coastguard Worker  * CYGWIN uses this DLL reference...
40*193032a3SAndroid Build Coastguard Worker  */
41*193032a3SAndroid Build Coastguard Worker # define PROGNAME  __progname
42*193032a3SAndroid Build Coastguard Worker extern char __declspec(dllimport) *__progname;
43*193032a3SAndroid Build Coastguard Worker #else
44*193032a3SAndroid Build Coastguard Worker /*
45*193032a3SAndroid Build Coastguard Worker  * ...while elsewhere, we simply use the first argument passed.
46*193032a3SAndroid Build Coastguard Worker  */
47*193032a3SAndroid Build Coastguard Worker # define PROGNAME  *argv
48*193032a3SAndroid Build Coastguard Worker # define __inline__ __inline
49*193032a3SAndroid Build Coastguard Worker #endif
50*193032a3SAndroid Build Coastguard Worker 
51*193032a3SAndroid Build Coastguard Worker /* Initialise the public variables. */
52*193032a3SAndroid Build Coastguard Worker 
53*193032a3SAndroid Build Coastguard Worker int optind = 1;				/* index for first non-option arg     */
54*193032a3SAndroid Build Coastguard Worker int opterr = 1;				/* enable built-in error messages     */
55*193032a3SAndroid Build Coastguard Worker 
56*193032a3SAndroid Build Coastguard Worker char *optarg = NULL;			/* pointer to current option argument */
57*193032a3SAndroid Build Coastguard Worker 
58*193032a3SAndroid Build Coastguard Worker #define CHAR  char			/* argument type selector */
59*193032a3SAndroid Build Coastguard Worker 
60*193032a3SAndroid Build Coastguard Worker #define getopt_switchar         '-'	/* option prefix character in argv    */
61*193032a3SAndroid Build Coastguard Worker #define getopt_pluschar         '+'	/* prefix for POSIX mode in optstring */
62*193032a3SAndroid Build Coastguard Worker #define getopt_takes_argument   ':'	/* marker for optarg in optstring     */
63*193032a3SAndroid Build Coastguard Worker #define getopt_arg_assign       '='     /* longopt argument field separator   */
64*193032a3SAndroid Build Coastguard Worker #define getopt_unknown          '?'	/* return code for unmatched option   */
65*193032a3SAndroid Build Coastguard Worker #define getopt_ordered           1      /* return code for ordered non-option */
66*193032a3SAndroid Build Coastguard Worker 
67*193032a3SAndroid Build Coastguard Worker #define getopt_all_done         -1	/* return code to indicate completion */
68*193032a3SAndroid Build Coastguard Worker 
69*193032a3SAndroid Build Coastguard Worker enum
70*193032a3SAndroid Build Coastguard Worker { /* All `getopt' API functions are implemented via calls to the
71*193032a3SAndroid Build Coastguard Worker    * common static function `getopt_parse()'; these `mode' selectors
72*193032a3SAndroid Build Coastguard Worker    * determine the behaviour of `getopt_parse()', to deliver the
73*193032a3SAndroid Build Coastguard Worker    * appropriate result in each case.
74*193032a3SAndroid Build Coastguard Worker    */
75*193032a3SAndroid Build Coastguard Worker   getopt_mode_standard = 0,	/* getopt()	      */
76*193032a3SAndroid Build Coastguard Worker   getopt_mode_long,		/* getopt_long()      */
77*193032a3SAndroid Build Coastguard Worker   getopt_mode_long_only		/* getopt_long_only() */
78*193032a3SAndroid Build Coastguard Worker };
79*193032a3SAndroid Build Coastguard Worker 
80*193032a3SAndroid Build Coastguard Worker enum
81*193032a3SAndroid Build Coastguard Worker { /* When attempting to match a command line argument to a long form option,
82*193032a3SAndroid Build Coastguard Worker    * these indicate the status of the match.
83*193032a3SAndroid Build Coastguard Worker    */
84*193032a3SAndroid Build Coastguard Worker   getopt_no_match = 0,		/* no successful match			     */
85*193032a3SAndroid Build Coastguard Worker   getopt_abbreviated_match,	/* argument is an abbreviation for an option */
86*193032a3SAndroid Build Coastguard Worker   getopt_exact_match		/* argument matches the full option name     */
87*193032a3SAndroid Build Coastguard Worker };
88*193032a3SAndroid Build Coastguard Worker 
89*193032a3SAndroid Build Coastguard Worker int optopt = getopt_unknown;	/* return value for option being evaluated   */
90*193032a3SAndroid Build Coastguard Worker 
91*193032a3SAndroid Build Coastguard Worker /* Some BSD applications expect to be able to reinitialise `getopt' parsing
92*193032a3SAndroid Build Coastguard Worker  * by setting a global variable called `optreset'.  We provide an obfuscated
93*193032a3SAndroid Build Coastguard Worker  * API, which allows applications to emulate this brain damage; however, any
94*193032a3SAndroid Build Coastguard Worker  * use of this is non-portable, and is strongly discouraged.
95*193032a3SAndroid Build Coastguard Worker  */
96*193032a3SAndroid Build Coastguard Worker #define optreset  __mingw_optreset
97*193032a3SAndroid Build Coastguard Worker int optreset = 0;
98*193032a3SAndroid Build Coastguard Worker 
99*193032a3SAndroid Build Coastguard Worker static __inline__
getopt_missing_arg(const CHAR * optstring)100*193032a3SAndroid Build Coastguard Worker int getopt_missing_arg( const CHAR *optstring )
101*193032a3SAndroid Build Coastguard Worker {
102*193032a3SAndroid Build Coastguard Worker   /* Helper function to determine the appropriate return value,
103*193032a3SAndroid Build Coastguard Worker    * for the case where a required option argument is missing.
104*193032a3SAndroid Build Coastguard Worker    */
105*193032a3SAndroid Build Coastguard Worker   if( (*optstring == getopt_pluschar) || (*optstring == getopt_switchar) )
106*193032a3SAndroid Build Coastguard Worker     ++optstring;
107*193032a3SAndroid Build Coastguard Worker   return (*optstring == getopt_takes_argument)
108*193032a3SAndroid Build Coastguard Worker     ? getopt_takes_argument
109*193032a3SAndroid Build Coastguard Worker     : getopt_unknown;
110*193032a3SAndroid Build Coastguard Worker }
111*193032a3SAndroid Build Coastguard Worker 
112*193032a3SAndroid Build Coastguard Worker /* `complain' macro facilitates the generation of simple built-in
113*193032a3SAndroid Build Coastguard Worker  * error messages, displayed on various fault conditions, provided
114*193032a3SAndroid Build Coastguard Worker  * `opterr' is non-zero.
115*193032a3SAndroid Build Coastguard Worker  */
116*193032a3SAndroid Build Coastguard Worker #define	complain( MSG, ARG )  if( opterr ) \
117*193032a3SAndroid Build Coastguard Worker   fprintf( stderr, "%s: "MSG"\n", PROGNAME, ARG )
118*193032a3SAndroid Build Coastguard Worker 
119*193032a3SAndroid Build Coastguard Worker static __inline__
getopt_argerror(int mode,char * fmt,CHAR * prog,struct option * opt,int retval)120*193032a3SAndroid Build Coastguard Worker int getopt_argerror( int mode, char *fmt, CHAR *prog, struct option *opt, int retval )
121*193032a3SAndroid Build Coastguard Worker {
122*193032a3SAndroid Build Coastguard Worker   /* Helper function, to generate more complex built-in error
123*193032a3SAndroid Build Coastguard Worker    * messages, for invalid arguments to long form options ...
124*193032a3SAndroid Build Coastguard Worker    */
125*193032a3SAndroid Build Coastguard Worker   if( opterr )
126*193032a3SAndroid Build Coastguard Worker   {
127*193032a3SAndroid Build Coastguard Worker     /* ... but, displayed only if `opterr' is non-zero.
128*193032a3SAndroid Build Coastguard Worker      */
129*193032a3SAndroid Build Coastguard Worker     char flag[] = "--";
130*193032a3SAndroid Build Coastguard Worker     if( mode != getopt_mode_long )
131*193032a3SAndroid Build Coastguard Worker       /*
132*193032a3SAndroid Build Coastguard Worker        * only display one hyphen, for implicit long form options,
133*193032a3SAndroid Build Coastguard Worker        * improperly resolved by `getopt_long_only()'.
134*193032a3SAndroid Build Coastguard Worker        */
135*193032a3SAndroid Build Coastguard Worker       flag[1] = 0;
136*193032a3SAndroid Build Coastguard Worker     /*
137*193032a3SAndroid Build Coastguard Worker      * always preface the program name ...
138*193032a3SAndroid Build Coastguard Worker      */
139*193032a3SAndroid Build Coastguard Worker     fprintf( stderr, "%s: ", prog );
140*193032a3SAndroid Build Coastguard Worker     /*
141*193032a3SAndroid Build Coastguard Worker      * to the appropriate, option specific message.
142*193032a3SAndroid Build Coastguard Worker      */
143*193032a3SAndroid Build Coastguard Worker     fprintf( stderr, fmt, flag, opt->name );
144*193032a3SAndroid Build Coastguard Worker   }
145*193032a3SAndroid Build Coastguard Worker   /* Whether displaying the message, or not, always set `optopt'
146*193032a3SAndroid Build Coastguard Worker    * to identify the faulty option ...
147*193032a3SAndroid Build Coastguard Worker    */
148*193032a3SAndroid Build Coastguard Worker   optopt = opt->val;
149*193032a3SAndroid Build Coastguard Worker   /*
150*193032a3SAndroid Build Coastguard Worker    * and return the `invalid option' indicator.
151*193032a3SAndroid Build Coastguard Worker    */
152*193032a3SAndroid Build Coastguard Worker   return retval;
153*193032a3SAndroid Build Coastguard Worker }
154*193032a3SAndroid Build Coastguard Worker 
155*193032a3SAndroid Build Coastguard Worker /* `getopt_conventions' establish behavioural options, to control
156*193032a3SAndroid Build Coastguard Worker  * the operation of `getopt_parse()', e.g. to select between POSIX
157*193032a3SAndroid Build Coastguard Worker  * and GNU style argument parsing behaviour.
158*193032a3SAndroid Build Coastguard Worker  */
159*193032a3SAndroid Build Coastguard Worker #define getopt_set_conventions  0x1000
160*193032a3SAndroid Build Coastguard Worker #define getopt_posixly_correct  0x0010
161*193032a3SAndroid Build Coastguard Worker 
162*193032a3SAndroid Build Coastguard Worker static __inline__
getopt_conventions(int flags)163*193032a3SAndroid Build Coastguard Worker int getopt_conventions( int flags )
164*193032a3SAndroid Build Coastguard Worker {
165*193032a3SAndroid Build Coastguard Worker   static int conventions = 0;
166*193032a3SAndroid Build Coastguard Worker 
167*193032a3SAndroid Build Coastguard Worker   if( (conventions == 0) && ((flags & getopt_set_conventions) == 0) )
168*193032a3SAndroid Build Coastguard Worker   {
169*193032a3SAndroid Build Coastguard Worker     /* default conventions have not yet been established;
170*193032a3SAndroid Build Coastguard Worker      * initialise them now!
171*193032a3SAndroid Build Coastguard Worker      */
172*193032a3SAndroid Build Coastguard Worker     conventions = getopt_set_conventions;
173*193032a3SAndroid Build Coastguard Worker     if( (flags == getopt_pluschar) || (getenv( "POSIXLY_CORRECT" ) != NULL) )
174*193032a3SAndroid Build Coastguard Worker       conventions |= getopt_posixly_correct;
175*193032a3SAndroid Build Coastguard Worker   }
176*193032a3SAndroid Build Coastguard Worker 
177*193032a3SAndroid Build Coastguard Worker   else if( flags & getopt_set_conventions )
178*193032a3SAndroid Build Coastguard Worker     /*
179*193032a3SAndroid Build Coastguard Worker      * default conventions may have already been established,
180*193032a3SAndroid Build Coastguard Worker      * but this is a specific request to augment them.
181*193032a3SAndroid Build Coastguard Worker      */
182*193032a3SAndroid Build Coastguard Worker     conventions |= flags;
183*193032a3SAndroid Build Coastguard Worker 
184*193032a3SAndroid Build Coastguard Worker   /* in any event, return the currently established conventions.
185*193032a3SAndroid Build Coastguard Worker    */
186*193032a3SAndroid Build Coastguard Worker   return conventions;
187*193032a3SAndroid Build Coastguard Worker }
188*193032a3SAndroid Build Coastguard Worker 
189*193032a3SAndroid Build Coastguard Worker static __inline__
is_switchar(CHAR flag)190*193032a3SAndroid Build Coastguard Worker int is_switchar( CHAR flag )
191*193032a3SAndroid Build Coastguard Worker {
192*193032a3SAndroid Build Coastguard Worker   /* A simple helper function, used to identify the switch character
193*193032a3SAndroid Build Coastguard Worker    * introducing an optional command line argument.
194*193032a3SAndroid Build Coastguard Worker    */
195*193032a3SAndroid Build Coastguard Worker   return flag == getopt_switchar;
196*193032a3SAndroid Build Coastguard Worker }
197*193032a3SAndroid Build Coastguard Worker 
198*193032a3SAndroid Build Coastguard Worker static __inline__
getopt_match(CHAR lookup,const CHAR * opt_string)199*193032a3SAndroid Build Coastguard Worker const CHAR *getopt_match( CHAR lookup, const CHAR *opt_string )
200*193032a3SAndroid Build Coastguard Worker {
201*193032a3SAndroid Build Coastguard Worker   /* Helper function, used to identify short form options.
202*193032a3SAndroid Build Coastguard Worker    */
203*193032a3SAndroid Build Coastguard Worker   if( (*opt_string == getopt_pluschar) || (*opt_string == getopt_switchar) )
204*193032a3SAndroid Build Coastguard Worker     ++opt_string;
205*193032a3SAndroid Build Coastguard Worker   if( *opt_string == getopt_takes_argument )
206*193032a3SAndroid Build Coastguard Worker     ++opt_string;
207*193032a3SAndroid Build Coastguard Worker   do if( lookup == *opt_string ) return opt_string;
208*193032a3SAndroid Build Coastguard Worker      while( *++opt_string );
209*193032a3SAndroid Build Coastguard Worker   return NULL;
210*193032a3SAndroid Build Coastguard Worker }
211*193032a3SAndroid Build Coastguard Worker 
212*193032a3SAndroid Build Coastguard Worker static __inline__
getopt_match_long(const CHAR * nextchar,const CHAR * optname)213*193032a3SAndroid Build Coastguard Worker int getopt_match_long( const CHAR *nextchar, const CHAR *optname )
214*193032a3SAndroid Build Coastguard Worker {
215*193032a3SAndroid Build Coastguard Worker   /* Helper function, used to identify potential matches for
216*193032a3SAndroid Build Coastguard Worker    * long form options.
217*193032a3SAndroid Build Coastguard Worker    */
218*193032a3SAndroid Build Coastguard Worker   CHAR matchchar;
219*193032a3SAndroid Build Coastguard Worker   while( (matchchar = *nextchar++) && (matchchar == *optname) )
220*193032a3SAndroid Build Coastguard Worker     /*
221*193032a3SAndroid Build Coastguard Worker      * skip over initial substring which DOES match.
222*193032a3SAndroid Build Coastguard Worker      */
223*193032a3SAndroid Build Coastguard Worker     ++optname;
224*193032a3SAndroid Build Coastguard Worker 
225*193032a3SAndroid Build Coastguard Worker   if( matchchar )
226*193032a3SAndroid Build Coastguard Worker   {
227*193032a3SAndroid Build Coastguard Worker     /* did NOT match the entire argument to an initial substring
228*193032a3SAndroid Build Coastguard Worker      * of a defined option name ...
229*193032a3SAndroid Build Coastguard Worker      */
230*193032a3SAndroid Build Coastguard Worker     if( matchchar != getopt_arg_assign )
231*193032a3SAndroid Build Coastguard Worker       /*
232*193032a3SAndroid Build Coastguard Worker        * ... and didn't stop at an `=' internal field separator,
233*193032a3SAndroid Build Coastguard Worker        * so this is NOT a possible match.
234*193032a3SAndroid Build Coastguard Worker        */
235*193032a3SAndroid Build Coastguard Worker       return getopt_no_match;
236*193032a3SAndroid Build Coastguard Worker 
237*193032a3SAndroid Build Coastguard Worker     /* DID stop at an `=' internal field separator,
238*193032a3SAndroid Build Coastguard Worker      * so this IS a possible match, and what follows is an
239*193032a3SAndroid Build Coastguard Worker      * argument to the possibly matched option.
240*193032a3SAndroid Build Coastguard Worker      */
241*193032a3SAndroid Build Coastguard Worker     optarg = (char *)(nextchar);
242*193032a3SAndroid Build Coastguard Worker   }
243*193032a3SAndroid Build Coastguard Worker   return *optname
244*193032a3SAndroid Build Coastguard Worker     /*
245*193032a3SAndroid Build Coastguard Worker      * if we DIDN'T match the ENTIRE text of the option name,
246*193032a3SAndroid Build Coastguard Worker      * then it's a possible abbreviated match ...
247*193032a3SAndroid Build Coastguard Worker      */
248*193032a3SAndroid Build Coastguard Worker     ? getopt_abbreviated_match
249*193032a3SAndroid Build Coastguard Worker     /*
250*193032a3SAndroid Build Coastguard Worker      * but if we DID match the entire option name,
251*193032a3SAndroid Build Coastguard Worker      * then it's a DEFINITE EXACT match.
252*193032a3SAndroid Build Coastguard Worker      */
253*193032a3SAndroid Build Coastguard Worker     : getopt_exact_match;
254*193032a3SAndroid Build Coastguard Worker }
255*193032a3SAndroid Build Coastguard Worker 
256*193032a3SAndroid Build Coastguard Worker static __inline__
getopt_resolved(int mode,int argc,CHAR * const * argv,int * argind,struct option * opt,int index,int * retindex,const CHAR * optstring)257*193032a3SAndroid Build Coastguard Worker int getopt_resolved( int mode, int argc, CHAR *const *argv, int *argind,
258*193032a3SAndroid Build Coastguard Worker struct option *opt, int index, int *retindex, const CHAR *optstring )
259*193032a3SAndroid Build Coastguard Worker {
260*193032a3SAndroid Build Coastguard Worker   /* Helper function to establish appropriate return conditions,
261*193032a3SAndroid Build Coastguard Worker    * on resolution of a long form option.
262*193032a3SAndroid Build Coastguard Worker    */
263*193032a3SAndroid Build Coastguard Worker   if( retindex != NULL )
264*193032a3SAndroid Build Coastguard Worker     *retindex = index;
265*193032a3SAndroid Build Coastguard Worker 
266*193032a3SAndroid Build Coastguard Worker   /* On return, `optind' should normally refer to the argument, if any,
267*193032a3SAndroid Build Coastguard Worker    * which follows the current one; it is convenient to set this, before
268*193032a3SAndroid Build Coastguard Worker    * checking for the presence of any `optarg'.
269*193032a3SAndroid Build Coastguard Worker    */
270*193032a3SAndroid Build Coastguard Worker   optind = *argind + 1;
271*193032a3SAndroid Build Coastguard Worker 
272*193032a3SAndroid Build Coastguard Worker   if( optarg && (opt[index].has_arg == no_argument) )
273*193032a3SAndroid Build Coastguard Worker     /*
274*193032a3SAndroid Build Coastguard Worker      * it is an error for the user to specify an option specific argument
275*193032a3SAndroid Build Coastguard Worker      * with an option which doesn't expect one!
276*193032a3SAndroid Build Coastguard Worker      */
277*193032a3SAndroid Build Coastguard Worker     return getopt_argerror( mode, "option `%s%s' doesn't accept an argument\n",
278*193032a3SAndroid Build Coastguard Worker 	PROGNAME, opt + index, getopt_unknown );
279*193032a3SAndroid Build Coastguard Worker 
280*193032a3SAndroid Build Coastguard Worker   else if( (optarg == NULL) && (opt[index].has_arg == required_argument) )
281*193032a3SAndroid Build Coastguard Worker   {
282*193032a3SAndroid Build Coastguard Worker     /* similarly, it is an error if no argument is specified
283*193032a3SAndroid Build Coastguard Worker      * with an option which requires one ...
284*193032a3SAndroid Build Coastguard Worker      */
285*193032a3SAndroid Build Coastguard Worker     if( optind < argc )
286*193032a3SAndroid Build Coastguard Worker       /*
287*193032a3SAndroid Build Coastguard Worker        * ... except that the requirement may be satisfied from
288*193032a3SAndroid Build Coastguard Worker        * the following command line argument, if any ...
289*193032a3SAndroid Build Coastguard Worker        */
290*193032a3SAndroid Build Coastguard Worker       optarg = argv[*argind = optind++];
291*193032a3SAndroid Build Coastguard Worker 
292*193032a3SAndroid Build Coastguard Worker     else
293*193032a3SAndroid Build Coastguard Worker       /* so fail this case, only if no such argument exists!
294*193032a3SAndroid Build Coastguard Worker        */
295*193032a3SAndroid Build Coastguard Worker       return getopt_argerror( mode, "option `%s%s' requires an argument\n",
296*193032a3SAndroid Build Coastguard Worker 	  PROGNAME, opt + index, getopt_missing_arg( optstring ) );
297*193032a3SAndroid Build Coastguard Worker   }
298*193032a3SAndroid Build Coastguard Worker 
299*193032a3SAndroid Build Coastguard Worker   /* when the caller has provided a return buffer ...
300*193032a3SAndroid Build Coastguard Worker    */
301*193032a3SAndroid Build Coastguard Worker   if( opt[index].flag != NULL )
302*193032a3SAndroid Build Coastguard Worker   {
303*193032a3SAndroid Build Coastguard Worker     /* ... then we place the proper return value there,
304*193032a3SAndroid Build Coastguard Worker      * and return a status code of zero ...
305*193032a3SAndroid Build Coastguard Worker      */
306*193032a3SAndroid Build Coastguard Worker     *(opt[index].flag) = opt[index].val;
307*193032a3SAndroid Build Coastguard Worker     return 0;
308*193032a3SAndroid Build Coastguard Worker   }
309*193032a3SAndroid Build Coastguard Worker   /* ... otherwise, the return value becomes the status code.
310*193032a3SAndroid Build Coastguard Worker    */
311*193032a3SAndroid Build Coastguard Worker   return opt[index].val;
312*193032a3SAndroid Build Coastguard Worker }
313*193032a3SAndroid Build Coastguard Worker 
314*193032a3SAndroid Build Coastguard Worker static __inline__
getopt_verify(const CHAR * nextchar,const CHAR * optstring)315*193032a3SAndroid Build Coastguard Worker int getopt_verify( const CHAR *nextchar, const CHAR *optstring )
316*193032a3SAndroid Build Coastguard Worker {
317*193032a3SAndroid Build Coastguard Worker   /* Helper function, called by getopt_parse() when invoked
318*193032a3SAndroid Build Coastguard Worker    * by getopt_long_only(), to verify when an unmatched or an
319*193032a3SAndroid Build Coastguard Worker    * ambiguously matched long form option string is valid as
320*193032a3SAndroid Build Coastguard Worker    * a short form option specification.
321*193032a3SAndroid Build Coastguard Worker    */
322*193032a3SAndroid Build Coastguard Worker   if( ! (nextchar && *nextchar && optstring && *optstring) )
323*193032a3SAndroid Build Coastguard Worker     /*
324*193032a3SAndroid Build Coastguard Worker      * There are no characters to be matched, or there are no
325*193032a3SAndroid Build Coastguard Worker      * valid short form option characters to which they can be
326*193032a3SAndroid Build Coastguard Worker      * matched, so this can never be valid.
327*193032a3SAndroid Build Coastguard Worker      */
328*193032a3SAndroid Build Coastguard Worker     return 0;
329*193032a3SAndroid Build Coastguard Worker 
330*193032a3SAndroid Build Coastguard Worker   while( *nextchar )
331*193032a3SAndroid Build Coastguard Worker   {
332*193032a3SAndroid Build Coastguard Worker     /* For each command line character in turn ...
333*193032a3SAndroid Build Coastguard Worker      */
334*193032a3SAndroid Build Coastguard Worker     const CHAR *test;
335*193032a3SAndroid Build Coastguard Worker     if( (test = getopt_match( *nextchar++, optstring )) == NULL )
336*193032a3SAndroid Build Coastguard Worker       /*
337*193032a3SAndroid Build Coastguard Worker        * ... there is no short form option to match the current
338*193032a3SAndroid Build Coastguard Worker        * candidate, so the entire argument fails.
339*193032a3SAndroid Build Coastguard Worker        */
340*193032a3SAndroid Build Coastguard Worker       return 0;
341*193032a3SAndroid Build Coastguard Worker 
342*193032a3SAndroid Build Coastguard Worker     if( test[1] == getopt_takes_argument )
343*193032a3SAndroid Build Coastguard Worker       /*
344*193032a3SAndroid Build Coastguard Worker        * The current candidate is valid, and it matches an option
345*193032a3SAndroid Build Coastguard Worker        * which takes an argument, so this command line argument is
346*193032a3SAndroid Build Coastguard Worker        * a valid short form option specification; accept it.
347*193032a3SAndroid Build Coastguard Worker        */
348*193032a3SAndroid Build Coastguard Worker       return 1;
349*193032a3SAndroid Build Coastguard Worker   }
350*193032a3SAndroid Build Coastguard Worker   /* If we get to here, then every character in the command line
351*193032a3SAndroid Build Coastguard Worker    * argument was valid as a short form option; accept it.
352*193032a3SAndroid Build Coastguard Worker    */
353*193032a3SAndroid Build Coastguard Worker   return 1;
354*193032a3SAndroid Build Coastguard Worker }
355*193032a3SAndroid Build Coastguard Worker 
356*193032a3SAndroid Build Coastguard Worker static
357*193032a3SAndroid Build Coastguard Worker #define getopt_std_args int argc, CHAR *const argv[], const CHAR *optstring
getopt_parse(int mode,getopt_std_args,...)358*193032a3SAndroid Build Coastguard Worker int getopt_parse( int mode, getopt_std_args, ... )
359*193032a3SAndroid Build Coastguard Worker {
360*193032a3SAndroid Build Coastguard Worker   /* Common core implementation for ALL `getopt' functions.
361*193032a3SAndroid Build Coastguard Worker    */
362*193032a3SAndroid Build Coastguard Worker   static int argind = 0;
363*193032a3SAndroid Build Coastguard Worker   static int optbase = 0;
364*193032a3SAndroid Build Coastguard Worker   static const CHAR *nextchar = NULL;
365*193032a3SAndroid Build Coastguard Worker   static int optmark = 0;
366*193032a3SAndroid Build Coastguard Worker 
367*193032a3SAndroid Build Coastguard Worker   if( (optreset |= (optind < 1)) || (optind < optbase) )
368*193032a3SAndroid Build Coastguard Worker   {
369*193032a3SAndroid Build Coastguard Worker     /* POSIX does not prescribe any definitive mechanism for restarting
370*193032a3SAndroid Build Coastguard Worker      * a `getopt' scan, but some applications may require such capability.
371*193032a3SAndroid Build Coastguard Worker      * We will support it, by allowing the caller to adjust the value of
372*193032a3SAndroid Build Coastguard Worker      * `optind' downwards, (nominally setting it to zero).  Since POSIX
373*193032a3SAndroid Build Coastguard Worker      * wants `optind' to have an initial value of one, but we want all
374*193032a3SAndroid Build Coastguard Worker      * of our internal place holders to be initialised to zero, when we
375*193032a3SAndroid Build Coastguard Worker      * are called for the first time, we will handle such a reset by
376*193032a3SAndroid Build Coastguard Worker      * adjusting all of the internal place holders to one less than
377*193032a3SAndroid Build Coastguard Worker      * the adjusted `optind' value, (but never to less than zero).
378*193032a3SAndroid Build Coastguard Worker      */
379*193032a3SAndroid Build Coastguard Worker     if( optreset )
380*193032a3SAndroid Build Coastguard Worker     {
381*193032a3SAndroid Build Coastguard Worker       /* User has explicitly requested reinitialisation...
382*193032a3SAndroid Build Coastguard Worker        * We need to reset `optind' to it's normal initial value of 1,
383*193032a3SAndroid Build Coastguard Worker        * to avoid a potential infinitely recursive loop; by doing this
384*193032a3SAndroid Build Coastguard Worker        * up front, we also ensure that the remaining place holders
385*193032a3SAndroid Build Coastguard Worker        * will be correctly reinitialised to no less than zero.
386*193032a3SAndroid Build Coastguard Worker        */
387*193032a3SAndroid Build Coastguard Worker       optind = 1;
388*193032a3SAndroid Build Coastguard Worker 
389*193032a3SAndroid Build Coastguard Worker       /* We also need to clear the `optreset' request...
390*193032a3SAndroid Build Coastguard Worker        */
391*193032a3SAndroid Build Coastguard Worker       optreset = 0;
392*193032a3SAndroid Build Coastguard Worker     }
393*193032a3SAndroid Build Coastguard Worker 
394*193032a3SAndroid Build Coastguard Worker     /* Now, we may safely reinitialise the internal place holders, to
395*193032a3SAndroid Build Coastguard Worker      * one less than `optind', without fear of making them negative.
396*193032a3SAndroid Build Coastguard Worker      */
397*193032a3SAndroid Build Coastguard Worker     optmark = optbase = argind = optind - 1;
398*193032a3SAndroid Build Coastguard Worker     nextchar = NULL;
399*193032a3SAndroid Build Coastguard Worker   }
400*193032a3SAndroid Build Coastguard Worker 
401*193032a3SAndroid Build Coastguard Worker   /* From a POSIX perspective, the following is `undefined behaviour';
402*193032a3SAndroid Build Coastguard Worker    * we implement it thus, for compatibility with GNU and BSD getopt.
403*193032a3SAndroid Build Coastguard Worker    */
404*193032a3SAndroid Build Coastguard Worker   else if( optind > (argind + 1) )
405*193032a3SAndroid Build Coastguard Worker   {
406*193032a3SAndroid Build Coastguard Worker     /* Some applications expect to be able to manipulate `optind',
407*193032a3SAndroid Build Coastguard Worker      * causing `getopt' to skip over one or more elements of `argv';
408*193032a3SAndroid Build Coastguard Worker      * POSIX doesn't require us to support this brain-damaged concept;
409*193032a3SAndroid Build Coastguard Worker      * (indeed, POSIX defines no particular behaviour, in the event of
410*193032a3SAndroid Build Coastguard Worker      *  such usage, so it must be considered a bug for an application
411*193032a3SAndroid Build Coastguard Worker      *  to rely on any particular outcome); nonetheless, Mac-OS-X and
412*193032a3SAndroid Build Coastguard Worker      * BSD actually provide *documented* support for this capability,
413*193032a3SAndroid Build Coastguard Worker      * so we ensure that our internal place holders keep track of
414*193032a3SAndroid Build Coastguard Worker      * external `optind' increments; (`argind' must lag by one).
415*193032a3SAndroid Build Coastguard Worker      */
416*193032a3SAndroid Build Coastguard Worker     argind = optind - 1;
417*193032a3SAndroid Build Coastguard Worker 
418*193032a3SAndroid Build Coastguard Worker     /* When `optind' is misused, in this fashion, we also abandon any
419*193032a3SAndroid Build Coastguard Worker      * residual text in the argument we had been parsing; this is done
420*193032a3SAndroid Build Coastguard Worker      * without any further processing of such abandoned text, assuming
421*193032a3SAndroid Build Coastguard Worker      * that the caller is equipped to handle it appropriately.
422*193032a3SAndroid Build Coastguard Worker      */
423*193032a3SAndroid Build Coastguard Worker     nextchar = NULL;
424*193032a3SAndroid Build Coastguard Worker   }
425*193032a3SAndroid Build Coastguard Worker 
426*193032a3SAndroid Build Coastguard Worker   if( nextchar && *nextchar )
427*193032a3SAndroid Build Coastguard Worker   {
428*193032a3SAndroid Build Coastguard Worker     /* we are parsing a standard, or short format, option argument ...
429*193032a3SAndroid Build Coastguard Worker      */
430*193032a3SAndroid Build Coastguard Worker     const CHAR *optchar;
431*193032a3SAndroid Build Coastguard Worker     if( (optchar = getopt_match( optopt = *nextchar++, optstring )) != NULL )
432*193032a3SAndroid Build Coastguard Worker     {
433*193032a3SAndroid Build Coastguard Worker       /* we have identified it as valid ...
434*193032a3SAndroid Build Coastguard Worker        */
435*193032a3SAndroid Build Coastguard Worker       if( optchar[1] == getopt_takes_argument )
436*193032a3SAndroid Build Coastguard Worker       {
437*193032a3SAndroid Build Coastguard Worker 	/* and determined that it requires an associated argument ...
438*193032a3SAndroid Build Coastguard Worker 	 */
439*193032a3SAndroid Build Coastguard Worker 	if( ! *(optarg = (char *)(nextchar)) )
440*193032a3SAndroid Build Coastguard Worker 	{
441*193032a3SAndroid Build Coastguard Worker 	  /* the argument is NOT attached ...
442*193032a3SAndroid Build Coastguard Worker 	   */
443*193032a3SAndroid Build Coastguard Worker 	  if( optchar[2] == getopt_takes_argument )
444*193032a3SAndroid Build Coastguard Worker 	    /*
445*193032a3SAndroid Build Coastguard Worker 	     * but this GNU extension marks it as optional,
446*193032a3SAndroid Build Coastguard Worker 	     * so we don't provide one on this occasion.
447*193032a3SAndroid Build Coastguard Worker 	     */
448*193032a3SAndroid Build Coastguard Worker 	    optarg = NULL;
449*193032a3SAndroid Build Coastguard Worker 
450*193032a3SAndroid Build Coastguard Worker 	  /* otherwise this option takes a mandatory argument,
451*193032a3SAndroid Build Coastguard Worker 	   * so, provided there is one available ...
452*193032a3SAndroid Build Coastguard Worker 	   */
453*193032a3SAndroid Build Coastguard Worker 	  else if( (argc - argind) > 1 )
454*193032a3SAndroid Build Coastguard Worker 	    /*
455*193032a3SAndroid Build Coastguard Worker 	     * we take the following command line argument,
456*193032a3SAndroid Build Coastguard Worker 	     * as the appropriate option argument.
457*193032a3SAndroid Build Coastguard Worker 	     */
458*193032a3SAndroid Build Coastguard Worker 	    optarg = argv[++argind];
459*193032a3SAndroid Build Coastguard Worker 
460*193032a3SAndroid Build Coastguard Worker 	  /* but if no further argument is available,
461*193032a3SAndroid Build Coastguard Worker 	   * then there is nothing we can do, except for
462*193032a3SAndroid Build Coastguard Worker 	   * issuing the requisite diagnostic message.
463*193032a3SAndroid Build Coastguard Worker 	   */
464*193032a3SAndroid Build Coastguard Worker 	  else
465*193032a3SAndroid Build Coastguard Worker 	  {
466*193032a3SAndroid Build Coastguard Worker 	    complain( "option requires an argument -- %c", optopt );
467*193032a3SAndroid Build Coastguard Worker 	    return getopt_missing_arg( optstring );
468*193032a3SAndroid Build Coastguard Worker 	  }
469*193032a3SAndroid Build Coastguard Worker 	}
470*193032a3SAndroid Build Coastguard Worker 	optind = argind + 1;
471*193032a3SAndroid Build Coastguard Worker 	nextchar = NULL;
472*193032a3SAndroid Build Coastguard Worker       }
473*193032a3SAndroid Build Coastguard Worker       else
474*193032a3SAndroid Build Coastguard Worker 	optarg = NULL;
475*193032a3SAndroid Build Coastguard Worker       optind = (nextchar && *nextchar) ? argind : argind + 1;
476*193032a3SAndroid Build Coastguard Worker       return optopt;
477*193032a3SAndroid Build Coastguard Worker     }
478*193032a3SAndroid Build Coastguard Worker     /* if we didn't find a valid match for the specified option character,
479*193032a3SAndroid Build Coastguard Worker      * then we fall through to here, so take appropriate diagnostic action.
480*193032a3SAndroid Build Coastguard Worker      */
481*193032a3SAndroid Build Coastguard Worker     if( mode == getopt_mode_long_only )
482*193032a3SAndroid Build Coastguard Worker     {
483*193032a3SAndroid Build Coastguard Worker       complain( "unrecognised option `-%s'", --nextchar );
484*193032a3SAndroid Build Coastguard Worker       nextchar = NULL;
485*193032a3SAndroid Build Coastguard Worker       optopt = 0;
486*193032a3SAndroid Build Coastguard Worker     }
487*193032a3SAndroid Build Coastguard Worker     else
488*193032a3SAndroid Build Coastguard Worker       complain( "invalid option -- %c", optopt );
489*193032a3SAndroid Build Coastguard Worker     optind = (nextchar && *nextchar) ? argind : argind + 1;
490*193032a3SAndroid Build Coastguard Worker     return getopt_unknown;
491*193032a3SAndroid Build Coastguard Worker   }
492*193032a3SAndroid Build Coastguard Worker 
493*193032a3SAndroid Build Coastguard Worker   if( optmark > optbase )
494*193032a3SAndroid Build Coastguard Worker   {
495*193032a3SAndroid Build Coastguard Worker     /* This can happen, in GNU parsing mode ONLY, when we have
496*193032a3SAndroid Build Coastguard Worker      * skipped over non-option arguments, and found a subsequent
497*193032a3SAndroid Build Coastguard Worker      * option argument; in this case we permute the arguments.
498*193032a3SAndroid Build Coastguard Worker      */
499*193032a3SAndroid Build Coastguard Worker     int index;
500*193032a3SAndroid Build Coastguard Worker     /*
501*193032a3SAndroid Build Coastguard Worker      * `optspan' specifies the number of contiguous arguments
502*193032a3SAndroid Build Coastguard Worker      * which are spanned by the current option, and so must be
503*193032a3SAndroid Build Coastguard Worker      * moved together during permutation.
504*193032a3SAndroid Build Coastguard Worker      */
505*193032a3SAndroid Build Coastguard Worker     int optspan = argind - optmark + 1;
506*193032a3SAndroid Build Coastguard Worker     /*
507*193032a3SAndroid Build Coastguard Worker      * we use `this_arg' to store these temporarily.
508*193032a3SAndroid Build Coastguard Worker      */
509*193032a3SAndroid Build Coastguard Worker     CHAR **this_arg = (CHAR **)malloc(optspan * sizeof(CHAR *));
510*193032a3SAndroid Build Coastguard Worker     if( this_arg == NULL )
511*193032a3SAndroid Build Coastguard Worker       return getopt_unknown;
512*193032a3SAndroid Build Coastguard Worker     /*
513*193032a3SAndroid Build Coastguard Worker      * we cannot manipulate `argv' directly, since the `getopt'
514*193032a3SAndroid Build Coastguard Worker      * API prototypes it as `read-only'; this cast to `arglist'
515*193032a3SAndroid Build Coastguard Worker      * allows us to work around that restriction.
516*193032a3SAndroid Build Coastguard Worker      */
517*193032a3SAndroid Build Coastguard Worker     CHAR **arglist = (char **)(argv);
518*193032a3SAndroid Build Coastguard Worker 
519*193032a3SAndroid Build Coastguard Worker     /* save temporary copies of the arguments which are associated
520*193032a3SAndroid Build Coastguard Worker      * with the current option ...
521*193032a3SAndroid Build Coastguard Worker      */
522*193032a3SAndroid Build Coastguard Worker     for( index = 0; index < optspan; ++index )
523*193032a3SAndroid Build Coastguard Worker       this_arg[index] = arglist[optmark + index];
524*193032a3SAndroid Build Coastguard Worker 
525*193032a3SAndroid Build Coastguard Worker     /* move all preceding non-option arguments to the right,
526*193032a3SAndroid Build Coastguard Worker      * overwriting these saved arguments, while making space
527*193032a3SAndroid Build Coastguard Worker      * to replace them in their permuted location.
528*193032a3SAndroid Build Coastguard Worker      */
529*193032a3SAndroid Build Coastguard Worker     for( --optmark; optmark >= optbase; --optmark )
530*193032a3SAndroid Build Coastguard Worker       arglist[optmark + optspan] = arglist[optmark];
531*193032a3SAndroid Build Coastguard Worker 
532*193032a3SAndroid Build Coastguard Worker     /* restore the temporarily saved option arguments to
533*193032a3SAndroid Build Coastguard Worker      * their permuted location.
534*193032a3SAndroid Build Coastguard Worker      */
535*193032a3SAndroid Build Coastguard Worker     for( index = 0; index < optspan; ++index )
536*193032a3SAndroid Build Coastguard Worker       arglist[optbase + index] = this_arg[index];
537*193032a3SAndroid Build Coastguard Worker 
538*193032a3SAndroid Build Coastguard Worker     free(this_arg);
539*193032a3SAndroid Build Coastguard Worker 
540*193032a3SAndroid Build Coastguard Worker     /* adjust `optbase', to account for the relocated option.
541*193032a3SAndroid Build Coastguard Worker      */
542*193032a3SAndroid Build Coastguard Worker     optbase += optspan;
543*193032a3SAndroid Build Coastguard Worker   }
544*193032a3SAndroid Build Coastguard Worker 
545*193032a3SAndroid Build Coastguard Worker   else
546*193032a3SAndroid Build Coastguard Worker     /* no permutation occurred ...
547*193032a3SAndroid Build Coastguard Worker      * simply adjust `optbase' for all options parsed so far.
548*193032a3SAndroid Build Coastguard Worker      */
549*193032a3SAndroid Build Coastguard Worker     optbase = argind + 1;
550*193032a3SAndroid Build Coastguard Worker 
551*193032a3SAndroid Build Coastguard Worker   /* enter main parsing loop ...
552*193032a3SAndroid Build Coastguard Worker    */
553*193032a3SAndroid Build Coastguard Worker   while( argc > ++argind )
554*193032a3SAndroid Build Coastguard Worker   {
555*193032a3SAndroid Build Coastguard Worker     /* inspect each argument in turn, identifying possible options ...
556*193032a3SAndroid Build Coastguard Worker      */
557*193032a3SAndroid Build Coastguard Worker     if( is_switchar( *(nextchar = argv[optmark = argind]) ) && *++nextchar )
558*193032a3SAndroid Build Coastguard Worker     {
559*193032a3SAndroid Build Coastguard Worker       /* we've found a candidate option argument ... */
560*193032a3SAndroid Build Coastguard Worker 
561*193032a3SAndroid Build Coastguard Worker       if( is_switchar( *nextchar ) )
562*193032a3SAndroid Build Coastguard Worker       {
563*193032a3SAndroid Build Coastguard Worker 	/* it's a double hyphen argument ... */
564*193032a3SAndroid Build Coastguard Worker 
565*193032a3SAndroid Build Coastguard Worker 	const CHAR *refchar = nextchar;
566*193032a3SAndroid Build Coastguard Worker 	if( *++refchar )
567*193032a3SAndroid Build Coastguard Worker 	{
568*193032a3SAndroid Build Coastguard Worker 	  /* and it looks like a long format option ...
569*193032a3SAndroid Build Coastguard Worker 	   * `getopt_long' mode must be active to accept it as such,
570*193032a3SAndroid Build Coastguard Worker 	   * `getopt_long_only' also qualifies, but we must downgrade
571*193032a3SAndroid Build Coastguard Worker 	   * it to force explicit handling as a long format option.
572*193032a3SAndroid Build Coastguard Worker 	   */
573*193032a3SAndroid Build Coastguard Worker 	  if( mode >= getopt_mode_long )
574*193032a3SAndroid Build Coastguard Worker 	  {
575*193032a3SAndroid Build Coastguard Worker 	    nextchar = refchar;
576*193032a3SAndroid Build Coastguard Worker 	    mode = getopt_mode_long;
577*193032a3SAndroid Build Coastguard Worker 	  }
578*193032a3SAndroid Build Coastguard Worker 	}
579*193032a3SAndroid Build Coastguard Worker 	else
580*193032a3SAndroid Build Coastguard Worker 	{
581*193032a3SAndroid Build Coastguard Worker 	  /* this is an explicit `--' end of options marker, so wrap up now!
582*193032a3SAndroid Build Coastguard Worker 	   */
583*193032a3SAndroid Build Coastguard Worker 	  if( optmark > optbase )
584*193032a3SAndroid Build Coastguard Worker 	  {
585*193032a3SAndroid Build Coastguard Worker 	    /* permuting the argument list as necessary ...
586*193032a3SAndroid Build Coastguard Worker 	     * (note use of `this_arg' and `arglist', as above).
587*193032a3SAndroid Build Coastguard Worker 	     */
588*193032a3SAndroid Build Coastguard Worker 	    CHAR *this_arg = argv[optmark];
589*193032a3SAndroid Build Coastguard Worker 	    CHAR **arglist = (CHAR **)(argv);
590*193032a3SAndroid Build Coastguard Worker 
591*193032a3SAndroid Build Coastguard Worker 	    /* move all preceding non-option arguments to the right ...
592*193032a3SAndroid Build Coastguard Worker 	     */
593*193032a3SAndroid Build Coastguard Worker 	    do arglist[optmark] = arglist[optmark - 1];
594*193032a3SAndroid Build Coastguard Worker 	       while( optmark-- > optbase );
595*193032a3SAndroid Build Coastguard Worker 
596*193032a3SAndroid Build Coastguard Worker 	    /* reinstate the `--' marker, in its permuted location.
597*193032a3SAndroid Build Coastguard Worker 	     */
598*193032a3SAndroid Build Coastguard Worker 	    arglist[optbase] = this_arg;
599*193032a3SAndroid Build Coastguard Worker 	  }
600*193032a3SAndroid Build Coastguard Worker 	  /* ... before finally bumping `optbase' past the `--' marker,
601*193032a3SAndroid Build Coastguard Worker 	   * and returning the `all done' completion indicator.
602*193032a3SAndroid Build Coastguard Worker 	   */
603*193032a3SAndroid Build Coastguard Worker 	  optind = ++optbase;
604*193032a3SAndroid Build Coastguard Worker 	  return getopt_all_done;
605*193032a3SAndroid Build Coastguard Worker 	}
606*193032a3SAndroid Build Coastguard Worker       }
607*193032a3SAndroid Build Coastguard Worker       else if( mode < getopt_mode_long_only )
608*193032a3SAndroid Build Coastguard Worker       {
609*193032a3SAndroid Build Coastguard Worker 	/* it's not an explicit long option, and `getopt_long_only' isn't active,
610*193032a3SAndroid Build Coastguard Worker 	 * so we must explicitly try to match it as a short option.
611*193032a3SAndroid Build Coastguard Worker 	 */
612*193032a3SAndroid Build Coastguard Worker 	mode = getopt_mode_standard;
613*193032a3SAndroid Build Coastguard Worker       }
614*193032a3SAndroid Build Coastguard Worker 
615*193032a3SAndroid Build Coastguard Worker       if( mode >= getopt_mode_long )
616*193032a3SAndroid Build Coastguard Worker       {
617*193032a3SAndroid Build Coastguard Worker 	/* the current argument is a long form option, (either explicitly,
618*193032a3SAndroid Build Coastguard Worker 	 * introduced by a double hyphen, or implicitly because we were called
619*193032a3SAndroid Build Coastguard Worker 	 * by `getopt_long_only'); this is where we parse it.
620*193032a3SAndroid Build Coastguard Worker 	 */
621*193032a3SAndroid Build Coastguard Worker 	int lookup;
622*193032a3SAndroid Build Coastguard Worker 	int matched = -1;
623*193032a3SAndroid Build Coastguard Worker 
624*193032a3SAndroid Build Coastguard Worker 	/* we need to fetch the `extra' function arguments, which are
625*193032a3SAndroid Build Coastguard Worker 	 * specified for the `getopt_long' APIs.
626*193032a3SAndroid Build Coastguard Worker 	 */
627*193032a3SAndroid Build Coastguard Worker 	va_list refptr;
628*193032a3SAndroid Build Coastguard Worker 	va_start( refptr, optstring );
629*193032a3SAndroid Build Coastguard Worker 	struct option *longopts = va_arg( refptr, struct option * );
630*193032a3SAndroid Build Coastguard Worker 	int *optindex = va_arg( refptr, int * );
631*193032a3SAndroid Build Coastguard Worker 	va_end( refptr );
632*193032a3SAndroid Build Coastguard Worker 
633*193032a3SAndroid Build Coastguard Worker 	/* ensuring that `optarg' does not inherit any junk, from parsing
634*193032a3SAndroid Build Coastguard Worker 	 * preceding arguments ...
635*193032a3SAndroid Build Coastguard Worker 	 */
636*193032a3SAndroid Build Coastguard Worker 	optarg = NULL;
637*193032a3SAndroid Build Coastguard Worker 	for( lookup = 0; longopts && longopts[lookup].name; ++lookup )
638*193032a3SAndroid Build Coastguard Worker 	{
639*193032a3SAndroid Build Coastguard Worker 	  /* scan the list of defined long form options ...
640*193032a3SAndroid Build Coastguard Worker 	   */
641*193032a3SAndroid Build Coastguard Worker           switch( getopt_match_long( nextchar, longopts[lookup].name ) )
642*193032a3SAndroid Build Coastguard Worker           {
643*193032a3SAndroid Build Coastguard Worker 	    /* looking for possible matches for the current argument.
644*193032a3SAndroid Build Coastguard Worker 	     */
645*193032a3SAndroid Build Coastguard Worker             case getopt_exact_match:
646*193032a3SAndroid Build Coastguard Worker 	      /*
647*193032a3SAndroid Build Coastguard Worker 	       * when an exact match is found,
648*193032a3SAndroid Build Coastguard Worker 	       * return it immediately, setting `nextchar' to NULL,
649*193032a3SAndroid Build Coastguard Worker 	       * to ensure we don't mistakenly try to match any
650*193032a3SAndroid Build Coastguard Worker 	       * subsequent characters as short form options.
651*193032a3SAndroid Build Coastguard Worker 	       */
652*193032a3SAndroid Build Coastguard Worker 	      nextchar = NULL;
653*193032a3SAndroid Build Coastguard Worker 	      return getopt_resolved( mode, argc, argv, &argind,
654*193032a3SAndroid Build Coastguard Worker 		  longopts, lookup, optindex, optstring );
655*193032a3SAndroid Build Coastguard Worker 
656*193032a3SAndroid Build Coastguard Worker 	    case getopt_abbreviated_match:
657*193032a3SAndroid Build Coastguard Worker 	      /*
658*193032a3SAndroid Build Coastguard Worker 	       * but, for a partial (initial substring) match ...
659*193032a3SAndroid Build Coastguard Worker 	       */
660*193032a3SAndroid Build Coastguard Worker 	      if( matched >= 0 )
661*193032a3SAndroid Build Coastguard Worker 	      {
662*193032a3SAndroid Build Coastguard Worker 		/* if this is not the first, then we have an ambiguity ...
663*193032a3SAndroid Build Coastguard Worker 		 */
664*193032a3SAndroid Build Coastguard Worker 		if( (mode == getopt_mode_long_only)
665*193032a3SAndroid Build Coastguard Worker 		  /*
666*193032a3SAndroid Build Coastguard Worker 		   * However, in the case of getopt_long_only(), if
667*193032a3SAndroid Build Coastguard Worker 		   * the entire ambiguously matched string represents
668*193032a3SAndroid Build Coastguard Worker 		   * a valid short option specification, then we may
669*193032a3SAndroid Build Coastguard Worker 		   * proceed to interpret it as such.
670*193032a3SAndroid Build Coastguard Worker 		   */
671*193032a3SAndroid Build Coastguard Worker 		&&  getopt_verify( nextchar, optstring )  )
672*193032a3SAndroid Build Coastguard Worker 		  return getopt_parse( mode, argc, argv, optstring );
673*193032a3SAndroid Build Coastguard Worker 
674*193032a3SAndroid Build Coastguard Worker 		/* If we get to here, then the ambiguously matched
675*193032a3SAndroid Build Coastguard Worker 		 * partial long option isn't valid for short option
676*193032a3SAndroid Build Coastguard Worker 		 * evaluation; reset parser context to resume with
677*193032a3SAndroid Build Coastguard Worker 		 * the following command line argument, diagnose
678*193032a3SAndroid Build Coastguard Worker 		 * ambiguity, and bail out.
679*193032a3SAndroid Build Coastguard Worker 		 */
680*193032a3SAndroid Build Coastguard Worker 		optopt = 0;
681*193032a3SAndroid Build Coastguard Worker 		nextchar = NULL;
682*193032a3SAndroid Build Coastguard Worker 		optind = argind + 1;
683*193032a3SAndroid Build Coastguard Worker 		complain( "option `%s' is ambiguous", argv[argind] );
684*193032a3SAndroid Build Coastguard Worker 		return getopt_unknown;
685*193032a3SAndroid Build Coastguard Worker 	      }
686*193032a3SAndroid Build Coastguard Worker 	      /* otherwise just note that we've found a possible match ...
687*193032a3SAndroid Build Coastguard Worker 	       */
688*193032a3SAndroid Build Coastguard Worker 	      matched = lookup;
689*193032a3SAndroid Build Coastguard Worker           }
690*193032a3SAndroid Build Coastguard Worker 	}
691*193032a3SAndroid Build Coastguard Worker 	if( matched >= 0 )
692*193032a3SAndroid Build Coastguard Worker 	{
693*193032a3SAndroid Build Coastguard Worker 	  /* if we get to here, then we found exactly one partial match,
694*193032a3SAndroid Build Coastguard Worker 	   * so return it, as for an exact match.
695*193032a3SAndroid Build Coastguard Worker 	   */
696*193032a3SAndroid Build Coastguard Worker 	  nextchar = NULL;
697*193032a3SAndroid Build Coastguard Worker 	  return getopt_resolved( mode, argc, argv, &argind,
698*193032a3SAndroid Build Coastguard Worker 	      longopts, matched, optindex, optstring );
699*193032a3SAndroid Build Coastguard Worker 	}
700*193032a3SAndroid Build Coastguard Worker 	/* if here, then we had what SHOULD have been a long form option,
701*193032a3SAndroid Build Coastguard Worker 	 * but it is unmatched ...
702*193032a3SAndroid Build Coastguard Worker 	 */
703*193032a3SAndroid Build Coastguard Worker 	if( (mode < getopt_mode_long_only)
704*193032a3SAndroid Build Coastguard Worker 	  /*
705*193032a3SAndroid Build Coastguard Worker 	   * ... although paradoxically, `mode == getopt_mode_long_only'
706*193032a3SAndroid Build Coastguard Worker 	   * allows us to still try to match it as a short form option.
707*193032a3SAndroid Build Coastguard Worker 	   */
708*193032a3SAndroid Build Coastguard Worker         ||  (getopt_verify( nextchar, optstring ) == 0)  )
709*193032a3SAndroid Build Coastguard Worker 	{
710*193032a3SAndroid Build Coastguard Worker 	  /* When it cannot be matched, reset the parsing context to
711*193032a3SAndroid Build Coastguard Worker 	   * resume from the next argument, diagnose the failed match,
712*193032a3SAndroid Build Coastguard Worker 	   * and bail out.
713*193032a3SAndroid Build Coastguard Worker 	   */
714*193032a3SAndroid Build Coastguard Worker 	  optopt = 0;
715*193032a3SAndroid Build Coastguard Worker 	  nextchar = NULL;
716*193032a3SAndroid Build Coastguard Worker 	  optind = argind + 1;
717*193032a3SAndroid Build Coastguard Worker 	  complain( "unrecognised option `%s'", argv[argind] );
718*193032a3SAndroid Build Coastguard Worker 	  return getopt_unknown;
719*193032a3SAndroid Build Coastguard Worker 	}
720*193032a3SAndroid Build Coastguard Worker       }
721*193032a3SAndroid Build Coastguard Worker       /* fall through to handle standard short form options...
722*193032a3SAndroid Build Coastguard Worker        * when the option argument format is neither explictly identified
723*193032a3SAndroid Build Coastguard Worker        * as long, nor implicitly matched as such, and the argument isn't
724*193032a3SAndroid Build Coastguard Worker        * just a bare hyphen, (which isn't an option), then we make one
725*193032a3SAndroid Build Coastguard Worker        * recursive call to explicitly interpret it as short format.
726*193032a3SAndroid Build Coastguard Worker        */
727*193032a3SAndroid Build Coastguard Worker       if( *nextchar )
728*193032a3SAndroid Build Coastguard Worker 	return getopt_parse( mode, argc, argv, optstring );
729*193032a3SAndroid Build Coastguard Worker     }
730*193032a3SAndroid Build Coastguard Worker     /* if we get to here, then we've parsed a non-option argument ...
731*193032a3SAndroid Build Coastguard Worker      * in GNU compatibility mode, we step over it, so we can permute
732*193032a3SAndroid Build Coastguard Worker      * any subsequent option arguments, but ...
733*193032a3SAndroid Build Coastguard Worker      */
734*193032a3SAndroid Build Coastguard Worker     if( *optstring == getopt_switchar )
735*193032a3SAndroid Build Coastguard Worker     {
736*193032a3SAndroid Build Coastguard Worker       /* if `optstring' begins with a `-' character, this special
737*193032a3SAndroid Build Coastguard Worker        * GNU specific behaviour requires us to return the non-option
738*193032a3SAndroid Build Coastguard Worker        * arguments in strict order, as pseudo-arguments to a special
739*193032a3SAndroid Build Coastguard Worker        * option, with return value defined as `getopt_ordered'.
740*193032a3SAndroid Build Coastguard Worker        */
741*193032a3SAndroid Build Coastguard Worker       nextchar = NULL;
742*193032a3SAndroid Build Coastguard Worker       optind = argind + 1;
743*193032a3SAndroid Build Coastguard Worker       optarg = argv[argind];
744*193032a3SAndroid Build Coastguard Worker       return getopt_ordered;
745*193032a3SAndroid Build Coastguard Worker     }
746*193032a3SAndroid Build Coastguard Worker     if( getopt_conventions( *optstring ) & getopt_posixly_correct )
747*193032a3SAndroid Build Coastguard Worker       /*
748*193032a3SAndroid Build Coastguard Worker        * otherwise ...
749*193032a3SAndroid Build Coastguard Worker        * for POSIXLY_CORRECT behaviour, or if `optstring' begins with
750*193032a3SAndroid Build Coastguard Worker        * a `+' character, then we break out of the parsing loop, so that
751*193032a3SAndroid Build Coastguard Worker        * the scan ends at the current argument, with no permutation.
752*193032a3SAndroid Build Coastguard Worker        */
753*193032a3SAndroid Build Coastguard Worker       break;
754*193032a3SAndroid Build Coastguard Worker   }
755*193032a3SAndroid Build Coastguard Worker   /* fall through when all arguments have been evaluated,
756*193032a3SAndroid Build Coastguard Worker    */
757*193032a3SAndroid Build Coastguard Worker   optind = optbase;
758*193032a3SAndroid Build Coastguard Worker   return getopt_all_done;
759*193032a3SAndroid Build Coastguard Worker }
760*193032a3SAndroid Build Coastguard Worker 
761*193032a3SAndroid Build Coastguard Worker /* All three public API entry points are trivially defined,
762*193032a3SAndroid Build Coastguard Worker  * in terms of the internal `getopt_parse' function.
763*193032a3SAndroid Build Coastguard Worker  */
getopt(getopt_std_args)764*193032a3SAndroid Build Coastguard Worker int getopt( getopt_std_args )
765*193032a3SAndroid Build Coastguard Worker {
766*193032a3SAndroid Build Coastguard Worker   return getopt_parse( getopt_mode_standard, argc, argv, optstring );
767*193032a3SAndroid Build Coastguard Worker }
768*193032a3SAndroid Build Coastguard Worker 
getopt_long(getopt_std_args,const struct option * opts,int * index)769*193032a3SAndroid Build Coastguard Worker int getopt_long( getopt_std_args, const struct option *opts, int *index )
770*193032a3SAndroid Build Coastguard Worker {
771*193032a3SAndroid Build Coastguard Worker   return getopt_parse( getopt_mode_long, argc, argv, optstring, opts, index );
772*193032a3SAndroid Build Coastguard Worker }
773*193032a3SAndroid Build Coastguard Worker 
getopt_long_only(getopt_std_args,const struct option * opts,int * index)774*193032a3SAndroid Build Coastguard Worker int getopt_long_only( getopt_std_args, const struct option *opts, int *index )
775*193032a3SAndroid Build Coastguard Worker {
776*193032a3SAndroid Build Coastguard Worker   return getopt_parse( getopt_mode_long_only, argc, argv, optstring, opts, index );
777*193032a3SAndroid Build Coastguard Worker }
778*193032a3SAndroid Build Coastguard Worker 
779*193032a3SAndroid Build Coastguard Worker #ifdef __weak_alias
780*193032a3SAndroid Build Coastguard Worker /*
781*193032a3SAndroid Build Coastguard Worker  * These Microsnot style uglified aliases are provided for compatibility
782*193032a3SAndroid Build Coastguard Worker  * with the previous MinGW implementation of the getopt API.
783*193032a3SAndroid Build Coastguard Worker  */
784*193032a3SAndroid Build Coastguard Worker __weak_alias( getopt, _getopt )
785*193032a3SAndroid Build Coastguard Worker __weak_alias( getopt_long, _getopt_long )
786*193032a3SAndroid Build Coastguard Worker __weak_alias( getopt_long_only, _getopt_long_only )
787*193032a3SAndroid Build Coastguard Worker #endif
788