expr.y revision 1.50 1 /* $NetBSD: expr.y,v 1.50 2025/03/15 10:31:28 rillig Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jaromir Dolecek <jdolecek (at) NetBSD.org>, J.T. Conklin <jtc (at) NetBSD.org>
9 * and Roland Illig <rillig (at) NetBSD.org>.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 %{
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: expr.y,v 1.50 2025/03/15 10:31:28 rillig Exp $");
36
37 #include <sys/types.h>
38
39 #include <err.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <locale.h>
43 #include <regex.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 static const char * const *av;
50 static unsigned skip_level;
51
52 static void yyerror(const char *, ...) __dead;
53 static int yylex(void);
54 static int is_empty_or_zero(const char *);
55 static int is_integer(const char *);
56 static const char *eval_arith(const char *, const char *, const char *);
57 static int eval_compare(const char *, const char *, const char *);
58 static const char *eval_match(const char *, const char *);
59
60 #define YYSTYPE const char *
61
62 %}
63 %token STRING
64 %left SPEC_OR
65 %left SPEC_AND
66 %left COMPARE
67 %left ADD_SUB_OPERATOR
68 %left MUL_DIV_MOD_OPERATOR
69 %left SPEC_REG
70 %left LENGTH
71 %left LEFT_PARENT RIGHT_PARENT
72
73 %%
74
75 exp: expr {
76 (void)printf("%s\n", $1);
77 return is_empty_or_zero($1);
78 }
79 ;
80
81 expr: item
82 | expr SPEC_OR {
83 $$ = is_empty_or_zero($1) ? NULL : "1";
84 if ($$)
85 skip_level++;
86 } expr {
87 if ($3)
88 $$ = $1;
89 else
90 $$ = $4;
91 if ($3)
92 skip_level--;
93 }
94 | expr SPEC_AND {
95 $$ = is_empty_or_zero($1) ? NULL : "1";
96 if (!$$)
97 skip_level++;
98 } expr {
99 if ($3 && !is_empty_or_zero($4))
100 $$ = $1;
101 else
102 $$ = "0";
103 if (!$3)
104 skip_level--;
105 }
106 | expr SPEC_REG expr {
107 $$ = skip_level == 0 ? eval_match($1, $3) : "";
108 }
109 | expr ADD_SUB_OPERATOR expr {
110 $$ = skip_level == 0 ? eval_arith($1, $2, $3) : "";
111 }
112 | expr MUL_DIV_MOD_OPERATOR expr {
113 $$ = skip_level == 0 ? eval_arith($1, $2, $3) : "";
114 }
115 | expr COMPARE expr {
116 $$ = skip_level == 0 && eval_compare($1, $2, $3) ? "1" : "0";
117 }
118 | LEFT_PARENT expr RIGHT_PARENT {
119 $$ = $2;
120 }
121 | LENGTH expr {
122 char *ln;
123
124 asprintf(&ln, "%ld", (long) strlen($2));
125 if (ln == NULL)
126 err(1, NULL);
127 $$ = ln;
128 }
129 ;
130
131 item: STRING
132 | ADD_SUB_OPERATOR
133 | MUL_DIV_MOD_OPERATOR
134 | COMPARE
135 | SPEC_OR
136 | SPEC_AND
137 | SPEC_REG
138 | LENGTH
139 ;
140 %%
141
142 static int
143 is_empty_or_zero(const char *str)
144 {
145 char *endptr;
146
147 return str[0] == '\0'
148 || (strtoll(str, &endptr, 10) == 0 && endptr[0] == '\0');
149 }
150
151 static int
152 is_integer(const char *str)
153 {
154 char *endptr;
155
156 (void)strtoll(str, &endptr, 10);
157 /* note we treat empty string as valid number */
158 return endptr[0] == '\0';
159 }
160
161 static int64_t
162 to_integer(const char *str)
163 {
164 errno = 0;
165 int64_t num = strtoll(str, NULL, 10);
166 if (errno == ERANGE) {
167 yyerror("value '%s' is too %s is %lld", str,
168 num > 0 ? "big, maximum" : "small, minimum",
169 num > 0 ? LLONG_MAX : LLONG_MIN);
170 }
171 return num;
172 }
173
174 static const char *
175 eval_arith(const char *left, const char *op, const char *right)
176 {
177 int64_t res, l, r;
178
179 res = 0;
180
181 if (!is_integer(left))
182 yyerror("non-integer argument '%s'", left);
183 if (!is_integer(right))
184 yyerror("non-integer argument '%s'", right);
185
186 l = to_integer(left);
187 r = to_integer(right);
188
189 switch (op[0]) {
190 case '+':
191 if ((r > 0 && l > INT64_MAX - r) ||
192 (r < 0 && l < INT64_MIN - r))
193 goto integer_overflow;
194 res = l + r;
195 break;
196 case '-':
197 if ((r > 0 && l < INT64_MIN + r) ||
198 (r < 0 && l > INT64_MAX + r))
199 goto integer_overflow;
200 res = l - r;
201 break;
202 case '/':
203 if (r == 0)
204 goto invalid_zero;
205 if (l == INT64_MIN && r == -1)
206 goto integer_overflow;
207 res = l / r;
208 break;
209 case '%':
210 if (r == 0)
211 goto invalid_zero;
212 if (l == INT64_MIN && r == -1)
213 goto integer_overflow;
214 res = l % r;
215 break;
216 case '*':
217 if (l < 0 && r < 0 && l != INT64_MIN && r != INT64_MIN) {
218 l = -l;
219 r = -r;
220 }
221
222 if (l < 0 && r >= 0) {
223 int64_t tmp = l;
224 l = r;
225 r = tmp;
226 }
227
228 if ((l < 0 && r < 0) ||
229 (r > 0 && l > INT64_MAX / r) ||
230 (r <= 0 && l != 0 && r < INT64_MIN / l))
231 goto integer_overflow;
232 res = l * r;
233 break;
234 }
235
236 char *val;
237 (void)asprintf(&val, "%lld", (long long int)res);
238 if (val == NULL)
239 err(1, NULL);
240 return val;
241
242 integer_overflow:
243 yyerror("integer overflow or underflow occurred for "
244 "operation '%s %s %s'", left, op, right);
245
246 invalid_zero:
247 yyerror("second argument to '%s' must not be zero", op);
248 }
249
250 static int
251 eval_compare(const char *left, const char *op, const char *right)
252 {
253 int64_t l, r;
254
255 if (is_integer(left) && is_integer(right)) {
256 l = strtoll(left, NULL, 10);
257 r = strtoll(right, NULL, 10);
258 } else {
259 l = strcoll(left, right);
260 r = 0;
261 }
262
263 switch (op[0]) {
264 case '=':
265 return l == r;
266 case '>':
267 if (op[1] == '=')
268 return l >= r;
269 else
270 return l > r;
271 case '<':
272 if (op[1] == '=')
273 return l <= r;
274 else
275 return l < r;
276 default:
277 return l != r;
278 }
279 }
280
281 static const char *
282 eval_match(const char *str, const char *re)
283 {
284 regex_t rp;
285 regmatch_t rm[2];
286 int rc;
287
288 if ((rc = regcomp(&rp, re, REG_BASIC)) != 0) {
289 char errbuf[256];
290 (void)regerror(rc, &rp, errbuf, sizeof(errbuf));
291 yyerror("%s", errbuf);
292 }
293
294 if (regexec(&rp, str, 2, rm, 0) == 0 && rm[0].rm_so == 0) {
295 char *val;
296 if (rm[1].rm_so >= 0) {
297 (void)asprintf(&val, "%.*s",
298 (int)(rm[1].rm_eo - rm[1].rm_so),
299 str + rm[1].rm_so);
300 } else {
301 (void)asprintf(&val, "%d",
302 (int)(rm[0].rm_eo - rm[0].rm_so));
303 }
304 if (val == NULL)
305 err(1, NULL);
306 return val;
307 }
308
309 if (rp.re_nsub == 0)
310 return "0";
311 else
312 return "";
313 }
314
315 static const char *x = "|&=<>+-*/%:()";
316 static const int x_token[] = {
317 SPEC_OR, SPEC_AND, COMPARE, COMPARE, COMPARE, ADD_SUB_OPERATOR,
318 ADD_SUB_OPERATOR, MUL_DIV_MOD_OPERATOR, MUL_DIV_MOD_OPERATOR,
319 MUL_DIV_MOD_OPERATOR, SPEC_REG, LEFT_PARENT, RIGHT_PARENT
320 };
321
322 static int handle_ddash = 1;
323
324 int
325 yylex(void)
326 {
327 const char *p = *av++;
328 int retval;
329
330 if (!p)
331 retval = 0;
332 else if (p[0] == '\0')
333 retval = STRING;
334 else if (p[1] == '\0') {
335 const char *w = strchr(x, p[0]);
336 if (w) {
337 retval = x_token[w-x];
338 } else {
339 retval = STRING;
340 }
341 } else if (p[1] == '=' && p[2] == '\0'
342 && (p[0] == '>' || p[0] == '<' || p[0] == '!'))
343 retval = COMPARE;
344 else if (handle_ddash && p[0] == '-' && p[1] == '-' && p[2] == '\0') {
345 /* ignore "--" if passed as first argument and isn't followed
346 * by another STRING */
347 retval = yylex();
348 if (retval != STRING && retval != LEFT_PARENT
349 && retval != RIGHT_PARENT) {
350 /* is not followed by string or parenthesis, use as
351 * STRING */
352 retval = STRING;
353 av--; /* was increased in call to yylex() above */
354 p = "--";
355 } else {
356 /* "--" is to be ignored */
357 p = yylval;
358 }
359 } else if (strcmp(p, "length") == 0)
360 retval = LENGTH;
361 else
362 retval = STRING;
363
364 handle_ddash = 0;
365 yylval = p;
366
367 return retval;
368 }
369
370 /*
371 * Print error message and exit with error 2 (syntax error).
372 */
373 static __printflike(1, 2) void
374 yyerror(const char *fmt, ...)
375 {
376 va_list arg;
377
378 va_start(arg, fmt);
379 verrx(2, fmt, arg);
380 va_end(arg);
381 }
382
383 int
384 main(int argc, const char * const *argv)
385 {
386 setprogname(argv[0]);
387 (void)setlocale(LC_ALL, "");
388
389 if (argc == 1) {
390 (void)fprintf(stderr, "usage: %s expression\n",
391 getprogname());
392 exit(2);
393 }
394
395 av = argv + 1;
396
397 return yyparse();
398 }
399