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