Home | History | Annotate | Line # | Download | only in ksh
expr.c revision 1.4
      1 /*	$NetBSD: expr.c,v 1.4 1999/10/20 15:49:15 hubertf 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;	/* precidence: 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 	struct tbl *val;		/* value from token() */
    123 	struct tbl *evaling;		/* variable that is being recursively
    124 					 * expanded (EXPRINEVAL flag set)
    125 					 */
    126 };
    127 
    128 enum error_type { ET_UNEXPECTED, ET_BADLIT, ET_RECURSIVE,
    129 		  ET_LVALUE, ET_RDONLY, ET_STR };
    130 
    131 static void        evalerr  ARGS((Expr_state *es, enum error_type type,
    132 				  const char *str)) GCC_FUNC_ATTR(noreturn);
    133 static struct tbl *evalexpr ARGS((Expr_state *es, enum prec prec));
    134 static void        token    ARGS((Expr_state *es));
    135 static struct tbl *do_ppmm  ARGS((Expr_state *es, enum token op,
    136 				  struct tbl *vasn, bool_t is_prefix));
    137 static void	   assign_check ARGS((Expr_state *es, enum token op,
    138 				      struct tbl *vasn));
    139 static struct tbl *tempvar  ARGS((void));
    140 static struct tbl *intvar   ARGS((Expr_state *es, struct tbl *vp));
    141 
    142 /*
    143  * parse and evalute expression
    144  */
    145 int
    146 evaluate(expr, rval, error_ok)
    147 	const char *expr;
    148 	long *rval;
    149 	int error_ok;
    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);
    157 	*rval = v.val.i;
    158 	return ret;
    159 }
    160 
    161 /*
    162  * parse and evalute expression, storing result in vp.
    163  */
    164 int
    165 v_evaluate(vp, expr, error_ok)
    166 	struct tbl *vp;
    167 	const char *expr;
    168 	volatile int error_ok;
    169 {
    170 	struct tbl *v;
    171 	Expr_state curstate;
    172 	Expr_state * const es = &curstate;
    173 	int i;
    174 
    175 	/* save state to allow recursive calls */
    176 	curstate.expression = curstate.tokp = expr;
    177 	curstate.noassign = 0;
    178 	curstate.evaling = (struct tbl *) 0;
    179 
    180 	newenv(E_ERRH);
    181 	i = ksh_sigsetjmp(e->jbuf, 0);
    182 	if (i) {
    183 		/* Clear EXPRINEVAL in of any variables we were playing with */
    184 		if (curstate.evaling)
    185 			curstate.evaling->flag &= ~EXPRINEVAL;
    186 		quitenv();
    187 		if (i == LAEXPR) {
    188 			if (error_ok == KSH_RETURN_ERROR)
    189 				return 0;
    190 			errorf(null);
    191 		}
    192 		unwind(i);
    193 		/*NOTREACHED*/
    194 	}
    195 
    196 	token(es);
    197 #if 1 /* ifdef-out to disallow empty expressions to be treated as 0 */
    198 	if (es->tok == END) {
    199 		es->tok = LIT;
    200 		es->val = tempvar();
    201 	}
    202 #endif /* 0 */
    203 	v = intvar(es, evalexpr(es, MAX_PREC));
    204 
    205 	if (es->tok != END)
    206 		evalerr(es, ET_UNEXPECTED, (char *) 0);
    207 
    208 	if (vp->flag & INTEGER)
    209 		setint_v(vp, v);
    210 	else
    211 		/* can fail if readony */
    212 		setstr(vp, str_val(v), error_ok);
    213 
    214 	quitenv();
    215 
    216 	return 1;
    217 }
    218 
    219 static void
    220 evalerr(es, type, str)
    221 	Expr_state *es;
    222 	enum error_type type;
    223 	const char *str;
    224 {
    225 	char tbuf[2];
    226 	const char *s;
    227 
    228 	switch (type) {
    229 	case ET_UNEXPECTED:
    230 		switch (es->tok) {
    231 		case VAR:
    232 			s = es->val->name;
    233 			break;
    234 		case LIT:
    235 			s = str_val(es->val);
    236 			break;
    237 		case END:
    238 			s = "end of expression";
    239 			break;
    240 		case BAD:
    241 			tbuf[0] = *es->tokp;
    242 			tbuf[1] = '\0';
    243 			s = tbuf;
    244 			break;
    245 		default:
    246 			s = opinfo[(int)es->tok].name;
    247 		}
    248 		warningf(TRUE, "%s: unexpected `%s'", es->expression, s);
    249 		break;
    250 
    251 	case ET_BADLIT:
    252 		warningf(TRUE, "%s: bad number `%s'", es->expression, str);
    253 		break;
    254 
    255 	case ET_RECURSIVE:
    256 		warningf(TRUE, "%s: expression recurses on parameter `%s'",
    257 			es->expression, str);
    258 		break;
    259 
    260 	case ET_LVALUE:
    261 		warningf(TRUE, "%s: %s requires lvalue",
    262 			es->expression, str);
    263 		break;
    264 
    265 	case ET_RDONLY:
    266 		warningf(TRUE, "%s: %s applied to read only variable",
    267 			es->expression, str);
    268 		break;
    269 
    270 	default: /* keep gcc happy */
    271 	case ET_STR:
    272 		warningf(TRUE, "%s: %s", es->expression, str);
    273 		break;
    274 	}
    275 	unwind(LAEXPR);
    276 }
    277 
    278 static struct tbl *
    279 evalexpr(es, prec)
    280 	Expr_state *es;
    281 	enum prec prec;
    282 {
    283 	struct tbl *vl, UNINITIALIZED(*vr), *vasn;
    284 	enum token op;
    285 	long UNINITIALIZED(res);
    286 
    287 	if (prec == P_PRIMARY) {
    288 		op = es->tok;
    289 		if (op == O_BNOT || op == O_LNOT || op == O_MINUS
    290 		    || op == O_PLUS)
    291 		{
    292 			token(es);
    293 			vl = intvar(es, evalexpr(es, P_PRIMARY));
    294 			if (op == O_BNOT)
    295 				vl->val.i = ~vl->val.i;
    296 			else if (op == O_LNOT)
    297 				vl->val.i = !vl->val.i;
    298 			else if (op == O_MINUS)
    299 				vl->val.i = -vl->val.i;
    300 			/* op == O_PLUS is a no-op */
    301 		} else if (op == OPEN_PAREN) {
    302 			token(es);
    303 			vl = evalexpr(es, MAX_PREC);
    304 			if (es->tok != CLOSE_PAREN)
    305 				evalerr(es, ET_STR, "missing )");
    306 			token(es);
    307 		} else if (op == O_PLUSPLUS || op == O_MINUSMINUS) {
    308 			token(es);
    309 			vl = do_ppmm(es, op, es->val, TRUE);
    310 			token(es);
    311 		} else if (op == VAR || op == LIT) {
    312 			vl = es->val;
    313 			token(es);
    314 		} else {
    315 			evalerr(es, ET_UNEXPECTED, (char *) 0);
    316 			/*NOTREACHED*/
    317 		}
    318 		if (es->tok == O_PLUSPLUS || es->tok == O_MINUSMINUS) {
    319 			vl = do_ppmm(es, es->tok, vl, FALSE);
    320 			token(es);
    321 		}
    322 		return vl;
    323 	}
    324 	vl = evalexpr(es, ((int) prec) - 1);
    325 	for (op = es->tok; IS_BINOP(op) && opinfo[(int) op].prec == prec;
    326 		op = es->tok)
    327 	{
    328 		token(es);
    329 		vasn = vl;
    330 		if (op != O_ASN) /* vl may not have a value yet */
    331 			vl = intvar(es, vl);
    332 		if (IS_ASSIGNOP(op)) {
    333 			assign_check(es, op, vasn);
    334 			vr = intvar(es, evalexpr(es, P_ASSIGN));
    335 		} else if (op != O_TERN && op != O_LAND && op != O_LOR)
    336 			vr = intvar(es, evalexpr(es, ((int) prec) - 1));
    337 		if ((op == O_DIV || op == O_MOD || op == O_DIVASN
    338 		     || op == O_MODASN) && vr->val.i == 0)
    339 		{
    340 			if (es->noassign)
    341 				vr->val.i = 1;
    342 			else
    343 				evalerr(es, ET_STR, "zero divisor");
    344 		}
    345 		switch ((int) op) {
    346 		case O_TIMES:
    347 		case O_TIMESASN:
    348 			res = vl->val.i * vr->val.i;
    349 			break;
    350 		case O_DIV:
    351 		case O_DIVASN:
    352 			res = vl->val.i / vr->val.i;
    353 			break;
    354 		case O_MOD:
    355 		case O_MODASN:
    356 			res = vl->val.i % vr->val.i;
    357 			break;
    358 		case O_PLUS:
    359 		case O_PLUSASN:
    360 			res = vl->val.i + vr->val.i;
    361 			break;
    362 		case O_MINUS:
    363 		case O_MINUSASN:
    364 			res = vl->val.i - vr->val.i;
    365 			break;
    366 		case O_LSHIFT:
    367 		case O_LSHIFTASN:
    368 			res = vl->val.i << vr->val.i;
    369 			break;
    370 		case O_RSHIFT:
    371 		case O_RSHIFTASN:
    372 			res = vl->val.i >> vr->val.i;
    373 			break;
    374 		case O_LT:
    375 			res = vl->val.i < vr->val.i;
    376 			break;
    377 		case O_LE:
    378 			res = vl->val.i <= vr->val.i;
    379 			break;
    380 		case O_GT:
    381 			res = vl->val.i > vr->val.i;
    382 			break;
    383 		case O_GE:
    384 			res = vl->val.i >= vr->val.i;
    385 			break;
    386 		case O_EQ:
    387 			res = vl->val.i == vr->val.i;
    388 			break;
    389 		case O_NE:
    390 			res = vl->val.i != vr->val.i;
    391 			break;
    392 		case O_BAND:
    393 		case O_BANDASN:
    394 			res = vl->val.i & vr->val.i;
    395 			break;
    396 		case O_BXOR:
    397 		case O_BXORASN:
    398 			res = vl->val.i ^ vr->val.i;
    399 			break;
    400 		case O_BOR:
    401 		case O_BORASN:
    402 			res = vl->val.i | vr->val.i;
    403 			break;
    404 		case O_LAND:
    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_LOR:
    413 			if (vl->val.i)
    414 				es->noassign++;
    415 			vr = intvar(es, evalexpr(es, ((int) prec) - 1));
    416 			res = vl->val.i || vr->val.i;
    417 			if (vl->val.i)
    418 				es->noassign--;
    419 			break;
    420 		case O_TERN:
    421 			{
    422 				int e = vl->val.i != 0;
    423 				if (!e)
    424 					es->noassign++;
    425 				vl = evalexpr(es, MAX_PREC);
    426 				if (!e)
    427 					es->noassign--;
    428 				if (es->tok != CTERN)
    429 					evalerr(es, ET_STR, "missing :");
    430 				token(es);
    431 				if (e)
    432 					es->noassign++;
    433 				vr = evalexpr(es, P_TERN);
    434 				if (e)
    435 					es->noassign--;
    436 				vl = e ? vl : vr;
    437 			}
    438 			break;
    439 		case O_ASN:
    440 			res = vr->val.i;
    441 			break;
    442 		case O_COMMA:
    443 			res = vr->val.i;
    444 			break;
    445 		}
    446 		if (IS_ASSIGNOP(op)) {
    447 			vr->val.i = res;
    448 			if (vasn->flag & INTEGER)
    449 				setint_v(vasn, vr);
    450 			else
    451 				setint(vasn, res);
    452 			vl = vr;
    453 		} else if (op != O_TERN)
    454 			vl->val.i = res;
    455 	}
    456 	return vl;
    457 }
    458 
    459 static void
    460 token(es)
    461 	Expr_state *es;
    462 {
    463 	const char *cp;
    464 	int c;
    465 	char *tvar;
    466 
    467 	/* skip white space */
    468 	for (cp = es->tokp; (c = *cp), isspace(c); cp++)
    469 		;
    470 	es->tokp = cp;
    471 
    472 	if (c == '\0')
    473 		es->tok = END;
    474 	else if (letter(c)) {
    475 		for (; letnum(c); c = *cp)
    476 			cp++;
    477 		if (c == '[') {
    478 			int len;
    479 
    480 			len = array_ref_len(cp);
    481 			if (len == 0)
    482 				evalerr(es, ET_STR, "missing ]");
    483 			cp += len;
    484 		}
    485 #ifdef KSH
    486 		else if (c == '(' /*)*/ ) {
    487 		    /* todo: add math functions (all take single argument):
    488 		     * abs acos asin atan cos cosh exp int log sin sinh sqrt
    489 		     * tan tanh
    490 		     */
    491 		    ;
    492 		}
    493 #endif /* KSH */
    494 		if (es->noassign) {
    495 			es->val = tempvar();
    496 			es->val->flag |= EXPRLVALUE;
    497 		} else {
    498 			tvar = str_nsave(es->tokp, cp - es->tokp, ATEMP);
    499 			es->val = global(tvar);
    500 			afree(tvar, ATEMP);
    501 		}
    502 		es->tok = VAR;
    503 	} else if (digit(c)) {
    504 		for (; c != '_' && (letnum(c) || c == '#'); c = *cp++)
    505 			;
    506 		tvar = str_nsave(es->tokp, --cp - es->tokp, ATEMP);
    507 		es->val = tempvar();
    508 		es->val->flag &= ~INTEGER;
    509 		es->val->type = 0;
    510 		es->val->val.s = tvar;
    511 		if (setint_v(es->val, es->val) == NULL)
    512 			evalerr(es, ET_BADLIT, tvar);
    513 		afree(tvar, ATEMP);
    514 		es->tok = LIT;
    515 	} else {
    516 		int i, n0;
    517 
    518 		for (i = 0; (n0 = opinfo[i].name[0]); i++)
    519 			if (c == n0
    520 			    && strncmp(cp, opinfo[i].name, opinfo[i].len) == 0)
    521 			{
    522 				es->tok = (enum token) i;
    523 				cp += opinfo[i].len;
    524 				break;
    525 			}
    526 		if (!n0)
    527 			es->tok = BAD;
    528 	}
    529 	es->tokp = cp;
    530 }
    531 
    532 /* Do a ++ or -- operation */
    533 static struct tbl *
    534 do_ppmm(es, op, vasn, is_prefix)
    535 	Expr_state *es;
    536 	enum token op;
    537 	struct tbl *vasn;
    538 	bool_t is_prefix;
    539 {
    540 	struct tbl *vl;
    541 	int oval;
    542 
    543 	assign_check(es, op, vasn);
    544 
    545 	vl = intvar(es, vasn);
    546 	oval = op == O_PLUSPLUS ? vl->val.i++ : vl->val.i--;
    547 	if (vasn->flag & INTEGER)
    548 		setint_v(vasn, vl);
    549 	else
    550 		setint(vasn, vl->val.i);
    551 	if (!is_prefix)		/* undo the inc/dec */
    552 		vl->val.i = oval;
    553 
    554 	return vl;
    555 }
    556 
    557 static void
    558 assign_check(es, op, vasn)
    559 	Expr_state *es;
    560 	enum token op;
    561 	struct tbl *vasn;
    562 {
    563 	if (vasn->name[0] == '\0' && !(vasn->flag & EXPRLVALUE))
    564 		evalerr(es, ET_LVALUE, opinfo[(int) op].name);
    565 	else if (vasn->flag & RDONLY)
    566 		evalerr(es, ET_RDONLY, opinfo[(int) op].name);
    567 }
    568 
    569 static struct tbl *
    570 tempvar()
    571 {
    572 	register struct tbl *vp;
    573 
    574 	vp = (struct tbl*) alloc(sizeof(struct tbl), ATEMP);
    575 	vp->flag = ISSET|INTEGER;
    576 	vp->type = 0;
    577 	vp->areap = ATEMP;
    578 	vp->val.i = 0;
    579 	vp->name[0] = '\0';
    580 	return vp;
    581 }
    582 
    583 /* cast (string) variable to temporary integer variable */
    584 static struct tbl *
    585 intvar(es, vp)
    586 	Expr_state *es;
    587 	struct tbl *vp;
    588 {
    589 	struct tbl *vq;
    590 
    591 	/* try to avoid replacing a temp var with another temp var */
    592 	if (vp->name[0] == '\0'
    593 	    && (vp->flag & (ISSET|INTEGER|EXPRLVALUE)) == (ISSET|INTEGER))
    594 		return vp;
    595 
    596 	vq = tempvar();
    597 	if (setint_v(vq, vp) == NULL) {
    598 		if (vp->flag & EXPRINEVAL)
    599 			evalerr(es, ET_RECURSIVE, vp->name);
    600 		es->evaling = vp;
    601 		vp->flag |= EXPRINEVAL;
    602 		v_evaluate(vq, str_val(vp), KSH_UNWIND_ERROR);
    603 		vp->flag &= ~EXPRINEVAL;
    604 		es->evaling = (struct tbl *) 0;
    605 	}
    606 	return vq;
    607 }
    608