1 /*
2 * *****************************************************************************
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
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.
17 *
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.
29 *
30 * *****************************************************************************
31 *
32 * Adapted from https://github.com/skeeto/optparse
33 *
34 * *****************************************************************************
35 *
36 * Code for getopt_long() replacement. It turns out that getopt_long() has
37 * different behavior on different platforms.
38 *
39 */
40
41 #include <assert.h>
42 #include <stdbool.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include <status.h>
47 #include <opt.h>
48 #include <vm.h>
49
bc_opt_longoptsEnd(const BcOptLong * longopts,size_t i)50 static inline bool bc_opt_longoptsEnd(const BcOptLong *longopts, size_t i) {
51 return !longopts[i].name && !longopts[i].val;
52 }
53
bc_opt_longopt(const BcOptLong * longopts,int c)54 static const char* bc_opt_longopt(const BcOptLong *longopts, int c) {
55
56 size_t i;
57
58 for (i = 0; !bc_opt_longoptsEnd(longopts, i); ++i) {
59 if (longopts[i].val == c) return longopts[i].name;
60 }
61
62 return "NULL";
63 }
64
bc_opt_error(BcErr err,int c,const char * str)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);
68 }
69
bc_opt_type(const BcOptLong * longopts,char c)70 static int bc_opt_type(const BcOptLong *longopts, char c) {
71
72 size_t i;
73
74 if (c == ':') return -1;
75
76 for (i = 0; !bc_opt_longoptsEnd(longopts, i) && longopts[i].val != c; ++i);
77
78 if (bc_opt_longoptsEnd(longopts, i)) return -1;
79
80 return (int) longopts[i].type;
81 }
82
bc_opt_parseShort(BcOpt * o,const BcOptLong * longopts)83 static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
84
85 int type;
86 char *next;
87 char *option = o->argv[o->optind];
88 int ret = -1;
89
90 o->optopt = 0;
91 o->optarg = NULL;
92
93 option += o->subopt + 1;
94 o->optopt = option[0];
95
96 type = bc_opt_type(longopts, option[0]);
97 next = o->argv[o->optind + 1];
98
99 switch (type) {
100
101 case -1:
102 case BC_OPT_BC_ONLY:
103 case BC_OPT_DC_ONLY:
104 {
105 if (type == -1 || (type == BC_OPT_BC_ONLY && BC_IS_DC) ||
106 (type == BC_OPT_DC_ONLY && BC_IS_BC))
107 {
108 char str[2] = {0, 0};
109
110 str[0] = option[0];
111 o->optind += 1;
112
113 bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str);
114 }
115 }
116 // Fallthrough.
117 BC_FALLTHROUGH
118
119 case BC_OPT_NONE:
120 {
121 if (option[1]) o->subopt += 1;
122 else {
123 o->subopt = 0;
124 o->optind += 1;
125 }
126
127 ret = (int) option[0];
128 break;
129 }
130
131 case BC_OPT_REQUIRED:
132 {
133 o->subopt = 0;
134 o->optind += 1;
135
136 if (option[1]) o->optarg = option + 1;
137 else if (next != NULL) {
138 o->optarg = next;
139 o->optind += 1;
140 }
141 else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0],
142 bc_opt_longopt(longopts, option[0]));
143
144
145 ret = (int) option[0];
146 break;
147 }
148 }
149
150 return ret;
151 }
152
bc_opt_longoptsMatch(const char * name,const char * option)153 static bool bc_opt_longoptsMatch(const char *name, const char *option) {
154
155 const char *a = option, *n = name;
156
157 if (name == NULL) return false;
158
159 for (; *a && *n && *a != '='; ++a, ++n) {
160 if (*a != *n) return false;
161 }
162
163 return (*n == '\0' && (*a == '\0' || *a == '='));
164 }
165
bc_opt_longoptsArg(char * option)166 static char* bc_opt_longoptsArg(char *option) {
167
168 for (; *option && *option != '='; ++option);
169
170 if (*option == '=') return option + 1;
171 else return NULL;
172 }
173
bc_opt_parse(BcOpt * o,const BcOptLong * longopts)174 int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
175
176 size_t i;
177 char *option;
178 bool empty;
179
180 do {
181
182 option = o->argv[o->optind];
183 if (option == NULL) return -1;
184
185 empty = !strcmp(option, "");
186 o->optind += empty;
187
188 } while (empty);
189
190 if (BC_OPT_ISDASHDASH(option)) {
191
192 // Consume "--".
193 o->optind += 1;
194 return -1;
195 }
196 else if (BC_OPT_ISSHORTOPT(option)) return bc_opt_parseShort(o, longopts);
197 else if (!BC_OPT_ISLONGOPT(option)) return -1;
198
199 o->optopt = 0;
200 o->optarg = NULL;
201
202 // Skip "--" at beginning of the option.
203 option += 2;
204 o->optind += 1;
205
206 for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++) {
207
208 const char *name = longopts[i].name;
209
210 if (bc_opt_longoptsMatch(name, option)) {
211
212 char *arg;
213
214 o->optopt = longopts[i].val;
215 arg = bc_opt_longoptsArg(option);
216
217 if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) ||
218 (longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC))
219 {
220 bc_opt_error(BC_ERR_FATAL_OPTION, o->optopt, name);
221 }
222
223 if (longopts[i].type == BC_OPT_NONE && arg != NULL)
224 {
225 bc_opt_error(BC_ERR_FATAL_OPTION_ARG, o->optopt, name);
226 }
227
228 if (arg != NULL) o->optarg = arg;
229 else if (longopts[i].type == BC_OPT_REQUIRED) {
230
231 o->optarg = o->argv[o->optind];
232
233 if (o->optarg != NULL) o->optind += 1;
234 else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG,
235 o->optopt, name);
236 }
237
238 return o->optopt;
239 }
240 }
241
242 bc_opt_error(BC_ERR_FATAL_OPTION, 0, option);
243
244 return -1;
245 }
246
bc_opt_init(BcOpt * o,char * argv[])247 void bc_opt_init(BcOpt *o, char *argv[]) {
248 o->argv = argv;
249 o->optind = 1;
250 o->subopt = 0;
251 o->optarg = NULL;
252 }
253