Debian Codegraph @

components/virtualterminal/source/unix/vtemupty.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 Unix pseudoterminal device implementation
5
6 Copyright (C) 2021 Alexander Koblov ([email protected])
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 }
27
28 unit VTEmuPty;
29
30 {$mode delphi}
31
32 interface
33
34 uses
35 Classes, SysUtils, BaseUnix, TermIO, InitC, VTEmuCtl;
36
37 // Under Linux and BSD forkpty is situated in libutil.so library
38 {$IF NOT (DEFINED(DARWIN) OR DEFINED(HAIKU))}
39 {$LINKLIB util}
40 {$ENDIF}
41
42 type
43
44 { TPtyDevice }
45
46 TPtyDevice = class(TCustomPtyDevice)
47 private
48 Fpty: LongInt;
49 FThread: TThread;
50 FChildPid: THandle;
51 FEventPipe: TFilDes;
52 FLength, FCols, FRows: Integer;
53 FBuffer: array[0..8191] of AnsiChar;
54 protected
55 procedure ReadySync;
56 procedure ReadThread;
57 procedure SetConnected(AValue: Boolean); override;
58 function CreatePseudoConsole(const cmd: String): Boolean;
59 public
60 constructor Create(AOwner: TComponent); override;
61 destructor Destroy; override;
62 function WriteStr(const Str: string): Integer; override;
63 function SetCurrentDir(const Path: String): Boolean; override;
64 function SetScreenSize(aCols, aRows: Integer): Boolean; override;
65 end;
66
67 implementation
68
69 uses
70 Errors, DCOSUtils, DCStrUtils, DCUnix;
71
72 type
73 Pwinsize = ^winsize;
74 Ptermios = ^termios;
75
76 function forkpty(__amaster: Plongint; __name: Pchar; __termp: Ptermios; __winp: Pwinsize): longint;cdecl;external clib name 'forkpty';
77 function execl(__path: Pchar; __arg: Pchar): longint;cdecl;varargs;external clib name 'execl';
78
79 { TPtyDevice }
80
81 procedure TPtyDevice.SetConnected(AValue: Boolean);
82 var
83 AShell: String;
84 Symbol: Byte = 0;
85 begin
86 if FConnected = AValue then Exit;
87 FConnected:= AValue;
88
89 if FConnected then
90 begin
91 AShell:= mbGetEnvironmentVariable('SHELL');
92 if Length(AShell) = 0 then AShell:= '/bin/sh';
93 FConnected:= CreatePseudoConsole(AShell);
94 if FConnected then
95 begin
96 FThread:= TThread.ExecuteInThread(ReadThread);
97 end;
98 end
99 else begin
100 if FChildPid > 0 then
101 begin
102 FpKill(FChildPid, SIGTERM);
103 end;
104 FileWrite(FEventPipe[1], Symbol, 1);
105 end;
106 end;
107
108 procedure TPtyDevice.ReadySync;
109 begin
110 if Assigned(FOnRxBuf) then
111 FOnRxBuf(Self, FBuffer, FLength);
112 end;
113
114 procedure TPtyDevice.ReadThread;
115 var
116 ret: cint;
117 symbol: byte = 0;
118 fds: array[0..1] of tpollfd;
119 begin
120 fds[0].fd:= FEventPipe[0];
121 fds[0].events:= POLLIN;
122
123 fds[1].fd:= Fpty;
124 fds[1].events:= POLLIN;
125
126 while FConnected do
127 begin
128 repeat
129 ret:= fpPoll(@fds[0], 2, -1);
130 until (ret <> -1) or (fpGetErrNo <> ESysEINTR);
131 if (ret = -1) then
132 begin
133 WriteLn(SysErrorMessage(fpGetErrNo));
134 Break;
135 end;
136 if (fds[0].events and fds[0].revents <> 0) then
137 begin
138 while FileRead(fds[0].fd, symbol, 1) <> -1 do;
139 Break;
140 end;
141 if (fds[1].events and fds[1].revents <> 0) then
142 begin
143 FLength:= FileRead(Fpty, FBuffer, SizeOf(FBuffer));
144 if (FLength > 0) then TThread.Synchronize(FThread, ReadySync);
145 end;
146 end;
147 end;
148
149 function TPtyDevice.CreatePseudoConsole(const cmd: String): Boolean;
150 var
151 ws: TWinSize;
152 begin
153 ws.ws_row:= FRows;
154 ws.ws_col:= FCols;
155 ws.ws_xpixel:= 0;
156 ws.ws_ypixel:= 0;
157
158 FChildPid:= forkpty(@Fpty, nil, nil, @ws);
159
160 if FChildPid = 0 then
161 begin
162 FileCloseOnExecAll;
163 setenv('TERM', 'xterm-256color', 1);
164 execl(PAnsiChar(cmd), PAnsiChar(cmd), nil);
165 Errors.PError('execl() failed. Command: '+ cmd, cerrno);
166 fpExit(127);
167 end;
168 Result:= (FChildPid > 0);
169 end;
170
171 constructor TPtyDevice.Create(AOwner: TComponent);
172 begin
173 inherited Create(AOwner);
174 if fpPipe(FEventPipe) < 0 then
175 WriteLn(SysErrorMessage(fpGetErrNo))
176 else begin
177 // Set both ends of pipe non blocking
178 FileCloseOnExec(FEventPipe[0]); FileCloseOnExec(FEventPipe[1]);
179 FpFcntl(FEventPipe[0], F_SetFl, FpFcntl(FEventPipe[0], F_GetFl) or O_NONBLOCK);
180 FpFcntl(FEventPipe[1], F_SetFl, FpFcntl(FEventPipe[1], F_GetFl) or O_NONBLOCK);
181 end;
182 end;
183
184 destructor TPtyDevice.Destroy;
185 begin
186 SetConnected(False);
187 inherited Destroy;
188 FileClose(FEventPipe[0]);
189 FileClose(FEventPipe[1]);
190 end;
191
192 function TPtyDevice.WriteStr(const Str: string): Integer;
193 begin
194 Result:= FileWrite(Fpty, Pointer(Str)^, Length(Str));
195 end;
196
197 function TPtyDevice.SetCurrentDir(const Path: String): Boolean;
198 begin
199 Result:= WriteStr(' cd ' + EscapeNoQuotes(Path) + #13) > 0;
200 end;
201
202 function TPtyDevice.SetScreenSize(aCols, aRows: Integer): Boolean;
203 var
204 ws: TWinSize;
205 begin
206 ws.ws_row:= aRows;
207 ws.ws_col:= aCols;
208 ws.ws_xpixel:= 0;
209 ws.ws_ypixel:= 0;
210
211 Result:= FpIOCtl(Fpty,TIOCSWINSZ,@ws) = 0;
212
213 if Result then
214 begin
215 FCols:= aCols;
216 FRows:= aRows;
217 end;
218 end;
219
220 end.
221