Module Name:    src
Committed By:   kre
Date:           Fri Jun 30 23:01:21 UTC 2017

Modified Files:
        src/bin/sh: eval.c redir.c redir.h

Log Message:
Include redirections in trace output from "set -x"


To generate a diff of this commit:
cvs rdiff -u -r1.150 -r1.151 src/bin/sh/eval.c
cvs rdiff -u -r1.57 -r1.58 src/bin/sh/redir.c
cvs rdiff -u -r1.23 -r1.24 src/bin/sh/redir.h

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.150 src/bin/sh/eval.c:1.151
--- src/bin/sh/eval.c:1.150	Mon Jun 19 03:21:31 2017
+++ src/bin/sh/eval.c	Fri Jun 30 23:01:21 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: eval.c,v 1.150 2017/06/19 03:21:31 kre Exp $	*/
+/*	$NetBSD: eval.c,v 1.151 2017/06/30 23:01:21 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.150 2017/06/19 03:21:31 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.151 2017/06/30 23:01:21 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -276,9 +276,24 @@ evaltree(union node *n, int flags)
 		break;
 	case NREDIR:
 		expredir(n->nredir.redirect);
+		if (xflag && n->nredir.redirect) {
+			union node *rn;
+
+			out2str(expandstr(ps4val(), line_number));
+			out2str("using redirections:");
+			for (rn = n->nredir.redirect; rn; rn = rn->nfile.next)
+				(void) outredir(&errout, rn, ' ');
+			out2str(" do\n");
+			flushout(&errout);
+		}
 		redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP);
 		evaltree(n->nredir.n, flags);
 		popredir();
+		if (xflag && n->nredir.redirect) {
+			out2str(expandstr(ps4val(), line_number));
+			out2str("done\n");
+			flushout(&errout);
+		}
 		break;
 	case NSUBSHELL:
 		evalsubshell(n, flags & ~EV_MORE);
@@ -422,7 +437,7 @@ evalfor(union node *n, int flags)
 			f |= EV_MORE;
 
 		if (xflag) {
-			out2str(ps4val());
+			out2str(expandstr(ps4val(), line_number));
 			out2str("for ");
 			out2str(n->nfor.var);
 			out2c('=');
@@ -505,6 +520,16 @@ evalsubshell(union node *n, int flags)
 	int backgnd = (n->type == NBACKGND);
 
 	expredir(n->nredir.redirect);
+	if (xflag && n->nredir.redirect) {
+		union node *rn;
+
+		out2str(expandstr(ps4val(), line_number));
+		out2str("using redirections:");
+		for (rn = n->nredir.redirect; rn; rn = rn->nfile.next)
+			(void) outredir(&errout, rn, ' ');
+		out2str(" do subshell\n");
+		flushout(&errout);
+	}
 	INTOFF;
 	jp = makejob(n, 1);
 	if (forkshell(jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
@@ -517,6 +542,11 @@ evalsubshell(union node *n, int flags)
 	}
 	exitstatus = backgnd ? 0 : waitforjob(jp);
 	INTON;
+	if (!backgnd && xflag && n->nredir.redirect) {
+		out2str(expandstr(ps4val(), line_number));
+		out2str("done subshell\n");
+		flushout(&errout);
+	}
 }
 
 
@@ -771,8 +801,7 @@ evalcommand(union node *cmd, int flgs, s
 	setstackmark(&smark);
 	back_exitstatus = 0;
 
-	if (cmd != NULL)
-		line_number = cmd->ncmd.lineno;
+	line_number = cmd->ncmd.lineno;
 
 	arglist.lastp = &arglist.list;
 	varflag = 1;
@@ -830,7 +859,9 @@ evalcommand(union node *cmd, int flgs, s
 	/* Print the command if xflag is set. */
 	if (xflag) {
 		char sep = 0;
-		out2str(ps4val());
+		union node *rn;
+
+		out2str(expandstr(ps4val(), line_number));
 		for (sp = varlist.list ; sp ; sp = sp->next) {
 			char *p;
 
@@ -859,6 +890,9 @@ evalcommand(union node *cmd, int flgs, s
 			out2shstr(sp->text);
 			sep = ' ';
 		}
+		for (rn = cmd->ncmd.redirect; rn; rn = rn->nfile.next)
+			if (outredir(&errout, rn, sep))
+				sep = ' ';
 		outc('\n', &errout);
 		flushout(&errout);
 	}

Index: src/bin/sh/redir.c
diff -u src/bin/sh/redir.c:1.57 src/bin/sh/redir.c:1.58
--- src/bin/sh/redir.c:1.57	Mon May 29 22:21:00 2017
+++ src/bin/sh/redir.c	Fri Jun 30 23:01:21 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $	*/
+/*	$NetBSD: redir.c,v 1.58 2017/06/30 23:01:21 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.57 2017/05/29 22:21:00 kre Exp $");
+__RCSID("$NetBSD: redir.c,v 1.58 2017/06/30 23:01:21 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -878,3 +878,70 @@ fdflagscmd(int argc, char *argv[])
 	}
 	return 0;
 }
+
+#undef MAX		/* in case we inherited them from somewhere */
+#undef MIN
+
+#define	MIN(a,b)	(/*CONSTCOND*/((a)<=(b)) ? (a) : (b))
+#define	MAX(a,b)	(/*CONSTCOND*/((a)>=(b)) ? (a) : (b))
+
+		/* now make the compiler work for us... */
+#define	MIN_REDIR	MIN(MIN(MIN(MIN(NTO,NFROM), MIN(NTOFD,NFROMFD)), \
+		   MIN(MIN(NCLOBBER,NAPPEND), MIN(NHERE,NXHERE))), NFROMTO)
+#define	MAX_REDIR	MAX(MAX(MAX(MAX(NTO,NFROM), MAX(NTOFD,NFROMFD)), \
+		   MAX(MAX(NCLOBBER,NAPPEND), MAX(NHERE,NXHERE))), NFROMTO)
+
+static const char *redir_sym[MAX_REDIR - MIN_REDIR + 1] = {
+	[NTO      - MIN_REDIR]=	">",
+	[NFROM    - MIN_REDIR]=	"<",
+	[NTOFD    - MIN_REDIR]=	">&",
+	[NFROMFD  - MIN_REDIR]=	"<&",
+	[NCLOBBER - MIN_REDIR]=	">|",
+	[NAPPEND  - MIN_REDIR]=	">>",
+	[NHERE    - MIN_REDIR]=	"<<",
+	[NXHERE   - MIN_REDIR]=	"<<",
+	[NFROMTO  - MIN_REDIR]=	"<>",
+};
+
+int
+outredir(struct output *out, union node *n, int sep)
+{
+	if (n == NULL)
+		return 0;
+	if (n->type < MIN_REDIR || n->type > MAX_REDIR ||
+	    redir_sym[n->type - MIN_REDIR] == NULL)
+		return 0;
+
+	if (sep)
+		outc(sep, out);
+
+	/*
+	 * ugly, but all redir node types have "fd" in same slot...
+	 *	(and code other places assumes it as well)
+	 */
+	if ((redir_sym[n->type - MIN_REDIR][0] == '<' && n->nfile.fd != 0) ||
+	    (redir_sym[n->type - MIN_REDIR][0] == '>' && n->nfile.fd != 1))
+		outfmt(out, "%d", n->nfile.fd);
+
+	outstr(redir_sym[n->type - MIN_REDIR], out);
+
+	switch (n->type) {
+	case NHERE:
+		outstr("'...'", out);
+		break;
+	case NXHERE:
+		outstr("...", out);
+		break;
+	case NTOFD:
+	case NFROMFD:
+		if (n->ndup.dupfd < 0)
+			outc('-', out);
+		else
+			outfmt(out, "%d", n->ndup.dupfd);
+		break;
+	default:
+		outstr(n->nfile.expfname, out);
+		break;
+	}
+	return 1;
+}

Index: src/bin/sh/redir.h
diff -u src/bin/sh/redir.h:1.23 src/bin/sh/redir.h:1.24
--- src/bin/sh/redir.h:1.23	Sat Apr 29 15:14:28 2017
+++ src/bin/sh/redir.h	Fri Jun 30 23:01:21 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: redir.h,v 1.23 2017/04/29 15:14:28 kre Exp $	*/
+/*	$NetBSD: redir.h,v 1.24 2017/06/30 23:01:21 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -49,5 +49,7 @@ int movefd(int, int);
 int to_upper_fd(int);
 void register_sh_fd(int, void (*)(int, int));
 void sh_close(int);
+struct output;
+int outredir(struct output *, union node *, int);
 
 int max_user_fd;		/* highest fd used by user */

Reply via email to