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
;
112 struct utf8_data last
;
115 #define INPUT_DISCARD 0x1
116 #define INPUT_LAST 0x2
118 const struct input_state
*state
;
123 * All input received since we were last in the ground state. Sent to
124 * control clients on connection.
126 struct evbuffer
*since_ground
;
129 /* Helper functions. */
130 struct input_transition
;
131 static int input_split(struct input_ctx
*);
132 static int input_get(struct input_ctx
*, u_int
, int, int);
133 static void printflike(2, 3) input_reply(struct input_ctx
*, const char *, ...);
134 static void input_set_state(struct input_ctx
*,
135 const struct input_transition
*);
136 static void input_reset_cell(struct input_ctx
*);
138 static void input_osc_4(struct input_ctx
*, const char *);
139 static void input_osc_8(struct input_ctx
*, const char *);
140 static void input_osc_10(struct input_ctx
*, const char *);
141 static void input_osc_11(struct input_ctx
*, const char *);
142 static void input_osc_12(struct input_ctx
*, const char *);
143 static void input_osc_52(struct input_ctx
*, const char *);
144 static void input_osc_104(struct input_ctx
*, const char *);
145 static void input_osc_110(struct input_ctx
*, const char *);
146 static void input_osc_111(struct input_ctx
*, const char *);
147 static void input_osc_112(struct input_ctx
*, const char *);
148 static void input_osc_133(struct input_ctx
*, const char *);
150 /* Transition entry/exit handlers. */
151 static void input_clear(struct input_ctx
*);
152 static void input_ground(struct input_ctx
*);
153 static void input_enter_dcs(struct input_ctx
*);
154 static void input_enter_osc(struct input_ctx
*);
155 static void input_exit_osc(struct input_ctx
*);
156 static void input_enter_apc(struct input_ctx
*);
157 static void input_exit_apc(struct input_ctx
*);
158 static void input_enter_rename(struct input_ctx
*);
159 static void input_exit_rename(struct input_ctx
*);
161 /* Input state handlers. */
162 static int input_print(struct input_ctx
*);
163 static int input_intermediate(struct input_ctx
*);
164 static int input_parameter(struct input_ctx
*);
165 static int input_input(struct input_ctx
*);
166 static int input_c0_dispatch(struct input_ctx
*);
167 static int input_esc_dispatch(struct input_ctx
*);
168 static int input_csi_dispatch(struct input_ctx
*);
169 static void input_csi_dispatch_rm(struct input_ctx
*);
170 static void input_csi_dispatch_rm_private(struct input_ctx
*);
171 static void input_csi_dispatch_sm(struct input_ctx
*);
172 static void input_csi_dispatch_sm_private(struct input_ctx
*);
173 static void input_csi_dispatch_sm_graphics(struct input_ctx
*);
174 static void input_csi_dispatch_winops(struct input_ctx
*);
175 static void input_csi_dispatch_sgr_256(struct input_ctx
*, int, u_int
*);
176 static void input_csi_dispatch_sgr_rgb(struct input_ctx
*, int, u_int
*);
177 static void input_csi_dispatch_sgr(struct input_ctx
*);
178 static int input_dcs_dispatch(struct input_ctx
*);
179 static int input_top_bit_set(struct input_ctx
*);
180 static int input_end_bel(struct input_ctx
*);
182 /* Command table comparison function. */
183 static int input_table_compare(const void *, const void *);
185 /* Command table entry. */
186 struct input_table_entry
{
192 /* Escape commands. */
193 enum input_esc_type
{
211 /* Escape command table. */
212 static const struct input_table_entry input_esc_table
[] = {
213 { '0', "(", INPUT_ESC_SCSG0_ON
},
214 { '0', ")", INPUT_ESC_SCSG1_ON
},
215 { '7', "", INPUT_ESC_DECSC
},
216 { '8', "", INPUT_ESC_DECRC
},
217 { '8', "#", INPUT_ESC_DECALN
},
218 { '=', "", INPUT_ESC_DECKPAM
},
219 { '>', "", INPUT_ESC_DECKPNM
},
220 { 'B', "(", INPUT_ESC_SCSG0_OFF
},
221 { 'B', ")", INPUT_ESC_SCSG1_OFF
},
222 { 'D', "", INPUT_ESC_IND
},
223 { 'E', "", INPUT_ESC_NEL
},
224 { 'H', "", INPUT_ESC_HTS
},
225 { 'M', "", INPUT_ESC_RI
},
226 { '\\', "", INPUT_ESC_ST
},
227 { 'c', "", INPUT_ESC_RIS
},
230 /* Control (CSI) commands. */
231 enum input_csi_type
{
258 INPUT_CSI_RM_PRIVATE
,
263 INPUT_CSI_SM_PRIVATE
,
264 INPUT_CSI_SM_GRAPHICS
,
272 /* Control (CSI) command table. */
273 static const struct input_table_entry input_csi_table
[] = {
274 { '@', "", INPUT_CSI_ICH
},
275 { 'A', "", INPUT_CSI_CUU
},
276 { 'B', "", INPUT_CSI_CUD
},
277 { 'C', "", INPUT_CSI_CUF
},
278 { 'D', "", INPUT_CSI_CUB
},
279 { 'E', "", INPUT_CSI_CNL
},
280 { 'F', "", INPUT_CSI_CPL
},
281 { 'G', "", INPUT_CSI_HPA
},
282 { 'H', "", INPUT_CSI_CUP
},
283 { 'J', "", INPUT_CSI_ED
},
284 { 'K', "", INPUT_CSI_EL
},
285 { 'L', "", INPUT_CSI_IL
},
286 { 'M', "", INPUT_CSI_DL
},
287 { 'P', "", INPUT_CSI_DCH
},
288 { 'S', "", INPUT_CSI_SU
},
289 { 'S', "?", INPUT_CSI_SM_GRAPHICS
},
290 { 'T', "", INPUT_CSI_SD
},
291 { 'X', "", INPUT_CSI_ECH
},
292 { 'Z', "", INPUT_CSI_CBT
},
293 { '`', "", INPUT_CSI_HPA
},
294 { 'b', "", INPUT_CSI_REP
},
295 { 'c', "", INPUT_CSI_DA
},
296 { 'c', ">", INPUT_CSI_DA_TWO
},
297 { 'd', "", INPUT_CSI_VPA
},
298 { 'f', "", INPUT_CSI_CUP
},
299 { 'g', "", INPUT_CSI_TBC
},
300 { 'h', "", INPUT_CSI_SM
},
301 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
302 { 'l', "", INPUT_CSI_RM
},
303 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
304 { 'm', "", INPUT_CSI_SGR
},
305 { 'm', ">", INPUT_CSI_MODSET
},
306 { 'n', "", INPUT_CSI_DSR
},
307 { 'n', ">", INPUT_CSI_MODOFF
},
308 { 'q', " ", INPUT_CSI_DECSCUSR
},
309 { 'q', ">", INPUT_CSI_XDA
},
310 { 'r', "", INPUT_CSI_DECSTBM
},
311 { 's', "", INPUT_CSI_SCP
},
312 { 't', "", INPUT_CSI_WINOPS
},
313 { 'u', "", INPUT_CSI_RCP
}
316 /* Input transition. */
317 struct input_transition
{
321 int (*handler
)(struct input_ctx
*);
322 const struct input_state
*state
;
328 void (*enter
)(struct input_ctx
*);
329 void (*exit
)(struct input_ctx
*);
330 const struct input_transition
*transitions
;
333 /* State transitions available from all states. */
334 #define INPUT_STATE_ANYWHERE \
335 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
336 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
337 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
339 /* Forward declarations of state tables. */
340 static const struct input_transition input_state_ground_table
[];
341 static const struct input_transition input_state_esc_enter_table
[];
342 static const struct input_transition input_state_esc_intermediate_table
[];
343 static const struct input_transition input_state_csi_enter_table
[];
344 static const struct input_transition input_state_csi_parameter_table
[];
345 static const struct input_transition input_state_csi_intermediate_table
[];
346 static const struct input_transition input_state_csi_ignore_table
[];
347 static const struct input_transition input_state_dcs_enter_table
[];
348 static const struct input_transition input_state_dcs_parameter_table
[];
349 static const struct input_transition input_state_dcs_intermediate_table
[];
350 static const struct input_transition input_state_dcs_handler_table
[];
351 static const struct input_transition input_state_dcs_escape_table
[];
352 static const struct input_transition input_state_dcs_ignore_table
[];
353 static const struct input_transition input_state_osc_string_table
[];
354 static const struct input_transition input_state_apc_string_table
[];
355 static const struct input_transition input_state_rename_string_table
[];
356 static const struct input_transition input_state_consume_st_table
[];
358 /* ground state definition. */
359 static const struct input_state input_state_ground
= {
362 input_state_ground_table
365 /* esc_enter state definition. */
366 static const struct input_state input_state_esc_enter
= {
369 input_state_esc_enter_table
372 /* esc_intermediate state definition. */
373 static const struct input_state input_state_esc_intermediate
= {
376 input_state_esc_intermediate_table
379 /* csi_enter state definition. */
380 static const struct input_state input_state_csi_enter
= {
383 input_state_csi_enter_table
386 /* csi_parameter state definition. */
387 static const struct input_state input_state_csi_parameter
= {
390 input_state_csi_parameter_table
393 /* csi_intermediate state definition. */
394 static const struct input_state input_state_csi_intermediate
= {
397 input_state_csi_intermediate_table
400 /* csi_ignore state definition. */
401 static const struct input_state input_state_csi_ignore
= {
404 input_state_csi_ignore_table
407 /* dcs_enter state definition. */
408 static const struct input_state input_state_dcs_enter
= {
410 input_enter_dcs
, NULL
,
411 input_state_dcs_enter_table
414 /* dcs_parameter state definition. */
415 static const struct input_state input_state_dcs_parameter
= {
418 input_state_dcs_parameter_table
421 /* dcs_intermediate state definition. */
422 static const struct input_state input_state_dcs_intermediate
= {
425 input_state_dcs_intermediate_table
428 /* dcs_handler state definition. */
429 static const struct input_state input_state_dcs_handler
= {
432 input_state_dcs_handler_table
435 /* dcs_escape state definition. */
436 static const struct input_state input_state_dcs_escape
= {
439 input_state_dcs_escape_table
442 /* dcs_ignore state definition. */
443 static const struct input_state input_state_dcs_ignore
= {
446 input_state_dcs_ignore_table
449 /* osc_string state definition. */
450 static const struct input_state input_state_osc_string
= {
452 input_enter_osc
, input_exit_osc
,
453 input_state_osc_string_table
456 /* apc_string state definition. */
457 static const struct input_state input_state_apc_string
= {
459 input_enter_apc
, input_exit_apc
,
460 input_state_apc_string_table
463 /* rename_string state definition. */
464 static const struct input_state input_state_rename_string
= {
466 input_enter_rename
, input_exit_rename
,
467 input_state_rename_string_table
470 /* consume_st state definition. */
471 static const struct input_state input_state_consume_st
= {
473 input_enter_rename
, NULL
, /* rename also waits for ST */
474 input_state_consume_st_table
477 /* ground state table. */
478 static const struct input_transition input_state_ground_table
[] = {
479 INPUT_STATE_ANYWHERE
,
481 { 0x00, 0x17, input_c0_dispatch
, NULL
},
482 { 0x19, 0x19, input_c0_dispatch
, NULL
},
483 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
484 { 0x20, 0x7e, input_print
, NULL
},
485 { 0x7f, 0x7f, NULL
, NULL
},
486 { 0x80, 0xff, input_top_bit_set
, NULL
},
488 { -1, -1, NULL
, NULL
}
491 /* esc_enter state table. */
492 static const struct input_transition input_state_esc_enter_table
[] = {
493 INPUT_STATE_ANYWHERE
,
495 { 0x00, 0x17, input_c0_dispatch
, NULL
},
496 { 0x19, 0x19, input_c0_dispatch
, NULL
},
497 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
498 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
499 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
500 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
501 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
502 { 0x58, 0x58, NULL
, &input_state_consume_st
},
503 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
504 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
505 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
506 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
507 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
508 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
509 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
510 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
511 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
512 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
513 { 0x7f, 0xff, NULL
, NULL
},
515 { -1, -1, NULL
, NULL
}
518 /* esc_intermediate state table. */
519 static const struct input_transition input_state_esc_intermediate_table
[] = {
520 INPUT_STATE_ANYWHERE
,
522 { 0x00, 0x17, input_c0_dispatch
, NULL
},
523 { 0x19, 0x19, input_c0_dispatch
, NULL
},
524 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
525 { 0x20, 0x2f, input_intermediate
, NULL
},
526 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
527 { 0x7f, 0xff, NULL
, NULL
},
529 { -1, -1, NULL
, NULL
}
532 /* csi_enter state table. */
533 static const struct input_transition input_state_csi_enter_table
[] = {
534 INPUT_STATE_ANYWHERE
,
536 { 0x00, 0x17, input_c0_dispatch
, NULL
},
537 { 0x19, 0x19, input_c0_dispatch
, NULL
},
538 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
539 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
540 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
541 { 0x3a, 0x3a, input_parameter
, &input_state_csi_parameter
},
542 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
543 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
544 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
545 { 0x7f, 0xff, NULL
, NULL
},
547 { -1, -1, NULL
, NULL
}
550 /* csi_parameter state table. */
551 static const struct input_transition input_state_csi_parameter_table
[] = {
552 INPUT_STATE_ANYWHERE
,
554 { 0x00, 0x17, input_c0_dispatch
, NULL
},
555 { 0x19, 0x19, input_c0_dispatch
, NULL
},
556 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
557 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
558 { 0x30, 0x39, input_parameter
, NULL
},
559 { 0x3a, 0x3a, input_parameter
, NULL
},
560 { 0x3b, 0x3b, input_parameter
, NULL
},
561 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
562 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
563 { 0x7f, 0xff, NULL
, NULL
},
565 { -1, -1, NULL
, NULL
}
568 /* csi_intermediate state table. */
569 static const struct input_transition input_state_csi_intermediate_table
[] = {
570 INPUT_STATE_ANYWHERE
,
572 { 0x00, 0x17, input_c0_dispatch
, NULL
},
573 { 0x19, 0x19, input_c0_dispatch
, NULL
},
574 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
575 { 0x20, 0x2f, input_intermediate
, NULL
},
576 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
577 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
578 { 0x7f, 0xff, NULL
, NULL
},
580 { -1, -1, NULL
, NULL
}
583 /* csi_ignore state table. */
584 static const struct input_transition input_state_csi_ignore_table
[] = {
585 INPUT_STATE_ANYWHERE
,
587 { 0x00, 0x17, input_c0_dispatch
, NULL
},
588 { 0x19, 0x19, input_c0_dispatch
, NULL
},
589 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
590 { 0x20, 0x3f, NULL
, NULL
},
591 { 0x40, 0x7e, NULL
, &input_state_ground
},
592 { 0x7f, 0xff, NULL
, NULL
},
594 { -1, -1, NULL
, NULL
}
597 /* dcs_enter state table. */
598 static const struct input_transition input_state_dcs_enter_table
[] = {
599 INPUT_STATE_ANYWHERE
,
601 { 0x00, 0x17, NULL
, NULL
},
602 { 0x19, 0x19, NULL
, NULL
},
603 { 0x1c, 0x1f, NULL
, NULL
},
604 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
605 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
606 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
607 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
608 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
609 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
610 { 0x7f, 0xff, NULL
, NULL
},
612 { -1, -1, NULL
, NULL
}
615 /* dcs_parameter state table. */
616 static const struct input_transition input_state_dcs_parameter_table
[] = {
617 INPUT_STATE_ANYWHERE
,
619 { 0x00, 0x17, NULL
, NULL
},
620 { 0x19, 0x19, NULL
, NULL
},
621 { 0x1c, 0x1f, NULL
, NULL
},
622 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
623 { 0x30, 0x39, input_parameter
, NULL
},
624 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
625 { 0x3b, 0x3b, input_parameter
, NULL
},
626 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
627 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
628 { 0x7f, 0xff, NULL
, NULL
},
630 { -1, -1, NULL
, NULL
}
633 /* dcs_intermediate state table. */
634 static const struct input_transition input_state_dcs_intermediate_table
[] = {
635 INPUT_STATE_ANYWHERE
,
637 { 0x00, 0x17, NULL
, NULL
},
638 { 0x19, 0x19, NULL
, NULL
},
639 { 0x1c, 0x1f, NULL
, NULL
},
640 { 0x20, 0x2f, input_intermediate
, NULL
},
641 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
642 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
643 { 0x7f, 0xff, NULL
, NULL
},
645 { -1, -1, NULL
, NULL
}
648 /* dcs_handler state table. */
649 static const struct input_transition input_state_dcs_handler_table
[] = {
650 /* No INPUT_STATE_ANYWHERE */
652 { 0x00, 0x1a, input_input
, NULL
},
653 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
654 { 0x1c, 0xff, input_input
, NULL
},
656 { -1, -1, NULL
, NULL
}
659 /* dcs_escape state table. */
660 static const struct input_transition input_state_dcs_escape_table
[] = {
661 /* No INPUT_STATE_ANYWHERE */
663 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
664 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
665 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
667 { -1, -1, NULL
, NULL
}
670 /* dcs_ignore state table. */
671 static const struct input_transition input_state_dcs_ignore_table
[] = {
672 INPUT_STATE_ANYWHERE
,
674 { 0x00, 0x17, NULL
, NULL
},
675 { 0x19, 0x19, NULL
, NULL
},
676 { 0x1c, 0x1f, NULL
, NULL
},
677 { 0x20, 0xff, NULL
, NULL
},
679 { -1, -1, NULL
, NULL
}
682 /* osc_string state table. */
683 static const struct input_transition input_state_osc_string_table
[] = {
684 INPUT_STATE_ANYWHERE
,
686 { 0x00, 0x06, NULL
, NULL
},
687 { 0x07, 0x07, input_end_bel
, &input_state_ground
},
688 { 0x08, 0x17, NULL
, NULL
},
689 { 0x19, 0x19, NULL
, NULL
},
690 { 0x1c, 0x1f, NULL
, NULL
},
691 { 0x20, 0xff, input_input
, NULL
},
693 { -1, -1, NULL
, NULL
}
696 /* apc_string state table. */
697 static const struct input_transition input_state_apc_string_table
[] = {
698 INPUT_STATE_ANYWHERE
,
700 { 0x00, 0x17, NULL
, NULL
},
701 { 0x19, 0x19, NULL
, NULL
},
702 { 0x1c, 0x1f, NULL
, NULL
},
703 { 0x20, 0xff, input_input
, NULL
},
705 { -1, -1, NULL
, NULL
}
708 /* rename_string state table. */
709 static const struct input_transition input_state_rename_string_table
[] = {
710 INPUT_STATE_ANYWHERE
,
712 { 0x00, 0x17, NULL
, NULL
},
713 { 0x19, 0x19, NULL
, NULL
},
714 { 0x1c, 0x1f, NULL
, NULL
},
715 { 0x20, 0xff, input_input
, NULL
},
717 { -1, -1, NULL
, NULL
}
720 /* consume_st state table. */
721 static const struct input_transition input_state_consume_st_table
[] = {
722 INPUT_STATE_ANYWHERE
,
724 { 0x00, 0x17, NULL
, NULL
},
725 { 0x19, 0x19, NULL
, NULL
},
726 { 0x1c, 0x1f, NULL
, NULL
},
727 { 0x20, 0xff, NULL
, NULL
},
729 { -1, -1, NULL
, NULL
}
732 /* Input table compare. */
734 input_table_compare(const void *key
, const void *value
)
736 const struct input_ctx
*ictx
= key
;
737 const struct input_table_entry
*entry
= value
;
739 if (ictx
->ch
!= entry
->ch
)
740 return (ictx
->ch
- entry
->ch
);
741 return (strcmp(ictx
->interm_buf
, entry
->interm
));
745 * Timer - if this expires then have been waiting for a terminator for too
746 * long, so reset to ground.
749 input_timer_callback(__unused
int fd
, __unused
short events
, void *arg
)
751 struct input_ctx
*ictx
= arg
;
753 log_debug("%s: %s expired" , __func__
, ictx
->state
->name
);
754 input_reset(ictx
, 0);
757 /* Start the timer. */
759 input_start_timer(struct input_ctx
*ictx
)
761 struct timeval tv
= { .tv_sec
= 5, .tv_usec
= 0 };
763 event_del(&ictx
->timer
);
764 event_add(&ictx
->timer
, &tv
);
767 /* Reset cell state to default. */
769 input_reset_cell(struct input_ctx
*ictx
)
771 memcpy(&ictx
->cell
.cell
, &grid_default_cell
, sizeof ictx
->cell
.cell
);
773 ictx
->cell
.g0set
= ictx
->cell
.g1set
= 0;
775 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
780 /* Save screen state. */
782 input_save_state(struct input_ctx
*ictx
)
784 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
785 struct screen
*s
= sctx
->s
;
787 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
788 ictx
->old_cx
= s
->cx
;
789 ictx
->old_cy
= s
->cy
;
790 ictx
->old_mode
= s
->mode
;
793 /* Restore screen state. */
795 input_restore_state(struct input_ctx
*ictx
)
797 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
799 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
800 if (ictx
->old_mode
& MODE_ORIGIN
)
801 screen_write_mode_set(sctx
, MODE_ORIGIN
);
803 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
804 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
, 0);
807 /* Initialise input parser. */
809 input_init(struct window_pane
*wp
, struct bufferevent
*bev
,
810 struct colour_palette
*palette
)
812 struct input_ctx
*ictx
;
814 ictx
= xcalloc(1, sizeof *ictx
);
817 ictx
->palette
= palette
;
819 ictx
->input_space
= INPUT_BUF_START
;
820 ictx
->input_buf
= xmalloc(INPUT_BUF_START
);
822 ictx
->since_ground
= evbuffer_new();
823 if (ictx
->since_ground
== NULL
)
824 fatalx("out of memory");
826 evtimer_set(&ictx
->timer
, input_timer_callback
, ictx
);
828 input_reset(ictx
, 0);
832 /* Destroy input parser. */
834 input_free(struct input_ctx
*ictx
)
838 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
839 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
840 free(ictx
->param_list
[i
].str
);
843 event_del(&ictx
->timer
);
845 free(ictx
->input_buf
);
846 evbuffer_free(ictx
->since_ground
);
851 /* Reset input state and clear screen. */
853 input_reset(struct input_ctx
*ictx
, int clear
)
855 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
856 struct window_pane
*wp
= ictx
->wp
;
858 input_reset_cell(ictx
);
860 if (clear
&& wp
!= NULL
) {
861 if (TAILQ_EMPTY(&wp
->modes
))
862 screen_write_start_pane(sctx
, wp
, &wp
->base
);
864 screen_write_start(sctx
, &wp
->base
);
865 screen_write_reset(sctx
);
866 screen_write_stop(sctx
);
871 ictx
->state
= &input_state_ground
;
875 /* Return pending data. */
877 input_pending(struct input_ctx
*ictx
)
879 return (ictx
->since_ground
);
882 /* Change input state. */
884 input_set_state(struct input_ctx
*ictx
, const struct input_transition
*itr
)
886 if (ictx
->state
->exit
!= NULL
)
887 ictx
->state
->exit(ictx
);
888 ictx
->state
= itr
->state
;
889 if (ictx
->state
->enter
!= NULL
)
890 ictx
->state
->enter(ictx
);
895 input_parse(struct input_ctx
*ictx
, u_char
*buf
, size_t len
)
897 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
898 const struct input_state
*state
= NULL
;
899 const struct input_transition
*itr
= NULL
;
902 /* Parse the input. */
904 ictx
->ch
= buf
[off
++];
906 /* Find the transition. */
907 if (ictx
->state
!= state
||
909 ictx
->ch
< itr
->first
||
910 ictx
->ch
> itr
->last
) {
911 itr
= ictx
->state
->transitions
;
912 while (itr
->first
!= -1 && itr
->last
!= -1) {
913 if (ictx
->ch
>= itr
->first
&&
914 ictx
->ch
<= itr
->last
)
918 if (itr
->first
== -1 || itr
->last
== -1) {
919 /* No transition? Eh? */
920 fatalx("no transition from state");
926 * Any state except print stops the current collection. This is
927 * an optimization to avoid checking if the attributes have
928 * changed for every character. It will stop unnecessarily for
929 * sequences that don't make a terminal change, but they should
932 if (itr
->handler
!= input_print
)
933 screen_write_collect_end(sctx
);
936 * Execute the handler, if any. Don't switch state if it
939 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
942 /* And switch state, if necessary. */
943 if (itr
->state
!= NULL
)
944 input_set_state(ictx
, itr
);
946 /* If not in ground state, save input. */
947 if (ictx
->state
!= &input_state_ground
)
948 evbuffer_add(ictx
->since_ground
, &ictx
->ch
, 1);
952 /* Parse input from pane. */
954 input_parse_pane(struct window_pane
*wp
)
959 new_data
= window_pane_get_new_data(wp
, &wp
->offset
, &new_size
);
960 input_parse_buffer(wp
, new_data
, new_size
);
961 window_pane_update_used_data(wp
, &wp
->offset
, new_size
);
964 /* Parse given input. */
966 input_parse_buffer(struct window_pane
*wp
, u_char
*buf
, size_t len
)
968 struct input_ctx
*ictx
= wp
->ictx
;
969 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
974 window_update_activity(wp
->window
);
975 wp
->flags
|= PANE_CHANGED
;
977 /* Flag new input while in a mode. */
978 if (!TAILQ_EMPTY(&wp
->modes
))
979 wp
->flags
|= PANE_UNSEENCHANGES
;
981 /* NULL wp if there is a mode set as don't want to update the tty. */
982 if (TAILQ_EMPTY(&wp
->modes
))
983 screen_write_start_pane(sctx
, wp
, &wp
->base
);
985 screen_write_start(sctx
, &wp
->base
);
987 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__
, wp
->id
,
988 ictx
->state
->name
, len
, (int)len
, buf
);
990 input_parse(ictx
, buf
, len
);
991 screen_write_stop(sctx
);
994 /* Parse given input for screen. */
996 input_parse_screen(struct input_ctx
*ictx
, struct screen
*s
,
997 screen_write_init_ctx_cb cb
, void *arg
, u_char
*buf
, size_t len
)
999 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1004 screen_write_start_callback(sctx
, s
, cb
, arg
);
1005 input_parse(ictx
, buf
, len
);
1006 screen_write_stop(sctx
);
1009 /* Split the parameter list (if any). */
1011 input_split(struct input_ctx
*ictx
)
1015 struct input_param
*ip
;
1018 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1019 if (ictx
->param_list
[i
].type
== INPUT_STRING
)
1020 free(ictx
->param_list
[i
].str
);
1022 ictx
->param_list_len
= 0;
1024 if (ictx
->param_len
== 0)
1026 ip
= &ictx
->param_list
[0];
1028 ptr
= ictx
->param_buf
;
1029 while ((out
= strsep(&ptr
, ";")) != NULL
) {
1031 ip
->type
= INPUT_MISSING
;
1033 if (strchr(out
, ':') != NULL
) {
1034 ip
->type
= INPUT_STRING
;
1035 ip
->str
= xstrdup(out
);
1037 ip
->type
= INPUT_NUMBER
;
1038 ip
->num
= strtonum(out
, 0, INT_MAX
, &errstr
);
1043 ip
= &ictx
->param_list
[++ictx
->param_list_len
];
1044 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
1048 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1049 ip
= &ictx
->param_list
[i
];
1050 if (ip
->type
== INPUT_MISSING
)
1051 log_debug("parameter %u: missing", i
);
1052 else if (ip
->type
== INPUT_STRING
)
1053 log_debug("parameter %u: string %s", i
, ip
->str
);
1054 else if (ip
->type
== INPUT_NUMBER
)
1055 log_debug("parameter %u: number %d", i
, ip
->num
);
1061 /* Get an argument or return default value. */
1063 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
1065 struct input_param
*ip
;
1068 if (validx
>= ictx
->param_list_len
)
1070 ip
= &ictx
->param_list
[validx
];
1071 if (ip
->type
== INPUT_MISSING
)
1073 if (ip
->type
== INPUT_STRING
)
1076 if (retval
< minval
)
1081 /* Reply to terminal query. */
1083 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
1085 struct bufferevent
*bev
= ictx
->event
;
1093 xvasprintf(&reply
, fmt
, ap
);
1096 log_debug("%s: %s", __func__
, reply
);
1097 bufferevent_write(bev
, reply
, strlen(reply
));
1101 /* Clear saved state. */
1103 input_clear(struct input_ctx
*ictx
)
1105 event_del(&ictx
->timer
);
1107 *ictx
->interm_buf
= '\0';
1108 ictx
->interm_len
= 0;
1110 *ictx
->param_buf
= '\0';
1111 ictx
->param_len
= 0;
1113 *ictx
->input_buf
= '\0';
1114 ictx
->input_len
= 0;
1116 ictx
->input_end
= INPUT_END_ST
;
1118 ictx
->flags
&= ~INPUT_DISCARD
;
1121 /* Reset for ground state. */
1123 input_ground(struct input_ctx
*ictx
)
1125 event_del(&ictx
->timer
);
1126 evbuffer_drain(ictx
->since_ground
, EVBUFFER_LENGTH(ictx
->since_ground
));
1128 if (ictx
->input_space
> INPUT_BUF_START
) {
1129 ictx
->input_space
= INPUT_BUF_START
;
1130 ictx
->input_buf
= xrealloc(ictx
->input_buf
, INPUT_BUF_START
);
1134 /* Output this character to the screen. */
1136 input_print(struct input_ctx
*ictx
)
1138 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1141 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1143 set
= ictx
->cell
.set
== 0 ? ictx
->cell
.g0set
: ictx
->cell
.g1set
;
1145 ictx
->cell
.cell
.attr
|= GRID_ATTR_CHARSET
;
1147 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1149 utf8_set(&ictx
->cell
.cell
.data
, ictx
->ch
);
1150 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1152 utf8_copy(&ictx
->last
, &ictx
->cell
.cell
.data
);
1153 ictx
->flags
|= INPUT_LAST
;
1155 ictx
->cell
.cell
.attr
&= ~GRID_ATTR_CHARSET
;
1160 /* Collect intermediate string. */
1162 input_intermediate(struct input_ctx
*ictx
)
1164 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
1165 ictx
->flags
|= INPUT_DISCARD
;
1167 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
1168 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
1174 /* Collect parameter string. */
1176 input_parameter(struct input_ctx
*ictx
)
1178 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
1179 ictx
->flags
|= INPUT_DISCARD
;
1181 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
1182 ictx
->param_buf
[ictx
->param_len
] = '\0';
1188 /* Collect input string. */
1190 input_input(struct input_ctx
*ictx
)
1194 available
= ictx
->input_space
;
1195 while (ictx
->input_len
+ 1 >= available
) {
1197 if (available
> INPUT_BUF_LIMIT
) {
1198 ictx
->flags
|= INPUT_DISCARD
;
1201 ictx
->input_buf
= xrealloc(ictx
->input_buf
, available
);
1202 ictx
->input_space
= available
;
1204 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
1205 ictx
->input_buf
[ictx
->input_len
] = '\0';
1210 /* Execute C0 control sequence. */
1212 input_c0_dispatch(struct input_ctx
*ictx
)
1214 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1215 struct window_pane
*wp
= ictx
->wp
;
1216 struct screen
*s
= sctx
->s
;
1218 ictx
->utf8started
= 0; /* can't be valid UTF-8 */
1220 log_debug("%s: '%c'", __func__
, ictx
->ch
);
1223 case '\000': /* NUL */
1225 case '\007': /* BEL */
1227 alerts_queue(wp
->window
, WINDOW_BELL
);
1229 case '\010': /* BS */
1230 screen_write_backspace(sctx
);
1232 case '\011': /* HT */
1233 /* Don't tab beyond the end of the line. */
1234 if (s
->cx
>= screen_size_x(s
) - 1)
1237 /* Find the next tab point, or use the last column if none. */
1240 if (bit_test(s
->tabs
, s
->cx
))
1242 } while (s
->cx
< screen_size_x(s
) - 1);
1244 case '\012': /* LF */
1245 case '\013': /* VT */
1246 case '\014': /* FF */
1247 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1248 if (s
->mode
& MODE_CRLF
)
1249 screen_write_carriagereturn(sctx
);
1251 case '\015': /* CR */
1252 screen_write_carriagereturn(sctx
);
1254 case '\016': /* SO */
1257 case '\017': /* SI */
1261 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1265 ictx
->flags
&= ~INPUT_LAST
;
1269 /* Execute escape sequence. */
1271 input_esc_dispatch(struct input_ctx
*ictx
)
1273 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1274 struct screen
*s
= sctx
->s
;
1275 struct input_table_entry
*entry
;
1277 if (ictx
->flags
& INPUT_DISCARD
)
1279 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
1281 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
1282 sizeof input_esc_table
[0], input_table_compare
);
1283 if (entry
== NULL
) {
1284 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1288 switch (entry
->type
) {
1290 colour_palette_clear(ictx
->palette
);
1291 input_reset_cell(ictx
);
1292 screen_write_reset(sctx
);
1293 screen_write_fullredraw(sctx
);
1296 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1299 screen_write_carriagereturn(sctx
);
1300 screen_write_linefeed(sctx
, 0, ictx
->cell
.cell
.bg
);
1303 if (s
->cx
< screen_size_x(s
))
1304 bit_set(s
->tabs
, s
->cx
);
1307 screen_write_reverseindex(sctx
, ictx
->cell
.cell
.bg
);
1309 case INPUT_ESC_DECKPAM
:
1310 screen_write_mode_set(sctx
, MODE_KKEYPAD
);
1312 case INPUT_ESC_DECKPNM
:
1313 screen_write_mode_clear(sctx
, MODE_KKEYPAD
);
1315 case INPUT_ESC_DECSC
:
1316 input_save_state(ictx
);
1318 case INPUT_ESC_DECRC
:
1319 input_restore_state(ictx
);
1321 case INPUT_ESC_DECALN
:
1322 screen_write_alignmenttest(sctx
);
1324 case INPUT_ESC_SCSG0_ON
:
1325 ictx
->cell
.g0set
= 1;
1327 case INPUT_ESC_SCSG0_OFF
:
1328 ictx
->cell
.g0set
= 0;
1330 case INPUT_ESC_SCSG1_ON
:
1331 ictx
->cell
.g1set
= 1;
1333 case INPUT_ESC_SCSG1_OFF
:
1334 ictx
->cell
.g1set
= 0;
1337 /* ST terminates OSC but the state transition already did it. */
1341 ictx
->flags
&= ~INPUT_LAST
;
1345 /* Execute control sequence. */
1347 input_csi_dispatch(struct input_ctx
*ictx
)
1349 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1350 struct screen
*s
= sctx
->s
;
1351 struct input_table_entry
*entry
;
1353 u_int cx
, bg
= ictx
->cell
.cell
.bg
;
1355 if (ictx
->flags
& INPUT_DISCARD
)
1358 log_debug("%s: '%c' \"%s\" \"%s\"", __func__
, ictx
->ch
,
1359 ictx
->interm_buf
, ictx
->param_buf
);
1361 if (input_split(ictx
) != 0)
1364 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1365 sizeof input_csi_table
[0], input_table_compare
);
1366 if (entry
== NULL
) {
1367 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1371 switch (entry
->type
) {
1373 /* Find the previous tab point, n times. */
1375 if (cx
> screen_size_x(s
) - 1)
1376 cx
= screen_size_x(s
) - 1;
1377 n
= input_get(ictx
, 0, 1, 1);
1380 while (cx
> 0 && n
-- > 0) {
1383 while (cx
> 0 && !bit_test(s
->tabs
, cx
));
1388 n
= input_get(ictx
, 0, 1, 1);
1390 screen_write_cursorleft(sctx
, n
);
1393 n
= input_get(ictx
, 0, 1, 1);
1395 screen_write_cursordown(sctx
, n
);
1398 n
= input_get(ictx
, 0, 1, 1);
1400 screen_write_cursorright(sctx
, n
);
1403 n
= input_get(ictx
, 0, 1, 1);
1404 m
= input_get(ictx
, 1, 1, 1);
1405 if (n
!= -1 && m
!= -1)
1406 screen_write_cursormove(sctx
, m
- 1, n
- 1, 1);
1408 case INPUT_CSI_MODSET
:
1409 n
= input_get(ictx
, 0, 0, 0);
1412 m
= input_get(ictx
, 1, 0, 0);
1415 * Set the extended key reporting mode as per the client
1416 * request, unless "extended-keys" is set to "off".
1418 ek
= options_get_number(global_options
, "extended-keys");
1421 screen_write_mode_clear(sctx
, EXTENDED_KEY_MODES
);
1423 screen_write_mode_set(sctx
, MODE_KEYS_EXTENDED_2
);
1424 else if (m
== 1 || ek
== 2)
1425 screen_write_mode_set(sctx
, MODE_KEYS_EXTENDED
);
1427 case INPUT_CSI_MODOFF
:
1428 n
= input_get(ictx
, 0, 0, 0);
1433 * Clear the extended key reporting mode as per the client
1434 * request, unless "extended-keys always" forces into mode 1.
1436 screen_write_mode_clear(sctx
,
1437 MODE_KEYS_EXTENDED
|MODE_KEYS_EXTENDED_2
);
1438 if (options_get_number(global_options
, "extended-keys") == 2)
1439 screen_write_mode_set(sctx
, MODE_KEYS_EXTENDED
);
1441 case INPUT_CSI_WINOPS
:
1442 input_csi_dispatch_winops(ictx
);
1445 n
= input_get(ictx
, 0, 1, 1);
1447 screen_write_cursorup(sctx
, n
);
1450 n
= input_get(ictx
, 0, 1, 1);
1452 screen_write_carriagereturn(sctx
);
1453 screen_write_cursordown(sctx
, n
);
1457 n
= input_get(ictx
, 0, 1, 1);
1459 screen_write_carriagereturn(sctx
);
1460 screen_write_cursorup(sctx
, n
);
1464 switch (input_get(ictx
, 0, 0, 0)) {
1469 input_reply(ictx
, "\033[?1;2;4c");
1471 input_reply(ictx
, "\033[?1;2c");
1475 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1479 case INPUT_CSI_DA_TWO
:
1480 switch (input_get(ictx
, 0, 0, 0)) {
1484 input_reply(ictx
, "\033[>84;0;0c");
1487 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1492 n
= input_get(ictx
, 0, 1, 1);
1494 screen_write_clearcharacter(sctx
, n
, bg
);
1497 n
= input_get(ictx
, 0, 1, 1);
1499 screen_write_deletecharacter(sctx
, n
, bg
);
1501 case INPUT_CSI_DECSTBM
:
1502 n
= input_get(ictx
, 0, 1, 1);
1503 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1504 if (n
!= -1 && m
!= -1)
1505 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1508 n
= input_get(ictx
, 0, 1, 1);
1510 screen_write_deleteline(sctx
, n
, bg
);
1513 switch (input_get(ictx
, 0, 0, 0)) {
1517 input_reply(ictx
, "\033[0n");
1520 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1523 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1528 switch (input_get(ictx
, 0, 0, 0)) {
1532 screen_write_clearendofscreen(sctx
, bg
);
1535 screen_write_clearstartofscreen(sctx
, bg
);
1538 screen_write_clearscreen(sctx
, bg
);
1541 if (input_get(ictx
, 1, 0, 0) == 0) {
1543 * Linux console extension to clear history
1544 * (for example before locking the screen).
1546 screen_write_clearhistory(sctx
);
1550 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1555 switch (input_get(ictx
, 0, 0, 0)) {
1559 screen_write_clearendofline(sctx
, bg
);
1562 screen_write_clearstartofline(sctx
, bg
);
1565 screen_write_clearline(sctx
, bg
);
1568 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1573 n
= input_get(ictx
, 0, 1, 1);
1575 screen_write_cursormove(sctx
, n
- 1, -1, 1);
1578 n
= input_get(ictx
, 0, 1, 1);
1580 screen_write_insertcharacter(sctx
, n
, bg
);
1583 n
= input_get(ictx
, 0, 1, 1);
1585 screen_write_insertline(sctx
, n
, bg
);
1588 n
= input_get(ictx
, 0, 1, 1);
1592 m
= screen_size_x(s
) - s
->cx
;
1596 if (~ictx
->flags
& INPUT_LAST
)
1599 utf8_copy(&ictx
->cell
.cell
.data
, &ictx
->last
);
1600 for (i
= 0; i
< n
; i
++)
1601 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
1604 input_restore_state(ictx
);
1607 input_csi_dispatch_rm(ictx
);
1609 case INPUT_CSI_RM_PRIVATE
:
1610 input_csi_dispatch_rm_private(ictx
);
1613 input_save_state(ictx
);
1616 input_csi_dispatch_sgr(ictx
);
1619 input_csi_dispatch_sm(ictx
);
1621 case INPUT_CSI_SM_PRIVATE
:
1622 input_csi_dispatch_sm_private(ictx
);
1624 case INPUT_CSI_SM_GRAPHICS
:
1625 input_csi_dispatch_sm_graphics(ictx
);
1628 n
= input_get(ictx
, 0, 1, 1);
1630 screen_write_scrollup(sctx
, n
, bg
);
1633 n
= input_get(ictx
, 0, 1, 1);
1635 screen_write_scrolldown(sctx
, n
, bg
);
1638 switch (input_get(ictx
, 0, 0, 0)) {
1642 if (s
->cx
< screen_size_x(s
))
1643 bit_clear(s
->tabs
, s
->cx
);
1646 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1649 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1654 n
= input_get(ictx
, 0, 1, 1);
1656 screen_write_cursormove(sctx
, -1, n
- 1, 1);
1658 case INPUT_CSI_DECSCUSR
:
1659 n
= input_get(ictx
, 0, 0, 0);
1661 screen_set_cursor_style(n
, &s
->cstyle
, &s
->mode
);
1664 n
= input_get(ictx
, 0, 0, 0);
1666 input_reply(ictx
, "\033P>|tmux %s\033\\", getversion());
1671 ictx
->flags
&= ~INPUT_LAST
;
1675 /* Handle CSI RM. */
1677 input_csi_dispatch_rm(struct input_ctx
*ictx
)
1679 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1682 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1683 switch (input_get(ictx
, i
, 0, -1)) {
1687 screen_write_mode_clear(sctx
, MODE_INSERT
);
1690 screen_write_mode_set(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1693 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1699 /* Handle CSI private RM. */
1701 input_csi_dispatch_rm_private(struct input_ctx
*ictx
)
1703 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1704 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1707 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1708 switch (input_get(ictx
, i
, 0, -1)) {
1711 case 1: /* DECCKM */
1712 screen_write_mode_clear(sctx
, MODE_KCURSOR
);
1714 case 3: /* DECCOLM */
1715 screen_write_cursormove(sctx
, 0, 0, 1);
1716 screen_write_clearscreen(sctx
, gc
->bg
);
1719 screen_write_mode_clear(sctx
, MODE_ORIGIN
);
1720 screen_write_cursormove(sctx
, 0, 0, 1);
1722 case 7: /* DECAWM */
1723 screen_write_mode_clear(sctx
, MODE_WRAP
);
1726 screen_write_mode_clear(sctx
, MODE_CURSOR_BLINKING
);
1727 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1730 screen_write_mode_clear(sctx
, MODE_CURSOR
);
1736 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1739 screen_write_mode_clear(sctx
, MODE_FOCUSON
);
1742 screen_write_mode_clear(sctx
, MODE_MOUSE_UTF8
);
1745 screen_write_mode_clear(sctx
, MODE_MOUSE_SGR
);
1749 screen_write_alternateoff(sctx
, gc
, 0);
1752 screen_write_alternateoff(sctx
, gc
, 1);
1755 screen_write_mode_clear(sctx
, MODE_BRACKETPASTE
);
1758 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1764 /* Handle CSI SM. */
1766 input_csi_dispatch_sm(struct input_ctx
*ictx
)
1768 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1771 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1772 switch (input_get(ictx
, i
, 0, -1)) {
1776 screen_write_mode_set(sctx
, MODE_INSERT
);
1779 screen_write_mode_clear(sctx
, MODE_CURSOR_VERY_VISIBLE
);
1782 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1788 /* Handle CSI private SM. */
1790 input_csi_dispatch_sm_private(struct input_ctx
*ictx
)
1792 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1793 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1796 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1797 switch (input_get(ictx
, i
, 0, -1)) {
1800 case 1: /* DECCKM */
1801 screen_write_mode_set(sctx
, MODE_KCURSOR
);
1803 case 3: /* DECCOLM */
1804 screen_write_cursormove(sctx
, 0, 0, 1);
1805 screen_write_clearscreen(sctx
, ictx
->cell
.cell
.bg
);
1808 screen_write_mode_set(sctx
, MODE_ORIGIN
);
1809 screen_write_cursormove(sctx
, 0, 0, 1);
1811 case 7: /* DECAWM */
1812 screen_write_mode_set(sctx
, MODE_WRAP
);
1815 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING
);
1816 screen_write_mode_set(sctx
, MODE_CURSOR_BLINKING_SET
);
1819 screen_write_mode_set(sctx
, MODE_CURSOR
);
1822 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1823 screen_write_mode_set(sctx
, MODE_MOUSE_STANDARD
);
1826 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1827 screen_write_mode_set(sctx
, MODE_MOUSE_BUTTON
);
1830 screen_write_mode_clear(sctx
, ALL_MOUSE_MODES
);
1831 screen_write_mode_set(sctx
, MODE_MOUSE_ALL
);
1834 screen_write_mode_set(sctx
, MODE_FOCUSON
);
1837 screen_write_mode_set(sctx
, MODE_MOUSE_UTF8
);
1840 screen_write_mode_set(sctx
, MODE_MOUSE_SGR
);
1844 screen_write_alternateon(sctx
, gc
, 0);
1847 screen_write_alternateon(sctx
, gc
, 1);
1850 screen_write_mode_set(sctx
, MODE_BRACKETPASTE
);
1853 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1859 /* Handle CSI graphics SM. */
1861 input_csi_dispatch_sm_graphics(__unused
struct input_ctx
*ictx
)
1866 if (ictx
->param_list_len
> 3)
1868 n
= input_get(ictx
, 0, 0, 0);
1869 m
= input_get(ictx
, 1, 0, 0);
1870 o
= input_get(ictx
, 2, 0, 0);
1872 if (n
== 1 && (m
== 1 || m
== 2 || m
== 4))
1873 input_reply(ictx
, "\033[?%d;0;%uS", n
, SIXEL_COLOUR_REGISTERS
);
1875 input_reply(ictx
, "\033[?%d;3;%dS", n
, o
);
1879 /* Handle CSI window operations. */
1881 input_csi_dispatch_winops(struct input_ctx
*ictx
)
1883 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1884 struct screen
*s
= sctx
->s
;
1885 struct window_pane
*wp
= ictx
->wp
;
1886 struct window
*w
= NULL
;
1887 u_int x
= screen_size_x(s
), y
= screen_size_y(s
);
1894 while ((n
= input_get(ictx
, m
, 0, -1)) != -1) {
1911 if (input_get(ictx
, m
, 0, -1) == -1)
1917 if (input_get(ictx
, m
, 0, -1) == -1)
1923 input_reply(ictx
, "\033[4;%u;%ut", y
* w
->ypixel
,
1929 input_reply(ictx
, "\033[5;%u;%ut", y
* w
->ypixel
,
1935 input_reply(ictx
, "\033[6;%u;%ut", w
->ypixel
,
1939 input_reply(ictx
, "\033[8;%u;%ut", y
, x
);
1942 input_reply(ictx
, "\033[9;%u;%ut", y
, x
);
1946 switch (input_get(ictx
, m
, 0, -1)) {
1951 screen_push_title(sctx
->s
);
1957 switch (input_get(ictx
, m
, 0, -1)) {
1962 screen_pop_title(sctx
->s
);
1965 notify_pane("pane-title-changed", wp
);
1966 server_redraw_window_borders(w
);
1967 server_status_window(w
);
1972 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1979 /* Helper for 256 colour SGR. */
1981 input_csi_dispatch_sgr_256_do(struct input_ctx
*ictx
, int fgbg
, int c
)
1983 struct grid_cell
*gc
= &ictx
->cell
.cell
;
1985 if (c
== -1 || c
> 255) {
1988 else if (fgbg
== 48)
1992 gc
->fg
= c
| COLOUR_FLAG_256
;
1993 else if (fgbg
== 48)
1994 gc
->bg
= c
| COLOUR_FLAG_256
;
1995 else if (fgbg
== 58)
1996 gc
->us
= c
| COLOUR_FLAG_256
;
2001 /* Handle CSI SGR for 256 colours. */
2003 input_csi_dispatch_sgr_256(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
2007 c
= input_get(ictx
, (*i
) + 1, 0, -1);
2008 if (input_csi_dispatch_sgr_256_do(ictx
, fgbg
, c
))
2012 /* Helper for RGB colour SGR. */
2014 input_csi_dispatch_sgr_rgb_do(struct input_ctx
*ictx
, int fgbg
, int r
, int g
,
2017 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2019 if (r
== -1 || r
> 255)
2021 if (g
== -1 || g
> 255)
2023 if (b
== -1 || b
> 255)
2027 gc
->fg
= colour_join_rgb(r
, g
, b
);
2028 else if (fgbg
== 48)
2029 gc
->bg
= colour_join_rgb(r
, g
, b
);
2030 else if (fgbg
== 58)
2031 gc
->us
= colour_join_rgb(r
, g
, b
);
2035 /* Handle CSI SGR for RGB colours. */
2037 input_csi_dispatch_sgr_rgb(struct input_ctx
*ictx
, int fgbg
, u_int
*i
)
2041 r
= input_get(ictx
, (*i
) + 1, 0, -1);
2042 g
= input_get(ictx
, (*i
) + 2, 0, -1);
2043 b
= input_get(ictx
, (*i
) + 3, 0, -1);
2044 if (input_csi_dispatch_sgr_rgb_do(ictx
, fgbg
, r
, g
, b
))
2048 /* Handle CSI SGR with a ISO parameter. */
2050 input_csi_dispatch_sgr_colon(struct input_ctx
*ictx
, u_int i
)
2052 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2053 char *s
= ictx
->param_list
[i
].str
, *copy
, *ptr
, *out
;
2058 for (n
= 0; n
< nitems(p
); n
++)
2062 ptr
= copy
= xstrdup(s
);
2063 while ((out
= strsep(&ptr
, ":")) != NULL
) {
2065 p
[n
++] = strtonum(out
, 0, INT_MAX
, &errstr
);
2066 if (errstr
!= NULL
|| n
== nitems(p
)) {
2072 if (n
== nitems(p
)) {
2077 log_debug("%s: %u = %d", __func__
, n
- 1, p
[n
- 1]);
2088 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2091 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2092 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2095 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2096 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2099 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2100 gc
->attr
|= GRID_ATTR_UNDERSCORE_3
;
2103 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2104 gc
->attr
|= GRID_ATTR_UNDERSCORE_4
;
2107 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2108 gc
->attr
|= GRID_ATTR_UNDERSCORE_5
;
2113 if (n
< 2 || (p
[0] != 38 && p
[0] != 48 && p
[0] != 58))
2125 input_csi_dispatch_sgr_rgb_do(ictx
, p
[0], p
[i
], p
[i
+ 1],
2131 input_csi_dispatch_sgr_256_do(ictx
, p
[0], p
[2]);
2136 /* Handle CSI SGR. */
2138 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
2140 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2144 if (ictx
->param_list_len
== 0) {
2145 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2149 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
2150 if (ictx
->param_list
[i
].type
== INPUT_STRING
) {
2151 input_csi_dispatch_sgr_colon(ictx
, i
);
2154 n
= input_get(ictx
, i
, 0, 0);
2158 if (n
== 38 || n
== 48 || n
== 58) {
2160 switch (input_get(ictx
, i
, 0, -1)) {
2162 input_csi_dispatch_sgr_rgb(ictx
, n
, &i
);
2165 input_csi_dispatch_sgr_256(ictx
, n
, &i
);
2174 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2178 gc
->attr
|= GRID_ATTR_BRIGHT
;
2181 gc
->attr
|= GRID_ATTR_DIM
;
2184 gc
->attr
|= GRID_ATTR_ITALICS
;
2187 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2188 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
2192 gc
->attr
|= GRID_ATTR_BLINK
;
2195 gc
->attr
|= GRID_ATTR_REVERSE
;
2198 gc
->attr
|= GRID_ATTR_HIDDEN
;
2201 gc
->attr
|= GRID_ATTR_STRIKETHROUGH
;
2204 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2205 gc
->attr
|= GRID_ATTR_UNDERSCORE_2
;
2208 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
2211 gc
->attr
&= ~GRID_ATTR_ITALICS
;
2214 gc
->attr
&= ~GRID_ATTR_ALL_UNDERSCORE
;
2217 gc
->attr
&= ~GRID_ATTR_BLINK
;
2220 gc
->attr
&= ~GRID_ATTR_REVERSE
;
2223 gc
->attr
&= ~GRID_ATTR_HIDDEN
;
2226 gc
->attr
&= ~GRID_ATTR_STRIKETHROUGH
;
2255 gc
->attr
|= GRID_ATTR_OVERLINE
;
2258 gc
->attr
&= ~GRID_ATTR_OVERLINE
;
2287 /* End of input with BEL. */
2289 input_end_bel(struct input_ctx
*ictx
)
2291 log_debug("%s", __func__
);
2293 ictx
->input_end
= INPUT_END_BEL
;
2298 /* DCS string started. */
2300 input_enter_dcs(struct input_ctx
*ictx
)
2302 log_debug("%s", __func__
);
2305 input_start_timer(ictx
);
2306 ictx
->flags
&= ~INPUT_LAST
;
2309 /* DCS terminator (ST) received. */
2311 input_dcs_dispatch(struct input_ctx
*ictx
)
2313 struct window_pane
*wp
= ictx
->wp
;
2314 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2315 u_char
*buf
= ictx
->input_buf
;
2316 size_t len
= ictx
->input_len
;
2317 const char prefix
[] = "tmux;";
2318 const u_int prefixlen
= (sizeof prefix
) - 1;
2319 long long allow_passthrough
= 0;
2322 struct sixel_image
*si
;
2328 if (ictx
->flags
& INPUT_DISCARD
) {
2329 log_debug("%s: %zu bytes (discard)", __func__
, len
);
2335 if (buf
[0] == 'q') {
2336 si
= sixel_parse(buf
, len
, w
->xpixel
, w
->ypixel
);
2338 screen_write_sixelimage(sctx
, si
, ictx
->cell
.cell
.bg
);
2342 allow_passthrough
= options_get_number(wp
->options
, "allow-passthrough");
2343 if (!allow_passthrough
)
2345 log_debug("%s: \"%s\"", __func__
, buf
);
2347 if (len
>= prefixlen
&& strncmp(buf
, prefix
, prefixlen
) == 0) {
2348 screen_write_rawstring(sctx
, buf
+ prefixlen
, len
- prefixlen
,
2349 allow_passthrough
== 2);
2355 /* OSC string started. */
2357 input_enter_osc(struct input_ctx
*ictx
)
2359 log_debug("%s", __func__
);
2362 input_start_timer(ictx
);
2363 ictx
->flags
&= ~INPUT_LAST
;
2366 /* OSC terminator (ST) received. */
2368 input_exit_osc(struct input_ctx
*ictx
)
2370 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2371 struct window_pane
*wp
= ictx
->wp
;
2372 u_char
*p
= ictx
->input_buf
;
2375 if (ictx
->flags
& INPUT_DISCARD
)
2377 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
2380 log_debug("%s: \"%s\" (end %s)", __func__
, p
,
2381 ictx
->input_end
== INPUT_END_ST
? "ST" : "BEL");
2384 while (*p
>= '0' && *p
<= '9')
2385 option
= option
* 10 + *p
++ - '0';
2386 if (*p
!= ';' && *p
!= '\0')
2395 options_get_number(wp
->options
, "allow-set-title") &&
2396 screen_set_title(sctx
->s
, p
)) {
2397 notify_pane("pane-title-changed", wp
);
2398 server_redraw_window_borders(wp
->window
);
2399 server_status_window(wp
->window
);
2403 input_osc_4(ictx
, p
);
2406 if (utf8_isvalid(p
)) {
2407 screen_set_path(sctx
->s
, p
);
2409 server_redraw_window_borders(wp
->window
);
2410 server_status_window(wp
->window
);
2415 input_osc_8(ictx
, p
);
2418 input_osc_10(ictx
, p
);
2421 input_osc_11(ictx
, p
);
2424 input_osc_12(ictx
, p
);
2427 input_osc_52(ictx
, p
);
2430 input_osc_104(ictx
, p
);
2433 input_osc_110(ictx
, p
);
2436 input_osc_111(ictx
, p
);
2439 input_osc_112(ictx
, p
);
2442 input_osc_133(ictx
, p
);
2445 log_debug("%s: unknown '%u'", __func__
, option
);
2450 /* APC string started. */
2452 input_enter_apc(struct input_ctx
*ictx
)
2454 log_debug("%s", __func__
);
2457 input_start_timer(ictx
);
2458 ictx
->flags
&= ~INPUT_LAST
;
2461 /* APC terminator (ST) received. */
2463 input_exit_apc(struct input_ctx
*ictx
)
2465 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2466 struct window_pane
*wp
= ictx
->wp
;
2468 if (ictx
->flags
& INPUT_DISCARD
)
2470 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2472 if (screen_set_title(sctx
->s
, ictx
->input_buf
) && wp
!= NULL
) {
2473 notify_pane("pane-title-changed", wp
);
2474 server_redraw_window_borders(wp
->window
);
2475 server_status_window(wp
->window
);
2479 /* Rename string started. */
2481 input_enter_rename(struct input_ctx
*ictx
)
2483 log_debug("%s", __func__
);
2486 input_start_timer(ictx
);
2487 ictx
->flags
&= ~INPUT_LAST
;
2490 /* Rename terminator (ST) received. */
2492 input_exit_rename(struct input_ctx
*ictx
)
2494 struct window_pane
*wp
= ictx
->wp
;
2496 struct options_entry
*o
;
2500 if (ictx
->flags
& INPUT_DISCARD
)
2502 if (!options_get_number(ictx
->wp
->options
, "allow-rename"))
2504 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
2506 if (!utf8_isvalid(ictx
->input_buf
))
2510 if (ictx
->input_len
== 0) {
2511 o
= options_get_only(w
->options
, "automatic-rename");
2513 options_remove_or_default(o
, -1, NULL
);
2514 if (!options_get_number(w
->options
, "automatic-rename"))
2515 window_set_name(w
, "");
2517 options_set_number(w
->options
, "automatic-rename", 0);
2518 window_set_name(w
, ictx
->input_buf
);
2520 server_redraw_window_borders(w
);
2521 server_status_window(w
);
2524 /* Open UTF-8 character. */
2526 input_top_bit_set(struct input_ctx
*ictx
)
2528 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
2529 struct utf8_data
*ud
= &ictx
->utf8data
;
2531 ictx
->flags
&= ~INPUT_LAST
;
2533 if (!ictx
->utf8started
) {
2534 if (utf8_open(ud
, ictx
->ch
) != UTF8_MORE
)
2536 ictx
->utf8started
= 1;
2540 switch (utf8_append(ud
, ictx
->ch
)) {
2544 ictx
->utf8started
= 0;
2549 ictx
->utf8started
= 0;
2551 log_debug("%s %hhu '%*s' (width %hhu)", __func__
, ud
->size
,
2552 (int)ud
->size
, ud
->data
, ud
->width
);
2554 utf8_copy(&ictx
->cell
.cell
.data
, ud
);
2555 screen_write_collect_add(sctx
, &ictx
->cell
.cell
);
2557 utf8_copy(&ictx
->last
, &ictx
->cell
.cell
.data
);
2558 ictx
->flags
|= INPUT_LAST
;
2563 /* Reply to a colour request. */
2565 input_osc_colour_reply(struct input_ctx
*ictx
, u_int n
, int c
)
2571 c
= colour_force_rgb(c
);
2574 colour_split_rgb(c
, &r
, &g
, &b
);
2576 if (ictx
->input_end
== INPUT_END_BEL
)
2580 input_reply(ictx
, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2581 n
, r
, r
, g
, g
, b
, b
, end
);
2584 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2586 input_osc_4(struct input_ctx
*ictx
, const char *p
)
2588 char *copy
, *s
, *next
= NULL
;
2590 int c
, bad
= 0, redraw
= 0;
2592 copy
= s
= xstrdup(p
);
2593 while (s
!= NULL
&& *s
!= '\0') {
2594 idx
= strtol(s
, &next
, 10);
2595 if (*next
++ != ';') {
2599 if (idx
< 0 || idx
>= 256) {
2604 s
= strsep(&next
, ";");
2605 if (strcmp(s
, "?") == 0) {
2606 c
= colour_palette_get(ictx
->palette
, idx
);
2608 input_osc_colour_reply(ictx
, 4, c
);
2611 if ((c
= colour_parseX11(s
)) == -1) {
2615 if (colour_palette_set(ictx
->palette
, idx
, c
))
2620 log_debug("bad OSC 4: %s", p
);
2622 screen_write_fullredraw(&ictx
->ctx
);
2626 /* Handle the OSC 8 sequence for embedding hyperlinks. */
2628 input_osc_8(struct input_ctx
*ictx
, const char *p
)
2630 struct hyperlinks
*hl
= ictx
->ctx
.s
->hyperlinks
;
2631 struct grid_cell
*gc
= &ictx
->cell
.cell
;
2632 const char *start
, *end
, *uri
;
2635 for (start
= p
; (end
= strpbrk(start
, ":;")) != NULL
; start
= end
+ 1) {
2636 if (end
- start
>= 4 && strncmp(start
, "id=", 3) == 0) {
2639 id
= xstrndup(start
+ 3, end
- start
- 3);
2642 /* The first ; is the end of parameters and start of the URI. */
2646 if (end
== NULL
|| *end
!= ';')
2654 gc
->link
= hyperlinks_put(hl
, uri
, id
);
2656 log_debug("hyperlink (anonymous) %s = %u", uri
, gc
->link
);
2658 log_debug("hyperlink (id=%s) %s = %u", id
, uri
, gc
->link
);
2663 log_debug("bad OSC 8 %s", p
);
2668 * Get a client with a foreground for the pane. There isn't much to choose
2669 * between them so just use the first.
2672 input_get_fg_client(struct window_pane
*wp
)
2674 struct window
*w
= wp
->window
;
2675 struct client
*loop
;
2677 TAILQ_FOREACH(loop
, &clients
, entry
) {
2678 if (loop
->flags
& CLIENT_UNATTACHEDFLAGS
)
2680 if (loop
->session
== NULL
|| !session_has(loop
->session
, w
))
2682 if (loop
->tty
.fg
== -1)
2684 return (loop
->tty
.fg
);
2689 /* Get a client with a background for the pane. */
2691 input_get_bg_client(struct window_pane
*wp
)
2693 struct window
*w
= wp
->window
;
2694 struct client
*loop
;
2696 TAILQ_FOREACH(loop
, &clients
, entry
) {
2697 if (loop
->flags
& CLIENT_UNATTACHEDFLAGS
)
2699 if (loop
->session
== NULL
|| !session_has(loop
->session
, w
))
2701 if (loop
->tty
.bg
== -1)
2703 return (loop
->tty
.bg
);
2709 * If any control mode client exists that has provided a bg color, return it.
2710 * Otherwise, return -1.
2713 input_get_bg_control_client(struct window_pane
*wp
)
2717 if (wp
->control_bg
== -1)
2720 TAILQ_FOREACH(c
, &clients
, entry
) {
2721 if (c
->flags
& CLIENT_CONTROL
)
2722 return (wp
->control_bg
);
2728 * If any control mode client exists that has provided a fg color, return it.
2729 * Otherwise, return -1.
2732 input_get_fg_control_client(struct window_pane
*wp
)
2736 if (wp
->control_fg
== -1)
2739 TAILQ_FOREACH(c
, &clients
, entry
) {
2740 if (c
->flags
& CLIENT_CONTROL
)
2741 return (wp
->control_fg
);
2746 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2748 input_osc_10(struct input_ctx
*ictx
, const char *p
)
2750 struct window_pane
*wp
= ictx
->wp
;
2751 struct grid_cell defaults
;
2754 if (strcmp(p
, "?") == 0) {
2757 c
= input_get_fg_control_client(wp
);
2759 tty_default_colours(&defaults
, wp
);
2760 if (COLOUR_DEFAULT(defaults
.fg
))
2761 c
= input_get_fg_client(wp
);
2765 input_osc_colour_reply(ictx
, 10, c
);
2769 if ((c
= colour_parseX11(p
)) == -1) {
2770 log_debug("bad OSC 10: %s", p
);
2773 if (ictx
->palette
!= NULL
) {
2774 ictx
->palette
->fg
= c
;
2776 wp
->flags
|= PANE_STYLECHANGED
;
2777 screen_write_fullredraw(&ictx
->ctx
);
2781 /* Handle the OSC 110 sequence for resetting foreground colour. */
2783 input_osc_110(struct input_ctx
*ictx
, const char *p
)
2785 struct window_pane
*wp
= ictx
->wp
;
2789 if (ictx
->palette
!= NULL
) {
2790 ictx
->palette
->fg
= 8;
2792 wp
->flags
|= PANE_STYLECHANGED
;
2793 screen_write_fullredraw(&ictx
->ctx
);
2797 /* Handle the OSC 11 sequence for setting and querying background colour. */
2799 input_osc_11(struct input_ctx
*ictx
, const char *p
)
2801 struct window_pane
*wp
= ictx
->wp
;
2802 struct grid_cell defaults
;
2805 if (strcmp(p
, "?") == 0) {
2808 c
= input_get_bg_control_client(wp
);
2810 tty_default_colours(&defaults
, wp
);
2811 if (COLOUR_DEFAULT(defaults
.bg
))
2812 c
= input_get_bg_client(wp
);
2816 input_osc_colour_reply(ictx
, 11, c
);
2820 if ((c
= colour_parseX11(p
)) == -1) {
2821 log_debug("bad OSC 11: %s", p
);
2824 if (ictx
->palette
!= NULL
) {
2825 ictx
->palette
->bg
= c
;
2827 wp
->flags
|= PANE_STYLECHANGED
;
2828 screen_write_fullredraw(&ictx
->ctx
);
2832 /* Handle the OSC 111 sequence for resetting background colour. */
2834 input_osc_111(struct input_ctx
*ictx
, const char *p
)
2836 struct window_pane
*wp
= ictx
->wp
;
2840 if (ictx
->palette
!= NULL
) {
2841 ictx
->palette
->bg
= 8;
2843 wp
->flags
|= PANE_STYLECHANGED
;
2844 screen_write_fullredraw(&ictx
->ctx
);
2848 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2850 input_osc_12(struct input_ctx
*ictx
, const char *p
)
2852 struct window_pane
*wp
= ictx
->wp
;
2855 if (strcmp(p
, "?") == 0) {
2857 c
= ictx
->ctx
.s
->ccolour
;
2859 c
= ictx
->ctx
.s
->default_ccolour
;
2860 input_osc_colour_reply(ictx
, 12, c
);
2865 if ((c
= colour_parseX11(p
)) == -1) {
2866 log_debug("bad OSC 12: %s", p
);
2869 screen_set_cursor_colour(ictx
->ctx
.s
, c
);
2872 /* Handle the OSC 112 sequence for resetting cursor colour. */
2874 input_osc_112(struct input_ctx
*ictx
, const char *p
)
2876 if (*p
== '\0') /* no arguments allowed */
2877 screen_set_cursor_colour(ictx
->ctx
.s
, -1);
2880 /* Handle the OSC 133 sequence. */
2882 input_osc_133(struct input_ctx
*ictx
, const char *p
)
2884 struct grid
*gd
= ictx
->ctx
.s
->grid
;
2885 u_int line
= ictx
->ctx
.s
->cy
+ gd
->hsize
;
2886 struct grid_line
*gl
;
2888 if (line
> gd
->hsize
+ gd
->sy
- 1)
2890 gl
= grid_get_line(gd
, line
);
2894 gl
->flags
|= GRID_LINE_START_PROMPT
;
2897 gl
->flags
|= GRID_LINE_START_OUTPUT
;
2902 /* Handle the OSC 52 sequence for setting the clipboard. */
2904 input_osc_52(struct input_ctx
*ictx
, const char *p
)
2906 struct window_pane
*wp
= ictx
->wp
;
2908 const char *buf
= NULL
;
2912 struct screen_write_ctx ctx
;
2913 struct paste_buffer
*pb
;
2914 const char* allow
= "cpqs01234567";
2915 char flags
[sizeof "cpqs01234567"] = "";
2920 state
= options_get_number(global_options
, "set-clipboard");
2924 if ((end
= strchr(p
, ';')) == NULL
)
2929 log_debug("%s: %s", __func__
, end
);
2931 for (i
= 0; p
+ i
!= end
; i
++) {
2932 if (strchr(allow
, p
[i
]) != NULL
&& strchr(flags
, p
[i
]) == NULL
)
2935 log_debug("%s: %.*s %s", __func__
, (int)(end
- p
- 1), p
, flags
);
2937 if (strcmp(end
, "?") == 0) {
2938 if ((pb
= paste_get_top(NULL
)) != NULL
)
2939 buf
= paste_buffer_data(pb
, &len
);
2940 if (ictx
->input_end
== INPUT_END_BEL
)
2941 input_reply_clipboard(ictx
->event
, buf
, len
, "\007");
2943 input_reply_clipboard(ictx
->event
, buf
, len
, "\033\\");
2947 len
= (strlen(end
) / 4) * 3;
2952 if ((outlen
= b64_pton(end
, out
, len
)) == -1) {
2957 screen_write_start_pane(&ctx
, wp
, NULL
);
2958 screen_write_setselection(&ctx
, flags
, out
, outlen
);
2959 screen_write_stop(&ctx
);
2960 notify_pane("pane-set-clipboard", wp
);
2962 paste_add(NULL
, out
, outlen
);
2965 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2967 input_osc_104(struct input_ctx
*ictx
, const char *p
)
2971 int bad
= 0, redraw
= 0;
2974 colour_palette_clear(ictx
->palette
);
2975 screen_write_fullredraw(&ictx
->ctx
);
2979 copy
= s
= xstrdup(p
);
2980 while (*s
!= '\0') {
2981 idx
= strtol(s
, &s
, 10);
2982 if (*s
!= '\0' && *s
!= ';') {
2986 if (idx
< 0 || idx
>= 256) {
2990 if (colour_palette_set(ictx
->palette
, idx
, -1))
2996 log_debug("bad OSC 104: %s", p
);
2998 screen_write_fullredraw(&ictx
->ctx
);
3003 input_reply_clipboard(struct bufferevent
*bev
, const char *buf
, size_t len
,
3009 if (buf
!= NULL
&& len
!= 0) {
3010 if (len
>= ((size_t)INT_MAX
* 3 / 4) - 1)
3012 outlen
= 4 * ((len
+ 2) / 3) + 1;
3013 out
= xmalloc(outlen
);
3014 if ((outlen
= b64_ntop(buf
, len
, out
, outlen
)) == -1) {
3020 bufferevent_write(bev
, "\033]52;;", 6);
3022 bufferevent_write(bev
, out
, outlen
);
3023 bufferevent_write(bev
, end
, strlen(end
));