Module Name: src Committed By: kre Date: Mon Nov 22 05:17:43 UTC 2021
Modified Files: src/bin/sh: eval.c expand.c expand.h nodetypes redir.c Log Message: PR bin/53550 Here we go again... One more time to redo how here docs are processed (it has been a few years since the last time!) This is actually a relatively minor change, mostly to timimg (to just when things happen). Now here docs are expanded at the same time the "filename" word in a redirect is expanded, rather than later when the heredoc was being sent to its process. This actually makes things more consistent - but does break one of the ATF tests which was testing that we were (effectively) internally inconsistent in this area. Not all shells agree on the context in which redirection expansions should happen, some make any side effects visible to the parent shell (the majority do) others do the redirection expansions in a subshell so any side effcts are lost. We used to have a foot in each camp, with the majority for everything but here docs, and the minority for here docs. Now we're all the way with LBJ ... (or something like that). To generate a diff of this commit: cvs rdiff -u -r1.185 -r1.186 src/bin/sh/eval.c cvs rdiff -u -r1.140 -r1.141 src/bin/sh/expand.c cvs rdiff -u -r1.25 -r1.26 src/bin/sh/expand.h cvs rdiff -u -r1.19 -r1.20 src/bin/sh/nodetypes cvs rdiff -u -r1.71 -r1.72 src/bin/sh/redir.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/bin/sh/eval.c diff -u src/bin/sh/eval.c:1.185 src/bin/sh/eval.c:1.186 --- src/bin/sh/eval.c:1.185 Tue Nov 16 11:27:50 2021 +++ src/bin/sh/eval.c Mon Nov 22 05:17:43 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: eval.c,v 1.185 2021/11/16 11:27:50 kre Exp $ */ +/* $NetBSD: eval.c,v 1.186 2021/11/22 05:17:43 kre Exp $ */ /*- * Copyright (c) 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; #else -__RCSID("$NetBSD: eval.c,v 1.185 2021/11/16 11:27:50 kre Exp $"); +__RCSID("$NetBSD: eval.c,v 1.186 2021/11/22 05:17:43 kre Exp $"); #endif #endif /* not lint */ @@ -618,6 +618,12 @@ expredir(union node *n) fixredir(redir, fn.list->text, 1); } break; + case NHERE: + redir->nhere.text = redir->nhere.doc->narg.text; + break; + case NXHERE: + redir->nhere.text = expandhere(redir->nhere.doc); + break; } } } Index: src/bin/sh/expand.c diff -u src/bin/sh/expand.c:1.140 src/bin/sh/expand.c:1.141 --- src/bin/sh/expand.c:1.140 Wed Nov 10 15:26:34 2021 +++ src/bin/sh/expand.c Mon Nov 22 05:17:43 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: expand.c,v 1.140 2021/11/10 15:26:34 kre Exp $ */ +/* $NetBSD: expand.c,v 1.141 2021/11/22 05:17:43 kre Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; #else -__RCSID("$NetBSD: expand.c,v 1.140 2021/11/10 15:26:34 kre Exp $"); +__RCSID("$NetBSD: expand.c,v 1.141 2021/11/22 05:17:43 kre Exp $"); #endif #endif /* not lint */ @@ -143,16 +143,16 @@ STATIC void rmescapes_nl(char *); * Expand shell variables and backquotes inside a here document. */ -void -expandhere(union node *arg, int fd) +char * +expandhere(union node *arg) { int len; - VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere() fd=%d\n", fd)); - herefd = fd; + VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere(%p)\n", arg)); expandarg(arg, NULL, 0); len = rmescapes(stackblock()); - xwrite(fd, stackblock(), len); + VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere() -> %d\n", len)); + return stalloc(len + 1); /* include the \0 */ } Index: src/bin/sh/expand.h diff -u src/bin/sh/expand.h:1.25 src/bin/sh/expand.h:1.26 --- src/bin/sh/expand.h:1.25 Thu Feb 13 05:19:05 2020 +++ src/bin/sh/expand.h Mon Nov 22 05:17:43 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: expand.h,v 1.25 2020/02/13 05:19:05 kre Exp $ */ +/* $NetBSD: expand.h,v 1.26 2021/11/22 05:17:43 kre Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -65,7 +65,7 @@ struct arglist { union node; -void expandhere(union node *, int); +char *expandhere(union node *); void expandarg(union node *, struct arglist *, int); int rmescapes(char *); int casematch(union node *, char *); Index: src/bin/sh/nodetypes diff -u src/bin/sh/nodetypes:1.19 src/bin/sh/nodetypes:1.20 --- src/bin/sh/nodetypes:1.19 Tue Nov 16 11:25:44 2021 +++ src/bin/sh/nodetypes Mon Nov 22 05:17:43 2021 @@ -1,4 +1,4 @@ -# $NetBSD: nodetypes,v 1.19 2021/11/16 11:25:44 kre Exp $ +# $NetBSD: nodetypes,v 1.20 2021/11/22 05:17:43 kre Exp $ # Copyright (c) 1991, 1993 # The Regents of the University of California. All rights reserved. # @@ -143,6 +143,7 @@ NXHERE nhere # fd<<! next nodeptr # next redirection in list fd int # file descriptor being redirected doc nodeptr # input to command (NARG node) + text temp char *text # expanded heredoc content NNOT nnot # ! command (actually pipeline) NDNOT nnot # ! ! pipeline (optimisation) Index: src/bin/sh/redir.c diff -u src/bin/sh/redir.c:1.71 src/bin/sh/redir.c:1.72 --- src/bin/sh/redir.c:1.71 Tue Nov 16 11:27:50 2021 +++ src/bin/sh/redir.c Mon Nov 22 05:17:43 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: redir.c,v 1.71 2021/11/16 11:27:50 kre Exp $ */ +/* $NetBSD: redir.c,v 1.72 2021/11/22 05:17:43 kre Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95"; #else -__RCSID("$NetBSD: redir.c,v 1.71 2021/11/16 11:27:50 kre Exp $"); +__RCSID("$NetBSD: redir.c,v 1.72 2021/11/22 05:17:43 kre Exp $"); #endif #endif /* not lint */ @@ -459,12 +459,12 @@ openhere(const union node *redir) if (pipe(pip) < 0) error("Pipe call failed"); - 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; - } + len = strlen(redir->nhere.text); + VTRACE(DBG_REDIR, ("openhere(%p) [%d] \"%.*s\"%s\n", redir, len, + (len < 40 ? len : 40), redir->nhere.text, (len < 40 ? "" : "..."))); + if (len <= PIPESIZE) { /* XXX eventually copy FreeBSD method */ + xwrite(pip[1], redir->nhere.text, len); + goto out; } VTRACE(DBG_REDIR, (" forking [%d,%d]\n", pip[0], pip[1])); if (forkshell(NULL, NULL, FORK_NOJOB) == 0) { @@ -476,10 +476,7 @@ openhere(const union node *redir) signal(SIGTSTP, SIG_IGN); #endif 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], redir->nhere.text, len); VTRACE(DBG_PROCS|DBG_REDIR, ("wrote here doc. exiting(0)\n")); _exit(0); }