diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-15 13:33:02 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-15 13:33:02 +0200 |
commit | 063847d6bd23e184c409f37645ba90fa4d039ada (patch) | |
tree | 63e360e3e0a2f46d187ef2e21487753a52697efa /shell/hush.c | |
parent | 197a6b3c14a8be7101903118516e0e16ec843eb5 (diff) | |
download | busybox-w32-063847d6bd23e184c409f37645ba90fa4d039ada.tar.gz busybox-w32-063847d6bd23e184c409f37645ba90fa4d039ada.tar.bz2 busybox-w32-063847d6bd23e184c409f37645ba90fa4d039ada.zip |
shell/math: return string error indicator, not integer
function old new delta
expand_and_evaluate_arith 87 106 +19
expand_one_var 1563 1570 +7
arith 12 18 +6
evaluate_string 678 680 +2
arith_apply 1269 1271 +2
builtin_umask 133 132 -1
ash_arith 118 75 -43
expand_vars_to_list 1094 1038 -56
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/3 up/down: 36/-100) Total: -64 bytes
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'shell/hush.c')
-rw-r--r-- | shell/hush.c | 40 |
1 files changed, 15 insertions, 25 deletions
diff --git a/shell/hush.c b/shell/hush.c index 4ca5403de..ad30ac1ea 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -4461,7 +4461,7 @@ static char *encode_then_expand_string(const char *str, int process_bkslash, int | |||
4461 | } | 4461 | } |
4462 | 4462 | ||
4463 | #if ENABLE_SH_MATH_SUPPORT | 4463 | #if ENABLE_SH_MATH_SUPPORT |
4464 | static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p) | 4464 | static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p) |
4465 | { | 4465 | { |
4466 | arith_state_t math_state; | 4466 | arith_state_t math_state; |
4467 | arith_t res; | 4467 | arith_t res; |
@@ -4472,8 +4472,11 @@ static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p) | |||
4472 | //math_state.endofname = endofname; | 4472 | //math_state.endofname = endofname; |
4473 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); | 4473 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
4474 | res = arith(&math_state, exp_str ? exp_str : arg); | 4474 | res = arith(&math_state, exp_str ? exp_str : arg); |
4475 | *errcode_p = math_state.errcode; | ||
4476 | free(exp_str); | 4475 | free(exp_str); |
4476 | if (errmsg_p) | ||
4477 | *errmsg_p = math_state.errmsg; | ||
4478 | if (math_state.errmsg) | ||
4479 | die_if_script(math_state.errmsg); | ||
4477 | return res; | 4480 | return res; |
4478 | } | 4481 | } |
4479 | #endif | 4482 | #endif |
@@ -4714,22 +4717,26 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha | |||
4714 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> | 4717 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
4715 | */ | 4718 | */ |
4716 | arith_t beg, len; | 4719 | arith_t beg, len; |
4717 | int errcode = 0; | 4720 | const char *errmsg; |
4718 | 4721 | ||
4719 | beg = expand_and_evaluate_arith(exp_word, &errcode); | 4722 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
4723 | if (errmsg) | ||
4724 | goto arith_err; | ||
4720 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); | 4725 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
4721 | *p++ = SPECIAL_VAR_SYMBOL; | 4726 | *p++ = SPECIAL_VAR_SYMBOL; |
4722 | exp_word = p; | 4727 | exp_word = p; |
4723 | p = strchr(p, SPECIAL_VAR_SYMBOL); | 4728 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
4724 | *p = '\0'; | 4729 | *p = '\0'; |
4725 | len = expand_and_evaluate_arith(exp_word, &errcode); | 4730 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
4731 | if (errmsg) | ||
4732 | goto arith_err; | ||
4726 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); | 4733 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
4727 | 4734 | if (len >= 0) { /* bash compat: len < 0 is illegal */ | |
4728 | if (errcode >= 0 && len >= 0) { /* bash compat: len < 0 is illegal */ | ||
4729 | if (beg < 0) /* bash compat */ | 4735 | if (beg < 0) /* bash compat */ |
4730 | beg = 0; | 4736 | beg = 0; |
4731 | debug_printf_varexp("from val:'%s'\n", val); | 4737 | debug_printf_varexp("from val:'%s'\n", val); |
4732 | if (len == 0 || !val || beg >= strlen(val)) { | 4738 | if (len == 0 || !val || beg >= strlen(val)) { |
4739 | arith_err: | ||
4733 | val = NULL; | 4740 | val = NULL; |
4734 | } else { | 4741 | } else { |
4735 | /* Paranoia. What if user entered 9999999999999 | 4742 | /* Paranoia. What if user entered 9999999999999 |
@@ -4926,28 +4933,11 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg) | |||
4926 | #if ENABLE_SH_MATH_SUPPORT | 4933 | #if ENABLE_SH_MATH_SUPPORT |
4927 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ | 4934 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
4928 | arith_t res; | 4935 | arith_t res; |
4929 | int errcode; | ||
4930 | 4936 | ||
4931 | arg++; /* skip '+' */ | 4937 | arg++; /* skip '+' */ |
4932 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ | 4938 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
4933 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); | 4939 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
4934 | res = expand_and_evaluate_arith(arg, &errcode); | 4940 | res = expand_and_evaluate_arith(arg, NULL); |
4935 | |||
4936 | if (errcode < 0) { | ||
4937 | const char *msg = "error in arithmetic"; | ||
4938 | switch (errcode) { | ||
4939 | case -3: | ||
4940 | msg = "exponent less than 0"; | ||
4941 | break; | ||
4942 | case -2: | ||
4943 | msg = "divide by 0"; | ||
4944 | break; | ||
4945 | case -5: | ||
4946 | msg = "expression recursion loop detected"; | ||
4947 | break; | ||
4948 | } | ||
4949 | die_if_script(msg); | ||
4950 | } | ||
4951 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); | 4941 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
4952 | sprintf(arith_buf, arith_t_fmt, res); | 4942 | sprintf(arith_buf, arith_t_fmt, res); |
4953 | val = arith_buf; | 4943 | val = arith_buf; |