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