1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
package Tk::IO;
use strict;
use vars qw($VERSION);
$VERSION = '4.005'; # $Id: //depot/Tkutf8/IO/IO.pm#4 $
require 5.002;
use Tk::Event qw($XS_VERSION);
use Carp;
use base qw(DynaLoader IO::Handle);
bootstrap Tk::IO;
my %fh2obj;
my %obj2fh;
sub new
{
my ($package,%args) = @_;
# Do whatever IO::Handle does
my $fh = $package->SUPER::new;
%{*$fh} = (); # The hash is used for configure options
${*$fh} = ''; # The scalar is used as the 'readable' buffer
@{*$fh} = (); # The array
$fh->configure(%args);
return $fh;
}
sub pending
{
my $fh = shift;
return ${*$fh};
}
sub cget
{
my ($fh,$key) = @_;
return ${*$fh}{$key};
}
sub configure
{
my ($fh,%args) = @_;
my $key;
foreach $key (keys %args)
{
my $val = $args{$key};
$val = Tk::Callback->new($val) if ($key =~ /command$/);
${*$fh}{$key} = $val;
}
}
sub killpg
{
my ($fh,$sig) = @_;
my $pid = $fh->pid;
croak 'No child' unless (defined $pid);
kill($sig,-$pid);
}
sub kill
{
my ($fh,$sig) = @_;
my $pid = $fh->pid;
croak 'No child' unless (defined $pid);
kill($sig,$pid) || croak "Cannot kill($sig,$pid):$!";
}
sub readable
{
my $fh = shift;
my $count = sysread($fh,${*$fh},1,length(${*$fh}));
if ($count < 0)
{
if (exists ${*$fh}{-errorcommand})
{
${*$fh}{-errorcommand}->Call($!);
}
else
{
warn "Cannot read $fh:$!";
$fh->close;
}
}
elsif ($count)
{
if (exists ${*$fh}{-linecommand})
{
my $eol = index(${*$fh},"\n");
if ($eol >= 0)
{
my $line = substr(${*$fh},0,++$eol);
substr(${*$fh},0,$eol) = '';
${*$fh}{-linecommand}->Call($line);
}
}
}
else
{
$fh->close;
}
}
sub pid
{
my $fh = shift;
return ${*$fh}{-pid};
}
sub command
{
my $fh = shift;
my $cmd = ${*$fh}{'-exec'};
return (wantarray) ? @$cmd : $cmd;
}
sub exec
{
my $fh = shift;
my $pid = open($fh,'-|');
if ($pid)
{
${*$fh} = '' unless (defined ${*$fh});
${*$fh}{'-exec'} = [@_];
${*$fh}{'-pid'} = $pid;
if (exists ${*$fh}{-linecommand})
{
my $w = ${*$fh}{-widget};
$w = 'Tk' unless (defined $w);
$w->fileevent($fh,'readable',[$fh,'readable']);
${*$fh}{_readable} = $w;
}
else
{
require Tk::Pretty;
croak Tk::Pretty::Pretty(\%{*$fh});
}
return $pid;
}
else
{
# make STDERR same as STDOUT here
setpgrp;
exec(@_) || die 'Cannot exec ',join(' ',@_),":$!";
}
}
sub wait
{
my $fh = shift;
my $code;
my $ch = delete ${*$fh}{-childcommand};
${*$fh}{-childcommand} = Tk::Callback->new(sub { $code = shift });
Tk::Event::DoOneEvent(0) until (defined $code);
if (defined $ch)
{
${*$fh}{-childcommand} = $ch;
$ch->Call($code,$fh)
}
return $code;
}
sub close
{
my $fh = shift;
my $code;
if (defined fileno($fh))
{
my $w = delete ${*$fh}{_readable};
$w->fileevent($fh,'readable','') if (defined $w);
$code = close($fh);
if (exists ${*$fh}{-childcommand})
{
${*$fh}{-childcommand}->Call($?,$fh);
}
}
return $code;
}
1;
__END__
|