commandline.c revision 1.2 1 1.2 christos /* $NetBSD: commandline.c,v 1.2 2018/08/12 13:02:37 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
7 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
8 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 1.1 christos *
10 1.1 christos * See the COPYRIGHT file distributed with this work for additional
11 1.1 christos * information regarding copyright ownership.
12 1.1 christos */
13 1.1 christos
14 1.1 christos /*
15 1.1 christos * Copyright (c) 1987, 1993, 1994
16 1.1 christos * The Regents of the University of California. All rights reserved.
17 1.1 christos *
18 1.1 christos * Redistribution and use in source and binary forms, with or without
19 1.1 christos * modification, are permitted provided that the following conditions
20 1.1 christos * are met:
21 1.1 christos * 1. Redistributions of source code must retain the above copyright
22 1.1 christos * notice, this list of conditions and the following disclaimer.
23 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
24 1.1 christos * notice, this list of conditions and the following disclaimer in the
25 1.1 christos * documentation and/or other materials provided with the distribution.
26 1.1 christos * 3. Neither the name of the University nor the names of its contributors
27 1.1 christos * may be used to endorse or promote products derived from this software
28 1.1 christos * without specific prior written permission.
29 1.1 christos *
30 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.1 christos * SUCH DAMAGE.
41 1.1 christos */
42 1.1 christos
43 1.1 christos
44 1.1 christos /*! \file
45 1.1 christos * This file was adapted from the NetBSD project's source tree, RCS ID:
46 1.1 christos * NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
47 1.1 christos *
48 1.1 christos * The primary change has been to rename items to the ISC namespace
49 1.1 christos * and format in the ISC coding style.
50 1.1 christos */
51 1.1 christos
52 1.1 christos #include <config.h>
53 1.1 christos
54 1.1 christos #include <stdio.h>
55 1.1 christos
56 1.1 christos #include <isc/commandline.h>
57 1.1 christos #include <isc/mem.h>
58 1.1 christos #include <isc/msgs.h>
59 1.1 christos #include <isc/print.h>
60 1.1 christos #include <isc/string.h>
61 1.1 christos #include <isc/util.h>
62 1.1 christos
63 1.1 christos /*% Index into parent argv vector. */
64 1.1 christos LIBISC_EXTERNAL_DATA int isc_commandline_index = 1;
65 1.1 christos /*% Character checked for validity. */
66 1.1 christos LIBISC_EXTERNAL_DATA int isc_commandline_option;
67 1.1 christos /*% Argument associated with option. */
68 1.1 christos LIBISC_EXTERNAL_DATA char *isc_commandline_argument;
69 1.1 christos /*% For printing error messages. */
70 1.1 christos LIBISC_EXTERNAL_DATA char *isc_commandline_progname;
71 1.1 christos /*% Print error messages. */
72 1.1 christos LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_errprint = ISC_TRUE;
73 1.1 christos /*% Reset processing. */
74 1.1 christos LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_reset = ISC_TRUE;
75 1.1 christos
76 1.1 christos static char endopt = '\0';
77 1.1 christos
78 1.1 christos #define BADOPT '?'
79 1.1 christos #define BADARG ':'
80 1.1 christos #define ENDOPT &endopt
81 1.1 christos
82 1.1 christos /*!
83 1.1 christos * getopt --
84 1.1 christos * Parse argc/argv argument vector.
85 1.1 christos */
86 1.1 christos int
87 1.1 christos isc_commandline_parse(int argc, char * const *argv, const char *options) {
88 1.1 christos static char *place = ENDOPT;
89 1.1 christos const char *option; /* Index into *options of option. */
90 1.1 christos
91 1.1 christos REQUIRE(argc >= 0 && argv != NULL && options != NULL);
92 1.1 christos
93 1.1 christos /*
94 1.1 christos * Update scanning pointer, either because a reset was requested or
95 1.1 christos * the previous argv was finished.
96 1.1 christos */
97 1.1 christos if (isc_commandline_reset || *place == '\0') {
98 1.1 christos if (isc_commandline_reset) {
99 1.1 christos isc_commandline_index = 1;
100 1.1 christos isc_commandline_reset = ISC_FALSE;
101 1.1 christos }
102 1.1 christos
103 1.1 christos if (isc_commandline_progname == NULL)
104 1.1 christos isc_commandline_progname = argv[0];
105 1.1 christos
106 1.1 christos if (isc_commandline_index >= argc ||
107 1.1 christos *(place = argv[isc_commandline_index]) != '-') {
108 1.1 christos /*
109 1.1 christos * Index out of range or points to non-option.
110 1.1 christos */
111 1.1 christos place = ENDOPT;
112 1.1 christos return (-1);
113 1.1 christos }
114 1.1 christos
115 1.1 christos if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
116 1.1 christos /*
117 1.1 christos * Found '--' to signal end of options. Advance
118 1.1 christos * index to next argv, the first non-option.
119 1.1 christos */
120 1.1 christos isc_commandline_index++;
121 1.1 christos place = ENDOPT;
122 1.1 christos return (-1);
123 1.1 christos }
124 1.1 christos }
125 1.1 christos
126 1.1 christos isc_commandline_option = *place++;
127 1.1 christos option = strchr(options, isc_commandline_option);
128 1.1 christos
129 1.1 christos /*
130 1.1 christos * Ensure valid option has been passed as specified by options string.
131 1.1 christos * '-:' is never a valid command line option because it could not
132 1.1 christos * distinguish ':' from the argument specifier in the options string.
133 1.1 christos */
134 1.1 christos if (isc_commandline_option == ':' || option == NULL) {
135 1.1 christos if (*place == '\0')
136 1.1 christos isc_commandline_index++;
137 1.1 christos
138 1.1 christos if (isc_commandline_errprint && *options != ':')
139 1.1 christos fprintf(stderr, "%s: %s -- %c\n",
140 1.1 christos isc_commandline_progname,
141 1.1 christos isc_msgcat_get(isc_msgcat,
142 1.1 christos ISC_MSGSET_COMMANDLINE,
143 1.1 christos ISC_MSG_ILLEGALOPT,
144 1.1 christos "illegal option"),
145 1.1 christos isc_commandline_option);
146 1.1 christos
147 1.1 christos return (BADOPT);
148 1.1 christos }
149 1.1 christos
150 1.1 christos if (*++option != ':') {
151 1.1 christos /*
152 1.1 christos * Option does not take an argument.
153 1.1 christos */
154 1.1 christos isc_commandline_argument = NULL;
155 1.1 christos
156 1.1 christos /*
157 1.1 christos * Skip to next argv if at the end of the current argv.
158 1.1 christos */
159 1.1 christos if (*place == '\0')
160 1.1 christos ++isc_commandline_index;
161 1.1 christos
162 1.1 christos } else {
163 1.1 christos /*
164 1.1 christos * Option needs an argument.
165 1.1 christos */
166 1.1 christos if (*place != '\0')
167 1.1 christos /*
168 1.1 christos * Option is in this argv, -D1 style.
169 1.1 christos */
170 1.1 christos isc_commandline_argument = place;
171 1.1 christos
172 1.1 christos else if (argc > ++isc_commandline_index)
173 1.1 christos /*
174 1.1 christos * Option is next argv, -D 1 style.
175 1.1 christos */
176 1.1 christos isc_commandline_argument = argv[isc_commandline_index];
177 1.1 christos
178 1.1 christos else {
179 1.1 christos /*
180 1.1 christos * Argument needed, but no more argv.
181 1.1 christos */
182 1.1 christos place = ENDOPT;
183 1.1 christos
184 1.1 christos /*
185 1.1 christos * Silent failure with "missing argument" return
186 1.1 christos * when ':' starts options string, per historical spec.
187 1.1 christos */
188 1.1 christos if (*options == ':')
189 1.1 christos return (BADARG);
190 1.1 christos
191 1.1 christos if (isc_commandline_errprint)
192 1.1 christos fprintf(stderr, "%s: %s -- %c\n",
193 1.1 christos isc_commandline_progname,
194 1.1 christos isc_msgcat_get(isc_msgcat,
195 1.1 christos ISC_MSGSET_COMMANDLINE,
196 1.1 christos ISC_MSG_OPTNEEDARG,
197 1.1 christos "option requires "
198 1.1 christos "an argument"),
199 1.1 christos isc_commandline_option);
200 1.1 christos
201 1.1 christos return (BADOPT);
202 1.1 christos }
203 1.1 christos
204 1.1 christos place = ENDOPT;
205 1.1 christos
206 1.1 christos /*
207 1.1 christos * Point to argv that follows argument.
208 1.1 christos */
209 1.1 christos isc_commandline_index++;
210 1.1 christos }
211 1.1 christos
212 1.1 christos return (isc_commandline_option);
213 1.1 christos }
214 1.1 christos
215 1.1 christos isc_result_t
216 1.1 christos isc_commandline_strtoargv(isc_mem_t *mctx, char *s, unsigned int *argcp,
217 1.1 christos char ***argvp, unsigned int n)
218 1.1 christos {
219 1.1 christos isc_result_t result;
220 1.1 christos
221 1.1 christos restart:
222 1.1 christos /* Discard leading whitespace. */
223 1.1 christos while (*s == ' ' || *s == '\t')
224 1.1 christos s++;
225 1.1 christos
226 1.1 christos if (*s == '\0') {
227 1.1 christos /* We have reached the end of the string. */
228 1.1 christos *argcp = n;
229 1.1 christos *argvp = isc_mem_get(mctx, n * sizeof(char *));
230 1.1 christos if (*argvp == NULL)
231 1.1 christos return (ISC_R_NOMEMORY);
232 1.1 christos } else {
233 1.1 christos char *p = s;
234 1.1 christos while (*p != ' ' && *p != '\t' && *p != '\0' && *p != '{') {
235 1.1 christos if (*p == '\n') {
236 1.1 christos *p = ' ';
237 1.1 christos goto restart;
238 1.1 christos }
239 1.1 christos p++;
240 1.1 christos }
241 1.1 christos
242 1.1 christos /* do "grouping", items between { and } are one arg */
243 1.1 christos if (*p == '{') {
244 1.1 christos char *t = p;
245 1.1 christos /*
246 1.1 christos * shift all characters to left by 1 to get rid of '{'
247 1.1 christos */
248 1.1 christos while (*t != '\0') {
249 1.1 christos t++;
250 1.1 christos *(t-1) = *t;
251 1.1 christos }
252 1.1 christos while (*p != '\0' && *p != '}') {
253 1.1 christos p++;
254 1.1 christos }
255 1.1 christos /* get rid of '}' character */
256 1.1 christos if (*p == '}') {
257 1.1 christos *p = '\0';
258 1.1 christos p++;
259 1.1 christos }
260 1.1 christos /* normal case, no "grouping" */
261 1.1 christos } else if (*p != '\0')
262 1.1 christos *p++ = '\0';
263 1.1 christos
264 1.1 christos result = isc_commandline_strtoargv(mctx, p,
265 1.1 christos argcp, argvp, n + 1);
266 1.1 christos if (result != ISC_R_SUCCESS)
267 1.1 christos return (result);
268 1.1 christos (*argvp)[n] = s;
269 1.1 christos }
270 1.1 christos
271 1.1 christos return (ISC_R_SUCCESS);
272 1.1 christos }
273