perl - Term::ReadKey, non-blocking read in raw mode: Detect EOF? -


when pipe stuff program, not seem character 0x4 indicate eof.

$ echo "abc" | map 'cat' saw a: \x61 saw b: \x62 saw c: \x63 saw : \x0a zzzbc ^c 

i have press ctrl+c quit, i'm not sure ctrl+c acting on. it's having shell send sigint pipeline? don't know how pipelines work on level.

here program map:

#!/usr/bin/env perl  use strict; use warnings;  use io::pty::easy; use term::readkey; use encode;  $#argv % 2 , die "odd number of args required.\n";  if ($#argv == -1) {     warn ("no args provided. command must specified.\n");     exit 1; }  # sure enter command string %mapping = @argv[-@argv..-2];  $interactive = -t stdin;  # %mapping = @argv; # @mapkeys = keys %mapping;  # warn @mapkeys; if ($interactive) {     print "spawning command in pty: @argv\n"     # print "\ncontinue? (y/n)";     # $y_n;     # while (($y_n = <stdin>) !~ /^(y|n)$/) {     #     print '(y/n)';     # }     # exit if $y_n eq "n\n"; }  $pty = io::pty::easy->new(); $spawnret = $pty->spawn("@argv")."\n";  print stderr "spawning has failed: @argv\n" if !$spawnret;  readmode 4; end {     readmode 0; # reset tty mode before exiting }  $i = undef; $j = 0;  {     local $| = 1;     while (1) {         myread();          # responsive key input, , pty output may behind 50ms         $key = readkey(0.05);         # last if !defined($key) || !$key;         if (defined($key)) {             $code = ord($key); # byte is...             if ($interactive , $code == 4) {                 # user types ctrl+d                 print stderr "saw ^d term, embarking on filicide 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 pty             if ($key eq "a") {                 $pty->write("zzz"); # print 'saw "a", wrote "zzz" pty';             } else {                 $pty->write($key); # print "wrote pty: $key";             }         }     } }  sub myread {     # read out pty's activity echo stdout     $from_pty = $pty->read(0);     if (defined($from_pty)) {         if ($from_pty) {             # print "read pty -->$from_pty<--\n";             print $from_pty;         } else {             if ($from_pty eq '') {                 # empty means eof means pty has exited, exit because fate sealed                 print stderr "got pty eof, quitting\n" if $interactive;                 $pty->close();                 last;             }         }     } } 

that explain why produced "zzzbc".

now question how can map able know echo "abc" having reached end of input? cf. echo "abc" | cat completes on own. readkey not seem provide api determining situation.

similarly not sure how same pass along eof child in pty. thinking might cause issues when command write file or something, because eof vs sending kill signal difference between writing file correctly , not exiting cleanly.

try reading stdin , not $pty object. pipe create via shell passes data stdin file descriptor 0, in perl handle .

the $pty, assume that's terminal. that's why script hangs (i guess).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -