152bd00bfSmillert#!./perl 252bd00bfSmillert 352bd00bfSmillertBEGIN { 452bd00bfSmillert chdir 't' if -d 't'; 5898184e3Ssthen require './test.pl'; 6*9f11ffb7Safresh1 set_up_inc('../lib'); 7898184e3Ssthen skip_all("VMS too picky about line endings for record-oriented pipes") 8898184e3Ssthen if $^O eq 'VMS'; 952bd00bfSmillert} 1052bd00bfSmillert 1152bd00bfSmillertuse strict; 1252bd00bfSmillert 13*9f11ffb7Safresh1++$|; 14*9f11ffb7Safresh1 1552bd00bfSmillertmy $Perl = which_perl(); 1652bd00bfSmillert 1752bd00bfSmillertmy $data = <<'EOD'; 1852bd00bfSmillertx 1952bd00bfSmillert yy 2052bd00bfSmillertz 2152bd00bfSmillertEOD 2252bd00bfSmillert 2352bd00bfSmillert(my $data2 = $data) =~ s/\n/\n\n/g; 2452bd00bfSmillert 2552bd00bfSmillertmy $t1 = { data => $data, write_c => [1,2,length $data], read_c => [1,2,3,length $data]}; 2652bd00bfSmillertmy $t2 = { data => $data2, write_c => [1,2,length $data2], read_c => [1,2,3,length $data2]}; 2752bd00bfSmillert 2852bd00bfSmillert$_->{write_c} = [1..length($_->{data})], 2952bd00bfSmillert $_->{read_c} = [1..length($_->{data})+1, 0xe000] # Need <0xffff for REx 3052bd00bfSmillert for (); # $t1, $t2; 3152bd00bfSmillert 3252bd00bfSmillertmy $c; # len write tests, for each: one _all test, and 3 each len+2 3352bd00bfSmillert$c += @{$_->{write_c}} * (1 + 3*@{$_->{read_c}}) for $t1, $t2; 3452bd00bfSmillert$c *= 3*2*2; # $how_w, file/pipe, 2 reports 3552bd00bfSmillert 3652bd00bfSmillert$c += 6; # Tests with sleep()... 3752bd00bfSmillert 3852bd00bfSmillertprint "1..$c\n"; 3952bd00bfSmillert 40*9f11ffb7Safresh1my $set_out = "binmode STDOUT, ':raw'"; 41*9f11ffb7Safresh1$set_out = "binmode STDOUT, ':raw:crlf'" 4252bd00bfSmillert if defined $main::use_crlf && $main::use_crlf == 1; 4352bd00bfSmillert 4452bd00bfSmillertsub testread ($$$$$$$) { 4552bd00bfSmillert my ($fh, $str, $read_c, $how_r, $write_c, $how_w, $why) = @_; 4652bd00bfSmillert my $buf = ''; 4752bd00bfSmillert if ($how_r eq 'readline_all') { 4852bd00bfSmillert $buf .= $_ while <$fh>; 4952bd00bfSmillert } elsif ($how_r eq 'readline') { 5052bd00bfSmillert $/ = \$read_c; 5152bd00bfSmillert $buf .= $_ while <$fh>; 5252bd00bfSmillert } elsif ($how_r eq 'read') { 5352bd00bfSmillert my($in, $c); 5452bd00bfSmillert $buf .= $in while $c = read($fh, $in, $read_c); 5552bd00bfSmillert } elsif ($how_r eq 'sysread') { 5652bd00bfSmillert my($in, $c); 5752bd00bfSmillert $buf .= $in while $c = sysread($fh, $in, $read_c); 5852bd00bfSmillert } else { 5952bd00bfSmillert die "Unrecognized read: '$how_r'"; 6052bd00bfSmillert } 6152bd00bfSmillert close $fh or die "close: $!"; 6252bd00bfSmillert # The only contamination allowed is with sysread/prints 6352bd00bfSmillert $buf =~ s/\r\n/\n/g if $how_r eq 'sysread' and $how_w =~ /print/; 6452bd00bfSmillert is(length $buf, length $str, "length with wrc=$write_c, rdc=$read_c, $how_w, $how_r, $why"); 6552bd00bfSmillert is($buf, $str, "content with wrc=$write_c, rdc=$read_c, $how_w, $how_r, $why"); 6652bd00bfSmillert} 6752bd00bfSmillert 6852bd00bfSmillertsub testpipe ($$$$$$) { 6952bd00bfSmillert my ($str, $write_c, $read_c, $how_w, $how_r, $why) = @_; 7052bd00bfSmillert (my $quoted = $str) =~ s/\n/\\n/g;; 7152bd00bfSmillert my $fh; 7252bd00bfSmillert if ($how_w eq 'print') { # AUTOFLUSH??? 7352bd00bfSmillert # Should be shell-neutral: 7452bd00bfSmillert open $fh, '-|', qq[$Perl -we "$set_out;print for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!"; 7552bd00bfSmillert } elsif ($how_w eq 'print/flush') { 7652bd00bfSmillert # shell-neutral and miniperl-enabled autoflush? qq(\x24\x7c) eq '$|' 77b8851fccSafresh1 if ($::IS_ASCII) { 7852bd00bfSmillert open $fh, '-|', qq[$Perl -we "$set_out;eval qq(\\x24\\x7c = 1) or die;print for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!"; 79b8851fccSafresh1 } 80b8851fccSafresh1 else { 81b8851fccSafresh1 open $fh, '-|', qq[$Perl -we "$set_out;eval qq(\\x5b\\x4f = 1) or die;print for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!"; 82b8851fccSafresh1 } 8352bd00bfSmillert } elsif ($how_w eq 'syswrite') { 8452bd00bfSmillert ### How to protect \$_ 85b8851fccSafresh1 if ($::IS_ASCII) { 8652bd00bfSmillert open $fh, '-|', qq[$Perl -we "$set_out;eval qq(sub w {syswrite STDOUT, \\x24_} 1) or die; w() for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!"; 87b8851fccSafresh1 } 88b8851fccSafresh1 else { 89b8851fccSafresh1 open $fh, '-|', qq[$Perl -we "$set_out;eval qq(sub w {syswrite STDOUT, \\x5B_} 1) or die; w() for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!"; 90b8851fccSafresh1 } 9152bd00bfSmillert } else { 9252bd00bfSmillert die "Unrecognized write: '$how_w'"; 9352bd00bfSmillert } 94*9f11ffb7Safresh1 binmode $fh; # remove any :utf8 set by PERL_UNICODE 9552bd00bfSmillert binmode $fh, ':crlf' 9652bd00bfSmillert if defined $main::use_crlf && $main::use_crlf == 1; 9752bd00bfSmillert testread($fh, $str, $read_c, $how_r, $write_c, $how_w, "pipe$why"); 9852bd00bfSmillert} 9952bd00bfSmillert 10052bd00bfSmillertsub testfile ($$$$$$) { 10152bd00bfSmillert my ($str, $write_c, $read_c, $how_w, $how_r, $why) = @_; 10252bd00bfSmillert my @data = grep length, split /(.{1,$write_c})/s, $str; 10352bd00bfSmillert 10443003dfeSmillert my $filename = tempfile(); 105b8851fccSafresh1 open my $fh, '>', $filename or die "open: > $filename: $!"; 10652bd00bfSmillert select $fh; 107*9f11ffb7Safresh1 binmode $fh; # remove any :utf8 set by PERL_UNICODE 10852bd00bfSmillert binmode $fh, ':crlf' 10952bd00bfSmillert if defined $main::use_crlf && $main::use_crlf == 1; 11052bd00bfSmillert if ($how_w eq 'print') { # AUTOFLUSH??? 11152bd00bfSmillert $| = 0; 11252bd00bfSmillert print $fh $_ for @data; 11352bd00bfSmillert } elsif ($how_w eq 'print/flush') { 11452bd00bfSmillert $| = 1; 11552bd00bfSmillert print $fh $_ for @data; 11652bd00bfSmillert } elsif ($how_w eq 'syswrite') { 11752bd00bfSmillert syswrite $fh, $_ for @data; 11852bd00bfSmillert } else { 11952bd00bfSmillert die "Unrecognized write: '$how_w'"; 12052bd00bfSmillert } 12152bd00bfSmillert close $fh or die "close: $!"; 122b8851fccSafresh1 open $fh, '<', $filename or die "open: < $filename: $!"; 123*9f11ffb7Safresh1 binmode $fh; 12452bd00bfSmillert binmode $fh, ':crlf' 12552bd00bfSmillert if defined $main::use_crlf && $main::use_crlf == 1; 12652bd00bfSmillert testread($fh, $str, $read_c, $how_r, $write_c, $how_w, "file$why"); 12752bd00bfSmillert} 12852bd00bfSmillert 12952bd00bfSmillert# shell-neutral and miniperl-enabled autoflush? qq(\x24\x7c) eq '$|' 130b8851fccSafresh1my $fh; 131b8851fccSafresh1if ($::IS_ASCII) { 132b8851fccSafresh1 open $fh, '-|', qq[$Perl -we "eval qq(\\x24\\x7c = 1) or die; binmode STDOUT; sleep 1, print for split //, qq(a\nb\n\nc\n\n\n)"] or die "open: $!"; 133b8851fccSafresh1} 134b8851fccSafresh1else { 135b8851fccSafresh1 open $fh, '-|', qq[$Perl -we "eval qq(\\x5B\\x4f = 1) or die; binmode STDOUT; sleep 1, print for split //, qq(a\nb\n\nc\n\n\n)"] or die "open: $!"; 136b8851fccSafresh1} 13752bd00bfSmillertok(1, 'open pipe'); 13852bd00bfSmillertbinmode $fh, q(:crlf); 13952bd00bfSmillertok(1, 'binmode'); 14052bd00bfSmillert$c = undef; 14152bd00bfSmillertmy @c; 14252bd00bfSmillertpush @c, ord $c while $c = getc $fh; 14352bd00bfSmillertok(1, 'got chars'); 14452bd00bfSmillertis(scalar @c, 9, 'got 9 chars'); 145b8851fccSafresh1is("@c", join(" ", utf8::unicode_to_native(97), 146b8851fccSafresh1 utf8::unicode_to_native(10), 147b8851fccSafresh1 utf8::unicode_to_native(98), 148b8851fccSafresh1 utf8::unicode_to_native(10), 149b8851fccSafresh1 utf8::unicode_to_native(10), 150b8851fccSafresh1 utf8::unicode_to_native(99), 151b8851fccSafresh1 utf8::unicode_to_native(10), 152b8851fccSafresh1 utf8::unicode_to_native(10), 153b8851fccSafresh1 utf8::unicode_to_native(10)), 154b8851fccSafresh1 'got expected chars'); 15552bd00bfSmillertok(close($fh), 'close'); 15652bd00bfSmillert 15752bd00bfSmillertfor my $s (1..2) { 15852bd00bfSmillert my $t = ($t1, $t2)[$s-1]; 15952bd00bfSmillert my $str = $t->{data}; 16052bd00bfSmillert my $r = $t->{read_c}; 16152bd00bfSmillert my $w = $t->{write_c}; 16252bd00bfSmillert for my $read_c (@$r) { 16352bd00bfSmillert for my $write_c (@$w) { 16452bd00bfSmillert for my $how_r (qw(readline_all readline read sysread)) { 16552bd00bfSmillert next if $how_r eq 'readline_all' and $read_c != 1; 16652bd00bfSmillert for my $how_w (qw(print print/flush syswrite)) { 16752bd00bfSmillert testfile($str, $write_c, $read_c, $how_w, $how_r, $s); 16852bd00bfSmillert testpipe($str, $write_c, $read_c, $how_w, $how_r, $s); 16952bd00bfSmillert } 17052bd00bfSmillert } 17152bd00bfSmillert } 17252bd00bfSmillert } 17352bd00bfSmillert} 17452bd00bfSmillert 17552bd00bfSmillert1; 176