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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
|
/* $OpenBSD: input.c,v 1.3 2009/06/03 23:30:40 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* 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 <sys/types.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
#define INPUT_C0CONTROL(ch) (ch <= 0x1f)
#define INPUT_INTERMEDIATE(ch) (ch == 0xa0 || (ch >= 0x20 && ch <= 0x2f))
#define INPUT_PARAMETER(ch) (ch >= 0x30 && ch <= 0x3f)
#define INPUT_UPPERCASE(ch) (ch >= 0x40 && ch <= 0x5f)
#define INPUT_LOWERCASE(ch) (ch >= 0x60 && ch <= 0x7e)
#define INPUT_DELETE(ch) (ch == 0x7f)
#define INPUT_C1CONTROL(ch) (ch >= 0x80 && ch <= 0x9f)
#define INPUT_G1DISPLAYABLE(ch) (ch >= 0xa1 && ch <= 0xfe)
#define INPUT_SPECIAL(ch) (ch == 0xff)
int input_get_argument(struct input_ctx *, u_int, uint16_t *, uint16_t);
int input_new_argument(struct input_ctx *);
int input_add_argument(struct input_ctx *, u_char);
void input_start_string(struct input_ctx *, int);
void input_abort_string(struct input_ctx *);
int input_add_string(struct input_ctx *, u_char);
char *input_get_string(struct input_ctx *);
void input_state(struct input_ctx *, void *);
void input_state_first(u_char, struct input_ctx *);
void input_state_escape(u_char, struct input_ctx *);
void input_state_intermediate(u_char, struct input_ctx *);
void input_state_sequence_first(u_char, struct input_ctx *);
void input_state_sequence_next(u_char, struct input_ctx *);
void input_state_sequence_intermediate(u_char, struct input_ctx *);
void input_state_string_next(u_char, struct input_ctx *);
void input_state_string_escape(u_char, struct input_ctx *);
void input_state_utf8(u_char, struct input_ctx *);
void input_handle_character(u_char, struct input_ctx *);
void input_handle_c0_control(u_char, struct input_ctx *);
void input_handle_c1_control(u_char, struct input_ctx *);
void input_handle_private_two(u_char, struct input_ctx *);
void input_handle_standard_two(u_char, struct input_ctx *);
void input_handle_sequence(u_char, struct input_ctx *);
void input_handle_sequence_cuu(struct input_ctx *);
void input_handle_sequence_cud(struct input_ctx *);
void input_handle_sequence_cuf(struct input_ctx *);
void input_handle_sequence_cub(struct input_ctx *);
void input_handle_sequence_dch(struct input_ctx *);
void input_handle_sequence_dl(struct input_ctx *);
void input_handle_sequence_ich(struct input_ctx *);
void input_handle_sequence_il(struct input_ctx *);
void input_handle_sequence_vpa(struct input_ctx *);
void input_handle_sequence_hpa(struct input_ctx *);
void input_handle_sequence_cup(struct input_ctx *);
void input_handle_sequence_cup(struct input_ctx *);
void input_handle_sequence_ed(struct input_ctx *);
void input_handle_sequence_el(struct input_ctx *);
void input_handle_sequence_sm(struct input_ctx *);
void input_handle_sequence_rm(struct input_ctx *);
void input_handle_sequence_decstbm(struct input_ctx *);
void input_handle_sequence_sgr(struct input_ctx *);
void input_handle_sequence_dsr(struct input_ctx *);
int input_sequence_cmp(const void *, const void *);
struct input_sequence_entry {
u_char ch;
void (*fn)(struct input_ctx *);
};
const struct input_sequence_entry input_sequence_table[] = {
{ '@', input_handle_sequence_ich },
{ 'A', input_handle_sequence_cuu },
{ 'B', input_handle_sequence_cud },
{ 'C', input_handle_sequence_cuf },
{ 'D', input_handle_sequence_cub },
{ 'G', input_handle_sequence_hpa },
{ 'H', input_handle_sequence_cup },
{ 'J', input_handle_sequence_ed },
{ 'K', input_handle_sequence_el },
{ 'L', input_handle_sequence_il },
{ 'M', input_handle_sequence_dl },
{ 'P', input_handle_sequence_dch },
{ 'd', input_handle_sequence_vpa },
{ 'f', input_handle_sequence_cup },
{ 'h', input_handle_sequence_sm },
{ 'l', input_handle_sequence_rm },
{ 'm', input_handle_sequence_sgr },
{ 'n', input_handle_sequence_dsr },
{ 'r', input_handle_sequence_decstbm },
};
int
input_sequence_cmp(const void *a, const void *b)
{
int ai = ((const struct input_sequence_entry *) a)->ch;
int bi = ((const struct input_sequence_entry *) b)->ch;
return (ai - bi);
}
int
input_new_argument(struct input_ctx *ictx)
{
struct input_arg *arg;
ARRAY_EXPAND(&ictx->args, 1);
arg = &ARRAY_LAST(&ictx->args);
arg->used = 0;
return (0);
}
int
input_add_argument(struct input_ctx *ictx, u_char ch)
{
struct input_arg *arg;
if (ARRAY_LENGTH(&ictx->args) == 0)
return (0);
arg = &ARRAY_LAST(&ictx->args);
if (arg->used > (sizeof arg->data) - 1)
return (-1);
arg->data[arg->used++] = ch;
return (0);
}
int
input_get_argument(struct input_ctx *ictx, u_int i, uint16_t *n, uint16_t d)
{
struct input_arg *arg;
const char *errstr;
*n = d;
if (i >= ARRAY_LENGTH(&ictx->args))
return (0);
arg = &ARRAY_ITEM(&ictx->args, i);
if (*arg->data == '\0')
return (0);
*n = strtonum(arg->data, 0, UINT16_MAX, &errstr);
if (errstr != NULL)
return (-1);
return (0);
}
void
input_start_string(struct input_ctx *ictx, int type)
{
ictx->string_type = type;
ictx->string_len = 0;
}
void
input_abort_string(struct input_ctx *ictx)
{
if (ictx->string_buf != NULL)
xfree(ictx->string_buf);
ictx->string_buf = NULL;
}
int
input_add_string(struct input_ctx *ictx, u_char ch)
{
ictx->string_buf = xrealloc(ictx->string_buf, 1, ictx->string_len + 1);
ictx->string_buf[ictx->string_len++] = ch;
if (ictx->string_len >= MAXSTRINGLEN) {
input_abort_string(ictx);
return (1);
}
return (0);
}
char *
input_get_string(struct input_ctx *ictx)
{
char *s;
if (ictx->string_buf == NULL || input_add_string(ictx, '\0') != 0)
return (xstrdup(""));
s = ictx->string_buf;
ictx->string_buf = NULL;
return (s);
}
void
input_state(struct input_ctx *ictx, void *state)
{
ictx->state = state;
}
void
input_init(struct window_pane *wp)
{
struct input_ctx *ictx = &wp->ictx;
ARRAY_INIT(&ictx->args);
ictx->string_len = 0;
ictx->string_buf = NULL;
memcpy(&ictx->cell, &grid_default_cell, sizeof ictx->cell);
memcpy(&ictx->saved_cell, &grid_default_cell, sizeof ictx->saved_cell);
ictx->saved_cx = 0;
ictx->saved_cy = 0;
input_state(ictx, input_state_first);
}
void
input_free(struct window_pane *wp)
{
if (wp->ictx.string_buf != NULL)
xfree(wp->ictx.string_buf);
ARRAY_FREE(&wp->ictx.args);
}
void
input_parse(struct window_pane *wp)
{
struct input_ctx *ictx = &wp->ictx;
u_char ch;
if (BUFFER_USED(wp->in) == 0)
return;
ictx->buf = BUFFER_OUT(wp->in);
ictx->len = BUFFER_USED(wp->in);
ictx->off = 0;
ictx->wp = wp;
log_debug2("entry; buffer=%zu", ictx->len);
if (wp->mode == NULL)
screen_write_start(&ictx->ctx, wp, &wp->base);
else
screen_write_start(&ictx->ctx, NULL, &wp->base);
if (ictx->off != ictx->len)
wp->window->flags |= WINDOW_ACTIVITY;
while (ictx->off < ictx->len) {
ch = ictx->buf[ictx->off++];
ictx->state(ch, ictx);
}
screen_write_stop(&ictx->ctx);
buffer_remove(wp->in, ictx->len);
}
void
input_state_first(u_char ch, struct input_ctx *ictx)
{
ictx->intermediate = '\0';
if (INPUT_C0CONTROL(ch)) {
if (ch == 0x1b)
input_state(ictx, input_state_escape);
else
input_handle_c0_control(ch, ictx);
return;
}
#if 0
if (INPUT_C1CONTROL(ch)) {
ch -= 0x40;
if (ch == '[')
input_state(ictx, input_state_sequence_first);
else if (ch == ']') {
input_start_string(ictx, STRING_SYSTEM);
input_state(ictx, input_state_string_next);
} else if (ch == '_') {
input_start_string(ictx, STRING_APPLICATION);
input_state(ictx, input_state_string_next);
} else
input_handle_c1_control(ch, ictx);
return;
}
#endif
if (INPUT_DELETE(ch))
return;
input_handle_character(ch, ictx);
}
void
input_state_escape(u_char ch, struct input_ctx *ictx)
{
/* Treat C1 control and G1 displayable as 7-bit equivalent. */
if (INPUT_C1CONTROL(ch) || INPUT_G1DISPLAYABLE(ch))
ch &= 0x7f;
if (INPUT_C0CONTROL(ch)) {
input_handle_c0_control(ch, ictx);
return;
}
if (INPUT_INTERMEDIATE(ch)) {
log_debug2(":: in1 %zu: %hhu (%c)", ictx->off, ch, ch);
ictx->intermediate = ch;
input_state(ictx, input_state_intermediate);
return;
}
if (INPUT_PARAMETER(ch)) {
input_state(ictx, input_state_first);
input_handle_private_two(ch, ictx);
return;
}
if (INPUT_UPPERCASE(ch)) {
if (ch == '[')
input_state(ictx, input_state_sequence_first);
else if (ch == ']') {
input_start_string(ictx, STRING_SYSTEM);
input_state(ictx, input_state_string_next);
} else if (ch == '_') {
input_start_string(ictx, STRING_APPLICATION);
input_state(ictx, input_state_string_next);
} else {
input_state(ictx, input_state_first);
input_handle_c1_control(ch, ictx);
}
return;
}
if (INPUT_LOWERCASE(ch)) {
input_state(ictx, input_state_first);
input_handle_standard_two(ch, ictx);
return;
}
input_state(ictx, input_state_first);
}
void
input_state_intermediate(u_char ch, struct input_ctx *ictx)
{
if (INPUT_INTERMEDIATE(ch)) {
/* Multiple intermediates currently ignored. */
log_debug2(":: in2 %zu: %hhu (%c)", ictx->off, ch, ch);
return;
}
if (INPUT_PARAMETER(ch)) {
input_state(ictx, input_state_first);
input_handle_private_two(ch, ictx);
return;
}
if (INPUT_UPPERCASE(ch) || INPUT_LOWERCASE(ch)) {
input_state(ictx, input_state_first);
input_handle_standard_two(ch, ictx);
return;
}
input_state(ictx, input_state_first);
}
void
input_state_sequence_first(u_char ch, struct input_ctx *ictx)
{
ictx->private = '\0';
ARRAY_CLEAR(&ictx->args);
input_state(ictx, input_state_sequence_next);
if (INPUT_PARAMETER(ch)) {
input_new_argument(ictx);
if (ch >= 0x3c && ch <= 0x3f) {
/* Private control sequence. */
ictx->private = ch;
return;
}
}
/* Pass character on directly. */
input_state_sequence_next(ch, ictx);
}
void
input_state_sequence_next(u_char ch, struct input_ctx *ictx)
{
if (INPUT_INTERMEDIATE(ch)) {
if (input_add_argument(ictx, '\0') != 0)
input_state(ictx, input_state_first);
else {
log_debug2(":: si1 %zu: %hhu (%c)", ictx->off, ch, ch);
input_state(ictx, input_state_sequence_intermediate);
}
return;
}
if (INPUT_PARAMETER(ch)) {
if (ch == ';') {
if (input_add_argument(ictx, '\0') != 0)
input_state(ictx, input_state_first);
else
input_new_argument(ictx);
} else if (input_add_argument(ictx, ch) != 0)
input_state(ictx, input_state_first);
return;
}
if (INPUT_UPPERCASE(ch) || INPUT_LOWERCASE(ch)) {
if (input_add_argument(ictx, '\0') != 0)
input_state(ictx, input_state_first);
else {
input_state(ictx, input_state_first);
input_handle_sequence(ch, ictx);
}
return;
}
input_state(ictx, input_state_first);
}
void
input_state_sequence_intermediate(u_char ch, struct input_ctx *ictx)
{
if (INPUT_INTERMEDIATE(ch)) {
log_debug2(":: si2 %zu: %hhu (%c)", ictx->off, ch, ch);
return;
}
if (INPUT_UPPERCASE(ch) || INPUT_LOWERCASE(ch)) {
input_state(ictx, input_state_first);
input_handle_sequence(ch, ictx);
return;
}
input_state(ictx, input_state_first);
}
void
input_state_string_next(u_char ch, struct input_ctx *ictx)
{
if (ch == 0x1b) {
input_state(ictx, input_state_string_escape);
return;
}
if (ch == 0x07) {
input_state_string_escape(ch, ictx);
return;
}
if (ch >= 0x20) {
if (input_add_string(ictx, ch) != 0)
input_state(ictx, input_state_first);
return;
}
}
void
input_state_string_escape(u_char ch, struct input_ctx *ictx)
{
char *s;
if (ch == '\007' || ch == '\\') {
input_state(ictx, input_state_first);
switch (ictx->string_type) {
case STRING_SYSTEM:
if (ch != '\007')
return;
s = input_get_string(ictx);
if ((s[0] != '0' && s[0] != '2') || s[1] != ';') {
xfree(s);
return;
}
screen_set_title(ictx->ctx.s, s + 2);
server_status_window(ictx->wp->window);
xfree(s);
break;
case STRING_APPLICATION:
if (ch != '\\')
return;
s = input_get_string(ictx);
screen_set_title(ictx->ctx.s, s);
server_status_window(ictx->wp->window);
xfree(s);
break;
case STRING_NAME:
if (ch != '\\')
return;
xfree(ictx->wp->window->name);
ictx->wp->window->name = input_get_string(ictx);
server_status_window(ictx->wp->window);
break;
}
return;
}
input_state(ictx, input_state_string_next);
input_state_string_next(ch, ictx);
}
void
input_state_utf8(u_char ch, struct input_ctx *ictx)
{
log_debug2("-- un %zu: %hhu (%c)", ictx->off, ch, ch);
ictx->utf8_buf[ictx->utf8_off++] = ch;
if (--ictx->utf8_len != 0)
return;
input_state(ictx, input_state_first);
ictx->cell.flags |= GRID_FLAG_UTF8;
screen_write_cell(&ictx->ctx, &ictx->cell, ictx->utf8_buf);
ictx->cell.flags &= ~GRID_FLAG_UTF8;
}
void
input_handle_character(u_char ch, struct input_ctx *ictx)
{
struct window_pane *wp = ictx->wp;
if (ch > 0x7f && options_get_number(&wp->window->options, "utf8")) {
/*
* UTF-8 sequence.
*
* 11000010-11011111 C2-DF start of 2-byte sequence
* 11100000-11101111 E0-EF start of 3-byte sequence
* 11110000-11110100 F0-F4 start of 4-byte sequence
*/
memset(ictx->utf8_buf, 0xff, sizeof ictx->utf8_buf);
ictx->utf8_buf[0] = ch;
ictx->utf8_off = 1;
if (ch >= 0xc2 && ch <= 0xdf) {
log_debug2("-- u2 %zu: %hhu (%c)", ictx->off, ch, ch);
input_state(ictx, input_state_utf8);
ictx->utf8_len = 1;
return;
}
if (ch >= 0xe0 && ch <= 0xef) {
log_debug2("-- u3 %zu: %hhu (%c)", ictx->off, ch, ch);
input_state(ictx, input_state_utf8);
ictx->utf8_len = 2;
return;
}
if (ch >= 0xf0 && ch <= 0xf4) {
log_debug2("-- u4 %zu: %hhu (%c)", ictx->off, ch, ch);
input_state(ictx, input_state_utf8);
ictx->utf8_len = 3;
return;
}
}
log_debug2("-- ch %zu: %hhu (%c)", ictx->off, ch, ch);
ictx->cell.data = ch;
screen_write_cell(&ictx->ctx, &ictx->cell, ictx->utf8_buf);
}
void
input_handle_c0_control(u_char ch, struct input_ctx *ictx)
{
struct screen *s = ictx->ctx.s;
log_debug2("-- c0 %zu: %hhu", ictx->off, ch);
switch (ch) {
case '\0': /* NUL */
break;
case '\n': /* LF */
screen_write_linefeed(&ictx->ctx);
break;
case '\r': /* CR */
screen_write_carriagereturn(&ictx->ctx);
break;
case '\007': /* BELL */
ictx->wp->window->flags |= WINDOW_BELL;
break;
case '\010': /* BS */
screen_write_cursorleft(&ictx->ctx, 1);
break;
case '\011': /* TAB */
s->cx = ((s->cx / 8) * 8) + 8;
if (s->cx > screen_size_x(s) - 1) {
s->cx = 0;
screen_write_cursordown(&ictx->ctx, 1);
}
screen_write_cursormove(&ictx->ctx, s->cx, s->cy);
break;
case '\016': /* SO */
ictx->cell.attr |= GRID_ATTR_CHARSET;
break;
case '\017': /* SI */
ictx->cell.attr &= ~GRID_ATTR_CHARSET;
break;
default:
log_debug("unknown c0: %hhu", ch);
break;
}
}
void
input_handle_c1_control(u_char ch, struct input_ctx *ictx)
{
log_debug2("-- c1 %zu: %hhu (%c)", ictx->off, ch, ch);
switch (ch) {
case 'D': /* IND */
screen_write_linefeed(&ictx->ctx);
break;
case 'E': /* NEL */
screen_write_carriagereturn(&ictx->ctx);
screen_write_linefeed(&ictx->ctx);
break;
case 'M': /* RI */
screen_write_reverseindex(&ictx->ctx);
break;
default:
log_debug("unknown c1: %hhu", ch);
break;
}
}
void
input_handle_private_two(u_char ch, struct input_ctx *ictx)
{
struct screen *s = ictx->ctx.s;
log_debug2(
"-- p2 %zu: %hhu (%c) %hhu", ictx->off, ch, ch, ictx->intermediate);
switch (ch) {
case '0': /* SCS */
/*
* Not really supported, but fake it up enough for those that
* use it to switch character sets (by redefining G0 to
* graphics set, rather than switching to G1).
*/
switch (ictx->intermediate) {
case '(': /* G0 */
ictx->cell.attr |= GRID_ATTR_CHARSET;
break;
}
break;
case '=': /* DECKPAM */
if (ictx->intermediate != '\0')
break;
screen_write_kkeypadmode(&ictx->ctx, 1);
log_debug("kkeypad on (application mode)");
break;
case '>': /* DECKPNM */
if (ictx->intermediate != '\0')
break;
screen_write_kkeypadmode(&ictx->ctx, 0);
log_debug("kkeypad off (number mode)");
break;
case '7': /* DECSC */
if (ictx->intermediate != '\0')
break;
memcpy(&ictx->saved_cell, &ictx->cell, sizeof ictx->saved_cell);
ictx->saved_cx = s->cx;
ictx->saved_cy = s->cy;
break;
case '8':
switch (ictx->intermediate) {
case '\0': /* DECRC */
memcpy(
&ictx->cell, &ictx->saved_cell, sizeof ictx->cell);
screen_write_cursormove(
&ictx->ctx, ictx->saved_cx, ictx->saved_cy);
break;
case '#': /* DECALN */
screen_write_alignmenttest(&ictx->ctx);
break;
}
break;
default:
log_debug("unknown p2: %hhu", ch);
break;
}
}
void
input_handle_standard_two(u_char ch, struct input_ctx *ictx)
{
log_debug2(
"-- s2 %zu: %hhu (%c) %hhu", ictx->off, ch, ch, ictx->intermediate);
switch (ch) {
case 'B': /* Dscs (ASCII) */
/*
* Not really supported, but fake it up enough for those that
* use it to switch character sets (by redefining G0 to
* graphics set, rather than switching to G1).
*/
switch (ictx->intermediate) {
case '(': /* G0 */
ictx->cell.attr &= ~GRID_ATTR_CHARSET;
break;
}
break;
case 'c': /* RIS */
memcpy(&ictx->cell, &grid_default_cell, sizeof ictx->cell);
memcpy(&ictx->saved_cell, &ictx->cell, sizeof ictx->saved_cell);
ictx->saved_cx = 0;
ictx->saved_cy = 0;
screen_write_scrollregion(
&ictx->ctx, 0, screen_size_y(ictx->ctx.s) - 1);
screen_write_insertmode(&ictx->ctx, 0);
screen_write_kcursormode(&ictx->ctx, 0);
screen_write_kkeypadmode(&ictx->ctx, 0);
screen_write_mousemode(&ictx->ctx, 0);
screen_write_clearscreen(&ictx->ctx);
screen_write_cursormove(&ictx->ctx, 0, 0);
break;
case 'k':
input_start_string(ictx, STRING_NAME);
input_state(ictx, input_state_string_next);
break;
default:
log_debug("unknown s2: %hhu", ch);
break;
}
}
void
input_handle_sequence(u_char ch, struct input_ctx *ictx)
{
struct input_sequence_entry *entry, find;
struct screen *s = ictx->ctx.s;
u_int i;
struct input_arg *iarg;
log_debug2("-- sq %zu: %hhu (%c): %u [sx=%u, sy=%u, cx=%u, cy=%u, "
"ru=%u, rl=%u]", ictx->off, ch, ch, ARRAY_LENGTH(&ictx->args),
screen_size_x(s), screen_size_y(s), s->cx, s->cy, s->rupper,
s->rlower);
for (i = 0; i < ARRAY_LENGTH(&ictx->args); i++) {
iarg = &ARRAY_ITEM(&ictx->args, i);
if (*iarg->data != '\0')
log_debug2(" ++ %u: %s", i, iarg->data);
}
find.ch = ch;
entry = bsearch(&find,
input_sequence_table, nitems(input_sequence_table),
sizeof input_sequence_table[0], input_sequence_cmp);
if (entry != NULL)
entry->fn(ictx);
else
log_debug("unknown sq: %c (%hhu %hhu)", ch, ch, ictx->private);
}
void
input_handle_sequence_cuu(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_cursorup(&ictx->ctx, n);
}
void
input_handle_sequence_cud(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_cursordown(&ictx->ctx, n);
}
void
input_handle_sequence_cuf(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_cursorright(&ictx->ctx, n);
}
void
input_handle_sequence_cub(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_cursorleft(&ictx->ctx, n);
}
void
input_handle_sequence_dch(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_deletecharacter(&ictx->ctx, n);
}
void
input_handle_sequence_dl(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_deleteline(&ictx->ctx, n);
}
void
input_handle_sequence_ich(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_insertcharacter(&ictx->ctx, n);
}
void
input_handle_sequence_il(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_insertline(&ictx->ctx, n);
}
void
input_handle_sequence_vpa(struct input_ctx *ictx)
{
struct screen *s = ictx->ctx.s;
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_cursormove(&ictx->ctx, s->cx, n - 1);
}
void
input_handle_sequence_hpa(struct input_ctx *ictx)
{
struct screen *s = ictx->ctx.s;
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (n == 0)
n = 1;
screen_write_cursormove(&ictx->ctx, n - 1, s->cy);
}
void
input_handle_sequence_cup(struct input_ctx *ictx)
{
uint16_t n, m;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 2)
return;
if (input_get_argument(ictx, 0, &n, 1) != 0)
return;
if (input_get_argument(ictx, 1, &m, 1) != 0)
return;
if (n == 0)
n = 1;
if (m == 0)
m = 1;
screen_write_cursormove(&ictx->ctx, m - 1, n - 1);
}
void
input_handle_sequence_ed(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 0) != 0)
return;
if (n > 2)
return;
switch (n) {
case 0:
screen_write_clearendofscreen(&ictx->ctx);
break;
case 1:
screen_write_clearstartofscreen(&ictx->ctx);
break;
case 2:
screen_write_clearscreen(&ictx->ctx);
break;
}
}
void
input_handle_sequence_el(struct input_ctx *ictx)
{
uint16_t n;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 0) != 0)
return;
if (n > 2)
return;
switch (n) {
case 0:
screen_write_clearendofline(&ictx->ctx);
break;
case 1:
screen_write_clearstartofline(&ictx->ctx);
break;
case 2:
screen_write_clearline(&ictx->ctx);
break;
}
}
void
input_handle_sequence_sm(struct input_ctx *ictx)
{
uint16_t n;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 0) != 0)
return;
if (ictx->private == '?') {
switch (n) {
case 1: /* GATM */
screen_write_kcursormode(&ictx->ctx, 1);
log_debug("kcursor on");
break;
case 25: /* TCEM */
screen_write_cursormode(&ictx->ctx, 1);
log_debug("cursor on");
break;
case 1000:
screen_write_mousemode(&ictx->ctx, 1);
log_debug("mouse on");
break;
default:
log_debug("unknown SM [%hhu]: %u", ictx->private, n);
break;
}
} else {
switch (n) {
case 4: /* IRM */
screen_write_insertmode(&ictx->ctx, 1);
log_debug("insert on");
break;
case 34:
/* Cursor high visibility not supported. */
break;
default:
log_debug("unknown SM [%hhu]: %u", ictx->private, n);
break;
}
}
}
void
input_handle_sequence_rm(struct input_ctx *ictx)
{
uint16_t n;
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 0) != 0)
return;
if (ictx->private == '?') {
switch (n) {
case 1: /* GATM */
screen_write_kcursormode(&ictx->ctx, 0);
log_debug("kcursor off");
break;
case 25: /* TCEM */
screen_write_cursormode(&ictx->ctx, 0);
log_debug("cursor off");
break;
case 1000:
screen_write_mousemode(&ictx->ctx, 0);
log_debug("mouse off");
break;
default:
log_debug("unknown RM [%hhu]: %u", ictx->private, n);
break;
}
} else if (ictx->private == '\0') {
switch (n) {
case 4: /* IRM */
screen_write_insertmode(&ictx->ctx, 0);
log_debug("insert off");
break;
case 34:
/* Cursor high visibility not supported. */
break;
default:
log_debug("unknown RM [%hhu]: %u", ictx->private, n);
break;
}
}
}
void
input_handle_sequence_dsr(struct input_ctx *ictx)
{
struct screen *s = ictx->ctx.s;
uint16_t n;
char reply[32];
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
if (input_get_argument(ictx, 0, &n, 0) != 0)
return;
if (ictx->private == '\0') {
switch (n) {
case 6: /* cursor position */
xsnprintf(reply, sizeof reply,
"\033[%u;%uR", s->cy + 1, s->cx + 1);
log_debug("cursor request, reply: %s", reply);
buffer_write(ictx->wp->out, reply, strlen(reply));
break;
}
}
}
void
input_handle_sequence_decstbm(struct input_ctx *ictx)
{
struct screen *s = ictx->ctx.s;
uint16_t n, m;
if (ictx->private != '\0')
return;
if (ARRAY_LENGTH(&ictx->args) > 2)
return;
if (input_get_argument(ictx, 0, &n, 0) != 0)
return;
if (input_get_argument(ictx, 1, &m, 0) != 0)
return;
if (n == 0)
n = 1;
if (m == 0)
m = screen_size_y(s);
screen_write_scrollregion(&ictx->ctx, n - 1, m - 1);
}
void
input_handle_sequence_sgr(struct input_ctx *ictx)
{
struct grid_cell *gc = &ictx->cell;
u_int i;
uint16_t m, o;
u_char attr;
if (ARRAY_LENGTH(&ictx->args) == 0) {
attr = gc->attr;
memcpy(gc, &grid_default_cell, sizeof *gc);
gc->attr |= (attr & GRID_ATTR_CHARSET);
return;
}
for (i = 0; i < ARRAY_LENGTH(&ictx->args); i++) {
if (input_get_argument(ictx, i, &m, 0) != 0)
return;
if (m == 38 || m == 48) {
i++;
if (input_get_argument(ictx, i, &o, 0) != 0)
return;
if (o != 5)
continue;
i++;
if (input_get_argument(ictx, i, &o, 0) != 0)
return;
if (m == 38) {
gc->flags |= GRID_FLAG_FG256;
gc->fg = o;
} else if (m == 48) {
gc->flags |= GRID_FLAG_BG256;
gc->bg = o;
}
continue;
}
switch (m) {
case 0:
case 10:
attr = gc->attr;
memcpy(gc, &grid_default_cell, sizeof *gc);
gc->attr |= (attr & GRID_ATTR_CHARSET);
break;
case 1:
gc->attr |= GRID_ATTR_BRIGHT;
break;
case 2:
gc->attr |= GRID_ATTR_DIM;
break;
case 3:
gc->attr |= GRID_ATTR_ITALICS;
break;
case 4:
gc->attr |= GRID_ATTR_UNDERSCORE;
break;
case 5:
gc->attr |= GRID_ATTR_BLINK;
break;
case 7:
gc->attr |= GRID_ATTR_REVERSE;
break;
case 8:
gc->attr |= GRID_ATTR_HIDDEN;
break;
case 22:
gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
break;
case 23:
gc->attr &= ~GRID_ATTR_ITALICS;
break;
case 24:
gc->attr &= ~GRID_ATTR_UNDERSCORE;
break;
case 25:
gc->attr &= ~GRID_ATTR_BLINK;
break;
case 27:
gc->attr &= ~GRID_ATTR_REVERSE;
break;
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
gc->flags &= ~GRID_FLAG_FG256;
gc->fg = m - 30;
break;
case 39:
gc->flags &= ~GRID_FLAG_FG256;
gc->fg = 8;
break;
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
gc->flags &= ~GRID_FLAG_BG256;
gc->bg = m - 40;
break;
case 49:
gc->flags &= ~GRID_FLAG_BG256;
gc->bg = 8;
break;
}
}
}
|