File: processbody.inc

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (1232 lines) | stat: -rw-r--r-- 36,088 bytes parent folder | download
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
{%MainUnit process.pp}
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 1999-2000 by the Free Pascal development team

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

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

 **********************************************************************}

Type
   {$ifdef processunicodestring}
   TProcessString = Unicodestring;
   TprocessChar   = UnicodeChar;

   { TProcessStrings }

   TProcessStrings = Class(TPersistent)
                private
                   name   : array of unicodestring;
                   function getcount: Integer;
                   function getname( index: integer): Unicodestring;
                public
                   procedure AssignTo(Dest: TPersistent); override;
                   procedure add(const s : Unicodestring);
                   procedure Clear;
                   procedure Delete(i:integer);

                   property Names[ index:integer]:Unicodestring read getname; default;
                   property Count : Integer read getcount;
   end;
   TProcessStringList = TProcessStrings;
   {$else}
   TprocessChar   = Char;
   TProcessString = String;
   TProcessStrings = TStrings;
   TProcessStringList = TStringList;
   {$endif}

   TFileWriteMode = (fwmTruncate, fwmAppend, fwmAtstart);


   TIODescriptor = class(TPersistent)
   private
     FAfterAllocateHandle: TAfterAllocateHandleEvent;
     FCloseHandleOnExecute: Boolean;
     FCustomHandle: THandle;
     FAutoCloseCustomHandle: Boolean;
     FCustomHandleIsInheritable: Boolean;
     FFileWriteMode: TFileWriteMode;
     FHandleType: TProcessHandleType;
     FFileName: TFileName;
     FIOType: TIOType;
     FOnGetHandle: TGetHandleEvent;
     FOwnerProcess: TProcess;
     FPipeBufferSize: cardinal;
     FProcess: TProcess;
     FTheirHandle : THandle;
     FTheirHandleIOType: TIOType;
     FHandleValid : Boolean;
     FStream : THandleStream;
     FOurHandle : THandle;
     procedure SetFileName(AValue: TFileName);
     procedure SetFileWriteMode(AValue: TFileWriteMode);
     procedure SetIOType(AValue: TIOType);
     procedure SetProcess(AValue: TProcess);
     function SysCreatePipeHandle: THandle;
     function SysPrepareCreatedHandleForProcess(aHandle: THandle): THandle;
     Function SysCreateFileNameHandle(const aFileName : string) : THandle;
     function SysNullFileName : string;
     function SysIsTypeSupported(AValue: TIOType) : Boolean;
   protected
     Procedure CheckNotRunning; virtual;
     // Create handles for new process
     Function PrepareCreatedHandleForProcess(aHandle : THandle) : THandle; virtual;
     Function CreateStandardHandle : THandle; virtual;
     Function CreatePipeHandle : THandle; virtual;
     Function CreateFileNameHandle : THandle; virtual;
     Function CreateNullFileHandle : THandle; virtual;
     Function CreateCustomHandle : THandle; virtual;
     Function CreateProcessHandle : THandle; virtual;
     Function ResolveProcessHandle : THandle; virtual;
     Function ResolveStream : THandleStream; virtual;
     Procedure CloseOurHandle; virtual;
     Procedure CloseTheirHandle(aForceClose : Boolean = false);virtual;
     Procedure PrepareHandles;virtual;
     Procedure ResetHandles;virtual;
     Property OwnerProcess : TProcess Read FOwnerProcess;
     Property PipeBufferSize : cardinal read FPipeBufferSize write FPipeBufferSize;
     Property OurHandle: THandle Read FOurHandle;
     Property HandleValid : Boolean Read FHandleValid;
     Property CloseHandleOnExecute : Boolean Read FCloseHandleOnExecute Write FCloseHandleOnExecute;
   public
     Constructor Create(aOwnerProcess : TProcess; aType : TProcessHandleType);
     Destructor Destroy; override;
     Property ProcessHandleType : TProcessHandleType Read FHandleType;
     Property CustomHandle : THandle Read FCustomHandle Write FCustomHandle;
     Property AutoCloseCustomHandle: Boolean Read FAutoCloseCustomHandle Write FAutoCloseCustomHandle;
     Property CustomHandleIsInheritable: Boolean Read FCustomHandleIsInheritable Write FCustomHandleIsInheritable; platform;

   Published
     Property IOType : TIOType Read FIOType Write SetIOType;
     Property FileName : TFileName Read FFileName Write SetFileName;
     Property OnGetHandle : TGetHandleEvent Read FOnGetHandle Write FOnGetHandle;
     Property AfterAllocateHandle : TAfterAllocateHandleEvent Read FAfterAllocateHandle Write FAfterAllocateHandle;
     Property Process : TProcess Read FProcess Write SetProcess;
     Property FileWriteMode : TFileWriteMode Read FFileWriteMode Write SetFileWriteMode;
   end;


  { TProcess }

  TProcess = Class (TComponent)
  Private
    FOnRunCommandEvent: TOnRunCommandEvent;
    FProcessOptions : TProcessOptions;
    FRunCommandSleepTime: Integer;
    FStartupOptions : TStartupOptions;
    FFillAttribute : Cardinal;
    FApplicationName : TProcessString;
    FConsoleTitle : TProcessString;
    FCommandLine : TProcessString;
    FCurrentDirectory : TProcessString;
    FDesktop : String;
    FEnvironment : TProcessStrings;
    FExecutable : TProcessString;
    FParameters : TProcessStrings;
    FShowWindow : TShowWindowOptions;
    FInherithandles : Boolean;
    {$ifdef UNIX}
    FForkEvent : TProcessForkEvent;
    {$endif UNIX}
    FProcessPriority : TProcessPriority;
    dwXCountchars,
    dwXSize,
    dwYsize,
    dwx,
    dwYcountChars,
    dwy : Cardinal;
    FXTermProgram: String;
    FPipeBufferSize : cardinal;
    FDescriptors: Array [TProcessHandleType] of TIODescriptor;
    function GetDescriptor(AIndex: Integer): TIODescriptor;
    Function  GetExitStatus : Integer;
    Function  GetExitCode : Integer;
    function GetInputStream: TOutputPipeStream;
    function GetOutputStream: TInputPipeStream;
    Function  GetRunning : Boolean;
    function GetStderrStream: TinputPipeStream;
    Function  GetWindowRect : TRect;
    procedure SetCommandLine(const AValue: TProcessString); deprecated;
    procedure SetDescriptor(AIndex: Integer; AValue: TIODescriptor);
    procedure SetParameters(const AValue: TProcessStrings);
    procedure SetPipeBufferSize(AValue: cardinal);
    Procedure SetWindowRect (Value : TRect);
    Procedure SetShowWindow (Value : TShowWindowOptions);
    Procedure SetWindowColumns (Value : Cardinal);
    Procedure SetWindowHeight (Value : Cardinal);
    Procedure SetWindowLeft (Value : Cardinal);
    Procedure SetWindowRows (Value : Cardinal);
    Procedure SetWindowTop (Value : Cardinal);
    Procedure SetWindowWidth (Value : Cardinal);
    procedure SetApplicationName(const Value: TProcessString);
    procedure SetProcessOptions(const Value: TProcessOptions);
    procedure SetActive(const Value: Boolean);
    procedure SetEnvironment(const Value: TProcessStrings);
    Procedure ConvertCommandLine;
    function  PeekExitStatus: Boolean;
    Procedure IntOnIdleSleep(Sender,Context : TObject;Status:TRunCommandEventCode;const Message:String);
  Protected
    FRunning : Boolean;
    FExitCode : Cardinal;
    FProcessID : Integer;
    FThreadID : Integer;
    FProcessHandle : Thandle;
    FThreadHandle : Thandle;
    procedure CloseProcessHandles; virtual;
    procedure Loaded; override;
    Procedure SysExecute; virtual;
    function CreateIODescriptor(aOwner: TProcess; aHandleType: TProcessHandleType): TIODescriptor; virtual;
  Public
    Constructor Create (AOwner : TComponent);override;
    Destructor Destroy; override;
    Procedure Execute; virtual;
    procedure CloseInput; virtual;
    procedure CloseOutput; virtual;
    procedure CloseStderr; virtual;
    Function Resume : Integer; virtual;
    Function Suspend : Integer; virtual;
    Function Terminate (AExitCode : Integer): Boolean; virtual;
    Function WaitOnExit : Boolean;
    Function WaitOnExit(Timeout : DWord) : Boolean;
    function ReadInputStream(p:TInputPipeStream;var BytesRead:integer;var DataLength:integer;var Data:string;MaxLoops:integer=10):boolean; virtual;
    function ReadInputStream(p:TInputPipeStream;data:TStream;MaxLoops:integer=10):boolean; virtual;
    function RunCommandLoop(out outputstring:string;out stderrstring:string; out anexitstatus:integer):integer; virtual;

    Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
    Property Handle : THandle Read FProcessHandle;
    Property ProcessHandle : THandle Read FProcessHandle;
    Property ThreadHandle : THandle Read FProcessHandle;
    Property ProcessID : Integer Read FProcessID;
    Property ThreadID : Integer Read FThreadID;
    Property Input  : TOutputPipeStream Read GetInputStream;
    Property Output : TInputPipeStream  Read GetOutputStream;
    Property Stderr : TinputPipeStream  Read GetStderrStream;
    Property ExitStatus : Integer Read GetExitStatus;
    Property ExitCode : Integer Read GetExitCode;
    Property InheritHandles : Boolean Read FInheritHandles Write FInheritHandles;
    Property OnRunCommandEvent : TOnRunCommandEvent Read FOnRunCommandEvent Write FOnRunCommandEvent;
    Property RunCommandSleepTime : Integer read FRunCommandSleepTime write FRunCommandSleepTime;
    {$ifdef UNIX}
    property OnForkEvent : TProcessForkEvent Read FForkEvent Write FForkEvent;
    {$endif UNIX}
  Published
    property PipeBufferSize : cardinal read FPipeBufferSize write SetPipeBufferSize default 1024;
    Property Active : Boolean Read GetRunning Write SetActive;
    Property ApplicationName : TProcessString Read FApplicationName Write SetApplicationName; deprecated;
    Property CommandLine : TProcessString Read FCommandLine Write SetCommandLine ; deprecated;
    Property Executable : TProcessString Read FExecutable Write FExecutable;
    Property Parameters : TProcessStrings Read FParameters Write SetParameters;
    Property ConsoleTitle : TProcessString Read FConsoleTitle Write FConsoleTitle;
    Property CurrentDirectory : TProcessString Read FCurrentDirectory Write FCurrentDirectory;
    Property Desktop : String Read FDesktop Write FDesktop;
    Property Environment : TProcessStrings Read FEnvironment Write SetEnvironment;
    Property Options : TProcessOptions Read FProcessOptions Write SetProcessOptions;
    Property Priority : TProcessPriority Read FProcessPriority Write FProcessPriority;
    Property StartupOptions : TStartupOptions Read FStartupOptions Write FStartupOptions;
    Property Running : Boolean Read GetRunning;
    Property ShowWindow : TShowWindowOptions Read FShowWindow Write SetShowWindow;
    Property WindowColumns : Cardinal Read dwXCountChars Write SetWindowColumns;
    Property WindowHeight : Cardinal Read dwYSize Write SetWindowHeight;
    Property WindowLeft : Cardinal Read dwX Write SetWindowLeft;
    Property WindowRows : Cardinal Read dwYCountChars Write SetWindowRows;
    Property WindowTop : Cardinal Read dwY Write SetWindowTop ;
    Property WindowWidth : Cardinal Read dwXSize Write SetWindowWidth;
    Property FillAttribute : Cardinal read FFillAttribute Write FFillAttribute;
    Property XTermProgram : String Read FXTermProgram Write FXTermProgram;
    Property InputDescriptor : TIODescriptor index Ord(phtInput) Read GetDescriptor Write SetDescriptor;
    Property OutputDescriptor : TIODescriptor Index Ord(phtOutput) Read GetDescriptor Write SetDescriptor;
    Property ErrorDescriptor : TIODescriptor Index Ord(phtError) Read GetDescriptor Write SetDescriptor;
  end;

  TProcessClass = Class of TProcess;

