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
68 * Aug 24, 2001 Manuel Novoa III
70 * Reduced the generated code size by about 30% (i386) and fixed several bugs.
72 * 1) In arith_apply():
73 * a) Cached values of *numptr and &(numptr[-1]).
74 * b) Removed redundant test for zero denominator.
77 * a) Eliminated redundant code for processing operator tokens by moving
78 * to a table-based implementation. Also folded handling of parens
80 * b) Combined all 3 loops which called arith_apply to reduce generated
81 * code size at the cost of speed.
83 * 3) The following expressions were treated as valid by the original code:
84 * 1() , 0! , 1 ( *3 ) .
85 * These bugs have been fixed by internally enclosing the expression in
86 * parens and then checking that all binary ops and right parens are
87 * preceded by a valid expression (NUM_TOKEN).
89 * Note: It may be desirable to replace Aaron's test for whitespace with
90 * ctype's isspace() if it is used by another busybox applet or if additional
91 * whitespace chars should be considered. Look below the "#include"s for a
95 * Aug 26, 2001 Manuel Novoa III
97 * Return 0 for null expressions. Pointed out by Vladimir Oleynik.
99 * Merge in Aaron's comments previously posted to the busybox list,
100 * modified slightly to take account of my changes to the code.
104 * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
106 * - allow access to variable,
107 * used recursive find value indirection (c=2*2; a="c"; $((a+=2)) produce 6)
108 * - realize assign syntax (VAR=expr, +=, *= etc)
109 * - realize exponentiation (** operator)
110 * - realize comma separated - expr, expr
111 * - realise ++expr --expr expr++ expr--
112 * - realise expr ? expr : expr (but, second expr calculate always)
113 * - allow hexadecimal and octal numbers
114 * - was restored loses XOR operator
115 * - remove one goto label, added three ;-)
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.
133 * Consider * and /. */
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 #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)
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)
155 /* all assign is right associativity and precedence eq, but (7+3)<<5 > 256 */
156 #define convert_prec_is_assign(prec) do { if (prec == 3) prec = 2; } while (0)
158 /* conditional is right associativity too */
159 #define TOK_CONDITIONAL tok_decl(4,0)
160 #define TOK_CONDITIONAL_SEP tok_decl(4,1)
162 #define TOK_OR tok_decl(5,0)
164 #define TOK_AND tok_decl(6,0)
166 #define TOK_BOR tok_decl(7,0)
168 #define TOK_BXOR tok_decl(8,0)
170 #define TOK_BAND tok_decl(9,0)
172 #define TOK_EQ tok_decl(10,0)
173 #define TOK_NE tok_decl(10,1)
175 #define TOK_LT tok_decl(11,0)
176 #define TOK_GT tok_decl(11,1)
177 #define TOK_GE tok_decl(11,2)
178 #define TOK_LE tok_decl(11,3)
180 #define TOK_LSHIFT tok_decl(12,0)
181 #define TOK_RSHIFT tok_decl(12,1)
183 #define TOK_ADD tok_decl(13,0)
184 #define TOK_SUB tok_decl(13,1)
186 #define TOK_MUL tok_decl(14,0)
187 #define TOK_DIV tok_decl(14,1)
188 #define TOK_REM tok_decl(14,2)
190 /* exponent is right associativity */
191 #define TOK_EXPONENT tok_decl(15,1)
193 /* For now unary operators. */
195 #define TOK_BNOT tok_decl(UNARYPREC,0)
196 #define TOK_NOT tok_decl(UNARYPREC,1)
198 #define TOK_UMINUS tok_decl(UNARYPREC+1,0)
199 #define TOK_UPLUS tok_decl(UNARYPREC+1,1)
201 #define PREC_PRE (UNARYPREC+2)
203 #define TOK_PRE_INC tok_decl(PREC_PRE, 0)
204 #define TOK_PRE_DEC tok_decl(PREC_PRE, 1)
206 #define PREC_POST (UNARYPREC+3)
208 #define TOK_POST_INC tok_decl(PREC_POST, 0)
209 #define TOK_POST_DEC tok_decl(PREC_POST, 1)
211 #define SPEC_PREC (UNARYPREC+4)
213 #define TOK_NUM tok_decl(SPEC_PREC, 0)
214 #define TOK_RPAREN tok_decl(SPEC_PREC, 1)
216 #define NUMPTR (*numstackptr)
219 tok_have_assign(operator op)
221 operator prec = PREC(op);
223 convert_prec_is_assign(prec);
224 return (prec == PREC(TOK_ASSIGN) ||
225 prec == PREC_PRE || prec == PREC_POST);
229 is_right_associativity(operator prec)
231 return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT)
232 || prec == PREC(TOK_CONDITIONAL));
237 arith_t contidional_second_val;
238 char contidional_second_val_initialized;
239 char *var; /* if NULL then is regular number,
240 else is variable name */
243 typedef struct chk_var_recursive_looped_t {
245 struct chk_var_recursive_looped_t *next;
246 } chk_var_recursive_looped_t;
248 static chk_var_recursive_looped_t *prev_chk_var_recursive;
251 arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks)
254 const char *p = lookupvar(t->var);
259 /* recursive try as expression */
260 chk_var_recursive_looped_t *cur;
261 chk_var_recursive_looped_t cur_save;
263 for (cur = prev_chk_var_recursive; cur; cur = cur->next) {
264 if (strcmp(cur->var, t->var) == 0) {
265 /* expression recursion loop detected */
269 /* save current lookuped var name */
270 cur = prev_chk_var_recursive;
271 cur_save.var = t->var;
273 prev_chk_var_recursive = &cur_save;
275 t->val = arith (p, &errcode, math_hooks);
276 /* restore previous ptr after recursiving */
277 prev_chk_var_recursive = cur;
280 /* allow undefined var as 0 */
286 /* "applying" a token means performing it on the top elements on the integer
287 * stack. For a unary operator it will only change the top element, but a
288 * binary operator will pop two arguments and push a result */
290 arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr, a_e_h_t *math_hooks)
293 arith_t numptr_val, rez;
294 int ret_arith_lookup_val;
296 /* There is no operator that can work without arguments */
297 if (NUMPTR == numstack) goto err;
298 numptr_m1 = NUMPTR - 1;
300 /* check operand is var with noninteger value */
301 ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks);
302 if (ret_arith_lookup_val)
303 return ret_arith_lookup_val;
305 rez = numptr_m1->val;
306 if (op == TOK_UMINUS)
308 else if (op == TOK_NOT)
310 else if (op == TOK_BNOT)
312 else if (op == TOK_POST_INC || op == TOK_PRE_INC)
314 else if (op == TOK_POST_DEC || op == TOK_PRE_DEC)
316 else if (op != TOK_UPLUS) {
317 /* Binary operators */
319 /* check and binary operators need two arguments */
320 if (numptr_m1 == numstack) goto err;
322 /* ... and they pop one */
325 if (op == TOK_CONDITIONAL) {
326 if (!numptr_m1->contidional_second_val_initialized) {
327 /* protect $((expr1 ? expr2)) without ": expr" */
330 rez = numptr_m1->contidional_second_val;
331 } else if (numptr_m1->contidional_second_val_initialized) {
332 /* protect $((expr1 : expr2)) without "expr ? " */
335 numptr_m1 = NUMPTR - 1;
336 if (op != TOK_ASSIGN) {
337 /* check operand is var with noninteger value for not '=' */
338 ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks);
339 if (ret_arith_lookup_val)
340 return ret_arith_lookup_val;
342 if (op == TOK_CONDITIONAL) {
343 numptr_m1->contidional_second_val = rez;
345 rez = numptr_m1->val;
346 if (op == TOK_BOR || op == TOK_OR_ASSIGN)
348 else if (op == TOK_OR)
349 rez = numptr_val || rez;
350 else if (op == TOK_BAND || op == TOK_AND_ASSIGN)
352 else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN)
354 else if (op == TOK_AND)
355 rez = rez && numptr_val;
356 else if (op == TOK_EQ)
357 rez = (rez == numptr_val);
358 else if (op == TOK_NE)
359 rez = (rez != numptr_val);
360 else if (op == TOK_GE)
361 rez = (rez >= numptr_val);
362 else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN)
364 else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN)
366 else if (op == TOK_GT)
367 rez = (rez > numptr_val);
368 else if (op == TOK_LT)
369 rez = (rez < numptr_val);
370 else if (op == TOK_LE)
371 rez = (rez <= numptr_val);
372 else if (op == TOK_MUL || op == TOK_MUL_ASSIGN)
374 else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN)
376 else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN)
378 else if (op == TOK_ASSIGN || op == TOK_COMMA)
380 else if (op == TOK_CONDITIONAL_SEP) {
381 if (numptr_m1 == numstack) {
382 /* protect $((expr : expr)) without "expr ? " */
385 numptr_m1->contidional_second_val_initialized = op;
386 numptr_m1->contidional_second_val = numptr_val;
387 } else if (op == TOK_CONDITIONAL) {
389 numptr_val : numptr_m1->contidional_second_val;
390 } else if (op == TOK_EXPONENT) {
392 return -3; /* exponent less than 0 */
401 } else if (numptr_val==0) /* zero divisor check */
403 else if (op == TOK_DIV || op == TOK_DIV_ASSIGN)
405 else if (op == TOK_REM || op == TOK_REM_ASSIGN)
408 if (tok_have_assign(op)) {
409 char buf[sizeof(arith_t)*3 + 2];
411 if (numptr_m1->var == NULL) {
415 /* save to shell variable */
416 sprintf(buf, arith_t_fmt, rez);
417 setvar(numptr_m1->var, buf);
418 /* after saving, make previous value for v++ or v-- */
419 if (op == TOK_POST_INC)
421 else if (op == TOK_POST_DEC)
424 numptr_m1->val = rez;
425 /* protect geting var value, is number now */
426 numptr_m1->var = NULL;
432 /* longest must be first */
433 static const char op_tokens[] ALIGN1 = {
434 '<','<','=',0, TOK_LSHIFT_ASSIGN,
435 '>','>','=',0, TOK_RSHIFT_ASSIGN,
436 '<','<', 0, TOK_LSHIFT,
437 '>','>', 0, TOK_RSHIFT,
444 '|','=', 0, TOK_OR_ASSIGN,
445 '&','=', 0, TOK_AND_ASSIGN,
446 '*','=', 0, TOK_MUL_ASSIGN,
447 '/','=', 0, TOK_DIV_ASSIGN,
448 '%','=', 0, TOK_REM_ASSIGN,
449 '+','=', 0, TOK_PLUS_ASSIGN,
450 '-','=', 0, TOK_MINUS_ASSIGN,
451 '-','-', 0, TOK_POST_DEC,
452 '^','=', 0, TOK_XOR_ASSIGN,
453 '+','+', 0, TOK_POST_INC,
454 '*','*', 0, TOK_EXPONENT,
470 '?', 0, TOK_CONDITIONAL,
471 ':', 0, TOK_CONDITIONAL_SEP,
477 #define ptr_to_rparen (&op_tokens[sizeof(op_tokens)-7])
479 const char* FAST_FUNC
480 endofname(const char *name)
485 if (!is_in_name(*name))
492 arith(const char *expr, int *perrcode, a_e_h_t *math_hooks)
496 const char *start_expr = expr = skip_whitespace(expr);
497 unsigned expr_len = strlen(expr) + 2;
498 /* Stack of integers */
499 /* The proof that there can be no more than strlen(startbuf)/2+1 integers
500 * in any given correct or incorrect expression is left as an exercise to
502 v_n_t *const numstack = alloca((expr_len / 2) * sizeof(numstack[0]));
503 v_n_t *numstackptr = numstack;
504 /* Stack of operator tokens */
505 operator *const stack = alloca(expr_len * sizeof(stack[0]));
506 operator *stackptr = stack;
508 *stackptr++ = lasttok = TOK_LPAREN; /* start off with a left paren */
517 expr = skip_whitespace(expr);
519 if (arithval == '\0') {
520 if (expr == start_expr) {
521 /* Null expression. */
526 /* This is only reached after all tokens have been extracted from the
527 * input stream. If there are still tokens on the operator stack, they
528 * are to be applied in order. At the end, there should be a final
529 * result on the integer stack */
531 if (expr != ptr_to_rparen + 1) {
532 /* If we haven't done so already, */
533 /* append a closing right paren */
534 expr = ptr_to_rparen;
535 /* and let the loop process it. */
538 /* At this point, we're done with the expression. */
539 if (numstackptr != numstack + 1) {
540 /* ... but if there isn't, it's bad */
544 /* expression is $((var)) only, lookup now */
545 errcode = arith_lookup_val(numstack, math_hooks);
553 size_t var_name_size = (p-expr) + 1; /* +1 for NUL */
554 numstackptr->var = alloca(var_name_size);
555 safe_strncpy(numstackptr->var, expr, var_name_size);
558 numstackptr->contidional_second_val_initialized = 0;
564 if (isdigit(arithval)) {
566 numstackptr->var = NULL;
568 numstackptr->val = strto_arith_t(expr, (char**) &expr, 0);
570 numstackptr->val = 0; /* bash compat */
574 /* Should be an operator */
577 const char *e = expr;
578 /* Compare expr to current op_tokens[] element */
579 while (*p && *e == *p)
581 if (*p == '\0') { /* match: operator is found */
585 /* Go to next element of op_tokens[] */
588 p += 2; /* skip NUL and TOK_foo bytes */
589 if (*p == '\0') /* no next element, operator not found */
592 op = p[1]; /* fetch TOK_foo value */
593 /* NB: expr now points past the operator */
595 /* post grammar: a++ reduce to num */
596 if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC)
599 /* Plus and minus are binary (not unary) _only_ if the last
600 * token was a number, or a right paren (which pretends to be
601 * a number, since it evaluates to one). Think about it.
603 if (lasttok != TOK_NUM) {
619 /* We don't want an unary operator to cause recursive descent on the
620 * stack, because there can be many in a row and it could cause an
621 * operator to be evaluated before its argument is pushed onto the
623 /* But for binary operators, "apply" everything on the operator
624 * stack until we find an operator with a lesser priority than the
625 * one we have just extracted. */
626 /* Left paren is given the lowest priority so it will never be
627 * "applied" in this way.
628 * if associativity is right and priority eq, applied also skip
631 if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
632 /* not left paren or unary */
633 if (lasttok != TOK_NUM) {
634 /* binary op must be preceded by a num */
637 while (stackptr != stack) {
638 if (op == TOK_RPAREN) {
639 /* The algorithm employed here is simple: while we don't
640 * hit an open paren nor the bottom of the stack, pop
641 * tokens and apply them */
642 if (stackptr[-1] == TOK_LPAREN) {
644 /* Any operator directly after a */
646 /* close paren should consider itself binary */
650 operator prev_prec = PREC(stackptr[-1]);
651 convert_prec_is_assign(prec);
652 convert_prec_is_assign(prev_prec);
653 if (prev_prec < prec)
655 /* check right assoc */
656 if (prev_prec == prec && is_right_associativity(prec))
659 errcode = arith_apply(*--stackptr, numstack, &numstackptr, math_hooks);
663 if (op == TOK_RPAREN) {
668 /* Push this operator to the stack and remember it. */
669 *stackptr++ = lasttok = op;
674 numstack->val = errcode = -1;
677 return numstack->val;
681 * Copyright (c) 1989, 1991, 1993, 1994
682 * The Regents of the University of California. All rights reserved.
684 * This code is derived from software contributed to Berkeley by
687 * Redistribution and use in source and binary forms, with or without
688 * modification, are permitted provided that the following conditions
690 * 1. Redistributions of source code must retain the above copyright
691 * notice, this list of conditions and the following disclaimer.
692 * 2. Redistributions in binary form must reproduce the above copyright
693 * notice, this list of conditions and the following disclaimer in the
694 * documentation and/or other materials provided with the distribution.
695 * 3. Neither the name of the University nor the names of its contributors
696 * may be used to endorse or promote products derived from this software
697 * without specific prior written permission.
699 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
700 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
701 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
702 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
703 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
704 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
705 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
706 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
707 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
708 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF