xref: /freebsd/contrib/libfido2/openbsd-compat/getopt_long.c (revision 0afa8e065e14bb8fd338d75690e0238c00167d40)
1*0afa8e06SEd Maste /*	$OpenBSD: getopt_long.c,v 1.25 2011/03/05 22:10:11 guenther Exp $	*/
2*0afa8e06SEd Maste /*	$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $	*/
3*0afa8e06SEd Maste 
4*0afa8e06SEd Maste /*
5*0afa8e06SEd Maste  * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
6*0afa8e06SEd Maste  *
7*0afa8e06SEd Maste  * Permission to use, copy, modify, and distribute this software for any
8*0afa8e06SEd Maste  * purpose with or without fee is hereby granted, provided that the above
9*0afa8e06SEd Maste  * copyright notice and this permission notice appear in all copies.
10*0afa8e06SEd Maste  *
11*0afa8e06SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12*0afa8e06SEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*0afa8e06SEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14*0afa8e06SEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*0afa8e06SEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*0afa8e06SEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17*0afa8e06SEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*0afa8e06SEd Maste  *
19*0afa8e06SEd Maste  * Sponsored in part by the Defense Advanced Research Projects
20*0afa8e06SEd Maste  * Agency (DARPA) and Air Force Research Laboratory, Air Force
21*0afa8e06SEd Maste  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22*0afa8e06SEd Maste  */
23*0afa8e06SEd Maste /*-
24*0afa8e06SEd Maste  * Copyright (c) 2000 The NetBSD Foundation, Inc.
25*0afa8e06SEd Maste  * All rights reserved.
26*0afa8e06SEd Maste  *
27*0afa8e06SEd Maste  * This code is derived from software contributed to The NetBSD Foundation
28*0afa8e06SEd Maste  * by Dieter Baron and Thomas Klausner.
29*0afa8e06SEd Maste  *
30*0afa8e06SEd Maste  * Redistribution and use in source and binary forms, with or without
31*0afa8e06SEd Maste  * modification, are permitted provided that the following conditions
32*0afa8e06SEd Maste  * are met:
33*0afa8e06SEd Maste  * 1. Redistributions of source code must retain the above copyright
34*0afa8e06SEd Maste  *    notice, this list of conditions and the following disclaimer.
35*0afa8e06SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
36*0afa8e06SEd Maste  *    notice, this list of conditions and the following disclaimer in the
37*0afa8e06SEd Maste  *    documentation and/or other materials provided with the distribution.
38*0afa8e06SEd Maste  *
39*0afa8e06SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40*0afa8e06SEd Maste  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41*0afa8e06SEd Maste  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0afa8e06SEd Maste  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43*0afa8e06SEd Maste  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44*0afa8e06SEd Maste  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45*0afa8e06SEd Maste  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46*0afa8e06SEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47*0afa8e06SEd Maste  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48*0afa8e06SEd Maste  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49*0afa8e06SEd Maste  * POSSIBILITY OF SUCH DAMAGE.
50*0afa8e06SEd Maste  */
51*0afa8e06SEd Maste 
52*0afa8e06SEd Maste /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt_long.c */
53*0afa8e06SEd Maste #include "openbsd-compat.h"
54*0afa8e06SEd Maste 
55*0afa8e06SEd Maste #if !defined(HAVE_GETOPT)
56*0afa8e06SEd Maste 
57*0afa8e06SEd Maste #if 0
58*0afa8e06SEd Maste #include <err.h>
59*0afa8e06SEd Maste #include <getopt.h>
60*0afa8e06SEd Maste #endif
61*0afa8e06SEd Maste #include <errno.h>
62*0afa8e06SEd Maste #include <stdlib.h>
63*0afa8e06SEd Maste #include <string.h>
64*0afa8e06SEd Maste #include <stdarg.h>
65*0afa8e06SEd Maste 
66*0afa8e06SEd Maste int	opterr = 1;		/* if error message should be printed */
67*0afa8e06SEd Maste int	optind = 1;		/* index into parent argv vector */
68*0afa8e06SEd Maste int	optopt = '?';		/* character checked for validity */
69*0afa8e06SEd Maste int	optreset;		/* reset getopt */
70*0afa8e06SEd Maste char    *optarg;		/* argument associated with option */
71*0afa8e06SEd Maste 
72*0afa8e06SEd Maste #define PRINT_ERROR	((opterr) && (*options != ':'))
73*0afa8e06SEd Maste 
74*0afa8e06SEd Maste #define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
75*0afa8e06SEd Maste #define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
76*0afa8e06SEd Maste #define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
77*0afa8e06SEd Maste 
78*0afa8e06SEd Maste /* return values */
79*0afa8e06SEd Maste #define	BADCH		(int)'?'
80*0afa8e06SEd Maste #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
81*0afa8e06SEd Maste #define	INORDER 	(int)1
82*0afa8e06SEd Maste 
83*0afa8e06SEd Maste #define	EMSG		""
84*0afa8e06SEd Maste 
85*0afa8e06SEd Maste static int getopt_internal(int, char * const *, const char *,
86*0afa8e06SEd Maste 			   const struct option *, int *, int);
87*0afa8e06SEd Maste static int parse_long_options(char * const *, const char *,
88*0afa8e06SEd Maste 			      const struct option *, int *, int);
89*0afa8e06SEd Maste static int gcd(int, int);
90*0afa8e06SEd Maste static void permute_args(int, int, int, char * const *);
91*0afa8e06SEd Maste 
92*0afa8e06SEd Maste static char *place = EMSG; /* option letter processing */
93*0afa8e06SEd Maste 
94*0afa8e06SEd Maste /* XXX: set optreset to 1 rather than these two */
95*0afa8e06SEd Maste static int nonopt_start = -1; /* first non option argument (for permute) */
96*0afa8e06SEd Maste static int nonopt_end = -1;   /* first option after non options (for permute) */
97*0afa8e06SEd Maste 
98*0afa8e06SEd Maste /* Error messages */
99*0afa8e06SEd Maste static const char recargchar[] = "option requires an argument -- %c";
100*0afa8e06SEd Maste static const char recargstring[] = "option requires an argument -- %s";
101*0afa8e06SEd Maste static const char ambig[] = "ambiguous option -- %.*s";
102*0afa8e06SEd Maste static const char noarg[] = "option doesn't take an argument -- %.*s";
103*0afa8e06SEd Maste static const char illoptchar[] = "unknown option -- %c";
104*0afa8e06SEd Maste static const char illoptstring[] = "unknown option -- %s";
105*0afa8e06SEd Maste 
106*0afa8e06SEd Maste /*
107*0afa8e06SEd Maste  * Compute the greatest common divisor of a and b.
108*0afa8e06SEd Maste  */
109*0afa8e06SEd Maste static int
gcd(int a,int b)110*0afa8e06SEd Maste gcd(int a, int b)
111*0afa8e06SEd Maste {
112*0afa8e06SEd Maste 	int c;
113*0afa8e06SEd Maste 
114*0afa8e06SEd Maste 	c = a % b;
115*0afa8e06SEd Maste 	while (c != 0) {
116*0afa8e06SEd Maste 		a = b;
117*0afa8e06SEd Maste 		b = c;
118*0afa8e06SEd Maste 		c = a % b;
119*0afa8e06SEd Maste 	}
120*0afa8e06SEd Maste 
121*0afa8e06SEd Maste 	return (b);
122*0afa8e06SEd Maste }
123*0afa8e06SEd Maste 
124*0afa8e06SEd Maste /*
125*0afa8e06SEd Maste  * Exchange the block from nonopt_start to nonopt_end with the block
126*0afa8e06SEd Maste  * from nonopt_end to opt_end (keeping the same order of arguments
127*0afa8e06SEd Maste  * in each block).
128*0afa8e06SEd Maste  */
129*0afa8e06SEd Maste static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)130*0afa8e06SEd Maste permute_args(int panonopt_start, int panonopt_end, int opt_end,
131*0afa8e06SEd Maste 	char * const *nargv)
132*0afa8e06SEd Maste {
133*0afa8e06SEd Maste 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
134*0afa8e06SEd Maste 	char *swap;
135*0afa8e06SEd Maste 
136*0afa8e06SEd Maste 	/*
137*0afa8e06SEd Maste 	 * compute lengths of blocks and number and size of cycles
138*0afa8e06SEd Maste 	 */
139*0afa8e06SEd Maste 	nnonopts = panonopt_end - panonopt_start;
140*0afa8e06SEd Maste 	nopts = opt_end - panonopt_end;
141*0afa8e06SEd Maste 	ncycle = gcd(nnonopts, nopts);
142*0afa8e06SEd Maste 	cyclelen = (opt_end - panonopt_start) / ncycle;
143*0afa8e06SEd Maste 
144*0afa8e06SEd Maste 	for (i = 0; i < ncycle; i++) {
145*0afa8e06SEd Maste 		cstart = panonopt_end+i;
146*0afa8e06SEd Maste 		pos = cstart;
147*0afa8e06SEd Maste 		for (j = 0; j < cyclelen; j++) {
148*0afa8e06SEd Maste 			if (pos >= panonopt_end)
149*0afa8e06SEd Maste 				pos -= nnonopts;
150*0afa8e06SEd Maste 			else
151*0afa8e06SEd Maste 				pos += nopts;
152*0afa8e06SEd Maste 			swap = nargv[pos];
153*0afa8e06SEd Maste 			/* LINTED const cast */
154*0afa8e06SEd Maste 			((char **) nargv)[pos] = nargv[cstart];
155*0afa8e06SEd Maste 			/* LINTED const cast */
156*0afa8e06SEd Maste 			((char **)nargv)[cstart] = swap;
157*0afa8e06SEd Maste 		}
158*0afa8e06SEd Maste 	}
159*0afa8e06SEd Maste }
160*0afa8e06SEd Maste 
161*0afa8e06SEd Maste /*
162*0afa8e06SEd Maste  * parse_long_options --
163*0afa8e06SEd Maste  *	Parse long options in argc/argv argument vector.
164*0afa8e06SEd Maste  * Returns -1 if short_too is set and the option does not match long_options.
165*0afa8e06SEd Maste  */
166*0afa8e06SEd Maste static int
parse_long_options(char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too)167*0afa8e06SEd Maste parse_long_options(char * const *nargv, const char *options,
168*0afa8e06SEd Maste 	const struct option *long_options, int *idx, int short_too)
169*0afa8e06SEd Maste {
170*0afa8e06SEd Maste 	char *current_argv, *has_equal;
171*0afa8e06SEd Maste 	size_t current_argv_len;
172*0afa8e06SEd Maste 	int i, match;
173*0afa8e06SEd Maste 
174*0afa8e06SEd Maste 	current_argv = place;
175*0afa8e06SEd Maste 	match = -1;
176*0afa8e06SEd Maste 
177*0afa8e06SEd Maste 	optind++;
178*0afa8e06SEd Maste 
179*0afa8e06SEd Maste 	if ((has_equal = strchr(current_argv, '=')) != NULL) {
180*0afa8e06SEd Maste 		/* argument found (--option=arg) */
181*0afa8e06SEd Maste 		current_argv_len = has_equal - current_argv;
182*0afa8e06SEd Maste 		has_equal++;
183*0afa8e06SEd Maste 	} else
184*0afa8e06SEd Maste 		current_argv_len = strlen(current_argv);
185*0afa8e06SEd Maste 
186*0afa8e06SEd Maste 	for (i = 0; long_options[i].name; i++) {
187*0afa8e06SEd Maste 		/* find matching long option */
188*0afa8e06SEd Maste 		if (strncmp(current_argv, long_options[i].name,
189*0afa8e06SEd Maste 		    current_argv_len))
190*0afa8e06SEd Maste 			continue;
191*0afa8e06SEd Maste 
192*0afa8e06SEd Maste 		if (strlen(long_options[i].name) == current_argv_len) {
193*0afa8e06SEd Maste 			/* exact match */
194*0afa8e06SEd Maste 			match = i;
195*0afa8e06SEd Maste 			break;
196*0afa8e06SEd Maste 		}
197*0afa8e06SEd Maste 		/*
198*0afa8e06SEd Maste 		 * If this is a known short option, don't allow
199*0afa8e06SEd Maste 		 * a partial match of a single character.
200*0afa8e06SEd Maste 		 */
201*0afa8e06SEd Maste 		if (short_too && current_argv_len == 1)
202*0afa8e06SEd Maste 			continue;
203*0afa8e06SEd Maste 
204*0afa8e06SEd Maste 		if (match == -1)	/* partial match */
205*0afa8e06SEd Maste 			match = i;
206*0afa8e06SEd Maste 		else {
207*0afa8e06SEd Maste 			/* ambiguous abbreviation */
208*0afa8e06SEd Maste 			if (PRINT_ERROR)
209*0afa8e06SEd Maste 				warnx(ambig, (int)current_argv_len,
210*0afa8e06SEd Maste 				     current_argv);
211*0afa8e06SEd Maste 			optopt = 0;
212*0afa8e06SEd Maste 			return (BADCH);
213*0afa8e06SEd Maste 		}
214*0afa8e06SEd Maste 	}
215*0afa8e06SEd Maste 	if (match != -1) {		/* option found */
216*0afa8e06SEd Maste 		if (long_options[match].has_arg == no_argument
217*0afa8e06SEd Maste 		    && has_equal) {
218*0afa8e06SEd Maste 			if (PRINT_ERROR)
219*0afa8e06SEd Maste 				warnx(noarg, (int)current_argv_len,
220*0afa8e06SEd Maste 				     current_argv);
221*0afa8e06SEd Maste 			/*
222*0afa8e06SEd Maste 			 * XXX: GNU sets optopt to val regardless of flag
223*0afa8e06SEd Maste 			 */
224*0afa8e06SEd Maste 			if (long_options[match].flag == NULL)
225*0afa8e06SEd Maste 				optopt = long_options[match].val;
226*0afa8e06SEd Maste 			else
227*0afa8e06SEd Maste 				optopt = 0;
228*0afa8e06SEd Maste 			return (BADARG);
229*0afa8e06SEd Maste 		}
230*0afa8e06SEd Maste 		if (long_options[match].has_arg == required_argument ||
231*0afa8e06SEd Maste 		    long_options[match].has_arg == optional_argument) {
232*0afa8e06SEd Maste 			if (has_equal)
233*0afa8e06SEd Maste 				optarg = has_equal;
234*0afa8e06SEd Maste 			else if (long_options[match].has_arg ==
235*0afa8e06SEd Maste 			    required_argument) {
236*0afa8e06SEd Maste 				/*
237*0afa8e06SEd Maste 				 * optional argument doesn't use next nargv
238*0afa8e06SEd Maste 				 */
239*0afa8e06SEd Maste 				optarg = nargv[optind++];
240*0afa8e06SEd Maste 			}
241*0afa8e06SEd Maste 		}
242*0afa8e06SEd Maste 		if ((long_options[match].has_arg == required_argument)
243*0afa8e06SEd Maste 		    && (optarg == NULL)) {
244*0afa8e06SEd Maste 			/*
245*0afa8e06SEd Maste 			 * Missing argument; leading ':' indicates no error
246*0afa8e06SEd Maste 			 * should be generated.
247*0afa8e06SEd Maste 			 */
248*0afa8e06SEd Maste 			if (PRINT_ERROR)
249*0afa8e06SEd Maste 				warnx(recargstring,
250*0afa8e06SEd Maste 				    current_argv);
251*0afa8e06SEd Maste 			/*
252*0afa8e06SEd Maste 			 * XXX: GNU sets optopt to val regardless of flag
253*0afa8e06SEd Maste 			 */
254*0afa8e06SEd Maste 			if (long_options[match].flag == NULL)
255*0afa8e06SEd Maste 				optopt = long_options[match].val;
256*0afa8e06SEd Maste 			else
257*0afa8e06SEd Maste 				optopt = 0;
258*0afa8e06SEd Maste 			--optind;
259*0afa8e06SEd Maste 			return (BADARG);
260*0afa8e06SEd Maste 		}
261*0afa8e06SEd Maste 	} else {			/* unknown option */
262*0afa8e06SEd Maste 		if (short_too) {
263*0afa8e06SEd Maste 			--optind;
264*0afa8e06SEd Maste 			return (-1);
265*0afa8e06SEd Maste 		}
266*0afa8e06SEd Maste 		if (PRINT_ERROR)
267*0afa8e06SEd Maste 			warnx(illoptstring, current_argv);
268*0afa8e06SEd Maste 		optopt = 0;
269*0afa8e06SEd Maste 		return (BADCH);
270*0afa8e06SEd Maste 	}
271*0afa8e06SEd Maste 	if (idx)
272*0afa8e06SEd Maste 		*idx = match;
273*0afa8e06SEd Maste 	if (long_options[match].flag) {
274*0afa8e06SEd Maste 		*long_options[match].flag = long_options[match].val;
275*0afa8e06SEd Maste 		return (0);
276*0afa8e06SEd Maste 	} else
277*0afa8e06SEd Maste 		return (long_options[match].val);
278*0afa8e06SEd Maste }
279*0afa8e06SEd Maste 
280*0afa8e06SEd Maste /*
281*0afa8e06SEd Maste  * getopt_internal --
282*0afa8e06SEd Maste  *	Parse argc/argv argument vector.  Called by user level routines.
283*0afa8e06SEd Maste  */
284*0afa8e06SEd Maste static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int flags)285*0afa8e06SEd Maste getopt_internal(int nargc, char * const *nargv, const char *options,
286*0afa8e06SEd Maste 	const struct option *long_options, int *idx, int flags)
287*0afa8e06SEd Maste {
288*0afa8e06SEd Maste 	char *oli;				/* option letter list index */
289*0afa8e06SEd Maste 	int optchar, short_too;
290*0afa8e06SEd Maste 	static int posixly_correct = -1;
291*0afa8e06SEd Maste 
292*0afa8e06SEd Maste 	if (options == NULL)
293*0afa8e06SEd Maste 		return (-1);
294*0afa8e06SEd Maste 
295*0afa8e06SEd Maste 	/*
296*0afa8e06SEd Maste 	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
297*0afa8e06SEd Maste 	 * XXX using optreset.  Work around this braindamage.
298*0afa8e06SEd Maste 	 */
299*0afa8e06SEd Maste 	if (optind == 0)
300*0afa8e06SEd Maste 		optind = optreset = 1;
301*0afa8e06SEd Maste 
302*0afa8e06SEd Maste 	/*
303*0afa8e06SEd Maste 	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
304*0afa8e06SEd Maste 	 * string begins with a '+'.
305*0afa8e06SEd Maste 	 */
306*0afa8e06SEd Maste 	if (posixly_correct == -1 || optreset)
307*0afa8e06SEd Maste 		posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
308*0afa8e06SEd Maste 	if (*options == '-')
309*0afa8e06SEd Maste 		flags |= FLAG_ALLARGS;
310*0afa8e06SEd Maste 	else if (posixly_correct || *options == '+')
311*0afa8e06SEd Maste 		flags &= ~FLAG_PERMUTE;
312*0afa8e06SEd Maste 	if (*options == '+' || *options == '-')
313*0afa8e06SEd Maste 		options++;
314*0afa8e06SEd Maste 
315*0afa8e06SEd Maste 	optarg = NULL;
316*0afa8e06SEd Maste 	if (optreset)
317*0afa8e06SEd Maste 		nonopt_start = nonopt_end = -1;
318*0afa8e06SEd Maste start:
319*0afa8e06SEd Maste 	if (optreset || !*place) {		/* update scanning pointer */
320*0afa8e06SEd Maste 		optreset = 0;
321*0afa8e06SEd Maste 		if (optind >= nargc) {          /* end of argument vector */
322*0afa8e06SEd Maste 			place = EMSG;
323*0afa8e06SEd Maste 			if (nonopt_end != -1) {
324*0afa8e06SEd Maste 				/* do permutation, if we have to */
325*0afa8e06SEd Maste 				permute_args(nonopt_start, nonopt_end,
326*0afa8e06SEd Maste 				    optind, nargv);
327*0afa8e06SEd Maste 				optind -= nonopt_end - nonopt_start;
328*0afa8e06SEd Maste 			}
329*0afa8e06SEd Maste 			else if (nonopt_start != -1) {
330*0afa8e06SEd Maste 				/*
331*0afa8e06SEd Maste 				 * If we skipped non-options, set optind
332*0afa8e06SEd Maste 				 * to the first of them.
333*0afa8e06SEd Maste 				 */
334*0afa8e06SEd Maste 				optind = nonopt_start;
335*0afa8e06SEd Maste 			}
336*0afa8e06SEd Maste 			nonopt_start = nonopt_end = -1;
337*0afa8e06SEd Maste 			return (-1);
338*0afa8e06SEd Maste 		}
339*0afa8e06SEd Maste 		if (*(place = nargv[optind]) != '-' ||
340*0afa8e06SEd Maste 		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
341*0afa8e06SEd Maste 			place = EMSG;		/* found non-option */
342*0afa8e06SEd Maste 			if (flags & FLAG_ALLARGS) {
343*0afa8e06SEd Maste 				/*
344*0afa8e06SEd Maste 				 * GNU extension:
345*0afa8e06SEd Maste 				 * return non-option as argument to option 1
346*0afa8e06SEd Maste 				 */
347*0afa8e06SEd Maste 				optarg = nargv[optind++];
348*0afa8e06SEd Maste 				return (INORDER);
349*0afa8e06SEd Maste 			}
350*0afa8e06SEd Maste 			if (!(flags & FLAG_PERMUTE)) {
351*0afa8e06SEd Maste 				/*
352*0afa8e06SEd Maste 				 * If no permutation wanted, stop parsing
353*0afa8e06SEd Maste 				 * at first non-option.
354*0afa8e06SEd Maste 				 */
355*0afa8e06SEd Maste 				return (-1);
356*0afa8e06SEd Maste 			}
357*0afa8e06SEd Maste 			/* do permutation */
358*0afa8e06SEd Maste 			if (nonopt_start == -1)
359*0afa8e06SEd Maste 				nonopt_start = optind;
360*0afa8e06SEd Maste 			else if (nonopt_end != -1) {
361*0afa8e06SEd Maste 				permute_args(nonopt_start, nonopt_end,
362*0afa8e06SEd Maste 				    optind, nargv);
363*0afa8e06SEd Maste 				nonopt_start = optind -
364*0afa8e06SEd Maste 				    (nonopt_end - nonopt_start);
365*0afa8e06SEd Maste 				nonopt_end = -1;
366*0afa8e06SEd Maste 			}
367*0afa8e06SEd Maste 			optind++;
368*0afa8e06SEd Maste 			/* process next argument */
369*0afa8e06SEd Maste 			goto start;
370*0afa8e06SEd Maste 		}
371*0afa8e06SEd Maste 		if (nonopt_start != -1 && nonopt_end == -1)
372*0afa8e06SEd Maste 			nonopt_end = optind;
373*0afa8e06SEd Maste 
374*0afa8e06SEd Maste 		/*
375*0afa8e06SEd Maste 		 * If we have "-" do nothing, if "--" we are done.
376*0afa8e06SEd Maste 		 */
377*0afa8e06SEd Maste 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
378*0afa8e06SEd Maste 			optind++;
379*0afa8e06SEd Maste 			place = EMSG;
380*0afa8e06SEd Maste 			/*
381*0afa8e06SEd Maste 			 * We found an option (--), so if we skipped
382*0afa8e06SEd Maste 			 * non-options, we have to permute.
383*0afa8e06SEd Maste 			 */
384*0afa8e06SEd Maste 			if (nonopt_end != -1) {
385*0afa8e06SEd Maste 				permute_args(nonopt_start, nonopt_end,
386*0afa8e06SEd Maste 				    optind, nargv);
387*0afa8e06SEd Maste 				optind -= nonopt_end - nonopt_start;
388*0afa8e06SEd Maste 			}
389*0afa8e06SEd Maste 			nonopt_start = nonopt_end = -1;
390*0afa8e06SEd Maste 			return (-1);
391*0afa8e06SEd Maste 		}
392*0afa8e06SEd Maste 	}
393*0afa8e06SEd Maste 
394*0afa8e06SEd Maste 	/*
395*0afa8e06SEd Maste 	 * Check long options if:
396*0afa8e06SEd Maste 	 *  1) we were passed some
397*0afa8e06SEd Maste 	 *  2) the arg is not just "-"
398*0afa8e06SEd Maste 	 *  3) either the arg starts with -- we are getopt_long_only()
399*0afa8e06SEd Maste 	 */
400*0afa8e06SEd Maste 	if (long_options != NULL && place != nargv[optind] &&
401*0afa8e06SEd Maste 	    (*place == '-' || (flags & FLAG_LONGONLY))) {
402*0afa8e06SEd Maste 		short_too = 0;
403*0afa8e06SEd Maste 		if (*place == '-')
404*0afa8e06SEd Maste 			place++;		/* --foo long option */
405*0afa8e06SEd Maste 		else if (*place != ':' && strchr(options, *place) != NULL)
406*0afa8e06SEd Maste 			short_too = 1;		/* could be short option too */
407*0afa8e06SEd Maste 
408*0afa8e06SEd Maste 		optchar = parse_long_options(nargv, options, long_options,
409*0afa8e06SEd Maste 		    idx, short_too);
410*0afa8e06SEd Maste 		if (optchar != -1) {
411*0afa8e06SEd Maste 			place = EMSG;
412*0afa8e06SEd Maste 			return (optchar);
413*0afa8e06SEd Maste 		}
414*0afa8e06SEd Maste 	}
415*0afa8e06SEd Maste 
416*0afa8e06SEd Maste 	if ((optchar = (int)*place++) == (int)':' ||
417*0afa8e06SEd Maste 	    (optchar == (int)'-' && *place != '\0') ||
418*0afa8e06SEd Maste 	    (oli = strchr(options, optchar)) == NULL) {
419*0afa8e06SEd Maste 		/*
420*0afa8e06SEd Maste 		 * If the user specified "-" and  '-' isn't listed in
421*0afa8e06SEd Maste 		 * options, return -1 (non-option) as per POSIX.
422*0afa8e06SEd Maste 		 * Otherwise, it is an unknown option character (or ':').
423*0afa8e06SEd Maste 		 */
424*0afa8e06SEd Maste 		if (optchar == (int)'-' && *place == '\0')
425*0afa8e06SEd Maste 			return (-1);
426*0afa8e06SEd Maste 		if (!*place)
427*0afa8e06SEd Maste 			++optind;
428*0afa8e06SEd Maste 		if (PRINT_ERROR)
429*0afa8e06SEd Maste 			warnx(illoptchar, optchar);
430*0afa8e06SEd Maste 		optopt = optchar;
431*0afa8e06SEd Maste 		return (BADCH);
432*0afa8e06SEd Maste 	}
433*0afa8e06SEd Maste 	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
434*0afa8e06SEd Maste 		/* -W long-option */
435*0afa8e06SEd Maste 		if (*place)			/* no space */
436*0afa8e06SEd Maste 			/* NOTHING */;
437*0afa8e06SEd Maste 		else if (++optind >= nargc) {	/* no arg */
438*0afa8e06SEd Maste 			place = EMSG;
439*0afa8e06SEd Maste 			if (PRINT_ERROR)
440*0afa8e06SEd Maste 				warnx(recargchar, optchar);
441*0afa8e06SEd Maste 			optopt = optchar;
442*0afa8e06SEd Maste 			return (BADARG);
443*0afa8e06SEd Maste 		} else				/* white space */
444*0afa8e06SEd Maste 			place = nargv[optind];
445*0afa8e06SEd Maste 		optchar = parse_long_options(nargv, options, long_options,
446*0afa8e06SEd Maste 		    idx, 0);
447*0afa8e06SEd Maste 		place = EMSG;
448*0afa8e06SEd Maste 		return (optchar);
449*0afa8e06SEd Maste 	}
450*0afa8e06SEd Maste 	if (*++oli != ':') {			/* doesn't take argument */
451*0afa8e06SEd Maste 		if (!*place)
452*0afa8e06SEd Maste 			++optind;
453*0afa8e06SEd Maste 	} else {				/* takes (optional) argument */
454*0afa8e06SEd Maste 		optarg = NULL;
455*0afa8e06SEd Maste 		if (*place)			/* no white space */
456*0afa8e06SEd Maste 			optarg = place;
457*0afa8e06SEd Maste 		else if (oli[1] != ':') {	/* arg not optional */
458*0afa8e06SEd Maste 			if (++optind >= nargc) {	/* no arg */
459*0afa8e06SEd Maste 				place = EMSG;
460*0afa8e06SEd Maste 				if (PRINT_ERROR)
461*0afa8e06SEd Maste 					warnx(recargchar, optchar);
462*0afa8e06SEd Maste 				optopt = optchar;
463*0afa8e06SEd Maste 				return (BADARG);
464*0afa8e06SEd Maste 			} else
465*0afa8e06SEd Maste 				optarg = nargv[optind];
466*0afa8e06SEd Maste 		}
467*0afa8e06SEd Maste 		place = EMSG;
468*0afa8e06SEd Maste 		++optind;
469*0afa8e06SEd Maste 	}
470*0afa8e06SEd Maste 	/* dump back option letter */
471*0afa8e06SEd Maste 	return (optchar);
472*0afa8e06SEd Maste }
473*0afa8e06SEd Maste 
474*0afa8e06SEd Maste /*
475*0afa8e06SEd Maste  * getopt --
476*0afa8e06SEd Maste  *	Parse argc/argv argument vector.
477*0afa8e06SEd Maste  *
478*0afa8e06SEd Maste  * [eventually this will replace the BSD getopt]
479*0afa8e06SEd Maste  */
480*0afa8e06SEd Maste int
getopt(int nargc,char * const * nargv,const char * options)481*0afa8e06SEd Maste getopt(int nargc, char * const *nargv, const char *options)
482*0afa8e06SEd Maste {
483*0afa8e06SEd Maste 
484*0afa8e06SEd Maste 	/*
485*0afa8e06SEd Maste 	 * We don't pass FLAG_PERMUTE to getopt_internal() since
486*0afa8e06SEd Maste 	 * the BSD getopt(3) (unlike GNU) has never done this.
487*0afa8e06SEd Maste 	 *
488*0afa8e06SEd Maste 	 * Furthermore, since many privileged programs call getopt()
489*0afa8e06SEd Maste 	 * before dropping privileges it makes sense to keep things
490*0afa8e06SEd Maste 	 * as simple (and bug-free) as possible.
491*0afa8e06SEd Maste 	 */
492*0afa8e06SEd Maste 	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
493*0afa8e06SEd Maste }
494*0afa8e06SEd Maste 
495*0afa8e06SEd Maste #if 0
496*0afa8e06SEd Maste /*
497*0afa8e06SEd Maste  * getopt_long --
498*0afa8e06SEd Maste  *	Parse argc/argv argument vector.
499*0afa8e06SEd Maste  */
500*0afa8e06SEd Maste int
501*0afa8e06SEd Maste getopt_long(int nargc, char * const *nargv, const char *options,
502*0afa8e06SEd Maste     const struct option *long_options, int *idx)
503*0afa8e06SEd Maste {
504*0afa8e06SEd Maste 
505*0afa8e06SEd Maste 	return (getopt_internal(nargc, nargv, options, long_options, idx,
506*0afa8e06SEd Maste 	    FLAG_PERMUTE));
507*0afa8e06SEd Maste }
508*0afa8e06SEd Maste 
509*0afa8e06SEd Maste /*
510*0afa8e06SEd Maste  * getopt_long_only --
511*0afa8e06SEd Maste  *	Parse argc/argv argument vector.
512*0afa8e06SEd Maste  */
513*0afa8e06SEd Maste int
514*0afa8e06SEd Maste getopt_long_only(int nargc, char * const *nargv, const char *options,
515*0afa8e06SEd Maste     const struct option *long_options, int *idx)
516*0afa8e06SEd Maste {
517*0afa8e06SEd Maste 
518*0afa8e06SEd Maste 	return (getopt_internal(nargc, nargv, options, long_options, idx,
519*0afa8e06SEd Maste 	    FLAG_PERMUTE|FLAG_LONGONLY));
520*0afa8e06SEd Maste }
521*0afa8e06SEd Maste #endif
522*0afa8e06SEd Maste 
523*0afa8e06SEd Maste #endif /* !defined(HAVE_GETOPT) */
524