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/hush.c | |
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/hush.c')
-rw-r--r-- | shell/hush.c | 42 |
1 files changed, 27 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; |