xref: /freebsd/bin/expr/expr.y (revision c9885518de2f8c1da3530130cbd3a5ed88f09a28)
15b81b6b3SRodney W. Grimes %{
25b81b6b3SRodney W. Grimes /* Written by Pace Willisson (pace@blitz.com)
355c497bfSJ.T. Conklin  * and placed in the public domain.
45b81b6b3SRodney W. Grimes  *
555c497bfSJ.T. Conklin  * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
655c497bfSJ.T. Conklin  *
72a456239SPeter Wemm  * $FreeBSD$
85b81b6b3SRodney W. Grimes  */
94cf61abaSJ.T. Conklin 
1064867286SStefan Eßer #include <sys/types.h>
11f07e4247SGarrett Wollman 
125b81b6b3SRodney W. Grimes #include <ctype.h>
1355c497bfSJ.T. Conklin #include <err.h>
14915198b4SStefan Eßer #include <errno.h>
15f07e4247SGarrett Wollman #include <inttypes.h>
16915198b4SStefan Eßer #include <limits.h>
17f07e4247SGarrett Wollman #include <locale.h>
18f07e4247SGarrett Wollman #include <stdio.h>
19f07e4247SGarrett Wollman #include <stdlib.h>
20f07e4247SGarrett Wollman #include <string.h>
21f07e4247SGarrett Wollman #include <regex.h>
22f07e4247SGarrett Wollman #include <unistd.h>
23f07e4247SGarrett Wollman 
24f07e4247SGarrett Wollman /*
25f07e4247SGarrett Wollman  * POSIX specifies a specific error code for syntax errors.  We exit
26f07e4247SGarrett Wollman  * with this code for all errors.
27f07e4247SGarrett Wollman  */
28f07e4247SGarrett Wollman #define	ERR_EXIT	2
295b81b6b3SRodney W. Grimes 
305b81b6b3SRodney W. Grimes enum valtype {
31717252eaSJoerg Wunsch 	integer, numeric_string, string
325b81b6b3SRodney W. Grimes } ;
335b81b6b3SRodney W. Grimes 
345b81b6b3SRodney W. Grimes struct val {
355b81b6b3SRodney W. Grimes 	enum valtype type;
365b81b6b3SRodney W. Grimes 	union {
375b81b6b3SRodney W. Grimes 		char *s;
38f07e4247SGarrett Wollman 		intmax_t i;
395b81b6b3SRodney W. Grimes 	} u;
405b81b6b3SRodney W. Grimes } ;
415b81b6b3SRodney W. Grimes 
425b81b6b3SRodney W. Grimes struct val *result;
433d06e95dSKris Kennaway 
44f07e4247SGarrett Wollman int		chk_div(intmax_t, intmax_t);
45f07e4247SGarrett Wollman int		chk_minus(intmax_t, intmax_t, intmax_t);
46f07e4247SGarrett Wollman int		chk_plus(intmax_t, intmax_t, intmax_t);
47f07e4247SGarrett Wollman int		chk_times(intmax_t, intmax_t, intmax_t);
487669d0fcSWarner Losh void		free_value(struct val *);
497669d0fcSWarner Losh int		is_zero_or_null(struct val *);
507669d0fcSWarner Losh int		isstring(struct val *);
51f07e4247SGarrett Wollman struct val	*make_integer(intmax_t);
527669d0fcSWarner Losh struct val	*make_str(const char *);
537669d0fcSWarner Losh struct val	*op_and(struct val *, struct val *);
547669d0fcSWarner Losh struct val	*op_colon(struct val *, struct val *);
557669d0fcSWarner Losh struct val	*op_div(struct val *, struct val *);
567669d0fcSWarner Losh struct val	*op_eq(struct val *, struct val *);
577669d0fcSWarner Losh struct val	*op_ge(struct val *, struct val *);
587669d0fcSWarner Losh struct val	*op_gt(struct val *, struct val *);
597669d0fcSWarner Losh struct val	*op_le(struct val *, struct val *);
607669d0fcSWarner Losh struct val	*op_lt(struct val *, struct val *);
617669d0fcSWarner Losh struct val	*op_minus(struct val *, struct val *);
627669d0fcSWarner Losh struct val	*op_ne(struct val *, struct val *);
637669d0fcSWarner Losh struct val	*op_or(struct val *, struct val *);
647669d0fcSWarner Losh struct val	*op_plus(struct val *, struct val *);
657669d0fcSWarner Losh struct val	*op_rem(struct val *, struct val *);
667669d0fcSWarner Losh struct val	*op_times(struct val *, struct val *);
67f07e4247SGarrett Wollman intmax_t	to_integer(struct val *);
687669d0fcSWarner Losh void		to_string(struct val *);
697669d0fcSWarner Losh int		yyerror(const char *);
707669d0fcSWarner Losh int		yylex(void);
717669d0fcSWarner Losh int		yyparse(void);
725b81b6b3SRodney W. Grimes 
731393277eSGarrett Wollman static int	eflag;
745b81b6b3SRodney W. Grimes char **av;
755b81b6b3SRodney W. Grimes %}
765b81b6b3SRodney W. Grimes 
775b81b6b3SRodney W. Grimes %union
785b81b6b3SRodney W. Grimes {
795b81b6b3SRodney W. Grimes 	struct val *val;
805b81b6b3SRodney W. Grimes }
815b81b6b3SRodney W. Grimes 
825b81b6b3SRodney W. Grimes %left <val> '|'
835b81b6b3SRodney W. Grimes %left <val> '&'
845b81b6b3SRodney W. Grimes %left <val> '=' '>' '<' GE LE NE
855b81b6b3SRodney W. Grimes %left <val> '+' '-'
865b81b6b3SRodney W. Grimes %left <val> '*' '/' '%'
875b81b6b3SRodney W. Grimes %left <val> ':'
885b81b6b3SRodney W. Grimes 
895b81b6b3SRodney W. Grimes %token <val> TOKEN
905b81b6b3SRodney W. Grimes %type <val> start expr
915b81b6b3SRodney W. Grimes 
925b81b6b3SRodney W. Grimes %%
935b81b6b3SRodney W. Grimes 
945b81b6b3SRodney W. Grimes start: expr { result = $$; }
955b81b6b3SRodney W. Grimes 
965b81b6b3SRodney W. Grimes expr:	TOKEN
975b81b6b3SRodney W. Grimes 	| '(' expr ')' { $$ = $2; }
985b81b6b3SRodney W. Grimes 	| expr '|' expr { $$ = op_or ($1, $3); }
995b81b6b3SRodney W. Grimes 	| expr '&' expr { $$ = op_and ($1, $3); }
1005b81b6b3SRodney W. Grimes 	| expr '=' expr { $$ = op_eq ($1, $3); }
1015b81b6b3SRodney W. Grimes 	| expr '>' expr { $$ = op_gt ($1, $3); }
1025b81b6b3SRodney W. Grimes 	| expr '<' expr { $$ = op_lt ($1, $3); }
1035b81b6b3SRodney W. Grimes 	| expr GE expr  { $$ = op_ge ($1, $3); }
1045b81b6b3SRodney W. Grimes 	| expr LE expr  { $$ = op_le ($1, $3); }
1055b81b6b3SRodney W. Grimes 	| expr NE expr  { $$ = op_ne ($1, $3); }
1065b81b6b3SRodney W. Grimes 	| expr '+' expr { $$ = op_plus ($1, $3); }
1075b81b6b3SRodney W. Grimes 	| expr '-' expr { $$ = op_minus ($1, $3); }
1085b81b6b3SRodney W. Grimes 	| expr '*' expr { $$ = op_times ($1, $3); }
1095b81b6b3SRodney W. Grimes 	| expr '/' expr { $$ = op_div ($1, $3); }
1105b81b6b3SRodney W. Grimes 	| expr '%' expr { $$ = op_rem ($1, $3); }
1115b81b6b3SRodney W. Grimes 	| expr ':' expr { $$ = op_colon ($1, $3); }
1125b81b6b3SRodney W. Grimes 	;
1135b81b6b3SRodney W. Grimes 
1145b81b6b3SRodney W. Grimes 
1155b81b6b3SRodney W. Grimes %%
1165b81b6b3SRodney W. Grimes 
1175b81b6b3SRodney W. Grimes struct val *
118f07e4247SGarrett Wollman make_integer(intmax_t i)
1195b81b6b3SRodney W. Grimes {
1205b81b6b3SRodney W. Grimes 	struct val *vp;
1215b81b6b3SRodney W. Grimes 
1225b81b6b3SRodney W. Grimes 	vp = (struct val *) malloc (sizeof (*vp));
1235b81b6b3SRodney W. Grimes 	if (vp == NULL) {
124f07e4247SGarrett Wollman 		errx(ERR_EXIT, "malloc() failed");
1255b81b6b3SRodney W. Grimes 	}
1265b81b6b3SRodney W. Grimes 
1275b81b6b3SRodney W. Grimes 	vp->type = integer;
1285b81b6b3SRodney W. Grimes 	vp->u.i  = i;
1295b81b6b3SRodney W. Grimes 	return vp;
1305b81b6b3SRodney W. Grimes }
1315b81b6b3SRodney W. Grimes 
1325b81b6b3SRodney W. Grimes struct val *
1337669d0fcSWarner Losh make_str(const char *s)
1345b81b6b3SRodney W. Grimes {
1355b81b6b3SRodney W. Grimes 	struct val *vp;
136f07e4247SGarrett Wollman 	char *ep;
1375b81b6b3SRodney W. Grimes 
1385b81b6b3SRodney W. Grimes 	vp = (struct val *) malloc (sizeof (*vp));
1395b81b6b3SRodney W. Grimes 	if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) {
140f07e4247SGarrett Wollman 		errx(ERR_EXIT, "malloc() failed");
1415b81b6b3SRodney W. Grimes 	}
1425b81b6b3SRodney W. Grimes 
143f07e4247SGarrett Wollman 	/*
144f07e4247SGarrett Wollman 	 * Previously we tried to scan the string to see if it ``looked like''
145f07e4247SGarrett Wollman 	 * an integer (erroneously, as it happened).  Let strtoimax() do the
146f07e4247SGarrett Wollman 	 * dirty work.  We could cache the value, except that we are using
147f07e4247SGarrett Wollman 	 * a union and need to preserve the original string form until we
148f07e4247SGarrett Wollman 	 * are certain that it is not needed.
149f07e4247SGarrett Wollman 	 *
150f07e4247SGarrett Wollman 	 * IEEE Std.1003.1-2001 says:
151f07e4247SGarrett Wollman 	 * /integer/ An argument consisting only of an (optional) unary minus
152f07e4247SGarrett Wollman 	 *	     followed by digits.
153f07e4247SGarrett Wollman 	 *
154f07e4247SGarrett Wollman 	 * This means that arguments which consist of digits followed by
155f07e4247SGarrett Wollman 	 * non-digits MUST NOT be considered integers.  strtoimax() will
156f07e4247SGarrett Wollman 	 * figure this out for us.
157f07e4247SGarrett Wollman 	 */
1581393277eSGarrett Wollman 	if (eflag)
159f07e4247SGarrett Wollman 		(void)strtoimax(s, &ep, 10);
1601393277eSGarrett Wollman 	else
1611393277eSGarrett Wollman 		(void)strtol(s, &ep, 10);
1622a353a9fSJoerg Wunsch 
163f07e4247SGarrett Wollman 	if (*ep != '\0')
164717252eaSJoerg Wunsch 		vp->type = string;
165f07e4247SGarrett Wollman 	else
166f07e4247SGarrett Wollman 		vp->type = numeric_string;
1672a353a9fSJoerg Wunsch 
1685b81b6b3SRodney W. Grimes 	return vp;
1695b81b6b3SRodney W. Grimes }
1705b81b6b3SRodney W. Grimes 
1715b81b6b3SRodney W. Grimes 
1725b81b6b3SRodney W. Grimes void
1737669d0fcSWarner Losh free_value(struct val *vp)
1745b81b6b3SRodney W. Grimes {
175717252eaSJoerg Wunsch 	if (vp->type == string || vp->type == numeric_string)
1765b81b6b3SRodney W. Grimes 		free (vp->u.s);
1775b81b6b3SRodney W. Grimes }
1785b81b6b3SRodney W. Grimes 
1795b81b6b3SRodney W. Grimes 
180f07e4247SGarrett Wollman intmax_t
1817669d0fcSWarner Losh to_integer(struct val *vp)
1825b81b6b3SRodney W. Grimes {
183f07e4247SGarrett Wollman 	intmax_t i;
1845b81b6b3SRodney W. Grimes 
1855b81b6b3SRodney W. Grimes 	if (vp->type == integer)
1865b81b6b3SRodney W. Grimes 		return 1;
1875b81b6b3SRodney W. Grimes 
188717252eaSJoerg Wunsch 	if (vp->type == string)
1895b81b6b3SRodney W. Grimes 		return 0;
1905b81b6b3SRodney W. Grimes 
191717252eaSJoerg Wunsch 	/* vp->type == numeric_string, make it numeric */
192915198b4SStefan Eßer 	errno = 0;
1931393277eSGarrett Wollman 	if (eflag) {
194f07e4247SGarrett Wollman 		i  = strtoimax(vp->u.s, (char **)NULL, 10);
195f07e4247SGarrett Wollman 		if (errno == ERANGE)
196f07e4247SGarrett Wollman 			err(ERR_EXIT, NULL);
1971393277eSGarrett Wollman 	} else {
1981393277eSGarrett Wollman 		i = strtol(vp->u.s, (char **)NULL, 10);
1991393277eSGarrett Wollman 	}
200f07e4247SGarrett Wollman 
2015b81b6b3SRodney W. Grimes 	free (vp->u.s);
2025b81b6b3SRodney W. Grimes 	vp->u.i = i;
203717252eaSJoerg Wunsch 	vp->type = integer;
2045b81b6b3SRodney W. Grimes 	return 1;
2055b81b6b3SRodney W. Grimes }
2065b81b6b3SRodney W. Grimes 
2075b81b6b3SRodney W. Grimes void
2087669d0fcSWarner Losh to_string(struct val *vp)
2095b81b6b3SRodney W. Grimes {
2105b81b6b3SRodney W. Grimes 	char *tmp;
2115b81b6b3SRodney W. Grimes 
212717252eaSJoerg Wunsch 	if (vp->type == string || vp->type == numeric_string)
2135b81b6b3SRodney W. Grimes 		return;
2145b81b6b3SRodney W. Grimes 
215f07e4247SGarrett Wollman 	/*
216f07e4247SGarrett Wollman 	 * log_10(x) ~= 0.3 * log_2(x).  Rounding up gives the number
217f07e4247SGarrett Wollman 	 * of digits; add one each for the sign and terminating null
218f07e4247SGarrett Wollman 	 * character, respectively.
219f07e4247SGarrett Wollman 	 */
220f07e4247SGarrett Wollman #define	NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
221f07e4247SGarrett Wollman 	tmp = malloc(NDIGITS(vp->u.i));
222f07e4247SGarrett Wollman 	if (tmp == NULL)
223f07e4247SGarrett Wollman 		errx(ERR_EXIT, "malloc() failed");
2245b81b6b3SRodney W. Grimes 
225f07e4247SGarrett Wollman 	sprintf(tmp, "%jd", vp->u.i);
2265b81b6b3SRodney W. Grimes 	vp->type = string;
2275b81b6b3SRodney W. Grimes 	vp->u.s  = tmp;
2285b81b6b3SRodney W. Grimes }
2295b81b6b3SRodney W. Grimes 
2305b81b6b3SRodney W. Grimes 
2315b81b6b3SRodney W. Grimes int
2327669d0fcSWarner Losh isstring(struct val *vp)
2335b81b6b3SRodney W. Grimes {
234717252eaSJoerg Wunsch 	/* only TRUE if this string is not a valid integer */
2355b81b6b3SRodney W. Grimes 	return (vp->type == string);
2365b81b6b3SRodney W. Grimes }
2375b81b6b3SRodney W. Grimes 
2385b81b6b3SRodney W. Grimes 
2395b81b6b3SRodney W. Grimes int
2407669d0fcSWarner Losh yylex(void)
2415b81b6b3SRodney W. Grimes {
2425b81b6b3SRodney W. Grimes 	char *p;
2435b81b6b3SRodney W. Grimes 
2445b81b6b3SRodney W. Grimes 	if (*av == NULL)
2455b81b6b3SRodney W. Grimes 		return (0);
2465b81b6b3SRodney W. Grimes 
2475b81b6b3SRodney W. Grimes 	p = *av++;
2485b81b6b3SRodney W. Grimes 
2495b81b6b3SRodney W. Grimes 	if (strlen (p) == 1) {
2505b81b6b3SRodney W. Grimes 		if (strchr ("|&=<>+-*/%:()", *p))
2515b81b6b3SRodney W. Grimes 			return (*p);
2525b81b6b3SRodney W. Grimes 	} else if (strlen (p) == 2 && p[1] == '=') {
2535b81b6b3SRodney W. Grimes 		switch (*p) {
2545b81b6b3SRodney W. Grimes 		case '>': return (GE);
2555b81b6b3SRodney W. Grimes 		case '<': return (LE);
2565b81b6b3SRodney W. Grimes 		case '!': return (NE);
2575b81b6b3SRodney W. Grimes 		}
2585b81b6b3SRodney W. Grimes 	}
2595b81b6b3SRodney W. Grimes 
2605b81b6b3SRodney W. Grimes 	yylval.val = make_str (p);
2615b81b6b3SRodney W. Grimes 	return (TOKEN);
2625b81b6b3SRodney W. Grimes }
2635b81b6b3SRodney W. Grimes 
2645b81b6b3SRodney W. Grimes int
2657669d0fcSWarner Losh is_zero_or_null(struct val *vp)
2665b81b6b3SRodney W. Grimes {
2675b81b6b3SRodney W. Grimes 	if (vp->type == integer) {
2685b81b6b3SRodney W. Grimes 		return (vp->u.i == 0);
2695b81b6b3SRodney W. Grimes 	} else {
270c9fe00dcSJ.T. Conklin 		return (*vp->u.s == 0 || (to_integer (vp) && vp->u.i == 0));
2715b81b6b3SRodney W. Grimes 	}
2725b81b6b3SRodney W. Grimes 	/* NOTREACHED */
2735b81b6b3SRodney W. Grimes }
2745b81b6b3SRodney W. Grimes 
275717252eaSJoerg Wunsch int
276f07e4247SGarrett Wollman main(int argc, char *argv[])
2775b81b6b3SRodney W. Grimes {
278f07e4247SGarrett Wollman 	int c;
2794cf61abaSJ.T. Conklin 
280f07e4247SGarrett Wollman 	setlocale (LC_ALL, "");
281c9885518SGarrett Wollman 	if (getenv("EXPR_COMPAT") != NULL
282c9885518SGarrett Wollman 	    || check_utility_compat("expr")) {
28396ab7da3SGarrett Wollman 		av = argv + 1;
28494a48596SGarrett Wollman 		eflag = 1;
28596ab7da3SGarrett Wollman 	} else {
2861393277eSGarrett Wollman 		while ((c = getopt(argc, argv, "e")) != -1)
287f07e4247SGarrett Wollman 			switch (c) {
2881393277eSGarrett Wollman 			case 'e':
2891393277eSGarrett Wollman 				eflag = 1;
2901393277eSGarrett Wollman 				break;
2911393277eSGarrett Wollman 
292f07e4247SGarrett Wollman 			default:
2931393277eSGarrett Wollman 				fprintf(stderr,
2941393277eSGarrett Wollman 				    "usage: expr [-e] expression\n");
295f07e4247SGarrett Wollman 				exit(ERR_EXIT);
296f07e4247SGarrett Wollman 			}
297f07e4247SGarrett Wollman 		av = argv + optind;
29896ab7da3SGarrett Wollman 	}
2995b81b6b3SRodney W. Grimes 
3005b81b6b3SRodney W. Grimes 	yyparse();
3015b81b6b3SRodney W. Grimes 
3025b81b6b3SRodney W. Grimes 	if (result->type == integer)
303f07e4247SGarrett Wollman 		printf("%jd\n", result->u.i);
3045b81b6b3SRodney W. Grimes 	else
3055b81b6b3SRodney W. Grimes 		printf("%s\n", result->u.s);
3065b81b6b3SRodney W. Grimes 
307717252eaSJoerg Wunsch 	return (is_zero_or_null(result));
3085b81b6b3SRodney W. Grimes }
3095b81b6b3SRodney W. Grimes 
3105b81b6b3SRodney W. Grimes int
3117669d0fcSWarner Losh yyerror(const char *s __unused)
3125b81b6b3SRodney W. Grimes {
313f07e4247SGarrett Wollman 	errx(ERR_EXIT, "syntax error");
3145b81b6b3SRodney W. Grimes }
3155b81b6b3SRodney W. Grimes 
3165b81b6b3SRodney W. Grimes 
3175b81b6b3SRodney W. Grimes struct val *
3187669d0fcSWarner Losh op_or(struct val *a, struct val *b)
3195b81b6b3SRodney W. Grimes {
3205b81b6b3SRodney W. Grimes 	if (is_zero_or_null (a)) {
3215b81b6b3SRodney W. Grimes 		free_value (a);
3225b81b6b3SRodney W. Grimes 		return (b);
3235b81b6b3SRodney W. Grimes 	} else {
3245b81b6b3SRodney W. Grimes 		free_value (b);
3255b81b6b3SRodney W. Grimes 		return (a);
3265b81b6b3SRodney W. Grimes 	}
3275b81b6b3SRodney W. Grimes }
3285b81b6b3SRodney W. Grimes 
3295b81b6b3SRodney W. Grimes struct val *
3307669d0fcSWarner Losh op_and(struct val *a, struct val *b)
3315b81b6b3SRodney W. Grimes {
3325b81b6b3SRodney W. Grimes 	if (is_zero_or_null (a) || is_zero_or_null (b)) {
3335b81b6b3SRodney W. Grimes 		free_value (a);
3345b81b6b3SRodney W. Grimes 		free_value (b);
335f07e4247SGarrett Wollman 		return (make_integer ((intmax_t)0));
3365b81b6b3SRodney W. Grimes 	} else {
3375b81b6b3SRodney W. Grimes 		free_value (b);
3385b81b6b3SRodney W. Grimes 		return (a);
3395b81b6b3SRodney W. Grimes 	}
3405b81b6b3SRodney W. Grimes }
3415b81b6b3SRodney W. Grimes 
3425b81b6b3SRodney W. Grimes struct val *
3437669d0fcSWarner Losh op_eq(struct val *a, struct val *b)
3445b81b6b3SRodney W. Grimes {
3455b81b6b3SRodney W. Grimes 	struct val *r;
3465b81b6b3SRodney W. Grimes 
3475b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
3485b81b6b3SRodney W. Grimes 		to_string (a);
3495b81b6b3SRodney W. Grimes 		to_string (b);
350f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) == 0));
3515b81b6b3SRodney W. Grimes 	} else {
352717252eaSJoerg Wunsch 		(void)to_integer(a);
353717252eaSJoerg Wunsch 		(void)to_integer(b);
354f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i == b->u.i));
3555b81b6b3SRodney W. Grimes 	}
3565b81b6b3SRodney W. Grimes 
3575b81b6b3SRodney W. Grimes 	free_value (a);
3585b81b6b3SRodney W. Grimes 	free_value (b);
3595b81b6b3SRodney W. Grimes 	return r;
3605b81b6b3SRodney W. Grimes }
3615b81b6b3SRodney W. Grimes 
3625b81b6b3SRodney W. Grimes struct val *
3637669d0fcSWarner Losh op_gt(struct val *a, struct val *b)
3645b81b6b3SRodney W. Grimes {
3655b81b6b3SRodney W. Grimes 	struct val *r;
3665b81b6b3SRodney W. Grimes 
3675b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
3685b81b6b3SRodney W. Grimes 		to_string (a);
3695b81b6b3SRodney W. Grimes 		to_string (b);
370f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) > 0));
3715b81b6b3SRodney W. Grimes 	} else {
372717252eaSJoerg Wunsch 		(void)to_integer(a);
373717252eaSJoerg Wunsch 		(void)to_integer(b);
374f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i > b->u.i));
3755b81b6b3SRodney W. Grimes 	}
3765b81b6b3SRodney W. Grimes 
3775b81b6b3SRodney W. Grimes 	free_value (a);
3785b81b6b3SRodney W. Grimes 	free_value (b);
3795b81b6b3SRodney W. Grimes 	return r;
3805b81b6b3SRodney W. Grimes }
3815b81b6b3SRodney W. Grimes 
3825b81b6b3SRodney W. Grimes struct val *
3837669d0fcSWarner Losh op_lt(struct val *a, struct val *b)
3845b81b6b3SRodney W. Grimes {
3855b81b6b3SRodney W. Grimes 	struct val *r;
3865b81b6b3SRodney W. Grimes 
3875b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
3885b81b6b3SRodney W. Grimes 		to_string (a);
3895b81b6b3SRodney W. Grimes 		to_string (b);
390f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) < 0));
3915b81b6b3SRodney W. Grimes 	} else {
392717252eaSJoerg Wunsch 		(void)to_integer(a);
393717252eaSJoerg Wunsch 		(void)to_integer(b);
394f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i < b->u.i));
3955b81b6b3SRodney W. Grimes 	}
3965b81b6b3SRodney W. Grimes 
3975b81b6b3SRodney W. Grimes 	free_value (a);
3985b81b6b3SRodney W. Grimes 	free_value (b);
3995b81b6b3SRodney W. Grimes 	return r;
4005b81b6b3SRodney W. Grimes }
4015b81b6b3SRodney W. Grimes 
4025b81b6b3SRodney W. Grimes struct val *
4037669d0fcSWarner Losh op_ge(struct val *a, struct val *b)
4045b81b6b3SRodney W. Grimes {
4055b81b6b3SRodney W. Grimes 	struct val *r;
4065b81b6b3SRodney W. Grimes 
4075b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
4085b81b6b3SRodney W. Grimes 		to_string (a);
4095b81b6b3SRodney W. Grimes 		to_string (b);
410f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) >= 0));
4115b81b6b3SRodney W. Grimes 	} else {
412717252eaSJoerg Wunsch 		(void)to_integer(a);
413717252eaSJoerg Wunsch 		(void)to_integer(b);
414f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i >= b->u.i));
4155b81b6b3SRodney W. Grimes 	}
4165b81b6b3SRodney W. Grimes 
4175b81b6b3SRodney W. Grimes 	free_value (a);
4185b81b6b3SRodney W. Grimes 	free_value (b);
4195b81b6b3SRodney W. Grimes 	return r;
4205b81b6b3SRodney W. Grimes }
4215b81b6b3SRodney W. Grimes 
4225b81b6b3SRodney W. Grimes struct val *
4237669d0fcSWarner Losh op_le(struct val *a, struct val *b)
4245b81b6b3SRodney W. Grimes {
4255b81b6b3SRodney W. Grimes 	struct val *r;
4265b81b6b3SRodney W. Grimes 
4275b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
4285b81b6b3SRodney W. Grimes 		to_string (a);
4295b81b6b3SRodney W. Grimes 		to_string (b);
430f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) <= 0));
4315b81b6b3SRodney W. Grimes 	} else {
432717252eaSJoerg Wunsch 		(void)to_integer(a);
433717252eaSJoerg Wunsch 		(void)to_integer(b);
434f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i <= b->u.i));
4355b81b6b3SRodney W. Grimes 	}
4365b81b6b3SRodney W. Grimes 
4375b81b6b3SRodney W. Grimes 	free_value (a);
4385b81b6b3SRodney W. Grimes 	free_value (b);
4395b81b6b3SRodney W. Grimes 	return r;
4405b81b6b3SRodney W. Grimes }
4415b81b6b3SRodney W. Grimes 
4425b81b6b3SRodney W. Grimes struct val *
4437669d0fcSWarner Losh op_ne(struct val *a, struct val *b)
4445b81b6b3SRodney W. Grimes {
4455b81b6b3SRodney W. Grimes 	struct val *r;
4465b81b6b3SRodney W. Grimes 
4475b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
4485b81b6b3SRodney W. Grimes 		to_string (a);
4495b81b6b3SRodney W. Grimes 		to_string (b);
450f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) != 0));
4515b81b6b3SRodney W. Grimes 	} else {
452717252eaSJoerg Wunsch 		(void)to_integer(a);
453717252eaSJoerg Wunsch 		(void)to_integer(b);
454f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i != b->u.i));
4555b81b6b3SRodney W. Grimes 	}
4565b81b6b3SRodney W. Grimes 
4575b81b6b3SRodney W. Grimes 	free_value (a);
4585b81b6b3SRodney W. Grimes 	free_value (b);
4595b81b6b3SRodney W. Grimes 	return r;
4605b81b6b3SRodney W. Grimes }
4615b81b6b3SRodney W. Grimes 
462915198b4SStefan Eßer int
463f07e4247SGarrett Wollman chk_plus(intmax_t a, intmax_t b, intmax_t r)
464915198b4SStefan Eßer {
4651393277eSGarrett Wollman 
466915198b4SStefan Eßer 	/* sum of two positive numbers must be positive */
467915198b4SStefan Eßer 	if (a > 0 && b > 0 && r <= 0)
468915198b4SStefan Eßer 		return 1;
469915198b4SStefan Eßer 	/* sum of two negative numbers must be negative */
470915198b4SStefan Eßer 	if (a < 0 && b < 0 && r >= 0)
471915198b4SStefan Eßer 		return 1;
472915198b4SStefan Eßer 	/* all other cases are OK */
473915198b4SStefan Eßer 	return 0;
474915198b4SStefan Eßer }
475915198b4SStefan Eßer 
4765b81b6b3SRodney W. Grimes struct val *
4777669d0fcSWarner Losh op_plus(struct val *a, struct val *b)
4785b81b6b3SRodney W. Grimes {
4795b81b6b3SRodney W. Grimes 	struct val *r;
4805b81b6b3SRodney W. Grimes 
4815b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
482f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
4835b81b6b3SRodney W. Grimes 	}
4845b81b6b3SRodney W. Grimes 
4851393277eSGarrett Wollman 	if (eflag) {
4861393277eSGarrett Wollman 		r = make_integer(a->u.i + b->u.i);
487915198b4SStefan Eßer 		if (chk_plus(a->u.i, b->u.i, r->u.i)) {
488f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
489915198b4SStefan Eßer 		}
4901393277eSGarrett Wollman 	} else
4911393277eSGarrett Wollman 		r = make_integer((long)a->u.i + (long)b->u.i);
4921393277eSGarrett Wollman 
4935b81b6b3SRodney W. Grimes 	free_value (a);
4945b81b6b3SRodney W. Grimes 	free_value (b);
4955b81b6b3SRodney W. Grimes 	return r;
4965b81b6b3SRodney W. Grimes }
4975b81b6b3SRodney W. Grimes 
498915198b4SStefan Eßer int
499f07e4247SGarrett Wollman chk_minus(intmax_t a, intmax_t b, intmax_t r)
500915198b4SStefan Eßer {
5011393277eSGarrett Wollman 
502f07e4247SGarrett Wollman 	/* special case subtraction of INTMAX_MIN */
503f07e4247SGarrett Wollman 	if (b == INTMAX_MIN) {
504915198b4SStefan Eßer 		if (a >= 0)
505915198b4SStefan Eßer 			return 1;
506915198b4SStefan Eßer 		else
507915198b4SStefan Eßer 			return 0;
508915198b4SStefan Eßer 	}
509f07e4247SGarrett Wollman 	/* this is allowed for b != INTMAX_MIN */
510915198b4SStefan Eßer 	return chk_plus (a, -b, r);
511915198b4SStefan Eßer }
512915198b4SStefan Eßer 
5135b81b6b3SRodney W. Grimes struct val *
5147669d0fcSWarner Losh op_minus(struct val *a, struct val *b)
5155b81b6b3SRodney W. Grimes {
5165b81b6b3SRodney W. Grimes 	struct val *r;
5175b81b6b3SRodney W. Grimes 
5185b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
519f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
5205b81b6b3SRodney W. Grimes 	}
5215b81b6b3SRodney W. Grimes 
5221393277eSGarrett Wollman 	if (eflag) {
5231393277eSGarrett Wollman 		r = make_integer(a->u.i - b->u.i);
524915198b4SStefan Eßer 		if (chk_minus(a->u.i, b->u.i, r->u.i)) {
525f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
526915198b4SStefan Eßer 		}
5271393277eSGarrett Wollman 	} else
5281393277eSGarrett Wollman 		r = make_integer((long)a->u.i - (long)b->u.i);
5291393277eSGarrett Wollman 
5305b81b6b3SRodney W. Grimes 	free_value (a);
5315b81b6b3SRodney W. Grimes 	free_value (b);
5325b81b6b3SRodney W. Grimes 	return r;
5335b81b6b3SRodney W. Grimes }
5345b81b6b3SRodney W. Grimes 
535915198b4SStefan Eßer int
536f07e4247SGarrett Wollman chk_times(intmax_t a, intmax_t b, intmax_t r)
537915198b4SStefan Eßer {
538915198b4SStefan Eßer 	/* special case: first operand is 0, no overflow possible */
539915198b4SStefan Eßer 	if (a == 0)
540915198b4SStefan Eßer 		return 0;
541915198b4SStefan Eßer 	/* cerify that result of division matches second operand */
542915198b4SStefan Eßer 	if (r / a != b)
543915198b4SStefan Eßer 		return 1;
544915198b4SStefan Eßer 	return 0;
545915198b4SStefan Eßer }
546915198b4SStefan Eßer 
5475b81b6b3SRodney W. Grimes struct val *
5487669d0fcSWarner Losh op_times(struct val *a, struct val *b)
5495b81b6b3SRodney W. Grimes {
5505b81b6b3SRodney W. Grimes 	struct val *r;
5515b81b6b3SRodney W. Grimes 
5525b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
553f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
5545b81b6b3SRodney W. Grimes 	}
5555b81b6b3SRodney W. Grimes 
5561393277eSGarrett Wollman 	if (eflag) {
5571393277eSGarrett Wollman 		r = make_integer(a->u.i * b->u.i);
558915198b4SStefan Eßer 		if (chk_times(a->u.i, b->u.i, r->u.i)) {
559f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
560915198b4SStefan Eßer 		}
5611393277eSGarrett Wollman 	} else
5621393277eSGarrett Wollman 		r = make_integer((long)a->u.i * (long)b->u.i);
5631393277eSGarrett Wollman 
5645b81b6b3SRodney W. Grimes 	free_value (a);
5655b81b6b3SRodney W. Grimes 	free_value (b);
5665b81b6b3SRodney W. Grimes 	return (r);
5675b81b6b3SRodney W. Grimes }
5685b81b6b3SRodney W. Grimes 
569915198b4SStefan Eßer int
570f07e4247SGarrett Wollman chk_div(intmax_t a, intmax_t b)
571915198b4SStefan Eßer {
572915198b4SStefan Eßer 	/* div by zero has been taken care of before */
573f07e4247SGarrett Wollman 	/* only INTMAX_MIN / -1 causes overflow */
574f07e4247SGarrett Wollman 	if (a == INTMAX_MIN && b == -1)
575915198b4SStefan Eßer 		return 1;
576915198b4SStefan Eßer 	/* everything else is OK */
577915198b4SStefan Eßer 	return 0;
578915198b4SStefan Eßer }
579915198b4SStefan Eßer 
5805b81b6b3SRodney W. Grimes struct val *
5817669d0fcSWarner Losh op_div(struct val *a, struct val *b)
5825b81b6b3SRodney W. Grimes {
5835b81b6b3SRodney W. Grimes 	struct val *r;
5845b81b6b3SRodney W. Grimes 
5855b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
586f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
5875b81b6b3SRodney W. Grimes 	}
5885b81b6b3SRodney W. Grimes 
5895b81b6b3SRodney W. Grimes 	if (b->u.i == 0) {
590f07e4247SGarrett Wollman 		errx(ERR_EXIT, "division by zero");
5915b81b6b3SRodney W. Grimes 	}
5925b81b6b3SRodney W. Grimes 
5931393277eSGarrett Wollman 	if (eflag) {
5941393277eSGarrett Wollman 		r = make_integer(a->u.i / b->u.i);
5953d06e95dSKris Kennaway 		if (chk_div(a->u.i, b->u.i)) {
596f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
597915198b4SStefan Eßer 		}
5981393277eSGarrett Wollman 	} else
5991393277eSGarrett Wollman 		r = make_integer((long)a->u.i / (long)b->u.i);
6001393277eSGarrett Wollman 
6015b81b6b3SRodney W. Grimes 	free_value (a);
6025b81b6b3SRodney W. Grimes 	free_value (b);
6035b81b6b3SRodney W. Grimes 	return r;
6045b81b6b3SRodney W. Grimes }
6055b81b6b3SRodney W. Grimes 
6065b81b6b3SRodney W. Grimes struct val *
6077669d0fcSWarner Losh op_rem(struct val *a, struct val *b)
6085b81b6b3SRodney W. Grimes {
6095b81b6b3SRodney W. Grimes 	struct val *r;
6105b81b6b3SRodney W. Grimes 
6115b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
612f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
6135b81b6b3SRodney W. Grimes 	}
6145b81b6b3SRodney W. Grimes 
6155b81b6b3SRodney W. Grimes 	if (b->u.i == 0) {
616f07e4247SGarrett Wollman 		errx(ERR_EXIT, "division by zero");
6175b81b6b3SRodney W. Grimes 	}
6185b81b6b3SRodney W. Grimes 
6191393277eSGarrett Wollman 	if (eflag)
6201393277eSGarrett Wollman 		r = make_integer(a->u.i % b->u.i);
621915198b4SStefan Eßer 	        /* chk_rem necessary ??? */
6221393277eSGarrett Wollman 	else
6231393277eSGarrett Wollman 		r = make_integer((long)a->u.i % (long)b->u.i);
6241393277eSGarrett Wollman 
6255b81b6b3SRodney W. Grimes 	free_value (a);
6265b81b6b3SRodney W. Grimes 	free_value (b);
6275b81b6b3SRodney W. Grimes 	return r;
6285b81b6b3SRodney W. Grimes }
6295b81b6b3SRodney W. Grimes 
6305b81b6b3SRodney W. Grimes struct val *
6317669d0fcSWarner Losh op_colon(struct val *a, struct val *b)
6325b81b6b3SRodney W. Grimes {
6334ba5f298SAndrew Moore 	regex_t rp;
6344cf61abaSJ.T. Conklin 	regmatch_t rm[2];
6354ba5f298SAndrew Moore 	char errbuf[256];
6364ba5f298SAndrew Moore 	int eval;
6374ba5f298SAndrew Moore 	struct val *v;
638c9fe00dcSJ.T. Conklin 
639c9fe00dcSJ.T. Conklin 	/* coerce to both arguments to strings */
640c9fe00dcSJ.T. Conklin 	to_string(a);
641c9fe00dcSJ.T. Conklin 	to_string(b);
6425b81b6b3SRodney W. Grimes 
6434ba5f298SAndrew Moore 	/* compile regular expression */
6444a13ab7cSJ.T. Conklin 	if ((eval = regcomp (&rp, b->u.s, 0)) != 0) {
6454ba5f298SAndrew Moore 		regerror (eval, &rp, errbuf, sizeof(errbuf));
646f07e4247SGarrett Wollman 		errx(ERR_EXIT, "%s", errbuf);
6475b81b6b3SRodney W. Grimes 	}
6484ba5f298SAndrew Moore 
6494ba5f298SAndrew Moore 	/* compare string against pattern */
6504a13ab7cSJ.T. Conklin 	/* remember that patterns are anchored to the beginning of the line */
6513d06e95dSKris Kennaway 	if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) {
6524ba5f298SAndrew Moore 		if (rm[1].rm_so >= 0) {
65355c497bfSJ.T. Conklin 			*(a->u.s + rm[1].rm_eo) = '\0';
6544ba5f298SAndrew Moore 			v = make_str (a->u.s + rm[1].rm_so);
6554ba5f298SAndrew Moore 
6564ba5f298SAndrew Moore 		} else {
657f07e4247SGarrett Wollman 			v = make_integer ((intmax_t)(rm[0].rm_eo - rm[0].rm_so));
6584ba5f298SAndrew Moore 		}
6594ba5f298SAndrew Moore 	} else {
66055c497bfSJ.T. Conklin 		if (rp.re_nsub == 0) {
661f07e4247SGarrett Wollman 			v = make_integer ((intmax_t)0);
66255c497bfSJ.T. Conklin 		} else {
66355c497bfSJ.T. Conklin 			v = make_str ("");
66455c497bfSJ.T. Conklin 		}
6654ba5f298SAndrew Moore 	}
6664ba5f298SAndrew Moore 
6674ba5f298SAndrew Moore 	/* free arguments and pattern buffer */
6684ba5f298SAndrew Moore 	free_value (a);
6694ba5f298SAndrew Moore 	free_value (b);
6704ba5f298SAndrew Moore 	regfree (&rp);
6714ba5f298SAndrew Moore 
6724ba5f298SAndrew Moore 	return v;
6734ba5f298SAndrew Moore }
674