Home | History | Annotate | Line # | Download | only in stdlib
getopt_long.c revision 1.24.16.1
      1  1.24.16.1       jym /*	$NetBSD: getopt_long.c,v 1.24.16.1 2009/05/13 19:18:27 jym Exp $	*/
      2        1.4  christos 
      3        1.4  christos /*-
      4        1.4  christos  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5        1.4  christos  * All rights reserved.
      6        1.4  christos  *
      7        1.4  christos  * This code is derived from software contributed to The NetBSD Foundation
      8        1.4  christos  * by Dieter Baron and Thomas Klausner.
      9        1.1       mcr  *
     10        1.1       mcr  * Redistribution and use in source and binary forms, with or without
     11        1.1       mcr  * modification, are permitted provided that the following conditions
     12        1.1       mcr  * are met:
     13        1.1       mcr  * 1. Redistributions of source code must retain the above copyright
     14        1.1       mcr  *    notice, this list of conditions and the following disclaimer.
     15        1.1       mcr  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1       mcr  *    notice, this list of conditions and the following disclaimer in the
     17        1.1       mcr  *    documentation and/or other materials provided with the distribution.
     18        1.1       mcr  *
     19        1.4  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.4  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.4  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.4  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.4  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.4  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.4  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.4  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.4  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.4  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.4  christos  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1       mcr  */
     31        1.1       mcr 
     32       1.17       jmc #if HAVE_NBTOOL_CONFIG_H
     33       1.17       jmc #include "nbtool_config.h"
     34       1.17       jmc #endif
     35       1.17       jmc 
     36        1.2     lukem #include <sys/cdefs.h>
     37  1.24.16.1       jym __RCSID("$NetBSD: getopt_long.c,v 1.24.16.1 2009/05/13 19:18:27 jym Exp $");
     38        1.2     lukem 
     39        1.5  christos #include "namespace.h"
     40        1.5  christos 
     41        1.2     lukem #include <assert.h>
     42       1.15        tv #include <err.h>
     43        1.2     lukem #include <errno.h>
     44       1.17       jmc #if HAVE_NBTOOL_CONFIG_H
     45       1.17       jmc #include "compat_getopt.h"
     46       1.17       jmc #else
     47       1.15        tv #include <getopt.h>
     48       1.17       jmc #endif
     49        1.1       mcr #include <stdlib.h>
     50        1.1       mcr #include <string.h>
     51        1.4  christos 
     52       1.23  christos #if HAVE_NBTOOL_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
     53       1.23  christos #define REPLACE_GETOPT
     54       1.23  christos #endif
     55       1.23  christos 
     56       1.23  christos #ifdef REPLACE_GETOPT
     57        1.4  christos #ifdef __weak_alias
     58        1.4  christos __weak_alias(getopt,_getopt)
     59        1.4  christos #endif
     60        1.4  christos int	opterr = 1;		/* if error message should be printed */
     61        1.4  christos int	optind = 1;		/* index into parent argv vector */
     62        1.4  christos int	optopt = '?';		/* character checked for validity */
     63        1.4  christos int	optreset;		/* reset getopt */
     64        1.4  christos char    *optarg;		/* argument associated with option */
     65       1.16     lukem #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
     66       1.14        tv static int optreset;
     67        1.4  christos #endif
     68        1.1       mcr 
     69        1.4  christos #ifdef __weak_alias
     70        1.4  christos __weak_alias(getopt_long,_getopt_long)
     71        1.4  christos #endif
     72        1.2     lukem 
     73        1.4  christos #define IGNORE_FIRST	(*options == '-' || *options == '+')
     74        1.4  christos #define PRINT_ERROR	((opterr) && ((*options != ':') \
     75        1.4  christos 				      || (IGNORE_FIRST && options[1] != ':')))
     76        1.4  christos #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
     77        1.4  christos #define PERMUTE         (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
     78        1.4  christos /* XXX: GNU ignores PC if *options == '-' */
     79        1.4  christos #define IN_ORDER        (!IS_POSIXLY_CORRECT && *options == '-')
     80        1.1       mcr 
     81        1.4  christos /* return values */
     82        1.1       mcr #define	BADCH	(int)'?'
     83        1.9       wiz #define	BADARG		((IGNORE_FIRST && options[1] == ':') \
     84        1.9       wiz 			 || (*options == ':') ? (int)':' : (int)'?')
     85        1.4  christos #define INORDER (int)1
     86        1.4  christos 
     87        1.1       mcr #define	EMSG	""
     88        1.1       mcr 
     89  1.24.16.1       jym static int getopt_internal(int, char **, const char *);
     90  1.24.16.1       jym static int gcd(int, int);
     91  1.24.16.1       jym static void permute_args(int, int, int, char **);
     92        1.4  christos 
     93       1.18  christos static const char *place = EMSG; /* option letter processing */
     94        1.4  christos 
     95        1.4  christos /* XXX: set optreset to 1 rather than these two */
     96        1.4  christos static int nonopt_start = -1; /* first non option argument (for permute) */
     97        1.4  christos static int nonopt_end = -1;   /* first option after non options (for permute) */
     98        1.4  christos 
     99        1.4  christos /* Error messages */
    100        1.6   nathanw static const char recargchar[] = "option requires an argument -- %c";
    101        1.6   nathanw static const char recargstring[] = "option requires an argument -- %s";
    102        1.4  christos static const char ambig[] = "ambiguous option -- %.*s";
    103        1.4  christos static const char noarg[] = "option doesn't take an argument -- %.*s";
    104       1.12      joda static const char illoptchar[] = "unknown option -- %c";
    105       1.12      joda static const char illoptstring[] = "unknown option -- %s";
    106        1.4  christos 
    107        1.4  christos 
    108        1.1       mcr /*
    109        1.4  christos  * Compute the greatest common divisor of a and b.
    110        1.4  christos  */
    111        1.4  christos static int
    112  1.24.16.1       jym gcd(int a, int b)
    113        1.4  christos {
    114        1.4  christos 	int c;
    115        1.4  christos 
    116        1.4  christos 	c = a % b;
    117        1.4  christos 	while (c != 0) {
    118        1.4  christos 		a = b;
    119        1.4  christos 		b = c;
    120        1.4  christos 		c = a % b;
    121        1.4  christos 	}
    122        1.4  christos 
    123        1.4  christos 	return b;
    124        1.4  christos }
    125        1.4  christos 
    126        1.4  christos /*
    127        1.4  christos  * Exchange the block from nonopt_start to nonopt_end with the block
    128        1.4  christos  * from nonopt_end to opt_end (keeping the same order of arguments
    129        1.4  christos  * in each block).
    130        1.4  christos  */
    131        1.4  christos static void
    132  1.24.16.1       jym permute_args(int panonopt_start, int panonopt_end, int opt_end, char **nargv)
    133        1.4  christos {
    134        1.4  christos 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
    135        1.4  christos 	char *swap;
    136        1.4  christos 
    137       1.10     lukem 	_DIAGASSERT(nargv != NULL);
    138       1.10     lukem 
    139        1.4  christos 	/*
    140        1.4  christos 	 * compute lengths of blocks and number and size of cycles
    141        1.4  christos 	 */
    142       1.13     lukem 	nnonopts = panonopt_end - panonopt_start;
    143       1.13     lukem 	nopts = opt_end - panonopt_end;
    144        1.4  christos 	ncycle = gcd(nnonopts, nopts);
    145       1.13     lukem 	cyclelen = (opt_end - panonopt_start) / ncycle;
    146        1.4  christos 
    147        1.4  christos 	for (i = 0; i < ncycle; i++) {
    148       1.13     lukem 		cstart = panonopt_end+i;
    149        1.4  christos 		pos = cstart;
    150        1.4  christos 		for (j = 0; j < cyclelen; j++) {
    151       1.13     lukem 			if (pos >= panonopt_end)
    152        1.4  christos 				pos -= nnonopts;
    153        1.4  christos 			else
    154        1.4  christos 				pos += nopts;
    155        1.4  christos 			swap = nargv[pos];
    156       1.19      yamt 			nargv[pos] = nargv[cstart];
    157       1.19      yamt 			nargv[cstart] = swap;
    158        1.4  christos 		}
    159        1.4  christos 	}
    160        1.4  christos }
    161        1.4  christos 
    162        1.4  christos /*
    163        1.4  christos  * getopt_internal --
    164        1.4  christos  *	Parse argc/argv argument vector.  Called by user level routines.
    165        1.4  christos  *  Returns -2 if -- is found (can be long option or end of options marker).
    166        1.1       mcr  */
    167        1.4  christos static int
    168  1.24.16.1       jym getopt_internal(int nargc, char **nargv, const char *options)
    169        1.1       mcr {
    170        1.1       mcr 	char *oli;				/* option letter list index */
    171        1.4  christos 	int optchar;
    172        1.1       mcr 
    173        1.2     lukem 	_DIAGASSERT(nargv != NULL);
    174        1.4  christos 	_DIAGASSERT(options != NULL);
    175        1.4  christos 
    176        1.4  christos 	optarg = NULL;
    177        1.8   thorpej 
    178        1.8   thorpej 	/*
    179        1.8   thorpej 	 * XXX Some programs (like rsyncd) expect to be able to
    180        1.8   thorpej 	 * XXX re-initialize optind to 0 and have getopt_long(3)
    181        1.8   thorpej 	 * XXX properly function again.  Work around this braindamage.
    182        1.8   thorpej 	 */
    183        1.8   thorpej 	if (optind == 0)
    184        1.8   thorpej 		optind = 1;
    185        1.2     lukem 
    186        1.4  christos 	if (optreset)
    187        1.4  christos 		nonopt_start = nonopt_end = -1;
    188        1.4  christos start:
    189        1.1       mcr 	if (optreset || !*place) {		/* update scanning pointer */
    190        1.1       mcr 		optreset = 0;
    191        1.4  christos 		if (optind >= nargc) {          /* end of argument vector */
    192        1.4  christos 			place = EMSG;
    193        1.4  christos 			if (nonopt_end != -1) {
    194        1.4  christos 				/* do permutation, if we have to */
    195        1.4  christos 				permute_args(nonopt_start, nonopt_end,
    196        1.4  christos 				    optind, nargv);
    197        1.4  christos 				optind -= nonopt_end - nonopt_start;
    198        1.4  christos 			}
    199        1.4  christos 			else if (nonopt_start != -1) {
    200        1.4  christos 				/*
    201        1.4  christos 				 * If we skipped non-options, set optind
    202        1.4  christos 				 * to the first of them.
    203        1.4  christos 				 */
    204        1.4  christos 				optind = nonopt_start;
    205        1.4  christos 			}
    206        1.4  christos 			nonopt_start = nonopt_end = -1;
    207        1.4  christos 			return -1;
    208        1.4  christos 		}
    209        1.9       wiz 		if ((*(place = nargv[optind]) != '-')
    210        1.9       wiz 		    || (place[1] == '\0')) {    /* found non-option */
    211        1.1       mcr 			place = EMSG;
    212        1.4  christos 			if (IN_ORDER) {
    213        1.4  christos 				/*
    214        1.4  christos 				 * GNU extension:
    215        1.4  christos 				 * return non-option as argument to option 1
    216        1.4  christos 				 */
    217        1.4  christos 				optarg = nargv[optind++];
    218        1.4  christos 				return INORDER;
    219        1.4  christos 			}
    220        1.4  christos 			if (!PERMUTE) {
    221        1.4  christos 				/*
    222        1.4  christos 				 * if no permutation wanted, stop parsing
    223        1.4  christos 				 * at first non-option
    224        1.4  christos 				 */
    225        1.4  christos 				return -1;
    226        1.4  christos 			}
    227        1.4  christos 			/* do permutation */
    228        1.4  christos 			if (nonopt_start == -1)
    229        1.4  christos 				nonopt_start = optind;
    230        1.4  christos 			else if (nonopt_end != -1) {
    231        1.4  christos 				permute_args(nonopt_start, nonopt_end,
    232        1.4  christos 				    optind, nargv);
    233        1.4  christos 				nonopt_start = optind -
    234        1.4  christos 				    (nonopt_end - nonopt_start);
    235        1.4  christos 				nonopt_end = -1;
    236        1.4  christos 			}
    237        1.4  christos 			optind++;
    238        1.4  christos 			/* process next argument */
    239        1.4  christos 			goto start;
    240        1.1       mcr 		}
    241        1.4  christos 		if (nonopt_start != -1 && nonopt_end == -1)
    242        1.4  christos 			nonopt_end = optind;
    243        1.1       mcr 		if (place[1] && *++place == '-') {	/* found "--" */
    244        1.4  christos 			place++;
    245        1.4  christos 			return -2;
    246        1.4  christos 		}
    247        1.4  christos 	}
    248        1.4  christos 	if ((optchar = (int)*place++) == (int)':' ||
    249        1.4  christos 	    (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
    250        1.4  christos 		/* option letter unknown or ':' */
    251        1.4  christos 		if (!*place)
    252        1.4  christos 			++optind;
    253        1.4  christos 		if (PRINT_ERROR)
    254        1.6   nathanw 			warnx(illoptchar, optchar);
    255        1.4  christos 		optopt = optchar;
    256        1.4  christos 		return BADCH;
    257        1.4  christos 	}
    258        1.4  christos 	if (optchar == 'W' && oli[1] == ';') {		/* -W long-option */
    259        1.4  christos 		/* XXX: what if no long options provided (called by getopt)? */
    260        1.4  christos 		if (*place)
    261        1.4  christos 			return -2;
    262        1.4  christos 
    263        1.4  christos 		if (++optind >= nargc) {	/* no arg */
    264        1.1       mcr 			place = EMSG;
    265        1.4  christos 			if (PRINT_ERROR)
    266        1.6   nathanw 				warnx(recargchar, optchar);
    267        1.4  christos 			optopt = optchar;
    268        1.4  christos 			return BADARG;
    269        1.4  christos 		} else				/* white space */
    270        1.4  christos 			place = nargv[optind];
    271        1.1       mcr 		/*
    272        1.4  christos 		 * Handle -W arg the same as --arg (which causes getopt to
    273        1.4  christos 		 * stop parsing).
    274        1.1       mcr 		 */
    275        1.4  christos 		return -2;
    276        1.4  christos 	}
    277        1.4  christos 	if (*++oli != ':') {			/* doesn't take argument */
    278        1.1       mcr 		if (!*place)
    279        1.1       mcr 			++optind;
    280        1.4  christos 	} else {				/* takes (optional) argument */
    281        1.1       mcr 		optarg = NULL;
    282        1.1       mcr 		if (*place)			/* no white space */
    283       1.18  christos 			optarg = __UNCONST(place);
    284        1.4  christos 		/* XXX: disable test for :: if PC? (GNU doesn't) */
    285        1.4  christos 		else if (oli[1] != ':') {	/* arg not optional */
    286        1.4  christos 			if (++optind >= nargc) {	/* no arg */
    287        1.4  christos 				place = EMSG;
    288        1.4  christos 				if (PRINT_ERROR)
    289        1.6   nathanw 					warnx(recargchar, optchar);
    290        1.4  christos 				optopt = optchar;
    291        1.4  christos 				return BADARG;
    292        1.4  christos 			} else
    293        1.4  christos 				optarg = nargv[optind];
    294        1.4  christos 		}
    295        1.1       mcr 		place = EMSG;
    296        1.1       mcr 		++optind;
    297        1.1       mcr 	}
    298        1.4  christos 	/* dump back option letter */
    299        1.4  christos 	return optchar;
    300        1.1       mcr }
    301        1.1       mcr 
    302        1.4  christos #ifdef REPLACE_GETOPT
    303        1.1       mcr /*
    304        1.1       mcr  * getopt --
    305        1.1       mcr  *	Parse argc/argv argument vector.
    306        1.4  christos  *
    307        1.4  christos  * [eventually this will replace the real getopt]
    308        1.1       mcr  */
    309        1.1       mcr int
    310        1.4  christos getopt(nargc, nargv, options)
    311        1.1       mcr 	int nargc;
    312        1.1       mcr 	char * const *nargv;
    313        1.4  christos 	const char *options;
    314        1.1       mcr {
    315        1.1       mcr 	int retval;
    316       1.10     lukem 
    317       1.10     lukem 	_DIAGASSERT(nargv != NULL);
    318       1.10     lukem 	_DIAGASSERT(options != NULL);
    319        1.1       mcr 
    320       1.19      yamt 	retval = getopt_internal(nargc, __UNCONST(nargv), options);
    321       1.19      yamt 	if (retval == -2) {
    322        1.4  christos 		++optind;
    323        1.4  christos 		/*
    324        1.4  christos 		 * We found an option (--), so if we skipped non-options,
    325        1.4  christos 		 * we have to permute.
    326        1.4  christos 		 */
    327        1.4  christos 		if (nonopt_end != -1) {
    328        1.4  christos 			permute_args(nonopt_start, nonopt_end, optind,
    329        1.4  christos 				       nargv);
    330        1.4  christos 			optind -= nonopt_end - nonopt_start;
    331        1.4  christos 		}
    332        1.4  christos 		nonopt_start = nonopt_end = -1;
    333        1.1       mcr 		retval = -1;
    334        1.1       mcr 	}
    335        1.4  christos 	return retval;
    336        1.1       mcr }
    337        1.1       mcr #endif
    338        1.1       mcr 
    339        1.1       mcr /*
    340        1.1       mcr  * getopt_long --
    341        1.1       mcr  *	Parse argc/argv argument vector.
    342        1.1       mcr  */
    343        1.1       mcr int
    344  1.24.16.1       jym getopt_long(int nargc, char * const *nargv, const char *options,
    345  1.24.16.1       jym     const struct option *long_options, int *idx)
    346        1.1       mcr {
    347        1.1       mcr 	int retval;
    348        1.1       mcr 
    349       1.21  ginsbach #define IDENTICAL_INTERPRETATION(_x, _y)				\
    350       1.21  ginsbach 	(long_options[(_x)].has_arg == long_options[(_y)].has_arg &&	\
    351       1.21  ginsbach 	 long_options[(_x)].flag == long_options[(_y)].flag &&		\
    352       1.21  ginsbach 	 long_options[(_x)].val == long_options[(_y)].val)
    353       1.21  ginsbach 
    354        1.2     lukem 	_DIAGASSERT(nargv != NULL);
    355        1.4  christos 	_DIAGASSERT(options != NULL);
    356        1.2     lukem 	_DIAGASSERT(long_options != NULL);
    357        1.4  christos 	/* idx may be NULL */
    358        1.2     lukem 
    359       1.19      yamt 	retval = getopt_internal(nargc, __UNCONST(nargv), options);
    360       1.19      yamt 	if (retval == -2) {
    361        1.4  christos 		char *current_argv, *has_equal;
    362        1.4  christos 		size_t current_argv_len;
    363       1.21  ginsbach 		int i, ambiguous, match;
    364        1.1       mcr 
    365       1.18  christos 		current_argv = __UNCONST(place);
    366        1.4  christos 		match = -1;
    367       1.21  ginsbach 		ambiguous = 0;
    368        1.4  christos 
    369        1.4  christos 		optind++;
    370        1.4  christos 		place = EMSG;
    371        1.4  christos 
    372        1.4  christos 		if (*current_argv == '\0') {		/* found "--" */
    373        1.4  christos 			/*
    374        1.4  christos 			 * We found an option (--), so if we skipped
    375        1.4  christos 			 * non-options, we have to permute.
    376        1.4  christos 			 */
    377        1.4  christos 			if (nonopt_end != -1) {
    378        1.4  christos 				permute_args(nonopt_start, nonopt_end,
    379       1.19      yamt 				    optind, __UNCONST(nargv));
    380        1.4  christos 				optind -= nonopt_end - nonopt_start;
    381        1.4  christos 			}
    382        1.4  christos 			nonopt_start = nonopt_end = -1;
    383        1.4  christos 			return -1;
    384        1.1       mcr 		}
    385        1.1       mcr 		if ((has_equal = strchr(current_argv, '=')) != NULL) {
    386        1.4  christos 			/* argument found (--option=arg) */
    387        1.1       mcr 			current_argv_len = has_equal - current_argv;
    388        1.1       mcr 			has_equal++;
    389        1.1       mcr 		} else
    390        1.1       mcr 			current_argv_len = strlen(current_argv);
    391        1.4  christos 
    392        1.4  christos 		for (i = 0; long_options[i].name; i++) {
    393        1.4  christos 			/* find matching long option */
    394        1.4  christos 			if (strncmp(current_argv, long_options[i].name,
    395        1.4  christos 			    current_argv_len))
    396        1.1       mcr 				continue;
    397        1.1       mcr 
    398        1.4  christos 			if (strlen(long_options[i].name) ==
    399        1.4  christos 			    (unsigned)current_argv_len) {
    400        1.4  christos 				/* exact match */
    401        1.1       mcr 				match = i;
    402       1.21  ginsbach 				ambiguous = 0;
    403        1.1       mcr 				break;
    404        1.1       mcr 			}
    405        1.4  christos 			if (match == -1)		/* partial match */
    406        1.1       mcr 				match = i;
    407       1.21  ginsbach 			else if (!IDENTICAL_INTERPRETATION(i, match))
    408       1.21  ginsbach 				ambiguous = 1;
    409       1.21  ginsbach 		}
    410       1.21  ginsbach 		if (ambiguous) {
    411       1.21  ginsbach 			/* ambiguous abbreviation */
    412       1.21  ginsbach 			if (PRINT_ERROR)
    413       1.21  ginsbach 				warnx(ambig, (int)current_argv_len,
    414       1.21  ginsbach 				     current_argv);
    415       1.21  ginsbach 			optopt = 0;
    416       1.21  ginsbach 			return BADCH;
    417        1.1       mcr 		}
    418        1.4  christos 		if (match != -1) {			/* option found */
    419        1.4  christos 		        if (long_options[match].has_arg == no_argument
    420        1.4  christos 			    && has_equal) {
    421        1.4  christos 				if (PRINT_ERROR)
    422        1.7  sommerfe 					warnx(noarg, (int)current_argv_len,
    423        1.4  christos 					     current_argv);
    424        1.4  christos 				/*
    425        1.4  christos 				 * XXX: GNU sets optopt to val regardless of
    426        1.4  christos 				 * flag
    427        1.4  christos 				 */
    428        1.4  christos 				if (long_options[match].flag == NULL)
    429        1.4  christos 					optopt = long_options[match].val;
    430        1.4  christos 				else
    431        1.4  christos 					optopt = 0;
    432        1.4  christos 				return BADARG;
    433        1.4  christos 			}
    434        1.1       mcr 			if (long_options[match].has_arg == required_argument ||
    435        1.1       mcr 			    long_options[match].has_arg == optional_argument) {
    436        1.1       mcr 				if (has_equal)
    437        1.1       mcr 					optarg = has_equal;
    438        1.4  christos 				else if (long_options[match].has_arg ==
    439        1.4  christos 				    required_argument) {
    440        1.4  christos 					/*
    441        1.4  christos 					 * optional argument doesn't use
    442        1.4  christos 					 * next nargv
    443        1.4  christos 					 */
    444        1.1       mcr 					optarg = nargv[optind++];
    445        1.4  christos 				}
    446        1.1       mcr 			}
    447        1.1       mcr 			if ((long_options[match].has_arg == required_argument)
    448        1.1       mcr 			    && (optarg == NULL)) {
    449        1.1       mcr 				/*
    450        1.4  christos 				 * Missing argument; leading ':'
    451        1.1       mcr 				 * indicates no error should be generated
    452        1.1       mcr 				 */
    453        1.4  christos 				if (PRINT_ERROR)
    454        1.6   nathanw 					warnx(recargstring, current_argv);
    455        1.4  christos 				/*
    456        1.4  christos 				 * XXX: GNU sets optopt to val regardless
    457        1.4  christos 				 * of flag
    458        1.4  christos 				 */
    459        1.4  christos 				if (long_options[match].flag == NULL)
    460        1.4  christos 					optopt = long_options[match].val;
    461        1.4  christos 				else
    462        1.4  christos 					optopt = 0;
    463        1.4  christos 				--optind;
    464        1.4  christos 				return BADARG;
    465        1.4  christos 			}
    466        1.4  christos 		} else {			/* unknown option */
    467        1.4  christos 			if (PRINT_ERROR)
    468        1.6   nathanw 				warnx(illoptstring, current_argv);
    469        1.4  christos 			optopt = 0;
    470        1.4  christos 			return BADCH;
    471        1.1       mcr 		}
    472        1.1       mcr 		if (long_options[match].flag) {
    473        1.1       mcr 			*long_options[match].flag = long_options[match].val;
    474        1.1       mcr 			retval = 0;
    475        1.1       mcr 		} else
    476        1.1       mcr 			retval = long_options[match].val;
    477        1.4  christos 		if (idx)
    478        1.4  christos 			*idx = match;
    479        1.1       mcr 	}
    480        1.4  christos 	return retval;
    481       1.21  ginsbach #undef IDENTICAL_INTERPRETATION
    482        1.1       mcr }
    483