Calling external programs from D

Lionello Lunesu lio at lunesu.remove.com
Tue Apr 11 23:21:54 PDT 2006


BCS wrote:
> In article <e1g3sr$212p$1 at digitaldaemon.com>, Lionello Lunesu says...
>> Just some idea:
>>
>> Wouldn't it be nice if there were an "extern (System)" calling 
>> convention that would start external programs?
>>
>> For example:
>>
>> #extern (System) int wget(...);
>> #
>> #int main() {
>> #  return wget("http://whatever");
>> #}
>>
>> This would boost productivity especially for D scripts. Only too often I 
>> need a small, temporary program to process some data and I wish I had 
>> the compilable source of wget/grep/utility-x at hand to incorporate 
>> their functionality.
>>
>> Seems easy enough to implement using spawning/waiting. The program can 
>> be declared with "..." or some defined arugments, but all arguments to 
>> the function would have to be cast to char[] for passing to the new 
>> process. Would of course be even cooler if the linker could pull in the 
>> external program's main(), but I suppose that's a bit tricky :)
>>
>> L.
> 
> 
> 
> As for accessing stdin/stdout, let them return a Stream type spliced in with
> some pipes. You'd have to ditch the wait though.


Wooh, that's a whole different ball game. The "extern (System)" seemed 
nice, since the declared function will behave much like any other 
imported function. You can replace the external "wget" (or whatever) 
with your implementation at any time, without having to change the rest 
of the program.

The "calling convention" that you suggested is pretty tricky. If you 
were to do that for a normal function, you'd have to put it in a 
separate thread and synchronize communications with that thread, much 
like the stream in your example. IO with another process should 
definitely be made easier, but I suppose a good wrapper class can take 
care of that.

L.



More information about the Digitalmars-d mailing list