xref: /openbsd-src/usr.bin/tmux/input.c (revision fcde59b201a29a2b4570b00b71e7aa25d61cb5c1)
1 /* $OpenBSD: input.c,v 1.185 2020/10/30 11:34:13 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <netinet/in.h>
22 
23 #include <ctype.h>
24 #include <resolv.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 
29 #include "tmux.h"
30 
31 /*
32  * Based on the description by Paul Williams at:
33  *
34  * https://vt100.net/emu/dec_ansi_parser
35  *
36  * With the following changes:
37  *
38  * - 7-bit only.
39  *
40  * - Support for UTF-8.
41  *
42  * - OSC (but not APC) may be terminated by \007 as well as ST.
43  *
44  * - A state for APC similar to OSC. Some terminals appear to use this to set
45  *   the title.
46  *
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.
49  *
50  * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
51  *   be passed to the underlying terminals.
52  */
53 
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 */
60 };
61 
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;
72 	};
73 };
74 
75 /* Input parser context. */
76 struct input_ctx {
77 	struct window_pane     *wp;
78 	struct bufferevent     *event;
79 	struct screen_write_ctx ctx;
80 
81 	struct input_cell	cell;
82 
83 	struct input_cell	old_cell;
84 	u_int			old_cx;
85 	u_int			old_cy;
86 	int			old_mode;
87 
88 	u_char			interm_buf[4];
89 	size_t			interm_len;
90 
91 	u_char			param_buf[64];
92 	size_t			param_len;
93 
94 #define INPUT_BUF_START 32
95 #define INPUT_BUF_LIMIT 1048576
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;
103 
104 	struct input_param	param_list[24];
105 	u_int			param_list_len;
106 
107 	struct utf8_data	utf8data;
108 	int			utf8started;
109 
110 	int			ch;
111 	int			last;
112 
113 	int			flags;
114 #define INPUT_DISCARD 0x1
115 
116 	const struct input_state *state;
117 
118 	struct event		timer;
119 
120 	/*
121 	 * All input received since we were last in the ground state. Sent to
122 	 * control clients on connection.
123 	 */
124 	struct evbuffer		*since_ground;
125 };
126 
127 /* Helper functions. */
128 struct input_transition;
129 static int	input_split(struct input_ctx *);
130 static int	input_get(struct input_ctx *, u_int, int, int);
131 static void printflike(2, 3) input_reply(struct input_ctx *, const char *, ...);
132 static void	input_set_state(struct input_ctx *,
133 		    const struct input_transition *);
134 static void	input_reset_cell(struct input_ctx *);
135 
136 static void	input_osc_4(struct input_ctx *, const char *);
137 static void	input_osc_10(struct input_ctx *, const char *);
138 static void	input_osc_11(struct input_ctx *, const char *);
139 static void	input_osc_52(struct input_ctx *, const char *);
140 static void	input_osc_104(struct input_ctx *, const char *);
141 
142 /* Transition entry/exit handlers. */
143 static void	input_clear(struct input_ctx *);
144 static void	input_ground(struct input_ctx *);
145 static void	input_enter_dcs(struct input_ctx *);
146 static void	input_enter_osc(struct input_ctx *);
147 static void	input_exit_osc(struct input_ctx *);
148 static void	input_enter_apc(struct input_ctx *);
149 static void	input_exit_apc(struct input_ctx *);
150 static void	input_enter_rename(struct input_ctx *);
151 static void	input_exit_rename(struct input_ctx *);
152 
153 /* Input state handlers. */
154 static int	input_print(struct input_ctx *);
155 static int	input_intermediate(struct input_ctx *);
156 static int	input_parameter(struct input_ctx *);
157 static int	input_input(struct input_ctx *);
158 static int	input_c0_dispatch(struct input_ctx *);
159 static int	input_esc_dispatch(struct input_ctx *);
160 static int	input_csi_dispatch(struct input_ctx *);
161 static void	input_csi_dispatch_rm(struct input_ctx *);
162 static void	input_csi_dispatch_rm_private(struct input_ctx *);
163 static void	input_csi_dispatch_sm(struct input_ctx *);
164 static void	input_csi_dispatch_sm_private(struct input_ctx *);
165 static void	input_csi_dispatch_winops(struct input_ctx *);
166 static void	input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
167 static void	input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
168 static void	input_csi_dispatch_sgr(struct input_ctx *);
169 static int	input_dcs_dispatch(struct input_ctx *);
170 static int	input_top_bit_set(struct input_ctx *);
171 static int	input_end_bel(struct input_ctx *);
172 
173 /* Command table comparison function. */
174 static int	input_table_compare(const void *, const void *);
175 
176 /* Command table entry. */
177 struct input_table_entry {
178 	int		ch;
179 	const char     *interm;
180 	int		type;
181 };
182 
183 /* Escape commands. */
184 enum input_esc_type {
185 	INPUT_ESC_DECALN,
186 	INPUT_ESC_DECKPAM,
187 	INPUT_ESC_DECKPNM,
188 	INPUT_ESC_DECRC,
189 	INPUT_ESC_DECSC,
190 	INPUT_ESC_HTS,
191 	INPUT_ESC_IND,
192 	INPUT_ESC_NEL,
193 	INPUT_ESC_RI,
194 	INPUT_ESC_RIS,
195 	INPUT_ESC_SCSG0_OFF,
196 	INPUT_ESC_SCSG0_ON,
197 	INPUT_ESC_SCSG1_OFF,
198 	INPUT_ESC_SCSG1_ON,
199 	INPUT_ESC_ST,
200 };
201 
202 /* Escape command table. */
203 static const struct input_table_entry input_esc_table[] = {
204 	{ '0', "(", INPUT_ESC_SCSG0_ON },
205 	{ '0', ")", INPUT_ESC_SCSG1_ON },
206 	{ '7', "",  INPUT_ESC_DECSC },
207 	{ '8', "",  INPUT_ESC_DECRC },
208 	{ '8', "#", INPUT_ESC_DECALN },
209 	{ '=', "",  INPUT_ESC_DECKPAM },
210 	{ '>', "",  INPUT_ESC_DECKPNM },
211 	{ 'B', "(", INPUT_ESC_SCSG0_OFF },
212 	{ 'B', ")", INPUT_ESC_SCSG1_OFF },
213 	{ 'D', "",  INPUT_ESC_IND },
214 	{ 'E', "",  INPUT_ESC_NEL },
215 	{ 'H', "",  INPUT_ESC_HTS },
216 	{ 'M', "",  INPUT_ESC_RI },
217 	{ '\\', "", INPUT_ESC_ST },
218 	{ 'c', "",  INPUT_ESC_RIS },
219 };
220 
221 /* Control (CSI) commands. */
222 enum input_csi_type {
223 	INPUT_CSI_CBT,
224 	INPUT_CSI_CNL,
225 	INPUT_CSI_CPL,
226 	INPUT_CSI_CUB,
227 	INPUT_CSI_CUD,
228 	INPUT_CSI_CUF,
229 	INPUT_CSI_CUP,
230 	INPUT_CSI_CUU,
231 	INPUT_CSI_DA,
232 	INPUT_CSI_DA_TWO,
233 	INPUT_CSI_DCH,
234 	INPUT_CSI_DECSCUSR,
235 	INPUT_CSI_DECSTBM,
236 	INPUT_CSI_DL,
237 	INPUT_CSI_DSR,
238 	INPUT_CSI_ECH,
239 	INPUT_CSI_ED,
240 	INPUT_CSI_EL,
241 	INPUT_CSI_HPA,
242 	INPUT_CSI_ICH,
243 	INPUT_CSI_IL,
244 	INPUT_CSI_MODOFF,
245 	INPUT_CSI_MODSET,
246 	INPUT_CSI_RCP,
247 	INPUT_CSI_REP,
248 	INPUT_CSI_RM,
249 	INPUT_CSI_RM_PRIVATE,
250 	INPUT_CSI_SCP,
251 	INPUT_CSI_SD,
252 	INPUT_CSI_SGR,
253 	INPUT_CSI_SM,
254 	INPUT_CSI_SM_PRIVATE,
255 	INPUT_CSI_SU,
256 	INPUT_CSI_TBC,
257 	INPUT_CSI_VPA,
258 	INPUT_CSI_WINOPS,
259 	INPUT_CSI_XDA,
260 };
261 
262 /* Control (CSI) command table. */
263 static const struct input_table_entry input_csi_table[] = {
264 	{ '@', "",  INPUT_CSI_ICH },
265 	{ 'A', "",  INPUT_CSI_CUU },
266 	{ 'B', "",  INPUT_CSI_CUD },
267 	{ 'C', "",  INPUT_CSI_CUF },
268 	{ 'D', "",  INPUT_CSI_CUB },
269 	{ 'E', "",  INPUT_CSI_CNL },
270 	{ 'F', "",  INPUT_CSI_CPL },
271 	{ 'G', "",  INPUT_CSI_HPA },
272 	{ 'H', "",  INPUT_CSI_CUP },
273 	{ 'J', "",  INPUT_CSI_ED },
274 	{ 'K', "",  INPUT_CSI_EL },
275 	{ 'L', "",  INPUT_CSI_IL },
276 	{ 'M', "",  INPUT_CSI_DL },
277 	{ 'P', "",  INPUT_CSI_DCH },
278 	{ 'S', "",  INPUT_CSI_SU },
279 	{ 'T', "",  INPUT_CSI_SD },
280 	{ 'X', "",  INPUT_CSI_ECH },
281 	{ 'Z', "",  INPUT_CSI_CBT },
282 	{ '`', "",  INPUT_CSI_HPA },
283 	{ 'b', "",  INPUT_CSI_REP },
284 	{ 'c', "",  INPUT_CSI_DA },
285 	{ 'c', ">", INPUT_CSI_DA_TWO },
286 	{ 'd', "",  INPUT_CSI_VPA },
287 	{ 'f', "",  INPUT_CSI_CUP },
288 	{ 'g', "",  INPUT_CSI_TBC },
289 	{ 'h', "",  INPUT_CSI_SM },
290 	{ 'h', "?", INPUT_CSI_SM_PRIVATE },
291 	{ 'l', "",  INPUT_CSI_RM },
292 	{ 'l', "?", INPUT_CSI_RM_PRIVATE },
293 	{ 'm', "",  INPUT_CSI_SGR },
294 	{ 'm', ">", INPUT_CSI_MODSET },
295 	{ 'n', "",  INPUT_CSI_DSR },
296 	{ 'n', ">", INPUT_CSI_MODOFF },
297 	{ 'q', " ", INPUT_CSI_DECSCUSR },
298 	{ 'q', ">", INPUT_CSI_XDA },
299 	{ 'r', "",  INPUT_CSI_DECSTBM },
300 	{ 's', "",  INPUT_CSI_SCP },
301 	{ 't', "",  INPUT_CSI_WINOPS },
302 	{ 'u', "",  INPUT_CSI_RCP },
303 };
304 
305 /* Input transition. */
306 struct input_transition {
307 	int				first;
308 	int				last;
309 
310 	int				(*handler)(struct input_ctx *);
311 	const struct input_state       *state;
312 };
313 
314 /* Input state. */
315 struct input_state {
316 	const char			*name;
317 	void				(*enter)(struct input_ctx *);
318 	void				(*exit)(struct input_ctx *);
319 	const struct input_transition	*transitions;
320 };
321 
322 /* State transitions available from all states. */
323 #define INPUT_STATE_ANYWHERE \
324 	{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
325 	{ 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
326 	{ 0x1b, 0x1b, NULL,		 &input_state_esc_enter }
327 
328 /* Forward declarations of state tables. */
329 static const struct input_transition input_state_ground_table[];
330 static const struct input_transition input_state_esc_enter_table[];
331 static const struct input_transition input_state_esc_intermediate_table[];
332 static const struct input_transition input_state_csi_enter_table[];
333 static const struct input_transition input_state_csi_parameter_table[];
334 static const struct input_transition input_state_csi_intermediate_table[];
335 static const struct input_transition input_state_csi_ignore_table[];
336 static const struct input_transition input_state_dcs_enter_table[];
337 static const struct input_transition input_state_dcs_parameter_table[];
338 static const struct input_transition input_state_dcs_intermediate_table[];
339 static const struct input_transition input_state_dcs_handler_table[];
340 static const struct input_transition input_state_dcs_escape_table[];
341 static const struct input_transition input_state_dcs_ignore_table[];
342 static const struct input_transition input_state_osc_string_table[];
343 static const struct input_transition input_state_apc_string_table[];
344 static const struct input_transition input_state_rename_string_table[];
345 static const struct input_transition input_state_consume_st_table[];
346 
347 /* ground state definition. */
348 static const struct input_state input_state_ground = {
349 	"ground",
350 	input_ground, NULL,
351 	input_state_ground_table
352 };
353 
354 /* esc_enter state definition. */
355 static const struct input_state input_state_esc_enter = {
356 	"esc_enter",
357 	input_clear, NULL,
358 	input_state_esc_enter_table
359 };
360 
361 /* esc_intermediate state definition. */
362 static const struct input_state input_state_esc_intermediate = {
363 	"esc_intermediate",
364 	NULL, NULL,
365 	input_state_esc_intermediate_table
366 };
367 
368 /* csi_enter state definition. */
369 static const struct input_state input_state_csi_enter = {
370 	"csi_enter",
371 	input_clear, NULL,
372 	input_state_csi_enter_table
373 };
374 
375 /* csi_parameter state definition. */
376 static const struct input_state input_state_csi_parameter = {
377 	"csi_parameter",
378 	NULL, NULL,
379 	input_state_csi_parameter_table
380 };
381 
382 /* csi_intermediate state definition. */
383 static const struct input_state input_state_csi_intermediate = {
384 	"csi_intermediate",
385 	NULL, NULL,
386 	input_state_csi_intermediate_table
387 };
388 
389 /* csi_ignore state definition. */
390 static const struct input_state input_state_csi_ignore = {
391 	"csi_ignore",
392 	NULL, NULL,
393 	input_state_csi_ignore_table
394 };
395 
396 /* dcs_enter state definition. */
397 static const struct input_state input_state_dcs_enter = {
398 	"dcs_enter",
399 	input_enter_dcs, NULL,
400 	input_state_dcs_enter_table
401 };
402 
403 /* dcs_parameter state definition. */
404 static const struct input_state input_state_dcs_parameter = {
405 	"dcs_parameter",
406 	NULL, NULL,
407 	input_state_dcs_parameter_table
408 };
409 
410 /* dcs_intermediate state definition. */
411 static const struct input_state input_state_dcs_intermediate = {
412 	"dcs_intermediate",
413 	NULL, NULL,
414 	input_state_dcs_intermediate_table
415 };
416 
417 /* dcs_handler state definition. */
418 static const struct input_state input_state_dcs_handler = {
419 	"dcs_handler",
420 	NULL, NULL,
421 	input_state_dcs_handler_table
422 };
423 
424 /* dcs_escape state definition. */
425 static const struct input_state input_state_dcs_escape = {
426 	"dcs_escape",
427 	NULL, NULL,
428 	input_state_dcs_escape_table
429 };
430 
431 /* dcs_ignore state definition. */
432 static const struct input_state input_state_dcs_ignore = {
433 	"dcs_ignore",
434 	NULL, NULL,
435 	input_state_dcs_ignore_table
436 };
437 
438 /* osc_string state definition. */
439 static const struct input_state input_state_osc_string = {
440 	"osc_string",
441 	input_enter_osc, input_exit_osc,
442 	input_state_osc_string_table
443 };
444 
445 /* apc_string state definition. */
446 static const struct input_state input_state_apc_string = {
447 	"apc_string",
448 	input_enter_apc, input_exit_apc,
449 	input_state_apc_string_table
450 };
451 
452 /* rename_string state definition. */
453 static const struct input_state input_state_rename_string = {
454 	"rename_string",
455 	input_enter_rename, input_exit_rename,
456 	input_state_rename_string_table
457 };
458 
459 /* consume_st state definition. */
460 static const struct input_state input_state_consume_st = {
461 	"consume_st",
462 	input_enter_rename, NULL, /* rename also waits for ST */
463 	input_state_consume_st_table
464 };
465 
466 /* ground state table. */
467 static const struct input_transition input_state_ground_table[] = {
468 	INPUT_STATE_ANYWHERE,
469 
470 	{ 0x00, 0x17, input_c0_dispatch, NULL },
471 	{ 0x19, 0x19, input_c0_dispatch, NULL },
472 	{ 0x1c, 0x1f, input_c0_dispatch, NULL },
473 	{ 0x20, 0x7e, input_print,	 NULL },
474 	{ 0x7f, 0x7f, NULL,		 NULL },
475 	{ 0x80, 0xff, input_top_bit_set, NULL },
476 
477 	{ -1, -1, NULL, NULL }
478 };
479 
480 /* esc_enter state table. */
481 static const struct input_transition input_state_esc_enter_table[] = {
482 	INPUT_STATE_ANYWHERE,
483 
484 	{ 0x00, 0x17, input_c0_dispatch,  NULL },
485 	{ 0x19, 0x19, input_c0_dispatch,  NULL },
486 	{ 0x1c, 0x1f, input_c0_dispatch,  NULL },
487 	{ 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
488 	{ 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
489 	{ 0x50, 0x50, NULL,		  &input_state_dcs_enter },
490 	{ 0x51, 0x57, input_esc_dispatch, &input_state_ground },
491 	{ 0x58, 0x58, NULL,		  &input_state_consume_st },
492 	{ 0x59, 0x59, input_esc_dispatch, &input_state_ground },
493 	{ 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
494 	{ 0x5b, 0x5b, NULL,		  &input_state_csi_enter },
495 	{ 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
496 	{ 0x5d, 0x5d, NULL,		  &input_state_osc_string },
497 	{ 0x5e, 0x5e, NULL,		  &input_state_consume_st },
498 	{ 0x5f, 0x5f, NULL,		  &input_state_apc_string },
499 	{ 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
500 	{ 0x6b, 0x6b, NULL,		  &input_state_rename_string },
501 	{ 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
502 	{ 0x7f, 0xff, NULL,		  NULL },
503 
504 	{ -1, -1, NULL, NULL }
505 };
506 
507 /* esc_intermediate state table. */
508 static const struct input_transition input_state_esc_intermediate_table[] = {
509 	INPUT_STATE_ANYWHERE,
510 
511 	{ 0x00, 0x17, input_c0_dispatch,  NULL },
512 	{ 0x19, 0x19, input_c0_dispatch,  NULL },
513 	{ 0x1c, 0x1f, input_c0_dispatch,  NULL },
514 	{ 0x20, 0x2f, input_intermediate, NULL },
515 	{ 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
516 	{ 0x7f, 0xff, NULL,		  NULL },
517 
518 	{ -1, -1, NULL, NULL }
519 };
520 
521 /* csi_enter state table. */
522 static const struct input_transition input_state_csi_enter_table[] = {
523 	INPUT_STATE_ANYWHERE,
524 
525 	{ 0x00, 0x17, input_c0_dispatch,  NULL },
526 	{ 0x19, 0x19, input_c0_dispatch,  NULL },
527 	{ 0x1c, 0x1f, input_c0_dispatch,  NULL },
528 	{ 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
529 	{ 0x30, 0x39, input_parameter,	  &input_state_csi_parameter },
530 	{ 0x3a, 0x3a, input_parameter,	  &input_state_csi_parameter },
531 	{ 0x3b, 0x3b, input_parameter,	  &input_state_csi_parameter },
532 	{ 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
533 	{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
534 	{ 0x7f, 0xff, NULL,		  NULL },
535 
536 	{ -1, -1, NULL, NULL }
537 };
538 
539 /* csi_parameter state table. */
540 static const struct input_transition input_state_csi_parameter_table[] = {
541 	INPUT_STATE_ANYWHERE,
542 
543 	{ 0x00, 0x17, input_c0_dispatch,  NULL },
544 	{ 0x19, 0x19, input_c0_dispatch,  NULL },
545 	{ 0x1c, 0x1f, input_c0_dispatch,  NULL },
546 	{ 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
547 	{ 0x30, 0x39, input_parameter,	  NULL },
548 	{ 0x3a, 0x3a, input_parameter,	  NULL },
549 	{ 0x3b, 0x3b, input_parameter,	  NULL },
550 	{ 0x3c, 0x3f, NULL,		  &input_state_csi_ignore },
551 	{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
552 	{ 0x7f, 0xff, NULL,		  NULL },
553 
554 	{ -1, -1, NULL, NULL }
555 };
556 
557 /* csi_intermediate state table. */
558 static const struct input_transition input_state_csi_intermediate_table[] = {
559 	INPUT_STATE_ANYWHERE,
560 
561 	{ 0x00, 0x17, input_c0_dispatch,  NULL },
562 	{ 0x19, 0x19, input_c0_dispatch,  NULL },
563 	{ 0x1c, 0x1f, input_c0_dispatch,  NULL },
564 	{ 0x20, 0x2f, input_intermediate, NULL },
565 	{ 0x30, 0x3f, NULL,		  &input_state_csi_ignore },
566 	{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
567 	{ 0x7f, 0xff, NULL,		  NULL },
568 
569 	{ -1, -1, NULL, NULL }
570 };
571 
572 /* csi_ignore state table. */
573 static const struct input_transition input_state_csi_ignore_table[] = {
574 	INPUT_STATE_ANYWHERE,
575 
576 	{ 0x00, 0x17, input_c0_dispatch, NULL },
577 	{ 0x19, 0x19, input_c0_dispatch, NULL },
578 	{ 0x1c, 0x1f, input_c0_dispatch, NULL },
579 	{ 0x20, 0x3f, NULL,		 NULL },
580 	{ 0x40, 0x7e, NULL,		 &input_state_ground },
581 	{ 0x7f, 0xff, NULL,		 NULL },
582 
583 	{ -1, -1, NULL, NULL }
584 };
585 
586 /* dcs_enter state table. */
587 static const struct input_transition input_state_dcs_enter_table[] = {
588 	INPUT_STATE_ANYWHERE,
589 
590 	{ 0x00, 0x17, NULL,		  NULL },
591 	{ 0x19, 0x19, NULL,		  NULL },
592 	{ 0x1c, 0x1f, NULL,		  NULL },
593 	{ 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
594 	{ 0x30, 0x39, input_parameter,	  &input_state_dcs_parameter },
595 	{ 0x3a, 0x3a, NULL,		  &input_state_dcs_ignore },
596 	{ 0x3b, 0x3b, input_parameter,	  &input_state_dcs_parameter },
597 	{ 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
598 	{ 0x40, 0x7e, input_input,	  &input_state_dcs_handler },
599 	{ 0x7f, 0xff, NULL,		  NULL },
600 
601 	{ -1, -1, NULL, NULL }
602 };
603 
604 /* dcs_parameter state table. */
605 static const struct input_transition input_state_dcs_parameter_table[] = {
606 	INPUT_STATE_ANYWHERE,
607 
608 	{ 0x00, 0x17, NULL,		  NULL },
609 	{ 0x19, 0x19, NULL,		  NULL },
610 	{ 0x1c, 0x1f, NULL,		  NULL },
611 	{ 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
612 	{ 0x30, 0x39, input_parameter,	  NULL },
613 	{ 0x3a, 0x3a, NULL,		  &input_state_dcs_ignore },
614 	{ 0x3b, 0x3b, input_parameter,	  NULL },
615 	{ 0x3c, 0x3f, NULL,		  &input_state_dcs_ignore },
616 	{ 0x40, 0x7e, input_input,	  &input_state_dcs_handler },
617 	{ 0x7f, 0xff, NULL,		  NULL },
618 
619 	{ -1, -1, NULL, NULL }
620 };
621 
622 /* dcs_intermediate state table. */
623 static const struct input_transition input_state_dcs_intermediate_table[] = {
624 	INPUT_STATE_ANYWHERE,
625 
626 	{ 0x00, 0x17, NULL,		  NULL },
627 	{ 0x19, 0x19, NULL,		  NULL },
628 	{ 0x1c, 0x1f, NULL,		  NULL },
629 	{ 0x20, 0x2f, input_intermediate, NULL },
630 	{ 0x30, 0x3f, NULL,		  &input_state_dcs_ignore },
631 	{ 0x40, 0x7e, input_input,	  &input_state_dcs_handler },
632 	{ 0x7f, 0xff, NULL,		  NULL },
633 
634 	{ -1, -1, NULL, NULL }
635 };
636 
637 /* dcs_handler state table. */
638 static const struct input_transition input_state_dcs_handler_table[] = {
639 	/* No INPUT_STATE_ANYWHERE */
640 
641 	{ 0x00, 0x1a, input_input,  NULL },
642 	{ 0x1b, 0x1b, NULL,	    &input_state_dcs_escape },
643 	{ 0x1c, 0xff, input_input,  NULL },
644 
645 	{ -1, -1, NULL, NULL }
646 };
647 
648 /* dcs_escape state table. */
649 static const struct input_transition input_state_dcs_escape_table[] = {
650 	/* No INPUT_STATE_ANYWHERE */
651 
652 	{ 0x00, 0x5b, input_input,	  &input_state_dcs_handler },
653 	{ 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
654 	{ 0x5d, 0xff, input_input,	  &input_state_dcs_handler },
655 
656 	{ -1, -1, NULL, NULL }
657 };
658 
659 /* dcs_ignore state table. */
660 static const struct input_transition input_state_dcs_ignore_table[] = {
661 	INPUT_STATE_ANYWHERE,
662 
663 	{ 0x00, 0x17, NULL,	    NULL },
664 	{ 0x19, 0x19, NULL,	    NULL },
665 	{ 0x1c, 0x1f, NULL,	    NULL },
666 	{ 0x20, 0xff, NULL,	    NULL },
667 
668 	{ -1, -1, NULL, NULL }
669 };
670 
671 /* osc_string state table. */
672 static const struct input_transition input_state_osc_string_table[] = {
673 	INPUT_STATE_ANYWHERE,
674 
675 	{ 0x00, 0x06, NULL,	     NULL },
676 	{ 0x07, 0x07, input_end_bel, &input_state_ground },
677 	{ 0x08, 0x17, NULL,	     NULL },
678 	{ 0x19, 0x19, NULL,	     NULL },
679 	{ 0x1c, 0x1f, NULL,	     NULL },
680 	{ 0x20, 0xff, input_input,   NULL },
681 
682 	{ -1, -1, NULL, NULL }
683 };
684 
685 /* apc_string state table. */
686 static const struct input_transition input_state_apc_string_table[] = {
687 	INPUT_STATE_ANYWHERE,
688 
689 	{ 0x00, 0x17, NULL,	    NULL },
690 	{ 0x19, 0x19, NULL,	    NULL },
691 	{ 0x1c, 0x1f, NULL,	    NULL },
692 	{ 0x20, 0xff, input_input,  NULL },
693 
694 	{ -1, -1, NULL, NULL }
695 };
696 
697 /* rename_string state table. */
698 static const struct input_transition input_state_rename_string_table[] = {
699 	INPUT_STATE_ANYWHERE,
700 
701 	{ 0x00, 0x17, NULL,	    NULL },
702 	{ 0x19, 0x19, NULL,	    NULL },
703 	{ 0x1c, 0x1f, NULL,	    NULL },
704 	{ 0x20, 0xff, input_input,  NULL },
705 
706 	{ -1, -1, NULL, NULL }
707 };
708 
709 /* consume_st state table. */
710 static const struct input_transition input_state_consume_st_table[] = {
711 	INPUT_STATE_ANYWHERE,
712 
713 	{ 0x00, 0x17, NULL,	    NULL },
714 	{ 0x19, 0x19, NULL,	    NULL },
715 	{ 0x1c, 0x1f, NULL,	    NULL },
716 	{ 0x20, 0xff, NULL,	    NULL },
717 
718 	{ -1, -1, NULL, NULL }
719 };
720 
721 /* Input table compare. */
722 static int
723 input_table_compare(const void *key, const void *value)
724 {
725 	const struct input_ctx		*ictx = key;
726 	const struct input_table_entry	*entry = value;
727 
728 	if (ictx->ch != entry->ch)
729 		return (ictx->ch - entry->ch);
730 	return (strcmp(ictx->interm_buf, entry->interm));
731 }
732 
733 /*
734  * Timer - if this expires then have been waiting for a terminator for too
735  * long, so reset to ground.
736  */
737 static void
738 input_timer_callback(__unused int fd, __unused short events, void *arg)
739 {
740 	struct input_ctx	*ictx = arg;
741 
742 	log_debug("%s: %s expired" , __func__, ictx->state->name);
743 	input_reset(ictx, 0);
744 }
745 
746 /* Start the timer. */
747 static void
748 input_start_timer(struct input_ctx *ictx)
749 {
750 	struct timeval	tv = { .tv_sec = 5, .tv_usec = 0 };
751 
752 	event_del(&ictx->timer);
753 	event_add(&ictx->timer, &tv);
754 }
755 
756 /* Reset cell state to default. */
757 static void
758 input_reset_cell(struct input_ctx *ictx)
759 {
760 	memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
761 	ictx->cell.set = 0;
762 	ictx->cell.g0set = ictx->cell.g1set = 0;
763 
764 	memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
765 	ictx->old_cx = 0;
766 	ictx->old_cy = 0;
767 }
768 
769 /* Save screen state. */
770 static void
771 input_save_state(struct input_ctx *ictx)
772 {
773 	struct screen_write_ctx	*sctx = &ictx->ctx;
774 	struct screen		*s = sctx->s;
775 
776 	memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
777 	ictx->old_cx = s->cx;
778 	ictx->old_cy = s->cy;
779 	ictx->old_mode = s->mode;
780 }
781 
782 /* Restore screen state. */
783 static void
784 input_restore_state(struct input_ctx *ictx)
785 {
786 	struct screen_write_ctx	*sctx = &ictx->ctx;
787 
788 	memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
789 	if (ictx->old_mode & MODE_ORIGIN)
790 		screen_write_mode_set(sctx, MODE_ORIGIN);
791 	else
792 		screen_write_mode_clear(sctx, MODE_ORIGIN);
793 	screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy, 0);
794 }
795 
796 /* Initialise input parser. */
797 struct input_ctx *
798 input_init(struct window_pane *wp, struct bufferevent *bev)
799 {
800 	struct input_ctx	*ictx;
801 
802 	ictx = xcalloc(1, sizeof *ictx);
803 	ictx->wp = wp;
804 	ictx->event = bev;
805 
806 	ictx->input_space = INPUT_BUF_START;
807 	ictx->input_buf = xmalloc(INPUT_BUF_START);
808 
809 	ictx->since_ground = evbuffer_new();
810 	if (ictx->since_ground == NULL)
811 		fatalx("out of memory");
812 
813 	evtimer_set(&ictx->timer, input_timer_callback, ictx);
814 
815 	input_reset(ictx, 0);
816 	return (ictx);
817 }
818 
819 /* Destroy input parser. */
820 void
821 input_free(struct input_ctx *ictx)
822 {
823 	u_int	i;
824 
825 	for (i = 0; i < ictx->param_list_len; i++) {
826 		if (ictx->param_list[i].type == INPUT_STRING)
827 			free(ictx->param_list[i].str);
828 	}
829 
830 	event_del(&ictx->timer);
831 
832 	free(ictx->input_buf);
833 	evbuffer_free(ictx->since_ground);
834 
835 	free(ictx);
836 }
837 
838 /* Reset input state and clear screen. */
839 void
840 input_reset(struct input_ctx *ictx, int clear)
841 {
842 	struct screen_write_ctx	*sctx = &ictx->ctx;
843 	struct window_pane	*wp = ictx->wp;
844 
845 	input_reset_cell(ictx);
846 
847 	if (clear && wp != NULL) {
848 		if (TAILQ_EMPTY(&wp->modes))
849 			screen_write_start_pane(sctx, wp, &wp->base);
850 		else
851 			screen_write_start(sctx, &wp->base);
852 		screen_write_reset(sctx);
853 		screen_write_stop(sctx);
854 	}
855 
856 	input_clear(ictx);
857 
858 	ictx->last = -1;
859 
860 	ictx->state = &input_state_ground;
861 	ictx->flags = 0;
862 }
863 
864 /* Return pending data. */
865 struct evbuffer *
866 input_pending(struct input_ctx *ictx)
867 {
868 	return (ictx->since_ground);
869 }
870 
871 /* Change input state. */
872 static void
873 input_set_state(struct input_ctx *ictx, const struct input_transition *itr)
874 {
875 	if (ictx->state->exit != NULL)
876 		ictx->state->exit(ictx);
877 	ictx->state = itr->state;
878 	if (ictx->state->enter != NULL)
879 		ictx->state->enter(ictx);
880 }
881 
882 /* Parse data. */
883 static void
884 input_parse(struct input_ctx *ictx, u_char *buf, size_t len)
885 {
886 	struct screen_write_ctx		*sctx = &ictx->ctx;
887 	const struct input_state	*state = NULL;
888 	const struct input_transition	*itr = NULL;
889 	size_t				 off = 0;
890 
891 	/* Parse the input. */
892 	while (off < len) {
893 		ictx->ch = buf[off++];
894 
895 		/* Find the transition. */
896 		if (ictx->state != state ||
897 		    itr == NULL ||
898 		    ictx->ch < itr->first ||
899 		    ictx->ch > itr->last) {
900 			itr = ictx->state->transitions;
901 			while (itr->first != -1 && itr->last != -1) {
902 				if (ictx->ch >= itr->first &&
903 				    ictx->ch <= itr->last)
904 					break;
905 				itr++;
906 			}
907 			if (itr->first == -1 || itr->last == -1) {
908 				/* No transition? Eh? */
909 				fatalx("no transition from state");
910 			}
911 		}
912 		state = ictx->state;
913 
914 		/*
915 		 * Any state except print stops the current collection. This is
916 		 * an optimization to avoid checking if the attributes have
917 		 * changed for every character. It will stop unnecessarily for
918 		 * sequences that don't make a terminal change, but they should
919 		 * be the minority.
920 		 */
921 		if (itr->handler != input_print)
922 			screen_write_collect_end(sctx);
923 
924 		/*
925 		 * Execute the handler, if any. Don't switch state if it
926 		 * returns non-zero.
927 		 */
928 		if (itr->handler != NULL && itr->handler(ictx) != 0)
929 			continue;
930 
931 		/* And switch state, if necessary. */
932 		if (itr->state != NULL)
933 			input_set_state(ictx, itr);
934 
935 		/* If not in ground state, save input. */
936 		if (ictx->state != &input_state_ground)
937 			evbuffer_add(ictx->since_ground, &ictx->ch, 1);
938 	}
939 }
940 
941 /* Parse input from pane. */
942 void
943 input_parse_pane(struct window_pane *wp)
944 {
945 	void	*new_data;
946 	size_t	 new_size;
947 
948 	new_data = window_pane_get_new_data(wp, &wp->offset, &new_size);
949 	input_parse_buffer(wp, new_data, new_size);
950 	window_pane_update_used_data(wp, &wp->offset, new_size);
951 }
952 
953 /* Parse given input. */
954 void
955 input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
956 {
957 	struct input_ctx	*ictx = wp->ictx;
958 	struct screen_write_ctx	*sctx = &ictx->ctx;
959 
960 	if (len == 0)
961 		return;
962 
963 	window_update_activity(wp->window);
964 	wp->flags |= PANE_CHANGED;
965 
966 	/* NULL wp if there is a mode set as don't want to update the tty. */
967 	if (TAILQ_EMPTY(&wp->modes))
968 		screen_write_start_pane(sctx, wp, &wp->base);
969 	else
970 		screen_write_start(sctx, &wp->base);
971 
972 	log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
973 	    ictx->state->name, len, (int)len, buf);
974 
975 	input_parse(ictx, buf, len);
976 	screen_write_stop(sctx);
977 }
978 
979 /* Parse given input for screen. */
980 void
981 input_parse_screen(struct input_ctx *ictx, struct screen *s,
982     screen_write_init_ctx_cb cb, void *arg, u_char *buf, size_t len)
983 {
984 	struct screen_write_ctx	*sctx = &ictx->ctx;
985 
986 	if (len == 0)
987 		return;
988 
989 	screen_write_start_callback(sctx, s, cb, arg);
990 	input_parse(ictx, buf, len);
991 	screen_write_stop(sctx);
992 }
993 
994 /* Split the parameter list (if any). */
995 static int
996 input_split(struct input_ctx *ictx)
997 {
998 	const char		*errstr;
999 	char			*ptr, *out;
1000 	struct input_param	*ip;
1001 	u_int			 i;
1002 
1003 	for (i = 0; i < ictx->param_list_len; i++) {
1004 		if (ictx->param_list[i].type == INPUT_STRING)
1005 			free(ictx->param_list[i].str);
1006 	}
1007 	ictx->param_list_len = 0;
1008 
1009 	if (ictx->param_len == 0)
1010 		return (0);
1011 	ip = &ictx->param_list[0];
1012 
1013 	ptr = ictx->param_buf;
1014 	while ((out = strsep(&ptr, ";")) != NULL) {
1015 		if (*out == '\0')
1016 			ip->type = INPUT_MISSING;
1017 		else {
1018 			if (strchr(out, ':') != NULL) {
1019 				ip->type = INPUT_STRING;
1020 				ip->str = xstrdup(out);
1021 			} else {
1022 				ip->type = INPUT_NUMBER;
1023 				ip->num = strtonum(out, 0, INT_MAX, &errstr);
1024 				if (errstr != NULL)
1025 					return (-1);
1026 			}
1027 		}
1028 		ip = &ictx->param_list[++ictx->param_list_len];
1029 		if (ictx->param_list_len == nitems(ictx->param_list))
1030 			return (-1);
1031 	}
1032 
1033 	for (i = 0; i < ictx->param_list_len; i++) {
1034 		ip = &ictx->param_list[i];
1035 		if (ip->type == INPUT_MISSING)
1036 			log_debug("parameter %u: missing", i);
1037 		else if (ip->type == INPUT_STRING)
1038 			log_debug("parameter %u: string %s", i, ip->str);
1039 		else if (ip->type == INPUT_NUMBER)
1040 			log_debug("parameter %u: number %d", i, ip->num);
1041 	}
1042 
1043 	return (0);
1044 }
1045 
1046 /* Get an argument or return default value. */
1047 static int
1048 input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1049 {
1050 	struct input_param	*ip;
1051 	int			 retval;
1052 
1053 	if (validx >= ictx->param_list_len)
1054 	    return (defval);
1055 	ip = &ictx->param_list[validx];
1056 	if (ip->type == INPUT_MISSING)
1057 		return (defval);
1058 	if (ip->type == INPUT_STRING)
1059 		return (-1);
1060 	retval = ip->num;
1061 	if (retval < minval)
1062 		return (minval);
1063 	return (retval);
1064 }
1065 
1066 /* Reply to terminal query. */
1067 static void
1068 input_reply(struct input_ctx *ictx, const char *fmt, ...)
1069 {
1070 	struct bufferevent	*bev = ictx->event;
1071 	va_list			 ap;
1072 	char			*reply;
1073 
1074 	va_start(ap, fmt);
1075 	xvasprintf(&reply, fmt, ap);
1076 	va_end(ap);
1077 
1078 	bufferevent_write(bev, reply, strlen(reply));
1079 	free(reply);
1080 }
1081 
1082 /* Clear saved state. */
1083 static void
1084 input_clear(struct input_ctx *ictx)
1085 {
1086 	event_del(&ictx->timer);
1087 
1088 	*ictx->interm_buf = '\0';
1089 	ictx->interm_len = 0;
1090 
1091 	*ictx->param_buf = '\0';
1092 	ictx->param_len = 0;
1093 
1094 	*ictx->input_buf = '\0';
1095 	ictx->input_len = 0;
1096 
1097 	ictx->input_end = INPUT_END_ST;
1098 
1099 	ictx->flags &= ~INPUT_DISCARD;
1100 }
1101 
1102 /* Reset for ground state. */
1103 static void
1104 input_ground(struct input_ctx *ictx)
1105 {
1106 	event_del(&ictx->timer);
1107 	evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
1108 
1109 	if (ictx->input_space > INPUT_BUF_START) {
1110 		ictx->input_space = INPUT_BUF_START;
1111 		ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1112 	}
1113 }
1114 
1115 /* Output this character to the screen. */
1116 static int
1117 input_print(struct input_ctx *ictx)
1118 {
1119 	struct screen_write_ctx	*sctx = &ictx->ctx;
1120 	int			 set;
1121 
1122 	ictx->utf8started = 0; /* can't be valid UTF-8 */
1123 
1124 	set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1125 	if (set == 1)
1126 		ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1127 	else
1128 		ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1129 
1130 	utf8_set(&ictx->cell.cell.data, ictx->ch);
1131 	screen_write_collect_add(sctx, &ictx->cell.cell);
1132 	ictx->last = ictx->ch;
1133 
1134 	ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1135 
1136 	return (0);
1137 }
1138 
1139 /* Collect intermediate string. */
1140 static int
1141 input_intermediate(struct input_ctx *ictx)
1142 {
1143 	if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
1144 		ictx->flags |= INPUT_DISCARD;
1145 	else {
1146 		ictx->interm_buf[ictx->interm_len++] = ictx->ch;
1147 		ictx->interm_buf[ictx->interm_len] = '\0';
1148 	}
1149 
1150 	return (0);
1151 }
1152 
1153 /* Collect parameter string. */
1154 static int
1155 input_parameter(struct input_ctx *ictx)
1156 {
1157 	if (ictx->param_len == (sizeof ictx->param_buf) - 1)
1158 		ictx->flags |= INPUT_DISCARD;
1159 	else {
1160 		ictx->param_buf[ictx->param_len++] = ictx->ch;
1161 		ictx->param_buf[ictx->param_len] = '\0';
1162 	}
1163 
1164 	return (0);
1165 }
1166 
1167 /* Collect input string. */
1168 static int
1169 input_input(struct input_ctx *ictx)
1170 {
1171 	size_t available;
1172 
1173 	available = ictx->input_space;
1174 	while (ictx->input_len + 1 >= available) {
1175 		available *= 2;
1176 		if (available > INPUT_BUF_LIMIT) {
1177 			ictx->flags |= INPUT_DISCARD;
1178 			return (0);
1179 		}
1180 		ictx->input_buf = xrealloc(ictx->input_buf, available);
1181 		ictx->input_space = available;
1182 	}
1183 	ictx->input_buf[ictx->input_len++] = ictx->ch;
1184 	ictx->input_buf[ictx->input_len] = '\0';
1185 
1186 	return (0);
1187 }
1188 
1189 /* Execute C0 control sequence. */
1190 static int
1191 input_c0_dispatch(struct input_ctx *ictx)
1192 {
1193 	struct screen_write_ctx	*sctx = &ictx->ctx;
1194 	struct window_pane	*wp = ictx->wp;
1195 	struct screen		*s = sctx->s;
1196 
1197 	ictx->utf8started = 0; /* can't be valid UTF-8 */
1198 
1199 	log_debug("%s: '%c'", __func__, ictx->ch);
1200 
1201 	switch (ictx->ch) {
1202 	case '\000':	/* NUL */
1203 		break;
1204 	case '\007':	/* BEL */
1205 		if (wp != NULL)
1206 			alerts_queue(wp->window, WINDOW_BELL);
1207 		break;
1208 	case '\010':	/* BS */
1209 		screen_write_backspace(sctx);
1210 		break;
1211 	case '\011':	/* HT */
1212 		/* Don't tab beyond the end of the line. */
1213 		if (s->cx >= screen_size_x(s) - 1)
1214 			break;
1215 
1216 		/* Find the next tab point, or use the last column if none. */
1217 		do {
1218 			s->cx++;
1219 			if (bit_test(s->tabs, s->cx))
1220 				break;
1221 		} while (s->cx < screen_size_x(s) - 1);
1222 		break;
1223 	case '\012':	/* LF */
1224 	case '\013':	/* VT */
1225 	case '\014':	/* FF */
1226 		screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1227 		if (s->mode & MODE_CRLF)
1228 			screen_write_carriagereturn(sctx);
1229 		break;
1230 	case '\015':	/* CR */
1231 		screen_write_carriagereturn(sctx);
1232 		break;
1233 	case '\016':	/* SO */
1234 		ictx->cell.set = 1;
1235 		break;
1236 	case '\017':	/* SI */
1237 		ictx->cell.set = 0;
1238 		break;
1239 	default:
1240 		log_debug("%s: unknown '%c'", __func__, ictx->ch);
1241 		break;
1242 	}
1243 
1244 	ictx->last = -1;
1245 	return (0);
1246 }
1247 
1248 /* Execute escape sequence. */
1249 static int
1250 input_esc_dispatch(struct input_ctx *ictx)
1251 {
1252 	struct screen_write_ctx		*sctx = &ictx->ctx;
1253 	struct window_pane		*wp = ictx->wp;
1254 	struct screen			*s = sctx->s;
1255 	struct input_table_entry	*entry;
1256 
1257 	if (ictx->flags & INPUT_DISCARD)
1258 		return (0);
1259 	log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1260 
1261 	entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
1262 	    sizeof input_esc_table[0], input_table_compare);
1263 	if (entry == NULL) {
1264 		log_debug("%s: unknown '%c'", __func__, ictx->ch);
1265 		return (0);
1266 	}
1267 
1268 	switch (entry->type) {
1269 	case INPUT_ESC_RIS:
1270 		if (wp != NULL)
1271 			window_pane_reset_palette(wp);
1272 		input_reset_cell(ictx);
1273 		screen_write_reset(sctx);
1274 		break;
1275 	case INPUT_ESC_IND:
1276 		screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1277 		break;
1278 	case INPUT_ESC_NEL:
1279 		screen_write_carriagereturn(sctx);
1280 		screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1281 		break;
1282 	case INPUT_ESC_HTS:
1283 		if (s->cx < screen_size_x(s))
1284 			bit_set(s->tabs, s->cx);
1285 		break;
1286 	case INPUT_ESC_RI:
1287 		screen_write_reverseindex(sctx, ictx->cell.cell.bg);
1288 		break;
1289 	case INPUT_ESC_DECKPAM:
1290 		screen_write_mode_set(sctx, MODE_KKEYPAD);
1291 		break;
1292 	case INPUT_ESC_DECKPNM:
1293 		screen_write_mode_clear(sctx, MODE_KKEYPAD);
1294 		break;
1295 	case INPUT_ESC_DECSC:
1296 		input_save_state(ictx);
1297 		break;
1298 	case INPUT_ESC_DECRC:
1299 		input_restore_state(ictx);
1300 		break;
1301 	case INPUT_ESC_DECALN:
1302 		screen_write_alignmenttest(sctx);
1303 		break;
1304 	case INPUT_ESC_SCSG0_ON:
1305 		ictx->cell.g0set = 1;
1306 		break;
1307 	case INPUT_ESC_SCSG0_OFF:
1308 		ictx->cell.g0set = 0;
1309 		break;
1310 	case INPUT_ESC_SCSG1_ON:
1311 		ictx->cell.g1set = 1;
1312 		break;
1313 	case INPUT_ESC_SCSG1_OFF:
1314 		ictx->cell.g1set = 0;
1315 		break;
1316 	case INPUT_ESC_ST:
1317 		/* ST terminates OSC but the state transition already did it. */
1318 		break;
1319 	}
1320 
1321 	ictx->last = -1;
1322 	return (0);
1323 }
1324 
1325 /* Execute control sequence. */
1326 static int
1327 input_csi_dispatch(struct input_ctx *ictx)
1328 {
1329 	struct screen_write_ctx	       *sctx = &ictx->ctx;
1330 	struct screen		       *s = sctx->s;
1331 	struct input_table_entry       *entry;
1332 	int				i, n, m;
1333 	u_int				cx, bg = ictx->cell.cell.bg;
1334 
1335 	if (ictx->flags & INPUT_DISCARD)
1336 		return (0);
1337 
1338 	log_debug("%s: '%c' \"%s\" \"%s\"",
1339 	    __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
1340 
1341 	if (input_split(ictx) != 0)
1342 		return (0);
1343 
1344 	entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
1345 	    sizeof input_csi_table[0], input_table_compare);
1346 	if (entry == NULL) {
1347 		log_debug("%s: unknown '%c'", __func__, ictx->ch);
1348 		return (0);
1349 	}
1350 
1351 	switch (entry->type) {
1352 	case INPUT_CSI_CBT:
1353 		/* Find the previous tab point, n times. */
1354 		cx = s->cx;
1355 		if (cx > screen_size_x(s) - 1)
1356 			cx = screen_size_x(s) - 1;
1357 		n = input_get(ictx, 0, 1, 1);
1358 		if (n == -1)
1359 			break;
1360 		while (cx > 0 && n-- > 0) {
1361 			do
1362 				cx--;
1363 			while (cx > 0 && !bit_test(s->tabs, cx));
1364 		}
1365 		s->cx = cx;
1366 		break;
1367 	case INPUT_CSI_CUB:
1368 		n = input_get(ictx, 0, 1, 1);
1369 		if (n != -1)
1370 			screen_write_cursorleft(sctx, n);
1371 		break;
1372 	case INPUT_CSI_CUD:
1373 		n = input_get(ictx, 0, 1, 1);
1374 		if (n != -1)
1375 			screen_write_cursordown(sctx, n);
1376 		break;
1377 	case INPUT_CSI_CUF:
1378 		n = input_get(ictx, 0, 1, 1);
1379 		if (n != -1)
1380 			screen_write_cursorright(sctx, n);
1381 		break;
1382 	case INPUT_CSI_CUP:
1383 		n = input_get(ictx, 0, 1, 1);
1384 		m = input_get(ictx, 1, 1, 1);
1385 		if (n != -1 && m != -1)
1386 			screen_write_cursormove(sctx, m - 1, n - 1, 1);
1387 		break;
1388 	case INPUT_CSI_MODSET:
1389 		n = input_get(ictx, 0, 0, 0);
1390 		m = input_get(ictx, 1, 0, 0);
1391 		if (n == 0 || (n == 4 && m == 0))
1392 			screen_write_mode_clear(sctx, MODE_KEXTENDED);
1393 		else if (n == 4 && (m == 1 || m == 2))
1394 			screen_write_mode_set(sctx, MODE_KEXTENDED);
1395 		break;
1396 	case INPUT_CSI_MODOFF:
1397 		n = input_get(ictx, 0, 0, 0);
1398 		if (n == 4)
1399 			screen_write_mode_clear(sctx, MODE_KEXTENDED);
1400 		break;
1401 	case INPUT_CSI_WINOPS:
1402 		input_csi_dispatch_winops(ictx);
1403 		break;
1404 	case INPUT_CSI_CUU:
1405 		n = input_get(ictx, 0, 1, 1);
1406 		if (n != -1)
1407 			screen_write_cursorup(sctx, n);
1408 		break;
1409 	case INPUT_CSI_CNL:
1410 		n = input_get(ictx, 0, 1, 1);
1411 		if (n != -1) {
1412 			screen_write_carriagereturn(sctx);
1413 			screen_write_cursordown(sctx, n);
1414 		}
1415 		break;
1416 	case INPUT_CSI_CPL:
1417 		n = input_get(ictx, 0, 1, 1);
1418 		if (n != -1) {
1419 			screen_write_carriagereturn(sctx);
1420 			screen_write_cursorup(sctx, n);
1421 		}
1422 		break;
1423 	case INPUT_CSI_DA:
1424 		switch (input_get(ictx, 0, 0, 0)) {
1425 		case -1:
1426 			break;
1427 		case 0:
1428 			input_reply(ictx, "\033[?1;2c");
1429 			break;
1430 		default:
1431 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1432 			break;
1433 		}
1434 		break;
1435 	case INPUT_CSI_DA_TWO:
1436 		switch (input_get(ictx, 0, 0, 0)) {
1437 		case -1:
1438 			break;
1439 		case 0:
1440 			input_reply(ictx, "\033[>84;0;0c");
1441 			break;
1442 		default:
1443 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1444 			break;
1445 		}
1446 		break;
1447 	case INPUT_CSI_ECH:
1448 		n = input_get(ictx, 0, 1, 1);
1449 		if (n != -1)
1450 			screen_write_clearcharacter(sctx, n, bg);
1451 		break;
1452 	case INPUT_CSI_DCH:
1453 		n = input_get(ictx, 0, 1, 1);
1454 		if (n != -1)
1455 			screen_write_deletecharacter(sctx, n, bg);
1456 		break;
1457 	case INPUT_CSI_DECSTBM:
1458 		n = input_get(ictx, 0, 1, 1);
1459 		m = input_get(ictx, 1, 1, screen_size_y(s));
1460 		if (n != -1 && m != -1)
1461 			screen_write_scrollregion(sctx, n - 1, m - 1);
1462 		break;
1463 	case INPUT_CSI_DL:
1464 		n = input_get(ictx, 0, 1, 1);
1465 		if (n != -1)
1466 			screen_write_deleteline(sctx, n, bg);
1467 		break;
1468 	case INPUT_CSI_DSR:
1469 		switch (input_get(ictx, 0, 0, 0)) {
1470 		case -1:
1471 			break;
1472 		case 5:
1473 			input_reply(ictx, "\033[0n");
1474 			break;
1475 		case 6:
1476 			input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1477 			break;
1478 		default:
1479 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1480 			break;
1481 		}
1482 		break;
1483 	case INPUT_CSI_ED:
1484 		switch (input_get(ictx, 0, 0, 0)) {
1485 		case -1:
1486 			break;
1487 		case 0:
1488 			screen_write_clearendofscreen(sctx, bg);
1489 			break;
1490 		case 1:
1491 			screen_write_clearstartofscreen(sctx, bg);
1492 			break;
1493 		case 2:
1494 			screen_write_clearscreen(sctx, bg);
1495 			break;
1496 		case 3:
1497 			if (input_get(ictx, 1, 0, 0) == 0) {
1498 				/*
1499 				 * Linux console extension to clear history
1500 				 * (for example before locking the screen).
1501 				 */
1502 				screen_write_clearhistory(sctx);
1503 			}
1504 			break;
1505 		default:
1506 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1507 			break;
1508 		}
1509 		break;
1510 	case INPUT_CSI_EL:
1511 		switch (input_get(ictx, 0, 0, 0)) {
1512 		case -1:
1513 			break;
1514 		case 0:
1515 			screen_write_clearendofline(sctx, bg);
1516 			break;
1517 		case 1:
1518 			screen_write_clearstartofline(sctx, bg);
1519 			break;
1520 		case 2:
1521 			screen_write_clearline(sctx, bg);
1522 			break;
1523 		default:
1524 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1525 			break;
1526 		}
1527 		break;
1528 	case INPUT_CSI_HPA:
1529 		n = input_get(ictx, 0, 1, 1);
1530 		if (n != -1)
1531 			screen_write_cursormove(sctx, n - 1, -1, 1);
1532 		break;
1533 	case INPUT_CSI_ICH:
1534 		n = input_get(ictx, 0, 1, 1);
1535 		if (n != -1)
1536 			screen_write_insertcharacter(sctx, n, bg);
1537 		break;
1538 	case INPUT_CSI_IL:
1539 		n = input_get(ictx, 0, 1, 1);
1540 		if (n != -1)
1541 			screen_write_insertline(sctx, n, bg);
1542 		break;
1543 	case INPUT_CSI_REP:
1544 		n = input_get(ictx, 0, 1, 1);
1545 		if (n == -1)
1546 			break;
1547 
1548 		m = screen_size_x(s) - s->cx;
1549 		if (n > m)
1550 			n = m;
1551 
1552 		if (ictx->last == -1)
1553 			break;
1554 		ictx->ch = ictx->last;
1555 
1556 		for (i = 0; i < n; i++)
1557 			input_print(ictx);
1558 		break;
1559 	case INPUT_CSI_RCP:
1560 		input_restore_state(ictx);
1561 		break;
1562 	case INPUT_CSI_RM:
1563 		input_csi_dispatch_rm(ictx);
1564 		break;
1565 	case INPUT_CSI_RM_PRIVATE:
1566 		input_csi_dispatch_rm_private(ictx);
1567 		break;
1568 	case INPUT_CSI_SCP:
1569 		input_save_state(ictx);
1570 		break;
1571 	case INPUT_CSI_SGR:
1572 		input_csi_dispatch_sgr(ictx);
1573 		break;
1574 	case INPUT_CSI_SM:
1575 		input_csi_dispatch_sm(ictx);
1576 		break;
1577 	case INPUT_CSI_SM_PRIVATE:
1578 		input_csi_dispatch_sm_private(ictx);
1579 		break;
1580 	case INPUT_CSI_SU:
1581 		n = input_get(ictx, 0, 1, 1);
1582 		if (n != -1)
1583 			screen_write_scrollup(sctx, n, bg);
1584 		break;
1585 	case INPUT_CSI_SD:
1586 		n = input_get(ictx, 0, 1, 1);
1587 		if (n != -1)
1588 			screen_write_scrolldown(sctx, n, bg);
1589 		break;
1590 	case INPUT_CSI_TBC:
1591 		switch (input_get(ictx, 0, 0, 0)) {
1592 		case -1:
1593 			break;
1594 		case 0:
1595 			if (s->cx < screen_size_x(s))
1596 				bit_clear(s->tabs, s->cx);
1597 			break;
1598 		case 3:
1599 			bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1600 			break;
1601 		default:
1602 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1603 			break;
1604 		}
1605 		break;
1606 	case INPUT_CSI_VPA:
1607 		n = input_get(ictx, 0, 1, 1);
1608 		if (n != -1)
1609 			screen_write_cursormove(sctx, -1, n - 1, 1);
1610 		break;
1611 	case INPUT_CSI_DECSCUSR:
1612 		n = input_get(ictx, 0, 0, 0);
1613 		if (n != -1)
1614 			screen_set_cursor_style(s, n);
1615 		break;
1616 	case INPUT_CSI_XDA:
1617 		n = input_get(ictx, 0, 0, 0);
1618 		if (n == 0)
1619 			input_reply(ictx, "\033P>|tmux %s\033\\", getversion());
1620 		break;
1621 
1622 	}
1623 
1624 	ictx->last = -1;
1625 	return (0);
1626 }
1627 
1628 /* Handle CSI RM. */
1629 static void
1630 input_csi_dispatch_rm(struct input_ctx *ictx)
1631 {
1632 	struct screen_write_ctx	*sctx = &ictx->ctx;
1633 	u_int			 i;
1634 
1635 	for (i = 0; i < ictx->param_list_len; i++) {
1636 		switch (input_get(ictx, i, 0, -1)) {
1637 		case -1:
1638 			break;
1639 		case 4:		/* IRM */
1640 			screen_write_mode_clear(sctx, MODE_INSERT);
1641 			break;
1642 		case 34:
1643 			screen_write_mode_set(sctx, MODE_BLINKING);
1644 			break;
1645 		default:
1646 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1647 			break;
1648 		}
1649 	}
1650 }
1651 
1652 /* Handle CSI private RM. */
1653 static void
1654 input_csi_dispatch_rm_private(struct input_ctx *ictx)
1655 {
1656 	struct screen_write_ctx	*sctx = &ictx->ctx;
1657 	struct grid_cell	*gc = &ictx->cell.cell;
1658 	u_int			 i;
1659 
1660 	for (i = 0; i < ictx->param_list_len; i++) {
1661 		switch (input_get(ictx, i, 0, -1)) {
1662 		case -1:
1663 			break;
1664 		case 1:		/* DECCKM */
1665 			screen_write_mode_clear(sctx, MODE_KCURSOR);
1666 			break;
1667 		case 3:		/* DECCOLM */
1668 			screen_write_cursormove(sctx, 0, 0, 1);
1669 			screen_write_clearscreen(sctx, gc->bg);
1670 			break;
1671 		case 6:		/* DECOM */
1672 			screen_write_mode_clear(sctx, MODE_ORIGIN);
1673 			screen_write_cursormove(sctx, 0, 0, 1);
1674 			break;
1675 		case 7:		/* DECAWM */
1676 			screen_write_mode_clear(sctx, MODE_WRAP);
1677 			break;
1678 		case 12:
1679 			screen_write_mode_clear(sctx, MODE_BLINKING);
1680 			break;
1681 		case 25:	/* TCEM */
1682 			screen_write_mode_clear(sctx, MODE_CURSOR);
1683 			break;
1684 		case 1000:
1685 		case 1001:
1686 		case 1002:
1687 		case 1003:
1688 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1689 			break;
1690 		case 1004:
1691 			screen_write_mode_clear(sctx, MODE_FOCUSON);
1692 			break;
1693 		case 1005:
1694 			screen_write_mode_clear(sctx, MODE_MOUSE_UTF8);
1695 			break;
1696 		case 1006:
1697 			screen_write_mode_clear(sctx, MODE_MOUSE_SGR);
1698 			break;
1699 		case 47:
1700 		case 1047:
1701 			screen_write_alternateoff(sctx, gc, 0);
1702 			break;
1703 		case 1049:
1704 			screen_write_alternateoff(sctx, gc, 1);
1705 			break;
1706 		case 2004:
1707 			screen_write_mode_clear(sctx, MODE_BRACKETPASTE);
1708 			break;
1709 		default:
1710 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1711 			break;
1712 		}
1713 	}
1714 }
1715 
1716 /* Handle CSI SM. */
1717 static void
1718 input_csi_dispatch_sm(struct input_ctx *ictx)
1719 {
1720 	struct screen_write_ctx	*sctx = &ictx->ctx;
1721 	u_int			 i;
1722 
1723 	for (i = 0; i < ictx->param_list_len; i++) {
1724 		switch (input_get(ictx, i, 0, -1)) {
1725 		case -1:
1726 			break;
1727 		case 4:		/* IRM */
1728 			screen_write_mode_set(sctx, MODE_INSERT);
1729 			break;
1730 		case 34:
1731 			screen_write_mode_clear(sctx, MODE_BLINKING);
1732 			break;
1733 		default:
1734 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1735 			break;
1736 		}
1737 	}
1738 }
1739 
1740 /* Handle CSI private SM. */
1741 static void
1742 input_csi_dispatch_sm_private(struct input_ctx *ictx)
1743 {
1744 	struct screen_write_ctx	*sctx = &ictx->ctx;
1745 	struct window_pane	*wp = ictx->wp;
1746 	struct grid_cell	*gc = &ictx->cell.cell;
1747 	u_int			 i;
1748 
1749 	for (i = 0; i < ictx->param_list_len; i++) {
1750 		switch (input_get(ictx, i, 0, -1)) {
1751 		case -1:
1752 			break;
1753 		case 1:		/* DECCKM */
1754 			screen_write_mode_set(sctx, MODE_KCURSOR);
1755 			break;
1756 		case 3:		/* DECCOLM */
1757 			screen_write_cursormove(sctx, 0, 0, 1);
1758 			screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1759 			break;
1760 		case 6:		/* DECOM */
1761 			screen_write_mode_set(sctx, MODE_ORIGIN);
1762 			screen_write_cursormove(sctx, 0, 0, 1);
1763 			break;
1764 		case 7:		/* DECAWM */
1765 			screen_write_mode_set(sctx, MODE_WRAP);
1766 			break;
1767 		case 12:
1768 			screen_write_mode_set(sctx, MODE_BLINKING);
1769 			break;
1770 		case 25:	/* TCEM */
1771 			screen_write_mode_set(sctx, MODE_CURSOR);
1772 			break;
1773 		case 1000:
1774 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1775 			screen_write_mode_set(sctx, MODE_MOUSE_STANDARD);
1776 			break;
1777 		case 1002:
1778 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1779 			screen_write_mode_set(sctx, MODE_MOUSE_BUTTON);
1780 			break;
1781 		case 1003:
1782 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1783 			screen_write_mode_set(sctx, MODE_MOUSE_ALL);
1784 			break;
1785 		case 1004:
1786 			if (sctx->s->mode & MODE_FOCUSON)
1787 				break;
1788 			screen_write_mode_set(sctx, MODE_FOCUSON);
1789 			if (wp != NULL)
1790 				wp->flags |= PANE_FOCUSPUSH; /* force update */
1791 			break;
1792 		case 1005:
1793 			screen_write_mode_set(sctx, MODE_MOUSE_UTF8);
1794 			break;
1795 		case 1006:
1796 			screen_write_mode_set(sctx, MODE_MOUSE_SGR);
1797 			break;
1798 		case 47:
1799 		case 1047:
1800 			screen_write_alternateon(sctx, gc, 0);
1801 			break;
1802 		case 1049:
1803 			screen_write_alternateon(sctx, gc, 1);
1804 			break;
1805 		case 2004:
1806 			screen_write_mode_set(sctx, MODE_BRACKETPASTE);
1807 			break;
1808 		default:
1809 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1810 			break;
1811 		}
1812 	}
1813 }
1814 
1815 /* Handle CSI window operations. */
1816 static void
1817 input_csi_dispatch_winops(struct input_ctx *ictx)
1818 {
1819 	struct screen_write_ctx	*sctx = &ictx->ctx;
1820 	struct screen		*s = sctx->s;
1821 	struct window_pane	*wp = ictx->wp;
1822 	u_int			 x = screen_size_x(s), y = screen_size_y(s);
1823 	int			 n, m;
1824 
1825 	m = 0;
1826 	while ((n = input_get(ictx, m, 0, -1)) != -1) {
1827 		switch (n) {
1828 		case 1:
1829 		case 2:
1830 		case 5:
1831 		case 6:
1832 		case 7:
1833 		case 11:
1834 		case 13:
1835 		case 14:
1836 		case 19:
1837 		case 20:
1838 		case 21:
1839 		case 24:
1840 			break;
1841 		case 3:
1842 		case 4:
1843 		case 8:
1844 			m++;
1845 			if (input_get(ictx, m, 0, -1) == -1)
1846 				return;
1847 			/* FALLTHROUGH */
1848 		case 9:
1849 		case 10:
1850 			m++;
1851 			if (input_get(ictx, m, 0, -1) == -1)
1852 				return;
1853 			break;
1854 		case 22:
1855 			m++;
1856 			switch (input_get(ictx, m, 0, -1)) {
1857 			case -1:
1858 				return;
1859 			case 0:
1860 			case 2:
1861 				screen_push_title(sctx->s);
1862 				break;
1863 			}
1864 			break;
1865 		case 23:
1866 			m++;
1867 			switch (input_get(ictx, m, 0, -1)) {
1868 			case -1:
1869 				return;
1870 			case 0:
1871 			case 2:
1872 				screen_pop_title(sctx->s);
1873 				if (wp != NULL) {
1874 					notify_pane("pane-title-changed", wp);
1875 					server_redraw_window_borders(wp->window);
1876 					server_status_window(wp->window);
1877 				}
1878 				break;
1879 			}
1880 			break;
1881 		case 18:
1882 			input_reply(ictx, "\033[8;%u;%ut", x, y);
1883 			break;
1884 		default:
1885 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1886 			break;
1887 		}
1888 		m++;
1889 	}
1890 }
1891 
1892 /* Helper for 256 colour SGR. */
1893 static int
1894 input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
1895 {
1896 	struct grid_cell	*gc = &ictx->cell.cell;
1897 
1898 	if (c == -1 || c > 255) {
1899 		if (fgbg == 38)
1900 			gc->fg = 8;
1901 		else if (fgbg == 48)
1902 			gc->bg = 8;
1903 	} else {
1904 		if (fgbg == 38)
1905 			gc->fg = c | COLOUR_FLAG_256;
1906 		else if (fgbg == 48)
1907 			gc->bg = c | COLOUR_FLAG_256;
1908 		else if (fgbg == 58)
1909 			gc->us = c | COLOUR_FLAG_256;
1910 	}
1911 	return (1);
1912 }
1913 
1914 /* Handle CSI SGR for 256 colours. */
1915 static void
1916 input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
1917 {
1918 	int	c;
1919 
1920 	c = input_get(ictx, (*i) + 1, 0, -1);
1921 	if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
1922 		(*i)++;
1923 }
1924 
1925 /* Helper for RGB colour SGR. */
1926 static int
1927 input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
1928     int b)
1929 {
1930 	struct grid_cell	*gc = &ictx->cell.cell;
1931 
1932 	if (r == -1 || r > 255)
1933 		return (0);
1934 	if (g == -1 || g > 255)
1935 		return (0);
1936 	if (b == -1 || b > 255)
1937 		return (0);
1938 
1939 	if (fgbg == 38)
1940 		gc->fg = colour_join_rgb(r, g, b);
1941 	else if (fgbg == 48)
1942 		gc->bg = colour_join_rgb(r, g, b);
1943 	else if (fgbg == 58)
1944 		gc->us = colour_join_rgb(r, g, b);
1945 	return (1);
1946 }
1947 
1948 /* Handle CSI SGR for RGB colours. */
1949 static void
1950 input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
1951 {
1952 	int	r, g, b;
1953 
1954 	r = input_get(ictx, (*i) + 1, 0, -1);
1955 	g = input_get(ictx, (*i) + 2, 0, -1);
1956 	b = input_get(ictx, (*i) + 3, 0, -1);
1957 	if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
1958 		(*i) += 3;
1959 }
1960 
1961 /* Handle CSI SGR with a ISO parameter. */
1962 static void
1963 input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
1964 {
1965 	struct grid_cell	*gc = &ictx->cell.cell;
1966 	char			*s = ictx->param_list[i].str, *copy, *ptr, *out;
1967 	int			 p[8];
1968 	u_int			 n;
1969 	const char		*errstr;
1970 
1971 	for (n = 0; n < nitems(p); n++)
1972 		p[n] = -1;
1973 	n = 0;
1974 
1975 	ptr = copy = xstrdup(s);
1976 	while ((out = strsep(&ptr, ":")) != NULL) {
1977 		if (*out != '\0') {
1978 			p[n++] = strtonum(out, 0, INT_MAX, &errstr);
1979 			if (errstr != NULL || n == nitems(p)) {
1980 				free(copy);
1981 				return;
1982 			}
1983 		} else {
1984 			n++;
1985 			if (n == nitems(p)) {
1986 				free(copy);
1987 				return;
1988 			}
1989 		}
1990 		log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
1991 	}
1992 	free(copy);
1993 
1994 	if (n == 0)
1995 		return;
1996 	if (p[0] == 4) {
1997 		if (n != 2)
1998 			return;
1999 		switch (p[1]) {
2000 		case 0:
2001 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2002 			break;
2003 		case 1:
2004 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2005 			gc->attr |= GRID_ATTR_UNDERSCORE;
2006 			break;
2007 		case 2:
2008 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2009 			gc->attr |= GRID_ATTR_UNDERSCORE_2;
2010 			break;
2011 		case 3:
2012 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2013 			gc->attr |= GRID_ATTR_UNDERSCORE_3;
2014 			break;
2015 		case 4:
2016 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2017 			gc->attr |= GRID_ATTR_UNDERSCORE_4;
2018 			break;
2019 		case 5:
2020 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2021 			gc->attr |= GRID_ATTR_UNDERSCORE_5;
2022 			break;
2023 		}
2024 		return;
2025 	}
2026 	if (n < 2 || (p[0] != 38 && p[0] != 48 && p[0] != 58))
2027 		return;
2028 	switch (p[1]) {
2029 	case 2:
2030 		if (n < 3)
2031 			break;
2032 		if (n == 5)
2033 			i = 2;
2034 		else
2035 			i = 3;
2036 		if (n < i + 3)
2037 			break;
2038 		input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
2039 		    p[i + 2]);
2040 		break;
2041 	case 5:
2042 		if (n < 3)
2043 			break;
2044 		input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
2045 		break;
2046 	}
2047 }
2048 
2049 /* Handle CSI SGR. */
2050 static void
2051 input_csi_dispatch_sgr(struct input_ctx *ictx)
2052 {
2053 	struct grid_cell	*gc = &ictx->cell.cell;
2054 	u_int			 i;
2055 	int			 n;
2056 
2057 	if (ictx->param_list_len == 0) {
2058 		memcpy(gc, &grid_default_cell, sizeof *gc);
2059 		return;
2060 	}
2061 
2062 	for (i = 0; i < ictx->param_list_len; i++) {
2063 		if (ictx->param_list[i].type == INPUT_STRING) {
2064 			input_csi_dispatch_sgr_colon(ictx, i);
2065 			continue;
2066 		}
2067 		n = input_get(ictx, i, 0, 0);
2068 		if (n == -1)
2069 			continue;
2070 
2071 		if (n == 38 || n == 48 || n == 58) {
2072 			i++;
2073 			switch (input_get(ictx, i, 0, -1)) {
2074 			case 2:
2075 				input_csi_dispatch_sgr_rgb(ictx, n, &i);
2076 				break;
2077 			case 5:
2078 				input_csi_dispatch_sgr_256(ictx, n, &i);
2079 				break;
2080 			}
2081 			continue;
2082 		}
2083 
2084 		switch (n) {
2085 		case 0:
2086 			memcpy(gc, &grid_default_cell, sizeof *gc);
2087 			break;
2088 		case 1:
2089 			gc->attr |= GRID_ATTR_BRIGHT;
2090 			break;
2091 		case 2:
2092 			gc->attr |= GRID_ATTR_DIM;
2093 			break;
2094 		case 3:
2095 			gc->attr |= GRID_ATTR_ITALICS;
2096 			break;
2097 		case 4:
2098 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2099 			gc->attr |= GRID_ATTR_UNDERSCORE;
2100 			break;
2101 		case 5:
2102 			gc->attr |= GRID_ATTR_BLINK;
2103 			break;
2104 		case 7:
2105 			gc->attr |= GRID_ATTR_REVERSE;
2106 			break;
2107 		case 8:
2108 			gc->attr |= GRID_ATTR_HIDDEN;
2109 			break;
2110 		case 9:
2111 			gc->attr |= GRID_ATTR_STRIKETHROUGH;
2112 			break;
2113 		case 22:
2114 			gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
2115 			break;
2116 		case 23:
2117 			gc->attr &= ~GRID_ATTR_ITALICS;
2118 			break;
2119 		case 24:
2120 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2121 			break;
2122 		case 25:
2123 			gc->attr &= ~GRID_ATTR_BLINK;
2124 			break;
2125 		case 27:
2126 			gc->attr &= ~GRID_ATTR_REVERSE;
2127 			break;
2128 		case 28:
2129 			gc->attr &= ~GRID_ATTR_HIDDEN;
2130 			break;
2131 		case 29:
2132 			gc->attr &= ~GRID_ATTR_STRIKETHROUGH;
2133 			break;
2134 		case 30:
2135 		case 31:
2136 		case 32:
2137 		case 33:
2138 		case 34:
2139 		case 35:
2140 		case 36:
2141 		case 37:
2142 			gc->fg = n - 30;
2143 			break;
2144 		case 39:
2145 			gc->fg = 8;
2146 			break;
2147 		case 40:
2148 		case 41:
2149 		case 42:
2150 		case 43:
2151 		case 44:
2152 		case 45:
2153 		case 46:
2154 		case 47:
2155 			gc->bg = n - 40;
2156 			break;
2157 		case 49:
2158 			gc->bg = 8;
2159 			break;
2160 		case 53:
2161 			gc->attr |= GRID_ATTR_OVERLINE;
2162 			break;
2163 		case 55:
2164 			gc->attr &= ~GRID_ATTR_OVERLINE;
2165 			break;
2166 		case 59:
2167 			gc->us = 0;
2168 			break;
2169 		case 90:
2170 		case 91:
2171 		case 92:
2172 		case 93:
2173 		case 94:
2174 		case 95:
2175 		case 96:
2176 		case 97:
2177 			gc->fg = n;
2178 			break;
2179 		case 100:
2180 		case 101:
2181 		case 102:
2182 		case 103:
2183 		case 104:
2184 		case 105:
2185 		case 106:
2186 		case 107:
2187 			gc->bg = n - 10;
2188 			break;
2189 		}
2190 	}
2191 }
2192 
2193 /* End of input with BEL. */
2194 static int
2195 input_end_bel(struct input_ctx *ictx)
2196 {
2197 	log_debug("%s", __func__);
2198 
2199 	ictx->input_end = INPUT_END_BEL;
2200 
2201 	return (0);
2202 }
2203 
2204 /* DCS string started. */
2205 static void
2206 input_enter_dcs(struct input_ctx *ictx)
2207 {
2208 	log_debug("%s", __func__);
2209 
2210 	input_clear(ictx);
2211 	input_start_timer(ictx);
2212 	ictx->last = -1;
2213 }
2214 
2215 /* DCS terminator (ST) received. */
2216 static int
2217 input_dcs_dispatch(struct input_ctx *ictx)
2218 {
2219 	struct screen_write_ctx	*sctx = &ictx->ctx;
2220 	u_char			*buf = ictx->input_buf;
2221 	size_t			 len = ictx->input_len;
2222 	const char		 prefix[] = "tmux;";
2223 	const u_int		 prefixlen = (sizeof prefix) - 1;
2224 
2225 	if (ictx->flags & INPUT_DISCARD)
2226 		return (0);
2227 
2228 	log_debug("%s: \"%s\"", __func__, buf);
2229 
2230 	if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0)
2231 		screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen);
2232 
2233 	return (0);
2234 }
2235 
2236 /* OSC string started. */
2237 static void
2238 input_enter_osc(struct input_ctx *ictx)
2239 {
2240 	log_debug("%s", __func__);
2241 
2242 	input_clear(ictx);
2243 	input_start_timer(ictx);
2244 	ictx->last = -1;
2245 }
2246 
2247 /* OSC terminator (ST) received. */
2248 static void
2249 input_exit_osc(struct input_ctx *ictx)
2250 {
2251 	struct screen_write_ctx	*sctx = &ictx->ctx;
2252 	struct window_pane	*wp = ictx->wp;
2253 	u_char			*p = ictx->input_buf;
2254 	u_int			 option;
2255 
2256 	if (ictx->flags & INPUT_DISCARD)
2257 		return;
2258 	if (ictx->input_len < 1 || *p < '0' || *p > '9')
2259 		return;
2260 
2261 	log_debug("%s: \"%s\" (end %s)", __func__, p,
2262 	    ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2263 
2264 	option = 0;
2265 	while (*p >= '0' && *p <= '9')
2266 		option = option * 10 + *p++ - '0';
2267 	if (*p == ';')
2268 		p++;
2269 
2270 	switch (option) {
2271 	case 0:
2272 	case 2:
2273 		if (screen_set_title(sctx->s, p) && wp != NULL) {
2274 			notify_pane("pane-title-changed", wp);
2275 			server_redraw_window_borders(wp->window);
2276 			server_status_window(wp->window);
2277 		}
2278 		break;
2279 	case 4:
2280 		input_osc_4(ictx, p);
2281 		break;
2282 	case 7:
2283 		if (utf8_isvalid(p)) {
2284 			screen_set_path(sctx->s, p);
2285 			if (wp != NULL) {
2286 				server_redraw_window_borders(wp->window);
2287 				server_status_window(wp->window);
2288 			}
2289 		}
2290 		break;
2291 	case 10:
2292 		input_osc_10(ictx, p);
2293 		break;
2294 	case 11:
2295 		input_osc_11(ictx, p);
2296 		break;
2297 	case 12:
2298 		if (utf8_isvalid(p) && *p != '?') /* ? is colour request */
2299 			screen_set_cursor_colour(sctx->s, p);
2300 		break;
2301 	case 52:
2302 		input_osc_52(ictx, p);
2303 		break;
2304 	case 104:
2305 		input_osc_104(ictx, p);
2306 		break;
2307 	case 112:
2308 		if (*p == '\0') /* no arguments allowed */
2309 			screen_set_cursor_colour(sctx->s, "");
2310 		break;
2311 	default:
2312 		log_debug("%s: unknown '%u'", __func__, option);
2313 		break;
2314 	}
2315 }
2316 
2317 /* APC string started. */
2318 static void
2319 input_enter_apc(struct input_ctx *ictx)
2320 {
2321 	log_debug("%s", __func__);
2322 
2323 	input_clear(ictx);
2324 	input_start_timer(ictx);
2325 	ictx->last = -1;
2326 }
2327 
2328 /* APC terminator (ST) received. */
2329 static void
2330 input_exit_apc(struct input_ctx *ictx)
2331 {
2332 	struct screen_write_ctx	*sctx = &ictx->ctx;
2333 	struct window_pane	*wp = ictx->wp;
2334 
2335 	if (ictx->flags & INPUT_DISCARD)
2336 		return;
2337 	log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2338 
2339 	if (screen_set_title(sctx->s, ictx->input_buf) && wp != NULL) {
2340 		notify_pane("pane-title-changed", wp);
2341 		server_redraw_window_borders(wp->window);
2342 		server_status_window(wp->window);
2343 	}
2344 }
2345 
2346 /* Rename string started. */
2347 static void
2348 input_enter_rename(struct input_ctx *ictx)
2349 {
2350 	log_debug("%s", __func__);
2351 
2352 	input_clear(ictx);
2353 	input_start_timer(ictx);
2354 	ictx->last = -1;
2355 }
2356 
2357 /* Rename terminator (ST) received. */
2358 static void
2359 input_exit_rename(struct input_ctx *ictx)
2360 {
2361 	struct window_pane	*wp = ictx->wp;
2362 	struct options_entry	*o;
2363 
2364 	if (wp == NULL)
2365 		return;
2366 	if (ictx->flags & INPUT_DISCARD)
2367 		return;
2368 	if (!options_get_number(ictx->wp->options, "allow-rename"))
2369 		return;
2370 	log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2371 
2372 	if (!utf8_isvalid(ictx->input_buf))
2373 		return;
2374 
2375 	if (ictx->input_len == 0) {
2376 		o = options_get_only(wp->window->options, "automatic-rename");
2377 		if (o != NULL)
2378 			options_remove_or_default(o, -1, NULL);
2379 		return;
2380 	}
2381 	window_set_name(wp->window, ictx->input_buf);
2382 	options_set_number(wp->window->options, "automatic-rename", 0);
2383 	server_redraw_window_borders(wp->window);
2384 	server_status_window(wp->window);
2385 }
2386 
2387 /* Open UTF-8 character. */
2388 static int
2389 input_top_bit_set(struct input_ctx *ictx)
2390 {
2391 	struct screen_write_ctx	*sctx = &ictx->ctx;
2392 	struct utf8_data	*ud = &ictx->utf8data;
2393 
2394 	ictx->last = -1;
2395 
2396 	if (!ictx->utf8started) {
2397 		if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2398 			return (0);
2399 		ictx->utf8started = 1;
2400 		return (0);
2401 	}
2402 
2403 	switch (utf8_append(ud, ictx->ch)) {
2404 	case UTF8_MORE:
2405 		return (0);
2406 	case UTF8_ERROR:
2407 		ictx->utf8started = 0;
2408 		return (0);
2409 	case UTF8_DONE:
2410 		break;
2411 	}
2412 	ictx->utf8started = 0;
2413 
2414 	log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2415 	    (int)ud->size, ud->data, ud->width);
2416 
2417 	utf8_copy(&ictx->cell.cell.data, ud);
2418 	screen_write_collect_add(sctx, &ictx->cell.cell);
2419 
2420 	return (0);
2421 }
2422 
2423 /* Parse colour from OSC. */
2424 static int
2425 input_osc_parse_colour(const char *p, u_int *r, u_int *g, u_int *b)
2426 {
2427 	u_int		 rsize, gsize, bsize;
2428 	const char	*cp, *s = p;
2429 
2430 	if (sscanf(p, "rgb:%x/%x/%x", r, g, b) != 3)
2431 		return (0);
2432 	p += 4;
2433 
2434 	cp = strchr(p, '/');
2435 	rsize = cp - p;
2436 	if (rsize == 1)
2437 		(*r) = (*r) | ((*r) << 4);
2438 	else if (rsize == 3)
2439 		(*r) >>= 4;
2440 	else if (rsize == 4)
2441 		(*r) >>= 8;
2442 	else if (rsize != 2)
2443 		return (0);
2444 
2445 	p = cp + 1;
2446 	cp = strchr(p, '/');
2447 	gsize = cp - p;
2448 	if (gsize == 1)
2449 		(*g) = (*g) | ((*g) << 4);
2450 	else if (gsize == 3)
2451 		(*g) >>= 4;
2452 	else if (gsize == 4)
2453 		(*g) >>= 8;
2454 	else if (gsize != 2)
2455 		return (0);
2456 
2457 	bsize = strlen(cp + 1);
2458 	if (bsize == 1)
2459 		(*b) = (*b) | ((*b) << 4);
2460 	else if (bsize == 3)
2461 		(*b) >>= 4;
2462 	else if (bsize == 4)
2463 		(*b) >>= 8;
2464 	else if (bsize != 2)
2465 		return (0);
2466 
2467 	log_debug("%s: %s = %02x%02x%02x", __func__, s, *r, *g, *b);
2468 	return (1);
2469 }
2470 
2471 /* Reply to a colour request. */
2472 static void
2473 input_osc_colour_reply(struct input_ctx *ictx, u_int n, int c)
2474 {
2475     u_char	 r, g, b;
2476     const char	*end;
2477 
2478     if (c == 8 || (~c & COLOUR_FLAG_RGB))
2479 	    return;
2480     colour_split_rgb(c, &r, &g, &b);
2481 
2482     if (ictx->input_end == INPUT_END_BEL)
2483 	    end = "\007";
2484     else
2485 	    end = "\033\\";
2486     input_reply(ictx, "\033]%u;rgb:%02hhx/%02hhx/%02hhx%s", n, r, g, b, end);
2487 }
2488 
2489 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2490 static void
2491 input_osc_4(struct input_ctx *ictx, const char *p)
2492 {
2493 	struct window_pane	*wp = ictx->wp;
2494 	char			*copy, *s, *next = NULL;
2495 	long			 idx;
2496 	u_int			 r, g, b;
2497 
2498 	if (wp == NULL)
2499 		return;
2500 
2501 	copy = s = xstrdup(p);
2502 	while (s != NULL && *s != '\0') {
2503 		idx = strtol(s, &next, 10);
2504 		if (*next++ != ';')
2505 			goto bad;
2506 		if (idx < 0 || idx >= 0x100)
2507 			goto bad;
2508 
2509 		s = strsep(&next, ";");
2510 		if (!input_osc_parse_colour(s, &r, &g, &b)) {
2511 			s = next;
2512 			continue;
2513 		}
2514 
2515 		window_pane_set_palette(wp, idx, colour_join_rgb(r, g, b));
2516 		s = next;
2517 	}
2518 
2519 	free(copy);
2520 	return;
2521 
2522 bad:
2523 	log_debug("bad OSC 4: %s", p);
2524 	free(copy);
2525 }
2526 
2527 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2528 static void
2529 input_osc_10(struct input_ctx *ictx, const char *p)
2530 {
2531 	struct window_pane	*wp = ictx->wp;
2532 	struct grid_cell	 defaults;
2533 	u_int			 r, g, b;
2534 
2535 	if (wp == NULL)
2536 		return;
2537 
2538 	if (strcmp(p, "?") == 0) {
2539 		tty_default_colours(&defaults, wp);
2540 		input_osc_colour_reply(ictx, 10, defaults.fg);
2541 		return;
2542 	}
2543 
2544 	if (!input_osc_parse_colour(p, &r, &g, &b))
2545 		goto bad;
2546 	wp->fg = colour_join_rgb(r, g, b);
2547 	wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
2548 
2549 	return;
2550 
2551 bad:
2552 	log_debug("bad OSC 10: %s", p);
2553 }
2554 
2555 /* Handle the OSC 11 sequence for setting and querying background colour. */
2556 static void
2557 input_osc_11(struct input_ctx *ictx, const char *p)
2558 {
2559 	struct window_pane	*wp = ictx->wp;
2560 	struct grid_cell	 defaults;
2561 	u_int			 r, g, b;
2562 
2563 	if (wp == NULL)
2564 		return;
2565 
2566 	if (strcmp(p, "?") == 0) {
2567 		tty_default_colours(&defaults, wp);
2568 		input_osc_colour_reply(ictx, 11, defaults.bg);
2569 		return;
2570 	}
2571 
2572 	if (!input_osc_parse_colour(p, &r, &g, &b))
2573 	    goto bad;
2574 	wp->bg = colour_join_rgb(r, g, b);
2575 	wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
2576 
2577 	return;
2578 
2579 bad:
2580 	log_debug("bad OSC 11: %s", p);
2581 }
2582 
2583 /* Handle the OSC 52 sequence for setting the clipboard. */
2584 static void
2585 input_osc_52(struct input_ctx *ictx, const char *p)
2586 {
2587 	struct window_pane	*wp = ictx->wp;
2588 	char			*end;
2589 	const char		*buf;
2590 	size_t			 len;
2591 	u_char			*out;
2592 	int			 outlen, state;
2593 	struct screen_write_ctx	 ctx;
2594 	struct paste_buffer	*pb;
2595 
2596 	if (wp == NULL)
2597 		return;
2598 	state = options_get_number(global_options, "set-clipboard");
2599 	if (state != 2)
2600 		return;
2601 
2602 	if ((end = strchr(p, ';')) == NULL)
2603 		return;
2604 	end++;
2605 	if (*end == '\0')
2606 		return;
2607 	log_debug("%s: %s", __func__, end);
2608 
2609 	if (strcmp(end, "?") == 0) {
2610 		if ((pb = paste_get_top(NULL)) != NULL) {
2611 			buf = paste_buffer_data(pb, &len);
2612 			outlen = 4 * ((len + 2) / 3) + 1;
2613 			out = xmalloc(outlen);
2614 			if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
2615 				free(out);
2616 				return;
2617 			}
2618 		} else {
2619 			outlen = 0;
2620 			out = NULL;
2621 		}
2622 		bufferevent_write(ictx->event, "\033]52;;", 6);
2623 		if (outlen != 0)
2624 			bufferevent_write(ictx->event, out, outlen);
2625 		if (ictx->input_end == INPUT_END_BEL)
2626 			bufferevent_write(ictx->event, "\007", 1);
2627 		else
2628 			bufferevent_write(ictx->event, "\033\\", 2);
2629 		free(out);
2630 		return;
2631 	}
2632 
2633 	len = (strlen(end) / 4) * 3;
2634 	if (len == 0)
2635 		return;
2636 
2637 	out = xmalloc(len);
2638 	if ((outlen = b64_pton(end, out, len)) == -1) {
2639 		free(out);
2640 		return;
2641 	}
2642 
2643 	screen_write_start_pane(&ctx, wp, NULL);
2644 	screen_write_setselection(&ctx, out, outlen);
2645 	screen_write_stop(&ctx);
2646 	notify_pane("pane-set-clipboard", wp);
2647 
2648 	paste_add(NULL, out, outlen);
2649 }
2650 
2651 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2652 static void
2653 input_osc_104(struct input_ctx *ictx, const char *p)
2654 {
2655 	struct window_pane	*wp = ictx->wp;
2656 	char			*copy, *s;
2657 	long			 idx;
2658 
2659 	if (wp == NULL)
2660 		return;
2661 
2662 	if (*p == '\0') {
2663 		window_pane_reset_palette(wp);
2664 		return;
2665 	}
2666 
2667 	copy = s = xstrdup(p);
2668 	while (*s != '\0') {
2669 		idx = strtol(s, &s, 10);
2670 		if (*s != '\0' && *s != ';')
2671 			goto bad;
2672 		if (idx < 0 || idx >= 0x100)
2673 			goto bad;
2674 
2675 		window_pane_unset_palette(wp, idx);
2676 		if (*s == ';')
2677 			s++;
2678 	}
2679 	free(copy);
2680 	return;
2681 
2682 bad:
2683 	log_debug("bad OSC 104: %s", p);
2684 	free(copy);
2685 }
2686