xref: /freebsd-12.1/lib/libc/stdlib/getopt_long.c (revision f2fd86b7)
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 
73*f2fd86b7SAndrey A. Chernov #define GNU_COMPATIBLE		/* Be more compatible, configure's use us! */
74*f2fd86b7SAndrey A. Chernov 
75*f2fd86b7SAndrey 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 
100829a229dSAndrey A. Chernov static int getopt_internal(int, char * const *, const char *,
101829a229dSAndrey A. Chernov 			   const struct option *, int *, int);
102829a229dSAndrey A. Chernov static int parse_long_options(char * const *, const char *,
103829a229dSAndrey A. Chernov 			      const struct option *, int *, int);
104e6fc380cSAlfred Perlstein static int gcd(int, int);
105e6fc380cSAlfred Perlstein static void permute_args(int, int, int, char * const *);
106a35a7e76SEric Melville 
107a35a7e76SEric Melville static char *place = EMSG; /* option letter processing */
108a35a7e76SEric Melville 
109a35a7e76SEric Melville /* XXX: set optreset to 1 rather than these two */
110a35a7e76SEric Melville static int nonopt_start = -1; /* first non option argument (for permute) */
111a35a7e76SEric Melville static int nonopt_end = -1;   /* first option after non options (for permute) */
112a35a7e76SEric Melville 
113a35a7e76SEric Melville /* Error messages */
114a35a7e76SEric Melville static const char recargchar[] = "option requires an argument -- %c";
115*f2fd86b7SAndrey A. Chernov /* From P1003.2 */
116*f2fd86b7SAndrey A. Chernov static const char illoptchar[] = "illegal option -- %c";
117*f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
118*f2fd86b7SAndrey A. Chernov static const char gnuoptchar[] = "invalid option -- %c";
119*f2fd86b7SAndrey A. Chernov 
120*f2fd86b7SAndrey A. Chernov static const char recargstring[] = "option `--%s' requires an argument";
121*f2fd86b7SAndrey A. Chernov static const char ambig[] = "option `--%.*s' is ambiguous";
122*f2fd86b7SAndrey A. Chernov static const char noarg[] = "option `--%.*s' doesn't allow an argument";
123*f2fd86b7SAndrey A. Chernov static const char illoptstring[] = "unrecognized option `--%s'";
124*f2fd86b7SAndrey A. Chernov #else
125a35a7e76SEric Melville static const char recargstring[] = "option requires an argument -- %s";
126a35a7e76SEric Melville static const char ambig[] = "ambiguous option -- %.*s";
127a35a7e76SEric Melville static const char noarg[] = "option doesn't take an argument -- %.*s";
128a35a7e76SEric Melville static const char illoptstring[] = "unknown option -- %s";
129*f2fd86b7SAndrey A. Chernov #endif
130a35a7e76SEric Melville 
131a35a7e76SEric Melville /*
132a35a7e76SEric Melville  * Compute the greatest common divisor of a and b.
133a35a7e76SEric Melville  */
134a35a7e76SEric Melville static int
135829a229dSAndrey A. Chernov gcd(int a, int b)
136a35a7e76SEric Melville {
137a35a7e76SEric Melville 	int c;
138a35a7e76SEric Melville 
139a35a7e76SEric Melville 	c = a % b;
140a35a7e76SEric Melville 	while (c != 0) {
141a35a7e76SEric Melville 		a = b;
142a35a7e76SEric Melville 		b = c;
143a35a7e76SEric Melville 		c = a % b;
144a35a7e76SEric Melville 	}
145a35a7e76SEric Melville 
146829a229dSAndrey A. Chernov 	return (b);
147a35a7e76SEric Melville }
148a35a7e76SEric Melville 
149a35a7e76SEric Melville /*
150a35a7e76SEric Melville  * Exchange the block from nonopt_start to nonopt_end with the block
151a35a7e76SEric Melville  * from nonopt_end to opt_end (keeping the same order of arguments
152a35a7e76SEric Melville  * in each block).
153a35a7e76SEric Melville  */
154a35a7e76SEric Melville static void
155829a229dSAndrey A. Chernov permute_args(int panonopt_start, int panonopt_end, int opt_end,
156829a229dSAndrey A. Chernov 	char * const *nargv)
157a35a7e76SEric Melville {
158a35a7e76SEric Melville 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
159a35a7e76SEric Melville 	char *swap;
160a35a7e76SEric Melville 
161a35a7e76SEric Melville 	/*
162a35a7e76SEric Melville 	 * compute lengths of blocks and number and size of cycles
163a35a7e76SEric Melville 	 */
164a35a7e76SEric Melville 	nnonopts = panonopt_end - panonopt_start;
165a35a7e76SEric Melville 	nopts = opt_end - panonopt_end;
166a35a7e76SEric Melville 	ncycle = gcd(nnonopts, nopts);
167a35a7e76SEric Melville 	cyclelen = (opt_end - panonopt_start) / ncycle;
168a35a7e76SEric Melville 
169a35a7e76SEric Melville 	for (i = 0; i < ncycle; i++) {
170a35a7e76SEric Melville 		cstart = panonopt_end+i;
171a35a7e76SEric Melville 		pos = cstart;
172a35a7e76SEric Melville 		for (j = 0; j < cyclelen; j++) {
173a35a7e76SEric Melville 			if (pos >= panonopt_end)
174a35a7e76SEric Melville 				pos -= nnonopts;
175a35a7e76SEric Melville 			else
176a35a7e76SEric Melville 				pos += nopts;
177a35a7e76SEric Melville 			swap = nargv[pos];
178a35a7e76SEric Melville 			/* LINTED const cast */
179a35a7e76SEric Melville 			((char **) nargv)[pos] = nargv[cstart];
180a35a7e76SEric Melville 			/* LINTED const cast */
181a35a7e76SEric Melville 			((char **)nargv)[cstart] = swap;
182a35a7e76SEric Melville 		}
183a35a7e76SEric Melville 	}
184a35a7e76SEric Melville }
185a35a7e76SEric Melville 
186a35a7e76SEric Melville /*
187829a229dSAndrey A. Chernov  * parse_long_options --
188829a229dSAndrey A. Chernov  *	Parse long options in argc/argv argument vector.
189829a229dSAndrey A. Chernov  * Returns -1 if short_too is set and the option does not match long_options.
190a35a7e76SEric Melville  */
191a35a7e76SEric Melville static int
192829a229dSAndrey A. Chernov parse_long_options(char * const *nargv, const char *options,
193829a229dSAndrey A. Chernov 	const struct option *long_options, int *idx, int short_too)
194a35a7e76SEric Melville {
195829a229dSAndrey A. Chernov 	char *current_argv, *has_equal;
196829a229dSAndrey A. Chernov 	size_t current_argv_len;
197829a229dSAndrey A. Chernov 	int i, match;
198a35a7e76SEric Melville 
199829a229dSAndrey A. Chernov 	current_argv = place;
200829a229dSAndrey A. Chernov 	match = -1;
201a35a7e76SEric Melville 
202829a229dSAndrey A. Chernov 	optind++;
203829a229dSAndrey A. Chernov 
204829a229dSAndrey A. Chernov 	if ((has_equal = strchr(current_argv, '=')) != NULL) {
205829a229dSAndrey A. Chernov 		/* argument found (--option=arg) */
206829a229dSAndrey A. Chernov 		current_argv_len = has_equal - current_argv;
207829a229dSAndrey A. Chernov 		has_equal++;
208829a229dSAndrey A. Chernov 	} else
209829a229dSAndrey A. Chernov 		current_argv_len = strlen(current_argv);
210829a229dSAndrey A. Chernov 
211829a229dSAndrey A. Chernov 	for (i = 0; long_options[i].name; i++) {
212829a229dSAndrey A. Chernov 		/* find matching long option */
213829a229dSAndrey A. Chernov 		if (strncmp(current_argv, long_options[i].name,
214829a229dSAndrey A. Chernov 		    current_argv_len))
215829a229dSAndrey A. Chernov 			continue;
216829a229dSAndrey A. Chernov 
217829a229dSAndrey A. Chernov 		if (strlen(long_options[i].name) == current_argv_len) {
218829a229dSAndrey A. Chernov 			/* exact match */
219829a229dSAndrey A. Chernov 			match = i;
220829a229dSAndrey A. Chernov 			break;
221829a229dSAndrey A. Chernov 		}
222829a229dSAndrey A. Chernov 		/*
223829a229dSAndrey A. Chernov 		 * If this is a known short option, don't allow
224829a229dSAndrey A. Chernov 		 * a partial match of a single character.
225829a229dSAndrey A. Chernov 		 */
226829a229dSAndrey A. Chernov 		if (short_too && current_argv_len == 1)
227829a229dSAndrey A. Chernov 			continue;
228829a229dSAndrey A. Chernov 
229829a229dSAndrey A. Chernov 		if (match == -1)	/* partial match */
230829a229dSAndrey A. Chernov 			match = i;
231829a229dSAndrey A. Chernov 		else {
232829a229dSAndrey A. Chernov 			/* ambiguous abbreviation */
233829a229dSAndrey A. Chernov 			if (PRINT_ERROR)
234829a229dSAndrey A. Chernov 				warnx(ambig, (int)current_argv_len,
235829a229dSAndrey A. Chernov 				     current_argv);
236829a229dSAndrey A. Chernov 			optopt = 0;
237829a229dSAndrey A. Chernov 			return (BADCH);
238829a229dSAndrey A. Chernov 		}
239829a229dSAndrey A. Chernov 	}
240829a229dSAndrey A. Chernov 	if (match != -1) {		/* option found */
241829a229dSAndrey A. Chernov 		if (long_options[match].has_arg == no_argument
242829a229dSAndrey A. Chernov 		    && has_equal) {
243829a229dSAndrey A. Chernov 			if (PRINT_ERROR)
244829a229dSAndrey A. Chernov 				warnx(noarg, (int)current_argv_len,
245829a229dSAndrey A. Chernov 				     current_argv);
246829a229dSAndrey A. Chernov 			/*
247829a229dSAndrey A. Chernov 			 * XXX: GNU sets optopt to val regardless of flag
248829a229dSAndrey A. Chernov 			 */
249829a229dSAndrey A. Chernov 			if (long_options[match].flag == NULL)
250829a229dSAndrey A. Chernov 				optopt = long_options[match].val;
251829a229dSAndrey A. Chernov 			else
252829a229dSAndrey A. Chernov 				optopt = 0;
253829a229dSAndrey A. Chernov 			return (BADARG);
254829a229dSAndrey A. Chernov 		}
255829a229dSAndrey A. Chernov 		if (long_options[match].has_arg == required_argument ||
256829a229dSAndrey A. Chernov 		    long_options[match].has_arg == optional_argument) {
257829a229dSAndrey A. Chernov 			if (has_equal)
258829a229dSAndrey A. Chernov 				optarg = has_equal;
259829a229dSAndrey A. Chernov 			else if (long_options[match].has_arg ==
260829a229dSAndrey A. Chernov 			    required_argument) {
261829a229dSAndrey A. Chernov 				/*
262829a229dSAndrey A. Chernov 				 * optional argument doesn't use next nargv
263829a229dSAndrey A. Chernov 				 */
264829a229dSAndrey A. Chernov 				optarg = nargv[optind++];
265829a229dSAndrey A. Chernov 			}
266829a229dSAndrey A. Chernov 		}
267829a229dSAndrey A. Chernov 		if ((long_options[match].has_arg == required_argument)
268829a229dSAndrey A. Chernov 		    && (optarg == NULL)) {
269829a229dSAndrey A. Chernov 			/*
270829a229dSAndrey A. Chernov 			 * Missing argument; leading ':' indicates no error
271829a229dSAndrey A. Chernov 			 * should be generated.
272829a229dSAndrey A. Chernov 			 */
273829a229dSAndrey A. Chernov 			if (PRINT_ERROR)
274829a229dSAndrey A. Chernov 				warnx(recargstring,
275829a229dSAndrey A. Chernov 				    current_argv);
276829a229dSAndrey A. Chernov 			/*
277829a229dSAndrey A. Chernov 			 * XXX: GNU sets optopt to val regardless of flag
278829a229dSAndrey A. Chernov 			 */
279829a229dSAndrey A. Chernov 			if (long_options[match].flag == NULL)
280829a229dSAndrey A. Chernov 				optopt = long_options[match].val;
281829a229dSAndrey A. Chernov 			else
282829a229dSAndrey A. Chernov 				optopt = 0;
283829a229dSAndrey A. Chernov 			--optind;
284829a229dSAndrey A. Chernov 			return (BADARG);
285829a229dSAndrey A. Chernov 		}
286829a229dSAndrey A. Chernov 	} else {			/* unknown option */
287829a229dSAndrey A. Chernov 		if (short_too) {
288829a229dSAndrey A. Chernov 			--optind;
289829a229dSAndrey A. Chernov 			return (-1);
290829a229dSAndrey A. Chernov 		}
291829a229dSAndrey A. Chernov 		if (PRINT_ERROR)
292829a229dSAndrey A. Chernov 			warnx(illoptstring, current_argv);
293829a229dSAndrey A. Chernov 		optopt = 0;
294829a229dSAndrey A. Chernov 		return (BADCH);
295829a229dSAndrey A. Chernov 	}
296829a229dSAndrey A. Chernov 	if (idx)
297829a229dSAndrey A. Chernov 		*idx = match;
298829a229dSAndrey A. Chernov 	if (long_options[match].flag) {
299829a229dSAndrey A. Chernov 		*long_options[match].flag = long_options[match].val;
300829a229dSAndrey A. Chernov 		return (0);
301829a229dSAndrey A. Chernov 	} else
302829a229dSAndrey A. Chernov 		return (long_options[match].val);
303829a229dSAndrey A. Chernov }
304a35a7e76SEric Melville 
305a35a7e76SEric Melville /*
306829a229dSAndrey A. Chernov  * getopt_internal --
307829a229dSAndrey A. Chernov  *	Parse argc/argv argument vector.  Called by user level routines.
308829a229dSAndrey A. Chernov  */
309829a229dSAndrey A. Chernov static int
310829a229dSAndrey A. Chernov getopt_internal(int nargc, char * const *nargv, const char *options,
311829a229dSAndrey A. Chernov 	const struct option *long_options, int *idx, int flags)
312829a229dSAndrey A. Chernov {
313829a229dSAndrey A. Chernov 	char *oli;				/* option letter list index */
314829a229dSAndrey A. Chernov 	int optchar, short_too;
315*f2fd86b7SAndrey A. Chernov 	int posixly_correct;
316829a229dSAndrey A. Chernov 
317829a229dSAndrey A. Chernov 	if (options == NULL)
318829a229dSAndrey A. Chernov 		return (-1);
319829a229dSAndrey A. Chernov 
320829a229dSAndrey A. Chernov 	/*
321829a229dSAndrey A. Chernov 	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
322829a229dSAndrey A. Chernov 	 * string begins with a '+'.
323829a229dSAndrey A. Chernov 	 */
324829a229dSAndrey A. Chernov 	posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
325*f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
326ee43cb7aSAndrey A. Chernov 	if (*options == '-')
327829a229dSAndrey A. Chernov 		flags |= FLAG_ALLARGS;
328*f2fd86b7SAndrey A. Chernov 	else if (posixly_correct || *options == '+')
329*f2fd86b7SAndrey A. Chernov 		flags &= ~FLAG_PERMUTE;
330*f2fd86b7SAndrey A. Chernov #else
331*f2fd86b7SAndrey A. Chernov 	if (posixly_correct || *options == '+')
332*f2fd86b7SAndrey A. Chernov 		flags &= ~FLAG_PERMUTE;
333*f2fd86b7SAndrey A. Chernov 	else if (*options == '-')
334*f2fd86b7SAndrey A. Chernov 		flags |= FLAG_ALLARGS;
335*f2fd86b7SAndrey A. Chernov #endif
336829a229dSAndrey A. Chernov 	if (*options == '+' || *options == '-')
337829a229dSAndrey A. Chernov 		options++;
338829a229dSAndrey A. Chernov 
339829a229dSAndrey A. Chernov 	/*
340829a229dSAndrey A. Chernov 	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
341829a229dSAndrey A. Chernov 	 * XXX using optreset.  Work around this braindamage.
342a35a7e76SEric Melville 	 */
343a35a7e76SEric Melville 	if (optind == 0)
344829a229dSAndrey A. Chernov 		optind = optreset = 1;
345a35a7e76SEric Melville 
346829a229dSAndrey A. Chernov 	optarg = NULL;
347a35a7e76SEric Melville 	if (optreset)
348a35a7e76SEric Melville 		nonopt_start = nonopt_end = -1;
349a35a7e76SEric Melville start:
350a35a7e76SEric Melville 	if (optreset || !*place) {		/* update scanning pointer */
351a35a7e76SEric Melville 		optreset = 0;
352a35a7e76SEric Melville 		if (optind >= nargc) {          /* end of argument vector */
353a35a7e76SEric Melville 			place = EMSG;
354a35a7e76SEric Melville 			if (nonopt_end != -1) {
355a35a7e76SEric Melville 				/* do permutation, if we have to */
356a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
357a35a7e76SEric Melville 				    optind, nargv);
358a35a7e76SEric Melville 				optind -= nonopt_end - nonopt_start;
359a35a7e76SEric Melville 			}
360a35a7e76SEric Melville 			else if (nonopt_start != -1) {
361a35a7e76SEric Melville 				/*
362a35a7e76SEric Melville 				 * If we skipped non-options, set optind
363a35a7e76SEric Melville 				 * to the first of them.
364a35a7e76SEric Melville 				 */
365a35a7e76SEric Melville 				optind = nonopt_start;
366a35a7e76SEric Melville 			}
367a35a7e76SEric Melville 			nonopt_start = nonopt_end = -1;
368829a229dSAndrey A. Chernov 			return (-1);
369a35a7e76SEric Melville 		}
370829a229dSAndrey A. Chernov 		if (*(place = nargv[optind]) != '-' ||
371829a229dSAndrey A. Chernov 		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
372829a229dSAndrey A. Chernov 			place = EMSG;		/* found non-option */
373829a229dSAndrey A. Chernov 			if (flags & FLAG_ALLARGS) {
374a35a7e76SEric Melville 				/*
375a35a7e76SEric Melville 				 * GNU extension:
376a35a7e76SEric Melville 				 * return non-option as argument to option 1
377a35a7e76SEric Melville 				 */
378a35a7e76SEric Melville 				optarg = nargv[optind++];
379829a229dSAndrey A. Chernov 				return (INORDER);
380a35a7e76SEric Melville 			}
381829a229dSAndrey A. Chernov 			if (!(flags & FLAG_PERMUTE)) {
382a35a7e76SEric Melville 				/*
383829a229dSAndrey A. Chernov 				 * If no permutation wanted, stop parsing
384829a229dSAndrey A. Chernov 				 * at first non-option.
385a35a7e76SEric Melville 				 */
386829a229dSAndrey A. Chernov 				return (-1);
387a35a7e76SEric Melville 			}
388a35a7e76SEric Melville 			/* do permutation */
389a35a7e76SEric Melville 			if (nonopt_start == -1)
390a35a7e76SEric Melville 				nonopt_start = optind;
391a35a7e76SEric Melville 			else if (nonopt_end != -1) {
392a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
393a35a7e76SEric Melville 				    optind, nargv);
394a35a7e76SEric Melville 				nonopt_start = optind -
395a35a7e76SEric Melville 				    (nonopt_end - nonopt_start);
396a35a7e76SEric Melville 				nonopt_end = -1;
397a35a7e76SEric Melville 			}
398a35a7e76SEric Melville 			optind++;
399a35a7e76SEric Melville 			/* process next argument */
400a35a7e76SEric Melville 			goto start;
401a35a7e76SEric Melville 		}
402a35a7e76SEric Melville 		if (nonopt_start != -1 && nonopt_end == -1)
403a35a7e76SEric Melville 			nonopt_end = optind;
404829a229dSAndrey A. Chernov 
405829a229dSAndrey A. Chernov 		/*
406829a229dSAndrey A. Chernov 		 * If we have "-" do nothing, if "--" we are done.
407829a229dSAndrey A. Chernov 		 */
408829a229dSAndrey A. Chernov 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
409829a229dSAndrey A. Chernov 			optind++;
410829a229dSAndrey A. Chernov 			place = EMSG;
411829a229dSAndrey A. Chernov 			/*
412829a229dSAndrey A. Chernov 			 * We found an option (--), so if we skipped
413829a229dSAndrey A. Chernov 			 * non-options, we have to permute.
414829a229dSAndrey A. Chernov 			 */
415829a229dSAndrey A. Chernov 			if (nonopt_end != -1) {
416829a229dSAndrey A. Chernov 				permute_args(nonopt_start, nonopt_end,
417829a229dSAndrey A. Chernov 				    optind, nargv);
418829a229dSAndrey A. Chernov 				optind -= nonopt_end - nonopt_start;
419829a229dSAndrey A. Chernov 			}
420829a229dSAndrey A. Chernov 			nonopt_start = nonopt_end = -1;
421829a229dSAndrey A. Chernov 			return (-1);
422a35a7e76SEric Melville 		}
423a35a7e76SEric Melville 	}
424829a229dSAndrey A. Chernov 
425829a229dSAndrey A. Chernov 	/*
426829a229dSAndrey A. Chernov 	 * Check long options if:
427829a229dSAndrey A. Chernov 	 *  1) we were passed some
428829a229dSAndrey A. Chernov 	 *  2) the arg is not just "-"
429829a229dSAndrey A. Chernov 	 *  3) either the arg starts with -- we are getopt_long_only()
430829a229dSAndrey A. Chernov 	 */
431829a229dSAndrey A. Chernov 	if (long_options != NULL && place != nargv[optind] &&
432829a229dSAndrey A. Chernov 	    (*place == '-' || (flags & FLAG_LONGONLY))) {
433829a229dSAndrey A. Chernov 		short_too = 0;
434829a229dSAndrey A. Chernov 		if (*place == '-')
435829a229dSAndrey A. Chernov 			place++;		/* --foo long option */
436829a229dSAndrey A. Chernov 		else if (*place != ':' && strchr(options, *place) != NULL)
437829a229dSAndrey A. Chernov 			short_too = 1;		/* could be short option too */
438829a229dSAndrey A. Chernov 
439829a229dSAndrey A. Chernov 		optchar = parse_long_options(nargv, options, long_options,
440829a229dSAndrey A. Chernov 		    idx, short_too);
441829a229dSAndrey A. Chernov 		if (optchar != -1) {
442829a229dSAndrey A. Chernov 			place = EMSG;
443829a229dSAndrey A. Chernov 			return (optchar);
444829a229dSAndrey A. Chernov 		}
445829a229dSAndrey A. Chernov 	}
446829a229dSAndrey A. Chernov 
447a35a7e76SEric Melville 	if ((optchar = (int)*place++) == (int)':' ||
448829a229dSAndrey A. Chernov 	    (optchar == (int)'-' && *place != '\0') ||
449829a229dSAndrey A. Chernov 	    (oli = strchr(options, optchar)) == NULL) {
450829a229dSAndrey A. Chernov 		/*
451829a229dSAndrey A. Chernov 		 * If the user specified "-" and  '-' isn't listed in
452829a229dSAndrey A. Chernov 		 * options, return -1 (non-option) as per POSIX.
453829a229dSAndrey A. Chernov 		 * Otherwise, it is an unknown option character (or ':').
454829a229dSAndrey A. Chernov 		 */
455829a229dSAndrey A. Chernov 		if (optchar == (int)'-' && *place == '\0')
456829a229dSAndrey A. Chernov 			return (-1);
457a35a7e76SEric Melville 		if (!*place)
458a35a7e76SEric Melville 			++optind;
459*f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
460*f2fd86b7SAndrey A. Chernov 		if (PRINT_ERROR)
461*f2fd86b7SAndrey A. Chernov 			warnx(posixly_correct ? illoptchar : gnuoptchar,
462*f2fd86b7SAndrey A. Chernov 			      optchar);
463*f2fd86b7SAndrey A. Chernov #else
464a35a7e76SEric Melville 		if (PRINT_ERROR)
465a35a7e76SEric Melville 			warnx(illoptchar, optchar);
466*f2fd86b7SAndrey A. Chernov #endif
467a35a7e76SEric Melville 		optopt = optchar;
468829a229dSAndrey A. Chernov 		return (BADCH);
469a35a7e76SEric Melville 	}
470829a229dSAndrey A. Chernov 	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
471829a229dSAndrey A. Chernov 		/* -W long-option */
472829a229dSAndrey A. Chernov 		if (*place)			/* no space */
473829a229dSAndrey A. Chernov 			/* NOTHING */;
474829a229dSAndrey A. Chernov 		else if (++optind >= nargc) {	/* no arg */
475a35a7e76SEric Melville 			place = EMSG;
476a35a7e76SEric Melville 			if (PRINT_ERROR)
477a35a7e76SEric Melville 				warnx(recargchar, optchar);
478a35a7e76SEric Melville 			optopt = optchar;
479829a229dSAndrey A. Chernov 			return (BADARG);
480a35a7e76SEric Melville 		} else				/* white space */
481a35a7e76SEric Melville 			place = nargv[optind];
482829a229dSAndrey A. Chernov 		optchar = parse_long_options(nargv, options, long_options,
483829a229dSAndrey A. Chernov 		    idx, 0);
484829a229dSAndrey A. Chernov 		place = EMSG;
485829a229dSAndrey A. Chernov 		return (optchar);
486a35a7e76SEric Melville 	}
487a35a7e76SEric Melville 	if (*++oli != ':') {			/* doesn't take argument */
488a35a7e76SEric Melville 		if (!*place)
489a35a7e76SEric Melville 			++optind;
490a35a7e76SEric Melville 	} else {				/* takes (optional) argument */
491a35a7e76SEric Melville 		optarg = NULL;
492a35a7e76SEric Melville 		if (*place)			/* no white space */
493a35a7e76SEric Melville 			optarg = place;
494a35a7e76SEric Melville 		/* XXX: disable test for :: if PC? (GNU doesn't) */
495a35a7e76SEric Melville 		else if (oli[1] != ':') {	/* arg not optional */
496a35a7e76SEric Melville 			if (++optind >= nargc) {	/* no arg */
497a35a7e76SEric Melville 				place = EMSG;
498a35a7e76SEric Melville 				if (PRINT_ERROR)
499a35a7e76SEric Melville 					warnx(recargchar, optchar);
500a35a7e76SEric Melville 				optopt = optchar;
501829a229dSAndrey A. Chernov 				return (BADARG);
502a35a7e76SEric Melville 			} else
503a35a7e76SEric Melville 				optarg = nargv[optind];
504829a229dSAndrey A. Chernov 		} else if (!(flags & FLAG_PERMUTE)) {
505829a229dSAndrey A. Chernov 			/*
506829a229dSAndrey A. Chernov 			 * If permutation is disabled, we can accept an
507829a229dSAndrey A. Chernov 			 * optional arg separated by whitespace.
508829a229dSAndrey A. Chernov 			 */
509829a229dSAndrey A. Chernov 			if (optind + 1 < nargc)
510829a229dSAndrey A. Chernov 				optarg = nargv[++optind];
511a35a7e76SEric Melville 		}
512a35a7e76SEric Melville 		place = EMSG;
513a35a7e76SEric Melville 		++optind;
514a35a7e76SEric Melville 	}
515a35a7e76SEric Melville 	/* dump back option letter */
516829a229dSAndrey A. Chernov 	return (optchar);
517a35a7e76SEric Melville }
518a35a7e76SEric Melville 
519a35a7e76SEric Melville #ifdef REPLACE_GETOPT
520a35a7e76SEric Melville /*
521a35a7e76SEric Melville  * getopt --
522a35a7e76SEric Melville  *	Parse argc/argv argument vector.
523a35a7e76SEric Melville  *
524829a229dSAndrey A. Chernov  * [eventually this will replace the BSD getopt]
525a35a7e76SEric Melville  */
526a35a7e76SEric Melville int
527829a229dSAndrey A. Chernov getopt(int nargc, char * const *nargv, const char *options)
528a35a7e76SEric Melville {
529a35a7e76SEric Melville 
530a35a7e76SEric Melville 	/*
531829a229dSAndrey A. Chernov 	 * We don't pass FLAG_PERMUTE to getopt_internal() since
532829a229dSAndrey A. Chernov 	 * the BSD getopt(3) (unlike GNU) has never done this.
533829a229dSAndrey A. Chernov 	 *
534829a229dSAndrey A. Chernov 	 * Furthermore, since many privileged programs call getopt()
535829a229dSAndrey A. Chernov 	 * before dropping privileges it makes sense to keep things
536829a229dSAndrey A. Chernov 	 * as simple (and bug-free) as possible.
537a35a7e76SEric Melville 	 */
538829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
539a35a7e76SEric Melville }
540829a229dSAndrey A. Chernov #endif /* REPLACE_GETOPT */
541a35a7e76SEric Melville 
542a35a7e76SEric Melville /*
543a35a7e76SEric Melville  * getopt_long --
544a35a7e76SEric Melville  *	Parse argc/argv argument vector.
545a35a7e76SEric Melville  */
546a35a7e76SEric Melville int
547a35a7e76SEric Melville getopt_long(nargc, nargv, options, long_options, idx)
548a35a7e76SEric Melville 	int nargc;
549a35a7e76SEric Melville 	char * const *nargv;
550a35a7e76SEric Melville 	const char *options;
551a35a7e76SEric Melville 	const struct option *long_options;
552a35a7e76SEric Melville 	int *idx;
553a35a7e76SEric Melville {
554a35a7e76SEric Melville 
555829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, long_options, idx,
556829a229dSAndrey A. Chernov 	    FLAG_PERMUTE));
557829a229dSAndrey A. Chernov }
558a35a7e76SEric Melville 
559a35a7e76SEric Melville /*
560829a229dSAndrey A. Chernov  * getopt_long_only --
561829a229dSAndrey A. Chernov  *	Parse argc/argv argument vector.
562a35a7e76SEric Melville  */
563829a229dSAndrey A. Chernov int
564829a229dSAndrey A. Chernov getopt_long_only(nargc, nargv, options, long_options, idx)
565829a229dSAndrey A. Chernov 	int nargc;
566829a229dSAndrey A. Chernov 	char * const *nargv;
567829a229dSAndrey A. Chernov 	const char *options;
568829a229dSAndrey A. Chernov 	const struct option *long_options;
569829a229dSAndrey A. Chernov 	int *idx;
570829a229dSAndrey A. Chernov {
571a35a7e76SEric Melville 
572829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, long_options, idx,
573829a229dSAndrey A. Chernov 	    FLAG_PERMUTE|FLAG_LONGONLY));
574a35a7e76SEric Melville }
575