xref: /freebsd/lib/libc/stdlib/getopt_long.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1298207c3SAndrey A. Chernov /*	$OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 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 <Todd.Miller@courtesan.com>
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  *
39a35a7e76SEric Melville  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40a35a7e76SEric Melville  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41a35a7e76SEric Melville  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42a35a7e76SEric Melville  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43a35a7e76SEric Melville  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44a35a7e76SEric Melville  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45a35a7e76SEric Melville  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46a35a7e76SEric Melville  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47a35a7e76SEric Melville  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48a35a7e76SEric Melville  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49a35a7e76SEric Melville  * POSSIBILITY OF SUCH DAMAGE.
50a35a7e76SEric Melville  */
51a35a7e76SEric Melville 
52a35a7e76SEric Melville #include <err.h>
53a35a7e76SEric Melville #include <errno.h>
54a35a7e76SEric Melville #include <getopt.h>
55a35a7e76SEric Melville #include <stdlib.h>
56a35a7e76SEric Melville #include <string.h>
57a35a7e76SEric Melville 
58f2fd86b7SAndrey A. Chernov #define GNU_COMPATIBLE		/* Be more compatible, configure's use us! */
59f2fd86b7SAndrey A. Chernov 
60d3ff3b5fSAndrey A. Chernov #if 0				/* we prefer to keep our getopt(3) */
61829a229dSAndrey A. Chernov #define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
62a35a7e76SEric Melville #endif
63a35a7e76SEric Melville 
64a35a7e76SEric Melville #ifdef REPLACE_GETOPT
65a35a7e76SEric Melville int	opterr = 1;		/* if error message should be printed */
66a35a7e76SEric Melville int	optind = 1;		/* index into parent argv vector */
67a35a7e76SEric Melville int	optopt = '?';		/* character checked for validity */
68a35a7e76SEric Melville int	optreset;		/* reset getopt */
69a35a7e76SEric Melville char    *optarg;		/* argument associated with option */
70a35a7e76SEric Melville #endif
71a35a7e76SEric Melville 
72829a229dSAndrey A. Chernov #define PRINT_ERROR	((opterr) && (*options != ':'))
73a35a7e76SEric Melville 
74829a229dSAndrey A. Chernov #define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
75829a229dSAndrey A. Chernov #define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
76829a229dSAndrey A. Chernov #define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
77a35a7e76SEric Melville 
78a35a7e76SEric Melville /* return values */
79a35a7e76SEric Melville #define	BADCH		(int)'?'
80829a229dSAndrey A. Chernov #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
81a35a7e76SEric Melville #define	INORDER 	(int)1
82a35a7e76SEric Melville 
83*0348c8fcSAlex Richardson static char EMSG[] = "";
84a35a7e76SEric Melville 
859f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
869f06a99eSAndrey A. Chernov #define NO_PREFIX	(-1)
879f06a99eSAndrey A. Chernov #define D_PREFIX	0
889f06a99eSAndrey A. Chernov #define DD_PREFIX	1
899f06a99eSAndrey A. Chernov #define W_PREFIX	2
909f06a99eSAndrey A. Chernov #endif
919f06a99eSAndrey A. Chernov 
92829a229dSAndrey A. Chernov static int getopt_internal(int, char * const *, const char *,
93829a229dSAndrey A. Chernov 			   const struct option *, int *, int);
94829a229dSAndrey A. Chernov static int parse_long_options(char * const *, const char *,
95ed4fbbd5SAndrey A. Chernov 			      const struct option *, int *, int, int);
96e6fc380cSAlfred Perlstein static int gcd(int, int);
97e6fc380cSAlfred Perlstein static void permute_args(int, int, int, char * const *);
98a35a7e76SEric Melville 
99a35a7e76SEric Melville static char *place = EMSG; /* option letter processing */
100a35a7e76SEric Melville 
101a35a7e76SEric Melville /* XXX: set optreset to 1 rather than these two */
102a35a7e76SEric Melville static int nonopt_start = -1; /* first non option argument (for permute) */
103a35a7e76SEric Melville static int nonopt_end = -1;   /* first option after non options (for permute) */
104a35a7e76SEric Melville 
105a35a7e76SEric Melville /* Error messages */
106a35a7e76SEric Melville static const char recargchar[] = "option requires an argument -- %c";
1079f06a99eSAndrey A. Chernov static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
108f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
1099f06a99eSAndrey A. Chernov static int dash_prefix = NO_PREFIX;
110f2fd86b7SAndrey A. Chernov static const char gnuoptchar[] = "invalid option -- %c";
111f2fd86b7SAndrey A. Chernov 
1129f06a99eSAndrey A. Chernov static const char recargstring[] = "option `%s%s' requires an argument";
1139f06a99eSAndrey A. Chernov static const char ambig[] = "option `%s%.*s' is ambiguous";
1149f06a99eSAndrey A. Chernov static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
1159f06a99eSAndrey A. Chernov static const char illoptstring[] = "unrecognized option `%s%s'";
116f2fd86b7SAndrey A. Chernov #else
117a35a7e76SEric Melville static const char recargstring[] = "option requires an argument -- %s";
118a35a7e76SEric Melville static const char ambig[] = "ambiguous option -- %.*s";
119a35a7e76SEric Melville static const char noarg[] = "option doesn't take an argument -- %.*s";
120a35a7e76SEric Melville static const char illoptstring[] = "unknown option -- %s";
121f2fd86b7SAndrey A. Chernov #endif
122a35a7e76SEric Melville 
123a35a7e76SEric Melville /*
124a35a7e76SEric Melville  * Compute the greatest common divisor of a and b.
125a35a7e76SEric Melville  */
126a35a7e76SEric Melville static int
gcd(int a,int b)127829a229dSAndrey A. Chernov gcd(int a, int b)
128a35a7e76SEric Melville {
129a35a7e76SEric Melville 	int c;
130a35a7e76SEric Melville 
131a35a7e76SEric Melville 	c = a % b;
132a35a7e76SEric Melville 	while (c != 0) {
133a35a7e76SEric Melville 		a = b;
134a35a7e76SEric Melville 		b = c;
135a35a7e76SEric Melville 		c = a % b;
136a35a7e76SEric Melville 	}
137a35a7e76SEric Melville 
138829a229dSAndrey A. Chernov 	return (b);
139a35a7e76SEric Melville }
140a35a7e76SEric Melville 
141a35a7e76SEric Melville /*
142a35a7e76SEric Melville  * Exchange the block from nonopt_start to nonopt_end with the block
143a35a7e76SEric Melville  * from nonopt_end to opt_end (keeping the same order of arguments
144a35a7e76SEric Melville  * in each block).
145a35a7e76SEric Melville  */
146a35a7e76SEric Melville static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)147829a229dSAndrey A. Chernov permute_args(int panonopt_start, int panonopt_end, int opt_end,
148829a229dSAndrey A. Chernov 	char * const *nargv)
149a35a7e76SEric Melville {
150a35a7e76SEric Melville 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
151a35a7e76SEric Melville 	char *swap;
152a35a7e76SEric Melville 
153a35a7e76SEric Melville 	/*
154a35a7e76SEric Melville 	 * compute lengths of blocks and number and size of cycles
155a35a7e76SEric Melville 	 */
156a35a7e76SEric Melville 	nnonopts = panonopt_end - panonopt_start;
157a35a7e76SEric Melville 	nopts = opt_end - panonopt_end;
158a35a7e76SEric Melville 	ncycle = gcd(nnonopts, nopts);
159a35a7e76SEric Melville 	cyclelen = (opt_end - panonopt_start) / ncycle;
160a35a7e76SEric Melville 
161a35a7e76SEric Melville 	for (i = 0; i < ncycle; i++) {
162a35a7e76SEric Melville 		cstart = panonopt_end+i;
163a35a7e76SEric Melville 		pos = cstart;
164a35a7e76SEric Melville 		for (j = 0; j < cyclelen; j++) {
165a35a7e76SEric Melville 			if (pos >= panonopt_end)
166a35a7e76SEric Melville 				pos -= nnonopts;
167a35a7e76SEric Melville 			else
168a35a7e76SEric Melville 				pos += nopts;
169a35a7e76SEric Melville 			swap = nargv[pos];
170a35a7e76SEric Melville 			/* LINTED const cast */
171a35a7e76SEric Melville 			((char **) nargv)[pos] = nargv[cstart];
172a35a7e76SEric Melville 			/* LINTED const cast */
173a35a7e76SEric Melville 			((char **)nargv)[cstart] = swap;
174a35a7e76SEric Melville 		}
175a35a7e76SEric Melville 	}
176a35a7e76SEric Melville }
177a35a7e76SEric Melville 
178a35a7e76SEric Melville /*
179829a229dSAndrey A. Chernov  * parse_long_options --
180829a229dSAndrey A. Chernov  *	Parse long options in argc/argv argument vector.
181829a229dSAndrey A. Chernov  * Returns -1 if short_too is set and the option does not match long_options.
182a35a7e76SEric Melville  */
183a35a7e76SEric Melville static int
parse_long_options(char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too,int flags)184829a229dSAndrey A. Chernov parse_long_options(char * const *nargv, const char *options,
185ed4fbbd5SAndrey A. Chernov 	const struct option *long_options, int *idx, int short_too, int flags)
186a35a7e76SEric Melville {
187829a229dSAndrey A. Chernov 	char *current_argv, *has_equal;
1889f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
189*0348c8fcSAlex Richardson 	const char *current_dash;
1909f06a99eSAndrey A. Chernov #endif
191829a229dSAndrey A. Chernov 	size_t current_argv_len;
192ed4fbbd5SAndrey A. Chernov 	int i, match, exact_match, second_partial_match;
193a35a7e76SEric Melville 
194829a229dSAndrey A. Chernov 	current_argv = place;
1959f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
1969f06a99eSAndrey A. Chernov 	switch (dash_prefix) {
1979f06a99eSAndrey A. Chernov 		case D_PREFIX:
1989f06a99eSAndrey A. Chernov 			current_dash = "-";
1999f06a99eSAndrey A. Chernov 			break;
2009f06a99eSAndrey A. Chernov 		case DD_PREFIX:
2019f06a99eSAndrey A. Chernov 			current_dash = "--";
2029f06a99eSAndrey A. Chernov 			break;
2039f06a99eSAndrey A. Chernov 		case W_PREFIX:
2049f06a99eSAndrey A. Chernov 			current_dash = "-W ";
2059f06a99eSAndrey A. Chernov 			break;
2069f06a99eSAndrey A. Chernov 		default:
2079f06a99eSAndrey A. Chernov 			current_dash = "";
2089f06a99eSAndrey A. Chernov 			break;
2099f06a99eSAndrey A. Chernov 	}
2109f06a99eSAndrey A. Chernov #endif
211829a229dSAndrey A. Chernov 	match = -1;
212ed4fbbd5SAndrey A. Chernov 	exact_match = 0;
213ed4fbbd5SAndrey A. Chernov 	second_partial_match = 0;
214a35a7e76SEric Melville 
215829a229dSAndrey A. Chernov 	optind++;
216829a229dSAndrey A. Chernov 
217829a229dSAndrey A. Chernov 	if ((has_equal = strchr(current_argv, '=')) != NULL) {
218829a229dSAndrey A. Chernov 		/* argument found (--option=arg) */
219829a229dSAndrey A. Chernov 		current_argv_len = has_equal - current_argv;
220829a229dSAndrey A. Chernov 		has_equal++;
221829a229dSAndrey A. Chernov 	} else
222829a229dSAndrey A. Chernov 		current_argv_len = strlen(current_argv);
223829a229dSAndrey A. Chernov 
224829a229dSAndrey A. Chernov 	for (i = 0; long_options[i].name; i++) {
225829a229dSAndrey A. Chernov 		/* find matching long option */
226829a229dSAndrey A. Chernov 		if (strncmp(current_argv, long_options[i].name,
227829a229dSAndrey A. Chernov 		    current_argv_len))
228829a229dSAndrey A. Chernov 			continue;
229829a229dSAndrey A. Chernov 
230829a229dSAndrey A. Chernov 		if (strlen(long_options[i].name) == current_argv_len) {
231829a229dSAndrey A. Chernov 			/* exact match */
232829a229dSAndrey A. Chernov 			match = i;
233ed4fbbd5SAndrey A. Chernov 			exact_match = 1;
234829a229dSAndrey A. Chernov 			break;
235829a229dSAndrey A. Chernov 		}
236829a229dSAndrey A. Chernov 		/*
237829a229dSAndrey A. Chernov 		 * If this is a known short option, don't allow
238829a229dSAndrey A. Chernov 		 * a partial match of a single character.
239829a229dSAndrey A. Chernov 		 */
240f853699aSAndrey A. Chernov 		if (short_too && current_argv_len == 1)
241829a229dSAndrey A. Chernov 			continue;
242829a229dSAndrey A. Chernov 
243ed4fbbd5SAndrey A. Chernov 		if (match == -1)	/* first partial match */
244829a229dSAndrey A. Chernov 			match = i;
245ed4fbbd5SAndrey A. Chernov 		else if ((flags & FLAG_LONGONLY) ||
246ed4fbbd5SAndrey A. Chernov 			 long_options[i].has_arg !=
247ed4fbbd5SAndrey A. Chernov 			     long_options[match].has_arg ||
248ed4fbbd5SAndrey A. Chernov 			 long_options[i].flag != long_options[match].flag ||
249ed4fbbd5SAndrey A. Chernov 			 long_options[i].val != long_options[match].val)
250ed4fbbd5SAndrey A. Chernov 			second_partial_match = 1;
251ed4fbbd5SAndrey A. Chernov 	}
252ed4fbbd5SAndrey A. Chernov 	if (!exact_match && second_partial_match) {
253829a229dSAndrey A. Chernov 		/* ambiguous abbreviation */
254829a229dSAndrey A. Chernov 		if (PRINT_ERROR)
2559f06a99eSAndrey A. Chernov 			warnx(ambig,
2569f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2579f06a99eSAndrey A. Chernov 			     current_dash,
2589f06a99eSAndrey A. Chernov #endif
2599f06a99eSAndrey A. Chernov 			     (int)current_argv_len,
260829a229dSAndrey A. Chernov 			     current_argv);
261829a229dSAndrey A. Chernov 		optopt = 0;
262829a229dSAndrey A. Chernov 		return (BADCH);
263829a229dSAndrey A. Chernov 	}
264829a229dSAndrey A. Chernov 	if (match != -1) {		/* option found */
265829a229dSAndrey A. Chernov 		if (long_options[match].has_arg == no_argument
266829a229dSAndrey A. Chernov 		    && has_equal) {
267829a229dSAndrey A. Chernov 			if (PRINT_ERROR)
2689f06a99eSAndrey A. Chernov 				warnx(noarg,
2699f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2709f06a99eSAndrey A. Chernov 				     current_dash,
2719f06a99eSAndrey A. Chernov #endif
2729f06a99eSAndrey A. Chernov 				     (int)current_argv_len,
273829a229dSAndrey A. Chernov 				     current_argv);
274829a229dSAndrey A. Chernov 			/*
275829a229dSAndrey A. Chernov 			 * XXX: GNU sets optopt to val regardless of flag
276829a229dSAndrey A. Chernov 			 */
277829a229dSAndrey A. Chernov 			if (long_options[match].flag == NULL)
278829a229dSAndrey A. Chernov 				optopt = long_options[match].val;
279829a229dSAndrey A. Chernov 			else
280829a229dSAndrey A. Chernov 				optopt = 0;
28188485399SAndrey A. Chernov #ifdef GNU_COMPATIBLE
28288485399SAndrey A. Chernov 			return (BADCH);
28388485399SAndrey A. Chernov #else
284829a229dSAndrey A. Chernov 			return (BADARG);
28588485399SAndrey A. Chernov #endif
286829a229dSAndrey A. Chernov 		}
287829a229dSAndrey A. Chernov 		if (long_options[match].has_arg == required_argument ||
288829a229dSAndrey A. Chernov 		    long_options[match].has_arg == optional_argument) {
289829a229dSAndrey A. Chernov 			if (has_equal)
290829a229dSAndrey A. Chernov 				optarg = has_equal;
291829a229dSAndrey A. Chernov 			else if (long_options[match].has_arg ==
292829a229dSAndrey A. Chernov 			    required_argument) {
293829a229dSAndrey A. Chernov 				/*
294829a229dSAndrey A. Chernov 				 * optional argument doesn't use next nargv
295829a229dSAndrey A. Chernov 				 */
296829a229dSAndrey A. Chernov 				optarg = nargv[optind++];
297829a229dSAndrey A. Chernov 			}
298829a229dSAndrey A. Chernov 		}
299829a229dSAndrey A. Chernov 		if ((long_options[match].has_arg == required_argument)
300829a229dSAndrey A. Chernov 		    && (optarg == NULL)) {
301829a229dSAndrey A. Chernov 			/*
302829a229dSAndrey A. Chernov 			 * Missing argument; leading ':' indicates no error
303829a229dSAndrey A. Chernov 			 * should be generated.
304829a229dSAndrey A. Chernov 			 */
305829a229dSAndrey A. Chernov 			if (PRINT_ERROR)
306829a229dSAndrey A. Chernov 				warnx(recargstring,
3079f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
3089f06a99eSAndrey A. Chernov 				    current_dash,
3099f06a99eSAndrey A. Chernov #endif
310829a229dSAndrey A. Chernov 				    current_argv);
311829a229dSAndrey A. Chernov 			/*
312829a229dSAndrey A. Chernov 			 * XXX: GNU sets optopt to val regardless of flag
313829a229dSAndrey A. Chernov 			 */
314829a229dSAndrey A. Chernov 			if (long_options[match].flag == NULL)
315829a229dSAndrey A. Chernov 				optopt = long_options[match].val;
316829a229dSAndrey A. Chernov 			else
317829a229dSAndrey A. Chernov 				optopt = 0;
318829a229dSAndrey A. Chernov 			--optind;
319829a229dSAndrey A. Chernov 			return (BADARG);
320829a229dSAndrey A. Chernov 		}
321829a229dSAndrey A. Chernov 	} else {			/* unknown option */
322829a229dSAndrey A. Chernov 		if (short_too) {
323829a229dSAndrey A. Chernov 			--optind;
324829a229dSAndrey A. Chernov 			return (-1);
325829a229dSAndrey A. Chernov 		}
326829a229dSAndrey A. Chernov 		if (PRINT_ERROR)
3279f06a99eSAndrey A. Chernov 			warnx(illoptstring,
3289f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
3299f06a99eSAndrey A. Chernov 			      current_dash,
3309f06a99eSAndrey A. Chernov #endif
3319f06a99eSAndrey A. Chernov 			      current_argv);
332829a229dSAndrey A. Chernov 		optopt = 0;
333829a229dSAndrey A. Chernov 		return (BADCH);
334829a229dSAndrey A. Chernov 	}
335829a229dSAndrey A. Chernov 	if (idx)
336829a229dSAndrey A. Chernov 		*idx = match;
337829a229dSAndrey A. Chernov 	if (long_options[match].flag) {
338829a229dSAndrey A. Chernov 		*long_options[match].flag = long_options[match].val;
339829a229dSAndrey A. Chernov 		return (0);
340829a229dSAndrey A. Chernov 	} else
341829a229dSAndrey A. Chernov 		return (long_options[match].val);
342829a229dSAndrey A. Chernov }
343a35a7e76SEric Melville 
344a35a7e76SEric Melville /*
345829a229dSAndrey A. Chernov  * getopt_internal --
346829a229dSAndrey A. Chernov  *	Parse argc/argv argument vector.  Called by user level routines.
347829a229dSAndrey A. Chernov  */
348829a229dSAndrey A. Chernov static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int flags)349829a229dSAndrey A. Chernov getopt_internal(int nargc, char * const *nargv, const char *options,
350829a229dSAndrey A. Chernov 	const struct option *long_options, int *idx, int flags)
351829a229dSAndrey A. Chernov {
352829a229dSAndrey A. Chernov 	char *oli;				/* option letter list index */
353829a229dSAndrey A. Chernov 	int optchar, short_too;
354298207c3SAndrey A. Chernov 	static int posixly_correct = -1;
355829a229dSAndrey A. Chernov 
356829a229dSAndrey A. Chernov 	if (options == NULL)
357829a229dSAndrey A. Chernov 		return (-1);
358829a229dSAndrey A. Chernov 
359829a229dSAndrey A. Chernov 	/*
360829a229dSAndrey A. Chernov 	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
361829a229dSAndrey A. Chernov 	 * XXX using optreset.  Work around this braindamage.
362a35a7e76SEric Melville 	 */
363a35a7e76SEric Melville 	if (optind == 0)
364829a229dSAndrey A. Chernov 		optind = optreset = 1;
365a35a7e76SEric Melville 
366298207c3SAndrey A. Chernov 	/*
367298207c3SAndrey A. Chernov 	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
368298207c3SAndrey A. Chernov 	 * string begins with a '+'.
369298207c3SAndrey A. Chernov 	 */
370298207c3SAndrey A. Chernov 	if (posixly_correct == -1 || optreset)
371298207c3SAndrey A. Chernov 		posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
372298207c3SAndrey A. Chernov 	if (*options == '-')
373298207c3SAndrey A. Chernov 		flags |= FLAG_ALLARGS;
374298207c3SAndrey A. Chernov 	else if (posixly_correct || *options == '+')
375298207c3SAndrey A. Chernov 		flags &= ~FLAG_PERMUTE;
376298207c3SAndrey A. Chernov 	if (*options == '+' || *options == '-')
377298207c3SAndrey A. Chernov 		options++;
378298207c3SAndrey A. Chernov 
379829a229dSAndrey A. Chernov 	optarg = NULL;
380a35a7e76SEric Melville 	if (optreset)
381a35a7e76SEric Melville 		nonopt_start = nonopt_end = -1;
382a35a7e76SEric Melville start:
383a35a7e76SEric Melville 	if (optreset || !*place) {		/* update scanning pointer */
384a35a7e76SEric Melville 		optreset = 0;
385a35a7e76SEric Melville 		if (optind >= nargc) {          /* end of argument vector */
386a35a7e76SEric Melville 			place = EMSG;
387a35a7e76SEric Melville 			if (nonopt_end != -1) {
388a35a7e76SEric Melville 				/* do permutation, if we have to */
389a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
390a35a7e76SEric Melville 				    optind, nargv);
391a35a7e76SEric Melville 				optind -= nonopt_end - nonopt_start;
392a35a7e76SEric Melville 			}
393a35a7e76SEric Melville 			else if (nonopt_start != -1) {
394a35a7e76SEric Melville 				/*
395a35a7e76SEric Melville 				 * If we skipped non-options, set optind
396a35a7e76SEric Melville 				 * to the first of them.
397a35a7e76SEric Melville 				 */
398a35a7e76SEric Melville 				optind = nonopt_start;
399a35a7e76SEric Melville 			}
400a35a7e76SEric Melville 			nonopt_start = nonopt_end = -1;
401829a229dSAndrey A. Chernov 			return (-1);
402a35a7e76SEric Melville 		}
403829a229dSAndrey A. Chernov 		if (*(place = nargv[optind]) != '-' ||
4043700175bSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4053700175bSAndrey A. Chernov 		    place[1] == '\0') {
4063700175bSAndrey A. Chernov #else
407829a229dSAndrey A. Chernov 		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
4083700175bSAndrey A. Chernov #endif
409829a229dSAndrey A. Chernov 			place = EMSG;		/* found non-option */
410829a229dSAndrey A. Chernov 			if (flags & FLAG_ALLARGS) {
411a35a7e76SEric Melville 				/*
412a35a7e76SEric Melville 				 * GNU extension:
413a35a7e76SEric Melville 				 * return non-option as argument to option 1
414a35a7e76SEric Melville 				 */
415a35a7e76SEric Melville 				optarg = nargv[optind++];
416829a229dSAndrey A. Chernov 				return (INORDER);
417a35a7e76SEric Melville 			}
418829a229dSAndrey A. Chernov 			if (!(flags & FLAG_PERMUTE)) {
419a35a7e76SEric Melville 				/*
420829a229dSAndrey A. Chernov 				 * If no permutation wanted, stop parsing
421829a229dSAndrey A. Chernov 				 * at first non-option.
422a35a7e76SEric Melville 				 */
423829a229dSAndrey A. Chernov 				return (-1);
424a35a7e76SEric Melville 			}
425a35a7e76SEric Melville 			/* do permutation */
426a35a7e76SEric Melville 			if (nonopt_start == -1)
427a35a7e76SEric Melville 				nonopt_start = optind;
428a35a7e76SEric Melville 			else if (nonopt_end != -1) {
429a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
430a35a7e76SEric Melville 				    optind, nargv);
431a35a7e76SEric Melville 				nonopt_start = optind -
432a35a7e76SEric Melville 				    (nonopt_end - nonopt_start);
433a35a7e76SEric Melville 				nonopt_end = -1;
434a35a7e76SEric Melville 			}
435a35a7e76SEric Melville 			optind++;
436a35a7e76SEric Melville 			/* process next argument */
437a35a7e76SEric Melville 			goto start;
438a35a7e76SEric Melville 		}
439a35a7e76SEric Melville 		if (nonopt_start != -1 && nonopt_end == -1)
440a35a7e76SEric Melville 			nonopt_end = optind;
441829a229dSAndrey A. Chernov 
442829a229dSAndrey A. Chernov 		/*
443829a229dSAndrey A. Chernov 		 * If we have "-" do nothing, if "--" we are done.
444829a229dSAndrey A. Chernov 		 */
445829a229dSAndrey A. Chernov 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
446829a229dSAndrey A. Chernov 			optind++;
447829a229dSAndrey A. Chernov 			place = EMSG;
448829a229dSAndrey A. Chernov 			/*
449829a229dSAndrey A. Chernov 			 * We found an option (--), so if we skipped
450829a229dSAndrey A. Chernov 			 * non-options, we have to permute.
451829a229dSAndrey A. Chernov 			 */
452829a229dSAndrey A. Chernov 			if (nonopt_end != -1) {
453829a229dSAndrey A. Chernov 				permute_args(nonopt_start, nonopt_end,
454829a229dSAndrey A. Chernov 				    optind, nargv);
455829a229dSAndrey A. Chernov 				optind -= nonopt_end - nonopt_start;
456829a229dSAndrey A. Chernov 			}
457829a229dSAndrey A. Chernov 			nonopt_start = nonopt_end = -1;
458829a229dSAndrey A. Chernov 			return (-1);
459a35a7e76SEric Melville 		}
460a35a7e76SEric Melville 	}
461829a229dSAndrey A. Chernov 
462829a229dSAndrey A. Chernov 	/*
463829a229dSAndrey A. Chernov 	 * Check long options if:
464829a229dSAndrey A. Chernov 	 *  1) we were passed some
465829a229dSAndrey A. Chernov 	 *  2) the arg is not just "-"
466829a229dSAndrey A. Chernov 	 *  3) either the arg starts with -- we are getopt_long_only()
467829a229dSAndrey A. Chernov 	 */
468829a229dSAndrey A. Chernov 	if (long_options != NULL && place != nargv[optind] &&
469829a229dSAndrey A. Chernov 	    (*place == '-' || (flags & FLAG_LONGONLY))) {
470829a229dSAndrey A. Chernov 		short_too = 0;
4719f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4729f06a99eSAndrey A. Chernov 		dash_prefix = D_PREFIX;
4739f06a99eSAndrey A. Chernov #endif
4749f06a99eSAndrey A. Chernov 		if (*place == '-') {
475829a229dSAndrey A. Chernov 			place++;		/* --foo long option */
476253b638eSKyle Evans 			if (*place == '\0')
477253b638eSKyle Evans 				return (BADARG);	/* malformed option */
4789f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4799f06a99eSAndrey A. Chernov 			dash_prefix = DD_PREFIX;
4809f06a99eSAndrey A. Chernov #endif
4819f06a99eSAndrey A. Chernov 		} else if (*place != ':' && strchr(options, *place) != NULL)
482829a229dSAndrey A. Chernov 			short_too = 1;		/* could be short option too */
483829a229dSAndrey A. Chernov 
484829a229dSAndrey A. Chernov 		optchar = parse_long_options(nargv, options, long_options,
485ed4fbbd5SAndrey A. Chernov 		    idx, short_too, flags);
486829a229dSAndrey A. Chernov 		if (optchar != -1) {
487829a229dSAndrey A. Chernov 			place = EMSG;
488829a229dSAndrey A. Chernov 			return (optchar);
489829a229dSAndrey A. Chernov 		}
490829a229dSAndrey A. Chernov 	}
491829a229dSAndrey A. Chernov 
492a35a7e76SEric Melville 	if ((optchar = (int)*place++) == (int)':' ||
493829a229dSAndrey A. Chernov 	    (optchar == (int)'-' && *place != '\0') ||
494829a229dSAndrey A. Chernov 	    (oli = strchr(options, optchar)) == NULL) {
495829a229dSAndrey A. Chernov 		/*
496829a229dSAndrey A. Chernov 		 * If the user specified "-" and  '-' isn't listed in
497829a229dSAndrey A. Chernov 		 * options, return -1 (non-option) as per POSIX.
498829a229dSAndrey A. Chernov 		 * Otherwise, it is an unknown option character (or ':').
499829a229dSAndrey A. Chernov 		 */
500829a229dSAndrey A. Chernov 		if (optchar == (int)'-' && *place == '\0')
501829a229dSAndrey A. Chernov 			return (-1);
502a35a7e76SEric Melville 		if (!*place)
503a35a7e76SEric Melville 			++optind;
504f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
505f2fd86b7SAndrey A. Chernov 		if (PRINT_ERROR)
506f2fd86b7SAndrey A. Chernov 			warnx(posixly_correct ? illoptchar : gnuoptchar,
507f2fd86b7SAndrey A. Chernov 			      optchar);
508f2fd86b7SAndrey A. Chernov #else
509a35a7e76SEric Melville 		if (PRINT_ERROR)
510a35a7e76SEric Melville 			warnx(illoptchar, optchar);
511f2fd86b7SAndrey A. Chernov #endif
512a35a7e76SEric Melville 		optopt = optchar;
513829a229dSAndrey A. Chernov 		return (BADCH);
514a35a7e76SEric Melville 	}
515829a229dSAndrey A. Chernov 	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
516829a229dSAndrey A. Chernov 		/* -W long-option */
517829a229dSAndrey A. Chernov 		if (*place)			/* no space */
518829a229dSAndrey A. Chernov 			/* NOTHING */;
519829a229dSAndrey A. Chernov 		else if (++optind >= nargc) {	/* no arg */
520a35a7e76SEric Melville 			place = EMSG;
521a35a7e76SEric Melville 			if (PRINT_ERROR)
522a35a7e76SEric Melville 				warnx(recargchar, optchar);
523a35a7e76SEric Melville 			optopt = optchar;
524829a229dSAndrey A. Chernov 			return (BADARG);
525a35a7e76SEric Melville 		} else				/* white space */
526a35a7e76SEric Melville 			place = nargv[optind];
5279f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
5289f06a99eSAndrey A. Chernov 		dash_prefix = W_PREFIX;
5299f06a99eSAndrey A. Chernov #endif
530829a229dSAndrey A. Chernov 		optchar = parse_long_options(nargv, options, long_options,
531ed4fbbd5SAndrey A. Chernov 		    idx, 0, flags);
532829a229dSAndrey A. Chernov 		place = EMSG;
533829a229dSAndrey A. Chernov 		return (optchar);
534a35a7e76SEric Melville 	}
535a35a7e76SEric Melville 	if (*++oli != ':') {			/* doesn't take argument */
536a35a7e76SEric Melville 		if (!*place)
537a35a7e76SEric Melville 			++optind;
538a35a7e76SEric Melville 	} else {				/* takes (optional) argument */
539a35a7e76SEric Melville 		optarg = NULL;
540a35a7e76SEric Melville 		if (*place)			/* no white space */
541a35a7e76SEric Melville 			optarg = place;
542a35a7e76SEric Melville 		else if (oli[1] != ':') {	/* arg not optional */
543a35a7e76SEric Melville 			if (++optind >= nargc) {	/* no arg */
544a35a7e76SEric Melville 				place = EMSG;
545a35a7e76SEric Melville 				if (PRINT_ERROR)
546a35a7e76SEric Melville 					warnx(recargchar, optchar);
547a35a7e76SEric Melville 				optopt = optchar;
548829a229dSAndrey A. Chernov 				return (BADARG);
549a35a7e76SEric Melville 			} else
550a35a7e76SEric Melville 				optarg = nargv[optind];
551f27c7b47SAndrey A. Chernov 		}
552a35a7e76SEric Melville 		place = EMSG;
553a35a7e76SEric Melville 		++optind;
554a35a7e76SEric Melville 	}
555a35a7e76SEric Melville 	/* dump back option letter */
556829a229dSAndrey A. Chernov 	return (optchar);
557a35a7e76SEric Melville }
558a35a7e76SEric Melville 
559a35a7e76SEric Melville #ifdef REPLACE_GETOPT
560a35a7e76SEric Melville /*
561a35a7e76SEric Melville  * getopt --
562a35a7e76SEric Melville  *	Parse argc/argv argument vector.
563a35a7e76SEric Melville  *
564829a229dSAndrey A. Chernov  * [eventually this will replace the BSD getopt]
565a35a7e76SEric Melville  */
566a35a7e76SEric Melville int
567829a229dSAndrey A. Chernov getopt(int nargc, char * const *nargv, const char *options)
568a35a7e76SEric Melville {
569a35a7e76SEric Melville 
570a35a7e76SEric Melville 	/*
571829a229dSAndrey A. Chernov 	 * We don't pass FLAG_PERMUTE to getopt_internal() since
572829a229dSAndrey A. Chernov 	 * the BSD getopt(3) (unlike GNU) has never done this.
573829a229dSAndrey A. Chernov 	 *
574829a229dSAndrey A. Chernov 	 * Furthermore, since many privileged programs call getopt()
575829a229dSAndrey A. Chernov 	 * before dropping privileges it makes sense to keep things
576829a229dSAndrey A. Chernov 	 * as simple (and bug-free) as possible.
577a35a7e76SEric Melville 	 */
578829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
579a35a7e76SEric Melville }
580829a229dSAndrey A. Chernov #endif /* REPLACE_GETOPT */
581a35a7e76SEric Melville 
582a35a7e76SEric Melville /*
583a35a7e76SEric Melville  * getopt_long --
584a35a7e76SEric Melville  *	Parse argc/argv argument vector.
585a35a7e76SEric Melville  */
586a35a7e76SEric Melville int
587d3ff3b5fSAndrey A. Chernov getopt_long(int nargc, char * const *nargv, const char *options,
588d3ff3b5fSAndrey A. Chernov 	const struct option *long_options, int *idx)
589a35a7e76SEric Melville {
590a35a7e76SEric Melville 
591829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, long_options, idx,
592829a229dSAndrey A. Chernov 	    FLAG_PERMUTE));
593829a229dSAndrey A. Chernov }
594a35a7e76SEric Melville 
595a35a7e76SEric Melville /*
596829a229dSAndrey A. Chernov  * getopt_long_only --
597829a229dSAndrey A. Chernov  *	Parse argc/argv argument vector.
598a35a7e76SEric Melville  */
599829a229dSAndrey A. Chernov int
600d3ff3b5fSAndrey A. Chernov getopt_long_only(int nargc, char * const *nargv, const char *options,
601d3ff3b5fSAndrey A. Chernov 	const struct option *long_options, int *idx)
602829a229dSAndrey A. Chernov {
603a35a7e76SEric Melville 
604829a229dSAndrey A. Chernov 	return (getopt_internal(nargc, nargv, options, long_options, idx,
605829a229dSAndrey A. Chernov 	    FLAG_PERMUTE|FLAG_LONGONLY));
606a35a7e76SEric Melville }
607