getopt_long.c revision 162555
1228753Smm/*	$OpenBSD: getopt_long.c,v 1.17 2004/06/03 18:46:52 millert Exp $	*/
2228753Smm/*	$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $	*/
3228753Smm
4228753Smm/*
5228753Smm * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
6228753Smm *
7228753Smm * Permission to use, copy, modify, and distribute this software for any
8228753Smm * purpose with or without fee is hereby granted, provided that the above
9228753Smm * copyright notice and this permission notice appear in all copies.
10228753Smm *
11228753Smm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12228753Smm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13228753Smm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14228753Smm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15228753Smm * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16228753Smm * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17228753Smm * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18228753Smm *
19228753Smm * Sponsored in part by the Defense Advanced Research Projects
20228753Smm * Agency (DARPA) and Air Force Research Laboratory, Air Force
21228753Smm * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22228753Smm */
23228753Smm/*-
24228753Smm * Copyright (c) 2000 The NetBSD Foundation, Inc.
25228753Smm * All rights reserved.
26228763Smm *
27228753Smm * This code is derived from software contributed to The NetBSD Foundation
28232153Smm * by Dieter Baron and Thomas Klausner.
29228753Smm *
30228753Smm * Redistribution and use in source and binary forms, with or without
31228753Smm * modification, are permitted provided that the following conditions
32228753Smm * are met:
33228753Smm * 1. Redistributions of source code must retain the above copyright
34228753Smm *    notice, this list of conditions and the following disclaimer.
35228753Smm * 2. Redistributions in binary form must reproduce the above copyright
36228753Smm *    notice, this list of conditions and the following disclaimer in the
37228753Smm *    documentation and/or other materials provided with the distribution.
38228753Smm * 3. All advertising materials mentioning features or use of this software
39228753Smm *    must display the following acknowledgement:
40228753Smm *        This product includes software developed by the NetBSD
41228753Smm *        Foundation, Inc. and its contributors.
42228753Smm * 4. Neither the name of The NetBSD Foundation nor the names of its
43228753Smm *    contributors may be used to endorse or promote products derived
44228753Smm *    from this software without specific prior written permission.
45228753Smm *
46228753Smm * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47228753Smm * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48228753Smm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49228753Smm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50228753Smm * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51228753Smm * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52228753Smm * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53228753Smm * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54228753Smm * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55228753Smm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56228753Smm * POSSIBILITY OF SUCH DAMAGE.
57228753Smm */
58228753Smm
59228753Smm#if 0
60228753Smm#if defined(LIBC_SCCS) && !defined(lint)
61228753Smmstatic char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $";
62228753Smm#endif /* LIBC_SCCS and not lint */
63228753Smm#endif
64228753Smm#include <sys/cdefs.h>
65228753Smm__FBSDID("$FreeBSD: head/lib/libc/stdlib/getopt_long.c 162555 2006-09-22 17:01:38Z ache $");
66228753Smm
67228753Smm#include <err.h>
68228753Smm#include <errno.h>
69228753Smm#include <getopt.h>
70228753Smm#include <stdlib.h>
71228753Smm#include <string.h>
72228753Smm
73228753Smm#define GNU_COMPATIBLE		/* Be more compatible, configure's use us! */
74228753Smm
75228753Smm#ifndef GNU_COMPATIBLE
76228753Smm#define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
77228753Smm#endif
78228753Smm
79228753Smm#ifdef REPLACE_GETOPT
80228753Smmint	opterr = 1;		/* if error message should be printed */
81228753Smmint	optind = 1;		/* index into parent argv vector */
82228753Smmint	optopt = '?';		/* character checked for validity */
83228753Smmint	optreset;		/* reset getopt */
84228753Smmchar    *optarg;		/* argument associated with option */
85228753Smm#endif
86228753Smm
87228753Smm#define PRINT_ERROR	((opterr) && (*options != ':'))
88228753Smm
89228753Smm#define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
90228753Smm#define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
91228753Smm#define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
92228753Smm
93228753Smm/* return values */
94228753Smm#define	BADCH		(int)'?'
95228753Smm#define	BADARG		((*options == ':') ? (int)':' : (int)'?')
96228753Smm#define	INORDER 	(int)1
97228753Smm
98228753Smm#define	EMSG		""
99228753Smm
100228753Smm#ifdef GNU_COMPATIBLE
101228753Smm#define NO_PREFIX	(-1)
102228753Smm#define D_PREFIX	0
103228753Smm#define DD_PREFIX	1
104228753Smm#define W_PREFIX	2
105228753Smm#endif
106228753Smm
107228753Smmstatic int getopt_internal(int, char * const *, const char *,
108228753Smm			   const struct option *, int *, int);
109228753Smmstatic int parse_long_options(char * const *, const char *,
110228753Smm			      const struct option *, int *, int, int);
111228753Smmstatic int gcd(int, int);
112228753Smmstatic void permute_args(int, int, int, char * const *);
113228753Smm
114228753Smmstatic char *place = EMSG; /* option letter processing */
115228753Smm
116228753Smm/* XXX: set optreset to 1 rather than these two */
117228753Smmstatic int nonopt_start = -1; /* first non option argument (for permute) */
118228753Smmstatic int nonopt_end = -1;   /* first option after non options (for permute) */
119228753Smm
120228753Smm/* Error messages */
121228753Smmstatic const char recargchar[] = "option requires an argument -- %c";
122228753Smmstatic const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
123228753Smm#ifdef GNU_COMPATIBLE
124228753Smmstatic int dash_prefix = NO_PREFIX;
125228753Smmstatic const char gnuoptchar[] = "invalid option -- %c";
126228753Smm
127228753Smmstatic const char recargstring[] = "option `%s%s' requires an argument";
128232153Smmstatic const char ambig[] = "option `%s%.*s' is ambiguous";
129228753Smmstatic const char noarg[] = "option `%s%.*s' doesn't allow an argument";
130228753Smmstatic const char illoptstring[] = "unrecognized option `%s%s'";
131228753Smm#else
132228753Smmstatic const char recargstring[] = "option requires an argument -- %s";
133228753Smmstatic const char ambig[] = "ambiguous option -- %.*s";
134228753Smmstatic const char noarg[] = "option doesn't take an argument -- %.*s";
135228753Smmstatic const char illoptstring[] = "unknown option -- %s";
136228753Smm#endif
137228753Smm
138228753Smm/*
139228753Smm * Compute the greatest common divisor of a and b.
140228753Smm */
141228753Smmstatic int
142228753Smmgcd(int a, int b)
143228753Smm{
144228753Smm	int c;
145228753Smm
146228753Smm	c = a % b;
147228753Smm	while (c != 0) {
148228753Smm		a = b;
149228753Smm		b = c;
150228753Smm		c = a % b;
151228753Smm	}
152228753Smm
153228753Smm	return (b);
154228753Smm}
155228753Smm
156228753Smm/*
157228753Smm * Exchange the block from nonopt_start to nonopt_end with the block
158228753Smm * from nonopt_end to opt_end (keeping the same order of arguments
159228753Smm * in each block).
160228753Smm */
161228753Smmstatic void
162228753Smmpermute_args(int panonopt_start, int panonopt_end, int opt_end,
163228753Smm	char * const *nargv)
164228753Smm{
165228753Smm	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
166228753Smm	char *swap;
167228753Smm
168228753Smm	/*
169228753Smm	 * compute lengths of blocks and number and size of cycles
170228753Smm	 */
171228753Smm	nnonopts = panonopt_end - panonopt_start;
172228753Smm	nopts = opt_end - panonopt_end;
173228753Smm	ncycle = gcd(nnonopts, nopts);
174228753Smm	cyclelen = (opt_end - panonopt_start) / ncycle;
175228753Smm
176228753Smm	for (i = 0; i < ncycle; i++) {
177228753Smm		cstart = panonopt_end+i;
178228753Smm		pos = cstart;
179228753Smm		for (j = 0; j < cyclelen; j++) {
180228753Smm			if (pos >= panonopt_end)
181228753Smm				pos -= nnonopts;
182228753Smm			else
183228753Smm				pos += nopts;
184228753Smm			swap = nargv[pos];
185228753Smm			/* LINTED const cast */
186228753Smm			((char **) nargv)[pos] = nargv[cstart];
187228753Smm			/* LINTED const cast */
188228753Smm			((char **)nargv)[cstart] = swap;
189228753Smm		}
190228753Smm	}
191228753Smm}
192228753Smm
193228753Smm/*
194228753Smm * parse_long_options --
195228753Smm *	Parse long options in argc/argv argument vector.
196228753Smm * Returns -1 if short_too is set and the option does not match long_options.
197228753Smm */
198228753Smmstatic int
199228753Smmparse_long_options(char * const *nargv, const char *options,
200228753Smm	const struct option *long_options, int *idx, int short_too, int flags)
201228753Smm{
202228753Smm	char *current_argv, *has_equal;
203228753Smm#ifdef GNU_COMPATIBLE
204228753Smm	char *current_dash;
205228753Smm#endif
206228753Smm	size_t current_argv_len;
207228753Smm	int i, match, exact_match, second_partial_match;
208228753Smm
209228753Smm	current_argv = place;
210228753Smm#ifdef GNU_COMPATIBLE
211228753Smm	switch (dash_prefix) {
212228753Smm		case D_PREFIX:
213228753Smm			current_dash = "-";
214228753Smm			break;
215228753Smm		case DD_PREFIX:
216228753Smm			current_dash = "--";
217228753Smm			break;
218228753Smm		case W_PREFIX:
219228753Smm			current_dash = "-W ";
220228753Smm			break;
221228753Smm		default:
222228753Smm			current_dash = "";
223228753Smm			break;
224228753Smm	}
225228753Smm#endif
226228753Smm	match = -1;
227228753Smm	exact_match = 0;
228228753Smm	second_partial_match = 0;
229228753Smm
230228753Smm	optind++;
231228753Smm
232228753Smm	if ((has_equal = strchr(current_argv, '=')) != NULL) {
233228753Smm		/* argument found (--option=arg) */
234228753Smm		current_argv_len = has_equal - current_argv;
235228753Smm		has_equal++;
236228753Smm	} else
237228753Smm		current_argv_len = strlen(current_argv);
238228753Smm
239228753Smm	for (i = 0; long_options[i].name; i++) {
240228753Smm		/* find matching long option */
241228753Smm		if (strncmp(current_argv, long_options[i].name,
242228753Smm		    current_argv_len))
243228753Smm			continue;
244228753Smm
245228753Smm		if (strlen(long_options[i].name) == current_argv_len) {
246228753Smm			/* exact match */
247228753Smm			match = i;
248228753Smm			exact_match = 1;
249228753Smm			break;
250228753Smm		}
251228753Smm		/*
252228753Smm		 * If this is a known short option, don't allow
253228753Smm		 * a partial match of a single character.
254228753Smm		 */
255228753Smm		if (short_too && current_argv_len == 1)
256228753Smm			continue;
257228753Smm
258228753Smm		if (match == -1)        /* first partial match */
259228753Smm			match = i;
260228753Smm		else if ((flags & FLAG_LONGONLY) ||
261228753Smm			 long_options[i].has_arg !=
262228753Smm			     long_options[match].has_arg ||
263228753Smm			 long_options[i].flag != long_options[match].flag ||
264228753Smm			 long_options[i].val != long_options[match].val)
265228753Smm			second_partial_match = 1;
266228753Smm	}
267228753Smm	if (!exact_match && second_partial_match) {
268228753Smm		/* ambiguous abbreviation */
269228753Smm		if (PRINT_ERROR)
270228753Smm			warnx(ambig,
271228753Smm#ifdef GNU_COMPATIBLE
272228753Smm			     current_dash,
273228753Smm#endif
274228753Smm			     (int)current_argv_len,
275228753Smm			     current_argv);
276228753Smm		optopt = 0;
277228753Smm		return (BADCH);
278228753Smm	}
279228753Smm	if (match != -1) {		/* option found */
280228753Smm		if (long_options[match].has_arg == no_argument
281228753Smm		    && has_equal) {
282228753Smm			if (PRINT_ERROR)
283228753Smm				warnx(noarg,
284228753Smm#ifdef GNU_COMPATIBLE
285228753Smm				     current_dash,
286228753Smm#endif
287228753Smm				     (int)current_argv_len,
288228753Smm				     current_argv);
289228753Smm			/*
290228753Smm			 * XXX: GNU sets optopt to val regardless of flag
291228753Smm			 */
292228753Smm			if (long_options[match].flag == NULL)
293228753Smm				optopt = long_options[match].val;
294228753Smm			else
295228753Smm				optopt = 0;
296228753Smm#ifdef GNU_COMPATIBLE
297228753Smm			return (BADCH);
298228753Smm#else
299228753Smm			return (BADARG);
300228753Smm#endif
301228753Smm		}
302228753Smm		if (long_options[match].has_arg == required_argument ||
303228753Smm		    long_options[match].has_arg == optional_argument) {
304228753Smm			if (has_equal)
305228753Smm				optarg = has_equal;
306228753Smm			else if (long_options[match].has_arg ==
307228753Smm			    required_argument) {
308228753Smm				/*
309228753Smm				 * optional argument doesn't use next nargv
310228753Smm				 */
311228753Smm				optarg = nargv[optind++];
312228753Smm			}
313228753Smm		}
314228753Smm		if ((long_options[match].has_arg == required_argument)
315228753Smm		    && (optarg == NULL)) {
316228753Smm			/*
317228753Smm			 * Missing argument; leading ':' indicates no error
318228753Smm			 * should be generated.
319228753Smm			 */
320228753Smm			if (PRINT_ERROR)
321228753Smm				warnx(recargstring,
322228753Smm#ifdef GNU_COMPATIBLE
323228753Smm				    current_dash,
324228753Smm#endif
325228753Smm				    current_argv);
326228753Smm			/*
327228753Smm			 * XXX: GNU sets optopt to val regardless of flag
328228753Smm			 */
329228753Smm			if (long_options[match].flag == NULL)
330228753Smm				optopt = long_options[match].val;
331228753Smm			else
332228753Smm				optopt = 0;
333228753Smm			--optind;
334228753Smm			return (BADARG);
335228753Smm		}
336228753Smm	} else {			/* unknown option */
337228753Smm		if (short_too) {
338228753Smm			--optind;
339228753Smm			return (-1);
340228753Smm		}
341228753Smm		if (PRINT_ERROR)
342228753Smm			warnx(illoptstring,
343228753Smm#ifdef GNU_COMPATIBLE
344228753Smm			      current_dash,
345228753Smm#endif
346228753Smm			      current_argv);
347228753Smm		optopt = 0;
348228753Smm		return (BADCH);
349228753Smm	}
350228753Smm	if (idx)
351228753Smm		*idx = match;
352228753Smm	if (long_options[match].flag) {
353228753Smm		*long_options[match].flag = long_options[match].val;
354228753Smm		return (0);
355228753Smm	} else
356228753Smm		return (long_options[match].val);
357228753Smm}
358228753Smm
359228753Smm/*
360228753Smm * getopt_internal --
361228753Smm *	Parse argc/argv argument vector.  Called by user level routines.
362228753Smm */
363228753Smmstatic int
364228753Smmgetopt_internal(int nargc, char * const *nargv, const char *options,
365228753Smm	const struct option *long_options, int *idx, int flags)
366228753Smm{
367228753Smm	char *oli;				/* option letter list index */
368232153Smm	int optchar, short_too;
369228753Smm	int posixly_correct;
370228753Smm
371228753Smm	if (options == NULL)
372228753Smm		return (-1);
373228753Smm
374228753Smm	/*
375228753Smm	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
376228753Smm	 * string begins with a '+'.
377228753Smm	 */
378228753Smm	posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
379228753Smm#ifdef GNU_COMPATIBLE
380228753Smm	if (*options == '-')
381228753Smm		flags |= FLAG_ALLARGS;
382228753Smm	else if (posixly_correct || *options == '+')
383228753Smm		flags &= ~FLAG_PERMUTE;
384228753Smm#else
385228753Smm	if (posixly_correct || *options == '+')
386228753Smm		flags &= ~FLAG_PERMUTE;
387228753Smm	else if (*options == '-')
388228753Smm		flags |= FLAG_ALLARGS;
389228753Smm#endif
390228753Smm	if (*options == '+' || *options == '-')
391228753Smm		options++;
392228753Smm
393228753Smm	/*
394228753Smm	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
395228753Smm	 * XXX using optreset.  Work around this braindamage.
396228753Smm	 */
397228753Smm	if (optind == 0)
398228753Smm		optind = optreset = 1;
399228753Smm
400228753Smm	optarg = NULL;
401228753Smm	if (optreset)
402228753Smm		nonopt_start = nonopt_end = -1;
403228753Smmstart:
404228753Smm	if (optreset || !*place) {		/* update scanning pointer */
405228753Smm		optreset = 0;
406228753Smm		if (optind >= nargc) {          /* end of argument vector */
407228753Smm			place = EMSG;
408228753Smm			if (nonopt_end != -1) {
409228753Smm				/* do permutation, if we have to */
410228753Smm				permute_args(nonopt_start, nonopt_end,
411228753Smm				    optind, nargv);
412228753Smm				optind -= nonopt_end - nonopt_start;
413228753Smm			}
414228753Smm			else if (nonopt_start != -1) {
415228753Smm				/*
416228753Smm				 * If we skipped non-options, set optind
417228753Smm				 * to the first of them.
418228753Smm				 */
419228753Smm				optind = nonopt_start;
420228753Smm			}
421228753Smm			nonopt_start = nonopt_end = -1;
422228753Smm			return (-1);
423228753Smm		}
424228753Smm		if (*(place = nargv[optind]) != '-' ||
425228753Smm#ifdef GNU_COMPATIBLE
426228753Smm		    place[1] == '\0') {
427228753Smm#else
428228753Smm		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
429228753Smm#endif
430228753Smm			place = EMSG;		/* found non-option */
431228753Smm			if (flags & FLAG_ALLARGS) {
432228753Smm				/*
433228753Smm				 * GNU extension:
434228753Smm				 * return non-option as argument to option 1
435228753Smm				 */
436228753Smm				optarg = nargv[optind++];
437228753Smm				return (INORDER);
438228753Smm			}
439228753Smm			if (!(flags & FLAG_PERMUTE)) {
440228753Smm				/*
441228753Smm				 * If no permutation wanted, stop parsing
442228753Smm				 * at first non-option.
443228753Smm				 */
444228753Smm				return (-1);
445228753Smm			}
446228753Smm			/* do permutation */
447228753Smm			if (nonopt_start == -1)
448228753Smm				nonopt_start = optind;
449228753Smm			else if (nonopt_end != -1) {
450228753Smm				permute_args(nonopt_start, nonopt_end,
451228753Smm				    optind, nargv);
452228753Smm				nonopt_start = optind -
453228753Smm				    (nonopt_end - nonopt_start);
454				nonopt_end = -1;
455			}
456			optind++;
457			/* process next argument */
458			goto start;
459		}
460		if (nonopt_start != -1 && nonopt_end == -1)
461			nonopt_end = optind;
462
463		/*
464		 * If we have "-" do nothing, if "--" we are done.
465		 */
466		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
467			optind++;
468			place = EMSG;
469			/*
470			 * We found an option (--), so if we skipped
471			 * non-options, we have to permute.
472			 */
473			if (nonopt_end != -1) {
474				permute_args(nonopt_start, nonopt_end,
475				    optind, nargv);
476				optind -= nonopt_end - nonopt_start;
477			}
478			nonopt_start = nonopt_end = -1;
479			return (-1);
480		}
481	}
482
483	/*
484	 * Check long options if:
485	 *  1) we were passed some
486	 *  2) the arg is not just "-"
487	 *  3) either the arg starts with -- we are getopt_long_only()
488	 */
489	if (long_options != NULL && place != nargv[optind] &&
490	    (*place == '-' || (flags & FLAG_LONGONLY))) {
491		short_too = 0;
492#ifdef GNU_COMPATIBLE
493		dash_prefix = D_PREFIX;
494#endif
495		if (*place == '-') {
496			place++;		/* --foo long option */
497#ifdef GNU_COMPATIBLE
498			dash_prefix = DD_PREFIX;
499#endif
500		} else if (*place != ':' && strchr(options, *place) != NULL)
501			short_too = 1;		/* could be short option too */
502
503		optchar = parse_long_options(nargv, options, long_options,
504		    idx, short_too, flags);
505		if (optchar != -1) {
506			place = EMSG;
507			return (optchar);
508		}
509	}
510
511	if ((optchar = (int)*place++) == (int)':' ||
512	    (optchar == (int)'-' && *place != '\0') ||
513	    (oli = strchr(options, optchar)) == NULL) {
514		/*
515		 * If the user specified "-" and  '-' isn't listed in
516		 * options, return -1 (non-option) as per POSIX.
517		 * Otherwise, it is an unknown option character (or ':').
518		 */
519		if (optchar == (int)'-' && *place == '\0')
520			return (-1);
521		if (!*place)
522			++optind;
523#ifdef GNU_COMPATIBLE
524		if (PRINT_ERROR)
525			warnx(posixly_correct ? illoptchar : gnuoptchar,
526			      optchar);
527#else
528		if (PRINT_ERROR)
529			warnx(illoptchar, optchar);
530#endif
531		optopt = optchar;
532		return (BADCH);
533	}
534	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
535		/* -W long-option */
536		if (*place)			/* no space */
537			/* NOTHING */;
538		else if (++optind >= nargc) {	/* no arg */
539			place = EMSG;
540			if (PRINT_ERROR)
541				warnx(recargchar, optchar);
542			optopt = optchar;
543			return (BADARG);
544		} else				/* white space */
545			place = nargv[optind];
546#ifdef GNU_COMPATIBLE
547		dash_prefix = W_PREFIX;
548#endif
549		optchar = parse_long_options(nargv, options, long_options,
550		    idx, 0, flags);
551		place = EMSG;
552		return (optchar);
553	}
554	if (*++oli != ':') {			/* doesn't take argument */
555		if (!*place)
556			++optind;
557	} else {				/* takes (optional) argument */
558		optarg = NULL;
559		if (*place)			/* no white space */
560			optarg = place;
561		else if (oli[1] != ':') {	/* arg not optional */
562			if (++optind >= nargc) {	/* no arg */
563				place = EMSG;
564				if (PRINT_ERROR)
565					warnx(recargchar, optchar);
566				optopt = optchar;
567				return (BADARG);
568			} else
569				optarg = nargv[optind];
570		}
571#ifndef GNU_COMPATIBLE
572		/* XXX: disable test for :: if PC? (GNU doesn't) */
573		else if (!(flags & FLAG_PERMUTE)) {
574			/*
575			 * If permutation is disabled, we can accept an
576			 * optional arg separated by whitespace so long
577			 * as it does not start with a dash (-).
578			 */
579			if (optind + 1 < nargc && *nargv[optind + 1] != '-')
580				optarg = nargv[++optind];
581		}
582#endif
583		place = EMSG;
584		++optind;
585	}
586	/* dump back option letter */
587	return (optchar);
588}
589
590#ifdef REPLACE_GETOPT
591/*
592 * getopt --
593 *	Parse argc/argv argument vector.
594 *
595 * [eventually this will replace the BSD getopt]
596 */
597int
598getopt(int nargc, char * const *nargv, const char *options)
599{
600
601	/*
602	 * We don't pass FLAG_PERMUTE to getopt_internal() since
603	 * the BSD getopt(3) (unlike GNU) has never done this.
604	 *
605	 * Furthermore, since many privileged programs call getopt()
606	 * before dropping privileges it makes sense to keep things
607	 * as simple (and bug-free) as possible.
608	 */
609	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
610}
611#endif /* REPLACE_GETOPT */
612
613/*
614 * getopt_long --
615 *	Parse argc/argv argument vector.
616 */
617int
618getopt_long(nargc, nargv, options, long_options, idx)
619	int nargc;
620	char * const *nargv;
621	const char *options;
622	const struct option *long_options;
623	int *idx;
624{
625
626	return (getopt_internal(nargc, nargv, options, long_options, idx,
627	    FLAG_PERMUTE));
628}
629
630/*
631 * getopt_long_only --
632 *	Parse argc/argv argument vector.
633 */
634int
635getopt_long_only(nargc, nargv, options, long_options, idx)
636	int nargc;
637	char * const *nargv;
638	const char *options;
639	const struct option *long_options;
640	int *idx;
641{
642
643	return (getopt_internal(nargc, nargv, options, long_options, idx,
644	    FLAG_PERMUTE|FLAG_LONGONLY));
645}
646