From 6fa139d56eac8251b9a64c249c8e6fc8131f405c Mon Sep 17 00:00:00 2001 From: jilles Date: Sun, 3 Feb 2013 15:54:57 +0000 Subject: sh: Expand here documents in the current process. Expand here documents at the same point other redirections are expanded but use a non-fork subshell environment (like simple command substitutions) for compatibility. Substitition errors result in an empty here document like before. As a result, a fork is avoided for short (<4K) expanded here documents. Unexpanded here documents (with quoted end marker after <<) are not affected by this change. They already only forked when >4K. Side effects: * Order of expansion is slightly different. * Slow expansions are not executed in parallel with the redirected command. * A non-fork subshell environment is subtly different from a forked process. --- bin/sh/redir.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'bin/sh/redir.c') diff --git a/bin/sh/redir.c b/bin/sh/redir.c index 915b2fc..c11cb71 100644 --- a/bin/sh/redir.c +++ b/bin/sh/redir.c @@ -251,18 +251,23 @@ movefd: static int openhere(union node *redir) { + char *p; int pip[2]; int len = 0; if (pipe(pip) < 0) error("Pipe call failed: %s", strerror(errno)); - if (redir->type == NHERE) { - len = strlen(redir->nhere.doc->narg.text); - if (len <= PIPESIZE) { - xwrite(pip[1], redir->nhere.doc->narg.text, len); - goto out; - } + + if (redir->type == NXHERE) + p = redir->nhere.expdoc; + else + p = redir->nhere.doc->narg.text; + len = strlen(p); + if (len <= PIPESIZE) { + xwrite(pip[1], p, len); + goto out; } + if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { close(pip[0]); signal(SIGINT, SIG_IGN); @@ -270,10 +275,7 @@ openhere(union node *redir) signal(SIGHUP, SIG_IGN); signal(SIGTSTP, SIG_IGN); signal(SIGPIPE, SIG_DFL); - if (redir->type == NHERE) - xwrite(pip[1], redir->nhere.doc->narg.text, len); - else - expandhere(redir->nhere.doc, pip[1]); + xwrite(pip[1], p, len); _exit(0); } out: -- cgit v1.1