2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 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.
50 static inline bool bc_opt_longoptsEnd(const BcOptLong *longopts, size_t i) {
51 return !longopts[i].name && !longopts[i].val;
54 static const char* bc_opt_longopt(const BcOptLong *longopts, int c) {
58 for (i = 0; !bc_opt_longoptsEnd(longopts, i); ++i) {
59 if (longopts[i].val == c) return longopts[i].name;
65 static void bc_opt_error(BcErr err, int c, const char *str) {
66 if (err == BC_ERR_FATAL_OPTION) bc_vm_error(err, 0, str);
67 else bc_vm_error(err, 0, (int) c, str);
70 static int bc_opt_type(const BcOptLong *longopts, char c) {
74 if (c == ':') return -1;
76 for (i = 0; !bc_opt_longoptsEnd(longopts, i) && longopts[i].val != c; ++i);
78 if (bc_opt_longoptsEnd(longopts, i)) return -1;
80 return (int) longopts[i].type;
83 static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
87 char *option = o->argv[o->optind];
93 option += o->subopt + 1;
94 o->optopt = option[0];
96 type = bc_opt_type(longopts, option[0]);
97 next = o->argv[o->optind + 1];
105 if (type == -1 || (type == BC_OPT_BC_ONLY && BC_IS_DC) ||
106 (type == BC_OPT_DC_ONLY && BC_IS_BC))
108 char str[2] = {0, 0};
113 bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str);
121 if (option[1]) o->subopt += 1;
127 ret = (int) option[0];
131 case BC_OPT_REQUIRED:
136 if (option[1]) o->optarg = option + 1;
137 else if (next != NULL) {
141 else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0],
142 bc_opt_longopt(longopts, option[0]));
145 ret = (int) option[0];
153 static bool bc_opt_longoptsMatch(const char *name, const char *option) {
155 const char *a = option, *n = name;
157 if (name == NULL) return false;
159 for (; *a && *n && *a != '='; ++a, ++n) {
160 if (*a != *n) return false;
163 return (*n == '\0' && (*a == '\0' || *a == '='));
166 static char* bc_opt_longoptsArg(char *option) {
168 for (; *option && *option != '='; ++option);
170 if (*option == '=') return option + 1;
174 int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
182 option = o->argv[o->optind];
183 if (option == NULL) return -1;
185 empty = !strcmp(option, "");
190 if (BC_OPT_ISDASHDASH(option)) {
196 else if (BC_OPT_ISSHORTOPT(option)) return bc_opt_parseShort(o, longopts);
197 else if (!BC_OPT_ISLONGOPT(option)) return -1;
202 // Skip "--" at beginning of the option.
206 for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++) {
208 const char *name = longopts[i].name;
210 if (bc_opt_longoptsMatch(name, option)) {
214 o->optopt = longopts[i].val;
215 arg = bc_opt_longoptsArg(option);
217 if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) ||
218 (longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC))
220 bc_opt_error(BC_ERR_FATAL_OPTION, o->optopt, name);
223 if (longopts[i].type == BC_OPT_NONE && arg != NULL)
225 bc_opt_error(BC_ERR_FATAL_OPTION_ARG, o->optopt, name);
228 if (arg != NULL) o->optarg = arg;
229 else if (longopts[i].type == BC_OPT_REQUIRED) {
231 o->optarg = o->argv[o->optind];
233 if (o->optarg != NULL) o->optind += 1;
234 else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG,
242 bc_opt_error(BC_ERR_FATAL_OPTION, 0, option);
247 void bc_opt_init(BcOpt *o, char *argv[]) {