xref: /netbsd-src/bin/expr/expr.y (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: expr.y,v 1.33 2006/03/17 14:43:11 rumble 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@NetBSD.org> and J.T. Conklin <jtc@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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 %{
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: expr.y,v 1.33 2006/03/17 14:43:11 rumble Exp $");
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <limits.h>
49 #include <locale.h>
50 #include <regex.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 static const char * const *av;
57 
58 static void yyerror(const char *, ...);
59 static int yylex(void);
60 static int is_zero_or_null(const char *);
61 static int is_integer(const char *);
62 static int64_t perform_arith_op(const char *, const char *, const char *);
63 
64 int main(int, const char * const *);
65 
66 #define YYSTYPE	const char *
67 
68 %}
69 %token STRING
70 %left SPEC_OR
71 %left SPEC_AND
72 %left COMPARE
73 %left ADD_SUB_OPERATOR
74 %left MUL_DIV_MOD_OPERATOR
75 %left SPEC_REG
76 %left LENGTH
77 %left LEFT_PARENT RIGHT_PARENT
78 
79 %%
80 
81 exp:	expr = {
82 		(void) printf("%s\n", $1);
83 		return (is_zero_or_null($1));
84 		}
85 	;
86 
87 expr:	item { $$ = $1; }
88 	| expr SPEC_OR expr = {
89 		/*
90 		 * Return evaluation of first expression if it is neither
91 		 * an empty string nor zero; otherwise, returns the evaluation
92 		 * of second expression.
93 		 */
94 		if (!is_zero_or_null($1))
95 			$$ = $1;
96 		else
97 			$$ = $3;
98 		}
99 	| expr SPEC_AND expr = {
100 		/*
101 		 * Returns the evaluation of first expr if neither expression
102 		 * evaluates to an empty string or zero; otherwise, returns
103 		 * zero.
104 		 */
105 		if (!is_zero_or_null($1) && !is_zero_or_null($3))
106 			$$ = $1;
107 		else
108 			$$ = "0";
109 		}
110 	| expr SPEC_REG expr = {
111 		/*
112 		 * The ``:'' operator matches first expr against the second,
113 		 * which must be a regular expression.
114 		 */
115 		regex_t rp;
116 		regmatch_t rm[2];
117 		int eval;
118 
119 		/* compile regular expression */
120 		if ((eval = regcomp(&rp, $3, REG_BASIC)) != 0) {
121 			char errbuf[256];
122 			(void)regerror(eval, &rp, errbuf, sizeof(errbuf));
123 			yyerror("%s", errbuf);
124 			/* NOT REACHED */
125 		}
126 
127 		/* compare string against pattern --  remember that patterns
128 		   are anchored to the beginning of the line */
129 		if (regexec(&rp, $1, 2, rm, 0) == 0 && rm[0].rm_so == 0) {
130 			char *val;
131 			if (rm[1].rm_so >= 0) {
132 				(void) asprintf(&val, "%.*s",
133 					(int) (rm[1].rm_eo - rm[1].rm_so),
134 					$1 + rm[1].rm_so);
135 			} else {
136 				(void) asprintf(&val, "%d",
137 					(int)(rm[0].rm_eo - rm[0].rm_so));
138 			}
139 			if (val == NULL)
140 				err(1, NULL);
141 			$$ = val;
142 		} else {
143 			if (rp.re_nsub == 0) {
144 				$$ = "0";
145 			} else {
146 				$$ = "";
147 			}
148 		}
149 
150 		}
151 	| expr ADD_SUB_OPERATOR expr = {
152 		/* Returns the results of addition, subtraction */
153 		char *val;
154 		int64_t res;
155 
156 		res = perform_arith_op($1, $2, $3);
157 		(void) asprintf(&val, "%lld", (long long int) res);
158 		if (val == NULL)
159 			err(1, NULL);
160 		$$ = val;
161                 }
162 
163 	| expr MUL_DIV_MOD_OPERATOR expr = {
164 		/*
165 		 * Returns the results of multiply, divide or remainder of
166 		 * numeric-valued arguments.
167 		 */
168 		char *val;
169 		int64_t res;
170 
171 		res = perform_arith_op($1, $2, $3);
172 		(void) asprintf(&val, "%lld", (long long int) res);
173 		if (val == NULL)
174 			err(1, NULL);
175 		$$ = val;
176 
177 		}
178 	| expr COMPARE expr = {
179 		/*
180 		 * Returns the results of integer comparison if both arguments
181 		 * are integers; otherwise, returns the results of string
182 		 * comparison using the locale-specific collation sequence.
183 		 * The result of each comparison is 1 if the specified relation
184 		 * is true, or 0 if the relation is false.
185 		 */
186 
187 		int64_t l, r;
188 		int res;
189 
190 		res = 0;
191 
192 		/*
193 		 * Slight hack to avoid differences in the compare code
194 		 * between string and numeric compare.
195 		 */
196 		if (is_integer($1) && is_integer($3)) {
197 			/* numeric comparison */
198 			l = strtoll($1, NULL, 10);
199 			r = strtoll($3, NULL, 10);
200 		} else {
201 			/* string comparison */
202 			l = strcoll($1, $3);
203 			r = 0;
204 		}
205 
206 		switch($2[0]) {
207 		case '=': /* equal */
208 			res = (l == r);
209 			break;
210 		case '>': /* greater or greater-equal */
211 			if ($2[1] == '=')
212 				res = (l >= r);
213 			else
214 				res = (l > r);
215 			break;
216 		case '<': /* lower or lower-equal */
217 			if ($2[1] == '=')
218 				res = (l <= r);
219 			else
220 				res = (l < r);
221 			break;
222 		case '!': /* not equal */
223 			/* the check if this is != was done in yylex() */
224 			res = (l != r);
225 		}
226 
227 		$$ = (res) ? "1" : "0";
228 
229 		}
230 	| LEFT_PARENT expr RIGHT_PARENT { $$ = $2; }
231 	| LENGTH expr {
232 		/*
233 		 * Return length of 'expr' in bytes.
234 		 */
235 		char *ln;
236 
237 		asprintf(&ln, "%ld", (long) strlen($2));
238 		if (ln == NULL)
239 			err(1, NULL);
240 		$$ = ln;
241 		}
242 	;
243 
244 item:	STRING
245 	| ADD_SUB_OPERATOR
246 	| MUL_DIV_MOD_OPERATOR
247 	| COMPARE
248 	| SPEC_OR
249 	| SPEC_AND
250 	| SPEC_REG
251 	| LENGTH
252 	;
253 %%
254 
255 /*
256  * Returns 1 if the string is empty or contains only numeric zero.
257  */
258 static int
259 is_zero_or_null(const char *str)
260 {
261 	char *endptr;
262 
263 	return str[0] == '\0'
264 		|| ( strtoll(str, &endptr, 10) == 0LL
265 			&& endptr[0] == '\0');
266 }
267 
268 /*
269  * Returns 1 if the string is an integer.
270  */
271 static int
272 is_integer(const char *str)
273 {
274 	char *endptr;
275 
276 	(void) strtoll(str, &endptr, 10);
277 	/* note we treat empty string as valid number */
278 	return (endptr[0] == '\0');
279 }
280 
281 static int64_t
282 perform_arith_op(const char *left, const char *op, const char *right)
283 {
284 	int64_t res, sign, l, r;
285 	u_int64_t temp;
286 
287 	res = 0;
288 
289 	if (!is_integer(left)) {
290 		yyerror("non-integer argument '%s'", left);
291 		/* NOTREACHED */
292 	}
293 	if (!is_integer(right)) {
294 		yyerror("non-integer argument '%s'", right);
295 		/* NOTREACHED */
296 	}
297 
298 	errno = 0;
299 	l = strtoll(left, NULL, 10);
300 	if (errno == ERANGE) {
301 		yyerror("value '%s' is %s is %lld", left,
302 		    (l > 0) ? "too big, maximum" : "too small, minimum",
303 		    (l > 0) ? LLONG_MAX : LLONG_MIN);
304 		/* NOTREACHED */
305 	}
306 
307 	errno = 0;
308 	r = strtoll(right, NULL, 10);
309 	if (errno == ERANGE) {
310 		yyerror("value '%s' is %s is %lld", right,
311 		    (l > 0) ? "too big, maximum" : "too small, minimum",
312 	  	    (l > 0) ? LLONG_MAX : LLONG_MIN);
313 		/* NOTREACHED */
314 	}
315 
316 	switch(op[0]) {
317 	case '+':
318 		/*
319 		 * Do the op into an unsigned to avoid overflow and then cast
320 		 * back to check the resulting signage.
321 		 */
322 		temp = l + r;
323 		res = (int64_t) temp;
324 		/* very simplistic check for over-& underflow */
325 		if ((res < 0 && l > 0 && r > 0)
326 	  	    || (res > 0 && l < 0 && r < 0))
327 			yyerror("integer overflow or underflow occurred for "
328                             "operation '%s %s %s'", left, op, right);
329 		break;
330 	case '-':
331 		/*
332 		 * Do the op into an unsigned to avoid overflow and then cast
333 		 * back to check the resulting signage.
334 		 */
335 		temp = l - r;
336 		res = (int64_t) temp;
337 		/* very simplistic check for over-& underflow */
338 		if ((res < 0 && l > 0 && l > r)
339 		    || (res > 0 && l < 0 && l < r) )
340 			yyerror("integer overflow or underflow occurred for "
341 			    "operation '%s %s %s'", left, op, right);
342 		break;
343 	case '/':
344 		if (r == 0)
345 			yyerror("second argument to '%s' must not be zero", op);
346 		res = l / r;
347 
348 		break;
349 	case '%':
350 		if (r == 0)
351 			yyerror("second argument to '%s' must not be zero", op);
352 		res = l % r;
353 		break;
354 	case '*':
355 		/* shortcut */
356 		if ((l == 0) || (r == 0)) {
357 			res = 0;
358 			break;
359 		}
360 
361 		sign = 1;
362 		if (l < 0)
363 			sign *= -1;
364 		if (r < 0)
365 			sign *= -1;
366 
367 		res = l * r;
368 		/*
369 		 * XXX: not the most portable but works on anything with 2's
370 		 * complement arithmetic. If the signs don't match or the
371 		 * result was 0 on 2's complement this overflowed.
372 		 */
373 		if ((res < 0 && sign > 0) || (res > 0 && sign < 0) ||
374 		    (res == 0))
375 			yyerror("integer overflow or underflow occurred for "
376 			    "operation '%s %s %s'", left, op, right);
377 			/* NOTREACHED */
378 		break;
379 	}
380 	return res;
381 }
382 
383 static const char *x = "|&=<>+-*/%:()";
384 static const int x_token[] = {
385 	SPEC_OR, SPEC_AND, COMPARE, COMPARE, COMPARE, ADD_SUB_OPERATOR,
386 	ADD_SUB_OPERATOR, MUL_DIV_MOD_OPERATOR, MUL_DIV_MOD_OPERATOR,
387 	MUL_DIV_MOD_OPERATOR, SPEC_REG, LEFT_PARENT, RIGHT_PARENT
388 };
389 
390 static int handle_ddash = 1;
391 
392 int
393 yylex(void)
394 {
395 	const char *p = *av++;
396 	int retval;
397 
398 	if (!p)
399 		retval = 0;
400 	else if (p[1] == '\0') {
401 		const char *w = strchr(x, p[0]);
402 		if (w) {
403 			retval = x_token[w-x];
404 		} else {
405 			retval = STRING;
406 		}
407 	} else if (p[1] == '=' && p[2] == '\0'
408 			&& (p[0] == '>' || p[0] == '<' || p[0] == '!'))
409 		retval = COMPARE;
410 	else if (handle_ddash && p[0] == '-' && p[1] == '-' && p[2] == '\0') {
411 		/* ignore "--" if passed as first argument and isn't followed
412 		 * by another STRING */
413 		retval = yylex();
414 		if (retval != STRING && retval != LEFT_PARENT
415 		    && retval != RIGHT_PARENT) {
416 			/* is not followed by string or parenthesis, use as
417 			 * STRING */
418 			retval = STRING;
419 			av--;	/* was increased in call to yylex() above */
420 			p = "--";
421 		} else {
422 			/* "--" is to be ignored */
423 			p = yylval;
424 		}
425 	} else if (strcmp(p, "length") == 0)
426 		retval = LENGTH;
427 	else
428 		retval = STRING;
429 
430 	handle_ddash = 0;
431 	yylval = p;
432 
433 	return retval;
434 }
435 
436 /*
437  * Print error message and exit with error 2 (syntax error).
438  */
439 static void
440 yyerror(const char *fmt, ...)
441 {
442 	va_list arg;
443 
444 	va_start(arg, fmt);
445 	verrx(2, fmt, arg);
446 	va_end(arg);
447 }
448 
449 int
450 main(int argc, const char * const *argv)
451 {
452 	setprogname(argv[0]);
453 	(void)setlocale(LC_ALL, "");
454 
455 	if (argc == 1) {
456 		(void)fprintf(stderr, "usage: %s expression\n",
457 		    getprogname());
458 		exit(2);
459 	}
460 
461 	av = argv + 1;
462 
463 	exit(yyparse());
464 	/* NOTREACHED */
465 }
466