Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CurrentDirectory to JclSysUtils.Execute #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions jcl/source/common/JclSysUtils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -500,29 +500,33 @@ function IntToStrZeroPad(Value, Count: Integer): string;

function Execute(const CommandLine: string; OutputLineCallback: TTextHandler; RawOutput: Boolean = False;
AbortPtr: PBoolean = nil; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
OutputLineCallback: TTextHandler; RawOutput: Boolean = False; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; var Output: string; RawOutput: Boolean = False;
AbortPtr: PBoolean = nil; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
var Output: string; RawOutput: Boolean = False; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;

function Execute(const CommandLine: string; OutputLineCallback, ErrorLineCallback: TTextHandler;
RawOutput: Boolean = False; RawError: Boolean = False; AbortPtr: PBoolean = nil;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
OutputLineCallback, ErrorLineCallback: TTextHandler; RawOutput: Boolean = False; RawError: Boolean = False;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; var Output, Error: string;
RawOutput: Boolean = False; RawError: Boolean = False; AbortPtr: PBoolean = nil;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
var Output, Error: string; RawOutput: Boolean = False; RawError: Boolean = False;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;

type
{$IFDEF MSWINDOWS}
Expand All @@ -548,6 +552,7 @@ function Execute(const CommandLine: string; AbortEvent: TJclEvent;
FCreateProcessFlags: DWORD;
FStartupVisibility: TStartupVisibility;
FBeforeResume: TJclExecuteCmdProcessOptionBeforeResumeEvent;
FCurrentDirectory: string;
{$ENDIF MSWINDOWS}

FExitCode: Cardinal;
Expand All @@ -573,6 +578,7 @@ function Execute(const CommandLine: string; AbortEvent: TJclEvent;
property CreateProcessFlags: DWORD read FCreateProcessFlags write FCreateProcessFlags;
property StartupVisibility: TStartupVisibility read FStartupVisibility write FStartupVisibility;
property BeforeResume: TJclExecuteCmdProcessOptionBeforeResumeEvent read FBeforeResume write FBeforeResume;
property CurrentDirectory: string read FCurrentDirectory write FCurrentDirectory;
{$ENDIF MSWINDOWS}

// out:
Expand Down Expand Up @@ -3009,6 +3015,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
CommandLine: string;
AbortPtr: PBoolean;
Flags: DWORD;
PCurrentDirectory: PChar;
begin
Result := False;

Expand Down Expand Up @@ -3064,6 +3071,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
UniqueString(CommandLine); // CommandLine must be in a writable memory block
ResetMemory(ProcessInfo, SizeOf(ProcessInfo));
ProcessEvent := nil;
PCurrentDirectory := Iff(Length(Options.CurrentDirectory) = 0, nil, PChar(Options.CurrentDirectory));
try
Flags := Options.CreateProcessFlags and not (NORMAL_PRIORITY_CLASS or IDLE_PRIORITY_CLASS or
HIGH_PRIORITY_CLASS or REALTIME_PRIORITY_CLASS);
Expand All @@ -3072,7 +3080,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
Flags := Flags or CREATE_SUSPENDED;

if CreateProcess(nil, PChar(CommandLine), nil, nil, True, Flags,
nil, nil, StartupInfo, ProcessInfo) then
nil, PCurrentDirectory, StartupInfo, ProcessInfo) then
begin
Result := True;
try
Expand Down Expand Up @@ -3266,7 +3274,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
function InternalExecute(CommandLine: string; AbortPtr: PBoolean; AbortEvent: TJclEvent;
var Output: string; OutputLineCallback: TTextHandler; RawOutput: Boolean;
MergeError: Boolean; var Error: string; ErrorLineCallback: TTextHandler; RawError: Boolean;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean; const CurrentDirectory: string): Cardinal;
var
Options: TJclExecuteCmdProcessOptions;
begin
Expand All @@ -3282,6 +3290,7 @@ function InternalExecute(CommandLine: string; AbortPtr: PBoolean; AbortEvent: TJ
Options.ErrorLineCallback := ErrorLineCallback;
Options.RawError := RawError;
Options.ProcessPriority := ProcessPriority;
Options.CurrentDirectory := CurrentDirectory;

ExecuteCmdProcess(Options);

Expand All @@ -3300,93 +3309,99 @@ function InternalExecute(CommandLine: string; AbortPtr: PBoolean; AbortEvent: TJ
That is, for RawOutput = False, lines not terminated by a line feed (#10) are deleted from Output. }

function Execute(const CommandLine: string; var Output: string; RawOutput: Boolean;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Error: string;
begin
Error := '';
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, nil, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; var Output: string; RawOutput: Boolean;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean; const CurrentDirectory: string): Cardinal;
var
Error: string;
begin
Error := '';
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, nil, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

{ TODO -cHelp :
Author: Robert Rossmair
OutputLineCallback called once per line of output. }

function Execute(const CommandLine: string; OutputLineCallback: TTextHandler; RawOutput: Boolean;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, OutputLineCallback, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; OutputLineCallback: TTextHandler; RawOutput: Boolean;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean; const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, OutputLineCallback, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

{ TODO -cHelp :
RawOutput: Do not process isolated carriage returns (#13).
That is, for RawOutput = False, lines not terminated by a line feed (#10) are deleted from Output. }

function Execute(const CommandLine: string; var Output, Error: string; RawOutput, RawError: Boolean;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
begin
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, nil, RawOutput, False, Error,
nil, RawError, ProcessPriority, AutoConvertOem);
nil, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; var Output, Error: string;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
begin
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, nil, RawOutput, False, Error,
nil, RawError, ProcessPriority, AutoConvertOem);
nil, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

{ TODO -cHelp :
Author: Robert Rossmair
OutputLineCallback called once per line of output. }

function Execute(const CommandLine: string; OutputLineCallback, ErrorLineCallback: TTextHandler;
RawOutput, RawError: Boolean; AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
RawOutput, RawError: Boolean; AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, OutputLineCallback, RawOutput, False, Error,
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem);
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; OutputLineCallback, ErrorLineCallback: TTextHandler;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, OutputLineCallback, RawOutput, False, Error,
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem);
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

//=== { TJclCommandLineTool } ================================================
Expand Down