| 1 |
{%MainUnit castleopendocument.pas} |
| 2 |
{ |
| 3 |
Copyright 2012-2024 Michalis Kamburelis and Lazarus developers. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
Parts of this file are based on Lazarus LCL code, which has |
| 10 |
exactly the same license as our "Castle Game Engine": |
| 11 |
LGPL with static linking exception, see COPYING.txt for details. |
| 12 |
|
| 13 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
|
| 17 |
---------------------------------------------------------------------------- |
| 18 |
} |
| 19 |
|
| 20 |
{ RunCmdFromPath (using TProcess on FPC, or Unix-specific System call on Delphi). |
| 21 |
This is used by OpenUrl and/or OpenDocument on some platforms. } |
| 22 |
|
| 23 |
{$ifdef read_uses} |
| 24 |
// With Delphi, this will compile only on platforms that have Posix.Stdlib, and that's good. |
| 25 |
{$ifdef FPC} BaseUnix, Process, {$else} Posix.Stdlib, {$endif} |
| 26 |
{$endif} |
| 27 |
|
| 28 |
{$ifdef read_implementation} |
| 29 |
|
| 30 |
{ Based on Lazarus LCL: |
| 31 |
lcl/lclstrconsts.pas |
| 32 |
lcl/include/unixfileutil.inc |
| 33 |
lcl/utf8process.pp |
| 34 |
lcl/include/sysenvapis_unix.inc |
| 35 |
} |
| 36 |
|
| 37 |
function FileIsExecutable(const AFilename: string): boolean; |
| 38 |
{$ifdef FPC} |
| 39 |
var |
| 40 |
Info: Stat; |
| 41 |
{$endif} |
| 42 |
begin |
| 43 |
{$ifdef FPC} |
| 44 |
// First check AFilename is not a directory and then check if executable |
| 45 |
Result:= (FpStat(AFilename,info)<>-1) and FPS_ISREG(info.st_mode) and |
| 46 |
(BaseUnix.FpAccess(AFilename,BaseUnix.X_OK)=0); |
| 47 |
{$else} |
| 48 |
// For now, in Delphi, just assume it's executable. |
| 49 |
Result := true; |
| 50 |
{$endif} |
| 51 |
end; |
| 52 |
|
| 53 |
{$ifdef FPC} |
| 54 |
|
| 55 |
resourcestring |
| 56 |
lisProgramFileNotFound = 'program file not found %s'; |
| 57 |
lisCanNotExecute = 'can not execute %s'; |
| 58 |
|
| 59 |
{ Runs a short command which should point to an executable in |
| 60 |
the environment PATH. |
| 61 |
|
| 62 |
CGE: simplified this to use TProcess.Executable, TProcess.Parameters |
| 63 |
instead of TProcess.CommandLine (Lazarus TProcessUTF8 didn't have these |
| 64 |
improvements). This removes the need for paranoid quoting of strings |
| 65 |
everywhere. |
| 66 |
This always takes exactly 1 parameter now --- which is actually Ok |
| 67 |
for usage in this unit. } |
| 68 |
procedure RunCmdFromPath(ProgramFilename, Parameter: string); |
| 69 |
var |
| 70 |
OldProgramFilename: String; |
| 71 |
BrowserProcess: TProcess; |
| 72 |
begin |
| 73 |
OldProgramFilename:=ProgramFilename; |
| 74 |
ProgramFilename:=PathFileSearch(ProgramFilename); |
| 75 |
|
| 76 |
if ProgramFilename='' then |
| 77 |
raise EFOpenError.Create(Format(lisProgramFileNotFound, [OldProgramFilename])); |
| 78 |
if not FileIsExecutable(ProgramFilename) then |
| 79 |
raise EFOpenError.Create(Format(lisCanNotExecute, [ProgramFilename])); |
| 80 |
|
| 81 |
// run |
| 82 |
BrowserProcess := TProcess.Create(nil); |
| 83 |
try |
| 84 |
BrowserProcess.Executable := ProgramFilename; |
| 85 |
BrowserProcess.Parameters.Add(Parameter); |
| 86 |
|
| 87 |
WritelnLog('Executing', 'Executable: "' + ProgramFilename + |
| 88 |
'", Parameter: "' + Parameter + '"'); |
| 89 |
|
| 90 |
BrowserProcess.Execute; |
| 91 |
finally |
| 92 |
BrowserProcess.Free; |
| 93 |
end; |
| 94 |
end; |
| 95 |
|
| 96 |
{$else} |
| 97 |
|
| 98 |
procedure RunCmdFromPath(const ProgramFilename, Parameter: string); |
| 99 |
var |
| 100 |
Command: AnsiString; |
| 101 |
begin |
| 102 |
if (Pos('"', ProgramFilename) <> 0) or |
| 103 |
(Pos('"', Parameter) <> 0) then |
| 104 |
begin |
| 105 |
WritelnWarning('RunCmdFromPath', 'Not implemented for the case when ProgramFilename or Parameter contain a double quote'); |
| 106 |
Exit; |
| 107 |
end; |
| 108 |
|
| 109 |
Command := Format('"%s" "%s"', [ProgramFilename, Parameter]); |
| 110 |
_System(PAnsiChar(Command)); |
| 111 |
end; |
| 112 |
|
| 113 |
{$endif} |
| 114 |
|
| 115 |
{$endif read_implementation} |