-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConsoleProcess.pp
More file actions
270 lines (241 loc) · 6.99 KB
/
Copy pathConsoleProcess.pp
File metadata and controls
270 lines (241 loc) · 6.99 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
unit ConsoleProcess;
{-----------------------------------------------------------------------------
Author: Zaher Dirkey
Purpose:
License: mit(https://opensource.org/licenses/MIT)
-----------------------------------------------------------------------------}
{$mode objfpc}{$H+}
interface
uses
Forms, Classes, SysUtils, process,
mnUtils, mnStreams;
type
TmnConsoleThread = class;
{ TExecuteObject }
TExecuteObject =class(TObject)
public
procedure Prepare(const ConsoleThread: TmnConsoleThread); virtual; abstract;
procedure Execute(const ConsoleThread: TmnConsoleThread); virtual; abstract;
procedure Unprepare(const ConsoleThread: TmnConsoleThread); virtual;
end;
TmnLogKind = (lgLog, lgStatus, lgDone, lgMessage);
TmnOnLog = procedure(S: String; Kind: TmnLogKind) of object;
{ TmnConsoleThread }
TmnConsoleThread = class(TThread)
private
FEnvironment: TStrings;
FExecutable: string;
FCurrentDirectory: string;
FParameters: string;
FExecuteObject: TExecuteObject;
FOnLog: TmnOnLog;
FPassword: string;
FProcess: TProcess;
procedure SetExecuteObject(AValue: TExecuteObject);
protected
StreamWrapper: TmnWrapperStream;
FString: String;
FKind: TmnLogKind;
procedure DoOnLog; virtual; //To Sync
public
Status: Integer;
IgnoreError: Boolean;
Message: string;
constructor Create(vExecutable, vCurrentDirectory, vParameters: string; vOnLog: TmnOnLog = nil);
destructor Destroy; override;
procedure Kill;
procedure Execute; override;
procedure ReadPrompt; virtual;
procedure ReadStream; virtual;
procedure Log(S: String; Kind: TmnLogKind = lgLog);
property OnLog: TmnOnLog read FOnLog write FOnLog;
property Password: string read FPassword write FPassword;
property Environment: TStrings read FEnvironment;
property ExecuteObject: TExecuteObject read FExecuteObject write SetExecuteObject;
end;
implementation
{ TExecuteObject }
procedure TExecuteObject.Unprepare(const ConsoleThread: TmnConsoleThread);
begin
end;
{ TmnConsoleThread }
procedure TmnConsoleThread.SetExecuteObject(AValue: TExecuteObject);
begin
if FExecuteObject <> AValue then
FExecuteObject :=AValue;
end;
procedure TmnConsoleThread.DoOnLog;
begin
if Assigned(FOnLog) then
FOnLog(FString, FKind);
end;
procedure TmnConsoleThread.Log(S: String; Kind: TmnLogKind);
begin
FString := S;
FKind := Kind;
Synchronize(@DoOnLog);
FString := '';
end;
constructor TmnConsoleThread.Create(vExecutable, vCurrentDirectory, vParameters: string; vOnLog: TmnOnLog);
begin
inherited Create(True);
FreeOnTerminate := True;
FOnLog := vOnLog;
FEnvironment := TStringList.Create();
FExecutable:= vExecutable;
FCurrentDirectory := vCurrentDirectory;
FParameters := vParameters;
end;
destructor TmnConsoleThread.Destroy;
begin
FreeAndNil(FExecuteObject);
FreeAndNil(FEnvironment);
inherited Destroy;
end;
procedure TmnConsoleThread.Kill;
begin
if FProcess <> nil then
begin
//FProcess. //send sginal ctrl+c
FProcess.Terminate(1);
end;
end;
{procedure TmnConsoleThread.Read;
var
T: String;
aBuffer: array[0..79] of AnsiChar;
function ReadNow(out C: DWORD): Boolean;
begin
if (FProcess.Output.NumBytesAvailable > 0) then
C := FProcess.Output.Read(aBuffer, SizeOf(aBuffer))
else
C := 0;
Result := C > 0;
end;
var
C: DWORD;
begin
aBuffer := '';
while FProcess.Running and not Terminated do
begin
if ReadNow(C) then
begin
SetString(T, aBuffer, C);
if T <> '' then
Log(T);
end;
end;
end;}
procedure TmnConsoleThread.ReadPrompt;
var
T: String;
aBuffer: array[0..79] of AnsiChar;
C: DWORD;
begin
aBuffer := '';
C := FProcess.Output.Read(aBuffer, SizeOf(aBuffer));
if C <> 0 then
begin
SetString(T, aBuffer, C);
Log(T);
end;
end;
procedure TmnConsoleThread.ReadStream;
var
S: utf8string;
b: Boolean;
begin
if FProcess.Output <> nil then
begin
try
while not Terminated do
begin
b := StreamWrapper.ReadUTF8Line(S, False);
if not b and not (FProcess.Running) then
break;
Log(S);
end;
except
on e: Exception do
begin
if FProcess.Running and Terminated then
FProcess.Terminate(0);
end;
end;
end;
end;
procedure TmnConsoleThread.Execute;
procedure StreamWriteLn(S: string);
var
i : integer;
begin
for i := 1 to Length(S) do
FProcess.Input.WriteByte(Byte(S[i]));
FProcess.Input.WriteByte(13);
FProcess.Input.WriteByte(10);
end;
var
i: Integer;
begin
FProcess := TProcess.Create(nil);
FProcess.CurrentDirectory := FCurrentDirectory;
FProcess.Executable := FExecutable;
CommandToList(FParameters, FProcess.Parameters);
FProcess.Options := [poUsePipes, poStderrToOutPut, poNoConsole{, poDetached}]; //do not use poDetached with pg_ctl
FProcess.ShowWindow := swoHide;
FProcess.ConsoleTitle := 'Console';
FProcess.InheritHandles := True;
FProcess.CurrentDirectory := Application.Location;
FProcess.StartupOptions := [suoUseShowWindow]; //<- need it in linux to hide window
if (Environment.Count > 0) then //if there is one, we need to copy system environments
begin
for i := 0 to GetEnvironmentVariableCount -1 do
FProcess.Environment.Add(GetEnvironmentString(i));
FProcess.Environment.AddStrings(Environment);
// Examples
//ConsoleThread.Environment.Add('PGPASSWORD=sss');
//ConsoleThread.Environment.Add('LANG=ru_RU.UTF-8');
//ConsoleThread.Environment.Add('PYTHONIOENCODING=utf-8');
end;
Status := 0;
try
try
if FExecuteObject <> nil then
FExecuteObject.Prepare(Self);
Log(Message, lgStatus);
Log(FProcess.Executable + ' ' + StringReplace(FProcess.Parameters.Text, #13#10, ' ', [rfReplaceAll]));
FProcess.Execute;
StreamWrapper := TmnWrapperStream.Create(FProcess.Output, False);
StreamWrapper.EndOfLine := #10;
ReadPrompt;
if FProcess.Running and (FProcess.Input <> nil) and (Password <> '') then
begin
StreamWriteLn(Password);
// StreamWriteLn(Password);
end;
FProcess.CloseInput;
if FProcess.Running then
ReadStream;
Status := FProcess.ExitStatus;
FreeAndNil(FProcess);
if (IgnoreError or (Status = 0)) and (FExecuteObject <> nil) then
FExecuteObject.Execute(Self);
except
on E: Exception do
begin
Log(E.Message, lgMessage);
raise;
end;
end;
finally
//Log('Execute Time: "' + TicksToString(GetTickCount64 - d) + '" with Status: ' + IntToStr(Status) );
Log(Message + ' return: ' + IntToStr(Status) );
end;
if FProcess <> nil then
begin
FProcess.WaitOnExit;
FreeAndNil(StreamWrapper);
FreeAndNil(FProcess);
end;
end;
end.