Edit report at https://bugs.php.net/bug.php?id=34972&edit=1

 ID:                 34972
 Comment by:         hans+php at hansdude dot com
 Reported by:        VJTD3 at VJTD3 dot com
 Summary:            STDIN won't allow nonblocking.
 Status:             Assigned
 Type:               Bug
 Package:            Streams related
 Operating System:   win32 only
 PHP Version:        5.2CVS-2008-07-15
 Assigned To:        pajoye
 Block user comment: N
 Private report:     N

 New Comment:

I have a use case affected by this bug that others do not mention: parallel 
processing.

There is no way to spawn a bunch of processes and babysit them until they're 
done while still capturing output.

While the previous comment makes valid points, I hope someone will still try to 
find a solution to this.


Previous Comments:
------------------------------------------------------------------------
[2011-05-14 02:06:50] dewi at dewimorgan dot com

As I understand it (and some or all of this post may be wrong: please 
correct!), this is a platform limitation, since stuff like Windows' 
SetCommTimeouts() doesn't work for the stdin handle. I believe that this is 
because of the way pipes are handled under Windows, which makes them line-based 
rather than character-based. 

Any true "fix" (rather than just editing the docs) is a Hard Problem, since PHP 
has no control over Windows pipes.

The windows PHP port could internally do something nasty like:
if (ftell(STDIN) === 0) {
  // STDIN is the real STDIN, from a file or piped command.
}
else {
  // STDIN is a faked STDIN, built from keypresses.
}

This would fix the issue for those wanting interactive input, but the issue 
would still show up for pipes that aren't line separated:
ReplayKeyboardInput.exe | myScript.php

And even ignoring pipes, faking STDIN isn't trivial, either, but would probably 
involve a call to PeekConsoleInput().

I'm pretty sure if someone has some magic way for the receiving end of a pipe 
to make the pipe behave in a character-based way under Windows, then they'd 
happily implement it.

I'm considerably less convinced that they'd be willing to muddy PHP with a 
STDIN kludge that wouldn't work in some cases.

The NEED most people are expressing here is the need for interactive console 
input, via nonblocking keyboard reads, rather than the need for character-based 
pipes.

So, a "more correct" option would likely to be getting the ncurses extension 
working under Windows - the one platform that really needs it, and doesn't have 
it :) The public domain pdcurses.sourceforge.net may be useful here.

Workarounds:

As a workaround for PHP programmers on windows in the meantime, an external 
program that reads a single keypress can be used.

For people who don't mind blocking, this should work:
$KeyPressed = `perl -e "use Term::ReadKey;ReadMode 'cbreak'; print(ReadKey(0)); 
ReadMode 'normal';"`

To get nonblocking behaviour, where you can just read pending keystrokes, you'd 
need to launch that in a separate process, give it focus, and get it to send 
the characters to PHP whenever it read a character, and get your php file to 
poll the same file/socket for changes. 

If you don't have perl installed, a similar external program could likely be 
written in PowerShell, running as root and using $host.ui.rawui.KeyAvailable a 
WMI listener - but that's a crazy hoop to have to leap through.

Unless you have a really pressing need to write your app in PHP, then, odds 
are, it's easier to just write your script in a language which has functions 
for reading the keyboard directly (Perl, PowerShell, Python, AutoHotKey...). Or 
write a php readkey extension that launches a keyboard monitor in a separate 
thread or something, with start, stop, and flush functions for that key buffer.

------------------------------------------------------------------------
[2011-01-01 10:48:41] paj...@php.net

@support at aculane dot com

what makes you think that anything has been done about this feature request?

------------------------------------------------------------------------
[2011-01-01 10:33:12] support at aculane dot com

PHP 5.3 (5.3.4) VC9 x86 Thread Safe (2010-Dec-09 23:14:27) windows 7(64bit os)

Exact same problem.

------------------------------------------------------------------------
[2010-12-24 10:14:21] lgynove at 163 dot com

5 years after the bug should not be there.

------------------------------------------------------------------------
[2010-10-02 17:25:14] axellc dot perso at gmail dot com

Have same problem with 5.3.3 on windows (7) 
I still can't use my php5-cli shell applications on Windows :s

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=34972


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=34972&edit=1

Reply via email to