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