Procedure CommandToList(S : TProcessString; List : TProcessStrings);

{$ifdef unix}
Var
  TryTerminals : Array of string;
  XTermProgram : String;
  SignalWaitTime : Integer = 20; // Wait time in ms. after sending SIGTERM
  Function DetectXTerm : String;
{$endif unix}

function RunCommandIndir(const curdir:TProcessString;const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; out exitstatus:integer; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):integer;
function RunCommandIndir(const curdir:TProcessString;const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):boolean;
function RunCommand(const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):boolean;

function RunCommandInDir(const curdir,cmdline:TProcessString;out outputstring:string):boolean; deprecated;
function RunCommand(const cmdline:TProcessString;out outputstring:string):boolean; deprecated;

// Allows override of the class instantiated for RunCommand*.

var DefaultTProcess : TProcessClass = TProcess;

const
  SErrCannotCreatePipes = 'Failed to create pipes';

{$IFDEF LINUX}
  INVALID_HANDLE_VALUE = DWORD(-1);
{$ENDIF}

implementation

{$IFDEF MSWINDOWS}
{$i win_process.inc}
{$ELSE}
{$i unix_process.inc}
{$ENDIF}

{$IFNDEF SKIPHELPERS}
function TIOTypeHelper.ToString : string;

begin
  WriteStr(Result,Self);
end;

{ TProcessHandleTypeHelper }

function TProcessHandleTypeHelper.ToString: string;
begin
  WriteStr(Result,Self);
end;


{ TProcessOption }

function TProcessOptionHelper.ToString: String;
begin
  WriteStr(Result,Self);
end;

{ TShowWindowOptionsHelper }

function TShowWindowOptionsHelper.ToString: String;
begin
  WriteStr(Result,Self);
end;

{ TStartupOptionHelper }

function TStartupOptionHelper.ToString: String;
begin
  WriteStr(Result,Self);
end;

{ TProcessPriorityhelper }

function TProcessPriorityhelper.ToString: String;
begin
  WriteStr(Result,Self);
end;

{ TRunCommandEventCodeHelper }

function TRunCommandEventCodeHelper.ToString: string;
begin
  WriteStr(Result,Self);
end;

{$ENDIF SKIPHELPERS}

Procedure CommandToList(S : TProcessString; List : TProcessStrings);

  Function GetNextWord : TProcessString;

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

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

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

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

     if  (Length(Result) > 0)
     and (Result[1] = Result[Length(Result)]) // if 1st AnsiChar = last AnsiChar and..
     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)

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

  end;

Var
  W : TProcessString;

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

Constructor TProcess.Create (AOwner : TComponent);

Var
  HT : TProcessHandleType;

begin
  Inherited;
  FProcessPriority:=ppNormal;
  FShowWindow:=swoNone;
  FInheritHandles:=True;
  {$ifdef UNIX}
  FForkEvent:=nil;
  {$endif UNIX}
  FPipeBufferSize := 1024;
  FEnvironment:=TProcessStringList.Create;
  FParameters:=TProcessStringList.Create;
  FRunCommandSleepTime:=100;
  FOnRunCommandEvent:=@IntOnIdleSleep;
  For HT in TProcessHandleType do
    FDescriptors[HT]:=CreateIODescriptor(Self,HT)
end;

Destructor TProcess.Destroy;

Var
  HT : TProcessHandleType;

begin
  FParameters.Free;
  FEnvironment.Free;
  CloseProcessHandles;
  For HT in TProcessHandleType do
    FreeAndNil(FDescriptors[HT]);
  Inherited Destroy;
end;

Function TProcess.CreateIODescriptor(aOwner : TProcess; aHandleType : TProcessHandleType) :TIODescriptor;

begin
  Result:=TIODescriptor.Create(aOwner,aHandleType);
end;

function TProcess.GetDescriptor(AIndex: Integer): TIODescriptor;
begin
  Result:=FDescriptors[TProcessHandleType(aIndex)];
end;

function TProcess.GetInputStream: TOutputPipeStream;
begin
  Result:=FDescriptors[phtInput].ResolveStream as TOutputPipeStream;
end;

function TProcess.GetOutputStream: TInputPipeStream;
begin
  Result:=FDescriptors[phtOutput].ResolveStream as TInputPipeStream;
end;



Function TProcess.GetExitStatus : Integer;

begin
  GetRunning;
  Result:=Integer(FExitCode);
end;

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

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

Function TProcess.GetRunning : Boolean;

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

function TProcess.GetStderrStream: TinputPipeStream;
begin
  Result:=FDescriptors[phtError].ResolveStream as TInputPipeStream;
end;

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

Procedure TProcess.Execute;

Var
  HT : TProcessHandleType;

begin
  for HT in TProcessHandleType do
    FDescriptors[HT].ResetHandles;
  SysExecute;
end;


procedure TProcess.CloseInput;
begin
  FDescriptors[phtInput].CloseOurHandle;
end;

procedure TProcess.CloseOutput;
begin
  FDescriptors[phtOutput].CloseOurHandle;
end;

procedure TProcess.CloseStderr;
begin
  FDescriptors[phtError].CloseOurHandle;
end;

Procedure TProcess.SetWindowColumns (Value : Cardinal);

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


Procedure TProcess.SetWindowHeight (Value : Cardinal);

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

Procedure TProcess.SetWindowLeft (Value : Cardinal);

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

Procedure TProcess.SetWindowTop (Value : Cardinal);

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

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

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

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

procedure TProcess.SetDescriptor(AIndex: Integer; AValue: TIODescriptor);
begin
  FDescriptors[TProcessHandleType(aIndex)].Assign(AValue);
end;

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

procedure TProcess.SetPipeBufferSize(AValue: cardinal);
var
  HT: TProcessHandleType;
begin
  if FPipeBufferSize = AValue then Exit;
  FPipeBufferSize := AValue;
  for HT in TProcessHandleType do
    FDescriptors[HT].PipeBufferSize:=AValue;
end;

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


Procedure TProcess.SetWindowRows (Value : Cardinal);

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

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

procedure TProcess.SetProcessOptions(const Value: TProcessOptions);

var
  HT : TProcessHandleType;

begin
  FProcessOptions := Value;
  If poNewConsole in FProcessOptions then
    Exclude(FProcessOptions,poNoConsole);
  if poRunSuspended in FProcessOptions then
    Exclude(FProcessOptions,poWaitOnExit);
  if poUsePipes in FProcessOptions then
    for HT in TProcessHandleType do
      FDescriptors[HT].IOType:=iotPipe;
  if poStderrToOutPut in FProcessOptions then
    FDescriptors[phtError].IOType:=iotDefault;
  if poPassInput in FProcessOptions then
    FDescriptors[phtInput].IOType:=iotDefault;
end;

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

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

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

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

function TProcess.ReadInputStream(p:TInputPipeStream;var BytesRead:integer;var DataLength:integer;var data:string;MaxLoops:integer=10):boolean;
var Available, NumBytes: integer;
begin
    Available:=P.NumBytesAvailable;
    result:=Available>0;
    if not result then
     exit;
    while (available > 0) and (MaxLoops>0) do
      begin
        if (BytesRead + available > DataLength) then
          begin
            DataLength:=BytesRead + max(READ_BYTES,available);
            Setlength(Data,DataLength);
          end;
        NumBytes := p.Read(data[1+BytesRead], Available);
        if NumBytes > 0 then
          Inc(BytesRead, NumBytes);
        Available:=P.NumBytesAvailable;
        dec(MaxLoops);
      end;
end;

function TProcess.ReadInputStream(p:TInputPipeStream;data:TStream;MaxLoops:integer=10):boolean;
const
  BufSize = 4096;
var
  Buffer: array[0..BufSize - 1] of byte;
  ReadBytes: integer;
  Available : integer;
begin
  Available:=P.NumBytesAvailable;
  result:=Available>0;
  if not result then
    Exit;
  while (available > 0) and (MaxLoops>0) do
  begin
    ReadBytes := Output.Read({%H-}Buffer, min(BufSize,Available));
    data.Write(Buffer, ReadBytes);
    Available:=P.NumBytesAvailable;
    dec(MaxLoops);
  end;
end;

procedure TProcess.IntOnIdleSleep(Sender,Context : TObject;status:TRunCommandEventCode;const message:string);
begin
  if status=RunCommandIdle then
    sleep(FRunCommandSleepTime);
end;

// helperfunction that does the bulk of the work.
// We need to also collect stderr output in order to avoid
// lock out if the stderr pipe is full.
function TProcess.RunCommandLoop(out outputstring:string;
                            out stderrstring:string; out anexitstatus:integer):integer;
var
    bytesread : integer;
    outputlength, stderrlength : integer;
    stderrbytesread : integer;
    gotoutput,gotoutputstderr : boolean;
begin
  result:=-1;
    try
    Options := Options + [poUsePipes];
    bytesread:=0;
    outputlength:=0;
    stderrbytesread:=0;
    stderrlength:=0;
    Execute;
    while 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.
        gotoutput:=ReadInputStream(output,BytesRead,OutputLength,OutputString,1);
        // The check for assigned(P.stderr) is mainly here so that
        // if we use poStderrToOutput in p.Options, we do not access invalid memory.
        gotoutputstderr:=false;
        if assigned(stderr) then
            gotoutputstderr:=ReadInputStream(StdErr,StdErrBytesRead,StdErrLength,StdErrString,1);
 
        if (porunidle in options) and not gotoutput and not gotoutputstderr and Assigned(FOnRunCommandEvent) Then
          FOnRunCommandEvent(self,Nil,RunCommandIdle,'');
      end;
    // Get left output after end of execution
    ReadInputStream(output,BytesRead,OutputLength,OutputString,250);
    setlength(outputstring,BytesRead);
    if assigned(stderr) then
      ReadInputStream(StdErr,StdErrBytesRead,StdErrLength,StdErrString,250);
    setlength(stderrstring,StderrBytesRead);
    anexitstatus:=exitstatus;
    result:=0; // we came to here, document that.
    if Assigned(FOnRunCommandEvent) then          // allow external apps to react to that and finish GUI
      FOnRunCommandEvent(self,Nil,RunCommandFinished,'');

    except
      on e : Exception do
         begin
           result:=1;
           setlength(outputstring,BytesRead);
           setlength(stderrstring,StderrBytesRead);
           if Assigned(FOnRunCommandEvent) then
             FOnRunCommandEvent(self,Nil,RunCommandException,e.Message);
         end;
     end;
end;

{ Functions without StderrString }

Const
  ForbiddenOptions = [poRunSuspended,poWaitOnExit];

function RunCommandIndir(const curdir:TProcessString;const exename:TProcessString;const commands:array of TProcessString;out outputstring:string;out exitstatus:integer; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):integer;
Var
    p : TProcess;
    i : integer;
    ErrorString : String;
begin
  p:=DefaultTProcess.create(nil);
  if Options<>[] then
    P.Options:=Options - ForbiddenOptions;
  P.ShowWindow:=SwOptions;
  p.Executable:=exename;
  if curdir<>'' then
    p.CurrentDirectory:=curdir;
  if high(commands)>=0 then
   for i:=low(commands) to high(commands) do
     p.Parameters.add(commands[i]);
  try
    result:=p.RunCommandLoop(outputstring,errorstring,exitstatus);
  finally
    p.free;
  end;
end;

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

function RunCommandIndir(const curdir:TProcessString;const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):boolean;
Var
    p : TProcess;
    i,
    exitstatus : integer;
    ErrorString : String;
begin
  p:=DefaultTProcess.create(nil);
  if Options<>[] then
    P.Options:=Options - ForbiddenOptions;
  P.ShowWindow:=SwOptions;
  p.Executable:=exename;
  if curdir<>'' then
    p.CurrentDirectory:=curdir;
  if high(commands)>=0 then
   for i:=low(commands) to high(commands) do
     p.Parameters.add(commands[i]);
  try
    result:=p.RunCommandLoop(outputstring,errorstring,exitstatus)=0;
  finally
    p.free;
  end;
  if exitstatus<>0 then result:=false;
end;

function RunCommand(const cmdline:TProcessString;out outputstring:String):boolean; deprecated;
Var
    p : TProcess;
    exitstatus : integer;
    ErrorString : String;
begin
  p:=DefaultTProcess.create(nil);
  p.setcommandline(cmdline);
  try
    result:=p.RunCommandLoop(outputstring,errorstring,exitstatus)=0;
  finally
    p.free;
  end;
  if exitstatus<>0 then result:=false;
end;

function RunCommand(const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):boolean;
Var
    p : TProcess;
    i,
    exitstatus : integer;
    ErrorString : String;
begin
  p:=DefaultTProcess.create(nil);
  if Options<>[] then
    P.Options:=Options - ForbiddenOptions;
  P.ShowWindow:=SwOptions;
  p.Executable:=exename;
  if high(commands)>=0 then
   for i:=low(commands) to high(commands) do
     p.Parameters.add(commands[i]);
  try
    result:=p.RunCommandLoop(outputstring,errorstring,exitstatus)=0;
  finally
    p.free;
  end;
  if exitstatus<>0 then result:=false;
end;

{$ifdef processunicodestring}
// dummy subset of tstrings.
{ TProcessStrings }

function TProcessStrings.getname( index: integer): Unicodestring;
begin
  if index<length(name) then
     result:=name[index]
  else
     result:='';
end;

function TProcessStrings.getcount: Integer;
begin
  result:=length(name);
end;

procedure TProcessStrings.AssignTo(Dest: TPersistent);
var i : integer;
begin
  inherited assign(dest);
  if dest is TStrings then
    begin
      setlength(name,tstrings(dest).count);
      for i:=0 to length(name)-1 do
        name[i]:=tstrings(dest)[i];
    end;
  if dest is tprocessstrings then
     name:=copy(tprocessstrings(dest).name);
end;

procedure TProcessStrings.add(const s: Unicodestring);
var len : integer;
begin
  len:=length(name);
  setlength(name, len+1);
  name[len]:=s;
end;

procedure TProcessStrings.Clear;
begin
 setlength(name,0);
end;

procedure TProcessStrings.Delete(i: integer);
var len,j : integer;
begin
  len:=length(name);
  if len=0 then exit;
  if (i<>len-1) and (len<>1) then
     begin
       for j:=i+1 to len-1 do
         name[j-1]:=name[j];
       setlength(name,len-1)
     end
  else
    setlength(name,len-1)
end;
{$endif}

{ TIODescriptor }

procedure TIODescriptor.SetFileName(AValue: TFileName);
begin
  if FileName=AValue then Exit;
  CheckNotRunning;
  FFileName:=AValue;
  if aValue<>'' then
    FIOType:=iotFile;
end;

procedure TIODescriptor.SetFileWriteMode(AValue: TFileWriteMode);
begin
  if FFileWriteMode=AValue then Exit;
  CheckNotRunning;
  FFileWriteMode:=AValue;
end;

procedure TIODescriptor.SetIOType(AValue: TIOType);

var
  S : String;

begin
  if FIOType=AValue then Exit;
  CheckNotRunning;
  if not SysIsTypeSupported(aValue) then
    begin
    WriteStr(S,aValue);
    Raise EProcess.CreateFmt('I/O Type "%s" not supported on this platform',[S]);
    end;
  FIOType:=AValue;
  // Some cleanup
  if aValue<>iotProcess then
    FProcess:=Nil;
  if aValue<>iotFile then
    FFileName:='';
end;

procedure TIODescriptor.SetProcess(AValue: TProcess);

begin
  if FProcess=AValue then Exit;
  CheckNotRunning;
  if (FOwnerProcess=FProcess) then
    Raise EProcess.Create('Remote process cannot refer to self process');
  if Assigned(FOwnerProcess) and Assigned(FProcess) then
    FProcess.RemoveComponent(FOwnerProcess);
  if (aValue<>Nil) then
    FIOType:=iotProcess;
  FProcess:=AValue;
  if Assigned(FOwnerProcess) and Assigned(FProcess) then
    FProcess.RemoveComponent(FOwnerProcess);
  if Self.ProcessHandleType=phtInput then
    FProcess.OutputDescriptor.IOType:=iotPipe
  else
    FProcess.InputDescriptor.IOType:=iotPipe;
end;


procedure TIODescriptor.CheckNotRunning;
begin
  If Assigned(FOwnerProcess) then
    if FOwnerProcess.Active then
       Raise EProcess.Create('Cannot perform operation while process is running');
end;

function TIODescriptor.PrepareCreatedHandleForProcess(aHandle: THandle): THandle;
begin
  Result:=SysPrepareCreatedHandleForProcess(aHandle);
end;

Function TIODescriptor.CreateStandardHandle : THandle;

begin
  case ProcessHandleType of
    phtInput:  Result:=StdInputHandle;
    phtOutput:  Result:=StdOutputHandle;
    phtError:  Result:=StdErrorHandle;
  end;
end;

Function TIODescriptor.CreatePipeHandle : THandle;

begin
  Result:=SysCreatePipeHandle;
end;

{$IFNDEF OS_HASCREATEPIPE}
Function TIODescriptor.SysCreatePipeHandle : THandle;

var
  HIn,HOut : Thandle;
begin
  HIn:=THandle(INVALID_HANDLE_VALUE);
  HOut:=HIn;
  If not CreatePipeHandles(Hin,HOut) then
    Raise EProcess.Create(SErrCannotCreatePipes);
  case ProcessHandleType of
    phtInput:
      begin
      Result:=HIn;
      FOurHandle:=hOut;
      end;
    phtOutput,
    phtError:
      begin
      Result:=hOut;
      FOurHandle:=HIn;
      end;
  end;
end;
{$ENDIF}

Function TIODescriptor.CreateFileNameHandle : THandle;

begin
  Result:=SysCreateFileNameHandle(FileName);
  if (ProcessHandleType<>phtInput) then
    case FFileWriteMode of
      fwmAtstart: ;
      fwmTruncate : FileTruncate(Result,0);
      fwmAppend : FileSeek(Result,0,soFromEnd);
    end;
end;

function TIODescriptor.CreateNullFileHandle: THandle;
begin
  Result:=SysCreateFileNameHandle(SysNullFileName);
end;

Function TIODescriptor.CreateCustomHandle : THandle;

begin
  Result:=FCustomHandle;
  if Assigned(FOnGetHandle) then
    FOnGetHandle(Self,Result,FCloseHandleOnExecute);
  if FCustomHandle=THandle(INVALID_HANDLE_VALUE) then
    Raise EProcess.Create('Cannot get custom handle. No handle set');
end;

Function TIODescriptor.CreateProcessHandle : THandle;

