commandline.c revision dafcb997e390efa4423883dafd100c975c4095d6
3523e19da21545ade45394cb64d7462f20b77347Tinderbox User * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Portions Copyright (C) 1999-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * copyright notice and this permission notice appear in all copies.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * PERFORMANCE OF THIS SOFTWARE.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Copyright (c) 1987, 1993, 1994
7cd594b8427fe742d44295790ba367e1de22a47dEvan Hunt * The Regents of the University of California. All rights reserved.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Redistribution and use in source and binary forms, with or without
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * modification, are permitted provided that the following conditions
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * 1. Redistributions of source code must retain the above copyright
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * notice, this list of conditions and the following disclaimer.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * 2. Redistributions in binary form must reproduce the above copyright
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * notice, this list of conditions and the following disclaimer in the
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * documentation and/or other materials provided with the distribution.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * 3. All advertising materials mentioning features or use of this software
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * must display the following acknowledgement:
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * This product includes software developed by the University of
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * California, Berkeley and its contributors.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * 4. Neither the name of the University nor the names of its contributors
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * may be used to endorse or promote products derived from this software
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * without specific prior written permission.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * SUCH DAMAGE.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington/* $Id: commandline.c,v 1.16 2004/03/05 05:10:44 marka Exp $ */
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * This file was adapted from the NetBSD project's source tree, RCS ID:
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * The primary change has been to rename items to the ISC namespace
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * and format in the ISC coding style.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Principal Authors: Computer Systems Research Group at UC Berkeley
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 * Principal ISC caretaker: DCL
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington/* Index into parent argv vector. */
b493dfe8bce94b05efc0f161238d32f1234c5670Brian WellingtonLIBISC_EXTERNAL_DATA int isc_commandline_index = 1;
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington/* Character checked for validity. */
b493dfe8bce94b05efc0f161238d32f1234c5670Brian WellingtonLIBISC_EXTERNAL_DATA int isc_commandline_option;
ffcbf9ea917725d49be6b006efa02613de5ef242Mark Andrews/* Argument associated with option. */
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉LIBISC_EXTERNAL_DATA char *isc_commandline_argument;
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉/* For printing error messages. */
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉LIBISC_EXTERNAL_DATA char *isc_commandline_progname;
7cd594b8427fe742d44295790ba367e1de22a47dEvan Hunt/* Print error messages. */
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_errprint = ISC_TRUE;
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉/* Reset processing. */
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_reset = ISC_TRUE;
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt * Parse argc/argv argument vector.
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Huntisc_commandline_parse(int argc, char * const *argv, const char *options) {
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt char *option; /* Index into *options of option. */
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 REQUIRE(argc >= 0 && argv != NULL && options != NULL);
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 * Update scanning pointer, either because a reset was requested or
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt * the previous argv was finished.
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 if (isc_commandline_reset || *place == '\0') {
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 *(place = argv[isc_commandline_index]) != '-') {
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 * Index out of range or points to non-option.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Found '--' to signal end of options. Advance
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * index to next argv, the first non-option.
0f1938c98281e79faeaffa3c481731e3f8b67c08Brian Wellington option = strchr(options, isc_commandline_option);
d457997b3b3ab1b79059707db89ae1dfb1d3c195Mark Andrews * Ensure valid option has been passed as specified by options string.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * '-:' is never a valid command line option because it could not
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * distinguish ':' from the argument specifier in the options string.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington if (isc_commandline_option == ':' || option == NULL) {
2ee2c1be1873d6fb477c085029ecc1dd63e8abb6Mark Andrews if (isc_commandline_errprint && *options != ':')
d457997b3b3ab1b79059707db89ae1dfb1d3c195Mark Andrews "illegal option"),
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Option does not take an argument.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Skip to next argv if at the end of the current argv.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Option needs an argument.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Option is in this argv, -D1 style.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Option is next argv, -D 1 style.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington isc_commandline_argument = argv[isc_commandline_index];
20c21edadacb58824d0c81505e26213070d6358bAndreas Gustafsson * Argument needed, but no more argv.
20c21edadacb58824d0c81505e26213070d6358bAndreas Gustafsson * Silent failure with "missing argument" return
d97255474029990830adfc050b2869726ccc6f6fBrian Wellington * when ':' starts options string, per historical spec.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington "option requires "
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington "an argument"),
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * Point to argv that follows argument.