blob: 76d22c9bd51633cbcbf46d398d5069425fbfcdbb [file] [log] [blame]
Mike Frysinger98c52642009-04-02 10:02:37 +00001/*
Denys Vlasenkobed7c812010-09-16 11:50:46 +02002 * Arithmetic code ripped out of ash shell for code sharing.
Mike Frysinger98c52642009-04-02 10:02:37 +00003 *
Denys Vlasenko73067272010-01-12 22:11:24 +01004 * This code is derived from software contributed to Berkeley by
5 * Kenneth Almquist.
6 *
7 * Original BSD copyright notice is retained at the end of this file.
8 *
Mike Frysinger98c52642009-04-02 10:02:37 +00009 * Copyright (c) 1989, 1991, 1993, 1994
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
13 * was re-ported from NetBSD and debianized.
14 *
Mike Frysinger98c52642009-04-02 10:02:37 +000015 * rewrite arith.y to micro stack based cryptic algorithm by
16 * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
17 *
18 * Modified by Paul Mundt <lethal@linux-sh.org> (c) 2004 to support
19 * dynamic variables.
20 *
21 * Modified by Vladimir Oleynik <dzo@simtreas.ru> (c) 2001-2005 to be
22 * used in busybox and size optimizations,
23 * rewrote arith (see notes to this), added locale support,
24 * rewrote dynamic variables.
Denys Vlasenko73067272010-01-12 22:11:24 +010025 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020026 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysinger98c52642009-04-02 10:02:37 +000027 */
Mike Frysinger98c52642009-04-02 10:02:37 +000028/* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
Denys Vlasenko063847d2010-09-15 13:33:02 +020029 *
30 * Permission is hereby granted, free of charge, to any person obtaining
31 * a copy of this software and associated documentation files (the
32 * "Software"), to deal in the Software without restriction, including
33 * without limitation the rights to use, copy, modify, merge, publish,
34 * distribute, sublicense, and/or sell copies of the Software, and to
35 * permit persons to whom the Software is furnished to do so, subject to
36 * the following conditions:
37 *
38 * The above copyright notice and this permission notice shall be
39 * included in all copies or substantial portions of the Software.
40 *
41 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 */
Mike Frysinger98c52642009-04-02 10:02:37 +000049
50/* This is my infix parser/evaluator. It is optimized for size, intended
51 * as a replacement for yacc-based parsers. However, it may well be faster
52 * than a comparable parser written in yacc. The supported operators are
53 * listed in #defines below. Parens, order of operations, and error handling
54 * are supported. This code is thread safe. The exact expression format should
Denys Vlasenkobd147702010-09-13 11:11:40 +020055 * be that which POSIX specifies for shells.
56 *
57 * The code uses a simple two-stack algorithm. See
Mike Frysinger98c52642009-04-02 10:02:37 +000058 * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html
59 * for a detailed explanation of the infix-to-postfix algorithm on which
60 * this is based (this code differs in that it applies operators immediately
61 * to the stack instead of adding them to a queue to end up with an
Denys Vlasenkobd147702010-09-13 11:11:40 +020062 * expression).
Denys Vlasenkobd147702010-09-13 11:11:40 +020063 */
Mike Frysinger98c52642009-04-02 10:02:37 +000064
65/*
66 * Aug 24, 2001 Manuel Novoa III
67 *
68 * Reduced the generated code size by about 30% (i386) and fixed several bugs.
69 *
70 * 1) In arith_apply():
71 * a) Cached values of *numptr and &(numptr[-1]).
72 * b) Removed redundant test for zero denominator.
73 *
74 * 2) In arith():
75 * a) Eliminated redundant code for processing operator tokens by moving
76 * to a table-based implementation. Also folded handling of parens
77 * into the table.
78 * b) Combined all 3 loops which called arith_apply to reduce generated
79 * code size at the cost of speed.
80 *
81 * 3) The following expressions were treated as valid by the original code:
82 * 1() , 0! , 1 ( *3 ) .
83 * These bugs have been fixed by internally enclosing the expression in
84 * parens and then checking that all binary ops and right parens are
85 * preceded by a valid expression (NUM_TOKEN).
86 *
87 * Note: It may be desirable to replace Aaron's test for whitespace with
88 * ctype's isspace() if it is used by another busybox applet or if additional
89 * whitespace chars should be considered. Look below the "#include"s for a
90 * precompiler test.
91 */
Mike Frysinger98c52642009-04-02 10:02:37 +000092/*
93 * Aug 26, 2001 Manuel Novoa III
94 *
95 * Return 0 for null expressions. Pointed out by Vladimir Oleynik.
96 *
97 * Merge in Aaron's comments previously posted to the busybox list,
98 * modified slightly to take account of my changes to the code.
99 *
100 */
Mike Frysinger98c52642009-04-02 10:02:37 +0000101/*
102 * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
103 *
104 * - allow access to variable,
Denys Vlasenkobd147702010-09-13 11:11:40 +0200105 * use recursive value indirection: c="2*2"; a="c"; echo $((a+=2)) produce 6
106 * - implement assign syntax (VAR=expr, +=, *= etc)
107 * - implement exponentiation (** operator)
108 * - implement comma separated - expr, expr
109 * - implement ++expr --expr expr++ expr--
110 * - implement expr ? expr : expr (but second expr is always calculated)
Mike Frysinger98c52642009-04-02 10:02:37 +0000111 * - allow hexadecimal and octal numbers
Denys Vlasenkobd147702010-09-13 11:11:40 +0200112 * - restore lost XOR operator
113 * - protect $((num num)) as true zero expr (Manuel's error)
Mike Frysinger98c52642009-04-02 10:02:37 +0000114 * - always use special isspace(), see comment from bash ;-)
115 */
Denys Vlasenko03dad222010-01-12 23:29:57 +0100116#include "libbb.h"
117#include "math.h"
118
Mike Frysinger98c52642009-04-02 10:02:37 +0000119typedef unsigned char operator;
120
121/* An operator's token id is a bit of a bitfield. The lower 5 bits are the
122 * precedence, and 3 high bits are an ID unique across operators of that
123 * precedence. The ID portion is so that multiple operators can have the
124 * same precedence, ensuring that the leftmost one is evaluated first.
Denys Vlasenkobd147702010-09-13 11:11:40 +0200125 * Consider * and /
126 */
127#define tok_decl(prec,id) (((id)<<5) | (prec))
128#define PREC(op) ((op) & 0x1F)
Mike Frysinger98c52642009-04-02 10:02:37 +0000129
Denys Vlasenkobd147702010-09-13 11:11:40 +0200130#define TOK_LPAREN tok_decl(0,0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000131
Denys Vlasenkobd147702010-09-13 11:11:40 +0200132#define TOK_COMMA tok_decl(1,0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000133
Denys Vlasenkobd147702010-09-13 11:11:40 +0200134/* All assignments are right associative and have the same precedence,
135 * but there are 11 of them, which doesn't fit into 3 bits for unique id.
136 * Abusing another precedence level:
137 */
138#define TOK_ASSIGN tok_decl(2,0)
139#define TOK_AND_ASSIGN tok_decl(2,1)
140#define TOK_OR_ASSIGN tok_decl(2,2)
141#define TOK_XOR_ASSIGN tok_decl(2,3)
142#define TOK_PLUS_ASSIGN tok_decl(2,4)
143#define TOK_MINUS_ASSIGN tok_decl(2,5)
144#define TOK_LSHIFT_ASSIGN tok_decl(2,6)
145#define TOK_RSHIFT_ASSIGN tok_decl(2,7)
Mike Frysinger98c52642009-04-02 10:02:37 +0000146
Denys Vlasenkobd147702010-09-13 11:11:40 +0200147#define TOK_MUL_ASSIGN tok_decl(3,0)
148#define TOK_DIV_ASSIGN tok_decl(3,1)
149#define TOK_REM_ASSIGN tok_decl(3,2)
Mike Frysinger98c52642009-04-02 10:02:37 +0000150
Denys Vlasenkobd147702010-09-13 11:11:40 +0200151#define fix_assignment_prec(prec) do { if (prec == 3) prec = 2; } while (0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000152
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200153/* Ternary conditional operator is right associative too */
Denys Vlasenkobd147702010-09-13 11:11:40 +0200154#define TOK_CONDITIONAL tok_decl(4,0)
155#define TOK_CONDITIONAL_SEP tok_decl(4,1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000156
Denys Vlasenkobd147702010-09-13 11:11:40 +0200157#define TOK_OR tok_decl(5,0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000158
Denys Vlasenkobd147702010-09-13 11:11:40 +0200159#define TOK_AND tok_decl(6,0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000160
Denys Vlasenkobd147702010-09-13 11:11:40 +0200161#define TOK_BOR tok_decl(7,0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000162
Denys Vlasenkobd147702010-09-13 11:11:40 +0200163#define TOK_BXOR tok_decl(8,0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000164
Denys Vlasenkobd147702010-09-13 11:11:40 +0200165#define TOK_BAND tok_decl(9,0)
Mike Frysinger98c52642009-04-02 10:02:37 +0000166
Denys Vlasenkobd147702010-09-13 11:11:40 +0200167#define TOK_EQ tok_decl(10,0)
168#define TOK_NE tok_decl(10,1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000169
Denys Vlasenkobd147702010-09-13 11:11:40 +0200170#define TOK_LT tok_decl(11,0)
171#define TOK_GT tok_decl(11,1)
172#define TOK_GE tok_decl(11,2)
173#define TOK_LE tok_decl(11,3)
Mike Frysinger98c52642009-04-02 10:02:37 +0000174
Denys Vlasenkobd147702010-09-13 11:11:40 +0200175#define TOK_LSHIFT tok_decl(12,0)
176#define TOK_RSHIFT tok_decl(12,1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000177
Denys Vlasenkobd147702010-09-13 11:11:40 +0200178#define TOK_ADD tok_decl(13,0)
179#define TOK_SUB tok_decl(13,1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000180
Denys Vlasenkobd147702010-09-13 11:11:40 +0200181#define TOK_MUL tok_decl(14,0)
182#define TOK_DIV tok_decl(14,1)
183#define TOK_REM tok_decl(14,2)
Mike Frysinger98c52642009-04-02 10:02:37 +0000184
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200185/* Exponent is right associative */
Denys Vlasenkobd147702010-09-13 11:11:40 +0200186#define TOK_EXPONENT tok_decl(15,1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000187
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200188/* Unary operators */
Denys Vlasenkobd147702010-09-13 11:11:40 +0200189#define UNARYPREC 16
190#define TOK_BNOT tok_decl(UNARYPREC,0)
191#define TOK_NOT tok_decl(UNARYPREC,1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000192
Denys Vlasenkobd147702010-09-13 11:11:40 +0200193#define TOK_UMINUS tok_decl(UNARYPREC+1,0)
194#define TOK_UPLUS tok_decl(UNARYPREC+1,1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000195
Denys Vlasenkobd147702010-09-13 11:11:40 +0200196#define PREC_PRE (UNARYPREC+2)
Mike Frysinger98c52642009-04-02 10:02:37 +0000197
Denys Vlasenkobd147702010-09-13 11:11:40 +0200198#define TOK_PRE_INC tok_decl(PREC_PRE, 0)
199#define TOK_PRE_DEC tok_decl(PREC_PRE, 1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000200
Denys Vlasenkobd147702010-09-13 11:11:40 +0200201#define PREC_POST (UNARYPREC+3)
Mike Frysinger98c52642009-04-02 10:02:37 +0000202
Denys Vlasenkobd147702010-09-13 11:11:40 +0200203#define TOK_POST_INC tok_decl(PREC_POST, 0)
204#define TOK_POST_DEC tok_decl(PREC_POST, 1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000205
Denys Vlasenkobd147702010-09-13 11:11:40 +0200206#define SPEC_PREC (UNARYPREC+4)
Mike Frysinger98c52642009-04-02 10:02:37 +0000207
Denys Vlasenkobd147702010-09-13 11:11:40 +0200208#define TOK_NUM tok_decl(SPEC_PREC, 0)
209#define TOK_RPAREN tok_decl(SPEC_PREC, 1)
Mike Frysinger98c52642009-04-02 10:02:37 +0000210
211static int
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200212is_assign_op(operator op)
Mike Frysinger98c52642009-04-02 10:02:37 +0000213{
214 operator prec = PREC(op);
Denys Vlasenkobd147702010-09-13 11:11:40 +0200215 fix_assignment_prec(prec);
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200216 return prec == PREC(TOK_ASSIGN)
217 || prec == PREC_PRE
218 || prec == PREC_POST;
Mike Frysinger98c52642009-04-02 10:02:37 +0000219}
220
221static int
Denys Vlasenkobd147702010-09-13 11:11:40 +0200222is_right_associative(operator prec)
Mike Frysinger98c52642009-04-02 10:02:37 +0000223{
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200224 return prec == PREC(TOK_ASSIGN)
225 || prec == PREC(TOK_EXPONENT)
226 || prec == PREC(TOK_CONDITIONAL);
Mike Frysinger98c52642009-04-02 10:02:37 +0000227}
228
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200229
Mike Frysinger98c52642009-04-02 10:02:37 +0000230typedef struct {
231 arith_t val;
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200232 /* We acquire second_val only when "expr1 : expr2" part
233 * of ternary ?: op is evaluated.
234 * We treat ?: as two binary ops: (expr ? (expr1 : expr2)).
235 * ':' produces a new value which has two parts, val and second_val;
236 * then '?' selects one of them based on its left side.
237 */
238 arith_t second_val;
239 char second_val_present;
240 /* If NULL then it's just a number, else it's a named variable */
241 char *var;
242} var_or_num_t;
Mike Frysinger98c52642009-04-02 10:02:37 +0000243
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200244typedef struct remembered_name {
245 struct remembered_name *next;
Mike Frysinger98c52642009-04-02 10:02:37 +0000246 const char *var;
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200247} remembered_name;
Mike Frysinger98c52642009-04-02 10:02:37 +0000248
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200249
Denys Vlasenko841878e2020-10-01 00:57:05 +0200250static arith_t
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200251evaluate_string(arith_state_t *math_state, const char *expr);
Mike Frysinger98c52642009-04-02 10:02:37 +0000252
Denys Vlasenko063847d2010-09-15 13:33:02 +0200253static const char*
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200254arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
Mike Frysinger98c52642009-04-02 10:02:37 +0000255{
256 if (t->var) {
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200257 const char *p = math_state->lookupvar(t->var);
Mike Frysinger98c52642009-04-02 10:02:37 +0000258 if (p) {
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200259 remembered_name *cur;
260 remembered_name cur_save;
Mike Frysinger98c52642009-04-02 10:02:37 +0000261
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200262 /* did we already see this name?
263 * testcase: a=b; b=a; echo $((a))
264 */
265 for (cur = math_state->list_of_recursed_names; cur; cur = cur->next) {
Mike Frysinger98c52642009-04-02 10:02:37 +0000266 if (strcmp(cur->var, t->var) == 0) {
Denys Vlasenko063847d2010-09-15 13:33:02 +0200267 /* Yes */
268 return "expression recursion loop detected";
Mike Frysinger98c52642009-04-02 10:02:37 +0000269 }
270 }
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200271
272 /* push current var name */
273 cur = math_state->list_of_recursed_names;
Mike Frysinger98c52642009-04-02 10:02:37 +0000274 cur_save.var = t->var;
275 cur_save.next = cur;
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200276 math_state->list_of_recursed_names = &cur_save;
Mike Frysinger98c52642009-04-02 10:02:37 +0000277
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200278 /* recursively evaluate p as expression */
279 t->val = evaluate_string(math_state, p);
280
281 /* pop current var name */
282 math_state->list_of_recursed_names = cur;
283
Denys Vlasenko063847d2010-09-15 13:33:02 +0200284 return math_state->errmsg;
Mike Frysinger98c52642009-04-02 10:02:37 +0000285 }
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200286 /* treat undefined var as 0 */
Mike Frysinger98c52642009-04-02 10:02:37 +0000287 t->val = 0;
288 }
289 return 0;
290}
291
Denys Vlasenkobd147702010-09-13 11:11:40 +0200292/* "Applying" a token means performing it on the top elements on the integer
293 * stack. For an unary operator it will only change the top element, but a
294 * binary operator will pop two arguments and push the result */
Denys Vlasenko063847d2010-09-15 13:33:02 +0200295static NOINLINE const char*
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200296arith_apply(arith_state_t *math_state, operator op, var_or_num_t *numstack, var_or_num_t **numstackptr)
Mike Frysinger98c52642009-04-02 10:02:37 +0000297{
Denys Vlasenkobd147702010-09-13 11:11:40 +0200298#define NUMPTR (*numstackptr)
299
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200300 var_or_num_t *top_of_stack;
301 arith_t rez;
Denys Vlasenko063847d2010-09-15 13:33:02 +0200302 const char *err;
Mike Frysinger98c52642009-04-02 10:02:37 +0000303
304 /* There is no operator that can work without arguments */
Denys Vlasenkobd147702010-09-13 11:11:40 +0200305 if (NUMPTR == numstack)
306 goto err;
Mike Frysinger98c52642009-04-02 10:02:37 +0000307
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200308 top_of_stack = NUMPTR - 1;
309
310 /* Resolve name to value, if needed */
311 err = arith_lookup_val(math_state, top_of_stack);
Denys Vlasenko06d44d72010-09-13 12:49:03 +0200312 if (err)
313 return err;
Mike Frysinger98c52642009-04-02 10:02:37 +0000314
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200315 rez = top_of_stack->val;
Mike Frysinger98c52642009-04-02 10:02:37 +0000316 if (op == TOK_UMINUS)
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200317 rez = -rez;
Mike Frysinger98c52642009-04-02 10:02:37 +0000318 else if (op == TOK_NOT)
319 rez = !rez;
320 else if (op == TOK_BNOT)
321 rez = ~rez;
322 else if (op == TOK_POST_INC || op == TOK_PRE_INC)
323 rez++;
324 else if (op == TOK_POST_DEC || op == TOK_PRE_DEC)
325 rez--;
326 else if (op != TOK_UPLUS) {
327 /* Binary operators */
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200328 arith_t right_side_val;
329 char bad_second_val;
Mike Frysinger98c52642009-04-02 10:02:37 +0000330
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200331 /* Binary operators need two arguments */
332 if (top_of_stack == numstack)
Mike Frysinger98c52642009-04-02 10:02:37 +0000333 goto err;
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200334 /* ...and they pop one */
335 NUMPTR = top_of_stack; /* this decrements NUMPTR */
336
337 bad_second_val = top_of_stack->second_val_present;
338 if (op == TOK_CONDITIONAL) { /* ? operation */
339 /* Make next if (...) protect against
340 * $((expr1 ? expr2)) - that is, missing ": expr" */
341 bad_second_val = !bad_second_val;
Mike Frysinger98c52642009-04-02 10:02:37 +0000342 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200343 if (bad_second_val) {
344 /* Protect against $((expr <not_?_op> expr1 : expr2)) */
345 return "malformed ?: operator";
346 }
347
348 top_of_stack--; /* now points to left side */
349
Mike Frysinger98c52642009-04-02 10:02:37 +0000350 if (op != TOK_ASSIGN) {
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200351 /* Resolve left side value (unless the op is '=') */
352 err = arith_lookup_val(math_state, top_of_stack);
Denys Vlasenko06d44d72010-09-13 12:49:03 +0200353 if (err)
354 return err;
Mike Frysinger98c52642009-04-02 10:02:37 +0000355 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200356
357 right_side_val = rez;
358 rez = top_of_stack->val;
359 if (op == TOK_CONDITIONAL) /* ? operation */
360 rez = (rez ? right_side_val : top_of_stack[1].second_val);
361 else if (op == TOK_CONDITIONAL_SEP) { /* : operation */
362 if (top_of_stack == numstack) {
363 /* Protect against $((expr : expr)) */
364 return "malformed ?: operator";
Mike Frysinger98c52642009-04-02 10:02:37 +0000365 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200366 top_of_stack->second_val_present = op;
367 top_of_stack->second_val = right_side_val;
368 }
369 else if (op == TOK_BOR || op == TOK_OR_ASSIGN)
370 rez |= right_side_val;
371 else if (op == TOK_OR)
372 rez = right_side_val || rez;
373 else if (op == TOK_BAND || op == TOK_AND_ASSIGN)
374 rez &= right_side_val;
375 else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN)
376 rez ^= right_side_val;
377 else if (op == TOK_AND)
378 rez = rez && right_side_val;
379 else if (op == TOK_EQ)
380 rez = (rez == right_side_val);
381 else if (op == TOK_NE)
382 rez = (rez != right_side_val);
383 else if (op == TOK_GE)
384 rez = (rez >= right_side_val);
385 else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN)
386 rez >>= right_side_val;
387 else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN)
388 rez <<= right_side_val;
389 else if (op == TOK_GT)
390 rez = (rez > right_side_val);
391 else if (op == TOK_LT)
392 rez = (rez < right_side_val);
393 else if (op == TOK_LE)
394 rez = (rez <= right_side_val);
395 else if (op == TOK_MUL || op == TOK_MUL_ASSIGN)
396 rez *= right_side_val;
397 else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN)
398 rez += right_side_val;
399 else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN)
400 rez -= right_side_val;
401 else if (op == TOK_ASSIGN || op == TOK_COMMA)
402 rez = right_side_val;
403 else if (op == TOK_EXPONENT) {
Denys Vlasenkobd147702010-09-13 11:11:40 +0200404 arith_t c;
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200405 if (right_side_val < 0)
Denys Vlasenko063847d2010-09-15 13:33:02 +0200406 return "exponent less than 0";
Denys Vlasenkobd147702010-09-13 11:11:40 +0200407 c = 1;
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200408 while (--right_side_val >= 0)
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100409 c *= rez;
Denys Vlasenkobd147702010-09-13 11:11:40 +0200410 rez = c;
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200411 }
412 else if (right_side_val == 0)
Denys Vlasenko063847d2010-09-15 13:33:02 +0200413 return "divide by zero";
Denys Vlasenko8a475de2014-11-18 14:32:58 +0100414 else if (op == TOK_DIV || op == TOK_DIV_ASSIGN
415 || op == TOK_REM || op == TOK_REM_ASSIGN) {
416 /*
417 * bash 4.2.45 x86 64bit: SEGV on 'echo $((2**63 / -1))'
418 *
419 * MAX_NEGATIVE_INT / -1 = MAX_POSITIVE_INT+1
420 * and thus is not representable.
421 * Some CPUs segfault trying such op.
Denys Vlasenkof5add442014-11-20 01:43:30 +0100422 * Others overflow MAX_POSITIVE_INT+1 to
Denys Vlasenko8a475de2014-11-18 14:32:58 +0100423 * MAX_NEGATIVE_INT (0x7fff+1 = 0x8000).
424 * Make sure to at least not SEGV here:
425 */
426 if (right_side_val == -1
427 && rez << 1 == 0 /* MAX_NEGATIVE_INT or 0 */
428 ) {
429 right_side_val = 1;
430 }
431 if (op == TOK_DIV || op == TOK_DIV_ASSIGN)
432 rez /= right_side_val;
433 else {
434 rez %= right_side_val;
435 }
436 }
Mike Frysinger98c52642009-04-02 10:02:37 +0000437 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200438
439 if (is_assign_op(op)) {
Denis Vlasenkocc8289d2009-04-03 21:13:31 +0000440 char buf[sizeof(arith_t)*3 + 2];
Mike Frysinger98c52642009-04-02 10:02:37 +0000441
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200442 if (top_of_stack->var == NULL) {
Mike Frysinger98c52642009-04-02 10:02:37 +0000443 /* Hmm, 1=2 ? */
444 goto err;
445 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200446 /* Save to shell variable */
447 sprintf(buf, ARITH_FMT, rez);
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200448 math_state->setvar(top_of_stack->var, buf);
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200449 /* After saving, make previous value for v++ or v-- */
Mike Frysinger98c52642009-04-02 10:02:37 +0000450 if (op == TOK_POST_INC)
451 rez--;
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200452 if (op == TOK_POST_DEC)
Mike Frysinger98c52642009-04-02 10:02:37 +0000453 rez++;
454 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200455
456 top_of_stack->val = rez;
457 /* Erase var name, it is just a number now */
458 top_of_stack->var = NULL;
Denys Vlasenko063847d2010-09-15 13:33:02 +0200459 return NULL;
Mike Frysinger98c52642009-04-02 10:02:37 +0000460 err:
Denys Vlasenko063847d2010-09-15 13:33:02 +0200461 return "arithmetic syntax error";
Denys Vlasenkobd147702010-09-13 11:11:40 +0200462#undef NUMPTR
Mike Frysinger98c52642009-04-02 10:02:37 +0000463}
464
465/* longest must be first */
466static const char op_tokens[] ALIGN1 = {
467 '<','<','=',0, TOK_LSHIFT_ASSIGN,
468 '>','>','=',0, TOK_RSHIFT_ASSIGN,
469 '<','<', 0, TOK_LSHIFT,
470 '>','>', 0, TOK_RSHIFT,
471 '|','|', 0, TOK_OR,
472 '&','&', 0, TOK_AND,
473 '!','=', 0, TOK_NE,
474 '<','=', 0, TOK_LE,
475 '>','=', 0, TOK_GE,
476 '=','=', 0, TOK_EQ,
477 '|','=', 0, TOK_OR_ASSIGN,
478 '&','=', 0, TOK_AND_ASSIGN,
479 '*','=', 0, TOK_MUL_ASSIGN,
480 '/','=', 0, TOK_DIV_ASSIGN,
481 '%','=', 0, TOK_REM_ASSIGN,
482 '+','=', 0, TOK_PLUS_ASSIGN,
483 '-','=', 0, TOK_MINUS_ASSIGN,
484 '-','-', 0, TOK_POST_DEC,
485 '^','=', 0, TOK_XOR_ASSIGN,
486 '+','+', 0, TOK_POST_INC,
487 '*','*', 0, TOK_EXPONENT,
488 '!', 0, TOK_NOT,
489 '<', 0, TOK_LT,
490 '>', 0, TOK_GT,
491 '=', 0, TOK_ASSIGN,
492 '|', 0, TOK_BOR,
493 '&', 0, TOK_BAND,
494 '*', 0, TOK_MUL,
495 '/', 0, TOK_DIV,
496 '%', 0, TOK_REM,
497 '+', 0, TOK_ADD,
498 '-', 0, TOK_SUB,
499 '^', 0, TOK_BXOR,
500 /* uniq */
501 '~', 0, TOK_BNOT,
502 ',', 0, TOK_COMMA,
503 '?', 0, TOK_CONDITIONAL,
504 ':', 0, TOK_CONDITIONAL_SEP,
505 ')', 0, TOK_RPAREN,
506 '(', 0, TOK_LPAREN,
507 0
508};
Denys Vlasenkob771c652010-09-13 00:34:26 +0200509#define ptr_to_rparen (&op_tokens[sizeof(op_tokens)-7])
Mike Frysinger98c52642009-04-02 10:02:37 +0000510
Denys Vlasenko9edd2682019-05-19 17:23:31 +0200511#if ENABLE_FEATURE_SH_MATH_BASE
512static arith_t strto_arith_t(const char *nptr, char **endptr)
513{
514 unsigned base;
515 arith_t n;
516
517# if ENABLE_FEATURE_SH_MATH_64
518 n = strtoull(nptr, endptr, 0);
519# else
520 n = strtoul(nptr, endptr, 0);
521# endif
522 if (**endptr != '#'
523 || (*nptr < '1' || *nptr > '9')
524 || (n < 2 || n > 64)
525 ) {
526 return n;
527 }
528
529 /* It's "N#nnnn" or "NN#nnnn" syntax, NN can't start with 0,
530 * NN is in 2..64 range.
531 */
532 base = (unsigned)n;
533 n = 0;
534 nptr = *endptr + 1;
Denys Vlasenkoca1ce4b2019-09-22 18:26:05 +0200535 for (;;) {
536 unsigned digit = (unsigned)*nptr - '0';
Denys Vlasenko74274062019-10-22 14:25:43 +0200537 if (digit >= 10 /* not 0..9 */
538 && digit <= 'z' - '0' /* needed to reject e.g. $((64#~)) */
539 ) {
Denys Vlasenkoc58d7852019-09-22 23:40:10 +0200540 /* in bases up to 36, case does not matter for a-z */
Denys Vlasenkoca1ce4b2019-09-22 18:26:05 +0200541 digit = (unsigned)(*nptr | 0x20) - ('a' - 10);
Denys Vlasenkoc58d7852019-09-22 23:40:10 +0200542 if (base > 36 && *nptr <= '_') {
Denys Vlasenko74274062019-10-22 14:25:43 +0200543 /* otherwise, A-Z,@,_ are 36-61,62,63 */
544 if (*nptr == '_')
Denys Vlasenkoc58d7852019-09-22 23:40:10 +0200545 digit = 63;
Denys Vlasenko74274062019-10-22 14:25:43 +0200546 else if (*nptr == '@')
547 digit = 62;
Denys Vlasenkoc58d7852019-09-22 23:40:10 +0200548 else if (digit < 36) /* A-Z */
549 digit += 36 - 10;
550 else
Denys Vlasenko74274062019-10-22 14:25:43 +0200551 break; /* error: one of [\]^ */
Denys Vlasenkoc58d7852019-09-22 23:40:10 +0200552 }
553 //bb_error_msg("ch:'%c'%d digit:%u", *nptr, *nptr, digit);
554 //if (digit < 10) - example where we need this?
555 // break;
Denys Vlasenkoca1ce4b2019-09-22 18:26:05 +0200556 }
Denys Vlasenkoc58d7852019-09-22 23:40:10 +0200557 if (digit >= base)
558 break;
Denys Vlasenko9edd2682019-05-19 17:23:31 +0200559 /* bash does not check for overflows */
Denys Vlasenkoca1ce4b2019-09-22 18:26:05 +0200560 n = n * base + digit;
561 nptr++;
Denys Vlasenko9edd2682019-05-19 17:23:31 +0200562 }
Denys Vlasenko74274062019-10-22 14:25:43 +0200563 /* Note: we do not set errno on bad chars, we just set a pointer
564 * to the first invalid char. For example, this allows
565 * "N#" (empty "nnnn" part): 64#+1 is a valid expression,
566 * it means 64# + 1, whereas 64#~... is not, since ~ is not a valid
567 * operator.
568 */
Denys Vlasenko9edd2682019-05-19 17:23:31 +0200569 *endptr = (char*)nptr;
570 return n;
571}
Denys Vlasenko8b35f202019-05-26 14:02:10 +0200572#else /* !ENABLE_FEATURE_SH_MATH_BASE */
573# if ENABLE_FEATURE_SH_MATH_64
574# define strto_arith_t(nptr, endptr) strtoull(nptr, endptr, 0)
575# else
576# define strto_arith_t(nptr, endptr) strtoul(nptr, endptr, 0)
577# endif
Denys Vlasenko9edd2682019-05-19 17:23:31 +0200578#endif
579
Denys Vlasenko841878e2020-10-01 00:57:05 +0200580static arith_t
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200581evaluate_string(arith_state_t *math_state, const char *expr)
Mike Frysinger98c52642009-04-02 10:02:37 +0000582{
Denys Vlasenkob771c652010-09-13 00:34:26 +0200583 operator lasttok;
Denys Vlasenko063847d2010-09-15 13:33:02 +0200584 const char *errmsg;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200585 const char *start_expr = expr = skip_whitespace(expr);
586 unsigned expr_len = strlen(expr) + 2;
Mike Frysinger98c52642009-04-02 10:02:37 +0000587 /* Stack of integers */
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200588 /* The proof that there can be no more than strlen(startbuf)/2+1
589 * integers in any given correct or incorrect expression
590 * is left as an exercise to the reader. */
591 var_or_num_t *const numstack = alloca((expr_len / 2) * sizeof(numstack[0]));
592 var_or_num_t *numstackptr = numstack;
Mike Frysinger98c52642009-04-02 10:02:37 +0000593 /* Stack of operator tokens */
Denys Vlasenkob771c652010-09-13 00:34:26 +0200594 operator *const stack = alloca(expr_len * sizeof(stack[0]));
595 operator *stackptr = stack;
Mike Frysinger98c52642009-04-02 10:02:37 +0000596
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200597 /* Start with a left paren */
598 *stackptr++ = lasttok = TOK_LPAREN;
Denys Vlasenko063847d2010-09-15 13:33:02 +0200599 errmsg = NULL;
Mike Frysinger98c52642009-04-02 10:02:37 +0000600
601 while (1) {
Denys Vlasenkob771c652010-09-13 00:34:26 +0200602 const char *p;
603 operator op;
604 operator prec;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200605
606 expr = skip_whitespace(expr);
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200607 if (*expr == '\0') {
Denys Vlasenkob771c652010-09-13 00:34:26 +0200608 if (expr == start_expr) {
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200609 /* Null expression */
Denys Vlasenkob771c652010-09-13 00:34:26 +0200610 numstack->val = 0;
611 goto ret;
Mike Frysinger98c52642009-04-02 10:02:37 +0000612 }
613
614 /* This is only reached after all tokens have been extracted from the
615 * input stream. If there are still tokens on the operator stack, they
616 * are to be applied in order. At the end, there should be a final
617 * result on the integer stack */
618
Denys Vlasenkob771c652010-09-13 00:34:26 +0200619 if (expr != ptr_to_rparen + 1) {
Denys Vlasenkobd147702010-09-13 11:11:40 +0200620 /* If we haven't done so already,
621 * append a closing right paren
622 * and let the loop process it */
Denys Vlasenkob771c652010-09-13 00:34:26 +0200623 expr = ptr_to_rparen;
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200624//bb_error_msg("expr=')'");
Mike Frysinger98c52642009-04-02 10:02:37 +0000625 continue;
626 }
Denys Vlasenkobd147702010-09-13 11:11:40 +0200627 /* At this point, we're done with the expression */
Denys Vlasenkob771c652010-09-13 00:34:26 +0200628 if (numstackptr != numstack + 1) {
Denys Vlasenkobd147702010-09-13 11:11:40 +0200629 /* ...but if there isn't, it's bad */
Denys Vlasenkob771c652010-09-13 00:34:26 +0200630 goto err;
Mike Frysinger98c52642009-04-02 10:02:37 +0000631 }
Denys Vlasenkob771c652010-09-13 00:34:26 +0200632 goto ret;
Mike Frysinger98c52642009-04-02 10:02:37 +0000633 }
634
Mike Frysinger98c52642009-04-02 10:02:37 +0000635 p = endofname(expr);
636 if (p != expr) {
Denys Vlasenkob771c652010-09-13 00:34:26 +0200637 /* Name */
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200638 size_t var_name_size = (p - expr) + 1; /* +1 for NUL */
Mike Frysinger98c52642009-04-02 10:02:37 +0000639 numstackptr->var = alloca(var_name_size);
640 safe_strncpy(numstackptr->var, expr, var_name_size);
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200641//bb_error_msg("var:'%s'", numstackptr->var);
Mike Frysinger98c52642009-04-02 10:02:37 +0000642 expr = p;
643 num:
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200644 numstackptr->second_val_present = 0;
Mike Frysinger98c52642009-04-02 10:02:37 +0000645 numstackptr++;
646 lasttok = TOK_NUM;
647 continue;
648 }
Denys Vlasenkob771c652010-09-13 00:34:26 +0200649
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200650 if (isdigit(*expr)) {
Denys Vlasenkob771c652010-09-13 00:34:26 +0200651 /* Number */
Mike Frysinger98c52642009-04-02 10:02:37 +0000652 numstackptr->var = NULL;
Denys Vlasenko71016ba2009-06-05 16:24:29 +0200653 errno = 0;
Denys Vlasenko8b35f202019-05-26 14:02:10 +0200654 numstackptr->val = strto_arith_t(expr, (char**) &expr);
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200655//bb_error_msg("val:%lld", numstackptr->val);
Denys Vlasenko71016ba2009-06-05 16:24:29 +0200656 if (errno)
657 numstackptr->val = 0; /* bash compat */
Mike Frysinger98c52642009-04-02 10:02:37 +0000658 goto num;
659 }
Mike Frysinger98c52642009-04-02 10:02:37 +0000660
Denys Vlasenkob771c652010-09-13 00:34:26 +0200661 /* Should be an operator */
Denys Vlasenkof19e3c12018-01-28 20:13:33 +0100662
Denys Vlasenkod84a6042021-09-25 22:04:45 +0200663 /* Special case: XYZ--, XYZ++, --XYZ, ++XYZ are recognized
664 * only if XYZ is a variable name, not a number or EXPR. IOW:
Denys Vlasenkof19e3c12018-01-28 20:13:33 +0100665 * "a+++v" is a++ + v.
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200666 * "(a)+++7" is ( a ) + + + 7.
Denys Vlasenkof19e3c12018-01-28 20:13:33 +0100667 * "7+++v" is 7 + ++v, not 7++ + v.
Denys Vlasenkod84a6042021-09-25 22:04:45 +0200668 * "--7" is - - 7, not --7.
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200669 * "++++a" is + + ++a, not ++ ++a.
Denys Vlasenkof19e3c12018-01-28 20:13:33 +0100670 */
Denys Vlasenkod84a6042021-09-25 22:04:45 +0200671 if ((expr[0] == '+' || expr[0] == '-')
Denys Vlasenkof19e3c12018-01-28 20:13:33 +0100672 && (expr[1] == expr[0])
673 ) {
Denys Vlasenkod84a6042021-09-25 22:04:45 +0200674 if (numstackptr == numstack || !numstackptr[-1].var) { /* not a VAR++ */
675 char next = skip_whitespace(expr + 2)[0];
676 if (!(isalpha(next) || next == '_')) { /* not a ++VAR */
677 //bb_error_msg("special %c%c", expr[0], expr[0]);
678 op = (expr[0] == '+' ? TOK_ADD : TOK_SUB);
679 expr++;
680 goto tok_found1;
681 }
682 }
Denys Vlasenkof19e3c12018-01-28 20:13:33 +0100683 }
684
Denys Vlasenkob771c652010-09-13 00:34:26 +0200685 p = op_tokens;
686 while (1) {
Denys Vlasenkob771c652010-09-13 00:34:26 +0200687 /* Compare expr to current op_tokens[] element */
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200688 const char *e = expr;
689 while (1) {
690 if (*p == '\0') {
691 /* Match: operator is found */
692 expr = e;
693 goto tok_found;
694 }
695 if (*p != *e)
696 break;
697 p++;
698 e++;
Mike Frysinger98c52642009-04-02 10:02:37 +0000699 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200700 /* No match, go to next element of op_tokens[] */
Mike Frysinger98c52642009-04-02 10:02:37 +0000701 while (*p)
702 p++;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200703 p += 2; /* skip NUL and TOK_foo bytes */
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200704 if (*p == '\0') {
705 /* No next element, operator not found */
706 //math_state->syntax_error_at = expr;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200707 goto err;
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200708 }
Mike Frysinger98c52642009-04-02 10:02:37 +0000709 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200710 tok_found:
Denys Vlasenkob771c652010-09-13 00:34:26 +0200711 op = p[1]; /* fetch TOK_foo value */
Denys Vlasenkof19e3c12018-01-28 20:13:33 +0100712 tok_found1:
Denys Vlasenkob771c652010-09-13 00:34:26 +0200713 /* NB: expr now points past the operator */
Mike Frysinger98c52642009-04-02 10:02:37 +0000714
715 /* post grammar: a++ reduce to num */
716 if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC)
717 lasttok = TOK_NUM;
718
719 /* Plus and minus are binary (not unary) _only_ if the last
Denys Vlasenko71016ba2009-06-05 16:24:29 +0200720 * token was a number, or a right paren (which pretends to be
Mike Frysinger98c52642009-04-02 10:02:37 +0000721 * a number, since it evaluates to one). Think about it.
722 * It makes sense. */
723 if (lasttok != TOK_NUM) {
724 switch (op) {
725 case TOK_ADD:
726 op = TOK_UPLUS;
727 break;
728 case TOK_SUB:
729 op = TOK_UMINUS;
730 break;
731 case TOK_POST_INC:
732 op = TOK_PRE_INC;
733 break;
734 case TOK_POST_DEC:
735 op = TOK_PRE_DEC;
736 break;
737 }
738 }
Denys Vlasenko71016ba2009-06-05 16:24:29 +0200739 /* We don't want an unary operator to cause recursive descent on the
Mike Frysinger98c52642009-04-02 10:02:37 +0000740 * stack, because there can be many in a row and it could cause an
741 * operator to be evaluated before its argument is pushed onto the
Denys Vlasenkobd147702010-09-13 11:11:40 +0200742 * integer stack.
743 * But for binary operators, "apply" everything on the operator
Mike Frysinger98c52642009-04-02 10:02:37 +0000744 * stack until we find an operator with a lesser priority than the
Denys Vlasenko395b97a2010-09-17 18:02:17 +0200745 * one we have just extracted. If op is right-associative,
746 * then stop "applying" on the equal priority too.
Denys Vlasenkobd147702010-09-13 11:11:40 +0200747 * Left paren is given the lowest priority so it will never be
Mike Frysinger98c52642009-04-02 10:02:37 +0000748 * "applied" in this way.
Mike Frysinger98c52642009-04-02 10:02:37 +0000749 */
750 prec = PREC(op);
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200751//bb_error_msg("prec:%02x", prec);
Mike Frysinger98c52642009-04-02 10:02:37 +0000752 if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
753 /* not left paren or unary */
754 if (lasttok != TOK_NUM) {
755 /* binary op must be preceded by a num */
756 goto err;
757 }
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200758 /* The algorithm employed here is simple: while we don't
759 * hit an open paren nor the bottom of the stack, pop
760 * tokens and apply them */
Mike Frysinger98c52642009-04-02 10:02:37 +0000761 while (stackptr != stack) {
Denys Vlasenko51850c82010-09-13 01:09:11 +0200762 operator prev_op = *--stackptr;
Mike Frysinger98c52642009-04-02 10:02:37 +0000763 if (op == TOK_RPAREN) {
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200764//bb_error_msg("op == TOK_RPAREN");
Denys Vlasenko51850c82010-09-13 01:09:11 +0200765 if (prev_op == TOK_LPAREN) {
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200766//bb_error_msg("prev_op == TOK_LPAREN");
767//bb_error_msg(" %p %p numstackptr[-1].var:'%s'", numstack, numstackptr-1, numstackptr[-1].var);
768 if (numstackptr[-1].var) {
769 /* Expression is (var), lookup now */
770 errmsg = arith_lookup_val(math_state, &numstackptr[-1]);
771 if (errmsg)
772 goto err_with_custom_msg;
773 /* Erase var name: (var) is just a number, for example, (var) = 1 is not valid */
774 numstackptr[-1].var = NULL;
775 }
Denys Vlasenkobd147702010-09-13 11:11:40 +0200776 /* Any operator directly after a
777 * close paren should consider itself binary */
Mike Frysinger98c52642009-04-02 10:02:37 +0000778 lasttok = TOK_NUM;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200779 goto next;
Mike Frysinger98c52642009-04-02 10:02:37 +0000780 }
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200781//bb_error_msg("prev_op != TOK_LPAREN");
Mike Frysinger98c52642009-04-02 10:02:37 +0000782 } else {
Denys Vlasenko51850c82010-09-13 01:09:11 +0200783 operator prev_prec = PREC(prev_op);
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200784//bb_error_msg("op != TOK_RPAREN");
Denys Vlasenkobd147702010-09-13 11:11:40 +0200785 fix_assignment_prec(prec);
786 fix_assignment_prec(prev_prec);
Denys Vlasenko51850c82010-09-13 01:09:11 +0200787 if (prev_prec < prec
Denys Vlasenkobd147702010-09-13 11:11:40 +0200788 || (prev_prec == prec && is_right_associative(prec))
Denys Vlasenko51850c82010-09-13 01:09:11 +0200789 ) {
790 stackptr++;
Mike Frysinger98c52642009-04-02 10:02:37 +0000791 break;
Denys Vlasenko51850c82010-09-13 01:09:11 +0200792 }
Mike Frysinger98c52642009-04-02 10:02:37 +0000793 }
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200794//bb_error_msg("arith_apply(prev_op:%02x)", prev_op);
Denys Vlasenko063847d2010-09-15 13:33:02 +0200795 errmsg = arith_apply(math_state, prev_op, numstack, &numstackptr);
796 if (errmsg)
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200797 goto err_with_custom_msg;
Mike Frysinger98c52642009-04-02 10:02:37 +0000798 }
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200799 if (op == TOK_RPAREN)
Mike Frysinger98c52642009-04-02 10:02:37 +0000800 goto err;
Mike Frysinger98c52642009-04-02 10:02:37 +0000801 }
802
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200803 /* Push this operator to the stack and remember it */
Denys Vlasenko1be73dd2021-09-26 13:25:49 +0200804//bb_error_msg("push op:%02x", op);
Mike Frysinger98c52642009-04-02 10:02:37 +0000805 *stackptr++ = lasttok = op;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200806 next: ;
807 } /* while (1) */
808
809 err:
Denys Vlasenko063847d2010-09-15 13:33:02 +0200810 errmsg = "arithmetic syntax error";
Denys Vlasenkobed7c812010-09-16 11:50:46 +0200811 err_with_custom_msg:
812 numstack->val = -1;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200813 ret:
Denys Vlasenko063847d2010-09-15 13:33:02 +0200814 math_state->errmsg = errmsg;
Denys Vlasenkob771c652010-09-13 00:34:26 +0200815 return numstack->val;
Mike Frysinger98c52642009-04-02 10:02:37 +0000816}
817
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200818arith_t FAST_FUNC
819arith(arith_state_t *math_state, const char *expr)
820{
Denys Vlasenko063847d2010-09-15 13:33:02 +0200821 math_state->errmsg = NULL;
Denys Vlasenko0eac8ff2010-09-13 12:49:52 +0200822 math_state->list_of_recursed_names = NULL;
823 return evaluate_string(math_state, expr);
824}
825
Denis Vlasenkocc8289d2009-04-03 21:13:31 +0000826/*
Mike Frysinger98c52642009-04-02 10:02:37 +0000827 * Copyright (c) 1989, 1991, 1993, 1994
828 * The Regents of the University of California. All rights reserved.
829 *
830 * This code is derived from software contributed to Berkeley by
831 * Kenneth Almquist.
832 *
833 * Redistribution and use in source and binary forms, with or without
834 * modification, are permitted provided that the following conditions
835 * are met:
836 * 1. Redistributions of source code must retain the above copyright
837 * notice, this list of conditions and the following disclaimer.
838 * 2. Redistributions in binary form must reproduce the above copyright
839 * notice, this list of conditions and the following disclaimer in the
840 * documentation and/or other materials provided with the distribution.
841 * 3. Neither the name of the University nor the names of its contributors
842 * may be used to endorse or promote products derived from this software
843 * without specific prior written permission.
844 *
Denys Vlasenko95f79532017-08-02 14:26:33 +0200845 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
Mike Frysinger98c52642009-04-02 10:02:37 +0000846 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
847 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
848 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
849 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
850 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
851 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
852 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
853 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
854 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
855 * SUCH DAMAGE.
856 */