in reply to Re^3: Testing for a background process waiting for input (use a thread)
in thread Testing for a background process waiting for input
a "unixish" implementation of BrowserUk's idea
Nice!++
the backspace cancellation trick would only work under rare circumstances (AFAICT) — simply reading from stdin would, for example, not treat the backspaces in any special way.
I added a little extra diagnostics. a) When the parent detects that the child entered an input state, it sends it a string "hello\n"; b) when the child enters an input state, it prints out what it receives:
#! perl -slw use strict; use threads; use threads::shared; our $N //= 12; our $I //= 0; my $cmd = qq[$^X -E"\$|++; sleep $N; print 'Kid got ', scalar <STDIN> +if $I; sleep 2;say 'Kid done'"]; my $timeout = time() + 10; my $inInputState :shared = 0; my $pid = open CMD, '|-', $cmd or die $!; my $old = select CMD; $|++; select $old; my $timedOut = 0; async { $inInputState = 1 if printf CMD " \b"x2048; }->detach; Win32::Sleep 10 until !kill 0, $pid or $timedOut = time() > $timeout or $inInputState ; if( $timedOut ) { print "Command timed out"; kill 3, $pid; } if( $inInputState ) { print "Child waiting for input"; print CMD 'hello'; } else { print "Kid never entered input state"; } print 'Parent done';
The upshot shows that on Windows at least, even though the child (perl in this case) only uses a standard read from stdin scalar <STDIN>, the CRT provides line-editing that allows the backspace cancellation to work:
C:\test>detectChildInputState -I=0 -N=2 Kid done Kid never entered input state Parent done C:\test>detectChildInputState -I=1 -N=2 Child waiting for input Parent done Kid got hello Kid done C:\test>detectChildInputState -I=0 -N=10 Command timed out Kid never entered input state Parent done C:\test>detectChildInputState -I=1 -N=10 Child waiting for input Parent done Kid got hello Kid done C:\test>detectChildInputState -I=0 -N=12 Command timed out Kid never entered input state Parent done C:\test>detectChildInputState -I=1 -N=12 Command timed out Kid never entered input state Parent done
But I guess what you are saying is that under *nix, the standard CRT input routines don't provided line editing facilities, unless the program in question uses a readline(3) library or similar.
Would nulls be an workable alternative? Or DC1/DC3 (XON/XOFF)?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Testing for a background process waiting for input (use a thread)
by Eliya (Vicar) on Apr 15, 2012 at 04:22 UTC |