forked from tmux/tmux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.c
2490 lines (2200 loc) · 57.1 KB
/
input.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
/* $OpenBSD$ */
/*
* Copyright (c) 2007 Nicholas Marriott <[email protected]>
*
* 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 <netinet/in.h>
#include <resolv.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tmux.h"
/*
* Based on the description by Paul Williams at:
*
* https://vt100.net/emu/dec_ansi_parser
*
* With the following changes:
*
* - 7-bit only.
*
* - Support for UTF-8.
*
* - OSC (but not APC) may be terminated by \007 as well as ST.
*
* - A state for APC similar to OSC. Some terminals appear to use this to set
* the title.
*
* - A state for the screen \033k...\033\\ sequence to rename a window. This is
* pretty stupid but not supporting it is more trouble than it is worth.
*
* - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
* be passed to the underlying terminals.
*/
/* Input parser cell. */
struct input_cell {
struct grid_cell cell;
int set;
int g0set; /* 1 if ACS */
int g1set; /* 1 if ACS */
};
/* Input parser argument. */
struct input_param {
enum {
INPUT_MISSING,
INPUT_NUMBER,
INPUT_STRING
} type;
union {
int num;
char *str;
};
};
/* Input parser context. */
struct input_ctx {
struct window_pane *wp;
struct screen_write_ctx ctx;
struct input_cell cell;
struct input_cell old_cell;
u_int old_cx;
u_int old_cy;
int old_mode;
u_char interm_buf[4];
size_t interm_len;
u_char param_buf[64];
size_t param_len;
#define INPUT_BUF_START 32
#define INPUT_BUF_LIMIT 1048576
u_char *input_buf;
size_t input_len;
size_t input_space;
enum {
INPUT_END_ST,
INPUT_END_BEL
} input_end;
struct input_param param_list[24];
u_int param_list_len;
struct utf8_data utf8data;
int utf8started;
int ch;
int last;
int flags;
#define INPUT_DISCARD 0x1
const struct input_state *state;
struct event timer;
/*
* All input received since we were last in the ground state. Sent to
* control clients on connection.
*/
struct evbuffer *since_ground;
};
/* Helper functions. */
struct input_transition;
static int input_split(struct input_ctx *);
static int input_get(struct input_ctx *, u_int, int, int);
static void printflike(2, 3) input_reply(struct input_ctx *, const char *, ...);
static void input_set_state(struct window_pane *,
const struct input_transition *);
static void input_reset_cell(struct input_ctx *);
static void input_osc_4(struct input_ctx *, const char *);
static void input_osc_10(struct input_ctx *, const char *);
static void input_osc_11(struct input_ctx *, const char *);
static void input_osc_52(struct input_ctx *, const char *);
static void input_osc_104(struct input_ctx *, const char *);
/* Transition entry/exit handlers. */
static void input_clear(struct input_ctx *);
static void input_ground(struct input_ctx *);
static void input_enter_dcs(struct input_ctx *);
static void input_enter_osc(struct input_ctx *);
static void input_exit_osc(struct input_ctx *);
static void input_enter_apc(struct input_ctx *);
static void input_exit_apc(struct input_ctx *);
static void input_enter_rename(struct input_ctx *);
static void input_exit_rename(struct input_ctx *);
/* Input state handlers. */
static int input_print(struct input_ctx *);
static int input_intermediate(struct input_ctx *);
static int input_parameter(struct input_ctx *);
static int input_input(struct input_ctx *);
static int input_c0_dispatch(struct input_ctx *);
static int input_esc_dispatch(struct input_ctx *);
static int input_csi_dispatch(struct input_ctx *);
static void input_csi_dispatch_rm(struct input_ctx *);
static void input_csi_dispatch_rm_private(struct input_ctx *);
static void input_csi_dispatch_sm(struct input_ctx *);
static void input_csi_dispatch_sm_private(struct input_ctx *);
static void input_csi_dispatch_winops(struct input_ctx *);
static void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
static void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
static void input_csi_dispatch_sgr(struct input_ctx *);
static int input_dcs_dispatch(struct input_ctx *);
static int input_top_bit_set(struct input_ctx *);
static int input_end_bel(struct input_ctx *);
/* Command table comparison function. */
static int input_table_compare(const void *, const void *);
/* Command table entry. */
struct input_table_entry {
int ch;
const char *interm;
int type;
};
/* Escape commands. */
enum input_esc_type {
INPUT_ESC_DECALN,
INPUT_ESC_DECKPAM,
INPUT_ESC_DECKPNM,
INPUT_ESC_DECRC,
INPUT_ESC_DECSC,
INPUT_ESC_HTS,
INPUT_ESC_IND,
INPUT_ESC_NEL,
INPUT_ESC_RI,
INPUT_ESC_RIS,
INPUT_ESC_SCSG0_OFF,
INPUT_ESC_SCSG0_ON,
INPUT_ESC_SCSG1_OFF,
INPUT_ESC_SCSG1_ON,
INPUT_ESC_ST,
};
/* Escape command table. */
static const struct input_table_entry input_esc_table[] = {
{ '0', "(", INPUT_ESC_SCSG0_ON },
{ '0', ")", INPUT_ESC_SCSG1_ON },
{ '7', "", INPUT_ESC_DECSC },
{ '8', "", INPUT_ESC_DECRC },
{ '8', "#", INPUT_ESC_DECALN },
{ '=', "", INPUT_ESC_DECKPAM },
{ '>', "", INPUT_ESC_DECKPNM },
{ 'B', "(", INPUT_ESC_SCSG0_OFF },
{ 'B', ")", INPUT_ESC_SCSG1_OFF },
{ 'D', "", INPUT_ESC_IND },
{ 'E', "", INPUT_ESC_NEL },
{ 'H', "", INPUT_ESC_HTS },
{ 'M', "", INPUT_ESC_RI },
{ '\\', "", INPUT_ESC_ST },
{ 'c', "", INPUT_ESC_RIS },
};
/* Control (CSI) commands. */
enum input_csi_type {
INPUT_CSI_CBT,
INPUT_CSI_CNL,
INPUT_CSI_CPL,
INPUT_CSI_CUB,
INPUT_CSI_CUD,
INPUT_CSI_CUF,
INPUT_CSI_CUP,
INPUT_CSI_CUU,
INPUT_CSI_DA,
INPUT_CSI_DA_TWO,
INPUT_CSI_DCH,
INPUT_CSI_DECSCUSR,
INPUT_CSI_DECSTBM,
INPUT_CSI_DL,
INPUT_CSI_DSR,
INPUT_CSI_ECH,
INPUT_CSI_ED,
INPUT_CSI_EL,
INPUT_CSI_HPA,
INPUT_CSI_ICH,
INPUT_CSI_IL,
INPUT_CSI_RCP,
INPUT_CSI_REP,
INPUT_CSI_RM,
INPUT_CSI_RM_PRIVATE,
INPUT_CSI_SCP,
INPUT_CSI_SGR,
INPUT_CSI_SM,
INPUT_CSI_SM_PRIVATE,
INPUT_CSI_SU,
INPUT_CSI_TBC,
INPUT_CSI_VPA,
INPUT_CSI_WINOPS,
};
/* Control (CSI) command table. */
static const struct input_table_entry input_csi_table[] = {
{ '@', "", INPUT_CSI_ICH },
{ 'A', "", INPUT_CSI_CUU },
{ 'B', "", INPUT_CSI_CUD },
{ 'C', "", INPUT_CSI_CUF },
{ 'D', "", INPUT_CSI_CUB },
{ 'E', "", INPUT_CSI_CNL },
{ 'F', "", INPUT_CSI_CPL },
{ 'G', "", INPUT_CSI_HPA },
{ 'H', "", INPUT_CSI_CUP },
{ 'J', "", INPUT_CSI_ED },
{ 'K', "", INPUT_CSI_EL },
{ 'L', "", INPUT_CSI_IL },
{ 'M', "", INPUT_CSI_DL },
{ 'P', "", INPUT_CSI_DCH },
{ 'S', "", INPUT_CSI_SU },
{ 'X', "", INPUT_CSI_ECH },
{ 'Z', "", INPUT_CSI_CBT },
{ '`', "", INPUT_CSI_HPA },
{ 'b', "", INPUT_CSI_REP },
{ 'c', "", INPUT_CSI_DA },
{ 'c', ">", INPUT_CSI_DA_TWO },
{ 'd', "", INPUT_CSI_VPA },
{ 'f', "", INPUT_CSI_CUP },
{ 'g', "", INPUT_CSI_TBC },
{ 'h', "", INPUT_CSI_SM },
{ 'h', "?", INPUT_CSI_SM_PRIVATE },
{ 'l', "", INPUT_CSI_RM },
{ 'l', "?", INPUT_CSI_RM_PRIVATE },
{ 'm', "", INPUT_CSI_SGR },
{ 'n', "", INPUT_CSI_DSR },
{ 'q', " ", INPUT_CSI_DECSCUSR },
{ 'r', "", INPUT_CSI_DECSTBM },
{ 's', "", INPUT_CSI_SCP },
{ 't', "", INPUT_CSI_WINOPS },
{ 'u', "", INPUT_CSI_RCP },
};
/* Input transition. */
struct input_transition {
int first;
int last;
int (*handler)(struct input_ctx *);
const struct input_state *state;
};
/* Input state. */
struct input_state {
const char *name;
void (*enter)(struct input_ctx *);
void (*exit)(struct input_ctx *);
const struct input_transition *transitions;
};
/* State transitions available from all states. */
#define INPUT_STATE_ANYWHERE \
{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
{ 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
{ 0x1b, 0x1b, NULL, &input_state_esc_enter }
/* Forward declarations of state tables. */
static const struct input_transition input_state_ground_table[];
static const struct input_transition input_state_esc_enter_table[];
static const struct input_transition input_state_esc_intermediate_table[];
static const struct input_transition input_state_csi_enter_table[];
static const struct input_transition input_state_csi_parameter_table[];
static const struct input_transition input_state_csi_intermediate_table[];
static const struct input_transition input_state_csi_ignore_table[];
static const struct input_transition input_state_dcs_enter_table[];
static const struct input_transition input_state_dcs_parameter_table[];
static const struct input_transition input_state_dcs_intermediate_table[];
static const struct input_transition input_state_dcs_handler_table[];
static const struct input_transition input_state_dcs_escape_table[];
static const struct input_transition input_state_dcs_ignore_table[];
static const struct input_transition input_state_osc_string_table[];
static const struct input_transition input_state_apc_string_table[];
static const struct input_transition input_state_rename_string_table[];
static const struct input_transition input_state_consume_st_table[];
/* ground state definition. */
static const struct input_state input_state_ground = {
"ground",
input_ground, NULL,
input_state_ground_table
};
/* esc_enter state definition. */
static const struct input_state input_state_esc_enter = {
"esc_enter",
input_clear, NULL,
input_state_esc_enter_table
};
/* esc_intermediate state definition. */
static const struct input_state input_state_esc_intermediate = {
"esc_intermediate",
NULL, NULL,
input_state_esc_intermediate_table
};
/* csi_enter state definition. */
static const struct input_state input_state_csi_enter = {
"csi_enter",
input_clear, NULL,
input_state_csi_enter_table
};
/* csi_parameter state definition. */
static const struct input_state input_state_csi_parameter = {
"csi_parameter",
NULL, NULL,
input_state_csi_parameter_table
};
/* csi_intermediate state definition. */
static const struct input_state input_state_csi_intermediate = {
"csi_intermediate",
NULL, NULL,
input_state_csi_intermediate_table
};
/* csi_ignore state definition. */
static const struct input_state input_state_csi_ignore = {
"csi_ignore",
NULL, NULL,
input_state_csi_ignore_table
};
/* dcs_enter state definition. */
static const struct input_state input_state_dcs_enter = {
"dcs_enter",
input_enter_dcs, NULL,
input_state_dcs_enter_table
};
/* dcs_parameter state definition. */
static const struct input_state input_state_dcs_parameter = {
"dcs_parameter",
NULL, NULL,
input_state_dcs_parameter_table
};
/* dcs_intermediate state definition. */
static const struct input_state input_state_dcs_intermediate = {
"dcs_intermediate",
NULL, NULL,
input_state_dcs_intermediate_table
};
/* dcs_handler state definition. */
static const struct input_state input_state_dcs_handler = {
"dcs_handler",
NULL, NULL,
input_state_dcs_handler_table
};
/* dcs_escape state definition. */
static const struct input_state input_state_dcs_escape = {
"dcs_escape",
NULL, NULL,
input_state_dcs_escape_table
};
/* dcs_ignore state definition. */
static const struct input_state input_state_dcs_ignore = {
"dcs_ignore",
NULL, NULL,
input_state_dcs_ignore_table
};
/* osc_string state definition. */
static const struct input_state input_state_osc_string = {
"osc_string",
input_enter_osc, input_exit_osc,
input_state_osc_string_table
};
/* apc_string state definition. */
static const struct input_state input_state_apc_string = {
"apc_string",
input_enter_apc, input_exit_apc,
input_state_apc_string_table
};
/* rename_string state definition. */
static const struct input_state input_state_rename_string = {
"rename_string",
input_enter_rename, input_exit_rename,
input_state_rename_string_table
};
/* consume_st state definition. */
static const struct input_state input_state_consume_st = {
"consume_st",
input_enter_rename, NULL, /* rename also waits for ST */
input_state_consume_st_table
};
/* ground state table. */
static const struct input_transition input_state_ground_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, input_c0_dispatch, NULL },
{ 0x19, 0x19, input_c0_dispatch, NULL },
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x7e, input_print, NULL },
{ 0x7f, 0x7f, NULL, NULL },
{ 0x80, 0xff, input_top_bit_set, NULL },
{ -1, -1, NULL, NULL }
};
/* esc_enter state table. */
static const struct input_transition input_state_esc_enter_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, input_c0_dispatch, NULL },
{ 0x19, 0x19, input_c0_dispatch, NULL },
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
{ 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
{ 0x50, 0x50, NULL, &input_state_dcs_enter },
{ 0x51, 0x57, input_esc_dispatch, &input_state_ground },
{ 0x58, 0x58, NULL, &input_state_consume_st },
{ 0x59, 0x59, input_esc_dispatch, &input_state_ground },
{ 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
{ 0x5b, 0x5b, NULL, &input_state_csi_enter },
{ 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
{ 0x5d, 0x5d, NULL, &input_state_osc_string },
{ 0x5e, 0x5e, NULL, &input_state_consume_st },
{ 0x5f, 0x5f, NULL, &input_state_apc_string },
{ 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
{ 0x6b, 0x6b, NULL, &input_state_rename_string },
{ 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* esc_intermediate state table. */
static const struct input_transition input_state_esc_intermediate_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, input_c0_dispatch, NULL },
{ 0x19, 0x19, input_c0_dispatch, NULL },
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x2f, input_intermediate, NULL },
{ 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* csi_enter state table. */
static const struct input_transition input_state_csi_enter_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, input_c0_dispatch, NULL },
{ 0x19, 0x19, input_c0_dispatch, NULL },
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
{ 0x30, 0x39, input_parameter, &input_state_csi_parameter },
{ 0x3a, 0x3a, input_parameter, &input_state_csi_parameter },
{ 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
{ 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* csi_parameter state table. */
static const struct input_transition input_state_csi_parameter_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, input_c0_dispatch, NULL },
{ 0x19, 0x19, input_c0_dispatch, NULL },
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
{ 0x30, 0x39, input_parameter, NULL },
{ 0x3a, 0x3a, input_parameter, NULL },
{ 0x3b, 0x3b, input_parameter, NULL },
{ 0x3c, 0x3f, NULL, &input_state_csi_ignore },
{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* csi_intermediate state table. */
static const struct input_transition input_state_csi_intermediate_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, input_c0_dispatch, NULL },
{ 0x19, 0x19, input_c0_dispatch, NULL },
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x2f, input_intermediate, NULL },
{ 0x30, 0x3f, NULL, &input_state_csi_ignore },
{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* csi_ignore state table. */
static const struct input_transition input_state_csi_ignore_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, input_c0_dispatch, NULL },
{ 0x19, 0x19, input_c0_dispatch, NULL },
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x3f, NULL, NULL },
{ 0x40, 0x7e, NULL, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* dcs_enter state table. */
static const struct input_transition input_state_dcs_enter_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
{ 0x30, 0x39, input_parameter, &input_state_dcs_parameter },
{ 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
{ 0x3b, 0x3b, input_parameter, &input_state_dcs_parameter },
{ 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
{ 0x40, 0x7e, input_input, &input_state_dcs_handler },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* dcs_parameter state table. */
static const struct input_transition input_state_dcs_parameter_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
{ 0x30, 0x39, input_parameter, NULL },
{ 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
{ 0x3b, 0x3b, input_parameter, NULL },
{ 0x3c, 0x3f, NULL, &input_state_dcs_ignore },
{ 0x40, 0x7e, input_input, &input_state_dcs_handler },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* dcs_intermediate state table. */
static const struct input_transition input_state_dcs_intermediate_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0x2f, input_intermediate, NULL },
{ 0x30, 0x3f, NULL, &input_state_dcs_ignore },
{ 0x40, 0x7e, input_input, &input_state_dcs_handler },
{ 0x7f, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* dcs_handler state table. */
static const struct input_transition input_state_dcs_handler_table[] = {
/* No INPUT_STATE_ANYWHERE */
{ 0x00, 0x1a, input_input, NULL },
{ 0x1b, 0x1b, NULL, &input_state_dcs_escape },
{ 0x1c, 0xff, input_input, NULL },
{ -1, -1, NULL, NULL }
};
/* dcs_escape state table. */
static const struct input_transition input_state_dcs_escape_table[] = {
/* No INPUT_STATE_ANYWHERE */
{ 0x00, 0x5b, input_input, &input_state_dcs_handler },
{ 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
{ 0x5d, 0xff, input_input, &input_state_dcs_handler },
{ -1, -1, NULL, NULL }
};
/* dcs_ignore state table. */
static const struct input_transition input_state_dcs_ignore_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* osc_string state table. */
static const struct input_transition input_state_osc_string_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x06, NULL, NULL },
{ 0x07, 0x07, input_end_bel, &input_state_ground },
{ 0x08, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0xff, input_input, NULL },
{ -1, -1, NULL, NULL }
};
/* apc_string state table. */
static const struct input_transition input_state_apc_string_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0xff, input_input, NULL },
{ -1, -1, NULL, NULL }
};
/* rename_string state table. */
static const struct input_transition input_state_rename_string_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0xff, input_input, NULL },
{ -1, -1, NULL, NULL }
};
/* consume_st state table. */
static const struct input_transition input_state_consume_st_table[] = {
INPUT_STATE_ANYWHERE,
{ 0x00, 0x17, NULL, NULL },
{ 0x19, 0x19, NULL, NULL },
{ 0x1c, 0x1f, NULL, NULL },
{ 0x20, 0xff, NULL, NULL },
{ -1, -1, NULL, NULL }
};
/* Input table compare. */
static int
input_table_compare(const void *key, const void *value)
{
const struct input_ctx *ictx = key;
const struct input_table_entry *entry = value;
if (ictx->ch != entry->ch)
return (ictx->ch - entry->ch);
return (strcmp(ictx->interm_buf, entry->interm));
}
/*
* Timer - if this expires then have been waiting for a terminator for too
* long, so reset to ground.
*/
static void
input_timer_callback(__unused int fd, __unused short events, void *arg)
{
struct input_ctx *ictx = arg;
struct window_pane *wp = ictx->wp;
log_debug("%s: %%%u %s expired" , __func__, wp->id, ictx->state->name);
input_reset(wp, 0);
}
/* Start the timer. */
static void
input_start_timer(struct input_ctx *ictx)
{
struct timeval tv = { .tv_usec = 100000 };
event_del(&ictx->timer);
event_add(&ictx->timer, &tv);
}
/* Reset cell state to default. */
static void
input_reset_cell(struct input_ctx *ictx)
{
memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
ictx->cell.set = 0;
ictx->cell.g0set = ictx->cell.g1set = 0;
memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
ictx->old_cx = 0;
ictx->old_cy = 0;
}
/* Save screen state. */
static void
input_save_state(struct input_ctx *ictx)
{
struct screen_write_ctx *sctx = &ictx->ctx;
struct screen *s = sctx->s;
memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
ictx->old_cx = s->cx;
ictx->old_cy = s->cy;
ictx->old_mode = s->mode;
}
static void
input_restore_state(struct input_ctx *ictx)
{
struct screen_write_ctx *sctx = &ictx->ctx;
memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
if (ictx->old_mode & MODE_ORIGIN)
screen_write_mode_set(sctx, MODE_ORIGIN);
else
screen_write_mode_clear(sctx, MODE_ORIGIN);
screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy, 0);
}
/* Initialise input parser. */
void
input_init(struct window_pane *wp)
{
struct input_ctx *ictx;
ictx = wp->ictx = xcalloc(1, sizeof *ictx);
ictx->input_space = INPUT_BUF_START;
ictx->input_buf = xmalloc(INPUT_BUF_START);
ictx->since_ground = evbuffer_new();
if (ictx->since_ground == NULL)
fatalx("out of memory");
evtimer_set(&ictx->timer, input_timer_callback, ictx);
input_reset(wp, 0);
}
/* Destroy input parser. */
void
input_free(struct window_pane *wp)
{
struct input_ctx *ictx = wp->ictx;
u_int i;
for (i = 0; i < ictx->param_list_len; i++) {
if (ictx->param_list[i].type == INPUT_STRING)
free(ictx->param_list[i].str);
}
event_del(&ictx->timer);
free(ictx->input_buf);
evbuffer_free(ictx->since_ground);
free(ictx);
wp->ictx = NULL;
}
/* Reset input state and clear screen. */
void
input_reset(struct window_pane *wp, int clear)
{
struct input_ctx *ictx = wp->ictx;
struct screen_write_ctx *sctx = &ictx->ctx;
input_reset_cell(ictx);
if (clear) {
if (TAILQ_EMPTY(&wp->modes))
screen_write_start(sctx, wp, &wp->base);
else
screen_write_start(sctx, NULL, &wp->base);
screen_write_reset(sctx);
screen_write_stop(sctx);
}
input_clear(ictx);
ictx->last = -1;
ictx->state = &input_state_ground;
ictx->flags = 0;
}
/* Return pending data. */
struct evbuffer *
input_pending(struct window_pane *wp)
{
return (wp->ictx->since_ground);
}
/* Change input state. */
static void
input_set_state(struct window_pane *wp, const struct input_transition *itr)
{
struct input_ctx *ictx = wp->ictx;
if (ictx->state->exit != NULL)
ictx->state->exit(ictx);
ictx->state = itr->state;
if (ictx->state->enter != NULL)
ictx->state->enter(ictx);
}
/* Parse input. */
void
input_parse(struct window_pane *wp)
{
struct evbuffer *evb = wp->event->input;
input_parse_buffer(wp, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb));
evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
}
/* Parse given input. */
void
input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
{
struct input_ctx *ictx = wp->ictx;
struct screen_write_ctx *sctx = &ictx->ctx;
const struct input_transition *itr;
size_t off = 0;
if (len == 0)
return;
window_update_activity(wp->window);
wp->flags |= PANE_CHANGED;
notify_input(wp, buf, len);
/*
* Open the screen. Use NULL wp if there is a mode set as don't want to
* update the tty.
*/
if (TAILQ_EMPTY(&wp->modes))
screen_write_start(sctx, wp, &wp->base);
else
screen_write_start(sctx, NULL, &wp->base);
ictx->wp = wp;
log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
ictx->state->name, len, (int)len, buf);
/* Parse the input. */
while (off < len) {
ictx->ch = buf[off++];
/* Find the transition. */
itr = ictx->state->transitions;
while (itr->first != -1 && itr->last != -1) {
if (ictx->ch >= itr->first && ictx->ch <= itr->last)
break;
itr++;
}
if (itr->first == -1 || itr->last == -1) {
/* No transition? Eh? */
fatalx("no transition from state");
}
/*
* Any state except print stops the current collection. This is
* an optimization to avoid checking if the attributes have
* changed for every character. It will stop unnecessarily for
* sequences that don't make a terminal change, but they should
* be the minority.
*/
if (itr->handler != input_print)
screen_write_collect_end(sctx);
/*
* Execute the handler, if any. Don't switch state if it
* returns non-zero.
*/
if (itr->handler != NULL && itr->handler(ictx) != 0)
continue;
/* And switch state, if necessary. */
if (itr->state != NULL)
input_set_state(wp, itr);
/* If not in ground state, save input. */
if (ictx->state != &input_state_ground)
evbuffer_add(ictx->since_ground, &ictx->ch, 1);
}
/* Close the screen. */
screen_write_stop(sctx);
}
/* Split the parameter list (if any). */
static int
input_split(struct input_ctx *ictx)
{
const char *errstr;
char *ptr, *out;
struct input_param *ip;
u_int i;
for (i = 0; i < ictx->param_list_len; i++) {
if (ictx->param_list[i].type == INPUT_STRING)
free(ictx->param_list[i].str);
}
ictx->param_list_len = 0;
if (ictx->param_len == 0)
return (0);
ip = &ictx->param_list[0];
ptr = ictx->param_buf;
while ((out = strsep(&ptr, ";")) != NULL) {
if (*out == '\0')
ip->type = INPUT_MISSING;
else {
if (strchr(out, ':') != NULL) {
ip->type = INPUT_STRING;
ip->str = xstrdup(out);
} else {
ip->type = INPUT_NUMBER;
ip->num = strtonum(out, 0, INT_MAX, &errstr);
if (errstr != NULL)
return (-1);
}
}
ip = &ictx->param_list[++ictx->param_list_len];
if (ictx->param_list_len == nitems(ictx->param_list))
return (-1);
}
for (i = 0; i < ictx->param_list_len; i++) {
ip = &ictx->param_list[i];
if (ip->type == INPUT_MISSING)
log_debug("parameter %u: missing", i);