2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Adapted from https://github.com/skeeto/optparse
34 * *****************************************************************************
36 * Code for getopt_long() replacement. It turns out that getopt_long() has
37 * different behavior on different platforms.
51 * Returns true if index @a i is the end of the longopts array.
52 * @param longopts The long options array.
53 * @param i The index to test.
54 * @return True if @a i is the last index, false otherwise.
56 static inline bool bc_opt_longoptsEnd(const BcOptLong *longopts, size_t i) {
57 return !longopts[i].name && !longopts[i].val;
61 * Returns the name of the long option that matches the character @a c.
62 * @param longopts The long options array.
63 * @param c The character to match against.
64 * @return The name of the long option that matches @a c, or "NULL".
66 static const char* bc_opt_longopt(const BcOptLong *longopts, int c) {
70 for (i = 0; !bc_opt_longoptsEnd(longopts, i); ++i) {
71 if (longopts[i].val == c) return longopts[i].name;
80 * Issues a fatal error for an option parsing failure.
81 * @param err The error.
82 * @param c The character for the failing option.
83 * @param str Either the string for the failing option, or the invalid
85 * @param use_short True if the short option should be used for error printing,
88 static void bc_opt_error(BcErr err, int c, const char *str, bool use_short) {
90 if (err == BC_ERR_FATAL_OPTION) {
96 short_str[0] = (char) c;
99 bc_error(err, 0, short_str);
101 else bc_error(err, 0, str);
103 else bc_error(err, 0, (int) c, str);
107 * Returns the type of the long option that matches @a c.
108 * @param longopts The long options array.
109 * @param c The character to match against.
110 * @return The type of the long option as an integer, or -1 if none.
112 static int bc_opt_type(const BcOptLong *longopts, char c) {
116 if (c == ':') return -1;
118 for (i = 0; !bc_opt_longoptsEnd(longopts, i) && longopts[i].val != c; ++i);
120 if (bc_opt_longoptsEnd(longopts, i)) return -1;
122 return (int) longopts[i].type;
126 * Parses a short option.
127 * @param o The option parser.
128 * @param longopts The long options array.
129 * @return The character for the short option, or -1 if none left.
131 static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
135 char *option = o->argv[o->optind];
138 // Make sure to clear these.
142 // Get the next option.
143 option += o->subopt + 1;
144 o->optopt = option[0];
146 // Get the type and the next data.
147 type = bc_opt_type(longopts, option[0]);
148 next = o->argv[o->optind + 1];
156 // Check for invalid option and barf if so.
157 if (type == -1 || (type == BC_OPT_BC_ONLY && BC_IS_DC) ||
158 (type == BC_OPT_DC_ONLY && BC_IS_BC))
160 char str[2] = {0, 0};
165 bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str, true);
173 // If there is something else, update the suboption.
174 if (option[1]) o->subopt += 1;
177 // Go to the next argument.
182 ret = (int) option[0];
187 case BC_OPT_REQUIRED_BC_ONLY:
190 bc_opt_error(BC_ERR_FATAL_OPTION, option[0],
191 bc_opt_longopt(longopts, option[0]), true);
196 case BC_OPT_REQUIRED:
198 // Always go to the next argument.
202 // Use the next characters, if they exist.
203 if (option[1]) o->optarg = option + 1;
204 else if (next != NULL) {
210 // No argument, barf.
211 else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0],
212 bc_opt_longopt(longopts, option[0]), true);
215 ret = (int) option[0];
225 * Ensures that a long option argument matches a long option name, regardless of
226 * "=<data>" at the end.
227 * @param name The name to match.
228 * @param option The command-line argument.
229 * @return True if @a option matches @a name, false otherwise.
231 static bool bc_opt_longoptsMatch(const char *name, const char *option) {
233 const char *a = option, *n = name;
235 // Can never match a NULL name.
236 if (name == NULL) return false;
239 for (; *a && *n && *a != '='; ++a, ++n) {
240 if (*a != *n) return false;
243 // Ensure they both end at the same place.
244 return (*n == '\0' && (*a == '\0' || *a == '='));
248 * Returns a pointer to the argument of a long option, or NULL if it not in the
250 * @param option The option to find the argument of.
251 * @return A pointer to the argument of the option, or NULL if none.
253 static char* bc_opt_longoptsArg(char *option) {
255 // Find the end or equals sign.
256 for (; *option && *option != '='; ++option);
258 if (*option == '=') return option + 1;
262 int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
268 // This just eats empty options.
271 option = o->argv[o->optind];
272 if (option == NULL) return -1;
274 empty = !strcmp(option, "");
279 // If the option is just a "--".
280 if (BC_OPT_ISDASHDASH(option)) {
286 // Parse a short option.
287 else if (BC_OPT_ISSHORTOPT(option)) return bc_opt_parseShort(o, longopts);
288 // If the option is not long at this point, we are done.
289 else if (!BC_OPT_ISLONGOPT(option)) return -1;
295 // Skip "--" at beginning of the option.
299 // Loop through the valid long options.
300 for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++) {
302 const char *name = longopts[i].name;
304 // If we have a match...
305 if (bc_opt_longoptsMatch(name, option)) {
309 // Get the option char and the argument.
310 o->optopt = longopts[i].val;
311 arg = bc_opt_longoptsArg(option);
313 // Error if the option is invalid..
314 if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) ||
315 (longopts[i].type == BC_OPT_REQUIRED_BC_ONLY && BC_IS_DC) ||
316 (longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC))
318 bc_opt_error(BC_ERR_FATAL_OPTION, o->optopt, name, false);
321 // Error if we have an argument and should not.
322 if (longopts[i].type == BC_OPT_NONE && arg != NULL)
324 bc_opt_error(BC_ERR_FATAL_OPTION_ARG, o->optopt, name, false);
327 // Set the argument, or check the next argument if we don't have
329 if (arg != NULL) o->optarg = arg;
330 else if (longopts[i].type == BC_OPT_REQUIRED ||
331 longopts[i].type == BC_OPT_REQUIRED_BC_ONLY)
333 // Get the next argument.
334 o->optarg = o->argv[o->optind];
336 // All's good if it exists; otherwise, barf.
337 if (o->optarg != NULL) o->optind += 1;
338 else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG,
339 o->optopt, name, false);
346 // If we reach this point, the option is invalid.
347 bc_opt_error(BC_ERR_FATAL_OPTION, 0, option, false);
354 void bc_opt_init(BcOpt *o, char *argv[]) {