xref: /freebsd/bin/expr/expr.y (revision 9ddb49cbe45441fa3f3a10f6dd355e9956480b5f)
15b81b6b3SRodney W. Grimes %{
29ddb49cbSWarner Losh /*-
39ddb49cbSWarner Losh  * Written by Pace Willisson (pace@blitz.com)
455c497bfSJ.T. Conklin  * and placed in the public domain.
55b81b6b3SRodney W. Grimes  *
655c497bfSJ.T. Conklin  * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
755c497bfSJ.T. Conklin  *
82a456239SPeter Wemm  * $FreeBSD$
95b81b6b3SRodney W. Grimes  */
104cf61abaSJ.T. Conklin 
1164867286SStefan Eßer #include <sys/types.h>
12f07e4247SGarrett Wollman 
135b81b6b3SRodney W. Grimes #include <ctype.h>
1455c497bfSJ.T. Conklin #include <err.h>
15915198b4SStefan Eßer #include <errno.h>
16f07e4247SGarrett Wollman #include <inttypes.h>
17915198b4SStefan Eßer #include <limits.h>
18f07e4247SGarrett Wollman #include <locale.h>
19f07e4247SGarrett Wollman #include <stdio.h>
20f07e4247SGarrett Wollman #include <stdlib.h>
21f07e4247SGarrett Wollman #include <string.h>
22f07e4247SGarrett Wollman #include <regex.h>
23f07e4247SGarrett Wollman #include <unistd.h>
24f07e4247SGarrett Wollman 
25f07e4247SGarrett Wollman /*
26f07e4247SGarrett Wollman  * POSIX specifies a specific error code for syntax errors.  We exit
27f07e4247SGarrett Wollman  * with this code for all errors.
28f07e4247SGarrett Wollman  */
29f07e4247SGarrett Wollman #define	ERR_EXIT	2
305b81b6b3SRodney W. Grimes 
315b81b6b3SRodney W. Grimes enum valtype {
32717252eaSJoerg Wunsch 	integer, numeric_string, string
335b81b6b3SRodney W. Grimes } ;
345b81b6b3SRodney W. Grimes 
355b81b6b3SRodney W. Grimes struct val {
365b81b6b3SRodney W. Grimes 	enum valtype type;
375b81b6b3SRodney W. Grimes 	union {
385b81b6b3SRodney W. Grimes 		char *s;
39f07e4247SGarrett Wollman 		intmax_t i;
405b81b6b3SRodney W. Grimes 	} u;
415b81b6b3SRodney W. Grimes } ;
425b81b6b3SRodney W. Grimes 
435b81b6b3SRodney W. Grimes struct val *result;
443d06e95dSKris Kennaway 
45f07e4247SGarrett Wollman int		chk_div(intmax_t, intmax_t);
46f07e4247SGarrett Wollman int		chk_minus(intmax_t, intmax_t, intmax_t);
47f07e4247SGarrett Wollman int		chk_plus(intmax_t, intmax_t, intmax_t);
48f07e4247SGarrett Wollman int		chk_times(intmax_t, intmax_t, intmax_t);
497669d0fcSWarner Losh void		free_value(struct val *);
507669d0fcSWarner Losh int		is_zero_or_null(struct val *);
517669d0fcSWarner Losh int		isstring(struct val *);
52f07e4247SGarrett Wollman struct val	*make_integer(intmax_t);
537669d0fcSWarner Losh struct val	*make_str(const char *);
547669d0fcSWarner Losh struct val	*op_and(struct val *, struct val *);
557669d0fcSWarner Losh struct val	*op_colon(struct val *, struct val *);
567669d0fcSWarner Losh struct val	*op_div(struct val *, struct val *);
577669d0fcSWarner Losh struct val	*op_eq(struct val *, struct val *);
587669d0fcSWarner Losh struct val	*op_ge(struct val *, struct val *);
597669d0fcSWarner Losh struct val	*op_gt(struct val *, struct val *);
607669d0fcSWarner Losh struct val	*op_le(struct val *, struct val *);
617669d0fcSWarner Losh struct val	*op_lt(struct val *, struct val *);
627669d0fcSWarner Losh struct val	*op_minus(struct val *, struct val *);
637669d0fcSWarner Losh struct val	*op_ne(struct val *, struct val *);
647669d0fcSWarner Losh struct val	*op_or(struct val *, struct val *);
657669d0fcSWarner Losh struct val	*op_plus(struct val *, struct val *);
667669d0fcSWarner Losh struct val	*op_rem(struct val *, struct val *);
677669d0fcSWarner Losh struct val	*op_times(struct val *, struct val *);
68f07e4247SGarrett Wollman intmax_t	to_integer(struct val *);
697669d0fcSWarner Losh void		to_string(struct val *);
707669d0fcSWarner Losh int		yyerror(const char *);
717669d0fcSWarner Losh int		yylex(void);
727669d0fcSWarner Losh int		yyparse(void);
735b81b6b3SRodney W. Grimes 
741393277eSGarrett Wollman static int	eflag;
755b81b6b3SRodney W. Grimes char **av;
765b81b6b3SRodney W. Grimes %}
775b81b6b3SRodney W. Grimes 
785b81b6b3SRodney W. Grimes %union
795b81b6b3SRodney W. Grimes {
805b81b6b3SRodney W. Grimes 	struct val *val;
815b81b6b3SRodney W. Grimes }
825b81b6b3SRodney W. Grimes 
835b81b6b3SRodney W. Grimes %left <val> '|'
845b81b6b3SRodney W. Grimes %left <val> '&'
855b81b6b3SRodney W. Grimes %left <val> '=' '>' '<' GE LE NE
865b81b6b3SRodney W. Grimes %left <val> '+' '-'
875b81b6b3SRodney W. Grimes %left <val> '*' '/' '%'
885b81b6b3SRodney W. Grimes %left <val> ':'
895b81b6b3SRodney W. Grimes 
905b81b6b3SRodney W. Grimes %token <val> TOKEN
915b81b6b3SRodney W. Grimes %type <val> start expr
925b81b6b3SRodney W. Grimes 
935b81b6b3SRodney W. Grimes %%
945b81b6b3SRodney W. Grimes 
955b81b6b3SRodney W. Grimes start: expr { result = $$; }
965b81b6b3SRodney W. Grimes 
975b81b6b3SRodney W. Grimes expr:	TOKEN
985b81b6b3SRodney W. Grimes 	| '(' expr ')' { $$ = $2; }
995b81b6b3SRodney W. Grimes 	| expr '|' expr { $$ = op_or ($1, $3); }
1005b81b6b3SRodney W. Grimes 	| expr '&' expr { $$ = op_and ($1, $3); }
1015b81b6b3SRodney W. Grimes 	| expr '=' expr { $$ = op_eq ($1, $3); }
1025b81b6b3SRodney W. Grimes 	| expr '>' expr { $$ = op_gt ($1, $3); }
1035b81b6b3SRodney W. Grimes 	| expr '<' expr { $$ = op_lt ($1, $3); }
1045b81b6b3SRodney W. Grimes 	| expr GE expr  { $$ = op_ge ($1, $3); }
1055b81b6b3SRodney W. Grimes 	| expr LE expr  { $$ = op_le ($1, $3); }
1065b81b6b3SRodney W. Grimes 	| expr NE expr  { $$ = op_ne ($1, $3); }
1075b81b6b3SRodney W. Grimes 	| expr '+' expr { $$ = op_plus ($1, $3); }
1085b81b6b3SRodney W. Grimes 	| expr '-' expr { $$ = op_minus ($1, $3); }
1095b81b6b3SRodney W. Grimes 	| expr '*' expr { $$ = op_times ($1, $3); }
1105b81b6b3SRodney W. Grimes 	| expr '/' expr { $$ = op_div ($1, $3); }
1115b81b6b3SRodney W. Grimes 	| expr '%' expr { $$ = op_rem ($1, $3); }
1125b81b6b3SRodney W. Grimes 	| expr ':' expr { $$ = op_colon ($1, $3); }
1135b81b6b3SRodney W. Grimes 	;
1145b81b6b3SRodney W. Grimes 
1155b81b6b3SRodney W. Grimes 
1165b81b6b3SRodney W. Grimes %%
1175b81b6b3SRodney W. Grimes 
1185b81b6b3SRodney W. Grimes struct val *
119f07e4247SGarrett Wollman make_integer(intmax_t i)
1205b81b6b3SRodney W. Grimes {
1215b81b6b3SRodney W. Grimes 	struct val *vp;
1225b81b6b3SRodney W. Grimes 
1235b81b6b3SRodney W. Grimes 	vp = (struct val *) malloc (sizeof (*vp));
1245b81b6b3SRodney W. Grimes 	if (vp == NULL) {
125f07e4247SGarrett Wollman 		errx(ERR_EXIT, "malloc() failed");
1265b81b6b3SRodney W. Grimes 	}
1275b81b6b3SRodney W. Grimes 
1285b81b6b3SRodney W. Grimes 	vp->type = integer;
1295b81b6b3SRodney W. Grimes 	vp->u.i  = i;
1305b81b6b3SRodney W. Grimes 	return vp;
1315b81b6b3SRodney W. Grimes }
1325b81b6b3SRodney W. Grimes 
1335b81b6b3SRodney W. Grimes struct val *
1347669d0fcSWarner Losh make_str(const char *s)
1355b81b6b3SRodney W. Grimes {
1365b81b6b3SRodney W. Grimes 	struct val *vp;
137f07e4247SGarrett Wollman 	char *ep;
1385b81b6b3SRodney W. Grimes 
1395b81b6b3SRodney W. Grimes 	vp = (struct val *) malloc (sizeof (*vp));
1405b81b6b3SRodney W. Grimes 	if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) {
141f07e4247SGarrett Wollman 		errx(ERR_EXIT, "malloc() failed");
1425b81b6b3SRodney W. Grimes 	}
1435b81b6b3SRodney W. Grimes 
144f07e4247SGarrett Wollman 	/*
145f07e4247SGarrett Wollman 	 * Previously we tried to scan the string to see if it ``looked like''
146f07e4247SGarrett Wollman 	 * an integer (erroneously, as it happened).  Let strtoimax() do the
147f07e4247SGarrett Wollman 	 * dirty work.  We could cache the value, except that we are using
148f07e4247SGarrett Wollman 	 * a union and need to preserve the original string form until we
149f07e4247SGarrett Wollman 	 * are certain that it is not needed.
150f07e4247SGarrett Wollman 	 *
151f07e4247SGarrett Wollman 	 * IEEE Std.1003.1-2001 says:
152f07e4247SGarrett Wollman 	 * /integer/ An argument consisting only of an (optional) unary minus
153f07e4247SGarrett Wollman 	 *	     followed by digits.
154f07e4247SGarrett Wollman 	 *
155f07e4247SGarrett Wollman 	 * This means that arguments which consist of digits followed by
156f07e4247SGarrett Wollman 	 * non-digits MUST NOT be considered integers.  strtoimax() will
157f07e4247SGarrett Wollman 	 * figure this out for us.
158f07e4247SGarrett Wollman 	 */
1591393277eSGarrett Wollman 	if (eflag)
160f07e4247SGarrett Wollman 		(void)strtoimax(s, &ep, 10);
1611393277eSGarrett Wollman 	else
1621393277eSGarrett Wollman 		(void)strtol(s, &ep, 10);
1632a353a9fSJoerg Wunsch 
164f07e4247SGarrett Wollman 	if (*ep != '\0')
165717252eaSJoerg Wunsch 		vp->type = string;
166f07e4247SGarrett Wollman 	else
167f07e4247SGarrett Wollman 		vp->type = numeric_string;
1682a353a9fSJoerg Wunsch 
1695b81b6b3SRodney W. Grimes 	return vp;
1705b81b6b3SRodney W. Grimes }
1715b81b6b3SRodney W. Grimes 
1725b81b6b3SRodney W. Grimes 
1735b81b6b3SRodney W. Grimes void
1747669d0fcSWarner Losh free_value(struct val *vp)
1755b81b6b3SRodney W. Grimes {
176717252eaSJoerg Wunsch 	if (vp->type == string || vp->type == numeric_string)
1775b81b6b3SRodney W. Grimes 		free (vp->u.s);
1785b81b6b3SRodney W. Grimes }
1795b81b6b3SRodney W. Grimes 
1805b81b6b3SRodney W. Grimes 
181f07e4247SGarrett Wollman intmax_t
1827669d0fcSWarner Losh to_integer(struct val *vp)
1835b81b6b3SRodney W. Grimes {
184f07e4247SGarrett Wollman 	intmax_t i;
1855b81b6b3SRodney W. Grimes 
1865b81b6b3SRodney W. Grimes 	if (vp->type == integer)
1875b81b6b3SRodney W. Grimes 		return 1;
1885b81b6b3SRodney W. Grimes 
189717252eaSJoerg Wunsch 	if (vp->type == string)
1905b81b6b3SRodney W. Grimes 		return 0;
1915b81b6b3SRodney W. Grimes 
192717252eaSJoerg Wunsch 	/* vp->type == numeric_string, make it numeric */
193915198b4SStefan Eßer 	errno = 0;
1941393277eSGarrett Wollman 	if (eflag) {
195f07e4247SGarrett Wollman 		i  = strtoimax(vp->u.s, (char **)NULL, 10);
196f07e4247SGarrett Wollman 		if (errno == ERANGE)
197f07e4247SGarrett Wollman 			err(ERR_EXIT, NULL);
1981393277eSGarrett Wollman 	} else {
1991393277eSGarrett Wollman 		i = strtol(vp->u.s, (char **)NULL, 10);
2001393277eSGarrett Wollman 	}
201f07e4247SGarrett Wollman 
2025b81b6b3SRodney W. Grimes 	free (vp->u.s);
2035b81b6b3SRodney W. Grimes 	vp->u.i = i;
204717252eaSJoerg Wunsch 	vp->type = integer;
2055b81b6b3SRodney W. Grimes 	return 1;
2065b81b6b3SRodney W. Grimes }
2075b81b6b3SRodney W. Grimes 
2085b81b6b3SRodney W. Grimes void
2097669d0fcSWarner Losh to_string(struct val *vp)
2105b81b6b3SRodney W. Grimes {
2115b81b6b3SRodney W. Grimes 	char *tmp;
2125b81b6b3SRodney W. Grimes 
213717252eaSJoerg Wunsch 	if (vp->type == string || vp->type == numeric_string)
2145b81b6b3SRodney W. Grimes 		return;
2155b81b6b3SRodney W. Grimes 
216f07e4247SGarrett Wollman 	/*
217f07e4247SGarrett Wollman 	 * log_10(x) ~= 0.3 * log_2(x).  Rounding up gives the number
218f07e4247SGarrett Wollman 	 * of digits; add one each for the sign and terminating null
219f07e4247SGarrett Wollman 	 * character, respectively.
220f07e4247SGarrett Wollman 	 */
221f07e4247SGarrett Wollman #define	NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
222f07e4247SGarrett Wollman 	tmp = malloc(NDIGITS(vp->u.i));
223f07e4247SGarrett Wollman 	if (tmp == NULL)
224f07e4247SGarrett Wollman 		errx(ERR_EXIT, "malloc() failed");
2255b81b6b3SRodney W. Grimes 
226f07e4247SGarrett Wollman 	sprintf(tmp, "%jd", vp->u.i);
2275b81b6b3SRodney W. Grimes 	vp->type = string;
2285b81b6b3SRodney W. Grimes 	vp->u.s  = tmp;
2295b81b6b3SRodney W. Grimes }
2305b81b6b3SRodney W. Grimes 
2315b81b6b3SRodney W. Grimes 
2325b81b6b3SRodney W. Grimes int
2337669d0fcSWarner Losh isstring(struct val *vp)
2345b81b6b3SRodney W. Grimes {
235717252eaSJoerg Wunsch 	/* only TRUE if this string is not a valid integer */
2365b81b6b3SRodney W. Grimes 	return (vp->type == string);
2375b81b6b3SRodney W. Grimes }
2385b81b6b3SRodney W. Grimes 
2395b81b6b3SRodney W. Grimes 
2405b81b6b3SRodney W. Grimes int
2417669d0fcSWarner Losh yylex(void)
2425b81b6b3SRodney W. Grimes {
2435b81b6b3SRodney W. Grimes 	char *p;
2445b81b6b3SRodney W. Grimes 
2455b81b6b3SRodney W. Grimes 	if (*av == NULL)
2465b81b6b3SRodney W. Grimes 		return (0);
2475b81b6b3SRodney W. Grimes 
2485b81b6b3SRodney W. Grimes 	p = *av++;
2495b81b6b3SRodney W. Grimes 
2505b81b6b3SRodney W. Grimes 	if (strlen (p) == 1) {
2515b81b6b3SRodney W. Grimes 		if (strchr ("|&=<>+-*/%:()", *p))
2525b81b6b3SRodney W. Grimes 			return (*p);
2535b81b6b3SRodney W. Grimes 	} else if (strlen (p) == 2 && p[1] == '=') {
2545b81b6b3SRodney W. Grimes 		switch (*p) {
2555b81b6b3SRodney W. Grimes 		case '>': return (GE);
2565b81b6b3SRodney W. Grimes 		case '<': return (LE);
2575b81b6b3SRodney W. Grimes 		case '!': return (NE);
2585b81b6b3SRodney W. Grimes 		}
2595b81b6b3SRodney W. Grimes 	}
2605b81b6b3SRodney W. Grimes 
2615b81b6b3SRodney W. Grimes 	yylval.val = make_str (p);
2625b81b6b3SRodney W. Grimes 	return (TOKEN);
2635b81b6b3SRodney W. Grimes }
2645b81b6b3SRodney W. Grimes 
2655b81b6b3SRodney W. Grimes int
2667669d0fcSWarner Losh is_zero_or_null(struct val *vp)
2675b81b6b3SRodney W. Grimes {
2685b81b6b3SRodney W. Grimes 	if (vp->type == integer) {
2695b81b6b3SRodney W. Grimes 		return (vp->u.i == 0);
2705b81b6b3SRodney W. Grimes 	} else {
271c9fe00dcSJ.T. Conklin 		return (*vp->u.s == 0 || (to_integer (vp) && vp->u.i == 0));
2725b81b6b3SRodney W. Grimes 	}
2735b81b6b3SRodney W. Grimes 	/* NOTREACHED */
2745b81b6b3SRodney W. Grimes }
2755b81b6b3SRodney W. Grimes 
276717252eaSJoerg Wunsch int
277f07e4247SGarrett Wollman main(int argc, char *argv[])
2785b81b6b3SRodney W. Grimes {
279f07e4247SGarrett Wollman 	int c;
2804cf61abaSJ.T. Conklin 
281f07e4247SGarrett Wollman 	setlocale (LC_ALL, "");
282c9885518SGarrett Wollman 	if (getenv("EXPR_COMPAT") != NULL
283c9885518SGarrett Wollman 	    || check_utility_compat("expr")) {
28496ab7da3SGarrett Wollman 		av = argv + 1;
28594a48596SGarrett Wollman 		eflag = 1;
28696ab7da3SGarrett Wollman 	} else {
2871393277eSGarrett Wollman 		while ((c = getopt(argc, argv, "e")) != -1)
288f07e4247SGarrett Wollman 			switch (c) {
2891393277eSGarrett Wollman 			case 'e':
2901393277eSGarrett Wollman 				eflag = 1;
2911393277eSGarrett Wollman 				break;
2921393277eSGarrett Wollman 
293f07e4247SGarrett Wollman 			default:
2941393277eSGarrett Wollman 				fprintf(stderr,
2951393277eSGarrett Wollman 				    "usage: expr [-e] expression\n");
296f07e4247SGarrett Wollman 				exit(ERR_EXIT);
297f07e4247SGarrett Wollman 			}
298f07e4247SGarrett Wollman 		av = argv + optind;
29996ab7da3SGarrett Wollman 	}
3005b81b6b3SRodney W. Grimes 
3015b81b6b3SRodney W. Grimes 	yyparse();
3025b81b6b3SRodney W. Grimes 
3035b81b6b3SRodney W. Grimes 	if (result->type == integer)
304f07e4247SGarrett Wollman 		printf("%jd\n", result->u.i);
3055b81b6b3SRodney W. Grimes 	else
3065b81b6b3SRodney W. Grimes 		printf("%s\n", result->u.s);
3075b81b6b3SRodney W. Grimes 
308717252eaSJoerg Wunsch 	return (is_zero_or_null(result));
3095b81b6b3SRodney W. Grimes }
3105b81b6b3SRodney W. Grimes 
3115b81b6b3SRodney W. Grimes int
3127669d0fcSWarner Losh yyerror(const char *s __unused)
3135b81b6b3SRodney W. Grimes {
314f07e4247SGarrett Wollman 	errx(ERR_EXIT, "syntax error");
3155b81b6b3SRodney W. Grimes }
3165b81b6b3SRodney W. Grimes 
3175b81b6b3SRodney W. Grimes 
3185b81b6b3SRodney W. Grimes struct val *
3197669d0fcSWarner Losh op_or(struct val *a, struct val *b)
3205b81b6b3SRodney W. Grimes {
3215b81b6b3SRodney W. Grimes 	if (is_zero_or_null (a)) {
3225b81b6b3SRodney W. Grimes 		free_value (a);
3235b81b6b3SRodney W. Grimes 		return (b);
3245b81b6b3SRodney W. Grimes 	} else {
3255b81b6b3SRodney W. Grimes 		free_value (b);
3265b81b6b3SRodney W. Grimes 		return (a);
3275b81b6b3SRodney W. Grimes 	}
3285b81b6b3SRodney W. Grimes }
3295b81b6b3SRodney W. Grimes 
3305b81b6b3SRodney W. Grimes struct val *
3317669d0fcSWarner Losh op_and(struct val *a, struct val *b)
3325b81b6b3SRodney W. Grimes {
3335b81b6b3SRodney W. Grimes 	if (is_zero_or_null (a) || is_zero_or_null (b)) {
3345b81b6b3SRodney W. Grimes 		free_value (a);
3355b81b6b3SRodney W. Grimes 		free_value (b);
336f07e4247SGarrett Wollman 		return (make_integer ((intmax_t)0));
3375b81b6b3SRodney W. Grimes 	} else {
3385b81b6b3SRodney W. Grimes 		free_value (b);
3395b81b6b3SRodney W. Grimes 		return (a);
3405b81b6b3SRodney W. Grimes 	}
3415b81b6b3SRodney W. Grimes }
3425b81b6b3SRodney W. Grimes 
3435b81b6b3SRodney W. Grimes struct val *
3447669d0fcSWarner Losh op_eq(struct val *a, struct val *b)
3455b81b6b3SRodney W. Grimes {
3465b81b6b3SRodney W. Grimes 	struct val *r;
3475b81b6b3SRodney W. Grimes 
3485b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
3495b81b6b3SRodney W. Grimes 		to_string (a);
3505b81b6b3SRodney W. Grimes 		to_string (b);
351f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) == 0));
3525b81b6b3SRodney W. Grimes 	} else {
353717252eaSJoerg Wunsch 		(void)to_integer(a);
354717252eaSJoerg Wunsch 		(void)to_integer(b);
355f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i == b->u.i));
3565b81b6b3SRodney W. Grimes 	}
3575b81b6b3SRodney W. Grimes 
3585b81b6b3SRodney W. Grimes 	free_value (a);
3595b81b6b3SRodney W. Grimes 	free_value (b);
3605b81b6b3SRodney W. Grimes 	return r;
3615b81b6b3SRodney W. Grimes }
3625b81b6b3SRodney W. Grimes 
3635b81b6b3SRodney W. Grimes struct val *
3647669d0fcSWarner Losh op_gt(struct val *a, struct val *b)
3655b81b6b3SRodney W. Grimes {
3665b81b6b3SRodney W. Grimes 	struct val *r;
3675b81b6b3SRodney W. Grimes 
3685b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
3695b81b6b3SRodney W. Grimes 		to_string (a);
3705b81b6b3SRodney W. Grimes 		to_string (b);
371f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) > 0));
3725b81b6b3SRodney W. Grimes 	} else {
373717252eaSJoerg Wunsch 		(void)to_integer(a);
374717252eaSJoerg Wunsch 		(void)to_integer(b);
375f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i > b->u.i));
3765b81b6b3SRodney W. Grimes 	}
3775b81b6b3SRodney W. Grimes 
3785b81b6b3SRodney W. Grimes 	free_value (a);
3795b81b6b3SRodney W. Grimes 	free_value (b);
3805b81b6b3SRodney W. Grimes 	return r;
3815b81b6b3SRodney W. Grimes }
3825b81b6b3SRodney W. Grimes 
3835b81b6b3SRodney W. Grimes struct val *
3847669d0fcSWarner Losh op_lt(struct val *a, struct val *b)
3855b81b6b3SRodney W. Grimes {
3865b81b6b3SRodney W. Grimes 	struct val *r;
3875b81b6b3SRodney W. Grimes 
3885b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
3895b81b6b3SRodney W. Grimes 		to_string (a);
3905b81b6b3SRodney W. Grimes 		to_string (b);
391f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) < 0));
3925b81b6b3SRodney W. Grimes 	} else {
393717252eaSJoerg Wunsch 		(void)to_integer(a);
394717252eaSJoerg Wunsch 		(void)to_integer(b);
395f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i < b->u.i));
3965b81b6b3SRodney W. Grimes 	}
3975b81b6b3SRodney W. Grimes 
3985b81b6b3SRodney W. Grimes 	free_value (a);
3995b81b6b3SRodney W. Grimes 	free_value (b);
4005b81b6b3SRodney W. Grimes 	return r;
4015b81b6b3SRodney W. Grimes }
4025b81b6b3SRodney W. Grimes 
4035b81b6b3SRodney W. Grimes struct val *
4047669d0fcSWarner Losh op_ge(struct val *a, struct val *b)
4055b81b6b3SRodney W. Grimes {
4065b81b6b3SRodney W. Grimes 	struct val *r;
4075b81b6b3SRodney W. Grimes 
4085b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
4095b81b6b3SRodney W. Grimes 		to_string (a);
4105b81b6b3SRodney W. Grimes 		to_string (b);
411f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) >= 0));
4125b81b6b3SRodney W. Grimes 	} else {
413717252eaSJoerg Wunsch 		(void)to_integer(a);
414717252eaSJoerg Wunsch 		(void)to_integer(b);
415f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i >= b->u.i));
4165b81b6b3SRodney W. Grimes 	}
4175b81b6b3SRodney W. Grimes 
4185b81b6b3SRodney W. Grimes 	free_value (a);
4195b81b6b3SRodney W. Grimes 	free_value (b);
4205b81b6b3SRodney W. Grimes 	return r;
4215b81b6b3SRodney W. Grimes }
4225b81b6b3SRodney W. Grimes 
4235b81b6b3SRodney W. Grimes struct val *
4247669d0fcSWarner Losh op_le(struct val *a, struct val *b)
4255b81b6b3SRodney W. Grimes {
4265b81b6b3SRodney W. Grimes 	struct val *r;
4275b81b6b3SRodney W. Grimes 
4285b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
4295b81b6b3SRodney W. Grimes 		to_string (a);
4305b81b6b3SRodney W. Grimes 		to_string (b);
431f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) <= 0));
4325b81b6b3SRodney W. Grimes 	} else {
433717252eaSJoerg Wunsch 		(void)to_integer(a);
434717252eaSJoerg Wunsch 		(void)to_integer(b);
435f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i <= b->u.i));
4365b81b6b3SRodney W. Grimes 	}
4375b81b6b3SRodney W. Grimes 
4385b81b6b3SRodney W. Grimes 	free_value (a);
4395b81b6b3SRodney W. Grimes 	free_value (b);
4405b81b6b3SRodney W. Grimes 	return r;
4415b81b6b3SRodney W. Grimes }
4425b81b6b3SRodney W. Grimes 
4435b81b6b3SRodney W. Grimes struct val *
4447669d0fcSWarner Losh op_ne(struct val *a, struct val *b)
4455b81b6b3SRodney W. Grimes {
4465b81b6b3SRodney W. Grimes 	struct val *r;
4475b81b6b3SRodney W. Grimes 
4485b81b6b3SRodney W. Grimes 	if (isstring (a) || isstring (b)) {
4495b81b6b3SRodney W. Grimes 		to_string (a);
4505b81b6b3SRodney W. Grimes 		to_string (b);
451f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(strcoll (a->u.s, b->u.s) != 0));
4525b81b6b3SRodney W. Grimes 	} else {
453717252eaSJoerg Wunsch 		(void)to_integer(a);
454717252eaSJoerg Wunsch 		(void)to_integer(b);
455f07e4247SGarrett Wollman 		r = make_integer ((intmax_t)(a->u.i != b->u.i));
4565b81b6b3SRodney W. Grimes 	}
4575b81b6b3SRodney W. Grimes 
4585b81b6b3SRodney W. Grimes 	free_value (a);
4595b81b6b3SRodney W. Grimes 	free_value (b);
4605b81b6b3SRodney W. Grimes 	return r;
4615b81b6b3SRodney W. Grimes }
4625b81b6b3SRodney W. Grimes 
463915198b4SStefan Eßer int
464f07e4247SGarrett Wollman chk_plus(intmax_t a, intmax_t b, intmax_t r)
465915198b4SStefan Eßer {
4661393277eSGarrett Wollman 
467915198b4SStefan Eßer 	/* sum of two positive numbers must be positive */
468915198b4SStefan Eßer 	if (a > 0 && b > 0 && r <= 0)
469915198b4SStefan Eßer 		return 1;
470915198b4SStefan Eßer 	/* sum of two negative numbers must be negative */
471915198b4SStefan Eßer 	if (a < 0 && b < 0 && r >= 0)
472915198b4SStefan Eßer 		return 1;
473915198b4SStefan Eßer 	/* all other cases are OK */
474915198b4SStefan Eßer 	return 0;
475915198b4SStefan Eßer }
476915198b4SStefan Eßer 
4775b81b6b3SRodney W. Grimes struct val *
4787669d0fcSWarner Losh op_plus(struct val *a, struct val *b)
4795b81b6b3SRodney W. Grimes {
4805b81b6b3SRodney W. Grimes 	struct val *r;
4815b81b6b3SRodney W. Grimes 
4825b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
483f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
4845b81b6b3SRodney W. Grimes 	}
4855b81b6b3SRodney W. Grimes 
4861393277eSGarrett Wollman 	if (eflag) {
4871393277eSGarrett Wollman 		r = make_integer(a->u.i + b->u.i);
488915198b4SStefan Eßer 		if (chk_plus(a->u.i, b->u.i, r->u.i)) {
489f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
490915198b4SStefan Eßer 		}
4911393277eSGarrett Wollman 	} else
4921393277eSGarrett Wollman 		r = make_integer((long)a->u.i + (long)b->u.i);
4931393277eSGarrett Wollman 
4945b81b6b3SRodney W. Grimes 	free_value (a);
4955b81b6b3SRodney W. Grimes 	free_value (b);
4965b81b6b3SRodney W. Grimes 	return r;
4975b81b6b3SRodney W. Grimes }
4985b81b6b3SRodney W. Grimes 
499915198b4SStefan Eßer int
500f07e4247SGarrett Wollman chk_minus(intmax_t a, intmax_t b, intmax_t r)
501915198b4SStefan Eßer {
5021393277eSGarrett Wollman 
503f07e4247SGarrett Wollman 	/* special case subtraction of INTMAX_MIN */
504f07e4247SGarrett Wollman 	if (b == INTMAX_MIN) {
505915198b4SStefan Eßer 		if (a >= 0)
506915198b4SStefan Eßer 			return 1;
507915198b4SStefan Eßer 		else
508915198b4SStefan Eßer 			return 0;
509915198b4SStefan Eßer 	}
510f07e4247SGarrett Wollman 	/* this is allowed for b != INTMAX_MIN */
511915198b4SStefan Eßer 	return chk_plus (a, -b, r);
512915198b4SStefan Eßer }
513915198b4SStefan Eßer 
5145b81b6b3SRodney W. Grimes struct val *
5157669d0fcSWarner Losh op_minus(struct val *a, struct val *b)
5165b81b6b3SRodney W. Grimes {
5175b81b6b3SRodney W. Grimes 	struct val *r;
5185b81b6b3SRodney W. Grimes 
5195b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
520f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
5215b81b6b3SRodney W. Grimes 	}
5225b81b6b3SRodney W. Grimes 
5231393277eSGarrett Wollman 	if (eflag) {
5241393277eSGarrett Wollman 		r = make_integer(a->u.i - b->u.i);
525915198b4SStefan Eßer 		if (chk_minus(a->u.i, b->u.i, r->u.i)) {
526f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
527915198b4SStefan Eßer 		}
5281393277eSGarrett Wollman 	} else
5291393277eSGarrett Wollman 		r = make_integer((long)a->u.i - (long)b->u.i);
5301393277eSGarrett Wollman 
5315b81b6b3SRodney W. Grimes 	free_value (a);
5325b81b6b3SRodney W. Grimes 	free_value (b);
5335b81b6b3SRodney W. Grimes 	return r;
5345b81b6b3SRodney W. Grimes }
5355b81b6b3SRodney W. Grimes 
536915198b4SStefan Eßer int
537f07e4247SGarrett Wollman chk_times(intmax_t a, intmax_t b, intmax_t r)
538915198b4SStefan Eßer {
539915198b4SStefan Eßer 	/* special case: first operand is 0, no overflow possible */
540915198b4SStefan Eßer 	if (a == 0)
541915198b4SStefan Eßer 		return 0;
542915198b4SStefan Eßer 	/* cerify that result of division matches second operand */
543915198b4SStefan Eßer 	if (r / a != b)
544915198b4SStefan Eßer 		return 1;
545915198b4SStefan Eßer 	return 0;
546915198b4SStefan Eßer }
547915198b4SStefan Eßer 
5485b81b6b3SRodney W. Grimes struct val *
5497669d0fcSWarner Losh op_times(struct val *a, struct val *b)
5505b81b6b3SRodney W. Grimes {
5515b81b6b3SRodney W. Grimes 	struct val *r;
5525b81b6b3SRodney W. Grimes 
5535b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
554f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
5555b81b6b3SRodney W. Grimes 	}
5565b81b6b3SRodney W. Grimes 
5571393277eSGarrett Wollman 	if (eflag) {
5581393277eSGarrett Wollman 		r = make_integer(a->u.i * b->u.i);
559915198b4SStefan Eßer 		if (chk_times(a->u.i, b->u.i, r->u.i)) {
560f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
561915198b4SStefan Eßer 		}
5621393277eSGarrett Wollman 	} else
5631393277eSGarrett Wollman 		r = make_integer((long)a->u.i * (long)b->u.i);
5641393277eSGarrett Wollman 
5655b81b6b3SRodney W. Grimes 	free_value (a);
5665b81b6b3SRodney W. Grimes 	free_value (b);
5675b81b6b3SRodney W. Grimes 	return (r);
5685b81b6b3SRodney W. Grimes }
5695b81b6b3SRodney W. Grimes 
570915198b4SStefan Eßer int
571f07e4247SGarrett Wollman chk_div(intmax_t a, intmax_t b)
572915198b4SStefan Eßer {
573915198b4SStefan Eßer 	/* div by zero has been taken care of before */
574f07e4247SGarrett Wollman 	/* only INTMAX_MIN / -1 causes overflow */
575f07e4247SGarrett Wollman 	if (a == INTMAX_MIN && b == -1)
576915198b4SStefan Eßer 		return 1;
577915198b4SStefan Eßer 	/* everything else is OK */
578915198b4SStefan Eßer 	return 0;
579915198b4SStefan Eßer }
580915198b4SStefan Eßer 
5815b81b6b3SRodney W. Grimes struct val *
5827669d0fcSWarner Losh op_div(struct val *a, struct val *b)
5835b81b6b3SRodney W. Grimes {
5845b81b6b3SRodney W. Grimes 	struct val *r;
5855b81b6b3SRodney W. Grimes 
5865b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
587f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
5885b81b6b3SRodney W. Grimes 	}
5895b81b6b3SRodney W. Grimes 
5905b81b6b3SRodney W. Grimes 	if (b->u.i == 0) {
591f07e4247SGarrett Wollman 		errx(ERR_EXIT, "division by zero");
5925b81b6b3SRodney W. Grimes 	}
5935b81b6b3SRodney W. Grimes 
5941393277eSGarrett Wollman 	if (eflag) {
5951393277eSGarrett Wollman 		r = make_integer(a->u.i / b->u.i);
5963d06e95dSKris Kennaway 		if (chk_div(a->u.i, b->u.i)) {
597f07e4247SGarrett Wollman 			errx(ERR_EXIT, "overflow");
598915198b4SStefan Eßer 		}
5991393277eSGarrett Wollman 	} else
6001393277eSGarrett Wollman 		r = make_integer((long)a->u.i / (long)b->u.i);
6011393277eSGarrett Wollman 
6025b81b6b3SRodney W. Grimes 	free_value (a);
6035b81b6b3SRodney W. Grimes 	free_value (b);
6045b81b6b3SRodney W. Grimes 	return r;
6055b81b6b3SRodney W. Grimes }
6065b81b6b3SRodney W. Grimes 
6075b81b6b3SRodney W. Grimes struct val *
6087669d0fcSWarner Losh op_rem(struct val *a, struct val *b)
6095b81b6b3SRodney W. Grimes {
6105b81b6b3SRodney W. Grimes 	struct val *r;
6115b81b6b3SRodney W. Grimes 
6125b81b6b3SRodney W. Grimes 	if (!to_integer(a) || !to_integer(b)) {
613f07e4247SGarrett Wollman 		errx(ERR_EXIT, "non-numeric argument");
6145b81b6b3SRodney W. Grimes 	}
6155b81b6b3SRodney W. Grimes 
6165b81b6b3SRodney W. Grimes 	if (b->u.i == 0) {
617f07e4247SGarrett Wollman 		errx(ERR_EXIT, "division by zero");
6185b81b6b3SRodney W. Grimes 	}
6195b81b6b3SRodney W. Grimes 
6201393277eSGarrett Wollman 	if (eflag)
6211393277eSGarrett Wollman 		r = make_integer(a->u.i % b->u.i);
622915198b4SStefan Eßer 	        /* chk_rem necessary ??? */
6231393277eSGarrett Wollman 	else
6241393277eSGarrett Wollman 		r = make_integer((long)a->u.i % (long)b->u.i);
6251393277eSGarrett Wollman 
6265b81b6b3SRodney W. Grimes 	free_value (a);
6275b81b6b3SRodney W. Grimes 	free_value (b);
6285b81b6b3SRodney W. Grimes 	return r;
6295b81b6b3SRodney W. Grimes }
6305b81b6b3SRodney W. Grimes 
6315b81b6b3SRodney W. Grimes struct val *
6327669d0fcSWarner Losh op_colon(struct val *a, struct val *b)
6335b81b6b3SRodney W. Grimes {
6344ba5f298SAndrew Moore 	regex_t rp;
6354cf61abaSJ.T. Conklin 	regmatch_t rm[2];
6364ba5f298SAndrew Moore 	char errbuf[256];
6374ba5f298SAndrew Moore 	int eval;
6384ba5f298SAndrew Moore 	struct val *v;
639c9fe00dcSJ.T. Conklin 
640c9fe00dcSJ.T. Conklin 	/* coerce to both arguments to strings */
641c9fe00dcSJ.T. Conklin 	to_string(a);
642c9fe00dcSJ.T. Conklin 	to_string(b);
6435b81b6b3SRodney W. Grimes 
6444ba5f298SAndrew Moore 	/* compile regular expression */
6454a13ab7cSJ.T. Conklin 	if ((eval = regcomp (&rp, b->u.s, 0)) != 0) {
6464ba5f298SAndrew Moore 		regerror (eval, &rp, errbuf, sizeof(errbuf));
647f07e4247SGarrett Wollman 		errx(ERR_EXIT, "%s", errbuf);
6485b81b6b3SRodney W. Grimes 	}
6494ba5f298SAndrew Moore 
6504ba5f298SAndrew Moore 	/* compare string against pattern */
6514a13ab7cSJ.T. Conklin 	/* remember that patterns are anchored to the beginning of the line */
6523d06e95dSKris Kennaway 	if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) {
6534ba5f298SAndrew Moore 		if (rm[1].rm_so >= 0) {
65455c497bfSJ.T. Conklin 			*(a->u.s + rm[1].rm_eo) = '\0';
6554ba5f298SAndrew Moore 			v = make_str (a->u.s + rm[1].rm_so);
6564ba5f298SAndrew Moore 
6574ba5f298SAndrew Moore 		} else {
658f07e4247SGarrett Wollman 			v = make_integer ((intmax_t)(rm[0].rm_eo - rm[0].rm_so));
6594ba5f298SAndrew Moore 		}
6604ba5f298SAndrew Moore 	} else {
66155c497bfSJ.T. Conklin 		if (rp.re_nsub == 0) {
662f07e4247SGarrett Wollman 			v = make_integer ((intmax_t)0);
66355c497bfSJ.T. Conklin 		} else {
66455c497bfSJ.T. Conklin 			v = make_str ("");
66555c497bfSJ.T. Conklin 		}
6664ba5f298SAndrew Moore 	}
6674ba5f298SAndrew Moore 
6684ba5f298SAndrew Moore 	/* free arguments and pattern buffer */
6694ba5f298SAndrew Moore 	free_value (a);
6704ba5f298SAndrew Moore 	free_value (b);
6714ba5f298SAndrew Moore 	regfree (&rp);
6724ba5f298SAndrew Moore 
6734ba5f298SAndrew Moore 	return v;
6744ba5f298SAndrew Moore }
675