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