home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / IPC / Open3.pm < prev   
Encoding:
Perl POD Document  |  1999-12-28  |  6.9 KB  |  252 lines

  1. package IPC::Open3;
  2.  
  3. use strict;
  4. no strict 'refs'; # because users pass me bareword filehandles
  5. use vars qw($VERSION @ISA @EXPORT $Fh $Me);
  6.  
  7. require 5.001;
  8. require Exporter;
  9.  
  10. use Carp;
  11. use Symbol 'qualify';
  12.  
  13. $VERSION    = 1.0101;
  14. @ISA        = qw(Exporter);
  15. @EXPORT        = qw(open3);
  16.  
  17. =head1 NAME
  18.  
  19. IPC::Open3, open3 - open a process for reading, writing, and error handling
  20.  
  21. =head1 SYNOPSIS
  22.  
  23.     $pid = open3(\*WTRFH, \*RDRFH, \*ERRFH,
  24.             'some cmd and args', 'optarg', ...);
  25.  
  26. =head1 DESCRIPTION
  27.  
  28. Extremely similar to open2(), open3() spawns the given $cmd and
  29. connects RDRFH for reading, WTRFH for writing, and ERRFH for errors.  If
  30. ERRFH is '', or the same as RDRFH, then STDOUT and STDERR of the child are
  31. on the same file handle.  The WTRFH will have autoflush turned on.
  32.  
  33. If WTRFH begins with "E<lt>&", then WTRFH will be closed in the parent, and
  34. the child will read from it directly.  If RDRFH or ERRFH begins with
  35. "E<gt>&", then the child will send output directly to that file handle.
  36. In both cases, there will be a dup(2) instead of a pipe(2) made.
  37.  
  38. If you try to read from the child's stdout writer and their stderr
  39. writer, you'll have problems with blocking, which means you'll
  40. want to use select(), which means you'll have to use sysread() instead
  41. of normal stuff.
  42.  
  43. open3() returns the process ID of the child process.  It doesn't return on
  44. failure: it just raises an exception matching C</^open3:/>.
  45.  
  46. =head1 WARNING
  47.  
  48. It will not create these file handles for you.  You have to do this
  49. yourself.  So don't pass it empty variables expecting them to get filled
  50. in for you.
  51.  
  52. Additionally, this is very dangerous as you may block forever.  It
  53. assumes it's going to talk to something like B<bc>, both writing to it
  54. and reading from it.  This is presumably safe because you "know" that
  55. commands like B<bc> will read a line at a time and output a line at a
  56. time.  Programs like B<sort> that read their entire input stream first,
  57. however, are quite apt to cause deadlock.
  58.  
  59. The big problem with this approach is that if you don't have control
  60. over source code being run in the child process, you can't control
  61. what it does with pipe buffering.  Thus you can't just open a pipe to
  62. C<cat -v> and continually read and write a line from it.
  63.  
  64. =cut
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. $Fh = 'FHOPEN000';    # package static in case called more than once
  72. $Me = 'open3 (bug)';    # you should never see this, it's always localized
  73.  
  74.  
  75. sub xfork {
  76.     my $pid = fork;
  77.     defined $pid or croak "$Me: fork failed: $!";
  78.     return $pid;
  79. }
  80.  
  81. sub xpipe {
  82.     pipe $_[0], $_[1] or croak "$Me: pipe($_[0], $_[1]) failed: $!";
  83. }
  84.  
  85.  
  86. sub xopen {
  87.     open $_[0], $_[1] or croak "$Me: open($_[0], $_[1]) failed: $!";
  88. }
  89.  
  90. sub xclose {
  91.     close $_[0] or croak "$Me: close($_[0]) failed: $!";
  92. }
  93.  
  94. my $do_spawn = $^O eq 'os2';
  95.  
  96. sub _open3 {
  97.     local $Me = shift;
  98.     my($package, $dad_wtr, $dad_rdr, $dad_err, @cmd) = @_;
  99.     my($dup_wtr, $dup_rdr, $dup_err, $kidpid);
  100.  
  101.     $dad_wtr            or croak "$Me: wtr should not be null";
  102.     $dad_rdr            or croak "$Me: rdr should not be null";
  103.     $dad_err = $dad_rdr if ($dad_err eq '');
  104.  
  105.     $dup_wtr = ($dad_wtr =~ s/^[<>]&//);
  106.     $dup_rdr = ($dad_rdr =~ s/^[<>]&//);
  107.     $dup_err = ($dad_err =~ s/^[<>]&//);
  108.  
  109.     $dad_wtr = qualify $dad_wtr, $package;
  110.     $dad_rdr = qualify $dad_rdr, $package;
  111.     $dad_err = qualify $dad_err, $package;
  112.  
  113.     my $kid_rdr = ++$Fh;
  114.     my $kid_wtr = ++$Fh;
  115.     my $kid_err = ++$Fh;
  116.  
  117.     xpipe $kid_rdr, $dad_wtr if !$dup_wtr;
  118.     xpipe $dad_rdr, $kid_wtr if !$dup_rdr;
  119.     xpipe $dad_err, $kid_err if !$dup_err && $dad_err ne $dad_rdr;
  120.  
  121.     $kidpid = $do_spawn ? -1 : xfork;
  122.     if ($kidpid == 0) {        # Kid
  123.     if ($dad_rdr ne $dad_err && $dup_err
  124.         && fileno($dad_err) == fileno(STDOUT)) {
  125.         my $tmp = ++$Fh;
  126.         xopen($tmp, ">&$dad_err");
  127.         $dad_err = $tmp;
  128.     }
  129.  
  130.     if ($dup_wtr) {
  131.         xopen \*STDIN,  "<&$dad_wtr" if fileno(STDIN) != fileno($dad_wtr);
  132.     } else {
  133.         xclose $dad_wtr;
  134.         xopen \*STDIN,  "<&$kid_rdr";
  135.         xclose $kid_rdr;
  136.     }
  137.     if ($dup_rdr) {
  138.         xopen \*STDOUT, ">&$dad_rdr" if fileno(STDOUT) != fileno($dad_rdr);
  139.     } else {
  140.         xclose $dad_rdr;
  141.         xopen \*STDOUT, ">&$kid_wtr";
  142.         xclose $kid_wtr;
  143.     }
  144.     if ($dad_rdr ne $dad_err) {
  145.         if ($dup_err) {
  146.         xopen \*STDERR, ">&$dad_err"
  147.             if fileno(STDERR) != fileno($dad_err);
  148.         } else {
  149.         xclose $dad_err;
  150.         xopen \*STDERR, ">&$kid_err";
  151.         xclose $kid_err;
  152.         }
  153.     } else {
  154.         xopen \*STDERR, ">&STDOUT" if fileno(STDERR) != fileno(STDOUT);
  155.     }
  156.     local($")=(" ");
  157.     exec @cmd
  158.         or croak "open3: exec of @cmd failed";
  159.     } elsif ($do_spawn) {
  160.  
  161.     my @close;
  162.     if ($dup_wtr) {
  163.       $kid_rdr = $dad_wtr;
  164.       push @close, \*{$kid_rdr};
  165.     } else {
  166.       push @close, \*{$dad_wtr}, \*{$kid_rdr};
  167.     }
  168.     if ($dup_rdr) {
  169.       $kid_wtr = $dad_rdr;
  170.       push @close, \*{$kid_wtr};
  171.     } else {
  172.       push @close, \*{$dad_rdr}, \*{$kid_wtr};
  173.     }
  174.     if ($dad_rdr ne $dad_err) {
  175.         if ($dup_err) {
  176.           $kid_err = $dad_err ;
  177.           push @close, \*{$kid_err};
  178.         } else {
  179.           push @close, \*{$dad_err}, \*{$kid_err};
  180.         }
  181.     } else {
  182.       $kid_err = $kid_wtr;
  183.     }
  184.     require IO::Pipe;
  185.     $kidpid = eval {
  186.         spawn_with_handles( [ { mode => 'r',
  187.                     open_as => \*{$kid_rdr},
  188.                     handle => \*STDIN },
  189.                   { mode => 'w',
  190.                     open_as => \*{$kid_wtr},
  191.                     handle => \*STDOUT },
  192.                   { mode => 'w',
  193.                     open_as => \*{$kid_err},
  194.                     handle => \*STDERR },
  195.                 ], \@close, @cmd);
  196.     };
  197.     die "open3: $@" if $@;
  198.     }
  199.  
  200.     xclose $kid_rdr if !$dup_wtr;
  201.     xclose $kid_wtr if !$dup_rdr;
  202.     xclose $kid_err if !$dup_err && $dad_rdr ne $dad_err;
  203.     xclose $dad_wtr if $dup_wtr;
  204.  
  205.     select((select($dad_wtr), $| = 1)[0]); # unbuffer pipe
  206.     $kidpid;
  207. }
  208.  
  209. sub open3 {
  210.     if (@_ < 4) {
  211.     local $" = ', ';
  212.     croak "open3(@_): not enough arguments";
  213.     }
  214.     return _open3 'open3', scalar caller, @_
  215. }
  216.  
  217. sub spawn_with_handles {
  218.     my $fds = shift;        # Fields: handle, mode, open_as
  219.     my $close_in_child = shift;
  220.     my ($fd, $pid, @saved_fh, $saved, %saved, @errs);
  221.     require Fcntl;
  222.  
  223.     foreach $fd (@$fds) {
  224.     $fd->{tmp_copy} = IO::Handle->new_from_fd($fd->{handle}, $fd->{mode});
  225.     $saved{fileno $fd->{handle}} = $fd->{tmp_copy};
  226.     }
  227.     foreach $fd (@$fds) {
  228.     bless $fd->{handle}, 'IO::Handle'
  229.         unless eval { $fd->{handle}->isa('IO::Handle') } ;
  230.     $fd->{handle}->fdopen($saved{fileno $fd->{open_as}} || $fd->{open_as},
  231.                   $fd->{mode});
  232.     }
  233.     foreach $fd (@$close_in_child) {
  234.     fcntl($fd, Fcntl::F_SETFD(), 1) or push @errs, "fcntl $fd: $!"
  235.         unless $saved{fileno $fd};    # Do not close what we redirect!
  236.     }
  237.  
  238.     unless (@errs) {
  239.     $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
  240.     push @errs, "IO::Pipe: Can't spawn-NOWAIT: $!" if !$pid || $pid < 0;
  241.     }
  242.  
  243.     foreach $fd (@$fds) {
  244.     $fd->{handle}->fdopen($fd->{tmp_copy}, $fd->{mode});
  245.     $fd->{tmp_copy}->close or croak "Can't close: $!";
  246.     }
  247.     croak join "\n", @errs if @errs;
  248.     return $pid;
  249. }
  250.  
  251. 1; # so require is happy
  252.