/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* ident "%Z%%M% %I% %E% SMI"
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/**
* getopt() for function definition and requirements. Unlike that
* definition, this implementation moves non-options to the end of the
* argv array rather than quitting at the first non-option.
*/
public class Getopt {
private int argc;
private boolean opterr = true;
/*
* _sp is required to keep state between successive calls to
* getopt() while extracting aggregated short-options (ie: -abcd).
*/
/**
* Creates a {Code Getopt} instance to parse the given command-line
* arguments. Modifies the given args array by swapping the
* positions of non-options and options so that non-options appear
* at the end of the array.
*/
{
// No defensive copy; Getopt is expected to modify the given
// args array
validate();
}
private void
validate()
{
throw new NullPointerException("program name is null");
}
int i = 0;
if (s == null) {
throw new NullPointerException("null arg at index " + i);
}
++i;
}
throw new NullPointerException("option string is null");
}
}
private static class StringRef {
private String s;
public String
get()
{
return s;
}
public StringRef
{
s = value;
return this;
}
}
/*
* Generalized error processing method. If the optstr parameter is
* null, the character c is converted to a string and displayed
* instead.
*/
void
{
StringWriter w = new StringWriter();
PrintWriter p = new PrintWriter(w);
}
}
/*
* Determine if the specified character (c) is present in the string
* (optstring) as a regular, single character option. If the option
* is found, return an index into optstring where the short-option
* character is found, otherwise return -1. The characters ':' and
* '(' are not allowed.
*/
static int
{
if (c == ':' || c == '(') {
return -1;
}
int ch;
for (int i = 0; i < len; ++i) {
if (ch == c) {
return i;
}
}
}
return -1;
}
/**
* Determine if the specified string (opt) is present in the string
* (optstring) as a long-option contained within parenthesis. If the
* long-option specifies option-argument, return a reference to it
* in longoptarg. Otherwise set the longoptarg reference to null.
* If the option is found, return an index into optstring at the
* position of the short-option character associated with the
* long-option; otherwise return -1.
*
* @param optstring the entire optstring passed to the {@code
* Getopt} constructor
* @param opt the long option read from the command line
* @param longoptarg the value of the option is returned in this
* parameter, if an option exists. Possible return values in
* longoptarg are:
* <ul>
* <li><b>NULL:</b> No argument was found</li>
* <li><b>empty string (""):</b> Argument was explicitly left empty
* by the user (e.g., --option= )</li>
* <li><b>valid string:</b> Argument found on the command line</li>
* </ul>
* @return index to equivalent short-option in optstring, or -1 if
* option not found in optstring.
*/
static int
{
int cp; // index into optstring, beginning of one option spec
int ip; // index into optstring, traverses every char
char ic; // optstring char
int il; // optstring length
int op; // index into opt
char oc; // opt char
int ol; // opt length
boolean match; // true if opt is matching part of optstring
do {
break;
break;
while (ic == '(') {
break;
op = 0;
match = true;
++ip;
}
/* may be an empty string - OK */
} else {
}
return cp;
}
break;
}
/*
* Handle double-colon in optstring ("a::(longa)") The old
* getopt() accepts it and treats it as a required argument.
*/
--cp;
}
return -1;
}
/**
* Get the current option value.
*/
public String
{
return optarg;
}
/**
* Get the index of the next option to be parsed.
*/
public int
{
return optind;
}
/**
* Gets the command-line arguments.
*/
public String[]
getArgv()
{
// No defensive copy: Getopt is expected to modify the given
// args array.
return args;
}
/**
* Gets the aggregated short option that just failed. Since long
* options can't be aggregated, a failed long option can be obtained
* by {@code getArgv()[getOptind() - 1]}.
*/
public int
{
return optopt;
}
/**
* Set to {@code false} to suppress diagnostic messages to stderr.
*/
public void
{
}
/**
* Gets the next option character, or -1 if there are no more
* options. If getopt() encounters a short-option character or a
* long-option string not described in the {@code optionString}
* argument to the constructor, it returns the question-mark (?)
* character. If it detects a missing option-argument, it also
* returns the question-mark (?) character, unless the first
* character of the {@code optionString} argument was a colon (:),
* in which case getopt() returns the colon (:) character.
* <p>
* This implementation swaps the positions of options and
* non-options in the given argv array.
*/
public int
getopt()
{
char c;
int cp;
boolean longopt;
/*
* Has the end of the options been encountered? The following
* implements the SUS requirements:
*
* If, when getopt() is called:
* - the first character of argv[optind] is not '-'
* - argv[optind] is the string "-"
* getopt() returns -1 without changing optind if
* - argv[optind] is the string "--"
* getopt() returns -1 after incrementing optind
*/
if (_sp == 1) {
boolean nonOption;
do {
nonOption = false;
return EOF;
++optind;
return EOF;
// non-option: here we deviate from the SUS requirements
// by not quitting, and instead move non-options to the
// end of the args array
nonOption = true;
}
--argc;
}
} while (nonOption);
}
/*
* Getting this far indicates that an option has been encountered.
* Note that the syntax of optstring applies special meanings to
* the characters ':' and '(', so they are not permissible as
* option letters. A special meaning is also applied to the ')'
* character, but its meaning can be determined from context.
* Note that the specification only requires that the alnum
* characters be accepted.
*
* If the second character of the argument is a '-' this must be
* a long-option, otherwise it must be a short option. Scan for
* the option in optstring by the appropriate algorithm. Either
* scan will return an index to the short-option character in
* optstring if the option is found and -1 otherwise.
*
* For an unrecognized long-option, optopt will equal 0, but
* since long-options can't aggregate the failing option can be
* identified by argv[optind-1].
*/
if (!(longopt
longoptarg)) != -1)
err("%s: illegal option -- %s", c,
/*
* Note: When the long option is unrecognized, optopt will
* be '-' here, which matches the specification.
*/
++optind;
_sp = 1;
}
return '?';
}
/*
* A valid option has been identified. If it should have an
* option-argument, process that now. SUS defines the setting
* of optarg as follows:
*
* 1. If the option was the last character in an element of
* argv, then optarg contains the next element of argv, and
* optind is incremented by 2. If the resulting value of
* optind is not less than argc, this indicates a missing
* option-argument, and getopt() returns an error indication.
*
* 2. Otherwise, optarg points to the string following the
* option character in that element of argv, and optind is
* incremented by 1.
*
* The second clause allows -abcd (where b requires an
* option-argument) to be interpreted as "-a -b cd".
*
* Note that the option-argument can legally be an empty string,
* such as:
* command --option= operand
* which explicitly sets the value of --option to nil
*/
// The option takes an argument
/*
* The option argument was explicitly set to the empty
* string on the command line (--option=)
*/
optind++;
err("%s: option requires an argument -- %s", c,
_sp = 1;
} else
_sp = 1;
} else {
// The option does NOT take an argument
// User supplied an arg to an option that takes none
c = '?';
}
_sp = 1;
++optind;
}
}
return (c);
}
}