From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH?] Nofork and removing newlines
Date: Mon, 4 Mar 2024 21:52:02 -0800 [thread overview]
Message-ID: <CAH+w=7aFJH-5GAu6puszGKvjqN-w0sPCi6v-vGPauDHVujDnag@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 410 bytes --]
On Tue, Feb 27, 2024 at 12:53 PM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> The intent was to have ${ ... } act more like parameter substitution.
> It might be possible/reasonable to have ${ ... } strip newlines and
> "${ ... }" keep them, if that feels better.
The attached patch implements this, for purposes of discussion. The
doc updates are much larger than the actual code change.
[-- Attachment #2: nofork-nonewlines.txt --]
[-- Type: text/plain, Size: 3711 bytes --]
diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 183ca6e03..b77942697 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -1950,6 +1950,9 @@ the braces by whitespace, like `tt(${ )...tt( })', is replaced by its
standard output. Like `tt(${|)...tt(})' and unlike
`tt($LPAR())...tt(RPAR())', the command executes in the current shell
context with function local behaviors and does not create a subshell.
+Word splitting does not apply unless tt(SH_WORD_SPLIT) is set, but
+trailing newlines em(are) stripped unless the substitution is enclosed
+in double quotes.
Note that because the `tt(${|)...tt(})' and `tt(${ )...tt( })' forms
must be parsed at once as both string tokens and commands, all other
diff --git a/Etc/FAQ.yo b/Etc/FAQ.yo
index 4a86050e6..0515d2fca 100644
--- a/Etc/FAQ.yo
+++ b/Etc/FAQ.yo
@@ -1092,10 +1092,11 @@ sect(Comparisons of forking and non-forking command substitution)
affects the caller.
mytt($(command)) removes trailing newlines from the output of mytt(command)
- when substituting, whereas mytt(${ command }) and its variants do not.
- The latter is consistent with mytt(${|...}) from mksh but differs from
- bash and ksh, so in emulation modes, newlines are stripped from command
- output (not from tt(REPLY) assignments).
+ when substituting, as does mytt(${ command }) when not quoted. Placing
+ double quotes around mytt("${ command }"), or using either mytt(${|...})
+ format, retains newlines. The latter is consistent with mytt(${|...})
+ from mksh, but mytt("${ command }") differs from bash and ksh, so in
+ emulation modes, newlines stripped even from quoted command output.
When not enclosed in double quotes, the expansion of mytt($(command)) is
split on tt(IFS) into an array of words. In contrast, and unlike both
diff --git a/Src/subst.c b/Src/subst.c
index 49f7336bb..785137357 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2005,7 +2005,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
int onoerrs = noerrs, rplylen;
noerrs = 2;
rplylen = zstuff(&cmdarg, rplytmp);
- if (! EMULATION(EMULATE_ZSH)) {
+ if (! EMULATION(EMULATE_ZSH) || !qt) {
/* bash and ksh strip trailing newlines here */
while (rplylen > 0 && cmdarg[rplylen-1] == '\n')
rplylen--;
diff --git a/Test/D10nofork.ztst b/Test/D10nofork.ztst
index d6a5588df..1c6a30cb0 100644
--- a/Test/D10nofork.ztst
+++ b/Test/D10nofork.ztst
@@ -159,7 +159,7 @@ F:Why not use this error in the previous case as well?
1:unbalanced braces, part 4+
?(eval):1: closing brace expected
- purr ${ purr STDOUT }
+ purr "${ purr STDOUT }"
0:capture stdout
>STDOUT
>
@@ -322,7 +322,7 @@ F:Fiddly here to get EOF past the test syntax
0:here-string behavior
>in a here string
- <<<${ purr $'stdout as a here string' }
+ <<<"${ purr $'stdout as a here string' }"
0:another capture stdout
>stdout as a here string
>
@@ -331,7 +331,7 @@ F:Fiddly here to get EOF past the test syntax
wrap=${ purr "capture in environment assignment" } typeset -p wrap
0:assignment context
>typeset -g wrap='REPLY in environment assignment'
->typeset -g wrap=$'capture in environment assignment\n'
+>typeset -g wrap='capture in environment assignment'
# Repeat return and exit tests with stdout capture
@@ -410,7 +410,7 @@ F:must do this before evaluating the next test block
0:ignored braces, part 1
>buried}
- purr ${ purr ${REPLY:-buried}}}
+ purr "${ purr ${REPLY:-buried}}}"
0:ignored braces, part 2
>buried
>}
@@ -418,7 +418,6 @@ F:must do this before evaluating the next test block
purr ${ { echo nested ;} }
0:ignored braces, part 3
>nested
->
purr ${ { echo nested } } DONE
1:ignored braces, part 4
next reply other threads:[~2024-03-05 5:52 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-05 5:52 Bart Schaefer [this message]
2024-03-05 6:56 ` Stephane Chazelas
2024-03-05 22:48 ` Bart Schaefer
2024-03-06 17:57 ` Stephane Chazelas
2024-03-06 19:45 ` Bart Schaefer
2024-03-06 22:22 ` Mikael Magnusson
2024-03-06 22:42 ` Bart Schaefer
2024-03-07 4:53 ` Bart Schaefer
2024-03-07 7:02 ` Lawrence Velázquez
2024-03-07 8:09 ` ${<file} (Was: [PATCH?] Nofork and removing newlines) Stephane Chazelas
2024-03-08 1:29 ` [PATCH?] Nofork and removing newlines Bart Schaefer
2024-03-08 22:15 ` Oliver Kiddle
2024-03-08 23:28 ` Bart Schaefer
2024-03-09 20:43 ` Oliver Kiddle
2024-03-10 6:11 ` Bart Schaefer
2024-03-12 17:54 ` Bart Schaefer
2024-03-12 23:19 ` Oliver Kiddle
2024-03-13 4:13 ` Bart Schaefer
2024-03-14 22:15 ` Oliver Kiddle
2024-03-15 8:42 ` Stephane Chazelas
2024-03-27 1:16 ` Bart Schaefer
2024-03-27 7:05 ` Bart Schaefer
2024-03-07 7:10 ` Stephane Chazelas
2024-03-08 0:37 ` Bart Schaefer
2024-03-07 6:52 ` Lawrence Velázquez
2024-03-07 8:26 ` Mikael Magnusson
2024-03-07 19:02 ` Bart Schaefer
2024-04-02 6:45 ` Lawrence Velázquez
2024-03-06 19:43 ` Stephane Chazelas
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:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAH+w=7aFJH-5GAu6puszGKvjqN-w0sPCi6v-vGPauDHVujDnag@mail.gmail.com' \
--to=schaefer@brasslantern.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* 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
https://git.vuxu.org/mirror/zsh/
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).