/*
* arithmetic code ripped out of ash shell for code sharing
*
+ * This code is derived from software contributed to Berkeley by
+ * Kenneth Almquist.
+ *
+ * Original BSD copyright notice is retained at the end of this file.
+ *
* Copyright (c) 1989, 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
* was re-ported from NetBSD and debianized.
*
- * This code is derived from software contributed to Berkeley by
- * Kenneth Almquist.
- *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
- *
- * Original BSD copyright notice is retained at the end of this file.
- */
-/*
* rewrite arith.y to micro stack based cryptic algorithm by
* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
*
* used in busybox and size optimizations,
* rewrote arith (see notes to this), added locale support,
* rewrote dynamic variables.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
-
-#include "busybox.h"
-#include "math.h"
-
-#define a_e_h_t arith_eval_hooks_t
-#define lookupvar (math_hooks->lookupvar)
-#define setvar (math_hooks->setvar)
-#define endofname (math_hooks->endofname)
-
/* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
Permission is hereby granted, free of charge, to any person obtaining
* whitespace chars should be considered. Look below the "#include"s for a
* precompiler test.
*/
-
/*
* Aug 26, 2001 Manuel Novoa III
*
* modified slightly to take account of my changes to the code.
*
*/
-
/*
* (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
*
* - protect $((num num)) as true zero expr (Manuel`s error)
* - always use special isspace(), see comment from bash ;-)
*/
+#include "libbb.h"
+#include "math.h"
+
+#define a_e_h_t arith_eval_hooks_t
+#define lookupvar (math_hooks->lookupvar)
+#define setvar (math_hooks->setvar )
+#define endofname (math_hooks->endofname)
#define arith_isspace(arithval) \
(arithval == ' ' || arithval == '\n' || arithval == '\t')
arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks)
{
if (t->var) {
- const char * p = lookupvar(t->var);
+ const char *p = lookupvar(t->var);
if (p) {
int errcode;
/* "applying" a token means performing it on the top elements on the integer
* stack. For a unary operator it will only change the top element, but a
* binary operator will pop two arguments and push a result */
-static int
+static NOINLINE int
arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr, a_e_h_t *math_hooks)
{
v_n_t *numptr_m1;
rez %= numptr_val;
}
if (tok_have_assign(op)) {
- char buf[sizeof(arith_t_type)*3 + 2];
+ char buf[sizeof(arith_t)*3 + 2];
if (numptr_m1->var == NULL) {
/* Hmm, 1=2 ? */
goto err;
}
/* save to shell variable */
- snprintf(buf, sizeof(buf), arith_t_fmt, (arith_t_type) rez);
- setvar(numptr_m1->var, buf, 0);
+ sprintf(buf, arith_t_fmt, rez);
+ setvar(numptr_m1->var, buf);
/* after saving, make previous value for v++ or v-- */
if (op == TOK_POST_INC)
rez--;
}
if (isdigit(arithval)) {
numstackptr->var = NULL;
+ errno = 0;
+ /* call strtoul[l]: */
numstackptr->val = strto_arith_t(expr, (char **) &expr, 0);
+ if (errno)
+ numstackptr->val = 0; /* bash compat */
goto num;
}
for (p = op_tokens; ; p++) {
lasttok = TOK_NUM;
/* Plus and minus are binary (not unary) _only_ if the last
- * token was as number, or a right paren (which pretends to be
+ * token was a number, or a right paren (which pretends to be
* a number, since it evaluates to one). Think about it.
* It makes sense. */
if (lasttok != TOK_NUM) {
break;
}
}
- /* We don't want a unary operator to cause recursive descent on the
+ /* We don't want an unary operator to cause recursive descent on the
* stack, because there can be many in a row and it could cause an
* operator to be evaluated before its argument is pushed onto the
* integer stack. */
} /* while */
}
-/*-
+/*
* Copyright (c) 1989, 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*