PageRenderTime 71ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/chromium/third_party/cygwin/lib/perl5/5.10/i686-cygwin/IO/Pipe.pm

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