4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <netinet/in.h>
32 * Based on the description by Paul Williams at:
34 * https://vt100.net/emu/dec_ansi_parser
36 * With the following changes:
40 * - Support for UTF-8.
42 * - OSC (but not APC) may be terminated by \007 as well as ST.
44 * - A state for APC similar to OSC. Some terminals appear to use this to set
47 * - A state for the screen \033k...\033\\ sequence to rename a window. This is
48 * pretty stupid but not supporting it is more trouble than it is worth.
50 * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
51 * be passed to the underlying terminals.
54 /* Input parser cell. */
56 struct grid_cell cell
;
58 int g0set
; /* 1 if ACS */
59 int g1set
; /* 1 if ACS */
62 /* Input parser argument. */
75 /* Input parser context. */
77 struct window_pane
*wp
;
78 struct bufferevent
*event
;
79 struct screen_write_ctx ctx
;
80 struct colour_palette
*palette
;
82 struct input_cell cell
;
84 struct input_cell old_cell
;
95 #define INPUT_BUF_START 32
104 struct input_param param_list
[24];
105 u_int param_list_len
;
107 struct utf8_data utf8data
;
111 struct utf8_data last
;
114 #define INPUT_DISCARD 0x1
115 #define INPUT_LAST 0x2
117 const struct input_state
*state
;
122 * All input received since we were last in the ground state. Sent to
123 * control clients on connection.
125 struct evbuffer
*since_ground
;
128 /* Helper functions. */
129 struct input_transition
;
130 static int input_split(struct input_ctx
*);
131 static int input_get(struct input_ctx
*, u_int
, int, int);
132 static void printflike(2, 3) input_reply(struct input_ctx
*, const char *, ...);
133 static void input_set_state(struct input_ctx
*,
134 const struct input_transition
*);
135 static void input_reset_cell(struct input_ctx
*);
137 static void input_osc_4(struct input_ctx
*, const char *);
138 static void input_osc_8(struct input_ctx
*, const char *);
139 static void input_osc_10(struct input_ctx
*, const char *);
140 static void input_osc_11(struct input_ctx
*, const char *);
141 static void input_osc_12(struct input_ctx
*, const char *);
142 static void input_osc_52(struct input_ctx
*, const char *);
143 static void input_osc_104(struct input_ctx
*, const char *);
144 static void input_osc_110(struct input_ctx
*, const char *);
145 static void input_osc_111(struct input_ctx
*, const char *);
146 static void input_osc_112(struct input_ctx
*, const char *);
147 static void input_osc_133(struct input_ctx
*, const char *);
149 /* Transition entry/exit handlers. */
150 static void input_clear(struct input_ctx
*);
151 static void input_ground(struct input_ctx
*);
152 static void input_enter_dcs(struct input_ctx
*);
153 static void input_enter_osc(struct input_ctx
*);
154 static void input_exit_osc(struct input_ctx
*);
155 static void input_enter_apc(struct input_ctx
*);
156 static void input_exit_apc(struct input_ctx
*);
157 static void input_enter_rename(struct input_ctx
*);
158 static void input_exit_rename(struct input_ctx
*);
160 /* Input state handlers. */
161 static int input_print(struct input_ctx
*);
162 static int input_intermediate(struct input_ctx
*);
163 static int input_parameter(struct input_ctx
*);
164 static int input_input(struct input_ctx
*);
165 static int input_c0_dispatch(struct input_ctx
*);
166 static int input_esc_dispatch(struct input_ctx
*);
167 static int input_csi_dispatch(struct input_ctx
*);
168 static void input_csi_dispatch_rm(struct input_ctx
*);
169 static void input_csi_dispatch_rm_private(struct input_ctx
*);
170 static void input_csi_dispatch_sm(struct input_ctx
*);
171 static void input_csi_dispatch_sm_private(struct input_ctx
*);
172 static void input_csi_dispatch_sm_graphics(struct input_ctx
*);
173 static void input_csi_dispatch_winops(struct input_ctx
*);
174 static void input_csi_dispatch_sgr_256(struct input_ctx
*, int, u_int
*);
175 static void input_csi_dispatch_sgr_rgb(struct input_ctx
*, int, u_int
*);
176 static void input_csi_dispatch_sgr(struct input_ctx
*);
177 static int input_dcs_dispatch(struct input_ctx
*);
178 static int input_top_bit_set(struct input_ctx
*);
179 static int input_end_bel(struct input_ctx
*);
181 /* Command table comparison function. */
182 static int input_table_compare(const void *, const void *);
184 /* Command table entry. */
185 struct input_table_entry
{
191 /* Escape commands. */
192 enum input_esc_type
{
210 /* Escape command table. */
211 static const struct input_table_entry input_esc_table
[] = {
212 { '0', "(", INPUT_ESC_SCSG0_ON
},
213 { '0', ")", INPUT_ESC_SCSG1_ON
},
214 { '7', "", INPUT_ESC_DECSC
},
215 { '8', "", INPUT_ESC_DECRC
},
216 { '8', "#", INPUT_ESC_DECALN
},
217 { '=', "", INPUT_ESC_DECKPAM
},
218 { '>', "", INPUT_ESC_DECKPNM
},
219 { 'B', "(", INPUT_ESC_SCSG0_OFF
},
220 { 'B', ")", INPUT_ESC_SCSG1_OFF
},
221 { 'D', "", INPUT_ESC_IND
},
222 { 'E', "", INPUT_ESC_NEL
},
223 { 'H', "", INPUT_ESC_HTS
},
224 { 'M', "", INPUT_ESC_RI
},
225 { '\\', "", INPUT_ESC_ST
},
226 { 'c', "", INPUT_ESC_RIS
},
229 /* Control (CSI) commands. */
230 enum input_csi_type
{
257 INPUT_CSI_RM_PRIVATE
,
262 INPUT_CSI_SM_PRIVATE
,
263 INPUT_CSI_SM_GRAPHICS
,
271 /* Control (CSI) command table. */
272 static const struct input_table_entry input_csi_table
[] = {
273 { '@', "", INPUT_CSI_ICH
},
274 { 'A', "", INPUT_CSI_CUU
},
275 { 'B', "", INPUT_CSI_CUD
},
276 { 'C', "", INPUT_CSI_CUF
},
277 { 'D', "", INPUT_CSI_CUB
},
278 { 'E', "", INPUT_CSI_CNL
},
279 { 'F', "", INPUT_CSI_CPL
},
280 { 'G', "", INPUT_CSI_HPA
},
281 { 'H', "", INPUT_CSI_CUP
},
282 { 'J', "", INPUT_CSI_ED
},
283 { 'K', "", INPUT_CSI_EL
},
284 { 'L', "", INPUT_CSI_IL
},
285 { 'M', "", INPUT_CSI_DL
},
286 { 'P', "", INPUT_CSI_DCH
},
287 { 'S', "", INPUT_CSI_SU
},
288 { 'S', "?", INPUT_CSI_SM_GRAPHICS
},
289 { 'T', "", INPUT_CSI_SD
},
290 { 'X', "", INPUT_CSI_ECH
},
291 { 'Z', "", INPUT_CSI_CBT
},
292 { '`', "", INPUT_CSI_HPA
},
293 { 'b', "", INPUT_CSI_REP
},
294 { 'c', "", INPUT_CSI_DA
},
295 { 'c', ">", INPUT_CSI_DA_TWO
},
296 { 'd', "", INPUT_CSI_VPA
},
297 { 'f', "", INPUT_CSI_CUP
},
298 { 'g', "", INPUT_CSI_TBC
},
299 { 'h', "", INPUT_CSI_SM
},
300 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
301 { 'l', "", INPUT_CSI_RM
},
302 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
303 { 'm', "", INPUT_CSI_SGR
},
304 { 'm', ">", INPUT_CSI_MODSET
},
305 { 'n', "", INPUT_CSI_DSR
},
306 { 'n', ">", INPUT_CSI_MODOFF
},
307 { 'q', " ", INPUT_CSI_DECSCUSR
},
308 { 'q', ">", INPUT_CSI_XDA
},
309 { 'r', "", INPUT_CSI_DECSTBM
},
310 { 's', "", INPUT_CSI_SCP
},
311 { 't', "", INPUT_CSI_WINOPS
},
312 { 'u', "", INPUT_CSI_RCP
}
315 /* Input transition. */
316 struct input_transition
{
320 int (*handler
)(struct input_ctx
*);
321 const struct input_state
*state
;
327 void (*enter
)(struct input_ctx
*);
328 void (*exit
)(struct input_ctx
*);
329 const struct input_transition
*transitions
;
332 /* State transitions available from all states. */
333 #define INPUT_STATE_ANYWHERE \
334 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
335 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
336 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
338 /* Forward declarations of state tables. */
339 static const struct input_transition input_state_ground_table
[];
340 static const struct input_transition input_state_esc_enter_table
[];
341 static const struct input_transition input_state_esc_intermediate_table
[];
342 static const struct input_transition input_state_csi_enter_table
[];
343 static const struct input_transition input_state_csi_parameter_table
[];
344 static const struct input_transition input_state_csi_intermediate_table
[];
345 static const struct input_transition input_state_csi_ignore_table
[];
346 static const struct input_transition input_state_dcs_enter_table
[];
347 static const struct input_transition input_state_dcs_parameter_table
[];
348 static const struct input_transition input_state_dcs_intermediate_table
[];
349 static const struct input_transition input_state_dcs_handler_table
[];
350 static const struct input_transition input_state_dcs_escape_table
[];
351 static const struct input_transition input_state_dcs_ignore_table
[];
352 static const struct input_transition input_state_osc_string_table
[];
353 static const struct input_transition input_state_apc_string_table
[];
354 static const struct input_transition input_state_rename_string_table
[];
355 static const struct input_transition input_state_consume_st_table
[];
357 /* ground state definition. */
358 static const struct input_state input_state_ground
= {
361 input_state_ground_table
364 /* esc_enter state definition. */
365 static const struct input_state input_state_esc_enter
= {
368 input_state_esc_enter_table
371 /* esc_intermediate state definition. */
372 static const struct input_state input_state_esc_intermediate
= {
375 input_state_esc_intermediate_table
378 /* csi_enter state definition. */
379 static const struct input_state input_state_csi_enter
= {
382 input_state_csi_enter_table
385 /* csi_parameter state definition. */
386 static const struct input_state input_state_csi_parameter
= {
389 input_state_csi_parameter_table
392 /* csi_intermediate state definition. */
393 static const struct input_state input_state_csi_intermediate
= {
396 input_state_csi_intermediate_table
399 /* csi_ignore state definition. */
400 static const struct input_state input_state_csi_ignore
= {
403 input_state_csi_ignore_table
406 /* dcs_enter state definition. */
407 static const struct input_state input_state_dcs_enter
= {
409 input_enter_dcs
, NULL
,
410 input_state_dcs_enter_table
413 /* dcs_parameter state definition. */
414 static const struct input_state input_state_dcs_parameter
= {
417 input_state_dcs_parameter_table
420 /* dcs_intermediate state definition. */
421 static const struct input_state input_state_dcs_intermediate
= {
424 input_state_dcs_intermediate_table
427 /* dcs_handler state definition. */
428 static const struct input_state input_state_dcs_handler
= {
431 input_state_dcs_handler_table
434 /* dcs_escape state definition. */
435 static const struct input_state input_state_dcs_escape
= {
438 input_state_dcs_escape_table
441 /* dcs_ignore state definition. */
442 static const struct input_state input_state_dcs_ignore
= {
445 input_state_dcs_ignore_table
448 /* osc_string state definition. */
449 static const struct input_state input_state_osc_string
= {
451 input_enter_osc
, input_exit_osc
,
452 input_state_osc_string_table
455 /* apc_string state definition. */
456 static const struct input_state input_state_apc_string
= {
458 input_enter_apc
, input_exit_apc
,
459 input_state_apc_string_table
462 /* rename_string state definition. */
463 static const struct input_state input_state_rename_string
= {
465 input_enter_rename
, input_exit_rename
,
466 input_state_rename_string_table
469 /* consume_st state definition. */
470 static const struct input_state input_state_consume_st
= {
472 input_enter_rename
, NULL
, /* rename also waits for ST */
473 input_state_consume_st_table
476 /* ground state table. */
477 static const struct input_transition input_state_ground_table
[] = {
478 INPUT_STATE_ANYWHERE
,
480 { 0x00, 0x17, input_c0_dispatch
, NULL
},
481 { 0x19, 0x19, input_c0_dispatch
, NULL
},
482 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
483 { 0x20, 0x7e, input_print
, NULL
},
484 { 0x7f, 0x7f, NULL
, NULL
},
485 { 0x80, 0xff, input_top_bit_set
, NULL
},
487 { -1, -1, NULL
, NULL
}
490 /* esc_enter state table. */
491 static const struct input_transition input_state_esc_enter_table
[] = {
492 INPUT_STATE_ANYWHERE
,
494 { 0x00, 0x17, input_c0_dispatch
, NULL
},
495 { 0x19, 0x19, input_c0_dispatch
, NULL
},
496 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
497 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
498 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
499 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
500 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
501 { 0x58, 0x58, NULL
, &input_state_consume_st
},
502 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
503 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
504 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
505 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
506 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
507 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
508 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
509 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
510 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
511 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
512 { 0x7f, 0xff, NULL
, NULL
},
514 { -1, -1, NULL
, NULL
}
517 /* esc_intermediate state table. */
518 static const struct input_transition input_state_esc_intermediate_table
[] = {
519 INPUT_STATE_ANYWHERE
,
521 { 0x00, 0x17, input_c0_dispatch
, NULL
},
522 { 0x19, 0x19, input_c0_dispatch
, NULL
},
523 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
524 { 0x20, 0x2f, input_intermediate
, NULL
},
525 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
526 { 0x7f, 0xff, NULL
, NULL
},
528 { -1, -1, NULL
, NULL
}
531 /* csi_enter state table. */
532 static const struct input_transition input_state_csi_enter_table
[] = {
533 INPUT_STATE_ANYWHERE
,
535 { 0x00, 0x17, input_c0_dispatch
, NULL
},
536 { 0x19, 0x19, input_c0_dispatch
, NULL
},
537 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
538 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
539 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
540 { 0x3a, 0x3a, input_parameter
, &input_state_csi_parameter
},
541 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
542 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
543 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
544 { 0x7f, 0xff, NULL
, NULL
},
546 { -1, -1, NULL
, NULL
}
549 /* csi_parameter state table. */
550 static const struct input_transition input_state_csi_parameter_table
[] = {
551 INPUT_STATE_ANYWHERE
,
553 { 0x00, 0x17, input_c0_dispatch
, NULL
},
554 { 0x19, 0x19, input_c0_dispatch
, NULL
},
555 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
556 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
557 { 0x30, 0x39, input_parameter
, NULL
},
558 { 0x3a, 0x3a, input_parameter
, NULL
},
559 { 0x3b, 0x3b, input_parameter
, NULL
},
560 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
561 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
562 { 0x7f, 0xff, NULL
, NULL
},
564 { -1, -1, NULL
, NULL
}
567 /* csi_intermediate state table. */
568 static const struct input_transition input_state_csi_intermediate_table
[] = {
569 INPUT_STATE_ANYWHERE
,
571 { 0x00, 0x17, input_c0_dispatch
, NULL
},
572 { 0x19, 0x19, input_c0_dispatch
, NULL
},
573 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
574 { 0x20, 0x2f, input_intermediate
, NULL
},
575 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
576 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
577 { 0x7f, 0xff, NULL
, NULL
},
579 { -1, -1, NULL
, NULL
}
582 /* csi_ignore state table. */
583 static const struct input_transition input_state_csi_ignore_table
[] = {
584 INPUT_STATE_ANYWHERE
,
586 { 0x00, 0x17, input_c0_dispatch
, NULL
},
587 { 0x19, 0x19, input_c0_dispatch
, NULL
},
588 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
589 { 0x20, 0x3f, NULL
, NULL
},
590 { 0x40, 0x7e, NULL
, &input_state_ground
},
591 { 0x7f, 0xff, NULL
, NULL
},
593 { -1, -1, NULL
, NULL
}
596 /* dcs_enter state table. */
597 static const struct input_transition input_state_dcs_enter_table
[] = {
598 INPUT_STATE_ANYWHERE
,
600 { 0x00, 0x17, NULL
, NULL
},
601 { 0x19, 0x19, NULL
, NULL
},
602 { 0x1c, 0x1f, NULL
, NULL
},
603 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
604 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
605 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
606 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
607 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
608 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
609 { 0x7f, 0xff, NULL
, NULL
},
611 { -1, -1, NULL
, NULL
}
614 /* dcs_parameter state table. */
615 static const struct input_transition input_state_dcs_parameter_table
[] = {
616 INPUT_STATE_ANYWHERE
,
618 { 0x00, 0x17, NULL
, NULL
},
619 { 0x19, 0x19, NULL
, NULL
},
620 { 0x1c, 0x1f, NULL
, NULL
},
621 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
622 { 0x30, 0x39, input_parameter
, NULL
},
623 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
624 { 0x3b, 0x3b, input_parameter
, NULL
},
625 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
626 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
627 { 0x7f, 0xff, NULL
, NULL
},
629 { -1, -1, NULL
, NULL
}
632 /* dcs_intermediate state table. */
633 static const struct input_transition input_state_dcs_intermediate_table
[] = {
634 INPUT_STATE_ANYWHERE
,
636 { 0x00, 0x17, NULL
, NULL
},
637 { 0x19, 0x19, NULL
, NULL
},
638 { 0x1c, 0x1f, NULL
, NULL
},
639 { 0x20, 0x2f, input_intermediate
, NULL
},
640 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
641 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
642 { 0x7f, 0xff, NULL
, NULL
},
644 { -1, -1, NULL
, NULL
}
647 /* dcs_handler state table. */
648 static const struct input_transition input_state_dcs_handler_table
[] = {
649 /* No INPUT_STATE_ANYWHERE */
651 { 0x00, 0x1a, input_input
, NULL
},
652 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
653 { 0x1c, 0xff, input_input
, NULL
},
655 { -1, -1, NULL
, NULL
}
658 /* dcs_escape state table. */
659 static const struct input_transition input_state_dcs_escape_table
[] = {
660 /* No INPUT_STATE_ANYWHERE */
662 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
663 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
664 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
666 { -1, -1, NULL
, NULL
}
669 /* dcs_ignore state table. */
670 static const struct input_transition input_state_dcs_ignore_table
[] = {
671 INPUT_STATE_ANYWHERE
,
673 { 0x00, 0x17, NULL
, NULL
},
674 { 0x19, 0x19, NULL
, NULL
},
675 { 0x1c, 0x1f, NULL
, NULL
},
676 { 0x20, 0xff, NULL
, NULL
},
678 { -1, -1, NULL
, NULL
}
681 /* osc_string state table. */
682 static const struct input_transition input_state_osc_string_table
[] = {
683 INPUT_STATE_ANYWHERE
,
685 { 0x00, 0x06, NULL
, NULL
},
686 { 0x07, 0x07, input_end_bel
, &input_state_ground
},
687 { 0x08, 0x17, NULL
, NULL
},
688 { 0x19, 0x19, NULL
, NULL
},
689 { 0x1c, 0x1f, NULL
, NULL
},
690 { 0x20, 0xff, input_input
, NULL
},
692 { -1, -1, NULL
, NULL
}
695 /* apc_string state table. */
696 static const struct input_transition input_state_apc_string_table
[] = {
697 INPUT_STATE_ANYWHERE
,
699 { 0x00, 0x17, NULL
, NULL
},
700 { 0x19, 0x19, NULL
, NULL
},
701 { 0x1c, 0x1f, NULL
, NULL
},
702 { 0x20, 0xff, input_input
, NULL
},
704 { -1, -1, NULL
, NULL
}
707 /* rename_string state table. */
708 static const struct input_transition input_state_rename_string_table
[] = {
709 INPUT_STATE_ANYWHERE
,
711 { 0x00, 0x17, NULL
, NULL
},
712 { 0x19, 0x19, NULL
, NULL
},
713 { 0x1c, 0x1f, NULL
, NULL
},
714 { 0x20, 0xff, input_input
, NULL
},
716 { -1, -1, NULL
, NULL
}
719 /* consume_st state table. */
720 static const struct input_transition input_state_consume_st_table
[] = {
721 INPUT_STATE_ANYWHERE
,
723 { 0x00, 0x17, NULL
, NULL
},
724 { 0x19, 0x19, NULL
, NULL
},
725 { 0x1c, 0x1f, NULL
, NULL
},
726 { 0x20, 0xff, NULL
, NULL
},
728 { -1, -1, NULL
, NULL
}
731 /* Maximum of bytes allowed to read in a single input. */
732 static size_t input_buffer_size
= INPUT_BUF_DEFAULT_SIZE
;
734 /* Input table compare. */
736 input_table_compare(const void *key
, const void *value
)
738 const struct input_ctx
*ictx
= key
;
739 const struct input_table_entry
*entry
= value
;
741 if (ictx
->ch
!= entry
->ch
)
742 return (ictx
->ch
- entry
->ch
);
743 return (strcmp(ictx
->interm_buf
, entry
->interm
));
747 * Timer - if this expires then have been waiting for a terminator for too
748 * long, so reset to ground.
751 input_timer_callback(__unused
int fd
, __unused
short events
, void *arg
)
753 struct input_ctx
*ictx
= arg
;
755 log_debug("%s: %s expired" , __func__
, ictx
->state
->name
);
756 input_reset(ictx
, 0);
759 /* Start the timer. */
761 input_start_timer(struct input_ctx
*ictx
)
763 struct timeval tv
= { .tv_sec
= 5, .tv_usec
= 0 };
765 event_del(&ictx
->timer
);
766 event_add(&ictx
->timer
, &tv
);
769 /* Reset cell state to default. */
771 input_reset_cell(struct input_ctx
*ictx
)
773 memcpy(&ictx
->cell
.cell
, &grid_default_cell
, sizeof ictx
->cell
.cell
);
775 ictx
->cell
.g0set
= ictx
->cell
.g1set
= 0;
777 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
782 /* Save screen state. */
784 input_save_state(struct input_ctx
*ictx
)
786 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
787 struct screen
*s
= sctx
->s
;
789 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
790 ictx
->old_cx
= s
->cx
;
791 ictx
->old_cy
= s
->cy
;
792 ictx
->old_mode
= s
->mode
;
795 /* Restore screen state. */
797 input_restore_state(struct input_ctx
*ictx
)
799 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
801 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
802 if (ictx
->old_mode
& MODE_ORIGIN
)
803 screen_write_mode_set(sctx
, MODE_ORIGIN
);
805 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
806 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
, 0);
809 /* Initialise input parser. */
811 input_init(struct window_pane
*wp
, struct bufferevent
*bev
,
812 struct colour_palette
*palette
)
814 struct input_ctx
*ictx
;
816 ictx
= xcalloc(1, sizeof *ictx
);
819 ictx
->palette
= palette
;
821 ictx
->input_space
= INPUT_BUF_START
;
822 ictx
->input_buf
= xmalloc(INPUT_BUF_START
);
824 ictx
->since_ground
= evbuffer_new();
825 if (ictx
->since_ground
== NULL
)
826 fatalx("out of memory");
828 evtimer_set(&ictx
->timer
, input_timer_callback
, ictx
);
830 input_reset(ictx
, 0);
834 /* Destroy input parser. */
836 input_free(struct input_ctx
*ictx
)
840 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
841 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
842 free(ictx
->param_list
[i
].str
);
845 event_del(&ictx
->timer
);
847 free(ictx
->input_buf
);
848 evbuffer_free(ictx
->since_ground
);
853 /* Reset input state and clear screen. */
855 input_reset(struct input_ctx
*ictx
, int clear
)
857 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
858 struct window_pane
*wp
= ictx
->wp
;
860 input_reset_cell(ictx
);
862 if (clear
&& wp
!= NULL
) {
863 if (TAILQ_EMPTY(&wp
->modes
))
864 screen_write_start_pane(sctx
, wp
, &wp
->base
);
866 screen_write_start(sctx
, &wp
->base
);
867 screen_write_reset(sctx
);
868 screen_write_stop(sctx
);
873 ictx
->state
= &input_state_ground
;
877 /* Return pending data. */
879 input_pending(struct input_ctx
*ictx
)
881 return (ictx
->since_ground
);
884 /* Change input state. */
886 input_set_state(struct input_ctx
*ictx
, const struct input_transition
*itr
)
888 if (ictx
->state
->exit
!= NULL
)
889 ictx
->state
->exit(ictx
);
890 ictx
->state
= itr
->state
;
891 if (ictx
->state
->enter
!= NULL
)
892 ictx
->state
->enter(ictx
);
897 input_parse(struct input_ctx
*ictx
, u_char
*buf
, size_t len
)
899 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
900 const struct input_state
*state
= NULL
;
901 const struct input_transition
*itr
= NULL
;
904 /* Parse the input. */
906 ictx
->ch
= buf
[off
++];
908 /* Find the transition. */
909 if (ictx
->state
!= state
||
911 ictx
->ch
< itr
->first
||
912 ictx
->ch
> itr
->last
) {
913 itr
= ictx
->state
->transitions
;
914 while (itr
->first
!= -1 && itr
->last
!= -1) {
915 if (ictx
->ch
>= itr
->first
&&
916 ictx
->ch
<= itr
->last
)
920 if (itr
->first
== -1 || itr
->last
== -1) {
921 /* No transition? Eh? */
922 fatalx("no transition from state");
928 * Any state except print stops the current collection. This is
929 * an optimization to avoid checking if the attributes have
930 * changed for every character. It will stop unnecessarily for
931 * sequences that don't make a terminal change, but they should
934 if (itr
->handler
!= input_print
)
935 screen_write_collect_end(sctx
);
938 * Execute the handler, if any. Don't switch state if it
941 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
944 /* And switch state, if necessary. */
945 if (itr
->state
!= NULL
)
946 input_set_state(ictx
, itr
);
948 /* If not in ground state, save input. */
949 if (ictx
->state
!= &input_state_ground
)
950 evbuffer_add(ictx
->since_ground
, &ictx
->ch
, 1);
954 /* Parse input from pane. */
956 input_parse_pane(struct window_pane
*wp
)
961 new_data
= window_pane_get_new_data(wp
, &wp
->offset
, &new_size
);
962 input_parse_buffer(wp
, new_data
, new_size
);
963 window_pane_update_used_data(wp
, &wp
->offset
, new_size
);
966 /* Parse given input. */
968 input_parse_buffer(struct window_pane
*wp
, u_char
*buf
, size_t len
)
970 struct input_ctx
*ictx
= wp
->ictx
;
971 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
976 window_update_activity(wp
->window
);
977 wp
->flags
|= PANE_CHANGED
;
979 /* Flag new input while in a mode. */
980 if (!TAILQ_EMPTY(&wp
->modes
))
981 wp
->flags
|= PANE_UNSEENCHANGES
;
983 /* NULL wp if there is a mode set as don't want to update the tty. */
984 if (TAILQ_EMPTY(&wp
->modes
))
985 screen_write_start_pane(sctx
, wp
, &wp
->base
);
987 screen_write_start(sctx
, &wp
->base
);
989 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__
, wp
->id
,
990 ictx
->state
->name
, len
, (int)len
, buf
);
992 input_parse(ictx
, buf
, len
);
993 screen_write_stop(sctx
);
996 /* Parse given input for screen. */
998 input_parse_screen(struct input_ctx
*ictx
, struct screen
*s
,
999 screen_write_init_ctx_cb cb
, void *arg
, u_char
*buf
, size_t len
)
1001 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1006 screen_write_start_callback(sctx
, s
, cb
, arg
);
1007 input_parse(ictx
, buf
, len
);
1008 screen_write_stop(sctx
);
1011 /* Split the parameter list (if any). */
1013 input_split(struct input_ctx
*ictx
)
1017 struct input_param
*ip
;
1020 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1021 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
1022 free(ictx
->param_list
[i
].str
);
1024 ictx
->param_list_len
= 0;
1026 if (ictx
->param_len
== 0)
1028 ip
= &ictx
->param_list
[0];
1030 ptr
= ictx
->param_buf
;
1031 while ((out
= strsep(&ptr
, ";")) != NULL
) {
1033 ip
->type
= INPUT_MISSING
;
1035 if (strchr(out
, ':') != NULL
) {
1036 ip
->type
= INPUT_STRING
;
1037 ip
->str
= xstrdup(out
);
1039 ip
->type
= INPUT_NUMBER
;
1040 ip
->num
= strtonum(out
, 0, INT_MAX
, &errstr
);
1045 ip
= &ictx
->param_list
[++ictx
->param_list_len
];
1046 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
1050 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1051 ip
= &ictx
->param_list
[i
];
1052 if (ip
->type
== INPUT_MISSING
)
1053 log_debug("parameter %u: missing", i
);
1054 else if (ip
->type
== INPUT_STRING
)
1055 log_debug("parameter %u: string %s", i
, ip
->str
);
1056 else if (ip
->type
== INPUT_NUMBER
)
1057 log_debug("parameter %u: number %d", i
, ip
->num
);
1063 /* Get an argument or return default value. */
1065 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
1067 struct input_param
*ip
;
1070 if (validx
>= ictx
->param_list_len
)
1072 ip
= &ictx
->param_list
[validx
];
1073 if (ip
->type
== INPUT_MISSING
)
1075 if (ip
->type
== INPUT_STRING
)
1078 if (retval
< minval
)
1083 /* Reply to terminal query. */
1085 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
1087 struct bufferevent
*bev
= ictx
->event
;
1095 xvasprintf(&reply
, fmt
, ap
);
1098 log_debug("%s: %s", __func__
, reply
);
1099 bufferevent_write(bev
, reply
, strlen(reply
));
1103 /* Clear saved state. */
1105 input_clear(struct input_ctx
*ictx
)
1107 event_del(&ictx
->timer
);
1109 *ictx
->interm_buf
= '\0';
1110 ictx
->interm_len
= 0;
1112 *ictx
->param_buf
= '\0';
1113 ictx
->param_len
= 0;
1115 *ictx
->input_buf
= '\0';
1116 ictx
->input_len
= 0;
1118 ictx
->input_end
= INPUT_END_ST
;
1120 ictx
->flags
&= ~INPUT_DISCARD
;
1123 /* Reset for ground state. */
1125 input_ground(struct input_ctx
*ictx
)
1127 event_del(&ictx
->timer
);
1128 evbuffer_drain(ictx
->since_ground
, EVBUFFER_LENGTH(ictx
->since_ground
));
1130 if (ictx
->input_space
> INPUT_BUF_START
) {
1131 ictx
->input_space
= INPUT_BUF_START
;
1132 ictx
->input_buf
= xrealloc(ictx
->input_buf
, INPUT_BUF_START
);
1136 /* Output this character to the screen. */
1138 input_print(struct input_ctx
*ictx
)
1140 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1143 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1145 set
= ictx
->cell
.set
== 0 ? ictx
->cell
.g0set
: ictx
->cell
.g1set
;
1147 ictx
->cell
.cell
.attr
|= GRID_ATTR_CHARSET
;
1149 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1150 utf8_set(&ictx
->cell
.cell
.data
, ictx
->ch
);
1151 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1153 utf8_copy(&ictx
->last
, &ictx
->cell
.cell
.data
);
1154 ictx
->flags
|= INPUT_LAST
;
1156 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1161 /* Collect intermediate string. */
1163 input_intermediate(struct input_ctx
*ictx
)
1165 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
1166 ictx
->flags
|= INPUT_DISCARD
;
1168 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
1169 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
1175 /* Collect parameter string. */
1177 input_parameter(struct input_ctx
*ictx
)
1179 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
1180 ictx
->flags
|= INPUT_DISCARD
;
1182 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
1183 ictx
->param_buf
[ictx
->param_len
] = '\0';
1189 /* Collect input string. */
1191 input_input(struct input_ctx
*ictx
)
1195 available
= ictx
->input_space
;
1196 while (ictx
->input_len
+ 1 >= available
) {
1198 if (available
> input_buffer_size
) {
1199 ictx
->flags
|= INPUT_DISCARD
;
1202 ictx
->input_buf
= xrealloc(ictx
->input_buf
, available
);
1203 ictx
->input_space
= available
;
1205 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
1206 ictx
->input_buf
[ictx
->input_len
] = '\0';
1211 /* Execute C0 control sequence. */
1213 input_c0_dispatch(struct input_ctx
*ictx
)
1215 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1216 struct window_pane
*wp
= ictx
->wp
;
1217 struct screen
*s
= sctx
->s
;
1218 struct grid_cell gc
, first_gc
;
1219 u_int cx
= s
->cx
, line
= s
->cy
+ s
->grid
->hsize
;
1221 int has_content
= 0;
1223 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1225 log_debug("%s: '%c'", __func__
, ictx
->ch
);
1228 case '\000': /* NUL */
1230 case '\007': /* BEL */
1232 alerts_queue(wp
->window
, WINDOW_BELL
);
1234 case '\010': /* BS */
1235 screen_write_backspace(sctx
);
1237 case '\011': /* HT */
1238 /* Don't tab beyond the end of the line. */
1239 if (s
->cx
>= screen_size_x(s
) - 1)
1242 /* Find the next tab point, or use the last column if none. */
1243 grid_get_cell(s
->grid
, s
->cx
, line
, &first_gc
);
1246 grid_get_cell(s
->grid
, cx
, line
, &gc
);
1247 if (gc
.data
.size
!= 1 ||
1248 *gc
.data
.data
!= ' ' ||
1249 !grid_cells_look_equal(&gc
, &first_gc
))
1253 if (bit_test(s
->tabs
, cx
))
1255 } while (cx
< screen_size_x(s
) - 1);
1258 if (has_content
|| width
> sizeof gc
.data
.data
)
1261 grid_get_cell(s
->grid
, s
->cx
, line
, &gc
);
1262 grid_set_tab(&gc
, width
);
1263 screen_write_collect_add(sctx
, &gc
);
1266 case '\012': /* LF */
1267 case '\013': /* VT */
1268 case '\014': /* FF */
1269 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1270 if (s
->mode
& MODE_CRLF
)
1271 screen_write_carriagereturn(sctx
);
1273 case '\015': /* CR */
1274 screen_write_carriagereturn(sctx
);
1276 case '\016': /* SO */
1279 case '\017': /* SI */
1283 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1287 ictx
->flags
&= ~INPUT_LAST
;
1291 /* Execute escape sequence. */
1293 input_esc_dispatch(struct input_ctx
*ictx
)
1295 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1296 struct screen
*s
= sctx
->s
;
1297 struct input_table_entry
*entry
;
1299 if (ictx
->flags
& INPUT_DISCARD
)
1301 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
1303 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
1304 sizeof input_esc_table
[0], input_table_compare
);
1305 if (entry
== NULL
) {
1306 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1310 switch (entry
->type
) {
1312 colour_palette_clear(ictx
->palette
);
1313 input_reset_cell(ictx
);
1314 screen_write_reset(sctx
);
1315 screen_write_fullredraw(sctx
);
1318 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1321 screen_write_carriagereturn(sctx
);
1322 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1325 if (s
->cx
< screen_size_x(s
))
1326 bit_set(s
->tabs
, s
->cx
);
1329 screen_write_reverseindex(sctx
, ictx
->cell
.cell
.bg
);
1331 case INPUT_ESC_DECKPAM
:
1332 screen_write_mode_set(sctx
, MODE_KKEYPAD
);
1334 case INPUT_ESC_DECKPNM
:
1335 screen_write_mode_clear(sctx
, MODE_KKEYPAD
);
1337 case INPUT_ESC_DECSC
:
1338 input_save_state(ictx
);
1340 case INPUT_ESC_DECRC
:
1341 input_restore_state(ictx
);
1343 case INPUT_ESC_DECALN
:
1344 screen_write_alignmenttest(sctx
);
1346 case INPUT_ESC_SCSG0_ON
:
1347 ictx
->cell
.g0set
= 1;
1349 case INPUT_ESC_SCSG0_OFF
:
1350 ictx
->cell
.g0set
= 0;
1352 case INPUT_ESC_SCSG1_ON
:
1353 ictx
->cell
.g1set
= 1;
1355 case INPUT_ESC_SCSG1_OFF
:
1356 ictx
->cell
.g1set
= 0;
1359 /* ST terminates OSC but the state transition already did it. */
1363 ictx
->flags
&= ~INPUT_LAST
;
1367 /* Execute control sequence. */
1369 input_csi_dispatch(struct input_ctx
*ictx
)
1371 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1372 struct screen
*s
= sctx
->s
;
1373 struct input_table_entry
*entry
;
1374 int i
, n
, m
, ek
, set
;
1375 u_int cx
, bg
= ictx
->cell
.cell
.bg
;
1377 if (ictx
->flags
& INPUT_DISCARD
)
1380 log_debug("%s: '%c' \"%s\" \"%s\"", __func__
, ictx
->ch
,
1381 ictx
->interm_buf
, ictx
->param_buf
);
1383 if (input_split(ictx
) != 0)
1386 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1387 sizeof input_csi_table
[0], input_table_compare
);
1388 if (entry
== NULL
) {
1389 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1393 switch (entry
->type
) {
1395 /* Find the previous tab point, n times. */
1397 if (cx
> screen_size_x(s
) - 1)
1398 cx
= screen_size_x(s
) - 1;
1399 n
= input_get(ictx
, 0, 1, 1);
1402 while (cx
> 0 && n
-- > 0) {
1405 while (cx
> 0 && !bit_test(s
->tabs
, cx
));
1410 n
= input_get(ictx
, 0, 1, 1);
1412 screen_write_cursorleft(sctx
, n
);
1415 n
= input_get(ictx
, 0, 1, 1);
1417 screen_write_cursordown(sctx
, n
);
1420 n
= input_get(ictx
, 0, 1, 1);
1422 screen_write_cursorright(sctx
, n
);
1425 n
= input_get(ictx
, 0, 1, 1);
1426 m
= input_get(ictx
, 1, 1, 1);
1427 if (n
!= -1 && m
!= -1)
1428 screen_write_cursormove(sctx
, m
- 1, n
- 1, 1);
1430 case INPUT_CSI_MODSET
:
1431 n
= input_get(ictx
, 0, 0, 0);
1434 m
= input_get(ictx
, 1, 0, 0);
1437 * Set the extended key reporting mode as per the client
1438 * request, unless "extended-keys" is set to "off".
1440 ek
= options_get_number(global_options
, "extended-keys");
1443 screen_write_mode_clear(sctx
, EXTENDED_KEY_MODES
);
1445 screen_write_mode_set(sctx
, MODE_KEYS_EXTENDED_2
);
1446 else if (m
== 1 || ek
== 2)
1447 screen_write_mode_set(sctx
, MODE_KEYS_EXTENDED
);
1449 case INPUT_CSI_MODOFF
:
1450 n
= input_get(ictx
, 0, 0, 0);
1455 * Clear the extended key reporting mode as per the client
1456 * request, unless "extended-keys always" forces into mode 1.
1458 screen_write_mode_clear(sctx
,
1459 MODE_KEYS_EXTENDED
|MODE_KEYS_EXTENDED_2
);
1460 if (options_get_number(global_options
, "extended-keys") == 2)
1461 screen_write_mode_set(sctx
, MODE_KEYS_EXTENDED
);
1463 case INPUT_CSI_WINOPS
:
1464 input_csi_dispatch_winops(ictx
);
1467 n
= input_get(ictx
, 0, 1, 1);
1469 screen_write_cursorup(sctx
, n
);
1472 n
= input_get(ictx
, 0, 1, 1);
1474 screen_write_carriagereturn(sctx
);
1475 screen_write_cursordown(sctx
, n
);
1479 n
= input_get(ictx
, 0, 1, 1);
1481 screen_write_carriagereturn(sctx
);
1482 screen_write_cursorup(sctx
, n
);
1486 switch (input_get(ictx
, 0, 0, 0)) {
1491 input_reply(ictx
, "\033[?1;2;4c");
1493 input_reply(ictx
, "\033[?1;2c");
1497 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1501 case INPUT_CSI_DA_TWO
:
1502 switch (input_get(ictx
, 0, 0, 0)) {
1506 input_reply(ictx
, "\033[>84;0;0c");
1509 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1514 n
= input_get(ictx
, 0, 1, 1);
1516 screen_write_clearcharacter(sctx
, n
, bg
);
1519 n
= input_get(ictx
, 0, 1, 1);
1521 screen_write_deletecharacter(sctx
, n
, bg
);
1523 case INPUT_CSI_DECSTBM
:
1524 n
= input_get(ictx
, 0, 1, 1);
1525 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1526 if (n
!= -1 && m
!= -1)
1527 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1530 n
= input_get(ictx
, 0, 1, 1);
1532 screen_write_deleteline(sctx
, n
, bg
);
1535 switch (input_get(ictx
, 0, 0, 0)) {
1539 input_reply(ictx
, "\033[0n");
1542 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1545 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1550 switch (input_get(ictx
, 0, 0, 0)) {
1554 screen_write_clearendofscreen(sctx
, bg
);
1557 screen_write_clearstartofscreen(sctx
, bg
);
1560 screen_write_clearscreen(sctx
, bg
);
1563 if (input_get(ictx
, 1, 0, 0) == 0) {
1565 * Linux console extension to clear history
1566 * (for example before locking the screen).
1568 screen_write_clearhistory(sctx
);
1572 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1577 switch (input_get(ictx
, 0, 0, 0)) {
1581 screen_write_clearendofline(sctx
, bg
);
1584 screen_write_clearstartofline(sctx
, bg
);
1587 screen_write_clearline(sctx
, bg
);
1590 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1595 n
= input_get(ictx
, 0, 1, 1);
1597 screen_write_cursormove(sctx
, n
- 1, -1, 1);
1600 n
= input_get(ictx
, 0, 1, 1);
1602 screen_write_insertcharacter(sctx
, n
, bg
);
1605 n
= input_get(ictx
, 0, 1, 1);
1607 screen_write_insertline(sctx
, n
, bg
);
1610 n
= input_get(ictx
, 0, 1, 1);
1614 m
= screen_size_x(s
) - s
->cx
;
1618 if (~ictx
->flags
& INPUT_LAST
)
1621 set
= ictx
->cell
.set
== 0 ? ictx
->cell
.g0set
: ictx
->cell
.g1set
;
1623 ictx
->cell
.cell
.attr
|= GRID_ATTR_CHARSET
;
1625 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1626 utf8_copy(&ictx
->cell
.cell
.data
, &ictx
->last
);
1627 for (i
= 0; i
< n
; i
++)
1628 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1631 input_restore_state(ictx
);
1634 input_csi_dispatch_rm(ictx
);
1636 case INPUT_CSI_RM_PRIVATE
:
1637 input_csi_dispatch_rm_private(ictx
);
1640 input_save_state(ictx
);
1643 input_csi_dispatch_sgr(ictx
);
1646 input_csi_dispatch_sm(ictx
);
1648 case INPUT_CSI_SM_PRIVATE
:
1649 input_csi_dispatch_sm_private(ictx
);
1651 case INPUT_CSI_SM_GRAPHICS
:
1652 input_csi_dispatch_sm_graphics(ictx
);
1655 n
= input_get(ictx
, 0, 1, 1);
1657 screen_write_scrollup(sctx
, n
, bg
);
1660 n
= input_get(ictx
, 0, 1, 1);
1662 screen_write_scrolldown(sctx
, n
, bg
);
1665 switch (input_get(ictx
, 0, 0, 0)) {
1669 if (s
->cx
< screen_size_x(s
))
1670 bit_clear(s
->tabs
, s
->cx
);
1673 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1676 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1681 n
= input_get(ictx
, 0, 1, 1);
1683 screen_write_cursormove(sctx
, -1, n
- 1, 1);
1685 case INPUT_CSI_DECSCUSR
:
1686 n
= input_get(ictx
, 0, 0, 0);
1688 screen_set_cursor_style(n
, &s
->cstyle
, &s
->mode
);
1691 n
= input_get(ictx
, 0, 0, 0);
1693 input_reply(ictx
, "\033P>|tmux %s\033\\", getversion());
1698 ictx
->flags
&= ~INPUT_LAST
;
1702 /* Handle CSI RM. */
1704 input_csi_dispatch_rm(struct input_ctx
*ictx
)
1706 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1709 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1710 switch (input_get(ictx
, i
, 0, -1)) {
1714 screen_write_mode_clear(sctx
, MODE_INSERT
);
1717 screen_write_mode_set(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1720 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1726 /* Handle CSI private RM. */
1728 input_csi_dispatch_rm_private(struct input_ctx
*ictx
)
1730 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1731 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1734 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1735 switch (input_get(ictx
, i
, 0, -1)) {
1738 case 1: /* DECCKM */
1739 screen_write_mode_clear(sctx
, MODE_KCURSOR
);
1741 case 3: /* DECCOLM */
1742 screen_write_cursormove(sctx
, 0, 0, 1);
1743 screen_write_clearscreen(sctx
, gc
->bg
);
1746 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
1747 screen_write_cursormove(sctx
, 0, 0, 1);
1749 case 7: /* DECAWM */
1750 screen_write_mode_clear(sctx
, MODE_WRAP
);
1753 screen_write_mode_clear(sctx
, MODE_CURSOR_BLINKING
);
1754 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1757 screen_write_mode_clear(sctx
, MODE_CURSOR
);
1763 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1766 screen_write_mode_clear(sctx
, MODE_FOCUSON
);
1769 screen_write_mode_clear(sctx
, MODE_MOUSE_UTF8
);
1772 screen_write_mode_clear(sctx
, MODE_MOUSE_SGR
);
1776 screen_write_alternateoff(sctx
, gc
, 0);
1779 screen_write_alternateoff(sctx
, gc
, 1);
1782 screen_write_mode_clear(sctx
, MODE_BRACKETPASTE
);
1785 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1791 /* Handle CSI SM. */
1793 input_csi_dispatch_sm(struct input_ctx
*ictx
)
1795 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1798 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1799 switch (input_get(ictx
, i
, 0, -1)) {
1803 screen_write_mode_set(sctx
, MODE_INSERT
);
1806 screen_write_mode_clear(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1809 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1815 /* Handle CSI private SM. */
1817 input_csi_dispatch_sm_private(struct input_ctx
*ictx
)
1819 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1820 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1823 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1824 switch (input_get(ictx
, i
, 0, -1)) {
1827 case 1: /* DECCKM */
1828 screen_write_mode_set(sctx
, MODE_KCURSOR
);
1830 case 3: /* DECCOLM */
1831 screen_write_cursormove(sctx
, 0, 0, 1);
1832 screen_write_clearscreen(sctx
, ictx
->cell
.cell
.bg
);
1835 screen_write_mode_set(sctx
, MODE_ORIGIN
);
1836 screen_write_cursormove(sctx
, 0, 0, 1);
1838 case 7: /* DECAWM */
1839 screen_write_mode_set(sctx
, MODE_WRAP
);
1842 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING
);
1843 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1846 screen_write_mode_set(sctx
, MODE_CURSOR
);
1849 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1850 screen_write_mode_set(sctx
, MODE_MOUSE_STANDARD
);
1853 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1854 screen_write_mode_set(sctx
, MODE_MOUSE_BUTTON
);
1857 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1858 screen_write_mode_set(sctx
, MODE_MOUSE_ALL
);
1861 screen_write_mode_set(sctx
, MODE_FOCUSON
);
1864 screen_write_mode_set(sctx
, MODE_MOUSE_UTF8
);
1867 screen_write_mode_set(sctx
, MODE_MOUSE_SGR
);
1871 screen_write_alternateon(sctx
, gc
, 0);
1874 screen_write_alternateon(sctx
, gc
, 1);
1877 screen_write_mode_set(sctx
, MODE_BRACKETPASTE
);
1880 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1886 /* Handle CSI graphics SM. */
1888 input_csi_dispatch_sm_graphics(__unused
struct input_ctx
*ictx
)
1893 if (ictx
->param_list_len
> 3)
1895 n
= input_get(ictx
, 0, 0, 0);
1896 m
= input_get(ictx
, 1, 0, 0);
1897 o
= input_get(ictx
, 2, 0, 0);
1899 if (n
== 1 && (m
== 1 || m
== 2 || m
== 4))
1900 input_reply(ictx
, "\033[?%d;0;%uS", n
, SIXEL_COLOUR_REGISTERS
);
1902 input_reply(ictx
, "\033[?%d;3;%dS", n
, o
);
1906 /* Handle CSI window operations. */
1908 input_csi_dispatch_winops(struct input_ctx
*ictx
)
1910 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1911 struct screen
*s
= sctx
->s
;
1912 struct window_pane
*wp
= ictx
->wp
;
1913 struct window
*w
= NULL
;
1914 u_int x
= screen_size_x(s
), y
= screen_size_y(s
);
1921 while ((n
= input_get(ictx
, m
, 0, -1)) != -1) {
1938 if (input_get(ictx
, m
, 0, -1) == -1)
1944 if (input_get(ictx
, m
, 0, -1) == -1)
1950 input_reply(ictx
, "\033[4;%u;%ut", y
* w
->ypixel
,
1956 input_reply(ictx
, "\033[5;%u;%ut", y
* w
->ypixel
,
1962 input_reply(ictx
, "\033[6;%u;%ut", w
->ypixel
,
1966 input_reply(ictx
, "\033[8;%u;%ut", y
, x
);
1969 input_reply(ictx
, "\033[9;%u;%ut", y
, x
);
1973 switch (input_get(ictx
, m
, 0, -1)) {
1978 screen_push_title(sctx
->s
);
1984 switch (input_get(ictx
, m
, 0, -1)) {
1989 screen_pop_title(sctx
->s
);
1992 notify_pane("pane-title-changed", wp
);
1993 server_redraw_window_borders(w
);
1994 server_status_window(w
);
1999 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
2006 /* Helper for 256 colour SGR. */
2008 input_csi_dispatch_sgr_256_do(struct input_ctx
*ictx
, int fgbg
, int c
)
2010 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2012 if (c
== -1 || c
> 255) {
2015 else if (fgbg
== 48)
2019 gc
->fg
= c
| COLOUR_FLAG_256
;
2020 else if (fgbg
== 48)
2021 gc
->bg
= c
| COLOUR_FLAG_256
;
2022 else if (fgbg
== 58)
2023 gc
->us
= c
| COLOUR_FLAG_256
;
2028 /* Handle CSI SGR for 256 colours. */
2030 input_csi_dispatch_sgr_256(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
2034 c
= input_get(ictx
, (*i
) + 1, 0, -1);
2035 if (input_csi_dispatch_sgr_256_do(ictx
, fgbg
, c
))
2039 /* Helper for RGB colour SGR. */
2041 input_csi_dispatch_sgr_rgb_do(struct input_ctx
*ictx
, int fgbg
, int r
, int g
,
2044 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2046 if (r
== -1 || r
> 255)
2048 if (g
== -1 || g
> 255)
2050 if (b
== -1 || b
> 255)
2054 gc
->fg
= colour_join_rgb(r
, g
, b
);
2055 else if (fgbg
== 48)
2056 gc
->bg
= colour_join_rgb(r
, g
, b
);
2057 else if (fgbg
== 58)
2058 gc
->us
= colour_join_rgb(r
, g
, b
);
2062 /* Handle CSI SGR for RGB colours. */
2064 input_csi_dispatch_sgr_rgb(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
2068 r
= input_get(ictx
, (*i
) + 1, 0, -1);
2069 g
= input_get(ictx
, (*i
) + 2, 0, -1);
2070 b
= input_get(ictx
, (*i
) + 3, 0, -1);
2071 if (input_csi_dispatch_sgr_rgb_do(ictx
, fgbg
, r
, g
, b
))
2075 /* Handle CSI SGR with a ISO parameter. */
2077 input_csi_dispatch_sgr_colon(struct input_ctx
*ictx
, u_int i
)
2079 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2080 char *s
= ictx
->param_list
[i
].str
, *copy
, *ptr
, *out
;
2085 for (n
= 0; n
< nitems(p
); n
++)
2089 ptr
= copy
= xstrdup(s
);
2090 while ((out
= strsep(&ptr
, ":")) != NULL
) {
2092 p
[n
++] = strtonum(out
, 0, INT_MAX
, &errstr
);
2093 if (errstr
!= NULL
|| n
== nitems(p
)) {
2099 if (n
== nitems(p
)) {
2104 log_debug("%s: %u = %d", __func__
, n
- 1, p
[n
- 1]);
2115 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2118 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2119 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2122 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2123 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2126 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2127 gc
->attr
|= GRID_ATTR_UNDERSCORE_3
;
2130 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2131 gc
->attr
|= GRID_ATTR_UNDERSCORE_4
;
2134 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2135 gc
->attr
|= GRID_ATTR_UNDERSCORE_5
;
2140 if (n
< 2 || (p
[0] != 38 && p
[0] != 48 && p
[0] != 58))
2152 input_csi_dispatch_sgr_rgb_do(ictx
, p
[0], p
[i
], p
[i
+ 1],
2158 input_csi_dispatch_sgr_256_do(ictx
, p
[0], p
[2]);
2163 /* Handle CSI SGR. */
2165 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
2167 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2171 if (ictx
->param_list_len
== 0) {
2172 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2176 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
2177 if (ictx
->param_list
[i
].type
== INPUT_STRING
) {
2178 input_csi_dispatch_sgr_colon(ictx
, i
);
2181 n
= input_get(ictx
, i
, 0, 0);
2185 if (n
== 38 || n
== 48 || n
== 58) {
2187 switch (input_get(ictx
, i
, 0, -1)) {
2189 input_csi_dispatch_sgr_rgb(ictx
, n
, &i
);
2192 input_csi_dispatch_sgr_256(ictx
, n
, &i
);
2201 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2205 gc
->attr
|= GRID_ATTR_BRIGHT
;
2208 gc
->attr
|= GRID_ATTR_DIM
;
2211 gc
->attr
|= GRID_ATTR_ITALICS
;
2214 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2215 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2219 gc
->attr
|= GRID_ATTR_BLINK
;
2222 gc
->attr
|= GRID_ATTR_REVERSE
;
2225 gc
->attr
|= GRID_ATTR_HIDDEN
;
2228 gc
->attr
|= GRID_ATTR_STRIKETHROUGH
;
2231 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2232 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2235 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
2238 gc
->attr
&= ~GRID_ATTR_ITALICS
;
2241 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2244 gc
->attr
&= ~GRID_ATTR_BLINK
;
2247 gc
->attr
&= ~GRID_ATTR_REVERSE
;
2250 gc
->attr
&= ~GRID_ATTR_HIDDEN
;
2253 gc
->attr
&= ~GRID_ATTR_STRIKETHROUGH
;
2282 gc
->attr
|= GRID_ATTR_OVERLINE
;
2285 gc
->attr
&= ~GRID_ATTR_OVERLINE
;
2314 /* End of input with BEL. */
2316 input_end_bel(struct input_ctx
*ictx
)
2318 log_debug("%s", __func__
);
2320 ictx
->input_end
= INPUT_END_BEL
;
2325 /* DCS string started. */
2327 input_enter_dcs(struct input_ctx
*ictx
)
2329 log_debug("%s", __func__
);
2332 input_start_timer(ictx
);
2333 ictx
->flags
&= ~INPUT_LAST
;
2336 /* DCS terminator (ST) received. */
2338 input_dcs_dispatch(struct input_ctx
*ictx
)
2340 struct window_pane
*wp
= ictx
->wp
;
2341 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2342 u_char
*buf
= ictx
->input_buf
;
2343 size_t len
= ictx
->input_len
;
2344 const char prefix
[] = "tmux;";
2345 const u_int prefixlen
= (sizeof prefix
) - 1;
2346 long long allow_passthrough
= 0;
2349 struct sixel_image
*si
;
2356 if (ictx
->flags
& INPUT_DISCARD
) {
2357 log_debug("%s: %zu bytes (discard)", __func__
, len
);
2363 if (buf
[0] == 'q') {
2364 if (input_split(ictx
) != 0)
2366 p2
= input_get(ictx
, 1, 0, 0);
2369 si
= sixel_parse(buf
, len
, p2
, w
->xpixel
, w
->ypixel
);
2371 screen_write_sixelimage(sctx
, si
, ictx
->cell
.cell
.bg
);
2375 allow_passthrough
= options_get_number(wp
->options
, "allow-passthrough");
2376 if (!allow_passthrough
)
2378 log_debug("%s: \"%s\"", __func__
, buf
);
2380 if (len
>= prefixlen
&& strncmp(buf
, prefix
, prefixlen
) == 0) {
2381 screen_write_rawstring(sctx
, buf
+ prefixlen
, len
- prefixlen
,
2382 allow_passthrough
== 2);
2388 /* OSC string started. */
2390 input_enter_osc(struct input_ctx
*ictx
)
2392 log_debug("%s", __func__
);
2395 input_start_timer(ictx
);
2396 ictx
->flags
&= ~INPUT_LAST
;
2399 /* OSC terminator (ST) received. */
2401 input_exit_osc(struct input_ctx
*ictx
)
2403 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2404 struct window_pane
*wp
= ictx
->wp
;
2405 u_char
*p
= ictx
->input_buf
;
2408 if (ictx
->flags
& INPUT_DISCARD
)
2410 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
2413 log_debug("%s: \"%s\" (end %s)", __func__
, p
,
2414 ictx
->input_end
== INPUT_END_ST
? "ST" : "BEL");
2417 while (*p
>= '0' && *p
<= '9')
2418 option
= option
* 10 + *p
++ - '0';
2419 if (*p
!= ';' && *p
!= '\0')
2428 options_get_number(wp
->options
, "allow-set-title") &&
2429 screen_set_title(sctx
->s
, p
)) {
2430 notify_pane("pane-title-changed", wp
);
2431 server_redraw_window_borders(wp
->window
);
2432 server_status_window(wp
->window
);
2436 input_osc_4(ictx
, p
);
2439 if (utf8_isvalid(p
)) {
2440 screen_set_path(sctx
->s
, p
);
2442 server_redraw_window_borders(wp
->window
);
2443 server_status_window(wp
->window
);
2448 input_osc_8(ictx
, p
);
2451 input_osc_10(ictx
, p
);
2454 input_osc_11(ictx
, p
);
2457 input_osc_12(ictx
, p
);
2460 input_osc_52(ictx
, p
);
2463 input_osc_104(ictx
, p
);
2466 input_osc_110(ictx
, p
);
2469 input_osc_111(ictx
, p
);
2472 input_osc_112(ictx
, p
);
2475 input_osc_133(ictx
, p
);
2478 log_debug("%s: unknown '%u'", __func__
, option
);
2483 /* APC string started. */
2485 input_enter_apc(struct input_ctx
*ictx
)
2487 log_debug("%s", __func__
);
2490 input_start_timer(ictx
);
2491 ictx
->flags
&= ~INPUT_LAST
;
2494 /* APC terminator (ST) received. */
2496 input_exit_apc(struct input_ctx
*ictx
)
2498 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2499 struct window_pane
*wp
= ictx
->wp
;
2501 if (ictx
->flags
& INPUT_DISCARD
)
2503 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2505 if (screen_set_title(sctx
->s
, ictx
->input_buf
) && wp
!= NULL
) {
2506 notify_pane("pane-title-changed", wp
);
2507 server_redraw_window_borders(wp
->window
);
2508 server_status_window(wp
->window
);
2512 /* Rename string started. */
2514 input_enter_rename(struct input_ctx
*ictx
)
2516 log_debug("%s", __func__
);
2519 input_start_timer(ictx
);
2520 ictx
->flags
&= ~INPUT_LAST
;
2523 /* Rename terminator (ST) received. */
2525 input_exit_rename(struct input_ctx
*ictx
)
2527 struct window_pane
*wp
= ictx
->wp
;
2529 struct options_entry
*o
;
2533 if (ictx
->flags
& INPUT_DISCARD
)
2535 if (!options_get_number(ictx
->wp
->options
, "allow-rename"))
2537 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2539 if (!utf8_isvalid(ictx
->input_buf
))
2543 if (ictx
->input_len
== 0) {
2544 o
= options_get_only(w
->options
, "automatic-rename");
2546 options_remove_or_default(o
, -1, NULL
);
2547 if (!options_get_number(w
->options
, "automatic-rename"))
2548 window_set_name(w
, "");
2550 options_set_number(w
->options
, "automatic-rename", 0);
2551 window_set_name(w
, ictx
->input_buf
);
2553 server_redraw_window_borders(w
);
2554 server_status_window(w
);
2557 /* Open UTF-8 character. */
2559 input_top_bit_set(struct input_ctx
*ictx
)
2561 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2562 struct utf8_data
*ud
= &ictx
->utf8data
;
2564 ictx
->flags
&= ~INPUT_LAST
;
2566 if (!ictx
->utf8started
) {
2567 if (utf8_open(ud
, ictx
->ch
) != UTF8_MORE
)
2569 ictx
->utf8started
= 1;
2573 switch (utf8_append(ud
, ictx
->ch
)) {
2577 ictx
->utf8started
= 0;
2582 ictx
->utf8started
= 0;
2584 log_debug("%s %hhu '%*s' (width %hhu)", __func__
, ud
->size
,
2585 (int)ud
->size
, ud
->data
, ud
->width
);
2587 utf8_copy(&ictx
->cell
.cell
.data
, ud
);
2588 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
2590 utf8_copy(&ictx
->last
, &ictx
->cell
.cell
.data
);
2591 ictx
->flags
|= INPUT_LAST
;
2596 /* Reply to a colour request. */
2598 input_osc_colour_reply(struct input_ctx
*ictx
, u_int n
, int c
)
2604 c
= colour_force_rgb(c
);
2607 colour_split_rgb(c
, &r
, &g
, &b
);
2609 if (ictx
->input_end
== INPUT_END_BEL
)
2613 input_reply(ictx
, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2614 n
, r
, r
, g
, g
, b
, b
, end
);
2617 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2619 input_osc_4(struct input_ctx
*ictx
, const char *p
)
2621 char *copy
, *s
, *next
= NULL
;
2623 int c
, bad
= 0, redraw
= 0;
2625 copy
= s
= xstrdup(p
);
2626 while (s
!= NULL
&& *s
!= '\0') {
2627 idx
= strtol(s
, &next
, 10);
2628 if (*next
++ != ';') {
2632 if (idx
< 0 || idx
>= 256) {
2637 s
= strsep(&next
, ";");
2638 if (strcmp(s
, "?") == 0) {
2639 c
= colour_palette_get(ictx
->palette
, idx
);
2641 input_osc_colour_reply(ictx
, 4, c
);
2644 if ((c
= colour_parseX11(s
)) == -1) {
2648 if (colour_palette_set(ictx
->palette
, idx
, c
))
2653 log_debug("bad OSC 4: %s", p
);
2655 screen_write_fullredraw(&ictx
->ctx
);
2659 /* Handle the OSC 8 sequence for embedding hyperlinks. */
2661 input_osc_8(struct input_ctx
*ictx
, const char *p
)
2663 struct hyperlinks
*hl
= ictx
->ctx
.s
->hyperlinks
;
2664 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2665 const char *start
, *end
, *uri
;
2668 for (start
= p
; (end
= strpbrk(start
, ":;")) != NULL
; start
= end
+ 1) {
2669 if (end
- start
>= 4 && strncmp(start
, "id=", 3) == 0) {
2672 id
= xstrndup(start
+ 3, end
- start
- 3);
2675 /* The first ; is the end of parameters and start of the URI. */
2679 if (end
== NULL
|| *end
!= ';')
2687 gc
->link
= hyperlinks_put(hl
, uri
, id
);
2689 log_debug("hyperlink (anonymous) %s = %u", uri
, gc
->link
);
2691 log_debug("hyperlink (id=%s) %s = %u", id
, uri
, gc
->link
);
2696 log_debug("bad OSC 8 %s", p
);
2701 * Get a client with a foreground for the pane. There isn't much to choose
2702 * between them so just use the first.
2705 input_get_fg_client(struct window_pane
*wp
)
2707 struct window
*w
= wp
->window
;
2708 struct client
*loop
;
2710 TAILQ_FOREACH(loop
, &clients
, entry
) {
2711 if (loop
->flags
& CLIENT_UNATTACHEDFLAGS
)
2713 if (loop
->session
== NULL
|| !session_has(loop
->session
, w
))
2715 if (loop
->tty
.fg
== -1)
2717 return (loop
->tty
.fg
);
2722 /* Get a client with a background for the pane. */
2724 input_get_bg_client(struct window_pane
*wp
)
2726 struct window
*w
= wp
->window
;
2727 struct client
*loop
;
2729 TAILQ_FOREACH(loop
, &clients
, entry
) {
2730 if (loop
->flags
& CLIENT_UNATTACHEDFLAGS
)
2732 if (loop
->session
== NULL
|| !session_has(loop
->session
, w
))
2734 if (loop
->tty
.bg
== -1)
2736 return (loop
->tty
.bg
);
2742 * If any control mode client exists that has provided a bg color, return it.
2743 * Otherwise, return -1.
2746 input_get_bg_control_client(struct window_pane
*wp
)
2750 if (wp
->control_bg
== -1)
2753 TAILQ_FOREACH(c
, &clients
, entry
) {
2754 if (c
->flags
& CLIENT_CONTROL
)
2755 return (wp
->control_bg
);
2761 * If any control mode client exists that has provided a fg color, return it.
2762 * Otherwise, return -1.
2765 input_get_fg_control_client(struct window_pane
*wp
)
2769 if (wp
->control_fg
== -1)
2772 TAILQ_FOREACH(c
, &clients
, entry
) {
2773 if (c
->flags
& CLIENT_CONTROL
)
2774 return (wp
->control_fg
);
2779 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2781 input_osc_10(struct input_ctx
*ictx
, const char *p
)
2783 struct window_pane
*wp
= ictx
->wp
;
2784 struct grid_cell defaults
;
2787 if (strcmp(p
, "?") == 0) {
2790 c
= input_get_fg_control_client(wp
);
2792 tty_default_colours(&defaults
, wp
);
2793 if (COLOUR_DEFAULT(defaults
.fg
))
2794 c
= input_get_fg_client(wp
);
2798 input_osc_colour_reply(ictx
, 10, c
);
2802 if ((c
= colour_parseX11(p
)) == -1) {
2803 log_debug("bad OSC 10: %s", p
);
2806 if (ictx
->palette
!= NULL
) {
2807 ictx
->palette
->fg
= c
;
2809 wp
->flags
|= PANE_STYLECHANGED
;
2810 screen_write_fullredraw(&ictx
->ctx
);
2814 /* Handle the OSC 110 sequence for resetting foreground colour. */
2816 input_osc_110(struct input_ctx
*ictx
, const char *p
)
2818 struct window_pane
*wp
= ictx
->wp
;
2822 if (ictx
->palette
!= NULL
) {
2823 ictx
->palette
->fg
= 8;
2825 wp
->flags
|= PANE_STYLECHANGED
;
2826 screen_write_fullredraw(&ictx
->ctx
);
2830 /* Handle the OSC 11 sequence for setting and querying background colour. */
2832 input_osc_11(struct input_ctx
*ictx
, const char *p
)
2834 struct window_pane
*wp
= ictx
->wp
;
2835 struct grid_cell defaults
;
2838 if (strcmp(p
, "?") == 0) {
2841 c
= input_get_bg_control_client(wp
);
2843 tty_default_colours(&defaults
, wp
);
2844 if (COLOUR_DEFAULT(defaults
.bg
))
2845 c
= input_get_bg_client(wp
);
2849 input_osc_colour_reply(ictx
, 11, c
);
2853 if ((c
= colour_parseX11(p
)) == -1) {
2854 log_debug("bad OSC 11: %s", p
);
2857 if (ictx
->palette
!= NULL
) {
2858 ictx
->palette
->bg
= c
;
2860 wp
->flags
|= PANE_STYLECHANGED
;
2861 screen_write_fullredraw(&ictx
->ctx
);
2865 /* Handle the OSC 111 sequence for resetting background colour. */
2867 input_osc_111(struct input_ctx
*ictx
, const char *p
)
2869 struct window_pane
*wp
= ictx
->wp
;
2873 if (ictx
->palette
!= NULL
) {
2874 ictx
->palette
->bg
= 8;
2876 wp
->flags
|= PANE_STYLECHANGED
;
2877 screen_write_fullredraw(&ictx
->ctx
);
2881 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2883 input_osc_12(struct input_ctx
*ictx
, const char *p
)
2885 struct window_pane
*wp
= ictx
->wp
;
2888 if (strcmp(p
, "?") == 0) {
2890 c
= ictx
->ctx
.s
->ccolour
;
2892 c
= ictx
->ctx
.s
->default_ccolour
;
2893 input_osc_colour_reply(ictx
, 12, c
);
2898 if ((c
= colour_parseX11(p
)) == -1) {
2899 log_debug("bad OSC 12: %s", p
);
2902 screen_set_cursor_colour(ictx
->ctx
.s
, c
);
2905 /* Handle the OSC 112 sequence for resetting cursor colour. */
2907 input_osc_112(struct input_ctx
*ictx
, const char *p
)
2909 if (*p
== '\0') /* no arguments allowed */
2910 screen_set_cursor_colour(ictx
->ctx
.s
, -1);
2913 /* Handle the OSC 133 sequence. */
2915 input_osc_133(struct input_ctx
*ictx
, const char *p
)
2917 struct grid
*gd
= ictx
->ctx
.s
->grid
;
2918 u_int line
= ictx
->ctx
.s
->cy
+ gd
->hsize
;
2919 struct grid_line
*gl
;
2921 if (line
> gd
->hsize
+ gd
->sy
- 1)
2923 gl
= grid_get_line(gd
, line
);
2927 gl
->flags
|= GRID_LINE_START_PROMPT
;
2930 gl
->flags
|= GRID_LINE_START_OUTPUT
;
2935 /* Handle the OSC 52 sequence for setting the clipboard. */
2937 input_osc_52(struct input_ctx
*ictx
, const char *p
)
2939 struct window_pane
*wp
= ictx
->wp
;
2941 const char *buf
= NULL
;
2945 struct screen_write_ctx ctx
;
2946 struct paste_buffer
*pb
;
2947 const char* allow
= "cpqs01234567";
2948 char flags
[sizeof "cpqs01234567"] = "";
2953 state
= options_get_number(global_options
, "set-clipboard");
2957 if ((end
= strchr(p
, ';')) == NULL
)
2962 log_debug("%s: %s", __func__
, end
);
2964 for (i
= 0; p
+ i
!= end
; i
++) {
2965 if (strchr(allow
, p
[i
]) != NULL
&& strchr(flags
, p
[i
]) == NULL
)
2968 log_debug("%s: %.*s %s", __func__
, (int)(end
- p
- 1), p
, flags
);
2970 if (strcmp(end
, "?") == 0) {
2971 if ((pb
= paste_get_top(NULL
)) != NULL
)
2972 buf
= paste_buffer_data(pb
, &len
);
2973 if (ictx
->input_end
== INPUT_END_BEL
)
2974 input_reply_clipboard(ictx
->event
, buf
, len
, "\007");
2976 input_reply_clipboard(ictx
->event
, buf
, len
, "\033\\");
2980 len
= (strlen(end
) / 4) * 3;
2985 if ((outlen
= b64_pton(end
, out
, len
)) == -1) {
2990 screen_write_start_pane(&ctx
, wp
, NULL
);
2991 screen_write_setselection(&ctx
, flags
, out
, outlen
);
2992 screen_write_stop(&ctx
);
2993 notify_pane("pane-set-clipboard", wp
);
2995 paste_add(NULL
, out
, outlen
);
2998 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
3000 input_osc_104(struct input_ctx
*ictx
, const char *p
)
3004 int bad
= 0, redraw
= 0;
3007 colour_palette_clear(ictx
->palette
);
3008 screen_write_fullredraw(&ictx
->ctx
);
3012 copy
= s
= xstrdup(p
);
3013 while (*s
!= '\0') {
3014 idx
= strtol(s
, &s
, 10);
3015 if (*s
!= '\0' && *s
!= ';') {
3019 if (idx
< 0 || idx
>= 256) {
3023 if (colour_palette_set(ictx
->palette
, idx
, -1))
3029 log_debug("bad OSC 104: %s", p
);
3031 screen_write_fullredraw(&ictx
->ctx
);
3036 input_reply_clipboard(struct bufferevent
*bev
, const char *buf
, size_t len
,
3042 if (buf
!= NULL
&& len
!= 0) {
3043 if (len
>= ((size_t)INT_MAX
* 3 / 4) - 1)
3045 outlen
= 4 * ((len
+ 2) / 3) + 1;
3046 out
= xmalloc(outlen
);
3047 if ((outlen
= b64_ntop(buf
, len
, out
, outlen
)) == -1) {
3053 bufferevent_write(bev
, "\033]52;;", 6);
3055 bufferevent_write(bev
, out
, outlen
);
3056 bufferevent_write(bev
, end
, strlen(end
));
3060 /* Set input buffer size. */
3062 input_set_buffer_size(size_t buffer_size
)
3064 log_debug("%s: %lu -> %lu", __func__
, input_buffer_size
, buffer_size
);
3065 input_buffer_size
= buffer_size
;