Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / lib / isc / commandline.c
blobd49a2414af1ed2be0d784e86b90fa262c4d7ff7a
1 /* $NetBSD: commandline.c,v 1.5 2014/12/10 04:37:59 christos Exp $ */
3 /*
4 * Portions Copyright (C) 2004, 2005, 2007, 2008, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Copyright (c) 1987, 1993, 1994
22 * The Regents of the University of California. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. Neither the name of the University nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
49 /* Id: commandline.c,v 1.22 2008/09/25 04:02:39 tbox Exp */
51 /*! \file
52 * This file was adapted from the NetBSD project's source tree, RCS ID:
53 * NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
55 * The primary change has been to rename items to the ISC namespace
56 * and format in the ISC coding style.
60 * \author Principal Authors: Computer Systems Research Group at UC Berkeley
61 * \author Principal ISC caretaker: DCL
64 #include <config.h>
66 #include <stdio.h>
68 #include <isc/commandline.h>
69 #include <isc/msgs.h>
70 #include <isc/string.h>
71 #include <isc/util.h>
73 /*% Index into parent argv vector. */
74 LIBISC_EXTERNAL_DATA int isc_commandline_index = 1;
75 /*% Character checked for validity. */
76 LIBISC_EXTERNAL_DATA int isc_commandline_option;
77 /*% Argument associated with option. */
78 LIBISC_EXTERNAL_DATA char *isc_commandline_argument;
79 /*% For printing error messages. */
80 LIBISC_EXTERNAL_DATA char *isc_commandline_progname;
81 /*% Print error messages. */
82 LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_errprint = ISC_TRUE;
83 /*% Reset processing. */
84 LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_reset = ISC_TRUE;
86 static char endopt = '\0';
88 #define BADOPT '?'
89 #define BADARG ':'
90 #define ENDOPT &endopt
92 /*!
93 * getopt --
94 * Parse argc/argv argument vector.
96 int
97 isc_commandline_parse(int argc, char * const *argv, const char *options) {
98 static char *place = ENDOPT;
99 char *option; /* Index into *options of option. */
101 REQUIRE(argc >= 0 && argv != NULL && options != NULL);
104 * Update scanning pointer, either because a reset was requested or
105 * the previous argv was finished.
107 if (isc_commandline_reset || *place == '\0') {
108 if (isc_commandline_reset) {
109 isc_commandline_index = 1;
110 isc_commandline_reset = ISC_FALSE;
113 if (isc_commandline_progname == NULL)
114 isc_commandline_progname = argv[0];
116 if (isc_commandline_index >= argc ||
117 *(place = argv[isc_commandline_index]) != '-') {
119 * Index out of range or points to non-option.
121 place = ENDOPT;
122 return (-1);
125 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
127 * Found '--' to signal end of options. Advance
128 * index to next argv, the first non-option.
130 isc_commandline_index++;
131 place = ENDOPT;
132 return (-1);
136 isc_commandline_option = *place++;
137 option = strchr(options, isc_commandline_option);
140 * Ensure valid option has been passed as specified by options string.
141 * '-:' is never a valid command line option because it could not
142 * distinguish ':' from the argument specifier in the options string.
144 if (isc_commandline_option == ':' || option == NULL) {
145 if (*place == '\0')
146 isc_commandline_index++;
148 if (isc_commandline_errprint && *options != ':')
149 fprintf(stderr, "%s: %s -- %c\n",
150 isc_commandline_progname,
151 isc_msgcat_get(isc_msgcat,
152 ISC_MSGSET_COMMANDLINE,
153 ISC_MSG_ILLEGALOPT,
154 "illegal option"),
155 isc_commandline_option);
157 return (BADOPT);
160 if (*++option != ':') {
162 * Option does not take an argument.
164 isc_commandline_argument = NULL;
167 * Skip to next argv if at the end of the current argv.
169 if (*place == '\0')
170 ++isc_commandline_index;
172 } else {
174 * Option needs an argument.
176 if (*place != '\0')
178 * Option is in this argv, -D1 style.
180 isc_commandline_argument = place;
182 else if (argc > ++isc_commandline_index)
184 * Option is next argv, -D 1 style.
186 isc_commandline_argument = argv[isc_commandline_index];
188 else {
190 * Argument needed, but no more argv.
192 place = ENDOPT;
195 * Silent failure with "missing argument" return
196 * when ':' starts options string, per historical spec.
198 if (*options == ':')
199 return (BADARG);
201 if (isc_commandline_errprint)
202 fprintf(stderr, "%s: %s -- %c\n",
203 isc_commandline_progname,
204 isc_msgcat_get(isc_msgcat,
205 ISC_MSGSET_COMMANDLINE,
206 ISC_MSG_OPTNEEDARG,
207 "option requires "
208 "an argument"),
209 isc_commandline_option);
211 return (BADOPT);
214 place = ENDOPT;
217 * Point to argv that follows argument.
219 isc_commandline_index++;
222 return (isc_commandline_option);