Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

expect - What is the equivalent of unbuffer program on Windows?

Hi according to this post, unbuffer connects to a command via a pseudo-terminal (pty), which makes the system treat it as an interactive process, therefore not using any stdout buffering.

I would like to use this function on Windows. May I know what is the equivalent of unbuffer program on Windows? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I spent some time on this and succeeded. I found this blog during research, and decided to return and provide my solution to save the next guy some time. I'm responding as a guest with a false email so I won't be interacting, but no further information should be required.

On Jul 18 '12 at 19:41 Harry Johnston wrote:

"In principle, if you know how much data to expect, you could use the console API functions to create a console for the application to write to, and then read the output from the console. But you can't do that from Java, you would need to write a C application to do it for you."

Thing is, there is already a utility that does this. It's written for a slightly different use, but it can be coxed into providing the desired result. Its intended purpose is to enable a windows console app to interact with a Linux style tty terminal. It does this by running a hidden console and accesses the console buffer directly. If you tried to use it – you'd fail. I got lucky and discovered that there are undocumented switches for this utility which will allow it to provide simple unbuffered output. Without the switches it fails with the error – the output is not a tty – when trying to pipe output.

The utility is called winpty. You can get it here:

https://github.com/rprichard/winpty/releases

The undocumented switches are mentioned here:

https://github.com/rprichard/winpty/issues/103

I’m using the MSYS2 version. You’ll need the msys-2.0.dll to use it.

Simply run:

winpty.exe -Xallow-non-tty -Xplain your_program.exe | receive_unbuffered_output.exe

-Xallow-non-tty , will allow piped output

-Xplain , will remove the added Linux terminal escape codes (or whatever they’re called)

Required files are:

winpty.exe
winpty-agent.exe
winpty.dll
msys-2.0.dll

winpty-debugserver.exe – Not needed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...