-
Notifications
You must be signed in to change notification settings - Fork 1
/
term.c
3866 lines (3605 loc) · 132 KB
/
term.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 (c) 2019-2022, Evgeniy Baskov. All rights reserved */
#include "feature.h"
#define _DEFAULT_SOURCE
#include <assert.h>
#include "config.h"
#include "input.h"
#include "line.h"
#include "mouse.h"
#include "nrcs.h"
#include "screen.h"
#include "term.h"
#include "tty.h"
#if USE_URI
# include "uri.h"
#endif
#include "window.h"
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <wchar.h>
#define ESC_MAX_PARAM 32
#define ESC_MAX_STR 256
#define ESC_MAX_LONG_STR 0x10000000
#define ESC_DUMP_MAX 768
#define SGR_BUFSIZ 64
#define MAX_REPORT 1024
#define CSI "\x9B"
#define OSC "\x9D"
#define DCS "\x90"
#define ESC "\x1B"
#define ST "\x9C"
#define TABSR_INIT_CAP 48
#define TABSR_CAP_STEP(x) (4*(x)/3)
#define TABSR_MAX_ENTRY 6
#define STR_CAP_STEP(x) (4*(x)/3)
#define PARAM(i, d) (term->esc.param[i] > 0 ? (uparam_t)term->esc.param[i] : (uparam_t)(d))
#define CHK_VT(v) { if (term->vt_level < (v)) break; }
enum mode_status {
modstate_unrecognized,
modstate_enabled,
modstate_disabled,
modstate_aways_enabled,
modstate_aways_disabled,
};
struct term_mode {
bool echo : 1;
bool crlf : 1;
bool focused : 1;
bool reverse_video : 1;
bool sixel : 1;
bool eight_bit : 1;
bool protected : 1;
bool track_focus : 1;
bool scroll_on_output : 1;
bool no_scroll_on_input : 1;
bool columns_132 : 1;
bool preserve_display_132 : 1;
bool enable_columns_132 : 1;
bool enable_nrcs : 1;
bool title_set_utf8 : 1;
bool title_query_utf8 : 1;
bool title_set_hex : 1;
bool title_query_hex : 1;
bool bracketed_paste : 1;
bool keep_clipboard : 1;
bool led_num_lock : 1;
bool led_caps_lock : 1;
bool led_scroll_lock : 1;
bool allow_change_font : 1;
bool paste_quote : 1;
bool paste_literal_nl : 1;
bool bell_raise : 1;
bool bell_urgent : 1;
bool altscreen_scroll : 1;
bool inband_resize : 1;
bool utf8 : 1;
};
/* Parsing state
* IMPORTANT: Elements should not be
* rearranged, state machine will break
* */
enum escape_state {
esc_ground,
esc_esc_entry, esc_esc_1, esc_esc_2, esc_esc_ignore,
esc_csi_entry, esc_csi_0, esc_csi_1, esc_csi_2, esc_csi_ignore,
esc_dcs_entry, esc_dcs_0, esc_dcs_1, esc_dcs_2,
esc_osc_entry, esc_osc_1, esc_osc_2, esc_osc_string,
esc_dcs_string,
esc_ign_entry, esc_ign_string,
esc_vt52_entry, esc_vt52_cup_0, esc_vt52_cup_1,
};
struct term {
struct screen scr;
/* VT52 mode saves current cursor G0-G3,GL,GR and origin
* state, and current terminal mode, and restores on exit */
struct cursor vt52_saved_c;
struct term_mode vt52_saved_mode;
struct screen_mode vt52_saved_screen_mode;
/* If this flag is set application
* already knows that terminal window
* would be resized, so it would be
* incorrect to read from terminal before
* resize */
bool requested_resize;
/* OSC 52 character description
* of selection being pasted from */
struct {
uint8_t from;
uint8_t leftover[3];
uint8_t leftover_len;
bool bracketed : 1;
bool quote : 1;
bool literal_nl : 1;
bool utf8 : 1;
} paste;
/* Mouse state */
struct mouse_state mstate;
/* Keyboard state */
struct keyboard_state kstate;
/* TTY State, input buffer */
struct tty tty;
/* Terminal modes, most of DECSET/DECRST and RM/SM modes
* are stored here */
struct term_mode mode;
/* XTerm checksum extension flags */
struct checksum_mode checksum_mode;
#if USE_URI
/* URI match state for URI autodetection */
struct uri_match_state uri_match;
ssize_t shift_bookmark;
#endif
/* Bell volume */
uint8_t bvol;
/* This is compressed bit array,
* that stores encoded modes for
* XTSAVE/XTRESTORE
* [0-96] : 12 bytes;
* [1000-1063] : 8 bytes;
* [2000-2063] : 8 bytes */
uint8_t saved_modbits[28];
/* Other parts of saved state for XTSAVE/XTRESTORE */
enum mouse_mode saved_mouse_mode;
enum mouse_format saved_mouse_format;
uint8_t saved_keyboard_type;
struct escape {
/* Parser state */
enum escape_state state;
/* Encoded escape sequence description
* that allows to dispatch in one 'switch'*/
uparam_t selector;
/* Saved state/selector, used for ST interpretation */
enum escape_state old_state;
uparam_t old_selector;
/* CSI/DCS argument count and arguments */
size_t i;
iparam_t param[ESC_MAX_PARAM];
/* IMPORTANT: subpar_mask must contain
* at least ESC_MAX_PARAM bits */
uint32_t subpar_mask;
/* Length of OSC/DCS string part */
size_t str_len;
/* Storage capacity */
size_t str_cap;
/* Short strings are not allocated,
* but long strings are */
uint8_t str_data[ESC_MAX_STR + 1];
uint8_t *str_ptr;
} esc;
/* Emulated VT version, like 420 */
uint16_t vt_version;
/* Emulation level, usually vt_version/100 */
uint16_t vt_level;
/* Global color palette */
color_t palette[PALETTE_SIZE];
};
static inline void term_esc_start(struct term *term) {
term->esc.selector = 0;
}
static inline void term_esc_start_seq(struct term *term) {
memset(term->esc.param, 0xFF, (term->esc.i + 1)*sizeof term->esc.param[0]);
term->esc.i = 0;
term->esc.subpar_mask = 0;
term->esc.selector = 0;
}
static inline uint8_t *term_esc_str(struct term *term) {
return term->esc.str_ptr ? term->esc.str_ptr : term->esc.str_data;
}
static inline void term_esc_start_string(struct term *term) {
term->esc.str_len = 0;
term->esc.str_data[0] = 0;
term->esc.str_cap = ESC_MAX_STR;
term->esc.selector = 0;
}
static inline void term_esc_finish_string(struct term *term) {
free(term->esc.str_ptr);
term->esc.str_ptr = NULL;
}
static void term_request_resize(struct term *term, int16_t w, int16_t h, bool in_cells) {
struct window *win = screen_window(&term->scr);
struct extent cur = window_get_size(win);
struct extent scr = window_get_screen_size(win);
if (in_cells) {
struct extent ce = window_get_cell_size(win);
struct border bo = window_get_border(win);
if (w > 0) w = w * ce.width + bo.left + bo.right;
if (h > 0) h = h * ce.height + bo.top + bo.bottom;
}
w = !w ? scr.width : w < 0 ? cur.width : w;
h = !h ? scr.height : h < 0 ? cur.height : h;
term->requested_resize |= window_resize(win, w, h);
}
static void term_set_132(struct term *term, bool set) {
struct screen *scr = &term->scr;
term->mode.columns_132 = set;
screen_reset_margins(scr);
screen_move_to(scr, screen_min_ox(scr), screen_min_oy(scr));
if (!term->mode.preserve_display_132)
screen_erase(scr, 0, 0, screen_width(scr), screen_height(scr), false);
if (window_cfg(screen_window(scr))->allow_window_ops)
term_request_resize(term, set ? 132 : 80, 24, true);
}
static void term_set_vt52(struct term *term, bool set) {
/* This thing is mess... */
struct screen *scr = &term->scr;
struct screen_mode *smode = &scr->mode;
if (set) {
term->kstate.keyboad_vt52 = 1;
term->vt_level = 0;
term->vt52_saved_c = *screen_cursor(scr);
term->vt52_saved_mode = term->mode;
term->vt52_saved_screen_mode = *smode;
*screen_cursor(scr) = (struct cursor) {
.x = screen_cursor(scr)->x,
.y = screen_cursor(scr)->y,
.pending = screen_cursor(scr)->pending,
.gl = 0,
.gl_ss = 0,
.gr = 2,
.gn = { cs94_ascii, cs94_ascii, cs94_ascii, cs94_dec_graph }
};
term->mode = (struct term_mode) {
.focused = term->mode.focused,
.reverse_video = term->mode.reverse_video,
};
*smode = (struct screen_mode) {
.wrap = 1
};
screen_set_altscreen(&term->scr, 0, 0, 0);
term_esc_start_seq(term);
} else {
term->kstate.keyboad_vt52 = 0;
term->vt_level = 1;
struct cursor *c = screen_cursor(scr);
term->vt52_saved_c.x = c->x;
term->vt52_saved_c.y = c->y;
term->vt52_saved_c.pending = c->pending;
*screen_cursor(scr) = term->vt52_saved_c;
*smode = term->vt52_saved_screen_mode;
bool alt = screen_altscreen(scr);
smode->altscreen = 0;
term->mode = term->vt52_saved_mode;
screen_set_altscreen(&term->scr, alt, 0, 0);
}
}
void term_set_reverse(struct term *term, bool set) {
if (set ^ term->mode.reverse_video) {
SWAP(term->palette[SPECIAL_BG], term->palette[SPECIAL_FG]);
SWAP(term->palette[0], term->palette[7]);
SWAP(term->palette[8], term->palette[15]);
SWAP(term->palette[SPECIAL_CURSOR_BG], term->palette[SPECIAL_CURSOR_FG]);
SWAP(term->palette[SPECIAL_SELECTED_BG], term->palette[SPECIAL_SELECTED_FG]);
window_set_colors(screen_window(&term->scr), term->palette[SPECIAL_BG], term->palette[SPECIAL_CURSOR_FG]);
}
term->mode.reverse_video = set;
}
static void term_load_config(struct term *term, bool reset) {
struct instance_config *cfg = window_cfg(screen_window(&term->scr));
if (reset) {
/* Initialize escape parameters to default values */
memset(term->esc.param, 0xFF, sizeof term->esc.param);
term->mstate = (struct mouse_state) {0};
term->mode = (struct term_mode) {
.focused = term->mode.focused,
.title_query_utf8 = cfg->utf8,
.title_set_utf8 = cfg->utf8,
.utf8 = cfg->utf8,
.enable_columns_132 = true,
};
term->kstate = (struct keyboard_state) {
.appcursor = cfg->appcursor,
.appkey = cfg->appkey,
.backspace_is_del = cfg->backspace_is_delete,
.delete_is_del = cfg->delete_is_delete,
.fkey_inc_step = cfg->fkey_increment,
.has_meta = cfg->has_meta,
.keyboard_mapping = cfg->mapping,
.keylock = cfg->lock,
.meta_escape = cfg->meta_is_esc,
.modkey_cursor = cfg->modify_cursor,
.modkey_fn = cfg->modify_function,
.modkey_keypad = cfg->modify_keypad,
.modkey_other = cfg->modify_other,
.modkey_other_fmt = cfg->modify_other_fmt,
.modkey_legacy_allow_edit_keypad = cfg->allow_legacy_edit,
.modkey_legacy_allow_function = cfg->allow_legacy_function,
.modkey_legacy_allow_keypad = cfg->allow_legacy_keypad,
.modkey_legacy_allow_misc = cfg->allow_legacy_misc,
.allow_numlock = cfg->numlock,
};
}
for (size_t i = 0; i < PALETTE_SIZE; i++)
term->palette[i] = cfg->palette[i];
term_set_reverse(term, cfg->reverse_video);
switch (cfg->bell_volume) {
case 0: term->bvol = 0; break;
case 1: term->bvol = cfg->bell_low_volume; break;
case 2: term->bvol = cfg->bell_high_volume;
}
term->mode.no_scroll_on_input = !cfg->scroll_on_input;
term->mode.scroll_on_output = cfg->scroll_on_output;
term->mode.keep_clipboard = cfg->keep_clipboard;
term->mode.bell_raise = cfg->raise_on_bell;
term->mode.bell_urgent = cfg->urgency_on_bell;
}
void term_reload_config(struct term *term) {
screen_load_config(&term->scr, false);
term_load_config(term, false);
}
static void term_do_reset(struct term *term, bool hard) {
struct screen *scr = &term->scr;
struct window *win = screen_window(scr);
if (term->mode.columns_132)
term_set_132(term, false);
screen_set_altscreen(scr, false, false, false);
ssize_t cx, cy;
bool cpending;
screen_store_cursor_position(scr, &cx, &cy, &cpending);
screen_load_config(scr, true);
screen_reset_margins(scr);
screen_reset_tabs(scr);
term_load_config(term, true);
keyboard_reset_udk(term);
#if USE_URI
uri_match_reset(&term->uri_match, false);
#endif
window_set_pointer_shape(win, "");
window_set_pointer_mode(win, hide_never);
window_set_mouse(win, USE_URI);
window_set_colors(win, term->palette[SPECIAL_BG], term->palette[SPECIAL_CURSOR_FG]);
window_set_autorepeat(win, window_cfg(win)->autorepeat);
if (hard) {
screen_erase(scr, 0, 0, screen_width(scr), screen_height(scr), false);
screen_free_scrollback(scr, window_cfg(win)->scrollback_size);
term->vt_version = window_cfg(win)->vt_version;
term->vt_level = term->vt_version / 100;
if (!term->vt_level) term_set_vt52(term, 1);
window_set_title(win, target_icon_label | target_title, NULL,
term->mode.utf8 || term->mode.title_set_utf8);
} else {
screen_load_cursor_position(scr, cx, cy, cpending);
}
term->esc.state = esc_ground;
}
static void term_esc_dump(struct term *term, bool use_info) {
if (use_info && !gconfig.trace_controls) return;
const char *pref = use_info ? "Seq: " : "Unrecognized ";
term_esc_str(term)[term->esc.str_len] = 0;
char buf[ESC_DUMP_MAX] = "^[";
size_t pos = 2;
switch (term->esc.state) {
case esc_esc_entry:
case esc_esc_1:
buf[pos++] = 0x20 + ((term->esc.selector & I0_MASK) >> 9) - 1;
/* fallthrough */
case esc_esc_2:
buf[pos++] = 0x20 + ((term->esc.selector & I1_MASK) >> 14) - 1;
buf[pos++] = E_MASK & term->esc.selector;
break;
case esc_csi_entry:
case esc_csi_0:
case esc_csi_1:
case esc_csi_2:
case esc_dcs_string:
buf[pos++] = term->esc.state == esc_dcs_string ? 'P' :'[';
if (term->esc.selector & P_MASK)
buf[pos++] = '<' + ((term->esc.selector & P_MASK) >> 6) - 1;
for (size_t i = 0; i < term->esc.i; i++) {
pos += snprintf(buf + pos, ESC_DUMP_MAX - pos, "%u", term->esc.param[i]);
if (i < term->esc.i - 1) buf[pos++] = term->esc.subpar_mask & (1 << (i + 1)) ? ':' : ';' ;
}
if (term->esc.selector & I0_MASK)
buf[pos++] = 0x20 + ((term->esc.selector & I0_MASK) >> 9) - 1;
if (term->esc.selector & I1_MASK)
buf[pos++] = 0x20 + ((term->esc.selector & I1_MASK) >> 14) - 1;
buf[pos++] = (C_MASK & term->esc.selector) + 0x40;
if (term->esc.state != esc_dcs_string) break;
buf[pos] = 0;
(use_info ? info : warn)("%s%s%s^[\\", pref, buf, term_esc_str(term));
return;
case esc_osc_string:
(use_info ? info : warn)("%s^[]%u;%s^[\\", pref, term->esc.selector, term_esc_str(term));
default:
return;
}
buf[pos] = 0;
(use_info ? info : warn)("%s%s", pref, buf);
}
static void term_dispatch_da(struct term *term, uparam_t mode) {
switch (mode) {
case P('='): /* Tertinary DA */
CHK_VT(4);
/* DECREPTUI */
term_answerback(term, DCS"!|00000000"ST);
break;
case P('>'): /* Secondary DA */ {
uparam_t ver = 0;
switch (term->vt_version) {
case 100: ver = 0; break;
case 220: ver = 1; break;
case 240: ver = 2; break;
case 330: ver = 18; break;
case 340: ver = 19; break;
case 320: ver = 24; break;
case 420: ver = 41; break;
case 510: ver = 61; break;
case 520: ver = 64; break;
case 525: ver = 65; break;
}
term_answerback(term, CSI">%u;%u;0c", ver, NSST_VERSION);
break;
}
default: /* Primary DA */
if (term->vt_version < 200) {
switch (term->vt_version) {
case 125: term_answerback(term, CSI"?12;2;0;10c"); break;
case 102: term_answerback(term, CSI"?6c"); break;
case 101: term_answerback(term, CSI"?1;0c"); break;
default: term_answerback(term, CSI"?1;2c");
}
} else {
/*1 - 132-columns
*2 - Printer
*3 - ReGIS graphics
*4 - Sixel graphics
*6 - Selective erase
*8 - User-defined keys
*9 - National Replacement Character sets
*15 - Technical characters
*16 - Locator port
*17 - Terminal state interrogation
*18 - User windows
*21 - Horizontal scrolling
*22 - ANSI color
*28 - Rectangular editing
*29 - ANSI text locator (i.e., DEC Locator mode).
*/
term_answerback(term, CSI"?%u;1;2;6%s;9;15%sc",
60 + term->vt_version/100,
term->kstate.keyboard_mapping == keymap_vt220 ? ";8" : "",
term->vt_level >= 4 ? ";16;17;18;21;22;28;29" : ";22;29");
}
}
}
static void term_dispatch_dsr(struct term *term) {
struct screen *scr = &term->scr;
if (term->esc.selector & P_MASK) {
switch (term->esc.param[0]) {
case 6: /* DECXCPR -- CSI ? Py ; Px ; R ; 1 */
term_answerback(term, CSI"?%zu;%zu%sR",
screen_cursor_y(scr) - screen_min_oy(scr) + 1,
screen_cursor_x(scr) - screen_min_ox(scr) + 1,
term->vt_level >= 4 ? ";1" : "");
break;
case 15: /* Printer status -- Has printer*/
CHK_VT(2);
term_answerback(term, printer_is_available(screen_printer(scr)) ? CSI"?10n" : CSI"?13n");
break;
case 25: /* User defined keys lock */
CHK_VT(2);
term_answerback(term, CSI"?%un", 20 + term->kstate.udk_locked);
break;
case 26: /* Keyboard language -- North American */
CHK_VT(2);
term_answerback(term, CSI"?27;1%sn",
term->vt_level >= 4 ? ";0;0" : /* ready, LK201 */
term->vt_level >= 3 ? ";0" : ""); /* ready */
break;
case 53: /* Report locator status */
case 55:
CHK_VT(4);
term_answerback(term, CSI"?53n"); /* Locator available */
break;
case 56: /* Report locator type */
CHK_VT(4);
term_answerback(term, CSI"?57;1n"); /* Mouse */
break;
case 62: /* DECMSR, Macro space -- No data, no space for macros */
CHK_VT(4);
term_answerback(term, CSI"0*{");
break;
case 63: /* DECCKSR, Memory checksum -- 0000 (hex) */
CHK_VT(4);
term_answerback(term, DCS"%u!~0000"ST, PARAM(1, 0));
break;
case 75: /* Data integrity -- Ready, no errors */
CHK_VT(4);
term_answerback(term, CSI"?70n");
break;
case 85: /* Multi-session configuration -- Not configured */
CHK_VT(4);
term_answerback(term, CSI"?83n");
}
} else {
switch (term->esc.param[0]) {
case 5: /* Health report -- OK */
term_answerback(term, CSI"0n");
break;
case 6: /* CPR -- CSI Py ; Px R */
term_answerback(term, CSI"%zu;%zuR",
screen_cursor_y(scr) - screen_min_oy(scr) + 1,
screen_cursor_x(scr) - screen_min_ox(scr) + 1);
break;
}
}
}
static inline bool term_parse_cursor_report(struct term *term, char *dstr) {
struct screen *scr = &term->scr;
/* Cursor Y */
ssize_t y = strtoul(dstr, &dstr, 10);
if (!dstr || *dstr++ != ';') return 0;
/* Cursor X */
ssize_t x = strtoul(dstr, &dstr, 10);
if (!dstr || *dstr++ != ';') return 0;
/* Page, always '1' */
if (*dstr++ != '1' || *dstr++ != ';') return 0;
/* SGR */
char sgr0 = *dstr++, sgr1 = 0x40;
if ((sgr0 & 0xD0) != 0x40) return 0;
/* Optional extended byte */
if (sgr0 & 0x20) sgr1 = *dstr++;
if ((sgr1 & 0xF0) != 0x40 || *dstr++ != ';') return 0;
/* Protection */
char prot = *dstr++;
if ((prot & 0xFE) != 0x40 || *dstr++ != ';') return 0;
/* Flags */
char flags = *dstr++;
if ((flags & 0xF0) != 0x40 || *dstr++ != ';') return 0;
/* GL */
unsigned long gl = strtoul(dstr, &dstr, 10);
if (!dstr || *dstr++ != ';'|| gl > 3) return 0;
/* GR */
unsigned long gr = strtoul(dstr, &dstr, 10);
if (!dstr || *dstr++ != ';' || gr > 3) return 0;
/* G0 - G3 sizes */
char c96 = *dstr++;
if ((flags & 0xF0) != 0x40 || *dstr++ != ';') return 0;
/* G0 - G3 */
enum charset gn[4];
for (size_t i = 0; i < 4; i++) {
uparam_t sel = 0;
char c;
if ((c = *dstr++) < 0x30) {
sel |= I1(c);
if ((c = *dstr++) < 0x30) return 0;
}
sel |= E(c);
if ((gn[i] = nrcs_parse(sel, (c96 >> i) & 1,
term->vt_level, term->mode.enable_nrcs)) == nrcs_invalid) return 0;
}
/* Everything is OK, load */
*screen_cursor(scr) = (struct cursor) {
.x = MIN(x, screen_width(scr) - 1),
.y = MIN(y, screen_height(scr) - 1),
.origin = flags & 1,
.pending = !!(flags & 8),
.gn = { gn[0], gn[1], gn[2], gn[3] },
.gl = gl,
.gr = gr,
.gl_ss = flags & 4 ? 3 : flags & 2 ? 2 : screen_cursor(scr)->gl
};
#if USE_URI
uri_unref(screen_sgr(scr)->uri);
#endif
*screen_sgr(scr) = (struct attr) {
.fg = screen_sgr(scr)->fg,
.bg = screen_sgr(scr)->bg,
.ul = screen_sgr(scr)->ul,
.uri = screen_sgr(scr)->uri,
.protected = prot & 1,
.bold = sgr0 & 1,
.underlined = sgr0 & 2,
.blink = sgr0 & 4,
.reverse = sgr0 & 8,
.italic = sgr1 & 1,
.faint = sgr1 & 2,
.strikethrough = sgr1 & 4,
.invisible = sgr1 & 8,
};
return 1;
}
static inline bool term_parse_tabs_report(struct term *term, const uint8_t *dstr, const uint8_t *dend) {
screen_clear_tabs(&term->scr);
for (ssize_t tab = 0; dstr <= dend; dstr++) {
if (*dstr == '/' || dstr == dend) {
if (tab - 1 < screen_width(&term->scr))
screen_set_tab(&term->scr, tab - 1, 1);
tab = 0;
} else if (isdigit(*dstr)) {
tab = 10 * tab + *dstr - '0';
} else return 0;
}
return 1;
}
static void term_dispatch_dcs(struct term *term) {
struct screen *scr = &term->scr;
/* Fixup parameter count */
term->esc.i += term->esc.param[term->esc.i] >= 0;
if (term->esc.state != esc_dcs_string) {
term->esc.selector = term->esc.old_selector;
term->esc.state = term->esc.old_state;
}
term_esc_dump(term, 1);
/* Only SGR is allowed to have subparams */
if (term->esc.subpar_mask) {
term_esc_dump(term, 0);
goto finish;
}
uint8_t *dstr = term_esc_str(term);
uint8_t *dend = dstr + term->esc.str_len;
switch (term->esc.selector) {
case C('s') | P('='): /* iTerm2 synchronized updates */
switch (PARAM(0,0)) {
case 1: /* Begin synchronous update */
window_set_sync(screen_window(scr), 1);
break;
case 2: /* End synchronous update */
window_set_sync(screen_window(scr), 0);
break;
default:
term_esc_dump(term, 0);
}
break;
case C('q') | I0('$'): /* DECRQSS -> DECRPSS */ {
if (term->esc.str_len && term->esc.str_len < 3) {
uint16_t id = *dstr | dstr[1] << 8;
switch (id) {
case 'm': /* -> SGR */ {
char sgr[SGR_BUFSIZ];
encode_sgr(sgr, sgr + sizeof sgr, screen_sgr(scr));
term_answerback(term, DCS"1$r%sm"ST, sgr);
break;
}
case 'r': /* -> DECSTBM */
term_answerback(term, DCS"1$r%zu;%zur"ST,
screen_min_y(scr) + 1, screen_max_y(scr));
break;
case 's': /* -> DECSLRM */
term_answerback(term, term->vt_level >= 4 ? DCS"1$r%zu;%zus"ST : DCS"0$r"ST,
screen_min_x(scr) + 1, screen_max_x(scr));
break;
case 't': /* -> DECSLPP */
/* Can't report less than 24 lines */
term_answerback(term, DCS"1$r%zut"ST, MAX(screen_height(scr), 24));
break;
case '|' << 8 | '$': /* -> DECSCPP */
/* It should be either 80 or 132 despite actual column count
* New apps use ioctl(TIOGWINSZ, ...) instead */
term_answerback(term, DCS"1$r%u$|"ST, term->mode.columns_132 ? 132 : 80);
break;
case 'q' << 8 | '"': /* -> DECSCA */
term_answerback(term, DCS"1$r%u\"q"ST, 2 - (screen_sgr(scr)->protected && !term->mode.protected));
break;
case 'q' << 8 | ' ': /* -> DECSCUSR */
term_answerback(term, DCS"1$r%u q"ST, window_cfg(screen_window(scr))->cursor_shape);
break;
case '|' << 8 | '*': /* -> DECSLNS */
term_answerback(term, DCS"1$r%zu*|"ST, screen_height(scr));
break;
case 'x' << 8 | '*': /* -> DECSACE */;
struct screen_mode *smode = &scr->mode;
term_answerback(term, term->vt_level < 4 ? DCS"0$r"ST :
DCS"1$r%u*x"ST, smode->attr_ext_rectangle + 1);
break;
case 'p' << 8 | '"': /* -> DECSCL */
term_answerback(term, DCS"1$r%u%s\"p"ST, 60 + MAX(term->vt_level, 1),
term->vt_level >= 2 ? (term->mode.eight_bit ? ";2" : ";1") : "");
break;
case 't' << 8 | ' ': /* -> DECSWBV */ {
uparam_t val = 8;
if (term->bvol == window_cfg(screen_window(scr))->bell_low_volume) val = 4;
else if (!term->bvol) val = 0;
term_answerback(term, DCS"1$r%u t"ST, val);
break;
}
case 'u' << 8 | ' ': /* -> DECSMBV */ {
uparam_t val = screen_get_margin_bell_volume(scr) * 4;
term_answerback(term, DCS"1$r%u u"ST, val);
break;
}
default:
/* Invalid request */
term_answerback(term, DCS"0$r"ST);
term_esc_dump(term, 0);
}
} else term_esc_dump(term, 0);
break;
}
case C('t') | I0('$'): /* DECRSPS */
switch (PARAM(0, 0)) {
case 1: /* <- DECCIR */
if (!term_parse_cursor_report(term, (char *)term_esc_str(term)))
term_esc_dump(term, 0);
break;
case 2: /* <- DECTABSR */
if (!term_parse_tabs_report(term, term_esc_str(term),
term_esc_str(term) + term->esc.str_len))
term_esc_dump(term, 0);
break;
default:
term_esc_dump(term, 0);
}
break;
case C('|'): /* DECUDK */
keyboard_set_udk(term, dstr, dend, !PARAM(0, 0), !PARAM(1, 0));
break;
case C('u') | I0('!'): /* DECAUPSS */ {
uint32_t sel = 0;
if (term->esc.str_len == 1 && *dstr > 0x2F && *dstr < 0x7F) {
sel = E(*dstr);
} else if (term->esc.str_len == 2 && *dstr >= 0x20 && *dstr < 0x30 && dstr[1] > 0x2F && dstr[1] < 0x7F) {
sel = E(dstr[1]) | I0(dstr[0]);
} else {
term_esc_dump(term, 0);
break;
}
enum charset cs = nrcs_parse(sel, PARAM(0, 0), term->vt_level, term->mode.enable_nrcs);
if (cs != nrcs_invalid) screen_set_upcs(scr, cs);
break;
}
case C('q') | I0('+'): /* XTGETTCAP */ {
// TODO Termcap: Support proper tcap db
// for now, just implement select few of them
bool valid = true;
if (!strcmp((char *)dstr, "436F") || /* "Co" */
!strcmp((char *)dstr, "636F6C6F7266")) { /* "colors" */
uint8_t tmp[16];
int len = snprintf((char *)tmp, sizeof tmp, "%u", PALETTE_SIZE - SPECIAL_PALETTE_SIZE);
*dend = '=';
hex_encode(dend + 1, tmp, tmp + len);
} else if (!strcmp((char *)dstr, "544E") || /* "TN" */
!strcmp((char *)dstr, "6E6A6D65")) { /* "name" */
const uint8_t tmp[5] = "nsst";
*dend = '=';
hex_encode(dend + 1, tmp, tmp + sizeof tmp - 1);
} else if (!strcmp((char *)dstr, "524742")) /* "RGB" == 8 */ {
const uint8_t tmp[2] = "8";
*dend = '=';
hex_encode(dend + 1, tmp, tmp + sizeof tmp - 1);
} else {
valid = false;
}
term_answerback(term, DCS"%u+r%s"ST, valid, dstr);
break;
}
//case C('p') | I0('+'): /* XTSETTCAP */ // TODO Termcap
// break;
default:
term_esc_dump(term, 0);
}
finish:
term_esc_finish_string(term);
term->esc.old_state = 0;
term->esc.state = esc_ground;
}
static enum clip_target decode_target(uint8_t targ, bool mode) {
switch (targ) {
case 'p': return clip_primary;
case 'q': return clip_secondary;
case 'c': return clip_clipboard;
case 's': return mode ? clip_clipboard : clip_primary;
default:
return clip_invalid;
}
}
static uint32_t selector_to_cid(uint32_t sel, bool rev) {
switch (sel) {
case 10: return rev ? SPECIAL_BG : SPECIAL_FG;
case 11: return rev ? SPECIAL_FG : SPECIAL_BG;
case 12: return rev ? SPECIAL_CURSOR_BG : SPECIAL_CURSOR_FG;
case 17: return rev ? SPECIAL_SELECTED_FG : SPECIAL_SELECTED_BG;
case 19: return rev ? SPECIAL_SELECTED_BG : SPECIAL_SELECTED_FG;
}
warn("Unreachable");
return 0;
}
static void term_colors_changed(struct term *term, uint32_t sel, color_t col) {
struct window *win = screen_window(&term->scr);
switch (sel) {
case 10:
if (term->mode.reverse_video)
window_set_colors(win, col, 0);
break;
case 11:
if (!(term->mode.reverse_video))
window_set_colors(win, term->palette[SPECIAL_CURSOR_BG] = col, 0);
else
window_set_colors(win, 0, term->palette[SPECIAL_CURSOR_FG] = col);
break;
case 12:
if (!(term->mode.reverse_video))
window_set_colors(win, 0, col);
break;
case 17: case 19:
screen_damage_selection(&term->scr);
}
}
static void term_do_set_color(struct term *term, uint32_t sel, uint8_t *dstr, uint8_t *dend) {
color_t col;
uint32_t cid = selector_to_cid(sel, term->mode.reverse_video);
if (!strcmp((char *)dstr, "?")) {
col = term->palette[cid];
term_answerback(term, OSC"%u;rgb:%04x/%04x/%04x"ST, sel,
color_r(col) * 0x101, color_g(col) * 0x101, color_b(col) * 0x101);
} else if ((col = parse_color(dstr, dend))) {
term->palette[cid] = col;
term_colors_changed(term, sel, col);
} else term_esc_dump(term, 0);
}
static void term_do_reset_color(struct term *term) {
uint32_t cid = selector_to_cid(term->esc.selector - 100, term->mode.reverse_video);
color_t *palette = window_cfg(screen_window(&term->scr))->palette;
term->palette[cid] = palette[selector_to_cid(term->esc.selector - 100, 0)];
term_colors_changed(term, term->esc.selector - 100, term->esc.selector == 111 ?
palette[SPECIAL_CURSOR_BG] : term->palette[cid]);
}
static inline bool is_osc_state(uint32_t state) {
return state == esc_osc_string || state == esc_osc_1 || state == esc_osc_2;
}
void send_notification(const char *notify_cmd, const char *title, const char *msg) {
if (gconfig.trace_misc)
info("Notify cmd='%s' title='%s' text='%s'", notify_cmd, title, msg);
if (!fork()) {
execlp(notify_cmd,
notify_cmd,
title ? title : "",
msg, NULL);
_exit(127);
}
}
static void term_dispatch_osc(struct term *term) {
if (!is_osc_state(term->esc.state)) {
term->esc.state = term->esc.old_state;
term->esc.selector = term->esc.old_selector;
}
term_esc_dump(term, 1);
uint8_t *dstr = term_esc_str(term);
uint8_t *dend = dstr + term->esc.str_len;
struct screen *scr = &term->scr;
struct instance_config *cfg = window_cfg(screen_window(scr));
switch (term->esc.selector) {
color_t col;
case 0: /* Change window icon name and title */
case 1: /* Change window icon name */
case 2: /* Change window title */ {
if (term->mode.title_set_hex) {
if (*hex_decode(dstr, dstr, dend)) {
term_esc_dump(term, 0);
break;
}
dend = memchr(dstr, 0, ESC_MAX_STR);
}
bool set_utf8 = term->mode.utf8 || window_cfg(term_window(term))->force_utf8_title;
uint8_t *res = NULL;
if (term->mode.title_set_utf8 && !set_utf8) {
uint8_t *dst = dstr;
const uint8_t *ptr = dst;
uint32_t val = 0;
while (*ptr && utf8_decode(&val, &ptr, dend))
*dst++ = val;
*dst = '\0';
} else if (!term->mode.title_set_utf8 && set_utf8) {
res = xalloc(term->esc.str_len * 2 + 1);