xref: /netbsd-src/bin/sh/arithmetic.c (revision e76b58b2db7baa56b76bb9e8c4e74e750709d375)
1*e76b58b2Skre /*	$NetBSD: arithmetic.c,v 1.5 2018/04/21 23:01:29 kre Exp $	*/
2cc8e58edSkre 
3cc8e58edSkre /*-
4cc8e58edSkre  * Copyright (c) 1993
5cc8e58edSkre  *	The Regents of the University of California.  All rights reserved.
6cc8e58edSkre  * Copyright (c) 2007
7cc8e58edSkre  *	Herbert Xu <herbert@gondor.apana.org.au>.  All rights reserved.
8cc8e58edSkre  *
9cc8e58edSkre  * This code is derived from software contributed to Berkeley by
10cc8e58edSkre  * Kenneth Almquist.
11cc8e58edSkre  *
12cc8e58edSkre  * Redistribution and use in source and binary forms, with or without
13cc8e58edSkre  * modification, are permitted provided that the following conditions
14cc8e58edSkre  * are met:
15cc8e58edSkre  * 1. Redistributions of source code must retain the above copyright
16cc8e58edSkre  *    notice, this list of conditions and the following disclaimer.
17cc8e58edSkre  * 2. Redistributions in binary form must reproduce the above copyright
18cc8e58edSkre  *    notice, this list of conditions and the following disclaimer in the
19cc8e58edSkre  *    documentation and/or other materials provided with the distribution.
20cc8e58edSkre  * 3. Neither the name of the University nor the names of its contributors
21cc8e58edSkre  *    may be used to endorse or promote products derived from this software
22cc8e58edSkre  *    without specific prior written permission.
23cc8e58edSkre  *
24cc8e58edSkre  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25cc8e58edSkre  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26cc8e58edSkre  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27cc8e58edSkre  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28cc8e58edSkre  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29cc8e58edSkre  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30cc8e58edSkre  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31cc8e58edSkre  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32cc8e58edSkre  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33cc8e58edSkre  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34cc8e58edSkre  * SUCH DAMAGE.
35cc8e58edSkre  *
36cc8e58edSkre  * From FreeBSD, who obtained it from dash, modified both times...
37cc8e58edSkre  */
38cc8e58edSkre 
39cc8e58edSkre #include <sys/cdefs.h>
40cc8e58edSkre 
41cc8e58edSkre #ifndef lint
42*e76b58b2Skre __RCSID("$NetBSD: arithmetic.c,v 1.5 2018/04/21 23:01:29 kre Exp $");
43cc8e58edSkre #endif /* not lint */
44cc8e58edSkre 
45cc8e58edSkre #include <limits.h>
46cc8e58edSkre #include <errno.h>
47cc8e58edSkre #include <inttypes.h>
48cc8e58edSkre #include <stdlib.h>
49cc8e58edSkre #include <stdio.h>
50727a69dcSkre #include <string.h>
51cc8e58edSkre 
52cc8e58edSkre #include "shell.h"
53cc8e58edSkre #include "arithmetic.h"
54cc8e58edSkre #include "arith_tokens.h"
55cc8e58edSkre #include "expand.h"
56cc8e58edSkre #include "error.h"
57cc8e58edSkre #include "memalloc.h"
58cc8e58edSkre #include "output.h"
59cc8e58edSkre #include "options.h"
60cc8e58edSkre #include "var.h"
6144443aa0Skre #include "show.h"
62*e76b58b2Skre #include "syntax.h"
63cc8e58edSkre 
64cc8e58edSkre #if ARITH_BOR + ARITH_ASS_GAP != ARITH_BORASS || \
65cc8e58edSkre 	ARITH_ASS + ARITH_ASS_GAP != ARITH_EQ
66cc8e58edSkre #error Arithmetic tokens are out of order.
67cc8e58edSkre #endif
68cc8e58edSkre 
69cc8e58edSkre static const char *arith_startbuf;
70cc8e58edSkre 
71cc8e58edSkre const char *arith_buf;
72cc8e58edSkre union a_token_val a_t_val;
73cc8e58edSkre 
74cc8e58edSkre static int last_token;
75cc8e58edSkre 
76727a69dcSkre int arith_lno, arith_var_lno;
77727a69dcSkre 
78cc8e58edSkre #define ARITH_PRECEDENCE(op, prec) [op - ARITH_BINOP_MIN] = prec
79cc8e58edSkre 
80cc8e58edSkre static const char prec[ARITH_BINOP_MAX - ARITH_BINOP_MIN] = {
81cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_MUL, 0),
82cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_DIV, 0),
83cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_REM, 0),
84cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_ADD, 1),
85cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_SUB, 1),
86cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_LSHIFT, 2),
87cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_RSHIFT, 2),
88cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_LT, 3),
89cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_LE, 3),
90cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_GT, 3),
91cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_GE, 3),
92cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_EQ, 4),
93cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_NE, 4),
94cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_BAND, 5),
95cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_BXOR, 6),
96cc8e58edSkre 	ARITH_PRECEDENCE(ARITH_BOR, 7),
97cc8e58edSkre };
98cc8e58edSkre 
99cc8e58edSkre #define ARITH_MAX_PREC 8
100cc8e58edSkre 
101cc8e58edSkre int expcmd(int, char **);
102cc8e58edSkre 
103cc8e58edSkre static void __dead
arith_err(const char * s)104cc8e58edSkre arith_err(const char *s)
105cc8e58edSkre {
106cc8e58edSkre 	error("arithmetic expression: %s: \"%s\"", s, arith_startbuf);
107cc8e58edSkre 	/* NOTREACHED */
108cc8e58edSkre }
109cc8e58edSkre 
110cc8e58edSkre static intmax_t
arith_lookupvarint(char * varname)111cc8e58edSkre arith_lookupvarint(char *varname)
112cc8e58edSkre {
113cc8e58edSkre 	const char *str;
114cc8e58edSkre 	char *p;
115cc8e58edSkre 	intmax_t result;
116727a69dcSkre 	const int oln = line_number;
117cc8e58edSkre 
118727a69dcSkre 	VTRACE(DBG_ARITH, ("Arith var lookup(\"%s\") with lno=%d\n", varname,
119727a69dcSkre 	    arith_var_lno));
120727a69dcSkre 
121727a69dcSkre 	line_number = arith_var_lno;
122cc8e58edSkre 	str = lookupvar(varname);
123727a69dcSkre 	line_number = oln;
124727a69dcSkre 
125cc8e58edSkre 	if (uflag && str == NULL)
126cc8e58edSkre 		arith_err("variable not set");
127cc8e58edSkre 	if (str == NULL || *str == '\0')
128cc8e58edSkre 		str = "0";
129cc8e58edSkre 	errno = 0;
130cc8e58edSkre 	result = strtoimax(str, &p, 0);
131*e76b58b2Skre 	if (errno != 0 || *p != '\0') {
132*e76b58b2Skre 		if (errno == 0) {
133*e76b58b2Skre 			while (*p != '\0' && is_space(*p))
134*e76b58b2Skre 				p++;
135*e76b58b2Skre 			if (*p == '\0')
136*e76b58b2Skre 				return result;
137*e76b58b2Skre 		}
138cc8e58edSkre 		arith_err("variable contains non-numeric value");
139*e76b58b2Skre 	}
140cc8e58edSkre 	return result;
141cc8e58edSkre }
142cc8e58edSkre 
143cc8e58edSkre static inline int
arith_prec(int op)144cc8e58edSkre arith_prec(int op)
145cc8e58edSkre {
146cc8e58edSkre 
147cc8e58edSkre 	return prec[op - ARITH_BINOP_MIN];
148cc8e58edSkre }
149cc8e58edSkre 
150cc8e58edSkre static inline int
higher_prec(int op1,int op2)151cc8e58edSkre higher_prec(int op1, int op2)
152cc8e58edSkre {
153cc8e58edSkre 
154cc8e58edSkre 	return arith_prec(op1) < arith_prec(op2);
155cc8e58edSkre }
156cc8e58edSkre 
157cc8e58edSkre static intmax_t
do_binop(int op,intmax_t a,intmax_t b)158cc8e58edSkre do_binop(int op, intmax_t a, intmax_t b)
159cc8e58edSkre {
160cc8e58edSkre 
16144443aa0Skre 	VTRACE(DBG_ARITH, ("Arith do binop %d (%jd, %jd)\n", op, a, b));
162cc8e58edSkre 	switch (op) {
163cc8e58edSkre 	default:
164cc8e58edSkre 		arith_err("token error");
165cc8e58edSkre 	case ARITH_REM:
166cc8e58edSkre 	case ARITH_DIV:
167cc8e58edSkre 		if (b == 0)
168cc8e58edSkre 			arith_err("division by zero");
169cc8e58edSkre 		if (a == INTMAX_MIN && b == -1)
170cc8e58edSkre 			arith_err("divide error");
171cc8e58edSkre 		return op == ARITH_REM ? a % b : a / b;
172cc8e58edSkre 	case ARITH_MUL:
173cc8e58edSkre 		return (uintmax_t)a * (uintmax_t)b;
174cc8e58edSkre 	case ARITH_ADD:
175cc8e58edSkre 		return (uintmax_t)a + (uintmax_t)b;
176cc8e58edSkre 	case ARITH_SUB:
177cc8e58edSkre 		return (uintmax_t)a - (uintmax_t)b;
178cc8e58edSkre 	case ARITH_LSHIFT:
179cc8e58edSkre 		return (uintmax_t)a << (b & (sizeof(uintmax_t) * CHAR_BIT - 1));
180cc8e58edSkre 	case ARITH_RSHIFT:
181cc8e58edSkre 		return a >> (b & (sizeof(uintmax_t) * CHAR_BIT - 1));
182cc8e58edSkre 	case ARITH_LT:
183cc8e58edSkre 		return a < b;
184cc8e58edSkre 	case ARITH_LE:
185cc8e58edSkre 		return a <= b;
186cc8e58edSkre 	case ARITH_GT:
187cc8e58edSkre 		return a > b;
188cc8e58edSkre 	case ARITH_GE:
189cc8e58edSkre 		return a >= b;
190cc8e58edSkre 	case ARITH_EQ:
191cc8e58edSkre 		return a == b;
192cc8e58edSkre 	case ARITH_NE:
193cc8e58edSkre 		return a != b;
194cc8e58edSkre 	case ARITH_BAND:
195cc8e58edSkre 		return a & b;
196cc8e58edSkre 	case ARITH_BXOR:
197cc8e58edSkre 		return a ^ b;
198cc8e58edSkre 	case ARITH_BOR:
199cc8e58edSkre 		return a | b;
200cc8e58edSkre 	}
201cc8e58edSkre }
202cc8e58edSkre 
203e79133b5Skre static intmax_t assignment(int, int);
204e79133b5Skre static intmax_t comma_list(int, int);
205cc8e58edSkre 
206cc8e58edSkre static intmax_t
primary(int token,union a_token_val * val,int op,int noeval)207cc8e58edSkre primary(int token, union a_token_val *val, int op, int noeval)
208cc8e58edSkre {
209cc8e58edSkre 	intmax_t result;
210e79133b5Skre 	char sresult[DIGITS(result) + 1];
211cc8e58edSkre 
21244443aa0Skre 	VTRACE(DBG_ARITH, ("Arith primary: token %d op %d%s\n",
21344443aa0Skre 	    token, op, noeval ? " noeval" : ""));
21444443aa0Skre 
215cc8e58edSkre 	switch (token) {
216cc8e58edSkre 	case ARITH_LPAREN:
217e79133b5Skre 		result = comma_list(op, noeval);
218cc8e58edSkre 		if (last_token != ARITH_RPAREN)
219cc8e58edSkre 			arith_err("expecting ')'");
220cc8e58edSkre 		last_token = arith_token();
221cc8e58edSkre 		return result;
222cc8e58edSkre 	case ARITH_NUM:
223cc8e58edSkre 		last_token = op;
224cc8e58edSkre 		return val->val;
225cc8e58edSkre 	case ARITH_VAR:
226e79133b5Skre 		result = noeval ? val->val : arith_lookupvarint(val->name);
227e79133b5Skre 		if (op == ARITH_INCR || op == ARITH_DECR) {
228e79133b5Skre 			last_token = arith_token();
229e79133b5Skre 			if (noeval)
230e79133b5Skre 				return val->val;
231e79133b5Skre 
232e79133b5Skre 			snprintf(sresult, sizeof(sresult), ARITH_FORMAT_STR,
233e79133b5Skre 			    result + (op == ARITH_INCR ? 1 : -1));
234e79133b5Skre 			setvar(val->name, sresult, 0);
235e79133b5Skre 		} else
236cc8e58edSkre 			last_token = op;
237e79133b5Skre 		return result;
238cc8e58edSkre 	case ARITH_ADD:
239cc8e58edSkre 		*val = a_t_val;
240cc8e58edSkre 		return primary(op, val, arith_token(), noeval);
241cc8e58edSkre 	case ARITH_SUB:
242cc8e58edSkre 		*val = a_t_val;
243cc8e58edSkre 		return -primary(op, val, arith_token(), noeval);
244cc8e58edSkre 	case ARITH_NOT:
245cc8e58edSkre 		*val = a_t_val;
246cc8e58edSkre 		return !primary(op, val, arith_token(), noeval);
247cc8e58edSkre 	case ARITH_BNOT:
248cc8e58edSkre 		*val = a_t_val;
249cc8e58edSkre 		return ~primary(op, val, arith_token(), noeval);
250e79133b5Skre 	case ARITH_INCR:
251e79133b5Skre 	case ARITH_DECR:
252e79133b5Skre 		if (op != ARITH_VAR)
253e79133b5Skre 			arith_err("incr/decr require var name");
254e79133b5Skre 		last_token = arith_token();
255e79133b5Skre 		if (noeval)
256e79133b5Skre 			return val->val;
257e79133b5Skre 		result = arith_lookupvarint(a_t_val.name);
258e79133b5Skre 		snprintf(sresult, sizeof(sresult), ARITH_FORMAT_STR,
259e79133b5Skre 			    result += (token == ARITH_INCR ? 1 : -1));
260e79133b5Skre 		setvar(a_t_val.name, sresult, 0);
261e79133b5Skre 		return result;
262cc8e58edSkre 	default:
263cc8e58edSkre 		arith_err("expecting primary");
264cc8e58edSkre 	}
265cc8e58edSkre 	return 0;	/* never reached */
266cc8e58edSkre }
267cc8e58edSkre 
268cc8e58edSkre static intmax_t
binop2(intmax_t a,int op,int precedence,int noeval)269cc8e58edSkre binop2(intmax_t a, int op, int precedence, int noeval)
270cc8e58edSkre {
271cc8e58edSkre 	union a_token_val val;
272cc8e58edSkre 	intmax_t b;
273cc8e58edSkre 	int op2;
274cc8e58edSkre 	int token;
275cc8e58edSkre 
27644443aa0Skre 	VTRACE(DBG_ARITH, ("Arith: binop2 %jd op %d (P:%d)%s\n",
27744443aa0Skre 	    a, op, precedence, noeval ? " noeval" : ""));
27844443aa0Skre 
279cc8e58edSkre 	for (;;) {
280cc8e58edSkre 		token = arith_token();
281cc8e58edSkre 		val = a_t_val;
282cc8e58edSkre 
283cc8e58edSkre 		b = primary(token, &val, arith_token(), noeval);
284cc8e58edSkre 
285cc8e58edSkre 		op2 = last_token;
286cc8e58edSkre 		if (op2 >= ARITH_BINOP_MIN && op2 < ARITH_BINOP_MAX &&
287cc8e58edSkre 		    higher_prec(op2, op)) {
288cc8e58edSkre 			b = binop2(b, op2, arith_prec(op), noeval);
289cc8e58edSkre 			op2 = last_token;
290cc8e58edSkre 		}
291cc8e58edSkre 
292cc8e58edSkre 		a = noeval ? b : do_binop(op, a, b);
293cc8e58edSkre 
294cc8e58edSkre 		if (op2 < ARITH_BINOP_MIN || op2 >= ARITH_BINOP_MAX ||
295cc8e58edSkre 		    arith_prec(op2) >= precedence)
296cc8e58edSkre 			return a;
297cc8e58edSkre 
298cc8e58edSkre 		op = op2;
299cc8e58edSkre 	}
300cc8e58edSkre }
301cc8e58edSkre 
302cc8e58edSkre static intmax_t
binop(int token,union a_token_val * val,int op,int noeval)303cc8e58edSkre binop(int token, union a_token_val *val, int op, int noeval)
304cc8e58edSkre {
305cc8e58edSkre 	intmax_t a = primary(token, val, op, noeval);
306cc8e58edSkre 
307cc8e58edSkre 	op = last_token;
308cc8e58edSkre 	if (op < ARITH_BINOP_MIN || op >= ARITH_BINOP_MAX)
309cc8e58edSkre 		return a;
310cc8e58edSkre 
311cc8e58edSkre 	return binop2(a, op, ARITH_MAX_PREC, noeval);
312cc8e58edSkre }
313cc8e58edSkre 
314cc8e58edSkre static intmax_t
and(int token,union a_token_val * val,int op,int noeval)315cc8e58edSkre and(int token, union a_token_val *val, int op, int noeval)
316cc8e58edSkre {
317cc8e58edSkre 	intmax_t a = binop(token, val, op, noeval);
318cc8e58edSkre 	intmax_t b;
319cc8e58edSkre 
320cc8e58edSkre 	op = last_token;
321cc8e58edSkre 	if (op != ARITH_AND)
322cc8e58edSkre 		return a;
323cc8e58edSkre 
32444443aa0Skre 	VTRACE(DBG_ARITH, ("Arith: AND %jd%s\n", a, noeval ? " noeval" : ""));
32544443aa0Skre 
326cc8e58edSkre 	token = arith_token();
327cc8e58edSkre 	*val = a_t_val;
328cc8e58edSkre 
329cc8e58edSkre 	b = and(token, val, arith_token(), noeval | !a);
330cc8e58edSkre 
331cc8e58edSkre 	return a && b;
332cc8e58edSkre }
333cc8e58edSkre 
334cc8e58edSkre static intmax_t
or(int token,union a_token_val * val,int op,int noeval)335cc8e58edSkre or(int token, union a_token_val *val, int op, int noeval)
336cc8e58edSkre {
337cc8e58edSkre 	intmax_t a = and(token, val, op, noeval);
338cc8e58edSkre 	intmax_t b;
339cc8e58edSkre 
340cc8e58edSkre 	op = last_token;
341cc8e58edSkre 	if (op != ARITH_OR)
342cc8e58edSkre 		return a;
343cc8e58edSkre 
34444443aa0Skre 	VTRACE(DBG_ARITH, ("Arith: OR %jd%s\n", a, noeval ? " noeval" : ""));
34544443aa0Skre 
346cc8e58edSkre 	token = arith_token();
347cc8e58edSkre 	*val = a_t_val;
348cc8e58edSkre 
349cc8e58edSkre 	b = or(token, val, arith_token(), noeval | !!a);
350cc8e58edSkre 
351cc8e58edSkre 	return a || b;
352cc8e58edSkre }
353cc8e58edSkre 
354cc8e58edSkre static intmax_t
cond(int token,union a_token_val * val,int op,int noeval)355cc8e58edSkre cond(int token, union a_token_val *val, int op, int noeval)
356cc8e58edSkre {
357cc8e58edSkre 	intmax_t a = or(token, val, op, noeval);
358cc8e58edSkre 	intmax_t b;
359cc8e58edSkre 	intmax_t c;
360cc8e58edSkre 
361cc8e58edSkre 	if (last_token != ARITH_QMARK)
362cc8e58edSkre 		return a;
363cc8e58edSkre 
36444443aa0Skre 	VTRACE(DBG_ARITH, ("Arith: ?: %jd%s\n", a, noeval ? " noeval" : ""));
36544443aa0Skre 
366cc8e58edSkre 	b = assignment(arith_token(), noeval | !a);
367cc8e58edSkre 
368cc8e58edSkre 	if (last_token != ARITH_COLON)
369cc8e58edSkre 		arith_err("expecting ':'");
370cc8e58edSkre 
371cc8e58edSkre 	token = arith_token();
372cc8e58edSkre 	*val = a_t_val;
373cc8e58edSkre 
374cc8e58edSkre 	c = cond(token, val, arith_token(), noeval | !!a);
375cc8e58edSkre 
376cc8e58edSkre 	return a ? b : c;
377cc8e58edSkre }
378cc8e58edSkre 
379cc8e58edSkre static intmax_t
assignment(int var,int noeval)380cc8e58edSkre assignment(int var, int noeval)
381cc8e58edSkre {
382cc8e58edSkre 	union a_token_val val = a_t_val;
383cc8e58edSkre 	int op = arith_token();
384cc8e58edSkre 	intmax_t result;
385cc8e58edSkre 	char sresult[DIGITS(result) + 1];
386cc8e58edSkre 
387cc8e58edSkre 
388cc8e58edSkre 	if (var != ARITH_VAR)
389cc8e58edSkre 		return cond(var, &val, op, noeval);
390cc8e58edSkre 
391cc8e58edSkre 	if (op != ARITH_ASS && (op < ARITH_ASS_MIN || op >= ARITH_ASS_MAX))
392cc8e58edSkre 		return cond(var, &val, op, noeval);
393cc8e58edSkre 
39444443aa0Skre 	VTRACE(DBG_ARITH, ("Arith: %s ASSIGN %d%s\n", val.name, op,
39544443aa0Skre 	    noeval ? " noeval" : ""));
39644443aa0Skre 
397cc8e58edSkre 	result = assignment(arith_token(), noeval);
398cc8e58edSkre 	if (noeval)
399cc8e58edSkre 		return result;
400cc8e58edSkre 
401cc8e58edSkre 	if (op != ARITH_ASS)
402cc8e58edSkre 		result = do_binop(op - ARITH_ASS_GAP,
403cc8e58edSkre 		    arith_lookupvarint(val.name), result);
404cc8e58edSkre 	snprintf(sresult, sizeof(sresult), ARITH_FORMAT_STR, result);
405cc8e58edSkre 	setvar(val.name, sresult, 0);
406cc8e58edSkre 	return result;
407cc8e58edSkre }
408cc8e58edSkre 
409e79133b5Skre static intmax_t
comma_list(int token,int noeval)410e79133b5Skre comma_list(int token, int noeval)
411e79133b5Skre {
412e79133b5Skre 	intmax_t result = assignment(token, noeval);
413e79133b5Skre 
414e79133b5Skre 	while (last_token == ARITH_COMMA) {
415e79133b5Skre 		VTRACE(DBG_ARITH, ("Arith: comma discarding %jd%s\n", result,
416e79133b5Skre 		    noeval ? " noeval" : ""));
417e79133b5Skre 		result = assignment(arith_token(), noeval);
418e79133b5Skre 	}
419e79133b5Skre 
420e79133b5Skre 	return result;
421e79133b5Skre }
422e79133b5Skre 
423cc8e58edSkre intmax_t
arith(const char * s,int lno)424727a69dcSkre arith(const char *s, int lno)
425cc8e58edSkre {
426cc8e58edSkre 	struct stackmark smark;
427cc8e58edSkre 	intmax_t result;
428727a69dcSkre 	const char *p;
429727a69dcSkre 	int nls = 0;
430cc8e58edSkre 
431cc8e58edSkre 	setstackmark(&smark);
432cc8e58edSkre 
433727a69dcSkre 	arith_lno = lno;
434727a69dcSkre 
435727a69dcSkre 	CTRACE(DBG_ARITH, ("Arith(\"%s\", %d) @%d\n", s, lno, arith_lno));
436727a69dcSkre 
437727a69dcSkre 	/* check if it is possible we might reference LINENO */
438727a69dcSkre 	p = s;
439727a69dcSkre 	while ((p = strchr(p, 'L')) != NULL) {
440727a69dcSkre 		if (p[1] == 'I' && p[2] == 'N') {
441727a69dcSkre 			/* if it is possible, we need to correct airth_lno */
442727a69dcSkre 			p = s;
443727a69dcSkre 			while ((p = strchr(p, '\n')) != NULL)
444727a69dcSkre 				nls++, p++;
445727a69dcSkre 			VTRACE(DBG_ARITH, ("Arith found %d newlines\n", nls));
446727a69dcSkre 			arith_lno -= nls;
447727a69dcSkre 			break;
448727a69dcSkre 		}
449727a69dcSkre 		p++;
450727a69dcSkre 	}
45144443aa0Skre 
452cc8e58edSkre 	arith_buf = arith_startbuf = s;
453cc8e58edSkre 
454e79133b5Skre 	result = comma_list(arith_token(), 0);
455cc8e58edSkre 
456cc8e58edSkre 	if (last_token)
457cc8e58edSkre 		arith_err("expecting end of expression");
458cc8e58edSkre 
459cc8e58edSkre 	popstackmark(&smark);
460cc8e58edSkre 
46144443aa0Skre 	CTRACE(DBG_ARITH, ("Arith result=%jd\n", result));
46244443aa0Skre 
463cc8e58edSkre 	return result;
464cc8e58edSkre }
465cc8e58edSkre 
466cc8e58edSkre /*
467cc8e58edSkre  *  The let(1)/exp(1) builtin.
468cc8e58edSkre  */
469cc8e58edSkre int
expcmd(int argc,char ** argv)470cc8e58edSkre expcmd(int argc, char **argv)
471cc8e58edSkre {
472cc8e58edSkre 	const char *p;
473cc8e58edSkre 	char *concat;
474cc8e58edSkre 	char **ap;
475cc8e58edSkre 	intmax_t i;
476cc8e58edSkre 
477cc8e58edSkre 	if (argc > 1) {
478cc8e58edSkre 		p = argv[1];
479cc8e58edSkre 		if (argc > 2) {
480cc8e58edSkre 			/*
481cc8e58edSkre 			 * Concatenate arguments.
482cc8e58edSkre 			 */
483cc8e58edSkre 			STARTSTACKSTR(concat);
484cc8e58edSkre 			ap = argv + 2;
485cc8e58edSkre 			for (;;) {
486cc8e58edSkre 				while (*p)
487cc8e58edSkre 					STPUTC(*p++, concat);
488cc8e58edSkre 				if ((p = *ap++) == NULL)
489cc8e58edSkre 					break;
490cc8e58edSkre 				STPUTC(' ', concat);
491cc8e58edSkre 			}
492cc8e58edSkre 			STPUTC('\0', concat);
493cc8e58edSkre 			p = grabstackstr(concat);
494cc8e58edSkre 		}
495cc8e58edSkre 	} else
496cc8e58edSkre 		p = "";
497cc8e58edSkre 
498727a69dcSkre 	i = arith(p, line_number);
499cc8e58edSkre 
500cc8e58edSkre 	out1fmt(ARITH_FORMAT_STR "\n", i);
501cc8e58edSkre 	return !i;
502cc8e58edSkre }
503