Spawning a console in Windows (similar to forkpty on linux)

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 9 11:19:15 PDT 2015


On Saturday, 9 May 2015 at 13:01:27 UTC, wobbles wrote:
> On Saturday, 9 May 2015 at 13:00:01 UTC, wobbles wrote:
>> On Saturday, 9 May 2015 at 12:48:16 UTC, Kagamin wrote:
>>> On Saturday, 9 May 2015 at 12:26:58 UTC, wobbles wrote:
>>>> What I mean is, if the cmd.exe hasnt flushed it's output, my 
>>>> cmdPid.stdout.readln (or whatever) will block until it does. 
>>>> I dont really want this.
>>>
>>> Are you sure cmd is the culprit? It should have sensible 
>>> buffering. Also do you want just a console window or also a 
>>> command interpreter attached to it?
>>
>> My windows knowledge isnt marvelous, but I believe I'll need 
>> the interpreter attached.
>>
>> Just as an example of running cmd through std.process, running 
>> this on my system:
>> 	auto pipes = pipeShell("cmd.exe");
>> 	write(pipes.stdout.readln);
>> 	write(pipes.stdout.readln);
>> 	write(pipes.stdout.readln);
>>        return;
>> will print
>> `
>> Microsoft Windows [Version 6.3.9600]
>> (c) 2013 Microsoft Corporation. All rights reserved.
>>
>> `
>> and then exits.
>>
>> However, adding another "write" line before the return; will 
>> cause the program to hang there, waiting for the cmd.exe 
>> process to flush it's next line.
>>
>> On Linux, I'm able to edit a file descriptor after I've 
>> created it to tell it to read/write asynchronously, I cant 
>> seem to find anything similar on windows however.
>
> Spoke too soon. Looks like this is what I need:
> http://www.codeproject.com/Articles/534/An-Introduction-to-Processes-Asynchronous-Process

You need a loop that run until the PID is invalid. something like:

---
while(true)
{
     Thread.sleep(dur!"msecs"(10));

     // operation on i/o streams
     write(pipes.stdout.readln);

     if (pipes.pid.tryWait.terminated)
         break;
}
---

also note that the piped process needs its whole output to be 
read before it terminates, otherwise it can stick (never ends).


More information about the Digitalmars-d-learn mailing list