2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 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.
57 bc_opt_longoptsEnd(const BcOptLong* longopts, size_t i)
59 return !longopts[i].name && !longopts[i].val;
63 * Returns the name of the long option that matches the character @a c.
64 * @param longopts The long options array.
65 * @param c The character to match against.
66 * @return The name of the long option that matches @a c, or "NULL".
69 bc_opt_longopt(const BcOptLong* longopts, int c)
73 for (i = 0; !bc_opt_longoptsEnd(longopts, i); ++i)
75 if (longopts[i].val == c) return longopts[i].name;
86 * Issues a fatal error for an option parsing failure.
87 * @param err The error.
88 * @param c The character for the failing option.
89 * @param str Either the string for the failing option, or the invalid
91 * @param use_short True if the short option should be used for error printing,
95 bc_opt_error(BcErr err, int c, const char* str, bool use_short)
97 if (err == BC_ERR_FATAL_OPTION)
103 short_str[0] = (char) c;
106 bc_error(err, 0, short_str);
108 else bc_error(err, 0, str);
110 else bc_error(err, 0, (int) c, str);
114 * Returns the type of the long option that matches @a c.
115 * @param longopts The long options array.
116 * @param c The character to match against.
117 * @return The type of the long option as an integer, or -1 if none.
120 bc_opt_type(const BcOptLong* longopts, char c)
124 if (c == ':') return -1;
126 for (i = 0; !bc_opt_longoptsEnd(longopts, i) && longopts[i].val != c; ++i)
131 if (bc_opt_longoptsEnd(longopts, i)) return -1;
133 return (int) longopts[i].type;
137 * Parses a short option.
138 * @param o The option parser.
139 * @param longopts The long options array.
140 * @return The character for the short option, or -1 if none left.
143 bc_opt_parseShort(BcOpt* o, const BcOptLong* longopts)
147 char* option = o->argv[o->optind];
150 // Make sure to clear these.
154 // Get the next option.
155 option += o->subopt + 1;
156 o->optopt = option[0];
158 // Get the type and the next data.
159 type = bc_opt_type(longopts, option[0]);
160 next = o->argv[o->optind + 1];
168 // Check for invalid option and barf if so.
169 if (type == -1 || (type == BC_OPT_BC_ONLY && BC_IS_DC) ||
170 (type == BC_OPT_DC_ONLY && BC_IS_BC))
172 char str[2] = { 0, 0 };
177 bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str, true);
186 // If there is something else, update the suboption.
187 if (option[1]) o->subopt += 1;
190 // Go to the next argument.
195 ret = (int) option[0];
200 case BC_OPT_REQUIRED_BC_ONLY:
205 bc_opt_error(BC_ERR_FATAL_OPTION, option[0],
206 bc_opt_longopt(longopts, option[0]), true);
214 case BC_OPT_REQUIRED:
216 // Always go to the next argument.
220 // Use the next characters, if they exist.
221 if (option[1]) o->optarg = option + 1;
222 else if (next != NULL)
228 // No argument, barf.
231 bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0],
232 bc_opt_longopt(longopts, option[0]), true);
235 ret = (int) option[0];
245 * Ensures that a long option argument matches a long option name, regardless of
246 * "=<data>" at the end.
247 * @param name The name to match.
248 * @param option The command-line argument.
249 * @return True if @a option matches @a name, false otherwise.
252 bc_opt_longoptsMatch(const char* name, const char* option)
254 const char* a = option;
255 const char* n = name;
257 // Can never match a NULL name.
258 if (name == NULL) return false;
261 for (; *a && *n && *a != '='; ++a, ++n)
263 if (*a != *n) return false;
266 // Ensure they both end at the same place.
267 return (*n == '\0' && (*a == '\0' || *a == '='));
271 * Returns a pointer to the argument of a long option, or NULL if it not in the
273 * @param option The option to find the argument of.
274 * @return A pointer to the argument of the option, or NULL if none.
277 bc_opt_longoptsArg(char* option)
279 // Find the end or equals sign.
280 for (; *option && *option != '='; ++option)
285 if (*option == '=') return option + 1;
290 bc_opt_parse(BcOpt* o, const BcOptLong* longopts)
296 // This just eats empty options.
299 option = o->argv[o->optind];
300 if (option == NULL) return -1;
302 empty = !strcmp(option, "");
307 // If the option is just a "--".
308 if (BC_OPT_ISDASHDASH(option))
314 // Parse a short option.
315 else if (BC_OPT_ISSHORTOPT(option)) return bc_opt_parseShort(o, longopts);
316 // If the option is not long at this point, we are done.
317 else if (!BC_OPT_ISLONGOPT(option)) return -1;
323 // Skip "--" at beginning of the option.
327 // Loop through the valid long options.
328 for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++)
330 const char* name = longopts[i].name;
332 // If we have a match...
333 if (bc_opt_longoptsMatch(name, option))
337 // Get the option char and the argument.
338 o->optopt = longopts[i].val;
339 arg = bc_opt_longoptsArg(option);
341 // Error if the option is invalid..
342 if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) ||
343 (longopts[i].type == BC_OPT_REQUIRED_BC_ONLY && BC_IS_DC) ||
344 (longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC))
346 bc_opt_error(BC_ERR_FATAL_OPTION, o->optopt, name, false);
349 // Error if we have an argument and should not.
350 if (longopts[i].type == BC_OPT_NONE && arg != NULL)
352 bc_opt_error(BC_ERR_FATAL_OPTION_ARG, o->optopt, name, false);
355 // Set the argument, or check the next argument if we don't have
357 if (arg != NULL) o->optarg = arg;
358 else if (longopts[i].type == BC_OPT_REQUIRED ||
359 longopts[i].type == BC_OPT_REQUIRED_BC_ONLY)
361 // Get the next argument.
362 o->optarg = o->argv[o->optind];
364 // All's good if it exists; otherwise, barf.
365 if (o->optarg != NULL) o->optind += 1;
368 bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, o->optopt, name,
377 // If we reach this point, the option is invalid.
378 bc_opt_error(BC_ERR_FATAL_OPTION, 0, option, false);
388 bc_opt_init(BcOpt* o, char* argv[])