diff options
author | Jilles Tjoelker <jilles@FreeBSD.org> | 2017-04-28 16:16:22 +0000 |
---|---|---|
committer | Jilles Tjoelker <jilles@FreeBSD.org> | 2017-04-28 16:16:22 +0000 |
commit | d4993b6db20ea7e28a6db8c466194386ddd7a2a4 (patch) | |
tree | 9d260f18b1e86cef1fe2e870cd212b07fa615a1c /bin/sh | |
parent | b6ecf4345016b4997da439a7cb28df637627f52d (diff) | |
download | src-d4993b6db20ea7e28a6db8c466194386ddd7a2a4.tar.gz src-d4993b6db20ea7e28a6db8c466194386ddd7a2a4.zip |
sh: Simplify handling of newlines in command substitution.
Unless we need to split on newlines, just append them as normal and remove
them at the end.
Notes
Notes:
svn path=/head/; revision=317559
Diffstat (limited to 'bin/sh')
-rw-r--r-- | bin/sh/expand.c | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/bin/sh/expand.c b/bin/sh/expand.c index af88771babe1..30f0ec50ec58 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -462,6 +462,7 @@ expbackq(union node *cmd, int quoted, int flag, struct worddest *dst) int quotes = flag & (EXP_GLOB | EXP_CASE); size_t nnl; const char *ifs; + int startloc; INTOFF; p = grabstackstr(dest); @@ -469,6 +470,7 @@ expbackq(union node *cmd, int quoted, int flag, struct worddest *dst) ungrabstackstr(p, dest); p = in.buf; + startloc = dest - stackblock(); nnl = 0; if (!quoted && flag & EXP_SPLIT) ifs = ifsset() ? ifsval() : " \t\n"; @@ -490,31 +492,24 @@ expbackq(union node *cmd, int quoted, int flag, struct worddest *dst) lastc = *p++; if (lastc == '\0') continue; - if (lastc == '\n') { - nnl++; - } else { - if (nnl > 0) { - if (strchr(ifs, '\n') != NULL) { - NEXTWORD('\n', flag, dest, dst); - nnl = 0; - } else { - CHECKSTRSPACE(nnl + 2, dest); - while (nnl > 0) { - nnl--; - USTPUTC('\n', dest); - } - } - } - if (strchr(ifs, lastc) != NULL) + if (nnl > 0 && lastc != '\n') { + NEXTWORD('\n', flag, dest, dst); + nnl = 0; + } + if (strchr(ifs, lastc) != NULL) { + if (lastc == '\n') + nnl++; + else NEXTWORD(lastc, flag, dest, dst); - else { - CHECKSTRSPACE(2, dest); - if (quotes && syntax[(int)lastc] == CCTL) - USTPUTC(CTLESC, dest); - USTPUTC(lastc, dest); - } + } else { + CHECKSTRSPACE(2, dest); + if (quotes && syntax[(int)lastc] == CCTL) + USTPUTC(CTLESC, dest); + USTPUTC(lastc, dest); } } + while (dest > stackblock() + startloc && STTOPC(dest) == '\n') + STUNPUTC(dest); if (in.fd >= 0) close(in.fd); |