Untitled diff

Created Diff never expires
121 removals
651 lines
100 additions
632 lines
{
{
This file is part of the Free Component Library (FCL)
This file is part of the Free Component Library (FCL)
Copyright (c) 1999-2000 by the Free Pascal development team
Copyright (c) 1999-2000 by the Free Pascal development team


See the file COPYING.FPC, included in this distribution,
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
for details about the copyright.


This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


**********************************************************************}
**********************************************************************}
{$mode objfpc}
{$mode objfpc}
{$h+}
{$h+}
unit process;
unit process;


interface
interface


Uses Classes,
Uses Classes,
pipes,
pipes,
SysUtils;
SysUtils;


Type
Type
TProcessOption = (poRunSuspended,poWaitOnExit,
TProcessOption = (poRunSuspended,poWaitOnExit,
poUsePipes,poStderrToOutPut,
poUsePipes,poStderrToOutPut,
poNoConsole,poNewConsole,
poNoConsole,poNewConsole,
poDefaultErrorMode,poNewProcessGroup,
poDefaultErrorMode,poNewProcessGroup,
poDebugProcess,poDebugOnlyThisProcess);
poDebugProcess,poDebugOnlyThisProcess);


TShowWindowOptions = (swoNone,swoHIDE,swoMaximize,swoMinimize,swoRestore,swoShow,
TShowWindowOptions = (swoNone,swoHIDE,swoMaximize,swoMinimize,swoRestore,swoShow,
swoShowDefault,swoShowMaximized,swoShowMinimized,
swoShowDefault,swoShowMaximized,swoShowMinimized,
swoshowMinNOActive,swoShowNA,swoShowNoActivate,swoShowNormal);
swoshowMinNOActive,swoShowNA,swoShowNoActivate,swoShowNormal);


TStartupOption = (suoUseShowWindow,suoUseSize,suoUsePosition,
TStartupOption = (suoUseShowWindow,suoUseSize,suoUsePosition,
suoUseCountChars,suoUseFillAttribute);
suoUseCountChars,suoUseFillAttribute);


TProcessPriority = (ppHigh,ppIdle,ppNormal,ppRealTime);
TProcessPriority = (ppHigh,ppIdle,ppNormal,ppRealTime);


TProcessOptions = set of TProcessOption;
TProcessOptions = set of TProcessOption;
TStartupOptions = set of TStartupOption;
TStartupOptions = set of TStartupOption;




Type
Type
{$ifdef UNIX}
{$ifdef UNIX}
TProcessForkEvent = procedure(Sender : TObject) of object;
TProcessForkEvent = procedure(Sender : TObject) of object;
{$endif UNIX}
{$endif UNIX}


{ TProcess }
{ TProcess }


