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