diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-21 17:54:46 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-21 17:54:46 +0200 |
commit | 3f78cec34745069cf0a92a16dfccff66d98ef5ba (patch) | |
tree | 1b2b8cd18d3ecfa92b9ae6b5fde3e66366da0cf7 /shell | |
parent | 349ef96bb5eae3c487884dd0e88c84a6ba0a1efa (diff) | |
download | busybox-w32-3f78cec34745069cf0a92a16dfccff66d98ef5ba.tar.gz busybox-w32-3f78cec34745069cf0a92a16dfccff66d98ef5ba.tar.bz2 busybox-w32-3f78cec34745069cf0a92a16dfccff66d98ef5ba.zip |
hush: handle expansions in ${var?expanded_word} constructs
function old new delta
expand_vars_to_list 2209 2229 +20
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
-rw-r--r-- | shell/hush.c | 42 | ||||
-rw-r--r-- | shell/hush_test/hush-vars/param_expand_indicate_error.right | 15 | ||||
-rwxr-xr-x | shell/hush_test/hush-vars/param_expand_indicate_error.tests | 20 |
3 files changed, 62 insertions, 15 deletions
diff --git a/shell/hush.c b/shell/hush.c index 1937d24e4..6d91a534a 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -2403,6 +2403,23 @@ static char *expand_pseudo_dquoted(const char *str) | |||
2403 | return exp_str; | 2403 | return exp_str; |
2404 | } | 2404 | } |
2405 | 2405 | ||
2406 | #if ENABLE_SH_MATH_SUPPORT | ||
2407 | static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p) | ||
2408 | { | ||
2409 | arith_eval_hooks_t hooks; | ||
2410 | arith_t res; | ||
2411 | char *exp_str; | ||
2412 | |||
2413 | hooks.lookupvar = get_local_var_value; | ||
2414 | hooks.setvar = set_local_var_from_halves; | ||
2415 | hooks.endofname = endofname; | ||
2416 | exp_str = expand_pseudo_dquoted(arg); | ||
2417 | res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); | ||
2418 | free(exp_str); | ||
2419 | return res; | ||
2420 | } | ||
2421 | #endif | ||
2422 | |||
2406 | /* Expand all variable references in given string, adding words to list[] | 2423 | /* Expand all variable references in given string, adding words to list[] |
2407 | * at n, n+1,... positions. Return updated n (so that list[n] is next one | 2424 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
2408 | * to be filled). This routine is extremely tricky: has to deal with | 2425 | * to be filled). This routine is extremely tricky: has to deal with |
@@ -2427,7 +2444,7 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char | |||
2427 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { | 2444 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
2428 | char first_ch; | 2445 | char first_ch; |
2429 | int i; | 2446 | int i; |
2430 | char *dyn_val = NULL; | 2447 | char *to_be_freed = NULL; |
2431 | const char *val = NULL; | 2448 | const char *val = NULL; |
2432 | #if ENABLE_HUSH_TICK | 2449 | #if ENABLE_HUSH_TICK |
2433 | o_string subst_result = NULL_O_STRING; | 2450 | o_string subst_result = NULL_O_STRING; |
@@ -2528,21 +2545,13 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char | |||
2528 | #endif | 2545 | #endif |
2529 | #if ENABLE_SH_MATH_SUPPORT | 2546 | #if ENABLE_SH_MATH_SUPPORT |
2530 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ | 2547 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
2531 | arith_eval_hooks_t hooks; | ||
2532 | arith_t res; | 2548 | arith_t res; |
2533 | int errcode; | 2549 | int errcode; |
2534 | char *exp_str; | ||
2535 | 2550 | ||
2536 | arg++; /* skip '+' */ | 2551 | arg++; /* skip '+' */ |
2537 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ | 2552 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
2538 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); | 2553 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
2539 | 2554 | res = expand_and_evaluate_arith(arg, &errcode); | |
2540 | exp_str = expand_pseudo_dquoted(arg); | ||
2541 | hooks.lookupvar = get_local_var_value; | ||
2542 | hooks.setvar = set_local_var_from_halves; | ||
2543 | hooks.endofname = endofname; | ||
2544 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); | ||
2545 | free(exp_str); | ||
2546 | 2555 | ||
2547 | if (errcode < 0) { | 2556 | if (errcode < 0) { |
2548 | const char *msg = "error in arithmetic"; | 2557 | const char *msg = "error in arithmetic"; |
@@ -2628,8 +2637,8 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char | |||
2628 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); | 2637 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
2629 | if (exp_op == *exp_word) /* ## or %% */ | 2638 | if (exp_op == *exp_word) /* ## or %% */ |
2630 | exp_word++; | 2639 | exp_word++; |
2631 | val = dyn_val = xstrdup(val); | 2640 | val = to_be_freed = xstrdup(val); |
2632 | loc = scan(dyn_val, exp_word, match_at_left); | 2641 | loc = scan(to_be_freed, exp_word, match_at_left); |
2633 | if (match_at_left) /* # or ## */ | 2642 | if (match_at_left) /* # or ## */ |
2634 | val = loc; | 2643 | val = loc; |
2635 | else if (loc) /* % or %% and match was found */ | 2644 | else if (loc) /* % or %% and match was found */ |
@@ -2662,7 +2671,7 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char | |||
2662 | if (len == 0 || !val || beg >= strlen(val)) | 2671 | if (len == 0 || !val || beg >= strlen(val)) |
2663 | val = ""; | 2672 | val = ""; |
2664 | else | 2673 | else |
2665 | val = dyn_val = xstrndup(val + beg, len); | 2674 | val = to_be_freed = xstrndup(val + beg, len); |
2666 | //bb_error_msg("val:'%s'", val); | 2675 | //bb_error_msg("val:'%s'", val); |
2667 | } else | 2676 | } else |
2668 | #endif | 2677 | #endif |
@@ -2699,13 +2708,16 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char | |||
2699 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, | 2708 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
2700 | (exp_save == ':') ? "true" : "false", use_word); | 2709 | (exp_save == ':') ? "true" : "false", use_word); |
2701 | if (use_word) { | 2710 | if (use_word) { |
2711 | to_be_freed = expand_pseudo_dquoted(exp_word); | ||
2712 | if (to_be_freed) | ||
2713 | exp_word = to_be_freed; | ||
2702 | if (exp_op == '?') { | 2714 | if (exp_op == '?') { |
2703 | //TODO: how interactive bash aborts expansion mid-command? | ||
2704 | /* mimic bash message */ | 2715 | /* mimic bash message */ |
2705 | die_if_script("%s: %s", | 2716 | die_if_script("%s: %s", |
2706 | var, | 2717 | var, |
2707 | exp_word[0] ? exp_word : "parameter null or not set" | 2718 | exp_word[0] ? exp_word : "parameter null or not set" |
2708 | ); | 2719 | ); |
2720 | //TODO: how interactive bash aborts expansion mid-command? | ||
2709 | } else { | 2721 | } else { |
2710 | val = exp_word; | 2722 | val = exp_word; |
2711 | } | 2723 | } |
@@ -2750,7 +2762,7 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char | |||
2750 | if (val) { | 2762 | if (val) { |
2751 | o_addQstr(output, val, strlen(val)); | 2763 | o_addQstr(output, val, strlen(val)); |
2752 | } | 2764 | } |
2753 | free(dyn_val); | 2765 | free(to_be_freed); |
2754 | /* Do the check to avoid writing to a const string */ | 2766 | /* Do the check to avoid writing to a const string */ |
2755 | if (*p != SPECIAL_VAR_SYMBOL) | 2767 | if (*p != SPECIAL_VAR_SYMBOL) |
2756 | *p = SPECIAL_VAR_SYMBOL; | 2768 | *p = SPECIAL_VAR_SYMBOL; |
diff --git a/shell/hush_test/hush-vars/param_expand_indicate_error.right b/shell/hush_test/hush-vars/param_expand_indicate_error.right index 590bb2001..06fcc5104 100644 --- a/shell/hush_test/hush-vars/param_expand_indicate_error.right +++ b/shell/hush_test/hush-vars/param_expand_indicate_error.right | |||
@@ -1,26 +1,41 @@ | |||
1 | hush: syntax error: unterminated ${name} | 1 | hush: syntax error: unterminated ${name} |
2 | 0 | 2 | 0 |
3 | 0 | 3 | 0 |
4 | ==== | ||
4 | _ | 5 | _ |
5 | hush: 1: parameter null or not set | 6 | hush: 1: parameter null or not set |
6 | hush: 1: parameter null or not set | 7 | hush: 1: parameter null or not set |
7 | hush: 1: message1 | 8 | hush: 1: message1 |
8 | hush: 1: message1 | 9 | hush: 1: message1 |
10 | hush: 1: unset! | ||
11 | hush: 1: null or unset! | ||
12 | ==== | ||
9 | _aaaa | 13 | _aaaa |
10 | _aaaa | 14 | _aaaa |
11 | _aaaa | 15 | _aaaa |
12 | _aaaa | 16 | _aaaa |
13 | _aaaa | 17 | _aaaa |
18 | _aaaa | ||
19 | _aaaa | ||
20 | ==== | ||
14 | _ | 21 | _ |
15 | hush: f: parameter null or not set | 22 | hush: f: parameter null or not set |
16 | hush: f: parameter null or not set | 23 | hush: f: parameter null or not set |
17 | hush: f: message3 | 24 | hush: f: message3 |
18 | hush: f: message3 | 25 | hush: f: message3 |
26 | hush: f: unset! | ||
27 | hush: f: null or unset! | ||
28 | ==== | ||
19 | _ | 29 | _ |
20 | _ | 30 | _ |
21 | hush: f: parameter null or not set | 31 | hush: f: parameter null or not set |
22 | _ | 32 | _ |
23 | hush: f: message4 | 33 | hush: f: message4 |
34 | _ | ||
35 | hush: f: null or unset! | ||
36 | ==== | ||
37 | _fff | ||
38 | _fff | ||
24 | _fff | 39 | _fff |
25 | _fff | 40 | _fff |
26 | _fff | 41 | _fff |
diff --git a/shell/hush_test/hush-vars/param_expand_indicate_error.tests b/shell/hush_test/hush-vars/param_expand_indicate_error.tests index bccba3e1b..be14b1e37 100755 --- a/shell/hush_test/hush-vars/param_expand_indicate_error.tests +++ b/shell/hush_test/hush-vars/param_expand_indicate_error.tests | |||
@@ -5,36 +5,56 @@ | |||
5 | "$THIS_SH" -c 'echo ${:?}' | 5 | "$THIS_SH" -c 'echo ${:?}' |
6 | 6 | ||
7 | # then some funky ones | 7 | # then some funky ones |
8 | # note: bash prints 1 - treats it as "length of $#"? We print 0 | ||
8 | "$THIS_SH" -c 'echo ${#?}' | 9 | "$THIS_SH" -c 'echo ${#?}' |
10 | # bash prints 0 | ||
9 | "$THIS_SH" -c 'echo ${#:?}' | 11 | "$THIS_SH" -c 'echo ${#:?}' |
10 | 12 | ||
11 | # now some valid ones | 13 | # now some valid ones |
14 | export msg_unset="unset!" | ||
15 | export msg_null_or_unset="null or unset!" | ||
16 | |||
17 | echo ==== | ||
12 | "$THIS_SH" -c 'set --; echo _$1' | 18 | "$THIS_SH" -c 'set --; echo _$1' |
13 | "$THIS_SH" -c 'set --; echo _${1?}' | 19 | "$THIS_SH" -c 'set --; echo _${1?}' |
14 | "$THIS_SH" -c 'set --; echo _${1:?}' | 20 | "$THIS_SH" -c 'set --; echo _${1:?}' |
15 | "$THIS_SH" -c 'set --; echo _${1?message1}' | 21 | "$THIS_SH" -c 'set --; echo _${1?message1}' |
16 | "$THIS_SH" -c 'set --; echo _${1:?message1}' | 22 | "$THIS_SH" -c 'set --; echo _${1:?message1}' |
23 | "$THIS_SH" -c 'set --; echo _${1?$msg_unset}' | ||
24 | "$THIS_SH" -c 'set --; echo _${1:?$msg_null_or_unset}' | ||
17 | 25 | ||
26 | echo ==== | ||
18 | "$THIS_SH" -c 'set -- aaaa; echo _$1' | 27 | "$THIS_SH" -c 'set -- aaaa; echo _$1' |
19 | "$THIS_SH" -c 'set -- aaaa; echo _${1?}' | 28 | "$THIS_SH" -c 'set -- aaaa; echo _${1?}' |
20 | "$THIS_SH" -c 'set -- aaaa; echo _${1:?}' | 29 | "$THIS_SH" -c 'set -- aaaa; echo _${1:?}' |
21 | "$THIS_SH" -c 'set -- aaaa; echo _${1?word}' | 30 | "$THIS_SH" -c 'set -- aaaa; echo _${1?word}' |
22 | "$THIS_SH" -c 'set -- aaaa; echo _${1:?word}' | 31 | "$THIS_SH" -c 'set -- aaaa; echo _${1:?word}' |
32 | "$THIS_SH" -c 'set -- aaaa; echo _${1?$msg_unset}' | ||
33 | "$THIS_SH" -c 'set -- aaaa; echo _${1:?$msg_null_or_unset}' | ||
23 | 34 | ||
35 | echo ==== | ||
24 | "$THIS_SH" -c 'unset f; echo _$f' | 36 | "$THIS_SH" -c 'unset f; echo _$f' |
25 | "$THIS_SH" -c 'unset f; echo _${f?}' | 37 | "$THIS_SH" -c 'unset f; echo _${f?}' |
26 | "$THIS_SH" -c 'unset f; echo _${f:?}' | 38 | "$THIS_SH" -c 'unset f; echo _${f:?}' |
27 | "$THIS_SH" -c 'unset f; echo _${f?message3}' | 39 | "$THIS_SH" -c 'unset f; echo _${f?message3}' |
28 | "$THIS_SH" -c 'unset f; echo _${f:?message3}' | 40 | "$THIS_SH" -c 'unset f; echo _${f:?message3}' |
41 | "$THIS_SH" -c 'unset f; echo _${f?$msg_unset}' | ||
42 | "$THIS_SH" -c 'unset f; echo _${f:?$msg_null_or_unset}' | ||
29 | 43 | ||
44 | echo ==== | ||
30 | "$THIS_SH" -c 'f=; echo _$f' | 45 | "$THIS_SH" -c 'f=; echo _$f' |
31 | "$THIS_SH" -c 'f=; echo _${f?}' | 46 | "$THIS_SH" -c 'f=; echo _${f?}' |
32 | "$THIS_SH" -c 'f=; echo _${f:?}' | 47 | "$THIS_SH" -c 'f=; echo _${f:?}' |
33 | "$THIS_SH" -c 'f=; echo _${f?word}' | 48 | "$THIS_SH" -c 'f=; echo _${f?word}' |
34 | "$THIS_SH" -c 'f=; echo _${f:?message4}' | 49 | "$THIS_SH" -c 'f=; echo _${f:?message4}' |
50 | "$THIS_SH" -c 'f=; echo _${f?$msg_unset}' | ||
51 | "$THIS_SH" -c 'f=; echo _${f:?$msg_null_or_unset}' | ||
35 | 52 | ||
53 | echo ==== | ||
36 | "$THIS_SH" -c 'f=fff; echo _$f' | 54 | "$THIS_SH" -c 'f=fff; echo _$f' |
37 | "$THIS_SH" -c 'f=fff; echo _${f?}' | 55 | "$THIS_SH" -c 'f=fff; echo _${f?}' |
38 | "$THIS_SH" -c 'f=fff; echo _${f:?}' | 56 | "$THIS_SH" -c 'f=fff; echo _${f:?}' |
39 | "$THIS_SH" -c 'f=fff; echo _${f?word}' | 57 | "$THIS_SH" -c 'f=fff; echo _${f?word}' |
40 | "$THIS_SH" -c 'f=fff; echo _${f:?word}' | 58 | "$THIS_SH" -c 'f=fff; echo _${f:?word}' |
59 | "$THIS_SH" -c 'f=fff; echo _${f?$msg_unset}' | ||
60 | "$THIS_SH" -c 'f=fff; echo _${f:?$msg_null_or_unset}' | ||