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_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_winops(struct input_ctx
*);
173 static void input_csi_dispatch_sgr_256(struct input_ctx
*, int, u_int
*);
174 static void input_csi_dispatch_sgr_rgb(struct input_ctx
*, int, u_int
*);
175 static void input_csi_dispatch_sgr(struct input_ctx
*);
176 static int input_dcs_dispatch(struct input_ctx
*);
177 static int input_top_bit_set(struct input_ctx
*);
178 static int input_end_bel(struct input_ctx
*);
180 /* Command table comparison function. */
181 static int input_table_compare(const void *, const void *);
183 /* Command table entry. */
184 struct input_table_entry
{
190 /* Escape commands. */
191 enum input_esc_type
{
209 /* Escape command table. */
210 static const struct input_table_entry input_esc_table
[] = {
211 { '0', "(", INPUT_ESC_SCSG0_ON
},
212 { '0', ")", INPUT_ESC_SCSG1_ON
},
213 { '7', "", INPUT_ESC_DECSC
},
214 { '8', "", INPUT_ESC_DECRC
},
215 { '8', "#", INPUT_ESC_DECALN
},
216 { '=', "", INPUT_ESC_DECKPAM
},
217 { '>', "", INPUT_ESC_DECKPNM
},
218 { 'B', "(", INPUT_ESC_SCSG0_OFF
},
219 { 'B', ")", INPUT_ESC_SCSG1_OFF
},
220 { 'D', "", INPUT_ESC_IND
},
221 { 'E', "", INPUT_ESC_NEL
},
222 { 'H', "", INPUT_ESC_HTS
},
223 { 'M', "", INPUT_ESC_RI
},
224 { '\\', "", INPUT_ESC_ST
},
225 { 'c', "", INPUT_ESC_RIS
},
228 /* Control (CSI) commands. */
229 enum input_csi_type
{
256 INPUT_CSI_RM_PRIVATE
,
261 INPUT_CSI_SM_PRIVATE
,
269 /* Control (CSI) command table. */
270 static const struct input_table_entry input_csi_table
[] = {
271 { '@', "", INPUT_CSI_ICH
},
272 { 'A', "", INPUT_CSI_CUU
},
273 { 'B', "", INPUT_CSI_CUD
},
274 { 'C', "", INPUT_CSI_CUF
},
275 { 'D', "", INPUT_CSI_CUB
},
276 { 'E', "", INPUT_CSI_CNL
},
277 { 'F', "", INPUT_CSI_CPL
},
278 { 'G', "", INPUT_CSI_HPA
},
279 { 'H', "", INPUT_CSI_CUP
},
280 { 'J', "", INPUT_CSI_ED
},
281 { 'K', "", INPUT_CSI_EL
},
282 { 'L', "", INPUT_CSI_IL
},
283 { 'M', "", INPUT_CSI_DL
},
284 { 'P', "", INPUT_CSI_DCH
},
285 { 'S', "", INPUT_CSI_SU
},
286 { 'T', "", INPUT_CSI_SD
},
287 { 'X', "", INPUT_CSI_ECH
},
288 { 'Z', "", INPUT_CSI_CBT
},
289 { '`', "", INPUT_CSI_HPA
},
290 { 'b', "", INPUT_CSI_REP
},
291 { 'c', "", INPUT_CSI_DA
},
292 { 'c', ">", INPUT_CSI_DA_TWO
},
293 { 'd', "", INPUT_CSI_VPA
},
294 { 'f', "", INPUT_CSI_CUP
},
295 { 'g', "", INPUT_CSI_TBC
},
296 { 'h', "", INPUT_CSI_SM
},
297 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
298 { 'l', "", INPUT_CSI_RM
},
299 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
300 { 'm', "", INPUT_CSI_SGR
},
301 { 'm', ">", INPUT_CSI_MODSET
},
302 { 'n', "", INPUT_CSI_DSR
},
303 { 'n', ">", INPUT_CSI_MODOFF
},
304 { 'q', " ", INPUT_CSI_DECSCUSR
},
305 { 'q', ">", INPUT_CSI_XDA
},
306 { 'r', "", INPUT_CSI_DECSTBM
},
307 { 's', "", INPUT_CSI_SCP
},
308 { 't', "", INPUT_CSI_WINOPS
},
309 { 'u', "", INPUT_CSI_RCP
},
312 /* Input transition. */
313 struct input_transition
{
317 int (*handler
)(struct input_ctx
*);
318 const struct input_state
*state
;
324 void (*enter
)(struct input_ctx
*);
325 void (*exit
)(struct input_ctx
*);
326 const struct input_transition
*transitions
;
329 /* State transitions available from all states. */
330 #define INPUT_STATE_ANYWHERE \
331 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
332 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
333 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
335 /* Forward declarations of state tables. */
336 static const struct input_transition input_state_ground_table
[];
337 static const struct input_transition input_state_esc_enter_table
[];
338 static const struct input_transition input_state_esc_intermediate_table
[];
339 static const struct input_transition input_state_csi_enter_table
[];
340 static const struct input_transition input_state_csi_parameter_table
[];
341 static const struct input_transition input_state_csi_intermediate_table
[];
342 static const struct input_transition input_state_csi_ignore_table
[];
343 static const struct input_transition input_state_dcs_enter_table
[];
344 static const struct input_transition input_state_dcs_parameter_table
[];
345 static const struct input_transition input_state_dcs_intermediate_table
[];
346 static const struct input_transition input_state_dcs_handler_table
[];
347 static const struct input_transition input_state_dcs_escape_table
[];
348 static const struct input_transition input_state_dcs_ignore_table
[];
349 static const struct input_transition input_state_osc_string_table
[];
350 static const struct input_transition input_state_apc_string_table
[];
351 static const struct input_transition input_state_rename_string_table
[];
352 static const struct input_transition input_state_consume_st_table
[];
354 /* ground state definition. */
355 static const struct input_state input_state_ground
= {
358 input_state_ground_table
361 /* esc_enter state definition. */
362 static const struct input_state input_state_esc_enter
= {
365 input_state_esc_enter_table
368 /* esc_intermediate state definition. */
369 static const struct input_state input_state_esc_intermediate
= {
372 input_state_esc_intermediate_table
375 /* csi_enter state definition. */
376 static const struct input_state input_state_csi_enter
= {
379 input_state_csi_enter_table
382 /* csi_parameter state definition. */
383 static const struct input_state input_state_csi_parameter
= {
386 input_state_csi_parameter_table
389 /* csi_intermediate state definition. */
390 static const struct input_state input_state_csi_intermediate
= {
393 input_state_csi_intermediate_table
396 /* csi_ignore state definition. */
397 static const struct input_state input_state_csi_ignore
= {
400 input_state_csi_ignore_table
403 /* dcs_enter state definition. */
404 static const struct input_state input_state_dcs_enter
= {
406 input_enter_dcs
, NULL
,
407 input_state_dcs_enter_table
410 /* dcs_parameter state definition. */
411 static const struct input_state input_state_dcs_parameter
= {
414 input_state_dcs_parameter_table
417 /* dcs_intermediate state definition. */
418 static const struct input_state input_state_dcs_intermediate
= {
421 input_state_dcs_intermediate_table
424 /* dcs_handler state definition. */
425 static const struct input_state input_state_dcs_handler
= {
428 input_state_dcs_handler_table
431 /* dcs_escape state definition. */
432 static const struct input_state input_state_dcs_escape
= {
435 input_state_dcs_escape_table
438 /* dcs_ignore state definition. */
439 static const struct input_state input_state_dcs_ignore
= {
442 input_state_dcs_ignore_table
445 /* osc_string state definition. */
446 static const struct input_state input_state_osc_string
= {
448 input_enter_osc
, input_exit_osc
,
449 input_state_osc_string_table
452 /* apc_string state definition. */
453 static const struct input_state input_state_apc_string
= {
455 input_enter_apc
, input_exit_apc
,
456 input_state_apc_string_table
459 /* rename_string state definition. */
460 static const struct input_state input_state_rename_string
= {
462 input_enter_rename
, input_exit_rename
,
463 input_state_rename_string_table
466 /* consume_st state definition. */
467 static const struct input_state input_state_consume_st
= {
469 input_enter_rename
, NULL
, /* rename also waits for ST */
470 input_state_consume_st_table
473 /* ground state table. */
474 static const struct input_transition input_state_ground_table
[] = {
475 INPUT_STATE_ANYWHERE
,
477 { 0x00, 0x17, input_c0_dispatch
, NULL
},
478 { 0x19, 0x19, input_c0_dispatch
, NULL
},
479 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
480 { 0x20, 0x7e, input_print
, NULL
},
481 { 0x7f, 0x7f, NULL
, NULL
},
482 { 0x80, 0xff, input_top_bit_set
, NULL
},
484 { -1, -1, NULL
, NULL
}
487 /* esc_enter state table. */
488 static const struct input_transition input_state_esc_enter_table
[] = {
489 INPUT_STATE_ANYWHERE
,
491 { 0x00, 0x17, input_c0_dispatch
, NULL
},
492 { 0x19, 0x19, input_c0_dispatch
, NULL
},
493 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
494 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
495 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
496 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
497 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
498 { 0x58, 0x58, NULL
, &input_state_consume_st
},
499 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
500 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
501 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
502 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
503 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
504 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
505 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
506 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
507 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
508 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
509 { 0x7f, 0xff, NULL
, NULL
},
511 { -1, -1, NULL
, NULL
}
514 /* esc_intermediate state table. */
515 static const struct input_transition input_state_esc_intermediate_table
[] = {
516 INPUT_STATE_ANYWHERE
,
518 { 0x00, 0x17, input_c0_dispatch
, NULL
},
519 { 0x19, 0x19, input_c0_dispatch
, NULL
},
520 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
521 { 0x20, 0x2f, input_intermediate
, NULL
},
522 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
523 { 0x7f, 0xff, NULL
, NULL
},
525 { -1, -1, NULL
, NULL
}
528 /* csi_enter state table. */
529 static const struct input_transition input_state_csi_enter_table
[] = {
530 INPUT_STATE_ANYWHERE
,
532 { 0x00, 0x17, input_c0_dispatch
, NULL
},
533 { 0x19, 0x19, input_c0_dispatch
, NULL
},
534 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
535 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
536 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
537 { 0x3a, 0x3a, input_parameter
, &input_state_csi_parameter
},
538 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
539 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
540 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
541 { 0x7f, 0xff, NULL
, NULL
},
543 { -1, -1, NULL
, NULL
}
546 /* csi_parameter state table. */
547 static const struct input_transition input_state_csi_parameter_table
[] = {
548 INPUT_STATE_ANYWHERE
,
550 { 0x00, 0x17, input_c0_dispatch
, NULL
},
551 { 0x19, 0x19, input_c0_dispatch
, NULL
},
552 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
553 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
554 { 0x30, 0x39, input_parameter
, NULL
},
555 { 0x3a, 0x3a, input_parameter
, NULL
},
556 { 0x3b, 0x3b, input_parameter
, NULL
},
557 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
558 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
559 { 0x7f, 0xff, NULL
, NULL
},
561 { -1, -1, NULL
, NULL
}
564 /* csi_intermediate state table. */
565 static const struct input_transition input_state_csi_intermediate_table
[] = {
566 INPUT_STATE_ANYWHERE
,
568 { 0x00, 0x17, input_c0_dispatch
, NULL
},
569 { 0x19, 0x19, input_c0_dispatch
, NULL
},
570 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
571 { 0x20, 0x2f, input_intermediate
, NULL
},
572 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
573 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
574 { 0x7f, 0xff, NULL
, NULL
},
576 { -1, -1, NULL
, NULL
}
579 /* csi_ignore state table. */
580 static const struct input_transition input_state_csi_ignore_table
[] = {
581 INPUT_STATE_ANYWHERE
,
583 { 0x00, 0x17, input_c0_dispatch
, NULL
},
584 { 0x19, 0x19, input_c0_dispatch
, NULL
},
585 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
586 { 0x20, 0x3f, NULL
, NULL
},
587 { 0x40, 0x7e, NULL
, &input_state_ground
},
588 { 0x7f, 0xff, NULL
, NULL
},
590 { -1, -1, NULL
, NULL
}
593 /* dcs_enter state table. */
594 static const struct input_transition input_state_dcs_enter_table
[] = {
595 INPUT_STATE_ANYWHERE
,
597 { 0x00, 0x17, NULL
, NULL
},
598 { 0x19, 0x19, NULL
, NULL
},
599 { 0x1c, 0x1f, NULL
, NULL
},
600 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
601 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
602 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
603 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
604 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
605 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
606 { 0x7f, 0xff, NULL
, NULL
},
608 { -1, -1, NULL
, NULL
}
611 /* dcs_parameter state table. */
612 static const struct input_transition input_state_dcs_parameter_table
[] = {
613 INPUT_STATE_ANYWHERE
,
615 { 0x00, 0x17, NULL
, NULL
},
616 { 0x19, 0x19, NULL
, NULL
},
617 { 0x1c, 0x1f, NULL
, NULL
},
618 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
619 { 0x30, 0x39, input_parameter
, NULL
},
620 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
621 { 0x3b, 0x3b, input_parameter
, NULL
},
622 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
623 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
624 { 0x7f, 0xff, NULL
, NULL
},
626 { -1, -1, NULL
, NULL
}
629 /* dcs_intermediate state table. */
630 static const struct input_transition input_state_dcs_intermediate_table
[] = {
631 INPUT_STATE_ANYWHERE
,
633 { 0x00, 0x17, NULL
, NULL
},
634 { 0x19, 0x19, NULL
, NULL
},
635 { 0x1c, 0x1f, NULL
, NULL
},
636 { 0x20, 0x2f, input_intermediate
, NULL
},
637 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
638 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
639 { 0x7f, 0xff, NULL
, NULL
},
641 { -1, -1, NULL
, NULL
}
644 /* dcs_handler state table. */
645 static const struct input_transition input_state_dcs_handler_table
[] = {
646 /* No INPUT_STATE_ANYWHERE */
648 { 0x00, 0x1a, input_input
, NULL
},
649 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
650 { 0x1c, 0xff, input_input
, NULL
},
652 { -1, -1, NULL
, NULL
}
655 /* dcs_escape state table. */
656 static const struct input_transition input_state_dcs_escape_table
[] = {
657 /* No INPUT_STATE_ANYWHERE */
659 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
660 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
661 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
663 { -1, -1, NULL
, NULL
}
666 /* dcs_ignore state table. */
667 static const struct input_transition input_state_dcs_ignore_table
[] = {
668 INPUT_STATE_ANYWHERE
,
670 { 0x00, 0x17, NULL
, NULL
},
671 { 0x19, 0x19, NULL
, NULL
},
672 { 0x1c, 0x1f, NULL
, NULL
},
673 { 0x20, 0xff, NULL
, NULL
},
675 { -1, -1, NULL
, NULL
}
678 /* osc_string state table. */
679 static const struct input_transition input_state_osc_string_table
[] = {
680 INPUT_STATE_ANYWHERE
,
682 { 0x00, 0x06, NULL
, NULL
},
683 { 0x07, 0x07, input_end_bel
, &input_state_ground
},
684 { 0x08, 0x17, NULL
, NULL
},
685 { 0x19, 0x19, NULL
, NULL
},
686 { 0x1c, 0x1f, NULL
, NULL
},
687 { 0x20, 0xff, input_input
, NULL
},
689 { -1, -1, NULL
, NULL
}
692 /* apc_string state table. */
693 static const struct input_transition input_state_apc_string_table
[] = {
694 INPUT_STATE_ANYWHERE
,
696 { 0x00, 0x17, NULL
, NULL
},
697 { 0x19, 0x19, NULL
, NULL
},
698 { 0x1c, 0x1f, NULL
, NULL
},
699 { 0x20, 0xff, input_input
, NULL
},
701 { -1, -1, NULL
, NULL
}
704 /* rename_string state table. */
705 static const struct input_transition input_state_rename_string_table
[] = {
706 INPUT_STATE_ANYWHERE
,
708 { 0x00, 0x17, NULL
, NULL
},
709 { 0x19, 0x19, NULL
, NULL
},
710 { 0x1c, 0x1f, NULL
, NULL
},
711 { 0x20, 0xff, input_input
, NULL
},
713 { -1, -1, NULL
, NULL
}
716 /* consume_st state table. */
717 static const struct input_transition input_state_consume_st_table
[] = {
718 INPUT_STATE_ANYWHERE
,
720 { 0x00, 0x17, NULL
, NULL
},
721 { 0x19, 0x19, NULL
, NULL
},
722 { 0x1c, 0x1f, NULL
, NULL
},
723 { 0x20, 0xff, NULL
, NULL
},
725 { -1, -1, NULL
, NULL
}
728 /* Input table compare. */
730 input_table_compare(const void *key
, const void *value
)
732 const struct input_ctx
*ictx
= key
;
733 const struct input_table_entry
*entry
= value
;
735 if (ictx
->ch
!= entry
->ch
)
736 return (ictx
->ch
- entry
->ch
);
737 return (strcmp(ictx
->interm_buf
, entry
->interm
));
741 * Timer - if this expires then have been waiting for a terminator for too
742 * long, so reset to ground.
745 input_timer_callback(__unused
int fd
, __unused
short events
, void *arg
)
747 struct input_ctx
*ictx
= arg
;
749 log_debug("%s: %s expired" , __func__
, ictx
->state
->name
);
750 input_reset(ictx
, 0);
753 /* Start the timer. */
755 input_start_timer(struct input_ctx
*ictx
)
757 struct timeval tv
= { .tv_sec
= 5, .tv_usec
= 0 };
759 event_del(&ictx
->timer
);
760 event_add(&ictx
->timer
, &tv
);
763 /* Reset cell state to default. */
765 input_reset_cell(struct input_ctx
*ictx
)
767 memcpy(&ictx
->cell
.cell
, &grid_default_cell
, sizeof ictx
->cell
.cell
);
769 ictx
->cell
.g0set
= ictx
->cell
.g1set
= 0;
771 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
776 /* Save screen state. */
778 input_save_state(struct input_ctx
*ictx
)
780 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
781 struct screen
*s
= sctx
->s
;
783 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
784 ictx
->old_cx
= s
->cx
;
785 ictx
->old_cy
= s
->cy
;
786 ictx
->old_mode
= s
->mode
;
789 /* Restore screen state. */
791 input_restore_state(struct input_ctx
*ictx
)
793 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
795 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
796 if (ictx
->old_mode
& MODE_ORIGIN
)
797 screen_write_mode_set(sctx
, MODE_ORIGIN
);
799 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
800 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
, 0);
803 /* Initialise input parser. */
805 input_init(struct window_pane
*wp
, struct bufferevent
*bev
,
806 struct colour_palette
*palette
)
808 struct input_ctx
*ictx
;
810 ictx
= xcalloc(1, sizeof *ictx
);
813 ictx
->palette
= palette
;
815 ictx
->input_space
= INPUT_BUF_START
;
816 ictx
->input_buf
= xmalloc(INPUT_BUF_START
);
818 ictx
->since_ground
= evbuffer_new();
819 if (ictx
->since_ground
== NULL
)
820 fatalx("out of memory");
822 evtimer_set(&ictx
->timer
, input_timer_callback
, ictx
);
824 input_reset(ictx
, 0);
828 /* Destroy input parser. */
830 input_free(struct input_ctx
*ictx
)
834 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
835 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
836 free(ictx
->param_list
[i
].str
);
839 event_del(&ictx
->timer
);
841 free(ictx
->input_buf
);
842 evbuffer_free(ictx
->since_ground
);
847 /* Reset input state and clear screen. */
849 input_reset(struct input_ctx
*ictx
, int clear
)
851 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
852 struct window_pane
*wp
= ictx
->wp
;
854 input_reset_cell(ictx
);
856 if (clear
&& wp
!= NULL
) {
857 if (TAILQ_EMPTY(&wp
->modes
))
858 screen_write_start_pane(sctx
, wp
, &wp
->base
);
860 screen_write_start(sctx
, &wp
->base
);
861 screen_write_reset(sctx
);
862 screen_write_stop(sctx
);
869 ictx
->state
= &input_state_ground
;
873 /* Return pending data. */
875 input_pending(struct input_ctx
*ictx
)
877 return (ictx
->since_ground
);
880 /* Change input state. */
882 input_set_state(struct input_ctx
*ictx
, const struct input_transition
*itr
)
884 if (ictx
->state
->exit
!= NULL
)
885 ictx
->state
->exit(ictx
);
886 ictx
->state
= itr
->state
;
887 if (ictx
->state
->enter
!= NULL
)
888 ictx
->state
->enter(ictx
);
893 input_parse(struct input_ctx
*ictx
, u_char
*buf
, size_t len
)
895 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
896 const struct input_state
*state
= NULL
;
897 const struct input_transition
*itr
= NULL
;
900 /* Parse the input. */
902 ictx
->ch
= buf
[off
++];
904 /* Find the transition. */
905 if (ictx
->state
!= state
||
907 ictx
->ch
< itr
->first
||
908 ictx
->ch
> itr
->last
) {
909 itr
= ictx
->state
->transitions
;
910 while (itr
->first
!= -1 && itr
->last
!= -1) {
911 if (ictx
->ch
>= itr
->first
&&
912 ictx
->ch
<= itr
->last
)
916 if (itr
->first
== -1 || itr
->last
== -1) {
917 /* No transition? Eh? */
918 fatalx("no transition from state");
924 * Any state except print stops the current collection. This is
925 * an optimization to avoid checking if the attributes have
926 * changed for every character. It will stop unnecessarily for
927 * sequences that don't make a terminal change, but they should
930 if (itr
->handler
!= input_print
)
931 screen_write_collect_end(sctx
);
934 * Execute the handler, if any. Don't switch state if it
937 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
940 /* And switch state, if necessary. */
941 if (itr
->state
!= NULL
)
942 input_set_state(ictx
, itr
);
944 /* If not in ground state, save input. */
945 if (ictx
->state
!= &input_state_ground
)
946 evbuffer_add(ictx
->since_ground
, &ictx
->ch
, 1);
950 /* Parse input from pane. */
952 input_parse_pane(struct window_pane
*wp
)
957 new_data
= window_pane_get_new_data(wp
, &wp
->offset
, &new_size
);
958 input_parse_buffer(wp
, new_data
, new_size
);
959 window_pane_update_used_data(wp
, &wp
->offset
, new_size
);
962 /* Parse given input. */
964 input_parse_buffer(struct window_pane
*wp
, u_char
*buf
, size_t len
)
966 struct input_ctx
*ictx
= wp
->ictx
;
967 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
972 window_update_activity(wp
->window
);
973 wp
->flags
|= PANE_CHANGED
;
975 /* Flag new input while in a mode. */
976 if (!TAILQ_EMPTY(&wp
->modes
))
977 wp
->flags
|= PANE_UNSEENCHANGES
;
979 /* NULL wp if there is a mode set as don't want to update the tty. */
980 if (TAILQ_EMPTY(&wp
->modes
))
981 screen_write_start_pane(sctx
, wp
, &wp
->base
);
983 screen_write_start(sctx
, &wp
->base
);
985 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__
, wp
->id
,
986 ictx
->state
->name
, len
, (int)len
, buf
);
988 input_parse(ictx
, buf
, len
);
989 screen_write_stop(sctx
);
992 /* Parse given input for screen. */
994 input_parse_screen(struct input_ctx
*ictx
, struct screen
*s
,
995 screen_write_init_ctx_cb cb
, void *arg
, u_char
*buf
, size_t len
)
997 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1002 screen_write_start_callback(sctx
, s
, cb
, arg
);
1003 input_parse(ictx
, buf
, len
);
1004 screen_write_stop(sctx
);
1007 /* Split the parameter list (if any). */
1009 input_split(struct input_ctx
*ictx
)
1013 struct input_param
*ip
;
1016 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1017 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
1018 free(ictx
->param_list
[i
].str
);
1020 ictx
->param_list_len
= 0;
1022 if (ictx
->param_len
== 0)
1024 ip
= &ictx
->param_list
[0];
1026 ptr
= ictx
->param_buf
;
1027 while ((out
= strsep(&ptr
, ";")) != NULL
) {
1029 ip
->type
= INPUT_MISSING
;
1031 if (strchr(out
, ':') != NULL
) {
1032 ip
->type
= INPUT_STRING
;
1033 ip
->str
= xstrdup(out
);
1035 ip
->type
= INPUT_NUMBER
;
1036 ip
->num
= strtonum(out
, 0, INT_MAX
, &errstr
);
1041 ip
= &ictx
->param_list
[++ictx
->param_list_len
];
1042 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
1046 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1047 ip
= &ictx
->param_list
[i
];
1048 if (ip
->type
== INPUT_MISSING
)
1049 log_debug("parameter %u: missing", i
);
1050 else if (ip
->type
== INPUT_STRING
)
1051 log_debug("parameter %u: string %s", i
, ip
->str
);
1052 else if (ip
->type
== INPUT_NUMBER
)
1053 log_debug("parameter %u: number %d", i
, ip
->num
);
1059 /* Get an argument or return default value. */
1061 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
1063 struct input_param
*ip
;
1066 if (validx
>= ictx
->param_list_len
)
1068 ip
= &ictx
->param_list
[validx
];
1069 if (ip
->type
== INPUT_MISSING
)
1071 if (ip
->type
== INPUT_STRING
)
1074 if (retval
< minval
)
1079 /* Reply to terminal query. */
1081 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
1083 struct bufferevent
*bev
= ictx
->event
;
1091 xvasprintf(&reply
, fmt
, ap
);
1094 log_debug("%s: %s", __func__
, reply
);
1095 bufferevent_write(bev
, reply
, strlen(reply
));
1099 /* Clear saved state. */
1101 input_clear(struct input_ctx
*ictx
)
1103 event_del(&ictx
->timer
);
1105 *ictx
->interm_buf
= '\0';
1106 ictx
->interm_len
= 0;
1108 *ictx
->param_buf
= '\0';
1109 ictx
->param_len
= 0;
1111 *ictx
->input_buf
= '\0';
1112 ictx
->input_len
= 0;
1114 ictx
->input_end
= INPUT_END_ST
;
1116 ictx
->flags
&= ~INPUT_DISCARD
;
1119 /* Reset for ground state. */
1121 input_ground(struct input_ctx
*ictx
)
1123 event_del(&ictx
->timer
);
1124 evbuffer_drain(ictx
->since_ground
, EVBUFFER_LENGTH(ictx
->since_ground
));
1126 if (ictx
->input_space
> INPUT_BUF_START
) {
1127 ictx
->input_space
= INPUT_BUF_START
;
1128 ictx
->input_buf
= xrealloc(ictx
->input_buf
, INPUT_BUF_START
);
1132 /* Output this character to the screen. */
1134 input_print(struct input_ctx
*ictx
)
1136 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1139 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1141 set
= ictx
->cell
.set
== 0 ? ictx
->cell
.g0set
: ictx
->cell
.g1set
;
1143 ictx
->cell
.cell
.attr
|= GRID_ATTR_CHARSET
;
1145 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1147 utf8_set(&ictx
->cell
.cell
.data
, ictx
->ch
);
1148 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1149 ictx
->last
= ictx
->ch
;
1151 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1156 /* Collect intermediate string. */
1158 input_intermediate(struct input_ctx
*ictx
)
1160 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
1161 ictx
->flags
|= INPUT_DISCARD
;
1163 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
1164 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
1170 /* Collect parameter string. */
1172 input_parameter(struct input_ctx
*ictx
)
1174 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
1175 ictx
->flags
|= INPUT_DISCARD
;
1177 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
1178 ictx
->param_buf
[ictx
->param_len
] = '\0';
1184 /* Collect input string. */
1186 input_input(struct input_ctx
*ictx
)
1190 available
= ictx
->input_space
;
1191 while (ictx
->input_len
+ 1 >= available
) {
1193 if (available
> INPUT_BUF_LIMIT
) {
1194 ictx
->flags
|= INPUT_DISCARD
;
1197 ictx
->input_buf
= xrealloc(ictx
->input_buf
, available
);
1198 ictx
->input_space
= available
;
1200 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
1201 ictx
->input_buf
[ictx
->input_len
] = '\0';
1206 /* Execute C0 control sequence. */
1208 input_c0_dispatch(struct input_ctx
*ictx
)
1210 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1211 struct window_pane
*wp
= ictx
->wp
;
1212 struct screen
*s
= sctx
->s
;
1214 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1216 log_debug("%s: '%c'", __func__
, ictx
->ch
);
1219 case '\000': /* NUL */
1221 case '\007': /* BEL */
1223 alerts_queue(wp
->window
, WINDOW_BELL
);
1225 case '\010': /* BS */
1226 screen_write_backspace(sctx
);
1228 case '\011': /* HT */
1229 /* Don't tab beyond the end of the line. */
1230 if (s
->cx
>= screen_size_x(s
) - 1)
1233 /* Find the next tab point, or use the last column if none. */
1236 if (bit_test(s
->tabs
, s
->cx
))
1238 } while (s
->cx
< screen_size_x(s
) - 1);
1240 case '\012': /* LF */
1241 case '\013': /* VT */
1242 case '\014': /* FF */
1243 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1244 if (s
->mode
& MODE_CRLF
)
1245 screen_write_carriagereturn(sctx
);
1247 case '\015': /* CR */
1248 screen_write_carriagereturn(sctx
);
1250 case '\016': /* SO */
1253 case '\017': /* SI */
1257 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1265 /* Execute escape sequence. */
1267 input_esc_dispatch(struct input_ctx
*ictx
)
1269 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1270 struct screen
*s
= sctx
->s
;
1271 struct input_table_entry
*entry
;
1273 if (ictx
->flags
& INPUT_DISCARD
)
1275 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
1277 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
1278 sizeof input_esc_table
[0], input_table_compare
);
1279 if (entry
== NULL
) {
1280 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1284 switch (entry
->type
) {
1286 colour_palette_clear(ictx
->palette
);
1287 input_reset_cell(ictx
);
1288 screen_write_reset(sctx
);
1289 screen_write_fullredraw(sctx
);
1292 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1295 screen_write_carriagereturn(sctx
);
1296 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1299 if (s
->cx
< screen_size_x(s
))
1300 bit_set(s
->tabs
, s
->cx
);
1303 screen_write_reverseindex(sctx
, ictx
->cell
.cell
.bg
);
1305 case INPUT_ESC_DECKPAM
:
1306 screen_write_mode_set(sctx
, MODE_KKEYPAD
);
1308 case INPUT_ESC_DECKPNM
:
1309 screen_write_mode_clear(sctx
, MODE_KKEYPAD
);
1311 case INPUT_ESC_DECSC
:
1312 input_save_state(ictx
);
1314 case INPUT_ESC_DECRC
:
1315 input_restore_state(ictx
);
1317 case INPUT_ESC_DECALN
:
1318 screen_write_alignmenttest(sctx
);
1320 case INPUT_ESC_SCSG0_ON
:
1321 ictx
->cell
.g0set
= 1;
1323 case INPUT_ESC_SCSG0_OFF
:
1324 ictx
->cell
.g0set
= 0;
1326 case INPUT_ESC_SCSG1_ON
:
1327 ictx
->cell
.g1set
= 1;
1329 case INPUT_ESC_SCSG1_OFF
:
1330 ictx
->cell
.g1set
= 0;
1333 /* ST terminates OSC but the state transition already did it. */
1341 /* Execute control sequence. */
1343 input_csi_dispatch(struct input_ctx
*ictx
)
1345 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1346 struct screen
*s
= sctx
->s
;
1347 struct input_table_entry
*entry
;
1349 u_int cx
, bg
= ictx
->cell
.cell
.bg
;
1351 if (ictx
->flags
& INPUT_DISCARD
)
1354 log_debug("%s: '%c' \"%s\" \"%s\"", __func__
, ictx
->ch
,
1355 ictx
->interm_buf
, ictx
->param_buf
);
1357 if (input_split(ictx
) != 0)
1360 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1361 sizeof input_csi_table
[0], input_table_compare
);
1362 if (entry
== NULL
) {
1363 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1367 switch (entry
->type
) {
1369 /* Find the previous tab point, n times. */
1371 if (cx
> screen_size_x(s
) - 1)
1372 cx
= screen_size_x(s
) - 1;
1373 n
= input_get(ictx
, 0, 1, 1);
1376 while (cx
> 0 && n
-- > 0) {
1379 while (cx
> 0 && !bit_test(s
->tabs
, cx
));
1384 n
= input_get(ictx
, 0, 1, 1);
1386 screen_write_cursorleft(sctx
, n
);
1389 n
= input_get(ictx
, 0, 1, 1);
1391 screen_write_cursordown(sctx
, n
);
1394 n
= input_get(ictx
, 0, 1, 1);
1396 screen_write_cursorright(sctx
, n
);
1399 n
= input_get(ictx
, 0, 1, 1);
1400 m
= input_get(ictx
, 1, 1, 1);
1401 if (n
!= -1 && m
!= -1)
1402 screen_write_cursormove(sctx
, m
- 1, n
- 1, 1);
1404 case INPUT_CSI_MODSET
:
1405 n
= input_get(ictx
, 0, 0, 0);
1406 m
= input_get(ictx
, 1, 0, 0);
1407 if (options_get_number(global_options
, "extended-keys") == 2)
1409 if (n
== 0 || (n
== 4 && m
== 0))
1410 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1411 else if (n
== 4 && (m
== 1 || m
== 2))
1412 screen_write_mode_set(sctx
, MODE_KEXTENDED
);
1414 case INPUT_CSI_MODOFF
:
1415 n
= input_get(ictx
, 0, 0, 0);
1417 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1419 case INPUT_CSI_WINOPS
:
1420 input_csi_dispatch_winops(ictx
);
1423 n
= input_get(ictx
, 0, 1, 1);
1425 screen_write_cursorup(sctx
, n
);
1428 n
= input_get(ictx
, 0, 1, 1);
1430 screen_write_carriagereturn(sctx
);
1431 screen_write_cursordown(sctx
, n
);
1435 n
= input_get(ictx
, 0, 1, 1);
1437 screen_write_carriagereturn(sctx
);
1438 screen_write_cursorup(sctx
, n
);
1442 switch (input_get(ictx
, 0, 0, 0)) {
1446 input_reply(ictx
, "\033[?1;2c");
1449 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1453 case INPUT_CSI_DA_TWO
:
1454 switch (input_get(ictx
, 0, 0, 0)) {
1458 input_reply(ictx
, "\033[>84;0;0c");
1461 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1466 n
= input_get(ictx
, 0, 1, 1);
1468 screen_write_clearcharacter(sctx
, n
, bg
);
1471 n
= input_get(ictx
, 0, 1, 1);
1473 screen_write_deletecharacter(sctx
, n
, bg
);
1475 case INPUT_CSI_DECSTBM
:
1476 n
= input_get(ictx
, 0, 1, 1);
1477 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1478 if (n
!= -1 && m
!= -1)
1479 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1482 n
= input_get(ictx
, 0, 1, 1);
1484 screen_write_deleteline(sctx
, n
, bg
);
1487 switch (input_get(ictx
, 0, 0, 0)) {
1491 input_reply(ictx
, "\033[0n");
1494 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1497 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1502 switch (input_get(ictx
, 0, 0, 0)) {
1506 screen_write_clearendofscreen(sctx
, bg
);
1509 screen_write_clearstartofscreen(sctx
, bg
);
1512 screen_write_clearscreen(sctx
, bg
);
1515 if (input_get(ictx
, 1, 0, 0) == 0) {
1517 * Linux console extension to clear history
1518 * (for example before locking the screen).
1520 screen_write_clearhistory(sctx
);
1524 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1529 switch (input_get(ictx
, 0, 0, 0)) {
1533 screen_write_clearendofline(sctx
, bg
);
1536 screen_write_clearstartofline(sctx
, bg
);
1539 screen_write_clearline(sctx
, bg
);
1542 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1547 n
= input_get(ictx
, 0, 1, 1);
1549 screen_write_cursormove(sctx
, n
- 1, -1, 1);
1552 n
= input_get(ictx
, 0, 1, 1);
1554 screen_write_insertcharacter(sctx
, n
, bg
);
1557 n
= input_get(ictx
, 0, 1, 1);
1559 screen_write_insertline(sctx
, n
, bg
);
1562 n
= input_get(ictx
, 0, 1, 1);
1566 m
= screen_size_x(s
) - s
->cx
;
1570 if (ictx
->last
== -1)
1572 ictx
->ch
= ictx
->last
;
1574 for (i
= 0; i
< n
; i
++)
1578 input_restore_state(ictx
);
1581 input_csi_dispatch_rm(ictx
);
1583 case INPUT_CSI_RM_PRIVATE
:
1584 input_csi_dispatch_rm_private(ictx
);
1587 input_save_state(ictx
);
1590 input_csi_dispatch_sgr(ictx
);
1593 input_csi_dispatch_sm(ictx
);
1595 case INPUT_CSI_SM_PRIVATE
:
1596 input_csi_dispatch_sm_private(ictx
);
1599 n
= input_get(ictx
, 0, 1, 1);
1601 screen_write_scrollup(sctx
, n
, bg
);
1604 n
= input_get(ictx
, 0, 1, 1);
1606 screen_write_scrolldown(sctx
, n
, bg
);
1609 switch (input_get(ictx
, 0, 0, 0)) {
1613 if (s
->cx
< screen_size_x(s
))
1614 bit_clear(s
->tabs
, s
->cx
);
1617 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1620 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1625 n
= input_get(ictx
, 0, 1, 1);
1627 screen_write_cursormove(sctx
, -1, n
- 1, 1);
1629 case INPUT_CSI_DECSCUSR
:
1630 n
= input_get(ictx
, 0, 0, 0);
1632 screen_set_cursor_style(n
, &s
->cstyle
, &s
->mode
);
1635 n
= input_get(ictx
, 0, 0, 0);
1637 input_reply(ictx
, "\033P>|tmux %s\033\\", getversion());
1646 /* Handle CSI RM. */
1648 input_csi_dispatch_rm(struct input_ctx
*ictx
)
1650 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1653 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1654 switch (input_get(ictx
, i
, 0, -1)) {
1658 screen_write_mode_clear(sctx
, MODE_INSERT
);
1661 screen_write_mode_set(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1664 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1670 /* Handle CSI private RM. */
1672 input_csi_dispatch_rm_private(struct input_ctx
*ictx
)
1674 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1675 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1678 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1679 switch (input_get(ictx
, i
, 0, -1)) {
1682 case 1: /* DECCKM */
1683 screen_write_mode_clear(sctx
, MODE_KCURSOR
);
1685 case 3: /* DECCOLM */
1686 screen_write_cursormove(sctx
, 0, 0, 1);
1687 screen_write_clearscreen(sctx
, gc
->bg
);
1690 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
1691 screen_write_cursormove(sctx
, 0, 0, 1);
1693 case 7: /* DECAWM */
1694 screen_write_mode_clear(sctx
, MODE_WRAP
);
1697 screen_write_mode_clear(sctx
, MODE_CURSOR_BLINKING
);
1698 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1701 screen_write_mode_clear(sctx
, MODE_CURSOR
);
1707 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1710 screen_write_mode_clear(sctx
, MODE_FOCUSON
);
1713 screen_write_mode_clear(sctx
, MODE_MOUSE_UTF8
);
1716 screen_write_mode_clear(sctx
, MODE_MOUSE_SGR
);
1720 screen_write_alternateoff(sctx
, gc
, 0);
1723 screen_write_alternateoff(sctx
, gc
, 1);
1726 screen_write_mode_clear(sctx
, MODE_BRACKETPASTE
);
1729 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1735 /* Handle CSI SM. */
1737 input_csi_dispatch_sm(struct input_ctx
*ictx
)
1739 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1742 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1743 switch (input_get(ictx
, i
, 0, -1)) {
1747 screen_write_mode_set(sctx
, MODE_INSERT
);
1750 screen_write_mode_clear(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1753 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1759 /* Handle CSI private SM. */
1761 input_csi_dispatch_sm_private(struct input_ctx
*ictx
)
1763 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1764 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1767 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1768 switch (input_get(ictx
, i
, 0, -1)) {
1771 case 1: /* DECCKM */
1772 screen_write_mode_set(sctx
, MODE_KCURSOR
);
1774 case 3: /* DECCOLM */
1775 screen_write_cursormove(sctx
, 0, 0, 1);
1776 screen_write_clearscreen(sctx
, ictx
->cell
.cell
.bg
);
1779 screen_write_mode_set(sctx
, MODE_ORIGIN
);
1780 screen_write_cursormove(sctx
, 0, 0, 1);
1782 case 7: /* DECAWM */
1783 screen_write_mode_set(sctx
, MODE_WRAP
);
1786 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING
);
1787 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1790 screen_write_mode_set(sctx
, MODE_CURSOR
);
1793 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1794 screen_write_mode_set(sctx
, MODE_MOUSE_STANDARD
);
1797 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1798 screen_write_mode_set(sctx
, MODE_MOUSE_BUTTON
);
1801 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1802 screen_write_mode_set(sctx
, MODE_MOUSE_ALL
);
1805 screen_write_mode_set(sctx
, MODE_FOCUSON
);
1808 screen_write_mode_set(sctx
, MODE_MOUSE_UTF8
);
1811 screen_write_mode_set(sctx
, MODE_MOUSE_SGR
);
1815 screen_write_alternateon(sctx
, gc
, 0);
1818 screen_write_alternateon(sctx
, gc
, 1);
1821 screen_write_mode_set(sctx
, MODE_BRACKETPASTE
);
1824 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1830 /* Handle CSI window operations. */
1832 input_csi_dispatch_winops(struct input_ctx
*ictx
)
1834 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1835 struct screen
*s
= sctx
->s
;
1836 struct window_pane
*wp
= ictx
->wp
;
1837 u_int x
= screen_size_x(s
), y
= screen_size_y(s
);
1841 while ((n
= input_get(ictx
, m
, 0, -1)) != -1) {
1860 if (input_get(ictx
, m
, 0, -1) == -1)
1866 if (input_get(ictx
, m
, 0, -1) == -1)
1871 switch (input_get(ictx
, m
, 0, -1)) {
1876 screen_push_title(sctx
->s
);
1882 switch (input_get(ictx
, m
, 0, -1)) {
1887 screen_pop_title(sctx
->s
);
1890 notify_pane("pane-title-changed", wp
);
1891 server_redraw_window_borders(wp
->window
);
1892 server_status_window(wp
->window
);
1897 input_reply(ictx
, "\033[8;%u;%ut", y
, x
);
1900 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1907 /* Helper for 256 colour SGR. */
1909 input_csi_dispatch_sgr_256_do(struct input_ctx
*ictx
, int fgbg
, int c
)
1911 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1913 if (c
== -1 || c
> 255) {
1916 else if (fgbg
== 48)
1920 gc
->fg
= c
| COLOUR_FLAG_256
;
1921 else if (fgbg
== 48)
1922 gc
->bg
= c
| COLOUR_FLAG_256
;
1923 else if (fgbg
== 58)
1924 gc
->us
= c
| COLOUR_FLAG_256
;
1929 /* Handle CSI SGR for 256 colours. */
1931 input_csi_dispatch_sgr_256(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1935 c
= input_get(ictx
, (*i
) + 1, 0, -1);
1936 if (input_csi_dispatch_sgr_256_do(ictx
, fgbg
, c
))
1940 /* Helper for RGB colour SGR. */
1942 input_csi_dispatch_sgr_rgb_do(struct input_ctx
*ictx
, int fgbg
, int r
, int g
,
1945 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1947 if (r
== -1 || r
> 255)
1949 if (g
== -1 || g
> 255)
1951 if (b
== -1 || b
> 255)
1955 gc
->fg
= colour_join_rgb(r
, g
, b
);
1956 else if (fgbg
== 48)
1957 gc
->bg
= colour_join_rgb(r
, g
, b
);
1958 else if (fgbg
== 58)
1959 gc
->us
= colour_join_rgb(r
, g
, b
);
1963 /* Handle CSI SGR for RGB colours. */
1965 input_csi_dispatch_sgr_rgb(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1969 r
= input_get(ictx
, (*i
) + 1, 0, -1);
1970 g
= input_get(ictx
, (*i
) + 2, 0, -1);
1971 b
= input_get(ictx
, (*i
) + 3, 0, -1);
1972 if (input_csi_dispatch_sgr_rgb_do(ictx
, fgbg
, r
, g
, b
))
1976 /* Handle CSI SGR with a ISO parameter. */
1978 input_csi_dispatch_sgr_colon(struct input_ctx
*ictx
, u_int i
)
1980 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1981 char *s
= ictx
->param_list
[i
].str
, *copy
, *ptr
, *out
;
1986 for (n
= 0; n
< nitems(p
); n
++)
1990 ptr
= copy
= xstrdup(s
);
1991 while ((out
= strsep(&ptr
, ":")) != NULL
) {
1993 p
[n
++] = strtonum(out
, 0, INT_MAX
, &errstr
);
1994 if (errstr
!= NULL
|| n
== nitems(p
)) {
2000 if (n
== nitems(p
)) {
2005 log_debug("%s: %u = %d", __func__
, n
- 1, p
[n
- 1]);
2016 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2019 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2020 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2023 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2024 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2027 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2028 gc
->attr
|= GRID_ATTR_UNDERSCORE_3
;
2031 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2032 gc
->attr
|= GRID_ATTR_UNDERSCORE_4
;
2035 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2036 gc
->attr
|= GRID_ATTR_UNDERSCORE_5
;
2041 if (n
< 2 || (p
[0] != 38 && p
[0] != 48 && p
[0] != 58))
2053 input_csi_dispatch_sgr_rgb_do(ictx
, p
[0], p
[i
], p
[i
+ 1],
2059 input_csi_dispatch_sgr_256_do(ictx
, p
[0], p
[2]);
2064 /* Handle CSI SGR. */
2066 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
2068 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2072 if (ictx
->param_list_len
== 0) {
2073 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2077 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
2078 if (ictx
->param_list
[i
].type
== INPUT_STRING
) {
2079 input_csi_dispatch_sgr_colon(ictx
, i
);
2082 n
= input_get(ictx
, i
, 0, 0);
2086 if (n
== 38 || n
== 48 || n
== 58) {
2088 switch (input_get(ictx
, i
, 0, -1)) {
2090 input_csi_dispatch_sgr_rgb(ictx
, n
, &i
);
2093 input_csi_dispatch_sgr_256(ictx
, n
, &i
);
2102 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2106 gc
->attr
|= GRID_ATTR_BRIGHT
;
2109 gc
->attr
|= GRID_ATTR_DIM
;
2112 gc
->attr
|= GRID_ATTR_ITALICS
;
2115 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2116 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2120 gc
->attr
|= GRID_ATTR_BLINK
;
2123 gc
->attr
|= GRID_ATTR_REVERSE
;
2126 gc
->attr
|= GRID_ATTR_HIDDEN
;
2129 gc
->attr
|= GRID_ATTR_STRIKETHROUGH
;
2132 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2133 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2136 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
2139 gc
->attr
&= ~GRID_ATTR_ITALICS
;
2142 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2145 gc
->attr
&= ~GRID_ATTR_BLINK
;
2148 gc
->attr
&= ~GRID_ATTR_REVERSE
;
2151 gc
->attr
&= ~GRID_ATTR_HIDDEN
;
2154 gc
->attr
&= ~GRID_ATTR_STRIKETHROUGH
;
2183 gc
->attr
|= GRID_ATTR_OVERLINE
;
2186 gc
->attr
&= ~GRID_ATTR_OVERLINE
;
2215 /* End of input with BEL. */
2217 input_end_bel(struct input_ctx
*ictx
)
2219 log_debug("%s", __func__
);
2221 ictx
->input_end
= INPUT_END_BEL
;
2226 /* DCS string started. */
2228 input_enter_dcs(struct input_ctx
*ictx
)
2230 log_debug("%s", __func__
);
2233 input_start_timer(ictx
);
2237 /* DCS terminator (ST) received. */
2239 input_dcs_dispatch(struct input_ctx
*ictx
)
2241 struct window_pane
*wp
= ictx
->wp
;
2242 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2243 u_char
*buf
= ictx
->input_buf
;
2244 size_t len
= ictx
->input_len
;
2245 const char prefix
[] = "tmux;";
2246 const u_int prefixlen
= (sizeof prefix
) - 1;
2247 long long allow_passthrough
= 0;
2251 if (ictx
->flags
& INPUT_DISCARD
)
2253 allow_passthrough
= options_get_number(wp
->options
,
2254 "allow-passthrough");
2255 if (!allow_passthrough
)
2257 log_debug("%s: \"%s\"", __func__
, buf
);
2259 if (len
>= prefixlen
&& strncmp(buf
, prefix
, prefixlen
) == 0) {
2260 screen_write_rawstring(sctx
, buf
+ prefixlen
, len
- prefixlen
,
2261 allow_passthrough
== 2);
2267 /* OSC string started. */
2269 input_enter_osc(struct input_ctx
*ictx
)
2271 log_debug("%s", __func__
);
2274 input_start_timer(ictx
);
2278 /* OSC terminator (ST) received. */
2280 input_exit_osc(struct input_ctx
*ictx
)
2282 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2283 struct window_pane
*wp
= ictx
->wp
;
2284 u_char
*p
= ictx
->input_buf
;
2287 if (ictx
->flags
& INPUT_DISCARD
)
2289 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
2292 log_debug("%s: \"%s\" (end %s)", __func__
, p
,
2293 ictx
->input_end
== INPUT_END_ST
? "ST" : "BEL");
2296 while (*p
>= '0' && *p
<= '9')
2297 option
= option
* 10 + *p
++ - '0';
2298 if (*p
!= ';' && *p
!= '\0')
2306 if (screen_set_title(sctx
->s
, p
) && wp
!= NULL
) {
2307 notify_pane("pane-title-changed", wp
);
2308 server_redraw_window_borders(wp
->window
);
2309 server_status_window(wp
->window
);
2313 input_osc_4(ictx
, p
);
2316 if (utf8_isvalid(p
)) {
2317 screen_set_path(sctx
->s
, p
);
2319 server_redraw_window_borders(wp
->window
);
2320 server_status_window(wp
->window
);
2325 input_osc_8(ictx
, p
);
2328 input_osc_10(ictx
, p
);
2331 input_osc_11(ictx
, p
);
2334 input_osc_12(ictx
, p
);
2337 input_osc_52(ictx
, p
);
2340 input_osc_104(ictx
, p
);
2343 input_osc_110(ictx
, p
);
2346 input_osc_111(ictx
, p
);
2349 input_osc_112(ictx
, p
);
2352 input_osc_133(ictx
, p
);
2355 log_debug("%s: unknown '%u'", __func__
, option
);
2360 /* APC string started. */
2362 input_enter_apc(struct input_ctx
*ictx
)
2364 log_debug("%s", __func__
);
2367 input_start_timer(ictx
);
2371 /* APC terminator (ST) received. */
2373 input_exit_apc(struct input_ctx
*ictx
)
2375 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2376 struct window_pane
*wp
= ictx
->wp
;
2378 if (ictx
->flags
& INPUT_DISCARD
)
2380 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2382 if (screen_set_title(sctx
->s
, ictx
->input_buf
) && wp
!= NULL
) {
2383 notify_pane("pane-title-changed", wp
);
2384 server_redraw_window_borders(wp
->window
);
2385 server_status_window(wp
->window
);
2389 /* Rename string started. */
2391 input_enter_rename(struct input_ctx
*ictx
)
2393 log_debug("%s", __func__
);
2396 input_start_timer(ictx
);
2400 /* Rename terminator (ST) received. */
2402 input_exit_rename(struct input_ctx
*ictx
)
2404 struct window_pane
*wp
= ictx
->wp
;
2406 struct options_entry
*o
;
2410 if (ictx
->flags
& INPUT_DISCARD
)
2412 if (!options_get_number(ictx
->wp
->options
, "allow-rename"))
2414 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2416 if (!utf8_isvalid(ictx
->input_buf
))
2420 if (ictx
->input_len
== 0) {
2421 o
= options_get_only(w
->options
, "automatic-rename");
2423 options_remove_or_default(o
, -1, NULL
);
2424 if (!options_get_number(w
->options
, "automatic-rename"))
2425 window_set_name(w
, "");
2427 options_set_number(w
->options
, "automatic-rename", 0);
2428 window_set_name(w
, ictx
->input_buf
);
2430 server_redraw_window_borders(w
);
2431 server_status_window(w
);
2434 /* Open UTF-8 character. */
2436 input_top_bit_set(struct input_ctx
*ictx
)
2438 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2439 struct utf8_data
*ud
= &ictx
->utf8data
;
2443 if (!ictx
->utf8started
) {
2444 if (utf8_open(ud
, ictx
->ch
) != UTF8_MORE
)
2446 ictx
->utf8started
= 1;
2450 switch (utf8_append(ud
, ictx
->ch
)) {
2454 ictx
->utf8started
= 0;
2459 ictx
->utf8started
= 0;
2461 log_debug("%s %hhu '%*s' (width %hhu)", __func__
, ud
->size
,
2462 (int)ud
->size
, ud
->data
, ud
->width
);
2464 utf8_copy(&ictx
->cell
.cell
.data
, ud
);
2465 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
2470 /* Reply to a colour request. */
2472 input_osc_colour_reply(struct input_ctx
*ictx
, u_int n
, int c
)
2478 c
= colour_force_rgb(c
);
2481 colour_split_rgb(c
, &r
, &g
, &b
);
2483 if (ictx
->input_end
== INPUT_END_BEL
)
2487 input_reply(ictx
, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2488 n
, r
, r
, g
, g
, b
, b
, end
);
2491 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2493 input_osc_4(struct input_ctx
*ictx
, const char *p
)
2495 char *copy
, *s
, *next
= NULL
;
2497 int c
, bad
= 0, redraw
= 0;
2499 copy
= s
= xstrdup(p
);
2500 while (s
!= NULL
&& *s
!= '\0') {
2501 idx
= strtol(s
, &next
, 10);
2502 if (*next
++ != ';') {
2506 if (idx
< 0 || idx
>= 256) {
2511 s
= strsep(&next
, ";");
2512 if (strcmp(s
, "?") == 0) {
2513 c
= colour_palette_get(ictx
->palette
, idx
);
2515 input_osc_colour_reply(ictx
, 4, c
);
2518 if ((c
= colour_parseX11(s
)) == -1) {
2522 if (colour_palette_set(ictx
->palette
, idx
, c
))
2527 log_debug("bad OSC 4: %s", p
);
2529 screen_write_fullredraw(&ictx
->ctx
);
2533 /* Handle the OSC 8 sequence for embedding hyperlinks. */
2535 input_osc_8(struct input_ctx
*ictx
, const char *p
)
2537 struct hyperlinks
*hl
= ictx
->ctx
.s
->hyperlinks
;
2538 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2539 const char *start
, *end
, *uri
;
2542 for (start
= p
; (end
= strpbrk(start
, ":;")) != NULL
; start
= end
+ 1) {
2543 if (end
- start
>= 4 && strncmp(start
, "id=", 3) == 0) {
2546 id
= xstrndup(start
+ 3, end
- start
- 3);
2549 /* The first ; is the end of parameters and start of the URI. */
2553 if (end
== NULL
|| *end
!= ';')
2561 gc
->link
= hyperlinks_put(hl
, uri
, id
);
2563 log_debug("hyperlink (anonymous) %s = %u", uri
, gc
->link
);
2565 log_debug("hyperlink (id=%s) %s = %u", id
, uri
, gc
->link
);
2570 log_debug("bad OSC 8 %s", p
);
2575 * Get a client with a foreground for the pane. There isn't much to choose
2576 * between them so just use the first.
2579 input_get_fg_client(struct window_pane
*wp
)
2581 struct window
*w
= wp
->window
;
2582 struct client
*loop
;
2584 TAILQ_FOREACH(loop
, &clients
, entry
) {
2585 if (loop
->flags
& CLIENT_UNATTACHEDFLAGS
)
2587 if (loop
->session
== NULL
|| !session_has(loop
->session
, w
))
2589 if (loop
->tty
.fg
== -1)
2591 return (loop
->tty
.fg
);
2596 /* Get a client with a background for the pane. */
2598 input_get_bg_client(struct window_pane
*wp
)
2600 struct window
*w
= wp
->window
;
2601 struct client
*loop
;
2603 TAILQ_FOREACH(loop
, &clients
, entry
) {
2604 if (loop
->flags
& CLIENT_UNATTACHEDFLAGS
)
2606 if (loop
->session
== NULL
|| !session_has(loop
->session
, w
))
2608 if (loop
->tty
.bg
== -1)
2610 return (loop
->tty
.bg
);
2615 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2617 input_osc_10(struct input_ctx
*ictx
, const char *p
)
2619 struct window_pane
*wp
= ictx
->wp
;
2620 struct grid_cell defaults
;
2623 if (strcmp(p
, "?") == 0) {
2626 tty_default_colours(&defaults
, wp
);
2627 if (COLOUR_DEFAULT(defaults
.fg
))
2628 c
= input_get_fg_client(wp
);
2631 input_osc_colour_reply(ictx
, 10, c
);
2635 if ((c
= colour_parseX11(p
)) == -1) {
2636 log_debug("bad OSC 10: %s", p
);
2639 if (ictx
->palette
!= NULL
) {
2640 ictx
->palette
->fg
= c
;
2642 wp
->flags
|= PANE_STYLECHANGED
;
2643 screen_write_fullredraw(&ictx
->ctx
);
2647 /* Handle the OSC 110 sequence for resetting foreground colour. */
2649 input_osc_110(struct input_ctx
*ictx
, const char *p
)
2651 struct window_pane
*wp
= ictx
->wp
;
2655 if (ictx
->palette
!= NULL
) {
2656 ictx
->palette
->fg
= 8;
2658 wp
->flags
|= PANE_STYLECHANGED
;
2659 screen_write_fullredraw(&ictx
->ctx
);
2663 /* Handle the OSC 11 sequence for setting and querying background colour. */
2665 input_osc_11(struct input_ctx
*ictx
, const char *p
)
2667 struct window_pane
*wp
= ictx
->wp
;
2668 struct grid_cell defaults
;
2671 if (strcmp(p
, "?") == 0) {
2674 tty_default_colours(&defaults
, wp
);
2675 if (COLOUR_DEFAULT(defaults
.bg
))
2676 c
= input_get_bg_client(wp
);
2679 input_osc_colour_reply(ictx
, 11, c
);
2683 if ((c
= colour_parseX11(p
)) == -1) {
2684 log_debug("bad OSC 11: %s", p
);
2687 if (ictx
->palette
!= NULL
) {
2688 ictx
->palette
->bg
= c
;
2690 wp
->flags
|= PANE_STYLECHANGED
;
2691 screen_write_fullredraw(&ictx
->ctx
);
2695 /* Handle the OSC 111 sequence for resetting background colour. */
2697 input_osc_111(struct input_ctx
*ictx
, const char *p
)
2699 struct window_pane
*wp
= ictx
->wp
;
2703 if (ictx
->palette
!= NULL
) {
2704 ictx
->palette
->bg
= 8;
2706 wp
->flags
|= PANE_STYLECHANGED
;
2707 screen_write_fullredraw(&ictx
->ctx
);
2711 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2713 input_osc_12(struct input_ctx
*ictx
, const char *p
)
2715 struct window_pane
*wp
= ictx
->wp
;
2718 if (strcmp(p
, "?") == 0) {
2720 c
= ictx
->ctx
.s
->ccolour
;
2722 c
= ictx
->ctx
.s
->default_ccolour
;
2723 input_osc_colour_reply(ictx
, 12, c
);
2728 if ((c
= colour_parseX11(p
)) == -1) {
2729 log_debug("bad OSC 12: %s", p
);
2732 screen_set_cursor_colour(ictx
->ctx
.s
, c
);
2735 /* Handle the OSC 112 sequence for resetting cursor colour. */
2737 input_osc_112(struct input_ctx
*ictx
, const char *p
)
2739 if (*p
== '\0') /* no arguments allowed */
2740 screen_set_cursor_colour(ictx
->ctx
.s
, -1);
2743 /* Handle the OSC 133 sequence. */
2745 input_osc_133(struct input_ctx
*ictx
, const char *p
)
2747 struct grid
*gd
= ictx
->ctx
.s
->grid
;
2748 u_int line
= ictx
->ctx
.s
->cy
+ gd
->hsize
;
2749 struct grid_line
*gl
;
2751 if (line
> gd
->hsize
+ gd
->sy
- 1)
2753 gl
= grid_get_line(gd
, line
);
2757 gl
->flags
|= GRID_LINE_START_PROMPT
;
2762 /* Handle the OSC 52 sequence for setting the clipboard. */
2764 input_osc_52(struct input_ctx
*ictx
, const char *p
)
2766 struct window_pane
*wp
= ictx
->wp
;
2768 const char *buf
= NULL
;
2772 struct screen_write_ctx ctx
;
2773 struct paste_buffer
*pb
;
2774 const char* allow
= "cpqs01234567";
2775 char flags
[sizeof "cpqs01234567"] = "";
2780 state
= options_get_number(global_options
, "set-clipboard");
2784 if ((end
= strchr(p
, ';')) == NULL
)
2789 log_debug("%s: %s", __func__
, end
);
2791 for (i
= 0; p
+ i
!= end
; i
++) {
2792 if (strchr(allow
, p
[i
]) != NULL
&& strchr(flags
, p
[i
]) == NULL
)
2795 log_debug("%s: %.*s %s", __func__
, (int)(end
- p
- 1), p
, flags
);
2797 if (strcmp(end
, "?") == 0) {
2798 if ((pb
= paste_get_top(NULL
)) != NULL
)
2799 buf
= paste_buffer_data(pb
, &len
);
2800 if (ictx
->input_end
== INPUT_END_BEL
)
2801 input_reply_clipboard(ictx
->event
, buf
, len
, "\007");
2803 input_reply_clipboard(ictx
->event
, buf
, len
, "\033\\");
2807 len
= (strlen(end
) / 4) * 3;
2812 if ((outlen
= b64_pton(end
, out
, len
)) == -1) {
2817 screen_write_start_pane(&ctx
, wp
, NULL
);
2818 screen_write_setselection(&ctx
, flags
, out
, outlen
);
2819 screen_write_stop(&ctx
);
2820 notify_pane("pane-set-clipboard", wp
);
2822 paste_add(NULL
, out
, outlen
);
2825 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2827 input_osc_104(struct input_ctx
*ictx
, const char *p
)
2831 int bad
= 0, redraw
= 0;
2834 colour_palette_clear(ictx
->palette
);
2835 screen_write_fullredraw(&ictx
->ctx
);
2839 copy
= s
= xstrdup(p
);
2840 while (*s
!= '\0') {
2841 idx
= strtol(s
, &s
, 10);
2842 if (*s
!= '\0' && *s
!= ';') {
2846 if (idx
< 0 || idx
>= 256) {
2850 if (colour_palette_set(ictx
->palette
, idx
, -1))
2856 log_debug("bad OSC 104: %s", p
);
2858 screen_write_fullredraw(&ictx
->ctx
);
2863 input_reply_clipboard(struct bufferevent
*bev
, const char *buf
, size_t len
,
2869 if (buf
!= NULL
&& len
!= 0) {
2870 if (len
>= ((size_t)INT_MAX
* 3 / 4) - 1)
2872 outlen
= 4 * ((len
+ 2) / 3) + 1;
2873 out
= xmalloc(outlen
);
2874 if ((outlen
= b64_ntop(buf
, len
, out
, outlen
)) == -1) {
2880 bufferevent_write(bev
, "\033]52;;", 6);
2882 bufferevent_write(bev
, out
, outlen
);
2883 bufferevent_write(bev
, end
, strlen(end
));