Getting an output string from a TProcess

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting an output string from a TProcess

Luciano de Souza
Hello all,

I want to execute FPC, but filtering the output in order to show only
the necessary information. The reason is I am blind, I use screen
reader and, in the console environment, it's not possible to read the
rows below without firstly read the rows above.
My intention is to use TProcess, calling fpc commmandline, replacing
all the non necessary information by ''. My first line will by
something like "compiling ' or 'linking '.
For this purpose, TProcess is wonderful.
I want a function that receives a commandline string and returns an
output string. This return will be afterwards used to  replacements.

function execute(AProcess: TProcess; instr: string):string;
var
buffer: integer;
begin
with AProcess do
begin
commandline := instr;
options := [poUsePipes];
execute;
output.read(buffer, 10);
result := buffer;
end;
end;

I am confused due to some reasons:
1. Commandline is signed as deprecated, so what is the best way to
pass a string to TProcess? Is the  commandline the first item of the
tstrings called parameter?
2. The output is a class derived from THandleStream. As the read
method is override, its behaviour is different from TStream.
So I ask: what's the correct code to inform as argument a TProcess and
a instruction string and as a return, to get the output string?

Regards,
Luciano
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Getting an output string from a TProcess

leledumbo
Administrator
> Commandline is signed as deprecated, so what is the best way to
pass a string to TProcess? Is the  commandline the first item of the
tstrings called parameter?

http://www.freepascal.org/docs-html/fcl/process/tprocess.commandline.html

See something that explains it there?

> The output is a class derived from THandleStream. As the read
method is override, its behaviour is different from TStream.

Not really, it still conforms to the Read method of TStream, the implementation should of course be adjusted to read from a process' output stream.

> So I ask: what's the correct code to inform as argument a TProcess and
a instruction string and as a return, to get the output string?

http://wiki.lazarus.freepascal.org/Executing_External_Programs#TProcess
Reply | Threaded
Open this post in threaded view
|

Re: Getting an output string from a TProcess

Marco van de Voort
In our previous episode, leledumbo said:
> > So I ask: what's the correct code to inform as argument a TProcess and
> a instruction string and as a return, to get the output string?

If very up to date FPC 2.7.1:

var s : ansistring;

if RunCommand(binaryname,['arg1','arg2'],s) then
  begin
    // success!
  end;

with binary name '/bin/df' or 'c:\windows\notepad.exe'

There are a couple of variants, see below. The variants that are equivalent
to "commandline" property use are deprecated.  (the unit this came from is
older than the cmdline deprecation, and these variants will share whatever
faith .commandline befalls)

The functions are basically minimal TProcess wrappers based on the already
named wiki page.

// most complex, allows separation of execution status and return value of
// product.
function RunCommandIndir(const curdir:string;const exename:string;const
commands:array of string;var outputstring:string;var
exitstatus:integer):integer;

// simpler form. In scripting uses, success or not is the only interesting
// metric.
function RunCommandIndir(const curdir:string;const exename:string;const
commands:array of string;var outputstring:string):boolean;

// simplest form, no dir.
function RunCommand(const exename:string;const commands:array of string;var
outputstring:string):boolean;

// real quick and dirty work. Deprecated?
function RunCommandInDir(const curdir,cmdline:string;var
outputstring:string):boolean; deprecated;
function RunCommand(const cmdline:string;var outputstring:string):boolean;
deprecated;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Getting an output string from a TProcess

leledumbo
Administrator
WUAOOOW!!! Where are those functions, Marco? Which revision implements them? And last but not least, do those functions consider BIG output?
Reply | Threaded
Open this post in threaded view
|

Re: Getting an output string from a TProcess

Marco van de Voort
In our previous episode, leledumbo said:
> WUAOOOW!!! Where are those functions, Marco?

In 2.7.1 (trunk)

> Which revision implements them?

r21437

> And last but not least, do those functions consider BIG output?

Yes. As said in the original message, the base is the large output wiki
source.

Please test them, then they can go into 2.6.2 (riskless since they don't modify
anything, they only add).

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Getting an output string from a TProcess

leledumbo
Administrator
> In 2.7.1 (trunk)

I mean... which unit?
Reply | Threaded
Open this post in threaded view
|

Re: Getting an output string from a TProcess

Michael Van Canneyt


On Sat, 2 Jun 2012, leledumbo wrote:

>> In 2.7.1 (trunk)
>
> I mean... which unit?

process.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal