zsh-workers
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: zsh-workers@sunsite.auc.dk
Subject: PATCH: Re: redirection and xtrace output
Date: Sun, 20 Feb 2000 01:18:30 +0000	[thread overview]
Message-ID: <1000220011830.ZM18156@candle.brasslantern.com> (raw)
In-Reply-To: <1000219191951.ZM11073@candle.brasslantern.com>

On Feb 19,  7:19pm, Bart Schaefer wrote:
} Subject: PATCH: redirection and xtrace output
}
} This patch introduces a global FILE *xtrerr which is used for (hopefully)
} all xtrace output.
} 
} There is still one problem with this:  The xtrace output of ( ... ) and
} { ... } constructs is NOT redirected with stderr, which it should be.

There was actually one other problem:  When tracing an external command,
the call to closem() was killing fileno(xtrerr) before it was flushed,
so some trace output didn't appear.

The following fixes all these problems, and also catches cases I missed
where execcmd() could return without closing/restoring xtrerr.  It took
me a ridiculously long time to figure out the right test to use for the
( ... ) and { ... } cases, but it turned out to be quite simple.  In the
process I created a test script; I arbitrarily numbered it after the cd
test, becaue it didn't seem to be in the same category as the others.

Index: Src/exec.c
===================================================================
@@ -129,7 +129,7 @@
 /**/
 mod_export Funcstack funcstack;
 
-#define execerr() if (!forked) { lastval = 1; return; } else _exit(1)
+#define execerr() if (!forked) { lastval = 1; goto done; } else _exit(1)
 
 static LinkList args;
 static int doneps4;
@@ -473,6 +473,7 @@
     }
 
     argv = makecline(args);
+    closem(3);
     child_unblock();
     if ((int) strlen(arg0) >= PATH_MAX) {
 	zerr("command too long: %s", arg0, 0);
@@ -1937,9 +1938,12 @@
 
     /* Make a copy of stderr for xtrace output before redirecting */
     fflush(xtrerr);
-    if (xtrerr == stderr &&
-	!(xtrerr = fdopen(movefd(dup(fileno(stderr))), "w")))
-	xtrerr = stderr;
+    if (xtrerr == stderr && (type < WC_SUBSH || type == WC_TIMED)) {
+	if (!(xtrerr = fdopen(movefd(dup(fileno(stderr))), "w")))
+	    xtrerr = stderr;
+	else
+	    fdtable[fileno(xtrerr)] = 3;
+    }
 
     /* Add pipeline input/output to mnodes */
     if (input)
Index: Test/.distfiles
===================================================================
@@ -3,5 +3,5 @@
     ztst.zsh
     01grammar.ztst 02alias.ztst 03quoting.ztst 04redirect.ztst
     05command.ztst 06arith.ztst 07cond.ztst 08traps.ztst 09funcdef.ztst
-    10prompt.ztst 50cd.ztst
+    10prompt.ztst 50cd.ztst 51xtrace.ztst
 '

--- /dev/null	Tue May  5 13:32:27 1998
+++ Test/51xtrace.ztst	Sat Feb 19 16:53:07 2000
@@ -0,0 +1,60 @@
+# Test that xtrace output is correctly generated
+
+%prep
+  mkdir xtrace.tmp && cd xtrace.tmp
+
+%test
+
+  set -x
+  print 'Tracing: builtin'
+  print 'Tracing: builtin 2>file' 2>xtrace.err
+  cat <<<'Tracing: external'
+  cat <<<'Tracing: external 2>file' 2>>xtrace.err
+  ( print 'Tracing: ( builtin )' )
+  ( print 'Tracing: ( builtin ) 2>file' ) 2>>xtrace.err
+  ( cat <<<'Tracing: ( external )' )
+  ( cat <<<'Tracing: ( external ) 2>file' ) 2>>xtrace.err
+  { print 'Tracing: { builtin }' }
+  { print 'Tracing: { builtin } 2>file' } 2>>xtrace.err
+  { cat <<<'Tracing: { external }' }
+  { cat <<<'Tracing: { external } 2>file' } 2>>xtrace.err
+  repeat 1 do print 'Tracing: do builtin done'; done
+  repeat 1 do print 'Tracing: do builtin done 2>file'; done 2>>xtrace.err
+  repeat 1 do cat <<<'Tracing: do external done'; done
+  repeat 1 do cat <<<'Tracing: do external done'; done 2>>xtrace.err
+  set +x
+  cat xtrace.err
+0:xtrace with and without redirection
+>Tracing: builtin
+>Tracing: builtin 2>file
+>Tracing: external
+>Tracing: external 2>file
+>Tracing: ( builtin )
+>Tracing: ( builtin ) 2>file
+>Tracing: ( external )
+>Tracing: ( external ) 2>file
+>Tracing: { builtin }
+>Tracing: { builtin } 2>file
+>Tracing: { external }
+>Tracing: { external } 2>file
+>Tracing: do builtin done
+>Tracing: do builtin done 2>file
+>Tracing: do external done
+>Tracing: do external done
+>+ZTST_execchunk:2> print Tracing: ( builtin ) 2>file
+>+ZTST_execchunk:2> cat
+>+ZTST_execchunk:2> print Tracing: { builtin } 2>file
+>+ZTST_execchunk:2> cat
+>+ZTST_execchunk:2> print Tracing: do builtin done 2>file
+>+ZTST_execchunk:2> cat
+?+ZTST_execchunk:2> print Tracing: builtin
+?+ZTST_execchunk:2> print Tracing: builtin 2>file
+?+ZTST_execchunk:2> cat
+?+ZTST_execchunk:2> cat
+?+ZTST_execchunk:2> print Tracing: ( builtin )
+?+ZTST_execchunk:2> cat
+?+ZTST_execchunk:2> print Tracing: { builtin }
+?+ZTST_execchunk:2> cat
+?+ZTST_execchunk:2> print Tracing: do builtin done
+?+ZTST_execchunk:2> cat
+?+ZTST_execchunk:2> set +x

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


      reply	other threads:[~2000-02-20  1:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-19 19:19 PATCH: " Bart Schaefer
2000-02-20  1:18 ` Bart Schaefer [this message]

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=1000220011830.ZM18156@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=zsh-workers@sunsite.auc.dk \
    /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).