Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / lib / 5.8.0 / sun4-solaris / IO / Pipe.pm
CommitLineData
86530b38
AT
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
7package IO::Pipe;
8
9use 5.006_001;
10
11use IO::Handle;
12use strict;
13our($VERSION);
14use Carp;
15use Symbol;
16
17$VERSION = "1.122";
18
19sub new {
20 my $type = shift;
21 my $class = ref($type) || $type || "IO::Pipe";
22 @_ == 0 || @_ == 2 or croak "usage: new $class [READFH, WRITEFH]";
23
24 my $me = bless gensym(), $class;
25
26 my($readfh,$writefh) = @_ ? @_ : $me->handles;
27
28 pipe($readfh, $writefh)
29 or return undef;
30
31 @{*$me} = ($readfh, $writefh);
32
33 $me;
34}
35
36sub handles {
37 @_ == 1 or croak 'usage: $pipe->handles()';
38 (IO::Pipe::End->new(), IO::Pipe::End->new());
39}
40
41my $do_spawn = $^O eq 'os2' || $^O eq 'MSWin32';
42
43sub _doit {
44 my $me = shift;
45 my $rw = shift;
46
47 my $pid = $do_spawn ? 0 : fork();
48
49 if($pid) { # Parent
50 return $pid;
51 }
52 elsif(defined $pid) { # Child or spawn
53 my $fh;
54 my $io = $rw ? \*STDIN : \*STDOUT;
55 my ($mode, $save) = $rw ? "r" : "w";
56 if ($do_spawn) {
57 require Fcntl;
58 $save = IO::Handle->new_from_fd($io, $mode);
59 my $handle = shift;
60 # Close in child:
61 unless ($^O eq 'MSWin32') {
62 fcntl($handle, Fcntl::F_SETFD(), 1) or croak "fcntl: $!";
63 }
64 $fh = $rw ? ${*$me}[0] : ${*$me}[1];
65 } else {
66 shift;
67 $fh = $rw ? $me->reader() : $me->writer(); # close the other end
68 }
69 bless $io, "IO::Handle";
70 $io->fdopen($fh, $mode);
71 $fh->close;
72
73 if ($do_spawn) {
74 $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
75 my $err = $!;
76
77 $io->fdopen($save, $mode);
78 $save->close or croak "Cannot close $!";
79 croak "IO::Pipe: Cannot spawn-NOWAIT: $err" if not $pid or $pid < 0;
80 return $pid;
81 } else {
82 exec @_ or
83 croak "IO::Pipe: Cannot exec: $!";
84 }
85 }
86 else {
87 croak "IO::Pipe: Cannot fork: $!";
88 }
89
90 # NOT Reached
91}
92
93sub reader {
94 @_ >= 1 or croak 'usage: $pipe->reader( [SUB_COMMAND_ARGS] )';
95 my $me = shift;
96
97 return undef
98 unless(ref($me) || ref($me = $me->new));
99
100 my $fh = ${*$me}[0];
101 my $pid = $me->_doit(0, $fh, @_)
102 if(@_);
103
104 close ${*$me}[1];
105 bless $me, ref($fh);
106 *$me = *$fh; # Alias self to handle
107 $me->fdopen($fh->fileno,"r")
108 unless defined($me->fileno);
109 bless $fh; # Really wan't un-bless here
110 ${*$me}{'io_pipe_pid'} = $pid
111 if defined $pid;
112
113 $me;
114}
115
116sub writer {
117 @_ >= 1 or croak 'usage: $pipe->writer( [SUB_COMMAND_ARGS] )';
118 my $me = shift;
119
120 return undef
121 unless(ref($me) || ref($me = $me->new));
122
123 my $fh = ${*$me}[1];
124 my $pid = $me->_doit(1, $fh, @_)
125 if(@_);
126
127 close ${*$me}[0];
128 bless $me, ref($fh);
129 *$me = *$fh; # Alias self to handle
130 $me->fdopen($fh->fileno,"w")
131 unless defined($me->fileno);
132 bless $fh; # Really wan't un-bless here
133 ${*$me}{'io_pipe_pid'} = $pid
134 if defined $pid;
135
136 $me;
137}
138
139package IO::Pipe::End;
140
141our(@ISA);
142
143@ISA = qw(IO::Handle);
144
145sub close {
146 my $fh = shift;
147 my $r = $fh->SUPER::close(@_);
148
149 waitpid(${*$fh}{'io_pipe_pid'},0)
150 if(defined ${*$fh}{'io_pipe_pid'});
151
152 $r;
153}
154
1551;
156
157__END__
158
159=head1 NAME
160
161IO::Pipe - supply object methods for pipes
162
163=head1 SYNOPSIS
164
165 use IO::Pipe;
166
167 $pipe = new IO::Pipe;
168
169 if($pid = fork()) { # Parent
170 $pipe->reader();
171
172 while(<$pipe>) {
173 ...
174 }
175
176 }
177 elsif(defined $pid) { # Child
178 $pipe->writer();
179
180 print $pipe ...
181 }
182
183 or
184
185 $pipe = new IO::Pipe;
186
187 $pipe->reader(qw(ls -l));
188
189 while(<$pipe>) {
190 ...
191 }
192
193=head1 DESCRIPTION
194
195C<IO::Pipe> provides an interface to creating pipes between
196processes.
197
198=head1 CONSTRUCTOR
199
200=over 4
201
202=item new ( [READER, WRITER] )
203
204Creates an C<IO::Pipe>, which is a reference to a newly created symbol
205(see the C<Symbol> package). C<IO::Pipe::new> optionally takes two
206arguments, which should be objects blessed into C<IO::Handle>, or a
207subclass thereof. These two objects will be used for the system call
208to C<pipe>. If no arguments are given then method C<handles> is called
209on the new C<IO::Pipe> object.
210
211These two handles are held in the array part of the GLOB until either
212C<reader> or C<writer> is called.
213
214=back
215
216=head1 METHODS
217
218=over 4
219
220=item reader ([ARGS])
221
222The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
223handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
224is called and C<ARGS> are passed to exec.
225
226=item writer ([ARGS])
227
228The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
229handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
230is called and C<ARGS> are passed to exec.
231
232=item handles ()
233
234This method is called during construction by C<IO::Pipe::new>
235on the newly created C<IO::Pipe> object. It returns an array of two objects
236blessed into C<IO::Pipe::End>, or a subclass thereof.
237
238=back
239
240=head1 SEE ALSO
241
242L<IO::Handle>
243
244=head1 AUTHOR
245
246Graham Barr. Currently maintained by the Perl Porters. Please report all
247bugs to <perl5-porters@perl.org>.
248
249=head1 COPYRIGHT
250
251Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
252This program is free software; you can redistribute it and/or
253modify it under the same terms as Perl itself.
254
255=cut