Untitled diff

Created Diff never expires
112 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
110 lines
84 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
82 lines
function internalRuncommand(p:TProcess;var outputstring:string;
Function ReadInputPipe(const Pipe: TInputPipeStream; var BytesRead,OutputLen: integer; var OutputString: string): Boolean;
var stderrstring:string; var exitstatus:integer):integer;
var
var
available : integer;
numbytes,bytesread,available : integer;
NumBytes : integer;
outputlength, stderrlength : integer;
begin
stderrnumbytes,stderrbytesread : integer;
Result := False; available := 0;
begin
if(BytesRead<0)then BytesRead := 0;
result:=-1;
if(OutputLen<0)then OutputLen := 0;
try
repeat
try
if(Pipe=nil)then exit;
p.Options := [poUsePipes];
if(Pipe.NumBytesAvailable>0)
bytesread:=0;
then available := Pipe.NumBytesAvailable
outputlength:=0;
else break;
stderrbytesread:=0;
if(BytesRead+available>OutputLen)then
stderrlength:=0;
p.Execute;
while p.Running do
begin
// Only call ReadFromStream if Data from corresponding stream
// is already available, otherwise, on linux, the read call
// is blocking, and thus it is not possible to be sure to handle
// big data amounts bboth on output and stderr pipes. PM.
available:=P.Output.NumBytesAvailable;
if available > 0 then
begin
if (BytesRead + available > outputlength) then
begin
outputlength:=BytesRead + READ_BYTES;
Setlength(outputstring,outputlength);
end;
NumBytes := p.Output.Read(outputstring[1+bytesread], available);
if NumBytes > 0 then
Inc(BytesRead, NumBytes);
end
// The check for assigned(P.stderr) is mainly here so that
// if we use poStderrToOutput in p.Options, we do not access invalid memory.
else if assigned(P.stderr) and (P.StdErr.NumBytesAvailable > 0) then
begin
available:=P.StdErr.NumBytesAvailable;
if (StderrBytesRead + available > stderrlength) then
begin
stderrlength:=StderrBytesRead + READ_BYTES;
Setlength(stderrstring,stderrlength);
end;
StderrNumBytes := p.StdErr.Read(stderrstring[1+StderrBytesRead], available);
if StderrNumBytes > 0 then
Inc(StderrBytesRead, StderrNumBytes);
end
else
Sleep(100);
end;
// Get left output after end of execution
available:=P.Output.NumBytesAvailable;
while available > 0 do
begin
if (BytesRead + available > outputlength) then
begin
outputlength:=BytesRead + READ_BYTES;
Setlength(outputstring,outputlength);
end;
NumBytes := p.Output.Read(outputstring[1+bytesread], available);
if NumBytes > 0 then
Inc(BytesRead, NumBytes);
available:=P.Output.NumBytesAvailable;
end;
setlength(outputstring,BytesRead);
while assigned(P.stderr) and (P.Stderr.NumBytesAvailable > 0) do
begin
available:=P.Stderr.NumBytesAvailable;
if (StderrBytesRead + available > stderrlength) then
begin
stderrlength:=StderrBytesRead + READ_BYTES;
Setlength(stderrstring,stderrlength);
end;
StderrNumBytes := p.StdErr.Read(stderrstring[1+StderrBytesRead], available);
if StderrNumBytes > 0 then
Inc(StderrBytesRead, StderrNumBytes);
end;
setlength(stderrstring,StderrBytesRead);
exitstatus:=p.exitstatus;
result:=0; // we came to here, document that.
except
on e : Exception do
begin
begin
result:=1;
OutputLen := BytesRead+READ_BYTES;
setlength(outputstring,BytesRead);
SetLength(OutputString,OutputLen);
end;
try
try
NumBytes := Pipe.Read(OutputString[1+BytesRead], available);
if(NumBytes>0)
then Inc(BytesRead, NumBytes);
except
Result := False;
end;
end;
finally
Result := True;
end;
until false;
end;

Function InternalRunCommand(var P: TProcess; var Stdout, Stderr: string; var ExitStatus:integer): integer;
var
bytesread_stdout : integer;
outputlen_stdout : integer;
bytesread_stderr : integer;
outputlen_stderr : integer;
begin
Result := -1; if(P=nil)then exit;
try
try
P.Options := [poUsePipes];
bytesread_stdout := 0;
outputlen_stdout := 0;
bytesread_stderr := 0;
outputlen_stderr := 0;
P.Execute;
while(P.Running)do
begin
if(not ReadInputPipe(P.Output, bytesread_stdout, outputlen_stdout, Stdout))and
(not ReadInputPipe(P.Stderr, bytesread_stderr, outputlen_stderr, Stderr))
then sleep(100);
end;
//Get remaining output after end of execution:
ReadInputPipe(P.Output, bytesread_stdout, outputlen_stdout, Stdout);
ReadInputPipe(P.Stderr, bytesread_stderr, outputlen_stderr, Stderr);
ExitStatus := P.ExitStatus;
Result := 0; // we came to here, document that.
except
Result := 1;
end;
finally
FreeAndNil(P);
SetLength(Stdout,bytesread_stdout);
SetLength(Stderr,bytesread_stderr);
end;
end;
finally
end;
p.free;
end;
Function RunCommand(const ExeName:string;const Commands:array of string;var Stdout,StdErr:string): Boolean;
end;
var

ExitStatus,i : integer;
function RunCommand(const exename:string;const commands:array of string;var outputstring:string):boolean;
P : TProcess;
Var
begin
p : TProcess;
P := TProcess.Create(nil);
i,
P.Executable := ExeName;
exitstatus : integer;
if high(commands)>=0
ErrorString : String;
then for i:=low(Commands) to high(Commands)
begin
do P.Parameters.Add(Commands[i]);
p:=TProcess.create(nil);
Result := (InternalRunCommand(P,StdOut,StdErr,ExitStatus)=0) and (ExitStatus=0);
p.Executable:=exename;
end;
if high(commands)>=0 then
for i:=low(commands) to high(commands) do
p.Parameters.add(commands[i]);
result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
if exitstatus<>0 then result:=false;
end;