Running external program from a D program [D2]

Jonathan M Davis jmdavisProg at gmail.com
Sun Feb 21 21:00:56 PST 2010


Jonathan M Davis wrote:

> Okay, I'm looking to be able to run an external program from within my D
> program. The library functions for this appear to be in std.process. You
> have system() and various versions of execv(). system() makes a call in
> shell. execv() executes the program without a shell.
> 
> What I'd _like_ to be able to do is run the program without a shell so
> that I don't have to worry about escaping special characters in file
> names, and I want to have access to the output from the program. This
> poses two problems.
> 
> 1. Is there a way to run a program without the shell and without the call
> terminating the program (per bugzilla 3158, execv takes over your program
> and terminates it when it's done, in spite of what the documentation says
> - it's certainly been killing my programs when I've tried)? The only way
> that I see to do that at the moment is to spawn a separate thread and run
> execv in it. I'd prefer to just make the call and wait for it to return.
> Is there a way to do so?

Well, upon actually trying execv() within a separate thread, it _still_ 
kills the program, so that doesn't seem to fly for some reason, which means 
that I don't even have a poor solution for successively calling a program 
from within D without using the shell. At the moment, it looks like phobos 
could really use such a function, but I don't know enough about the 
underlying system calls available to know how easy that is if the system's 
execv() always terminates the program.

> 
> 2. Is there a way to get at what the called program sends to stdout? I'm
> afraid that I don't have a clue how to get at that.

Upon closer examination, it looks like someone asked this question fairly 
recently on this list and didn't really get a useful response, so maybe 
there isn't a good way to do this. If not, then it would be _really_ nice if 
it were to be added to phobos.

> 
> Any help would be appreciated. Thanks.
> 
> - Jonathan M Davis



More information about the Digitalmars-d-learn mailing list