TProcess = Class (TComponent)
TProcess = Class (TComponent)
Private
Private
FProcessOptions : TProcessOptions;
FProcessOptions : TProcessOptions;
FStartupOptions : TStartupOptions;
FStartupOptions : TStartupOptions;
FProcessID : Integer;
FProcessID : Integer;
FTerminalProgram: String;
FTerminalProgram: String;
FThreadID : Integer;
FThreadID : Integer;
FProcessHandle : Thandle;
FProcessHandle : Thandle;
FThreadHandle : Thandle;
FThreadHandle : Thandle;
FFillAttribute : Cardinal;
FFillAttribute : Cardinal;
FApplicationName : string;
FApplicationName : string;
FConsoleTitle : String;
FConsoleTitle : String;
FCommandLine : String;
FCommandLine : String;
FCurrentDirectory : String;
FCurrentDirectory : String;
FDesktop : String;
FDesktop : String;
FEnvironment : Tstrings;
FEnvironment : Tstrings;
FExecutable : String;
FExecutable : String;
FParameters : TStrings;
FParameters : TStrings;
FShowWindow : TShowWindowOptions;
FShowWindow : TShowWindowOptions;
FInherithandles : Boolean;
FInherithandles : Boolean;
{$ifdef UNIX}
{$ifdef UNIX}
FForkEvent : TProcessForkEvent;
FForkEvent : TProcessForkEvent;
{$endif UNIX}
{$endif UNIX}
FProcessPriority : TProcessPriority;
FProcessPriority : TProcessPriority;
dwXCountchars,
dwXCountchars,
dwXSize,
dwXSize,
dwYsize,
dwYsize,
dwx,
dwx,
dwYcountChars,
dwYcountChars,
dwy : Cardinal;
dwy : Cardinal;
FXTermProgram: String;
FXTermProgram: String;
FPipeBufferSize : cardinal;
FPipeBufferSize : cardinal;
Procedure FreeStreams;
Procedure FreeStreams;
Function GetExitStatus : Integer;
Function GetExitStatus : Integer;
Function GetExitCode : Integer;
Function GetExitCode : Integer;
Function GetRunning : Boolean;
Function GetRunning : Boolean;
Function GetWindowRect : TRect;
Function GetWindowRect : TRect;
procedure SetCommandLine(const AValue: String);
procedure SetCommandLine(const AValue: String);
procedure SetParameters(const AValue: TStrings);
procedure SetParameters(const AValue: TStrings);
Procedure SetWindowRect (Value : TRect);
Procedure SetWindowRect (Value : TRect);
Procedure SetShowWindow (Value : TShowWindowOptions);
Procedure SetShowWindow (Value : TShowWindowOptions);
Procedure SetWindowColumns (Value : Cardinal);
Procedure SetWindowColumns (Value : Cardinal);
Procedure SetWindowHeight (Value : Cardinal);
Procedure SetWindowHeight (Value : Cardinal);
Procedure SetWindowLeft (Value : Cardinal);
Procedure SetWindowLeft (Value : Cardinal);
Procedure SetWindowRows (Value : Cardinal);
Procedure SetWindowRows (Value : Cardinal);
Procedure SetWindowTop (Value : Cardinal);
Procedure SetWindowTop (Value : Cardinal);
Procedure SetWindowWidth (Value : Cardinal);
Procedure SetWindowWidth (Value : Cardinal);
procedure SetApplicationName(const Value: String);
procedure SetApplicationName(const Value: String);
procedure SetProcessOptions(const Value: TProcessOptions);
procedure SetProcessOptions(const Value: TProcessOptions);
procedure SetActive(const Value: Boolean);
procedure SetActive(const Value: Boolean);
procedure SetEnvironment(const Value: TStrings);
procedure SetEnvironment(const Value: TStrings);
Procedure ConvertCommandLine;
Procedure ConvertCommandLine;
function PeekExitStatus: Boolean;
function PeekExitStatus: Boolean;
Protected
Protected
FRunning : Boolean;
FRunning : Boolean;
FExitCode : Cardinal;
FExitCode : Cardinal;
FInputStream : TOutputPipeStream;
FInputStream : TOutputPipeStream;
FOutputStream : TInputPipeStream;
FOutputStream : TInputPipeStream;
FStderrStream : TInputPipeStream;
FStderrStream : TInputPipeStream;
procedure CloseProcessHandles; virtual;
procedure CloseProcessHandles; virtual;
Procedure CreateStreams(InHandle,OutHandle,ErrHandle : Longint);virtual;
Procedure CreateStreams(InHandle,OutHandle,ErrHandle : Longint);virtual;
procedure FreeStream(var AStream: THandleStream);
procedure FreeStream(var AStream: THandleStream);
procedure Loaded; override;
procedure Loaded; override;
Public
Public
Constructor Create (AOwner : TComponent);override;
Constructor Create (AOwner : TComponent);override;
Destructor Destroy; override;
Destructor Destroy; override;
Procedure Execute; virtual;
Procedure Execute; virtual;
procedure CloseInput; virtual;
procedure CloseInput; virtual;
procedure CloseOutput; virtual;
procedure CloseOutput; virtual;
procedure CloseStderr; virtual;
procedure CloseStderr; virtual;
Function Resume : Integer; virtual;
Function Resume : Integer; virtual;
Function Suspend : Integer; virtual;
Function Suspend : Integer; virtual;
Function Terminate (AExitCode : Integer): Boolean; virtual;
Function Terminate (AExitCode : Integer): Boolean; virtual;
Function WaitOnExit : Boolean;
Function WaitOnExit : Boolean;
Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
Property Handle : THandle Read FProcessHandle;
Property Handle : THandle Read FProcessHandle;
Property ProcessHandle : THandle Read FProcessHandle;
Property ProcessHandle : THandle Read FProcessHandle;
Property ThreadHandle : THandle Read FThreadHandle;
Property ThreadHandle : THandle Read FThreadHandle;
Property ProcessID : Integer Read FProcessID;
Property ProcessID : Integer Read FProcessID;
Property ThreadID : Integer Read FThreadID;
Property ThreadID : Integer Read FThreadID;
Property Input : TOutputPipeStream Read FInputStream;
Property Input : TOutputPipeStream Read FInputStream;
Property Output : TInputPipeStream Read FOutputStream;
Property Output : TInputPipeStream Read FOutputStream;
Property Stderr : TinputPipeStream Read FStderrStream;
Property Stderr : TinputPipeStream Read FStderrStream;
Property ExitStatus : Integer Read GetExitStatus;
Property ExitStatus : Integer Read GetExitStatus;
Property ExitCode : Integer Read GetExitCode;
Property ExitCode : Integer Read GetExitCode;
Property InheritHandles : Boolean Read FInheritHandles Write FInheritHandles;
Property InheritHandles : Boolean Read FInheritHandles Write FInheritHandles;
{$ifdef UNIX}
{$ifdef UNIX}
property OnForkEvent : TProcessForkEvent Read FForkEvent Write FForkEvent;
property OnForkEvent : TProcessForkEvent Read FForkEvent Write FForkEvent;
{$endif UNIX}
{$endif UNIX}
Published
Published
property PipeBufferSize : cardinal read FPipeBufferSize write FPipeBufferSize default 1024;
property PipeBufferSize : cardinal read FPipeBufferSize write FPipeBufferSize default 1024;
Property Active : Boolean Read GetRunning Write SetActive;
Property Active : Boolean Read GetRunning Write SetActive;
Property ApplicationName : String Read FApplicationName Write SetApplicationName; deprecated;
Property ApplicationName : String Read FApplicationName Write SetApplicationName; deprecated;
Property CommandLine : String Read FCommandLine Write SetCommandLine ; deprecated;
Property CommandLine : String Read FCommandLine Write SetCommandLine ; deprecated;
Property Executable : String Read FExecutable Write FExecutable;
Property Executable : String Read FExecutable Write FExecutable;
Property Parameters : TStrings Read FParameters Write SetParameters;
Property Parameters : TStrings Read FParameters Write SetParameters;
Property ConsoleTitle : String Read FConsoleTitle Write FConsoleTitle;
Property ConsoleTitle : String Read FConsoleTitle Write FConsoleTitle;
Property CurrentDirectory : String Read FCurrentDirectory Write FCurrentDirectory;
Property CurrentDirectory : String Read FCurrentDirectory Write FCurrentDirectory;
Property Desktop : String Read FDesktop Write FDesktop;
Property Desktop : String Read FDesktop Write FDesktop;
Property Environment : TStrings Read FEnvironment Write SetEnvironment;
Property Environment : TStrings Read FEnvironment Write SetEnvironment;
Property Options : TProcessOptions Read FProcessOptions Write SetProcessOptions;
Property Options : TProcessOptions Read FProcessOptions Write SetProcessOptions;
Property Priority : TProcessPriority Read FProcessPriority Write FProcessPriority;
Property Priority : TProcessPriority Read FProcessPriority Write FProcessPriority;
Property StartupOptions : TStartupOptions Read FStartupOptions Write FStartupOptions;
Property StartupOptions : TStartupOptions Read FStartupOptions Write FStartupOptions;
Property Running : Boolean Read GetRunning;
Property Running : Boolean Read GetRunning;
Property ShowWindow : TShowWindowOptions Read FShowWindow Write SetShowWindow;
Property ShowWindow : TShowWindowOptions Read FShowWindow Write SetShowWindow;
Property WindowColumns : Cardinal Read dwXCountChars Write SetWindowColumns;
Property WindowColumns : Cardinal Read dwXCountChars Write SetWindowColumns;
Property WindowHeight : Cardinal Read dwYSize Write SetWindowHeight;
Property WindowHeight : Cardinal Read dwYSize Write SetWindowHeight;
Property WindowLeft : Cardinal Read dwX Write SetWindowLeft;
Property WindowLeft : Cardinal Read dwX Write SetWindowLeft;
Property WindowRows : Cardinal Read dwYCountChars Write SetWindowRows;
Property WindowRows : Cardinal Read dwYCountChars Write SetWindowRows;
Property WindowTop : Cardinal Read dwY Write SetWindowTop ;
Property WindowTop : Cardinal Read dwY Write SetWindowTop ;
Property WindowWidth : Cardinal Read dwXSize Write SetWindowWidth;
Property WindowWidth : Cardinal Read dwXSize Write SetWindowWidth;
Property FillAttribute : Cardinal read FFillAttribute Write FFillAttribute;
Property FillAttribute : Cardinal read FFillAttribute Write FFillAttribute;
Property XTermProgram : String Read FXTermProgram Write FXTermProgram;
Property XTermProgram : String Read FXTermProgram Write FXTermProgram;
end;
end;


