Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.busybox.net/busybox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-06-18 19:31:55 +0300
committerDenys Vlasenko <vda.linux@googlemail.com>2023-06-18 19:31:55 +0300
commit2d06c83b87e8c8330ef82bbdf4314112400877f7 (patch)
tree5114e354fdf6b3fffd38faf79ad0b34cb00b3b4c
parentc72c5552edecb8a2f97cd7e2d9a09a3059986cca (diff)
shell/math: code shrink
function old new delta evaluate_string 1489 1486 -3 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--shell/math.c23
1 files changed, 12 insertions, 11 deletions
diff --git a/shell/math.c b/shell/math.c
index 4c1b1b762..fee325307 100644
--- a/shell/math.c
+++ b/shell/math.c
@@ -264,10 +264,9 @@ static ALWAYS_INLINE int isalnum_(int c)
static arith_t
evaluate_string(arith_state_t *math_state, const char *expr);
-static const char*
-arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
+static arith_t
+arith_lookup_val(arith_state_t *math_state, const char *name)
{
- const char *name = t->var_name;
char c;
const char *p;
char *e = (char*)endofname(name);
@@ -277,6 +276,7 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
p = math_state->lookupvar(name);
*e = c;
if (p) {
+ arith_t val;
size_t len = e - name;
remembered_name *cur;
remembered_name remember;
@@ -289,7 +289,8 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
&& !isalnum_(cur->var_name[len])
) {
/* yes */
- return "expression recursion loop detected";
+ math_state->errmsg = "expression recursion loop detected";
+ return -1;
}
}
@@ -299,16 +300,16 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
math_state->list_of_recursed_names = &remember;
/* recursively evaluate p as expression */
- t->val = evaluate_string(math_state, p);
+ /* this sets math_state->errmsg on error */
+ val = evaluate_string(math_state, p);
/* pop current var name */
math_state->list_of_recursed_names = remember.next;
- return math_state->errmsg;
+ return val;
}
/* treat undefined var as 0 */
- t->val = 0;
- return NULL;
+ return 0;
}
/* "Applying" a token means performing it on the top elements on the integer
@@ -684,9 +685,9 @@ evaluate_string(arith_state_t *math_state, const char *expr)
|| expr[1] == '=' /* or "==..." */
) {
/* Evaluate variable to value */
- errmsg = arith_lookup_val(math_state, numstackptr);
- if (errmsg)
- goto err_with_custom_msg;
+ numstackptr->val = arith_lookup_val(math_state, numstackptr->var_name);
+ if (math_state->errmsg)
+ return numstackptr->val; /* -1 */
}
} else {
dbg("[%d] var:IGNORED", (int)(numstackptr - numstack));