1
當我管的東西到我的程序,它似乎並沒有得到任何角色像爲0x4,指示EOF。我不得不按Ctrl + C退出,但我不確定Ctrl + C的作用是什麼。它可能讓shell向管道發送一個SIGINT信號?我不知道管道是如何在這個層面上工作的。期限:: ReadKey,無阻塞讀取原始模式:檢測EOF?
這裏是我的程序map
:
#!/usr/bin/env perl
use strict;
use warnings;
use IO::Pty::Easy;
use Term::ReadKey;
use Encode;
$#ARGV % 2 and die "Odd number of args required.\n";
if ($#ARGV == -1) {
warn ("No args provided. A command must be specified.\n");
exit 1;
}
# be sure to enter the command as a string
my %mapping = @ARGV[[email protected]];
my $interactive = -t STDIN;
# my %mapping = @ARGV;
# my @mapkeys = keys %mapping;
# warn @mapkeys;
if ($interactive) {
print "Spawning command in pty: @ARGV\n"
# print "\nContinue? (y/n)";
# my $y_n;
# while (($y_n = <STDIN>) !~ /^(y|n)$/) {
# print '(y/n)';
# }
# exit if $y_n eq "n\n";
}
my $pty = IO::Pty::Easy->new();
my $spawnret = $pty->spawn("@ARGV")."\n";
print STDERR "Spawning has failed: @ARGV\n" if !$spawnret;
ReadMode 4;
END {
ReadMode 0; # Reset tty mode before exiting
}
my $i = undef;
my $j = 0;
{
local $| = 1;
while (1) {
myread();
# responsive to key input, and pty output may be behind by 50ms
my $key = ReadKey(0.05);
# last if !defined($key) || !$key;
if (defined($key)) {
my $code = ord($key); # this byte is...
if ($interactive and $code == 4) {
# User types Ctrl+D
print STDERR "Saw ^D from term, embarking on filicide with TERM signal\n";
$pty->kill("TERM", 0); # blocks till death of child
myread();
$pty->close();
last;
}
printf("saw %s: \\x%02X\n", $key, $code);
# echo translated input to pty
if ($key eq "a") {
$pty->write("zzz"); # print 'Saw "a", wrote "zzz" to pty';
} else {
$pty->write($key); # print "Wrote to pty: $key";
}
}
}
}
sub myread {
# read out pty's activity to echo to stdout
my $from_pty = $pty->read(0);
if (defined($from_pty)) {
if ($from_pty) {
# print "read from pty -->$from_pty<--\n";
print $from_pty;
} else {
if ($from_pty eq '') {
# empty means EOF means pty has exited, so I exit because my fate is sealed
print STDERR "Got back from pty EOF, quitting\n" if $interactive;
$pty->close();
last;
}
}
}
}
這可以解釋爲什麼它生產的 「zzzbc」。
現在的問題是怎樣才能得到map
能夠了解echo "abc"
已經到達輸入的結尾?比照echo "abc" | cat
自己完成。 ReadKey似乎沒有提供確定這種情況的API。
同樣,我不知道如何做同樣沿EOF傳遞給孩子在PTY。我想這可能會導致問題當命令是寫入文件或東西,因爲EOF VS發送一個kill信號是正確地寫入文件,而不是乾淨退出之間的差異。