3 * Written by Pace Willisson (pace@blitz.com)
4 * and placed in the public domain.
6 * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
24 * POSIX specifies a specific error code for syntax errors. We exit
25 * with this code for all errors.
30 integer, numeric_string, string
45 void assert_to_integer(struct val *);
46 void assert_div(intmax_t, intmax_t);
47 void assert_minus(intmax_t, intmax_t, intmax_t);
48 void assert_plus(intmax_t, intmax_t, intmax_t);
49 void assert_times(intmax_t, intmax_t, intmax_t);
50 int compare_vals(struct val *, struct val *);
51 void free_value(struct val *);
52 int is_integer(const char *);
53 int is_string(struct val *);
54 int is_zero_or_null(struct val *);
55 struct val *make_integer(intmax_t);
56 struct val *make_str(const char *);
57 struct val *op_and(struct val *, struct val *);
58 struct val *op_colon(struct val *, struct val *);
59 struct val *op_div(struct val *, struct val *);
60 struct val *op_eq(struct val *, struct val *);
61 struct val *op_ge(struct val *, struct val *);
62 struct val *op_gt(struct val *, struct val *);
63 struct val *op_le(struct val *, struct val *);
64 struct val *op_lt(struct val *, struct val *);
65 struct val *op_minus(struct val *, struct val *);
66 struct val *op_ne(struct val *, struct val *);
67 struct val *op_or(struct val *, struct val *);
68 struct val *op_plus(struct val *, struct val *);
69 struct val *op_rem(struct val *, struct val *);
70 struct val *op_times(struct val *, struct val *);
71 int to_integer(struct val *);
72 void to_string(struct val *);
73 int yyerror(const char *);
85 %left <val> '=' '>' '<' GE LE NE
87 %left <val> '*' '/' '%'
91 %type <val> start expr
95 start: expr { result = $$; }
98 | '(' expr ')' { $$ = $2; }
99 | expr '|' expr { $$ = op_or($1, $3); }
100 | expr '&' expr { $$ = op_and($1, $3); }
101 | expr '=' expr { $$ = op_eq($1, $3); }
102 | expr '>' expr { $$ = op_gt($1, $3); }
103 | expr '<' expr { $$ = op_lt($1, $3); }
104 | expr GE expr { $$ = op_ge($1, $3); }
105 | expr LE expr { $$ = op_le($1, $3); }
106 | expr NE expr { $$ = op_ne($1, $3); }
107 | expr '+' expr { $$ = op_plus($1, $3); }
108 | expr '-' expr { $$ = op_minus($1, $3); }
109 | expr '*' expr { $$ = op_times($1, $3); }
110 | expr '/' expr { $$ = op_div($1, $3); }
111 | expr '%' expr { $$ = op_rem($1, $3); }
112 | expr ':' expr { $$ = op_colon($1, $3); }
118 make_integer(intmax_t i)
122 vp = (struct val *)malloc(sizeof(*vp));
124 errx(ERR_EXIT, "malloc() failed");
132 make_str(const char *s)
136 vp = (struct val *)malloc(sizeof(*vp));
137 if (vp == NULL || ((vp->u.s = strdup(s)) == NULL))
138 errx(ERR_EXIT, "malloc() failed");
141 vp->type = numeric_string;
149 free_value(struct val *vp)
151 if (vp->type == string || vp->type == numeric_string)
156 to_integer(struct val *vp)
160 /* we can only convert numeric_string to integer, here */
161 if (vp->type == numeric_string) {
163 i = strtoimax(vp->u.s, (char **)NULL, 10);
164 /* just keep as numeric_string, if the conversion fails */
165 if (errno != ERANGE) {
171 return (vp->type == integer);
175 assert_to_integer(struct val *vp)
177 if (vp->type == string)
178 errx(ERR_EXIT, "not a decimal number: '%s'", vp->u.s);
180 errx(ERR_EXIT, "operand too large: '%s'", vp->u.s);
184 to_string(struct val *vp)
188 if (vp->type == string || vp->type == numeric_string)
192 * log_10(x) ~= 0.3 * log_2(x). Rounding up gives the number
193 * of digits; add one each for the sign and terminating null
194 * character, respectively.
196 #define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
197 tmp = malloc(NDIGITS(vp->u.i));
199 errx(ERR_EXIT, "malloc() failed");
201 sprintf(tmp, "%jd", vp->u.i);
207 is_integer(const char *s)
212 while (isspace((unsigned char)*s))
215 if (*s == '-' || (nonposix && *s == '+'))
219 while (isdigit((unsigned char)*s))
225 is_string(struct val *vp)
227 /* only TRUE if this string is not a valid integer */
228 return (vp->type == string);
241 if (strlen(p) == 1) {
242 if (strchr("|&=<>+-*/%:()", *p))
244 } else if (strlen(p) == 2 && p[1] == '=') {
246 case '>': return (GE);
247 case '<': return (LE);
248 case '!': return (NE);
252 yylval.val = make_str(p);
257 is_zero_or_null(struct val *vp)
259 if (vp->type == integer)
260 return (vp->u.i == 0);
262 return (*vp->u.s == 0 || (to_integer(vp) && vp->u.i == 0));
266 main(int argc, char *argv[])
270 setlocale(LC_ALL, "");
271 if (getenv("EXPR_COMPAT") != NULL
272 || check_utility_compat("expr")) {
276 while ((c = getopt(argc, argv, "e")) != -1) {
283 "usage: expr [-e] expression\n");
291 if (result->type == integer)
292 printf("%jd\n", result->u.i);
294 printf("%s\n", result->u.s);
296 return (is_zero_or_null(result));
300 yyerror(const char *s __unused)
302 errx(ERR_EXIT, "syntax error");
306 op_or(struct val *a, struct val *b)
308 if (!is_zero_or_null(a)) {
313 if (!is_zero_or_null(b))
316 return (make_integer((intmax_t)0));
320 op_and(struct val *a, struct val *b)
322 if (is_zero_or_null(a) || is_zero_or_null(b)) {
325 return (make_integer((intmax_t)0));
333 compare_vals(struct val *a, struct val *b)
337 if (is_string(a) || is_string(b)) {
340 r = strcoll(a->u.s, b->u.s);
342 assert_to_integer(a);
343 assert_to_integer(b);
346 else if (a->u.i < b->u.i)
358 op_eq(struct val *a, struct val *b)
360 return (make_integer((intmax_t)(compare_vals(a, b) == 0)));
364 op_gt(struct val *a, struct val *b)
366 return (make_integer((intmax_t)(compare_vals(a, b) > 0)));
370 op_lt(struct val *a, struct val *b)
372 return (make_integer((intmax_t)(compare_vals(a, b) < 0)));
376 op_ge(struct val *a, struct val *b)
378 return (make_integer((intmax_t)(compare_vals(a, b) >= 0)));
382 op_le(struct val *a, struct val *b)
384 return (make_integer((intmax_t)(compare_vals(a, b) <= 0)));
388 op_ne(struct val *a, struct val *b)
390 return (make_integer((intmax_t)(compare_vals(a, b) != 0)));
394 assert_plus(intmax_t a, intmax_t b, intmax_t r)
397 * sum of two positive numbers must be positive,
398 * sum of two negative numbers must be negative
400 if ((a > 0 && b > 0 && r <= 0) ||
401 (a < 0 && b < 0 && r >= 0))
402 errx(ERR_EXIT, "overflow");
406 op_plus(struct val *a, struct val *b)
410 assert_to_integer(a);
411 assert_to_integer(b);
412 r = make_integer(a->u.i + b->u.i);
413 assert_plus(a->u.i, b->u.i, r->u.i);
421 assert_minus(intmax_t a, intmax_t b, intmax_t r)
423 if ((a >= 0 && b < 0 && r <= 0) ||
424 (a < 0 && b > 0 && r >= 0))
425 errx(ERR_EXIT, "overflow");
429 op_minus(struct val *a, struct val *b)
433 assert_to_integer(a);
434 assert_to_integer(b);
435 r = make_integer(a->u.i - b->u.i);
436 assert_minus(a->u.i, b->u.i, r->u.i);
444 * We depend on undefined behaviour giving a result (in r).
445 * To test this result, pass it as volatile. This prevents
446 * optimizing away of the test based on the undefined behaviour.
449 assert_times(intmax_t a, intmax_t b, volatile intmax_t r)
452 * If the first operand is 0, no overflow is possible,
453 * else the result of the division test must match the
456 * Be careful to avoid overflow in the overflow test, as
457 * in assert_div(). Overflow in division would kill us
458 * with a SIGFPE before getting the test wrong. In old
459 * buggy versions, optimization used to give a null test
460 * instead of a SIGFPE.
462 if ((a == -1 && b == INTMAX_MIN) || (a != 0 && r / a != b))
463 errx(ERR_EXIT, "overflow");
467 op_times(struct val *a, struct val *b)
471 assert_to_integer(a);
472 assert_to_integer(b);
473 r = make_integer(a->u.i * b->u.i);
474 assert_times(a->u.i, b->u.i, r->u.i);
482 assert_div(intmax_t a, intmax_t b)
485 errx(ERR_EXIT, "division by zero");
486 /* only INTMAX_MIN / -1 causes overflow */
487 if (a == INTMAX_MIN && b == -1)
488 errx(ERR_EXIT, "overflow");
492 op_div(struct val *a, struct val *b)
496 assert_to_integer(a);
497 assert_to_integer(b);
498 /* assert based on operands only, not on result */
499 assert_div(a->u.i, b->u.i);
500 r = make_integer(a->u.i / b->u.i);
508 op_rem(struct val *a, struct val *b)
512 assert_to_integer(a);
513 assert_to_integer(b);
514 /* pass a=1 to only check for div by zero */
515 assert_div(1, b->u.i);
516 r = make_integer(a->u.i % b->u.i);
524 op_colon(struct val *a, struct val *b)
532 /* coerce both arguments to strings */
536 /* compile regular expression */
537 if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
538 regerror(eval, &rp, errbuf, sizeof(errbuf));
539 errx(ERR_EXIT, "%s", errbuf);
542 /* compare string against pattern */
543 /* remember that patterns are anchored to the beginning of the line */
544 if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0)
545 if (rm[1].rm_so >= 0) {
546 *(a->u.s + rm[1].rm_eo) = '\0';
547 v = make_str(a->u.s + rm[1].rm_so);
550 v = make_integer((intmax_t)(rm[0].rm_eo));
553 v = make_integer((intmax_t)0);
557 /* free arguments and pattern buffer */