home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!concert!sas!mozart.unx.sas.com!foster!sherman
- From: sherman@unx.sas.com (Chris Sherman)
- Subject: Re: Distinguishing stdout and stderr on a pipe
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Message-ID: <sherman.722141044@foster>
- Date: Thu, 19 Nov 1992 02:44:04 GMT
- References: <BxxI3B.G8p@da_vinci.it.uswc.uswest.com>
- Nntp-Posting-Host: foster.unx.sas.com
- Organization: SAS Institute Inc.
- Lines: 152
-
- In <BxxI3B.G8p@da_vinci.it.uswc.uswest.com> rray@lookout.it.uswc.uswest.com (Randy J. Ray) writes:
-
- >Is there a way to read from a pipe and capture both stdout and stderr, but
- >still be able to distunguish them? I need to run a command while formatting
- >returned data one way, and errors a different way.
-
- >I have not been able to find any references to this in the Camel book or the
- >USENIX tutorial.
-
- This hit the newsgroup a long time ago. It may be useful...
-
- ---------------------------------------------------------------------------
-
- >From comp.lang.perl Mon Mar 23 01:32:19 1992
- Path: wagner.unx.sas.com!mozart.unx.sas.com!sas!rock!taco!lll-winken!ames!agate!spool.mu.edu!caen!uvaarpa!mmdf
- From: marc@mit.edu (Marc Horowitz)
- Newsgroups: comp.lang.perl
- Subject: Re: how to grab STDERR of backticked command
- Message-ID: <1992Mar22.084122.25819@uvaarpa.Virginia.EDU>
- Date: 22 Mar 92 08:41:22 GMT
- Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
- Reply-To: Marc Horowitz <marc@mit.edu>
- Organization: The Internet
- Lines: 121
-
- |> This is all very well, but I have a different situation where I want
- |> to catch stdout and stderr and do different things with them!
-
- I have a modified version of open2, called open3, which lets you do
- different things with stdout and stderr. You might do something like:
-
- open(LOGFILE,">logfile");
- $child = &open3('INPUT','>&LOGFILE','ERRORS','cc','foo.c');
- while(<ERRORS>) {
- # do whatever
- }
-
- Marc
-
- --cut--
- # &open3: Marc Horowitz <marc@mit.edu>
- # derived mostly from &open2 by tom christiansen, <tchrist@convex.com>
- #
- # usage: $pid = open3('wtr', 'rdr', 'err' 'some cmd and args', 'optarg', ...);
- #
- # spawn the given $cmd and connect rdr for
- # reading, wtr for writing, and err for errors.
- # if err is '', or the same as rdr, then stdout and
- # stderr of the child are on the same fh. returns pid
- # of child, or 0 on failure.
-
-
- # if wtr begins with '>&', then wtr will be closed in the parent, and
- # the child will read from it directly. if rdr or err begins with
- # '>&', then the child will send output directly to that fd. In both
- # cases, there will be a dup() instead of a pipe() made.
-
-
- # WARNING: this is dangerous, as you may block forever
- # unless you are very careful.
- #
- # $wtr is left unbuffered.
- #
- # abort program if
- # rdr or wtr are null
- # pipe or fork or exec fails
-
- package open3;
-
- $fh = 'FHOPEN000'; # package static in case called more than once
-
- sub main'open3 {
- local($kidpid);
- local($dad_wtr, $dad_rdr, $dad_err, @cmd) = @_;
- local($dup_wtr, $dup_rdr, $dup_err);
-
- $dad_wtr || die "open3: wtr should not be null";
- $dad_rdr || die "open3: rdr should not be null";
- $dad_err = $dad_rdr if ($dad_err eq '');
-
- $dup_wtr = ($dad_wtr =~ s/^\>\&//);
- $dup_rdr = ($dad_rdr =~ s/^\>\&//);
- $dup_err = ($dad_err =~ s/^\>\&//);
-
- # force unqualified filehandles into callers' package
- local($package) = caller;
- $dad_wtr =~ s/^[^']+$/$package'$&/;
- $dad_rdr =~ s/^[^']+$/$package'$&/;
- $dad_err =~ s/^[^']+$/$package'$&/;
-
- local($kid_rdr) = ++$fh;
- local($kid_wtr) = ++$fh;
- local($kid_err) = ++$fh;
-
- if (!$dup_wtr) {
- pipe($kid_rdr, $dad_wtr) || die "open3: pipe 1 (stdin) failed: $!";
- }
- if (!$dup_rdr) {
- pipe($dad_rdr, $kid_wtr) || die "open3: pipe 2 (stdout) failed: $!";
- }
- if ($dad_err ne $dad_rdr && !$dup_err) {
- pipe($dad_err, $kid_err) || die "open3: pipe 3 (stderr) failed: $!";
- }
-
- if (($kidpid = fork) < 0) {
- die "open2: fork failed: $!";
- } elsif ($kidpid == 0) {
- if ($dup_wtr) {
- open(STDIN, ">&$dad_wtr") if (fileno(STDIN) != fileno($dad_wtr));
- } else {
- close($dad_wtr);
- open(STDIN, ">&$kid_rdr");
- }
- if ($dup_rdr) {
- open(STDOUT, ">&$dad_rdr") if (fileno(STDOUT) != fileno($dad_rdr));
- } else {
- close($dad_rdr);
- open(STDOUT, ">&$kid_wtr");
- }
- if ($dad_rdr ne $dad_err) {
- if ($dup_err) {
- open(STDERR, ">&$dad_err")
- if (fileno(STDERR) != fileno($dad_err));
- } else {
- close($dad_err);
- open(STDERR, ">&$kid_err");
- }
- } else {
- open(STDERR, ">&STDOUT") if (fileno(STDERR) != fileno(STDOUT));
- }
- exec @cmd;
-
- local($")=(" ");
- die "open2: exec of @cmd failed";
- }
-
- close $kid_rdr; close $kid_wtr; close $kid_err;
- if ($dup_wtr) {
- close($dad_wtr);
- }
-
- select((select($dad_wtr), $| = 1)[0]); # unbuffer pipe
- $kidpid;
- }
- 1; # so require is happy
- --cut--
-
- --
- ____/ / / __ / _ _/ ____/
- / / / / / / / Chris Sherman
- / ___ / _/ / /
- _____/ __/ __/ __/ _\ _____/ _____/ sherman@unx.sas.com
-