4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <netinet/in.h>
32 * Based on the description by Paul Williams at:
34 * https://vt100.net/emu/dec_ansi_parser
36 * With the following changes:
40 * - Support for UTF-8.
42 * - OSC (but not APC) may be terminated by \007 as well as ST.
44 * - A state for APC similar to OSC. Some terminals appear to use this to set
47 * - A state for the screen \033k...\033\\ sequence to rename a window. This is
48 * pretty stupid but not supporting it is more trouble than it is worth.
50 * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
51 * be passed to the underlying terminals.
54 /* Input parser cell. */
56 struct grid_cell cell
;
58 int g0set
; /* 1 if ACS */
59 int g1set
; /* 1 if ACS */
62 /* Input parser argument. */
75 /* Input parser context. */
77 struct window_pane
*wp
;
78 struct bufferevent
*event
;
79 struct screen_write_ctx ctx
;
80 struct colour_palette
*palette
;
82 struct input_cell cell
;
84 struct input_cell old_cell
;
95 #define INPUT_BUF_START 32
96 #define INPUT_BUF_LIMIT 1048576
105 struct input_param param_list
[24];
106 u_int param_list_len
;
108 struct utf8_data utf8data
;
115 #define INPUT_DISCARD 0x1
117 const struct input_state
*state
;
122 * All input received since we were last in the ground state. Sent to
123 * control clients on connection.
125 struct evbuffer
*since_ground
;
128 /* Helper functions. */
129 struct input_transition
;
130 static int input_split(struct input_ctx
*);
131 static int input_get(struct input_ctx
*, u_int
, int, int);
132 static void printflike(2, 3) input_reply(struct input_ctx
*, const char *, ...);
133 static void input_set_state(struct input_ctx
*,
134 const struct input_transition
*);
135 static void input_reset_cell(struct input_ctx
*);
137 static void input_osc_4(struct input_ctx
*, const char *);
138 static void input_osc_10(struct input_ctx
*, const char *);
139 static void input_osc_11(struct input_ctx
*, const char *);
140 static void input_osc_12(struct input_ctx
*, const char *);
141 static void input_osc_52(struct input_ctx
*, const char *);
142 static void input_osc_104(struct input_ctx
*, const char *);
143 static void input_osc_110(struct input_ctx
*, const char *);
144 static void input_osc_111(struct input_ctx
*, const char *);
145 static void input_osc_112(struct input_ctx
*, const char *);
147 /* Transition entry/exit handlers. */
148 static void input_clear(struct input_ctx
*);
149 static void input_ground(struct input_ctx
*);
150 static void input_enter_dcs(struct input_ctx
*);
151 static void input_enter_osc(struct input_ctx
*);
152 static void input_exit_osc(struct input_ctx
*);
153 static void input_enter_apc(struct input_ctx
*);
154 static void input_exit_apc(struct input_ctx
*);
155 static void input_enter_rename(struct input_ctx
*);
156 static void input_exit_rename(struct input_ctx
*);
158 /* Input state handlers. */
159 static int input_print(struct input_ctx
*);
160 static int input_intermediate(struct input_ctx
*);
161 static int input_parameter(struct input_ctx
*);
162 static int input_input(struct input_ctx
*);
163 static int input_c0_dispatch(struct input_ctx
*);
164 static int input_esc_dispatch(struct input_ctx
*);
165 static int input_csi_dispatch(struct input_ctx
*);
166 static void input_csi_dispatch_rm(struct input_ctx
*);
167 static void input_csi_dispatch_rm_private(struct input_ctx
*);
168 static void input_csi_dispatch_sm(struct input_ctx
*);
169 static void input_csi_dispatch_sm_private(struct input_ctx
*);
170 static void input_csi_dispatch_winops(struct input_ctx
*);
171 static void input_csi_dispatch_sgr_256(struct input_ctx
*, int, u_int
*);
172 static void input_csi_dispatch_sgr_rgb(struct input_ctx
*, int, u_int
*);
173 static void input_csi_dispatch_sgr(struct input_ctx
*);
174 static int input_dcs_dispatch(struct input_ctx
*);
175 static int input_top_bit_set(struct input_ctx
*);
176 static int input_end_bel(struct input_ctx
*);
178 /* Command table comparison function. */
179 static int input_table_compare(const void *, const void *);
181 /* Command table entry. */
182 struct input_table_entry
{
188 /* Escape commands. */
189 enum input_esc_type
{
207 /* Escape command table. */
208 static const struct input_table_entry input_esc_table
[] = {
209 { '0', "(", INPUT_ESC_SCSG0_ON
},
210 { '0', ")", INPUT_ESC_SCSG1_ON
},
211 { '7', "", INPUT_ESC_DECSC
},
212 { '8', "", INPUT_ESC_DECRC
},
213 { '8', "#", INPUT_ESC_DECALN
},
214 { '=', "", INPUT_ESC_DECKPAM
},
215 { '>', "", INPUT_ESC_DECKPNM
},
216 { 'B', "(", INPUT_ESC_SCSG0_OFF
},
217 { 'B', ")", INPUT_ESC_SCSG1_OFF
},
218 { 'D', "", INPUT_ESC_IND
},
219 { 'E', "", INPUT_ESC_NEL
},
220 { 'H', "", INPUT_ESC_HTS
},
221 { 'M', "", INPUT_ESC_RI
},
222 { '\\', "", INPUT_ESC_ST
},
223 { 'c', "", INPUT_ESC_RIS
},
226 /* Control (CSI) commands. */
227 enum input_csi_type
{
254 INPUT_CSI_RM_PRIVATE
,
259 INPUT_CSI_SM_PRIVATE
,
267 /* Control (CSI) command table. */
268 static const struct input_table_entry input_csi_table
[] = {
269 { '@', "", INPUT_CSI_ICH
},
270 { 'A', "", INPUT_CSI_CUU
},
271 { 'B', "", INPUT_CSI_CUD
},
272 { 'C', "", INPUT_CSI_CUF
},
273 { 'D', "", INPUT_CSI_CUB
},
274 { 'E', "", INPUT_CSI_CNL
},
275 { 'F', "", INPUT_CSI_CPL
},
276 { 'G', "", INPUT_CSI_HPA
},
277 { 'H', "", INPUT_CSI_CUP
},
278 { 'J', "", INPUT_CSI_ED
},
279 { 'K', "", INPUT_CSI_EL
},
280 { 'L', "", INPUT_CSI_IL
},
281 { 'M', "", INPUT_CSI_DL
},
282 { 'P', "", INPUT_CSI_DCH
},
283 { 'S', "", INPUT_CSI_SU
},
284 { 'T', "", INPUT_CSI_SD
},
285 { 'X', "", INPUT_CSI_ECH
},
286 { 'Z', "", INPUT_CSI_CBT
},
287 { '`', "", INPUT_CSI_HPA
},
288 { 'b', "", INPUT_CSI_REP
},
289 { 'c', "", INPUT_CSI_DA
},
290 { 'c', ">", INPUT_CSI_DA_TWO
},
291 { 'd', "", INPUT_CSI_VPA
},
292 { 'f', "", INPUT_CSI_CUP
},
293 { 'g', "", INPUT_CSI_TBC
},
294 { 'h', "", INPUT_CSI_SM
},
295 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
296 { 'l', "", INPUT_CSI_RM
},
297 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
298 { 'm', "", INPUT_CSI_SGR
},
299 { 'm', ">", INPUT_CSI_MODSET
},
300 { 'n', "", INPUT_CSI_DSR
},
301 { 'n', ">", INPUT_CSI_MODOFF
},
302 { 'q', " ", INPUT_CSI_DECSCUSR
},
303 { 'q', ">", INPUT_CSI_XDA
},
304 { 'r', "", INPUT_CSI_DECSTBM
},
305 { 's', "", INPUT_CSI_SCP
},
306 { 't', "", INPUT_CSI_WINOPS
},
307 { 'u', "", INPUT_CSI_RCP
},
310 /* Input transition. */
311 struct input_transition
{
315 int (*handler
)(struct input_ctx
*);
316 const struct input_state
*state
;
322 void (*enter
)(struct input_ctx
*);
323 void (*exit
)(struct input_ctx
*);
324 const struct input_transition
*transitions
;
327 /* State transitions available from all states. */
328 #define INPUT_STATE_ANYWHERE \
329 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
330 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
331 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
333 /* Forward declarations of state tables. */
334 static const struct input_transition input_state_ground_table
[];
335 static const struct input_transition input_state_esc_enter_table
[];
336 static const struct input_transition input_state_esc_intermediate_table
[];
337 static const struct input_transition input_state_csi_enter_table
[];
338 static const struct input_transition input_state_csi_parameter_table
[];
339 static const struct input_transition input_state_csi_intermediate_table
[];
340 static const struct input_transition input_state_csi_ignore_table
[];
341 static const struct input_transition input_state_dcs_enter_table
[];
342 static const struct input_transition input_state_dcs_parameter_table
[];
343 static const struct input_transition input_state_dcs_intermediate_table
[];
344 static const struct input_transition input_state_dcs_handler_table
[];
345 static const struct input_transition input_state_dcs_escape_table
[];
346 static const struct input_transition input_state_dcs_ignore_table
[];
347 static const struct input_transition input_state_osc_string_table
[];
348 static const struct input_transition input_state_apc_string_table
[];
349 static const struct input_transition input_state_rename_string_table
[];
350 static const struct input_transition input_state_consume_st_table
[];
352 /* ground state definition. */
353 static const struct input_state input_state_ground
= {
356 input_state_ground_table
359 /* esc_enter state definition. */
360 static const struct input_state input_state_esc_enter
= {
363 input_state_esc_enter_table
366 /* esc_intermediate state definition. */
367 static const struct input_state input_state_esc_intermediate
= {
370 input_state_esc_intermediate_table
373 /* csi_enter state definition. */
374 static const struct input_state input_state_csi_enter
= {
377 input_state_csi_enter_table
380 /* csi_parameter state definition. */
381 static const struct input_state input_state_csi_parameter
= {
384 input_state_csi_parameter_table
387 /* csi_intermediate state definition. */
388 static const struct input_state input_state_csi_intermediate
= {
391 input_state_csi_intermediate_table
394 /* csi_ignore state definition. */
395 static const struct input_state input_state_csi_ignore
= {
398 input_state_csi_ignore_table
401 /* dcs_enter state definition. */
402 static const struct input_state input_state_dcs_enter
= {
404 input_enter_dcs
, NULL
,
405 input_state_dcs_enter_table
408 /* dcs_parameter state definition. */
409 static const struct input_state input_state_dcs_parameter
= {
412 input_state_dcs_parameter_table
415 /* dcs_intermediate state definition. */
416 static const struct input_state input_state_dcs_intermediate
= {
419 input_state_dcs_intermediate_table
422 /* dcs_handler state definition. */
423 static const struct input_state input_state_dcs_handler
= {
426 input_state_dcs_handler_table
429 /* dcs_escape state definition. */
430 static const struct input_state input_state_dcs_escape
= {
433 input_state_dcs_escape_table
436 /* dcs_ignore state definition. */
437 static const struct input_state input_state_dcs_ignore
= {
440 input_state_dcs_ignore_table
443 /* osc_string state definition. */
444 static const struct input_state input_state_osc_string
= {
446 input_enter_osc
, input_exit_osc
,
447 input_state_osc_string_table
450 /* apc_string state definition. */
451 static const struct input_state input_state_apc_string
= {
453 input_enter_apc
, input_exit_apc
,
454 input_state_apc_string_table
457 /* rename_string state definition. */
458 static const struct input_state input_state_rename_string
= {
460 input_enter_rename
, input_exit_rename
,
461 input_state_rename_string_table
464 /* consume_st state definition. */
465 static const struct input_state input_state_consume_st
= {
467 input_enter_rename
, NULL
, /* rename also waits for ST */
468 input_state_consume_st_table
471 /* ground state table. */
472 static const struct input_transition input_state_ground_table
[] = {
473 INPUT_STATE_ANYWHERE
,
475 { 0x00, 0x17, input_c0_dispatch
, NULL
},
476 { 0x19, 0x19, input_c0_dispatch
, NULL
},
477 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
478 { 0x20, 0x7e, input_print
, NULL
},
479 { 0x7f, 0x7f, NULL
, NULL
},
480 { 0x80, 0xff, input_top_bit_set
, NULL
},
482 { -1, -1, NULL
, NULL
}
485 /* esc_enter state table. */
486 static const struct input_transition input_state_esc_enter_table
[] = {
487 INPUT_STATE_ANYWHERE
,
489 { 0x00, 0x17, input_c0_dispatch
, NULL
},
490 { 0x19, 0x19, input_c0_dispatch
, NULL
},
491 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
492 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
493 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
494 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
495 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
496 { 0x58, 0x58, NULL
, &input_state_consume_st
},
497 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
498 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
499 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
500 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
501 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
502 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
503 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
504 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
505 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
506 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
507 { 0x7f, 0xff, NULL
, NULL
},
509 { -1, -1, NULL
, NULL
}
512 /* esc_intermediate state table. */
513 static const struct input_transition input_state_esc_intermediate_table
[] = {
514 INPUT_STATE_ANYWHERE
,
516 { 0x00, 0x17, input_c0_dispatch
, NULL
},
517 { 0x19, 0x19, input_c0_dispatch
, NULL
},
518 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
519 { 0x20, 0x2f, input_intermediate
, NULL
},
520 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
521 { 0x7f, 0xff, NULL
, NULL
},
523 { -1, -1, NULL
, NULL
}
526 /* csi_enter state table. */
527 static const struct input_transition input_state_csi_enter_table
[] = {
528 INPUT_STATE_ANYWHERE
,
530 { 0x00, 0x17, input_c0_dispatch
, NULL
},
531 { 0x19, 0x19, input_c0_dispatch
, NULL
},
532 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
533 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
534 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
535 { 0x3a, 0x3a, input_parameter
, &input_state_csi_parameter
},
536 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
537 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
538 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
539 { 0x7f, 0xff, NULL
, NULL
},
541 { -1, -1, NULL
, NULL
}
544 /* csi_parameter state table. */
545 static const struct input_transition input_state_csi_parameter_table
[] = {
546 INPUT_STATE_ANYWHERE
,
548 { 0x00, 0x17, input_c0_dispatch
, NULL
},
549 { 0x19, 0x19, input_c0_dispatch
, NULL
},
550 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
551 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
552 { 0x30, 0x39, input_parameter
, NULL
},
553 { 0x3a, 0x3a, input_parameter
, NULL
},
554 { 0x3b, 0x3b, input_parameter
, NULL
},
555 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
556 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
557 { 0x7f, 0xff, NULL
, NULL
},
559 { -1, -1, NULL
, NULL
}
562 /* csi_intermediate state table. */
563 static const struct input_transition input_state_csi_intermediate_table
[] = {
564 INPUT_STATE_ANYWHERE
,
566 { 0x00, 0x17, input_c0_dispatch
, NULL
},
567 { 0x19, 0x19, input_c0_dispatch
, NULL
},
568 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
569 { 0x20, 0x2f, input_intermediate
, NULL
},
570 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
571 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
572 { 0x7f, 0xff, NULL
, NULL
},
574 { -1, -1, NULL
, NULL
}
577 /* csi_ignore state table. */
578 static const struct input_transition input_state_csi_ignore_table
[] = {
579 INPUT_STATE_ANYWHERE
,
581 { 0x00, 0x17, input_c0_dispatch
, NULL
},
582 { 0x19, 0x19, input_c0_dispatch
, NULL
},
583 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
584 { 0x20, 0x3f, NULL
, NULL
},
585 { 0x40, 0x7e, NULL
, &input_state_ground
},
586 { 0x7f, 0xff, NULL
, NULL
},
588 { -1, -1, NULL
, NULL
}
591 /* dcs_enter state table. */
592 static const struct input_transition input_state_dcs_enter_table
[] = {
593 INPUT_STATE_ANYWHERE
,
595 { 0x00, 0x17, NULL
, NULL
},
596 { 0x19, 0x19, NULL
, NULL
},
597 { 0x1c, 0x1f, NULL
, NULL
},
598 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
599 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
600 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
601 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
602 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
603 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
604 { 0x7f, 0xff, NULL
, NULL
},
606 { -1, -1, NULL
, NULL
}
609 /* dcs_parameter state table. */
610 static const struct input_transition input_state_dcs_parameter_table
[] = {
611 INPUT_STATE_ANYWHERE
,
613 { 0x00, 0x17, NULL
, NULL
},
614 { 0x19, 0x19, NULL
, NULL
},
615 { 0x1c, 0x1f, NULL
, NULL
},
616 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
617 { 0x30, 0x39, input_parameter
, NULL
},
618 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
619 { 0x3b, 0x3b, input_parameter
, NULL
},
620 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
621 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
622 { 0x7f, 0xff, NULL
, NULL
},
624 { -1, -1, NULL
, NULL
}
627 /* dcs_intermediate state table. */
628 static const struct input_transition input_state_dcs_intermediate_table
[] = {
629 INPUT_STATE_ANYWHERE
,
631 { 0x00, 0x17, NULL
, NULL
},
632 { 0x19, 0x19, NULL
, NULL
},
633 { 0x1c, 0x1f, NULL
, NULL
},
634 { 0x20, 0x2f, input_intermediate
, NULL
},
635 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
636 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
637 { 0x7f, 0xff, NULL
, NULL
},
639 { -1, -1, NULL
, NULL
}
642 /* dcs_handler state table. */
643 static const struct input_transition input_state_dcs_handler_table
[] = {
644 /* No INPUT_STATE_ANYWHERE */
646 { 0x00, 0x1a, input_input
, NULL
},
647 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
648 { 0x1c, 0xff, input_input
, NULL
},
650 { -1, -1, NULL
, NULL
}
653 /* dcs_escape state table. */
654 static const struct input_transition input_state_dcs_escape_table
[] = {
655 /* No INPUT_STATE_ANYWHERE */
657 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
658 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
659 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
661 { -1, -1, NULL
, NULL
}
664 /* dcs_ignore state table. */
665 static const struct input_transition input_state_dcs_ignore_table
[] = {
666 INPUT_STATE_ANYWHERE
,
668 { 0x00, 0x17, NULL
, NULL
},
669 { 0x19, 0x19, NULL
, NULL
},
670 { 0x1c, 0x1f, NULL
, NULL
},
671 { 0x20, 0xff, NULL
, NULL
},
673 { -1, -1, NULL
, NULL
}
676 /* osc_string state table. */
677 static const struct input_transition input_state_osc_string_table
[] = {
678 INPUT_STATE_ANYWHERE
,
680 { 0x00, 0x06, NULL
, NULL
},
681 { 0x07, 0x07, input_end_bel
, &input_state_ground
},
682 { 0x08, 0x17, NULL
, NULL
},
683 { 0x19, 0x19, NULL
, NULL
},
684 { 0x1c, 0x1f, NULL
, NULL
},
685 { 0x20, 0xff, input_input
, NULL
},
687 { -1, -1, NULL
, NULL
}
690 /* apc_string state table. */
691 static const struct input_transition input_state_apc_string_table
[] = {
692 INPUT_STATE_ANYWHERE
,
694 { 0x00, 0x17, NULL
, NULL
},
695 { 0x19, 0x19, NULL
, NULL
},
696 { 0x1c, 0x1f, NULL
, NULL
},
697 { 0x20, 0xff, input_input
, NULL
},
699 { -1, -1, NULL
, NULL
}
702 /* rename_string state table. */
703 static const struct input_transition input_state_rename_string_table
[] = {
704 INPUT_STATE_ANYWHERE
,
706 { 0x00, 0x17, NULL
, NULL
},
707 { 0x19, 0x19, NULL
, NULL
},
708 { 0x1c, 0x1f, NULL
, NULL
},
709 { 0x20, 0xff, input_input
, NULL
},
711 { -1, -1, NULL
, NULL
}
714 /* consume_st state table. */
715 static const struct input_transition input_state_consume_st_table
[] = {
716 INPUT_STATE_ANYWHERE
,
718 { 0x00, 0x17, NULL
, NULL
},
719 { 0x19, 0x19, NULL
, NULL
},
720 { 0x1c, 0x1f, NULL
, NULL
},
721 { 0x20, 0xff, NULL
, NULL
},
723 { -1, -1, NULL
, NULL
}
726 /* Input table compare. */
728 input_table_compare(const void *key
, const void *value
)
730 const struct input_ctx
*ictx
= key
;
731 const struct input_table_entry
*entry
= value
;
733 if (ictx
->ch
!= entry
->ch
)
734 return (ictx
->ch
- entry
->ch
);
735 return (strcmp(ictx
->interm_buf
, entry
->interm
));
739 * Timer - if this expires then have been waiting for a terminator for too
740 * long, so reset to ground.
743 input_timer_callback(__unused
int fd
, __unused
short events
, void *arg
)
745 struct input_ctx
*ictx
= arg
;
747 log_debug("%s: %s expired" , __func__
, ictx
->state
->name
);
748 input_reset(ictx
, 0);
751 /* Start the timer. */
753 input_start_timer(struct input_ctx
*ictx
)
755 struct timeval tv
= { .tv_sec
= 5, .tv_usec
= 0 };
757 event_del(&ictx
->timer
);
758 event_add(&ictx
->timer
, &tv
);
761 /* Reset cell state to default. */
763 input_reset_cell(struct input_ctx
*ictx
)
765 memcpy(&ictx
->cell
.cell
, &grid_default_cell
, sizeof ictx
->cell
.cell
);
767 ictx
->cell
.g0set
= ictx
->cell
.g1set
= 0;
769 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
774 /* Save screen state. */
776 input_save_state(struct input_ctx
*ictx
)
778 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
779 struct screen
*s
= sctx
->s
;
781 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
782 ictx
->old_cx
= s
->cx
;
783 ictx
->old_cy
= s
->cy
;
784 ictx
->old_mode
= s
->mode
;
787 /* Restore screen state. */
789 input_restore_state(struct input_ctx
*ictx
)
791 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
793 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
794 if (ictx
->old_mode
& MODE_ORIGIN
)
795 screen_write_mode_set(sctx
, MODE_ORIGIN
);
797 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
798 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
, 0);
801 /* Initialise input parser. */
803 input_init(struct window_pane
*wp
, struct bufferevent
*bev
,
804 struct colour_palette
*palette
)
806 struct input_ctx
*ictx
;
808 ictx
= xcalloc(1, sizeof *ictx
);
811 ictx
->palette
= palette
;
813 ictx
->input_space
= INPUT_BUF_START
;
814 ictx
->input_buf
= xmalloc(INPUT_BUF_START
);
816 ictx
->since_ground
= evbuffer_new();
817 if (ictx
->since_ground
== NULL
)
818 fatalx("out of memory");
820 evtimer_set(&ictx
->timer
, input_timer_callback
, ictx
);
822 input_reset(ictx
, 0);
826 /* Destroy input parser. */
828 input_free(struct input_ctx
*ictx
)
832 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
833 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
834 free(ictx
->param_list
[i
].str
);
837 event_del(&ictx
->timer
);
839 free(ictx
->input_buf
);
840 evbuffer_free(ictx
->since_ground
);
845 /* Reset input state and clear screen. */
847 input_reset(struct input_ctx
*ictx
, int clear
)
849 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
850 struct window_pane
*wp
= ictx
->wp
;
852 input_reset_cell(ictx
);
854 if (clear
&& wp
!= NULL
) {
855 if (TAILQ_EMPTY(&wp
->modes
))
856 screen_write_start_pane(sctx
, wp
, &wp
->base
);
858 screen_write_start(sctx
, &wp
->base
);
859 screen_write_reset(sctx
);
860 screen_write_stop(sctx
);
867 ictx
->state
= &input_state_ground
;
871 /* Return pending data. */
873 input_pending(struct input_ctx
*ictx
)
875 return (ictx
->since_ground
);
878 /* Change input state. */
880 input_set_state(struct input_ctx
*ictx
, const struct input_transition
*itr
)
882 if (ictx
->state
->exit
!= NULL
)
883 ictx
->state
->exit(ictx
);
884 ictx
->state
= itr
->state
;
885 if (ictx
->state
->enter
!= NULL
)
886 ictx
->state
->enter(ictx
);
891 input_parse(struct input_ctx
*ictx
, u_char
*buf
, size_t len
)
893 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
894 const struct input_state
*state
= NULL
;
895 const struct input_transition
*itr
= NULL
;
898 /* Parse the input. */
900 ictx
->ch
= buf
[off
++];
902 /* Find the transition. */
903 if (ictx
->state
!= state
||
905 ictx
->ch
< itr
->first
||
906 ictx
->ch
> itr
->last
) {
907 itr
= ictx
->state
->transitions
;
908 while (itr
->first
!= -1 && itr
->last
!= -1) {
909 if (ictx
->ch
>= itr
->first
&&
910 ictx
->ch
<= itr
->last
)
914 if (itr
->first
== -1 || itr
->last
== -1) {
915 /* No transition? Eh? */
916 fatalx("no transition from state");
922 * Any state except print stops the current collection. This is
923 * an optimization to avoid checking if the attributes have
924 * changed for every character. It will stop unnecessarily for
925 * sequences that don't make a terminal change, but they should
928 if (itr
->handler
!= input_print
)
929 screen_write_collect_end(sctx
);
932 * Execute the handler, if any. Don't switch state if it
935 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
938 /* And switch state, if necessary. */
939 if (itr
->state
!= NULL
)
940 input_set_state(ictx
, itr
);
942 /* If not in ground state, save input. */
943 if (ictx
->state
!= &input_state_ground
)
944 evbuffer_add(ictx
->since_ground
, &ictx
->ch
, 1);
948 /* Parse input from pane. */
950 input_parse_pane(struct window_pane
*wp
)
955 new_data
= window_pane_get_new_data(wp
, &wp
->offset
, &new_size
);
956 input_parse_buffer(wp
, new_data
, new_size
);
957 window_pane_update_used_data(wp
, &wp
->offset
, new_size
);
960 /* Parse given input. */
962 input_parse_buffer(struct window_pane
*wp
, u_char
*buf
, size_t len
)
964 struct input_ctx
*ictx
= wp
->ictx
;
965 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
970 window_update_activity(wp
->window
);
971 wp
->flags
|= PANE_CHANGED
;
973 /* NULL wp if there is a mode set as don't want to update the tty. */
974 if (TAILQ_EMPTY(&wp
->modes
))
975 screen_write_start_pane(sctx
, wp
, &wp
->base
);
977 screen_write_start(sctx
, &wp
->base
);
979 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__
, wp
->id
,
980 ictx
->state
->name
, len
, (int)len
, buf
);
982 input_parse(ictx
, buf
, len
);
983 screen_write_stop(sctx
);
986 /* Parse given input for screen. */
988 input_parse_screen(struct input_ctx
*ictx
, struct screen
*s
,
989 screen_write_init_ctx_cb cb
, void *arg
, u_char
*buf
, size_t len
)
991 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
996 screen_write_start_callback(sctx
, s
, cb
, arg
);
997 input_parse(ictx
, buf
, len
);
998 screen_write_stop(sctx
);
1001 /* Split the parameter list (if any). */
1003 input_split(struct input_ctx
*ictx
)
1007 struct input_param
*ip
;
1010 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1011 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
1012 free(ictx
->param_list
[i
].str
);
1014 ictx
->param_list_len
= 0;
1016 if (ictx
->param_len
== 0)
1018 ip
= &ictx
->param_list
[0];
1020 ptr
= ictx
->param_buf
;
1021 while ((out
= strsep(&ptr
, ";")) != NULL
) {
1023 ip
->type
= INPUT_MISSING
;
1025 if (strchr(out
, ':') != NULL
) {
1026 ip
->type
= INPUT_STRING
;
1027 ip
->str
= xstrdup(out
);
1029 ip
->type
= INPUT_NUMBER
;
1030 ip
->num
= strtonum(out
, 0, INT_MAX
, &errstr
);
1035 ip
= &ictx
->param_list
[++ictx
->param_list_len
];
1036 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
1040 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1041 ip
= &ictx
->param_list
[i
];
1042 if (ip
->type
== INPUT_MISSING
)
1043 log_debug("parameter %u: missing", i
);
1044 else if (ip
->type
== INPUT_STRING
)
1045 log_debug("parameter %u: string %s", i
, ip
->str
);
1046 else if (ip
->type
== INPUT_NUMBER
)
1047 log_debug("parameter %u: number %d", i
, ip
->num
);
1053 /* Get an argument or return default value. */
1055 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
1057 struct input_param
*ip
;
1060 if (validx
>= ictx
->param_list_len
)
1062 ip
= &ictx
->param_list
[validx
];
1063 if (ip
->type
== INPUT_MISSING
)
1065 if (ip
->type
== INPUT_STRING
)
1068 if (retval
< minval
)
1073 /* Reply to terminal query. */
1075 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
1077 struct bufferevent
*bev
= ictx
->event
;
1082 xvasprintf(&reply
, fmt
, ap
);
1085 bufferevent_write(bev
, reply
, strlen(reply
));
1089 /* Clear saved state. */
1091 input_clear(struct input_ctx
*ictx
)
1093 event_del(&ictx
->timer
);
1095 *ictx
->interm_buf
= '\0';
1096 ictx
->interm_len
= 0;
1098 *ictx
->param_buf
= '\0';
1099 ictx
->param_len
= 0;
1101 *ictx
->input_buf
= '\0';
1102 ictx
->input_len
= 0;
1104 ictx
->input_end
= INPUT_END_ST
;
1106 ictx
->flags
&= ~INPUT_DISCARD
;
1109 /* Reset for ground state. */
1111 input_ground(struct input_ctx
*ictx
)
1113 event_del(&ictx
->timer
);
1114 evbuffer_drain(ictx
->since_ground
, EVBUFFER_LENGTH(ictx
->since_ground
));
1116 if (ictx
->input_space
> INPUT_BUF_START
) {
1117 ictx
->input_space
= INPUT_BUF_START
;
1118 ictx
->input_buf
= xrealloc(ictx
->input_buf
, INPUT_BUF_START
);
1122 /* Output this character to the screen. */
1124 input_print(struct input_ctx
*ictx
)
1126 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1129 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1131 set
= ictx
->cell
.set
== 0 ? ictx
->cell
.g0set
: ictx
->cell
.g1set
;
1133 ictx
->cell
.cell
.attr
|= GRID_ATTR_CHARSET
;
1135 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1137 utf8_set(&ictx
->cell
.cell
.data
, ictx
->ch
);
1138 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1139 ictx
->last
= ictx
->ch
;
1141 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1146 /* Collect intermediate string. */
1148 input_intermediate(struct input_ctx
*ictx
)
1150 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
1151 ictx
->flags
|= INPUT_DISCARD
;
1153 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
1154 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
1160 /* Collect parameter string. */
1162 input_parameter(struct input_ctx
*ictx
)
1164 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
1165 ictx
->flags
|= INPUT_DISCARD
;
1167 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
1168 ictx
->param_buf
[ictx
->param_len
] = '\0';
1174 /* Collect input string. */
1176 input_input(struct input_ctx
*ictx
)
1180 available
= ictx
->input_space
;
1181 while (ictx
->input_len
+ 1 >= available
) {
1183 if (available
> INPUT_BUF_LIMIT
) {
1184 ictx
->flags
|= INPUT_DISCARD
;
1187 ictx
->input_buf
= xrealloc(ictx
->input_buf
, available
);
1188 ictx
->input_space
= available
;
1190 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
1191 ictx
->input_buf
[ictx
->input_len
] = '\0';
1196 /* Execute C0 control sequence. */
1198 input_c0_dispatch(struct input_ctx
*ictx
)
1200 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1201 struct window_pane
*wp
= ictx
->wp
;
1202 struct screen
*s
= sctx
->s
;
1204 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1206 log_debug("%s: '%c'", __func__
, ictx
->ch
);
1209 case '\000': /* NUL */
1211 case '\007': /* BEL */
1213 alerts_queue(wp
->window
, WINDOW_BELL
);
1215 case '\010': /* BS */
1216 screen_write_backspace(sctx
);
1218 case '\011': /* HT */
1219 /* Don't tab beyond the end of the line. */
1220 if (s
->cx
>= screen_size_x(s
) - 1)
1223 /* Find the next tab point, or use the last column if none. */
1226 if (bit_test(s
->tabs
, s
->cx
))
1228 } while (s
->cx
< screen_size_x(s
) - 1);
1230 case '\012': /* LF */
1231 case '\013': /* VT */
1232 case '\014': /* FF */
1233 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1234 if (s
->mode
& MODE_CRLF
)
1235 screen_write_carriagereturn(sctx
);
1237 case '\015': /* CR */
1238 screen_write_carriagereturn(sctx
);
1240 case '\016': /* SO */
1243 case '\017': /* SI */
1247 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1255 /* Execute escape sequence. */
1257 input_esc_dispatch(struct input_ctx
*ictx
)
1259 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1260 struct screen
*s
= sctx
->s
;
1261 struct input_table_entry
*entry
;
1263 if (ictx
->flags
& INPUT_DISCARD
)
1265 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
1267 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
1268 sizeof input_esc_table
[0], input_table_compare
);
1269 if (entry
== NULL
) {
1270 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1274 switch (entry
->type
) {
1276 colour_palette_clear(ictx
->palette
);
1277 input_reset_cell(ictx
);
1278 screen_write_reset(sctx
);
1279 screen_write_fullredraw(sctx
);
1282 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1285 screen_write_carriagereturn(sctx
);
1286 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1289 if (s
->cx
< screen_size_x(s
))
1290 bit_set(s
->tabs
, s
->cx
);
1293 screen_write_reverseindex(sctx
, ictx
->cell
.cell
.bg
);
1295 case INPUT_ESC_DECKPAM
:
1296 screen_write_mode_set(sctx
, MODE_KKEYPAD
);
1298 case INPUT_ESC_DECKPNM
:
1299 screen_write_mode_clear(sctx
, MODE_KKEYPAD
);
1301 case INPUT_ESC_DECSC
:
1302 input_save_state(ictx
);
1304 case INPUT_ESC_DECRC
:
1305 input_restore_state(ictx
);
1307 case INPUT_ESC_DECALN
:
1308 screen_write_alignmenttest(sctx
);
1310 case INPUT_ESC_SCSG0_ON
:
1311 ictx
->cell
.g0set
= 1;
1313 case INPUT_ESC_SCSG0_OFF
:
1314 ictx
->cell
.g0set
= 0;
1316 case INPUT_ESC_SCSG1_ON
:
1317 ictx
->cell
.g1set
= 1;
1319 case INPUT_ESC_SCSG1_OFF
:
1320 ictx
->cell
.g1set
= 0;
1323 /* ST terminates OSC but the state transition already did it. */
1331 /* Execute control sequence. */
1333 input_csi_dispatch(struct input_ctx
*ictx
)
1335 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1336 struct screen
*s
= sctx
->s
;
1337 struct input_table_entry
*entry
;
1339 u_int cx
, bg
= ictx
->cell
.cell
.bg
;
1341 if (ictx
->flags
& INPUT_DISCARD
)
1344 log_debug("%s: '%c' \"%s\" \"%s\"",
1345 __func__
, ictx
->ch
, ictx
->interm_buf
, ictx
->param_buf
);
1347 if (input_split(ictx
) != 0)
1350 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1351 sizeof input_csi_table
[0], input_table_compare
);
1352 if (entry
== NULL
) {
1353 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1357 switch (entry
->type
) {
1359 /* Find the previous tab point, n times. */
1361 if (cx
> screen_size_x(s
) - 1)
1362 cx
= screen_size_x(s
) - 1;
1363 n
= input_get(ictx
, 0, 1, 1);
1366 while (cx
> 0 && n
-- > 0) {
1369 while (cx
> 0 && !bit_test(s
->tabs
, cx
));
1374 n
= input_get(ictx
, 0, 1, 1);
1376 screen_write_cursorleft(sctx
, n
);
1379 n
= input_get(ictx
, 0, 1, 1);
1381 screen_write_cursordown(sctx
, n
);
1384 n
= input_get(ictx
, 0, 1, 1);
1386 screen_write_cursorright(sctx
, n
);
1389 n
= input_get(ictx
, 0, 1, 1);
1390 m
= input_get(ictx
, 1, 1, 1);
1391 if (n
!= -1 && m
!= -1)
1392 screen_write_cursormove(sctx
, m
- 1, n
- 1, 1);
1394 case INPUT_CSI_MODSET
:
1395 n
= input_get(ictx
, 0, 0, 0);
1396 m
= input_get(ictx
, 1, 0, 0);
1397 if (options_get_number(global_options
, "extended-keys") == 2)
1399 if (n
== 0 || (n
== 4 && m
== 0))
1400 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1401 else if (n
== 4 && (m
== 1 || m
== 2))
1402 screen_write_mode_set(sctx
, MODE_KEXTENDED
);
1404 case INPUT_CSI_MODOFF
:
1405 n
= input_get(ictx
, 0, 0, 0);
1407 screen_write_mode_clear(sctx
, MODE_KEXTENDED
);
1409 case INPUT_CSI_WINOPS
:
1410 input_csi_dispatch_winops(ictx
);
1413 n
= input_get(ictx
, 0, 1, 1);
1415 screen_write_cursorup(sctx
, n
);
1418 n
= input_get(ictx
, 0, 1, 1);
1420 screen_write_carriagereturn(sctx
);
1421 screen_write_cursordown(sctx
, n
);
1425 n
= input_get(ictx
, 0, 1, 1);
1427 screen_write_carriagereturn(sctx
);
1428 screen_write_cursorup(sctx
, n
);
1432 switch (input_get(ictx
, 0, 0, 0)) {
1436 input_reply(ictx
, "\033[?1;2c");
1439 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1443 case INPUT_CSI_DA_TWO
:
1444 switch (input_get(ictx
, 0, 0, 0)) {
1448 input_reply(ictx
, "\033[>84;0;0c");
1451 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1456 n
= input_get(ictx
, 0, 1, 1);
1458 screen_write_clearcharacter(sctx
, n
, bg
);
1461 n
= input_get(ictx
, 0, 1, 1);
1463 screen_write_deletecharacter(sctx
, n
, bg
);
1465 case INPUT_CSI_DECSTBM
:
1466 n
= input_get(ictx
, 0, 1, 1);
1467 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1468 if (n
!= -1 && m
!= -1)
1469 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1472 n
= input_get(ictx
, 0, 1, 1);
1474 screen_write_deleteline(sctx
, n
, bg
);
1477 switch (input_get(ictx
, 0, 0, 0)) {
1481 input_reply(ictx
, "\033[0n");
1484 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1487 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1492 switch (input_get(ictx
, 0, 0, 0)) {
1496 screen_write_clearendofscreen(sctx
, bg
);
1499 screen_write_clearstartofscreen(sctx
, bg
);
1502 screen_write_clearscreen(sctx
, bg
);
1505 if (input_get(ictx
, 1, 0, 0) == 0) {
1507 * Linux console extension to clear history
1508 * (for example before locking the screen).
1510 screen_write_clearhistory(sctx
);
1514 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1519 switch (input_get(ictx
, 0, 0, 0)) {
1523 screen_write_clearendofline(sctx
, bg
);
1526 screen_write_clearstartofline(sctx
, bg
);
1529 screen_write_clearline(sctx
, bg
);
1532 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1537 n
= input_get(ictx
, 0, 1, 1);
1539 screen_write_cursormove(sctx
, n
- 1, -1, 1);
1542 n
= input_get(ictx
, 0, 1, 1);
1544 screen_write_insertcharacter(sctx
, n
, bg
);
1547 n
= input_get(ictx
, 0, 1, 1);
1549 screen_write_insertline(sctx
, n
, bg
);
1552 n
= input_get(ictx
, 0, 1, 1);
1556 m
= screen_size_x(s
) - s
->cx
;
1560 if (ictx
->last
== -1)
1562 ictx
->ch
= ictx
->last
;
1564 for (i
= 0; i
< n
; i
++)
1568 input_restore_state(ictx
);
1571 input_csi_dispatch_rm(ictx
);
1573 case INPUT_CSI_RM_PRIVATE
:
1574 input_csi_dispatch_rm_private(ictx
);
1577 input_save_state(ictx
);
1580 input_csi_dispatch_sgr(ictx
);
1583 input_csi_dispatch_sm(ictx
);
1585 case INPUT_CSI_SM_PRIVATE
:
1586 input_csi_dispatch_sm_private(ictx
);
1589 n
= input_get(ictx
, 0, 1, 1);
1591 screen_write_scrollup(sctx
, n
, bg
);
1594 n
= input_get(ictx
, 0, 1, 1);
1596 screen_write_scrolldown(sctx
, n
, bg
);
1599 switch (input_get(ictx
, 0, 0, 0)) {
1603 if (s
->cx
< screen_size_x(s
))
1604 bit_clear(s
->tabs
, s
->cx
);
1607 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1610 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1615 n
= input_get(ictx
, 0, 1, 1);
1617 screen_write_cursormove(sctx
, -1, n
- 1, 1);
1619 case INPUT_CSI_DECSCUSR
:
1620 n
= input_get(ictx
, 0, 0, 0);
1622 screen_set_cursor_style(n
, &s
->cstyle
, &s
->mode
);
1625 n
= input_get(ictx
, 0, 0, 0);
1627 input_reply(ictx
, "\033P>|tmux %s\033\\", getversion());
1636 /* Handle CSI RM. */
1638 input_csi_dispatch_rm(struct input_ctx
*ictx
)
1640 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1643 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1644 switch (input_get(ictx
, i
, 0, -1)) {
1648 screen_write_mode_clear(sctx
, MODE_INSERT
);
1651 screen_write_mode_set(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1654 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1660 /* Handle CSI private RM. */
1662 input_csi_dispatch_rm_private(struct input_ctx
*ictx
)
1664 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1665 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1668 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1669 switch (input_get(ictx
, i
, 0, -1)) {
1672 case 1: /* DECCKM */
1673 screen_write_mode_clear(sctx
, MODE_KCURSOR
);
1675 case 3: /* DECCOLM */
1676 screen_write_cursormove(sctx
, 0, 0, 1);
1677 screen_write_clearscreen(sctx
, gc
->bg
);
1680 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
1681 screen_write_cursormove(sctx
, 0, 0, 1);
1683 case 7: /* DECAWM */
1684 screen_write_mode_clear(sctx
, MODE_WRAP
);
1687 screen_write_mode_clear(sctx
, MODE_CURSOR_BLINKING
);
1688 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1691 screen_write_mode_clear(sctx
, MODE_CURSOR
);
1697 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1700 screen_write_mode_clear(sctx
, MODE_FOCUSON
);
1703 screen_write_mode_clear(sctx
, MODE_MOUSE_UTF8
);
1706 screen_write_mode_clear(sctx
, MODE_MOUSE_SGR
);
1710 screen_write_alternateoff(sctx
, gc
, 0);
1713 screen_write_alternateoff(sctx
, gc
, 1);
1716 screen_write_mode_clear(sctx
, MODE_BRACKETPASTE
);
1719 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1725 /* Handle CSI SM. */
1727 input_csi_dispatch_sm(struct input_ctx
*ictx
)
1729 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1732 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1733 switch (input_get(ictx
, i
, 0, -1)) {
1737 screen_write_mode_set(sctx
, MODE_INSERT
);
1740 screen_write_mode_clear(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1743 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1749 /* Handle CSI private SM. */
1751 input_csi_dispatch_sm_private(struct input_ctx
*ictx
)
1753 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1754 struct window_pane
*wp
= ictx
->wp
;
1755 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1758 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1759 switch (input_get(ictx
, i
, 0, -1)) {
1762 case 1: /* DECCKM */
1763 screen_write_mode_set(sctx
, MODE_KCURSOR
);
1765 case 3: /* DECCOLM */
1766 screen_write_cursormove(sctx
, 0, 0, 1);
1767 screen_write_clearscreen(sctx
, ictx
->cell
.cell
.bg
);
1770 screen_write_mode_set(sctx
, MODE_ORIGIN
);
1771 screen_write_cursormove(sctx
, 0, 0, 1);
1773 case 7: /* DECAWM */
1774 screen_write_mode_set(sctx
, MODE_WRAP
);
1777 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING
);
1778 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1781 screen_write_mode_set(sctx
, MODE_CURSOR
);
1784 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1785 screen_write_mode_set(sctx
, MODE_MOUSE_STANDARD
);
1788 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1789 screen_write_mode_set(sctx
, MODE_MOUSE_BUTTON
);
1792 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1793 screen_write_mode_set(sctx
, MODE_MOUSE_ALL
);
1796 if (sctx
->s
->mode
& MODE_FOCUSON
)
1798 screen_write_mode_set(sctx
, MODE_FOCUSON
);
1801 if (wp
->flags
& PANE_FOCUSED
)
1802 bufferevent_write(wp
->event
, "\033[I", 3);
1804 bufferevent_write(wp
->event
, "\033[O", 3);
1807 screen_write_mode_set(sctx
, MODE_MOUSE_UTF8
);
1810 screen_write_mode_set(sctx
, MODE_MOUSE_SGR
);
1814 screen_write_alternateon(sctx
, gc
, 0);
1817 screen_write_alternateon(sctx
, gc
, 1);
1820 screen_write_mode_set(sctx
, MODE_BRACKETPASTE
);
1823 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1829 /* Handle CSI window operations. */
1831 input_csi_dispatch_winops(struct input_ctx
*ictx
)
1833 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1834 struct screen
*s
= sctx
->s
;
1835 struct window_pane
*wp
= ictx
->wp
;
1836 u_int x
= screen_size_x(s
), y
= screen_size_y(s
);
1840 while ((n
= input_get(ictx
, m
, 0, -1)) != -1) {
1859 if (input_get(ictx
, m
, 0, -1) == -1)
1865 if (input_get(ictx
, m
, 0, -1) == -1)
1870 switch (input_get(ictx
, m
, 0, -1)) {
1875 screen_push_title(sctx
->s
);
1881 switch (input_get(ictx
, m
, 0, -1)) {
1886 screen_pop_title(sctx
->s
);
1889 notify_pane("pane-title-changed", wp
);
1890 server_redraw_window_borders(wp
->window
);
1891 server_status_window(wp
->window
);
1896 input_reply(ictx
, "\033[8;%u;%ut", x
, y
);
1899 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1906 /* Helper for 256 colour SGR. */
1908 input_csi_dispatch_sgr_256_do(struct input_ctx
*ictx
, int fgbg
, int c
)
1910 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1912 if (c
== -1 || c
> 255) {
1915 else if (fgbg
== 48)
1919 gc
->fg
= c
| COLOUR_FLAG_256
;
1920 else if (fgbg
== 48)
1921 gc
->bg
= c
| COLOUR_FLAG_256
;
1922 else if (fgbg
== 58)
1923 gc
->us
= c
| COLOUR_FLAG_256
;
1928 /* Handle CSI SGR for 256 colours. */
1930 input_csi_dispatch_sgr_256(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1934 c
= input_get(ictx
, (*i
) + 1, 0, -1);
1935 if (input_csi_dispatch_sgr_256_do(ictx
, fgbg
, c
))
1939 /* Helper for RGB colour SGR. */
1941 input_csi_dispatch_sgr_rgb_do(struct input_ctx
*ictx
, int fgbg
, int r
, int g
,
1944 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1946 if (r
== -1 || r
> 255)
1948 if (g
== -1 || g
> 255)
1950 if (b
== -1 || b
> 255)
1954 gc
->fg
= colour_join_rgb(r
, g
, b
);
1955 else if (fgbg
== 48)
1956 gc
->bg
= colour_join_rgb(r
, g
, b
);
1957 else if (fgbg
== 58)
1958 gc
->us
= colour_join_rgb(r
, g
, b
);
1962 /* Handle CSI SGR for RGB colours. */
1964 input_csi_dispatch_sgr_rgb(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
1968 r
= input_get(ictx
, (*i
) + 1, 0, -1);
1969 g
= input_get(ictx
, (*i
) + 2, 0, -1);
1970 b
= input_get(ictx
, (*i
) + 3, 0, -1);
1971 if (input_csi_dispatch_sgr_rgb_do(ictx
, fgbg
, r
, g
, b
))
1975 /* Handle CSI SGR with a ISO parameter. */
1977 input_csi_dispatch_sgr_colon(struct input_ctx
*ictx
, u_int i
)
1979 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1980 char *s
= ictx
->param_list
[i
].str
, *copy
, *ptr
, *out
;
1985 for (n
= 0; n
< nitems(p
); n
++)
1989 ptr
= copy
= xstrdup(s
);
1990 while ((out
= strsep(&ptr
, ":")) != NULL
) {
1992 p
[n
++] = strtonum(out
, 0, INT_MAX
, &errstr
);
1993 if (errstr
!= NULL
|| n
== nitems(p
)) {
1999 if (n
== nitems(p
)) {
2004 log_debug("%s: %u = %d", __func__
, n
- 1, p
[n
- 1]);
2015 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2018 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2019 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2022 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2023 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2026 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2027 gc
->attr
|= GRID_ATTR_UNDERSCORE_3
;
2030 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2031 gc
->attr
|= GRID_ATTR_UNDERSCORE_4
;
2034 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2035 gc
->attr
|= GRID_ATTR_UNDERSCORE_5
;
2040 if (n
< 2 || (p
[0] != 38 && p
[0] != 48 && p
[0] != 58))
2052 input_csi_dispatch_sgr_rgb_do(ictx
, p
[0], p
[i
], p
[i
+ 1],
2058 input_csi_dispatch_sgr_256_do(ictx
, p
[0], p
[2]);
2063 /* Handle CSI SGR. */
2065 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
2067 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2071 if (ictx
->param_list_len
== 0) {
2072 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2076 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
2077 if (ictx
->param_list
[i
].type
== INPUT_STRING
) {
2078 input_csi_dispatch_sgr_colon(ictx
, i
);
2081 n
= input_get(ictx
, i
, 0, 0);
2085 if (n
== 38 || n
== 48 || n
== 58) {
2087 switch (input_get(ictx
, i
, 0, -1)) {
2089 input_csi_dispatch_sgr_rgb(ictx
, n
, &i
);
2092 input_csi_dispatch_sgr_256(ictx
, n
, &i
);
2100 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2103 gc
->attr
|= GRID_ATTR_BRIGHT
;
2106 gc
->attr
|= GRID_ATTR_DIM
;
2109 gc
->attr
|= GRID_ATTR_ITALICS
;
2112 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2113 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2117 gc
->attr
|= GRID_ATTR_BLINK
;
2120 gc
->attr
|= GRID_ATTR_REVERSE
;
2123 gc
->attr
|= GRID_ATTR_HIDDEN
;
2126 gc
->attr
|= GRID_ATTR_STRIKETHROUGH
;
2129 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2130 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2133 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
2136 gc
->attr
&= ~GRID_ATTR_ITALICS
;
2139 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2142 gc
->attr
&= ~GRID_ATTR_BLINK
;
2145 gc
->attr
&= ~GRID_ATTR_REVERSE
;
2148 gc
->attr
&= ~GRID_ATTR_HIDDEN
;
2151 gc
->attr
&= ~GRID_ATTR_STRIKETHROUGH
;
2180 gc
->attr
|= GRID_ATTR_OVERLINE
;
2183 gc
->attr
&= ~GRID_ATTR_OVERLINE
;
2212 /* End of input with BEL. */
2214 input_end_bel(struct input_ctx
*ictx
)
2216 log_debug("%s", __func__
);
2218 ictx
->input_end
= INPUT_END_BEL
;
2223 /* DCS string started. */
2225 input_enter_dcs(struct input_ctx
*ictx
)
2227 log_debug("%s", __func__
);
2230 input_start_timer(ictx
);
2234 /* DCS terminator (ST) received. */
2236 input_dcs_dispatch(struct input_ctx
*ictx
)
2238 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2239 u_char
*buf
= ictx
->input_buf
;
2240 size_t len
= ictx
->input_len
;
2241 const char prefix
[] = "tmux;";
2242 const u_int prefixlen
= (sizeof prefix
) - 1;
2244 if (ictx
->flags
& INPUT_DISCARD
)
2247 log_debug("%s: \"%s\"", __func__
, buf
);
2249 if (len
>= prefixlen
&& strncmp(buf
, prefix
, prefixlen
) == 0)
2250 screen_write_rawstring(sctx
, buf
+ prefixlen
, len
- prefixlen
);
2255 /* OSC string started. */
2257 input_enter_osc(struct input_ctx
*ictx
)
2259 log_debug("%s", __func__
);
2262 input_start_timer(ictx
);
2266 /* OSC terminator (ST) received. */
2268 input_exit_osc(struct input_ctx
*ictx
)
2270 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2271 struct window_pane
*wp
= ictx
->wp
;
2272 u_char
*p
= ictx
->input_buf
;
2275 if (ictx
->flags
& INPUT_DISCARD
)
2277 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
2280 log_debug("%s: \"%s\" (end %s)", __func__
, p
,
2281 ictx
->input_end
== INPUT_END_ST
? "ST" : "BEL");
2284 while (*p
>= '0' && *p
<= '9')
2285 option
= option
* 10 + *p
++ - '0';
2292 if (screen_set_title(sctx
->s
, p
) && wp
!= NULL
) {
2293 notify_pane("pane-title-changed", wp
);
2294 server_redraw_window_borders(wp
->window
);
2295 server_status_window(wp
->window
);
2299 input_osc_4(ictx
, p
);
2302 if (utf8_isvalid(p
)) {
2303 screen_set_path(sctx
->s
, p
);
2305 server_redraw_window_borders(wp
->window
);
2306 server_status_window(wp
->window
);
2311 input_osc_10(ictx
, p
);
2314 input_osc_11(ictx
, p
);
2317 input_osc_12(ictx
, p
);
2320 input_osc_52(ictx
, p
);
2323 input_osc_104(ictx
, p
);
2326 input_osc_110(ictx
, p
);
2329 input_osc_111(ictx
, p
);
2332 input_osc_112(ictx
, p
);
2335 log_debug("%s: unknown '%u'", __func__
, option
);
2340 /* APC string started. */
2342 input_enter_apc(struct input_ctx
*ictx
)
2344 log_debug("%s", __func__
);
2347 input_start_timer(ictx
);
2351 /* APC terminator (ST) received. */
2353 input_exit_apc(struct input_ctx
*ictx
)
2355 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2356 struct window_pane
*wp
= ictx
->wp
;
2358 if (ictx
->flags
& INPUT_DISCARD
)
2360 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2362 if (screen_set_title(sctx
->s
, ictx
->input_buf
) && wp
!= NULL
) {
2363 notify_pane("pane-title-changed", wp
);
2364 server_redraw_window_borders(wp
->window
);
2365 server_status_window(wp
->window
);
2369 /* Rename string started. */
2371 input_enter_rename(struct input_ctx
*ictx
)
2373 log_debug("%s", __func__
);
2376 input_start_timer(ictx
);
2380 /* Rename terminator (ST) received. */
2382 input_exit_rename(struct input_ctx
*ictx
)
2384 struct window_pane
*wp
= ictx
->wp
;
2386 struct options_entry
*o
;
2390 if (ictx
->flags
& INPUT_DISCARD
)
2392 if (!options_get_number(ictx
->wp
->options
, "allow-rename"))
2394 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2396 if (!utf8_isvalid(ictx
->input_buf
))
2400 if (ictx
->input_len
== 0) {
2401 o
= options_get_only(w
->options
, "automatic-rename");
2403 options_remove_or_default(o
, -1, NULL
);
2404 if (!options_get_number(w
->options
, "automatic-rename"))
2405 window_set_name(w
, "");
2407 options_set_number(w
->options
, "automatic-rename", 0);
2408 window_set_name(w
, ictx
->input_buf
);
2410 server_redraw_window_borders(w
);
2411 server_status_window(w
);
2414 /* Open UTF-8 character. */
2416 input_top_bit_set(struct input_ctx
*ictx
)
2418 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2419 struct utf8_data
*ud
= &ictx
->utf8data
;
2423 if (!ictx
->utf8started
) {
2424 if (utf8_open(ud
, ictx
->ch
) != UTF8_MORE
)
2426 ictx
->utf8started
= 1;
2430 switch (utf8_append(ud
, ictx
->ch
)) {
2434 ictx
->utf8started
= 0;
2439 ictx
->utf8started
= 0;
2441 log_debug("%s %hhu '%*s' (width %hhu)", __func__
, ud
->size
,
2442 (int)ud
->size
, ud
->data
, ud
->width
);
2444 utf8_copy(&ictx
->cell
.cell
.data
, ud
);
2445 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
2450 /* Parse colour from OSC. */
2452 input_osc_parse_colour(const char *p
)
2454 double c
, m
, y
, k
= 0;
2456 size_t len
= strlen(p
);
2460 if ((len
== 12 && sscanf(p
, "rgb:%02x/%02x/%02x", &r
, &g
, &b
) == 3) ||
2461 (len
== 7 && sscanf(p
, "#%02x%02x%02x", &r
, &g
, &b
) == 3) ||
2462 sscanf(p
, "%d,%d,%d", &r
, &g
, &b
) == 3)
2463 colour
= colour_join_rgb(r
, g
, b
);
2464 else if ((len
== 18 &&
2465 sscanf(p
, "rgb:%04x/%04x/%04x", &r
, &g
, &b
) == 3) ||
2466 (len
== 13 && sscanf(p
, "#%04x%04x%04x", &r
, &g
, &b
) == 3))
2467 colour
= colour_join_rgb(r
>> 8, g
>> 8, b
>> 8);
2468 else if ((sscanf(p
, "cmyk:%lf/%lf/%lf/%lf", &c
, &m
, &y
, &k
) == 4 ||
2469 sscanf(p
, "cmy:%lf/%lf/%lf", &c
, &m
, &y
) == 3) &&
2470 c
>= 0 && c
<= 1 && m
>= 0 && m
<= 1 &&
2471 y
>= 0 && y
<= 1 && k
>= 0 && k
<= 1) {
2472 colour
= colour_join_rgb(
2473 (1 - c
) * (1 - k
) * 255,
2474 (1 - m
) * (1 - k
) * 255,
2475 (1 - y
) * (1 - k
) * 255);
2477 while (len
!= 0 && *p
== ' ') {
2481 while (len
!= 0 && p
[len
- 1] == ' ')
2483 copy
= xstrndup(p
, len
);
2484 colour
= colour_byname(copy
);
2487 log_debug("%s: %s = %s", __func__
, p
, colour_tostring(colour
));
2491 /* Reply to a colour request. */
2493 input_osc_colour_reply(struct input_ctx
*ictx
, u_int n
, int c
)
2499 c
= colour_force_rgb(c
);
2502 colour_split_rgb(c
, &r
, &g
, &b
);
2504 if (ictx
->input_end
== INPUT_END_BEL
)
2508 input_reply(ictx
, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2509 n
, r
, r
, g
, g
, b
, b
, end
);
2512 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2514 input_osc_4(struct input_ctx
*ictx
, const char *p
)
2516 char *copy
, *s
, *next
= NULL
;
2518 int c
, bad
= 0, redraw
= 0;
2520 copy
= s
= xstrdup(p
);
2521 while (s
!= NULL
&& *s
!= '\0') {
2522 idx
= strtol(s
, &next
, 10);
2523 if (*next
++ != ';') {
2527 if (idx
< 0 || idx
>= 256) {
2532 s
= strsep(&next
, ";");
2533 if (strcmp(s
, "?") == 0) {
2534 c
= colour_palette_get(ictx
->palette
, idx
);
2536 input_osc_colour_reply(ictx
, 4, c
);
2539 if ((c
= input_osc_parse_colour(s
)) == -1) {
2543 if (colour_palette_set(ictx
->palette
, idx
, c
))
2548 log_debug("bad OSC 4: %s", p
);
2550 screen_write_fullredraw(&ictx
->ctx
);
2554 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2556 input_osc_10(struct input_ctx
*ictx
, const char *p
)
2558 struct window_pane
*wp
= ictx
->wp
;
2559 struct grid_cell defaults
;
2562 if (strcmp(p
, "?") == 0) {
2564 tty_default_colours(&defaults
, wp
);
2565 input_osc_colour_reply(ictx
, 10, defaults
.fg
);
2570 if ((c
= input_osc_parse_colour(p
)) == -1) {
2571 log_debug("bad OSC 10: %s", p
);
2574 if (ictx
->palette
!= NULL
) {
2575 ictx
->palette
->fg
= c
;
2577 wp
->flags
|= PANE_STYLECHANGED
;
2578 screen_write_fullredraw(&ictx
->ctx
);
2582 /* Handle the OSC 110 sequence for resetting foreground colour. */
2584 input_osc_110(struct input_ctx
*ictx
, const char *p
)
2586 struct window_pane
*wp
= ictx
->wp
;
2590 if (ictx
->palette
!= NULL
) {
2591 ictx
->palette
->fg
= 8;
2593 wp
->flags
|= PANE_STYLECHANGED
;
2594 screen_write_fullredraw(&ictx
->ctx
);
2598 /* Handle the OSC 11 sequence for setting and querying background colour. */
2600 input_osc_11(struct input_ctx
*ictx
, const char *p
)
2602 struct window_pane
*wp
= ictx
->wp
;
2603 struct grid_cell defaults
;
2606 if (strcmp(p
, "?") == 0) {
2608 tty_default_colours(&defaults
, wp
);
2609 input_osc_colour_reply(ictx
, 11, defaults
.bg
);
2614 if ((c
= input_osc_parse_colour(p
)) == -1) {
2615 log_debug("bad OSC 11: %s", p
);
2618 if (ictx
->palette
!= NULL
) {
2619 ictx
->palette
->bg
= c
;
2621 wp
->flags
|= PANE_STYLECHANGED
;
2622 screen_write_fullredraw(&ictx
->ctx
);
2626 /* Handle the OSC 111 sequence for resetting background colour. */
2628 input_osc_111(struct input_ctx
*ictx
, const char *p
)
2630 struct window_pane
*wp
= ictx
->wp
;
2634 if (ictx
->palette
!= NULL
) {
2635 ictx
->palette
->bg
= 8;
2637 wp
->flags
|= PANE_STYLECHANGED
;
2638 screen_write_fullredraw(&ictx
->ctx
);
2642 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2644 input_osc_12(struct input_ctx
*ictx
, const char *p
)
2646 struct window_pane
*wp
= ictx
->wp
;
2649 if (strcmp(p
, "?") == 0) {
2651 c
= ictx
->ctx
.s
->ccolour
;
2653 c
= ictx
->ctx
.s
->default_ccolour
;
2654 input_osc_colour_reply(ictx
, 12, c
);
2659 if ((c
= input_osc_parse_colour(p
)) == -1) {
2660 log_debug("bad OSC 12: %s", p
);
2663 screen_set_cursor_colour(ictx
->ctx
.s
, c
);
2666 /* Handle the OSC 112 sequence for resetting cursor colour. */
2668 input_osc_112(struct input_ctx
*ictx
, const char *p
)
2670 if (*p
== '\0') /* no arguments allowed */
2671 screen_set_cursor_colour(ictx
->ctx
.s
, -1);
2675 /* Handle the OSC 52 sequence for setting the clipboard. */
2677 input_osc_52(struct input_ctx
*ictx
, const char *p
)
2679 struct window_pane
*wp
= ictx
->wp
;
2685 struct screen_write_ctx ctx
;
2686 struct paste_buffer
*pb
;
2690 state
= options_get_number(global_options
, "set-clipboard");
2694 if ((end
= strchr(p
, ';')) == NULL
)
2699 log_debug("%s: %s", __func__
, end
);
2701 if (strcmp(end
, "?") == 0) {
2702 if ((pb
= paste_get_top(NULL
)) != NULL
) {
2703 buf
= paste_buffer_data(pb
, &len
);
2704 outlen
= 4 * ((len
+ 2) / 3) + 1;
2705 out
= xmalloc(outlen
);
2706 if ((outlen
= b64_ntop(buf
, len
, out
, outlen
)) == -1) {
2714 bufferevent_write(ictx
->event
, "\033]52;;", 6);
2716 bufferevent_write(ictx
->event
, out
, outlen
);
2717 if (ictx
->input_end
== INPUT_END_BEL
)
2718 bufferevent_write(ictx
->event
, "\007", 1);
2720 bufferevent_write(ictx
->event
, "\033\\", 2);
2725 len
= (strlen(end
) / 4) * 3;
2730 if ((outlen
= b64_pton(end
, out
, len
)) == -1) {
2735 screen_write_start_pane(&ctx
, wp
, NULL
);
2736 screen_write_setselection(&ctx
, out
, outlen
);
2737 screen_write_stop(&ctx
);
2738 notify_pane("pane-set-clipboard", wp
);
2740 paste_add(NULL
, out
, outlen
);
2743 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2745 input_osc_104(struct input_ctx
*ictx
, const char *p
)
2749 int bad
= 0, redraw
= 0;
2752 colour_palette_clear(ictx
->palette
);
2753 screen_write_fullredraw(&ictx
->ctx
);
2757 copy
= s
= xstrdup(p
);
2758 while (*s
!= '\0') {
2759 idx
= strtol(s
, &s
, 10);
2760 if (*s
!= '\0' && *s
!= ';') {
2764 if (idx
< 0 || idx
>= 256) {
2768 if (colour_palette_set(ictx
->palette
, idx
, -1))
2774 log_debug("bad OSC 104: %s", p
);
2776 screen_write_fullredraw(&ictx
->ctx
);