Thread overview
How to execute external program and process its output?
Jul 31, 2006
Marcin Kuszczak
Jul 31, 2006
Pragma
Jul 31, 2006
Marcin Kuszczak
Jul 31, 2006
Tom S
Jul 31, 2006
Marcin Kuszczak
Jul 31, 2006
BCS
Jul 31, 2006
Marcin Kuszczak
Jul 31, 2006
BCS
Aug 01, 2006
Regan Heath
Aug 01, 2006
Marcin Kuszczak
July 31, 2006
Hello!

I am trying to execute external command from D program, and then process its output. spawnvp() doesn't seem to be enough as it does not return output, but just status as int.

How to achieve it in D (I don't know method also in C(++), so even this will
help <g>)?

---
Regards
Marcin Kuszczak
(Aarti_pl)
July 31, 2006
Marcin Kuszczak wrote:
> Hello!
> 
> I am trying to execute external command from D program, and then process its
> output. spawnvp() doesn't seem to be enough as it does not return output,
> but just status as int.
> 
> How to achieve it in D (I don't know method also in C(++), so even this will
> help <g>)?
> 
> ---
> Regards
> Marcin Kuszczak
> (Aarti_pl)

Marcin,
  I don't know the "correct" way to do this, but perhaps I can offer a short-term hack to get you by?


char[] commandline = "echo 'hello world' > output.txt";
std.process.system(commandline);
writefln("result: %s",std.file.read("output.txt"));


Sure its not pretty, but it does work.
July 31, 2006
Pragma wrote:

> char[] commandline = "echo 'hello world' > output.txt";
> std.process.system(commandline);
> writefln("result: %s",std.file.read("output.txt"));

I had to modify a little bit program, but then it works - thanks a lot!

--
void main() {
  char[] commandline = "svn info >output.txt 2>&1";
  std.process.system(commandline);
  char[] result = cast(char[])(std.file.read("output.txt"));
  writefln("result: %s", result);
}
--

In the meantime I found also that there should be functions on C API

--
#include <stdio.h>
FILE *popen( const char *command, const char *type);
int pclose( FILE *stream);
--

but as I see it is not wrapped by D. I don't have any idea how to do it. If anyone can give some more info it would be great.

BTW if D wants to be also language for scripting, it should have such a possibility in standard library.

-- 
Regards
Marcin Kuszczak
(Aarti_pl)
July 31, 2006
Marcin Kuszczak wrote:
> Pragma wrote:
> 
>> char[] commandline = "echo 'hello world' > output.txt";
>> std.process.system(commandline);
>> writefln("result: %s",std.file.read("output.txt"));
> 
> I had to modify a little bit program, but then it works - thanks a lot!
> 
> --
> void main() {
>   char[] commandline = "svn info >output.txt 2>&1";
>   std.process.system(commandline);
>   char[] result = cast(char[])(std.file.read("output.txt"));    writefln("result: %s", result);
> }
> --
> 
> In the meantime I found also that there should be functions on C API
> 
> --
> #include <stdio.h>
> FILE *popen( const char *command, const char *type);
> int pclose( FILE *stream);
> --
> 
> but as I see it is not wrapped by D. I don't have any idea how to do it. If
> anyone can give some more info it would be great.

extern (C) {
    typedef void FILE;
    FILE* popen(char* cmd, char* type);
    int pclose(FILE* stream);
}

should work
July 31, 2006
Marcin Kuszczak wrote:
> Hello!
> 
> I am trying to execute external command from D program, and then process its
> output. spawnvp() doesn't seem to be enough as it does not return output,
> but just status as int.
> 
> How to achieve it in D (I don't know method also in C(++), so even this will
> help <g>)?
> 
> ---
> Regards
> Marcin Kuszczak
> (Aarti_pl)


IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.
July 31, 2006
Tom S wrote:

