hush: fix another corner case with backslashes in heredocs
authorDenys Vlasenko <dvlasenk@redhat.com>
Mon, 6 Sep 2010 09:46:03 +0000 (11:46 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Mon, 6 Sep 2010 09:46:03 +0000 (11:46 +0200)
function                                             old     new   delta
parse_stream                                        2395    2432     +37

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
shell/hush.c
shell/hush_test/hush-misc/heredoc_backslash1.right
shell/hush_test/hush-misc/heredoc_backslash1.tests

index e8aef2d7e626cf2070d024deaecc5b0a9a36cd62..4e29f01a1906a1ef36b289f065787f2bb7bb8586 100644 (file)
@@ -3174,6 +3174,7 @@ static char *fetch_till_str(o_string *as_string,
                ch = i_getch(input);
                nommu_addchr(as_string, ch);
                if (ch == '\n'
+               /* TODO: or EOF? (heredoc delimiter may end with <eof>, not only <eol> */
                 && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\')
                ) {
                        if (strcmp(heredoc.data + past_EOL, word) == 0) {
@@ -3182,7 +3183,8 @@ static char *fetch_till_str(o_string *as_string,
                                return heredoc.data;
                        }
                        do {
-                               o_addchr(&heredoc, ch);
+                               o_addchr(&heredoc, '\n');
+                               prev = 0; /* not \ */
                                past_EOL = heredoc.length;
  jump_in:
                                do {
@@ -3196,8 +3198,12 @@ static char *fetch_till_str(o_string *as_string,
                        return NULL;
                }
                o_addchr(&heredoc, ch);
+               if (prev == '\\' && ch == '\\')
+                       /* Correctly handle foo\\<eol> (not a line cont.) */
+                       prev = 0; /* not \ */
+               else
+                       prev = ch;
                nommu_addchr(as_string, ch);
-               prev = ch;
        }
 }
 
index 234c0172fc4f8d1dfc72ac3cdc2319765e19ffe7..2633908fb62e5caa573841cdd8f1e0ad293d3cfe 100644 (file)
@@ -1,18 +1,24 @@
 Quoted heredoc:
 a\
        b
+a\\
+       b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
        -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\
 
 Unquoted heredoc:
 a      b
+a\
+       b
  123456 -qwerty-\t-\-\"-\'-`-\--\z-\*-\?-
        -qwerty-\t-\-\"-\'-`-\--\z-\*-\?-
 cEOF2
 
 Quoted -heredoc:
 a\
+b
+a\\
 b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
@@ -20,6 +26,8 @@ c\
 
 Unquoted -heredoc:
 a      b
+a\
+b
  123456 -qwerty-\t-\-\"-\'-`-\--\z-\*-\?-
 -qwerty-\t-\-\"-\'-`-\--\z-\*-\?-
 cEOF4
index b70467df86c73487eecc2c37b6ddd4ab8e443152..22045d42e011c9066b5fd6c67c874c646f847522 100755 (executable)
@@ -7,6 +7,8 @@ echo Quoted heredoc:
 cat <<"EOF1"
 a\
        b
+a\\
+       b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
        -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\
@@ -17,6 +19,8 @@ echo Unquoted heredoc:
 cat <<EOF2
 a\
        b
+a\\
+       b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
        -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\
@@ -28,6 +32,8 @@ echo Quoted -heredoc:
 cat <<-"EOF3"
 a\
        b
+a\\
+       b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
        -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\
@@ -39,6 +45,8 @@ echo Unquoted -heredoc:
 cat <<-EOF4
 a\
        b
+a\\
+       b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
        -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\