arithmetic.c revision 1.1 1 1.1 kre /* $NetBSD: arithmetic.c,v 1.1 2017/03/20 11:26:07 kre Exp $ */
2 1.1 kre
3 1.1 kre /*-
4 1.1 kre * Copyright (c) 1993
5 1.1 kre * The Regents of the University of California. All rights reserved.
6 1.1 kre * Copyright (c) 2007
7 1.1 kre * Herbert Xu <herbert (at) gondor.apana.org.au>. All rights reserved.
8 1.1 kre *
9 1.1 kre * This code is derived from software contributed to Berkeley by
10 1.1 kre * Kenneth Almquist.
11 1.1 kre *
12 1.1 kre * Redistribution and use in source and binary forms, with or without
13 1.1 kre * modification, are permitted provided that the following conditions
14 1.1 kre * are met:
15 1.1 kre * 1. Redistributions of source code must retain the above copyright
16 1.1 kre * notice, this list of conditions and the following disclaimer.
17 1.1 kre * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 kre * notice, this list of conditions and the following disclaimer in the
19 1.1 kre * documentation and/or other materials provided with the distribution.
20 1.1 kre * 3. Neither the name of the University nor the names of its contributors
21 1.1 kre * may be used to endorse or promote products derived from this software
22 1.1 kre * without specific prior written permission.
23 1.1 kre *
24 1.1 kre * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 kre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 kre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 kre * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 kre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 kre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 kre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 kre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 kre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 kre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 kre * SUCH DAMAGE.
35 1.1 kre *
36 1.1 kre * From FreeBSD, who obtained it from dash, modified both times...
37 1.1 kre */
38 1.1 kre
39 1.1 kre #include <sys/cdefs.h>
40 1.1 kre
41 1.1 kre #ifndef lint
42 1.1 kre __RCSID("$NetBSD: arithmetic.c,v 1.1 2017/03/20 11:26:07 kre Exp $");
43 1.1 kre #endif /* not lint */
44 1.1 kre
45 1.1 kre #include <limits.h>
46 1.1 kre #include <errno.h>
47 1.1 kre #include <inttypes.h>
48 1.1 kre #include <stdlib.h>
49 1.1 kre #include <stdio.h>
50 1.1 kre
51 1.1 kre #include "shell.h"
52 1.1 kre #include "arithmetic.h"
53 1.1 kre #include "arith_tokens.h"
54 1.1 kre #include "expand.h"
55 1.1 kre #include "error.h"
56 1.1 kre #include "memalloc.h"
57 1.1 kre #include "output.h"
58 1.1 kre #include "options.h"
59 1.1 kre #include "var.h"
60 1.1 kre
61 1.1 kre #if ARITH_BOR + ARITH_ASS_GAP != ARITH_BORASS || \
62 1.1 kre ARITH_ASS + ARITH_ASS_GAP != ARITH_EQ
63 1.1 kre #error Arithmetic tokens are out of order.
64 1.1 kre #endif
65 1.1 kre
66 1.1 kre static const char *arith_startbuf;
67 1.1 kre
68 1.1 kre const char *arith_buf;
69 1.1 kre union a_token_val a_t_val;
70 1.1 kre
71 1.1 kre static int last_token;
72 1.1 kre
73 1.1 kre #define ARITH_PRECEDENCE(op, prec) [op - ARITH_BINOP_MIN] = prec
74 1.1 kre
75 1.1 kre static const char prec[ARITH_BINOP_MAX - ARITH_BINOP_MIN] = {
76 1.1 kre ARITH_PRECEDENCE(ARITH_MUL, 0),
77 1.1 kre ARITH_PRECEDENCE(ARITH_DIV, 0),
78 1.1 kre ARITH_PRECEDENCE(ARITH_REM, 0),
79 1.1 kre ARITH_PRECEDENCE(ARITH_ADD, 1),
80 1.1 kre ARITH_PRECEDENCE(ARITH_SUB, 1),
81 1.1 kre ARITH_PRECEDENCE(ARITH_LSHIFT, 2),
82 1.1 kre ARITH_PRECEDENCE(ARITH_RSHIFT, 2),
83 1.1 kre ARITH_PRECEDENCE(ARITH_LT, 3),
84 1.1 kre ARITH_PRECEDENCE(ARITH_LE, 3),
85 1.1 kre ARITH_PRECEDENCE(ARITH_GT, 3),
86 1.1 kre ARITH_PRECEDENCE(ARITH_GE, 3),
87 1.1 kre ARITH_PRECEDENCE(ARITH_EQ, 4),
88 1.1 kre ARITH_PRECEDENCE(ARITH_NE, 4),
89 1.1 kre ARITH_PRECEDENCE(ARITH_BAND, 5),
90 1.1 kre ARITH_PRECEDENCE(ARITH_BXOR, 6),
91 1.1 kre ARITH_PRECEDENCE(ARITH_BOR, 7),
92 1.1 kre };
93 1.1 kre
94 1.1 kre #define ARITH_MAX_PREC 8
95 1.1 kre
96 1.1 kre int expcmd(int, char **);
97 1.1 kre
98 1.1 kre static void __dead
99 1.1 kre arith_err(const char *s)
100 1.1 kre {
101 1.1 kre error("arithmetic expression: %s: \"%s\"", s, arith_startbuf);
102 1.1 kre /* NOTREACHED */
103 1.1 kre }
104 1.1 kre
105 1.1 kre static intmax_t
106 1.1 kre arith_lookupvarint(char *varname)
107 1.1 kre {
108 1.1 kre const char *str;
109 1.1 kre char *p;
110 1.1 kre intmax_t result;
111 1.1 kre
112 1.1 kre str = lookupvar(varname);
113 1.1 kre if (uflag && str == NULL)
114 1.1 kre arith_err("variable not set");
115 1.1 kre if (str == NULL || *str == '\0')
116 1.1 kre str = "0";
117 1.1 kre errno = 0;
118 1.1 kre result = strtoimax(str, &p, 0);
119 1.1 kre if (errno != 0 || *p != '\0')
120 1.1 kre arith_err("variable contains non-numeric value");
121 1.1 kre return result;
122 1.1 kre }
123 1.1 kre
124 1.1 kre static inline int
125 1.1 kre arith_prec(int op)
126 1.1 kre {
127 1.1 kre
128 1.1 kre return prec[op - ARITH_BINOP_MIN];
129 1.1 kre }
130 1.1 kre
131 1.1 kre static inline int
132 1.1 kre higher_prec(int op1, int op2)
133 1.1 kre {
134 1.1 kre
135 1.1 kre return arith_prec(op1) < arith_prec(op2);
136 1.1 kre }
137 1.1 kre
138 1.1 kre static intmax_t
139 1.1 kre do_binop(int op, intmax_t a, intmax_t b)
140 1.1 kre {
141 1.1 kre
142 1.1 kre switch (op) {
143 1.1 kre default:
144 1.1 kre arith_err("token error");
145 1.1 kre case ARITH_REM:
146 1.1 kre case ARITH_DIV:
147 1.1 kre if (b == 0)
148 1.1 kre arith_err("division by zero");
149 1.1 kre if (a == INTMAX_MIN && b == -1)
150 1.1 kre arith_err("divide error");
151 1.1 kre return op == ARITH_REM ? a % b : a / b;
152 1.1 kre case ARITH_MUL:
153 1.1 kre return (uintmax_t)a * (uintmax_t)b;
154 1.1 kre case ARITH_ADD:
155 1.1 kre return (uintmax_t)a + (uintmax_t)b;
156 1.1 kre case ARITH_SUB:
157 1.1 kre return (uintmax_t)a - (uintmax_t)b;
158 1.1 kre case ARITH_LSHIFT:
159 1.1 kre return (uintmax_t)a << (b & (sizeof(uintmax_t) * CHAR_BIT - 1));
160 1.1 kre case ARITH_RSHIFT:
161 1.1 kre return a >> (b & (sizeof(uintmax_t) * CHAR_BIT - 1));
162 1.1 kre case ARITH_LT:
163 1.1 kre return a < b;
164 1.1 kre case ARITH_LE:
165 1.1 kre return a <= b;
166 1.1 kre case ARITH_GT:
167 1.1 kre return a > b;
168 1.1 kre case ARITH_GE:
169 1.1 kre return a >= b;
170 1.1 kre case ARITH_EQ:
171 1.1 kre return a == b;
172 1.1 kre case ARITH_NE:
173 1.1 kre return a != b;
174 1.1 kre case ARITH_BAND:
175 1.1 kre return a & b;
176 1.1 kre case ARITH_BXOR:
177 1.1 kre return a ^ b;
178 1.1 kre case ARITH_BOR:
179 1.1 kre return a | b;
180 1.1 kre }
181 1.1 kre }
182 1.1 kre
183 1.1 kre static intmax_t assignment(int var, int noeval);
184 1.1 kre
185 1.1 kre static intmax_t
186 1.1 kre primary(int token, union a_token_val *val, int op, int noeval)
187 1.1 kre {
188 1.1 kre intmax_t result;
189 1.1 kre
190 1.1 kre switch (token) {
191 1.1 kre case ARITH_LPAREN:
192 1.1 kre result = assignment(op, noeval);
193 1.1 kre if (last_token != ARITH_RPAREN)
194 1.1 kre arith_err("expecting ')'");
195 1.1 kre last_token = arith_token();
196 1.1 kre return result;
197 1.1 kre case ARITH_NUM:
198 1.1 kre last_token = op;
199 1.1 kre return val->val;
200 1.1 kre case ARITH_VAR:
201 1.1 kre last_token = op;
202 1.1 kre return noeval ? val->val : arith_lookupvarint(val->name);
203 1.1 kre case ARITH_ADD:
204 1.1 kre *val = a_t_val;
205 1.1 kre return primary(op, val, arith_token(), noeval);
206 1.1 kre case ARITH_SUB:
207 1.1 kre *val = a_t_val;
208 1.1 kre return -primary(op, val, arith_token(), noeval);
209 1.1 kre case ARITH_NOT:
210 1.1 kre *val = a_t_val;
211 1.1 kre return !primary(op, val, arith_token(), noeval);
212 1.1 kre case ARITH_BNOT:
213 1.1 kre *val = a_t_val;
214 1.1 kre return ~primary(op, val, arith_token(), noeval);
215 1.1 kre default:
216 1.1 kre arith_err("expecting primary");
217 1.1 kre }
218 1.1 kre return 0; /* never reached */
219 1.1 kre }
220 1.1 kre
221 1.1 kre static intmax_t
222 1.1 kre binop2(intmax_t a, int op, int precedence, int noeval)
223 1.1 kre {
224 1.1 kre union a_token_val val;
225 1.1 kre intmax_t b;
226 1.1 kre int op2;
227 1.1 kre int token;
228 1.1 kre
229 1.1 kre for (;;) {
230 1.1 kre token = arith_token();
231 1.1 kre val = a_t_val;
232 1.1 kre
233 1.1 kre b = primary(token, &val, arith_token(), noeval);
234 1.1 kre
235 1.1 kre op2 = last_token;
236 1.1 kre if (op2 >= ARITH_BINOP_MIN && op2 < ARITH_BINOP_MAX &&
237 1.1 kre higher_prec(op2, op)) {
238 1.1 kre b = binop2(b, op2, arith_prec(op), noeval);
239 1.1 kre op2 = last_token;
240 1.1 kre }
241 1.1 kre
242 1.1 kre a = noeval ? b : do_binop(op, a, b);
243 1.1 kre
244 1.1 kre if (op2 < ARITH_BINOP_MIN || op2 >= ARITH_BINOP_MAX ||
245 1.1 kre arith_prec(op2) >= precedence)
246 1.1 kre return a;
247 1.1 kre
248 1.1 kre op = op2;
249 1.1 kre }
250 1.1 kre }
251 1.1 kre
252 1.1 kre static intmax_t
253 1.1 kre binop(int token, union a_token_val *val, int op, int noeval)
254 1.1 kre {
255 1.1 kre intmax_t a = primary(token, val, op, noeval);
256 1.1 kre
257 1.1 kre op = last_token;
258 1.1 kre if (op < ARITH_BINOP_MIN || op >= ARITH_BINOP_MAX)
259 1.1 kre return a;
260 1.1 kre
261 1.1 kre return binop2(a, op, ARITH_MAX_PREC, noeval);
262 1.1 kre }
263 1.1 kre
264 1.1 kre static intmax_t
265 1.1 kre and(int token, union a_token_val *val, int op, int noeval)
266 1.1 kre {
267 1.1 kre intmax_t a = binop(token, val, op, noeval);
268 1.1 kre intmax_t b;
269 1.1 kre
270 1.1 kre op = last_token;
271 1.1 kre if (op != ARITH_AND)
272 1.1 kre return a;
273 1.1 kre
274 1.1 kre token = arith_token();
275 1.1 kre *val = a_t_val;
276 1.1 kre
277 1.1 kre b = and(token, val, arith_token(), noeval | !a);
278 1.1 kre
279 1.1 kre return a && b;
280 1.1 kre }
281 1.1 kre
282 1.1 kre static intmax_t
283 1.1 kre or(int token, union a_token_val *val, int op, int noeval)
284 1.1 kre {
285 1.1 kre intmax_t a = and(token, val, op, noeval);
286 1.1 kre intmax_t b;
287 1.1 kre
288 1.1 kre op = last_token;
289 1.1 kre if (op != ARITH_OR)
290 1.1 kre return a;
291 1.1 kre
292 1.1 kre token = arith_token();
293 1.1 kre *val = a_t_val;
294 1.1 kre
295 1.1 kre b = or(token, val, arith_token(), noeval | !!a);
296 1.1 kre
297 1.1 kre return a || b;
298 1.1 kre }
299 1.1 kre
300 1.1 kre static intmax_t
301 1.1 kre cond(int token, union a_token_val *val, int op, int noeval)
302 1.1 kre {
303 1.1 kre intmax_t a = or(token, val, op, noeval);
304 1.1 kre intmax_t b;
305 1.1 kre intmax_t c;
306 1.1 kre
307 1.1 kre if (last_token != ARITH_QMARK)
308 1.1 kre return a;
309 1.1 kre
310 1.1 kre b = assignment(arith_token(), noeval | !a);
311 1.1 kre
312 1.1 kre if (last_token != ARITH_COLON)
313 1.1 kre arith_err("expecting ':'");
314 1.1 kre
315 1.1 kre token = arith_token();
316 1.1 kre *val = a_t_val;
317 1.1 kre
318 1.1 kre c = cond(token, val, arith_token(), noeval | !!a);
319 1.1 kre
320 1.1 kre return a ? b : c;
321 1.1 kre }
322 1.1 kre
323 1.1 kre static intmax_t
324 1.1 kre assignment(int var, int noeval)
325 1.1 kre {
326 1.1 kre union a_token_val val = a_t_val;
327 1.1 kre int op = arith_token();
328 1.1 kre intmax_t result;
329 1.1 kre char sresult[DIGITS(result) + 1];
330 1.1 kre
331 1.1 kre
332 1.1 kre if (var != ARITH_VAR)
333 1.1 kre return cond(var, &val, op, noeval);
334 1.1 kre
335 1.1 kre if (op != ARITH_ASS && (op < ARITH_ASS_MIN || op >= ARITH_ASS_MAX))
336 1.1 kre return cond(var, &val, op, noeval);
337 1.1 kre
338 1.1 kre result = assignment(arith_token(), noeval);
339 1.1 kre if (noeval)
340 1.1 kre return result;
341 1.1 kre
342 1.1 kre if (op != ARITH_ASS)
343 1.1 kre result = do_binop(op - ARITH_ASS_GAP,
344 1.1 kre arith_lookupvarint(val.name), result);
345 1.1 kre snprintf(sresult, sizeof(sresult), ARITH_FORMAT_STR, result);
346 1.1 kre setvar(val.name, sresult, 0);
347 1.1 kre return result;
348 1.1 kre }
349 1.1 kre
350 1.1 kre intmax_t
351 1.1 kre arith(const char *s)
352 1.1 kre {
353 1.1 kre struct stackmark smark;
354 1.1 kre intmax_t result;
355 1.1 kre
356 1.1 kre setstackmark(&smark);
357 1.1 kre
358 1.1 kre arith_buf = arith_startbuf = s;
359 1.1 kre
360 1.1 kre result = assignment(arith_token(), 0);
361 1.1 kre
362 1.1 kre if (last_token)
363 1.1 kre arith_err("expecting end of expression");
364 1.1 kre
365 1.1 kre popstackmark(&smark);
366 1.1 kre
367 1.1 kre return result;
368 1.1 kre }
369 1.1 kre
370 1.1 kre /*
371 1.1 kre * The let(1)/exp(1) builtin.
372 1.1 kre */
373 1.1 kre int
374 1.1 kre expcmd(int argc, char **argv)
375 1.1 kre {
376 1.1 kre const char *p;
377 1.1 kre char *concat;
378 1.1 kre char **ap;
379 1.1 kre intmax_t i;
380 1.1 kre
381 1.1 kre if (argc > 1) {
382 1.1 kre p = argv[1];
383 1.1 kre if (argc > 2) {
384 1.1 kre /*
385 1.1 kre * Concatenate arguments.
386 1.1 kre */
387 1.1 kre STARTSTACKSTR(concat);
388 1.1 kre ap = argv + 2;
389 1.1 kre for (;;) {
390 1.1 kre while (*p)
391 1.1 kre STPUTC(*p++, concat);
392 1.1 kre if ((p = *ap++) == NULL)
393 1.1 kre break;
394 1.1 kre STPUTC(' ', concat);
395 1.1 kre }
396 1.1 kre STPUTC('\0', concat);
397 1.1 kre p = grabstackstr(concat);
398 1.1 kre }
399 1.1 kre } else
400 1.1 kre p = "";
401 1.1 kre
402 1.1 kre i = arith(p);
403 1.1 kre
404 1.1 kre out1fmt(ARITH_FORMAT_STR "\n", i);
405 1.1 kre return !i;
406 1.1 kre }
407