EProcess = Class(Exception);
EProcess = Class(Exception);


Procedure CommandToList(S : String; List : TStrings);
Procedure CommandToList(S : String; List : TStrings);


{$ifdef unix}
{$ifdef unix}
Var
Var
TryTerminals : Array of string;
TryTerminals : Array of string;
XTermProgram : String;
XTermProgram : String;
Function DetectXTerm : String;
Function DetectXTerm : String;
{$endif unix}
{$endif unix}


function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string;var exitstatus:integer):integer;
function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string;var exitstatus:integer):integer;
function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string):boolean;
function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string):boolean;
function RunCommandInDir(const curdir,cmdline:string;var outputstring:string):boolean; deprecated;
function RunCommandInDir(const curdir,cmdline:string;var outputstring:string):boolean; deprecated;


function RunCommand(const exename:string;const commands:array of string;var outputstring:string):boolean;
function RunCommand(const exename:string;const commands:array of string;var stdout:string):boolean; overload;
function RunCommand(const cmdline:string;var outputstring:string):boolean; deprecated;
function RunCommand(const exename:string;const commands:array of string;var stdout,stderr:string):boolean; overload;
function RunCommand(const cmdline:string;var outputstring:string):boolean; overload; deprecated;




implementation
implementation


{$i process.inc}
{$i process.inc}


