Skip to content

Commit

Permalink
perltidy
Browse files Browse the repository at this point in the history
  • Loading branch information
toddr committed Jan 22, 2021
1 parent 42ef521 commit 4ff0896
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 173 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.16 2021-01-2 Todd Rinaldo <[email protected]>
* Switch to github for issue tracker.
* Switch to testsuite CI workflow.
* Tidy

2020-10-03 Todd Rinaldo <[email protected]> 1.15
* Skip winsize test on Solaris and QNX NTO
Expand Down
195 changes: 99 additions & 96 deletions Pty.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,140 +10,143 @@ require POSIX;

use vars qw(@ISA $VERSION);

$VERSION = '1.15'; # keep same as in Tty.pm
$VERSION = '1.15'; # keep same as in Tty.pm

@ISA = qw(IO::Handle);
eval { local $^W = 0; undef local $SIG{__DIE__}; require IO::Stty };
push @ISA, "IO::Stty" if (not $@); # if IO::Stty is installed
push @ISA, "IO::Stty" if ( not $@ ); # if IO::Stty is installed

sub new {
my ($class) = $_[0] || "IO::Pty";
$class = ref($class) if ref($class);
@_ <= 1 or croak 'usage: new $class';
my ($class) = $_[0] || "IO::Pty";
$class = ref($class) if ref($class);
@_ <= 1 or croak 'usage: new $class';

my ($ptyfd, $ttyfd, $ttyname) = pty_allocate();
my ( $ptyfd, $ttyfd, $ttyname ) = pty_allocate();

croak "Cannot open a pty" if not defined $ptyfd;
croak "Cannot open a pty" if not defined $ptyfd;

my $pty = $class->SUPER::new_from_fd($ptyfd, "r+");
croak "Cannot create a new $class from fd $ptyfd: $!" if not $pty;
$pty->autoflush(1);
bless $pty => $class;
my $pty = $class->SUPER::new_from_fd( $ptyfd, "r+" );
croak "Cannot create a new $class from fd $ptyfd: $!" if not $pty;
$pty->autoflush(1);
bless $pty => $class;

my $slave = IO::Tty->new_from_fd($ttyfd, "r+");
croak "Cannot create a new IO::Tty from fd $ttyfd: $!" if not $slave;
$slave->autoflush(1);
my $slave = IO::Tty->new_from_fd( $ttyfd, "r+" );
croak "Cannot create a new IO::Tty from fd $ttyfd: $!" if not $slave;
$slave->autoflush(1);

${*$pty}{'io_pty_slave'} = $slave;
${*$pty}{'io_pty_ttyname'} = $ttyname;
${*$slave}{'io_tty_ttyname'} = $ttyname;
${*$pty}{'io_pty_slave'} = $slave;
${*$pty}{'io_pty_ttyname'} = $ttyname;
${*$slave}{'io_tty_ttyname'} = $ttyname;

return $pty;
return $pty;
}

sub ttyname {
@_ == 1 or croak 'usage: $pty->ttyname();';
my $pty = shift;
${*$pty}{'io_pty_ttyname'};
@_ == 1 or croak 'usage: $pty->ttyname();';
my $pty = shift;
${*$pty}{'io_pty_ttyname'};
}


sub close_slave {
@_ == 1 or croak 'usage: $pty->close_slave();';
@_ == 1 or croak 'usage: $pty->close_slave();';

my $master = shift;
my $master = shift;

if (exists ${*$master}{'io_pty_slave'}) {
close ${*$master}{'io_pty_slave'};
delete ${*$master}{'io_pty_slave'};
}
if ( exists ${*$master}{'io_pty_slave'} ) {
close ${*$master}{'io_pty_slave'};
delete ${*$master}{'io_pty_slave'};
}
}

sub slave {
@_ == 1 or croak 'usage: $pty->slave();';
@_ == 1 or croak 'usage: $pty->slave();';

my $master = shift;
my $master = shift;

if (exists ${*$master}{'io_pty_slave'}) {
return ${*$master}{'io_pty_slave'};
}
if ( exists ${*$master}{'io_pty_slave'} ) {
return ${*$master}{'io_pty_slave'};
}

my $tty = ${*$master}{'io_pty_ttyname'};
my $tty = ${*$master}{'io_pty_ttyname'};

my $slave = new IO::Tty;
my $slave = new IO::Tty;

$slave->open($tty, O_RDWR | O_NOCTTY) ||
croak "Cannot open slave $tty: $!";
$slave->open( $tty, O_RDWR | O_NOCTTY )
|| croak "Cannot open slave $tty: $!";

return $slave;
return $slave;
}

