commandline.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Portions Copyright (C) 1999-2001, 2004, 2005, 2007, 2008, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. 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. Neither the name of the University 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 REGENTS 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 REGENTS 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.
*/
/* $Id: commandline.c,v 1.22 2008/09/25 04:02:39 tbox Exp $ */
/*! \file
* This file was adapted from the NetBSD project's source tree, RCS ID:
* NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
*
* The primary change has been to rename items to the ISC namespace
* and format in the ISC coding style.
*/
/*
* \author Principal Authors: Computer Systems Research Group at UC Berkeley
* \author Principal ISC caretaker: DCL
*/
#include <config.h>
#include <stdio.h>
#include <isc/commandline.h>
/*% Index into parent argv vector. */
/*% Character checked for validity. */
/*% Argument associated with option. */
/*% For printing error messages. */
/*% Print error messages. */
/*% Reset processing. */
static char endopt = '\0';
#define BADOPT '?'
#define BADARG ':'
/*!
* getopt --
*/
int
const char *option; /* Index into *options of option. */
/*
* Update scanning pointer, either because a reset was requested or
* the previous argv was finished.
*/
if (isc_commandline_reset) {
}
if (isc_commandline_progname == NULL)
isc_commandline_progname = argv[0];
if (isc_commandline_index >= argc ||
/*
* Index out of range or points to non-option.
*/
return (-1);
}
/*
* Found '--' to signal end of options. Advance
* index to next argv, the first non-option.
*/
return (-1);
}
}
isc_commandline_option = *place++;
/*
* Ensure valid option has been passed as specified by options string.
* '-:' is never a valid command line option because it could not
* distinguish ':' from the argument specifier in the options string.
*/
if (*place == '\0')
"illegal option"),
return (BADOPT);
}
if (*++option != ':') {
/*
* Option does not take an argument.
*/
/*
* Skip to next argv if at the end of the current argv.
*/
if (*place == '\0')
} else {
/*
* Option needs an argument.
*/
if (*place != '\0')
/*
* Option is in this argv, -D1 style.
*/
else if (argc > ++isc_commandline_index)
/*
* Option is next argv, -D 1 style.
*/
else {
/*
* Argument needed, but no more argv.
*/
/*
* Silent failure with "missing argument" return
* when ':' starts options string, per historical spec.
*/
if (*options == ':')
return (BADARG);
"option requires "
"an argument"),
return (BADOPT);
}
/*
* Point to argv that follows argument.
*/
}
return (isc_commandline_option);
}
char ***argvp, unsigned int n)
{
/* Discard leading whitespace. */
while (*s == ' ' || *s == '\t')
s++;
if (*s == '\0') {
/* We have reached the end of the string. */
*argcp = n;
return (ISC_R_NOMEMORY);
} else {
char *p = s;
while (*p != ' ' && *p != '\t' && *p != '\0' && *p != '{') {
if (*p == '\n') {
*p = ' ';
goto restart;
}
p++;
}
/* do "grouping", items between { and } are one arg */
if (*p == '{') {
char *t = p;
/*
* shift all characters to left by 1 to get rid of '{'
*/
while (*t != '\0') {
t++;
*(t-1) = *t;
}
while (*p != '\0' && *p != '}') {
p++;
}
/* get rid of '}' character */
if (*p == '}') {
*p = '\0';
p++;
}
/* normal case, no "grouping" */
} else if (*p != '\0')
*p++ = '\0';
if (result != ISC_R_SUCCESS)
return (result);
(*argvp)[n] = s;
}
return (ISC_R_SUCCESS);
}