lprocess.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. { Asynchronous process support
  2. Copyright (C) 2006-2008 Micha Nelissen
  3. This library is Free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. This license has been modified. See file LICENSE.ADDON for more information.
  15. Should you find these sources without a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit lProcess;
  19. {$mode objfpc}{$h+}
  20. interface
  21. uses
  22. sysutils, classes, process, levents, pipes;
  23. type
  24. TLInputPipeStream = class(TInputPipeStream)
  25. protected
  26. FEvent: TLHandle;
  27. public
  28. function Read(var Buffer; Count: longint): longint; override;
  29. end;
  30. TLOutputPipeStream = class(TOutputPipeStream)
  31. protected
  32. FEvent: TLHandle;
  33. public
  34. function Write(const Buffer; Count: longint): longint; override;
  35. end;
  36. TLProcess = class(TProcess)
  37. protected
  38. FInputEvent: TLHandle;
  39. FOutputEvent: TLHandle;
  40. FStderrEvent: TLHandle;
  41. FEventer: TLEventer;
  42. function GetOnNeedInput: TLHandleEvent;
  43. function GetOnHasOutput: TLHandleEvent;
  44. function GetOnHasStderr: TLHandleEvent;
  45. procedure SetOnNeedInput(NewOnInput: TLHandleEvent);
  46. procedure SetOnHasOutput(NewOnOutput: TLHandleEvent);
  47. procedure SetOnHasStderr(NewOnStderr: TLHandleEvent);
  48. public
  49. constructor Create(AOwner: TComponent); override;
  50. destructor Destroy; override;
  51. procedure CloseInput; override;
  52. procedure CloseOutput; override;
  53. procedure CloseStderr; override;
  54. procedure Execute; override;
  55. property InputEvent: TLHandle read FInputEvent;
  56. property OutputEvent: TLHandle read FOutputEvent;
  57. property StderrEvent: TLHandle read FStderrEvent;
  58. property Eventer: TLEventer read FEventer write FEventer;
  59. property OnNeedInput: TLHandleEvent read GetOnNeedInput write SetOnNeedInput;
  60. property OnHasOutput: TLHandleEvent read GetOnHasOutput write SetOnHasOutput;
  61. property OnHasStderr: TLHandleEvent read GetOnHasStderr write SetOnHasStderr;
  62. end;
  63. implementation
  64. function TLInputPipeStream.Read(var Buffer; Count: longint): longint;
  65. begin
  66. Result := inherited;
  67. FEvent.IgnoreRead := false;
  68. end;
  69. function TLOutputPipeStream.Write(const Buffer; Count: longint): longint;
  70. begin
  71. Result := inherited;
  72. FEvent.IgnoreWrite := false;
  73. end;
  74. constructor TLProcess.Create(AOwner: TComponent);
  75. begin
  76. inherited;
  77. FInputEvent := TLHandle.Create;
  78. FOutputEvent := TLHandle.Create;
  79. FStderrEvent := TLHandle.Create;
  80. end;
  81. destructor TLProcess.Destroy;
  82. begin
  83. inherited;
  84. FInputEvent.Free;
  85. FOutputEvent.Free;
  86. FStderrEvent.Free;
  87. end;
  88. procedure TLProcess.CloseInput;
  89. begin
  90. FEventer.UnplugHandle(FInputEvent);
  91. inherited;
  92. end;
  93. procedure TLProcess.CloseOutput;
  94. begin
  95. FEventer.UnplugHandle(FOutputEvent);
  96. inherited;
  97. end;
  98. procedure TLProcess.CloseStderr;
  99. begin
  100. FEventer.UnplugHandle(FStderrEvent);
  101. inherited;
  102. end;
  103. procedure TLProcess.Execute;
  104. begin
  105. inherited;
  106. if (poUsePipes in Options) and (FEventer <> nil) then
  107. begin
  108. if Input <> nil then
  109. begin
  110. FInputEvent.Handle := Input.Handle;
  111. FInputEvent.IgnoreRead := true;
  112. FEventer.AddHandle(FInputEvent);
  113. end;
  114. if Output <> nil then
  115. begin
  116. FOutputEvent.Handle := Output.Handle;
  117. FOutputEvent.IgnoreWrite := true;
  118. FEventer.AddHandle(FOutputEvent);
  119. end;
  120. if Stderr <> nil then
  121. begin
  122. FStderrEvent.Handle := Stderr.Handle;
  123. FStderrEvent.IgnoreWrite := true;
  124. FEventer.AddHandle(FStderrEvent);
  125. end;
  126. end;
  127. end;
  128. function TLProcess.GetOnNeedInput: TLHandleEvent;
  129. begin
  130. Result := FInputEvent.OnWrite;
  131. end;
  132. function TLProcess.GetOnHasOutput: TLHandleEvent;
  133. begin
  134. Result := FOutputEvent.OnRead;
  135. end;
  136. function TLProcess.GetOnHasStderr: TLHandleEvent;
  137. begin
  138. Result := FStderrEvent.OnRead;
  139. end;
  140. procedure TLProcess.SetOnNeedInput(NewOnInput: TLHandleEvent);
  141. begin
  142. FInputEvent.OnWrite := NewOnInput;
  143. end;
  144. procedure TLProcess.SetOnHasOutput(NewOnOutput: TLHandleEvent);
  145. begin
  146. FOutputEvent.OnRead := NewOnOutput;
  147. end;
  148. procedure TLProcess.SetOnHasStderr(NewOnStderr: TLHandleEvent);
  149. begin
  150. FStderrEvent.OnRead := NewOnStderr;
  151. end;
  152. end.