/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Dieter Baron and Thomas Klausner.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "lint.h"
#include <getopt.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "_libc_gettext.h"
/* Determine if an argument is required for this long option */
(!FLAG_IS_SET(FLAG_OPTIONAL_ARGS))) || \
/* return values */
static int getopt_internal(int, char * const *, const char *,
const struct option *, int *, int,
static int gcd(int, int);
static void permute_args(int, int, int, char * const *);
/* XXX: set optreset to 1 rather than these two */
/*
* Generalized error message output.
*
* NOTE ON ERROR MESSAGES: All the error messages in this file
* use %s (not %c) because they are all routed through warnx_getopt(),
* which takes a string argument. Character arguments passed
* to warnxchar() are converted to strings automatically before
* being passed to warnx_getopt().
*/
static void
}
/*
* Generalized error message output.
*/
static void
charbuf[0] = c;
}
/*
* Generalized error message output.
*/
static void
}
/*
* Compute the greatest common divisor of a and b.
*/
static int
gcd(int a, int b)
{
int c;
c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return (b);
}
/*
* Exchange the block from nonopt_start to nonopt_end with the block
* from nonopt_end to opt_end (keeping the same order of arguments
* in each block).
*/
static void
char * const *nargv)
{
char *swap;
/*
* compute lengths of blocks and number and size of cycles
*/
for (i = 0; i < ncycle; i++) {
cstart = panonopt_end+i;
for (j = 0; j < cyclelen; j++) {
if (pos >= panonopt_end)
else
}
}
} /* permute_args() */
/*
* Verify that each short option (character flag) has a long equivalent,
* and that each long option has a short option equivalent. Note that
* multiple long options can map to the same character.
*
* This behavior is defined by Sun's CLIP specification (11/12/02),
* and currently getopt_clip() is the only getopt variant that
* requires it.
*
* If error output is enabled and an error is found, this function
* prints ONE error message (the first error found) and returns an
* error value.
*
* ASSUMES: options != NULL
* ASSUMES: long_options may be NULL
*
* Returns < 0 if an error is found
* Returns >= 0 on success
*/
static int
/* LINTED: argument unused in function: nargc */
char *const *nargv,
const char *options,
const struct option *long_options,
int short_i = 0;
int long_i = 0;
int equivFound = 0;
int ch = 0;
/*
* Find a long option for each short option
*/
equivFound = 1;
if (ch == ':') {
continue;
}
if (FLAG_IS_SET(FLAG_W_SEMICOLON) &&
/* W; is a special case */
++short_i;
continue;
}
equivFound = 0;
if (long_options != NULL) {
for (long_i = 0; ((!equivFound) &&
++long_i) {
}
}
if ((!equivFound) && (PRINT_ERROR)) {
"%s: equivalent long option required -- %s"),
ch);
}
} /* short_i */
/*
* Find a short option for each long option. Note that if we came
* out of the above loop with equivFound==0, we are already done.
*/
for (long_i = 0; (equivFound &&
++long_i) {
!= NULL));
if ((!equivFound) && (PRINT_ERROR)) {
warnx_getopt(nargv[0],
"%s: equivalent short option required -- %s"),
}
} /* for long_i */
}
return (equivFound? 0:-1);
} /* verify_short_long_equivalents() */
/*
* parse_long_options --
* Returns -1 if short_too is set and the option does not match long_options.
*/
static int
{
int i = 0;
int match = 0;
match = -1;
optind++;
/* argument found (--option=arg) */
} else {
}
/* find matching long option */
current_argv_len) != 0) {
continue; /* no match */
}
if ((!FLAG_IS_SET(FLAG_ABBREV)) &&
(long_option_len > current_argv_len)) {
continue; /* Abbreviations are disabled */
}
if (long_option_len == current_argv_len) {
/* exact match */
match = i;
break;
}
/*
* If this is a known short option, don't allow
* a partial match of a single character.
*/
continue;
match = i;
else {
/* ambiguous abbreviation */
if (PRINT_ERROR) {
"%s: ambiguous option -- %s"),
(int)current_argv_len,
}
optopt = 0;
return (BADCH);
}
} /* for i */
(argv_equal_ptr != NULL)) {
if (PRINT_ERROR) {
"%s: option doesn't take an argument -- %s"),
(int)current_argv_len,
}
/*
* XXX: GNU sets optopt to val regardless of flag
*/
else
optopt = 0;
return (BADARG);
}
if (argv_equal_ptr != NULL) {
/* The next argv must be the option argument */
}
++optind; /* code below depends on this */
}
}
/*
* Missing argument; leading ':' indicates no error
* should be generated.
*/
if (PRINT_ERROR) {
warnx_getopt(nargv[0],
"%s: option requires an argument -- %s"),
}
/*
* XXX: GNU sets optopt to val regardless of flag
*/
else
optopt = 0;
--optind;
return (BADARG);
}
} else { /* unknown option */
if (short_too) {
--optind;
return (-1);
}
if (PRINT_ERROR) {
warnx_getopt(nargv[0],
_libc_gettext("%s: illegal option -- %s"),
}
optopt = 0;
return (BADCH);
}
if (idx)
return (0);
} else {
return (optopt);
}
} /* parse_long_options() */
/*
* getopt_internal() --
*
* This implements all of the getopt_long(), getopt_long_only(),
* getopt_clip() variants.
*/
static int
{
return (-1);
/*
* Disable GNU extensions if POSIXLY_CORRECT is set or options
* string begins with a '+'.
*/
if (posixly_correct == -1) {
}
if (FLAG_IS_SET(FLAG_PLUS_DASH_START)) {
/*
* + or - at start of optstring takes precedence
* over POSIXLY_CORRECT.
*/
if (*options == '+') {
/*
* leading + means POSIX-compliant; first non-option
* ends option list. Therefore, don't permute args.
*/
posixly_correct = 1;
} else if (*options == '-') {
posixly_correct = 0;
flags |= FLAG_ALLARGS;
}
options++;
}
} /* if FLAG_PLUS_DASH_START */
if (posixly_correct) {
flags &= ~FLAG_PERMUTE;
flags &= ~FLAG_ALLARGS;
}
/*
* Some programs (like GNU cvs) set optind to 0 to restart
* option processing. Work around this braindamage.
*
* The above problem comes from using global variables. We
* should avoid their use in the future.
*/
if (optind == 0) {
}
optopt = 0;
if (optreset) {
}
/*
* On the first call, make sure that there is a short equivalent
* for each long option, and vice versa. This is required by
* Sun's CLIP specification (11/12/02).
*/
/* function printed any necessary messages */
return (-1);
}
}
optreset = 0;
if (nonopt_end != -1) {
/* do permutation, if we have to */
} else if (nonopt_start != -1) {
/*
* If we skipped non-options, set optind
* to the first of them.
*/
}
return (-1);
}
if (flags & FLAG_ALLARGS) {
/*
* GNU extension:
* return non-option as argument to option '\1'
*/
return (INORDER);
}
if (!(flags & FLAG_PERMUTE)) {
/*
* If no permutation wanted, stop parsing
* at first non-option.
*/
return (-1);
}
/* do permutation */
if (nonopt_start == -1)
else if (nonopt_end != -1) {
nonopt_start = optind -
(nonopt_end - nonopt_start);
nonopt_end = -1;
}
optind++;
/* process next argument */
goto start;
}
nonopt_end = optind;
/*
* Check for "--" or "--foo" with no long options
* but if place is simply "-" leave it unmolested.
*/
optind++;
/*
* We found an option (--), so if we skipped
* non-options, we have to permute.
*/
if (nonopt_end != -1) {
}
return (-1);
}
}
/*
* Check long options if:
* 1) we were passed some
* 2) the arg is not just "-"
* 3) either the arg starts with -- or we are getopt_long_only()
*/
short_too = 0;
if (*place == '-')
place++; /* --foo long option */
if (optchar != -1) {
return (optchar);
}
}
/*
* If the user didn't specify '-' as an option,
* assume it means -1 as POSIX specifies.
*/
if (optchar == (int)'-')
return (-1);
/* option letter unknown or ':' */
if (!*place)
++optind;
if (PRINT_ERROR)
_libc_gettext("%s: illegal option -- %s"),
optchar);
return (BADCH);
}
if (FLAG_IS_SET(FLAG_W_SEMICOLON) &&
/* -W long-option */
/* LINTED: statement has no consequent: if */
if (*place) { /* no space */
/* NOTHING */;
if (PRINT_ERROR)
"%s: option requires an argument -- %s"),
optchar);
return (BADARG);
} else { /* white space */
}
/*
* PSARC 2003/645 - Match GNU behavior, set optarg to
* the long-option.
*/
}
return (optchar);
}
if (!*place)
++optind;
} else { /* takes (optional) argument */
if (*place) { /* no white space */
/* XXX: disable test for :: if PC? (GNU doesn't) */
} else if (!(FLAG_IS_SET(FLAG_OPTIONAL_ARGS) &&
/* arg is required (not optional) */
if (PRINT_ERROR) {
"%s: option requires an argument -- %s"),
optchar);
}
return (BADARG);
} else
}
++optind;
}
/* return valid option letter */
return (optchar);
} /* getopt_internal() */
/*
* getopt_long() --
*
* Requires that long options be preceded with a two dashes
* (e.g., --longoption).
*/
int
const char *optstring,
{
return (getopt_internal(
| FLAG_PLUS_DASH_START));
} /* getopt_long() */
/*
* getopt_long_only() --
*
* Long options may be preceded with a single dash (e.g., -longoption)
*/
int
const char *optstring,
{
return (getopt_internal(
| FLAG_LONGONLY));
} /* getopt_long_only() */
/*
* getopt_clip() --
* Sun's CLIP specification (11/12/02)
*
* o Does not allow arguments to be optional (optional_argument is
* treated as required_argument).
*
* o Does not allow long options to be abbreviated on the command line
*
* o Does not allow long argument to be preceded by a single dash
* (Double-dash '--' is required)
*
* o Stops option processing at the first non-option
*
* o Requires that every long option have a short-option (single
* character) equivalent and vice-versa. If a short option or
* long option without an equivalent is found, an error message
* is printed and -1 is returned on the first call, and errno
* is set to EINVAL.
*
* o Leading + or - in optstring is ignored, and opstring is
* treated as if it began after the + or - .
*/
int
const char *optstring,
{
return getopt_internal(
/*
* no permutation,
* no optional args,
* no long-only,
* no abbreviations
* no support for +- at start of optstring
* yes support for "W;" in optstring
*/
} /* getopt_clip() */