-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.c
More file actions
970 lines (909 loc) · 26.4 KB
/
terminal.c
File metadata and controls
970 lines (909 loc) · 26.4 KB
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
// https://espterm.github.io/docs/espterm-xterm.html
#include "terminal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "color.h"
#include "utf8.h"
static int min(const int a, const int b) {
return a < b ? a : b;
}
static int max(const int a, const int b) {
return a > b ? a : b;
}
static int par(const vtparse_t *p, const unsigned short int num, const int default_value) {
if (!num || num > p->num_params) return default_value;
return p->params[num - 1];
}
static int feedback(const terminal_t *terminal, const char *output, size_t length) {
if (!length) length = strlen(output);
return (int)write(terminal->feedback_fd, output, length);
}
static void do_print(const vtparse_t *p, const unsigned char ch, const int update_cursor) {
terminal_t *terminal = p->user_data;
screen_t *screen = terminal->screen;
// write(STDOUT_FILENO, &ch, 1);
// check if previous print was in the last screen position
if (screen->cursor.x >= screen->columns) {
screen->cursor.x = 0;
screen_scroll_v(screen, 1);
}
terminal->last_printed = (char)ch;
screen_put_ch(screen, (char)ch);
if (update_cursor) {
if (++screen->cursor.x >= screen->columns) {
// this is the last column
if (screen->cursor.y + 1 < screen->rows) {
screen->cursor.y++;
screen->cursor.x = 0;
}
// else {
// follows classic VT100-style "newline glitch" / xenl semantics
// it stays "in a limbo state" at column 81 / off right edge
// }
}
}
}
static int do_csi_pmod(vtparse_t *p, const int set) {
terminal_t *terminal = p->user_data;
const screen_t *screen = terminal->screen;
for (int i = 0; i < p->num_params; i++) {
const int ps = p->params[i];
switch (p->intermediate_chars[0]) {
case '\0':
switch (ps) {
case 20: // Automatic Newline (LNM).
terminal->lnm = set;
break;
// Default case (ignore unknown attributes)
default:
return -1;
}
break;
case '?':
switch (ps) {
case 1049: // Save cursor as in DECSC and use Alternate Screen Buffer, clearing it first.
screen_fill(screen, nullptr, screen->bg_color);
break;
// When set with CSI ? 1004 h, the terminal reports focus changes: ESC [ 1004 h for FocusIn (window
// gained focus) and ESC [ 1004 l for FocusOut (window lost focus). This helps applications like
// tmux or GUI tools track window focus without relying on OS events.
case 1004:
// CSI ? 2004 h/l (with "h" for "set") enables bracketed paste mode, where pasted text gets wrapped
// in special markers: ESC [ 200 ~ before the text and ESC [ 201 ~ after. This lets applications
// (like shells or editors) detect pasted input separately from typed text, avoiding issues like
// unwanted auto-indentation or command execution.
case 2004:
default:
return -1;
}
break;
default:
return -1;
}
}
return 0;
}
static int do_csi_sgr(vtparse_t *p) {
const terminal_t *terminal = p->user_data;
screen_t *screen = terminal->screen;
if (p->num_params == 0) {
reset:
// Default: reset all attributes
screen->fg_color = DEFAULT_FG;
screen->bg_color = DEFAULT_BG;
// TODO: reset other attributes (bold, underline, etc.)
return 0;
}
uint32_t *color_ptr = nullptr;
for (int i = 0; i < p->num_params; i++) {
switch (p->params[i]) {
case 0: // Reset all attributes
goto reset;
// Text style
case 1: // Bold
// TODO: implement bold handling
break;
case 2: // Faint
// TODO: implement faint handling
break;
case 3: // Italic
// TODO: implement italic handling
break;
case 4: // Underline
// TODO: implement underline handling
break;
case 5: // Blink
// TODO: implement blink handling
break;
case 7: // Reverse video
// TODO: implement reverse video handling
break;
case 8: // Conceal
// TODO: implement conceal handling
break;
case 9: // Crossed-out
// TODO: implement crossed-out handling
break;
// Reset style
case 20: // Fraktur off
// TODO: implement Fraktur off handling
break;
case 21: // Doubly-underlined off or Bold off
// TODO: implement double underline/bold off handling
break;
case 22: // Normal intensity (neither bold nor faint)
// TODO: implement normal intensity handling
break;
case 23: // Not italicized or Fraktur
// TODO: implement not italicized handling
break;
case 24: // Not underlined
// TODO: implement not underlined handling
break;
case 25: // Steady (not blinking)
// TODO: implement steady handling
break;
case 27: // Positive (not inverse)
// TODO: implement positive handling
break;
case 28: // Visible (not hidden)
// TODO: implement visible handling
break;
case 29: // Not crossed-out
// TODO: implement not crossed-out handling
break;
// Foreground colors (30-37)
case 30:
screen->fg_color = COLOR_BLACK;
break;
case 31:
screen->fg_color = COLOR_RED;
break;
case 32:
screen->fg_color = COLOR_GREEN;
break;
case 33:
screen->fg_color = COLOR_YELLOW;
break;
case 34:
screen->fg_color = COLOR_BLUE;
break;
case 35:
screen->fg_color = COLOR_MAGENTA;
break;
case 36:
screen->fg_color = COLOR_CYAN;
break;
case 37:
screen->fg_color = COLOR_WHITE;
break;
case 39:
screen->fg_color = DEFAULT_FG;
break;
// Background colors (40-47)
case 40:
screen->bg_color = COLOR_BLACK;
break;
case 41:
screen->bg_color = COLOR_RED;
break;
case 42:
screen->bg_color = COLOR_GREEN;
break;
case 43:
screen->bg_color = COLOR_YELLOW;
break; // Yellow
case 44:
screen->bg_color = COLOR_BLUE;
break;
case 45:
screen->bg_color = COLOR_MAGENTA;
break; // Magenta
case 46:
screen->bg_color = COLOR_CYAN;
break; // Cyan
case 47:
screen->bg_color = COLOR_WHITE;
break;
case 49:
screen->bg_color = DEFAULT_BG;
break;
// Bright foreground colors (90-97)
case 90:
screen->fg_color = COLOR_BRIGHT_BLACK;
break;
case 91:
screen->fg_color = COLOR_BRIGHT_RED;
break;
case 92:
screen->fg_color = COLOR_BRIGHT_GREEN;
break;
case 93:
screen->fg_color = COLOR_BRIGHT_YELLOW;
break;
case 94:
screen->fg_color = COLOR_BRIGHT_BLUE;
break;
case 95:
screen->fg_color = COLOR_BRIGHT_MAGENTA;
break;
case 96:
screen->fg_color = COLOR_BRIGHT_CYAN;
break;
case 97:
screen->fg_color = COLOR_BRIGHT_WHITE;
break;
// Bright background colors (100-107)
case 100:
screen->bg_color = COLOR_BRIGHT_BLACK;
break;
case 101:
screen->bg_color = COLOR_BRIGHT_RED;
break;
case 102:
screen->bg_color = COLOR_BRIGHT_GREEN;
break;
case 103:
screen->bg_color = COLOR_BRIGHT_YELLOW;
break;
case 104:
screen->bg_color = COLOR_BRIGHT_BLUE;
break;
case 105:
screen->bg_color = COLOR_BRIGHT_MAGENTA;
break;
case 106:
screen->bg_color = COLOR_BRIGHT_CYAN;
break;
case 107:
screen->bg_color = COLOR_BRIGHT_WHITE;
break;
// 256-color mode
case 38: // Foreground
color_ptr = &screen->fg_color;
goto color256;
case 48: // Background
color_ptr = &screen->bg_color;
color256:
// Pm = 3 8 ; 2 ; Pr; Pg; Pb -> Set color to the closest match in xterm's palette for the given RGB Pr/Pg/Pb.
if (p->params[i + 1] == 2 && i + 4 < p->num_params) {
// 24-bit RGB mode
*color_ptr = 0xFF << 24 | p->params[i + 2] << 16 | p->params[i + 3] << 8 | p->params[i + 4];
i += 4; // Skip next four parameters
}
// Pm = 3 8 ; 5 ; Ps -> Set color to Ps.
else if (p->params[i + 1] == 5 && i + 2 < p->num_params) {
// 256-color mode
const int color_index = p->params[i + 2];
*color_ptr = 0xFF << 24 | terminal->color_list[color_index & 0xFF];
i += 2; // Skip next two parameters
}
else {
return -1;
}
break;
// Default case (ignore unknown attributes)
default:
return -1;
}
}
return 0;
}
static void do_csi(vtparse_t *p, const unsigned char ch) {
terminal_t *terminal = p->user_data;
screen_t *screen = terminal->screen;
rect_t rect;
int n;
const int cur_x = screen->cursor.x;
const int cur_y = screen->cursor.y;
const int cur_max_y = screen->rows - 1;
const int cur_max_x = screen->columns - 1;
switch (ch) {
case '@':
// CSI Ps @ Insert Ps (Blank) Character(s) (default = 1) (ICH).
n = min(par(p, 1, 1), screen->columns - cur_x);
if (n > 0) {
rect_t src = {
.x = cur_x,
.y = cur_y,
.w = screen->columns - cur_x - n,
.h = 1
};
pt_t dst = {
.x = cur_x + n,
.y = cur_y
};
screen_move_clr(screen, &dst, &src);
}
break;
case 'A':
// CSI Ps A Cursor Up Ps Times (default = 1) (CUU).
screen->cursor.y = max(0, cur_y - par(p, 1, 1));
break;
case 'B':
// CSI Ps B Cursor Down Ps Times (default = 1) (CUD).
screen->cursor.y = min(cur_max_y, cur_y + par(p, 1, 1));
break;
case 'C':
// CSI Ps C Cursor Forward Ps Times (default = 1) (CUF).
screen->cursor.x = min(cur_max_x, cur_x + par(p, 1, 1));
break;
case 'D':
// CSI Ps D Cursor Backward Ps Times (default = 1) (CUB).
screen->cursor.x = max(0, cur_x - par(p, 1, 1));
break;
case 'E':
// CSI Ps E Cursor Next Line Ps Times (default = 1) (CNL).
screen->cursor.x = 0;
screen->cursor.y = min(cur_max_y, cur_y + par(p, 1, 1));
break;
case 'F':
// CSI Ps F Cursor Preceding Line Ps Times (default = 1) (CPL).
screen->cursor.x = 0;
screen->cursor.y = max(0, cur_y - par(p, 1, 1));
break;
case 'G':
// CSI Ps G Cursor Character Absolute [column] (default = [row,1]) (CHA).
screen->cursor.x = min(cur_max_x, max(0, par(p, 1, 1) - 1));
break;
case 'H':
// CSI Ps ; Ps H Cursor Position [row;column] (default = [1,1]) (CUP).
screen->cursor.y = min(cur_max_y, max(0, par(p, 1, 1) - 1));
screen->cursor.x = min(cur_max_x, max(0, par(p, 2, 1) - 1));
break;
case 'I':
// CSI Ps I Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
screen->cursor.x = min(cur_max_x, (cur_x / 8 + par(p, 1, 1)) * 8);
break;
case 'J':
// CSI Ps J Erase in Display (ED)
switch (par(p, 1, 0)) {
case 0: // Erase from cursor to end of screen
// clear current line from cursor to end
rect = (rect_t){.x = cur_x, .y = cur_y, .w = screen->columns - cur_x, .h = 1};
screen_fill(screen, &rect, screen->bg_color);
// clear lines below cursor
if (cur_y + 1 < screen->rows) {
rect = (rect_t){.x = 0, .y = cur_y + 1, .w = screen->columns, .h = screen->rows - (cur_y + 1)};
screen_fill(screen, &rect, screen->bg_color);
}
break;
case 1: // Erase from cursor to beginning of screen
// clear current line from beginning to cursor
rect = (rect_t){.x = 0, .y = cur_y, .w = cur_x + 1, .h = 1};
screen_fill(screen, &rect, screen->bg_color);
// clear lines above cursor
if (cur_y > 0) {
rect = (rect_t){.x = 0, .y = 0, .w = screen->columns, .h = cur_y};
screen_fill(screen, &rect, screen->bg_color);
}
break;
case 2: // Erase entire screen
case 3: // Erase entire screen and delete scrollback (treated same as 2)
rect = (rect_t){.x = 0, .y = 0, .w = screen->columns, .h = screen->rows};
screen_fill(screen, &rect, screen->bg_color);
break;
default:
goto unhandled;
}
break;
case 'K':
// CSI Ps K Erase in Line (EL).
switch (par(p, 1, 0)) {
case 0: // Erase from cursor to end of line
rect.x = cur_x;
rect.y = cur_y;
rect.w = screen->columns - rect.x;
rect.h = 1;
break;
case 1: // Erase from cursor to beginning of line
rect.x = 0;
rect.y = cur_y;
rect.w = cur_x + 1;
rect.h = 1;
break;
case 2: // Erase entire line
rect.x = 0;
rect.y = cur_y;
rect.w = screen->columns;
rect.h = 1;
break;
default:
goto unhandled;
}
screen_fill(screen, &rect, screen->bg_color);
break;
case 'L':
// CSI Ps L Insert Ps Line(s) (default = 1) (IL).
n = min(par(p, 1, 1), screen->rows - cur_y);
if (n > 0) {
rect_t src = {
.x = 0,
.y = cur_y,
.w = screen->columns,
.h = screen->rows - cur_y - n
};
pt_t dst = {
.x = 0,
.y = cur_y + n
};
screen_move_clr(screen, &dst, &src);
}
break;
case 'M':
// CSI Ps M Delete Ps Line(s) (default = 1) (DL).
n = min(par(p, 1, 1), screen->rows - cur_y);
if (n > 0) {
rect_t src = {
.x = 0,
.y = cur_y + n,
.w = screen->columns,
.h = screen->rows - cur_y - n
};
pt_t dst = {
.x = 0,
.y = cur_y
};
screen_move_clr(screen, &dst, &src);
}
break;
case 'P':
// CSI Ps P Delete Ps Character(s) (default = 1) (DCH).
n = min(par(p, 1, 1), screen->columns - cur_x);
if (n > 0) {
rect_t src = {
.x = cur_x + n,
.y = cur_y,
.w = screen->columns - cur_x - n,
.h = 1
};
pt_t dst = {
.x = cur_x,
.y = cur_y
};
screen_move_clr(screen, &dst, &src);
}
break;
case 'S':
// CSI Ps S Scroll up Ps lines (default = 1) (SU).
screen_scroll_v(screen, min(screen->rows, par(p, 1, 1)));
break;
case 'T':
// CSI Ps T Scroll down Ps lines (default = 1) (SD).
screen_scroll_v(screen, -min(screen->rows, par(p, 1, 1)));
break;
case 'X':
// CSI Ps X Erase Ps Character(s) (default = 1) (ECH).
rect = (rect_t){
.x = cur_x,
.y = cur_y,
.w = min(par(p, 1, 1), screen->columns - cur_x),
.h = 1
};
screen_fill(screen, &rect, screen->bg_color);
break;
case 'Z':
// CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
n = par(p, 1, 1);
while (n-- > 0) {
int x = (cur_x - 1) & ~7;
if (x < 0) x = 0;
screen->cursor.x = x;
}
break;
case '`':
// CSI Pm ` Character Position Absolute [column] (default = [row,1]) (HPA).
screen->cursor.x = min(cur_max_x, max(0, par(p, 1, 1) - 1));
break;
case 'a':
// CSI Pm a Character Position Relative [columns] (default = [row,col+1]) (HPR).
screen->cursor.x = min(cur_max_x, max(0, cur_x + par(p, 1, 1)));
break;
case 'b':
// CSI Ps b Repeat the preceding graphic character Ps times (REP).
n = min(par(p, 1, 1), screen->columns - cur_x);
if (n > 0) {
for (int i = 0; i < n; i++) {
screen_put_ch(screen, terminal->last_printed);
}
}
break;
case 'c':
// CSI Ps c Send Device Attributes (Primary DA).
// Ps = 0 or omitted -> request attributes from terminal.
if (par(p, 1, 0) != 0) goto unhandled;
feedback(terminal, "\x1B[?6c", 5);
break;
case 'd':
// CSI Pm d Line Position Absolute [row] (default = [1,column]) (VPA).
screen->cursor.y = min(cur_max_y, max(0, par(p, 1, 1) - 1));
break;
case 'e':
// CSI Pm e Line Position Relative [rows] (default = [row+1,column]) (VPR).
screen->cursor.y = min(cur_max_y, max(0, cur_y + par(p, 1, 1)));
break;
case 'f':
// CSI Ps ; Ps f Horizontal and Vertical Position [row;column] (default = [1,1]) (HVP).
screen->cursor.x = min(cur_max_x, max(0, par(p, 2, 1) - 1));
screen->cursor.y = min(cur_max_y, max(0, par(p, 1, 1) - 1));
break;
case 'h':
// DEC? Private Mode Set (SM/DECSET).
if (do_csi_pmod(p, 1)) goto unhandled;
break;
case 'l':
// DEC? Private Mode Reset (RM/DECRST).
if (do_csi_pmod(p, 0)) goto unhandled;
break;
case 'm':
// SGR – Select Graphic Rendition
if (do_csi_sgr(p)) goto unhandled;
break;
case 'n':
// Device Status Report (DSR)
switch (par(p, 1, 0)) {
case 5: {
// Pn=5 requests operating status (terminal replies CSI 0 n if OK, CSI 3 n if not)
feedback(terminal, "\x1B[0n", 4);
break;
}
case 6: {
// Pn=6 requests cursor position (terminal replies CSI row ; col R).
char response[32];
int len = snprintf(response, sizeof(response), "\x1B[%d;%dR", cur_y + 1, cur_x + 1);
if (len > 0) {
feedback(terminal, response, len);
}
break;
}
default:
goto unhandled;
}
break;
// case 'r':
// // Set Scrolling Region [top;bottom] (default = full size of win-dow) (DECSTBM).
// // TODO: Set scrolling region
// break;
// case 't':
// // Window manipulation (from dtterm, as well as extensions).
// // TODO: Save xterm icon and window title on stack and others
// break;
default:
unhandled:
printf("Unhandled CSI command: %s%c, params:", (const char *)p->intermediate_chars, ch);
for (int i = 0; i < p->num_params; i++) {
printf(" %d", p->params[i]);
}
printf("\n");
break;
}
}
static void do_osc_start(vtparse_t *p) {
terminal_t *terminal = p->user_data;
// initialize OSC buffer and command
terminal->osc_buffer_len = 0;
memset(terminal->osc_buffer, 0, sizeof(terminal->osc_buffer));
}
static void do_osc_put(vtparse_t *p, const unsigned char ch) {
terminal_t *terminal = p->user_data;
// append character to OSC buffer if there's space
if (terminal->osc_buffer_len < sizeof(terminal->osc_buffer) - 1) {
terminal->osc_buffer[terminal->osc_buffer_len++] = (char)ch;
}
}
static void do_osc(vtparse_t *p) {
terminal_t *terminal = p->user_data;
screen_t *screen = terminal->screen;
uint32_t *color_ptr = nullptr;
uint32_t color;
static const char *title_icon[3] = {
"icon name and window title",
"icon name",
"window title",
};
if (!terminal->osc_buffer_len) return;
int ps;
char *buf = terminal->osc_buffer;
char *semicolon = strchr(buf, ';');
if (semicolon) {
*semicolon = '\0';
ps = atoi(buf);
buf = semicolon + 1; // Pt starts here
}
else {
// No ';', treat whole buffer as Pt, ps from command or assume 0
ps = 0;
}
// handle OSC commands
switch (ps) {
case 0: // Change window title and icon name
case 1:
case 2:
// Set window title
printf("Set %s to: %s\n", title_icon[ps], buf);
break;
case 4: {
// Change color palette
unsigned int idx, r, g, b;
if (sscanf(buf, "%u;rgb:%02x/%02x/%02x", &idx, &r, &g, &b) == 4) {
color = (0xFFu << 24) | (r << 16) | (g << 8) | b;
if (idx < 256) terminal->color_list[idx] = color;
}
break;
}
case 10:
// Change foreground color
color = DEFAULT_FG;
color_ptr = &screen->fg_color;
goto color_parse;
case 11:
// Change background color
color = DEFAULT_BG;
color_ptr = &screen->bg_color;
goto color_parse;
case 12:
// Change cursor color
color = DEFAULT_FG;
color_ptr = &screen->cursor_color;
color_parse:
if (strncmp(terminal->osc_buffer, "rgb:", 4) == 0) {
unsigned int r, g, b;
if (sscanf(terminal->osc_buffer + 4, "%02x/%02x/%02x", &r, &g, &b) == 3) {
color = (0xFF << 24) | (r << 16) | (g << 8) | b;
}
}
else if (strncmp(terminal->osc_buffer, "?", 1) == 0) {
// Query color - not implemented
}
else {
// Try to parse as color index
unsigned int index;
if (sscanf(terminal->osc_buffer, "%u", &index) == 1) {
if (index < 256) {
color = 0xFF << 24 | terminal->color_list[index];
}
}
}
*color_ptr = color;
break;
default:
printf("OSC: Unhandled command %d: %s\n", ps, terminal->osc_buffer);
break;
}
// Reset OSC state
terminal->osc_buffer_len = 0;
memset(terminal->osc_buffer, 0, sizeof(terminal->osc_buffer));
}
void do_esc(vtparse_t *p, const unsigned char ch) {
terminal_t *terminal = p->user_data;
screen_t *screen = terminal->screen;
switch (ch) {
case '=':
terminal->keypad = KEYPAD_DECPAM;
break;
case '>':
terminal->keypad = KEYPAD_NORMAL;
break;
case '6': // DECBI – Back Index (VT420+)
if (screen->cursor.x > 0) {
screen->cursor.x--;
}
else {
// insert one blank at left margin, shift rest of line right
const rect_t src = {.x = 0, .y = screen->cursor.y, .w = screen->columns - 1, .h = 1};
const pt_t dst = {.x = 1, .y = screen->cursor.y};
screen_move_clr(screen, &dst, &src);
// cursor stays at x=0
}
break;
case '9': // DECFI – Forward Index (VT420+)
if (screen->cursor.x < screen->columns - 1) {
screen->cursor.x++;
}
else {
// insert one blank at right margin, shift rest of line left
const rect_t src = {.x = 1, .y = screen->cursor.y, .w = screen->columns - 1, .h = 1};
const pt_t dst = {.x = 0, .y = screen->cursor.y};
screen_move_clr(screen, &dst, &src);
// cursor stays at x=columns-1
}
break;
case '7':
// Save cursor position (DECSC)
terminal->saved_cursor = screen->cursor;
break;
case '8':
// Restore Cursor (DECRC)
screen->cursor = terminal->saved_cursor;
break;
case 'B':
switch (p->intermediate_chars[0]) {
case '(':
// skip charset
break;
default:
goto unhandled;
}
break;
case '\\': // String Terminator (ST)
// This terminates OSC, APC, PM, or SOS strings
if (terminal->osc_buffer_len) {
do_osc(p);
}
// For APC, PM, and SOS, we don't have specific handling yet
// Reset any state related to these sequences if needed
break;
default:
unhandled:
printf("Unhandled ESC %s %c\n", (const char *)p->intermediate_chars, ch);
break;
}
}
static void do_control(vtparse_t *p, const unsigned char ch) {
const terminal_t *terminal = p->user_data;
screen_t *screen = terminal->screen;
/*
** C0 Control Codes **
// Single-byte commands (0x00-0x1F) handle basic function.
// NUL (0x00): Ignored.
// DEL (0x7F): Ignored.
// ESC (0x1B): Starts multibyte sequences.
//
// SOH (0x01): Start of Heading (Ctrl-A).
// STX (0x02): Start of Text (Ctrl-B).
// ETX (0x03): End of Text (Ctrl-C).
// EOT (0x04): End of Transmission (Ctrl-D).
// ENQ (0x05): Return Terminal Status (Ctrl-E). Default response is an empty string, but may be overridden by a resource answerbackString.
// SO (0x0E): Shift Out (Ctrl-N) -> Switch to Alternate Character Set. This invokes the G1 character set.
// SI (0x0F): Shift In (Ctrl-O) -> Switch to Standard Character Set. This invokes the G0 character set (the default).
// DLE (0x10): Data Link Escape (Ctrl-P).
// DC1 (0x11): Device Control 1 (Ctrl-Q).
// DC2 (0x12): Device Control 2 (Ctrl-R).
// DC3 (0x13): Device Control 3 (Ctrl-S).
// DC4 (0x14): Device Control 4 (Ctrl-T).
// NAK (0x15): Negative Acknowledgment (Ctrl-U).
// SYN (0x16): Synchronous Idle (Ctrl-V).
// ETB (0x17): End of Transmission Block (Ctrl-W).
// CAN (0x18): Cancel (Ctrl-X).
// EM (0x19): End of Medium (Ctrl-Y).
// SUB (0x1A): Substitute (Ctrl-Z).
// FS (0x1C): File Separator (Ctrl-\).
// GS (0x1D): Group Separator (Ctrl-]).
// RS (0x1E): Record Separator (Ctrl-^).
// US (0x1F): Unit Separator (Ctrl-_).
*/
switch (ch) {
// BEL (0x07): Bell (Ctrl-G).
case '\x07':
if (terminal->osc_buffer_len) {
do_osc(p);
}
else {
// bell sound not implemented
}
break;
// BS (0x08): Backspace (Ctrl-H).
case '\b':
if (screen->cursor.x > 0) {
screen->cursor.x--;
}
break;
// TAB (0x09): Horizontal Tab (HT) (Ctrl-I).
case '\t': // TAB
screen->cursor.x = min(screen->columns - 1, (screen->cursor.x + 8) & ~7);
break;
// LF (0x0A): Line Feed or New Line (NL). (LF is Ctrl-J).
case '\n':
// VT (0x0B): Vertical Tab (Ctrl-K). This is treated the same as LF.
case '\v':
// FF (0x0C): Form Feed or New Page (NP). (FF is Ctrl-L). FF is treated the same as LF .
case '\f':
if (++screen->cursor.y >= screen->rows) {
screen_scroll_v(screen, 1);
screen->cursor.y = screen->rows - 1;
}
if (terminal->lnm || screen->cursor.x >= screen->columns) {
screen->cursor.x = 0;
}
break;
// CR (0x0D): Carriage Return (Ctrl-M).
case '\r':
screen->cursor.x = 0;
break;
default:
printf("Unhandled control: \\x%02x\n", ch);
break;
}
}
static void handle_action(vtparse_t *parser, const vtparse_action_t action, const unsigned char ch) {
#ifdef DEBUG_SCREEN
if (
action != VTPARSE_ACTION_OSC_START && action != VTPARSE_ACTION_OSC_PUT && action != VTPARSE_ACTION_OSC_END) {
const screen_t *screen = ((terminal_t *)parser->user_data)->screen;
if (action == VTPARSE_ACTION_PRINT || action == VTPARSE_ACTION_PUT || (action == VTPARSE_ACTION_ERROR && parser->state == VTPARSE_STATE_GROUND)) {
printf(">%c (\\x%02x)\n", ch, ch);
}
else if (action == VTPARSE_ACTION_EXECUTE) {
printf("#%s \\x%02x\n", ACTION_NAMES[action], ch);
}
else {
printf("#%s %s", ACTION_NAMES[action], (const char *)parser->intermediate_chars);
for (int i = 0; i < parser->num_params; i++) {
printf(" %d", parser->params[i]);
}
printf(" %c\n", ch);
}
#ifndef DEBUG_SCREEN_VERBOSE
if (action != VTPARSE_ACTION_PRINT && action != VTPARSE_ACTION_PUT && action != VTPARSE_ACTION_ERROR)
#endif
{
uint32_t saved_cursor[font_h][font_w];
screen_put_cursor(screen, saved_cursor, 1);
screen_save_bmp(screen, DEBUG_SCREEN);
screen_put_cursor(screen, saved_cursor, 0);
}
}
#endif
switch (action) {
case VTPARSE_ACTION_PRINT:
do_print(parser, ch, 1);
break;
case VTPARSE_ACTION_PUT:
do_print(parser, ch, 0);
break;
case VTPARSE_ACTION_EXECUTE:
do_control(parser, ch);
break;
case VTPARSE_ACTION_CSI_DISPATCH:
do_csi(parser, ch);
break;
case VTPARSE_ACTION_ESC_DISPATCH:
do_esc(parser, ch);
break;
case VTPARSE_ACTION_OSC_START:
do_osc_start(parser);
break;
case VTPARSE_ACTION_OSC_PUT:
do_osc_put(parser, ch);
break;
case VTPARSE_ACTION_OSC_END:
do_osc(parser);
break;
case VTPARSE_ACTION_ERROR:
// NOTE: ERROR may be reported because of STATE_TABLE[parser->state-1][ch] in vtparse()
// lacks [ch] entry for the state, this occurs at least for higher part of ascii table.
// Check if parser is in GROUND state and simply print this character instead of reporting rerror.
if (VTPARSE_STATE_GROUND == parser->state) {
do_print(parser, ch, 1);
}
else {
printf("No transition for \\x%02x in state %s\n", ch, STATE_NAMES[parser->state]);
}
break;
default:
printf("Unhandled action: %d\n", action);
}
}
void terminal_init(terminal_t *terminal, screen_t *screen, const int feedback_fd) {
*terminal = (terminal_t){};
terminal->feedback_fd = feedback_fd;
#ifdef DEBUG_DUMP
terminal->debug_fd = -1;
#endif
terminal->lnm = 1; // auto new line by default
terminal->screen = screen;
memcpy(terminal->color_list, color_list, sizeof(terminal->color_list));
vtparse_init(&terminal->parser, handle_action);
terminal->parser.user_data = terminal;
}
void terminal_feed(terminal_t *terminal, unsigned char *data, const int size) {
#ifdef DEBUG_DUMP
if (terminal->debug_fd >= 0) {
write(terminal->debug_fd, data, size);
}
#endif
const int ascii_size = utf8_to_cp437(data, size, &terminal->accumulator, &terminal->pending_bytes);
vtparse(&terminal->parser, data, ascii_size);
}