1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
{
Dummy process.inc - the simplest version based on SysUtils.ExecuteProcess
}
uses
Exec, AmigaDos, Utility;
Resourcestring
SNoCommandLine = 'Cannot execute empty command-line';
SErrCannotExecute = 'Failed to execute %s : %d';
SErrNoSuchProgram = 'Executable not found: "%s"';
procedure TProcess.CloseProcessHandles;
begin
end;
Function TProcess.PeekExitStatus : Boolean;
begin
Result := true; (* Dummy version assumes always synchronous execution *)
end;
function GetNextWordPos (const S: string): integer;
const
WhiteSpace = [' ', #9, #10, #13];
Literals = ['"', ''''];
var
WStart: integer;
InLiteral: boolean;
LastLiteral: char;
begin
WStart := 1;
(* Skip whitespaces at the beginning *)
while (WStart <= Length (S)) and (S [WStart] in WhiteSpace) do
Inc (WStart);
InLiteral := false;
LastLiteral := #0;
while (WStart <= Length (S)) and
(not (S [WStart] in WhiteSpace) or InLiteral) do
begin
if S [WStart] in Literals then
if InLiteral then
InLiteral := not (S [WStart] = LastLiteral)
else
begin
InLiteral := true;
LastLiteral := S [WStart];
end;
Inc (WStart);
end;
(* Skip whitespaces at the end *)
while (WStart <= Length (S)) and (S [WStart] in WhiteSpace) do
Inc (WStart);
Result := WStart;
end;
function MaybeQuote (const S: string): string;
begin
if (Pos (' ', S) <> 0) then
Result := '"' + S + '"'
else
Result := S;
end;
var
UID: Integer = 0;
{$ifdef MorphOS}
const
BUF_LINE = 0; // flush on \n, etc
BUF_FULL = 1; // never flush except when needed
BUF_NONE = 2; // no buffering
{$endif}
Procedure TProcess.Execute;
var
I: integer;
ExecName, FoundName: string;
E2: EProcess;
OrigDir: string;
Params: string;
TempName: string;
cos: BPTR;
{$ifdef MorphOS}
inA, inB, OutA, OutB: BPTR;
Res: Integer;
{$endif}
begin
if (ApplicationName = '') and (CommandLine = '') and (Executable = '') then
raise EProcess.Create (SNoCommandline);
if (FApplicationName <> '') then
ExecName := FApplicationName;
if (FCommandLine <> '') then
begin
Params := FCommandLine;
if ExecName = '' then
begin
I := GetNextWordPos (Params);
ExecName := Copy (Params, 1, Pred (I));
ExecName := Trim (ExecName);
Delete (Params, 1, Pred (I));
end
else if Copy (FCommandLine, 1, Length (ExecName)) = ExecName then
Delete (Params, 1, Succ (Length (ExecName)))
else
Delete (Params, 1, Pred (GetNextWordPos (Params)));
Params := Trim (Params);
end
else
for I := 0 to Pred (Parameters.Count) do
Params := Params + ' ' + MaybeQuote (Parameters [I]);
if (FExecutable <> '') and (ExecName = '') then
ExecName := Executable;
if not FileExists (ExecName) then
begin
FoundName := ExeSearch (ExecName, '');
if FoundName <> '' then
ExecName := FoundName
else
raise EProcess.CreateFmt (SErrNoSuchProgram, [ExecName]);
end;
if (FCurrentDirectory <> '') then
begin
GetDir (0, OrigDir);
ChDir (FCurrentDirectory);
end;
try
{$ifdef MorphOS}
if (poUsePipes in Options) and (not (poWaitOnExit in Options)) then
begin
FProcessID := 0;
// Pipenames, should be unique
TempName := 'PIPE:PrO_' + HexStr(Self) + HexStr(GetTickCount, 8);
inA := DOSOpen(PChar(TempName), MODE_OLDFILE);
inB := DOSOpen(PChar(TempName), MODE_NEWFILE);
TempName := TempName + 'o';
outA := DOSOpen(PChar(TempName), MODE_OLDFILE);
outB := DOSOpen(PChar(TempName), MODE_NEWFILE);
// set buffer for all pipes
SetVBuf(inA, nil, BUF_NONE, -1);
SetVBuf(inB, nil, BUF_LINE, -1);
SetVBuf(outA, nil, BUF_NONE, -1);
SetVBuf(outB, nil, BUF_LINE, -1);
// the actual Start of the command with given parameter and streams
Res := SystemTags(PChar(ExecName + ' ' + Params),
[SYS_Input, AsTag(outA),
SYS_Output, AsTag(inB),
SYS_Asynch, AsTag(True),
TAG_END]);
// the two streams will be destroyed by system, we do not need to care about
// the other two we will destroy when the PipeStreams they are attached to are destroyed
if Res <> -1 then
begin
FProcessID := 1;
CreateStreams(THandle(outB), THandle(inA),0);
end
else
begin
// if the command did not start, we need to delete all Streams
if outB <> BPTR(0) then DosClose(outB);
if outA <> BPTR(0) then DosClose(outA);
if inB <> BPTR(0) then DosClose(inB);
if inA <> BPTR(0) then DosClose(inA);
end;
end
else
{$endif}
begin
// if no streams needed we still use the old sychronous way
FProcessID := 0;
cos := BPTR(0);
repeat
Inc(UID);
TempName := 'T:PrO_'+ HexStr(FindTask(nil)) + '_' + IntToHex(UID,8);
until not FileExists(TempName);
//sysdebugln('TProcess start: "' + ExecName + ' ' + Params+'" >' + TempName);
cos := AmigaDos.DosOpen(PChar(TempName), MODE_READWRITE);
FExitCode := LongInt(amigados.Execute(PChar(ExecName + ' ' + Params), BPTR(0), cos));
DosSeek(cos, 0, OFFSET_BEGINNING);
CreateStreams(0, THandle(cos),0);
end;
//FExitCode := ExecuteProcess (ExecName, Params);
except
(* Normalize the raised exception so that it is aligned to other platforms. *)
On E: EOSError do
begin
raise EProcess.CreateFmt (SErrCannotExecute, [FCommandLine, E.ErrorCode]);
if (FCurrentDirectory <> '') then
ChDir (OrigDir);
end;
end;
if (FCurrentDirectory <> '') then
ChDir (OrigDir);
end;
Function TProcess.WaitOnExit : Boolean;
begin
Result:=True;
end;
Function TProcess.WaitOnExit(Timeout : DWord) : Boolean;
begin
Result:=True;
end;
Function TProcess.Suspend : Longint;
begin
Result:=0;
end;
Function TProcess.Resume : LongInt;
begin
Result:=0;
end;
Function TProcess.Terminate(AExitCode : Integer) : Boolean;
begin
Result:=False;
end;
Procedure TProcess.SetShowWindow (Value : TShowWindowOptions);
begin
end;
|