commandline.c 6.61 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Portions Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
Brian Wellington's avatar
Brian Wellington committed
3
 * Portions Copyright (C) 1999-2001  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
16 17 18 19 20 21 22 23 24 25 26 27 28 29
 */

/*
 * 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
 *    documentation and/or other materials provided with the distribution.
30
 * 3. Neither the name of the University nor the names of its contributors
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *    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.
 */

Automatic Updater's avatar
Automatic Updater committed
47
/* $Id: commandline.c,v 1.22 2008/09/25 04:02:39 tbox Exp $ */
48

49
/*! \file
50 51 52 53 54 55 56 57
 * 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.
 */

/*
58 59
 * \author Principal Authors: Computer Systems Research Group at UC Berkeley
 * \author Principal ISC caretaker: DCL
60 61
 */

David Lawrence's avatar
David Lawrence committed
62 63
#include <config.h>

64 65 66
#include <stdio.h>

#include <isc/commandline.h>
67
#include <isc/msgs.h>
68
#include <isc/string.h>
Bob Halley's avatar
Bob Halley committed
69
#include <isc/util.h>
70

71
/*% Index into parent argv vector. */
Danny Mayer's avatar
Danny Mayer committed
72
LIBISC_EXTERNAL_DATA int isc_commandline_index = 1;
73
/*% Character checked for validity. */
Danny Mayer's avatar
Danny Mayer committed
74
LIBISC_EXTERNAL_DATA int isc_commandline_option;
75
/*% Argument associated with option. */
Danny Mayer's avatar
Danny Mayer committed
76
LIBISC_EXTERNAL_DATA char *isc_commandline_argument;
77
/*% For printing error messages. */
Danny Mayer's avatar
Danny Mayer committed
78
LIBISC_EXTERNAL_DATA char *isc_commandline_progname;
79
/*% Print error messages. */
Danny Mayer's avatar
Danny Mayer committed
80
LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_errprint = ISC_TRUE;
81
/*% Reset processing. */
Danny Mayer's avatar
Danny Mayer committed
82
LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_reset = ISC_TRUE;
83

David Lawrence's avatar
David Lawrence committed
84 85
static char endopt = '\0';

86 87
#define	BADOPT	'?'
#define	BADARG	':'
David Lawrence's avatar
David Lawrence committed
88
#define ENDOPT  &endopt
89

90
/*!
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 * getopt --
 *	Parse argc/argv argument vector.
 */
int
isc_commandline_parse(int argc, char * const *argv, const char *options) {
	static char *place = ENDOPT;
	char *option;			/* Index into *options of option. */

	REQUIRE(argc >= 0 && argv != NULL && options != NULL);

	/*
	 * Update scanning pointer, either because a reset was requested or
	 * the previous argv was finished.
	 */
	if (isc_commandline_reset || *place == '\0') {
106 107 108 109
		if (isc_commandline_reset) {
			isc_commandline_index = 1;
			isc_commandline_reset = ISC_FALSE;
		}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

		if (isc_commandline_progname == NULL)
			isc_commandline_progname = argv[0];

		if (isc_commandline_index >= argc ||
		    *(place = argv[isc_commandline_index]) != '-') {
			/*
			 * Index out of range or points to non-option.
			 */
			place = ENDOPT;
			return (-1);
		}

		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
			/*
			 * Found '--' to signal end of options.  Advance
			 * index to next argv, the first non-option.
			 */
			isc_commandline_index++;
			place = ENDOPT;
			return (-1);
		}
	}

	isc_commandline_option = *place++;
	option = strchr(options, isc_commandline_option);

	/*
	 * 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 (isc_commandline_option == ':' || option == NULL) {
		if (*place == '\0')
			isc_commandline_index++;

		if (isc_commandline_errprint && *options != ':')
147
			fprintf(stderr, "%s: %s -- %c\n",
148
				isc_commandline_progname,
149 150 151 152
				isc_msgcat_get(isc_msgcat,
					       ISC_MSGSET_COMMANDLINE,
					       ISC_MSG_ILLEGALOPT,
					       "illegal option"),
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
				isc_commandline_option);

		return (BADOPT);
	}

	if (*++option != ':') {
		/*
		 * Option does not take an argument.
		 */
		isc_commandline_argument = NULL;

		/*
		 * Skip to next argv if at the end of the current argv.
		 */
		if (*place == '\0')
			++isc_commandline_index;

	} else {
		/*
		 * Option needs an argument.
		 */
		if (*place != '\0')
			/*
			 * Option is in this argv, -D1 style.
			 */
			isc_commandline_argument = place;

		else if (argc > ++isc_commandline_index)
			/*
			 * Option is next argv, -D 1 style.
			 */
			isc_commandline_argument = argv[isc_commandline_index];

		else {
			/*
			 * Argument needed, but no more argv.
			 */
			place = ENDOPT;

			/*
			 * Silent failure with "missing argument" return
			 * when ':' starts options string, per historical spec.
			 */
			if (*options == ':')
				return (BADARG);

			if (isc_commandline_errprint)
200 201 202 203 204
				fprintf(stderr, "%s: %s -- %c\n",
					isc_commandline_progname,
					isc_msgcat_get(isc_msgcat,
						       ISC_MSGSET_COMMANDLINE,
						       ISC_MSG_OPTNEEDARG,
Brian Wellington's avatar
Brian Wellington committed
205
						       "option requires "
206 207
						       "an argument"),
					isc_commandline_option);
208 209 210 211 212 213 214 215 216 217 218 219 220 221

			return (BADOPT);
		}

		place = ENDOPT;

		/*
		 * Point to argv that follows argument.
		 */
		isc_commandline_index++;
	}

	return (isc_commandline_option);
}