xref: /freebsd/contrib/bc/src/opt.c (revision e17f5b1d)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2020 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 
50 static inline bool bc_opt_longoptsEnd(const BcOptLong *longopts, size_t i) {
51 	return !longopts[i].name && !longopts[i].val;
52 }
53 
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 
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);
68 }
69 
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 
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_ERROR_FATAL_OPTION, option[0], str);
114 			}
115 		}
116 		// Fallthrough.
117 		case BC_OPT_NONE:
118 		{
119 			if (option[1]) o->subopt += 1;
120 			else {
121 				o->subopt = 0;
122 				o->optind += 1;
123 			}
124 
125 			ret = (int) option[0];
126 			break;
127 		}
128 
129 		case BC_OPT_REQUIRED:
130 		{
131 			o->subopt = 0;
132 			o->optind += 1;
133 
134 			if (option[1]) o->optarg = option + 1;
135 			else if (next != NULL) {
136 				o->optarg = next;
137 				o->optind += 1;
138 			}
139 			else bc_opt_error(BC_ERROR_FATAL_OPTION_NO_ARG, option[0],
140 			                  bc_opt_longopt(longopts, option[0]));
141 
142 
143 			ret = (int) option[0];
144 			break;
145 		}
146 	}
147 
148 	return ret;
149 }
150 
151 static bool bc_opt_longoptsMatch(const char *name, const char *option) {
152 
153 	const char *a = option, *n = name;
154 
155 	if (name == NULL) return false;
156 
157 	for (; *a && *n && *a != '='; ++a, ++n) {
158 		if (*a != *n) return false;
159 	}
160 
161 	return (*n == '\0' && (*a == '\0' || *a == '='));
162 }
163 
164 static char* bc_opt_longoptsArg(char *option) {
165 
166 	for (; *option && *option != '='; ++option);
167 
168 	if (*option == '=') return option + 1;
169 	else return NULL;
170 }
171 
172 int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
173 
174 	size_t i;
175 	char *option;
176 	bool empty;
177 
178 	do {
179 
180 		option = o->argv[o->optind];
181 		if (option == NULL) return -1;
182 
183 		empty = !strcmp(option, "");
184 		o->optind += empty;
185 
186 	} while (empty);
187 
188 	if (BC_OPT_ISDASHDASH(option)) {
189 
190 		// Consume "--".
191 		o->optind += 1;
192 		return -1;
193 	}
194 	else if (BC_OPT_ISSHORTOPT(option)) return bc_opt_parseShort(o, longopts);
195 	else if (!BC_OPT_ISLONGOPT(option)) return -1;
196 
197 	o->optopt = 0;
198 	o->optarg = NULL;
199 
200 	// Skip "--" at beginning of the option.
201 	option += 2;
202 	o->optind += 1;
203 
204 	for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++) {
205 
206 		const char *name = longopts[i].name;
207 
208 		if (bc_opt_longoptsMatch(name, option)) {
209 
210 			char *arg;
211 
212 			o->optopt = longopts[i].val;
213 			arg = bc_opt_longoptsArg(option);
214 
215 			if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) ||
216 			    (longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC))
217 			{
218 				bc_opt_error(BC_ERROR_FATAL_OPTION, o->optopt, name);
219 			}
220 
221 			if (longopts[i].type == BC_OPT_NONE && arg != NULL)
222 			{
223 				bc_opt_error(BC_ERROR_FATAL_OPTION_ARG, o->optopt, name);
224 			}
225 
226 			if (arg != NULL) o->optarg = arg;
227 			else if (longopts[i].type == BC_OPT_REQUIRED) {
228 
229 				o->optarg = o->argv[o->optind];
230 
231 				if (o->optarg != NULL) o->optind += 1;
232 				else bc_opt_error(BC_ERROR_FATAL_OPTION_NO_ARG,
233 				                  o->optopt, name);
234 			}
235 
236 			return o->optopt;
237 		}
238 	}
239 
240 	bc_opt_error(BC_ERROR_FATAL_OPTION, 0, option);
241 
242 	return -1;
243 }
244 
245 void bc_opt_init(BcOpt *o, char *argv[]) {
246 	o->argv = argv;
247 	o->optind = 1;
248 	o->subopt = 0;
249 	o->optarg = NULL;
250 }
251