2 * arithmetic code ripped out of ash shell for code sharing
4 * This code is derived from software contributed to Berkeley by
7 * Original BSD copyright notice is retained at the end of this file.
9 * Copyright (c) 1989, 1991, 1993, 1994
10 * The Regents of the University of California. All rights reserved.
12 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
13 * was re-ported from NetBSD and debianized.
15 * rewrite arith.y to micro stack based cryptic algorithm by
16 * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
18 * Modified by Paul Mundt <lethal@linux-sh.org> (c) 2004 to support
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.
26 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
28 /* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
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:
38 The above copyright notice and this permission notice shall be
39 included in all copies or substantial portions of the Software.
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.
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
55 * be that which POSIX specifies for shells.
57 * The code uses a simple two-stack algorithm. See
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
64 * To use the routine, call it with an expression string and error return
69 * Aug 24, 2001 Manuel Novoa III
71 * Reduced the generated code size by about 30% (i386) and fixed several bugs.
73 * 1) In arith_apply():
74 * a) Cached values of *numptr and &(numptr[-1]).
75 * b) Removed redundant test for zero denominator.
78 * a) Eliminated redundant code for processing operator tokens by moving
79 * to a table-based implementation. Also folded handling of parens
81 * b) Combined all 3 loops which called arith_apply to reduce generated
82 * code size at the cost of speed.
84 * 3) The following expressions were treated as valid by the original code:
85 * 1() , 0! , 1 ( *3 ) .
86 * These bugs have been fixed by internally enclosing the expression in
87 * parens and then checking that all binary ops and right parens are
88 * preceded by a valid expression (NUM_TOKEN).
90 * Note: It may be desirable to replace Aaron's test for whitespace with
91 * ctype's isspace() if it is used by another busybox applet or if additional
92 * whitespace chars should be considered. Look below the "#include"s for a
96 * Aug 26, 2001 Manuel Novoa III
98 * Return 0 for null expressions. Pointed out by Vladimir Oleynik.
100 * Merge in Aaron's comments previously posted to the busybox list,
101 * modified slightly to take account of my changes to the code.
105 * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
107 * - allow access to variable,
108 * use recursive value indirection: c="2*2"; a="c"; echo $((a+=2)) produce 6
109 * - implement assign syntax (VAR=expr, +=, *= etc)
110 * - implement exponentiation (** operator)
111 * - implement comma separated - expr, expr
112 * - implement ++expr --expr expr++ expr--
113 * - implement expr ? expr : expr (but second expr is always calculated)
114 * - allow hexadecimal and octal numbers
115 * - restore lost XOR operator
116 * - protect $((num num)) as true zero expr (Manuel's error)
117 * - always use special isspace(), see comment from bash ;-)
122 #define a_e_h_t arith_eval_hooks_t
123 #define lookupvar (math_hooks->lookupvar)
124 #define setvar (math_hooks->setvar )
125 //#define endofname (math_hooks->endofname)
127 typedef unsigned char operator;
129 /* An operator's token id is a bit of a bitfield. The lower 5 bits are the
130 * precedence, and 3 high bits are an ID unique across operators of that
131 * precedence. The ID portion is so that multiple operators can have the
132 * same precedence, ensuring that the leftmost one is evaluated first.
135 #define tok_decl(prec,id) (((id)<<5) | (prec))
136 #define PREC(op) ((op) & 0x1F)
138 #define TOK_LPAREN tok_decl(0,0)
140 #define TOK_COMMA tok_decl(1,0)
142 /* All assignments are right associative and have the same precedence,
143 * but there are 11 of them, which doesn't fit into 3 bits for unique id.
144 * Abusing another precedence level:
146 #define TOK_ASSIGN tok_decl(2,0)
147 #define TOK_AND_ASSIGN tok_decl(2,1)
148 #define TOK_OR_ASSIGN tok_decl(2,2)
149 #define TOK_XOR_ASSIGN tok_decl(2,3)
150 #define TOK_PLUS_ASSIGN tok_decl(2,4)
151 #define TOK_MINUS_ASSIGN tok_decl(2,5)
152 #define TOK_LSHIFT_ASSIGN tok_decl(2,6)
153 #define TOK_RSHIFT_ASSIGN tok_decl(2,7)
155 #define TOK_MUL_ASSIGN tok_decl(3,0)
156 #define TOK_DIV_ASSIGN tok_decl(3,1)
157 #define TOK_REM_ASSIGN tok_decl(3,2)
159 #define fix_assignment_prec(prec) do { if (prec == 3) prec = 2; } while (0)
161 /* ternary conditional operator is right associative too */
162 #define TOK_CONDITIONAL tok_decl(4,0)
163 #define TOK_CONDITIONAL_SEP tok_decl(4,1)
165 #define TOK_OR tok_decl(5,0)
167 #define TOK_AND tok_decl(6,0)
169 #define TOK_BOR tok_decl(7,0)
171 #define TOK_BXOR tok_decl(8,0)
173 #define TOK_BAND tok_decl(9,0)
175 #define TOK_EQ tok_decl(10,0)
176 #define TOK_NE tok_decl(10,1)
178 #define TOK_LT tok_decl(11,0)
179 #define TOK_GT tok_decl(11,1)
180 #define TOK_GE tok_decl(11,2)
181 #define TOK_LE tok_decl(11,3)
183 #define TOK_LSHIFT tok_decl(12,0)
184 #define TOK_RSHIFT tok_decl(12,1)
186 #define TOK_ADD tok_decl(13,0)
187 #define TOK_SUB tok_decl(13,1)
189 #define TOK_MUL tok_decl(14,0)
190 #define TOK_DIV tok_decl(14,1)
191 #define TOK_REM tok_decl(14,2)
193 /* exponent is right associative */
194 #define TOK_EXPONENT tok_decl(15,1)
196 /* unary operators */
198 #define TOK_BNOT tok_decl(UNARYPREC,0)
199 #define TOK_NOT tok_decl(UNARYPREC,1)
201 #define TOK_UMINUS tok_decl(UNARYPREC+1,0)
202 #define TOK_UPLUS tok_decl(UNARYPREC+1,1)
204 #define PREC_PRE (UNARYPREC+2)
206 #define TOK_PRE_INC tok_decl(PREC_PRE, 0)
207 #define TOK_PRE_DEC tok_decl(PREC_PRE, 1)
209 #define PREC_POST (UNARYPREC+3)
211 #define TOK_POST_INC tok_decl(PREC_POST, 0)
212 #define TOK_POST_DEC tok_decl(PREC_POST, 1)
214 #define SPEC_PREC (UNARYPREC+4)
216 #define TOK_NUM tok_decl(SPEC_PREC, 0)
217 #define TOK_RPAREN tok_decl(SPEC_PREC, 1)
220 tok_have_assign(operator op)
222 operator prec = PREC(op);
224 fix_assignment_prec(prec);
225 return (prec == PREC(TOK_ASSIGN) ||
226 prec == PREC_PRE || prec == PREC_POST);
230 is_right_associative(operator prec)
232 return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT)
233 || prec == PREC(TOK_CONDITIONAL));
238 arith_t contidional_second_val;
239 char contidional_second_val_initialized;
240 char *var; /* if NULL then is regular number,
241 else is variable name */
244 typedef struct chk_var_recursive_looped_t {
246 struct chk_var_recursive_looped_t *next;
247 } chk_var_recursive_looped_t;
249 static chk_var_recursive_looped_t *prev_chk_var_recursive;
252 arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks)
255 const char *p = lookupvar(t->var);
259 chk_var_recursive_looped_t *cur;
260 chk_var_recursive_looped_t cur_save;
262 /* recursively try p as expression */
264 for (cur = prev_chk_var_recursive; cur; cur = cur->next) {
265 if (strcmp(cur->var, t->var) == 0) {
266 /* expression recursion loop detected */
270 /* save current var name */
271 cur = prev_chk_var_recursive;
272 cur_save.var = t->var;
274 prev_chk_var_recursive = &cur_save;
276 t->val = arith(p, &errcode, math_hooks);
277 /* restore previous ptr after recursion */
278 prev_chk_var_recursive = cur;
281 /* allow undefined var as 0 */
287 /* "Applying" a token means performing it on the top elements on the integer
288 * stack. For an unary operator it will only change the top element, but a
289 * binary operator will pop two arguments and push the result */
291 arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr, a_e_h_t *math_hooks)
293 #define NUMPTR (*numstackptr)
296 arith_t numptr_val, rez;
297 int ret_arith_lookup_val;
299 /* There is no operator that can work without arguments */
300 if (NUMPTR == numstack)
302 numptr_m1 = NUMPTR - 1;
304 /* Check operand is var with noninteger value */
305 ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks);
306 if (ret_arith_lookup_val)
307 return ret_arith_lookup_val;
309 rez = numptr_m1->val;
310 if (op == TOK_UMINUS)
312 else if (op == TOK_NOT)
314 else if (op == TOK_BNOT)
316 else if (op == TOK_POST_INC || op == TOK_PRE_INC)
318 else if (op == TOK_POST_DEC || op == TOK_PRE_DEC)
320 else if (op != TOK_UPLUS) {
321 /* Binary operators */
323 /* check and binary operators need two arguments */
324 if (numptr_m1 == numstack) goto err;
326 /* ... and they pop one */
329 if (op == TOK_CONDITIONAL) {
330 if (!numptr_m1->contidional_second_val_initialized) {
331 /* protect $((expr1 ? expr2)) without ": expr" */
334 rez = numptr_m1->contidional_second_val;
335 } else if (numptr_m1->contidional_second_val_initialized) {
336 /* protect $((expr1 : expr2)) without "expr ? " */
339 numptr_m1 = NUMPTR - 1;
340 if (op != TOK_ASSIGN) {
341 /* check operand is var with noninteger value for not '=' */
342 ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks);
343 if (ret_arith_lookup_val)
344 return ret_arith_lookup_val;
346 if (op == TOK_CONDITIONAL) {
347 numptr_m1->contidional_second_val = rez;
349 rez = numptr_m1->val;
350 if (op == TOK_BOR || op == TOK_OR_ASSIGN)
352 else if (op == TOK_OR)
353 rez = numptr_val || rez;
354 else if (op == TOK_BAND || op == TOK_AND_ASSIGN)
356 else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN)
358 else if (op == TOK_AND)
359 rez = rez && numptr_val;
360 else if (op == TOK_EQ)
361 rez = (rez == numptr_val);
362 else if (op == TOK_NE)
363 rez = (rez != numptr_val);
364 else if (op == TOK_GE)
365 rez = (rez >= numptr_val);
366 else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN)
368 else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN)
370 else if (op == TOK_GT)
371 rez = (rez > numptr_val);
372 else if (op == TOK_LT)
373 rez = (rez < numptr_val);
374 else if (op == TOK_LE)
375 rez = (rez <= numptr_val);
376 else if (op == TOK_MUL || op == TOK_MUL_ASSIGN)
378 else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN)
380 else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN)
382 else if (op == TOK_ASSIGN || op == TOK_COMMA)
384 else if (op == TOK_CONDITIONAL_SEP) {
385 if (numptr_m1 == numstack) {
386 /* protect $((expr : expr)) without "expr ? " */
389 numptr_m1->contidional_second_val_initialized = op;
390 numptr_m1->contidional_second_val = numptr_val;
391 } else if (op == TOK_CONDITIONAL) {
393 numptr_val : numptr_m1->contidional_second_val;
394 } else if (op == TOK_EXPONENT) {
397 return -3; /* exponent less than 0 */
399 while (--numptr_val >= 0)
402 } else if (numptr_val==0) /* zero divisor check */
404 else if (op == TOK_DIV || op == TOK_DIV_ASSIGN)
406 else if (op == TOK_REM || op == TOK_REM_ASSIGN)
409 if (tok_have_assign(op)) {
410 char buf[sizeof(arith_t)*3 + 2];
412 if (numptr_m1->var == NULL) {
416 /* save to shell variable */
417 sprintf(buf, arith_t_fmt, rez);
418 setvar(numptr_m1->var, buf);
419 /* after saving, make previous value for v++ or v-- */
420 if (op == TOK_POST_INC)
422 else if (op == TOK_POST_DEC)
425 numptr_m1->val = rez;
426 /* erase var name, it is just a number now */
427 numptr_m1->var = NULL;
434 /* longest must be first */
435 static const char op_tokens[] ALIGN1 = {
436 '<','<','=',0, TOK_LSHIFT_ASSIGN,
437 '>','>','=',0, TOK_RSHIFT_ASSIGN,
438 '<','<', 0, TOK_LSHIFT,
439 '>','>', 0, TOK_RSHIFT,
446 '|','=', 0, TOK_OR_ASSIGN,
447 '&','=', 0, TOK_AND_ASSIGN,
448 '*','=', 0, TOK_MUL_ASSIGN,
449 '/','=', 0, TOK_DIV_ASSIGN,
450 '%','=', 0, TOK_REM_ASSIGN,
451 '+','=', 0, TOK_PLUS_ASSIGN,
452 '-','=', 0, TOK_MINUS_ASSIGN,
453 '-','-', 0, TOK_POST_DEC,
454 '^','=', 0, TOK_XOR_ASSIGN,
455 '+','+', 0, TOK_POST_INC,
456 '*','*', 0, TOK_EXPONENT,
472 '?', 0, TOK_CONDITIONAL,
473 ':', 0, TOK_CONDITIONAL_SEP,
478 #define ptr_to_rparen (&op_tokens[sizeof(op_tokens)-7])
480 const char* FAST_FUNC
481 endofname(const char *name)
486 if (!is_in_name(*name))
493 arith(const char *expr, int *perrcode, a_e_h_t *math_hooks)
497 const char *start_expr = expr = skip_whitespace(expr);
498 unsigned expr_len = strlen(expr) + 2;
499 /* Stack of integers */
500 /* The proof that there can be no more than strlen(startbuf)/2+1 integers
501 * in any given correct or incorrect expression is left as an exercise to
503 v_n_t *const numstack = alloca((expr_len / 2) * sizeof(numstack[0]));
504 v_n_t *numstackptr = numstack;
505 /* Stack of operator tokens */
506 operator *const stack = alloca(expr_len * sizeof(stack[0]));
507 operator *stackptr = stack;
509 *stackptr++ = lasttok = TOK_LPAREN; /* start off with a left paren */
518 expr = skip_whitespace(expr);
520 if (arithval == '\0') {
521 if (expr == start_expr) {
522 /* Null expression. */
527 /* This is only reached after all tokens have been extracted from the
528 * input stream. If there are still tokens on the operator stack, they
529 * are to be applied in order. At the end, there should be a final
530 * result on the integer stack */
532 if (expr != ptr_to_rparen + 1) {
533 /* If we haven't done so already,
534 * append a closing right paren
535 * and let the loop process it */
536 expr = ptr_to_rparen;
539 /* At this point, we're done with the expression */
540 if (numstackptr != numstack + 1) {
541 /* ...but if there isn't, it's bad */
545 /* expression is $((var)) only, lookup now */
546 errcode = arith_lookup_val(numstack, math_hooks);
554 size_t var_name_size = (p-expr) + 1; /* +1 for NUL */
555 numstackptr->var = alloca(var_name_size);
556 safe_strncpy(numstackptr->var, expr, var_name_size);
559 numstackptr->contidional_second_val_initialized = 0;
565 if (isdigit(arithval)) {
567 numstackptr->var = NULL;
569 numstackptr->val = strto_arith_t(expr, (char**) &expr, 0);
571 numstackptr->val = 0; /* bash compat */
575 /* Should be an operator */
578 const char *e = expr;
579 /* Compare expr to current op_tokens[] element */
580 while (*p && *e == *p)
582 if (*p == '\0') { /* match: operator is found */
586 /* Go to next element of op_tokens[] */
589 p += 2; /* skip NUL and TOK_foo bytes */
590 if (*p == '\0') /* no next element, operator not found */
593 op = p[1]; /* fetch TOK_foo value */
594 /* NB: expr now points past the operator */
596 /* post grammar: a++ reduce to num */
597 if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC)
600 /* Plus and minus are binary (not unary) _only_ if the last
601 * token was a number, or a right paren (which pretends to be
602 * a number, since it evaluates to one). Think about it.
604 if (lasttok != TOK_NUM) {
620 /* We don't want an unary operator to cause recursive descent on the
621 * stack, because there can be many in a row and it could cause an
622 * operator to be evaluated before its argument is pushed onto the
624 * But for binary operators, "apply" everything on the operator
625 * stack until we find an operator with a lesser priority than the
626 * one we have just extracted.
627 * Left paren is given the lowest priority so it will never be
628 * "applied" in this way.
629 * if associativity is right and priority eq, applied also skip
632 if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
633 /* not left paren or unary */
634 if (lasttok != TOK_NUM) {
635 /* binary op must be preceded by a num */
638 while (stackptr != stack) {
639 operator prev_op = *--stackptr;
640 if (op == TOK_RPAREN) {
641 /* The algorithm employed here is simple: while we don't
642 * hit an open paren nor the bottom of the stack, pop
643 * tokens and apply them */
644 if (prev_op == TOK_LPAREN) {
645 /* Any operator directly after a
646 * close paren should consider itself binary */
651 operator prev_prec = PREC(prev_op);
652 fix_assignment_prec(prec);
653 fix_assignment_prec(prev_prec);
655 || (prev_prec == prec && is_right_associative(prec))
661 errcode = arith_apply(prev_op, numstack, &numstackptr, math_hooks);
665 if (op == TOK_RPAREN) {
670 /* Push this operator to the stack and remember it. */
671 *stackptr++ = lasttok = op;
676 numstack->val = errcode = -1;
679 return numstack->val;
683 * Copyright (c) 1989, 1991, 1993, 1994
684 * The Regents of the University of California. All rights reserved.
686 * This code is derived from software contributed to Berkeley by
689 * Redistribution and use in source and binary forms, with or without
690 * modification, are permitted provided that the following conditions
692 * 1. Redistributions of source code must retain the above copyright
693 * notice, this list of conditions and the following disclaimer.
694 * 2. Redistributions in binary form must reproduce the above copyright
695 * notice, this list of conditions and the following disclaimer in the
696 * documentation and/or other materials provided with the distribution.
697 * 3. Neither the name of the University nor the names of its contributors
698 * may be used to endorse or promote products derived from this software
699 * without specific prior written permission.
701 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
702 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
703 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
704 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
705 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
706 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
707 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
708 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
709 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
710 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF