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 *);
148 /* Transition entry/exit handlers. */
149 static void input_clear(struct input_ctx
*);
150 static void input_ground(struct input_ctx
*);
151 static void input_enter_dcs(struct input_ctx
*);
152 static void input_enter_osc(struct input_ctx
*);
153 static void input_exit_osc(struct input_ctx
*);
154 static void input_enter_apc(struct input_ctx
*);
155 static void input_exit_apc(struct input_ctx
*);
156 static void input_enter_rename(struct input_ctx
*);
157 static void input_exit_rename(struct input_ctx
*);
159 /* Input state handlers. */
160 static int input_print(struct input_ctx
*);
161 static int input_intermediate(struct input_ctx
*);
162 static int input_parameter(struct input_ctx
*);
163 static int input_input(struct input_ctx
*);
164 static int input_c0_dispatch(struct input_ctx
*);
165 static int input_esc_dispatch(struct input_ctx
*);
166 static int input_csi_dispatch(struct input_ctx
*);
167 static void input_csi_dispatch_rm(struct input_ctx
*);
168 static void input_csi_dispatch_rm_private(struct input_ctx
*);
169 static void input_csi_dispatch_sm(struct input_ctx
*);
170 static void input_csi_dispatch_sm_private(struct input_ctx
*);
171 static void input_csi_dispatch_winops(struct input_ctx
*);
172 static void input_csi_dispatch_sgr_256(struct input_ctx
*, int, u_int
*);
173 static void input_csi_dispatch_sgr_rgb(struct input_ctx
*, int, u_int
*);
174 static void input_csi_dispatch_sgr(struct input_ctx
*);
175 static int input_dcs_dispatch(struct input_ctx
*);
176 static int input_top_bit_set(struct input_ctx
*);
177 static int input_end_bel(struct input_ctx
*);
179 /* Command table comparison function. */
180 static int input_table_compare(const void *, const void *);
182 /* Command table entry. */
183 struct input_table_entry
{
189 /* Escape commands. */
190 enum input_esc_type
{
208 /* Escape command table. */
209 static const struct input_table_entry input_esc_table
[] = {
210 { '0', "(", INPUT_ESC_SCSG0_ON
},
211 { '0', ")", INPUT_ESC_SCSG1_ON
},
212 { '7', "", INPUT_ESC_DECSC
},
213 { '8', "", INPUT_ESC_DECRC
},
214 { '8', "#", INPUT_ESC_DECALN
},
215 { '=', "", INPUT_ESC_DECKPAM
},
216 { '>', "", INPUT_ESC_DECKPNM
},
217 { 'B', "(", INPUT_ESC_SCSG0_OFF
},
218 { 'B', ")", INPUT_ESC_SCSG1_OFF
},
219 { 'D', "", INPUT_ESC_IND
},
220 { 'E', "", INPUT_ESC_NEL
},
221 { 'H', "", INPUT_ESC_HTS
},
222 { 'M', "", INPUT_ESC_RI
},
223 { '\\', "", INPUT_ESC_ST
},
224 { 'c', "", INPUT_ESC_RIS
},
227 /* Control (CSI) commands. */
228 enum input_csi_type
{
255 INPUT_CSI_RM_PRIVATE
,
260 INPUT_CSI_SM_PRIVATE
,
268 /* Control (CSI) command table. */
269 static const struct input_table_entry input_csi_table
[] = {
270 { '@', "", INPUT_CSI_ICH
},
271 { 'A', "", INPUT_CSI_CUU
},
272 { 'B', "", INPUT_CSI_CUD
},
273 { 'C', "", INPUT_CSI_CUF
},
274 { 'D', "", INPUT_CSI_CUB
},
275 { 'E', "", INPUT_CSI_CNL
},
276 { 'F', "", INPUT_CSI_CPL
},
277 { 'G', "", INPUT_CSI_HPA
},
278 { 'H', "", INPUT_CSI_CUP
},
279 { 'J', "", INPUT_CSI_ED
},
280 { 'K', "", INPUT_CSI_EL
},
281 { 'L', "", INPUT_CSI_IL
},
282 { 'M', "", INPUT_CSI_DL
},
283 { 'P', "", INPUT_CSI_DCH
},
284 { 'S', "", INPUT_CSI_SU
},
285 { 'T', "", INPUT_CSI_SD
},
286 { 'X', "", INPUT_CSI_ECH
},
287 { 'Z', "", INPUT_CSI_CBT
},
288 { '`', "", INPUT_CSI_HPA
},
289 { 'b', "", INPUT_CSI_REP
},
290 { 'c', "", INPUT_CSI_DA
},
291 { 'c', ">", INPUT_CSI_DA_TWO
},
292 { 'd', "", INPUT_CSI_VPA
},
293 { 'f', "", INPUT_CSI_CUP
},
294 { 'g', "", INPUT_CSI_TBC
},
295 { 'h', "", INPUT_CSI_SM
},
296 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
297 { 'l', "", INPUT_CSI_RM
},
298 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
299 { 'm', "", INPUT_CSI_SGR
},
300 { 'm', ">", INPUT_CSI_MODSET
},
301 { 'n', "", INPUT_CSI_DSR
},
302 { 'n', ">", INPUT_CSI_MODOFF
},
303 { 'q', " ", INPUT_CSI_DECSCUSR
},
304 { 'q', ">", INPUT_CSI_XDA
},
305 { 'r', "", INPUT_CSI_DECSTBM
},
306 { 's', "", INPUT_CSI_SCP
},
307 { 't', "", INPUT_CSI_WINOPS
},
308 { 'u', "", INPUT_CSI_RCP
},
311 /* Input transition. */
312 struct input_transition
{
316 int (*handler
)(struct input_ctx
*);
317 const struct input_state
*state
;
323 void (*enter
)(struct input_ctx
*);
324 void (*exit
)(struct input_ctx
*);
325 const struct input_transition
*transitions
;
328 /* State transitions available from all states. */
329 #define INPUT_STATE_ANYWHERE \
330 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
331 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
332 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
334 /* Forward declarations of state tables. */
335 static const struct input_transition input_state_ground_table
[];
336 static const struct input_transition input_state_esc_enter_table
[];
337 static const struct input_transition input_state_esc_intermediate_table
[];
338 static const struct input_transition input_state_csi_enter_table
[];
339 static const struct input_transition input_state_csi_parameter_table
[];
340 static const struct input_transition input_state_csi_intermediate_table
[];
341 static const struct input_transition input_state_csi_ignore_table
[];
342 static const struct input_transition input_state_dcs_enter_table
[];
343 static const struct input_transition input_state_dcs_parameter_table
[];
344 static const struct input_transition input_state_dcs_intermediate_table
[];
345 static const struct input_transition input_state_dcs_handler_table
[];
346 static const struct input_transition input_state_dcs_escape_table
[];
347 static const struct input_transition input_state_dcs_ignore_table
[];
348 static const struct input_transition input_state_osc_string_table
[];
349 static const struct input_transition input_state_apc_string_table
[];
350 static const struct input_transition input_state_rename_string_table
[];
351 static const struct input_transition input_state_consume_st_table
[];
353 /* ground state definition. */
354 static const struct input_state input_state_ground
= {
357 input_state_ground_table
360 /* esc_enter state definition. */
361 static const struct input_state input_state_esc_enter
= {
364 input_state_esc_enter_table
367 /* esc_intermediate state definition. */
368 static const struct input_state input_state_esc_intermediate
= {
371 input_state_esc_intermediate_table
374 /* csi_enter state definition. */
375 static const struct input_state input_state_csi_enter
= {
378 input_state_csi_enter_table
381 /* csi_parameter state definition. */
382 static const struct input_state input_state_csi_parameter
= {
385 input_state_csi_parameter_table
388 /* csi_intermediate state definition. */
389 static const struct input_state input_state_csi_intermediate
= {
392 input_state_csi_intermediate_table
395 /* csi_ignore state definition. */
396 static const struct input_state input_state_csi_ignore
= {
399 input_state_csi_ignore_table
402 /* dcs_enter state definition. */
403 static const struct input_state input_state_dcs_enter
= {
405 input_enter_dcs
, NULL
,
406 input_state_dcs_enter_table
409 /* dcs_parameter state definition. */
410 static const struct input_state input_state_dcs_parameter
= {
413 input_state_dcs_parameter_table
416 /* dcs_intermediate state definition. */
417 static const struct input_state input_state_dcs_intermediate
= {
420 input_state_dcs_intermediate_table
423 /* dcs_handler state definition. */
424 static const struct input_state input_state_dcs_handler
= {
427 input_state_dcs_handler_table
430 /* dcs_escape state definition. */
431 static const struct input_state input_state_dcs_escape
= {
434 input_state_dcs_escape_table
437 /* dcs_ignore state definition. */
438 static const struct input_state input_state_dcs_ignore
= {
441 input_state_dcs_ignore_table
444 /* osc_string state definition. */
445 static const struct input_state input_state_osc_string
= {
447 input_enter_osc
, input_exit_osc
,
448 input_state_osc_string_table
451 /* apc_string state definition. */
452 static const struct input_state input_state_apc_string
= {
454 input_enter_apc
, input_exit_apc
,
455 input_state_apc_string_table
458 /* rename_string state definition. */
459 static const struct input_state input_state_rename_string
= {
461 input_enter_rename
, input_exit_rename
,
462 input_state_rename_string_table
465 /* consume_st state definition. */
466 static const struct input_state input_state_consume_st
= {
468 input_enter_rename
, NULL
, /* rename also waits for ST */
469 input_state_consume_st_table
472 /* ground state table. */
473 static const struct input_transition input_state_ground_table
[] = {
474 INPUT_STATE_ANYWHERE
,
476 { 0x00, 0x17, input_c0_dispatch
, NULL
},
477 { 0x19, 0x19, input_c0_dispatch
, NULL
},
478 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
479 { 0x20, 0x7e, input_print
, NULL
},
480 { 0x7f, 0x7f, NULL
, NULL
},
481 { 0x80, 0xff, input_top_bit_set
, NULL
},
483 { -1, -1, NULL
, NULL
}
486 /* esc_enter state table. */
487 static const struct input_transition input_state_esc_enter_table
[] = {
488 INPUT_STATE_ANYWHERE
,
490 { 0x00, 0x17, input_c0_dispatch
, NULL
},
491 { 0x19, 0x19, input_c0_dispatch
, NULL
},
492 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
493 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
494 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
495 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
496 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
497 { 0x58, 0x58, NULL
, &input_state_consume_st
},
498 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
499 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
500 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
501 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
502 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
503 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
504 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
505 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
506 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
507 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
508 { 0x7f, 0xff, NULL
, NULL
},
510 { -1, -1, NULL
, NULL
}
513 /* esc_intermediate state table. */
514 static const struct input_transition input_state_esc_intermediate_table
[] = {
515 INPUT_STATE_ANYWHERE
,
517 { 0x00, 0x17, input_c0_dispatch
, NULL
},
518 { 0x19, 0x19, input_c0_dispatch
, NULL
},
519 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
520 { 0x20, 0x2f, input_intermediate
, NULL
},
521 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
522 { 0x7f, 0xff, NULL
, NULL
},
524 { -1, -1, NULL
, NULL
}
527 /* csi_enter state table. */
528 static const struct input_transition input_state_csi_enter_table
[] = {
529 INPUT_STATE_ANYWHERE
,
531 { 0x00, 0x17, input_c0_dispatch
, NULL
},
532 { 0x19, 0x19, input_c0_dispatch
, NULL
},
533 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
534 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
535 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
536 { 0x3a, 0x3a, input_parameter
, &input_state_csi_parameter
},
537 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
538 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
539 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
540 { 0x7f, 0xff, NULL
, NULL
},
542 { -1, -1, NULL
, NULL
}
545 /* csi_parameter state table. */
546 static const struct input_transition input_state_csi_parameter_table
[] = {
547 INPUT_STATE_ANYWHERE
,
549 { 0x00, 0x17, input_c0_dispatch
, NULL
},
550 { 0x19, 0x19, input_c0_dispatch
, NULL
},
551 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
552 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
553 { 0x30, 0x39, input_parameter
, NULL
},
554 { 0x3a, 0x3a, input_parameter
, NULL
},
555 { 0x3b, 0x3b, input_parameter
, NULL
},
556 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
557 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
558 { 0x7f, 0xff, NULL
, NULL
},
560 { -1, -1, NULL
, NULL
}
563 /* csi_intermediate state table. */
564 static const struct input_transition input_state_csi_intermediate_table
[] = {
565 INPUT_STATE_ANYWHERE
,
567 { 0x00, 0x17, input_c0_dispatch
, NULL
},
568 { 0x19, 0x19, input_c0_dispatch
, NULL
},
569 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
570 { 0x20, 0x2f, input_intermediate
, NULL
},
571 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
572 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
573 { 0x7f, 0xff, NULL
, NULL
},
575 { -1, -1, NULL
, NULL
}
578 /* csi_ignore state table. */
579 static const struct input_transition input_state_csi_ignore_table
[] = {
580 INPUT_STATE_ANYWHERE
,
582 { 0x00, 0x17, input_c0_dispatch
, NULL
},
583 { 0x19, 0x19, input_c0_dispatch
, NULL
},
584 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
585 { 0x20, 0x3f, NULL
, NULL
},
586 { 0x40, 0x7e, NULL
, &input_state_ground
},
587 { 0x7f, 0xff, NULL
, NULL
},
589 { -1, -1, NULL
, NULL
}
592 /* dcs_enter state table. */
593 static const struct input_transition input_state_dcs_enter_table
[] = {
594 INPUT_STATE_ANYWHERE
,
596 { 0x00, 0x17, NULL
, NULL
},
597 { 0x19, 0x19, NULL
, NULL
},
598 { 0x1c, 0x1f, NULL
, NULL
},
599 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
600 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
601 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
602 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
603 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
604 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
605 { 0x7f, 0xff, NULL
, NULL
},
607 { -1, -1, NULL
, NULL
}
610 /* dcs_parameter state table. */
611 static const struct input_transition input_state_dcs_parameter_table
[] = {
612 INPUT_STATE_ANYWHERE
,
614 { 0x00, 0x17, NULL
, NULL
},
615 { 0x19, 0x19, NULL
, NULL
},
616 { 0x1c, 0x1f, NULL
, NULL
},
617 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
618 { 0x30, 0x39, input_parameter
, NULL
},
619 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
620 { 0x3b, 0x3b, input_parameter
, NULL
},
621 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
622 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
623 { 0x7f, 0xff, NULL
, NULL
},
625 { -1, -1, NULL
, NULL
}
628 /* dcs_intermediate state table. */
629 static const struct input_transition input_state_dcs_intermediate_table
[] = {
630 INPUT_STATE_ANYWHERE
,
632 { 0x00, 0x17, NULL
, NULL
},
633 { 0x19, 0x19, NULL
, NULL
},
634 { 0x1c, 0x1f, NULL
, NULL
},
635 { 0x20, 0x2f, input_intermediate
, NULL
},
636 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
637 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
638 { 0x7f, 0xff, NULL
, NULL
},
640 { -1, -1, NULL
, NULL
}
643 /* dcs_handler state table. */
644 static const struct input_transition input_state_dcs_handler_table
[] = {
645 /* No INPUT_STATE_ANYWHERE */
647 { 0x00, 0x1a, input_input
, NULL
},
648 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
649 { 0x1c, 0xff, input_input
, NULL
},
651 { -1, -1, NULL
, NULL
}
654 /* dcs_escape state table. */
655 static const struct input_transition input_state_dcs_escape_table
[] = {
656 /* No INPUT_STATE_ANYWHERE */
658 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
659 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
660 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
662 { -1, -1, NULL
, NULL
}
665 /* dcs_ignore state table. */
666 static const struct input_transition input_state_dcs_ignore_table
[] = {
667 INPUT_STATE_ANYWHERE
,
669 { 0x00, 0x17, NULL
, NULL
},
670 { 0x19, 0x19, NULL
, NULL
},
671 { 0x1c, 0x1f, NULL
, NULL
},
672 { 0x20, 0xff, NULL
, NULL
},
674 { -1, -1, NULL
, NULL
}
677 /* osc_string state table. */
678 static const struct input_transition input_state_osc_string_table
[] = {
679 INPUT_STATE_ANYWHERE
,
681 { 0x00, 0x06, NULL
, NULL
},
682 { 0x07, 0x07, input_end_bel
, &input_state_ground
},
683 { 0x08, 0x17, NULL
, NULL
},
684 { 0x19, 0x19, NULL
, NULL
},
685 { 0x1c, 0x1f, NULL
, NULL
},
686 { 0x20, 0xff, input_input
, NULL
},
688 { -1, -1, NULL
, NULL
}
691 /* apc_string state table. */
692 static const struct input_transition input_state_apc_string_table
[] = {
693 INPUT_STATE_ANYWHERE
,
695 { 0x00, 0x17, NULL
, NULL
},
696 { 0x19, 0x19, NULL
, NULL
},
697 { 0x1c, 0x1f, NULL
, NULL
},
698 { 0x20, 0xff, input_input
, NULL
},
700 { -1, -1, NULL
, NULL
}
703 /* rename_string state table. */
704 static const struct input_transition input_state_rename_string_table
[] = {
705 INPUT_STATE_ANYWHERE
,
707 { 0x00, 0x17, NULL
, NULL
},
708 { 0x19, 0x19, NULL
, NULL
},
709 { 0x1c, 0x1f, NULL
, NULL
},
710 { 0x20, 0xff, input_input
, NULL
},
712 { -1, -1, NULL
, NULL
}
715 /* consume_st state table. */
716 static const struct input_transition input_state_consume_st_table
[] = {
717 INPUT_STATE_ANYWHERE
,
719 { 0x00, 0x17, NULL
, NULL
},
720 { 0x19, 0x19, NULL
, NULL
},
721 { 0x1c, 0x1f, NULL
, NULL
},
722 { 0x20, 0xff, NULL
, NULL
},
724 { -1, -1, NULL
, NULL
}
727 /* Input table compare. */
729 input_table_compare(const void *key
, const void *value
)
731 const struct input_ctx
*ictx
= key
;
732 const struct input_table_entry
*entry
= value
;
734 if (ictx
->ch
!= entry
->ch
)
735 return (ictx
->ch
- entry
->ch
);
736 return (strcmp(ictx
->interm_buf
, entry
->interm
));
740 * Timer - if this expires then have been waiting for a terminator for too
741 * long, so reset to ground.
744 input_timer_callback(__unused
int fd
, __unused
short events
, void *arg
)
746 struct input_ctx
*ictx
= arg
;
748 log_debug("%s: %s expired" , __func__
, ictx
->state
->name
);
749 input_reset(ictx
, 0);
752 /* Start the timer. */
754 input_start_timer(struct input_ctx
*ictx
)
756 struct timeval tv
= { .tv_sec
= 5, .tv_usec
= 0 };
758 event_del(&ictx
->timer
);
759 event_add(&ictx
->timer
, &tv
);
762 /* Reset cell state to default. */
764 input_reset_cell(struct input_ctx
*ictx
)
766 memcpy(&ictx
->cell
.cell
, &grid_default_cell
, sizeof ictx
->cell
.cell
);
768 ictx
->cell
.g0set
= ictx
->cell
.g1set
= 0;
770 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
775 /* Save screen state. */
777 input_save_state(struct input_ctx
*ictx
)
779 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
780 struct screen
*s
= sctx
->s
;
782 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
783 ictx
->old_cx
= s
->cx
;
784 ictx
->old_cy
= s
->cy
;
785 ictx
->old_mode
= s
->mode
;
788 /* Restore screen state. */
790 input_restore_state(struct input_ctx
*ictx
)
792 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
794 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
795 if (ictx
->old_mode
& MODE_ORIGIN
)
796 screen_write_mode_set(sctx
, MODE_ORIGIN
);
798 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
799 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
, 0);
802 /* Initialise input parser. */
804 input_init(struct window_pane
*wp
, struct bufferevent
*bev
,
805 struct colour_palette
*palette
)
807 struct input_ctx
*ictx
;
809 ictx
= xcalloc(1, sizeof *ictx
);
812 ictx
->palette
= palette
;
814 ictx
->input_space
= INPUT_BUF_START
;
815 ictx
->input_buf
= xmalloc(INPUT_BUF_START
);
817 ictx
->since_ground
= evbuffer_new();
818 if (ictx
->since_ground
== NULL
)
819 fatalx("out of memory");
821 evtimer_set(&ictx
->timer
, input_timer_callback
, ictx
);
823 input_reset(ictx
, 0);
827 /* Destroy input parser. */
829 input_free(struct input_ctx
*ictx
)
833 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
834 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
835 free(ictx
->param_list
[i
].str
);
838 event_del(&ictx
->timer
);
840 free(ictx
->input_buf
);
841 evbuffer_free(ictx
->since_ground
);
846 /* Reset input state and clear screen. */
848 input_reset(struct input_ctx
*ictx
, int clear
)
850 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
851 struct window_pane
*wp
= ictx
->wp
;
853 input_reset_cell(ictx
);
855 if (clear
&& wp
!= NULL
) {
856 if (TAILQ_EMPTY(&wp
->modes
))
857 screen_write_start_pane(sctx
, wp
, &wp
->base
);
859 screen_write_start(sctx
, &wp
->base
);
860 screen_write_reset(sctx
);
861 screen_write_stop(sctx
);
868 ictx
->state
= &input_state_ground
;
872 /* Return pending data. */
874 input_pending(struct input_ctx
*ictx
)
876 return (ictx
->since_ground
);
879 /* Change input state. */
881 input_set_state(struct input_ctx
*ictx
, const struct input_transition
*itr
)
883 if (ictx
->state
->exit
!= NULL
)
884 ictx
->state
->exit(ictx
);
885 ictx
->state
= itr
->state
;
886 if (ictx
->state
->enter
!= NULL
)
887 ictx
->state
->enter(ictx
);
892 input_parse(struct input_ctx
*ictx
, u_char
*buf
, size_t len
)
894 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
895 const struct input_state
*state
= NULL
;
896 const struct input_transition
*itr
= NULL
;
899 /* Parse the input. */
901 ictx
->ch
= buf
[off
++];
903 /* Find the transition. */
904 if (ictx
->state
!= state
||
906 ictx
->ch
< itr
->first
||
907 ictx
->ch
> itr
->last
) {
908 itr
= ictx
->state
->transitions
;
909 while (itr
->first
!= -1 && itr
->last
!= -1) {
910 if (ictx
->ch
>= itr
->first
&&
911 ictx
->ch
<= itr
->last
)
915 if (itr
->first
== -1 || itr
->last
== -1) {
916 /* No transition? Eh? */
917 fatalx("no transition from state");
923 * Any state except print stops the current collection. This is
924 * an optimization to avoid checking if the attributes have
925 * changed for every character. It will stop unnecessarily for
926 * sequences that don't make a terminal change, but they should
929 if (itr
->handler
!= input_print
)
930 screen_write_collect_end(sctx
);
933 * Execute the handler, if any. Don't switch state if it
936 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
939 /* And switch state, if necessary. */
940 if (itr
->state
!= NULL
)
941 input_set_state(ictx
, itr
);
943 /* If not in ground state, save input. */
944 if (ictx
->state
!= &input_state_ground
)
945 evbuffer_add(ictx
->since_ground
, &ictx
->ch
, 1);
949 /* Parse input from pane. */
951 input_parse_pane(struct window_pane
*wp
)
956 new_data
= window_pane_get_new_data(wp
, &wp
->offset
, &new_size
);
957 input_parse_buffer(wp
, new_data
, new_size
);
958 window_pane_update_used_data(wp
, &wp
->offset
, new_size
);
961 /* Parse given input. */
963 input_parse_buffer(struct window_pane
*wp
, u_char
*buf
, size_t len
)
965 struct input_ctx
*ictx
= wp
->ictx
;
966 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
971 window_update_activity(wp
->window
);
972 wp
->flags
|= PANE_CHANGED
;
974 /* NULL wp if there is a mode set as don't want to update the tty. */
975 if (TAILQ_EMPTY(&wp
->modes
))
976 screen_write_start_pane(sctx
, wp
, &wp
->base
);
978 screen_write_start(sctx
, &wp
->base
);
980 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__
, wp
->id
,
981 ictx
->state
->name
, len
, (int)len
, buf
);
983 input_parse(ictx
, buf
, len
);
984 screen_write_stop(sctx
);
987 /* Parse given input for screen. */
989 input_parse_screen(struct input_ctx
*ictx
, struct screen
*s
,
990 screen_write_init_ctx_cb cb
, void *arg
, u_char
*buf
, size_t len
)
992 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
997 screen_write_start_callback(sctx
, s
, cb
, arg
);
998 input_parse(ictx
, buf
, len
);
999 screen_write_stop(sctx
);
1002 /* Split the parameter list (if any). */
1004 input_split(struct input_ctx
*ictx
)
1008 struct input_param
*ip
;
1011 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1012 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
1013 free(ictx
->param_list
[i
].str
);
1015 ictx
->param_list_len
= 0;
1017 if (ictx
->param_len
== 0)
1019 ip
= &ictx
->param_list
[0];
1021 ptr
= ictx
->param_buf
;
1022 while ((out
= strsep(&ptr
, ";")) != NULL
) {
1024 ip
->type
= INPUT_MISSING
;
1026 if (strchr(out
, ':') != NULL
) {
1027 ip
->type
= INPUT_STRING
;
1028 ip
->str
= xstrdup(out
);
1030 ip
->type
= INPUT_NUMBER
;
1031 ip
->num
= strtonum(out
, 0, INT_MAX
, &errstr
);
1036 ip
= &ictx
->param_list
[++ictx
->param_list_len
];
1037 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
1041 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1042 ip
= &ictx
->param_list
[i
];
1043 if (ip
->type
== INPUT_MISSING
)
1044 log_debug("parameter %u: missing", i
);
1045 else if (ip
->type
== INPUT_STRING
)
1046 log_debug("parameter %u: string %s", i
, ip
->str
);
1047 else if (ip
->type
== INPUT_NUMBER
)
1048 log_debug("parameter %u: number %d", i
, ip
->num
);
1054 /* Get an argument or return default value. */
1056 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
1058 struct input_param
*ip
;
1061 if (validx
>= ictx
->param_list_len
)
1063 ip
= &ictx
->param_list
[validx
];
1064 if (ip
->type
== INPUT_MISSING
)
1066 if (ip
->type
== INPUT_STRING
)
1069 if (retval
< minval
)
1074 /* Reply to terminal query. */
1076 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
1078 struct bufferevent
*bev
= ictx
->event
;
1086 xvasprintf(&reply
, fmt
, ap
);
1089 bufferevent_write(bev
, reply
, strlen(reply
));
1093 /* Clear saved state. */
1095 input_clear(struct input_ctx
*ictx
)
1097 event_del(&ictx
->timer
);
1099 *ictx
->interm_buf
= '\0';
1100 ictx
->interm_len
= 0;
1102 *ictx
->param_buf
= '\0';
1103 ictx
->param_len
= 0;
1105 *ictx
->input_buf
= '\0';
1106 ictx
->input_len
= 0;
1108 ictx
->input_end
= INPUT_END_ST
;
1110 ictx
->flags
&= ~INPUT_DISCARD
;
1113 /* Reset for ground state. */
1115 input_ground(struct input_ctx
*ictx
)
1117 event_del(&ictx
->timer
);
1118 evbuffer_drain(ictx
->since_ground
, EVBUFFER_LENGTH(ictx
->since_ground
));
1120 if (ictx
->input_space
> INPUT_BUF_START
) {
1121 ictx
->input_space
= INPUT_BUF_START
;
1122 ictx
->input_buf
= xrealloc(ictx
->input_buf
, INPUT_BUF_START
);
1126 /* Output this character to the screen. */
1128 input_print(struct input_ctx
*ictx
)
1130 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1133 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1135 set
= ictx
->cell
.set
== 0 ? ictx
->cell
.g0set
: ictx
->cell
.g1set
;
1137 ictx
->cell
.cell
.attr
|= GRID_ATTR_CHARSET
;
1139 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1141 utf8_set(&ictx
->cell
.cell
.data
, ictx
->ch
);
1142 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1143 ictx
->last
= ictx
->ch
;
1145 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1150 /* Collect intermediate string. */
1152 input_intermediate(struct input_ctx
*ictx
)
1154 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
1155 ictx
->flags
|= INPUT_DISCARD
;
1157 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
1158 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
1164 /* Collect parameter string. */
1166 input_parameter(struct input_ctx
*ictx
)
1168 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
1169 ictx
->flags
|= INPUT_DISCARD
;
1171 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
1172 ictx
->param_buf
[ictx
->param_len
] = '\0';
1178 /* Collect input string. */
1180 input_input(struct input_ctx
*ictx
)
1184 available
= ictx
->input_space
;
1185 while (ictx
->input_len
+ 1 >= available
) {
1187 if (available
> INPUT_BUF_LIMIT
) {
1188 ictx
->flags
|= INPUT_DISCARD
;
1191 ictx
->input_buf
= xrealloc(ictx
->input_buf
, available
);
1192 ictx
->input_space
= available
;
1194 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
1195 ictx
->input_buf
[ictx
->input_len
] = '\0';
1200 /* Execute C0 control sequence. */
1202 input_c0_dispatch(struct input_ctx
*ictx
)
1204 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1205 struct window_pane
*wp
= ictx
->wp
;
1206 struct screen
*s
= sctx
->s
;
1208 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1210 log_debug("%s: '%c'", __func__
, ictx
->ch
);
1213 case '\000': /* NUL */
1215 case '\007': /* BEL */
1217 alerts_queue(wp
->window
, WINDOW_BELL
);
1219 case '\010': /* BS */
1220 screen_write_backspace(sctx
);
1222 case '\011': /* HT */
1223 /* Don't tab beyond the end of the line. */
1224 if (s
->cx
>= screen_size_x(s
) - 1)
1227 /* Find the next tab point, or use the last column if none. */
1230 if (bit_test(s
->tabs
, s
->cx
))
1232 } while (s
->cx
< screen_size_x(s
) - 1);
1234 case '\012': /* LF */
1235 case '\013': /* VT */
1236 case '\014': /* FF */
1237 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1238 if (s
->mode
& MODE_CRLF
)
1239 screen_write_carriagereturn(sctx
);
1241 case '\015': /* CR */
1242 screen_write_carriagereturn(sctx
);
1244 case '\016': /* SO */
1247 case '\017': /* SI */
1251 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1259 /* Execute escape sequence. */
1261 input_esc_dispatch(struct input_ctx
*ictx
)
1263 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1264 struct screen
*s
= sctx
->s
;
1265 struct input_table_entry
*entry
;
1267 if (ictx
->flags
& INPUT_DISCARD
)
1269 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
1271 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
1272 sizeof input_esc_table
[0], input_table_compare
);
1273 if (entry
== NULL
) {
1274 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1278 switch (entry
->type
) {
1280 colour_palette_clear(ictx
->palette
);
1281 input_reset_cell(ictx
);
1282 screen_write_reset(sctx
);
1283 screen_write_fullredraw(sctx
);
1286 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1289 screen_write_carriagereturn(sctx
);
1290 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1293 if (s
->cx
< screen_size_x(s
))
1294 bit_set(s
->tabs
, s
->cx
);
1297 screen_write_reverseindex(sctx
, ictx
->cell
.cell
.bg
);
1299 case INPUT_ESC_DECKPAM
:
1300 screen_write_mode_set(sctx
, MODE_KKEYPAD
);
1302 case INPUT_ESC_DECKPNM
:
1303 screen_write_mode_clear(sctx
, MODE_KKEYPAD
);
1305 case INPUT_ESC_DECSC
:
1306 input_save_state(ictx
);
1308 case INPUT_ESC_DECRC
:
1309 input_restore_state(ictx
);
1311 case INPUT_ESC_DECALN
:
1312 screen_write_alignmenttest(sctx
);
1314 case INPUT_ESC_SCSG0_ON
:
1315 ictx
->cell
.g0set
= 1;
1317 case INPUT_ESC_SCSG0_OFF
:
1318 ictx
->cell
.g0set
= 0;
1320 case INPUT_ESC_SCSG1_ON
:
1321 ictx
->cell
.g1set
= 1;
1323 case INPUT_ESC_SCSG1_OFF
:
1324 ictx
->cell
.g1set
= 0;
1327 /* ST terminates OSC but the state transition already did it. */
1335 /* Execute control sequence. */
1337 input_csi_dispatch(struct input_ctx
*ictx
)
1339 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1340 struct screen
*s
= sctx
->s
;
1341 struct input_table_entry
*entry
;
1343 u_int cx
, bg
= ictx
->cell
.cell
.bg
;
1345 if (ictx
->flags
& INPUT_DISCARD
)
1348 log_debug("%s: '%c' \"%s\" \"%s\"",
1349 __func__
, ictx
->ch
, ictx
->interm_buf
, ictx
->param_buf
);
1351 if (input_split(ictx
) != 0)
1354 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1355 sizeof input_csi_table
[0], input_table_compare
);
1356 if (entry
== NULL
) {
1357 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1361 switch (entry
->type
) {
1363 /* Find the previous tab point, n times. */
1365 if (cx
> screen_size_x(s
) - 1)
1366 cx
= screen_size_x(s
) - 1;
1367 n
= input_get(ictx
, 0, 1, 1);
1370 while (cx
> 0 && n
-- > 0) {
1373 while (cx
> 0 && !bit_test(s
->tabs
, cx
));
1378 n
= input_get(ictx
, 0, 1, 1);
1380 screen_write_cursorleft(sctx
, n
);
1383 n
= input_get(ictx
, 0, 1, 1);
1385 screen_write_cursordown(sctx
, n
);
1388 n
= input_get(ictx
, 0, 1, 1);
1390 screen_write_cursorright(sctx
, n
);
1393 n
= input_get(ictx
, 0, 1, 1);
1394 m
= input_get(ictx
, 1, 1, 1);
1395 if (n
!= -1 && m
!= -1)
1396 screen_write_cursormove(sctx
, m
- 1, n
- 1, 1);
1398 case INPUT_CSI_MODSET
:
1399 n
= input_get(ictx
, 0, 0, 0);
1400 m
= input_get(ictx
, 1, 0, 0);
1401 if (options_get_number(global_options
, "extended-keys") == 2)
1403 if (n
== 0 || (n
== 4 && m
== 0))
1404 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1405 else if (n
== 4 && (m
== 1 || m
== 2))
1406 screen_write_mode_set(sctx
, MODE_KEXTENDED
);
1408 case INPUT_CSI_MODOFF
:
1409 n
= input_get(ictx
, 0, 0, 0);
1411 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1413 case INPUT_CSI_WINOPS
:
1414 input_csi_dispatch_winops(ictx
);
1417 n
= input_get(ictx
, 0, 1, 1);
1419 screen_write_cursorup(sctx
, n
);
1422 n
= input_get(ictx
, 0, 1, 1);
1424 screen_write_carriagereturn(sctx
);
1425 screen_write_cursordown(sctx
, n
);
1429 n
= input_get(ictx
, 0, 1, 1);
1431 screen_write_carriagereturn(sctx
);
1432 screen_write_cursorup(sctx
, n
);
1436 switch (input_get(ictx
, 0, 0, 0)) {
1440 input_reply(ictx
, "\033[?1;2c");
1443 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1447 case INPUT_CSI_DA_TWO
:
1448 switch (input_get(ictx
, 0, 0, 0)) {
1452 input_reply(ictx
, "\033[>84;0;0c");
1455 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1460 n
= input_get(ictx
, 0, 1, 1);
1462 screen_write_clearcharacter(sctx
, n
, bg
);
1465 n
= input_get(ictx
, 0, 1, 1);
1467 screen_write_deletecharacter(sctx
, n
, bg
);
1469 case INPUT_CSI_DECSTBM
:
1470 n
= input_get(ictx
, 0, 1, 1);
1471 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1472 if (n
!= -1 && m
!= -1)
1473 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1476 n
= input_get(ictx
, 0, 1, 1);
1478 screen_write_deleteline(sctx
, n
, bg
);
1481 switch (input_get(ictx
, 0, 0, 0)) {
1485 input_reply(ictx
, "\033[0n");
1488 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1491 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1496 switch (input_get(ictx
, 0, 0, 0)) {
1500 screen_write_clearendofscreen(sctx
, bg
);
1503 screen_write_clearstartofscreen(sctx
, bg
);
1506 screen_write_clearscreen(sctx
, bg
);
1509 if (input_get(ictx
, 1, 0, 0) == 0) {
1511 * Linux console extension to clear history
1512 * (for example before locking the screen).
1514 screen_write_clearhistory(sctx
);
1518 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1523 switch (input_get(ictx
, 0, 0, 0)) {
1527 screen_write_clearendofline(sctx
, bg
);
1530 screen_write_clearstartofline(sctx
, bg
);
1533 screen_write_clearline(sctx
, bg
);
1536 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1541 n
= input_get(ictx
, 0, 1, 1);
1543 screen_write_cursormove(sctx
, n
- 1, -1, 1);
1546 n
= input_get(ictx
, 0, 1, 1);
1548 screen_write_insertcharacter(sctx
, n
, bg
);
1551 n
= input_get(ictx
, 0, 1, 1);
1553 screen_write_insertline(sctx
, n
, bg
);
1556 n
= input_get(ictx
, 0, 1, 1);
1560 m
= screen_size_x(s
) - s
->cx
;
1564 if (ictx
->last
== -1)
1566 ictx
->ch
= ictx
->last
;
1568 for (i
= 0; i
< n
; i
++)
1572 input_restore_state(ictx
);
1575 input_csi_dispatch_rm(ictx
);
1577 case INPUT_CSI_RM_PRIVATE
:
1578 input_csi_dispatch_rm_private(ictx
);
1581 input_save_state(ictx
);
1584 input_csi_dispatch_sgr(ictx
);
1587 input_csi_dispatch_sm(ictx
);
1589 case INPUT_CSI_SM_PRIVATE
:
1590 input_csi_dispatch_sm_private(ictx
);
1593 n
= input_get(ictx
, 0, 1, 1);
1595 screen_write_scrollup(sctx
, n
, bg
);
1598 n
= input_get(ictx
, 0, 1, 1);
1600 screen_write_scrolldown(sctx
, n
, bg
);
1603 switch (input_get(ictx
, 0, 0, 0)) {
1607 if (s
->cx
< screen_size_x(s
))
1608 bit_clear(s
->tabs
, s
->cx
);
1611 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1614 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1619 n
= input_get(ictx
, 0, 1, 1);
1621 screen_write_cursormove(sctx
, -1, n
- 1, 1);
1623 case INPUT_CSI_DECSCUSR
:
1624 n
= input_get(ictx
, 0, 0, 0);
1626 screen_set_cursor_style(n
, &s
->cstyle
, &s
->mode
);
1629 n
= input_get(ictx
, 0, 0, 0);
1631 input_reply(ictx
, "\033P>|tmux %s\033\\", getversion());
1640 /* Handle CSI RM. */
1642 input_csi_dispatch_rm(struct input_ctx
*ictx
)
1644 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1647 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1648 switch (input_get(ictx
, i
, 0, -1)) {
1652 screen_write_mode_clear(sctx
, MODE_INSERT
);
1655 screen_write_mode_set(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1658 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1664 /* Handle CSI private RM. */
1666 input_csi_dispatch_rm_private(struct input_ctx
*ictx
)
1668 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1669 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1672 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1673 switch (input_get(ictx
, i
, 0, -1)) {
1676 case 1: /* DECCKM */
1677 screen_write_mode_clear(sctx
, MODE_KCURSOR
);
1679 case 3: /* DECCOLM */
1680 screen_write_cursormove(sctx
, 0, 0, 1);
1681 screen_write_clearscreen(sctx
, gc
->bg
);
1684 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
1685 screen_write_cursormove(sctx
, 0, 0, 1);
1687 case 7: /* DECAWM */
1688 screen_write_mode_clear(sctx
, MODE_WRAP
);
1691 screen_write_mode_clear(sctx
, MODE_CURSOR_BLINKING
);
1692 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1695 screen_write_mode_clear(sctx
, MODE_CURSOR
);
1701 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1704 screen_write_mode_clear(sctx
, MODE_FOCUSON
);
1707 screen_write_mode_clear(sctx
, MODE_MOUSE_UTF8
);
1710 screen_write_mode_clear(sctx
, MODE_MOUSE_SGR
);
1714 screen_write_alternateoff(sctx
, gc
, 0);
1717 screen_write_alternateoff(sctx
, gc
, 1);
1720 screen_write_mode_clear(sctx
, MODE_BRACKETPASTE
);
1723 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1729 /* Handle CSI SM. */
1731 input_csi_dispatch_sm(struct input_ctx
*ictx
)
1733 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1736 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1737 switch (input_get(ictx
, i
, 0, -1)) {
1741 screen_write_mode_set(sctx
, MODE_INSERT
);
1744 screen_write_mode_clear(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1747 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1753 /* Handle CSI private SM. */
1755 input_csi_dispatch_sm_private(struct input_ctx
*ictx
)
1757 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1758 struct window_pane
*wp
= ictx
->wp
;
1759 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1762 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1763 switch (input_get(ictx
, i
, 0, -1)) {
1766 case 1: /* DECCKM */
1767 screen_write_mode_set(sctx
, MODE_KCURSOR
);
1769 case 3: /* DECCOLM */
1770 screen_write_cursormove(sctx
, 0, 0, 1);
1771 screen_write_clearscreen(sctx
, ictx
->cell
.cell
.bg
);
1774 screen_write_mode_set(sctx
, MODE_ORIGIN
);
1775 screen_write_cursormove(sctx
, 0, 0, 1);
1777 case 7: /* DECAWM */
1778 screen_write_mode_set(sctx
, MODE_WRAP
);
1781 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING
);
1782 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1785 screen_write_mode_set(sctx
, MODE_CURSOR
);
1788 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1789 screen_write_mode_set(sctx
, MODE_MOUSE_STANDARD
);
1792 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1793 screen_write_mode_set(sctx
, MODE_MOUSE_BUTTON
);
1796 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1797 screen_write_mode_set(sctx
, MODE_MOUSE_ALL
);
1800 if (sctx
->s
->mode
& MODE_FOCUSON
)
1802 screen_write_mode_set(sctx
, MODE_FOCUSON
);
1805 if (!options_get_number(global_options
, "focus-events"))
1807 if (wp
->flags
& PANE_FOCUSED
)
1808 bufferevent_write(wp
->event
, "\033[I", 3);
1810 bufferevent_write(wp
->event
, "\033[O", 3);
1813 screen_write_mode_set(sctx
, MODE_MOUSE_UTF8
);
1816 screen_write_mode_set(sctx
, MODE_MOUSE_SGR
);
1820 screen_write_alternateon(sctx
, gc
, 0);
1823 screen_write_alternateon(sctx
, gc
, 1);
1826 screen_write_mode_set(sctx
, MODE_BRACKETPASTE
);
1829 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1835 /* Handle CSI window operations. */
1837 input_csi_dispatch_winops(struct input_ctx
*ictx
)
1839 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1840 struct screen
*s
= sctx
->s
;
1841 struct window_pane
*wp
= ictx
->wp
;
1842 u_int x
= screen_size_x(s
), y
= screen_size_y(s
);
1846 while ((n
= input_get(ictx
, m
, 0, -1)) != -1) {
1865 if (input_get(ictx
, m
, 0, -1) == -1)
1871 if (input_get(ictx
, m
, 0, -1) == -1)
1876 switch (input_get(ictx
, m
, 0, -1)) {
1881 screen_push_title(sctx
->s
);
1887 switch (input_get(ictx
, m
, 0, -1)) {
1892 screen_pop_title(sctx
->s
);
1895 notify_pane("pane-title-changed", wp
);
1896 server_redraw_window_borders(wp
->window
);
1897 server_status_window(wp
->window
);
1902 input_reply(ictx
, "\033[8;%u;%ut", x
, y
);
1905 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1912 /* Helper for 256 colour SGR. */
1914 input_csi_dispatch_sgr_256_do(struct input_ctx
*ictx
, int fgbg
, int c
)
1916 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1918 if (c
== -1 || c
> 255) {
1921 else if (fgbg
== 48)
1925 gc
->fg
= c
| COLOUR_FLAG_256
;
1926 else if (fgbg
== 48)
1927 gc
->bg
= c
| COLOUR_FLAG_256
;
1928 else if (fgbg
== 58)
1929 gc
->us
= c
| COLOUR_FLAG_256
;
1934 /* Handle CSI SGR for 256 colours. */
1936 input_csi_dispatch_sgr_256(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1940 c
= input_get(ictx
, (*i
) + 1, 0, -1);
1941 if (input_csi_dispatch_sgr_256_do(ictx
, fgbg
, c
))
1945 /* Helper for RGB colour SGR. */
1947 input_csi_dispatch_sgr_rgb_do(struct input_ctx
*ictx
, int fgbg
, int r
, int g
,
1950 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1952 if (r
== -1 || r
> 255)
1954 if (g
== -1 || g
> 255)
1956 if (b
== -1 || b
> 255)
1960 gc
->fg
= colour_join_rgb(r
, g
, b
);
1961 else if (fgbg
== 48)
1962 gc
->bg
= colour_join_rgb(r
, g
, b
);
1963 else if (fgbg
== 58)
1964 gc
->us
= colour_join_rgb(r
, g
, b
);
1968 /* Handle CSI SGR for RGB colours. */
1970 input_csi_dispatch_sgr_rgb(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1974 r
= input_get(ictx
, (*i
) + 1, 0, -1);
1975 g
= input_get(ictx
, (*i
) + 2, 0, -1);
1976 b
= input_get(ictx
, (*i
) + 3, 0, -1);
1977 if (input_csi_dispatch_sgr_rgb_do(ictx
, fgbg
, r
, g
, b
))
1981 /* Handle CSI SGR with a ISO parameter. */
1983 input_csi_dispatch_sgr_colon(struct input_ctx
*ictx
, u_int i
)
1985 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1986 char *s
= ictx
->param_list
[i
].str
, *copy
, *ptr
, *out
;
1991 for (n
= 0; n
< nitems(p
); n
++)
1995 ptr
= copy
= xstrdup(s
);
1996 while ((out
= strsep(&ptr
, ":")) != NULL
) {
1998 p
[n
++] = strtonum(out
, 0, INT_MAX
, &errstr
);
1999 if (errstr
!= NULL
|| n
== nitems(p
)) {
2005 if (n
== nitems(p
)) {
2010 log_debug("%s: %u = %d", __func__
, n
- 1, p
[n
- 1]);
2021 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2024 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2025 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2028 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2029 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2032 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2033 gc
->attr
|= GRID_ATTR_UNDERSCORE_3
;
2036 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2037 gc
->attr
|= GRID_ATTR_UNDERSCORE_4
;
2040 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2041 gc
->attr
|= GRID_ATTR_UNDERSCORE_5
;
2046 if (n
< 2 || (p
[0] != 38 && p
[0] != 48 && p
[0] != 58))
2058 input_csi_dispatch_sgr_rgb_do(ictx
, p
[0], p
[i
], p
[i
+ 1],
2064 input_csi_dispatch_sgr_256_do(ictx
, p
[0], p
[2]);
2069 /* Handle CSI SGR. */
2071 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
2073 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2077 if (ictx
->param_list_len
== 0) {
2078 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2082 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
2083 if (ictx
->param_list
[i
].type
== INPUT_STRING
) {
2084 input_csi_dispatch_sgr_colon(ictx
, i
);
2087 n
= input_get(ictx
, i
, 0, 0);
2091 if (n
== 38 || n
== 48 || n
== 58) {
2093 switch (input_get(ictx
, i
, 0, -1)) {
2095 input_csi_dispatch_sgr_rgb(ictx
, n
, &i
);
2098 input_csi_dispatch_sgr_256(ictx
, n
, &i
);
2106 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2109 gc
->attr
|= GRID_ATTR_BRIGHT
;
2112 gc
->attr
|= GRID_ATTR_DIM
;
2115 gc
->attr
|= GRID_ATTR_ITALICS
;
2118 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2119 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2123 gc
->attr
|= GRID_ATTR_BLINK
;
2126 gc
->attr
|= GRID_ATTR_REVERSE
;
2129 gc
->attr
|= GRID_ATTR_HIDDEN
;
2132 gc
->attr
|= GRID_ATTR_STRIKETHROUGH
;
2135 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2136 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2139 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
2142 gc
->attr
&= ~GRID_ATTR_ITALICS
;
2145 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2148 gc
->attr
&= ~GRID_ATTR_BLINK
;
2151 gc
->attr
&= ~GRID_ATTR_REVERSE
;
2154 gc
->attr
&= ~GRID_ATTR_HIDDEN
;
2157 gc
->attr
&= ~GRID_ATTR_STRIKETHROUGH
;
2186 gc
->attr
|= GRID_ATTR_OVERLINE
;
2189 gc
->attr
&= ~GRID_ATTR_OVERLINE
;
2218 /* End of input with BEL. */
2220 input_end_bel(struct input_ctx
*ictx
)
2222 log_debug("%s", __func__
);
2224 ictx
->input_end
= INPUT_END_BEL
;
2229 /* DCS string started. */
2231 input_enter_dcs(struct input_ctx
*ictx
)
2233 log_debug("%s", __func__
);
2236 input_start_timer(ictx
);
2240 /* DCS terminator (ST) received. */
2242 input_dcs_dispatch(struct input_ctx
*ictx
)
2244 struct window_pane
*wp
= ictx
->wp
;
2245 struct options
*oo
= wp
->options
;
2246 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2247 u_char
*buf
= ictx
->input_buf
;
2248 size_t len
= ictx
->input_len
;
2249 const char prefix
[] = "tmux;";
2250 const u_int prefixlen
= (sizeof prefix
) - 1;
2251 long long allow_passthrough
= 0;
2255 if (ictx
->flags
& INPUT_DISCARD
)
2257 allow_passthrough
= options_get_number(oo
, "allow-passthrough");
2258 if (!allow_passthrough
)
2260 log_debug("%s: \"%s\"", __func__
, buf
);
2262 if (len
>= prefixlen
&& strncmp(buf
, prefix
, prefixlen
) == 0) {
2263 screen_write_rawstring(sctx
, buf
+ prefixlen
, len
- prefixlen
,
2264 allow_passthrough
== 2);
2270 /* OSC string started. */
2272 input_enter_osc(struct input_ctx
*ictx
)
2274 log_debug("%s", __func__
);
2277 input_start_timer(ictx
);
2281 /* OSC terminator (ST) received. */
2283 input_exit_osc(struct input_ctx
*ictx
)
2285 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2286 struct window_pane
*wp
= ictx
->wp
;
2287 u_char
*p
= ictx
->input_buf
;
2290 if (ictx
->flags
& INPUT_DISCARD
)
2292 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
2295 log_debug("%s: \"%s\" (end %s)", __func__
, p
,
2296 ictx
->input_end
== INPUT_END_ST
? "ST" : "BEL");
2299 while (*p
>= '0' && *p
<= '9')
2300 option
= option
* 10 + *p
++ - '0';
2301 if (*p
!= ';' && *p
!= '\0')
2309 if (screen_set_title(sctx
->s
, p
) && wp
!= NULL
) {
2310 notify_pane("pane-title-changed", wp
);
2311 server_redraw_window_borders(wp
->window
);
2312 server_status_window(wp
->window
);
2316 input_osc_4(ictx
, p
);
2319 if (utf8_isvalid(p
)) {
2320 screen_set_path(sctx
->s
, p
);
2322 server_redraw_window_borders(wp
->window
);
2323 server_status_window(wp
->window
);
2328 input_osc_8(ictx
, p
);
2331 input_osc_10(ictx
, p
);
2334 input_osc_11(ictx
, p
);
2337 input_osc_12(ictx
, p
);
2340 input_osc_52(ictx
, p
);
2343 input_osc_104(ictx
, p
);
2346 input_osc_110(ictx
, p
);
2349 input_osc_111(ictx
, p
);
2352 input_osc_112(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 /* Parse colour from OSC. */
2472 input_osc_parse_colour(const char *p
)
2474 double c
, m
, y
, k
= 0;
2476 size_t len
= strlen(p
);
2480 if ((len
== 12 && sscanf(p
, "rgb:%02x/%02x/%02x", &r
, &g
, &b
) == 3) ||
2481 (len
== 7 && sscanf(p
, "#%02x%02x%02x", &r
, &g
, &b
) == 3) ||
2482 sscanf(p
, "%d,%d,%d", &r
, &g
, &b
) == 3)
2483 colour
= colour_join_rgb(r
, g
, b
);
2484 else if ((len
== 18 &&
2485 sscanf(p
, "rgb:%04x/%04x/%04x", &r
, &g
, &b
) == 3) ||
2486 (len
== 13 && sscanf(p
, "#%04x%04x%04x", &r
, &g
, &b
) == 3))
2487 colour
= colour_join_rgb(r
>> 8, g
>> 8, b
>> 8);
2488 else if ((sscanf(p
, "cmyk:%lf/%lf/%lf/%lf", &c
, &m
, &y
, &k
) == 4 ||
2489 sscanf(p
, "cmy:%lf/%lf/%lf", &c
, &m
, &y
) == 3) &&
2490 c
>= 0 && c
<= 1 && m
>= 0 && m
<= 1 &&
2491 y
>= 0 && y
<= 1 && k
>= 0 && k
<= 1) {
2492 colour
= colour_join_rgb(
2493 (1 - c
) * (1 - k
) * 255,
2494 (1 - m
) * (1 - k
) * 255,
2495 (1 - y
) * (1 - k
) * 255);
2497 while (len
!= 0 && *p
== ' ') {
2501 while (len
!= 0 && p
[len
- 1] == ' ')
2503 copy
= xstrndup(p
, len
);
2504 colour
= colour_byname(copy
);
2507 log_debug("%s: %s = %s", __func__
, p
, colour_tostring(colour
));
2511 /* Reply to a colour request. */
2513 input_osc_colour_reply(struct input_ctx
*ictx
, u_int n
, int c
)
2519 c
= colour_force_rgb(c
);
2522 colour_split_rgb(c
, &r
, &g
, &b
);
2524 if (ictx
->input_end
== INPUT_END_BEL
)
2528 input_reply(ictx
, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2529 n
, r
, r
, g
, g
, b
, b
, end
);
2532 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2534 input_osc_4(struct input_ctx
*ictx
, const char *p
)
2536 char *copy
, *s
, *next
= NULL
;
2538 int c
, bad
= 0, redraw
= 0;
2540 copy
= s
= xstrdup(p
);
2541 while (s
!= NULL
&& *s
!= '\0') {
2542 idx
= strtol(s
, &next
, 10);
2543 if (*next
++ != ';') {
2547 if (idx
< 0 || idx
>= 256) {
2552 s
= strsep(&next
, ";");
2553 if (strcmp(s
, "?") == 0) {
2554 c
= colour_palette_get(ictx
->palette
, idx
);
2556 input_osc_colour_reply(ictx
, 4, c
);
2559 if ((c
= input_osc_parse_colour(s
)) == -1) {
2563 if (colour_palette_set(ictx
->palette
, idx
, c
))
2568 log_debug("bad OSC 4: %s", p
);
2570 screen_write_fullredraw(&ictx
->ctx
);
2574 /* Handle the OSC 8 sequence for embedding hyperlinks. */
2576 input_osc_8(struct input_ctx
*ictx
, const char *p
)
2578 struct hyperlinks
*hl
= ictx
->ctx
.s
->hyperlinks
;
2579 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2580 const char *start
, *end
, *uri
;
2583 for (start
= p
; (end
= strpbrk(start
, ":;")) != NULL
; start
= end
+ 1) {
2584 if (end
- start
>= 4 && strncmp(start
, "id=", 3) == 0) {
2587 id
= xstrndup(start
+ 3, end
- start
- 3);
2590 /* The first ; is the end of parameters and start of the URI. */
2594 if (end
== NULL
|| *end
!= ';')
2602 gc
->link
= hyperlinks_put(hl
, uri
, id
);
2604 log_debug("hyperlink (anonymous) %s = %u", uri
, gc
->link
);
2606 log_debug("hyperlink (id=%s) %s = %u", id
, uri
, gc
->link
);
2611 log_debug("bad OSC 8 %s", p
);
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) {
2625 tty_default_colours(&defaults
, wp
);
2626 input_osc_colour_reply(ictx
, 10, defaults
.fg
);
2631 if ((c
= input_osc_parse_colour(p
)) == -1) {
2632 log_debug("bad OSC 10: %s", p
);
2635 if (ictx
->palette
!= NULL
) {
2636 ictx
->palette
->fg
= c
;
2638 wp
->flags
|= PANE_STYLECHANGED
;
2639 screen_write_fullredraw(&ictx
->ctx
);
2643 /* Handle the OSC 110 sequence for resetting foreground colour. */
2645 input_osc_110(struct input_ctx
*ictx
, const char *p
)
2647 struct window_pane
*wp
= ictx
->wp
;
2651 if (ictx
->palette
!= NULL
) {
2652 ictx
->palette
->fg
= 8;
2654 wp
->flags
|= PANE_STYLECHANGED
;
2655 screen_write_fullredraw(&ictx
->ctx
);
2659 /* Handle the OSC 11 sequence for setting and querying background colour. */
2661 input_osc_11(struct input_ctx
*ictx
, const char *p
)
2663 struct window_pane
*wp
= ictx
->wp
;
2664 struct grid_cell defaults
;
2667 if (strcmp(p
, "?") == 0) {
2669 tty_default_colours(&defaults
, wp
);
2670 input_osc_colour_reply(ictx
, 11, defaults
.bg
);
2675 if ((c
= input_osc_parse_colour(p
)) == -1) {
2676 log_debug("bad OSC 11: %s", p
);
2679 if (ictx
->palette
!= NULL
) {
2680 ictx
->palette
->bg
= c
;
2682 wp
->flags
|= PANE_STYLECHANGED
;
2683 screen_write_fullredraw(&ictx
->ctx
);
2687 /* Handle the OSC 111 sequence for resetting background colour. */
2689 input_osc_111(struct input_ctx
*ictx
, const char *p
)
2691 struct window_pane
*wp
= ictx
->wp
;
2695 if (ictx
->palette
!= NULL
) {
2696 ictx
->palette
->bg
= 8;
2698 wp
->flags
|= PANE_STYLECHANGED
;
2699 screen_write_fullredraw(&ictx
->ctx
);
2703 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2705 input_osc_12(struct input_ctx
*ictx
, const char *p
)
2707 struct window_pane
*wp
= ictx
->wp
;
2710 if (strcmp(p
, "?") == 0) {
2712 c
= ictx
->ctx
.s
->ccolour
;
2714 c
= ictx
->ctx
.s
->default_ccolour
;
2715 input_osc_colour_reply(ictx
, 12, c
);
2720 if ((c
= input_osc_parse_colour(p
)) == -1) {
2721 log_debug("bad OSC 12: %s", p
);
2724 screen_set_cursor_colour(ictx
->ctx
.s
, c
);
2727 /* Handle the OSC 112 sequence for resetting cursor colour. */
2729 input_osc_112(struct input_ctx
*ictx
, const char *p
)
2731 if (*p
== '\0') /* no arguments allowed */
2732 screen_set_cursor_colour(ictx
->ctx
.s
, -1);
2736 /* Handle the OSC 52 sequence for setting the clipboard. */
2738 input_osc_52(struct input_ctx
*ictx
, const char *p
)
2740 struct window_pane
*wp
= ictx
->wp
;
2742 const char *buf
= NULL
;
2746 struct screen_write_ctx ctx
;
2747 struct paste_buffer
*pb
;
2748 const char* allow
= "cpqs01234567";
2749 char flags
[sizeof "cpqs01234567"] = "";
2754 state
= options_get_number(global_options
, "set-clipboard");
2758 if ((end
= strchr(p
, ';')) == NULL
)
2763 log_debug("%s: %s", __func__
, end
);
2765 for (i
= 0; p
+ i
!= end
; i
++) {
2766 if (strchr(allow
, p
[i
]) != NULL
&& strchr(flags
, p
[i
]) == NULL
)
2769 log_debug("%s: %.*s %s", __func__
, (int)(end
- p
- 1), p
, flags
);
2771 if (strcmp(end
, "?") == 0) {
2772 if ((pb
= paste_get_top(NULL
)) != NULL
)
2773 buf
= paste_buffer_data(pb
, &len
);
2774 if (ictx
->input_end
== INPUT_END_BEL
)
2775 input_reply_clipboard(ictx
->event
, buf
, len
, "\007");
2777 input_reply_clipboard(ictx
->event
, buf
, len
, "\033\\");
2781 len
= (strlen(end
) / 4) * 3;
2786 if ((outlen
= b64_pton(end
, out
, len
)) == -1) {
2791 screen_write_start_pane(&ctx
, wp
, NULL
);
2792 screen_write_setselection(&ctx
, flags
, out
, outlen
);
2793 screen_write_stop(&ctx
);
2794 notify_pane("pane-set-clipboard", wp
);
2796 paste_add(NULL
, out
, outlen
);
2799 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2801 input_osc_104(struct input_ctx
*ictx
, const char *p
)
2805 int bad
= 0, redraw
= 0;
2808 colour_palette_clear(ictx
->palette
);
2809 screen_write_fullredraw(&ictx
->ctx
);
2813 copy
= s
= xstrdup(p
);
2814 while (*s
!= '\0') {
2815 idx
= strtol(s
, &s
, 10);
2816 if (*s
!= '\0' && *s
!= ';') {
2820 if (idx
< 0 || idx
>= 256) {
2824 if (colour_palette_set(ictx
->palette
, idx
, -1))
2830 log_debug("bad OSC 104: %s", p
);
2832 screen_write_fullredraw(&ictx
->ctx
);
2837 input_reply_clipboard(struct bufferevent
*bev
, const char *buf
, size_t len
,
2843 if (buf
!= NULL
&& len
!= 0) {
2844 outlen
= 4 * ((len
+ 2) / 3) + 1;
2845 out
= xmalloc(outlen
);
2846 if ((outlen
= b64_ntop(buf
, len
, out
, outlen
)) == -1) {
2852 bufferevent_write(bev
, "\033]52;;", 6);
2854 bufferevent_write(bev
, out
, outlen
);
2855 bufferevent_write(bev
, end
, strlen(end
));