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)
11 #include <sys/types.h>
26 * POSIX specifies a specific error code for syntax errors. We exit
27 * with this code for all errors.
32 integer, numeric_string, string
47 void assert_to_integer(struct val *);
48 void assert_div(intmax_t, intmax_t);
49 void assert_minus(intmax_t, intmax_t, intmax_t);
50 void assert_plus(intmax_t, intmax_t, intmax_t);
51 void assert_times(intmax_t, intmax_t, intmax_t);
52 int compare_vals(struct val *, struct val *);
53 void free_value(struct val *);
54 int is_integer(const char *);
55 int is_string(struct val *);
56 int is_zero_or_null(struct val *);
57 struct val *make_integer(intmax_t);
58 struct val *make_str(const char *);
59 struct val *op_and(struct val *, struct val *);
60 struct val *op_colon(struct val *, struct val *);
61 struct val *op_div(struct val *, struct val *);
62 struct val *op_eq(struct val *, struct val *);
63 struct val *op_ge(struct val *, struct val *);
64 struct val *op_gt(struct val *, struct val *);
65 struct val *op_le(struct val *, struct val *);
66 struct val *op_lt(struct val *, struct val *);
67 struct val *op_minus(struct val *, struct val *);
68 struct val *op_ne(struct val *, struct val *);
69 struct val *op_or(struct val *, struct val *);
70 struct val *op_plus(struct val *, struct val *);
71 struct val *op_rem(struct val *, struct val *);
72 struct val *op_times(struct val *, struct val *);
73 int to_integer(struct val *);
74 void to_string(struct val *);
75 int yyerror(const char *);
88 %left <val> '=' '>' '<' GE LE NE
90 %left <val> '*' '/' '%'
94 %type <val> start expr
98 start: expr { result = $$; }
101 | '(' expr ')' { $$ = $2; }
102 | expr '|' expr { $$ = op_or($1, $3); }
103 | expr '&' expr { $$ = op_and($1, $3); }
104 | expr '=' expr { $$ = op_eq($1, $3); }
105 | expr '>' expr { $$ = op_gt($1, $3); }
106 | expr '<' expr { $$ = op_lt($1, $3); }
107 | expr GE expr { $$ = op_ge($1, $3); }
108 | expr LE expr { $$ = op_le($1, $3); }
109 | expr NE expr { $$ = op_ne($1, $3); }
110 | expr '+' expr { $$ = op_plus($1, $3); }
111 | expr '-' expr { $$ = op_minus($1, $3); }
112 | expr '*' expr { $$ = op_times($1, $3); }
113 | expr '/' expr { $$ = op_div($1, $3); }
114 | expr '%' expr { $$ = op_rem($1, $3); }
115 | expr ':' expr { $$ = op_colon($1, $3); }
121 make_integer(intmax_t i)
125 vp = (struct val *)malloc(sizeof(*vp));
127 errx(ERR_EXIT, "malloc() failed");
135 make_str(const char *s)
139 vp = (struct val *)malloc(sizeof(*vp));
140 if (vp == NULL || ((vp->u.s = strdup(s)) == NULL))
141 errx(ERR_EXIT, "malloc() failed");
144 vp->type = numeric_string;
152 free_value(struct val *vp)
154 if (vp->type == string || vp->type == numeric_string)
159 to_integer(struct val *vp)
163 /* we can only convert numeric_string to integer, here */
164 if (vp->type == numeric_string) {
166 i = strtoimax(vp->u.s, (char **)NULL, 10);
167 /* just keep as numeric_string, if the conversion fails */
168 if (errno != ERANGE) {
174 return (vp->type == integer);
178 assert_to_integer(struct val *vp)
180 if (vp->type == string)
181 errx(ERR_EXIT, "not a decimal number: '%s'", vp->u.s);
183 errx(ERR_EXIT, "operand too large: '%s'", vp->u.s);
187 to_string(struct val *vp)
191 if (vp->type == string || vp->type == numeric_string)
195 * log_10(x) ~= 0.3 * log_2(x). Rounding up gives the number
196 * of digits; add one each for the sign and terminating null
197 * character, respectively.
199 #define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
200 tmp = malloc(NDIGITS(vp->u.i));
202 errx(ERR_EXIT, "malloc() failed");
204 sprintf(tmp, "%jd", vp->u.i);
210 is_integer(const char *s)
215 while (isspace((unsigned char)*s))
218 if (*s == '-' || (nonposix && *s == '+'))
222 while (isdigit((unsigned char)*s))
228 is_string(struct val *vp)
230 /* only TRUE if this string is not a valid integer */
231 return (vp->type == string);
244 if (strlen(p) == 1) {
245 if (strchr("|&=<>+-*/%:()", *p))
247 } else if (strlen(p) == 2 && p[1] == '=') {
249 case '>': return (GE);
250 case '<': return (LE);
251 case '!': return (NE);
255 yylval.val = make_str(p);
260 is_zero_or_null(struct val *vp)
262 if (vp->type == integer)
263 return (vp->u.i == 0);
265 return (*vp->u.s == 0 || (to_integer(vp) && vp->u.i == 0));
269 main(int argc, char *argv[])
273 setlocale(LC_ALL, "");
274 if (getenv("EXPR_COMPAT") != NULL
275 || check_utility_compat("expr")) {
279 while ((c = getopt(argc, argv, "e")) != -1) {
286 "usage: expr [-e] expression\n");
294 if (result->type == integer)
295 printf("%jd\n", result->u.i);
297 printf("%s\n", result->u.s);
299 return (is_zero_or_null(result));
303 yyerror(const char *s __unused)
305 errx(ERR_EXIT, "syntax error");
309 op_or(struct val *a, struct val *b)
311 if (!is_zero_or_null(a)) {
316 if (!is_zero_or_null(b))
319 return (make_integer((intmax_t)0));
323 op_and(struct val *a, struct val *b)
325 if (is_zero_or_null(a) || is_zero_or_null(b)) {
328 return (make_integer((intmax_t)0));
336 compare_vals(struct val *a, struct val *b)
340 if (is_string(a) || is_string(b)) {
343 r = strcoll(a->u.s, b->u.s);
345 assert_to_integer(a);
346 assert_to_integer(b);
349 else if (a->u.i < b->u.i)
361 op_eq(struct val *a, struct val *b)
363 return (make_integer((intmax_t)(compare_vals(a, b) == 0)));
367 op_gt(struct val *a, struct val *b)
369 return (make_integer((intmax_t)(compare_vals(a, b) > 0)));
373 op_lt(struct val *a, struct val *b)
375 return (make_integer((intmax_t)(compare_vals(a, b) < 0)));
379 op_ge(struct val *a, struct val *b)
381 return (make_integer((intmax_t)(compare_vals(a, b) >= 0)));
385 op_le(struct val *a, struct val *b)
387 return (make_integer((intmax_t)(compare_vals(a, b) <= 0)));
391 op_ne(struct val *a, struct val *b)
393 return (make_integer((intmax_t)(compare_vals(a, b) != 0)));
397 assert_plus(intmax_t a, intmax_t b, intmax_t r)
400 * sum of two positive numbers must be positive,
401 * sum of two negative numbers must be negative
403 if ((a > 0 && b > 0 && r <= 0) ||
404 (a < 0 && b < 0 && r >= 0))
405 errx(ERR_EXIT, "overflow");
409 op_plus(struct val *a, struct val *b)
413 assert_to_integer(a);
414 assert_to_integer(b);
415 r = make_integer(a->u.i + b->u.i);
416 assert_plus(a->u.i, b->u.i, r->u.i);
424 assert_minus(intmax_t a, intmax_t b, intmax_t r)
426 /* special case subtraction of INTMAX_MIN */
427 if (b == INTMAX_MIN && a < 0)
428 errx(ERR_EXIT, "overflow");
429 /* check addition of negative subtrahend */
430 assert_plus(a, -b, r);
434 op_minus(struct val *a, struct val *b)
438 assert_to_integer(a);
439 assert_to_integer(b);
440 r = make_integer(a->u.i - b->u.i);
441 assert_minus(a->u.i, b->u.i, r->u.i);
449 assert_times(intmax_t a, intmax_t b, intmax_t r)
452 * if first operand is 0, no overflow is possible,
453 * else result of division test must match second operand
455 if (a != 0 && r / a != b)
456 errx(ERR_EXIT, "overflow");
460 op_times(struct val *a, struct val *b)
464 assert_to_integer(a);
465 assert_to_integer(b);
466 r = make_integer(a->u.i * b->u.i);
467 assert_times(a->u.i, b->u.i, r->u.i);
475 assert_div(intmax_t a, intmax_t b)
478 errx(ERR_EXIT, "division by zero");
479 /* only INTMAX_MIN / -1 causes overflow */
480 if (a == INTMAX_MIN && b == -1)
481 errx(ERR_EXIT, "overflow");
485 op_div(struct val *a, struct val *b)
489 assert_to_integer(a);
490 assert_to_integer(b);
491 /* assert based on operands only, not on result */
492 assert_div(a->u.i, b->u.i);
493 r = make_integer(a->u.i / b->u.i);
501 op_rem(struct val *a, struct val *b)
505 assert_to_integer(a);
506 assert_to_integer(b);
507 /* pass a=1 to only check for div by zero */
508 assert_div(1, b->u.i);
509 r = make_integer(a->u.i % b->u.i);
517 op_colon(struct val *a, struct val *b)
525 /* coerce both arguments to strings */
529 /* compile regular expression */
530 if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
531 regerror(eval, &rp, errbuf, sizeof(errbuf));
532 errx(ERR_EXIT, "%s", errbuf);
535 /* compare string against pattern */
536 /* remember that patterns are anchored to the beginning of the line */
537 if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0)
538 if (rm[1].rm_so >= 0) {
539 *(a->u.s + rm[1].rm_eo) = '\0';
540 v = make_str(a->u.s + rm[1].rm_so);
543 v = make_integer((intmax_t)(rm[0].rm_eo - rm[0].rm_so));
546 v = make_integer((intmax_t)0);
550 /* free arguments and pattern buffer */