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
96 #define INPUT_BUF_LIMIT 1048576
105 struct input_param param_list
[24];
106 u_int param_list_len
;
108 struct utf8_data utf8data
;
115 #define INPUT_DISCARD 0x1
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_10(struct input_ctx
*, const char *);
139 static void input_osc_11(struct input_ctx
*, const char *);
140 static void input_osc_52(struct input_ctx
*, const char *);
141 static void input_osc_104(struct input_ctx
*, const char *);
142 static void input_osc_110(struct input_ctx
*, const char *);
143 static void input_osc_111(struct input_ctx
*, const char *);
145 /* Transition entry/exit handlers. */
146 static void input_clear(struct input_ctx
*);
147 static void input_ground(struct input_ctx
*);
148 static void input_enter_dcs(struct input_ctx
*);
149 static void input_enter_osc(struct input_ctx
*);
150 static void input_exit_osc(struct input_ctx
*);
151 static void input_enter_apc(struct input_ctx
*);
152 static void input_exit_apc(struct input_ctx
*);
153 static void input_enter_rename(struct input_ctx
*);
154 static void input_exit_rename(struct input_ctx
*);
156 /* Input state handlers. */
157 static int input_print(struct input_ctx
*);
158 static int input_intermediate(struct input_ctx
*);
159 static int input_parameter(struct input_ctx
*);
160 static int input_input(struct input_ctx
*);
161 static int input_c0_dispatch(struct input_ctx
*);
162 static int input_esc_dispatch(struct input_ctx
*);
163 static int input_csi_dispatch(struct input_ctx
*);
164 static void input_csi_dispatch_rm(struct input_ctx
*);
165 static void input_csi_dispatch_rm_private(struct input_ctx
*);
166 static void input_csi_dispatch_sm(struct input_ctx
*);
167 static void input_csi_dispatch_sm_private(struct input_ctx
*);
168 static void input_csi_dispatch_winops(struct input_ctx
*);
169 static void input_csi_dispatch_sgr_256(struct input_ctx
*, int, u_int
*);
170 static void input_csi_dispatch_sgr_rgb(struct input_ctx
*, int, u_int
*);
171 static void input_csi_dispatch_sgr(struct input_ctx
*);
172 static int input_dcs_dispatch(struct input_ctx
*);
173 static int input_top_bit_set(struct input_ctx
*);
174 static int input_end_bel(struct input_ctx
*);
176 /* Command table comparison function. */
177 static int input_table_compare(const void *, const void *);
179 /* Command table entry. */
180 struct input_table_entry
{
186 /* Escape commands. */
187 enum input_esc_type
{
205 /* Escape command table. */
206 static const struct input_table_entry input_esc_table
[] = {
207 { '0', "(", INPUT_ESC_SCSG0_ON
},
208 { '0', ")", INPUT_ESC_SCSG1_ON
},
209 { '7', "", INPUT_ESC_DECSC
},
210 { '8', "", INPUT_ESC_DECRC
},
211 { '8', "#", INPUT_ESC_DECALN
},
212 { '=', "", INPUT_ESC_DECKPAM
},
213 { '>', "", INPUT_ESC_DECKPNM
},
214 { 'B', "(", INPUT_ESC_SCSG0_OFF
},
215 { 'B', ")", INPUT_ESC_SCSG1_OFF
},
216 { 'D', "", INPUT_ESC_IND
},
217 { 'E', "", INPUT_ESC_NEL
},
218 { 'H', "", INPUT_ESC_HTS
},
219 { 'M', "", INPUT_ESC_RI
},
220 { '\\', "", INPUT_ESC_ST
},
221 { 'c', "", INPUT_ESC_RIS
},
224 /* Control (CSI) commands. */
225 enum input_csi_type
{
252 INPUT_CSI_RM_PRIVATE
,
257 INPUT_CSI_SM_PRIVATE
,
265 /* Control (CSI) command table. */
266 static const struct input_table_entry input_csi_table
[] = {
267 { '@', "", INPUT_CSI_ICH
},
268 { 'A', "", INPUT_CSI_CUU
},
269 { 'B', "", INPUT_CSI_CUD
},
270 { 'C', "", INPUT_CSI_CUF
},
271 { 'D', "", INPUT_CSI_CUB
},
272 { 'E', "", INPUT_CSI_CNL
},
273 { 'F', "", INPUT_CSI_CPL
},
274 { 'G', "", INPUT_CSI_HPA
},
275 { 'H', "", INPUT_CSI_CUP
},
276 { 'J', "", INPUT_CSI_ED
},
277 { 'K', "", INPUT_CSI_EL
},
278 { 'L', "", INPUT_CSI_IL
},
279 { 'M', "", INPUT_CSI_DL
},
280 { 'P', "", INPUT_CSI_DCH
},
281 { 'S', "", INPUT_CSI_SU
},
282 { 'T', "", INPUT_CSI_SD
},
283 { 'X', "", INPUT_CSI_ECH
},
284 { 'Z', "", INPUT_CSI_CBT
},
285 { '`', "", INPUT_CSI_HPA
},
286 { 'b', "", INPUT_CSI_REP
},
287 { 'c', "", INPUT_CSI_DA
},
288 { 'c', ">", INPUT_CSI_DA_TWO
},
289 { 'd', "", INPUT_CSI_VPA
},
290 { 'f', "", INPUT_CSI_CUP
},
291 { 'g', "", INPUT_CSI_TBC
},
292 { 'h', "", INPUT_CSI_SM
},
293 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
294 { 'l', "", INPUT_CSI_RM
},
295 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
296 { 'm', "", INPUT_CSI_SGR
},
297 { 'm', ">", INPUT_CSI_MODSET
},
298 { 'n', "", INPUT_CSI_DSR
},
299 { 'n', ">", INPUT_CSI_MODOFF
},
300 { 'q', " ", INPUT_CSI_DECSCUSR
},
301 { 'q', ">", INPUT_CSI_XDA
},
302 { 'r', "", INPUT_CSI_DECSTBM
},
303 { 's', "", INPUT_CSI_SCP
},
304 { 't', "", INPUT_CSI_WINOPS
},
305 { 'u', "", INPUT_CSI_RCP
},
308 /* Input transition. */
309 struct input_transition
{
313 int (*handler
)(struct input_ctx
*);
314 const struct input_state
*state
;
320 void (*enter
)(struct input_ctx
*);
321 void (*exit
)(struct input_ctx
*);
322 const struct input_transition
*transitions
;
325 /* State transitions available from all states. */
326 #define INPUT_STATE_ANYWHERE \
327 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
328 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
329 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
331 /* Forward declarations of state tables. */
332 static const struct input_transition input_state_ground_table
[];
333 static const struct input_transition input_state_esc_enter_table
[];
334 static const struct input_transition input_state_esc_intermediate_table
[];
335 static const struct input_transition input_state_csi_enter_table
[];
336 static const struct input_transition input_state_csi_parameter_table
[];
337 static const struct input_transition input_state_csi_intermediate_table
[];
338 static const struct input_transition input_state_csi_ignore_table
[];
339 static const struct input_transition input_state_dcs_enter_table
[];
340 static const struct input_transition input_state_dcs_parameter_table
[];
341 static const struct input_transition input_state_dcs_intermediate_table
[];
342 static const struct input_transition input_state_dcs_handler_table
[];
343 static const struct input_transition input_state_dcs_escape_table
[];
344 static const struct input_transition input_state_dcs_ignore_table
[];
345 static const struct input_transition input_state_osc_string_table
[];
346 static const struct input_transition input_state_apc_string_table
[];
347 static const struct input_transition input_state_rename_string_table
[];
348 static const struct input_transition input_state_consume_st_table
[];
350 /* ground state definition. */
351 static const struct input_state input_state_ground
= {
354 input_state_ground_table
357 /* esc_enter state definition. */
358 static const struct input_state input_state_esc_enter
= {
361 input_state_esc_enter_table
364 /* esc_intermediate state definition. */
365 static const struct input_state input_state_esc_intermediate
= {
368 input_state_esc_intermediate_table
371 /* csi_enter state definition. */
372 static const struct input_state input_state_csi_enter
= {
375 input_state_csi_enter_table
378 /* csi_parameter state definition. */
379 static const struct input_state input_state_csi_parameter
= {
382 input_state_csi_parameter_table
385 /* csi_intermediate state definition. */
386 static const struct input_state input_state_csi_intermediate
= {
389 input_state_csi_intermediate_table
392 /* csi_ignore state definition. */
393 static const struct input_state input_state_csi_ignore
= {
396 input_state_csi_ignore_table
399 /* dcs_enter state definition. */
400 static const struct input_state input_state_dcs_enter
= {
402 input_enter_dcs
, NULL
,
403 input_state_dcs_enter_table
406 /* dcs_parameter state definition. */
407 static const struct input_state input_state_dcs_parameter
= {
410 input_state_dcs_parameter_table
413 /* dcs_intermediate state definition. */
414 static const struct input_state input_state_dcs_intermediate
= {
417 input_state_dcs_intermediate_table
420 /* dcs_handler state definition. */
421 static const struct input_state input_state_dcs_handler
= {
424 input_state_dcs_handler_table
427 /* dcs_escape state definition. */
428 static const struct input_state input_state_dcs_escape
= {
431 input_state_dcs_escape_table
434 /* dcs_ignore state definition. */
435 static const struct input_state input_state_dcs_ignore
= {
438 input_state_dcs_ignore_table
441 /* osc_string state definition. */
442 static const struct input_state input_state_osc_string
= {
444 input_enter_osc
, input_exit_osc
,
445 input_state_osc_string_table
448 /* apc_string state definition. */
449 static const struct input_state input_state_apc_string
= {
451 input_enter_apc
, input_exit_apc
,
452 input_state_apc_string_table
455 /* rename_string state definition. */
456 static const struct input_state input_state_rename_string
= {
458 input_enter_rename
, input_exit_rename
,
459 input_state_rename_string_table
462 /* consume_st state definition. */
463 static const struct input_state input_state_consume_st
= {
465 input_enter_rename
, NULL
, /* rename also waits for ST */
466 input_state_consume_st_table
469 /* ground state table. */
470 static const struct input_transition input_state_ground_table
[] = {
471 INPUT_STATE_ANYWHERE
,
473 { 0x00, 0x17, input_c0_dispatch
, NULL
},
474 { 0x19, 0x19, input_c0_dispatch
, NULL
},
475 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
476 { 0x20, 0x7e, input_print
, NULL
},
477 { 0x7f, 0x7f, NULL
, NULL
},
478 { 0x80, 0xff, input_top_bit_set
, NULL
},
480 { -1, -1, NULL
, NULL
}
483 /* esc_enter state table. */
484 static const struct input_transition input_state_esc_enter_table
[] = {
485 INPUT_STATE_ANYWHERE
,
487 { 0x00, 0x17, input_c0_dispatch
, NULL
},
488 { 0x19, 0x19, input_c0_dispatch
, NULL
},
489 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
490 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
491 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
492 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
493 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
494 { 0x58, 0x58, NULL
, &input_state_consume_st
},
495 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
496 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
497 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
498 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
499 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
500 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
501 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
502 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
503 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
504 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
505 { 0x7f, 0xff, NULL
, NULL
},
507 { -1, -1, NULL
, NULL
}
510 /* esc_intermediate state table. */
511 static const struct input_transition input_state_esc_intermediate_table
[] = {
512 INPUT_STATE_ANYWHERE
,
514 { 0x00, 0x17, input_c0_dispatch
, NULL
},
515 { 0x19, 0x19, input_c0_dispatch
, NULL
},
516 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
517 { 0x20, 0x2f, input_intermediate
, NULL
},
518 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
519 { 0x7f, 0xff, NULL
, NULL
},
521 { -1, -1, NULL
, NULL
}
524 /* csi_enter state table. */
525 static const struct input_transition input_state_csi_enter_table
[] = {
526 INPUT_STATE_ANYWHERE
,
528 { 0x00, 0x17, input_c0_dispatch
, NULL
},
529 { 0x19, 0x19, input_c0_dispatch
, NULL
},
530 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
531 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
532 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
533 { 0x3a, 0x3a, input_parameter
, &input_state_csi_parameter
},
534 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
535 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
536 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
537 { 0x7f, 0xff, NULL
, NULL
},
539 { -1, -1, NULL
, NULL
}
542 /* csi_parameter state table. */
543 static const struct input_transition input_state_csi_parameter_table
[] = {
544 INPUT_STATE_ANYWHERE
,
546 { 0x00, 0x17, input_c0_dispatch
, NULL
},
547 { 0x19, 0x19, input_c0_dispatch
, NULL
},
548 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
549 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
550 { 0x30, 0x39, input_parameter
, NULL
},
551 { 0x3a, 0x3a, input_parameter
, NULL
},
552 { 0x3b, 0x3b, input_parameter
, NULL
},
553 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
554 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
555 { 0x7f, 0xff, NULL
, NULL
},
557 { -1, -1, NULL
, NULL
}
560 /* csi_intermediate state table. */
561 static const struct input_transition input_state_csi_intermediate_table
[] = {
562 INPUT_STATE_ANYWHERE
,
564 { 0x00, 0x17, input_c0_dispatch
, NULL
},
565 { 0x19, 0x19, input_c0_dispatch
, NULL
},
566 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
567 { 0x20, 0x2f, input_intermediate
, NULL
},
568 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
569 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
570 { 0x7f, 0xff, NULL
, NULL
},
572 { -1, -1, NULL
, NULL
}
575 /* csi_ignore state table. */
576 static const struct input_transition input_state_csi_ignore_table
[] = {
577 INPUT_STATE_ANYWHERE
,
579 { 0x00, 0x17, input_c0_dispatch
, NULL
},
580 { 0x19, 0x19, input_c0_dispatch
, NULL
},
581 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
582 { 0x20, 0x3f, NULL
, NULL
},
583 { 0x40, 0x7e, NULL
, &input_state_ground
},
584 { 0x7f, 0xff, NULL
, NULL
},
586 { -1, -1, NULL
, NULL
}
589 /* dcs_enter state table. */
590 static const struct input_transition input_state_dcs_enter_table
[] = {
591 INPUT_STATE_ANYWHERE
,
593 { 0x00, 0x17, NULL
, NULL
},
594 { 0x19, 0x19, NULL
, NULL
},
595 { 0x1c, 0x1f, NULL
, NULL
},
596 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
597 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
598 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
599 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
600 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
601 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
602 { 0x7f, 0xff, NULL
, NULL
},
604 { -1, -1, NULL
, NULL
}
607 /* dcs_parameter state table. */
608 static const struct input_transition input_state_dcs_parameter_table
[] = {
609 INPUT_STATE_ANYWHERE
,
611 { 0x00, 0x17, NULL
, NULL
},
612 { 0x19, 0x19, NULL
, NULL
},
613 { 0x1c, 0x1f, NULL
, NULL
},
614 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
615 { 0x30, 0x39, input_parameter
, NULL
},
616 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
617 { 0x3b, 0x3b, input_parameter
, NULL
},
618 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
619 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
620 { 0x7f, 0xff, NULL
, NULL
},
622 { -1, -1, NULL
, NULL
}
625 /* dcs_intermediate state table. */
626 static const struct input_transition input_state_dcs_intermediate_table
[] = {
627 INPUT_STATE_ANYWHERE
,
629 { 0x00, 0x17, NULL
, NULL
},
630 { 0x19, 0x19, NULL
, NULL
},
631 { 0x1c, 0x1f, NULL
, NULL
},
632 { 0x20, 0x2f, input_intermediate
, NULL
},
633 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
634 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
635 { 0x7f, 0xff, NULL
, NULL
},
637 { -1, -1, NULL
, NULL
}
640 /* dcs_handler state table. */
641 static const struct input_transition input_state_dcs_handler_table
[] = {
642 /* No INPUT_STATE_ANYWHERE */
644 { 0x00, 0x1a, input_input
, NULL
},
645 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
646 { 0x1c, 0xff, input_input
, NULL
},
648 { -1, -1, NULL
, NULL
}
651 /* dcs_escape state table. */
652 static const struct input_transition input_state_dcs_escape_table
[] = {
653 /* No INPUT_STATE_ANYWHERE */
655 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
656 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
657 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
659 { -1, -1, NULL
, NULL
}
662 /* dcs_ignore state table. */
663 static const struct input_transition input_state_dcs_ignore_table
[] = {
664 INPUT_STATE_ANYWHERE
,
666 { 0x00, 0x17, NULL
, NULL
},
667 { 0x19, 0x19, NULL
, NULL
},
668 { 0x1c, 0x1f, NULL
, NULL
},
669 { 0x20, 0xff, NULL
, NULL
},
671 { -1, -1, NULL
, NULL
}
674 /* osc_string state table. */
675 static const struct input_transition input_state_osc_string_table
[] = {
676 INPUT_STATE_ANYWHERE
,
678 { 0x00, 0x06, NULL
, NULL
},
679 { 0x07, 0x07, input_end_bel
, &input_state_ground
},
680 { 0x08, 0x17, NULL
, NULL
},
681 { 0x19, 0x19, NULL
, NULL
},
682 { 0x1c, 0x1f, NULL
, NULL
},
683 { 0x20, 0xff, input_input
, NULL
},
685 { -1, -1, NULL
, NULL
}
688 /* apc_string state table. */
689 static const struct input_transition input_state_apc_string_table
[] = {
690 INPUT_STATE_ANYWHERE
,
692 { 0x00, 0x17, NULL
, NULL
},
693 { 0x19, 0x19, NULL
, NULL
},
694 { 0x1c, 0x1f, NULL
, NULL
},
695 { 0x20, 0xff, input_input
, NULL
},
697 { -1, -1, NULL
, NULL
}
700 /* rename_string state table. */
701 static const struct input_transition input_state_rename_string_table
[] = {
702 INPUT_STATE_ANYWHERE
,
704 { 0x00, 0x17, NULL
, NULL
},
705 { 0x19, 0x19, NULL
, NULL
},
706 { 0x1c, 0x1f, NULL
, NULL
},
707 { 0x20, 0xff, input_input
, NULL
},
709 { -1, -1, NULL
, NULL
}
712 /* consume_st state table. */
713 static const struct input_transition input_state_consume_st_table
[] = {
714 INPUT_STATE_ANYWHERE
,
716 { 0x00, 0x17, NULL
, NULL
},
717 { 0x19, 0x19, NULL
, NULL
},
718 { 0x1c, 0x1f, NULL
, NULL
},
719 { 0x20, 0xff, NULL
, NULL
},
721 { -1, -1, NULL
, NULL
}
724 /* Input table compare. */
726 input_table_compare(const void *key
, const void *value
)
728 const struct input_ctx
*ictx
= key
;
729 const struct input_table_entry
*entry
= value
;
731 if (ictx
->ch
!= entry
->ch
)
732 return (ictx
->ch
- entry
->ch
);
733 return (strcmp(ictx
->interm_buf
, entry
->interm
));
737 * Timer - if this expires then have been waiting for a terminator for too
738 * long, so reset to ground.
741 input_timer_callback(__unused
int fd
, __unused
short events
, void *arg
)
743 struct input_ctx
*ictx
= arg
;
745 log_debug("%s: %s expired" , __func__
, ictx
->state
->name
);
746 input_reset(ictx
, 0);
749 /* Start the timer. */
751 input_start_timer(struct input_ctx
*ictx
)
753 struct timeval tv
= { .tv_sec
= 5, .tv_usec
= 0 };
755 event_del(&ictx
->timer
);
756 event_add(&ictx
->timer
, &tv
);
759 /* Reset cell state to default. */
761 input_reset_cell(struct input_ctx
*ictx
)
763 memcpy(&ictx
->cell
.cell
, &grid_default_cell
, sizeof ictx
->cell
.cell
);
765 ictx
->cell
.g0set
= ictx
->cell
.g1set
= 0;
767 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
772 /* Save screen state. */
774 input_save_state(struct input_ctx
*ictx
)
776 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
777 struct screen
*s
= sctx
->s
;
779 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
780 ictx
->old_cx
= s
->cx
;
781 ictx
->old_cy
= s
->cy
;
782 ictx
->old_mode
= s
->mode
;
785 /* Restore screen state. */
787 input_restore_state(struct input_ctx
*ictx
)
789 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
791 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
792 if (ictx
->old_mode
& MODE_ORIGIN
)
793 screen_write_mode_set(sctx
, MODE_ORIGIN
);
795 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
796 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
, 0);
799 /* Initialise input parser. */
801 input_init(struct window_pane
*wp
, struct bufferevent
*bev
,
802 struct colour_palette
*palette
)
804 struct input_ctx
*ictx
;
806 ictx
= xcalloc(1, sizeof *ictx
);
809 ictx
->palette
= palette
;
811 ictx
->input_space
= INPUT_BUF_START
;
812 ictx
->input_buf
= xmalloc(INPUT_BUF_START
);
814 ictx
->since_ground
= evbuffer_new();
815 if (ictx
->since_ground
== NULL
)
816 fatalx("out of memory");
818 evtimer_set(&ictx
->timer
, input_timer_callback
, ictx
);
820 input_reset(ictx
, 0);
824 /* Destroy input parser. */
826 input_free(struct input_ctx
*ictx
)
830 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
831 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
832 free(ictx
->param_list
[i
].str
);
835 event_del(&ictx
->timer
);
837 free(ictx
->input_buf
);
838 evbuffer_free(ictx
->since_ground
);
843 /* Reset input state and clear screen. */
845 input_reset(struct input_ctx
*ictx
, int clear
)
847 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
848 struct window_pane
*wp
= ictx
->wp
;
850 input_reset_cell(ictx
);
852 if (clear
&& wp
!= NULL
) {
853 if (TAILQ_EMPTY(&wp
->modes
))
854 screen_write_start_pane(sctx
, wp
, &wp
->base
);
856 screen_write_start(sctx
, &wp
->base
);
857 screen_write_reset(sctx
);
858 screen_write_stop(sctx
);
865 ictx
->state
= &input_state_ground
;
869 /* Return pending data. */
871 input_pending(struct input_ctx
*ictx
)
873 return (ictx
->since_ground
);
876 /* Change input state. */
878 input_set_state(struct input_ctx
*ictx
, const struct input_transition
*itr
)
880 if (ictx
->state
->exit
!= NULL
)
881 ictx
->state
->exit(ictx
);
882 ictx
->state
= itr
->state
;
883 if (ictx
->state
->enter
!= NULL
)
884 ictx
->state
->enter(ictx
);
889 input_parse(struct input_ctx
*ictx
, u_char
*buf
, size_t len
)
891 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
892 const struct input_state
*state
= NULL
;
893 const struct input_transition
*itr
= NULL
;
896 /* Parse the input. */
898 ictx
->ch
= buf
[off
++];
900 /* Find the transition. */
901 if (ictx
->state
!= state
||
903 ictx
->ch
< itr
->first
||
904 ictx
->ch
> itr
->last
) {
905 itr
= ictx
->state
->transitions
;
906 while (itr
->first
!= -1 && itr
->last
!= -1) {
907 if (ictx
->ch
>= itr
->first
&&
908 ictx
->ch
<= itr
->last
)
912 if (itr
->first
== -1 || itr
->last
== -1) {
913 /* No transition? Eh? */
914 fatalx("no transition from state");
920 * Any state except print stops the current collection. This is
921 * an optimization to avoid checking if the attributes have
922 * changed for every character. It will stop unnecessarily for
923 * sequences that don't make a terminal change, but they should
926 if (itr
->handler
!= input_print
)
927 screen_write_collect_end(sctx
);
930 * Execute the handler, if any. Don't switch state if it
933 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
936 /* And switch state, if necessary. */
937 if (itr
->state
!= NULL
)
938 input_set_state(ictx
, itr
);
940 /* If not in ground state, save input. */
941 if (ictx
->state
!= &input_state_ground
)
942 evbuffer_add(ictx
->since_ground
, &ictx
->ch
, 1);
946 /* Parse input from pane. */
948 input_parse_pane(struct window_pane
*wp
)
953 new_data
= window_pane_get_new_data(wp
, &wp
->offset
, &new_size
);
954 input_parse_buffer(wp
, new_data
, new_size
);
955 window_pane_update_used_data(wp
, &wp
->offset
, new_size
);
958 /* Parse given input. */
960 input_parse_buffer(struct window_pane
*wp
, u_char
*buf
, size_t len
)
962 struct input_ctx
*ictx
= wp
->ictx
;
963 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
968 window_update_activity(wp
->window
);
969 wp
->flags
|= PANE_CHANGED
;
971 /* NULL wp if there is a mode set as don't want to update the tty. */
972 if (TAILQ_EMPTY(&wp
->modes
))
973 screen_write_start_pane(sctx
, wp
, &wp
->base
);
975 screen_write_start(sctx
, &wp
->base
);
977 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__
, wp
->id
,
978 ictx
->state
->name
, len
, (int)len
, buf
);
980 input_parse(ictx
, buf
, len
);
981 screen_write_stop(sctx
);
984 /* Parse given input for screen. */
986 input_parse_screen(struct input_ctx
*ictx
, struct screen
*s
,
987 screen_write_init_ctx_cb cb
, void *arg
, u_char
*buf
, size_t len
)
989 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
994 screen_write_start_callback(sctx
, s
, cb
, arg
);
995 input_parse(ictx
, buf
, len
);
996 screen_write_stop(sctx
);
999 /* Split the parameter list (if any). */
1001 input_split(struct input_ctx
*ictx
)
1005 struct input_param
*ip
;
1008 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1009 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
1010 free(ictx
->param_list
[i
].str
);
1012 ictx
->param_list_len
= 0;
1014 if (ictx
->param_len
== 0)
1016 ip
= &ictx
->param_list
[0];
1018 ptr
= ictx
->param_buf
;
1019 while ((out
= strsep(&ptr
, ";")) != NULL
) {
1021 ip
->type
= INPUT_MISSING
;
1023 if (strchr(out
, ':') != NULL
) {
1024 ip
->type
= INPUT_STRING
;
1025 ip
->str
= xstrdup(out
);
1027 ip
->type
= INPUT_NUMBER
;
1028 ip
->num
= strtonum(out
, 0, INT_MAX
, &errstr
);
1033 ip
= &ictx
->param_list
[++ictx
->param_list_len
];
1034 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
1038 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1039 ip
= &ictx
->param_list
[i
];
1040 if (ip
->type
== INPUT_MISSING
)
1041 log_debug("parameter %u: missing", i
);
1042 else if (ip
->type
== INPUT_STRING
)
1043 log_debug("parameter %u: string %s", i
, ip
->str
);
1044 else if (ip
->type
== INPUT_NUMBER
)
1045 log_debug("parameter %u: number %d", i
, ip
->num
);
1051 /* Get an argument or return default value. */
1053 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
1055 struct input_param
*ip
;
1058 if (validx
>= ictx
->param_list_len
)
1060 ip
= &ictx
->param_list
[validx
];
1061 if (ip
->type
== INPUT_MISSING
)
1063 if (ip
->type
== INPUT_STRING
)
1066 if (retval
< minval
)
1071 /* Reply to terminal query. */
1073 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
1075 struct bufferevent
*bev
= ictx
->event
;
1080 xvasprintf(&reply
, fmt
, ap
);
1083 bufferevent_write(bev
, reply
, strlen(reply
));
1087 /* Clear saved state. */
1089 input_clear(struct input_ctx
*ictx
)
1091 event_del(&ictx
->timer
);
1093 *ictx
->interm_buf
= '\0';
1094 ictx
->interm_len
= 0;
1096 *ictx
->param_buf
= '\0';
1097 ictx
->param_len
= 0;
1099 *ictx
->input_buf
= '\0';
1100 ictx
->input_len
= 0;
1102 ictx
->input_end
= INPUT_END_ST
;
1104 ictx
->flags
&= ~INPUT_DISCARD
;
1107 /* Reset for ground state. */
1109 input_ground(struct input_ctx
*ictx
)
1111 event_del(&ictx
->timer
);
1112 evbuffer_drain(ictx
->since_ground
, EVBUFFER_LENGTH(ictx
->since_ground
));
1114 if (ictx
->input_space
> INPUT_BUF_START
) {
1115 ictx
->input_space
= INPUT_BUF_START
;
1116 ictx
->input_buf
= xrealloc(ictx
->input_buf
, INPUT_BUF_START
);
1120 /* Output this character to the screen. */
1122 input_print(struct input_ctx
*ictx
)
1124 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1127 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1129 set
= ictx
->cell
.set
== 0 ? ictx
->cell
.g0set
: ictx
->cell
.g1set
;
1131 ictx
->cell
.cell
.attr
|= GRID_ATTR_CHARSET
;
1133 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1135 utf8_set(&ictx
->cell
.cell
.data
, ictx
->ch
);
1136 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1137 ictx
->last
= ictx
->ch
;
1139 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1144 /* Collect intermediate string. */
1146 input_intermediate(struct input_ctx
*ictx
)
1148 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
1149 ictx
->flags
|= INPUT_DISCARD
;
1151 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
1152 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
1158 /* Collect parameter string. */
1160 input_parameter(struct input_ctx
*ictx
)
1162 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
1163 ictx
->flags
|= INPUT_DISCARD
;
1165 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
1166 ictx
->param_buf
[ictx
->param_len
] = '\0';
1172 /* Collect input string. */
1174 input_input(struct input_ctx
*ictx
)
1178 available
= ictx
->input_space
;
1179 while (ictx
->input_len
+ 1 >= available
) {
1181 if (available
> INPUT_BUF_LIMIT
) {
1182 ictx
->flags
|= INPUT_DISCARD
;
1185 ictx
->input_buf
= xrealloc(ictx
->input_buf
, available
);
1186 ictx
->input_space
= available
;
1188 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
1189 ictx
->input_buf
[ictx
->input_len
] = '\0';
1194 /* Execute C0 control sequence. */
1196 input_c0_dispatch(struct input_ctx
*ictx
)
1198 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1199 struct window_pane
*wp
= ictx
->wp
;
1200 struct screen
*s
= sctx
->s
;
1202 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1204 log_debug("%s: '%c'", __func__
, ictx
->ch
);
1207 case '\000': /* NUL */
1209 case '\007': /* BEL */
1211 alerts_queue(wp
->window
, WINDOW_BELL
);
1213 case '\010': /* BS */
1214 screen_write_backspace(sctx
);
1216 case '\011': /* HT */
1217 /* Don't tab beyond the end of the line. */
1218 if (s
->cx
>= screen_size_x(s
) - 1)
1221 /* Find the next tab point, or use the last column if none. */
1224 if (bit_test(s
->tabs
, s
->cx
))
1226 } while (s
->cx
< screen_size_x(s
) - 1);
1228 case '\012': /* LF */
1229 case '\013': /* VT */
1230 case '\014': /* FF */
1231 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1232 if (s
->mode
& MODE_CRLF
)
1233 screen_write_carriagereturn(sctx
);
1235 case '\015': /* CR */
1236 screen_write_carriagereturn(sctx
);
1238 case '\016': /* SO */
1241 case '\017': /* SI */
1245 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1253 /* Execute escape sequence. */
1255 input_esc_dispatch(struct input_ctx
*ictx
)
1257 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1258 struct screen
*s
= sctx
->s
;
1259 struct input_table_entry
*entry
;
1261 if (ictx
->flags
& INPUT_DISCARD
)
1263 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
1265 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
1266 sizeof input_esc_table
[0], input_table_compare
);
1267 if (entry
== NULL
) {
1268 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1272 switch (entry
->type
) {
1274 colour_palette_clear(ictx
->palette
);
1275 input_reset_cell(ictx
);
1276 screen_write_reset(sctx
);
1277 screen_write_fullredraw(sctx
);
1280 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1283 screen_write_carriagereturn(sctx
);
1284 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1287 if (s
->cx
< screen_size_x(s
))
1288 bit_set(s
->tabs
, s
->cx
);
1291 screen_write_reverseindex(sctx
, ictx
->cell
.cell
.bg
);
1293 case INPUT_ESC_DECKPAM
:
1294 screen_write_mode_set(sctx
, MODE_KKEYPAD
);
1296 case INPUT_ESC_DECKPNM
:
1297 screen_write_mode_clear(sctx
, MODE_KKEYPAD
);
1299 case INPUT_ESC_DECSC
:
1300 input_save_state(ictx
);
1302 case INPUT_ESC_DECRC
:
1303 input_restore_state(ictx
);
1305 case INPUT_ESC_DECALN
:
1306 screen_write_alignmenttest(sctx
);
1308 case INPUT_ESC_SCSG0_ON
:
1309 ictx
->cell
.g0set
= 1;
1311 case INPUT_ESC_SCSG0_OFF
:
1312 ictx
->cell
.g0set
= 0;
1314 case INPUT_ESC_SCSG1_ON
:
1315 ictx
->cell
.g1set
= 1;
1317 case INPUT_ESC_SCSG1_OFF
:
1318 ictx
->cell
.g1set
= 0;
1321 /* ST terminates OSC but the state transition already did it. */
1329 /* Execute control sequence. */
1331 input_csi_dispatch(struct input_ctx
*ictx
)
1333 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1334 struct screen
*s
= sctx
->s
;
1335 struct input_table_entry
*entry
;
1337 u_int cx
, bg
= ictx
->cell
.cell
.bg
;
1339 if (ictx
->flags
& INPUT_DISCARD
)
1342 log_debug("%s: '%c' \"%s\" \"%s\"",
1343 __func__
, ictx
->ch
, ictx
->interm_buf
, ictx
->param_buf
);
1345 if (input_split(ictx
) != 0)
1348 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1349 sizeof input_csi_table
[0], input_table_compare
);
1350 if (entry
== NULL
) {
1351 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1355 switch (entry
->type
) {
1357 /* Find the previous tab point, n times. */
1359 if (cx
> screen_size_x(s
) - 1)
1360 cx
= screen_size_x(s
) - 1;
1361 n
= input_get(ictx
, 0, 1, 1);
1364 while (cx
> 0 && n
-- > 0) {
1367 while (cx
> 0 && !bit_test(s
->tabs
, cx
));
1372 n
= input_get(ictx
, 0, 1, 1);
1374 screen_write_cursorleft(sctx
, n
);
1377 n
= input_get(ictx
, 0, 1, 1);
1379 screen_write_cursordown(sctx
, n
);
1382 n
= input_get(ictx
, 0, 1, 1);
1384 screen_write_cursorright(sctx
, n
);
1387 n
= input_get(ictx
, 0, 1, 1);
1388 m
= input_get(ictx
, 1, 1, 1);
1389 if (n
!= -1 && m
!= -1)
1390 screen_write_cursormove(sctx
, m
- 1, n
- 1, 1);
1392 case INPUT_CSI_MODSET
:
1393 n
= input_get(ictx
, 0, 0, 0);
1394 m
= input_get(ictx
, 1, 0, 0);
1395 if (options_get_number(global_options
, "extended-keys") == 2)
1397 if (n
== 0 || (n
== 4 && m
== 0))
1398 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1399 else if (n
== 4 && (m
== 1 || m
== 2))
1400 screen_write_mode_set(sctx
, MODE_KEXTENDED
);
1402 case INPUT_CSI_MODOFF
:
1403 n
= input_get(ictx
, 0, 0, 0);
1405 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1407 case INPUT_CSI_WINOPS
:
1408 input_csi_dispatch_winops(ictx
);
1411 n
= input_get(ictx
, 0, 1, 1);
1413 screen_write_cursorup(sctx
, n
);
1416 n
= input_get(ictx
, 0, 1, 1);
1418 screen_write_carriagereturn(sctx
);
1419 screen_write_cursordown(sctx
, n
);
1423 n
= input_get(ictx
, 0, 1, 1);
1425 screen_write_carriagereturn(sctx
);
1426 screen_write_cursorup(sctx
, n
);
1430 switch (input_get(ictx
, 0, 0, 0)) {
1434 input_reply(ictx
, "\033[?1;2c");
1437 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1441 case INPUT_CSI_DA_TWO
:
1442 switch (input_get(ictx
, 0, 0, 0)) {
1446 input_reply(ictx
, "\033[>84;0;0c");
1449 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1454 n
= input_get(ictx
, 0, 1, 1);
1456 screen_write_clearcharacter(sctx
, n
, bg
);
1459 n
= input_get(ictx
, 0, 1, 1);
1461 screen_write_deletecharacter(sctx
, n
, bg
);
1463 case INPUT_CSI_DECSTBM
:
1464 n
= input_get(ictx
, 0, 1, 1);
1465 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1466 if (n
!= -1 && m
!= -1)
1467 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1470 n
= input_get(ictx
, 0, 1, 1);
1472 screen_write_deleteline(sctx
, n
, bg
);
1475 switch (input_get(ictx
, 0, 0, 0)) {
1479 input_reply(ictx
, "\033[0n");
1482 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1485 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1490 switch (input_get(ictx
, 0, 0, 0)) {
1494 screen_write_clearendofscreen(sctx
, bg
);
1497 screen_write_clearstartofscreen(sctx
, bg
);
1500 screen_write_clearscreen(sctx
, bg
);
1503 if (input_get(ictx
, 1, 0, 0) == 0) {
1505 * Linux console extension to clear history
1506 * (for example before locking the screen).
1508 screen_write_clearhistory(sctx
);
1512 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1517 switch (input_get(ictx
, 0, 0, 0)) {
1521 screen_write_clearendofline(sctx
, bg
);
1524 screen_write_clearstartofline(sctx
, bg
);
1527 screen_write_clearline(sctx
, bg
);
1530 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1535 n
= input_get(ictx
, 0, 1, 1);
1537 screen_write_cursormove(sctx
, n
- 1, -1, 1);
1540 n
= input_get(ictx
, 0, 1, 1);
1542 screen_write_insertcharacter(sctx
, n
, bg
);
1545 n
= input_get(ictx
, 0, 1, 1);
1547 screen_write_insertline(sctx
, n
, bg
);
1550 n
= input_get(ictx
, 0, 1, 1);
1554 m
= screen_size_x(s
) - s
->cx
;
1558 if (ictx
->last
== -1)
1560 ictx
->ch
= ictx
->last
;
1562 for (i
= 0; i
< n
; i
++)
1566 input_restore_state(ictx
);
1569 input_csi_dispatch_rm(ictx
);
1571 case INPUT_CSI_RM_PRIVATE
:
1572 input_csi_dispatch_rm_private(ictx
);
1575 input_save_state(ictx
);
1578 input_csi_dispatch_sgr(ictx
);
1581 input_csi_dispatch_sm(ictx
);
1583 case INPUT_CSI_SM_PRIVATE
:
1584 input_csi_dispatch_sm_private(ictx
);
1587 n
= input_get(ictx
, 0, 1, 1);
1589 screen_write_scrollup(sctx
, n
, bg
);
1592 n
= input_get(ictx
, 0, 1, 1);
1594 screen_write_scrolldown(sctx
, n
, bg
);
1597 switch (input_get(ictx
, 0, 0, 0)) {
1601 if (s
->cx
< screen_size_x(s
))
1602 bit_clear(s
->tabs
, s
->cx
);
1605 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1608 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1613 n
= input_get(ictx
, 0, 1, 1);
1615 screen_write_cursormove(sctx
, -1, n
- 1, 1);
1617 case INPUT_CSI_DECSCUSR
:
1618 n
= input_get(ictx
, 0, 0, 0);
1620 screen_set_cursor_style(s
, n
);
1623 n
= input_get(ictx
, 0, 0, 0);
1625 input_reply(ictx
, "\033P>|tmux %s\033\\", getversion());
1634 /* Handle CSI RM. */
1636 input_csi_dispatch_rm(struct input_ctx
*ictx
)
1638 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1641 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1642 switch (input_get(ictx
, i
, 0, -1)) {
1646 screen_write_mode_clear(sctx
, MODE_INSERT
);
1649 screen_write_mode_set(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1652 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1658 /* Handle CSI private RM. */
1660 input_csi_dispatch_rm_private(struct input_ctx
*ictx
)
1662 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1663 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1666 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1667 switch (input_get(ictx
, i
, 0, -1)) {
1670 case 1: /* DECCKM */
1671 screen_write_mode_clear(sctx
, MODE_KCURSOR
);
1673 case 3: /* DECCOLM */
1674 screen_write_cursormove(sctx
, 0, 0, 1);
1675 screen_write_clearscreen(sctx
, gc
->bg
);
1678 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
1679 screen_write_cursormove(sctx
, 0, 0, 1);
1681 case 7: /* DECAWM */
1682 screen_write_mode_clear(sctx
, MODE_WRAP
);
1685 screen_write_mode_clear(sctx
, MODE_CURSOR_BLINKING
);
1688 screen_write_mode_clear(sctx
, MODE_CURSOR
);
1694 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1697 screen_write_mode_clear(sctx
, MODE_FOCUSON
);
1700 screen_write_mode_clear(sctx
, MODE_MOUSE_UTF8
);
1703 screen_write_mode_clear(sctx
, MODE_MOUSE_SGR
);
1707 screen_write_alternateoff(sctx
, gc
, 0);
1710 screen_write_alternateoff(sctx
, gc
, 1);
1713 screen_write_mode_clear(sctx
, MODE_BRACKETPASTE
);
1716 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1722 /* Handle CSI SM. */
1724 input_csi_dispatch_sm(struct input_ctx
*ictx
)
1726 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1729 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1730 switch (input_get(ictx
, i
, 0, -1)) {
1734 screen_write_mode_set(sctx
, MODE_INSERT
);
1737 screen_write_mode_clear(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1740 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1746 /* Handle CSI private SM. */
1748 input_csi_dispatch_sm_private(struct input_ctx
*ictx
)
1750 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1751 struct window_pane
*wp
= ictx
->wp
;
1752 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1755 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1756 switch (input_get(ictx
, i
, 0, -1)) {
1759 case 1: /* DECCKM */
1760 screen_write_mode_set(sctx
, MODE_KCURSOR
);
1762 case 3: /* DECCOLM */
1763 screen_write_cursormove(sctx
, 0, 0, 1);
1764 screen_write_clearscreen(sctx
, ictx
->cell
.cell
.bg
);
1767 screen_write_mode_set(sctx
, MODE_ORIGIN
);
1768 screen_write_cursormove(sctx
, 0, 0, 1);
1770 case 7: /* DECAWM */
1771 screen_write_mode_set(sctx
, MODE_WRAP
);
1774 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING
);
1777 screen_write_mode_set(sctx
, MODE_CURSOR
);
1780 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1781 screen_write_mode_set(sctx
, MODE_MOUSE_STANDARD
);
1784 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1785 screen_write_mode_set(sctx
, MODE_MOUSE_BUTTON
);
1788 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1789 screen_write_mode_set(sctx
, MODE_MOUSE_ALL
);
1792 if (sctx
->s
->mode
& MODE_FOCUSON
)
1794 screen_write_mode_set(sctx
, MODE_FOCUSON
);
1797 if (wp
->flags
& PANE_FOCUSED
)
1798 bufferevent_write(wp
->event
, "\033[I", 3);
1800 bufferevent_write(wp
->event
, "\033[O", 3);
1803 screen_write_mode_set(sctx
, MODE_MOUSE_UTF8
);
1806 screen_write_mode_set(sctx
, MODE_MOUSE_SGR
);
1810 screen_write_alternateon(sctx
, gc
, 0);
1813 screen_write_alternateon(sctx
, gc
, 1);
1816 screen_write_mode_set(sctx
, MODE_BRACKETPASTE
);
1819 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1825 /* Handle CSI window operations. */
1827 input_csi_dispatch_winops(struct input_ctx
*ictx
)
1829 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1830 struct screen
*s
= sctx
->s
;
1831 struct window_pane
*wp
= ictx
->wp
;
1832 u_int x
= screen_size_x(s
), y
= screen_size_y(s
);
1836 while ((n
= input_get(ictx
, m
, 0, -1)) != -1) {
1855 if (input_get(ictx
, m
, 0, -1) == -1)
1861 if (input_get(ictx
, m
, 0, -1) == -1)
1866 switch (input_get(ictx
, m
, 0, -1)) {
1871 screen_push_title(sctx
->s
);
1877 switch (input_get(ictx
, m
, 0, -1)) {
1882 screen_pop_title(sctx
->s
);
1885 notify_pane("pane-title-changed", wp
);
1886 server_redraw_window_borders(wp
->window
);
1887 server_status_window(wp
->window
);
1892 input_reply(ictx
, "\033[8;%u;%ut", x
, y
);
1895 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1902 /* Helper for 256 colour SGR. */
1904 input_csi_dispatch_sgr_256_do(struct input_ctx
*ictx
, int fgbg
, int c
)
1906 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1908 if (c
== -1 || c
> 255) {
1911 else if (fgbg
== 48)
1915 gc
->fg
= c
| COLOUR_FLAG_256
;
1916 else if (fgbg
== 48)
1917 gc
->bg
= c
| COLOUR_FLAG_256
;
1918 else if (fgbg
== 58)
1919 gc
->us
= c
| COLOUR_FLAG_256
;
1924 /* Handle CSI SGR for 256 colours. */
1926 input_csi_dispatch_sgr_256(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1930 c
= input_get(ictx
, (*i
) + 1, 0, -1);
1931 if (input_csi_dispatch_sgr_256_do(ictx
, fgbg
, c
))
1935 /* Helper for RGB colour SGR. */
1937 input_csi_dispatch_sgr_rgb_do(struct input_ctx
*ictx
, int fgbg
, int r
, int g
,
1940 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1942 if (r
== -1 || r
> 255)
1944 if (g
== -1 || g
> 255)
1946 if (b
== -1 || b
> 255)
1950 gc
->fg
= colour_join_rgb(r
, g
, b
);
1951 else if (fgbg
== 48)
1952 gc
->bg
= colour_join_rgb(r
, g
, b
);
1953 else if (fgbg
== 58)
1954 gc
->us
= colour_join_rgb(r
, g
, b
);
1958 /* Handle CSI SGR for RGB colours. */
1960 input_csi_dispatch_sgr_rgb(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1964 r
= input_get(ictx
, (*i
) + 1, 0, -1);
1965 g
= input_get(ictx
, (*i
) + 2, 0, -1);
1966 b
= input_get(ictx
, (*i
) + 3, 0, -1);
1967 if (input_csi_dispatch_sgr_rgb_do(ictx
, fgbg
, r
, g
, b
))
1971 /* Handle CSI SGR with a ISO parameter. */
1973 input_csi_dispatch_sgr_colon(struct input_ctx
*ictx
, u_int i
)
1975 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1976 char *s
= ictx
->param_list
[i
].str
, *copy
, *ptr
, *out
;
1981 for (n
= 0; n
< nitems(p
); n
++)
1985 ptr
= copy
= xstrdup(s
);
1986 while ((out
= strsep(&ptr
, ":")) != NULL
) {
1988 p
[n
++] = strtonum(out
, 0, INT_MAX
, &errstr
);
1989 if (errstr
!= NULL
|| n
== nitems(p
)) {
1995 if (n
== nitems(p
)) {
2000 log_debug("%s: %u = %d", __func__
, n
- 1, p
[n
- 1]);
2011 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2014 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2015 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2018 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2019 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2022 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2023 gc
->attr
|= GRID_ATTR_UNDERSCORE_3
;
2026 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2027 gc
->attr
|= GRID_ATTR_UNDERSCORE_4
;
2030 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2031 gc
->attr
|= GRID_ATTR_UNDERSCORE_5
;
2036 if (n
< 2 || (p
[0] != 38 && p
[0] != 48 && p
[0] != 58))
2048 input_csi_dispatch_sgr_rgb_do(ictx
, p
[0], p
[i
], p
[i
+ 1],
2054 input_csi_dispatch_sgr_256_do(ictx
, p
[0], p
[2]);
2059 /* Handle CSI SGR. */
2061 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
2063 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2067 if (ictx
->param_list_len
== 0) {
2068 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2072 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
2073 if (ictx
->param_list
[i
].type
== INPUT_STRING
) {
2074 input_csi_dispatch_sgr_colon(ictx
, i
);
2077 n
= input_get(ictx
, i
, 0, 0);
2081 if (n
== 38 || n
== 48 || n
== 58) {
2083 switch (input_get(ictx
, i
, 0, -1)) {
2085 input_csi_dispatch_sgr_rgb(ictx
, n
, &i
);
2088 input_csi_dispatch_sgr_256(ictx
, n
, &i
);
2096 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2099 gc
->attr
|= GRID_ATTR_BRIGHT
;
2102 gc
->attr
|= GRID_ATTR_DIM
;
2105 gc
->attr
|= GRID_ATTR_ITALICS
;
2108 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2109 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2113 gc
->attr
|= GRID_ATTR_BLINK
;
2116 gc
->attr
|= GRID_ATTR_REVERSE
;
2119 gc
->attr
|= GRID_ATTR_HIDDEN
;
2122 gc
->attr
|= GRID_ATTR_STRIKETHROUGH
;
2125 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2126 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2129 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
2132 gc
->attr
&= ~GRID_ATTR_ITALICS
;
2135 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2138 gc
->attr
&= ~GRID_ATTR_BLINK
;
2141 gc
->attr
&= ~GRID_ATTR_REVERSE
;
2144 gc
->attr
&= ~GRID_ATTR_HIDDEN
;
2147 gc
->attr
&= ~GRID_ATTR_STRIKETHROUGH
;
2176 gc
->attr
|= GRID_ATTR_OVERLINE
;
2179 gc
->attr
&= ~GRID_ATTR_OVERLINE
;
2208 /* End of input with BEL. */
2210 input_end_bel(struct input_ctx
*ictx
)
2212 log_debug("%s", __func__
);
2214 ictx
->input_end
= INPUT_END_BEL
;
2219 /* DCS string started. */
2221 input_enter_dcs(struct input_ctx
*ictx
)
2223 log_debug("%s", __func__
);
2226 input_start_timer(ictx
);
2230 /* DCS terminator (ST) received. */
2232 input_dcs_dispatch(struct input_ctx
*ictx
)
2234 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2235 u_char
*buf
= ictx
->input_buf
;
2236 size_t len
= ictx
->input_len
;
2237 const char prefix
[] = "tmux;";
2238 const u_int prefixlen
= (sizeof prefix
) - 1;
2240 if (ictx
->flags
& INPUT_DISCARD
)
2243 log_debug("%s: \"%s\"", __func__
, buf
);
2245 if (len
>= prefixlen
&& strncmp(buf
, prefix
, prefixlen
) == 0)
2246 screen_write_rawstring(sctx
, buf
+ prefixlen
, len
- prefixlen
);
2251 /* OSC string started. */
2253 input_enter_osc(struct input_ctx
*ictx
)
2255 log_debug("%s", __func__
);
2258 input_start_timer(ictx
);
2262 /* OSC terminator (ST) received. */
2264 input_exit_osc(struct input_ctx
*ictx
)
2266 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2267 struct window_pane
*wp
= ictx
->wp
;
2268 u_char
*p
= ictx
->input_buf
;
2271 if (ictx
->flags
& INPUT_DISCARD
)
2273 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
2276 log_debug("%s: \"%s\" (end %s)", __func__
, p
,
2277 ictx
->input_end
== INPUT_END_ST
? "ST" : "BEL");
2280 while (*p
>= '0' && *p
<= '9')
2281 option
= option
* 10 + *p
++ - '0';
2288 if (screen_set_title(sctx
->s
, p
) && wp
!= NULL
) {
2289 notify_pane("pane-title-changed", wp
);
2290 server_redraw_window_borders(wp
->window
);
2291 server_status_window(wp
->window
);
2295 input_osc_4(ictx
, p
);
2298 if (utf8_isvalid(p
)) {
2299 screen_set_path(sctx
->s
, p
);
2301 server_redraw_window_borders(wp
->window
);
2302 server_status_window(wp
->window
);
2307 input_osc_10(ictx
, p
);
2310 input_osc_11(ictx
, p
);
2313 if (utf8_isvalid(p
) && *p
!= '?') /* ? is colour request */
2314 screen_set_cursor_colour(sctx
->s
, p
);
2317 input_osc_52(ictx
, p
);
2320 input_osc_104(ictx
, p
);
2323 input_osc_110(ictx
, p
);
2326 input_osc_111(ictx
, p
);
2329 if (*p
== '\0') /* no arguments allowed */
2330 screen_set_cursor_colour(sctx
->s
, "");
2333 log_debug("%s: unknown '%u'", __func__
, option
);
2338 /* APC string started. */
2340 input_enter_apc(struct input_ctx
*ictx
)
2342 log_debug("%s", __func__
);
2345 input_start_timer(ictx
);
2349 /* APC terminator (ST) received. */
2351 input_exit_apc(struct input_ctx
*ictx
)
2353 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2354 struct window_pane
*wp
= ictx
->wp
;
2356 if (ictx
->flags
& INPUT_DISCARD
)
2358 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2360 if (screen_set_title(sctx
->s
, ictx
->input_buf
) && wp
!= NULL
) {
2361 notify_pane("pane-title-changed", wp
);
2362 server_redraw_window_borders(wp
->window
);
2363 server_status_window(wp
->window
);
2367 /* Rename string started. */
2369 input_enter_rename(struct input_ctx
*ictx
)
2371 log_debug("%s", __func__
);
2374 input_start_timer(ictx
);
2378 /* Rename terminator (ST) received. */
2380 input_exit_rename(struct input_ctx
*ictx
)
2382 struct window_pane
*wp
= ictx
->wp
;
2383 struct options_entry
*o
;
2387 if (ictx
->flags
& INPUT_DISCARD
)
2389 if (!options_get_number(ictx
->wp
->options
, "allow-rename"))
2391 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2393 if (!utf8_isvalid(ictx
->input_buf
))
2396 if (ictx
->input_len
== 0) {
2397 o
= options_get_only(wp
->window
->options
, "automatic-rename");
2399 options_remove_or_default(o
, -1, NULL
);
2402 window_set_name(wp
->window
, ictx
->input_buf
);
2403 options_set_number(wp
->window
->options
, "automatic-rename", 0);
2404 server_redraw_window_borders(wp
->window
);
2405 server_status_window(wp
->window
);
2408 /* Open UTF-8 character. */
2410 input_top_bit_set(struct input_ctx
*ictx
)
2412 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2413 struct utf8_data
*ud
= &ictx
->utf8data
;
2417 if (!ictx
->utf8started
) {
2418 if (utf8_open(ud
, ictx
->ch
) != UTF8_MORE
)
2420 ictx
->utf8started
= 1;
2424 switch (utf8_append(ud
, ictx
->ch
)) {
2428 ictx
->utf8started
= 0;
2433 ictx
->utf8started
= 0;
2435 log_debug("%s %hhu '%*s' (width %hhu)", __func__
, ud
->size
,
2436 (int)ud
->size
, ud
->data
, ud
->width
);
2438 utf8_copy(&ictx
->cell
.cell
.data
, ud
);
2439 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
2444 /* Parse colour from OSC. */
2446 input_osc_parse_colour(const char *p
)
2448 double c
, m
, y
, k
= 0;
2450 size_t len
= strlen(p
);
2454 if ((len
== 12 && sscanf(p
, "rgb:%02x/%02x/%02x", &r
, &g
, &b
) == 3) ||
2455 (len
== 7 && sscanf(p
, "#%02x%02x%02x", &r
, &g
, &b
) == 3) ||
2456 sscanf(p
, "%d,%d,%d", &r
, &g
, &b
) == 3)
2457 colour
= colour_join_rgb(r
, g
, b
);
2458 else if ((len
== 18 &&
2459 sscanf(p
, "rgb:%04x/%04x/%04x", &r
, &g
, &b
) == 3) ||
2460 (len
== 13 && sscanf(p
, "#%04x%04x%04x", &r
, &g
, &b
) == 3))
2461 colour
= colour_join_rgb(r
>> 8, g
>> 8, b
>> 8);
2462 else if ((sscanf(p
, "cmyk:%lf/%lf/%lf/%lf", &c
, &m
, &y
, &k
) == 4 ||
2463 sscanf(p
, "cmy:%lf/%lf/%lf", &c
, &m
, &y
) == 3) &&
2464 c
>= 0 && c
<= 1 && m
>= 0 && m
<= 1 &&
2465 y
>= 0 && y
<= 1 && k
>= 0 && k
<= 1) {
2466 colour
= colour_join_rgb(
2467 (1 - c
) * (1 - k
) * 255,
2468 (1 - m
) * (1 - k
) * 255,
2469 (1 - y
) * (1 - k
) * 255);
2471 while (len
!= 0 && *p
== ' ') {
2475 while (len
!= 0 && p
[len
- 1] == ' ')
2477 copy
= xstrndup(p
, len
);
2478 colour
= colour_byname(copy
);
2481 log_debug("%s: %s = %s", __func__
, p
, colour_tostring(colour
));
2485 /* Reply to a colour request. */
2487 input_osc_colour_reply(struct input_ctx
*ictx
, u_int n
, int c
)
2492 if (c
== 8 || (~c
& COLOUR_FLAG_RGB
))
2494 colour_split_rgb(c
, &r
, &g
, &b
);
2496 if (ictx
->input_end
== INPUT_END_BEL
)
2500 input_reply(ictx
, "\033]%u;rgb:%02hhx/%02hhx/%02hhx%s", n
, r
, g
, b
, end
);
2503 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2505 input_osc_4(struct input_ctx
*ictx
, const char *p
)
2507 char *copy
, *s
, *next
= NULL
;
2509 int c
, bad
= 0, redraw
= 0;
2511 copy
= s
= xstrdup(p
);
2512 while (s
!= NULL
&& *s
!= '\0') {
2513 idx
= strtol(s
, &next
, 10);
2514 if (*next
++ != ';') {
2518 if (idx
< 0 || idx
>= 256) {
2523 s
= strsep(&next
, ";");
2524 if ((c
= input_osc_parse_colour(s
)) == -1) {
2528 if (colour_palette_set(ictx
->palette
, idx
, c
))
2533 log_debug("bad OSC 4: %s", p
);
2535 screen_write_fullredraw(&ictx
->ctx
);
2539 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2541 input_osc_10(struct input_ctx
*ictx
, const char *p
)
2543 struct window_pane
*wp
= ictx
->wp
;
2544 struct grid_cell defaults
;
2547 if (strcmp(p
, "?") == 0) {
2549 tty_default_colours(&defaults
, wp
);
2550 input_osc_colour_reply(ictx
, 10, defaults
.fg
);
2555 if ((c
= input_osc_parse_colour(p
)) == -1) {
2556 log_debug("bad OSC 10: %s", p
);
2559 if (ictx
->palette
!= NULL
) {
2560 ictx
->palette
->fg
= c
;
2562 wp
->flags
|= PANE_STYLECHANGED
;
2563 screen_write_fullredraw(&ictx
->ctx
);
2567 /* Handle the OSC 110 sequence for resetting background colour. */
2569 input_osc_110(struct input_ctx
*ictx
, const char *p
)
2571 struct window_pane
*wp
= ictx
->wp
;
2575 if (ictx
->palette
!= NULL
) {
2576 ictx
->palette
->fg
= 8;
2578 wp
->flags
|= PANE_STYLECHANGED
;
2579 screen_write_fullredraw(&ictx
->ctx
);
2583 /* Handle the OSC 11 sequence for setting and querying background colour. */
2585 input_osc_11(struct input_ctx
*ictx
, const char *p
)
2587 struct window_pane
*wp
= ictx
->wp
;
2588 struct grid_cell defaults
;
2591 if (strcmp(p
, "?") == 0) {
2593 tty_default_colours(&defaults
, wp
);
2594 input_osc_colour_reply(ictx
, 11, defaults
.bg
);
2599 if ((c
= input_osc_parse_colour(p
)) == -1) {
2600 log_debug("bad OSC 11: %s", p
);
2603 if (ictx
->palette
!= NULL
) {
2604 ictx
->palette
->bg
= c
;
2606 wp
->flags
|= PANE_STYLECHANGED
;
2607 screen_write_fullredraw(&ictx
->ctx
);
2611 /* Handle the OSC 111 sequence for resetting background colour. */
2613 input_osc_111(struct input_ctx
*ictx
, const char *p
)
2615 struct window_pane
*wp
= ictx
->wp
;
2619 if (ictx
->palette
!= NULL
) {
2620 ictx
->palette
->bg
= 8;
2622 wp
->flags
|= PANE_STYLECHANGED
;
2623 screen_write_fullredraw(&ictx
->ctx
);
2627 /* Handle the OSC 52 sequence for setting the clipboard. */
2629 input_osc_52(struct input_ctx
*ictx
, const char *p
)
2631 struct window_pane
*wp
= ictx
->wp
;
2637 struct screen_write_ctx ctx
;
2638 struct paste_buffer
*pb
;
2642 state
= options_get_number(global_options
, "set-clipboard");
2646 if ((end
= strchr(p
, ';')) == NULL
)
2651 log_debug("%s: %s", __func__
, end
);
2653 if (strcmp(end
, "?") == 0) {
2654 if ((pb
= paste_get_top(NULL
)) != NULL
) {
2655 buf
= paste_buffer_data(pb
, &len
);
2656 outlen
= 4 * ((len
+ 2) / 3) + 1;
2657 out
= xmalloc(outlen
);
2658 if ((outlen
= b64_ntop(buf
, len
, out
, outlen
)) == -1) {
2666 bufferevent_write(ictx
->event
, "\033]52;;", 6);
2668 bufferevent_write(ictx
->event
, out
, outlen
);
2669 if (ictx
->input_end
== INPUT_END_BEL
)
2670 bufferevent_write(ictx
->event
, "\007", 1);
2672 bufferevent_write(ictx
->event
, "\033\\", 2);
2677 len
= (strlen(end
) / 4) * 3;
2682 if ((outlen
= b64_pton(end
, out
, len
)) == -1) {
2687 screen_write_start_pane(&ctx
, wp
, NULL
);
2688 screen_write_setselection(&ctx
, out
, outlen
);
2689 screen_write_stop(&ctx
);
2690 notify_pane("pane-set-clipboard", wp
);
2692 paste_add(NULL
, out
, outlen
);
2695 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2697 input_osc_104(struct input_ctx
*ictx
, const char *p
)
2701 int bad
= 0, redraw
= 0;
2704 colour_palette_clear(ictx
->palette
);
2705 screen_write_fullredraw(&ictx
->ctx
);
2709 copy
= s
= xstrdup(p
);
2710 while (*s
!= '\0') {
2711 idx
= strtol(s
, &s
, 10);
2712 if (*s
!= '\0' && *s
!= ';') {
2716 if (idx
< 0 || idx
>= 256) {
2720 if (colour_palette_set(ictx
->palette
, idx
, -1))
2726 log_debug("bad OSC 104: %s", p
);
2728 screen_write_fullredraw(&ictx
->ctx
);