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