xref: /openbsd/bin/ksh/expr.c (revision b39c5158)
1 /*	$OpenBSD: expr.c,v 1.21 2009/06/01 19:00:57 deraadt Exp $	*/
2 
3 /*
4  * Korn expression evaluation
5  */
6 /*
7  * todo: better error handling: if in builtin, should be builtin error, etc.
8  */
9 
10 #include "sh.h"
11 #include <ctype.h>
12 
13 
14 /* The order of these enums is constrained by the order of opinfo[] */
15 enum token {
16 	/* some (long) unary operators */
17 	O_PLUSPLUS = 0, O_MINUSMINUS,
18 	/* binary operators */
19 	O_EQ, O_NE,
20 	/* assignments are assumed to be in range O_ASN .. O_BORASN */
21 	O_ASN, O_TIMESASN, O_DIVASN, O_MODASN, O_PLUSASN, O_MINUSASN,
22 	O_LSHIFTASN, O_RSHIFTASN, O_BANDASN, O_BXORASN, O_BORASN,
23 	O_LSHIFT, O_RSHIFT,
24 	O_LE, O_GE, O_LT, O_GT,
25 	O_LAND,
26 	O_LOR,
27 	O_TIMES, O_DIV, O_MOD,
28 	O_PLUS, O_MINUS,
29 	O_BAND,
30 	O_BXOR,
31 	O_BOR,
32 	O_TERN,
33 	O_COMMA,
34 	/* things after this aren't used as binary operators */
35 	/* unary that are not also binaries */
36 	O_BNOT, O_LNOT,
37 	/* misc */
38 	OPEN_PAREN, CLOSE_PAREN, CTERN,
39 	/* things that don't appear in the opinfo[] table */
40 	VAR, LIT, END, BAD
41 };
42 #define IS_BINOP(op) (((int)op) >= (int)O_EQ && ((int)op) <= (int)O_COMMA)
43 #define IS_ASSIGNOP(op)	((int)(op) >= (int)O_ASN && (int)(op) <= (int)O_BORASN)
44 
45 enum prec {
46 	P_PRIMARY = 0,		/* VAR, LIT, (), ~ ! - + */
47 	P_MULT,			/* * / % */
48 	P_ADD,			/* + - */
49 	P_SHIFT,		/* << >> */
50 	P_RELATION,		/* < <= > >= */
51 	P_EQUALITY,		/* == != */
52 	P_BAND,			/* & */
53 	P_BXOR,			/* ^ */
54 	P_BOR,			/* | */
55 	P_LAND,			/* && */
56 	P_LOR,			/* || */
57 	P_TERN,			/* ?: */
58 	P_ASSIGN,		/* = *= /= %= += -= <<= >>= &= ^= |= */
59 	P_COMMA			/* , */
60 };
61 #define MAX_PREC	P_COMMA
62 
63 struct opinfo {
64 	char		name[4];
65 	int		len;	/* name length */
66 	enum prec	prec;	/* precedence: lower is higher */
67 };
68 
69 /* Tokens in this table must be ordered so the longest are first
70  * (eg, += before +).  If you change something, change the order
71  * of enum token too.
72  */
73 static const struct opinfo opinfo[] = {
74 	{ "++",	 2, P_PRIMARY },	/* before + */
75 	{ "--",	 2, P_PRIMARY },	/* before - */
76 	{ "==",	 2, P_EQUALITY },	/* before = */
77 	{ "!=",	 2, P_EQUALITY },	/* before ! */
78 	{ "=",	 1, P_ASSIGN },		/* keep assigns in a block */
79 	{ "*=",	 2, P_ASSIGN },
80 	{ "/=",	 2, P_ASSIGN },
81 	{ "%=",	 2, P_ASSIGN },
82 	{ "+=",	 2, P_ASSIGN },
83 	{ "-=",	 2, P_ASSIGN },
84 	{ "<<=", 3, P_ASSIGN },
85 	{ ">>=", 3, P_ASSIGN },
86 	{ "&=",	 2, P_ASSIGN },
87 	{ "^=",	 2, P_ASSIGN },
88 	{ "|=",	 2, P_ASSIGN },
89 	{ "<<",	 2, P_SHIFT },
90 	{ ">>",	 2, P_SHIFT },
91 	{ "<=",	 2, P_RELATION },
92 	{ ">=",	 2, P_RELATION },
93 	{ "<",	 1, P_RELATION },
94 	{ ">",	 1, P_RELATION },
95 	{ "&&",	 2, P_LAND },
96 	{ "||",	 2, P_LOR },
97 	{ "*",	 1, P_MULT },
98 	{ "/",	 1, P_MULT },
99 	{ "%",	 1, P_MULT },
100 	{ "+",	 1, P_ADD },
101 	{ "-",	 1, P_ADD },
102 	{ "&",	 1, P_BAND },
103 	{ "^",	 1, P_BXOR },
104 	{ "|",	 1, P_BOR },
105 	{ "?",	 1, P_TERN },
106 	{ ",",	 1, P_COMMA },
107 	{ "~",	 1, P_PRIMARY },
108 	{ "!",	 1, P_PRIMARY },
109 	{ "(",	 1, P_PRIMARY },
110 	{ ")",	 1, P_PRIMARY },
111 	{ ":",	 1, P_PRIMARY },
112 	{ "",	 0, P_PRIMARY } /* end of table */
113 };
114 
115 
116 typedef struct expr_state Expr_state;
117 struct expr_state {
118 	const char *expression;		/* expression being evaluated */
119 	const char *tokp;		/* lexical position */
120 	enum token  tok;		/* token from token() */
121 	int	    noassign;		/* don't do assigns (for ?:,&&,||) */
122 	bool	    arith;		/* true if evaluating an $(())
123 					 * expression
124 					 */
125 	struct tbl *val;		/* value from token() */
126 	struct tbl *evaling;		/* variable that is being recursively
127 					 * expanded (EXPRINEVAL flag set)
128 					 */
129 };
130 
131 enum error_type {
132 	ET_UNEXPECTED, ET_BADLIT, ET_RECURSIVE,
133 	ET_LVALUE, ET_RDONLY, ET_STR
134 };
135 
136 static void	   evalerr(Expr_state *, enum error_type, const char *)
137 		    __attribute__((__noreturn__));
138 static struct tbl *evalexpr(Expr_state *, enum prec);
139 static void	   token(Expr_state *);
140 static struct tbl *do_ppmm(Expr_state *, enum token, struct tbl *, bool);
141 static void	   assign_check(Expr_state *, enum token, struct tbl *);
142 static struct tbl *tempvar(void);
143 static struct tbl *intvar(Expr_state *, struct tbl *);
144 
145 /*
146  * parse and evaluate expression
147  */
148 int
149 evaluate(const char *expr, long int *rval, int error_ok, bool arith)
150 {
151 	struct tbl v;
152 	int ret;
153 
154 	v.flag = DEFINED|INTEGER;
155 	v.type = 0;
156 	ret = v_evaluate(&v, expr, error_ok, arith);
157 	*rval = v.val.i;
158 	return ret;
159 }
160 
161 /*
162  * parse and evaluate expression, storing result in vp.
163  */
164 int
165 v_evaluate(struct tbl *vp, const char *expr, volatile int error_ok,
166     bool arith)
167 {
168 	struct tbl *v;
169 	Expr_state curstate;
170 	Expr_state * const es = &curstate;
171 	int i;
172 
173 	/* save state to allow recursive calls */
174 	curstate.expression = curstate.tokp = expr;
175 	curstate.noassign = 0;
176 	curstate.arith = arith;
177 	curstate.evaling = (struct tbl *) 0;
178 
179 	newenv(E_ERRH);
180 	i = sigsetjmp(e->jbuf, 0);
181 	if (i) {
182 		/* Clear EXPRINEVAL in of any variables we were playing with */
183 		if (curstate.evaling)
184 			curstate.evaling->flag &= ~EXPRINEVAL;
185 		quitenv(NULL);
186 		if (i == LAEXPR) {
187 			if (error_ok == KSH_RETURN_ERROR)
188 				return 0;
189 			errorf(null);
190 		}
191 		unwind(i);
192 		/* NOTREACHED */
193 	}
194 
195 	token(es);
196 #if 1 /* ifdef-out to disallow empty expressions to be treated as 0 */
197 	if (es->tok == END) {
198 		es->tok = LIT;
199 		es->val = tempvar();
200 	}
201 #endif /* 0 */
202 	v = intvar(es, evalexpr(es, MAX_PREC));
203 
204 	if (es->tok != END)
205 		evalerr(es, ET_UNEXPECTED, (char *) 0);
206 
207 	if (vp->flag & INTEGER)
208 		setint_v(vp, v, es->arith);
209 	else
210 		/* can fail if readonly */
211 		setstr(vp, str_val(v), error_ok);
212 
213 	quitenv(NULL);
214 
215 	return 1;
216 }
217 
218 static void
219 evalerr(Expr_state *es, enum error_type type, const char *str)
220 {
221 	char tbuf[2];
222 	const char *s;
223 
224 	es->arith = false;
225 	switch (type) {
226 	case ET_UNEXPECTED:
227 		switch (es->tok) {
228 		case VAR:
229 			s = es->val->name;
230 			break;
231 		case LIT:
232 			s = str_val(es->val);
233 			break;
234 		case END:
235 			s = "end of expression";
236 			break;
237 		case BAD:
238 			tbuf[0] = *es->tokp;
239 			tbuf[1] = '\0';
240 			s = tbuf;
241 			break;
242 		default:
243 			s = opinfo[(int)es->tok].name;
244 		}
245 		warningf(true, "%s: unexpected `%s'", es->expression, s);
246 		break;
247 
248 	case ET_BADLIT:
249 		warningf(true, "%s: bad number `%s'", es->expression, str);
250 		break;
251 
252 	case ET_RECURSIVE:
253 		warningf(true, "%s: expression recurses on parameter `%s'",
254 		    es->expression, str);
255 		break;
256 
257 	case ET_LVALUE:
258 		warningf(true, "%s: %s requires lvalue",
259 		    es->expression, str);
260 		break;
261 
262 	case ET_RDONLY:
263 		warningf(true, "%s: %s applied to read only variable",
264 		    es->expression, str);
265 		break;
266 
267 	default: /* keep gcc happy */
268 	case ET_STR:
269 		warningf(true, "%s: %s", es->expression, str);
270 		break;
271 	}
272 	unwind(LAEXPR);
273 }
274 
275 static struct tbl *
276 evalexpr(Expr_state *es, enum prec prec)
277 {
278 	struct tbl *vl, *vr = NULL, *vasn;
279 	enum token op;
280 	long res = 0;
281 
282 	if (prec == P_PRIMARY) {
283 		op = es->tok;
284 		if (op == O_BNOT || op == O_LNOT || op == O_MINUS ||
285 		    op == O_PLUS) {
286 			token(es);
287 			vl = intvar(es, evalexpr(es, P_PRIMARY));
288 			if (op == O_BNOT)
289 				vl->val.i = ~vl->val.i;
290 			else if (op == O_LNOT)
291 				vl->val.i = !vl->val.i;
292 			else if (op == O_MINUS)
293 				vl->val.i = -vl->val.i;
294 			/* op == O_PLUS is a no-op */
295 		} else if (op == OPEN_PAREN) {
296 			token(es);
297 			vl = evalexpr(es, MAX_PREC);
298 			if (es->tok != CLOSE_PAREN)
299 				evalerr(es, ET_STR, "missing )");
300 			token(es);
301 		} else if (op == O_PLUSPLUS || op == O_MINUSMINUS) {
302 			token(es);
303 			vl = do_ppmm(es, op, es->val, true);
304 			token(es);
305 		} else if (op == VAR || op == LIT) {
306 			vl = es->val;
307 			token(es);
308 		} else {
309 			evalerr(es, ET_UNEXPECTED, (char *) 0);
310 			/* NOTREACHED */
311 		}
312 		if (es->tok == O_PLUSPLUS || es->tok == O_MINUSMINUS) {
313 			vl = do_ppmm(es, es->tok, vl, false);
314 			token(es);
315 		}
316 		return vl;
317 	}
318 	vl = evalexpr(es, ((int) prec) - 1);
319 	for (op = es->tok; IS_BINOP(op) && opinfo[(int) op].prec == prec;
320 	    op = es->tok) {
321 		token(es);
322 		vasn = vl;
323 		if (op != O_ASN) /* vl may not have a value yet */
324 			vl = intvar(es, vl);
325 		if (IS_ASSIGNOP(op)) {
326 			assign_check(es, op, vasn);
327 			vr = intvar(es, evalexpr(es, P_ASSIGN));
328 		} else if (op != O_TERN && op != O_LAND && op != O_LOR)
329 			vr = intvar(es, evalexpr(es, ((int) prec) - 1));
330 		if ((op == O_DIV || op == O_MOD || op == O_DIVASN ||
331 		    op == O_MODASN) && vr->val.i == 0) {
332 			if (es->noassign)
333 				vr->val.i = 1;
334 			else
335 				evalerr(es, ET_STR, "zero divisor");
336 		}
337 		switch ((int) op) {
338 		case O_TIMES:
339 		case O_TIMESASN:
340 			res = vl->val.i * vr->val.i;
341 			break;
342 		case O_DIV:
343 		case O_DIVASN:
344 			res = vl->val.i / vr->val.i;
345 			break;
346 		case O_MOD:
347 		case O_MODASN:
348 			res = vl->val.i % vr->val.i;
349 			break;
350 		case O_PLUS:
351 		case O_PLUSASN:
352 			res = vl->val.i + vr->val.i;
353 			break;
354 		case O_MINUS:
355 		case O_MINUSASN:
356 			res = vl->val.i - vr->val.i;
357 			break;
358 		case O_LSHIFT:
359 		case O_LSHIFTASN:
360 			res = vl->val.i << vr->val.i;
361 			break;
362 		case O_RSHIFT:
363 		case O_RSHIFTASN:
364 			res = vl->val.i >> vr->val.i;
365 			break;
366 		case O_LT:
367 			res = vl->val.i < vr->val.i;
368 			break;
369 		case O_LE:
370 			res = vl->val.i <= vr->val.i;
371 			break;
372 		case O_GT:
373 			res = vl->val.i > vr->val.i;
374 			break;
375 		case O_GE:
376 			res = vl->val.i >= vr->val.i;
377 			break;
378 		case O_EQ:
379 			res = vl->val.i == vr->val.i;
380 			break;
381 		case O_NE:
382 			res = vl->val.i != vr->val.i;
383 			break;
384 		case O_BAND:
385 		case O_BANDASN:
386 			res = vl->val.i & vr->val.i;
387 			break;
388 		case O_BXOR:
389 		case O_BXORASN:
390 			res = vl->val.i ^ vr->val.i;
391 			break;
392 		case O_BOR:
393 		case O_BORASN:
394 			res = vl->val.i | vr->val.i;
395 			break;
396 		case O_LAND:
397 			if (!vl->val.i)
398 				es->noassign++;
399 			vr = intvar(es, evalexpr(es, ((int) prec) - 1));
400 			res = vl->val.i && vr->val.i;
401 			if (!vl->val.i)
402 				es->noassign--;
403 			break;
404 		case O_LOR:
405 			if (vl->val.i)
406 				es->noassign++;
407 			vr = intvar(es, evalexpr(es, ((int) prec) - 1));
408 			res = vl->val.i || vr->val.i;
409 			if (vl->val.i)
410 				es->noassign--;
411 			break;
412 		case O_TERN:
413 			{
414 				int e = vl->val.i != 0;
415 
416 				if (!e)
417 					es->noassign++;
418 				vl = evalexpr(es, MAX_PREC);
419 				if (!e)
420 					es->noassign--;
421 				if (es->tok != CTERN)
422 					evalerr(es, ET_STR, "missing :");
423 				token(es);
424 				if (e)
425 					es->noassign++;
426 				vr = evalexpr(es, P_TERN);
427 				if (e)
428 					es->noassign--;
429 				vl = e ? vl : vr;
430 			}
431 			break;
432 		case O_ASN:
433 			res = vr->val.i;
434 			break;
435 		case O_COMMA:
436 			res = vr->val.i;
437 			break;
438 		}
439 		if (IS_ASSIGNOP(op)) {
440 			vr->val.i = res;
441 			if (vasn->flag & INTEGER)
442 				setint_v(vasn, vr, es->arith);
443 			else
444 				setint(vasn, res);
445 			vl = vr;
446 		} else if (op != O_TERN)
447 			vl->val.i = res;
448 	}
449 	return vl;
450 }
451 
452 static void
453 token(Expr_state *es)
454 {
455 	const char *cp;
456 	int c;
457 	char *tvar;
458 
459 	/* skip white space */
460 	for (cp = es->tokp; (c = *cp), isspace(c); cp++)
461 		;
462 	es->tokp = cp;
463 
464 	if (c == '\0')
465 		es->tok = END;
466 	else if (letter(c)) {
467 		for (; letnum(c); c = *cp)
468 			cp++;
469 		if (c == '[') {
470 			int len;
471 
472 			len = array_ref_len(cp);
473 			if (len == 0)
474 				evalerr(es, ET_STR, "missing ]");
475 			cp += len;
476 		} else if (c == '(' /*)*/ ) {
477 			/* todo: add math functions (all take single argument):
478 			 * abs acos asin atan cos cosh exp int log sin sinh sqrt
479 			 * tan tanh
480 			 */
481 			;
482 		}
483 		if (es->noassign) {
484 			es->val = tempvar();
485 			es->val->flag |= EXPRLVALUE;
486 		} else {
487 			tvar = str_nsave(es->tokp, cp - es->tokp, ATEMP);
488 			es->val = global(tvar);
489 			afree(tvar, ATEMP);
490 		}
491 		es->tok = VAR;
492 	} else if (digit(c)) {
493 		for (; c != '_' && (letnum(c) || c == '#'); c = *cp++)
494 			;
495 		tvar = str_nsave(es->tokp, --cp - es->tokp, ATEMP);
496 		es->val = tempvar();
497 		es->val->flag &= ~INTEGER;
498 		es->val->type = 0;
499 		es->val->val.s = tvar;
500 		if (setint_v(es->val, es->val, es->arith) == NULL)
501 			evalerr(es, ET_BADLIT, tvar);
502 		afree(tvar, ATEMP);
503 		es->tok = LIT;
504 	} else {
505 		int i, n0;
506 
507 		for (i = 0; (n0 = opinfo[i].name[0]); i++)
508 			if (c == n0 &&
509 			    strncmp(cp, opinfo[i].name, opinfo[i].len) == 0) {
510 				es->tok = (enum token) i;
511 				cp += opinfo[i].len;
512 				break;
513 			}
514 		if (!n0)
515 			es->tok = BAD;
516 	}
517 	es->tokp = cp;
518 }
519 
520 /* Do a ++ or -- operation */
521 static struct tbl *
522 do_ppmm(Expr_state *es, enum token op, struct tbl *vasn, bool is_prefix)
523 {
524 	struct tbl *vl;
525 	int oval;
526 
527 	assign_check(es, op, vasn);
528 
529 	vl = intvar(es, vasn);
530 	oval = op == O_PLUSPLUS ? vl->val.i++ : vl->val.i--;
531 	if (vasn->flag & INTEGER)
532 		setint_v(vasn, vl, es->arith);
533 	else
534 		setint(vasn, vl->val.i);
535 	if (!is_prefix)		/* undo the inc/dec */
536 		vl->val.i = oval;
537 
538 	return vl;
539 }
540 
541 static void
542 assign_check(Expr_state *es, enum token op, struct tbl *vasn)
543 {
544 	if (es->tok == END ||
545 	    (vasn->name[0] == '\0' && !(vasn->flag & EXPRLVALUE)))
546 		evalerr(es, ET_LVALUE, opinfo[(int) op].name);
547 	else if (vasn->flag & RDONLY)
548 		evalerr(es, ET_RDONLY, opinfo[(int) op].name);
549 }
550 
551 static struct tbl *
552 tempvar(void)
553 {
554 	struct tbl *vp;
555 
556 	vp = (struct tbl*) alloc(sizeof(struct tbl), ATEMP);
557 	vp->flag = ISSET|INTEGER;
558 	vp->type = 0;
559 	vp->areap = ATEMP;
560 	vp->val.i = 0;
561 	vp->name[0] = '\0';
562 	return vp;
563 }
564 
565 /* cast (string) variable to temporary integer variable */
566 static struct tbl *
567 intvar(Expr_state *es, struct tbl *vp)
568 {
569 	struct tbl *vq;
570 
571 	/* try to avoid replacing a temp var with another temp var */
572 	if (vp->name[0] == '\0' &&
573 	    (vp->flag & (ISSET|INTEGER|EXPRLVALUE)) == (ISSET|INTEGER))
574 		return vp;
575 
576 	vq = tempvar();
577 	if (setint_v(vq, vp, es->arith) == NULL) {
578 		if (vp->flag & EXPRINEVAL)
579 			evalerr(es, ET_RECURSIVE, vp->name);
580 		es->evaling = vp;
581 		vp->flag |= EXPRINEVAL;
582 		v_evaluate(vq, str_val(vp), KSH_UNWIND_ERROR, es->arith);
583 		vp->flag &= ~EXPRINEVAL;
584 		es->evaling = (struct tbl *) 0;
585 	}
586 	return vq;
587 }
588