hush: fix another corner case with backslashes in heredocs

function                                             old     new   delta
parse_stream                                        2395    2432     +37

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
diff --git a/shell/hush.c b/shell/hush.c
index e8aef2d..4e29f01 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -3174,6 +3174,7 @@
 		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 @@
 				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 @@
 			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;
 	}
 }
 
diff --git a/shell/hush_test/hush-misc/heredoc_backslash1.right b/shell/hush_test/hush-misc/heredoc_backslash1.right
index 234c017..2633908 100644
--- a/shell/hush_test/hush-misc/heredoc_backslash1.right
+++ b/shell/hush_test/hush-misc/heredoc_backslash1.right
@@ -1,12 +1,16 @@
 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
@@ -14,12 +18,16 @@
 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-\*-\?-
 cEOF4
diff --git a/shell/hush_test/hush-misc/heredoc_backslash1.tests b/shell/hush_test/hush-misc/heredoc_backslash1.tests
index b70467d..22045d4 100755
--- a/shell/hush_test/hush-misc/heredoc_backslash1.tests
+++ b/shell/hush_test/hush-misc/heredoc_backslash1.tests
@@ -7,6 +7,8 @@
 cat <<"EOF1"
 a\
 	b
+a\\
+	b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 	-$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\
@@ -17,6 +19,8 @@
 cat <<EOF2
 a\
 	b
+a\\
+	b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 	-$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\
@@ -28,6 +32,8 @@
 cat <<-"EOF3"
 a\
 	b
+a\\
+	b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 	-$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\
@@ -39,6 +45,8 @@
 cat <<-EOF4
 a\
 	b
+a\\
+	b
  123456 -$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 	-$a-\t-\\-\"-\'-\`-\--\z-\*-\?-
 c\