Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: '<<-' here-documents oddity with line continuation



Op 09-02-18 om 17:07 schreef Martijn Dekker:
> Op 09-02-18 om 16:27 schreef Stephane Chazelas:
>> Note that there's also:
>>
>> cat << EOF
>> foo
>> E\
>> OF
>>
>> which zsh does differently from other shells (and that nobody
>> would ever do).
> 
> IOW, all shells support line continuation within the terminating
> delimiter except zsh. Eesh.
> 
> Somebody somewhere has probably done this. I'll see if I can rethink my
> patch to fix this as well.

Stéphane helped me realise my whole approach to the patch was wrong. Of
course line continuation should be handled within the loop that parses a
line in the first place, and not in between parsing individual lines. No
extra flag is needed at all.

Here's take two, which should fix all three issues. I also added a test
case, and slightly edited another test case to make sure line
continuation is not parsed if the delimiter is quoted.

- Martijn
diff --git a/Src/exec.c b/Src/exec.c
index c39680d..e5c6455 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -4387,8 +4387,17 @@ gethere(char **strp, int typ)
 		bptr = buf + bsiz;
 		bsiz *= 2;
 	    }
-	    if (lexstop || c == '\n')
+	    if (lexstop)
 		break;
+	    if (c == '\n') {
+		if (!qt && bptr > t && *(bptr - 1) == '\\') {
+		    /* line continuation */
+		    bptr--;
+		    c = hgetc();
+		    continue;
+		} else
+		    break;
+	    }
 	    *bptr++ = c;
 	    c = hgetc();
 	}
diff --git a/Test/A04redirect.ztst b/Test/A04redirect.ztst
index b8105cf..ef7ddb2 100644
--- a/Test/A04redirect.ztst
+++ b/Test/A04redirect.ztst
@@ -114,7 +114,7 @@
   heretest() {
     print First line
     cat <<'    HERE'
-    $foo$foo met celeste  'but with extra'  "stuff to test quoting"
+    $foo$foo met celeste  'but with extra'  "stuff to test quoting"\
     HERE
     print Last line
   }
@@ -125,19 +125,57 @@
   heretest
 0:Re-evaluation of function output with here document, quoted
 >First line
->    $foo$foo met celeste  'but with extra'  "stuff to test quoting"
+>    $foo$foo met celeste  'but with extra'  "stuff to test quoting"\
 >Last line
 >First line
->    $foo$foo met celeste  'but with extra'  "stuff to test quoting"
+>    $foo$foo met celeste  'but with extra'  "stuff to test quoting"\
 >Last line
 >First line
->    $foo$foo met celeste  'but with extra'  "stuff to test quoting"
+>    $foo$foo met celeste  'but with extra'  "stuff to test quoting"\
 >Last line
 
   read -r line <<'  HERE'
   HERE
 1:No input, not even newline, from empty here document.
 
+  heretest() {
+    print First line
+    cat <<-HERE
+	$foo\
+	$foo
+	some\
+	stuff
+	to\
+  test
+	tab\stripping
+	HERE
+    print Last line
+  }
+  heretest
+  eval "$(functions heretest)"
+  heretest
+  eval "$(functions heretest)"
+  heretest
+0:Line continuation in here-document with unquoted delimiter
+>First line
+>bar	bar
+>some	stuff
+>to  test
+>tab\stripping
+>Last line
+>First line
+>bar	bar
+>some	stuff
+>to  test
+>tab\stripping
+>Last line
+>First line
+>bar	bar
+>some	stuff
+>to  test
+>tab\stripping
+>Last line
+
   #
   # exec tests: perform these in subshells so if they fail the
   # shell won't exit.


Messages sorted by: Reverse Date, Date, Thread, Author