blib/lib/IO/Pty.pm

filter: Statementstmt 66.1% 80 / 121 Branchbran 30.4% 14 / 46 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 59.1% 110 / 186 CC 24 SCAR 38.2
CC
24
Cov
56%
make_slave_controlling_terminal
132.0
new
8.4
slave
5.1
CRAP 45.8
↑ Summary blib/lib/IO/Tty.pm »
1 # Documentation at the __END__
2  
3 package IO::Pty;
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use IO::Tty qw(TIOCSCTTY TCSETCTTY TIOCNOTTY);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 require POSIX;
11  
12 10 our @ISA = qw(IO::Handle);
13 10 our $VERSION = '1.29'; # keep same as in Tty.pm
14 10 eval { local $^W = 0; local $SIG{__DIE__}; require IO::Stty };
15 10 ▶push @ISA, "IO::Stty" if ( not $@ ); # if IO::Stty is installed
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 10 10
16  
17 sub new {
18 22 ▶ my ($class) = $_[0] || "IO::Pty";
Subroutine new: called CC=7 SCAR=21.3
Pod documented
Condition50%1/2
$_[0] || 'IO::Pty'
l!l
220
MC/DC0%0/1
$_[0] || 'IO::Pty'
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
19 22 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 22 22
20 22 ▶ @_ <= 1 or croak 'usage: new $class';
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 1 or croak("usage: new \$class") 0 22 22
21  
22 22 my ( $ptyfd, $ttyfd, $ttyname ) = pty_allocate();
23  
24 22 ▶ croak "Cannot open a pty" if not defined $ptyfd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptyfd 0 22 22
25  
26 22 my $pty = $class->SUPER::new_from_fd( $ptyfd, "r+" );
27 22 ▶ if (not $pty) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pty) 0 22 22
28 0 POSIX::close($ptyfd);
29 0 POSIX::close($ttyfd);
30 0 croak "Cannot create a new $class from fd $ptyfd: $!";
31 }
32 22 $pty->autoflush(1);
33 22 bless $pty => $class;
34  
35 22 my $slave = IO::Tty->new_from_fd( $ttyfd, "r+" );
36 22 ▶ if (not $slave) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($slave) 0 22 22
37 0 POSIX::close($ttyfd);
38 0 croak "Cannot create a new IO::Tty from fd $ttyfd: $!";
39 }
40 22 $slave->autoflush(1);
41  
42 22 ${*$pty}{'io_pty_slave'} = $slave;
43 22 ${*$pty}{'io_pty_ttyname'} = $ttyname;
44 22 ${*$slave}{'io_tty_ttyname'} = $ttyname;
45  
46 22 return $pty;
47 }
48  
49 sub ttyname {
50 2 ▶ @_ == 1 or croak 'usage: $pty->ttyname();';
Subroutine ttyname: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$pty->ttyname();") 0 2 2
51 2 my $pty = shift;
52 2 ${*$pty}{'io_pty_ttyname'};
53 }
54  
55 sub close_slave {
56 5 ▶ @_ == 1 or croak 'usage: $pty->close_slave();';
Subroutine close_slave: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$pty->close_slave();") 0 5 5
57  
58 5 my $master = shift;
59  
60 5 ▶ if ( exists ${*$master}{'io_pty_slave'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { *$master }->{"io_pty_slave"}) 5 0 5
61 5 close ${*$master}{'io_pty_slave'};
62 5 delete ${*$master}{'io_pty_slave'};
63 }
64 }
65  
66 sub slave {
67 20 ▶ @_ == 1 or croak 'usage: $pty->slave();';
Subroutine slave: called CC=5 SCAR=16.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$pty->slave();") 0 20 20
68  
69 20 my $master = shift;
70  
71 20 ▶ if ( exists ${*$master}{'io_pty_slave'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { *$master }->{"io_pty_slave"}) 18 2 20
72 18 return ${*$master}{'io_pty_slave'};
73 }
74  
75 2 my $tty = ${*$master}{'io_pty_ttyname'};
76  
77 2 my $slave_fd = IO::Tty::_open_tty($tty);
78 2 ▶ croak "Cannot open slave $tty: $!" if $slave_fd < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $slave_fd < 0 0 2 2
79  
80 2 my $slave = IO::Tty->new_from_fd( $slave_fd, "r+" );
81 2 ▶ if (not $slave) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($slave) 0 2 2
82 0 POSIX::close($slave_fd);
83 0 croak "Cannot create IO::Tty from fd $slave_fd: $!";
84 }
85 2 $slave->autoflush(1);
86  
87 2 ${*$slave}{'io_tty_ttyname'} = $tty;
88 2 ${*$master}{'io_pty_slave'} = $slave;
89  
90 2 return $slave;
91 }
92  
93 sub make_slave_controlling_terminal {
94 0 ▶ @_ == 1 or croak 'usage: $pty->make_slave_controlling_terminal();';
Subroutine make_slave_controlling_terminal: not called CC=11 SCAR=48.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$pty->make_slave_controlling_terminal();") 0 0 0
95  
96 0 my $self = shift;
97 0 local (*DEVTTY);
98  
99 # lose controlling terminal explicitly
100 0 if ( defined TIOCNOTTY ) {
101 0 ▶ if ( open( \*DEVTTY, "/dev/tty" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open \*DEVTTY, "/dev/tty") 0 0 0
102 0 ioctl( \*DEVTTY, TIOCNOTTY, 0 );
103 0 close \*DEVTTY;
104 }
105 }
106  
107 # Create a new 'session', lose controlling terminal.
108 0 ▶ if ( POSIX::setsid() == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (POSIX::setsid() == -1) 0 0 0
109 0 warn "setsid() failed, strange behavior may result: $!\r\n";
110 }
111  
112 0 ▶ if ( open( \*DEVTTY, "/dev/tty" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open \*DEVTTY, "/dev/tty") 0 0 0
113 0 warn "Could not disconnect from controlling terminal?!\n";
114 0 close \*DEVTTY;
115 }
116  
117 # now open slave, this should set it as controlling tty on some systems
118 # Use _open_tty() to ensure STREAMS modules (ptem, ldterm, ttcompat)
119 # are pushed on Solaris/HP-UX, matching the slave() method.
120 0 my $ttyname = ${*$self}{'io_pty_ttyname'};
121 0 my $slave_fd = IO::Tty::_open_tty($ttyname);
122 0 ▶ croak "Cannot open slave $ttyname: $!" if $slave_fd < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $slave_fd < 0 0 0 0
123 0 my $slv = IO::Tty->new_from_fd( $slave_fd, "r+" );
124 0 ▶ croak "Cannot create IO::Tty from fd $slave_fd: $!" if not $slv;
Branch: 1 branch
BranchTrueFalseTotal
unless $slv 0 0 0
125 0 $slv->autoflush(1);
126  
127 0 ▶ if ( not exists ${*$self}{'io_pty_slave'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists do { *$self }->{'io_pty_slave'}) { } 0 0 0
128 0 ${*$self}{'io_pty_slave'} = $slv;
129 }
130 else {
131 0 $slv->close;
132 }
133  
134 # Acquire a controlling terminal if this doesn't happen automatically
135 0 ▶ if ( not open( \*DEVTTY, "/dev/tty" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open \*DEVTTY, "/dev/tty") 0 0 0
136 0 if ( defined TIOCSCTTY ) {
137 0 ▶ if ( not defined ioctl( ${*$self}{'io_pty_slave'}, TIOCSCTTY, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ioctl(do { *$self }->{"io_pty_slave"}, 21518, 0)) 0 0 0
138 0 warn "warning: TIOCSCTTY failed, slave might not be set as controlling terminal: $!";
139 }
140 }
141 elsif ( defined TCSETCTTY ) {
142 if ( not defined ioctl( ${*$self}{'io_pty_slave'}, TCSETCTTY, 0 ) ) {
143 warn "warning: TCSETCTTY failed, slave might not be set as controlling terminal: $!";
144 }
145 }
146 else {
147 warn "warning: You have neither TIOCSCTTY nor TCSETCTTY on your system\n";
148 return 0;
149 }
150 }
151  
152 0 ▶ if ( not open( \*DEVTTY, "/dev/tty" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not open(\*DEVTTY, '/dev/tty')) { } 0 0 0
153 0 warn "Error: could not connect pty as controlling terminal!\n";
154 0 return undef;
155 }
156 else {
157 0 close \*DEVTTY;
158 }
159  
160 0 return 1;
161 }
162  
163 sub DESTROY {
164 22 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
165 # Only delete the internal reference; do not force-close the slave.
166 # Perl's refcounting will close the fd when no references remain.
167 # Force-closing here breaks consumers (e.g. IPC::Run) that hold
168 # their own reference to the slave obtained via $pty->slave().
169 22 delete ${*$self}{'io_pty_slave'};
170 }
171  
172 10 *clone_winsize_from = \&IO::Tty::clone_winsize_from;
173 10 *get_winsize = \&IO::Tty::get_winsize;
174 10 *set_winsize = \&IO::Tty::set_winsize;
175 10 *set_raw = \&IO::Tty::set_raw;
176  
177 10 1;
178  
179 __END__
↑ Summary blib/lib/IO/Tty.pm »