Merge branch 'obsd-master'
[tmux.git] / input.c
blob6216cfb88078d5306dc243ff4e1c33225d6b9efb
1 /* $OpenBSD$ */
3 /*
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>
23 #include <ctype.h>
24 #include <resolv.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
29 #include "tmux.h"
32 * Based on the description by Paul Williams at:
34 * https://vt100.net/emu/dec_ansi_parser
36 * With the following changes:
38 * - 7-bit only.
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
45 * the title.
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. */
55 struct input_cell {
56 struct grid_cell cell;
57 int set;
58 int g0set; /* 1 if ACS */
59 int g1set; /* 1 if ACS */
62 /* Input parser argument. */
63 struct input_param {
64 enum {
65 INPUT_MISSING,
66 INPUT_NUMBER,
67 INPUT_STRING
68 } type;
69 union {
70 int num;
71 char *str;
75 /* Input parser context. */
76 struct input_ctx {
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;
85 u_int old_cx;
86 u_int old_cy;
87 int old_mode;
89 u_char interm_buf[4];
90 size_t interm_len;
92 u_char param_buf[64];
93 size_t param_len;
95 #define INPUT_BUF_START 32
96 u_char *input_buf;
97 size_t input_len;
98 size_t input_space;
99 enum {
100 INPUT_END_ST,
101 INPUT_END_BEL
102 } input_end;
104 struct input_param param_list[24];
105 u_int param_list_len;
107 struct utf8_data utf8data;
108 int utf8started;
110 int ch;
111 struct utf8_data last;
113 int flags;
114 #define INPUT_DISCARD 0x1
115 #define INPUT_LAST 0x2
117 const struct input_state *state;
119 struct event timer;
122 * All input received since we were last in the ground state. Sent to
123 * control clients on connection.
125 struct evbuffer *since_ground;
128 /* Helper functions. */
129 struct input_transition;
130 static int input_split(struct input_ctx *);
131 static int input_get(struct input_ctx *, u_int, int, int);
132 static void printflike(2, 3) input_reply(struct input_ctx *, const char *, ...);
133 static void input_set_state(struct input_ctx *,
134 const struct input_transition *);
135 static void input_reset_cell(struct input_ctx *);
137 static void input_osc_4(struct input_ctx *, const char *);
138 static void input_osc_8(struct input_ctx *, const char *);
139 static void input_osc_10(struct input_ctx *, const char *);
140 static void input_osc_11(struct input_ctx *, const char *);
141 static void input_osc_12(struct input_ctx *, const char *);
142 static void input_osc_52(struct input_ctx *, const char *);
143 static void input_osc_104(struct input_ctx *, const char *);
144 static void input_osc_110(struct input_ctx *, const char *);
145 static void input_osc_111(struct input_ctx *, const char *);
146 static void input_osc_112(struct input_ctx *, const char *);
147 static void input_osc_133(struct input_ctx *, const char *);
149 /* Transition entry/exit handlers. */
150 static void input_clear(struct input_ctx *);
151 static void input_ground(struct input_ctx *);
152 static void input_enter_dcs(struct input_ctx *);
153 static void input_enter_osc(struct input_ctx *);
154 static void input_exit_osc(struct input_ctx *);
155 static void input_enter_apc(struct input_ctx *);
156 static void input_exit_apc(struct input_ctx *);
157 static void input_enter_rename(struct input_ctx *);
158 static void input_exit_rename(struct input_ctx *);
160 /* Input state handlers. */
161 static int input_print(struct input_ctx *);
162 static int input_intermediate(struct input_ctx *);
163 static int input_parameter(struct input_ctx *);
164 static int input_input(struct input_ctx *);
165 static int input_c0_dispatch(struct input_ctx *);
166 static int input_esc_dispatch(struct input_ctx *);
167 static int input_csi_dispatch(struct input_ctx *);
168 static void input_csi_dispatch_rm(struct input_ctx *);
169 static void input_csi_dispatch_rm_private(struct input_ctx *);
170 static void input_csi_dispatch_sm(struct input_ctx *);
171 static void input_csi_dispatch_sm_private(struct input_ctx *);
172 static void input_csi_dispatch_sm_graphics(struct input_ctx *);
173 static void input_csi_dispatch_winops(struct input_ctx *);
174 static void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
175 static void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
176 static void input_csi_dispatch_sgr(struct input_ctx *);
177 static int input_dcs_dispatch(struct input_ctx *);
178 static int input_top_bit_set(struct input_ctx *);
179 static int input_end_bel(struct input_ctx *);
181 /* Command table comparison function. */
182 static int input_table_compare(const void *, const void *);
184 /* Command table entry. */
185 struct input_table_entry {
186 int ch;
187 const char *interm;
188 int type;
191 /* Escape commands. */
192 enum input_esc_type {
193 INPUT_ESC_DECALN,
194 INPUT_ESC_DECKPAM,
195 INPUT_ESC_DECKPNM,
196 INPUT_ESC_DECRC,
197 INPUT_ESC_DECSC,
198 INPUT_ESC_HTS,
199 INPUT_ESC_IND,
200 INPUT_ESC_NEL,
201 INPUT_ESC_RI,
202 INPUT_ESC_RIS,
203 INPUT_ESC_SCSG0_OFF,
204 INPUT_ESC_SCSG0_ON,
205 INPUT_ESC_SCSG1_OFF,
206 INPUT_ESC_SCSG1_ON,
207 INPUT_ESC_ST
210 /* Escape command table. */
211 static const struct input_table_entry input_esc_table[] = {
212 { '0', "(", INPUT_ESC_SCSG0_ON },
213 { '0', ")", INPUT_ESC_SCSG1_ON },
214 { '7', "", INPUT_ESC_DECSC },
215 { '8', "", INPUT_ESC_DECRC },
216 { '8', "#", INPUT_ESC_DECALN },
217 { '=', "", INPUT_ESC_DECKPAM },
218 { '>', "", INPUT_ESC_DECKPNM },
219 { 'B', "(", INPUT_ESC_SCSG0_OFF },
220 { 'B', ")", INPUT_ESC_SCSG1_OFF },
221 { 'D', "", INPUT_ESC_IND },
222 { 'E', "", INPUT_ESC_NEL },
223 { 'H', "", INPUT_ESC_HTS },
224 { 'M', "", INPUT_ESC_RI },
225 { '\\', "", INPUT_ESC_ST },
226 { 'c', "", INPUT_ESC_RIS },
229 /* Control (CSI) commands. */
230 enum input_csi_type {
231 INPUT_CSI_CBT,
232 INPUT_CSI_CNL,
233 INPUT_CSI_CPL,
234 INPUT_CSI_CUB,
235 INPUT_CSI_CUD,
236 INPUT_CSI_CUF,
237 INPUT_CSI_CUP,
238 INPUT_CSI_CUU,
239 INPUT_CSI_DA,
240 INPUT_CSI_DA_TWO,
241 INPUT_CSI_DCH,
242 INPUT_CSI_DECSCUSR,
243 INPUT_CSI_DECSTBM,
244 INPUT_CSI_DL,
245 INPUT_CSI_DSR,
246 INPUT_CSI_ECH,
247 INPUT_CSI_ED,
248 INPUT_CSI_EL,
249 INPUT_CSI_HPA,
250 INPUT_CSI_ICH,
251 INPUT_CSI_IL,
252 INPUT_CSI_MODOFF,
253 INPUT_CSI_MODSET,
254 INPUT_CSI_RCP,
255 INPUT_CSI_REP,
256 INPUT_CSI_RM,
257 INPUT_CSI_RM_PRIVATE,
258 INPUT_CSI_SCP,
259 INPUT_CSI_SD,
260 INPUT_CSI_SGR,
261 INPUT_CSI_SM,
262 INPUT_CSI_SM_PRIVATE,
263 INPUT_CSI_SM_GRAPHICS,
264 INPUT_CSI_SU,
265 INPUT_CSI_TBC,
266 INPUT_CSI_VPA,
267 INPUT_CSI_WINOPS,
268 INPUT_CSI_XDA
271 /* Control (CSI) command table. */
272 static const struct input_table_entry input_csi_table[] = {
273 { '@', "", INPUT_CSI_ICH },
274 { 'A', "", INPUT_CSI_CUU },
275 { 'B', "", INPUT_CSI_CUD },
276 { 'C', "", INPUT_CSI_CUF },
277 { 'D', "", INPUT_CSI_CUB },
278 { 'E', "", INPUT_CSI_CNL },
279 { 'F', "", INPUT_CSI_CPL },
280 { 'G', "", INPUT_CSI_HPA },
281 { 'H', "", INPUT_CSI_CUP },
282 { 'J', "", INPUT_CSI_ED },
283 { 'K', "", INPUT_CSI_EL },
284 { 'L', "", INPUT_CSI_IL },
285 { 'M', "", INPUT_CSI_DL },
286 { 'P', "", INPUT_CSI_DCH },
287 { 'S', "", INPUT_CSI_SU },
288 { 'S', "?", INPUT_CSI_SM_GRAPHICS },
289 { 'T', "", INPUT_CSI_SD },
290 { 'X', "", INPUT_CSI_ECH },
291 { 'Z', "", INPUT_CSI_CBT },
292 { '`', "", INPUT_CSI_HPA },
293 { 'b', "", INPUT_CSI_REP },
294 { 'c', "", INPUT_CSI_DA },
295 { 'c', ">", INPUT_CSI_DA_TWO },
296 { 'd', "", INPUT_CSI_VPA },
297 { 'f', "", INPUT_CSI_CUP },
298 { 'g', "", INPUT_CSI_TBC },
299 { 'h', "", INPUT_CSI_SM },
300 { 'h', "?", INPUT_CSI_SM_PRIVATE },
301 { 'l', "", INPUT_CSI_RM },
302 { 'l', "?", INPUT_CSI_RM_PRIVATE },
303 { 'm', "", INPUT_CSI_SGR },
304 { 'm', ">", INPUT_CSI_MODSET },
305 { 'n', "", INPUT_CSI_DSR },
306 { 'n', ">", INPUT_CSI_MODOFF },
307 { 'q', " ", INPUT_CSI_DECSCUSR },
308 { 'q', ">", INPUT_CSI_XDA },
309 { 'r', "", INPUT_CSI_DECSTBM },
310 { 's', "", INPUT_CSI_SCP },
311 { 't', "", INPUT_CSI_WINOPS },
312 { 'u', "", INPUT_CSI_RCP }
315 /* Input transition. */
316 struct input_transition {
317 int first;
318 int last;
320 int (*handler)(struct input_ctx *);
321 const struct input_state *state;
324 /* Input state. */
325 struct input_state {
326 const char *name;
327 void (*enter)(struct input_ctx *);
328 void (*exit)(struct input_ctx *);
329 const struct input_transition *transitions;
332 /* State transitions available from all states. */
333 #define INPUT_STATE_ANYWHERE \
334 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
335 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
336 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
338 /* Forward declarations of state tables. */
339 static const struct input_transition input_state_ground_table[];
340 static const struct input_transition input_state_esc_enter_table[];
341 static const struct input_transition input_state_esc_intermediate_table[];
342 static const struct input_transition input_state_csi_enter_table[];
343 static const struct input_transition input_state_csi_parameter_table[];
344 static const struct input_transition input_state_csi_intermediate_table[];
345 static const struct input_transition input_state_csi_ignore_table[];
346 static const struct input_transition input_state_dcs_enter_table[];
347 static const struct input_transition input_state_dcs_parameter_table[];
348 static const struct input_transition input_state_dcs_intermediate_table[];
349 static const struct input_transition input_state_dcs_handler_table[];
350 static const struct input_transition input_state_dcs_escape_table[];
351 static const struct input_transition input_state_dcs_ignore_table[];
352 static const struct input_transition input_state_osc_string_table[];
353 static const struct input_transition input_state_apc_string_table[];
354 static const struct input_transition input_state_rename_string_table[];
355 static const struct input_transition input_state_consume_st_table[];
357 /* ground state definition. */
358 static const struct input_state input_state_ground = {
359 "ground",
360 input_ground, NULL,
361 input_state_ground_table
364 /* esc_enter state definition. */
365 static const struct input_state input_state_esc_enter = {
366 "esc_enter",
367 input_clear, NULL,
368 input_state_esc_enter_table
371 /* esc_intermediate state definition. */
372 static const struct input_state input_state_esc_intermediate = {
373 "esc_intermediate",
374 NULL, NULL,
375 input_state_esc_intermediate_table
378 /* csi_enter state definition. */
379 static const struct input_state input_state_csi_enter = {
380 "csi_enter",
381 input_clear, NULL,
382 input_state_csi_enter_table
385 /* csi_parameter state definition. */
386 static const struct input_state input_state_csi_parameter = {
387 "csi_parameter",
388 NULL, NULL,
389 input_state_csi_parameter_table
392 /* csi_intermediate state definition. */
393 static const struct input_state input_state_csi_intermediate = {
394 "csi_intermediate",
395 NULL, NULL,
396 input_state_csi_intermediate_table
399 /* csi_ignore state definition. */
400 static const struct input_state input_state_csi_ignore = {
401 "csi_ignore",
402 NULL, NULL,
403 input_state_csi_ignore_table
406 /* dcs_enter state definition. */
407 static const struct input_state input_state_dcs_enter = {
408 "dcs_enter",
409 input_enter_dcs, NULL,
410 input_state_dcs_enter_table
413 /* dcs_parameter state definition. */
414 static const struct input_state input_state_dcs_parameter = {
415 "dcs_parameter",
416 NULL, NULL,
417 input_state_dcs_parameter_table
420 /* dcs_intermediate state definition. */
421 static const struct input_state input_state_dcs_intermediate = {
422 "dcs_intermediate",
423 NULL, NULL,
424 input_state_dcs_intermediate_table
427 /* dcs_handler state definition. */
428 static const struct input_state input_state_dcs_handler = {
429 "dcs_handler",
430 NULL, NULL,
431 input_state_dcs_handler_table
434 /* dcs_escape state definition. */
435 static const struct input_state input_state_dcs_escape = {
436 "dcs_escape",
437 NULL, NULL,
438 input_state_dcs_escape_table
441 /* dcs_ignore state definition. */
442 static const struct input_state input_state_dcs_ignore = {
443 "dcs_ignore",
444 NULL, NULL,
445 input_state_dcs_ignore_table
448 /* osc_string state definition. */
449 static const struct input_state input_state_osc_string = {
450 "osc_string",
451 input_enter_osc, input_exit_osc,
452 input_state_osc_string_table
455 /* apc_string state definition. */
456 static const struct input_state input_state_apc_string = {
457 "apc_string",
458 input_enter_apc, input_exit_apc,
459 input_state_apc_string_table
462 /* rename_string state definition. */
463 static const struct input_state input_state_rename_string = {
464 "rename_string",
465 input_enter_rename, input_exit_rename,
466 input_state_rename_string_table
469 /* consume_st state definition. */
470 static const struct input_state input_state_consume_st = {
471 "consume_st",
472 input_enter_rename, NULL, /* rename also waits for ST */
473 input_state_consume_st_table
476 /* ground state table. */
477 static const struct input_transition input_state_ground_table[] = {
478 INPUT_STATE_ANYWHERE,
480 { 0x00, 0x17, input_c0_dispatch, NULL },
481 { 0x19, 0x19, input_c0_dispatch, NULL },
482 { 0x1c, 0x1f, input_c0_dispatch, NULL },
483 { 0x20, 0x7e, input_print, NULL },
484 { 0x7f, 0x7f, NULL, NULL },
485 { 0x80, 0xff, input_top_bit_set, NULL },
487 { -1, -1, NULL, NULL }
490 /* esc_enter state table. */
491 static const struct input_transition input_state_esc_enter_table[] = {
492 INPUT_STATE_ANYWHERE,
494 { 0x00, 0x17, input_c0_dispatch, NULL },
495 { 0x19, 0x19, input_c0_dispatch, NULL },
496 { 0x1c, 0x1f, input_c0_dispatch, NULL },
497 { 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
498 { 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
499 { 0x50, 0x50, NULL, &input_state_dcs_enter },
500 { 0x51, 0x57, input_esc_dispatch, &input_state_ground },
501 { 0x58, 0x58, NULL, &input_state_consume_st },
502 { 0x59, 0x59, input_esc_dispatch, &input_state_ground },
503 { 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
504 { 0x5b, 0x5b, NULL, &input_state_csi_enter },
505 { 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
506 { 0x5d, 0x5d, NULL, &input_state_osc_string },
507 { 0x5e, 0x5e, NULL, &input_state_consume_st },
508 { 0x5f, 0x5f, NULL, &input_state_apc_string },
509 { 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
510 { 0x6b, 0x6b, NULL, &input_state_rename_string },
511 { 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
512 { 0x7f, 0xff, NULL, NULL },
514 { -1, -1, NULL, NULL }
517 /* esc_intermediate state table. */
518 static const struct input_transition input_state_esc_intermediate_table[] = {
519 INPUT_STATE_ANYWHERE,
521 { 0x00, 0x17, input_c0_dispatch, NULL },
522 { 0x19, 0x19, input_c0_dispatch, NULL },
523 { 0x1c, 0x1f, input_c0_dispatch, NULL },
524 { 0x20, 0x2f, input_intermediate, NULL },
525 { 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
526 { 0x7f, 0xff, NULL, NULL },
528 { -1, -1, NULL, NULL }
531 /* csi_enter state table. */
532 static const struct input_transition input_state_csi_enter_table[] = {
533 INPUT_STATE_ANYWHERE,
535 { 0x00, 0x17, input_c0_dispatch, NULL },
536 { 0x19, 0x19, input_c0_dispatch, NULL },
537 { 0x1c, 0x1f, input_c0_dispatch, NULL },
538 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
539 { 0x30, 0x39, input_parameter, &input_state_csi_parameter },
540 { 0x3a, 0x3a, input_parameter, &input_state_csi_parameter },
541 { 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
542 { 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
543 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
544 { 0x7f, 0xff, NULL, NULL },
546 { -1, -1, NULL, NULL }
549 /* csi_parameter state table. */
550 static const struct input_transition input_state_csi_parameter_table[] = {
551 INPUT_STATE_ANYWHERE,
553 { 0x00, 0x17, input_c0_dispatch, NULL },
554 { 0x19, 0x19, input_c0_dispatch, NULL },
555 { 0x1c, 0x1f, input_c0_dispatch, NULL },
556 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
557 { 0x30, 0x39, input_parameter, NULL },
558 { 0x3a, 0x3a, input_parameter, NULL },
559 { 0x3b, 0x3b, input_parameter, NULL },
560 { 0x3c, 0x3f, NULL, &input_state_csi_ignore },
561 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
562 { 0x7f, 0xff, NULL, NULL },
564 { -1, -1, NULL, NULL }
567 /* csi_intermediate state table. */
568 static const struct input_transition input_state_csi_intermediate_table[] = {
569 INPUT_STATE_ANYWHERE,
571 { 0x00, 0x17, input_c0_dispatch, NULL },
572 { 0x19, 0x19, input_c0_dispatch, NULL },
573 { 0x1c, 0x1f, input_c0_dispatch, NULL },
574 { 0x20, 0x2f, input_intermediate, NULL },
575 { 0x30, 0x3f, NULL, &input_state_csi_ignore },
576 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
577 { 0x7f, 0xff, NULL, NULL },
579 { -1, -1, NULL, NULL }
582 /* csi_ignore state table. */
583 static const struct input_transition input_state_csi_ignore_table[] = {
584 INPUT_STATE_ANYWHERE,
586 { 0x00, 0x17, input_c0_dispatch, NULL },
587 { 0x19, 0x19, input_c0_dispatch, NULL },
588 { 0x1c, 0x1f, input_c0_dispatch, NULL },
589 { 0x20, 0x3f, NULL, NULL },
590 { 0x40, 0x7e, NULL, &input_state_ground },
591 { 0x7f, 0xff, NULL, NULL },
593 { -1, -1, NULL, NULL }
596 /* dcs_enter state table. */
597 static const struct input_transition input_state_dcs_enter_table[] = {
598 INPUT_STATE_ANYWHERE,
600 { 0x00, 0x17, NULL, NULL },
601 { 0x19, 0x19, NULL, NULL },
602 { 0x1c, 0x1f, NULL, NULL },
603 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
604 { 0x30, 0x39, input_parameter, &input_state_dcs_parameter },
605 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
606 { 0x3b, 0x3b, input_parameter, &input_state_dcs_parameter },
607 { 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
608 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
609 { 0x7f, 0xff, NULL, NULL },
611 { -1, -1, NULL, NULL }
614 /* dcs_parameter state table. */
615 static const struct input_transition input_state_dcs_parameter_table[] = {
616 INPUT_STATE_ANYWHERE,
618 { 0x00, 0x17, NULL, NULL },
619 { 0x19, 0x19, NULL, NULL },
620 { 0x1c, 0x1f, NULL, NULL },
621 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
622 { 0x30, 0x39, input_parameter, NULL },
623 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
624 { 0x3b, 0x3b, input_parameter, NULL },
625 { 0x3c, 0x3f, NULL, &input_state_dcs_ignore },
626 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
627 { 0x7f, 0xff, NULL, NULL },
629 { -1, -1, NULL, NULL }
632 /* dcs_intermediate state table. */
633 static const struct input_transition input_state_dcs_intermediate_table[] = {
634 INPUT_STATE_ANYWHERE,
636 { 0x00, 0x17, NULL, NULL },
637 { 0x19, 0x19, NULL, NULL },
638 { 0x1c, 0x1f, NULL, NULL },
639 { 0x20, 0x2f, input_intermediate, NULL },
640 { 0x30, 0x3f, NULL, &input_state_dcs_ignore },
641 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
642 { 0x7f, 0xff, NULL, NULL },
644 { -1, -1, NULL, NULL }
647 /* dcs_handler state table. */
648 static const struct input_transition input_state_dcs_handler_table[] = {
649 /* No INPUT_STATE_ANYWHERE */
651 { 0x00, 0x1a, input_input, NULL },
652 { 0x1b, 0x1b, NULL, &input_state_dcs_escape },
653 { 0x1c, 0xff, input_input, NULL },
655 { -1, -1, NULL, NULL }
658 /* dcs_escape state table. */
659 static const struct input_transition input_state_dcs_escape_table[] = {
660 /* No INPUT_STATE_ANYWHERE */
662 { 0x00, 0x5b, input_input, &input_state_dcs_handler },
663 { 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
664 { 0x5d, 0xff, input_input, &input_state_dcs_handler },
666 { -1, -1, NULL, NULL }
669 /* dcs_ignore state table. */
670 static const struct input_transition input_state_dcs_ignore_table[] = {
671 INPUT_STATE_ANYWHERE,
673 { 0x00, 0x17, NULL, NULL },
674 { 0x19, 0x19, NULL, NULL },
675 { 0x1c, 0x1f, NULL, NULL },
676 { 0x20, 0xff, NULL, NULL },
678 { -1, -1, NULL, NULL }
681 /* osc_string state table. */
682 static const struct input_transition input_state_osc_string_table[] = {
683 INPUT_STATE_ANYWHERE,
685 { 0x00, 0x06, NULL, NULL },
686 { 0x07, 0x07, input_end_bel, &input_state_ground },
687 { 0x08, 0x17, NULL, NULL },
688 { 0x19, 0x19, NULL, NULL },
689 { 0x1c, 0x1f, NULL, NULL },
690 { 0x20, 0xff, input_input, NULL },
692 { -1, -1, NULL, NULL }
695 /* apc_string state table. */
696 static const struct input_transition input_state_apc_string_table[] = {
697 INPUT_STATE_ANYWHERE,
699 { 0x00, 0x17, NULL, NULL },
700 { 0x19, 0x19, NULL, NULL },
701 { 0x1c, 0x1f, NULL, NULL },
702 { 0x20, 0xff, input_input, NULL },
704 { -1, -1, NULL, NULL }
707 /* rename_string state table. */
708 static const struct input_transition input_state_rename_string_table[] = {
709 INPUT_STATE_ANYWHERE,
711 { 0x00, 0x17, NULL, NULL },
712 { 0x19, 0x19, NULL, NULL },
713 { 0x1c, 0x1f, NULL, NULL },
714 { 0x20, 0xff, input_input, NULL },
716 { -1, -1, NULL, NULL }
719 /* consume_st state table. */
720 static const struct input_transition input_state_consume_st_table[] = {
721 INPUT_STATE_ANYWHERE,
723 { 0x00, 0x17, NULL, NULL },
724 { 0x19, 0x19, NULL, NULL },
725 { 0x1c, 0x1f, NULL, NULL },
726 { 0x20, 0xff, NULL, NULL },
728 { -1, -1, NULL, NULL }
731 /* Maximum of bytes allowed to read in a single input. */
732 static size_t input_buffer_size = INPUT_BUF_DEFAULT_SIZE;
734 /* Input table compare. */
735 static int
736 input_table_compare(const void *key, const void *value)
738 const struct input_ctx *ictx = key;
739 const struct input_table_entry *entry = value;
741 if (ictx->ch != entry->ch)
742 return (ictx->ch - entry->ch);
743 return (strcmp(ictx->interm_buf, entry->interm));
747 * Timer - if this expires then have been waiting for a terminator for too
748 * long, so reset to ground.
750 static void
751 input_timer_callback(__unused int fd, __unused short events, void *arg)
753 struct input_ctx *ictx = arg;
755 log_debug("%s: %s expired" , __func__, ictx->state->name);
756 input_reset(ictx, 0);
759 /* Start the timer. */
760 static void
761 input_start_timer(struct input_ctx *ictx)
763 struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
765 event_del(&ictx->timer);
766 event_add(&ictx->timer, &tv);
769 /* Reset cell state to default. */
770 static void
771 input_reset_cell(struct input_ctx *ictx)
773 memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
774 ictx->cell.set = 0;
775 ictx->cell.g0set = ictx->cell.g1set = 0;
777 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
778 ictx->old_cx = 0;
779 ictx->old_cy = 0;
782 /* Save screen state. */
783 static void
784 input_save_state(struct input_ctx *ictx)
786 struct screen_write_ctx *sctx = &ictx->ctx;
787 struct screen *s = sctx->s;
789 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
790 ictx->old_cx = s->cx;
791 ictx->old_cy = s->cy;
792 ictx->old_mode = s->mode;
795 /* Restore screen state. */
796 static void
797 input_restore_state(struct input_ctx *ictx)
799 struct screen_write_ctx *sctx = &ictx->ctx;
801 memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
802 if (ictx->old_mode & MODE_ORIGIN)
803 screen_write_mode_set(sctx, MODE_ORIGIN);
804 else
805 screen_write_mode_clear(sctx, MODE_ORIGIN);
806 screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy, 0);
809 /* Initialise input parser. */
810 struct input_ctx *
811 input_init(struct window_pane *wp, struct bufferevent *bev,
812 struct colour_palette *palette)
814 struct input_ctx *ictx;
816 ictx = xcalloc(1, sizeof *ictx);
817 ictx->wp = wp;
818 ictx->event = bev;
819 ictx->palette = palette;
821 ictx->input_space = INPUT_BUF_START;
822 ictx->input_buf = xmalloc(INPUT_BUF_START);
824 ictx->since_ground = evbuffer_new();
825 if (ictx->since_ground == NULL)
826 fatalx("out of memory");
828 evtimer_set(&ictx->timer, input_timer_callback, ictx);
830 input_reset(ictx, 0);
831 return (ictx);
834 /* Destroy input parser. */
835 void
836 input_free(struct input_ctx *ictx)
838 u_int i;
840 for (i = 0; i < ictx->param_list_len; i++) {
841 if (ictx->param_list[i].type == INPUT_STRING)
842 free(ictx->param_list[i].str);
845 event_del(&ictx->timer);
847 free(ictx->input_buf);
848 evbuffer_free(ictx->since_ground);
850 free(ictx);
853 /* Reset input state and clear screen. */
854 void
855 input_reset(struct input_ctx *ictx, int clear)
857 struct screen_write_ctx *sctx = &ictx->ctx;
858 struct window_pane *wp = ictx->wp;
860 input_reset_cell(ictx);
862 if (clear && wp != NULL) {
863 if (TAILQ_EMPTY(&wp->modes))
864 screen_write_start_pane(sctx, wp, &wp->base);
865 else
866 screen_write_start(sctx, &wp->base);
867 screen_write_reset(sctx);
868 screen_write_stop(sctx);
871 input_clear(ictx);
873 ictx->state = &input_state_ground;
874 ictx->flags = 0;
877 /* Return pending data. */
878 struct evbuffer *
879 input_pending(struct input_ctx *ictx)
881 return (ictx->since_ground);
884 /* Change input state. */
885 static void
886 input_set_state(struct input_ctx *ictx, const struct input_transition *itr)
888 if (ictx->state->exit != NULL)
889 ictx->state->exit(ictx);
890 ictx->state = itr->state;
891 if (ictx->state->enter != NULL)
892 ictx->state->enter(ictx);
895 /* Parse data. */
896 static void
897 input_parse(struct input_ctx *ictx, u_char *buf, size_t len)
899 struct screen_write_ctx *sctx = &ictx->ctx;
900 const struct input_state *state = NULL;
901 const struct input_transition *itr = NULL;
902 size_t off = 0;
904 /* Parse the input. */
905 while (off < len) {
906 ictx->ch = buf[off++];
908 /* Find the transition. */
909 if (ictx->state != state ||
910 itr == NULL ||
911 ictx->ch < itr->first ||
912 ictx->ch > itr->last) {
913 itr = ictx->state->transitions;
914 while (itr->first != -1 && itr->last != -1) {
915 if (ictx->ch >= itr->first &&
916 ictx->ch <= itr->last)
917 break;
918 itr++;
920 if (itr->first == -1 || itr->last == -1) {
921 /* No transition? Eh? */
922 fatalx("no transition from state");
925 state = ictx->state;
928 * Any state except print stops the current collection. This is
929 * an optimization to avoid checking if the attributes have
930 * changed for every character. It will stop unnecessarily for
931 * sequences that don't make a terminal change, but they should
932 * be the minority.
934 if (itr->handler != input_print)
935 screen_write_collect_end(sctx);
938 * Execute the handler, if any. Don't switch state if it
939 * returns non-zero.
941 if (itr->handler != NULL && itr->handler(ictx) != 0)
942 continue;
944 /* And switch state, if necessary. */
945 if (itr->state != NULL)
946 input_set_state(ictx, itr);
948 /* If not in ground state, save input. */
949 if (ictx->state != &input_state_ground)
950 evbuffer_add(ictx->since_ground, &ictx->ch, 1);
954 /* Parse input from pane. */
955 void
956 input_parse_pane(struct window_pane *wp)
958 void *new_data;
959 size_t new_size;
961 new_data = window_pane_get_new_data(wp, &wp->offset, &new_size);
962 input_parse_buffer(wp, new_data, new_size);
963 window_pane_update_used_data(wp, &wp->offset, new_size);
966 /* Parse given input. */
967 void
968 input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
970 struct input_ctx *ictx = wp->ictx;
971 struct screen_write_ctx *sctx = &ictx->ctx;
973 if (len == 0)
974 return;
976 window_update_activity(wp->window);
977 wp->flags |= PANE_CHANGED;
979 /* Flag new input while in a mode. */
980 if (!TAILQ_EMPTY(&wp->modes))
981 wp->flags |= PANE_UNSEENCHANGES;
983 /* NULL wp if there is a mode set as don't want to update the tty. */
984 if (TAILQ_EMPTY(&wp->modes))
985 screen_write_start_pane(sctx, wp, &wp->base);
986 else
987 screen_write_start(sctx, &wp->base);
989 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
990 ictx->state->name, len, (int)len, buf);
992 input_parse(ictx, buf, len);
993 screen_write_stop(sctx);
996 /* Parse given input for screen. */
997 void
998 input_parse_screen(struct input_ctx *ictx, struct screen *s,
999 screen_write_init_ctx_cb cb, void *arg, u_char *buf, size_t len)
1001 struct screen_write_ctx *sctx = &ictx->ctx;
1003 if (len == 0)
1004 return;
1006 screen_write_start_callback(sctx, s, cb, arg);
1007 input_parse(ictx, buf, len);
1008 screen_write_stop(sctx);
1011 /* Split the parameter list (if any). */
1012 static int
1013 input_split(struct input_ctx *ictx)
1015 const char *errstr;
1016 char *ptr, *out;
1017 struct input_param *ip;
1018 u_int i;
1020 for (i = 0; i < ictx->param_list_len; i++) {
1021 if (ictx->param_list[i].type == INPUT_STRING)
1022 free(ictx->param_list[i].str);
1024 ictx->param_list_len = 0;
1026 if (ictx->param_len == 0)
1027 return (0);
1028 ip = &ictx->param_list[0];
1030 ptr = ictx->param_buf;
1031 while ((out = strsep(&ptr, ";")) != NULL) {
1032 if (*out == '\0')
1033 ip->type = INPUT_MISSING;
1034 else {
1035 if (strchr(out, ':') != NULL) {
1036 ip->type = INPUT_STRING;
1037 ip->str = xstrdup(out);
1038 } else {
1039 ip->type = INPUT_NUMBER;
1040 ip->num = strtonum(out, 0, INT_MAX, &errstr);
1041 if (errstr != NULL)
1042 return (-1);
1045 ip = &ictx->param_list[++ictx->param_list_len];
1046 if (ictx->param_list_len == nitems(ictx->param_list))
1047 return (-1);
1050 for (i = 0; i < ictx->param_list_len; i++) {
1051 ip = &ictx->param_list[i];
1052 if (ip->type == INPUT_MISSING)
1053 log_debug("parameter %u: missing", i);
1054 else if (ip->type == INPUT_STRING)
1055 log_debug("parameter %u: string %s", i, ip->str);
1056 else if (ip->type == INPUT_NUMBER)
1057 log_debug("parameter %u: number %d", i, ip->num);
1060 return (0);
1063 /* Get an argument or return default value. */
1064 static int
1065 input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1067 struct input_param *ip;
1068 int retval;
1070 if (validx >= ictx->param_list_len)
1071 return (defval);
1072 ip = &ictx->param_list[validx];
1073 if (ip->type == INPUT_MISSING)
1074 return (defval);
1075 if (ip->type == INPUT_STRING)
1076 return (-1);
1077 retval = ip->num;
1078 if (retval < minval)
1079 return (minval);
1080 return (retval);
1083 /* Reply to terminal query. */
1084 static void
1085 input_reply(struct input_ctx *ictx, const char *fmt, ...)
1087 struct bufferevent *bev = ictx->event;
1088 va_list ap;
1089 char *reply;
1091 if (bev == NULL)
1092 return;
1094 va_start(ap, fmt);
1095 xvasprintf(&reply, fmt, ap);
1096 va_end(ap);
1098 log_debug("%s: %s", __func__, reply);
1099 bufferevent_write(bev, reply, strlen(reply));
1100 free(reply);
1103 /* Clear saved state. */
1104 static void
1105 input_clear(struct input_ctx *ictx)
1107 event_del(&ictx->timer);
1109 *ictx->interm_buf = '\0';
1110 ictx->interm_len = 0;
1112 *ictx->param_buf = '\0';
1113 ictx->param_len = 0;
1115 *ictx->input_buf = '\0';
1116 ictx->input_len = 0;
1118 ictx->input_end = INPUT_END_ST;
1120 ictx->flags &= ~INPUT_DISCARD;
1123 /* Reset for ground state. */
1124 static void
1125 input_ground(struct input_ctx *ictx)
1127 event_del(&ictx->timer);
1128 evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
1130 if (ictx->input_space > INPUT_BUF_START) {
1131 ictx->input_space = INPUT_BUF_START;
1132 ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1136 /* Output this character to the screen. */
1137 static int
1138 input_print(struct input_ctx *ictx)
1140 struct screen_write_ctx *sctx = &ictx->ctx;
1141 int set;
1143 ictx->utf8started = 0; /* can't be valid UTF-8 */
1145 set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1146 if (set == 1)
1147 ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1148 else
1149 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1150 utf8_set(&ictx->cell.cell.data, ictx->ch);
1151 screen_write_collect_add(sctx, &ictx->cell.cell);
1153 utf8_copy(&ictx->last, &ictx->cell.cell.data);
1154 ictx->flags |= INPUT_LAST;
1156 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1158 return (0);
1161 /* Collect intermediate string. */
1162 static int
1163 input_intermediate(struct input_ctx *ictx)
1165 if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
1166 ictx->flags |= INPUT_DISCARD;
1167 else {
1168 ictx->interm_buf[ictx->interm_len++] = ictx->ch;
1169 ictx->interm_buf[ictx->interm_len] = '\0';
1172 return (0);
1175 /* Collect parameter string. */
1176 static int
1177 input_parameter(struct input_ctx *ictx)
1179 if (ictx->param_len == (sizeof ictx->param_buf) - 1)
1180 ictx->flags |= INPUT_DISCARD;
1181 else {
1182 ictx->param_buf[ictx->param_len++] = ictx->ch;
1183 ictx->param_buf[ictx->param_len] = '\0';
1186 return (0);
1189 /* Collect input string. */
1190 static int
1191 input_input(struct input_ctx *ictx)
1193 size_t available;
1195 available = ictx->input_space;
1196 while (ictx->input_len + 1 >= available) {
1197 available *= 2;
1198 if (available > input_buffer_size) {
1199 ictx->flags |= INPUT_DISCARD;
1200 return (0);
1202 ictx->input_buf = xrealloc(ictx->input_buf, available);
1203 ictx->input_space = available;
1205 ictx->input_buf[ictx->input_len++] = ictx->ch;
1206 ictx->input_buf[ictx->input_len] = '\0';
1208 return (0);
1211 /* Execute C0 control sequence. */
1212 static int
1213 input_c0_dispatch(struct input_ctx *ictx)
1215 struct screen_write_ctx *sctx = &ictx->ctx;
1216 struct window_pane *wp = ictx->wp;
1217 struct screen *s = sctx->s;
1218 struct grid_cell gc, first_gc;
1219 u_int cx = s->cx, line = s->cy + s->grid->hsize;
1220 u_int width;
1221 int has_content = 0;
1223 ictx->utf8started = 0; /* can't be valid UTF-8 */
1225 log_debug("%s: '%c'", __func__, ictx->ch);
1227 switch (ictx->ch) {
1228 case '\000': /* NUL */
1229 break;
1230 case '\007': /* BEL */
1231 if (wp != NULL)
1232 alerts_queue(wp->window, WINDOW_BELL);
1233 break;
1234 case '\010': /* BS */
1235 screen_write_backspace(sctx);
1236 break;
1237 case '\011': /* HT */
1238 /* Don't tab beyond the end of the line. */
1239 if (s->cx >= screen_size_x(s) - 1)
1240 break;
1242 /* Find the next tab point, or use the last column if none. */
1243 grid_get_cell(s->grid, s->cx, line, &first_gc);
1244 do {
1245 if (!has_content) {
1246 grid_get_cell(s->grid, cx, line, &gc);
1247 if (gc.data.size != 1 ||
1248 *gc.data.data != ' ' ||
1249 !grid_cells_look_equal(&gc, &first_gc))
1250 has_content = 1;
1252 cx++;
1253 if (bit_test(s->tabs, cx))
1254 break;
1255 } while (cx < screen_size_x(s) - 1);
1257 width = cx - s->cx;
1258 if (has_content || width > sizeof gc.data.data)
1259 s->cx = cx;
1260 else {
1261 grid_get_cell(s->grid, s->cx, line, &gc);
1262 grid_set_tab(&gc, width);
1263 screen_write_collect_add(sctx, &gc);
1265 break;
1266 case '\012': /* LF */
1267 case '\013': /* VT */
1268 case '\014': /* FF */
1269 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1270 if (s->mode & MODE_CRLF)
1271 screen_write_carriagereturn(sctx);
1272 break;
1273 case '\015': /* CR */
1274 screen_write_carriagereturn(sctx);
1275 break;
1276 case '\016': /* SO */
1277 ictx->cell.set = 1;
1278 break;
1279 case '\017': /* SI */
1280 ictx->cell.set = 0;
1281 break;
1282 default:
1283 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1284 break;
1287 ictx->flags &= ~INPUT_LAST;
1288 return (0);
1291 /* Execute escape sequence. */
1292 static int
1293 input_esc_dispatch(struct input_ctx *ictx)
1295 struct screen_write_ctx *sctx = &ictx->ctx;
1296 struct screen *s = sctx->s;
1297 struct input_table_entry *entry;
1299 if (ictx->flags & INPUT_DISCARD)
1300 return (0);
1301 log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1303 entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
1304 sizeof input_esc_table[0], input_table_compare);
1305 if (entry == NULL) {
1306 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1307 return (0);
1310 switch (entry->type) {
1311 case INPUT_ESC_RIS:
1312 colour_palette_clear(ictx->palette);
1313 input_reset_cell(ictx);
1314 screen_write_reset(sctx);
1315 screen_write_fullredraw(sctx);
1316 break;
1317 case INPUT_ESC_IND:
1318 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1319 break;
1320 case INPUT_ESC_NEL:
1321 screen_write_carriagereturn(sctx);
1322 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1323 break;
1324 case INPUT_ESC_HTS:
1325 if (s->cx < screen_size_x(s))
1326 bit_set(s->tabs, s->cx);
1327 break;
1328 case INPUT_ESC_RI:
1329 screen_write_reverseindex(sctx, ictx->cell.cell.bg);
1330 break;
1331 case INPUT_ESC_DECKPAM:
1332 screen_write_mode_set(sctx, MODE_KKEYPAD);
1333 break;
1334 case INPUT_ESC_DECKPNM:
1335 screen_write_mode_clear(sctx, MODE_KKEYPAD);
1336 break;
1337 case INPUT_ESC_DECSC:
1338 input_save_state(ictx);
1339 break;
1340 case INPUT_ESC_DECRC:
1341 input_restore_state(ictx);
1342 break;
1343 case INPUT_ESC_DECALN:
1344 screen_write_alignmenttest(sctx);
1345 break;
1346 case INPUT_ESC_SCSG0_ON:
1347 ictx->cell.g0set = 1;
1348 break;
1349 case INPUT_ESC_SCSG0_OFF:
1350 ictx->cell.g0set = 0;
1351 break;
1352 case INPUT_ESC_SCSG1_ON:
1353 ictx->cell.g1set = 1;
1354 break;
1355 case INPUT_ESC_SCSG1_OFF:
1356 ictx->cell.g1set = 0;
1357 break;
1358 case INPUT_ESC_ST:
1359 /* ST terminates OSC but the state transition already did it. */
1360 break;
1363 ictx->flags &= ~INPUT_LAST;
1364 return (0);
1367 /* Execute control sequence. */
1368 static int
1369 input_csi_dispatch(struct input_ctx *ictx)
1371 struct screen_write_ctx *sctx = &ictx->ctx;
1372 struct screen *s = sctx->s;
1373 struct input_table_entry *entry;
1374 int i, n, m, ek, set;
1375 u_int cx, bg = ictx->cell.cell.bg;
1377 if (ictx->flags & INPUT_DISCARD)
1378 return (0);
1380 log_debug("%s: '%c' \"%s\" \"%s\"", __func__, ictx->ch,
1381 ictx->interm_buf, ictx->param_buf);
1383 if (input_split(ictx) != 0)
1384 return (0);
1386 entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
1387 sizeof input_csi_table[0], input_table_compare);
1388 if (entry == NULL) {
1389 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1390 return (0);
1393 switch (entry->type) {
1394 case INPUT_CSI_CBT:
1395 /* Find the previous tab point, n times. */
1396 cx = s->cx;
1397 if (cx > screen_size_x(s) - 1)
1398 cx = screen_size_x(s) - 1;
1399 n = input_get(ictx, 0, 1, 1);
1400 if (n == -1)
1401 break;
1402 while (cx > 0 && n-- > 0) {
1404 cx--;
1405 while (cx > 0 && !bit_test(s->tabs, cx));
1407 s->cx = cx;
1408 break;
1409 case INPUT_CSI_CUB:
1410 n = input_get(ictx, 0, 1, 1);
1411 if (n != -1)
1412 screen_write_cursorleft(sctx, n);
1413 break;
1414 case INPUT_CSI_CUD:
1415 n = input_get(ictx, 0, 1, 1);
1416 if (n != -1)
1417 screen_write_cursordown(sctx, n);
1418 break;
1419 case INPUT_CSI_CUF:
1420 n = input_get(ictx, 0, 1, 1);
1421 if (n != -1)
1422 screen_write_cursorright(sctx, n);
1423 break;
1424 case INPUT_CSI_CUP:
1425 n = input_get(ictx, 0, 1, 1);
1426 m = input_get(ictx, 1, 1, 1);
1427 if (n != -1 && m != -1)
1428 screen_write_cursormove(sctx, m - 1, n - 1, 1);
1429 break;
1430 case INPUT_CSI_MODSET:
1431 n = input_get(ictx, 0, 0, 0);
1432 if (n != 4)
1433 break;
1434 m = input_get(ictx, 1, 0, 0);
1437 * Set the extended key reporting mode as per the client
1438 * request, unless "extended-keys" is set to "off".
1440 ek = options_get_number(global_options, "extended-keys");
1441 if (ek == 0)
1442 break;
1443 screen_write_mode_clear(sctx, EXTENDED_KEY_MODES);
1444 if (m == 2)
1445 screen_write_mode_set(sctx, MODE_KEYS_EXTENDED_2);
1446 else if (m == 1 || ek == 2)
1447 screen_write_mode_set(sctx, MODE_KEYS_EXTENDED);
1448 break;
1449 case INPUT_CSI_MODOFF:
1450 n = input_get(ictx, 0, 0, 0);
1451 if (n != 4)
1452 break;
1455 * Clear the extended key reporting mode as per the client
1456 * request, unless "extended-keys always" forces into mode 1.
1458 screen_write_mode_clear(sctx,
1459 MODE_KEYS_EXTENDED|MODE_KEYS_EXTENDED_2);
1460 if (options_get_number(global_options, "extended-keys") == 2)
1461 screen_write_mode_set(sctx, MODE_KEYS_EXTENDED);
1462 break;
1463 case INPUT_CSI_WINOPS:
1464 input_csi_dispatch_winops(ictx);
1465 break;
1466 case INPUT_CSI_CUU:
1467 n = input_get(ictx, 0, 1, 1);
1468 if (n != -1)
1469 screen_write_cursorup(sctx, n);
1470 break;
1471 case INPUT_CSI_CNL:
1472 n = input_get(ictx, 0, 1, 1);
1473 if (n != -1) {
1474 screen_write_carriagereturn(sctx);
1475 screen_write_cursordown(sctx, n);
1477 break;
1478 case INPUT_CSI_CPL:
1479 n = input_get(ictx, 0, 1, 1);
1480 if (n != -1) {
1481 screen_write_carriagereturn(sctx);
1482 screen_write_cursorup(sctx, n);
1484 break;
1485 case INPUT_CSI_DA:
1486 switch (input_get(ictx, 0, 0, 0)) {
1487 case -1:
1488 break;
1489 case 0:
1490 #ifdef ENABLE_SIXEL
1491 input_reply(ictx, "\033[?1;2;4c");
1492 #else
1493 input_reply(ictx, "\033[?1;2c");
1494 #endif
1495 break;
1496 default:
1497 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1498 break;
1500 break;
1501 case INPUT_CSI_DA_TWO:
1502 switch (input_get(ictx, 0, 0, 0)) {
1503 case -1:
1504 break;
1505 case 0:
1506 input_reply(ictx, "\033[>84;0;0c");
1507 break;
1508 default:
1509 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1510 break;
1512 break;
1513 case INPUT_CSI_ECH:
1514 n = input_get(ictx, 0, 1, 1);
1515 if (n != -1)
1516 screen_write_clearcharacter(sctx, n, bg);
1517 break;
1518 case INPUT_CSI_DCH:
1519 n = input_get(ictx, 0, 1, 1);
1520 if (n != -1)
1521 screen_write_deletecharacter(sctx, n, bg);
1522 break;
1523 case INPUT_CSI_DECSTBM:
1524 n = input_get(ictx, 0, 1, 1);
1525 m = input_get(ictx, 1, 1, screen_size_y(s));
1526 if (n != -1 && m != -1)
1527 screen_write_scrollregion(sctx, n - 1, m - 1);
1528 break;
1529 case INPUT_CSI_DL:
1530 n = input_get(ictx, 0, 1, 1);
1531 if (n != -1)
1532 screen_write_deleteline(sctx, n, bg);
1533 break;
1534 case INPUT_CSI_DSR:
1535 switch (input_get(ictx, 0, 0, 0)) {
1536 case -1:
1537 break;
1538 case 5:
1539 input_reply(ictx, "\033[0n");
1540 break;
1541 case 6:
1542 input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1543 break;
1544 default:
1545 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1546 break;
1548 break;
1549 case INPUT_CSI_ED:
1550 switch (input_get(ictx, 0, 0, 0)) {
1551 case -1:
1552 break;
1553 case 0:
1554 screen_write_clearendofscreen(sctx, bg);
1555 break;
1556 case 1:
1557 screen_write_clearstartofscreen(sctx, bg);
1558 break;
1559 case 2:
1560 screen_write_clearscreen(sctx, bg);
1561 break;
1562 case 3:
1563 if (input_get(ictx, 1, 0, 0) == 0) {
1565 * Linux console extension to clear history
1566 * (for example before locking the screen).
1568 screen_write_clearhistory(sctx);
1570 break;
1571 default:
1572 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1573 break;
1575 break;
1576 case INPUT_CSI_EL:
1577 switch (input_get(ictx, 0, 0, 0)) {
1578 case -1:
1579 break;
1580 case 0:
1581 screen_write_clearendofline(sctx, bg);
1582 break;
1583 case 1:
1584 screen_write_clearstartofline(sctx, bg);
1585 break;
1586 case 2:
1587 screen_write_clearline(sctx, bg);
1588 break;
1589 default:
1590 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1591 break;
1593 break;
1594 case INPUT_CSI_HPA:
1595 n = input_get(ictx, 0, 1, 1);
1596 if (n != -1)
1597 screen_write_cursormove(sctx, n - 1, -1, 1);
1598 break;
1599 case INPUT_CSI_ICH:
1600 n = input_get(ictx, 0, 1, 1);
1601 if (n != -1)
1602 screen_write_insertcharacter(sctx, n, bg);
1603 break;
1604 case INPUT_CSI_IL:
1605 n = input_get(ictx, 0, 1, 1);
1606 if (n != -1)
1607 screen_write_insertline(sctx, n, bg);
1608 break;
1609 case INPUT_CSI_REP:
1610 n = input_get(ictx, 0, 1, 1);
1611 if (n == -1)
1612 break;
1614 m = screen_size_x(s) - s->cx;
1615 if (n > m)
1616 n = m;
1618 if (~ictx->flags & INPUT_LAST)
1619 break;
1621 set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1622 if (set == 1)
1623 ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1624 else
1625 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1626 utf8_copy(&ictx->cell.cell.data, &ictx->last);
1627 for (i = 0; i < n; i++)
1628 screen_write_collect_add(sctx, &ictx->cell.cell);
1629 break;
1630 case INPUT_CSI_RCP:
1631 input_restore_state(ictx);
1632 break;
1633 case INPUT_CSI_RM:
1634 input_csi_dispatch_rm(ictx);
1635 break;
1636 case INPUT_CSI_RM_PRIVATE:
1637 input_csi_dispatch_rm_private(ictx);
1638 break;
1639 case INPUT_CSI_SCP:
1640 input_save_state(ictx);
1641 break;
1642 case INPUT_CSI_SGR:
1643 input_csi_dispatch_sgr(ictx);
1644 break;
1645 case INPUT_CSI_SM:
1646 input_csi_dispatch_sm(ictx);
1647 break;
1648 case INPUT_CSI_SM_PRIVATE:
1649 input_csi_dispatch_sm_private(ictx);
1650 break;
1651 case INPUT_CSI_SM_GRAPHICS:
1652 input_csi_dispatch_sm_graphics(ictx);
1653 break;
1654 case INPUT_CSI_SU:
1655 n = input_get(ictx, 0, 1, 1);
1656 if (n != -1)
1657 screen_write_scrollup(sctx, n, bg);
1658 break;
1659 case INPUT_CSI_SD:
1660 n = input_get(ictx, 0, 1, 1);
1661 if (n != -1)
1662 screen_write_scrolldown(sctx, n, bg);
1663 break;
1664 case INPUT_CSI_TBC:
1665 switch (input_get(ictx, 0, 0, 0)) {
1666 case -1:
1667 break;
1668 case 0:
1669 if (s->cx < screen_size_x(s))
1670 bit_clear(s->tabs, s->cx);
1671 break;
1672 case 3:
1673 bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1674 break;
1675 default:
1676 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1677 break;
1679 break;
1680 case INPUT_CSI_VPA:
1681 n = input_get(ictx, 0, 1, 1);
1682 if (n != -1)
1683 screen_write_cursormove(sctx, -1, n - 1, 1);
1684 break;
1685 case INPUT_CSI_DECSCUSR:
1686 n = input_get(ictx, 0, 0, 0);
1687 if (n != -1)
1688 screen_set_cursor_style(n, &s->cstyle, &s->mode);
1689 break;
1690 case INPUT_CSI_XDA:
1691 n = input_get(ictx, 0, 0, 0);
1692 if (n == 0)
1693 input_reply(ictx, "\033P>|tmux %s\033\\", getversion());
1694 break;
1698 ictx->flags &= ~INPUT_LAST;
1699 return (0);
1702 /* Handle CSI RM. */
1703 static void
1704 input_csi_dispatch_rm(struct input_ctx *ictx)
1706 struct screen_write_ctx *sctx = &ictx->ctx;
1707 u_int i;
1709 for (i = 0; i < ictx->param_list_len; i++) {
1710 switch (input_get(ictx, i, 0, -1)) {
1711 case -1:
1712 break;
1713 case 4: /* IRM */
1714 screen_write_mode_clear(sctx, MODE_INSERT);
1715 break;
1716 case 34:
1717 screen_write_mode_set(sctx, MODE_CURSOR_VERY_VISIBLE);
1718 break;
1719 default:
1720 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1721 break;
1726 /* Handle CSI private RM. */
1727 static void
1728 input_csi_dispatch_rm_private(struct input_ctx *ictx)
1730 struct screen_write_ctx *sctx = &ictx->ctx;
1731 struct grid_cell *gc = &ictx->cell.cell;
1732 u_int i;
1734 for (i = 0; i < ictx->param_list_len; i++) {
1735 switch (input_get(ictx, i, 0, -1)) {
1736 case -1:
1737 break;
1738 case 1: /* DECCKM */
1739 screen_write_mode_clear(sctx, MODE_KCURSOR);
1740 break;
1741 case 3: /* DECCOLM */
1742 screen_write_cursormove(sctx, 0, 0, 1);
1743 screen_write_clearscreen(sctx, gc->bg);
1744 break;
1745 case 6: /* DECOM */
1746 screen_write_mode_clear(sctx, MODE_ORIGIN);
1747 screen_write_cursormove(sctx, 0, 0, 1);
1748 break;
1749 case 7: /* DECAWM */
1750 screen_write_mode_clear(sctx, MODE_WRAP);
1751 break;
1752 case 12:
1753 screen_write_mode_clear(sctx, MODE_CURSOR_BLINKING);
1754 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET);
1755 break;
1756 case 25: /* TCEM */
1757 screen_write_mode_clear(sctx, MODE_CURSOR);
1758 break;
1759 case 1000:
1760 case 1001:
1761 case 1002:
1762 case 1003:
1763 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1764 break;
1765 case 1004:
1766 screen_write_mode_clear(sctx, MODE_FOCUSON);
1767 break;
1768 case 1005:
1769 screen_write_mode_clear(sctx, MODE_MOUSE_UTF8);
1770 break;
1771 case 1006:
1772 screen_write_mode_clear(sctx, MODE_MOUSE_SGR);
1773 break;
1774 case 47:
1775 case 1047:
1776 screen_write_alternateoff(sctx, gc, 0);
1777 break;
1778 case 1049:
1779 screen_write_alternateoff(sctx, gc, 1);
1780 break;
1781 case 2004:
1782 screen_write_mode_clear(sctx, MODE_BRACKETPASTE);
1783 break;
1784 default:
1785 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1786 break;
1791 /* Handle CSI SM. */
1792 static void
1793 input_csi_dispatch_sm(struct input_ctx *ictx)
1795 struct screen_write_ctx *sctx = &ictx->ctx;
1796 u_int i;
1798 for (i = 0; i < ictx->param_list_len; i++) {
1799 switch (input_get(ictx, i, 0, -1)) {
1800 case -1:
1801 break;
1802 case 4: /* IRM */
1803 screen_write_mode_set(sctx, MODE_INSERT);
1804 break;
1805 case 34:
1806 screen_write_mode_clear(sctx, MODE_CURSOR_VERY_VISIBLE);
1807 break;
1808 default:
1809 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1810 break;
1815 /* Handle CSI private SM. */
1816 static void
1817 input_csi_dispatch_sm_private(struct input_ctx *ictx)
1819 struct screen_write_ctx *sctx = &ictx->ctx;
1820 struct grid_cell *gc = &ictx->cell.cell;
1821 u_int i;
1823 for (i = 0; i < ictx->param_list_len; i++) {
1824 switch (input_get(ictx, i, 0, -1)) {
1825 case -1:
1826 break;
1827 case 1: /* DECCKM */
1828 screen_write_mode_set(sctx, MODE_KCURSOR);
1829 break;
1830 case 3: /* DECCOLM */
1831 screen_write_cursormove(sctx, 0, 0, 1);
1832 screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1833 break;
1834 case 6: /* DECOM */
1835 screen_write_mode_set(sctx, MODE_ORIGIN);
1836 screen_write_cursormove(sctx, 0, 0, 1);
1837 break;
1838 case 7: /* DECAWM */
1839 screen_write_mode_set(sctx, MODE_WRAP);
1840 break;
1841 case 12:
1842 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING);
1843 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET);
1844 break;
1845 case 25: /* TCEM */
1846 screen_write_mode_set(sctx, MODE_CURSOR);
1847 break;
1848 case 1000:
1849 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1850 screen_write_mode_set(sctx, MODE_MOUSE_STANDARD);
1851 break;
1852 case 1002:
1853 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1854 screen_write_mode_set(sctx, MODE_MOUSE_BUTTON);
1855 break;
1856 case 1003:
1857 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1858 screen_write_mode_set(sctx, MODE_MOUSE_ALL);
1859 break;
1860 case 1004:
1861 screen_write_mode_set(sctx, MODE_FOCUSON);
1862 break;
1863 case 1005:
1864 screen_write_mode_set(sctx, MODE_MOUSE_UTF8);
1865 break;
1866 case 1006:
1867 screen_write_mode_set(sctx, MODE_MOUSE_SGR);
1868 break;
1869 case 47:
1870 case 1047:
1871 screen_write_alternateon(sctx, gc, 0);
1872 break;
1873 case 1049:
1874 screen_write_alternateon(sctx, gc, 1);
1875 break;
1876 case 2004:
1877 screen_write_mode_set(sctx, MODE_BRACKETPASTE);
1878 break;
1879 default:
1880 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1881 break;
1886 /* Handle CSI graphics SM. */
1887 static void
1888 input_csi_dispatch_sm_graphics(__unused struct input_ctx *ictx)
1890 #ifdef ENABLE_SIXEL
1891 int n, m, o;
1893 if (ictx->param_list_len > 3)
1894 return;
1895 n = input_get(ictx, 0, 0, 0);
1896 m = input_get(ictx, 1, 0, 0);
1897 o = input_get(ictx, 2, 0, 0);
1899 if (n == 1 && (m == 1 || m == 2 || m == 4))
1900 input_reply(ictx, "\033[?%d;0;%uS", n, SIXEL_COLOUR_REGISTERS);
1901 else
1902 input_reply(ictx, "\033[?%d;3;%dS", n, o);
1903 #endif
1906 /* Handle CSI window operations. */
1907 static void
1908 input_csi_dispatch_winops(struct input_ctx *ictx)
1910 struct screen_write_ctx *sctx = &ictx->ctx;
1911 struct screen *s = sctx->s;
1912 struct window_pane *wp = ictx->wp;
1913 struct window *w = NULL;
1914 u_int x = screen_size_x(s), y = screen_size_y(s);
1915 int n, m;
1917 if (wp != NULL)
1918 w = wp->window;
1920 m = 0;
1921 while ((n = input_get(ictx, m, 0, -1)) != -1) {
1922 switch (n) {
1923 case 1:
1924 case 2:
1925 case 5:
1926 case 6:
1927 case 7:
1928 case 11:
1929 case 13:
1930 case 20:
1931 case 21:
1932 case 24:
1933 break;
1934 case 3:
1935 case 4:
1936 case 8:
1937 m++;
1938 if (input_get(ictx, m, 0, -1) == -1)
1939 return;
1940 /* FALLTHROUGH */
1941 case 9:
1942 case 10:
1943 m++;
1944 if (input_get(ictx, m, 0, -1) == -1)
1945 return;
1946 break;
1947 case 14:
1948 if (w == NULL)
1949 break;
1950 input_reply(ictx, "\033[4;%u;%ut", y * w->ypixel,
1951 x * w->xpixel);
1952 break;
1953 case 15:
1954 if (w == NULL)
1955 break;
1956 input_reply(ictx, "\033[5;%u;%ut", y * w->ypixel,
1957 x * w->xpixel);
1958 break;
1959 case 16:
1960 if (w == NULL)
1961 break;
1962 input_reply(ictx, "\033[6;%u;%ut", w->ypixel,
1963 w->xpixel);
1964 break;
1965 case 18:
1966 input_reply(ictx, "\033[8;%u;%ut", y, x);
1967 break;
1968 case 19:
1969 input_reply(ictx, "\033[9;%u;%ut", y, x);
1970 break;
1971 case 22:
1972 m++;
1973 switch (input_get(ictx, m, 0, -1)) {
1974 case -1:
1975 return;
1976 case 0:
1977 case 2:
1978 screen_push_title(sctx->s);
1979 break;
1981 break;
1982 case 23:
1983 m++;
1984 switch (input_get(ictx, m, 0, -1)) {
1985 case -1:
1986 return;
1987 case 0:
1988 case 2:
1989 screen_pop_title(sctx->s);
1990 if (wp == NULL)
1991 break;
1992 notify_pane("pane-title-changed", wp);
1993 server_redraw_window_borders(w);
1994 server_status_window(w);
1995 break;
1997 break;
1998 default:
1999 log_debug("%s: unknown '%c'", __func__, ictx->ch);
2000 break;
2002 m++;
2006 /* Helper for 256 colour SGR. */
2007 static int
2008 input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
2010 struct grid_cell *gc = &ictx->cell.cell;
2012 if (c == -1 || c > 255) {
2013 if (fgbg == 38)
2014 gc->fg = 8;
2015 else if (fgbg == 48)
2016 gc->bg = 8;
2017 } else {
2018 if (fgbg == 38)
2019 gc->fg = c | COLOUR_FLAG_256;
2020 else if (fgbg == 48)
2021 gc->bg = c | COLOUR_FLAG_256;
2022 else if (fgbg == 58)
2023 gc->us = c | COLOUR_FLAG_256;
2025 return (1);
2028 /* Handle CSI SGR for 256 colours. */
2029 static void
2030 input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
2032 int c;
2034 c = input_get(ictx, (*i) + 1, 0, -1);
2035 if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
2036 (*i)++;
2039 /* Helper for RGB colour SGR. */
2040 static int
2041 input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
2042 int b)
2044 struct grid_cell *gc = &ictx->cell.cell;
2046 if (r == -1 || r > 255)
2047 return (0);
2048 if (g == -1 || g > 255)
2049 return (0);
2050 if (b == -1 || b > 255)
2051 return (0);
2053 if (fgbg == 38)
2054 gc->fg = colour_join_rgb(r, g, b);
2055 else if (fgbg == 48)
2056 gc->bg = colour_join_rgb(r, g, b);
2057 else if (fgbg == 58)
2058 gc->us = colour_join_rgb(r, g, b);
2059 return (1);
2062 /* Handle CSI SGR for RGB colours. */
2063 static void
2064 input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
2066 int r, g, b;
2068 r = input_get(ictx, (*i) + 1, 0, -1);
2069 g = input_get(ictx, (*i) + 2, 0, -1);
2070 b = input_get(ictx, (*i) + 3, 0, -1);
2071 if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
2072 (*i) += 3;
2075 /* Handle CSI SGR with a ISO parameter. */
2076 static void
2077 input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
2079 struct grid_cell *gc = &ictx->cell.cell;
2080 char *s = ictx->param_list[i].str, *copy, *ptr, *out;
2081 int p[8];
2082 u_int n;
2083 const char *errstr;
2085 for (n = 0; n < nitems(p); n++)
2086 p[n] = -1;
2087 n = 0;
2089 ptr = copy = xstrdup(s);
2090 while ((out = strsep(&ptr, ":")) != NULL) {
2091 if (*out != '\0') {
2092 p[n++] = strtonum(out, 0, INT_MAX, &errstr);
2093 if (errstr != NULL || n == nitems(p)) {
2094 free(copy);
2095 return;
2097 } else {
2098 n++;
2099 if (n == nitems(p)) {
2100 free(copy);
2101 return;
2104 log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
2106 free(copy);
2108 if (n == 0)
2109 return;
2110 if (p[0] == 4) {
2111 if (n != 2)
2112 return;
2113 switch (p[1]) {
2114 case 0:
2115 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2116 break;
2117 case 1:
2118 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2119 gc->attr |= GRID_ATTR_UNDERSCORE;
2120 break;
2121 case 2:
2122 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2123 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2124 break;
2125 case 3:
2126 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2127 gc->attr |= GRID_ATTR_UNDERSCORE_3;
2128 break;
2129 case 4:
2130 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2131 gc->attr |= GRID_ATTR_UNDERSCORE_4;
2132 break;
2133 case 5:
2134 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2135 gc->attr |= GRID_ATTR_UNDERSCORE_5;
2136 break;
2138 return;
2140 if (n < 2 || (p[0] != 38 && p[0] != 48 && p[0] != 58))
2141 return;
2142 switch (p[1]) {
2143 case 2:
2144 if (n < 3)
2145 break;
2146 if (n == 5)
2147 i = 2;
2148 else
2149 i = 3;
2150 if (n < i + 3)
2151 break;
2152 input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
2153 p[i + 2]);
2154 break;
2155 case 5:
2156 if (n < 3)
2157 break;
2158 input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
2159 break;
2163 /* Handle CSI SGR. */
2164 static void
2165 input_csi_dispatch_sgr(struct input_ctx *ictx)
2167 struct grid_cell *gc = &ictx->cell.cell;
2168 u_int i, link;
2169 int n;
2171 if (ictx->param_list_len == 0) {
2172 memcpy(gc, &grid_default_cell, sizeof *gc);
2173 return;
2176 for (i = 0; i < ictx->param_list_len; i++) {
2177 if (ictx->param_list[i].type == INPUT_STRING) {
2178 input_csi_dispatch_sgr_colon(ictx, i);
2179 continue;
2181 n = input_get(ictx, i, 0, 0);
2182 if (n == -1)
2183 continue;
2185 if (n == 38 || n == 48 || n == 58) {
2186 i++;
2187 switch (input_get(ictx, i, 0, -1)) {
2188 case 2:
2189 input_csi_dispatch_sgr_rgb(ictx, n, &i);
2190 break;
2191 case 5:
2192 input_csi_dispatch_sgr_256(ictx, n, &i);
2193 break;
2195 continue;
2198 switch (n) {
2199 case 0:
2200 link = gc->link;
2201 memcpy(gc, &grid_default_cell, sizeof *gc);
2202 gc->link = link;
2203 break;
2204 case 1:
2205 gc->attr |= GRID_ATTR_BRIGHT;
2206 break;
2207 case 2:
2208 gc->attr |= GRID_ATTR_DIM;
2209 break;
2210 case 3:
2211 gc->attr |= GRID_ATTR_ITALICS;
2212 break;
2213 case 4:
2214 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2215 gc->attr |= GRID_ATTR_UNDERSCORE;
2216 break;
2217 case 5:
2218 case 6:
2219 gc->attr |= GRID_ATTR_BLINK;
2220 break;
2221 case 7:
2222 gc->attr |= GRID_ATTR_REVERSE;
2223 break;
2224 case 8:
2225 gc->attr |= GRID_ATTR_HIDDEN;
2226 break;
2227 case 9:
2228 gc->attr |= GRID_ATTR_STRIKETHROUGH;
2229 break;
2230 case 21:
2231 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2232 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2233 break;
2234 case 22:
2235 gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
2236 break;
2237 case 23:
2238 gc->attr &= ~GRID_ATTR_ITALICS;
2239 break;
2240 case 24:
2241 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2242 break;
2243 case 25:
2244 gc->attr &= ~GRID_ATTR_BLINK;
2245 break;
2246 case 27:
2247 gc->attr &= ~GRID_ATTR_REVERSE;
2248 break;
2249 case 28:
2250 gc->attr &= ~GRID_ATTR_HIDDEN;
2251 break;
2252 case 29:
2253 gc->attr &= ~GRID_ATTR_STRIKETHROUGH;
2254 break;
2255 case 30:
2256 case 31:
2257 case 32:
2258 case 33:
2259 case 34:
2260 case 35:
2261 case 36:
2262 case 37:
2263 gc->fg = n - 30;
2264 break;
2265 case 39:
2266 gc->fg = 8;
2267 break;
2268 case 40:
2269 case 41:
2270 case 42:
2271 case 43:
2272 case 44:
2273 case 45:
2274 case 46:
2275 case 47:
2276 gc->bg = n - 40;
2277 break;
2278 case 49:
2279 gc->bg = 8;
2280 break;
2281 case 53:
2282 gc->attr |= GRID_ATTR_OVERLINE;
2283 break;
2284 case 55:
2285 gc->attr &= ~GRID_ATTR_OVERLINE;
2286 break;
2287 case 59:
2288 gc->us = 8;
2289 break;
2290 case 90:
2291 case 91:
2292 case 92:
2293 case 93:
2294 case 94:
2295 case 95:
2296 case 96:
2297 case 97:
2298 gc->fg = n;
2299 break;
2300 case 100:
2301 case 101:
2302 case 102:
2303 case 103:
2304 case 104:
2305 case 105:
2306 case 106:
2307 case 107:
2308 gc->bg = n - 10;
2309 break;
2314 /* End of input with BEL. */
2315 static int
2316 input_end_bel(struct input_ctx *ictx)
2318 log_debug("%s", __func__);
2320 ictx->input_end = INPUT_END_BEL;
2322 return (0);
2325 /* DCS string started. */
2326 static void
2327 input_enter_dcs(struct input_ctx *ictx)
2329 log_debug("%s", __func__);
2331 input_clear(ictx);
2332 input_start_timer(ictx);
2333 ictx->flags &= ~INPUT_LAST;
2336 /* DCS terminator (ST) received. */
2337 static int
2338 input_dcs_dispatch(struct input_ctx *ictx)
2340 struct window_pane *wp = ictx->wp;
2341 struct screen_write_ctx *sctx = &ictx->ctx;
2342 u_char *buf = ictx->input_buf;
2343 size_t len = ictx->input_len;
2344 const char prefix[] = "tmux;";
2345 const u_int prefixlen = (sizeof prefix) - 1;
2346 long long allow_passthrough = 0;
2347 #ifdef ENABLE_SIXEL
2348 struct window *w;
2349 struct sixel_image *si;
2350 int p2;
2351 #endif
2353 if (wp == NULL)
2354 return (0);
2356 if (ictx->flags & INPUT_DISCARD) {
2357 log_debug("%s: %zu bytes (discard)", __func__, len);
2358 return (0);
2361 #ifdef ENABLE_SIXEL
2362 w = wp->window;
2363 if (buf[0] == 'q') {
2364 if (input_split(ictx) != 0)
2365 return (0);
2366 p2 = input_get(ictx, 1, 0, 0);
2367 if (p2 == -1)
2368 p2 = 0;
2369 si = sixel_parse(buf, len, p2, w->xpixel, w->ypixel);
2370 if (si != NULL)
2371 screen_write_sixelimage(sctx, si, ictx->cell.cell.bg);
2373 #endif
2375 allow_passthrough = options_get_number(wp->options, "allow-passthrough");
2376 if (!allow_passthrough)
2377 return (0);
2378 log_debug("%s: \"%s\"", __func__, buf);
2380 if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0) {
2381 screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen,
2382 allow_passthrough == 2);
2385 return (0);
2388 /* OSC string started. */
2389 static void
2390 input_enter_osc(struct input_ctx *ictx)
2392 log_debug("%s", __func__);
2394 input_clear(ictx);
2395 input_start_timer(ictx);
2396 ictx->flags &= ~INPUT_LAST;
2399 /* OSC terminator (ST) received. */
2400 static void
2401 input_exit_osc(struct input_ctx *ictx)
2403 struct screen_write_ctx *sctx = &ictx->ctx;
2404 struct window_pane *wp = ictx->wp;
2405 u_char *p = ictx->input_buf;
2406 u_int option;
2408 if (ictx->flags & INPUT_DISCARD)
2409 return;
2410 if (ictx->input_len < 1 || *p < '0' || *p > '9')
2411 return;
2413 log_debug("%s: \"%s\" (end %s)", __func__, p,
2414 ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2416 option = 0;
2417 while (*p >= '0' && *p <= '9')
2418 option = option * 10 + *p++ - '0';
2419 if (*p != ';' && *p != '\0')
2420 return;
2421 if (*p == ';')
2422 p++;
2424 switch (option) {
2425 case 0:
2426 case 2:
2427 if (wp != NULL &&
2428 options_get_number(wp->options, "allow-set-title") &&
2429 screen_set_title(sctx->s, p)) {
2430 notify_pane("pane-title-changed", wp);
2431 server_redraw_window_borders(wp->window);
2432 server_status_window(wp->window);
2434 break;
2435 case 4:
2436 input_osc_4(ictx, p);
2437 break;
2438 case 7:
2439 if (utf8_isvalid(p)) {
2440 screen_set_path(sctx->s, p);
2441 if (wp != NULL) {
2442 server_redraw_window_borders(wp->window);
2443 server_status_window(wp->window);
2446 break;
2447 case 8:
2448 input_osc_8(ictx, p);
2449 break;
2450 case 10:
2451 input_osc_10(ictx, p);
2452 break;
2453 case 11:
2454 input_osc_11(ictx, p);
2455 break;
2456 case 12:
2457 input_osc_12(ictx, p);
2458 break;
2459 case 52:
2460 input_osc_52(ictx, p);
2461 break;
2462 case 104:
2463 input_osc_104(ictx, p);
2464 break;
2465 case 110:
2466 input_osc_110(ictx, p);
2467 break;
2468 case 111:
2469 input_osc_111(ictx, p);
2470 break;
2471 case 112:
2472 input_osc_112(ictx, p);
2473 break;
2474 case 133:
2475 input_osc_133(ictx, p);
2476 break;
2477 default:
2478 log_debug("%s: unknown '%u'", __func__, option);
2479 break;
2483 /* APC string started. */
2484 static void
2485 input_enter_apc(struct input_ctx *ictx)
2487 log_debug("%s", __func__);
2489 input_clear(ictx);
2490 input_start_timer(ictx);
2491 ictx->flags &= ~INPUT_LAST;
2494 /* APC terminator (ST) received. */
2495 static void
2496 input_exit_apc(struct input_ctx *ictx)
2498 struct screen_write_ctx *sctx = &ictx->ctx;
2499 struct window_pane *wp = ictx->wp;
2501 if (ictx->flags & INPUT_DISCARD)
2502 return;
2503 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2505 if (screen_set_title(sctx->s, ictx->input_buf) && wp != NULL) {
2506 notify_pane("pane-title-changed", wp);
2507 server_redraw_window_borders(wp->window);
2508 server_status_window(wp->window);
2512 /* Rename string started. */
2513 static void
2514 input_enter_rename(struct input_ctx *ictx)
2516 log_debug("%s", __func__);
2518 input_clear(ictx);
2519 input_start_timer(ictx);
2520 ictx->flags &= ~INPUT_LAST;
2523 /* Rename terminator (ST) received. */
2524 static void
2525 input_exit_rename(struct input_ctx *ictx)
2527 struct window_pane *wp = ictx->wp;
2528 struct window *w;
2529 struct options_entry *o;
2531 if (wp == NULL)
2532 return;
2533 if (ictx->flags & INPUT_DISCARD)
2534 return;
2535 if (!options_get_number(ictx->wp->options, "allow-rename"))
2536 return;
2537 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2539 if (!utf8_isvalid(ictx->input_buf))
2540 return;
2541 w = wp->window;
2543 if (ictx->input_len == 0) {
2544 o = options_get_only(w->options, "automatic-rename");
2545 if (o != NULL)
2546 options_remove_or_default(o, -1, NULL);
2547 if (!options_get_number(w->options, "automatic-rename"))
2548 window_set_name(w, "");
2549 } else {
2550 options_set_number(w->options, "automatic-rename", 0);
2551 window_set_name(w, ictx->input_buf);
2553 server_redraw_window_borders(w);
2554 server_status_window(w);
2557 /* Open UTF-8 character. */
2558 static int
2559 input_top_bit_set(struct input_ctx *ictx)
2561 struct screen_write_ctx *sctx = &ictx->ctx;
2562 struct utf8_data *ud = &ictx->utf8data;
2564 ictx->flags &= ~INPUT_LAST;
2566 if (!ictx->utf8started) {
2567 if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2568 return (0);
2569 ictx->utf8started = 1;
2570 return (0);
2573 switch (utf8_append(ud, ictx->ch)) {
2574 case UTF8_MORE:
2575 return (0);
2576 case UTF8_ERROR:
2577 ictx->utf8started = 0;
2578 return (0);
2579 case UTF8_DONE:
2580 break;
2582 ictx->utf8started = 0;
2584 log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2585 (int)ud->size, ud->data, ud->width);
2587 utf8_copy(&ictx->cell.cell.data, ud);
2588 screen_write_collect_add(sctx, &ictx->cell.cell);
2590 utf8_copy(&ictx->last, &ictx->cell.cell.data);
2591 ictx->flags |= INPUT_LAST;
2593 return (0);
2596 /* Reply to a colour request. */
2597 static void
2598 input_osc_colour_reply(struct input_ctx *ictx, u_int n, int c)
2600 u_char r, g, b;
2601 const char *end;
2603 if (c != -1)
2604 c = colour_force_rgb(c);
2605 if (c == -1)
2606 return;
2607 colour_split_rgb(c, &r, &g, &b);
2609 if (ictx->input_end == INPUT_END_BEL)
2610 end = "\007";
2611 else
2612 end = "\033\\";
2613 input_reply(ictx, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2614 n, r, r, g, g, b, b, end);
2617 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2618 static void
2619 input_osc_4(struct input_ctx *ictx, const char *p)
2621 char *copy, *s, *next = NULL;
2622 long idx;
2623 int c, bad = 0, redraw = 0;
2625 copy = s = xstrdup(p);
2626 while (s != NULL && *s != '\0') {
2627 idx = strtol(s, &next, 10);
2628 if (*next++ != ';') {
2629 bad = 1;
2630 break;
2632 if (idx < 0 || idx >= 256) {
2633 bad = 1;
2634 break;
2637 s = strsep(&next, ";");
2638 if (strcmp(s, "?") == 0) {
2639 c = colour_palette_get(ictx->palette, idx);
2640 if (c != -1)
2641 input_osc_colour_reply(ictx, 4, c);
2642 continue;
2644 if ((c = colour_parseX11(s)) == -1) {
2645 s = next;
2646 continue;
2648 if (colour_palette_set(ictx->palette, idx, c))
2649 redraw = 1;
2650 s = next;
2652 if (bad)
2653 log_debug("bad OSC 4: %s", p);
2654 if (redraw)
2655 screen_write_fullredraw(&ictx->ctx);
2656 free(copy);
2659 /* Handle the OSC 8 sequence for embedding hyperlinks. */
2660 static void
2661 input_osc_8(struct input_ctx *ictx, const char *p)
2663 struct hyperlinks *hl = ictx->ctx.s->hyperlinks;
2664 struct grid_cell *gc = &ictx->cell.cell;
2665 const char *start, *end, *uri;
2666 char *id = NULL;
2668 for (start = p; (end = strpbrk(start, ":;")) != NULL; start = end + 1) {
2669 if (end - start >= 4 && strncmp(start, "id=", 3) == 0) {
2670 if (id != NULL)
2671 goto bad;
2672 id = xstrndup(start + 3, end - start - 3);
2675 /* The first ; is the end of parameters and start of the URI. */
2676 if (*end == ';')
2677 break;
2679 if (end == NULL || *end != ';')
2680 goto bad;
2681 uri = end + 1;
2682 if (*uri == '\0') {
2683 gc->link = 0;
2684 free(id);
2685 return;
2687 gc->link = hyperlinks_put(hl, uri, id);
2688 if (id == NULL)
2689 log_debug("hyperlink (anonymous) %s = %u", uri, gc->link);
2690 else
2691 log_debug("hyperlink (id=%s) %s = %u", id, uri, gc->link);
2692 free(id);
2693 return;
2695 bad:
2696 log_debug("bad OSC 8 %s", p);
2697 free(id);
2701 * Get a client with a foreground for the pane. There isn't much to choose
2702 * between them so just use the first.
2704 static int
2705 input_get_fg_client(struct window_pane *wp)
2707 struct window *w = wp->window;
2708 struct client *loop;
2710 TAILQ_FOREACH(loop, &clients, entry) {
2711 if (loop->flags & CLIENT_UNATTACHEDFLAGS)
2712 continue;
2713 if (loop->session == NULL || !session_has(loop->session, w))
2714 continue;
2715 if (loop->tty.fg == -1)
2716 continue;
2717 return (loop->tty.fg);
2719 return (-1);
2722 /* Get a client with a background for the pane. */
2723 static int
2724 input_get_bg_client(struct window_pane *wp)
2726 struct window *w = wp->window;
2727 struct client *loop;
2729 TAILQ_FOREACH(loop, &clients, entry) {
2730 if (loop->flags & CLIENT_UNATTACHEDFLAGS)
2731 continue;
2732 if (loop->session == NULL || !session_has(loop->session, w))
2733 continue;
2734 if (loop->tty.bg == -1)
2735 continue;
2736 return (loop->tty.bg);
2738 return (-1);
2742 * If any control mode client exists that has provided a bg color, return it.
2743 * Otherwise, return -1.
2745 static int
2746 input_get_bg_control_client(struct window_pane *wp)
2748 struct client *c;
2750 if (wp->control_bg == -1)
2751 return (-1);
2753 TAILQ_FOREACH(c, &clients, entry) {
2754 if (c->flags & CLIENT_CONTROL)
2755 return (wp->control_bg);
2757 return (-1);
2761 * If any control mode client exists that has provided a fg color, return it.
2762 * Otherwise, return -1.
2764 static int
2765 input_get_fg_control_client(struct window_pane *wp)
2767 struct client *c;
2769 if (wp->control_fg == -1)
2770 return (-1);
2772 TAILQ_FOREACH(c, &clients, entry) {
2773 if (c->flags & CLIENT_CONTROL)
2774 return (wp->control_fg);
2776 return (-1);
2779 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2780 static void
2781 input_osc_10(struct input_ctx *ictx, const char *p)
2783 struct window_pane *wp = ictx->wp;
2784 struct grid_cell defaults;
2785 int c;
2787 if (strcmp(p, "?") == 0) {
2788 if (wp == NULL)
2789 return;
2790 c = input_get_fg_control_client(wp);
2791 if (c == -1) {
2792 tty_default_colours(&defaults, wp);
2793 if (COLOUR_DEFAULT(defaults.fg))
2794 c = input_get_fg_client(wp);
2795 else
2796 c = defaults.fg;
2798 input_osc_colour_reply(ictx, 10, c);
2799 return;
2802 if ((c = colour_parseX11(p)) == -1) {
2803 log_debug("bad OSC 10: %s", p);
2804 return;
2806 if (ictx->palette != NULL) {
2807 ictx->palette->fg = c;
2808 if (wp != NULL)
2809 wp->flags |= PANE_STYLECHANGED;
2810 screen_write_fullredraw(&ictx->ctx);
2814 /* Handle the OSC 110 sequence for resetting foreground colour. */
2815 static void
2816 input_osc_110(struct input_ctx *ictx, const char *p)
2818 struct window_pane *wp = ictx->wp;
2820 if (*p != '\0')
2821 return;
2822 if (ictx->palette != NULL) {
2823 ictx->palette->fg = 8;
2824 if (wp != NULL)
2825 wp->flags |= PANE_STYLECHANGED;
2826 screen_write_fullredraw(&ictx->ctx);
2830 /* Handle the OSC 11 sequence for setting and querying background colour. */
2831 static void
2832 input_osc_11(struct input_ctx *ictx, const char *p)
2834 struct window_pane *wp = ictx->wp;
2835 struct grid_cell defaults;
2836 int c;
2838 if (strcmp(p, "?") == 0) {
2839 if (wp == NULL)
2840 return;
2841 c = input_get_bg_control_client(wp);
2842 if (c == -1) {
2843 tty_default_colours(&defaults, wp);
2844 if (COLOUR_DEFAULT(defaults.bg))
2845 c = input_get_bg_client(wp);
2846 else
2847 c = defaults.bg;
2849 input_osc_colour_reply(ictx, 11, c);
2850 return;
2853 if ((c = colour_parseX11(p)) == -1) {
2854 log_debug("bad OSC 11: %s", p);
2855 return;
2857 if (ictx->palette != NULL) {
2858 ictx->palette->bg = c;
2859 if (wp != NULL)
2860 wp->flags |= PANE_STYLECHANGED;
2861 screen_write_fullredraw(&ictx->ctx);
2865 /* Handle the OSC 111 sequence for resetting background colour. */
2866 static void
2867 input_osc_111(struct input_ctx *ictx, const char *p)
2869 struct window_pane *wp = ictx->wp;
2871 if (*p != '\0')
2872 return;
2873 if (ictx->palette != NULL) {
2874 ictx->palette->bg = 8;
2875 if (wp != NULL)
2876 wp->flags |= PANE_STYLECHANGED;
2877 screen_write_fullredraw(&ictx->ctx);
2881 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2882 static void
2883 input_osc_12(struct input_ctx *ictx, const char *p)
2885 struct window_pane *wp = ictx->wp;
2886 int c;
2888 if (strcmp(p, "?") == 0) {
2889 if (wp != NULL) {
2890 c = ictx->ctx.s->ccolour;
2891 if (c == -1)
2892 c = ictx->ctx.s->default_ccolour;
2893 input_osc_colour_reply(ictx, 12, c);
2895 return;
2898 if ((c = colour_parseX11(p)) == -1) {
2899 log_debug("bad OSC 12: %s", p);
2900 return;
2902 screen_set_cursor_colour(ictx->ctx.s, c);
2905 /* Handle the OSC 112 sequence for resetting cursor colour. */
2906 static void
2907 input_osc_112(struct input_ctx *ictx, const char *p)
2909 if (*p == '\0') /* no arguments allowed */
2910 screen_set_cursor_colour(ictx->ctx.s, -1);
2913 /* Handle the OSC 133 sequence. */
2914 static void
2915 input_osc_133(struct input_ctx *ictx, const char *p)
2917 struct grid *gd = ictx->ctx.s->grid;
2918 u_int line = ictx->ctx.s->cy + gd->hsize;
2919 struct grid_line *gl;
2921 if (line > gd->hsize + gd->sy - 1)
2922 return;
2923 gl = grid_get_line(gd, line);
2925 switch (*p) {
2926 case 'A':
2927 gl->flags |= GRID_LINE_START_PROMPT;
2928 break;
2929 case 'C':
2930 gl->flags |= GRID_LINE_START_OUTPUT;
2931 break;
2935 /* Handle the OSC 52 sequence for setting the clipboard. */
2936 static void
2937 input_osc_52(struct input_ctx *ictx, const char *p)
2939 struct window_pane *wp = ictx->wp;
2940 char *end;
2941 const char *buf = NULL;
2942 size_t len = 0;
2943 u_char *out;
2944 int outlen, state;
2945 struct screen_write_ctx ctx;
2946 struct paste_buffer *pb;
2947 const char* allow = "cpqs01234567";
2948 char flags[sizeof "cpqs01234567"] = "";
2949 u_int i, j = 0;
2951 if (wp == NULL)
2952 return;
2953 state = options_get_number(global_options, "set-clipboard");
2954 if (state != 2)
2955 return;
2957 if ((end = strchr(p, ';')) == NULL)
2958 return;
2959 end++;
2960 if (*end == '\0')
2961 return;
2962 log_debug("%s: %s", __func__, end);
2964 for (i = 0; p + i != end; i++) {
2965 if (strchr(allow, p[i]) != NULL && strchr(flags, p[i]) == NULL)
2966 flags[j++] = p[i];
2968 log_debug("%s: %.*s %s", __func__, (int)(end - p - 1), p, flags);
2970 if (strcmp(end, "?") == 0) {
2971 if ((pb = paste_get_top(NULL)) != NULL)
2972 buf = paste_buffer_data(pb, &len);
2973 if (ictx->input_end == INPUT_END_BEL)
2974 input_reply_clipboard(ictx->event, buf, len, "\007");
2975 else
2976 input_reply_clipboard(ictx->event, buf, len, "\033\\");
2977 return;
2980 len = (strlen(end) / 4) * 3;
2981 if (len == 0)
2982 return;
2984 out = xmalloc(len);
2985 if ((outlen = b64_pton(end, out, len)) == -1) {
2986 free(out);
2987 return;
2990 screen_write_start_pane(&ctx, wp, NULL);
2991 screen_write_setselection(&ctx, flags, out, outlen);
2992 screen_write_stop(&ctx);
2993 notify_pane("pane-set-clipboard", wp);
2995 paste_add(NULL, out, outlen);
2998 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2999 static void
3000 input_osc_104(struct input_ctx *ictx, const char *p)
3002 char *copy, *s;
3003 long idx;
3004 int bad = 0, redraw = 0;
3006 if (*p == '\0') {
3007 colour_palette_clear(ictx->palette);
3008 screen_write_fullredraw(&ictx->ctx);
3009 return;
3012 copy = s = xstrdup(p);
3013 while (*s != '\0') {
3014 idx = strtol(s, &s, 10);
3015 if (*s != '\0' && *s != ';') {
3016 bad = 1;
3017 break;
3019 if (idx < 0 || idx >= 256) {
3020 bad = 1;
3021 break;
3023 if (colour_palette_set(ictx->palette, idx, -1))
3024 redraw = 1;
3025 if (*s == ';')
3026 s++;
3028 if (bad)
3029 log_debug("bad OSC 104: %s", p);
3030 if (redraw)
3031 screen_write_fullredraw(&ictx->ctx);
3032 free(copy);
3035 void
3036 input_reply_clipboard(struct bufferevent *bev, const char *buf, size_t len,
3037 const char *end)
3039 char *out = NULL;
3040 int outlen = 0;
3042 if (buf != NULL && len != 0) {
3043 if (len >= ((size_t)INT_MAX * 3 / 4) - 1)
3044 return;
3045 outlen = 4 * ((len + 2) / 3) + 1;
3046 out = xmalloc(outlen);
3047 if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
3048 free(out);
3049 return;
3053 bufferevent_write(bev, "\033]52;;", 6);
3054 if (outlen != 0)
3055 bufferevent_write(bev, out, outlen);
3056 bufferevent_write(bev, end, strlen(end));
3057 free(out);
3060 /* Set input buffer size. */
3061 void
3062 input_set_buffer_size(size_t buffer_size)
3064 log_debug("%s: %lu -> %lu", __func__, input_buffer_size, buffer_size);
3065 input_buffer_size = buffer_size;