> Marcin Kuszczak wrote:
>> Pragma wrote:
>> 
>>> char[] commandline = "echo 'hello world' > output.txt";
>>> std.process.system(commandline);
>>> writefln("result: %s",std.file.read("output.txt"));
>> 
>> I had to modify a little bit program, but then it works - thanks a lot!
>> 
>> --
>> void main() {
>>   char[] commandline = "svn info >output.txt 2>&1";
>>   std.process.system(commandline);
>>   char[] result = cast(char[])(std.file.read("output.txt"));
>>   writefln("result: %s", result);
>> }
>> --
>> 
>> In the meantime I found also that there should be functions on C API
>> 
>> --
>> #include <stdio.h>
>> FILE *popen( const char *command, const char *type);
>> int pclose( FILE *stream);
>> --
>> 
>> but as I see it is not wrapped by D. I don't have any idea how to do it. If anyone can give some more info it would be great.
> 
> extern (C) {
>      typedef void FILE;
>      FILE* popen(char* cmd, char* type);
>      int pclose(FILE* stream);
> }
> 
> should work

Thanks! using C was easier than I thought...

(Second method still does not work for me, but it is just a mater of
time :-) )

-- 
Regards
Marcin Kuszczak
(Aarti_pl)
July 31, 2006
BCS wrote:

> Marcin Kuszczak wrote:
>> Hello!
>> 
>> I am trying to execute external command from D program, and then process its output. spawnvp() doesn't seem to be enough as it does not return output, but just status as int.
>> 
>> How to achieve it in D (I don't know method also in C(++), so even this
>> will help <g>)?
>> 
>> ---
>> Regards
>> Marcin Kuszczak
>> (Aarti_pl)
> 
> 
> IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.

How to find it? is it somewere on Wiki?

-- 
Regards
Marcin Kuszczak
(Aarti_pl)
July 31, 2006
Marcin Kuszczak wrote:
> BCS wrote:
> 
> 
>>Marcin Kuszczak wrote:
>>
>>>Hello!
>>>
>>>I am trying to execute external command from D program, and then process
>>>its output. spawnvp() doesn't seem to be enough as it does not return
>>>output, but just status as int.
>>>
>>>How to achieve it in D (I don't know method also in C(++), so even this
>>>will help <g>)?
>>>
>>>---
>>>Regards
>>>Marcin Kuszczak
>>>(Aarti_pl)
>>
>>
>>IIRC somewhere back a few months ago, someone made a stream object that
>>did just what you want. You could even write to the program's standard
>>input by outputting to the stream.
> 
> 
> How to find it? is it somewere on Wiki?
> 

These look somewhat like what I remember.

http://www.digitalmars.com/d/archives/digitalmars/D/29556.html
http://www.digitalmars.com/d/archives/digitalmars/D/learn/542.html
August 01, 2006
On Mon, 31 Jul 2006 15:53:00 -0700, BCS <BCS@pathlink.com> wrote:
> Marcin Kuszczak wrote:
>> Hello!
>>  I am trying to execute external command from D program, and then
>> process its
>> output. spawnvp() doesn't seem to be enough as it does not return
>> output,
>> but just status as int.
>>  How to achieve it in D (I don't know method also in C(++), so even
>> this will
>> help <g>)?
>>  ---
>> Regards
>> Marcin Kuszczak
>> (Aarti_pl)
>
>
> IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.

Oh, me, me, pick me! Attached are the classes I wrote back then :) Let me know if you have any trouble.

Regan

August 01, 2006
Regan Heath wrote:

> On Mon, 31 Jul 2006 15:53:00 -0700, BCS <BCS@pathlink.com> wrote:
>> Marcin Kuszczak wrote:
>>> Hello!
>>>  I am trying to execute external command from D program, and then
>>> process its
>>> output. spawnvp() doesn't seem to be enough as it does not return
>>> output,
>>> but just status as int.
>>>  How to achieve it in D (I don't know method also in C(++), so even
>>> this will
>>> help <g>)?
>>>  ---
>>> Regards
>>> Marcin Kuszczak
>>> (Aarti_pl)
>>
>>
>> IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.
> 
> Oh, me, me, pick me! Attached are the classes I wrote back then :) Let me know if you have any trouble.
> 
> Regan

Thanks! I will give it a try.

It would be awesome if such a class could find its way into the standard library...

-- 
Regards
Marcin Kuszczak
(Aarti_pl)