-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt.c
1968 lines (1766 loc) · 45.9 KB
/
vt.c
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2004 Bruno T. C. de Oliveira
* Copyright © 2006 Pierre Habouzit
* Copyright © 2008-2016 Marc André Tanner
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <langinfo.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <wchar.h>
#if defined(__linux__) || defined(__CYGWIN__)
# include <pty.h>
#elif defined(__FreeBSD__) || defined(__DragonFly__)
# include <libutil.h>
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
# include <util.h>
#endif
#include "vt.h"
#ifdef _AIX
# include "forkpty-aix.c"
#elif defined __sun
# include "forkpty-sunos.c"
#endif
#ifndef NCURSES_ATTR_SHIFT
# define NCURSES_ATTR_SHIFT 8
#endif
#ifndef NCURSES_ACS
# ifdef PDCURSES
# define NCURSES_ACS(c) (acs_map[(unsigned char)(c)])
# else /* BSD curses */
# define NCURSES_ACS(c) (_acs_map[(unsigned char)(c)])
# endif
#endif
#ifdef NCURSES_VERSION
# ifndef NCURSES_EXT_COLORS
# define NCURSES_EXT_COLORS 0
# endif
# if !NCURSES_EXT_COLORS
# define MAX_COLOR_PAIRS MIN(COLOR_PAIRS, 256)
# endif
#endif
#ifndef MAX_COLOR_PAIRS
# define MAX_COLOR_PAIRS COLOR_PAIRS
#endif
#if defined _AIX && defined CTRL
# undef CTRL
#endif
#ifndef CTRL
# define CTRL(k) ((k) & 0x1F)
#endif
#define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define LENGTH(arr) (sizeof(arr) / sizeof((arr)[0]))
static bool is_utf8, has_default_colors;
static short color_pairs_reserved, color_pairs_max, color_pair_current;
static short *color2palette, default_fg, default_bg;
static char vt_term[32];
typedef struct {
wchar_t text;
attr_t attr;
short fg;
short bg;
} Cell;
typedef struct {
Cell *cells;
unsigned dirty:1;
} Row;
/* Buffer holding the current terminal window content (as an array) as well
* as the scroll back buffer content (as a circular/ring buffer).
*
* If new content is added to terminal the view port slides down and the
* previously top most line is moved into the scroll back buffer at postion
* scroll_index. This index will eventually wrap around and thus overwrite
* the oldest lines.
*
* In the scenerio below a scroll up has been performed. That is 'scroll_above'
* lines still lie above the current view port. Further scrolling up will show
* them. Similarly 'scroll_below' is the amount of lines below the current
* viewport.
*
* The function buffer_boundary sets the row pointers to the start/end range
* of the section delimiting the region before/after the viewport. The functions
* buffer_row_{first,last} return the first/last logical row. And
* buffer_row_{next,prev} allows to iterate over the logical lines in either
* direction.
*
* scroll back buffer
*
* scroll_buf->+----------------+-----+
* | | | ^ \
* | before | | | |
* current terminal content | viewport | | | |
* | | | |
* +----------------+-----+\ | | | s > scroll_above
* ^ | | i | \ | | i | c |
* | | | n | \ | | n | r |
* | | v | \ | | v | o |
* r | | i | \ | | i | l /
* o | viewport | s | >|<- scroll_index | s | l \
* w | | i | / | | i | |
* s | | b | / | after | b | s > scroll_below
* | | l | / | viewport | l | i |
* v | | e | / | | e | z /
* +----------------+-----+/ | unused | | e
* <- maxcols -> | scroll back | |
* <- cols -> | buffer | | |
* | | | |
* | | | v
* roll_buf + scroll_size->+----------------+-----+
* <- maxcols ->
* <- cols ->
*/
typedef struct {
Row *lines; /* array of Row pointers of size 'rows' */
Row *curs_row; /* row on which the cursor currently resides */
Row *scroll_buf; /* a ring buffer holding the scroll back content */
Row *scroll_top; /* row in lines where scrolling region starts */
Row *scroll_bot; /* row in lines where scrolling region ends */
bool *tabs; /* a boolean flag for each column whether it is a tab */
int scroll_size; /* maximal capacity of scroll back buffer (in lines) */
int scroll_index; /* current index into the ring buffer */
int scroll_above; /* number of lines above current viewport */
int scroll_below; /* number of lines below current viewport */
int rows, cols; /* current dimension of buffer */
int maxcols; /* allocated cells (maximal cols over time) */
attr_t curattrs, savattrs; /* current and saved attributes for cells */
int curs_col; /* current cursor column (zero based) */
int curs_srow, curs_scol; /* saved cursor row/colmn (zero based) */
short curfg, curbg; /* current fore and background colors */
short savfg, savbg; /* saved colors */
} Buffer;
struct Vt {
Buffer buffer_normal; /* normal screen buffer */
Buffer buffer_alternate; /* alternate screen buffer */
Buffer *buffer; /* currently active buffer (one of the above) */
attr_t defattrs; /* attributes to use for normal/empty cells */
short deffg, defbg; /* colors to use for back normal/empty cells (white/black) */
int pty; /* master side pty file descriptor */
pid_t pid; /* process id of the process running in this vt */
/* flags */
unsigned seen_input:1;
unsigned insert:1;
unsigned escaped:1;
unsigned curshid:1;
unsigned curskeymode:1;
unsigned bell:1;
unsigned relposmode:1;
unsigned mousetrack:1;
unsigned graphmode:1;
unsigned savgraphmode:1;
bool charsets[2];
/* buffers and parsing state */
char rbuf[BUFSIZ];
char ebuf[BUFSIZ];
unsigned int rlen, elen;
int srow, scol; /* last known offset to display start row, start column */
char title[256]; /* xterm style window title */
vt_title_handler_t title_handler; /* hook which is called when title changes */
vt_urgent_handler_t urgent_handler; /* hook which is called upon bell */
void *data; /* user supplied data */
};
static const char *keytable[KEY_MAX+1] = {
[KEY_ENTER] = "\r",
['\n'] = "\n",
/* for the arrow keys the CSI / SS3 sequences are not stored here
* because they depend on the current cursor terminal mode
*/
[KEY_UP] = "A",
[KEY_DOWN] = "B",
[KEY_RIGHT] = "C",
[KEY_LEFT] = "D",
#ifdef KEY_SUP
[KEY_SUP] = "\e[1;2A",
#endif
#ifdef KEY_SDOWN
[KEY_SDOWN] = "\e[1;2B",
#endif
[KEY_SRIGHT] = "\e[1;2C",
[KEY_SLEFT] = "\e[1;2D",
[KEY_BACKSPACE] = "\177",
[KEY_IC] = "\e[2~",
[KEY_DC] = "\e[3~",
[KEY_PPAGE] = "\e[5~",
[KEY_NPAGE] = "\e[6~",
[KEY_HOME] = "\e[7~",
[KEY_END] = "\e[8~",
[KEY_BTAB] = "\e[Z",
[KEY_SUSPEND] = "\x1A", /* Ctrl+Z gets mapped to this */
[KEY_F(1)] = "\e[11~",
[KEY_F(2)] = "\e[12~",
[KEY_F(3)] = "\e[13~",
[KEY_F(4)] = "\e[14~",
[KEY_F(5)] = "\e[15~",
[KEY_F(6)] = "\e[17~",
[KEY_F(7)] = "\e[18~",
[KEY_F(8)] = "\e[19~",
[KEY_F(9)] = "\e[20~",
[KEY_F(10)] = "\e[21~",
[KEY_F(11)] = "\e[23~",
[KEY_F(12)] = "\e[24~",
[KEY_F(13)] = "\e[23~",
[KEY_F(14)] = "\e[24~",
[KEY_F(15)] = "\e[25~",
[KEY_F(16)] = "\e[26~",
[KEY_F(17)] = "\e[28~",
[KEY_F(18)] = "\e[29~",
[KEY_F(19)] = "\e[31~",
[KEY_F(20)] = "\e[32~",
[KEY_F(21)] = "\e[33~",
[KEY_F(22)] = "\e[34~",
[KEY_RESIZE] = "",
#ifdef KEY_EVENT
[KEY_EVENT] = "",
#endif
};
static void puttab(Vt *t, int count);
static void process_nonprinting(Vt *t, wchar_t wc);
static void send_curs(Vt *t);
__attribute__ ((const))
static attr_t build_attrs(attr_t curattrs)
{
return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff))
>> NCURSES_ATTR_SHIFT;
}
static void row_set(Row *row, int start, int len, Buffer *t)
{
Cell cell = {
.text = L'\0',
.attr = t ? build_attrs(t->curattrs) : 0,
.fg = t ? t->curfg : -1,
.bg = t ? t->curbg : -1,
};
for (int i = start; i < len + start; i++)
row->cells[i] = cell;
row->dirty = true;
}
static void row_roll(Row *start, Row *end, int count)
{
int n = end - start;
count %= n;
if (count < 0)
count += n;
if (count) {
char buf[count * sizeof(Row)];
memcpy(buf, start, count * sizeof(Row));
memmove(start, start + count, (n - count) * sizeof(Row));
memcpy(end - count, buf, count * sizeof(Row));
for (Row *row = start; row < end; row++)
row->dirty = true;
}
}
static void buffer_clear(Buffer *b)
{
Cell cell = {
.text = L'\0',
.attr = A_NORMAL,
.fg = -1,
.bg = -1,
};
for (int i = 0; i < b->rows; i++) {
Row *row = b->lines + i;
for (int j = 0; j < b->cols; j++) {
row->cells[j] = cell;
row->dirty = true;
}
}
}
static void buffer_free(Buffer *b)
{
for (int i = 0; i < b->rows; i++)
free(b->lines[i].cells);
free(b->lines);
for (int i = 0; i < b->scroll_size; i++)
free(b->scroll_buf[i].cells);
free(b->scroll_buf);
free(b->tabs);
}
static void buffer_scroll(Buffer *b, int s)
{
/* work in screenfuls */
int ssz = b->scroll_bot - b->scroll_top;
if (s > ssz) {
buffer_scroll(b, ssz);
buffer_scroll(b, s - ssz);
return;
}
if (s < -ssz) {
buffer_scroll(b, -ssz);
buffer_scroll(b, s + ssz);
return;
}
b->scroll_above += s;
if (b->scroll_above >= b->scroll_size)
b->scroll_above = b->scroll_size;
if (s > 0 && b->scroll_size) {
for (int i = 0; i < s; i++) {
Row tmp = b->scroll_top[i];
b->scroll_top[i] = b->scroll_buf[b->scroll_index];
b->scroll_buf[b->scroll_index] = tmp;
b->scroll_index++;
if (b->scroll_index == b->scroll_size)
b->scroll_index = 0;
}
}
row_roll(b->scroll_top, b->scroll_bot, s);
if (s < 0 && b->scroll_size) {
for (int i = (-s) - 1; i >= 0; i--) {
b->scroll_index--;
if (b->scroll_index == -1)
b->scroll_index = b->scroll_size - 1;
Row tmp = b->scroll_top[i];
b->scroll_top[i] = b->scroll_buf[b->scroll_index];
b->scroll_buf[b->scroll_index] = tmp;
b->scroll_top[i].dirty = true;
}
}
}
static void buffer_resize(Buffer *b, int rows, int cols)
{
Row *lines = b->lines;
if (b->rows != rows) {
if (b->curs_row >= lines + rows) {
/* scroll up instead of simply chopping off bottom */
buffer_scroll(b, (b->curs_row - b->lines) - rows + 1);
}
while (b->rows > rows) {
free(lines[b->rows - 1].cells);
b->rows--;
}
lines = realloc(lines, sizeof(Row) * rows);
}
if (b->maxcols < cols) {
for (int row = 0; row < b->rows; row++) {
lines[row].cells = realloc(lines[row].cells, sizeof(Cell) * cols);
if (b->cols < cols)
row_set(lines + row, b->cols, cols - b->cols, NULL);
lines[row].dirty = true;
}
Row *sbuf = b->scroll_buf;
for (int row = 0; row < b->scroll_size; row++) {
sbuf[row].cells = realloc(sbuf[row].cells, sizeof(Cell) * cols);
if (b->cols < cols)
row_set(sbuf + row, b->cols, cols - b->cols, NULL);
}
b->tabs = realloc(b->tabs, sizeof(*b->tabs) * cols);
for (int col = b->cols; col < cols; col++)
b->tabs[col] = !(col & 7);
b->maxcols = cols;
b->cols = cols;
} else if (b->cols != cols) {
for (int row = 0; row < b->rows; row++)
lines[row].dirty = true;
b->cols = cols;
}
int deltarows = 0;
if (b->rows < rows) {
while (b->rows < rows) {
lines[b->rows].cells = calloc(b->maxcols, sizeof(Cell));
row_set(lines + b->rows, 0, b->maxcols, b);
b->rows++;
}
/* prepare for backfill */
if (b->curs_row >= b->scroll_bot - 1) {
deltarows = b->lines + rows - b->curs_row - 1;
if (deltarows > b->scroll_above)
deltarows = b->scroll_above;
}
}
b->curs_row += lines - b->lines;
b->scroll_top = lines;
b->scroll_bot = lines + rows;
b->lines = lines;
/* perform backfill */
if (deltarows > 0) {
buffer_scroll(b, -deltarows);
b->curs_row += deltarows;
}
}
static bool buffer_init(Buffer *b, int rows, int cols, int scroll_size)
{
b->curattrs = A_NORMAL; /* white text over black background */
b->curfg = b->curbg = -1;
if (scroll_size < 0)
scroll_size = 0;
if (scroll_size && !(b->scroll_buf = calloc(scroll_size, sizeof(Row))))
return false;
b->scroll_size = scroll_size;
buffer_resize(b, rows, cols);
return true;
}
static void buffer_boundry(Buffer *b, Row **bs, Row **be, Row **as, Row **ae) {
if (bs)
*bs = NULL;
if (be)
*be = NULL;
if (as)
*as = NULL;
if (ae)
*ae = NULL;
if (!b->scroll_size)
return;
if (b->scroll_above) {
if (bs)
*bs = &b->scroll_buf[(b->scroll_index - b->scroll_above + b->scroll_size) % b->scroll_size];
if (be)
*be = &b->scroll_buf[(b->scroll_index-1 + b->scroll_size) % b->scroll_size];
}
if (b->scroll_below) {
if (as)
*as = &b->scroll_buf[b->scroll_index];
if (ae)
*ae = &b->scroll_buf[(b->scroll_index + b->scroll_below-1) % b->scroll_size];
}
}
static Row *buffer_row_first(Buffer *b) {
Row *bstart;
if (!b->scroll_size || !b->scroll_above)
return b->lines;
buffer_boundry(b, &bstart, NULL, NULL, NULL);
return bstart;
}
static Row *buffer_row_last(Buffer *b) {
Row *aend;
if (!b->scroll_size || !b->scroll_below)
return b->lines + b->rows - 1;
buffer_boundry(b, NULL, NULL, NULL, &aend);
return aend;
}
static Row *buffer_row_next(Buffer *b, Row *row)
{
Row *before_start, *before_end, *after_start, *after_end;
Row *first = b->lines, *last = b->lines + b->rows - 1;
if (!row)
return NULL;
buffer_boundry(b, &before_start, &before_end, &after_start, &after_end);
if (row >= first && row < last)
return ++row;
if (row == last)
return after_start;
if (row == before_end)
return first;
if (row == after_end)
return NULL;
if (row == &b->scroll_buf[b->scroll_size - 1])
return b->scroll_buf;
return ++row;
}
static Row *buffer_row_prev(Buffer *b, Row *row)
{
Row *before_start, *before_end, *after_start, *after_end;
Row *first = b->lines, *last = b->lines + b->rows - 1;
if (!row)
return NULL;
buffer_boundry(b, &before_start, &before_end, &after_start, &after_end);
if (row > first && row <= last)
return --row;
if (row == first)
return before_end;
if (row == before_start)
return NULL;
if (row == after_start)
return last;
if (row == b->scroll_buf)
return &b->scroll_buf[b->scroll_size - 1];
return --row;
}
static void cursor_clamp(Vt *t)
{
Buffer *b = t->buffer;
Row *lines = t->relposmode ? b->scroll_top : b->lines;
int rows = t->relposmode ? b->scroll_bot - b->scroll_top : b->rows;
if (b->curs_row < lines)
b->curs_row = lines;
if (b->curs_row >= lines + rows)
b->curs_row = lines + rows - 1;
if (b->curs_col < 0)
b->curs_col = 0;
if (b->curs_col >= b->cols)
b->curs_col = b->cols - 1;
}
static void cursor_line_down(Vt *t)
{
Buffer *b = t->buffer;
row_set(b->curs_row, b->cols, b->maxcols - b->cols, NULL);
b->curs_row++;
if (b->curs_row < b->scroll_bot)
return;
vt_noscroll(t);
b->curs_row = b->scroll_bot - 1;
buffer_scroll(b, 1);
row_set(b->curs_row, 0, b->cols, b);
}
static void cursor_save(Vt *t)
{
Buffer *b = t->buffer;
b->curs_srow = b->curs_row - b->lines;
b->curs_scol = b->curs_col;
}
static void cursor_restore(Vt *t)
{
Buffer *b = t->buffer;
b->curs_row = b->lines + b->curs_srow;
b->curs_col = b->curs_scol;
cursor_clamp(t);
}
static void attributes_save(Vt *t)
{
Buffer *b = t->buffer;
b->savattrs = b->curattrs;
b->savfg = b->curfg;
b->savbg = b->curbg;
t->savgraphmode = t->graphmode;
}
static void attributes_restore(Vt *t)
{
Buffer *b = t->buffer;
b->curattrs = b->savattrs;
b->curfg = b->savfg;
b->curbg = b->savbg;
t->graphmode = t->savgraphmode;
}
static void new_escape_sequence(Vt *t)
{
t->escaped = true;
t->elen = 0;
t->ebuf[0] = '\0';
}
static void cancel_escape_sequence(Vt *t)
{
t->escaped = false;
t->elen = 0;
t->ebuf[0] = '\0';
}
static bool is_valid_csi_ender(int c)
{
return (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c == '@' || c == '`');
}
/* interprets a 'set attribute' (SGR) CSI escape sequence */
static void interpret_csi_sgr(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
if (pcount == 0) {
/* special case: reset attributes */
b->curattrs = A_NORMAL;
b->curfg = b->curbg = -1;
return;
}
for (int i = 0; i < pcount; i++) {
switch (param[i]) {
case 0:
b->curattrs = A_NORMAL;
b->curfg = b->curbg = -1;
break;
case 1:
b->curattrs |= A_BOLD;
break;
case 2:
b->curattrs |= A_DIM;
break;
#ifdef A_ITALIC
case 3:
b->curattrs |= A_ITALIC;
break;
#endif
case 4:
b->curattrs |= A_UNDERLINE;
break;
case 5:
b->curattrs |= A_BLINK;
break;
case 7:
b->curattrs |= A_REVERSE;
break;
case 8:
b->curattrs |= A_INVIS;
break;
case 22:
b->curattrs &= ~(A_BOLD | A_DIM);
break;
#ifdef A_ITALIC
case 23:
b->curattrs &= ~A_ITALIC;
break;
#endif
case 24:
b->curattrs &= ~A_UNDERLINE;
break;
case 25:
b->curattrs &= ~A_BLINK;
break;
case 27:
b->curattrs &= ~A_REVERSE;
break;
case 28:
b->curattrs &= ~A_INVIS;
break;
case 30 ... 37: /* fg */
b->curfg = param[i] - 30;
break;
case 38:
if ((i + 2) < pcount && param[i + 1] == 5) {
b->curfg = param[i + 2];
i += 2;
}
break;
case 39:
b->curfg = -1;
break;
case 40 ... 47: /* bg */
b->curbg = param[i] - 40;
break;
case 48:
if ((i + 2) < pcount && param[i + 1] == 5) {
b->curbg = param[i + 2];
i += 2;
}
break;
case 49:
b->curbg = -1;
break;
case 90 ... 97: /* hi fg */
b->curfg = param[i] - 82;
break;
case 100 ... 107: /* hi bg */
b->curbg = param[i] - 92;
break;
default:
break;
}
}
}
/* interprets an 'erase display' (ED) escape sequence */
static void interpret_csi_ed(Vt *t, int param[], int pcount)
{
Row *row, *start, *end;
Buffer *b = t->buffer;
attributes_save(t);
b->curattrs = A_NORMAL;
b->curfg = b->curbg = -1;
if (pcount && param[0] == 2) {
start = b->lines;
end = b->lines + b->rows;
} else if (pcount && param[0] == 1) {
start = b->lines;
end = b->curs_row;
row_set(b->curs_row, 0, b->curs_col + 1, b);
} else {
row_set(b->curs_row, b->curs_col, b->cols - b->curs_col, b);
start = b->curs_row + 1;
end = b->lines + b->rows;
}
for (row = start; row < end; row++)
row_set(row, 0, b->cols, b);
attributes_restore(t);
}
/* interprets a 'move cursor' (CUP) escape sequence */
static void interpret_csi_cup(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
Row *lines = t->relposmode ? b->scroll_top : b->lines;
if (pcount == 0) {
b->curs_row = lines;
b->curs_col = 0;
} else if (pcount == 1) {
b->curs_row = lines + param[0] - 1;
b->curs_col = 0;
} else {
b->curs_row = lines + param[0] - 1;
b->curs_col = param[1] - 1;
}
cursor_clamp(t);
}
/* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
* CPL, CHA, HPR, VPA, VPR, HPA */
static void interpret_csi_c(Vt *t, char verb, int param[], int pcount)
{
Buffer *b = t->buffer;
int n = (pcount && param[0] > 0) ? param[0] : 1;
switch (verb) {
case 'A':
b->curs_row -= n;
break;
case 'B':
case 'e':
b->curs_row += n;
break;
case 'C':
case 'a':
b->curs_col += n;
break;
case 'D':
b->curs_col -= n;
break;
case 'E':
b->curs_row += n;
b->curs_col = 0;
break;
case 'F':
b->curs_row -= n;
b->curs_col = 0;
break;
case 'G':
case '`':
b->curs_col = n - 1;
break;
case 'd':
b->curs_row = b->lines + n - 1;
break;
}
cursor_clamp(t);
}
/* Interpret the 'erase line' escape sequence */
static void interpret_csi_el(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
switch (pcount ? param[0] : 0) {
case 1:
row_set(b->curs_row, 0, b->curs_col + 1, b);
break;
case 2:
row_set(b->curs_row, 0, b->cols, b);
break;
default:
row_set(b->curs_row, b->curs_col, b->cols - b->curs_col, b);
break;
}
}
/* Interpret the 'insert blanks' sequence (ICH) */
static void interpret_csi_ich(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
Row *row = b->curs_row;
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (b->curs_col + n > b->cols)
n = b->cols - b->curs_col;
for (int i = b->cols - 1; i >= b->curs_col + n; i--)
row->cells[i] = row->cells[i - n];
row_set(row, b->curs_col, n, b);
}
/* Interpret the 'delete chars' sequence (DCH) */
static void interpret_csi_dch(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
Row *row = b->curs_row;
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (b->curs_col + n > b->cols)
n = b->cols - b->curs_col;
for (int i = b->curs_col; i < b->cols - n; i++)
row->cells[i] = row->cells[i + n];
row_set(row, b->cols - n, n, b);
}
/* Interpret an 'insert line' sequence (IL) */
static void interpret_csi_il(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (b->curs_row + n >= b->scroll_bot) {
for (Row *row = b->curs_row; row < b->scroll_bot; row++)
row_set(row, 0, b->cols, b);
} else {
row_roll(b->curs_row, b->scroll_bot, -n);
for (Row *row = b->curs_row; row < b->curs_row + n; row++)
row_set(row, 0, b->cols, b);
}
}
/* Interpret a 'delete line' sequence (DL) */
static void interpret_csi_dl(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (b->curs_row + n >= b->scroll_bot) {
for (Row *row = b->curs_row; row < b->scroll_bot; row++)
row_set(row, 0, b->cols, b);
} else {
row_roll(b->curs_row, b->scroll_bot, n);
for (Row *row = b->scroll_bot - n; row < b->scroll_bot; row++)
row_set(row, 0, b->cols, b);
}
}
/* Interpret an 'erase characters' (ECH) sequence */
static void interpret_csi_ech(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (b->curs_col + n > b->cols)
n = b->cols - b->curs_col;
row_set(b->curs_row, b->curs_col, n, b);
}
/* Interpret a 'set scrolling region' (DECSTBM) sequence */
static void interpret_csi_decstbm(Vt *t, int param[], int pcount)
{
Buffer *b = t->buffer;
int new_top, new_bot;
switch (pcount) {
case 0:
b->scroll_top = b->lines;
b->scroll_bot = b->lines + b->rows;
break;
case 2:
new_top = param[0] - 1;
new_bot = param[1];
/* clamp to bounds */
if (new_top < 0)
new_top = 0;
if (new_top >= b->rows)
new_top = b->rows - 1;
if (new_bot < 0)
new_bot = 0;
if (new_bot >= b->rows)
new_bot = b->rows;
/* check for range validity */
if (new_top < new_bot) {
b->scroll_top = b->lines + new_top;
b->scroll_bot = b->lines + new_bot;
}
break;
default:
return; /* malformed */
}
b->curs_row = b->scroll_top;
b->curs_col = 0;
}
static void interpret_csi_mode(Vt *t, int param[], int pcount, bool set)
{
for (int i = 0; i < pcount; i++) {
switch (param[i]) {
case 4: /* insert/replace mode */
t->insert = set;
break;
}
}
}
static void interpret_csi_priv_mode(Vt *t, int param[], int pcount, bool set)
{
for (int i = 0; i < pcount; i++) {
switch (param[i]) {
case 1: /* set application/normal cursor key mode (DECCKM) */
t->curskeymode = set;
break;
case 6: /* set origin to relative/absolute (DECOM) */
t->relposmode = set;
break;
case 25: /* make cursor visible/invisible (DECCM) */
t->curshid = !set;
break;
case 1049: /* combine 1047 + 1048 */
case 47: /* use alternate/normal screen buffer */
case 1047:
if (!set)
buffer_clear(&t->buffer_alternate);
t->buffer = set ? &t->buffer_alternate : &t->buffer_normal;
vt_dirty(t);
if (param[i] != 1049)
break;
/* fall through */
case 1048: /* save/restore cursor */
if (set)
cursor_save(t);
else
cursor_restore(t);
break;
case 1000: /* enable/disable normal mouse tracking */
t->mousetrack = set;
break;
}
}
}
static void interpret_csi(Vt *t)
{
Buffer *b = t->buffer;
int csiparam[16];
unsigned int param_count = 0;
const char *p = t->ebuf + 1;
char verb = t->ebuf[t->elen - 1];
/* parse numeric parameters */
for (p += (t->ebuf[1] == '?'); *p; p++) {