help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh-workers@zsh.org
Subject: PATCH: <<<: Document newline behavior and fix optimization
Date: Tue,  8 Mar 2022 19:58:32 +0100	[thread overview]
Message-ID: <20220308185832.27204-1-mikachu@gmail.com> (raw)

The =(<<<foo) optimization forgot to add a newline, but =(cat<<<foo)
always did, make the behavior consistent, and document it.

This optimization was added in 2005 in workers/21758 (ad2bd42c858) and
has no motivation for excluding the newline, so I assume it's just a
minor mistake.
 Doc/Zsh/redirect.yo   | 3 ++-
 README                | 5 +++++
 Src/exec.c            | 4 +++-
 Test/A04redirect.ztst | 2 +-
 4 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/Doc/Zsh/redirect.yo b/Doc/Zsh/redirect.yo
index 2b48974b40..fd40ab5a4b 100644
--- a/Doc/Zsh/redirect.yo
+++ b/Doc/Zsh/redirect.yo
@@ -86,7 +86,8 @@ item(tt(<<<) var(word))(
 Perform shell expansion on var(word) and pass the result
 to standard input.  This is known as a em(here-string).
 Compare the use of var(word) in here-documents above, where var(word)
-does not undergo shell expansion.
+does not undergo shell expansion.  The result will have a trailing newline
+after it.
 xitem(tt(<&) var(number))
 item(tt(>&) var(number))(
diff --git a/README b/README
index 353667289e..153f02b25b 100644
--- a/README
+++ b/README
@@ -33,6 +33,11 @@ details, see the documentation.
 Incompatibilities since 5.8
+The optimization for the =(<<<foo) construct with no command, which
+creates a temporary file with contents "foo", now adds a newline byte
+after "foo" for consistency with the behaviour of the <<< redirection
+everywhere else.
 PROMPT_SUBST expansion is no longer performed on arguments to prompt-
 expansion sequences such as %F.
diff --git a/Src/exec.c b/Src/exec.c
index f67074846f..70cbfc97fd 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -4836,8 +4836,10 @@ getoutputfile(char *cmd, char **eptr)
 	if (errflag)
 	    s = NULL;
-	else
+	else {
+	    s = dyncat(s, "\n");
+	}
     if (!s)             /* Unclear why we need to do this before open() */
diff --git a/Test/A04redirect.ztst b/Test/A04redirect.ztst
index 993138e7dc..17f6dfa29a 100644
--- a/Test/A04redirect.ztst
+++ b/Test/A04redirect.ztst
@@ -440,7 +440,7 @@
 # This tests the here-string to filename optimisation; we can't
 # test that it's actually being optimised, but we can test that it
 # still works.
-  cat =(<<<$'This string has been replaced\nby a file containing it.\n')
+  cat =(<<<$'This string has been replaced\nby a file containing it.')
 0:Optimised here-string to filename
 >This string has been replaced
 >by a file containing it.

             reply	other threads:[~2022-03-08 18:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-08 18:58 Mikael Magnusson [this message]
2022-03-08 19:38 ` Bart Schaefer
2022-03-08 19:42   ` Bart Schaefer
2022-03-09 11:10     ` Stephane Chazelas
2022-03-13 18:50 ` Stephane Chazelas
2022-03-13 19:28   ` Mikael Magnusson
2022-03-13 19:35     ` Stephane Chazelas
2022-03-30  7:17 ` Jun T
2022-03-30  8:02   ` Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220308185832.27204-1-mikachu@gmail.com \
    --to=mikachu@gmail.com \
    --cc=zsh-workers@zsh.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).