Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1 | /* |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 2 | * Arithmetic code ripped out of ash shell for code sharing. |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3 | * |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 4 | * 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 Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 9 | * 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 Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 15 | * 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 Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 25 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 26 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 27 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 28 | /* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com> |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 29 | * |
| 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 Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 49 | |
| 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 Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 55 | * be that which POSIX specifies for shells. |
| 56 | * |
| 57 | * The code uses a simple two-stack algorithm. See |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 58 | * 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 Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 62 | * expression). |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 63 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 64 | |
| 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 Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 92 | /* |
| 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 Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 101 | /* |
| 102 | * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
| 103 | * |
| 104 | * - allow access to variable, |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 105 | * 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 Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 111 | * - allow hexadecimal and octal numbers |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 112 | * - restore lost XOR operator |
| 113 | * - protect $((num num)) as true zero expr (Manuel's error) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 114 | * - always use special isspace(), see comment from bash ;-) |
| 115 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 116 | #include "libbb.h" |
| 117 | #include "math.h" |
| 118 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 119 | #define lookupvar (math_state->lookupvar) |
| 120 | #define setvar (math_state->setvar ) |
| 121 | //#define endofname (math_state->endofname) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 122 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 123 | typedef unsigned char operator; |
| 124 | |
| 125 | /* An operator's token id is a bit of a bitfield. The lower 5 bits are the |
| 126 | * precedence, and 3 high bits are an ID unique across operators of that |
| 127 | * precedence. The ID portion is so that multiple operators can have the |
| 128 | * same precedence, ensuring that the leftmost one is evaluated first. |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 129 | * Consider * and / |
| 130 | */ |
| 131 | #define tok_decl(prec,id) (((id)<<5) | (prec)) |
| 132 | #define PREC(op) ((op) & 0x1F) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 133 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 134 | #define TOK_LPAREN tok_decl(0,0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 135 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 136 | #define TOK_COMMA tok_decl(1,0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 137 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 138 | /* All assignments are right associative and have the same precedence, |
| 139 | * but there are 11 of them, which doesn't fit into 3 bits for unique id. |
| 140 | * Abusing another precedence level: |
| 141 | */ |
| 142 | #define TOK_ASSIGN tok_decl(2,0) |
| 143 | #define TOK_AND_ASSIGN tok_decl(2,1) |
| 144 | #define TOK_OR_ASSIGN tok_decl(2,2) |
| 145 | #define TOK_XOR_ASSIGN tok_decl(2,3) |
| 146 | #define TOK_PLUS_ASSIGN tok_decl(2,4) |
| 147 | #define TOK_MINUS_ASSIGN tok_decl(2,5) |
| 148 | #define TOK_LSHIFT_ASSIGN tok_decl(2,6) |
| 149 | #define TOK_RSHIFT_ASSIGN tok_decl(2,7) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 150 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 151 | #define TOK_MUL_ASSIGN tok_decl(3,0) |
| 152 | #define TOK_DIV_ASSIGN tok_decl(3,1) |
| 153 | #define TOK_REM_ASSIGN tok_decl(3,2) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 154 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 155 | #define fix_assignment_prec(prec) do { if (prec == 3) prec = 2; } while (0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 156 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 157 | /* Ternary conditional operator is right associative too */ |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 158 | #define TOK_CONDITIONAL tok_decl(4,0) |
| 159 | #define TOK_CONDITIONAL_SEP tok_decl(4,1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 160 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 161 | #define TOK_OR tok_decl(5,0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 162 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 163 | #define TOK_AND tok_decl(6,0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 164 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 165 | #define TOK_BOR tok_decl(7,0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 166 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 167 | #define TOK_BXOR tok_decl(8,0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 168 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 169 | #define TOK_BAND tok_decl(9,0) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 170 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 171 | #define TOK_EQ tok_decl(10,0) |
| 172 | #define TOK_NE tok_decl(10,1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 173 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 174 | #define TOK_LT tok_decl(11,0) |
| 175 | #define TOK_GT tok_decl(11,1) |
| 176 | #define TOK_GE tok_decl(11,2) |
| 177 | #define TOK_LE tok_decl(11,3) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 178 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 179 | #define TOK_LSHIFT tok_decl(12,0) |
| 180 | #define TOK_RSHIFT tok_decl(12,1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 181 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 182 | #define TOK_ADD tok_decl(13,0) |
| 183 | #define TOK_SUB tok_decl(13,1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 184 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 185 | #define TOK_MUL tok_decl(14,0) |
| 186 | #define TOK_DIV tok_decl(14,1) |
| 187 | #define TOK_REM tok_decl(14,2) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 188 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 189 | /* Exponent is right associative */ |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 190 | #define TOK_EXPONENT tok_decl(15,1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 191 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 192 | /* Unary operators */ |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 193 | #define UNARYPREC 16 |
| 194 | #define TOK_BNOT tok_decl(UNARYPREC,0) |
| 195 | #define TOK_NOT tok_decl(UNARYPREC,1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 196 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 197 | #define TOK_UMINUS tok_decl(UNARYPREC+1,0) |
| 198 | #define TOK_UPLUS tok_decl(UNARYPREC+1,1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 199 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 200 | #define PREC_PRE (UNARYPREC+2) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 201 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 202 | #define TOK_PRE_INC tok_decl(PREC_PRE, 0) |
| 203 | #define TOK_PRE_DEC tok_decl(PREC_PRE, 1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 204 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 205 | #define PREC_POST (UNARYPREC+3) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 206 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 207 | #define TOK_POST_INC tok_decl(PREC_POST, 0) |
| 208 | #define TOK_POST_DEC tok_decl(PREC_POST, 1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 209 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 210 | #define SPEC_PREC (UNARYPREC+4) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 211 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 212 | #define TOK_NUM tok_decl(SPEC_PREC, 0) |
| 213 | #define TOK_RPAREN tok_decl(SPEC_PREC, 1) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 214 | |
| 215 | static int |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 216 | is_assign_op(operator op) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 217 | { |
| 218 | operator prec = PREC(op); |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 219 | fix_assignment_prec(prec); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 220 | return prec == PREC(TOK_ASSIGN) |
| 221 | || prec == PREC_PRE |
| 222 | || prec == PREC_POST; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 226 | is_right_associative(operator prec) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 227 | { |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 228 | return prec == PREC(TOK_ASSIGN) |
| 229 | || prec == PREC(TOK_EXPONENT) |
| 230 | || prec == PREC(TOK_CONDITIONAL); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 233 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 234 | typedef struct { |
| 235 | arith_t val; |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 236 | /* We acquire second_val only when "expr1 : expr2" part |
| 237 | * of ternary ?: op is evaluated. |
| 238 | * We treat ?: as two binary ops: (expr ? (expr1 : expr2)). |
| 239 | * ':' produces a new value which has two parts, val and second_val; |
| 240 | * then '?' selects one of them based on its left side. |
| 241 | */ |
| 242 | arith_t second_val; |
| 243 | char second_val_present; |
| 244 | /* If NULL then it's just a number, else it's a named variable */ |
| 245 | char *var; |
| 246 | } var_or_num_t; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 247 | |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 248 | typedef struct remembered_name { |
| 249 | struct remembered_name *next; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 250 | const char *var; |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 251 | } remembered_name; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 252 | |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 253 | |
| 254 | static arith_t FAST_FUNC |
| 255 | evaluate_string(arith_state_t *math_state, const char *expr); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 256 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 257 | static const char* |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 258 | arith_lookup_val(arith_state_t *math_state, var_or_num_t *t) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 259 | { |
| 260 | if (t->var) { |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 261 | const char *p = lookupvar(t->var); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 262 | if (p) { |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 263 | remembered_name *cur; |
| 264 | remembered_name cur_save; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 265 | |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 266 | /* did we already see this name? |
| 267 | * testcase: a=b; b=a; echo $((a)) |
| 268 | */ |
| 269 | for (cur = math_state->list_of_recursed_names; cur; cur = cur->next) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 270 | if (strcmp(cur->var, t->var) == 0) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 271 | /* Yes */ |
| 272 | return "expression recursion loop detected"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 275 | |
| 276 | /* push current var name */ |
| 277 | cur = math_state->list_of_recursed_names; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 278 | cur_save.var = t->var; |
| 279 | cur_save.next = cur; |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 280 | math_state->list_of_recursed_names = &cur_save; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 281 | |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 282 | /* recursively evaluate p as expression */ |
| 283 | t->val = evaluate_string(math_state, p); |
| 284 | |
| 285 | /* pop current var name */ |
| 286 | math_state->list_of_recursed_names = cur; |
| 287 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 288 | return math_state->errmsg; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 289 | } |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 290 | /* treat undefined var as 0 */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 291 | t->val = 0; |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 296 | /* "Applying" a token means performing it on the top elements on the integer |
| 297 | * stack. For an unary operator it will only change the top element, but a |
| 298 | * binary operator will pop two arguments and push the result */ |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 299 | static NOINLINE const char* |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 300 | arith_apply(arith_state_t *math_state, operator op, var_or_num_t *numstack, var_or_num_t **numstackptr) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 301 | { |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 302 | #define NUMPTR (*numstackptr) |
| 303 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 304 | var_or_num_t *top_of_stack; |
| 305 | arith_t rez; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 306 | const char *err; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 307 | |
| 308 | /* There is no operator that can work without arguments */ |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 309 | if (NUMPTR == numstack) |
| 310 | goto err; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 311 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 312 | top_of_stack = NUMPTR - 1; |
| 313 | |
| 314 | /* Resolve name to value, if needed */ |
| 315 | err = arith_lookup_val(math_state, top_of_stack); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 316 | if (err) |
| 317 | return err; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 318 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 319 | rez = top_of_stack->val; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 320 | if (op == TOK_UMINUS) |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 321 | rez = -rez; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 322 | else if (op == TOK_NOT) |
| 323 | rez = !rez; |
| 324 | else if (op == TOK_BNOT) |
| 325 | rez = ~rez; |
| 326 | else if (op == TOK_POST_INC || op == TOK_PRE_INC) |
| 327 | rez++; |
| 328 | else if (op == TOK_POST_DEC || op == TOK_PRE_DEC) |
| 329 | rez--; |
| 330 | else if (op != TOK_UPLUS) { |
| 331 | /* Binary operators */ |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 332 | arith_t right_side_val; |
| 333 | char bad_second_val; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 334 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 335 | /* Binary operators need two arguments */ |
| 336 | if (top_of_stack == numstack) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 337 | goto err; |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 338 | /* ...and they pop one */ |
| 339 | NUMPTR = top_of_stack; /* this decrements NUMPTR */ |
| 340 | |
| 341 | bad_second_val = top_of_stack->second_val_present; |
| 342 | if (op == TOK_CONDITIONAL) { /* ? operation */ |
| 343 | /* Make next if (...) protect against |
| 344 | * $((expr1 ? expr2)) - that is, missing ": expr" */ |
| 345 | bad_second_val = !bad_second_val; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 346 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 347 | if (bad_second_val) { |
| 348 | /* Protect against $((expr <not_?_op> expr1 : expr2)) */ |
| 349 | return "malformed ?: operator"; |
| 350 | } |
| 351 | |
| 352 | top_of_stack--; /* now points to left side */ |
| 353 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 354 | if (op != TOK_ASSIGN) { |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 355 | /* Resolve left side value (unless the op is '=') */ |
| 356 | err = arith_lookup_val(math_state, top_of_stack); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 357 | if (err) |
| 358 | return err; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 359 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 360 | |
| 361 | right_side_val = rez; |
| 362 | rez = top_of_stack->val; |
| 363 | if (op == TOK_CONDITIONAL) /* ? operation */ |
| 364 | rez = (rez ? right_side_val : top_of_stack[1].second_val); |
| 365 | else if (op == TOK_CONDITIONAL_SEP) { /* : operation */ |
| 366 | if (top_of_stack == numstack) { |
| 367 | /* Protect against $((expr : expr)) */ |
| 368 | return "malformed ?: operator"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 369 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 370 | top_of_stack->second_val_present = op; |
| 371 | top_of_stack->second_val = right_side_val; |
| 372 | } |
| 373 | else if (op == TOK_BOR || op == TOK_OR_ASSIGN) |
| 374 | rez |= right_side_val; |
| 375 | else if (op == TOK_OR) |
| 376 | rez = right_side_val || rez; |
| 377 | else if (op == TOK_BAND || op == TOK_AND_ASSIGN) |
| 378 | rez &= right_side_val; |
| 379 | else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN) |
| 380 | rez ^= right_side_val; |
| 381 | else if (op == TOK_AND) |
| 382 | rez = rez && right_side_val; |
| 383 | else if (op == TOK_EQ) |
| 384 | rez = (rez == right_side_val); |
| 385 | else if (op == TOK_NE) |
| 386 | rez = (rez != right_side_val); |
| 387 | else if (op == TOK_GE) |
| 388 | rez = (rez >= right_side_val); |
| 389 | else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN) |
| 390 | rez >>= right_side_val; |
| 391 | else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN) |
| 392 | rez <<= right_side_val; |
| 393 | else if (op == TOK_GT) |
| 394 | rez = (rez > right_side_val); |
| 395 | else if (op == TOK_LT) |
| 396 | rez = (rez < right_side_val); |
| 397 | else if (op == TOK_LE) |
| 398 | rez = (rez <= right_side_val); |
| 399 | else if (op == TOK_MUL || op == TOK_MUL_ASSIGN) |
| 400 | rez *= right_side_val; |
| 401 | else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN) |
| 402 | rez += right_side_val; |
| 403 | else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN) |
| 404 | rez -= right_side_val; |
| 405 | else if (op == TOK_ASSIGN || op == TOK_COMMA) |
| 406 | rez = right_side_val; |
| 407 | else if (op == TOK_EXPONENT) { |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 408 | arith_t c; |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 409 | if (right_side_val < 0) |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 410 | return "exponent less than 0"; |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 411 | c = 1; |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 412 | while (--right_side_val >= 0) |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 413 | c *= rez; |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 414 | rez = c; |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 415 | } |
| 416 | else if (right_side_val == 0) |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 417 | return "divide by zero"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 418 | else if (op == TOK_DIV || op == TOK_DIV_ASSIGN) |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 419 | rez /= right_side_val; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 420 | else if (op == TOK_REM || op == TOK_REM_ASSIGN) |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 421 | rez %= right_side_val; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 422 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 423 | |
| 424 | if (is_assign_op(op)) { |
Denis Vlasenko | cc8289d | 2009-04-03 21:13:31 +0000 | [diff] [blame] | 425 | char buf[sizeof(arith_t)*3 + 2]; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 426 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 427 | if (top_of_stack->var == NULL) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 428 | /* Hmm, 1=2 ? */ |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 429 | //TODO: actually, bash allows ++7 but for some reason it evals to 7, not 8 |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 430 | goto err; |
| 431 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 432 | /* Save to shell variable */ |
| 433 | sprintf(buf, ARITH_FMT, rez); |
| 434 | setvar(top_of_stack->var, buf); |
| 435 | /* After saving, make previous value for v++ or v-- */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 436 | if (op == TOK_POST_INC) |
| 437 | rez--; |
| 438 | else if (op == TOK_POST_DEC) |
| 439 | rez++; |
| 440 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 441 | |
| 442 | top_of_stack->val = rez; |
| 443 | /* Erase var name, it is just a number now */ |
| 444 | top_of_stack->var = NULL; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 445 | return NULL; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 446 | err: |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 447 | return "arithmetic syntax error"; |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 448 | #undef NUMPTR |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* longest must be first */ |
| 452 | static const char op_tokens[] ALIGN1 = { |
| 453 | '<','<','=',0, TOK_LSHIFT_ASSIGN, |
| 454 | '>','>','=',0, TOK_RSHIFT_ASSIGN, |
| 455 | '<','<', 0, TOK_LSHIFT, |
| 456 | '>','>', 0, TOK_RSHIFT, |
| 457 | '|','|', 0, TOK_OR, |
| 458 | '&','&', 0, TOK_AND, |
| 459 | '!','=', 0, TOK_NE, |
| 460 | '<','=', 0, TOK_LE, |
| 461 | '>','=', 0, TOK_GE, |
| 462 | '=','=', 0, TOK_EQ, |
| 463 | '|','=', 0, TOK_OR_ASSIGN, |
| 464 | '&','=', 0, TOK_AND_ASSIGN, |
| 465 | '*','=', 0, TOK_MUL_ASSIGN, |
| 466 | '/','=', 0, TOK_DIV_ASSIGN, |
| 467 | '%','=', 0, TOK_REM_ASSIGN, |
| 468 | '+','=', 0, TOK_PLUS_ASSIGN, |
| 469 | '-','=', 0, TOK_MINUS_ASSIGN, |
| 470 | '-','-', 0, TOK_POST_DEC, |
| 471 | '^','=', 0, TOK_XOR_ASSIGN, |
| 472 | '+','+', 0, TOK_POST_INC, |
| 473 | '*','*', 0, TOK_EXPONENT, |
| 474 | '!', 0, TOK_NOT, |
| 475 | '<', 0, TOK_LT, |
| 476 | '>', 0, TOK_GT, |
| 477 | '=', 0, TOK_ASSIGN, |
| 478 | '|', 0, TOK_BOR, |
| 479 | '&', 0, TOK_BAND, |
| 480 | '*', 0, TOK_MUL, |
| 481 | '/', 0, TOK_DIV, |
| 482 | '%', 0, TOK_REM, |
| 483 | '+', 0, TOK_ADD, |
| 484 | '-', 0, TOK_SUB, |
| 485 | '^', 0, TOK_BXOR, |
| 486 | /* uniq */ |
| 487 | '~', 0, TOK_BNOT, |
| 488 | ',', 0, TOK_COMMA, |
| 489 | '?', 0, TOK_CONDITIONAL, |
| 490 | ':', 0, TOK_CONDITIONAL_SEP, |
| 491 | ')', 0, TOK_RPAREN, |
| 492 | '(', 0, TOK_LPAREN, |
| 493 | 0 |
| 494 | }; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 495 | #define ptr_to_rparen (&op_tokens[sizeof(op_tokens)-7]) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 496 | |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 497 | static arith_t FAST_FUNC |
| 498 | evaluate_string(arith_state_t *math_state, const char *expr) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 499 | { |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 500 | operator lasttok; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 501 | const char *errmsg; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 502 | const char *start_expr = expr = skip_whitespace(expr); |
| 503 | unsigned expr_len = strlen(expr) + 2; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 504 | /* Stack of integers */ |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 505 | /* The proof that there can be no more than strlen(startbuf)/2+1 |
| 506 | * integers in any given correct or incorrect expression |
| 507 | * is left as an exercise to the reader. */ |
| 508 | var_or_num_t *const numstack = alloca((expr_len / 2) * sizeof(numstack[0])); |
| 509 | var_or_num_t *numstackptr = numstack; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 510 | /* Stack of operator tokens */ |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 511 | operator *const stack = alloca(expr_len * sizeof(stack[0])); |
| 512 | operator *stackptr = stack; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 513 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 514 | /* Start with a left paren */ |
| 515 | *stackptr++ = lasttok = TOK_LPAREN; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 516 | errmsg = NULL; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 517 | |
| 518 | while (1) { |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 519 | const char *p; |
| 520 | operator op; |
| 521 | operator prec; |
| 522 | char arithval; |
| 523 | |
| 524 | expr = skip_whitespace(expr); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 525 | arithval = *expr; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 526 | if (arithval == '\0') { |
| 527 | if (expr == start_expr) { |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 528 | /* Null expression */ |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 529 | numstack->val = 0; |
| 530 | goto ret; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* This is only reached after all tokens have been extracted from the |
| 534 | * input stream. If there are still tokens on the operator stack, they |
| 535 | * are to be applied in order. At the end, there should be a final |
| 536 | * result on the integer stack */ |
| 537 | |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 538 | if (expr != ptr_to_rparen + 1) { |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 539 | /* If we haven't done so already, |
| 540 | * append a closing right paren |
| 541 | * and let the loop process it */ |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 542 | expr = ptr_to_rparen; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 543 | continue; |
| 544 | } |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 545 | /* At this point, we're done with the expression */ |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 546 | if (numstackptr != numstack + 1) { |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 547 | /* ...but if there isn't, it's bad */ |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 548 | goto err; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 549 | } |
| 550 | if (numstack->var) { |
| 551 | /* expression is $((var)) only, lookup now */ |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 552 | errmsg = arith_lookup_val(math_state, numstack); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 553 | } |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 554 | goto ret; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 557 | p = endofname(expr); |
| 558 | if (p != expr) { |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 559 | /* Name */ |
| 560 | size_t var_name_size = (p-expr) + 1; /* +1 for NUL */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 561 | numstackptr->var = alloca(var_name_size); |
| 562 | safe_strncpy(numstackptr->var, expr, var_name_size); |
| 563 | expr = p; |
| 564 | num: |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 565 | numstackptr->second_val_present = 0; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 566 | numstackptr++; |
| 567 | lasttok = TOK_NUM; |
| 568 | continue; |
| 569 | } |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 570 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 571 | if (isdigit(arithval)) { |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 572 | /* Number */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 573 | numstackptr->var = NULL; |
Denys Vlasenko | 71016ba | 2009-06-05 16:24:29 +0200 | [diff] [blame] | 574 | errno = 0; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 575 | numstackptr->val = strto_arith_t(expr, (char**) &expr, 0); |
Denys Vlasenko | 71016ba | 2009-06-05 16:24:29 +0200 | [diff] [blame] | 576 | if (errno) |
| 577 | numstackptr->val = 0; /* bash compat */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 578 | goto num; |
| 579 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 580 | |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 581 | /* Should be an operator */ |
| 582 | p = op_tokens; |
| 583 | while (1) { |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 584 | // TODO: bash allows 7+++v, treats it as 7 + ++v |
| 585 | // we treat it as 7++ + v and reject |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 586 | /* Compare expr to current op_tokens[] element */ |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 587 | const char *e = expr; |
| 588 | while (1) { |
| 589 | if (*p == '\0') { |
| 590 | /* Match: operator is found */ |
| 591 | expr = e; |
| 592 | goto tok_found; |
| 593 | } |
| 594 | if (*p != *e) |
| 595 | break; |
| 596 | p++; |
| 597 | e++; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 598 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 599 | /* No match, go to next element of op_tokens[] */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 600 | while (*p) |
| 601 | p++; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 602 | p += 2; /* skip NUL and TOK_foo bytes */ |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 603 | if (*p == '\0') { |
| 604 | /* No next element, operator not found */ |
| 605 | //math_state->syntax_error_at = expr; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 606 | goto err; |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 607 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 608 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 609 | tok_found: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 610 | op = p[1]; /* fetch TOK_foo value */ |
| 611 | /* NB: expr now points past the operator */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 612 | |
| 613 | /* post grammar: a++ reduce to num */ |
| 614 | if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC) |
| 615 | lasttok = TOK_NUM; |
| 616 | |
| 617 | /* Plus and minus are binary (not unary) _only_ if the last |
Denys Vlasenko | 71016ba | 2009-06-05 16:24:29 +0200 | [diff] [blame] | 618 | * token was a number, or a right paren (which pretends to be |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 619 | * a number, since it evaluates to one). Think about it. |
| 620 | * It makes sense. */ |
| 621 | if (lasttok != TOK_NUM) { |
| 622 | switch (op) { |
| 623 | case TOK_ADD: |
| 624 | op = TOK_UPLUS; |
| 625 | break; |
| 626 | case TOK_SUB: |
| 627 | op = TOK_UMINUS; |
| 628 | break; |
| 629 | case TOK_POST_INC: |
| 630 | op = TOK_PRE_INC; |
| 631 | break; |
| 632 | case TOK_POST_DEC: |
| 633 | op = TOK_PRE_DEC; |
| 634 | break; |
| 635 | } |
| 636 | } |
Denys Vlasenko | 71016ba | 2009-06-05 16:24:29 +0200 | [diff] [blame] | 637 | /* We don't want an unary operator to cause recursive descent on the |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 638 | * stack, because there can be many in a row and it could cause an |
| 639 | * operator to be evaluated before its argument is pushed onto the |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 640 | * integer stack. |
| 641 | * But for binary operators, "apply" everything on the operator |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 642 | * stack until we find an operator with a lesser priority than the |
Denys Vlasenko | 395b97a | 2010-09-17 18:02:17 +0200 | [diff] [blame] | 643 | * one we have just extracted. If op is right-associative, |
| 644 | * then stop "applying" on the equal priority too. |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 645 | * Left paren is given the lowest priority so it will never be |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 646 | * "applied" in this way. |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 647 | */ |
| 648 | prec = PREC(op); |
| 649 | if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) { |
| 650 | /* not left paren or unary */ |
| 651 | if (lasttok != TOK_NUM) { |
| 652 | /* binary op must be preceded by a num */ |
| 653 | goto err; |
| 654 | } |
| 655 | while (stackptr != stack) { |
Denys Vlasenko | 51850c8 | 2010-09-13 01:09:11 +0200 | [diff] [blame] | 656 | operator prev_op = *--stackptr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 657 | if (op == TOK_RPAREN) { |
| 658 | /* The algorithm employed here is simple: while we don't |
| 659 | * hit an open paren nor the bottom of the stack, pop |
| 660 | * tokens and apply them */ |
Denys Vlasenko | 51850c8 | 2010-09-13 01:09:11 +0200 | [diff] [blame] | 661 | if (prev_op == TOK_LPAREN) { |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 662 | /* Any operator directly after a |
| 663 | * close paren should consider itself binary */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 664 | lasttok = TOK_NUM; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 665 | goto next; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 666 | } |
| 667 | } else { |
Denys Vlasenko | 51850c8 | 2010-09-13 01:09:11 +0200 | [diff] [blame] | 668 | operator prev_prec = PREC(prev_op); |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 669 | fix_assignment_prec(prec); |
| 670 | fix_assignment_prec(prev_prec); |
Denys Vlasenko | 51850c8 | 2010-09-13 01:09:11 +0200 | [diff] [blame] | 671 | if (prev_prec < prec |
Denys Vlasenko | bd14770 | 2010-09-13 11:11:40 +0200 | [diff] [blame] | 672 | || (prev_prec == prec && is_right_associative(prec)) |
Denys Vlasenko | 51850c8 | 2010-09-13 01:09:11 +0200 | [diff] [blame] | 673 | ) { |
| 674 | stackptr++; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 675 | break; |
Denys Vlasenko | 51850c8 | 2010-09-13 01:09:11 +0200 | [diff] [blame] | 676 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 677 | } |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 678 | errmsg = arith_apply(math_state, prev_op, numstack, &numstackptr); |
| 679 | if (errmsg) |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 680 | goto err_with_custom_msg; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 681 | } |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 682 | if (op == TOK_RPAREN) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 683 | goto err; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 686 | /* Push this operator to the stack and remember it */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 687 | *stackptr++ = lasttok = op; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 688 | next: ; |
| 689 | } /* while (1) */ |
| 690 | |
| 691 | err: |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 692 | errmsg = "arithmetic syntax error"; |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 693 | err_with_custom_msg: |
| 694 | numstack->val = -1; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 695 | ret: |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 696 | math_state->errmsg = errmsg; |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 697 | return numstack->val; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 700 | arith_t FAST_FUNC |
| 701 | arith(arith_state_t *math_state, const char *expr) |
| 702 | { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 703 | math_state->errmsg = NULL; |
Denys Vlasenko | 0eac8ff | 2010-09-13 12:49:52 +0200 | [diff] [blame] | 704 | math_state->list_of_recursed_names = NULL; |
| 705 | return evaluate_string(math_state, expr); |
| 706 | } |
| 707 | |
Denis Vlasenko | cc8289d | 2009-04-03 21:13:31 +0000 | [diff] [blame] | 708 | /* |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 709 | * Copyright (c) 1989, 1991, 1993, 1994 |
| 710 | * The Regents of the University of California. All rights reserved. |
| 711 | * |
| 712 | * This code is derived from software contributed to Berkeley by |
| 713 | * Kenneth Almquist. |
| 714 | * |
| 715 | * Redistribution and use in source and binary forms, with or without |
| 716 | * modification, are permitted provided that the following conditions |
| 717 | * are met: |
| 718 | * 1. Redistributions of source code must retain the above copyright |
| 719 | * notice, this list of conditions and the following disclaimer. |
| 720 | * 2. Redistributions in binary form must reproduce the above copyright |
| 721 | * notice, this list of conditions and the following disclaimer in the |
| 722 | * documentation and/or other materials provided with the distribution. |
| 723 | * 3. Neither the name of the University nor the names of its contributors |
| 724 | * may be used to endorse or promote products derived from this software |
| 725 | * without specific prior written permission. |
| 726 | * |
| 727 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 728 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 729 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 730 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 731 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 732 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 733 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 734 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 735 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 736 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 737 | * SUCH DAMAGE. |
| 738 | */ |