aboutsummaryrefslogtreecommitdiff
path: root/shell/math.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-01-28 20:13:33 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-01-28 20:13:33 +0100
commitf19e3c1c6c96e3c709ac732ff70c586521f792d3 (patch)
treef2b16759f1eae8ac09205f0fb29f822d70610a36 /shell/math.c
parent675d24aeaff29dbce6dc116a4b7c9f6026ed5069 (diff)
downloadbusybox-f19e3c1c6c96e3c709ac732ff70c586521f792d3.tar.gz
shell: handle $((NUM++...) like bash does. Closes 10706
function old new delta evaluate_string 680 729 +49 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/math.c')
-rw-r--r--shell/math.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/shell/math.c b/shell/math.c
index f01f24362..611b3beab 100644
--- a/shell/math.c
+++ b/shell/math.c
@@ -598,10 +598,24 @@ evaluate_string(arith_state_t *math_state, const char *expr)
}
/* Should be an operator */
+
+ /* Special case: NUM-- and NUM++ are not recognized if NUM
+ * is a literal number, not a variable. IOW:
+ * "a+++v" is a++ + v.
+ * "7+++v" is 7 + ++v, not 7++ + v.
+ */
+ if (lasttok == TOK_NUM && !numstackptr[-1].var /* number literal */
+ && (expr[0] == '+' || expr[0] == '-')
+ && (expr[1] == expr[0])
+ ) {
+ //bb_error_msg("special %c%c", expr[0], expr[0]);
+ op = (expr[0] == '+' ? TOK_ADD : TOK_SUB);
+ expr += 1;
+ goto tok_found1;
+ }
+
p = op_tokens;
while (1) {
-// TODO: bash allows 7+++v, treats it as 7 + ++v
-// we treat it as 7++ + v and reject
/* Compare expr to current op_tokens[] element */
const char *e = expr;
while (1) {
@@ -627,6 +641,7 @@ evaluate_string(arith_state_t *math_state, const char *expr)
}
tok_found:
op = p[1]; /* fetch TOK_foo value */
+ tok_found1:
/* NB: expr now points past the operator */
/* post grammar: a++ reduce to num */