www.digitalmars.com         C & C++   DMDScript  

D - Q: execute system command and grab output?

reply Ant <Ant_member pathlink.com> writes:
How can we execute a system command from D
and grab the output ?

I guess executing it on a separate thread should be trivial,
(if not can you throw that in also? :)

(I've seen:

  extern(C) int system(char*);
  int main()
  {
      system("some_program");
      return 0;
  }

  // system is <stdlib.h>
  // exec.. is <unistd.h> is it preferred on unix?
)

Thanks

Ant
Oct 10 2003
parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
You can use popen and the like to implement a cross-platform solution .  Id
like a copy when your done ;).

C


"Ant" <Ant_member pathlink.com> wrote in message
news:bm7ts9$2i75$1 digitaldaemon.com...
 How can we execute a system command from D
 and grab the output ?

 I guess executing it on a separate thread should be trivial,
 (if not can you throw that in also? :)

 (I've seen:

   extern(C) int system(char*);
   int main()
   {
       system("some_program");
       return 0;
   }

   // system is <stdlib.h>
   // exec.. is <unistd.h> is it preferred on unix?
 )

 Thanks

 Ant

Oct 11 2003
next sibling parent Ant <Ant_member pathlink.com> writes:
In article <bm9k5k$1qc8$1 digitaldaemon.com>, Charles Sanders says...
You can use popen and the like to implement a cross-platform solution .  Id
like a copy when your done ;).

C

man popen "The popen() function opens a process by creating a pipe, forking, and invoking the shell." Thanks Ant
Oct 11 2003
prev sibling parent reply Ant <Ant_member pathlink.com> writes:
In article <bm9k5k$1qc8$1 digitaldaemon.com>, Charles Sanders says...
You can use popen and the like to implement a cross-platform solution .  Id
like a copy when your done ;).

C

see attachment. How do you like it? Please send back improvements or suggestions. For now this is good enought for me as I can run 'make' from leds and capture the output into a text widget :) (maybe it's just another "pointless wrappers around C runtime library functions or OS API functions" but i prefer to do it like this.) It's not ideal as we might want stderr also. (in linux/bash append " 2>&1" to the command to send stderr into stdout) Probably it's possible to grab stdin/out/err of a process that's already started but I've not idea here to look) Ant
Oct 13 2003
parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
Cool ill check it out when i get home :D.

C

"Ant" <Ant_member pathlink.com> wrote in message
news:bmf302$2vfc$1 digitaldaemon.com...
 In article <bm9k5k$1qc8$1 digitaldaemon.com>, Charles Sanders says...
You can use popen and the like to implement a cross-platform solution .


like a copy when your done ;).

C

see attachment. How do you like it? Please send back improvements or suggestions. For now this is good enought for me as I can run 'make' from leds and capture the output into a text widget :) (maybe it's just another "pointless wrappers around C runtime library functions or OS API

 but i prefer to do it like this.)

 It's not ideal as we might want stderr also.
 (in linux/bash append " 2>&1" to the command to send stderr into stdout)
 Probably it's possible to grab stdin/out/err of a process that's
 already started but I've not idea here to look)


 Ant

Oct 13 2003
next sibling parent Ant <Ant_member pathlink.com> writes:
In article <bmf5tk$22q$1 digitaldaemon.com>, Charles Sanders says...
Cool ill check it out when i get home :D.

C

opps... probably,after the cycle on read(), we need: // not tested if ( line.length > 0 ) { foreach(POpenLineListener listener; lineListeners) { listener.popenLineCallback(line.dup); } } but I guess this will never get us the last line if it's empty... Ant
Oct 13 2003
prev sibling parent Ant <Ant_member pathlink.com> writes:
In article <bmf5tk$22q$1 digitaldaemon.com>, Charles Sanders says...

You can use popen and the like to implement a cross-platform solution .

So, will it run on Windows just how it is? (I added a notification of "job end" to the interfaces.) Ant
Oct 15 2003