ash: fix another bit of var_bash4 bug
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 7 Aug 2010 20:24:36 +0000 (22:24 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 7 Aug 2010 20:24:36 +0000 (22:24 +0200)
But it _still_ doesn't pass! quoted case is a tough nut to crack

function                                             old     new   delta
redirect                                            1281    1286      +5
subevalvar                                          1141    1142      +1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c
shell/ash_test/ash-vars/var_bash4.right
shell/ash_test/ash-vars/var_bash4.tests

index 6befe0fd35d08a82e69aceaeaa12d63d62e294b7..4fbae24981b3eaf2a0fd90847621f46bb3b50e27 100644 (file)
@@ -6234,7 +6234,7 @@ parse_sub_pattern(char *arg, int varflags)
        unsigned char c;
 
        //char *org_arg = arg;
-       //bb_error_msg("arg:'%s'", arg);
+       //bb_error_msg("arg:'%s' varflags:%x", arg, varflags);
        idx = arg;
        while (1) {
                c = *arg;
@@ -6248,9 +6248,20 @@ parse_sub_pattern(char *arg, int varflags)
                        }
                }
                *idx++ = c;
-               if (!(varflags & VSQUOTE) && c == '\\' && arg[1] == '\\')
-                       arg++; /* skip both \\, not just first one */
                arg++;
+               /*
+                * Example: v='ab\c'; echo ${v/\\b/_\\_\z_}
+                * The result is a_\_z_c (not a\_\_z_c)!
+                *
+                * Enable debug prints in this function and you'll see:
+                * ash: arg:'\\b/_\\_z_' varflags:d
+                * ash: pattern:'\\b' repl:'_\_z_'
+                * That is, \\b is interpreted as \\b, but \\_ as \_!
+                * IOW: search pattern and replace string treat backslashes
+                * differently! That is the reason why we check repl below:
+                */
+               if (c == '\\' && *arg == '\\' && repl && !(varflags & VSQUOTE))
+                       arg++; /* skip both '\', not just first one */
        }
        *idx = c; /* NUL */
        //bb_error_msg("pattern:'%s' repl:'%s'", org_arg, repl);
index 2d4e45be86129f133c4b8449d673be536a684466..600e8532f6ea71cb1f61f683e6411ed6c63cc635 100644 (file)
@@ -11,7 +11,7 @@ Quoted:        a*b_\_\z_
 
 Source:        a\bc
 Replace str:   _\\_\z_
-Pattern:       single backslash and b: "replace literal b"
+Pattern:       single backslash and b: "replace b"
 In assignment: a\_\_z_c
 Unquoted:      a\_\_z_c
 Quoted:        a\_\_\z_c
index a6e98fd86388f461ded2a6ea43f6dbd18cf808a7..d5470614bbaa35330bdbbb42c380f0a3c74aebe3 100755 (executable)
@@ -30,7 +30,7 @@ v='a\bc'
 echo 'Source:       ' "$v"
 echo 'Replace str:  ' '_\\_\z_'
 
-echo 'Pattern:      ' 'single backslash and b: "replace literal b"'
+echo 'Pattern:      ' 'single backslash and b: "replace b"'
 r=${v/\b/_\\_\z_}
 echo 'In assignment:' "$r"
 echo 'Unquoted:     '  ${v/\b/_\\_\z_}