2 /* Written by Pace Willisson (pace@blitz.com)
3 * and placed in the public domain.
5 * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
7 * $FreeBSD: src/bin/expr/expr.y,v 1.14.2.3 2001/08/01 02:37:46 obrien Exp $
8 * $DragonFly: src/bin/expr/expr.y,v 1.6 2005/11/06 11:44:02 swildner Exp $
11 #include <sys/types.h>
23 integer
, numeric_string
, string
36 int chk_div
(quad_t
, quad_t
);
37 int chk_minus
(quad_t
, quad_t
, quad_t
);
38 int chk_plus
(quad_t
, quad_t
, quad_t
);
39 int chk_times
(quad_t
, quad_t
, quad_t
);
40 void free_value
(struct val
*);
41 int is_zero_or_null
(struct val
*);
42 int isstring
(struct val
*);
43 int main
(int, char **);
44 struct val
*make_integer
(quad_t
);
45 struct val
*make_str
(const char *);
46 struct val
*op_and
(struct val
*, struct val
*);
47 struct val
*op_colon
(struct val
*, struct val
*);
48 struct val
*op_div
(struct val
*, struct val
*);
49 struct val
*op_eq
(struct val
*, struct val
*);
50 struct val
*op_ge
(struct val
*, struct val
*);
51 struct val
*op_gt
(struct val
*, struct val
*);
52 struct val
*op_le
(struct val
*, struct val
*);
53 struct val
*op_lt
(struct val
*, struct val
*);
54 struct val
*op_minus
(struct val
*, struct val
*);
55 struct val
*op_ne
(struct val
*, struct val
*);
56 struct val
*op_or
(struct val
*, struct val
*);
57 struct val
*op_plus
(struct val
*, struct val
*);
58 struct val
*op_rem
(struct val
*, struct val
*);
59 struct val
*op_times
(struct val
*, struct val
*);
60 quad_t to_integer
(struct val
*);
61 void to_string
(struct val
*);
62 int yyerror (const char *);
76 %left
<val
> '=' '>' '<' GE LE NE
78 %left
<val
> '*' '/' '%'
82 %type
<val
> start expr
86 start: expr
{ result
= $$
; }
89 |
'(' expr
')' { $$
= $2; }
90 | expr
'|' expr
{ $$
= op_or
($1, $3); }
91 | expr
'&' expr
{ $$
= op_and
($1, $3); }
92 | expr
'=' expr
{ $$
= op_eq
($1, $3); }
93 | expr
'>' expr
{ $$
= op_gt
($1, $3); }
94 | expr
'<' expr
{ $$
= op_lt
($1, $3); }
95 | expr GE expr
{ $$
= op_ge
($1, $3); }
96 | expr LE expr
{ $$
= op_le
($1, $3); }
97 | expr NE expr
{ $$
= op_ne
($1, $3); }
98 | expr
'+' expr
{ $$
= op_plus
($1, $3); }
99 | expr
'-' expr
{ $$
= op_minus
($1, $3); }
100 | expr
'*' expr
{ $$
= op_times
($1, $3); }
101 | expr
'/' expr
{ $$
= op_div
($1, $3); }
102 | expr
'%' expr
{ $$
= op_rem
($1, $3); }
103 | expr
':' expr
{ $$
= op_colon
($1, $3); }
110 make_integer
(quad_t i
)
114 vp
= (struct val
*) malloc
(sizeof
(*vp
));
116 errx
(2, "malloc() failed");
125 make_str
(const char *s
)
131 vp
= (struct val
*) malloc
(sizeof
(*vp
));
132 if
(vp
== NULL ||
((vp
->u.s
= strdup
(s
)) == NULL
)) {
133 errx
(2, "malloc() failed");
136 for
(i
= 1, isint
= isdigit
(s
[0]) || s
[0] == '-';
137 isint
&& i
< strlen
(s
);
145 vp
->type
= numeric_string
;
154 free_value
(struct val
*vp
)
156 if
(vp
->type
== string || vp
->type
== numeric_string
)
162 to_integer
(struct val
*vp
)
166 if
(vp
->type
== integer
)
169 if
(vp
->type
== string)
172 /* vp->type == numeric_string, make it numeric */
174 i
= strtoll
(vp
->u.s
, (char**)NULL
, 10);
176 errx
(2, "overflow");
185 to_string
(struct val
*vp
)
189 if
(vp
->type
== string || vp
->type
== numeric_string
)
192 tmp
= malloc
((size_t)25);
194 errx
(2, "malloc() failed");
197 sprintf
(tmp
, "%lld", (long long)vp
->u.i
);
204 isstring
(struct val
*vp
)
206 /* only TRUE if this string is not a valid integer */
207 return
(vp
->type
== string);
221 if
(strlen
(p
) == 1) {
222 if
(strchr
("|&=<>+-*/%:()", *p
))
224 } else if
(strlen
(p
) == 2 && p
[1] == '=') {
226 case
'>': return
(GE
);
227 case
'<': return
(LE
);
228 case
'!': return
(NE
);
232 yylval.val
= make_str
(p
);
237 is_zero_or_null
(struct val
*vp
)
239 if
(vp
->type
== integer
) {
240 return
(vp
->u.i
== 0);
242 return
(*vp
->u.s
== 0 ||
(to_integer
(vp
) && vp
->u.i
== 0));
248 main
(int argc __unused
, char **argv
)
250 setlocale
(LC_ALL
, "");
256 if
(result
->type
== integer
)
257 printf
("%lld\n", (long long)result
->u.i
);
259 printf
("%s\n", result
->u.s
);
261 return
(is_zero_or_null
(result
));
265 yyerror(const char *s __unused
)
267 errx
(2, "syntax error");
272 op_or
(struct val
*a
, struct val
*b
)
274 if
(is_zero_or_null
(a
)) {
284 op_and
(struct val
*a
, struct val
*b
)
286 if
(is_zero_or_null
(a
) || is_zero_or_null
(b
)) {
289 return
(make_integer
((quad_t
)0));
297 op_eq
(struct val
*a
, struct val
*b
)
301 if
(isstring
(a
) || isstring
(b
)) {
304 r
= make_integer
((quad_t
)(strcoll
(a
->u.s
, b
->u.s
) == 0));
308 r
= make_integer
((quad_t
)(a
->u.i
== b
->u.i
));
317 op_gt
(struct val
*a
, struct val
*b
)
321 if
(isstring
(a
) || isstring
(b
)) {
324 r
= make_integer
((quad_t
)(strcoll
(a
->u.s
, b
->u.s
) > 0));
328 r
= make_integer
((quad_t
)(a
->u.i
> b
->u.i
));
337 op_lt
(struct val
*a
, struct val
*b
)
341 if
(isstring
(a
) || isstring
(b
)) {
344 r
= make_integer
((quad_t
)(strcoll
(a
->u.s
, b
->u.s
) < 0));
348 r
= make_integer
((quad_t
)(a
->u.i
< b
->u.i
));
357 op_ge
(struct val
*a
, struct val
*b
)
361 if
(isstring
(a
) || isstring
(b
)) {
364 r
= make_integer
((quad_t
)(strcoll
(a
->u.s
, b
->u.s
) >= 0));
368 r
= make_integer
((quad_t
)(a
->u.i
>= b
->u.i
));
377 op_le
(struct val
*a
, struct val
*b
)
381 if
(isstring
(a
) || isstring
(b
)) {
384 r
= make_integer
((quad_t
)(strcoll
(a
->u.s
, b
->u.s
) <= 0));
388 r
= make_integer
((quad_t
)(a
->u.i
<= b
->u.i
));
397 op_ne
(struct val
*a
, struct val
*b
)
401 if
(isstring
(a
) || isstring
(b
)) {
404 r
= make_integer
((quad_t
)(strcoll
(a
->u.s
, b
->u.s
) != 0));
408 r
= make_integer
((quad_t
)(a
->u.i
!= b
->u.i
));
417 chk_plus
(quad_t a
, quad_t b
, quad_t r
)
419 /* sum of two positive numbers must be positive */
420 if
(a
> 0 && b
> 0 && r
<= 0)
422 /* sum of two negative numbers must be negative */
423 if
(a
< 0 && b
< 0 && r
>= 0)
425 /* all other cases are OK */
430 op_plus
(struct val
*a
, struct val
*b
)
434 if
(!to_integer
(a
) ||
!to_integer
(b
)) {
435 errx
(2, "non-numeric argument");
438 r
= make_integer
(/*(quad_t)*/(a
->u.i
+ b
->u.i
));
439 if
(chk_plus
(a
->u.i
, b
->u.i
, r
->u.i
)) {
440 errx
(2, "overflow");
448 chk_minus
(quad_t a
, quad_t b
, quad_t r
)
450 /* special case subtraction of QUAD_MIN */
457 /* this is allowed for b != QUAD_MIN */
458 return chk_plus
(a
, -b
, r
);
462 op_minus
(struct val
*a
, struct val
*b
)
466 if
(!to_integer
(a
) ||
!to_integer
(b
)) {
467 errx
(2, "non-numeric argument");
470 r
= make_integer
(/*(quad_t)*/(a
->u.i
- b
->u.i
));
471 if
(chk_minus
(a
->u.i
, b
->u.i
, r
->u.i
)) {
472 errx
(2, "overflow");
480 chk_times
(quad_t a
, quad_t b
, quad_t r
)
482 /* special case: first operand is 0, no overflow possible */
485 /* cerify that result of division matches second operand */
492 op_times
(struct val
*a
, struct val
*b
)
496 if
(!to_integer
(a
) ||
!to_integer
(b
)) {
497 errx
(2, "non-numeric argument");
500 r
= make_integer
(/*(quad_t)*/(a
->u.i
* b
->u.i
));
501 if
(chk_times
(a
->u.i
, b
->u.i
, r
->u.i
)) {
502 errx
(2, "overflow");
510 chk_div
(quad_t a
, quad_t b
)
512 /* div by zero has been taken care of before */
513 /* only QUAD_MIN / -1 causes overflow */
514 if
(a
== QUAD_MIN
&& b
== -1)
516 /* everything else is OK */
521 op_div
(struct val
*a
, struct val
*b
)
525 if
(!to_integer
(a
) ||
!to_integer
(b
)) {
526 errx
(2, "non-numeric argument");
530 errx
(2, "division by zero");
533 r
= make_integer
(/*(quad_t)*/(a
->u.i
/ b
->u.i
));
534 if
(chk_div
(a
->u.i
, b
->u.i
)) {
535 errx
(2, "overflow");
543 op_rem
(struct val
*a
, struct val
*b
)
547 if
(!to_integer
(a
) ||
!to_integer
(b
)) {
548 errx
(2, "non-numeric argument");
552 errx
(2, "division by zero");
555 r
= make_integer
(/*(quad_t)*/(a
->u.i % b
->u.i
));
556 /* chk_rem necessary ??? */
563 op_colon
(struct val
*a
, struct val
*b
)
571 /* coerce to both arguments to strings */
575 /* compile regular expression */
576 if
((eval
= regcomp
(&rp
, b
->u.s
, 0)) != 0) {
577 regerror
(eval
, &rp
, errbuf
, sizeof
(errbuf
));
578 errx
(2, "%s", errbuf
);
581 /* compare string against pattern */
582 /* remember that patterns are anchored to the beginning of the line */
583 if
(regexec
(&rp
, a
->u.s
, (size_t)2, rm
, 0) == 0 && rm
[0].rm_so
== 0) {
584 if
(rm
[1].rm_so
>= 0) {
585 *(a
->u.s
+ rm
[1].rm_eo
) = '\0';
586 v
= make_str
(a
->u.s
+ rm
[1].rm_so
);
589 v
= make_integer
((quad_t
)(rm
[0].rm_eo
- rm
[0].rm_so
));
592 if
(rp.re_nsub
== 0) {
593 v
= make_integer
((quad_t
)0);
599 /* free arguments and pattern buffer */