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