/
vt.c
1570 lines (1392 loc) · 35.4 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-2011 Marc Andre 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.
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <langinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <wchar.h>
#ifdef __linux__
# include <pty.h>
#elif defined(__FreeBSD__)
# include <libutil.h>
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
# include <util.h>
#endif
#if defined(__CYGWIN__) || defined(_AIX)
# include <alloca.h>
#endif
#include "vt.h"
#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
#define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define COLOR_PALETTE_START 1
#define COLOR_PALETTE_END (unsigned)(MIN(512, COLOR_PAIRS))
static bool is_utf8, has_default_colors, use_color_palette;
static short *color2palette;
enum {
C0_NUL = 0x00,
C0_SOH, C0_STX, C0_ETX, C0_EOT, C0_ENQ, C0_ACK, C0_BEL,
C0_BS, C0_HT, C0_LF, C0_VT, C0_FF, C0_CR, C0_SO, C0_SI,
C0_DLE, C0_DC1, C0_DC2, D0_DC3, C0_DC4, C0_NAK, C0_SYN, C0_ETB,
C0_CAN, C0_EM, C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1,
};
enum {
C1_40 = 0x40,
C1_41, C1_BPH, C1_NBH, C1_44, C1_NEL, C1_SSA, C1_ESA,
C1_HTS, C1_HTJ, C1_VTS, C1_PLD, C1_PLU, C1_RI, C1_SS2, C1_SS3,
C1_DCS, C1_PU1, C1_PU2, C1_STS, C1_CCH, C1_MW, C1_SPA, C1_EPA,
C1_SOS, C1_59, C1_SCI, C1_CSI, CS_ST, C1_OSC, C1_PM, C1_APC,
};
enum {
CSI_ICH = 0x40,
CSI_CUU, CSI_CUD, CSI_CUF, CSI_CUB, CSI_CNL, CSI_CPL, CSI_CHA,
CSI_CUP, CSI_CHT, CSI_ED, CSI_EL, CSI_IL, CSI_DL, CSI_EF, CSI_EA,
CSI_DCH, CSI_SEE, CSI_CPR, CSI_SU, CSI_SD, CSI_NP, CSI_PP, CSI_CTC,
CSI_ECH, CSI_CVT, CSI_CBT, CSI_SRS, CSI_PTX, CSI_SDS, CSI_SIMD, CSI_5F,
CSI_HPA, CSI_HPR, CSI_REP, CSI_DA, CSI_VPA, CSI_VPR, CSI_HVP, CSI_TBC,
CSI_SM, CSI_MC, CSI_HPB, CSI_VPB, CSI_RM, CSI_SGR, CSI_DSR, CSI_DAQ,
CSI_70, CSI_71, CSI_72, CSI_73, CSI_74, CSI_75, CSI_76, CSI_77,
CSI_78, CSI_79, CSI_7A, CSI_7B, CSI_7C, CSI_7D, CSI_7E, CSI_7F
};
struct Vt {
int pty;
pid_t childpid;
/* 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;
bool charsets[2];
/* geometry */
int rows, cols, maxcols;
unsigned curattrs, savattrs, defattrs;
short curfg, curbg, savfg, savbg, deffg, defbg;
/* scrollback buffer */
struct Row *scroll_buf;
int scroll_buf_sz;
int scroll_buf_ptr;
int scroll_buf_len;
int scroll_amount;
struct Row *lines;
struct Row *scroll_top;
struct Row *scroll_bot;
/* cursor */
struct Row *curs_row;
int curs_col, curs_srow, curs_scol;
/* buffers and parsing state */
mbstate_t ps;
char rbuf[BUFSIZ];
char ebuf[BUFSIZ];
int rlen, elen;
/* custom escape sequence handler */
vt_handler_t handler;
void *data;
};
typedef struct Row {
wchar_t *text;
uint16_t *attr;
short *fg;
short *bg;
unsigned dirty:1;
} Row;
static char const * const keytable[KEY_MAX+1] = {
['\n'] = "\r",
/* 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_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[25~",
[KEY_F(14)] = "\e[26~",
[KEY_F(15)] = "\e[28~",
[KEY_F(16)] = "\e[29~",
[KEY_F(17)] = "\e[31~",
[KEY_F(18)] = "\e[32~",
[KEY_F(19)] = "\e[33~",
[KEY_F(20)] = "\e[34~",
};
static void process_nonprinting(Vt *t, wchar_t wc);
static void send_curs(Vt *t);
__attribute__ ((const))
static uint16_t build_attrs(unsigned curattrs)
{
return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff))
>> NCURSES_ATTR_SHIFT;
}
static void row_set(Row *row, int start, int len, Vt *t)
{
row->dirty = true;
wmemset(row->text + start, 0, len);
attr_t attr = t ? build_attrs(t->curattrs) : 0;
short fg = t ? t->curfg : -1;
short bg = t ? t->curbg : -1;
for (int i = start; i < len + start; i++) {
row->attr[i] = attr;
row->fg[i] = fg;
row->bg[i] = bg;
}
}
static void row_roll(Row *start, Row *end, int count)
{
int n = end - start;
count %= n;
if (count < 0)
count += n;
if (count) {
Row *buf = alloca(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 clamp_cursor_to_bounds(Vt *t)
{
if (t->curs_row < t->lines)
t->curs_row = t->lines;
if (t->curs_row >= t->lines + t->rows)
t->curs_row = t->lines + t->rows - 1;
if (t->curs_col < 0)
t->curs_col = 0;
if (t->curs_col >= t->cols)
t->curs_col = t->cols - 1;
}
static void save_curs(Vt *t)
{
t->curs_srow = t->curs_row - t->lines;
t->curs_scol = t->curs_col;
}
static void restore_curs(Vt *t)
{
t->curs_row = t->lines + t->curs_srow;
t->curs_col = t->curs_scol;
clamp_cursor_to_bounds(t);
}
static void save_attrs(Vt *t)
{
t->savattrs = t->curattrs;
t->savfg = t->curfg;
t->savbg = t->curbg;
}
static void restore_attrs(Vt *t)
{
t->curattrs = t->savattrs;
t->curfg = t->savfg;
t->curbg = t->savbg;
}
static void fill_scroll_buf(Vt *t, int s)
{
/* work in screenfuls */
int ssz = t->scroll_bot - t->scroll_top;
if (s > ssz) {
fill_scroll_buf(t, ssz);
fill_scroll_buf(t, s - ssz);
return;
}
if (s < -ssz) {
fill_scroll_buf(t, -ssz);
fill_scroll_buf(t, s + ssz);
return;
}
t->scroll_buf_len += s;
if (t->scroll_buf_len >= t->scroll_buf_sz)
t->scroll_buf_len = t->scroll_buf_sz;
if (s > 0 && t->scroll_buf_sz) {
for (int i = 0; i < s; i++) {
struct Row tmp = t->scroll_top[i];
t->scroll_top[i] = t->scroll_buf[t->scroll_buf_ptr];
t->scroll_buf[t->scroll_buf_ptr] = tmp;
t->scroll_buf_ptr++;
if (t->scroll_buf_ptr == t->scroll_buf_sz)
t->scroll_buf_ptr = 0;
}
}
row_roll(t->scroll_top, t->scroll_bot, s);
if (s < 0 && t->scroll_buf_sz) {
for (int i = (-s) - 1; i >= 0; i--) {
t->scroll_buf_ptr--;
if (t->scroll_buf_ptr == -1)
t->scroll_buf_ptr = t->scroll_buf_sz - 1;
struct Row tmp = t->scroll_top[i];
t->scroll_top[i] = t->scroll_buf[t->scroll_buf_ptr];
t->scroll_buf[t->scroll_buf_ptr] = tmp;
t->scroll_top[i].dirty = true;
}
}
}
static void cursor_line_down(Vt *t)
{
row_set(t->curs_row, t->cols, t->maxcols - t->cols, 0);
t->curs_row++;
if (t->curs_row < t->scroll_bot)
return;
vt_noscroll(t);
t->curs_row = t->scroll_bot - 1;
fill_scroll_buf(t, 1);
row_set(t->curs_row, 0, t->cols, t);
}
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)
{
if (pcount == 0) {
/* special case: reset attributes */
t->curattrs = A_NORMAL;
t->curfg = t->curbg = -1;
return;
}
for (int i = 0; i < pcount; i++) {
switch (param[i]) {
case 0:
t->curattrs = A_NORMAL;
t->curfg = t->curbg = -1;
break;
case 1:
t->curattrs |= A_BOLD;
break;
case 4:
t->curattrs |= A_UNDERLINE;
break;
case 5:
t->curattrs |= A_BLINK;
break;
case 7:
t->curattrs |= A_REVERSE;
break;
case 8:
t->curattrs |= A_INVIS;
break;
case 22:
t->curattrs &= ~A_BOLD;
break;
case 24:
t->curattrs &= ~A_UNDERLINE;
break;
case 25:
t->curattrs &= ~A_BLINK;
break;
case 27:
t->curattrs &= ~A_REVERSE;
break;
case 28:
t->curattrs &= ~A_INVIS;
break;
case 30 ... 37: /* fg */
t->curfg = param[i] - 30;
break;
case 38:
if ((i + 2) < pcount && param[i + 1] == 5) {
t->curfg = param[i + 2];
i += 2;
}
break;
case 39:
t->curfg = -1;
break;
case 40 ... 47: /* bg */
t->curbg = param[i] - 40;
break;
case 48:
if ((i + 2) < pcount && param[i + 1] == 5) {
t->curbg = param[i + 2];
i += 2;
}
break;
case 49:
t->curbg = -1;
break;
case 90 ... 97: /* hi fg */
t->curfg = param[i] - 82;
break;
case 100 ... 107: /* hi bg */
t->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;
save_attrs(t);
t->curattrs = A_NORMAL;
t->curfg = t->curbg = -1;
if (pcount && param[0] == 2) {
start = t->lines;
end = t->lines + t->rows;
} else if (pcount && param[0] == 1) {
start = t->lines;
end = t->curs_row;
row_set(t->curs_row, 0, t->curs_col + 1, t);
} else {
row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t);
start = t->curs_row + 1;
end = t->lines + t->rows;
}
for (row = start; row < end; row++)
row_set(row, 0, t->cols, t);
restore_attrs(t);
}
/* interprets a 'move cursor' (CUP) escape sequence */
static void interpret_csi_CUP(Vt *t, int param[], int pcount)
{
if (pcount == 0) {
t->curs_row = (t->relposmode ? t->scroll_top : t->lines);
t->curs_col = 0;
} else if (pcount == 1) {
t->curs_row = (t->relposmode ? t->scroll_top : t->lines) + param[0] - 1;
t->curs_col = 0;
} else {
t->curs_row = (t->relposmode ? t->scroll_top : t->lines) + param[0] - 1;
t->curs_col = param[1] - 1;
}
clamp_cursor_to_bounds(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)
{
int n = (pcount && param[0] > 0) ? param[0] : 1;
switch (verb) {
case 'A':
t->curs_row -= n;
break;
case 'B':
case 'e':
t->curs_row += n;
break;
case 'C':
case 'a':
t->curs_col += n;
break;
case 'D':
t->curs_col -= n;
break;
case 'E':
t->curs_row += n;
t->curs_col = 0;
break;
case 'F':
t->curs_row -= n;
t->curs_col = 0;
break;
case 'G':
case '`':
t->curs_col = param[0] - 1;
break;
case 'd':
t->curs_row = t->lines + param[0] - 1;
break;
}
clamp_cursor_to_bounds(t);
}
/* Interpret the 'erase line' escape sequence */
static void interpret_csi_EL(Vt *t, int param[], int pcount)
{
switch (pcount ? param[0] : 0) {
case 1:
row_set(t->curs_row, 0, t->curs_col + 1, t);
break;
case 2:
row_set(t->curs_row, 0, t->cols, t);
break;
default:
row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t);
break;
}
}
/* Interpret the 'insert blanks' sequence (ICH) */
static void interpret_csi_ICH(Vt *t, int param[], int pcount)
{
Row *row = t->curs_row;
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (t->curs_col + n > t->cols)
n = t->cols - t->curs_col;
for (int i = t->cols - 1; i >= t->curs_col + n; i--) {
row->text[i] = row->text[i - n];
row->attr[i] = row->attr[i - n];
row->bg[i] = row->bg[i - n];
row->fg[i] = row->fg[i - n];
}
row_set(row, t->curs_col, n, t);
}
/* Interpret the 'delete chars' sequence (DCH) */
static void interpret_csi_DCH(Vt *t, int param[], int pcount)
{
Row *row = t->curs_row;
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (t->curs_col + n > t->cols)
n = t->cols - t->curs_col;
for (int i = t->curs_col; i < t->cols - n; i++) {
row->text[i] = row->text[i + n];
row->attr[i] = row->attr[i + n];
row->bg[i] = row->bg[i + n];
row->fg[i] = row->fg[i + n];
}
row_set(row, t->cols - n, n, t);
}
/* Interpret an 'insert line' sequence (IL) */
static void interpret_csi_IL(Vt *t, int param[], int pcount)
{
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (t->curs_row + n >= t->scroll_bot) {
for (Row *row = t->curs_row; row < t->scroll_bot; row++)
row_set(row, 0, t->cols, t);
} else {
row_roll(t->curs_row, t->scroll_bot, -n);
for (Row *row = t->curs_row; row < t->curs_row + n; row++)
row_set(row, 0, t->cols, t);
}
}
/* Interpret a 'delete line' sequence (DL) */
static void interpret_csi_DL(Vt *t, int param[], int pcount)
{
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (t->curs_row + n >= t->scroll_bot) {
for (Row *row = t->curs_row; row < t->scroll_bot; row++)
row_set(row, 0, t->cols, t);
} else {
row_roll(t->curs_row, t->scroll_bot, n);
for (Row *row = t->scroll_bot - n; row < t->scroll_bot; row++)
row_set(row, 0, t->cols, t);
}
}
/* Interpret an 'erase characters' (ECH) sequence */
static void interpret_csi_ECH(Vt *t, int param[], int pcount)
{
int n = (pcount && param[0] > 0) ? param[0] : 1;
if (t->curs_col + n < t->cols)
n = t->cols - t->curs_col;
row_set(t->curs_row, t->curs_col, n, t);
}
/* Interpret a 'set scrolling region' (DECSTBM) sequence */
static void interpret_csi_DECSTBM(Vt *t, int param[], int pcount)
{
int new_top, new_bot;
switch (pcount) {
case 0:
t->scroll_top = t->lines;
t->scroll_bot = t->lines + t->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 >= t->rows)
new_top = t->rows - 1;
if (new_bot < 0)
new_bot = 0;
if (new_bot >= t->rows)
new_bot = t->rows;
/* check for range validity */
if (new_top < new_bot) {
t->scroll_top = t->lines + new_top;
t->scroll_bot = t->lines + new_bot;
}
break;
default:
return; /* malformed */
}
}
static void es_interpret_csi(Vt *t)
{
static int csiparam[BUFSIZ];
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++) {
if (IS_CONTROL(*p)) {
process_nonprinting(t, *p);
} else if (*p == ';') {
if (param_count >= (int)sizeof(csiparam))
return; /* too long! */
csiparam[param_count++] = 0;
} else if (isdigit((unsigned char)*p)) {
if (param_count == 0)
csiparam[param_count++] = 0;
csiparam[param_count - 1] *= 10;
csiparam[param_count - 1] += *p - '0';
}
}
if (t->ebuf[1] == '?') {
if (verb == 'h') { /* DEC Private Mode Set (DECSET) */
switch (csiparam[0]) {
case 1: /* set ANSI cursor (application) key mode (DECCKM) */
t->curskeymode = true;
break;
case 6: /* set origin to relative (DECOM) */
t->relposmode = true;
break;
case 25: /* make cursor visible (DECCM) */
t->curshid = false;
break;
case 47: /* use alternate screen buffer */
t->curattrs = A_NORMAL;
t->curfg = t->curbg = -1;
break;
case 1000: /* enable normal mouse tracking */
t->mousetrack = true;
break;
}
} else if (verb == 'l') { /* DEC Private Mode Reset (DECRST) */
switch (csiparam[0]) {
case 1: /* reset ANSI cursor (normal) key mode (DECCKM) */
t->curskeymode = false;
break;
case 6: /* set origin to absolute (DECOM) */
t->relposmode = false;
break;
case 25: /* make cursor visible (DECCM) */
t->curshid = true;
break;
case 47: /* use normal screen buffer */
t->curattrs = A_NORMAL;
t->curfg = t->curbg = -1;
break;
case 1000: /* disable normal mouse tracking */
t->mousetrack = false;
break;
}
}
}
/* delegate handling depending on command character (verb) */
switch (verb) {
case 'h':
if (param_count == 1 && csiparam[0] == 4) /* insert mode */
t->insert = true;
break;
case 'l':
if (param_count == 1 && csiparam[0] == 4) /* replace mode */
t->insert = false;
break;
case 'm': /* it's a 'set attribute' sequence */
interpret_csi_SGR(t, csiparam, param_count);
break;
case 'J': /* it's an 'erase display' sequence */
interpret_csi_ED(t, csiparam, param_count);
break;
case 'H':
case 'f': /* it's a 'move cursor' sequence */
interpret_csi_CUP(t, csiparam, param_count);
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'e':
case 'a':
case 'd':
case '`':
/* it is a 'relative move' */
interpret_csi_C(t, verb, csiparam, param_count);
break;
case 'K': /* erase line */
interpret_csi_EL(t, csiparam, param_count);
break;
case '@': /* insert characters */
interpret_csi_ICH(t, csiparam, param_count);
break;
case 'P': /* delete characters */
interpret_csi_DCH(t, csiparam, param_count);
break;
case 'L': /* insert lines */
interpret_csi_IL(t, csiparam, param_count);
break;
case 'M': /* delete lines */
interpret_csi_DL(t, csiparam, param_count);
break;
case 'X': /* erase chars */
interpret_csi_ECH(t, csiparam, param_count);
break;
case 'r': /* set scrolling region */
interpret_csi_DECSTBM(t, csiparam, param_count);
break;
case 's': /* save cursor location */
save_curs(t);
break;
case 'u': /* restore cursor location */
restore_curs(t);
break;
case 'n': /* query cursor location */
if (param_count == 1 && csiparam[0] == 6)
send_curs(t);
break;
default:
break;
}
}
/* Interpret an 'index' (IND) sequence */
static void interpret_esc_IND(Vt *t)
{
if (t->curs_row < t->lines + t->rows - 1)
t->curs_row++;
}
/* Interpret a 'reverse index' (RI) sequence */
static void interpret_esc_RI(Vt *t)
{
if (t->curs_row > t->lines)
t->curs_row--;
else {
row_roll(t->scroll_top, t->scroll_bot, -1);
row_set(t->scroll_top, 0, t->cols, t);
}
}
/* Interpret a 'next line' (NEL) sequence */
static void interpret_esc_NEL(Vt *t)
{
if (t->curs_row < t->lines + t->rows - 1) {
t->curs_row++;
t->curs_col = 0;
}
}
/* Interpret a 'select character set' (SCS) sequence */
static void interpret_esc_SCS(Vt *t)
{
/* ESC ( sets G0, ESC ) sets G1 */
t->charsets[! !(t->ebuf[0] == ')')] = (t->ebuf[1] == '0');
t->graphmode = t->charsets[0];
}
static void try_interpret_escape_seq(Vt *t)
{
char lastchar = t->ebuf[t->elen - 1];
if (!*t->ebuf)
return;
if (t->handler) {
switch ((*(t->handler)) (t, t->ebuf)) {
case VT_HANDLER_OK:
goto cancel;
case VT_HANDLER_NOTYET:
if (t->elen + 1 >= (int)sizeof(t->ebuf))
goto cancel;
return;
}
}
switch (*t->ebuf) {
case '(':
case ')':
if (t->elen == 2) {
interpret_esc_SCS(t);
cancel_escape_sequence(t);
return;
}
break;
case ']': /* xterm thing */
if (lastchar == '\a')
goto cancel;
break;
case '[':
if (is_valid_csi_ender(lastchar)) {
es_interpret_csi(t);
cancel_escape_sequence(t);
return;
}
break;
case '7': /* DECSC: save cursor and attributes */
save_attrs(t);
save_curs(t);
cancel_escape_sequence(t);
return;
case '8': /* DECRC: restore cursor and attributes */
restore_attrs(t);
restore_curs(t);
cancel_escape_sequence(t);
return;
case 'D': /* IND: index */
interpret_esc_IND(t);
cancel_escape_sequence(t);
return;
case 'M': /* RI: reverse index */
interpret_esc_RI(t);
cancel_escape_sequence(t);
return;
case 'E': /* NEL: next line */
interpret_esc_NEL(t);
cancel_escape_sequence(t);
return;
default:
goto cancel;
}
if (t->elen + 1 >= (int)sizeof(t->ebuf)) {
cancel:
#ifndef NDEBUG
fprintf(stderr, "cancelled: \\033");
for (int i = 0; i < (int)t->elen; i++) {
if (isprint(t->ebuf[i])) {
fputc(t->ebuf[i], stderr);
} else {
fprintf(stderr, "\\%03o", t->ebuf[i]);
}
}
fputc('\n', stderr);
#endif
cancel_escape_sequence(t);
}
}
static void process_nonprinting(Vt *t, wchar_t wc)
{
switch (wc) {
case C0_ESC:
new_escape_sequence(t);
break;
case C0_BEL:
if (t->bell)
beep();
break;
case C0_BS:
if (t->curs_col > 0)
t->curs_col--;
break;
case C0_HT: /* tab */
t->curs_col = (t->curs_col + 8) & ~7;
if (t->curs_col >= t->cols)
t->curs_col = t->cols - 1;
break;
case C0_CR:
t->curs_col = 0;
break;
case C0_VT:
case C0_FF:
case C0_LF:
cursor_line_down(t);
break;
case C0_SO: /* shift out, invoke the G1 character set */
t->graphmode = t->charsets[1];
break;
case C0_SI: /* shift in, invoke the G0 character set */
t->graphmode = t->charsets[0];
break;
}
}
static void is_utf8_locale(void)
{
const char *cset = nl_langinfo(CODESET) ? : "ANSI_X3.4-1968";
is_utf8 = !strcmp(cset, "UTF-8");
}
static wchar_t get_vt100_graphic(char c)
{
static char vt100_acs[] = "`afgjklmnopqrstuvwxyz{|}~";
/*
* 5f-7e standard vt100
* 40-5e rxvt extension for extra curses acs chars
*/
static uint16_t const vt100_utf8[62] = {
0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47
0, 0, 0, 0, 0, 0, 0, 0, // 48-4f
0, 0, 0, 0, 0, 0, 0, 0, // 50-57
0, 0, 0, 0, 0, 0, 0, 0x0020, // 58-5f
0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, // 60-67
0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, // 68-6f
0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, // 70-77
0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, // 78-7e
};
if (is_utf8)
return vt100_utf8[c - 0x41];
else if (strchr(vt100_acs, c))
return NCURSES_ACS(c);
return '\0';
}
static void put_wc(Vt *t, wchar_t wc)
{
int width = 0;
if (!t->seen_input) {
t->seen_input = 1;
kill(-t->childpid, SIGWINCH);
}
if (t->escaped) {
if (t->elen + 1 < (int)sizeof(t->ebuf)) {
t->ebuf[t->elen] = wc;
t->ebuf[++t->elen] = '\0';
try_interpret_escape_seq(t);
} else {
cancel_escape_sequence(t);
}
} else if (IS_CONTROL(wc)) {
process_nonprinting(t, wc);
} else {
Row *tmp;
if (t->graphmode) {
if (wc >= 0x41 && wc <= 0x7e) {
wchar_t gc = get_vt100_graphic(wc);
if (gc)
wc = gc;