Debian Codegraph @

components/virtualterminal/source/vtemuesc.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 Virtual terminal emulator escape codes
5
6 Alexander Koblov, 2021-2022
7
8 Based on ComPort Library
9 https://sourceforge.net/projects/comport
10 Author:
11 Dejan Crnila, 1998 - 2002
12 Maintainers:
13 Lars B. Dybdahl, 2003
14 License:
15 Public Domain
16 }
17
18 unit VTEmuEsc;
19
20 {$mode delphi}
21
22 interface
23
24 uses
25 Classes, LCLType;
26
27 type
28 // terminal character result
29 TEscapeResult = (erChar, erCode, erNothing);
30 // terminal escape codes
31 TEscapeCode = (ecUnknown, ecNotCompleted, ecCursorUp, ecCursorDown, ecCursorLeft,
32 ecCursorRight, ecCursorHome, ecCursorEnd, ecCursorMove, ecCursorMoveX, ecCursorMoveY,
33 ecReverseLineFeed, ecAppCursorLeft, ecAppCursorRight, ecAppCursorUp, ecAppCursorDown,
34 ecAppCursorHome, ecAppCursorEnd, ecCursorNextLine, ecCursorPrevLine, ecInsertKey,
35 ecDeleteKey, ecPageUpKey, ecPageDownKey,
36 ecMouseDown, ecMouseUp, ecEraseLineLeft, ecEraseLineRight, ecEraseScreenRight, ecEraseScreenLeft,
37 ecEraseLine, ecEraseScreen, ecEraseChar, ecDeleteChar, ecSetTab, ecClearTab, ecClearAllTabs,
38 ecIdentify, ecIdentResponse, ecQueryDevice, ecReportDeviceOK,
39 ecReportDeviceFailure, ecQueryCursorPos, ecReportCursorPos,
40 ecAttributes, ecSetMode, ecResetMode, ecReset, ecCharSet, ecSoftReset,
41 ecSaveCaretAndAttr, ecRestoreCaretAndAttr, ecSaveCaret, ecRestoreCaret,
42 ecTest, ecFuncKey, ecSetTextParams, ecScrollRegion, ecDeleteLine,
43 ecInsertLine, ecKeypadApp, ecKeypadNum, ecScrollUp, ecScrollDown);
44
45 // terminal escape codes processor
46 TEscapeCodes = class
47 private
48 FCharacter: TUTF8Char;
49 FCode: TEscapeCode;
50 FData: string;
51 FParams: TStrings;
52 public
53 constructor Create;
54 destructor Destroy; override;
55 function ProcessChar(const Ch: TUTF8Char): TEscapeResult; virtual; abstract;
56 function EscCodeToStr(Code: TEscapeCode; AParams: TStrings): string; virtual; abstract;
57 function GetParam(Num: Integer; AParams: TStrings): Integer;
58 property Data: string read FData;
59 property Code: TEscapeCode read FCode;
60 property Character: TUTF8Char read FCharacter;
61 property Params: TStrings read FParams;
62 end;
63
64 // VT52 escape codes
65 TEscapeCodesVT52 = class(TEscapeCodes)
66 private
67 FInSequence: Boolean;
68 function DetectCode(Str: string): TEscapeCode;
69 public
70 function ProcessChar(const Ch: TUTF8Char): TEscapeResult; override;
71 function EscCodeToStr(Code: TEscapeCode; AParams: TStrings): string; override;
72 end;
73
74 // ANSI/VT100 escape codes
75 TEscapeCodesVT100 = class(TEscapeCodes)
76 private
77 FInSequence: Boolean;
78 FInExtSequence: Boolean;
79 FInOscSequence: Boolean;
80 function DetectCode(Str: string): TEscapeCode;
81 function DetectExtCode(Str: string): TEscapeCode;
82 function DetectOscCode(Str: string): TEscapeCode;
83 public
84 function ProcessChar(const Ch: TUTF8Char): TEscapeResult; override;
85 function EscCodeToStr(Code: TEscapeCode; AParams: TStrings): string; override;
86 end;
87
88 implementation
89
90 uses
91 SysUtils;
92
93 (*****************************************
94 * TEscapeCodes class *
95 *****************************************)
96
97 constructor TEscapeCodes.Create;
98 begin
99 inherited Create;
100 FParams := TStringList.Create;
101 end;
102
103 destructor TEscapeCodes.Destroy;
104 begin
105 FParams.Free;
106 inherited Destroy;
107 end;
108
109 function TEscapeCodes.GetParam(Num: Integer; AParams: TStrings): Integer;
110 begin
111 if (AParams = nil) or (AParams.Count < Num) then
112 Result := 1
113 else
114 try
115 Result := StrToInt(AParams[Num - 1]);
116 except
117 Result := 1;
118 end;
119 end;
120
121 (*****************************************
122 * TEscapeCodesVT52 class *
123 *****************************************)
124
125 // process character
126 function TEscapeCodesVT52.ProcessChar(const Ch: TUTF8Char): TEscapeResult;
127 var
128 TempCode: TEscapeCode;
129 begin
130 Result := erNothing;
131 if not FInSequence then
132 begin
133 if Ch = #27 then
134 begin
135 FData := '';
136 FInSequence := True;
137 end
138 else begin
139 FCharacter := Ch;
140 Result := erChar;
141 end;
142 end else
143 begin
144 FData := FData + Ch;
145 TempCode := DetectCode(FData);
146 if TempCode <> ecNotCompleted then
147 begin
148 FCode := TempCode;
149 FInSequence := False;
150 Result := erCode;
151 end;
152 end;
153 end;
154
155 // escape code to string
156 function TEscapeCodesVT52.EscCodeToStr(Code: TEscapeCode; AParams: TStrings): string;
157 begin
158 case Code of
159 ecCursorUp: Result := #27'A';
160 ecCursorDown: Result := #27'B';
161 ecCursorRight: Result := #27'C';
162 ecCursorLeft: Result := #27'D';
163 ecCursorHome: Result := #27'H';
164 ecReverseLineFeed: Result := #27'I';
165 ecEraseScreenRight: Result := #27'J';
166 ecEraseLineRight: Result := #27'K';
167 ecIdentify: Result := #27'Z';
168 ecIdentResponse: Result := #27'/Z';
169 ecCursorMove: Result := #27'Y' +
170 Chr(GetParam(1, AParams) + 31) + Chr(GetParam(2, AParams) + 31);
171 else
172 Result := '';
173 end;
174 end;
175
176 // get escape code from string
177 function TEscapeCodesVT52.DetectCode(Str: string): TEscapeCode;
178 begin
179 Result := ecUnknown;
180 case Str[1] of
181 'A': Result := ecCursorUp;
182 'B': Result := ecCursorDown;
183 'C': Result := ecCursorRight;
184 'D': Result := ecCursorLeft;
185 'H': Result := ecCursorMove;
186 'I': Result := ecReverseLineFeed;
187 'J': Result := ecEraseScreenRight;
188 'K': Result := ecEraseLineRight;
189 'Z': Result := ecIdentify;
190 '/': begin
191 if Length(Str) = 1 then
192 Result := ecNotCompleted
193 else
194 if (Length(Str) = 2) and (Str = '/Z') then
195 Result := ecIdentResponse;
196 end;
197 'Y': begin
198 if Length(Str) < 3 then
199 Result := ecNotCompleted
200 else
201 begin
202 Result := ecCursorMove;
203 FParams.Add(IntToStr(Ord(Str[3]) - 31));
204 FParams.Add(IntToStr(Ord(Str[2]) - 31));
205 end;
206 end;
207 end;
208 end;
209
210 (*****************************************
211 * TEscapeCodesVT100class *
212 *****************************************)
213
214 // process character
215 function TEscapeCodesVT100.ProcessChar(const Ch: TUTF8Char): TEscapeResult;
216 var
217 TempCode: TEscapeCode;
218 begin
219 Result := erNothing;
220 if not FInSequence then
221 begin
222 if Ch = #27 then
223 begin
224 FData := '';
225 FInSequence := True;
226 end
227 else begin
228 FCharacter := Ch;
229 Result := erChar;
230 end;
231 end else
232 begin
233 FData := FData + Ch;
234 TempCode := ecNotCompleted;
235 if FInExtSequence then
236 TempCode := DetectExtCode(FData)
237 else if FInOscSequence then
238 TempCode := DetectOscCode(FData)
239 else
240 // character [ after ESC defines extended escape code
241 if FData[1] = '[' then
242 FInExtSequence := True
243 else if FData[1] = ']' then
244 FInOscSequence := True
245 else
246 TempCode := DetectCode(FData);
247 if TempCode <> ecNotCompleted then
248 begin
249 FCode := TempCode;
250 FInSequence := False;
251 FInExtSequence := False;
252 FInOscSequence := False;
253 Result := erCode;
254 end;
255 end;
256 end;
257
258 // escape code to string conversion
259 function TEscapeCodesVT100.EscCodeToStr(Code: TEscapeCode;
260 AParams: TStrings): string;
261 var
262 AKey: Integer;
263 begin
264 case Code of
265 ecIdentify: Result := #27'[c';
266 ecIdentResponse: Result := Format(#27'[?1;%dc', [GetParam(1, AParams)]);
267 ecQueryCursorPos: Result := #27'[6n';
268 ecReportCursorPos: Result := Format(#27'[%d;%dR', [GetParam(1, AParams), GetParam(2, AParams)]);
269 ecQueryDevice: Result := #27'[5n';
270 ecReportDeviceOK: Result := #27'[0n';
271 ecReportDeviceFailure: Result := #27'[3n';
272 ecCursorUp: Result := #27'[A';
273 ecCursorDown: Result := #27'[B';
274 ecCursorRight: Result := #27'[C';
275 ecAppCursorLeft: Result := #27'OD';
276 ecAppCursorUp: Result := #27'OA';
277 ecAppCursorDown: Result := #27'OB';
278 ecAppCursorRight: Result := #27'OC';
279 ecAppCursorHome: Result := #27'OH';
280 ecAppCursorEnd: Result := #27'OF';
281 ecCursorLeft: Result := #27'[D';
282 ecCursorHome: Result := #27'[H';
283 ecCursorEnd: Result := #27'[F';
284 ecCursorMove: Result := Format(#27'[%d;%df', [GetParam(1, AParams), GetParam(2, AParams)]);
285 ecEraseScreenRight: Result := #27'[J';
286 ecEraseLineRight: Result := #27'[K';
287 ecEraseScreen: Result := #27'[2J';
288 ecEraseLine: Result := #27'[2K';
289 ecSetTab: Result := #27'H';
290 ecClearTab: Result := #27'[g';
291 ecClearAllTabs: Result := #27'[3g';
292 ecAttributes: Result := #27'[m'; // popravi
293 ecSetMode: Result := #27'[h';
294 ecResetMode: Result := #27'[l';
295 ecReset: Result := #27'c';
296 ecSaveCaret: Result := #27'[s';
297 ecRestoreCaret: Result := #27'[u';
298 ecSaveCaretAndAttr: Result := #27'7';
299 ecRestoreCaretAndAttr: Result := #27'8';
300 ecTest: Result := #27'#8';
301 ecFuncKey:
302 begin
303 AKey:= GetParam(1, AParams);
304 case AKey of
305 0: Result := #27'OP';
306 1: Result := #27'OQ';
307 2: Result := #27'OR';
308 3: Result := #27'OS';
309 4: Result := #27'[15~';
310 5: Result := #27'[17~';
311 6: Result := #27'[18~';
312 7: Result := #27'[19~';
313 8: Result := #27'[20~';
314 9: Result := #27'[21~';
315 10: Result := #27'[23~';
316 11: Result := #27'[24~';
317 end;
318 end;
319 ecInsertKey: Result := #27'[2~';
320 ecDeleteKey: Result := #27'[3~';
321 ecPageUpKey: Result := #27'[5~';
322 ecPageDownKey: Result := #27'[6~';
323 ecMouseDown:
324 Result := Format(#27'[<%d;%d;%dM', [GetParam(1, AParams), GetParam(2, AParams), GetParam(3, AParams)]);
325 ecMouseUp:
326 Result := Format(#27'[<%d;%d;%dm', [GetParam(1, AParams), GetParam(2, AParams), GetParam(3, AParams)]);
327 else
328 Result := '';
329 end;
330 end;
331
332 // get vt100 escape code from string
333 function TEscapeCodesVT100.DetectCode(Str: string): TEscapeCode;
334 begin
335 if Length(Str) = 1 then
336 case Str[1] of
337 'H': Result := ecSetTab;
338 'c': Result := ecReset;
339 'M': Result := ecReverseLineFeed;
340 '7': Result := ecSaveCaretAndAttr;
341 '8': Result := ecRestoreCaretAndAttr;
342 '=': Result := ecKeypadApp;
343 '>': Result := ecKeypadNum;
344 '#': Result := ecNotCompleted;
345 'O': Result := ecNotCompleted;
346 '(': Result := ecNotCompleted;
347 else
348 Result := ecUnknown;
349 end
350 else
351 begin
352 Result := ecUnknown;
353 if Str = '#8' then
354 Result := ecTest
355 else if Str[1] = 'O' then
356 begin
357 case Str[2] of
358 'A': Result := ecAppCursorUp;
359 'B': Result := ecAppCursorDown;
360 'C': Result := ecAppCursorRight;
361 'D': Result := ecAppCursorLeft;
362 'H': Result := ecAppCursorHome;
363 'F': Result := ecAppCursorEnd;
364 end;
365 end
366 else if Str[1] = '(' then
367 begin
368 FParams.Clear;
369 FParams.Add(Str[2]);
370 Result:= ecCharSet;
371 end;
372 end;
373 end;
374
375 // get extended vt100 escape code from string
376 function TEscapeCodesVT100.DetectExtCode(Str: string): TEscapeCode;
377 var
378 LastCh: Char;
379 TempParams: TStrings;
380
381 procedure ParseParams(Str: string);
382 var
383 I: Integer;
384 TempStr: string;
385 begin
386 I := 1;
387 TempStr := '';
388 while I <= Length(Str) do
389 begin
390 if (Str[I] = ';') and (TempStr <> '') then
391 begin
392 TempParams.Add(TempStr);
393 TempStr := '';
394 end
395 else
396 TempStr := TempStr + Str[I];
397 Inc(I);
398 end;
399 if (TempStr <> '') then
400 TempParams.Add(TempStr);
401 end;
402
403 function CodeEraseScreen: TEscapeCode;
404 var
405 Str: string;
406 begin
407 if TempParams.Count = 0 then
408 Result := ecEraseScreenRight
409 else
410 begin
411 Str := TempParams[0];
412 case Str[1] of
413 '0': Result := ecEraseScreenRight;
414 '1': Result := ecEraseScreenLeft;
415 '2': Result := ecEraseScreen;
416 else
417 Result := ecUnknown;
418 end;
419 end;
420 TempParams.Clear;
421 end;
422
423 function CodeEraseLine: TEscapeCode;
424 var
425 Str: string;
426 begin
427 if TempParams.Count = 0 then
428 Result := ecEraseLineRight
429 else
430 begin
431 Str := TempParams[0];
432 case Str[1] of
433 '0': Result := ecEraseLineRight;
434 '1': Result := ecEraseLineLeft;
435 '2': Result := ecEraseLine;
436 else
437 Result := ecUnknown;
438 end;
439 end;
440 TempParams.Clear;
441 end;
442
443 function CodeTab: TEscapeCode;
444 var
445 Str: string;
446 begin
447 if TempParams.Count = 0 then
448 Result := ecClearTab
449 else
450 begin
451 Str := TempParams[0];
452 case Str[1] of
453 '0': Result := ecClearTab;
454 '3': Result := ecClearAllTabs;
455 else
456 Result := ecUnknown;
457 end;
458 end;
459 TempParams.Clear;
460 end;
461
462 function CodeDevice: TEscapeCode;
463 var
464 Str: string;
465 begin
466 if TempParams.Count = 0 then
467 Result := ecUnknown
468 else
469 begin
470 Str := TempParams[0];
471 case Str[1] of
472 '5': Result := ecQueryDevice;
473 '0': Result := ecReportDeviceOK;
474 '3': Result := ecReportDeviceFailure;
475 '6': Result := ecQueryCursorPos;
476 else
477 Result := ecUnknown;
478 end;
479 end;
480 TempParams.Clear;
481 end;
482
483 function CodeIdentify: TEscapeCode;
484 begin
485 if (TempParams.Count = 0) or
486 ((TempParams.Count = 1) and (TempParams[0] = '0'))
487 then
488 Result := ecIdentify
489 else
490 if (TempParams.Count = 2) and (TempParams[1] = '?1') then
491 Result := ecIdentResponse
492 else
493 Result := ecUnknown;
494 end;
495
496 begin
497 Result := ecNotCompleted;
498 LastCh := Str[Length(Str)];
499 {$IFDEF Unicode} if not CharInSet(LastCh,['A'..'Z', 'a'..'z']) then Exit;
500 {$ELSE} if not (LastCh in ['A'..'Z', 'a'..'z']) then Exit; {$ENDIF}
501 TempParams := TStringList.Create;
502 try
503 ParseParams(Copy(Str, 2, Length(Str) - 2));
504 case LastCh of
505 'A': Result := ecCursorUp;
506 'B': Result := ecCursorDown;
507 'C': Result := ecCursorRight;
508 'D': Result := ecCursorLeft;
509 'E': Result := ecCursorNextLine;
510 'F': Result := ecCursorPrevLine;
511 'H': Result := ecCursorMove;
512 'f': Result := ecCursorMove;
513 'd': Result := ecCursorMoveY;
514 'G': Result := ecCursorMoveX;
515 'J': Result := CodeEraseScreen;
516 'K': Result := CodeEraseLine;
517 'X': Result := ecEraseChar;
518 'P': Result := ecDeleteChar;
519 'g': Result := CodeTab;
520 'm': Result := ecAttributes;
521 'h': Result := ecSetMode;
522 'l': Result := ecResetMode;
523 's': Result := ecSaveCaret;
524 'u': Result := ecRestoreCaret;
525 'n': Result := CodeDevice;
526 'c': Result := CodeIdentify;
527 'R': Result := ecReportCursorPos;
528 'r': Result := ecScrollRegion;
529 'S': Result := ecScrollUp;
530 'T': Result := ecScrollDown;
531 'L': Result := ecInsertLine;
532 'M': Result := ecDeleteLine;
533 'p': Result := ecSoftReset;
534 else
535 Result := ecUnknown;
536 end;
537 FParams.Assign(TempParams);
538 finally
539 TempParams.Free;
540 end;
541 end;
542
543 function TEscapeCodesVT100.DetectOscCode(Str: string): TEscapeCode;
544 var
545 LastCh: Char;
546 begin
547 Result := ecNotCompleted;
548 LastCh := Str[Length(Str)];
549 if (LastCh = #7) then
550 begin
551 Result:= ecSetTextParams;
552 end;
553 end;
554
555 end.