summaryrefslogtreecommitdiff
path: root/utils/fppkg/lnet/lprocess.pp
blob: 08469ffe0d1dd2b83563a02dfb42471780f56fb9 (plain)
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
{ Asynchronous process support

  Copyright (C) 2006-2008 Micha Nelissen

  This library is Free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  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. See the GNU Library General Public License
  for more details.

  You should have received a Copy of the GNU Library General Public License
  along with This library; if not, Write to the Free Software Foundation,
  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  
  This license has been modified. See file LICENSE.ADDON for more information.
  Should you find these sources without a LICENSE File, please contact
  me at ales@chello.sk
}

unit lProcess;

{$mode objfpc}{$h+}

interface

uses
  sysutils, classes, process, levents, pipes;

type
  TLInputPipeStream = class(TInputPipeStream)
  protected
    FEvent: TLHandle;
  public
    function Read(var Buffer; Count: longint): longint; override;
  end;

  TLOutputPipeStream = class(TOutputPipeStream)
  protected
    FEvent: TLHandle;
  public
    function Write(const Buffer; Count: longint): longint; override;          
  end;

  TLProcess = class(TProcess)
  protected
    FInputEvent: TLHandle;
    FOutputEvent: TLHandle;
    FStderrEvent: TLHandle;
    FEventer: TLEventer;

    function  GetOnNeedInput: TLHandleEvent;
    function  GetOnHasOutput: TLHandleEvent;
    function  GetOnHasStderr: TLHandleEvent;
    procedure SetOnNeedInput(NewOnInput: TLHandleEvent);
    procedure SetOnHasOutput(NewOnOutput: TLHandleEvent);
    procedure SetOnHasStderr(NewOnStderr: TLHandleEvent);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    
    procedure CloseInput; override;
    procedure CloseOutput; override;
    procedure CloseStderr; override;
    procedure Execute; override;

    property InputEvent: TLHandle read FInputEvent;
    property OutputEvent: TLHandle read FOutputEvent;
    property StderrEvent: TLHandle read FStderrEvent;
    property Eventer: TLEventer read FEventer write FEventer;
    property OnNeedInput: TLHandleEvent read GetOnNeedInput write SetOnNeedInput;
    property OnHasOutput: TLHandleEvent read GetOnHasOutput write SetOnHasOutput;
    property OnHasStderr: TLHandleEvent read GetOnHasStderr write SetOnHasStderr;
  end;

implementation

function TLInputPipeStream.Read(var Buffer; Count: longint): longint;
begin
  Result := inherited;
  FEvent.IgnoreRead := false;
end;

function TLOutputPipeStream.Write(const Buffer; Count: longint): longint;
begin
  Result := inherited;
  FEvent.IgnoreWrite := false;
end;

constructor TLProcess.Create(AOwner: TComponent);
begin
  inherited;

  FInputEvent := TLHandle.Create;
  FOutputEvent := TLHandle.Create;
  FStderrEvent := TLHandle.Create;
end;

destructor TLProcess.Destroy;
begin
  inherited;
  FInputEvent.Free;
  FOutputEvent.Free;
  FStderrEvent.Free;
end;

procedure TLProcess.CloseInput;
begin
  FEventer.UnplugHandle(FInputEvent);
  inherited;
end;

procedure TLProcess.CloseOutput;
begin
  FEventer.UnplugHandle(FOutputEvent);
  inherited;
end;

procedure TLProcess.CloseStderr;
begin
  FEventer.UnplugHandle(FStderrEvent);
  inherited;
end;

procedure TLProcess.Execute;
begin
  inherited;

  if (poUsePipes in Options) and (FEventer <> nil) then
  begin
    if Input <> nil then
    begin
      FInputEvent.Handle := Input.Handle;
      FInputEvent.IgnoreRead := true;
      FEventer.AddHandle(FInputEvent);
    end;
    if Output <> nil then
    begin
      FOutputEvent.Handle := Output.Handle;
      FOutputEvent.IgnoreWrite := true;
      FEventer.AddHandle(FOutputEvent);
    end;
    if Stderr <> nil then
    begin
      FStderrEvent.Handle := Stderr.Handle;
      FStderrEvent.IgnoreWrite := true;
      FEventer.AddHandle(FStderrEvent);
    end;
  end;
end;

function TLProcess.GetOnNeedInput: TLHandleEvent;
begin
  Result := FInputEvent.OnWrite;
end;

function TLProcess.GetOnHasOutput: TLHandleEvent;
begin
  Result := FOutputEvent.OnRead;
end;

function TLProcess.GetOnHasStderr: TLHandleEvent;
begin
  Result := FStderrEvent.OnRead;
end;

procedure TLProcess.SetOnNeedInput(NewOnInput: TLHandleEvent);
begin
  FInputEvent.OnWrite := NewOnInput;
end;

procedure TLProcess.SetOnHasOutput(NewOnOutput: TLHandleEvent);
begin
  FOutputEvent.OnRead := NewOnOutput;
end;

procedure TLProcess.SetOnHasStderr(NewOnStderr: TLHandleEvent);
begin
  FStderrEvent.OnRead := NewOnStderr;
end;

end.