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 begin 0644 POpen.d M+RH*("H 4$]P96X*("H "B`J(&)Y($%N=&]N:6\ 36]N=&5I<F\*("H "B`J M(&QI8V5N<V4 3$=03`H *B\*"FUO9'5L92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PN"B`J(%-I;F-E(&$ <&EP92!I<R!B>2!D M969I;FET:6]N('5N:61I<F5C=&EO;F%L+"!T:&4 ='EP92!A<F=U;65N="!M M87D*("H <W!E8VEF>2!O;FQY(')E861I;F< ;W( =W)I=&EN9RP (&YO="` M8F]T:#L*("H =&AE(')E<W5L=&EN9R!S=')E86T :7, 8V]R<F5S<&]N9&EN M9VQY(')E860M;VYL>2!O<B!W<FET92UO;FQY+ H *B!<=&]D;R!W<FET92!T M;R!P:7!E(&UE=&AO9`H *B!<=&]D;R`_<F5A9"!B>2!T;VME;C\*("HO"G!U M8FQI8SH*8VQA<W, 4$]P96X*>PH)<')I=F%T93H*"49)3$4J('!&:6QE.PH) M4$]P96Y-;V1E(&UO9&4[" E03W!E;DQI;F5,:7-T96YE<EM=(&QI;F5,:7-T M96YE<G,[" E03W!E;D)Y=&5,:7-T96YE<EM=(&)Y=&5,:7-T96YE<G,[" H) M<'5B;&EC. H)" DO*BH*"2`J($-R96%T97, 82!N97< 4$]P96X 86YD(&5X M96-U=&5S(&$ 8V]M;6%N9"X*"2`J($!P87)A;2!C;VUM86YD('1H92!S>7-T M96T 8V]M;6%N9"!T;R!E>&5C=71E" D *B!`;6]D92!T:&4 4$]P96Y-;V1E M(" N4D5!1"!O<B`N5U))5$4I" D *B\*"71H:7,H8VAA<EM=(&-O;6UA;F0L M(%!/<&5N36]D92!M;V1E+"!B:70 =&AR96%D960I" E[" D)=&AI<RAN=6QL M+&YU;&PL8V]M;6%N9"P ;6]D92P =&AR96%D960I.PH)?0H)" DO*BH*"2`J M($-R96%T97, 82!N97< 4$]P96X 9F]R(&$ ;&EN92!L:7-T96YE<B!A;F0 M97AE8W5T97, 82!C;VUM86YD+ H)("H 0'!A<F%M(&-O;6UA;F0 =&AE('-Y M<W1E;2!C;VUM86YD('1O(&5X96-U=&4*"2`J($!M;V1E('1H92!03W!E;DUO M9&4 *"Y214%$(&]R("Y74DE412D*"2`J+PH)=&AI<RA03W!E;DQI;F5,:7-T M96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&UO9&4I" D)>PH)"0EC87-E(%!/<&5N36]D92Y214%$. H)"0D) M" D)"0EP1FEL92`]('!O<&5N*"AC:&%R*BDH8V]M;6%N9'XB7#`B*2P *&-H M87(J*2 B<EPP(BDI.PH)"0D)<F5A9"AT:')E861E9"D[" D)"0EB<F5A:SL* M"0D)" D)"6-A<V4 4$]P96Y-;V1E+E=2251%. H)"0D)<$9I;&4 /2!P;W!E M;B H8VAA<BHI*&-O;6UA;F1^(EPP(BDL("AC:&%R*BDH(G=<,"(I*3L*"0D) M"6)R96%K.PH)"7T*"7T*" DO*BH*"2`J($-L;W-E<R!T:&4 ;W!E;F5D('!I M<&4*"2`J+PH)?G1H:7,H*0H)>PH)"6-L;W-E*"D[" E]" H)+RHJ" D *B!# M;&]S97, =&AE(&]P96YE9"!P:7!E" D *B\*"7!R:79A=&4Z" EV;VED(&-L M;W-E*"D*"7L*"0EI9B`H('!&:6QE("$]/2!N=6QL("D*"0E[" D)"7!C;&]S M92AP1FEL92D[" D)"7!&:6QE(#T ;G5L;#L*"0E]" E]" H)<'5B;&EC. H) M" DO*BH*"2`J($%D9', 82!N97< ;&EN92!L:7-T96YE<B!T;R!T:&ES(%!/ M<&5N" D *B!`<&%R86T ;&ES=&5N97(*"2`J+PH)=F]I9"!A9&1,:6YE3&ES M=&5N97(H4$]P96Y,:6YE3&ES=&5N97( ;&ES=&5N97(I" E[" D):68 *"!L M:7-T96YE<B`A/3T ;G5L;"`I" D)>PH)"0EL:6YE3&ES=&5N97)S('X](&QI M<W1E;F5R.PH)"7T*"7T*"0H)+RHJ" D *B!!9&1S(&$ ;F5W(&)Y=&4 ;&ES M=&5N97( =&\ =&AI<R!03W!E; H)("H 0'!A<F%M(&QI<W1E;F5R" D *B\* M"79O:60 861D0GET94QI<W1E;F5R*%!/<&5N0GET94QI<W1E;F5R(&QI<W1E M;F5R*0H)>PH)"6EF(" ;&ES=&5N97( (3T](&YU;&P *0H)"7L*"0D)8GET M94QI<W1E;F5R<R!^/2!L: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)"7T*"0ER971U<FX ,#L*"7T* M" DO*BH*"2`J(%)E861S(&QI;F4 8GD ;&EN92!O<'1I;VYA;'D ;VX 82!S M97!A<F%T92!T:')E860*"2`J($!P87)A;2!L:7-T96YE<B!T:&4 4$]P96Y, M:7-T96YE< H)("H 0'!A<F%M('1H<F5A9&5D(&EF('1R=64 82!S97!A<F%T M92!T:')E860 =VEL;"!B92!C<F5A=&5D('1O(')E860 =&AE('!I<&4*"2`J M+PH)<')I=F%T90H)=F]I9"!R96%D*&)I="!T:')E861E9"D*"7L*"0EI9B`H M("%T:')E861E9"`I" D)>PH)"0ER96%D*"D[" D)?0H)"65L<V4*"0E[" D) M"51H<F5A9"!T:')E860 /2!N97< 5&AR96%D*"9R96%D*3L*"0D)=&AR96%D 4+G-T87)T*"D[" D)?0H)?0H*?0H ` end
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 .
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
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