sub make_slave_controlling_terminal {
@_ == 1 or croak 'usage: $pty->make_slave_controlling_terminal();';
@_ == 1 or croak 'usage: $pty->make_slave_controlling_terminal();';

my $self = shift;
local (*DEVTTY);

# loose controlling terminal explicitly
if ( defined TIOCNOTTY ) {
if ( open( \*DEVTTY, "/dev/tty" ) ) {
ioctl( \*DEVTTY, TIOCNOTTY, 0 );
close \*DEVTTY;
}
}

# Create a new 'session', lose controlling terminal.
if ( POSIX::setsid() == -1 ) {
warn "setsid() failed, strange behavior may result: $!\r\n" if $^W;
}

my $self = shift;
local(*DEVTTY);
if ( open( \*DEVTTY, "/dev/tty" ) ) {
warn "Could not disconnect from controlling terminal?!\n" if $^W;
close \*DEVTTY;
}

# now open slave, this should set it as controlling tty on some systems
my $ttyname = ${*$self}{'io_pty_ttyname'};
my $slv = new IO::Tty;
$slv->open( $ttyname, O_RDWR )
or croak "Cannot open slave $ttyname: $!";

if ( not exists ${*$self}{'io_pty_slave'} ) {
${*$self}{'io_pty_slave'} = $slv;
}
else {
$slv->close;
}

# Acquire a controlling terminal if this doesn't happen automatically
if ( not open( \*DEVTTY, "/dev/tty" ) ) {
if ( defined TIOCSCTTY ) {
if ( not defined ioctl( ${*$self}{'io_pty_slave'}, TIOCSCTTY, 0 ) ) {
warn "warning: TIOCSCTTY failed, slave might not be set as controlling terminal: $!" if $^W;
}
}
elsif ( defined TCSETCTTY ) {
if ( not defined ioctl( ${*$self}{'io_pty_slave'}, TCSETCTTY, 0 ) ) {
warn "warning: TCSETCTTY failed, slave might not be set as controlling terminal: $!" if $^W;
}
}
else {
warn "warning: You have neither TIOCSCTTY nor TCSETCTTY on your system\n" if $^W;
return 0;
}
}

# loose controlling terminal explicitly
if (defined TIOCNOTTY) {
if (open (\*DEVTTY, "/dev/tty")) {
ioctl( \*DEVTTY, TIOCNOTTY, 0 );
close \*DEVTTY;
if ( not open( \*DEVTTY, "/dev/tty" ) ) {
warn "Error: could not connect pty as controlling terminal!\n";
return undef;
}
}

# Create a new 'session', lose controlling terminal.
if (POSIX::setsid() == -1) {
warn "setsid() failed, strange behavior may result: $!\r\n" if $^W;
}

if (open(\*DEVTTY, "/dev/tty")) {
warn "Could not disconnect from controlling terminal?!\n" if $^W;
close \*DEVTTY;
}

# now open slave, this should set it as controlling tty on some systems
my $ttyname = ${*$self}{'io_pty_ttyname'};
my $slv = new IO::Tty;
$slv->open($ttyname, O_RDWR)
or croak "Cannot open slave $ttyname: $!";

if (not exists ${*$self}{'io_pty_slave'}) {
${*$self}{'io_pty_slave'} = $slv;
} else {
$slv->close;
}

# Acquire a controlling terminal if this doesn't happen automatically
if (not open(\*DEVTTY, "/dev/tty")) {
if (defined TIOCSCTTY) {
if (not defined ioctl( ${*$self}{'io_pty_slave'}, TIOCSCTTY, 0 )) {
warn "warning: TIOCSCTTY failed, slave might not be set as controlling terminal: $!" if $^W;
}
} elsif (defined TCSETCTTY) {
if (not defined ioctl( ${*$self}{'io_pty_slave'}, TCSETCTTY, 0 )) {
warn "warning: TCSETCTTY failed, slave might not be set as controlling terminal: $!" if $^W;
}
} else {
warn "warning: You have neither TIOCSCTTY nor TCSETCTTY on your system\n" if $^W;
return 0;
else {
close \*DEVTTY;
}
}

if (not open(\*DEVTTY, "/dev/tty")) {
warn "Error: could not connect pty as controlling terminal!\n";
return undef;
} else {
close \*DEVTTY;
}

return 1;

return 1;
}

*clone_winsize_from = \&IO::Tty::clone_winsize_from;
*get_winsize = \&IO::Tty::get_winsize;
*set_winsize = \&IO::Tty::set_winsize;
*set_raw = \&IO::Tty::set_raw;
*get_winsize = \&IO::Tty::get_winsize;
*set_winsize = \&IO::Tty::set_winsize;
*set_raw = \&IO::Tty::set_raw;

1;

Expand Down
Loading

0 comments on commit 4ff0896

Please sign in to comment.