Source code of Windows XP (NT5)
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.

239 lines
4.9 KiB

  1. # IO::Pipe.pm
  2. #
  3. # Copyright (c) 1996 Graham Barr <[email protected]>. All rights
  4. # reserved. 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.000;
  8. use IO::Handle;
  9. use strict;
  10. use vars qw($VERSION);
  11. use Carp;
  12. use Symbol;
  13. $VERSION = "1.0901";
  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()';
  73. my $me = shift;
  74. my $fh = ${*$me}[0];
  75. my $pid = $me->_doit(0, $fh, @_)
  76. if(@_);
  77. close ${*$me}[1];
  78. bless $me, ref($fh);
  79. *{*$me} = *{*$fh}; # Alias self to handle
  80. bless $fh; # Really wan't un-bless here
  81. ${*$me}{'io_pipe_pid'} = $pid
  82. if defined $pid;
  83. $me;
  84. }
  85. sub writer {
  86. @_ >= 1 or croak 'usage: $pipe->writer()';
  87. my $me = shift;
  88. my $fh = ${*$me}[1];
  89. my $pid = $me->_doit(1, $fh, @_)
  90. if(@_);
  91. close ${*$me}[0];
  92. bless $me, ref($fh);
  93. *{*$me} = *{*$fh}; # Alias self to handle
  94. bless $fh; # Really wan't un-bless here
  95. ${*$me}{'io_pipe_pid'} = $pid
  96. if defined $pid;
  97. $me;
  98. }
  99. package IO::Pipe::End;
  100. use vars qw(@ISA);
  101. @ISA = qw(IO::Handle);
  102. sub close {
  103. my $fh = shift;
  104. my $r = $fh->SUPER::close(@_);
  105. waitpid(${*$fh}{'io_pipe_pid'},0)
  106. if(defined ${*$fh}{'io_pipe_pid'});
  107. $r;
  108. }
  109. 1;
  110. __END__
  111. =head1 NAME
  112. IO::pipe - supply object methods for pipes
  113. =head1 SYNOPSIS
  114. use IO::Pipe;
  115. $pipe = new IO::Pipe;
  116. if($pid = fork()) { # Parent
  117. $pipe->reader();
  118. while(<$pipe> {
  119. ....
  120. }
  121. }
  122. elsif(defined $pid) { # Child
  123. $pipe->writer();
  124. print $pipe ....
  125. }
  126. or
  127. $pipe = new IO::Pipe;
  128. $pipe->reader(qw(ls -l));
  129. while(<$pipe>) {
  130. ....
  131. }
  132. =head1 DESCRIPTION
  133. C<IO::Pipe> provides an interface to createing pipes between
  134. processes.
  135. =head1 CONSTRCUTOR
  136. =over 4
  137. =item new ( [READER, WRITER] )
  138. Creates a C<IO::Pipe>, which is a reference to a newly created symbol
  139. (see the C<Symbol> package). C<IO::Pipe::new> optionally takes two
  140. arguments, which should be objects blessed into C<IO::Handle>, or a
  141. subclass thereof. These two objects will be used for the system call
  142. to C<pipe>. If no arguments are given then method C<handles> is called
  143. on the new C<IO::Pipe> object.
  144. These two handles are held in the array part of the GLOB until either
  145. C<reader> or C<writer> is called.
  146. =back
  147. =head1 METHODS
  148. =over 4
  149. =item reader ([ARGS])
  150. The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
  151. handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
  152. is called and C<ARGS> are passed to exec.
  153. =item writer ([ARGS])
  154. The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
  155. handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
  156. is called and C<ARGS> are passed to exec.
  157. =item handles ()
  158. This method is called during construction by C<IO::Pipe::new>
  159. on the newly created C<IO::Pipe> object. It returns an array of two objects
  160. blessed into C<IO::Pipe::End>, or a subclass thereof.
  161. =back
  162. =head1 SEE ALSO
  163. L<IO::Handle>
  164. =head1 AUTHOR
  165. Graham Barr <[email protected]>
  166. =head1 COPYRIGHT
  167. Copyright (c) 1996 Graham Barr. All rights reserved. This program is free
  168. software; you can redistribute it and/or modify it under the same terms
  169. as Perl itself.
  170. =cut