xref: /minix3/external/bsd/bind/dist/lib/isc/commandline.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: commandline.c,v 1.5 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Portions Copyright (C) 2004, 2005, 2007, 2008, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Portions Copyright (C) 1999-2001  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /*
21  * Copyright (c) 1987, 1993, 1994
22  *	The Regents of the University of California.  All rights reserved.
23  *
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.
35  *
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.
47  */
48 
49 /* Id: commandline.c,v 1.22 2008/09/25 04:02:39 tbox Exp  */
50 
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
54  *
55  * The primary change has been to rename items to the ISC namespace
56  * and format in the ISC coding style.
57  */
58 
59 /*
60  * \author Principal Authors: Computer Systems Research Group at UC Berkeley
61  * \author Principal ISC caretaker: DCL
62  */
63 
64 #include <config.h>
65 
66 #include <stdio.h>
67 
68 #include <isc/commandline.h>
69 #include <isc/msgs.h>
70 #include <isc/string.h>
71 #include <isc/util.h>
72 
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;
85 
86 static char endopt = '\0';
87 
88 #define	BADOPT	'?'
89 #define	BADARG	':'
90 #define ENDOPT  &endopt
91 
92 /*!
93  * getopt --
94  *	Parse argc/argv argument vector.
95  */
96 int
isc_commandline_parse(int argc,char * const * argv,const char * options)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. */
100 
101 	REQUIRE(argc >= 0 && argv != NULL && options != NULL);
102 
103 	/*
104 	 * Update scanning pointer, either because a reset was requested or
105 	 * the previous argv was finished.
106 	 */
107 	if (isc_commandline_reset || *place == '\0') {
108 		if (isc_commandline_reset) {
109 			isc_commandline_index = 1;
110 			isc_commandline_reset = ISC_FALSE;
111 		}
112 
113 		if (isc_commandline_progname == NULL)
114 			isc_commandline_progname = argv[0];
115 
116 		if (isc_commandline_index >= argc ||
117 		    *(place = argv[isc_commandline_index]) != '-') {
118 			/*
119 			 * Index out of range or points to non-option.
120 			 */
121 			place = ENDOPT;
122 			return (-1);
123 		}
124 
125 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
126 			/*
127 			 * Found '--' to signal end of options.  Advance
128 			 * index to next argv, the first non-option.
129 			 */
130 			isc_commandline_index++;
131 			place = ENDOPT;
132 			return (-1);
133 		}
134 	}
135 
136 	isc_commandline_option = *place++;
137 	option = strchr(options, isc_commandline_option);
138 
139 	/*
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.
143 	 */
144 	if (isc_commandline_option == ':' || option == NULL) {
145 		if (*place == '\0')
146 			isc_commandline_index++;
147 
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);
156 
157 		return (BADOPT);
158 	}
159 
160 	if (*++option != ':') {
161 		/*
162 		 * Option does not take an argument.
163 		 */
164 		isc_commandline_argument = NULL;
165 
166 		/*
167 		 * Skip to next argv if at the end of the current argv.
168 		 */
169 		if (*place == '\0')
170 			++isc_commandline_index;
171 
172 	} else {
173 		/*
174 		 * Option needs an argument.
175 		 */
176 		if (*place != '\0')
177 			/*
178 			 * Option is in this argv, -D1 style.
179 			 */
180 			isc_commandline_argument = place;
181 
182 		else if (argc > ++isc_commandline_index)
183 			/*
184 			 * Option is next argv, -D 1 style.
185 			 */
186 			isc_commandline_argument = argv[isc_commandline_index];
187 
188 		else {
189 			/*
190 			 * Argument needed, but no more argv.
191 			 */
192 			place = ENDOPT;
193 
194 			/*
195 			 * Silent failure with "missing argument" return
196 			 * when ':' starts options string, per historical spec.
197 			 */
198 			if (*options == ':')
199 				return (BADARG);
200 
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);
210 
211 			return (BADOPT);
212 		}
213 
214 		place = ENDOPT;
215 
216 		/*
217 		 * Point to argv that follows argument.
218 		 */
219 		isc_commandline_index++;
220 	}
221 
222 	return (isc_commandline_option);
223 }
224