xref: /freebsd-12.1/lib/libc/stdlib/getopt_long.c (revision ed4fbbd5)
1829a229dSAndrey A. Chernov /*	$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $	*/
2a35a7e76SEric Melville /*	$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $	*/
3a35a7e76SEric Melville 
4829a229dSAndrey A. Chernov /*
5829a229dSAndrey A. Chernov  * Copyright (c) 2002 Todd C. Miller <[email protected]>
6829a229dSAndrey A. Chernov  *
7829a229dSAndrey A. Chernov  * Permission to use, copy, modify, and distribute this software for any
8829a229dSAndrey A. Chernov  * purpose with or without fee is hereby granted, provided that the above
9829a229dSAndrey A. Chernov  * copyright notice and this permission notice appear in all copies.
10829a229dSAndrey A. Chernov  *
11829a229dSAndrey A. Chernov  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12829a229dSAndrey A. Chernov  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13829a229dSAndrey A. Chernov  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14829a229dSAndrey A. Chernov  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15829a229dSAndrey A. Chernov  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16829a229dSAndrey A. Chernov  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17829a229dSAndrey A. Chernov  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18829a229dSAndrey A. Chernov  *
19829a229dSAndrey A. Chernov  * Sponsored in part by the Defense Advanced Research Projects
20829a229dSAndrey A. Chernov  * Agency (DARPA) and Air Force Research Laboratory, Air Force
21829a229dSAndrey A. Chernov  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22829a229dSAndrey A. Chernov  */
23a35a7e76SEric Melville /*-
24a35a7e76SEric Melville  * Copyright (c) 2000 The NetBSD Foundation, Inc.
25a35a7e76SEric Melville  * All rights reserved.
26a35a7e76SEric Melville  *
27a35a7e76SEric Melville  * This code is derived from software contributed to The NetBSD Foundation
28a35a7e76SEric Melville  * by Dieter Baron and Thomas Klausner.
29a35a7e76SEric Melville  *
30a35a7e76SEric Melville  * Redistribution and use in source and binary forms, with or without
31a35a7e76SEric Melville  * modification, are permitted provided that the following conditions
32a35a7e76SEric Melville  * are met:
33a35a7e76SEric Melville  * 1. Redistributions of source code must retain the above copyright
34a35a7e76SEric Melville  *    notice, this list of conditions and the following disclaimer.
35a35a7e76SEric Melville  * 2. Redistributions in binary form must reproduce the above copyright
36a35a7e76SEric Melville  *    notice, this list of conditions and the following disclaimer in the
37a35a7e76SEric Melville  *    documentation and/or other materials provided with the distribution.
38a35a7e76SEric Melville  * 3. All advertising materials mentioning features or use of this software
39a35a7e76SEric Melville  *    must display the following acknowledgement:
40a35a7e76SEric Melville  *        This product includes software developed by the NetBSD
41a35a7e76SEric Melville  *        Foundation, Inc. and its contributors.
42a35a7e76SEric Melville  * 4. Neither the name of The NetBSD Foundation nor the names of its
43a35a7e76SEric Melville  *    contributors may be used to endorse or promote products derived
44a35a7e76SEric Melville  *    from this software without specific prior written permission.
45a35a7e76SEric Melville  *
46a35a7e76SEric Melville  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47a35a7e76SEric Melville  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48a35a7e76SEric Melville  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49a35a7e76SEric Melville  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50a35a7e76SEric Melville  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51a35a7e76SEric Melville  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52a35a7e76SEric Melville  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53a35a7e76SEric Melville  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54a35a7e76SEric Melville  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55a35a7e76SEric Melville  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56a35a7e76SEric Melville  * POSSIBILITY OF SUCH DAMAGE.
57a35a7e76SEric Melville  */
58a35a7e76SEric Melville 
59829a229dSAndrey A. Chernov #if 0
60a35a7e76SEric Melville #if defined(LIBC_SCCS) && !defined(lint)
61829a229dSAndrey A. Chernov static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $";
62a35a7e76SEric Melville #endif /* LIBC_SCCS and not lint */
63829a229dSAndrey A. Chernov #endif
64829a229dSAndrey A. Chernov #include <sys/cdefs.h>
65829a229dSAndrey A. Chernov __FBSDID("$FreeBSD$");
66a35a7e76SEric Melville 
67a35a7e76SEric Melville #include <err.h>
68a35a7e76SEric Melville #include <errno.h>
69a35a7e76SEric Melville #include <getopt.h>
70a35a7e76SEric Melville #include <stdlib.h>
71a35a7e76SEric Melville #include <string.h>
72a35a7e76SEric Melville 
73f2fd86b7SAndrey A. Chernov #define GNU_COMPATIBLE		/* Be more compatible, configure's use us! */
74f2fd86b7SAndrey A. Chernov 
75f2fd86b7SAndrey A. Chernov #ifndef GNU_COMPATIBLE
76829a229dSAndrey A. Chernov #define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
77a35a7e76SEric Melville #endif
78a35a7e76SEric Melville 
79a35a7e76SEric Melville #ifdef REPLACE_GETOPT
80a35a7e76SEric Melville int	opterr = 1;		/* if error message should be printed */
81a35a7e76SEric Melville int	optind = 1;		/* index into parent argv vector */
82a35a7e76SEric Melville int	optopt = '?';		/* character checked for validity */
83a35a7e76SEric Melville int	optreset;		/* reset getopt */
84a35a7e76SEric Melville char    *optarg;		/* argument associated with option */
85a35a7e76SEric Melville #endif
86a35a7e76SEric Melville 
87829a229dSAndrey A. Chernov #define PRINT_ERROR	((opterr) && (*options != ':'))
88a35a7e76SEric Melville 
89829a229dSAndrey A. Chernov #define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
90829a229dSAndrey A. Chernov #define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
91829a229dSAndrey A. Chernov #define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
92a35a7e76SEric Melville 
93a35a7e76SEric Melville /* return values */
94a35a7e76SEric Melville #define	BADCH		(int)'?'
95829a229dSAndrey A. Chernov #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
96a35a7e76SEric Melville #define	INORDER 	(int)1
97a35a7e76SEric Melville 
98a35a7e76SEric Melville #define	EMSG		""
99a35a7e76SEric Melville 
1009f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
1019f06a99eSAndrey A. Chernov #define NO_PREFIX	(-1)
1029f06a99eSAndrey A. Chernov #define D_PREFIX	0
1039f06a99eSAndrey A. Chernov #define DD_PREFIX	1
1049f06a99eSAndrey A. Chernov #define W_PREFIX	2
1059f06a99eSAndrey A. Chernov #endif
1069f06a99eSAndrey A. Chernov 
107829a229dSAndrey A. Chernov static int getopt_internal(int, char * const *, const char *,
108829a229dSAndrey A. Chernov 			   const struct option *, int *, int);
109829a229dSAndrey A. Chernov static int parse_long_options(char * const *, const char *,
110*ed4fbbd5SAndrey A. Chernov 			      const struct option *, int *, int, int);
111e6fc380cSAlfred Perlstein static int gcd(int, int);
112e6fc380cSAlfred Perlstein static void permute_args(int, int, int, char * const *);
113a35a7e76SEric Melville 
114a35a7e76SEric Melville static char *place = EMSG; /* option letter processing */
115a35a7e76SEric Melville 
116a35a7e76SEric Melville /* XXX: set optreset to 1 rather than these two */
117a35a7e76SEric Melville static int nonopt_start = -1; /* first non option argument (for permute) */
118a35a7e76SEric Melville static int nonopt_end = -1;   /* first option after non options (for permute) */
119a35a7e76SEric Melville 
120a35a7e76SEric Melville /* Error messages */
121a35a7e76SEric Melville static const char recargchar[] = "option requires an argument -- %c";
1229f06a99eSAndrey A. Chernov static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
123f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
1249f06a99eSAndrey A. Chernov static int dash_prefix = NO_PREFIX;
125f2fd86b7SAndrey A. Chernov static const char gnuoptchar[] = "invalid option -- %c";
126f2fd86b7SAndrey A. Chernov 
1279f06a99eSAndrey A. Chernov static const char recargstring[] = "option `%s%s' requires an argument";
1289f06a99eSAndrey A. Chernov static const char ambig[] = "option `%s%.*s' is ambiguous";
1299f06a99eSAndrey A. Chernov static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
1309f06a99eSAndrey A. Chernov static const char illoptstring[] = "unrecognized option `%s%s'";
131f2fd86b7SAndrey A. Chernov #else
132a35a7e76SEric Melville static const char recargstring[] = "option requires an argument -- %s";
133a35a7e76SEric Melville static const char ambig[] = "ambiguous option -- %.*s";
134a35a7e76SEric Melville static const char noarg[] = "option doesn't take an argument -- %.*s";
135a35a7e76SEric Melville static const char illoptstring[] = "unknown option -- %s";
136f2fd86b7SAndrey A. Chernov #endif
137a35a7e76SEric Melville 
138a35a7e76SEric Melville /*
139a35a7e76SEric Melville  * Compute the greatest common divisor of a and b.
140a35a7e76SEric Melville  */
141a35a7e76SEric Melville static int
142829a229dSAndrey A. Chernov gcd(int a, int b)
143a35a7e76SEric Melville {
144a35a7e76SEric Melville 	int c;
145a35a7e76SEric Melville 
146a35a7e76SEric Melville 	c = a % b;
147a35a7e76SEric Melville 	while (c != 0) {
148a35a7e76SEric Melville 		a = b;
149a35a7e76SEric Melville 		b = c;
150a35a7e76SEric Melville 		c = a % b;
151a35a7e76SEric Melville 	}
152a35a7e76SEric Melville 
153829a229dSAndrey A. Chernov 	return (b);
154a35a7e76SEric Melville }
155a35a7e76SEric Melville 
156a35a7e76SEric Melville /*
157a35a7e76SEric Melville  * Exchange the block from nonopt_start to nonopt_end with the block
158a35a7e76SEric Melville  * from nonopt_end to opt_end (keeping the same order of arguments
159a35a7e76SEric Melville  * in each block).
160a35a7e76SEric Melville  */
161a35a7e76SEric Melville static void
162829a229dSAndrey A. Chernov permute_args(int panonopt_start, int panonopt_end, int opt_end,
163829a229dSAndrey A. Chernov 	char * const *nargv)
164a35a7e76SEric Melville {
165a35a7e76SEric Melville 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
166a35a7e76SEric Melville 	char *swap;
167a35a7e76SEric Melville 
168a35a7e76SEric Melville 	/*
169a35a7e76SEric Melville 	 * compute lengths of blocks and number and size of cycles
170a35a7e76SEric Melville 	 */
171a35a7e76SEric Melville 	nnonopts = panonopt_end - panonopt_start;
172a35a7e76SEric Melville 	nopts = opt_end - panonopt_end;
173a35a7e76SEric Melville 	ncycle = gcd(nnonopts, nopts);
174a35a7e76SEric Melville 	cyclelen = (opt_end - panonopt_start) / ncycle;
175a35a7e76SEric Melville 
176a35a7e76SEric Melville 	for (i = 0; i < ncycle; i++) {
177a35a7e76SEric Melville 		cstart = panonopt_end+i;
178a35a7e76SEric Melville 		pos = cstart;
179a35a7e76SEric Melville 		for (j = 0; j < cyclelen; j++) {
180a35a7e76SEric Melville 			if (pos >= panonopt_end)
181a35a7e76SEric Melville 				pos -= nnonopts;
182a35a7e76SEric Melville 			else
183a35a7e76SEric Melville 				pos += nopts;
184a35a7e76SEric Melville 			swap = nargv[pos];
185a35a7e76SEric Melville 			/* LINTED const cast */
186a35a7e76SEric Melville 			((char **) nargv)[pos] = nargv[cstart];
187a35a7e76SEric Melville 			/* LINTED const cast */
188a35a7e76SEric Melville 			((char **)nargv)[cstart] = swap;
189a35a7e76SEric Melville 		}
190a35a7e76SEric Melville 	}
191a35a7e76SEric Melville }
192a35a7e76SEric Melville 
193a35a7e76SEric Melville /*
194829a229dSAndrey A. Chernov  * parse_long_options --
195829a229dSAndrey A. Chernov  *	Parse long options in argc/argv argument vector.
196829a229dSAndrey A. Chernov  * Returns -1 if short_too is set and the option does not match long_options.
197a35a7e76SEric Melville  */
198a35a7e76SEric Melville static int
199829a229dSAndrey A. Chernov parse_long_options(char * const *nargv, const char *options,
200*ed4fbbd5SAndrey A. Chernov 	const struct option *long_options, int *idx, int short_too, int flags)
201a35a7e76SEric Melville {
202829a229dSAndrey A. Chernov 	char *current_argv, *has_equal;
2039f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2049f06a99eSAndrey A. Chernov 	char *current_dash;
2059f06a99eSAndrey A. Chernov #endif
206829a229dSAndrey A. Chernov 	size_t current_argv_len;
207*ed4fbbd5SAndrey A. Chernov 	int i, match, exact_match, second_partial_match;
208a35a7e76SEric Melville 
209829a229dSAndrey A. Chernov 	current_argv = place;
2109f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2119f06a99eSAndrey A. Chernov 	switch (dash_prefix) {
2129f06a99eSAndrey A. Chernov 		case D_PREFIX:
2139f06a99eSAndrey A. Chernov 			current_dash = "-";
2149f06a99eSAndrey A. Chernov 			break;
2159f06a99eSAndrey A. Chernov 		case DD_PREFIX:
2169f06a99eSAndrey A. Chernov 			current_dash = "--";
2179f06a99eSAndrey A. Chernov 			break;
2189f06a99eSAndrey A. Chernov 		case W_PREFIX:
2199f06a99eSAndrey A. Chernov 			current_dash = "-W ";
2209f06a99eSAndrey A. Chernov 			break;
2219f06a99eSAndrey A. Chernov 		default:
2229f06a99eSAndrey A. Chernov 			current_dash = "";
2239f06a99eSAndrey A. Chernov 			break;
2249f06a99eSAndrey A. Chernov 	}
2259f06a99eSAndrey A. Chernov #endif
226829a229dSAndrey A. Chernov 	match = -1;
227*ed4fbbd5SAndrey A. Chernov 	exact_match = 0;
228*ed4fbbd5SAndrey A. Chernov 	second_partial_match = 0;
229a35a7e76SEric Melville 
230829a229dSAndrey A. Chernov 	optind++;
231829a229dSAndrey A. Chernov 
232829a229dSAndrey A. Chernov 	if ((has_equal = strchr(current_argv, '=')) != NULL) {
233829a229dSAndrey A. Chernov 		/* argument found (--option=arg) */
234829a229dSAndrey A. Chernov 		current_argv_len = has_equal - current_argv;
235829a229dSAndrey A. Chernov 		has_equal++;
236829a229dSAndrey A. Chernov 	} else
237829a229dSAndrey A. Chernov 		current_argv_len = strlen(current_argv);
238829a229dSAndrey A. Chernov 
239829a229dSAndrey A. Chernov 	for (i = 0; long_options[i].name; i++) {
240829a229dSAndrey A. Chernov 		/* find matching long option */
241829a229dSAndrey A. Chernov 		if (strncmp(current_argv, long_options[i].name,
242829a229dSAndrey A. Chernov 		    current_argv_len))
243829a229dSAndrey A. Chernov 			continue;
244829a229dSAndrey A. Chernov 
245829a229dSAndrey A. Chernov 		if (strlen(long_options[i].name) == current_argv_len) {
246829a229dSAndrey A. Chernov 			/* exact match */
247829a229dSAndrey A. Chernov 			match = i;
248*ed4fbbd5SAndrey A. Chernov 			exact_match = 1;
249829a229dSAndrey A. Chernov 			break;
250829a229dSAndrey A. Chernov 		}
251829a229dSAndrey A. Chernov 		/*
252829a229dSAndrey A. Chernov 		 * If this is a known short option, don't allow
253829a229dSAndrey A. Chernov 		 * a partial match of a single character.
254829a229dSAndrey A. Chernov 		 */
255*ed4fbbd5SAndrey A. Chernov 		if (short_too &&
256*ed4fbbd5SAndrey A. Chernov 		    (!(flags & FLAG_LONGONLY) || current_argv_len == 1))
257829a229dSAndrey A. Chernov 			continue;
258829a229dSAndrey A. Chernov 
259*ed4fbbd5SAndrey A. Chernov 		if (match == -1)        /* first partial match */
260829a229dSAndrey A. Chernov 			match = i;
261*ed4fbbd5SAndrey A. Chernov 		else if ((flags & FLAG_LONGONLY) ||
262*ed4fbbd5SAndrey A. Chernov 			 long_options[i].has_arg !=
263*ed4fbbd5SAndrey A. Chernov 			     long_options[match].has_arg ||
264*ed4fbbd5SAndrey A. Chernov 			 long_options[i].flag != long_options[match].flag ||
265*ed4fbbd5SAndrey A. Chernov 			 long_options[i].val != long_options[match].val)
266*ed4fbbd5SAndrey A. Chernov 			second_partial_match = 1;
267*ed4fbbd5SAndrey A. Chernov 	}
268*ed4fbbd5SAndrey A. Chernov 	if (!exact_match && second_partial_match) {
269829a229dSAndrey A. Chernov 		/* ambiguous abbreviation */
270829a229dSAndrey A. Chernov 		if (PRINT_ERROR)
2719f06a99eSAndrey A. Chernov 			warnx(ambig,
2729f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2739f06a99eSAndrey A. Chernov 			     current_dash,
2749f06a99eSAndrey A. Chernov #endif
2759f06a99eSAndrey A. Chernov 			     (int)current_argv_len,
276829a229dSAndrey A. Chernov 			     current_argv);
277829a229dSAndrey A. Chernov 		optopt = 0;
278829a229dSAndrey A. Chernov 		return (BADCH);
279829a229dSAndrey A. Chernov 	}
280829a229dSAndrey A. Chernov 	if (match != -1) {		/* option found */
281829a229dSAndrey A. Chernov 		if (long_options[match].has_arg == no_argument
282829a229dSAndrey A. Chernov 		    && has_equal) {
283829a229dSAndrey A. Chernov 			if (PRINT_ERROR)
2849f06a99eSAndrey A. Chernov 				warnx(noarg,
2859f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2869f06a99eSAndrey A. Chernov 				     current_dash,
2879f06a99eSAndrey A. Chernov #endif
2889f06a99eSAndrey A. Chernov 				     (int)current_argv_len,
289829a229dSAndrey A. Chernov 				     current_argv);
290829a229dSAndrey A. Chernov 			/*
291829a229dSAndrey A. Chernov 			 * XXX: GNU sets optopt to val regardless of flag
292829a229dSAndrey A. Chernov 			 */
293829a229dSAndrey A. Chernov 			if (long_options[match].flag == NULL)
294829a229dSAndrey A. Chernov 				optopt = long_options[match].val;
295829a229dSAndrey A. Chernov 			else
296829a229dSAndrey A. Chernov 				optopt = 0;
29788485399SAndrey A. Chernov #ifdef GNU_COMPATIBLE
29888485399SAndrey A. Chernov 			return (BADCH);
29988485399SAndrey A. Chernov #else
300829a229dSAndrey A. Chernov 			return (BADARG);
30188485399SAndrey A. Chernov #endif
302829a229dSAndrey A. Chernov 		}
303829a229dSAndrey A. Chernov 		if (long_options[match].has_arg == required_argument ||
304829a229dSAndrey A. Chernov 		    long_options[match].has_arg == optional_argument) {
305829a229dSAndrey A. Chernov 			if (has_equal)
306829a229dSAndrey A. Chernov 				optarg = has_equal;
307829a229dSAndrey A. Chernov 			else if (long_options[match].has_arg ==
308829a229dSAndrey A. Chernov 			    required_argument) {
309829a229dSAndrey A. Chernov 				/*
310829a229dSAndrey A. Chernov 				 * optional argument doesn't use next nargv
311829a229dSAndrey A. Chernov 				 */
312829a229dSAndrey A. Chernov 				optarg = nargv[optind++];
313829a229dSAndrey A. Chernov 			}
314829a229dSAndrey A. Chernov 		}
315829a229dSAndrey A. Chernov 		if ((long_options[match].has_arg == required_argument)
316829a229dSAndrey A. Chernov 		    && (optarg == NULL)) {
317829a229dSAndrey A. Chernov 			/*
318829a229dSAndrey A. Chernov 			 * Missing argument; leading ':' indicates no error
319829a229dSAndrey A. Chernov 			 * should be generated.
320829a229dSAndrey A. Chernov 			 */
321829a229dSAndrey A. Chernov 			if (PRINT_ERROR)
322829a229dSAndrey A. Chernov 				warnx(recargstring,
3239f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
3249f06a99eSAndrey A. Chernov 				    current_dash,
3259f06a99eSAndrey A. Chernov #endif
326829a229dSAndrey A. Chernov 				    current_argv);
327829a229dSAndrey A. Chernov 			/*
328829a229dSAndrey A. Chernov 			 * XXX: GNU sets optopt to val regardless of flag
329829a229dSAndrey A. Chernov 			 */
330829a229dSAndrey A. Chernov 			if (long_options[match].flag == NULL)
331829a229dSAndrey A. Chernov 				optopt = long_options[match].val;
332829a229dSAndrey A. Chernov 			else
333829a229dSAndrey A. Chernov 				optopt = 0;
334829a229dSAndrey A. Chernov 			--optind;
335829a229dSAndrey A. Chernov 			return (BADARG);
336829a229dSAndrey A. Chernov 		}
337829a229dSAndrey A. Chernov 	} else {			/* unknown option */
338829a229dSAndrey A. Chernov 		if (short_too) {
339829a229dSAndrey A. Chernov 			--optind;
340829a229dSAndrey A. Chernov 			return (-1);
341829a229dSAndrey A. Chernov 		}
342829a229dSAndrey A. Chernov 		if (PRINT_ERROR)
3439f06a99eSAndrey A. Chernov 			warnx(illoptstring,
3449f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
3459f06a99eSAndrey A. Chernov 			      current_dash,
3469f06a99eSAndrey A. Chernov #endif
3479f06a99eSAndrey A. Chernov 			      current_argv);
348829a229dSAndrey A. Chernov 		optopt = 0;
349829a229dSAndrey A. Chernov 		return (BADCH);
350829a229dSAndrey A. Chernov 	}
351829a229dSAndrey A. Chernov 	if (idx)
352829a229dSAndrey A. Chernov 		*idx = match;
353829a229dSAndrey A. Chernov 	if (long_options[match].flag) {
354829a229dSAndrey A. Chernov 		*long_options[match].flag = long_options[match].val;
355829a229dSAndrey A. Chernov 		return (0);
356829a229dSAndrey A. Chernov 	} else
357829a229dSAndrey A. Chernov 		return (long_options[match].val);
358829a229dSAndrey A. Chernov }
359a35a7e76SEric Melville 
360a35a7e76SEric Melville /*
361829a229dSAndrey A. Chernov  * getopt_internal --
362829a229dSAndrey A. Chernov  *	Parse argc/argv argument vector.  Called by user level routines.
363829a229dSAndrey A. Chernov  */
364829a229dSAndrey A. Chernov static int
365829a229dSAndrey A. Chernov getopt_internal(int nargc, char * const *nargv, const char *options,
366829a229dSAndrey A. Chernov 	const struct option *long_options, int *idx, int flags)
367829a229dSAndrey A. Chernov {
368829a229dSAndrey A. Chernov 	char *oli;				/* option letter list index */
369829a229dSAndrey A. Chernov 	int optchar, short_too;
370f2fd86b7SAndrey A. Chernov 	int posixly_correct;
371829a229dSAndrey A. Chernov 
372829a229dSAndrey A. Chernov 	if (options == NULL)
373829a229dSAndrey A. Chernov 		return (-1);
374829a229dSAndrey A. Chernov 
375829a229dSAndrey A. Chernov 	/*
376829a229dSAndrey A. Chernov 	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
377829a229dSAndrey A. Chernov 	 * string begins with a '+'.
378829a229dSAndrey A. Chernov 	 */
379829a229dSAndrey A. Chernov 	posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
380f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
381ee43cb7aSAndrey A. Chernov 	if (*options == '-')
382829a229dSAndrey A. Chernov 		flags |= FLAG_ALLARGS;
383f2fd86b7SAndrey A. Chernov 	else if (posixly_correct || *options == '+')
384f2fd86b7SAndrey A. Chernov 		flags &= ~FLAG_PERMUTE;
385f2fd86b7SAndrey A. Chernov #else
386f2fd86b7SAndrey A. Chernov 	if (posixly_correct || *options == '+')
387f2fd86b7SAndrey A. Chernov 		flags &= ~FLAG_PERMUTE;
388f2fd86b7SAndrey A. Chernov 	else if (*options == '-')
389f2fd86b7SAndrey A. Chernov 		flags |= FLAG_ALLARGS;
390f2fd86b7SAndrey A. Chernov #endif
391829a229dSAndrey A. Chernov 	if (*options == '+' || *options == '-')
392829a229dSAndrey A. Chernov 		options++;
393829a229dSAndrey A. Chernov 
394829a229dSAndrey A. Chernov 	/*
395829a229dSAndrey A. Chernov 	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
396829a229dSAndrey A. Chernov 	 * XXX using optreset.  Work around this braindamage.
397a35a7e76SEric Melville 	 */
398a35a7e76SEric Melville 	if (optind == 0)
399829a229dSAndrey A. Chernov 		optind = optreset = 1;
400a35a7e76SEric Melville 
401829a229dSAndrey A. Chernov 	optarg = NULL;
402a35a7e76SEric Melville 	if (optreset)
403a35a7e76SEric Melville 		nonopt_start = nonopt_end = -1;
404a35a7e76SEric Melville start:
405a35a7e76SEric Melville 	if (optreset || !*place) {		/* update scanning pointer */
406a35a7e76SEric Melville 		optreset = 0;
407a35a7e76SEric Melville 		if (optind >= nargc) {          /* end of argument vector */
408a35a7e76SEric Melville 			place = EMSG;
409a35a7e76SEric Melville 			if (nonopt_end != -1) {
410a35a7e76SEric Melville 				/* do permutation, if we have to */
411a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
412a35a7e76SEric Melville 				    optind, nargv);
413a35a7e76SEric Melville 				optind -= nonopt_end - nonopt_start;
414a35a7e76SEric Melville 			}
415a35a7e76SEric Melville 			else if (nonopt_start != -1) {
416a35a7e76SEric Melville 				/*
417a35a7e76SEric Melville 				 * If we skipped non-options, set optind
418a35a7e76SEric Melville 				 * to the first of them.
419a35a7e76SEric Melville 				 */
420a35a7e76SEric Melville 				optind = nonopt_start;
421a35a7e76SEric Melville 			}
422a35a7e76SEric Melville 			nonopt_start = nonopt_end = -1;
423829a229dSAndrey A. Chernov 			return (-1);
424a35a7e76SEric Melville 		}
425829a229dSAndrey A. Chernov 		if (*(place = nargv[optind]) != '-' ||
4263700175bSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4273700175bSAndrey A. Chernov 		    place[1] == '\0') {
4283700175bSAndrey A. Chernov #else
429829a229dSAndrey A. Chernov 		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
4303700175bSAndrey A. Chernov #endif
431829a229dSAndrey A. Chernov 			place = EMSG;		/* found non-option */
432829a229dSAndrey A. Chernov 			if (flags & FLAG_ALLARGS) {
433a35a7e76SEric Melville 				/*
434a35a7e76SEric Melville 				 * GNU extension:
435a35a7e76SEric Melville 				 * return non-option as argument to option 1
436a35a7e76SEric Melville 				 */
437a35a7e76SEric Melville 				optarg = nargv[optind++];
438829a229dSAndrey A. Chernov 				return (INORDER);
439a35a7e76SEric Melville 			}
440829a229dSAndrey A. Chernov 			if (!(flags & FLAG_PERMUTE)) {
441a35a7e76SEric Melville 				/*
442829a229dSAndrey A. Chernov 				 * If no permutation wanted, stop parsing
443829a229dSAndrey A. Chernov 				 * at first non-option.
444a35a7e76SEric Melville 				 */
445829a229dSAndrey A. Chernov 				return (-1);
446a35a7e76SEric Melville 			}
447a35a7e76SEric Melville 			/* do permutation */
448a35a7e76SEric Melville 			if (nonopt_start == -1)
449a35a7e76SEric Melville 				nonopt_start = optind;
450a35a7e76SEric Melville 			else if (nonopt_end != -1) {
451a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
452a35a7e76SEric Melville 				    optind, nargv);
453a35a7e76SEric Melville 				nonopt_start = optind -
454a35a7e76SEric Melville 				    (nonopt_end - nonopt_start);
455a35a7e76SEric Melville 				nonopt_end = -1;
456a35a7e76SEric Melville 			}
457a35a7e76SEric Melville 			optind++;
458a35a7e76SEric Melville 			/* process next argument */
459a35a7e76SEric Melville 			goto start;
460a35a7e76SEric Melville 		}
461a35a7e76SEric Melville 		if (nonopt_start != -1 && nonopt_end == -1)
462a35a7e76SEric Melville 			nonopt_end = optind;
463829a229dSAndrey A. Chernov 
464829a229dSAndrey A. Chernov 		/*
465829a229dSAndrey A. Chernov 		 * If we have "-" do nothing, if "--" we are done.
466829a229dSAndrey A. Chernov 		 */
467829a229dSAndrey A. Chernov 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
468829a229dSAndrey A. Chernov 			optind++;
469829a229dSAndrey A. Chernov 			place = EMSG;
470829a229dSAndrey A. Chernov 			/*
471829a229dSAndrey A. Chernov 			 * We found an option (--), so if we skipped
472829a229dSAndrey A. Chernov 			 * non-options, we have to permute.
473829a229dSAndrey A. Chernov 			 */
474829a229dSAndrey A. Chernov 			if (nonopt_end != -1) {
475829a229dSAndrey A. Chernov 				permute_args(nonopt_start, nonopt_end,
476829a229dSAndrey A. Chernov 				    optind, nargv);
477829a229dSAndrey A. Chernov 				optind -= nonopt_end - nonopt_start;
478829a229dSAndrey A. Chernov 			}
479829a229dSAndrey A. Chernov 			nonopt_start = nonopt_end = -1;
480829a229dSAndrey A. Chernov 			return (-1);
481a35a7e76SEric Melville 		}
482a35a7e76SEric Melville 	}
483829a229dSAndrey A. Chernov 
484829a229dSAndrey A. Chernov 	/*
485829a229dSAndrey A. Chernov 	 * Check long options if:
486829a229dSAndrey A. Chernov 	 *  1) we were passed some
487829a229dSAndrey A. Chernov 	 *  2) the arg is not just "-"
488829a229dSAndrey A. Chernov 	 *  3) either the arg starts with -- we are getopt_long_only()
489829a229dSAndrey A. Chernov 	 */
490829a229dSAndrey A. Chernov 	if (long_options != NULL && place != nargv[optind] &&
491829a229dSAndrey A. Chernov 	    (*place == '-' || (flags & FLAG_LONGONLY))) {
492829a229dSAndrey A. Chernov 		short_too = 0;
4939f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4949f06a99eSAndrey A. Chernov 		dash_prefix = D_PREFIX;
4959f06a99eSAndrey A. Chernov #endif
4969f06a99eSAndrey A. Chernov 		if (*place == '-') {
497829a229dSAndrey A. Chernov 			place++;		/* --foo long option */
4989f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4999f06a99eSAndrey A. Chernov 			dash_prefix = DD_PREFIX;
5009f06a99eSAndrey A. Chernov #endif
5019f06a99eSAndrey A. Chernov 		} else if (*place != ':' && strchr(options, *place) != NULL)
502829a229dSAndrey A. Chernov 			short_too = 1;		/* could be short option too */
503829a229dSAndrey A. Chernov 
504829a229dSAndrey A. Chernov 		optchar = parse_long_options(nargv, options, long_options,
505*ed4fbbd5SAndrey A. Chernov 		    idx, short_too, flags);
506829a229dSAndrey A. Chernov 		if (optchar != -1) {
507829a229dSAndrey A. Chernov 			place = EMSG;
508829a229dSAndrey A. Chernov 			return (optchar);
509829a229dSAndrey A. Chernov 		}
510829a229dSAndrey A. Chernov 	}
511829a229dSAndrey A. Chernov 
512a35a7e76SEric Melville 	if ((optchar = (int)*place++) == (int)':' ||
513829a229dSAndrey A. Chernov 	    (optchar == (int)'-' && *place != '\0') ||
514829a229dSAndrey A. Chernov 	    (oli = strchr(options, optchar)) == NULL) {
515829a229dSAndrey A. Chernov 		/*
516829a229dSAndrey A. Chernov 		 * If the user specified "-" and  '-' isn't listed in
517829a229dSAndrey A. Chernov 		 * options, return -1 (non-option) as per POSIX.
518829a229dSAndrey A. Chernov 		 * Otherwise, it is an unknown option character (or ':').
519829a229dSAndrey A. Chernov 		 */
520829a229dSAndrey A. Chernov 		if (optchar == (int)'-' && *place == '\0')
521829a229dSAndrey A. Chernov 			return (-1);
522a35a7e76SEric Melville 		if (!*place)
523a35a7e76SEric Melville 			++optind;
524f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
525f2fd86b7SAndrey A. Chernov 		if (PRINT_ERROR)
526f2fd86b7SAndrey A. Chernov 			warnx(posixly_correct ? illoptchar : gnuoptchar,
527f2fd86b7SAndrey A. Chernov 			      optchar);
528f2fd86b7SAndrey A. Chernov #else
529a35a7e76SEric Melville 		if (PRINT_ERROR)
530a35a7e76SEric Melville 			warnx(illoptchar, optchar);
531f2fd86b7SAndrey A. Chernov #endif
532a35a7e76SEric Melville 		optopt = optchar;
533829a229dSAndrey A. Chernov 		return (BADCH);
534a35a7e76SEric Melville 	}
535829a229dSAndrey A. Chernov 	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
536829a229dSAndrey A. Chernov 		/* -W long-option */
537829a229dSAndrey A. Chernov 		if (*place)			/* no space */
538829a229dSAndrey A. Chernov 			/* NOTHING */;
539829a229dSAndrey A. Chernov 		else if (++optind >= nargc) {	/* no arg */
540a35a7e76SEric Melville 			place = EMSG;
541a35a7e76SEric Melville 			if (PRINT_ERROR)
542a35a7e76SEric Melville 				warnx(recargchar, optchar);
543a35a7e76SEric Melville 			optopt = optchar;
544829a229dSAndrey A. Chernov 			return (BADARG);
545a35a7e76SEric Melville 		} else				/* white space */
546a35a7e76SEric Melville 			place = nargv[optind];
5479f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
5489f06a99eSAndrey A. Chernov 		dash_prefix = W_PREFIX;
5499f06a99eSAndrey A. Chernov #endif
550829a229dSAndrey A. Chernov 		optchar = parse_long_options(nargv, options, long_options,
551*ed4fbbd5SAndrey A. Chernov 		    idx, 0, flags);
552829a229dSAndrey A. Chernov 		place = EMSG;
553829a229dSAndrey A. Chernov 		return (optchar);
554a35a7e76SEric Melville 	}
555a35a7e76SEric Melville 	if (*++oli != ':') {			/* doesn't take argument */
556a35a7e76SEric Melville 		if (!*place)
557a35a7e76SEric Melville 			++optind;
558a35a7e76SEric Melville 	} else {				/* takes (optional) argument */
559a35a7e76SEric Melville 		optarg = NULL;
560a35a7e76SEric Melville 		if (*place)			/* no white space */
561a35a7e76SEric Melville 			optarg = place;
562a35a7e76SEric Melville 		/* XXX: disable test for :: if PC? (GNU doesn't) */
563a35a7e76SEric Melville 		else if (oli[1] != ':') {	/* arg not optional */
564a35a7e76SEric Melville 			if (++optind >= nargc) {	/* no arg */
565a35a7e76SEric Melville 				place = EMSG;
566a35a7e76SEric Melville 				if (PRINT_ERROR)
567a35a7e76SEric Melville 					warnx(recargchar, optchar);
568a35a7e76SEric Melville 				optopt = optchar;
569829a229dSAndrey A. Chernov 				return (BADARG);
570a35a7e76SEric Melville 			} else
571a35a7e76SEric Melville 				optarg = nargv[optind];
572829a229dSAndrey A. Chernov 		} else if (!(flags & FLAG_PERMUTE)) {
573829a229dSAndrey A. Chernov 			/*
574829a229dSAndrey A. Chernov 			 * If permutation is disabled, we can accept an
575829a229dSAndrey A. Chernov 			 * optional arg separated by whitespace.
576829a229dSAndrey A. Chernov 			 */
577829a229dSAndrey A. Chernov 			if (optind + 1 < nargc)
578829a229dSAndrey A. Chernov 				optarg = nargv[++optind];
579a35a7e76SEric Melville 		}
580a35a7e76SEric Melville 		place = EMSG;
581a35a7e76SEric Melville 		++optind;
582a35a7e76SEric Melville 	}
583a35a7e76SEric Melville 	/* dump back option letter */
584829a229dSAndrey A. Chernov 	return (optchar);
585a35a7e76SEric Melville }
586a35a7e76SEric Melville 
587a35a7e76SEric Melville #ifdef REPLACE_GETOPT
588a35a7e76SEric Melville /*
589a35a7e76SEric Melville  * getopt --
590a35a7e76SEric Melville  *	Parse argc/argv argument vector.
591a35a7e76SEric Melville  *
592829a229dSAndrey A. Chernov  * [eventually this will replace the BSD getopt]
593a35a7e76SEric Melville  */
594a35a7e76SEric Melville int
595829a229dSAndrey A. Chernov getopt(int nargc, char * const *nargv, const char *options)
596a35a7e76SEric Melville {
597a35a7e76SEric Melville 
598a35a7e76SEric Melville 	/*
599829a229dSAndrey A. Chernov 	 * We don't pass FLAG_PERMUTE to getopt_internal() since
600829a229dSAndrey A. Chernov 	 * the BSD getopt(3) (unlike GNU) has never done this.
601829a229dSAndrey A. Chernov 	 *
602829a229dSAndrey A. Chernov 	 * Furthermore, since many privileged programs call getopt()
603829a229dSAndrey A. Chernov 	 * before dropping privileges it makes sense to keep things
604829a229dSAndrey A. Chernov 	 * as simple (and bug-free) as possible.
605a35a7e76SEric Melville 	 */
606829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
607a35a7e76SEric Melville }
608829a229dSAndrey A. Chernov #endif /* REPLACE_GETOPT */
609a35a7e76SEric Melville 
610a35a7e76SEric Melville /*
611a35a7e76SEric Melville  * getopt_long --
612a35a7e76SEric Melville  *	Parse argc/argv argument vector.
613a35a7e76SEric Melville  */
614a35a7e76SEric Melville int
615a35a7e76SEric Melville getopt_long(nargc, nargv, options, long_options, idx)
616a35a7e76SEric Melville 	int nargc;
617a35a7e76SEric Melville 	char * const *nargv;
618a35a7e76SEric Melville 	const char *options;
619a35a7e76SEric Melville 	const struct option *long_options;
620a35a7e76SEric Melville 	int *idx;
621a35a7e76SEric Melville {
622a35a7e76SEric Melville 
623829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, long_options, idx,
624829a229dSAndrey A. Chernov 	    FLAG_PERMUTE));
625829a229dSAndrey A. Chernov }
626a35a7e76SEric Melville 
627a35a7e76SEric Melville /*
628829a229dSAndrey A. Chernov  * getopt_long_only --
629829a229dSAndrey A. Chernov  *	Parse argc/argv argument vector.
630a35a7e76SEric Melville  */
631829a229dSAndrey A. Chernov int
632829a229dSAndrey A. Chernov getopt_long_only(nargc, nargv, options, long_options, idx)
633829a229dSAndrey A. Chernov 	int nargc;
634829a229dSAndrey A. Chernov 	char * const *nargv;
635829a229dSAndrey A. Chernov 	const char *options;
636829a229dSAndrey A. Chernov 	const struct option *long_options;
637829a229dSAndrey A. Chernov 	int *idx;
638829a229dSAndrey A. Chernov {
639a35a7e76SEric Melville 
640829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, long_options, idx,
641829a229dSAndrey A. Chernov 	    FLAG_PERMUTE|FLAG_LONGONLY));
642a35a7e76SEric Melville }
643