diff options
author | Denys Vlasenko | 2023-06-13 16:15:18 +0200 |
---|---|---|
committer | Denys Vlasenko | 2023-06-13 16:15:18 +0200 |
commit | c1d7507a4d36bbeba88b09e8f0269fe81e1cb33f (patch) | |
tree | 82e24eb57e83df270997b9fff4cd19e32101c151 | |
parent | 8ccb3f7b1375862f8ef82cd9d225da986b80f054 (diff) | |
download | busybox-c1d7507a4d36bbeba88b09e8f0269fe81e1cb33f.zip busybox-c1d7507a4d36bbeba88b09e8f0269fe81e1cb33f.tar.gz |
shell/math: fix one name check, other minor cleanups
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/math.c | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/shell/math.c b/shell/math.c index af8e691..57eb569 100644 --- a/shell/math.c +++ b/shell/math.c @@ -245,6 +245,9 @@ typedef struct { char *var_name; } var_or_num_t; +#define VALID_NAME(name) ((name) && (name) != SECOND_VAL_VALID) +#define NOT_NAME(name) (!(name) || (name) == SECOND_VAL_VALID) + typedef struct remembered_name { struct remembered_name *next; @@ -258,7 +261,7 @@ 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) { - if (t->var_name && t->var_name != SECOND_VAL_VALID) { + if (VALID_NAME(t->var_name)) { const char *p = math_state->lookupvar(t->var_name); if (p) { remembered_name *cur; @@ -275,16 +278,15 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t) } /* push current var name */ - cur = math_state->list_of_recursed_names; remember.var_name = t->var_name; - remember.next = cur; + remember.next = math_state->list_of_recursed_names; math_state->list_of_recursed_names = &remember; /* recursively evaluate p as expression */ t->val = evaluate_string(math_state, p); /* pop current var name */ - math_state->list_of_recursed_names = cur; + math_state->list_of_recursed_names = remember.next; return math_state->errmsg; } @@ -446,7 +448,7 @@ arith_apply(arith_state_t *math_state, operator op, var_or_num_t *numstack, var_ if (is_assign_op(op)) { char buf[sizeof(arith_t)*3 + 2]; - if (!top_of_stack->var_name || top_of_stack->var_name == SECOND_VAL_VALID) { + if (NOT_NAME(top_of_stack->var_name)) { /* Hmm, 1=2 ? */ goto syntax_err; } @@ -708,9 +710,11 @@ evaluate_string(arith_state_t *math_state, const char *expr) if ((expr[0] == '+' || expr[0] == '-') && (expr[1] == expr[0]) ) { - if (numstackptr == numstack || !numstackptr[-1].var_name) { /* not a VAR++ */ + if (numstackptr == numstack || NOT_NAME(numstackptr[-1].var_name)) { + /* not a VAR++ */ char next = skip_whitespace(expr + 2)[0]; - if (!(isalpha(next) || next == '_')) { /* not a ++VAR */ + if (!(isalpha(next) || next == '_')) { + /* not a ++VAR */ //bb_error_msg("special %c%c", expr[0], expr[0]); op = (expr[0] == '+' ? TOK_ADD : TOK_SUB); expr++; @@ -802,7 +806,7 @@ evaluate_string(arith_state_t *math_state, const char *expr) if (prev_op == TOK_LPAREN) { //bb_error_msg("prev_op == TOK_LPAREN"); //bb_error_msg(" %p %p numstackptr[-1].var_name:'%s'", numstack, numstackptr-1, numstackptr[-1].var_name); - if (numstackptr[-1].var_name && numstackptr[-1].var_name != SECOND_VAL_VALID) { + if (VALID_NAME(numstackptr[-1].var_name)) { /* Expression is (var), lookup now */ errmsg = arith_lookup_val(math_state, &numstackptr[-1]); if (errmsg) |