all close() replaced with _close(); cleaned up debugging cruft
David Golden
15 years ago
47 | 47 | return *{$glob}{NAME}; |
48 | 48 | } |
49 | 49 | |
50 | ||
51 | 50 | sub _open { |
52 | # warn "# @_ : ";# . (defined fileno($_[0]) ? fileno($_[0]) : 'undef') . " , $_[1]\n" ; | |
53 | 51 | open $_[0], $_[1] or die "Error from open(" . join(q{, }, @_) . "): $!"; |
54 | 52 | _debug( "# open " . join( ", " , map { defined $_ ? _name($_) : 'undef' } @_ ) . " as " . fileno( $_[0] ) . "\n" ); |
55 | 53 | } |
56 | 54 | |
57 | 55 | sub _close { |
58 | # warn "# @_ : ";# . (defined fileno($_[0]) ? fileno($_[0]) : 'undef') . " , $_[1]\n" ; | |
59 | 56 | close $_[0] or die "Error from close(" . join(q{, }, @_) . "): $!"; |
60 | 57 | _debug( "# closed " . ( defined $_[0] ? _name($_[0]) : 'undef' ) . "\n" ); |
61 | 58 | } |
63 | 60 | sub _proxy_std { |
64 | 61 | my %proxies; |
65 | 62 | if ( ! defined fileno STDIN ) { |
66 | _debug( "# proxying STDIN ...\n" ); | |
67 | 63 | _open \*STDIN, "<" . File::Spec->devnull; |
68 | _debug( "# proxyed STDIN as " . (defined fileno STDIN ? fileno STDIN : 'undef' ) . "\n" ); | |
64 | _debug( "# proxied STDIN as " . (defined fileno STDIN ? fileno STDIN : 'undef' ) . "\n" ); | |
69 | 65 | $proxies{stdin} = \*STDIN; |
70 | 66 | } |
71 | 67 | if ( ! defined fileno STDOUT ) { |
72 | 68 | _open \*STDOUT, ">" . File::Spec->devnull; |
69 | _debug( "# proxied STDOUT as " . (defined fileno STDOUT ? fileno STDOUT : 'undef' ) . "\n" ); | |
73 | 70 | $proxies{stdout} = \*STDOUT; |
74 | 71 | } |
75 | 72 | if ( ! defined fileno STDERR ) { |
76 | 73 | _open \*STDERR, ">" . File::Spec->devnull; |
74 | _debug( "# proxied STDERR as " . (defined fileno STDERR ? fileno STDERR : 'undef' ) . "\n" ); | |
77 | 75 | $proxies{stderr} = \*STDERR; |
78 | 76 | } |
79 | 77 | return %proxies; |
94 | 92 | _open \*STDIN, "<&" . fileno $handles->{stdin}; |
95 | 93 | } |
96 | 94 | else { |
97 | close STDIN if defined fileno STDIN; | |
95 | _close \*STDIN if defined fileno STDIN; | |
98 | 96 | } |
99 | 97 | if ( defined fileno $handles->{stdout} ) { |
100 | 98 | _open \*STDOUT, ">&" . fileno $handles->{stdout}; |
101 | 99 | } |
102 | 100 | else { |
103 | close STDOUT if defined fileno STDOUT; | |
101 | _close \*STDOUT if defined fileno STDOUT; | |
104 | 102 | } |
105 | 103 | if ( defined fileno $handles->{stderr} ) { |
106 | 104 | _open \*STDERR, ">&" . fileno $handles->{stderr}; |
107 | 105 | } |
108 | 106 | else { |
109 | close STDERR if defined fileno STDERR; | |
107 | _close \*STDERR if defined fileno STDERR; | |
110 | 108 | } |
111 | 109 | } |
112 | 110 | |
148 | 146 | } |
149 | 147 | elsif ($pid == 0) { # child |
150 | 148 | untie *STDIN; untie *STDOUT; untie *STDERR; |
151 | close $stash->{tee}{$which}; | |
149 | _close $stash->{tee}{$which}; | |
152 | 150 | _debug( "# redirecting in child ...\n" ); |
153 | 151 | _open_std( $stash->{child}{$which} ); |
154 | 152 | exec @cmd, $stash->{flag_files}{$which}; |
166 | 164 | |
167 | 165 | sub _kill_tees { |
168 | 166 | my ($stash) = @_; |
169 | close $_ for values %{ $stash->{tee} }; | |
167 | _close $_ for values %{ $stash->{tee} }; | |
170 | 168 | if ( $use_system ) { |
171 | 169 | eval { Win32::Sleep(25) }; # 25 ms pause for output to get flushed, I hope |
172 | 170 | kill 1, $_ for values %{ $stash->{pid} }; # shut them down hard |
191 | 189 | my %proxy_std = _proxy_std(); |
192 | 190 | my $stash = { old => _copy_std() }; |
193 | 191 | $stash->{new}{$_} = $stash->{capture}{$_} = tempfile() for qw/stdout stderr/; |
192 | _debug("# will capture $_ on " .fileno($stash->{capture}{$_})."\n") for qw/stdout stderr/; | |
194 | 193 | # tees may change $stash->{new} |
195 | 194 | _start_tee( stdout => $stash ) if $tee_stdout; |
196 | 195 | _start_tee( stderr => $stash ) if $tee_stderr; |