Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
5.5 KiB

  1. # IO::Pipe.pm
  2. #
  3. # Copyright (c) 1996-8 Graham Barr <[email protected]>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. package IO::Pipe;
  7. require 5.005_64;
  8. use IO::Handle;
  9. use strict;
  10. our($VERSION);
  11. use Carp;
  12. use Symbol;
  13. $VERSION = "1.121";
  14. sub new {
  15. my $type = shift;
  16. my $class = ref($type) || $type || "IO::Pipe";
  17. @_ == 0 || @_ == 2 or croak "usage: new $class [READFH, WRITEFH]";
  18. my $me = bless gensym(), $class;
  19. my($readfh,$writefh) = @_ ? @_ : $me->handles;
  20. pipe($readfh, $writefh)
  21. or return undef;
  22. @{*$me} = ($readfh, $writefh);
  23. $me;
  24. }
  25. sub handles {
  26. @_ == 1 or croak 'usage: $pipe->handles()';
  27. (IO::Pipe::End->new(), IO::Pipe::End->new());
  28. }
  29. my $do_spawn = $^O eq 'os2';
  30. sub _doit {
  31. my $me = shift;
  32. my $rw = shift;
  33. my $pid = $do_spawn ? 0 : fork();
  34. if($pid) { # Parent
  35. return $pid;
  36. }
  37. elsif(defined $pid) { # Child or spawn
  38. my $fh;
  39. my $io = $rw ? \*STDIN : \*STDOUT;
  40. my ($mode, $save) = $rw ? "r" : "w";
  41. if ($do_spawn) {
  42. require Fcntl;
  43. $save = IO::Handle->new_from_fd($io, $mode);
  44. # Close in child:
  45. fcntl(shift, Fcntl::F_SETFD(), 1) or croak "fcntl: $!";
  46. $fh = $rw ? ${*$me}[0] : ${*$me}[1];
  47. } else {
  48. shift;
  49. $fh = $rw ? $me->reader() : $me->writer(); # close the other end
  50. }
  51. bless $io, "IO::Handle";
  52. $io->fdopen($fh, $mode);
  53. $fh->close;
  54. if ($do_spawn) {
  55. $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
  56. my $err = $!;
  57. $io->fdopen($save, $mode);
  58. $save->close or croak "Cannot close $!";
  59. croak "IO::Pipe: Cannot spawn-NOWAIT: $err" if not $pid or $pid < 0;
  60. return $pid;
  61. } else {
  62. exec @_ or
  63. croak "IO::Pipe: Cannot exec: $!";
  64. }
  65. }
  66. else {
  67. croak "IO::Pipe: Cannot fork: $!";
  68. }
  69. # NOT Reached
  70. }
  71. sub reader {
  72. @_ >= 1 or croak 'usage: $pipe->reader( [SUB_COMMAND_ARGS] )';
  73. my $me = shift;
  74. return undef
  75. unless(ref($me) || ref($me = $me->new));
  76. my $fh = ${*$me}[0];
  77. my $pid = $me->_doit(0, $fh, @_)
  78. if(@_);
  79. close ${*$me}[1];
  80. bless $me, ref($fh);
  81. *$me = *$fh; # Alias self to handle
  82. $me->fdopen($fh->fileno,"r")
  83. unless defined($me->fileno);
  84. bless $fh; # Really wan't un-bless here
  85. ${*$me}{'io_pipe_pid'} = $pid
  86. if defined $pid;
  87. $me;
  88. }
  89. sub writer {
  90. @_ >= 1 or croak 'usage: $pipe->writer( [SUB_COMMAND_ARGS] )';
  91. my $me = shift;
  92. return undef
  93. unless(ref($me) || ref($me = $me->new));
  94. my $fh = ${*$me}[1];
  95. my $pid = $me->_doit(1, $fh, @_)
  96. if(@_);
  97. close ${*$me}[0];
  98. bless $me, ref($fh);
  99. *$me = *$fh; # Alias self to handle
  100. $me->fdopen($fh->fileno,"w")
  101. unless defined($me->fileno);
  102. bless $fh; # Really wan't un-bless here
  103. ${*$me}{'io_pipe_pid'} = $pid
  104. if defined $pid;
  105. $me;
  106. }
  107. package IO::Pipe::End;
  108. our(@ISA);
  109. @ISA = qw(IO::Handle);
  110. sub close {
  111. my $fh = shift;
  112. my $r = $fh->SUPER::close(@_);
  113. waitpid(${*$fh}{'io_pipe_pid'},0)
  114. if(defined ${*$fh}{'io_pipe_pid'});
  115. $r;
  116. }
  117. 1;
  118. __END__
  119. =head1 NAME
  120. IO::Pipe - supply object methods for pipes
  121. =head1 SYNOPSIS
  122. use IO::Pipe;
  123. $pipe = new IO::Pipe;
  124. if($pid = fork()) { # Parent
  125. $pipe->reader();
  126. while(<$pipe> {
  127. ....
  128. }
  129. }
  130. elsif(defined $pid) { # Child
  131. $pipe->writer();
  132. print $pipe ....
  133. }
  134. or
  135. $pipe = new IO::Pipe;
  136. $pipe->reader(qw(ls -l));
  137. while(<$pipe>) {
  138. ....
  139. }
  140. =head1 DESCRIPTION
  141. C<IO::Pipe> provides an interface to creating pipes between
  142. processes.
  143. =head1 CONSTRUCTOR
  144. =over 4
  145. =item new ( [READER, WRITER] )
  146. Creates a C<IO::Pipe>, which is a reference to a newly created symbol
  147. (see the C<Symbol> package). C<IO::Pipe::new> optionally takes two
  148. arguments, which should be objects blessed into C<IO::Handle>, or a
  149. subclass thereof. These two objects will be used for the system call
  150. to C<pipe>. If no arguments are given then method C<handles> is called
  151. on the new C<IO::Pipe> object.
  152. These two handles are held in the array part of the GLOB until either
  153. C<reader> or C<writer> is called.
  154. =back
  155. =head1 METHODS
  156. =over 4
  157. =item reader ([ARGS])
  158. The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
  159. handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
  160. is called and C<ARGS> are passed to exec.
  161. =item writer ([ARGS])
  162. The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
  163. handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
  164. is called and C<ARGS> are passed to exec.
  165. =item handles ()
  166. This method is called during construction by C<IO::Pipe::new>
  167. on the newly created C<IO::Pipe> object. It returns an array of two objects
  168. blessed into C<IO::Pipe::End>, or a subclass thereof.
  169. =back
  170. =head1 SEE ALSO
  171. L<IO::Handle>
  172. =head1 AUTHOR
  173. Graham Barr. Currently maintained by the Perl Porters. Please report all
  174. bugs to <[email protected]>.
  175. =head1 COPYRIGHT
  176. Copyright (c) 1996-8 Graham Barr <[email protected]>. All rights reserved.
  177. This program is free software; you can redistribute it and/or
  178. modify it under the same terms as Perl itself.
  179. =cut