File Coverage

blib/lib/IO/Tty.pm
Criterion Covered Total %
statement 41 62 66.1
branch 5 22 22.7
condition 0 3 0.0
subroutine 10 13 76.9
pod 0 5 0.0
total 56 105 53.3


line stmt bran cond sub pod time code
1             # Documentation at the __END__
2             # -*-cperl-*-
3              
4             package IO::Tty;
5              
6 4     4   39 use strict;
  4         8  
  4         153  
7 4     4   20 use warnings;
  4         15  
  4         254  
8 4     4   2718 use IO::Handle;
  4         37167  
  4         262  
9 4     4   2246 use IO::File;
  4         10165  
  4         682  
10 4     4   2782 use IO::Tty::Constant;
  4         16  
  4         247  
11 4     4   26 use Carp;
  4         9  
  4         422  
12              
13             require POSIX;
14             require DynaLoader;
15              
16 4     4   27 use vars qw(@ISA $VERSION $XS_VERSION $CONFIG $DEBUG);
  4         8  
  4         6082  
17              
18             $VERSION = '1.20';
19             $XS_VERSION = "1.20";
20             @ISA = qw(IO::Handle);
21              
22             eval { local $^W = 0; undef local $SIG{__DIE__}; require IO::Stty };
23             push @ISA, "IO::Stty" if ( not $@ ); # if IO::Stty is installed
24              
25             BOOT_XS: {
26             # If I inherit DynaLoader then I inherit AutoLoader and I DON'T WANT TO
27             require DynaLoader;
28              
29             # DynaLoader calls dl_load_flags as a static method.
30             *dl_load_flags = DynaLoader->can('dl_load_flags');
31              
32             do {
33             defined(&bootstrap)
34             ? \&bootstrap
35             : \&DynaLoader::bootstrap;
36             }
37             ->(__PACKAGE__);
38             }
39              
40             sub import {
41 7     7   13897 IO::Tty::Constant->export_to_level( 1, @_ );
42             }
43              
44             sub open {
45 0     0 0 0 my ( $tty, $dev, $mode ) = @_;
46              
47 0 0       0 IO::File::open( $tty, $dev, $mode )
48             or return undef;
49              
50 0         0 $tty->autoflush;
51              
52 0         0 1;
53             }
54              
55             sub clone_winsize_from {
56 0     0 0 0 my ( $self, $fh ) = @_;
57 0 0       0 croak "Given filehandle is not a tty in clone_winsize_from, called"
58             if not POSIX::isatty($fh);
59 0 0       0 return 1 if not POSIX::isatty($self); # ignored for master ptys
60 0         0 my $winsize = " " x 1024; # preallocate memory for older perl versions
61 0         0 $winsize = ''; # But leave the SV as empty
62 0 0 0     0 ioctl( $fh, &IO::Tty::Constant::TIOCGWINSZ, $winsize )
63             and ioctl( $self, &IO::Tty::Constant::TIOCSWINSZ, $winsize )
64             and return 1;
65 0 0       0 warn "clone_winsize_from: error: $!" if $^W;
66 0         0 return undef;
67             }
68              
69             # ioctl() doesn't tell us how long the structure is, so we'll have to trim it
70             # after TIOCGWINSZ
71             my $SIZEOF_WINSIZE = length IO::Tty::pack_winsize( 0, 0, 0, 0 );
72              
73             sub get_winsize {
74 1     1 0 4 my $self = shift;
75 1         3 my $winsize = " " x 1024; # preallocate memory
76 1 50       8 ioctl( $self, IO::Tty::Constant::TIOCGWINSZ(), $winsize )
77             or croak "Cannot TIOCGWINSZ - $!";
78 1         2 substr( $winsize, $SIZEOF_WINSIZE ) = "";
79 1         33 return IO::Tty::unpack_winsize($winsize);
80             }
81              
82             sub set_winsize {
83 0     0 0 0 my $self = shift;
84 0         0 my $winsize = IO::Tty::pack_winsize(@_);
85 0 0       0 ioctl( $self, IO::Tty::Constant::TIOCSWINSZ(), $winsize )
86             or croak "Cannot TIOCSWINSZ - $!";
87             }
88              
89             sub set_raw($) {
90 3     3 0 7172 require POSIX;
91 3         13 my $self = shift;
92 3 50       119 return 1 if not POSIX::isatty($self);
93 3         85 my $ttyno = fileno($self);
94 3         117 my $termios = new POSIX::Termios;
95 3 50       58 unless ($termios) {
96 0         0 warn "set_raw: new POSIX::Termios failed: $!";
97 0         0 return undef;
98             }
99 3 50       88 unless ( $termios->getattr($ttyno) ) {
100 0         0 warn "set_raw: getattr($ttyno) failed: $!";
101 0         0 return undef;
102             }
103 3         42 $termios->setiflag(0);
104 3         22 $termios->setoflag(0);
105 3         16 $termios->setlflag(0);
106 3         26 $termios->setcc( &POSIX::VMIN, 1 );
107 3         32 $termios->setcc( &POSIX::VTIME, 0 );
108 3 50       48 unless ( $termios->setattr( $ttyno, &POSIX::TCSANOW ) ) {
109 0         0 warn "set_raw: setattr($ttyno) failed: $!";
110 0         0 return undef;
111             }
112 3         47 return 1;
113             }
114              
115             1;
116              
117             __END__