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(BcError err, int c, const char *str) {
66 if (err == BC_ERROR_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_ERROR_FATAL_OPTION, option[0], str);
119 if (option[1]) o->subopt += 1;
125 ret = (int) option[0];
129 case BC_OPT_REQUIRED:
134 if (option[1]) o->optarg = option + 1;
135 else if (next != NULL) {
139 else bc_opt_error(BC_ERROR_FATAL_OPTION_NO_ARG, option[0],
140 bc_opt_longopt(longopts, option[0]));
143 ret = (int) option[0];
151 static bool bc_opt_longoptsMatch(const char *name, const char *option) {
153 const char *a = option, *n = name;
155 if (name == NULL) return false;
157 for (; *a && *n && *a != '='; ++a, ++n) {
158 if (*a != *n) return false;
161 return (*n == '\0' && (*a == '\0' || *a == '='));
164 static char* bc_opt_longoptsArg(char *option) {
166 for (; *option && *option != '='; ++option);
168 if (*option == '=') return option + 1;
172 int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
180 option = o->argv[o->optind];
181 if (option == NULL) return -1;
183 empty = !strcmp(option, "");
188 if (BC_OPT_ISDASHDASH(option)) {
194 else if (BC_OPT_ISSHORTOPT(option)) return bc_opt_parseShort(o, longopts);
195 else if (!BC_OPT_ISLONGOPT(option)) return -1;
200 // Skip "--" at beginning of the option.
204 for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++) {
206 const char *name = longopts[i].name;
208 if (bc_opt_longoptsMatch(name, option)) {
212 o->optopt = longopts[i].val;
213 arg = bc_opt_longoptsArg(option);
215 if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) ||
216 (longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC))
218 bc_opt_error(BC_ERROR_FATAL_OPTION, o->optopt, name);
221 if (longopts[i].type == BC_OPT_NONE && arg != NULL)
223 bc_opt_error(BC_ERROR_FATAL_OPTION_ARG, o->optopt, name);
226 if (arg != NULL) o->optarg = arg;
227 else if (longopts[i].type == BC_OPT_REQUIRED) {
229 o->optarg = o->argv[o->optind];
231 if (o->optarg != NULL) o->optind += 1;
232 else bc_opt_error(BC_ERROR_FATAL_OPTION_NO_ARG,
240 bc_opt_error(BC_ERROR_FATAL_OPTION, 0, option);
245 void bc_opt_init(BcOpt *o, char *argv[]) {