diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-13 12:49:03 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-13 12:49:03 +0200 |
commit | 06d44d7dfb709bfe02e74d187cceb8591bbda3b4 (patch) | |
tree | 9a2a8e8381cecae29a5c02ed10995d0a0ad9d412 /shell/math.c | |
parent | bd14770b0c8594ce5b0ab9b0b1249b72fc781dd3 (diff) | |
download | busybox-w32-06d44d7dfb709bfe02e74d187cceb8591bbda3b4.tar.gz busybox-w32-06d44d7dfb709bfe02e74d187cceb8591bbda3b4.tar.bz2 busybox-w32-06d44d7dfb709bfe02e74d187cceb8591bbda3b4.zip |
shell/math.c: rename arith_eval_hooks to arith_state, put error code into it
function old new delta
expand_and_evaluate_arith 79 89 +10
arith 675 674 -1
arith_lookup_val 151 142 -9
ash_arith 135 122 -13
arith_apply 1304 1269 -35
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/4 up/down: 10/-58) Total: -48 bytes
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'shell/math.c')
-rw-r--r-- | shell/math.c | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/shell/math.c b/shell/math.c index a9fbc8910..555559a26 100644 --- a/shell/math.c +++ b/shell/math.c | |||
@@ -119,10 +119,9 @@ | |||
119 | #include "libbb.h" | 119 | #include "libbb.h" |
120 | #include "math.h" | 120 | #include "math.h" |
121 | 121 | ||
122 | #define a_e_h_t arith_eval_hooks_t | 122 | #define lookupvar (math_state->lookupvar) |
123 | #define lookupvar (math_hooks->lookupvar) | 123 | #define setvar (math_state->setvar ) |
124 | #define setvar (math_hooks->setvar ) | 124 | //#define endofname (math_state->endofname) |
125 | //#define endofname (math_hooks->endofname) | ||
126 | 125 | ||
127 | typedef unsigned char operator; | 126 | typedef unsigned char operator; |
128 | 127 | ||
@@ -249,13 +248,12 @@ typedef struct chk_var_recursive_looped_t { | |||
249 | static chk_var_recursive_looped_t *prev_chk_var_recursive; | 248 | static chk_var_recursive_looped_t *prev_chk_var_recursive; |
250 | 249 | ||
251 | static int | 250 | static int |
252 | arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks) | 251 | arith_lookup_val(arith_state_t *math_state, v_n_t *t) |
253 | { | 252 | { |
254 | if (t->var) { | 253 | if (t->var) { |
255 | const char *p = lookupvar(t->var); | 254 | const char *p = lookupvar(t->var); |
256 | 255 | ||
257 | if (p) { | 256 | if (p) { |
258 | int errcode; | ||
259 | chk_var_recursive_looped_t *cur; | 257 | chk_var_recursive_looped_t *cur; |
260 | chk_var_recursive_looped_t cur_save; | 258 | chk_var_recursive_looped_t cur_save; |
261 | 259 | ||
@@ -273,10 +271,10 @@ arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks) | |||
273 | cur_save.next = cur; | 271 | cur_save.next = cur; |
274 | prev_chk_var_recursive = &cur_save; | 272 | prev_chk_var_recursive = &cur_save; |
275 | 273 | ||
276 | t->val = arith(p, &errcode, math_hooks); | 274 | t->val = arith(math_state, p); |
277 | /* restore previous ptr after recursion */ | 275 | /* restore previous ptr after recursion */ |
278 | prev_chk_var_recursive = cur; | 276 | prev_chk_var_recursive = cur; |
279 | return errcode; | 277 | return math_state->errcode; |
280 | } | 278 | } |
281 | /* allow undefined var as 0 */ | 279 | /* allow undefined var as 0 */ |
282 | t->val = 0; | 280 | t->val = 0; |
@@ -288,13 +286,13 @@ arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks) | |||
288 | * stack. For an unary operator it will only change the top element, but a | 286 | * 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 */ | 287 | * binary operator will pop two arguments and push the result */ |
290 | static NOINLINE int | 288 | static NOINLINE int |
291 | arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr, a_e_h_t *math_hooks) | 289 | arith_apply(arith_state_t *math_state, operator op, v_n_t *numstack, v_n_t **numstackptr) |
292 | { | 290 | { |
293 | #define NUMPTR (*numstackptr) | 291 | #define NUMPTR (*numstackptr) |
294 | 292 | ||
295 | v_n_t *numptr_m1; | 293 | v_n_t *numptr_m1; |
296 | arith_t numptr_val, rez; | 294 | arith_t numptr_val, rez; |
297 | int ret_arith_lookup_val; | 295 | int err; |
298 | 296 | ||
299 | /* There is no operator that can work without arguments */ | 297 | /* There is no operator that can work without arguments */ |
300 | if (NUMPTR == numstack) | 298 | if (NUMPTR == numstack) |
@@ -302,9 +300,9 @@ arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr, a_e_h_t *math_hoo | |||
302 | numptr_m1 = NUMPTR - 1; | 300 | numptr_m1 = NUMPTR - 1; |
303 | 301 | ||
304 | /* Check operand is var with noninteger value */ | 302 | /* Check operand is var with noninteger value */ |
305 | ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks); | 303 | err = arith_lookup_val(math_state, numptr_m1); |
306 | if (ret_arith_lookup_val) | 304 | if (err) |
307 | return ret_arith_lookup_val; | 305 | return err; |
308 | 306 | ||
309 | rez = numptr_m1->val; | 307 | rez = numptr_m1->val; |
310 | if (op == TOK_UMINUS) | 308 | if (op == TOK_UMINUS) |
@@ -339,9 +337,9 @@ arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr, a_e_h_t *math_hoo | |||
339 | numptr_m1 = NUMPTR - 1; | 337 | numptr_m1 = NUMPTR - 1; |
340 | if (op != TOK_ASSIGN) { | 338 | if (op != TOK_ASSIGN) { |
341 | /* check operand is var with noninteger value for not '=' */ | 339 | /* check operand is var with noninteger value for not '=' */ |
342 | ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks); | 340 | err = arith_lookup_val(math_state, numptr_m1); |
343 | if (ret_arith_lookup_val) | 341 | if (err) |
344 | return ret_arith_lookup_val; | 342 | return err; |
345 | } | 343 | } |
346 | if (op == TOK_CONDITIONAL) { | 344 | if (op == TOK_CONDITIONAL) { |
347 | numptr_m1->contidional_second_val = rez; | 345 | numptr_m1->contidional_second_val = rez; |
@@ -490,7 +488,7 @@ endofname(const char *name) | |||
490 | } | 488 | } |
491 | 489 | ||
492 | arith_t | 490 | arith_t |
493 | arith(const char *expr, int *perrcode, a_e_h_t *math_hooks) | 491 | arith(arith_state_t *math_state, const char *expr) |
494 | { | 492 | { |
495 | operator lasttok; | 493 | operator lasttok; |
496 | int errcode; | 494 | int errcode; |
@@ -543,7 +541,7 @@ arith(const char *expr, int *perrcode, a_e_h_t *math_hooks) | |||
543 | } | 541 | } |
544 | if (numstack->var) { | 542 | if (numstack->var) { |
545 | /* expression is $((var)) only, lookup now */ | 543 | /* expression is $((var)) only, lookup now */ |
546 | errcode = arith_lookup_val(numstack, math_hooks); | 544 | errcode = arith_lookup_val(math_state, numstack); |
547 | } | 545 | } |
548 | goto ret; | 546 | goto ret; |
549 | } | 547 | } |
@@ -658,7 +656,7 @@ arith(const char *expr, int *perrcode, a_e_h_t *math_hooks) | |||
658 | break; | 656 | break; |
659 | } | 657 | } |
660 | } | 658 | } |
661 | errcode = arith_apply(prev_op, numstack, &numstackptr, math_hooks); | 659 | errcode = arith_apply(math_state, prev_op, numstack, &numstackptr); |
662 | if (errcode) | 660 | if (errcode) |
663 | goto ret; | 661 | goto ret; |
664 | } | 662 | } |
@@ -675,7 +673,7 @@ arith(const char *expr, int *perrcode, a_e_h_t *math_hooks) | |||
675 | err: | 673 | err: |
676 | numstack->val = errcode = -1; | 674 | numstack->val = errcode = -1; |
677 | ret: | 675 | ret: |
678 | *perrcode = errcode; | 676 | math_state->errcode = errcode; |
679 | return numstack->val; | 677 | return numstack->val; |
680 | } | 678 | } |
681 | 679 | ||