begin
  if Not Assigned(Process) then
    Raise EProcess.Create('Cannot get handle. No process assigned');
  case ProcessHandleType of
    phtInput:  Result:=Process.OutputDescriptor.OurHandle;
    phtOutput:  Result:=Process.InputDescriptor.OurHandle;
    phtError:  Result:=Process.InputDescriptor.OurHandle;
  end;
  if Result=THandle(INVALID_HANDLE_VALUE) then
    Raise EProcess.Create('Cannot get handle. Process not active');
end;

function TIODescriptor.ResolveStream: THandleStream;
begin
  if (FStream=Nil) and (FHandleValid) and (FTheirHandleIOType=iotPipe) then
    begin
    // Writeln(ProcessHandleType,' creating stream for stream ',IOType,': ',OurHandle);
    Case FHandleType of
      phtInput : FStream:=TOutputPipeStream.Create(OurHandle);
      phtError,
      phtOutput : FStream:=TInputPipeStream.Create(OurHandle);
    end;
    end;
  FOurHandle:=THandle(INVALID_HANDLE_VALUE);
  Result:=FStream;
end;


procedure TIODescriptor.CloseOurHandle;

var
  H : THandle;

begin
  if Not FHandleValid then
     exit;
  H:=OurHandle;
  // Writeln(StdErr, GetProcessID ,' : ',ProcessHandleType,' closing our handle ',IOType,': ',FOurHandle);
  FOurHandle:=THandle(INVALID_HANDLE_VALUE) ;
  if H<>THandle(INVALID_HANDLE_VALUE) then
    FileClose(H);
end;

procedure TIODescriptor.CloseTheirHandle(aForceClose: Boolean);
var
  H : THandle;

begin
  if Not FHandleValid then
     exit;
  If (FTheirHandleIOType=iotDefault) or not (CloseHandleOnExecute or aForceClose) then
    begin
    FTheirHandle:=THandle(INVALID_HANDLE_VALUE);
    exit;
    end;
  H:=ResolveProcessHandle;
  // Writeln(StdErr,GetProcessID,' : ',ProcessHandleType,' closing their handle ',IOType,': ',H);
  FTheirHandle:=THandle(INVALID_HANDLE_VALUE);
  if H<>THandle(INVALID_HANDLE_VALUE) then
    begin
    FileClose(H);
    end;
end;

procedure TIODescriptor.PrepareHandles;

var
  H : THandle;
  S : String;

begin
  WriteStr(S,IOType);
  H:=ResolveProcessHandle;
  // Writeln('PReparing handle ',S,' : ',H,' (ours: ',OurHandle,')');
  if H=THandle(INVALID_HANDLE_VALUE) then
     Raise EProcess.CreateFmt('Failed to prepare process handle for %s',[S]);
end;

procedure TIODescriptor.ResetHandles;

begin
  CloseOurHandle;
  CloseTheirHandle(True);
  FreeAndNil(FStream);
  FHandleValid:=False;
end;



function TIODescriptor.ResolveProcessHandle: THandle;

var
  H : THandle;

begin
  if not FHandleValid then
    begin
    FTheirHandleIOType := IOType;
    FOurHandle:=THAndle(INVALID_HANDLE_VALUE);
    FCloseHandleOnExecute:=(IOType<>iotDefault);
    if IOType = iotHandle then
      FCloseHandleOnExecute:=FAutoCloseCustomHandle;
    Case IOType of
      iotDefault : H:=CreateStandardHandle;
      iotPipe : H:=CreatePipeHandle;
      iotFile : H:=CreateFileNameHandle;
      iotProcess : H:=CreateProcessHandle;
      iotHandle : H:=CreateCustomHandle;
      iotNull : H:=CreateNullFileHandle;
    end;
    FTheirHandle:=PrepareCreatedHandleForProcess(H);
    if Assigned(FAfterAllocateHandle) then
      FAfterAllocateHandle(Self,FTheirHandle,FCloseHandleOnExecute);
    FHandleValid:=True;
    end;
  Result:=FTheirHandle;
end;

constructor TIODescriptor.Create(aOwnerProcess: TProcess; aType: TProcessHandleType);
begin
  FOwnerProcess:=aOwnerProcess;
  FHandleType:=aType;
  FCustomHandle:=THandle(INVALID_HANDLE_VALUE);
  FTheirHandle:=THandle(INVALID_HANDLE_VALUE);
  FOurHandle:=THandle(INVALID_HANDLE_VALUE);
  FPipeBufferSize := 1024;
  FAutoCloseCustomHandle := True;
end;

destructor TIODescriptor.Destroy;
begin
  FreeAndNil(FStream);
  ResetHandles;
  inherited Destroy;
end;

end.