Home | History | Annotate | Line # | Download | only in expr
expr.y revision 1.30
      1 /* $NetBSD: expr.y,v 1.30 2004/03/20 08:45:05 jdolecek 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  * 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.30 2004/03/20 08:45:05 jdolecek 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 LEFT_PARENT RIGHT_PARENT
     77 
     78 %%
     79 
     80 exp:	expr = {
     81 		(void) printf("%s\n", $1);
     82 		return (is_zero_or_null($1));
     83 		}
     84 	;
     85 
     86 expr:	item { $$ = $1; }
     87 	| expr SPEC_OR expr = {
     88 		/*
     89 		 * Return evaluation of first expression if it is neither
     90 		 * an empty string nor zero; otherwise, returns the evaluation
     91 		 * of second expression.
     92 		 */
     93 		if (!is_zero_or_null($1))
     94 			$$ = $1;
     95 		else
     96 			$$ = $3;
     97 		}
     98 	| expr SPEC_AND expr = {
     99 		/*
    100 		 * Returns the evaluation of first expr if neither expression
    101 		 * evaluates to an empty string or zero; otherwise, returns
    102 		 * zero.
    103 		 */
    104 		if (!is_zero_or_null($1) && !is_zero_or_null($3))
    105 			$$ = $1;
    106 		else
    107 			$$ = "0";
    108 		}
    109 	| expr SPEC_REG expr = {
    110 		/*
    111 		 * The ``:'' operator matches first expr against the second,
    112 		 * which must be a regular expression.
    113 		 */
    114 		regex_t rp;
    115 		regmatch_t rm[2];
    116 		int eval;
    117 
    118 		/* compile regular expression */
    119 		if ((eval = regcomp(&rp, $3, REG_BASIC)) != 0) {
    120 			char errbuf[256];
    121 			(void)regerror(eval, &rp, errbuf, sizeof(errbuf));
    122 			yyerror("%s", errbuf);
    123 			/* NOT REACHED */
    124 		}
    125 
    126 		/* compare string against pattern --  remember that patterns
    127 		   are anchored to the beginning of the line */
    128 		if (regexec(&rp, $1, 2, rm, 0) == 0 && rm[0].rm_so == 0) {
    129 			char *val;
    130 			if (rm[1].rm_so >= 0) {
    131 				(void) asprintf(&val, "%.*s",
    132 					(int) (rm[1].rm_eo - rm[1].rm_so),
    133 					$1 + rm[1].rm_so);
    134 			} else {
    135 				(void) asprintf(&val, "%d",
    136 					(int)(rm[0].rm_eo - rm[0].rm_so));
    137 			}
    138 			$$ = val;
    139 		} else {
    140 			if (rp.re_nsub == 0) {
    141 				$$ = "0";
    142 			} else {
    143 				$$ = "";
    144 			}
    145 		}
    146 
    147 		}
    148 	| expr ADD_SUB_OPERATOR expr = {
    149 		/* Returns the results of addition, subtraction */
    150 		char *val;
    151 		int64_t res;
    152 
    153 		res = perform_arith_op($1, $2, $3);
    154 		(void) asprintf(&val, "%lld", (long long int) res);
    155 		$$ = val;
    156                 }
    157 
    158 	| expr MUL_DIV_MOD_OPERATOR expr = {
    159 		/*
    160 		 * Returns the results of multiply, divide or remainder of
    161 		 * numeric-valued arguments.
    162 		 */
    163 		char *val;
    164 		int64_t res;
    165 
    166 		res = perform_arith_op($1, $2, $3);
    167 		(void) asprintf(&val, "%lld", (long long int) res);
    168 		$$ = val;
    169 
    170 		}
    171 	| expr COMPARE expr = {
    172 		/*
    173 		 * Returns the results of integer comparison if both arguments
    174 		 * are integers; otherwise, returns the results of string
    175 		 * comparison using the locale-specific collation sequence.
    176 		 * The result of each comparison is 1 if the specified relation
    177 		 * is true, or 0 if the relation is false.
    178 		 */
    179 
    180 		int64_t l, r;
    181 		int res;
    182 
    183 		/*
    184 		 * Slight hack to avoid differences in the compare code
    185 		 * between string and numeric compare.
    186 		 */
    187 		if (is_integer($1) && is_integer($3)) {
    188 			/* numeric comparison */
    189 			l = strtoll($1, NULL, 10);
    190 			r = strtoll($3, NULL, 10);
    191 		} else {
    192 			/* string comparison */
    193 			l = strcoll($1, $3);
    194 			r = 0;
    195 		}
    196 
    197 		switch($2[0]) {
    198 		case '=': /* equal */
    199 			res = (l == r);
    200 			break;
    201 		case '>': /* greater or greater-equal */
    202 			if ($2[1] == '=')
    203 				res = (l >= r);
    204 			else
    205 				res = (l > r);
    206 			break;
    207 		case '<': /* lower or lower-equal */
    208 			if ($2[1] == '=')
    209 				res = (l <= r);
    210 			else
    211 				res = (l < r);
    212 			break;
    213 		case '!': /* not equal */
    214 			/* the check if this is != was done in yylex() */
    215 			res = (l != r);
    216 		}
    217 
    218 		$$ = (res) ? "1" : "0";
    219 
    220 		}
    221 	| LEFT_PARENT expr RIGHT_PARENT { $$ = $2; }
    222 	;
    223 
    224 item:	STRING
    225 	| ADD_SUB_OPERATOR
    226 	| MUL_DIV_MOD_OPERATOR
    227 	| COMPARE
    228 	| SPEC_OR
    229 	| SPEC_AND
    230 	| SPEC_REG
    231 	;
    232 %%
    233 
    234 /*
    235  * Returns 1 if the string is empty or contains only numeric zero.
    236  */
    237 static int
    238 is_zero_or_null(const char *str)
    239 {
    240 	char *endptr;
    241 
    242 	return str[0] == '\0'
    243 		|| ( strtoll(str, &endptr, 10) == 0LL
    244 			&& endptr[0] == '\0');
    245 }
    246 
    247 /*
    248  * Returns 1 if the string is an integer.
    249  */
    250 static int
    251 is_integer(const char *str)
    252 {
    253 	char *endptr;
    254 
    255 	(void) strtoll(str, &endptr, 10);
    256 	/* note we treat empty string as valid number */
    257 	return (endptr[0] == '\0');
    258 }
    259 
    260 static int64_t
    261 perform_arith_op(const char *left, const char *op, const char *right)
    262 {
    263 	int64_t res, sign, l, r;
    264 	u_int64_t temp;
    265 
    266 	if (!is_integer(left)) {
    267 		yyerror("non-integer argument '%s'", left);
    268 		/* NOTREACHED */
    269 	}
    270 	if (!is_integer(right)) {
    271 		yyerror("non-integer argument '%s'", right);
    272 		/* NOTREACHED */
    273 	}
    274 
    275 	errno = 0;
    276 	l = strtoll(left, NULL, 10);
    277 	if (errno == ERANGE) {
    278 		yyerror("value '%s' is %s is %lld", left,
    279 		    (l > 0) ? "too big, maximum" : "too small, minimum",
    280 		    (l > 0) ? LLONG_MAX : LLONG_MIN);
    281 		/* NOTREACHED */
    282 	}
    283 
    284 	errno = 0;
    285 	r = strtoll(right, NULL, 10);
    286 	if (errno == ERANGE) {
    287 		yyerror("value '%s' is %s is %lld", right,
    288 		    (l > 0) ? "too big, maximum" : "too small, minimum",
    289 	  	    (l > 0) ? LLONG_MAX : LLONG_MIN);
    290 		/* NOTREACHED */
    291 	}
    292 
    293 	switch(op[0]) {
    294 	case '+':
    295 		/*
    296 		 * Do the op into an unsigned to avoid overflow and then cast
    297 		 * back to check the resulting signage.
    298 		 */
    299 		temp = l + r;
    300 		res = (int64_t) temp;
    301 		/* very simplistic check for over-& underflow */
    302 		if ((res < 0 && l > 0 && r > 0)
    303 	  	    || (res > 0 && l < 0 && r < 0))
    304 			yyerror("integer overflow or underflow occurred for "
    305                             "operation '%s %s %s'", left, op, right);
    306 		break;
    307 	case '-':
    308 		/*
    309 		 * Do the op into an unsigned to avoid overflow and then cast
    310 		 * back to check the resulting signage.
    311 		 */
    312 		temp = l - r;
    313 		res = (int64_t) temp;
    314 		/* very simplistic check for over-& underflow */
    315 		if ((res < 0 && l > 0 && l > r)
    316 		    || (res > 0 && l < 0 && l < r) )
    317 			yyerror("integer overflow or underflow occurred for "
    318 			    "operation '%s %s %s'", left, op, right);
    319 		break;
    320 	case '/':
    321 		if (r == 0)
    322 			yyerror("second argument to '%s' must not be zero", op);
    323 		res = l / r;
    324 
    325 		break;
    326 	case '%':
    327 		if (r == 0)
    328 			yyerror("second argument to '%s' must not be zero", op);
    329 		res = l % r;
    330 		break;
    331 	case '*':
    332 		/* shortcut */
    333 		if ((l == 0) || (r == 0)) {
    334 			res = 0;
    335 			break;
    336 		}
    337 
    338 		sign = 1;
    339 		if (l < 0)
    340 			sign *= -1;
    341 		if (r < 0)
    342 			sign *= -1;
    343 
    344 		res = l * r;
    345 		/*
    346 		 * XXX: not the most portable but works on anything with 2's
    347 		 * complement arithmetic. If the signs don't match or the
    348 		 * result was 0 on 2's complement this overflowed.
    349 		 */
    350 		if ((res < 0 && sign > 0) || (res > 0 && sign < 0) ||
    351 		    (res == 0))
    352 			yyerror("integer overflow or underflow occurred for "
    353 			    "operation '%s %s %s'", left, op, right);
    354 			/* NOTREACHED */
    355 		break;
    356 	}
    357 	return res;
    358 }
    359 
    360 static const char *x = "|&=<>+-*/%:()";
    361 static const int x_token[] = {
    362 	SPEC_OR, SPEC_AND, COMPARE, COMPARE, COMPARE, ADD_SUB_OPERATOR,
    363 	ADD_SUB_OPERATOR, MUL_DIV_MOD_OPERATOR, MUL_DIV_MOD_OPERATOR,
    364 	MUL_DIV_MOD_OPERATOR, SPEC_REG, LEFT_PARENT, RIGHT_PARENT
    365 };
    366 
    367 static int handle_ddash = 1;
    368 
    369 int
    370 yylex(void)
    371 {
    372 	const char *p = *av++;
    373 	int retval;
    374 
    375 	if (!p)
    376 		retval = 0;
    377 	else if (p[1] == '\0') {
    378 		const char *w = strchr(x, p[0]);
    379 		if (w) {
    380 			retval = x_token[w-x];
    381 		} else {
    382 			retval = STRING;
    383 		}
    384 	} else if (p[1] == '=' && p[2] == '\0'
    385 			&& (p[0] == '>' || p[0] == '<' || p[0] == '!'))
    386 		retval = COMPARE;
    387 	else if (handle_ddash && p[0] == '-' && p[1] == '-' && p[2] == '\0') {
    388 		/* ignore "--" if passed as first argument and isn't followed
    389 		 * by another STRING */
    390 		retval = yylex();
    391 		if (retval != STRING && retval != LEFT_PARENT
    392 		    && retval != RIGHT_PARENT) {
    393 			/* is not followed by string or parenthesis, use as
    394 			 * STRING */
    395 			retval = STRING;
    396 			av--;	/* was increased in call to yylex() above */
    397 			p = "--";
    398 		} else {
    399 			/* "--" is to be ignored */
    400 			p = yylval;
    401 		}
    402 	} else
    403 		retval = STRING;
    404 
    405 	handle_ddash = 0;
    406 	yylval = p;
    407 
    408 	return retval;
    409 }
    410 
    411 /*
    412  * Print error message and exit with error 2 (syntax error).
    413  */
    414 static void
    415 yyerror(const char *fmt, ...)
    416 {
    417 	va_list arg;
    418 
    419 	va_start(arg, fmt);
    420 	verrx(2, fmt, arg);
    421 	va_end(arg);
    422 }
    423 
    424 int
    425 main(int argc, const char * const *argv)
    426 {
    427 	setprogname(argv[0]);
    428 	(void)setlocale(LC_ALL, "");
    429 
    430 	if (argc == 1) {
    431 		(void)fprintf(stderr, "usage: %s expression\n",
    432 		    getprogname());
    433 		exit(2);
    434 	}
    435 
    436 	av = argv + 1;
    437 
    438 	exit(yyparse());
    439 	/* NOTREACHED */
    440 }
    441