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