Procedure CommandToList(S : String; List : TStrings);
Procedure CommandToList(S : String; List : TStrings);


Function GetNextWord : String;
Function GetNextWord : String;


Const
Const
WhiteSpace = [' ',#9,#10,#13];
WhiteSpace = [' ',#9,#10,#13];
Literals = ['"',''''];
Literals = ['"',''''];


Var
Var
Wstart,wend : Integer;
Wstart,wend : Integer;
InLiteral : Boolean;
InLiteral : Boolean;
LastLiteral : char;
LastLiteral : char;


begin
begin
WStart:=1;
WStart:=1;
While (WStart<=Length(S)) and (S[WStart] in WhiteSpace) do
While (WStart<=Length(S)) and (S[WStart] in WhiteSpace) do
Inc(WStart);
Inc(WStart);
WEnd:=WStart;
WEnd:=WStart;
InLiteral:=False;
InLiteral:=False;
LastLiteral:=#0;
LastLiteral:=#0;
While (Wend<=Length(S)) and (Not (S[Wend] in WhiteSpace) or InLiteral) do
While (Wend<=Length(S)) and (Not (S[Wend] in WhiteSpace) or InLiteral) do
begin
begin
if S[Wend] in Literals then
if S[Wend] in Literals then
If InLiteral then
If InLiteral then
InLiteral:=Not (S[Wend]=LastLiteral)
InLiteral:=Not (S[Wend]=LastLiteral)
else
else
begin
begin
InLiteral:=True;
InLiteral:=True;
LastLiteral:=S[Wend];
LastLiteral:=S[Wend];
end;
end;
inc(wend);
inc(wend);
end;
end;


Result:=Copy(S,WStart,WEnd-WStart);
Result:=Copy(S,WStart,WEnd-WStart);


if (Length(Result) > 0)
if (Length(Result) > 0)
and (Result[1] = Result[Length(Result)]) // if 1st char = last char and..
and (Result[1] = Result[Length(Result)]) // if 1st char = last char and..
and (Result[1] in Literals) then // it's one of the literals, then
and (Result[1] in Literals) then // it's one of the literals, then
Result:=Copy(Result, 2, Length(Result) - 2); //delete the 2 (but not others in it)
Result:=Copy(Result, 2, Length(Result) - 2); //delete the 2 (but not others in it)


While (WEnd<=Length(S)) and (S[Wend] in WhiteSpace) do
While (WEnd<=Length(S)) and (S[Wend] in WhiteSpace) do
inc(Wend);
inc(Wend);
Delete(S,1,WEnd-1);
Delete(S,1,WEnd-1);


end;
end;


Var
Var
W : String;
W : String;


begin
begin
While Length(S)>0 do
While Length(S)>0 do
begin
begin
W:=GetNextWord;
W:=GetNextWord;
If (W<>'') then
If (W<>'') then
List.Add(W);
List.Add(W);
end;
end;
end;
end;


Constructor TProcess.Create (AOwner : TComponent);
Constructor TProcess.Create (AOwner : TComponent);
begin
begin
Inherited;
Inherited;
FProcessPriority:=ppNormal;
FProcessPriority:=ppNormal;
FShowWindow:=swoNone;
FShowWindow:=swoNone;
FInheritHandles:=True;
FInheritHandles:=True;
{$ifdef UNIX}
{$ifdef UNIX}
FForkEvent:=nil;
FForkEvent:=nil;
{$endif UNIX}
{$endif UNIX}
FPipeBufferSize := 1024;
FPipeBufferSize := 1024;
FEnvironment:=TStringList.Create;
FEnvironment:=TStringList.Create;
FParameters:=TStringList.Create;
FParameters:=TStringList.Create;
end;
end;


Destructor TProcess.Destroy;
Destructor TProcess.Destroy;


begin
begin
FParameters.Free;
FParameters.Free;
FEnvironment.Free;
FEnvironment.Free;
FreeStreams;
FreeStreams;
CloseProcessHandles;
CloseProcessHandles;
Inherited Destroy;
Inherited Destroy;
end;
end;


Procedure TProcess.FreeStreams;
Procedure TProcess.FreeStreams;
begin
begin
If FStderrStream<>FOutputStream then
If FStderrStream<>FOutputStream then
FreeStream(THandleStream(FStderrStream));
FreeStream(THandleStream(FStderrStream));
FreeStream(THandleStream(FOutputStream));
FreeStream(THandleStream(FOutputStream));
FreeStream(THandleStream(FInputStream));
FreeStream(THandleStream(FInputStream));
end;
end;




Function TProcess.GetExitStatus : Integer;
Function TProcess.GetExitStatus : Integer;


begin
begin
GetRunning;
GetRunning;
Result:=FExitCode;
Result:=FExitCode;
end;
end;


{$IFNDEF OS_HASEXITCODE}
{$IFNDEF OS_HASEXITCODE}
Function TProcess.GetExitCode : Integer;
Function TProcess.GetExitCode : Integer;


begin
begin
if Not Running then
if Not Running then
Result:=GetExitStatus
Result:=GetExitStatus
else
else
Result:=0
Result:=0
end;
end;
{$ENDIF}
{$ENDIF}


Function TProcess.GetRunning : Boolean;
Function TProcess.GetRunning : Boolean;


begin
begin
IF FRunning then
IF FRunning then
FRunning:=Not PeekExitStatus;
FRunning:=Not PeekExitStatus;
Result:=FRunning;
Result:=FRunning;
end;
end;




Procedure TProcess.CreateStreams(InHandle,OutHandle,ErrHandle : Longint);
Procedure TProcess.CreateStreams(InHandle,OutHandle,ErrHandle : Longint);


begin
begin
FreeStreams;
FreeStreams;
FInputStream:=TOutputPipeStream.Create (InHandle);
FInputStream:=TOutputPipeStream.Create (InHandle);
FOutputStream:=TInputPipeStream.Create (OutHandle);
FOutputStream:=TInputPipeStream.Create (OutHandle);
if Not (poStderrToOutput in FProcessOptions) then
if Not (poStderrToOutput in FProcessOptions) then
FStderrStream:=TInputPipeStream.Create(ErrHandle);
FStderrStream:=TInputPipeStream.Create(ErrHandle);
end;
end;


procedure TProcess.FreeStream(var AStream: THandleStream);
procedure TProcess.FreeStream(var AStream: THandleStream);
begin
begin
if AStream = nil then exit;
if AStream = nil then exit;
FreeAndNil(AStream);
FreeAndNil(AStream);
end;
end;


procedure TProcess.Loaded;
procedure TProcess.Loaded;
begin
begin
inherited Loaded;
inherited Loaded;
If (csDesigning in ComponentState) and (FCommandLine<>'') then
If (csDesigning in ComponentState) and (FCommandLine<>'') then
ConvertCommandLine;
ConvertCommandLine;
end;
end;


procedure TProcess.CloseInput;
procedure TProcess.CloseInput;
begin
begin
FreeStream(THandleStream(FInputStream));
FreeStream(THandleStream(FInputStream));
end;
end;


procedure TProcess.CloseOutput;
procedure TProcess.CloseOutput;
begin
begin
FreeStream(THandleStream(FOutputStream));
FreeStream(THandleStream(FOutputStream));
end;
end;


procedure TProcess.CloseStderr;
procedure TProcess.CloseStderr;
begin
begin
FreeStream(THandleStream(FStderrStream));
FreeStream(THandleStream(FStderrStream));
end;
end;


Procedure TProcess.SetWindowColumns (Value : Cardinal);
Procedure TProcess.SetWindowColumns (Value : Cardinal);


begin
begin
if Value<>0 then
if Value<>0 then
Include(FStartupOptions,suoUseCountChars);
Include(FStartupOptions,suoUseCountChars);
dwXCountChars:=Value;
dwXCountChars:=Value;
end;
end;




Procedure TProcess.SetWindowHeight (Value : Cardinal);
Procedure TProcess.SetWindowHeight (Value : Cardinal);


begin
begin
if Value<>0 then
if Value<>0 then
include(FStartupOptions,suoUsePosition);
include(FStartupOptions,suoUsePosition);
dwYSize:=Value;
dwYSize:=Value;
end;
end;


Procedure TProcess.SetWindowLeft (Value : Cardinal);
Procedure TProcess.SetWindowLeft (Value : Cardinal);


begin
begin
if Value<>0 then
if Value<>0 then
Include(FStartupOptions,suoUseSize);
Include(FStartupOptions,suoUseSize);
dwx:=Value;
dwx:=Value;
end;
end;


Procedure TProcess.SetWindowTop (Value : Cardinal);
Procedure TProcess.SetWindowTop (Value : Cardinal);


begin
begin
if Value<>0 then
if Value<>0 then
Include(FStartupOptions,suoUsePosition);
Include(FStartupOptions,suoUsePosition);
dwy:=Value;
dwy:=Value;
end;
end;


Procedure TProcess.SetWindowWidth (Value : Cardinal);
Procedure TProcess.SetWindowWidth (Value : Cardinal);
begin
begin
If (Value<>0) then
If (Value<>0) then
Include(FStartupOptions,suoUseSize);
Include(FStartupOptions,suoUseSize);
dwXSize:=Value;
dwXSize:=Value;
end;
end;


Function TProcess.GetWindowRect : TRect;
Function TProcess.GetWindowRect : TRect;
begin
begin
With Result do
With Result do
begin
begin
Left:=dwx;
Left:=dwx;
Right:=dwx+dwxSize;
Right:=dwx+dwxSize;
Top:=dwy;
Top:=dwy;
Bottom:=dwy+dwysize;
Bottom:=dwy+dwysize;
end;
end;
end;
end;


procedure TProcess.SetCommandLine(const AValue: String);
procedure TProcess.SetCommandLine(const AValue: String);
begin
begin
if FCommandLine=AValue then exit;
if FCommandLine=AValue then exit;
FCommandLine:=AValue;
FCommandLine:=AValue;
If Not (csLoading in ComponentState) then
If Not (csLoading in ComponentState) then
ConvertCommandLine;
ConvertCommandLine;
end;
end;


procedure TProcess.SetParameters(const AValue: TStrings);
procedure TProcess.SetParameters(const AValue: TStrings);
begin
begin
FParameters.Assign(AValue);
FParameters.Assign(AValue);
end;
end;


Procedure TProcess.SetWindowRect (Value : Trect);
Procedure TProcess.SetWindowRect (Value : Trect);
begin
begin
Include(FStartupOptions,suoUseSize);
Include(FStartupOptions,suoUseSize);
Include(FStartupOptions,suoUsePosition);
Include(FStartupOptions,suoUsePosition);
With Value do
With Value do
begin
begin
dwx:=Left;
dwx:=Left;
dwxSize:=Right-Left;
dwxSize:=Right-Left;
dwy:=Top;
dwy:=Top;
dwySize:=Bottom-top;
dwySize:=Bottom-top;
end;
end;
end;
end;




Procedure TProcess.SetWindowRows (Value : Cardinal);
Procedure TProcess.SetWindowRows (Value : Cardinal);


begin
begin
if Value<>0 then
if Value<>0 then
Include(FStartupOptions,suoUseCountChars);
Include(FStartupOptions,suoUseCountChars);
dwYCountChars:=Value;
dwYCountChars:=Value;
end;
end;


procedure TProcess.SetApplicationName(const Value: String);
procedure TProcess.SetApplicationName(const Value: String);
begin
begin
FApplicationName := Value;
FApplicationName := Value;
If (csDesigning in ComponentState) and
If (csDesigning in ComponentState) and
(FCommandLine='') then
(FCommandLine='') then
FCommandLine:=Value;
FCommandLine:=Value;
end;
end;


procedure TProcess.SetProcessOptions(const Value: TProcessOptions);
procedure TProcess.SetProcessOptions(const Value: TProcessOptions);
begin
begin
FProcessOptions := Value;
FProcessOptions := Value;
If poNewConsole in FProcessOptions then
If poNewConsole in FProcessOptions then
Exclude(FProcessOptions,poNoConsole);
Exclude(FProcessOptions,poNoConsole);
if poRunSuspended in FProcessOptions then
if poRunSuspended in FProcessOptions then
Exclude(FProcessOptions,poWaitOnExit);
Exclude(FProcessOptions,poWaitOnExit);
end;
end;


procedure TProcess.SetActive(const Value: Boolean);
procedure TProcess.SetActive(const Value: Boolean);
begin
begin
if (Value<>GetRunning) then
if (Value<>GetRunning) then
If Value then
If Value then
Execute
Execute
else
else
Terminate(0);
Terminate(0);
end;
end;


procedure TProcess.SetEnvironment(const Value: TStrings);
procedure TProcess.SetEnvironment(const Value: TStrings);
begin
begin
FEnvironment.Assign(Value);
FEnvironment.Assign(Value);
end;
end;


procedure TProcess.ConvertCommandLine;
procedure TProcess.ConvertCommandLine;
begin
begin
FParameters.Clear;
FParameters.Clear;
CommandToList(FCommandLine,FParameters);
CommandToList(FCommandLine,FParameters);
If FParameters.Count>0 then
If FParameters.Count>0 then
begin
begin
Executable:=FParameters[0];
Executable:=FParameters[0];
FParameters.Delete(0);
FParameters.Delete(0);
end;
end;
end;
end;


Const
Const
READ_BYTES = 65536; // not too small to avoid fragmentation when reading large files.
READ_BYTES = 65536; // not too small to avoid fragmentation when reading large files.


Function ReadInputPipe(const Pipe: TInputPipeStream; var BytesRead,OutputLen: integer; var OutputString: string): Boolean;
var
available : integer;
NumBytes : integer;
begin
Result := False; available := 0;
if(BytesRead<0)then BytesRead := 0;
if(OutputLen<0)then OutputLen := 0;
repeat
if(Pipe=nil)then exit;
if(Pipe.NumBytesAvailable>0)
then available := Pipe.NumBytesAvailable
else break;
if(BytesRead+available>OutputLen)then
begin
OutputLen := BytesRead+READ_BYTES;
SetLength(OutputString,OutputLen);
end;
try
try
NumBytes := Pipe.Read(OutputString[1+BytesRead], available);
if(NumBytes>0)
then Inc(BytesRead, NumBytes);
except
Result := False;
end;
finally
Result := True;
end;
until false;
end;

// helperfunction that does the bulk of the work.
// helperfunction that does the bulk of the work.
// We need to also collect stderr output in order to avoid
// We need to also collect stderr output in order to avoid
// lock out if the stderr pipe is full.
// lock out if the stderr pipe is full.
function internalRuncommand(p:TProcess;var outputstring:string;
function internalruncommand(var P: TProcess; var Stdout, Stderr: string; var ExitStatus:integer): integer;
var stderrstring:string; var exitstatus:integer):integer;
var
var
bytesread_stdout : integer;
numbytes,bytesread,available : integer;
outputlen_stdout : integer;
outputlength, stderrlength : integer;
bytesread_stderr : integer;
stderrnumbytes,stderrbytesread : integer;
outputlen_stderr : integer;
begin
begin
result:=-1;
Result := -1; if(P=nil)then exit;
try
try
try
try
p.Options := [poUsePipes];
P.Options := [poUsePipes];
bytesread:=0;
bytesread_stdout := 0;
outputlength:=0;
outputlen_stdout := 0;
stderrbytesread:=0;
bytesread_stderr := 0;
stderrlength:=0;
outputlen_stderr := 0;
p.Execute;
P.Execute;
while p.Running do
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;
if(not ReadInputPipe(P.Output, bytesread_stdout, outputlen_stdout, Stdout))and
setlength(outputstring,BytesRead);
(not ReadInputPipe(P.Stderr, bytesread_stderr, outputlen_stderr, Stderr))
then sleep(100);
end;
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;
end;
finally
finally
p.free;
FreeAndNil(P);
end;
SetLength(Stdout,bytesread_stdout);
end;
SetLength(Stderr,bytesread_stderr);
end;
end;



{ Functions without StderrString }
{ Functions without StderrString }


function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string;var exitstatus:integer):integer;
function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string;var exitstatus:integer):integer;
Var
Var
p : TProcess;
p : TProcess;
i : integer;
i : integer;
ErrorString : String;
ErrorString : String;
begin
begin
p:=TProcess.create(nil);
p:=TProcess.create(nil);
p.Executable:=exename;
p.Executable:=exename;
if curdir<>'' then
if curdir<>'' then
p.CurrentDirectory:=curdir;
p.CurrentDirectory:=curdir;
if high(commands)>=0 then
if high(commands)>=0 then
for i:=low(commands) to high(commands) do
for i:=low(commands) to high(commands) do
p.Parameters.add(commands[i]);
p.Parameters.add(commands[i]);
result:=internalruncommand(p,outputstring,errorstring,exitstatus);
result:=internalruncommand(p,outputstring,errorstring,exitstatus);
end;
end;


function RunCommandInDir(const curdir,cmdline:string;var outputstring:string):boolean; deprecated;
function RunCommandInDir(const curdir,cmdline:string;var outputstring:string):boolean; deprecated;
Var
Var
p : TProcess;
p : TProcess;
exitstatus : integer;
exitstatus : integer;
ErrorString : String;
ErrorString : String;
begin
begin
p:=TProcess.create(nil);
p:=TProcess.create(nil);
p.setcommandline(cmdline);
p.setcommandline(cmdline);
if curdir<>'' then
if curdir<>'' then
p.CurrentDirectory:=curdir;
p.CurrentDirectory:=curdir;
result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
if exitstatus<>0 then result:=false;
if exitstatus<>0 then result:=false;
end;
end;


function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string):boolean;
function RunCommandIndir(const curdir:string;const exename:string;const commands:array of string;var outputstring:string):boolean;
Var
Var
p : TProcess;
p : TProcess;
i,
i,
exitstatus : integer;
exitstatus : integer;
ErrorString : String;
ErrorString : String;
begin
begin
p:=TProcess.create(nil);
p:=TProcess.create(nil);
p.Executable:=exename;
p.Executable:=exename;
if curdir<>'' then
if curdir<>'' then
p.CurrentDirectory:=curdir;
p.CurrentDirectory:=curdir;
if high(commands)>=0 then
if high(commands)>=0 then
for i:=low(commands) to high(commands) do
for i:=low(commands) to high(commands) do
p.Parameters.add(commands[i]);
p.Parameters.add(commands[i]);
result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
if exitstatus<>0 then result:=false;
if exitstatus<>0 then result:=false;
end;
end;


function RunCommand(const cmdline:string;var outputstring:string):boolean; deprecated;
function RunCommand(const cmdline:string;var outputstring:string):boolean; deprecated;
Var
Var
p : TProcess;
p : TProcess;
exitstatus : integer;
exitstatus : integer;
ErrorString : String;
ErrorString : String;
begin
begin
p:=TProcess.create(nil);
p:=TProcess.create(nil);
p.setcommandline(cmdline);
p.setcommandline(cmdline);
result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
if exitstatus<>0 then result:=false;
if exitstatus<>0 then result:=false;
end;
end;


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


function RunCommand(const exename:string;const commands:array of string;var stdout:string):boolean;
var
stderr : string;
begin
Result := RunCommand(exename, commands, stdout,stderr);
end;


end.
end.