| 1 | package IO::Tty::Util ; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use IO::Handle ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use IO::Select ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Carp ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | our $VERSION = '0.03' ; | |||||||||
| 9 | |||||||||||
| 10 | 3 | require Exporter ; | |||||||||
| 11 | 3 | our @ISA = qw(Exporter) ; | |||||||||
| 12 | 3 | our @EXPORT_OK = qw( openpty login_tty forkpty passthru ) ; | |||||||||
| 13 | |||||||||||
| 14 | 3 | require XSLoader ; | |||||||||
| 15 | 3 | XSLoader::load('IO::Tty::Util', $VERSION) ; | |||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 6 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=4 SCAR=20.4
| |||||||||||
| 19 | 6 | my $class = shift ; | |||||||||
| 20 | |||||||||||
| 21 | 6 | foreach my $a (@_){ | |||||||||
| 22 | 9 | ▶ | if ($a eq "passthru"){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | shift ; | |||||||||
| 24 | 0 | my ($pid, $master) = forkpty(@_) ; | |||||||||
| 25 | 0 | ▶ | croak("forkpty error: $!") unless $master ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | passthru($master) ; | |||||||||
| 27 | 0 | exit() ; | |||||||||
| 28 | } | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 6 | $class->export_to_level(1, $class, @_) ; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | 5 | ▶ | sub openpty { | ||||||||
|
Subroutine
openpty: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 36 | 5 | my ($rows, $cols) = @_ ; | |||||||||
| 37 | |||||||||||
| 38 | 5 | my ($master, $slave) = _openpty($rows, $cols) ; | |||||||||
| 39 | 5 | ▶ | return () unless defined($master) ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 5 | return (IO::Handle->new_from_fd($master, "r+"), IO::Handle->new_from_fd($slave, "r+")) ; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | 2 | ▶ | sub login_tty { | ||||||||
|
Subroutine
login_tty: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 46 | 2 | my $h = shift ; | |||||||||
| 47 | |||||||||||
| 48 | 2 | my $rc = _login_tty(fileno($h)) ; | |||||||||
| 49 | 2 | ▶ | return ($rc == -1 ? 0 : 1) ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 2 | ▶ | sub forkpty { | ||||||||
|
Subroutine
forkpty: called CC=7 SCAR=21.1
Pod
documented
| |||||||||||
| 54 | 2 | my ($rows, $cols, @cmd) = @_ ; | |||||||||
| 55 | |||||||||||
| 56 | 2 | my ($master, $slave) = openpty($rows, $cols) ; | |||||||||
| 57 | 2 | ▶ | return () unless defined($master) ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 2 | my $pid = fork() ; | |||||||||
| 60 | 2 | ▶ | return () unless defined($pid) ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | if ($pid){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 1 | close($slave) ; | |||||||||
| 64 | 1 | return ($pid, $master) ; | |||||||||
| 65 | } | ||||||||||
| 66 | else { | ||||||||||
| 67 | 1 | close($master) ; | |||||||||
| 68 | 1 | ▶ | return () unless login_tty($slave) ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | ▶ | return (0) unless scalar(@cmd) ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | ▶ | exec(@cmd) or die("Can't exec '@cmd': $!") ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } | ||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | sub passthru { | ||||||||
|
Subroutine
passthru: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 76 | 0 | my $master = shift ; | |||||||||
| 77 | |||||||||||
| 78 | 0 | STDOUT->autoflush(1) ; | |||||||||
| 79 | 0 | $master->autoflush(1) ; | |||||||||
| 80 | 0 | my $select = new IO::Select($master, \*STDIN) ; | |||||||||
| 81 | 0 | while (1){ | |||||||||
| 82 | 0 | my @ready = $select->can_read() ; | |||||||||
| 83 | 0 | foreach my $h (@ready){ | |||||||||
| 84 | 0 | my $buf = '' ; | |||||||||
| 85 | 0 | my $rc = sysread($h, $buf, 4096) ; | |||||||||
| 86 | 0 | ▶ | return if !$rc ; # pty seems to return error instead of EOF... | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | my $out = ($h eq \*STDIN ? $master : \*STDOUT) ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | # open(DEBUG, ">>/tmp/output") && print DEBUG "[$buf]\n" ; | ||||||||||
| 90 | 0 | ▶ | print $out $buf or croak("print error: $!") ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | 3 | 1 ; | |||||||||
| 99 | __END__ | ||||||||||