xref: /openbsd/usr.bin/tmux/input.c (revision 905646f0)
1 /* $OpenBSD: input.c,v 1.183 2020/08/19 06:37:23 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 		if (ictx->last == -1)
1549 			break;
1550 		ictx->ch = ictx->last;
1551 
1552 		for (i = 0; i < n; i++)
1553 			input_print(ictx);
1554 		break;
1555 	case INPUT_CSI_RCP:
1556 		input_restore_state(ictx);
1557 		break;
1558 	case INPUT_CSI_RM:
1559 		input_csi_dispatch_rm(ictx);
1560 		break;
1561 	case INPUT_CSI_RM_PRIVATE:
1562 		input_csi_dispatch_rm_private(ictx);
1563 		break;
1564 	case INPUT_CSI_SCP:
1565 		input_save_state(ictx);
1566 		break;
1567 	case INPUT_CSI_SGR:
1568 		input_csi_dispatch_sgr(ictx);
1569 		break;
1570 	case INPUT_CSI_SM:
1571 		input_csi_dispatch_sm(ictx);
1572 		break;
1573 	case INPUT_CSI_SM_PRIVATE:
1574 		input_csi_dispatch_sm_private(ictx);
1575 		break;
1576 	case INPUT_CSI_SU:
1577 		n = input_get(ictx, 0, 1, 1);
1578 		if (n != -1)
1579 			screen_write_scrollup(sctx, n, bg);
1580 		break;
1581 	case INPUT_CSI_SD:
1582 		n = input_get(ictx, 0, 1, 1);
1583 		if (n != -1)
1584 			screen_write_scrolldown(sctx, n, bg);
1585 		break;
1586 	case INPUT_CSI_TBC:
1587 		switch (input_get(ictx, 0, 0, 0)) {
1588 		case -1:
1589 			break;
1590 		case 0:
1591 			if (s->cx < screen_size_x(s))
1592 				bit_clear(s->tabs, s->cx);
1593 			break;
1594 		case 3:
1595 			bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1596 			break;
1597 		default:
1598 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1599 			break;
1600 		}
1601 		break;
1602 	case INPUT_CSI_VPA:
1603 		n = input_get(ictx, 0, 1, 1);
1604 		if (n != -1)
1605 			screen_write_cursormove(sctx, -1, n - 1, 1);
1606 		break;
1607 	case INPUT_CSI_DECSCUSR:
1608 		n = input_get(ictx, 0, 0, 0);
1609 		if (n != -1)
1610 			screen_set_cursor_style(s, n);
1611 		break;
1612 	case INPUT_CSI_XDA:
1613 		n = input_get(ictx, 0, 0, 0);
1614 		if (n == 0)
1615 			input_reply(ictx, "\033P>|tmux %s\033\\", getversion());
1616 		break;
1617 
1618 	}
1619 
1620 	ictx->last = -1;
1621 	return (0);
1622 }
1623 
1624 /* Handle CSI RM. */
1625 static void
1626 input_csi_dispatch_rm(struct input_ctx *ictx)
1627 {
1628 	struct screen_write_ctx	*sctx = &ictx->ctx;
1629 	u_int			 i;
1630 
1631 	for (i = 0; i < ictx->param_list_len; i++) {
1632 		switch (input_get(ictx, i, 0, -1)) {
1633 		case -1:
1634 			break;
1635 		case 4:		/* IRM */
1636 			screen_write_mode_clear(sctx, MODE_INSERT);
1637 			break;
1638 		case 34:
1639 			screen_write_mode_set(sctx, MODE_BLINKING);
1640 			break;
1641 		default:
1642 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1643 			break;
1644 		}
1645 	}
1646 }
1647 
1648 /* Handle CSI private RM. */
1649 static void
1650 input_csi_dispatch_rm_private(struct input_ctx *ictx)
1651 {
1652 	struct screen_write_ctx	*sctx = &ictx->ctx;
1653 	struct grid_cell	*gc = &ictx->cell.cell;
1654 	u_int			 i;
1655 
1656 	for (i = 0; i < ictx->param_list_len; i++) {
1657 		switch (input_get(ictx, i, 0, -1)) {
1658 		case -1:
1659 			break;
1660 		case 1:		/* DECCKM */
1661 			screen_write_mode_clear(sctx, MODE_KCURSOR);
1662 			break;
1663 		case 3:		/* DECCOLM */
1664 			screen_write_cursormove(sctx, 0, 0, 1);
1665 			screen_write_clearscreen(sctx, gc->bg);
1666 			break;
1667 		case 6:		/* DECOM */
1668 			screen_write_mode_clear(sctx, MODE_ORIGIN);
1669 			screen_write_cursormove(sctx, 0, 0, 1);
1670 			break;
1671 		case 7:		/* DECAWM */
1672 			screen_write_mode_clear(sctx, MODE_WRAP);
1673 			break;
1674 		case 12:
1675 			screen_write_mode_clear(sctx, MODE_BLINKING);
1676 			break;
1677 		case 25:	/* TCEM */
1678 			screen_write_mode_clear(sctx, MODE_CURSOR);
1679 			break;
1680 		case 1000:
1681 		case 1001:
1682 		case 1002:
1683 		case 1003:
1684 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1685 			break;
1686 		case 1004:
1687 			screen_write_mode_clear(sctx, MODE_FOCUSON);
1688 			break;
1689 		case 1005:
1690 			screen_write_mode_clear(sctx, MODE_MOUSE_UTF8);
1691 			break;
1692 		case 1006:
1693 			screen_write_mode_clear(sctx, MODE_MOUSE_SGR);
1694 			break;
1695 		case 47:
1696 		case 1047:
1697 			screen_write_alternateoff(sctx, gc, 0);
1698 			break;
1699 		case 1049:
1700 			screen_write_alternateoff(sctx, gc, 1);
1701 			break;
1702 		case 2004:
1703 			screen_write_mode_clear(sctx, MODE_BRACKETPASTE);
1704 			break;
1705 		default:
1706 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1707 			break;
1708 		}
1709 	}
1710 }
1711 
1712 /* Handle CSI SM. */
1713 static void
1714 input_csi_dispatch_sm(struct input_ctx *ictx)
1715 {
1716 	struct screen_write_ctx	*sctx = &ictx->ctx;
1717 	u_int			 i;
1718 
1719 	for (i = 0; i < ictx->param_list_len; i++) {
1720 		switch (input_get(ictx, i, 0, -1)) {
1721 		case -1:
1722 			break;
1723 		case 4:		/* IRM */
1724 			screen_write_mode_set(sctx, MODE_INSERT);
1725 			break;
1726 		case 34:
1727 			screen_write_mode_clear(sctx, MODE_BLINKING);
1728 			break;
1729 		default:
1730 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1731 			break;
1732 		}
1733 	}
1734 }
1735 
1736 /* Handle CSI private SM. */
1737 static void
1738 input_csi_dispatch_sm_private(struct input_ctx *ictx)
1739 {
1740 	struct screen_write_ctx	*sctx = &ictx->ctx;
1741 	struct window_pane	*wp = ictx->wp;
1742 	struct grid_cell	*gc = &ictx->cell.cell;
1743 	u_int			 i;
1744 
1745 	for (i = 0; i < ictx->param_list_len; i++) {
1746 		switch (input_get(ictx, i, 0, -1)) {
1747 		case -1:
1748 			break;
1749 		case 1:		/* DECCKM */
1750 			screen_write_mode_set(sctx, MODE_KCURSOR);
1751 			break;
1752 		case 3:		/* DECCOLM */
1753 			screen_write_cursormove(sctx, 0, 0, 1);
1754 			screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1755 			break;
1756 		case 6:		/* DECOM */
1757 			screen_write_mode_set(sctx, MODE_ORIGIN);
1758 			screen_write_cursormove(sctx, 0, 0, 1);
1759 			break;
1760 		case 7:		/* DECAWM */
1761 			screen_write_mode_set(sctx, MODE_WRAP);
1762 			break;
1763 		case 12:
1764 			screen_write_mode_set(sctx, MODE_BLINKING);
1765 			break;
1766 		case 25:	/* TCEM */
1767 			screen_write_mode_set(sctx, MODE_CURSOR);
1768 			break;
1769 		case 1000:
1770 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1771 			screen_write_mode_set(sctx, MODE_MOUSE_STANDARD);
1772 			break;
1773 		case 1002:
1774 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1775 			screen_write_mode_set(sctx, MODE_MOUSE_BUTTON);
1776 			break;
1777 		case 1003:
1778 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1779 			screen_write_mode_set(sctx, MODE_MOUSE_ALL);
1780 			break;
1781 		case 1004:
1782 			if (sctx->s->mode & MODE_FOCUSON)
1783 				break;
1784 			screen_write_mode_set(sctx, MODE_FOCUSON);
1785 			if (wp != NULL)
1786 				wp->flags |= PANE_FOCUSPUSH; /* force update */
1787 			break;
1788 		case 1005:
1789 			screen_write_mode_set(sctx, MODE_MOUSE_UTF8);
1790 			break;
1791 		case 1006:
1792 			screen_write_mode_set(sctx, MODE_MOUSE_SGR);
1793 			break;
1794 		case 47:
1795 		case 1047:
1796 			screen_write_alternateon(sctx, gc, 0);
1797 			break;
1798 		case 1049:
1799 			screen_write_alternateon(sctx, gc, 1);
1800 			break;
1801 		case 2004:
1802 			screen_write_mode_set(sctx, MODE_BRACKETPASTE);
1803 			break;
1804 		default:
1805 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1806 			break;
1807 		}
1808 	}
1809 }
1810 
1811 /* Handle CSI window operations. */
1812 static void
1813 input_csi_dispatch_winops(struct input_ctx *ictx)
1814 {
1815 	struct screen_write_ctx	*sctx = &ictx->ctx;
1816 	struct screen		*s = sctx->s;
1817 	struct window_pane	*wp = ictx->wp;
1818 	u_int			 x = screen_size_x(s), y = screen_size_y(s);
1819 	int			 n, m;
1820 
1821 	m = 0;
1822 	while ((n = input_get(ictx, m, 0, -1)) != -1) {
1823 		switch (n) {
1824 		case 1:
1825 		case 2:
1826 		case 5:
1827 		case 6:
1828 		case 7:
1829 		case 11:
1830 		case 13:
1831 		case 14:
1832 		case 19:
1833 		case 20:
1834 		case 21:
1835 		case 24:
1836 			break;
1837 		case 3:
1838 		case 4:
1839 		case 8:
1840 			m++;
1841 			if (input_get(ictx, m, 0, -1) == -1)
1842 				return;
1843 			/* FALLTHROUGH */
1844 		case 9:
1845 		case 10:
1846 			m++;
1847 			if (input_get(ictx, m, 0, -1) == -1)
1848 				return;
1849 			break;
1850 		case 22:
1851 			m++;
1852 			switch (input_get(ictx, m, 0, -1)) {
1853 			case -1:
1854 				return;
1855 			case 0:
1856 			case 2:
1857 				screen_push_title(sctx->s);
1858 				break;
1859 			}
1860 			break;
1861 		case 23:
1862 			m++;
1863 			switch (input_get(ictx, m, 0, -1)) {
1864 			case -1:
1865 				return;
1866 			case 0:
1867 			case 2:
1868 				screen_pop_title(sctx->s);
1869 				if (wp != NULL) {
1870 					notify_pane("pane-title-changed", wp);
1871 					server_redraw_window_borders(wp->window);
1872 					server_status_window(wp->window);
1873 				}
1874 				break;
1875 			}
1876 			break;
1877 		case 18:
1878 			input_reply(ictx, "\033[8;%u;%ut", x, y);
1879 			break;
1880 		default:
1881 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1882 			break;
1883 		}
1884 		m++;
1885 	}
1886 }
1887 
1888 /* Helper for 256 colour SGR. */
1889 static int
1890 input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
1891 {
1892 	struct grid_cell	*gc = &ictx->cell.cell;
1893 
1894 	if (c == -1 || c > 255) {
1895 		if (fgbg == 38)
1896 			gc->fg = 8;
1897 		else if (fgbg == 48)
1898 			gc->bg = 8;
1899 	} else {
1900 		if (fgbg == 38)
1901 			gc->fg = c | COLOUR_FLAG_256;
1902 		else if (fgbg == 48)
1903 			gc->bg = c | COLOUR_FLAG_256;
1904 		else if (fgbg == 58)
1905 			gc->us = c | COLOUR_FLAG_256;
1906 	}
1907 	return (1);
1908 }
1909 
1910 /* Handle CSI SGR for 256 colours. */
1911 static void
1912 input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
1913 {
1914 	int	c;
1915 
1916 	c = input_get(ictx, (*i) + 1, 0, -1);
1917 	if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
1918 		(*i)++;
1919 }
1920 
1921 /* Helper for RGB colour SGR. */
1922 static int
1923 input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
1924     int b)
1925 {
1926 	struct grid_cell	*gc = &ictx->cell.cell;
1927 
1928 	if (r == -1 || r > 255)
1929 		return (0);
1930 	if (g == -1 || g > 255)
1931 		return (0);
1932 	if (b == -1 || b > 255)
1933 		return (0);
1934 
1935 	if (fgbg == 38)
1936 		gc->fg = colour_join_rgb(r, g, b);
1937 	else if (fgbg == 48)
1938 		gc->bg = colour_join_rgb(r, g, b);
1939 	else if (fgbg == 58)
1940 		gc->us = colour_join_rgb(r, g, b);
1941 	return (1);
1942 }
1943 
1944 /* Handle CSI SGR for RGB colours. */
1945 static void
1946 input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
1947 {
1948 	int	r, g, b;
1949 
1950 	r = input_get(ictx, (*i) + 1, 0, -1);
1951 	g = input_get(ictx, (*i) + 2, 0, -1);
1952 	b = input_get(ictx, (*i) + 3, 0, -1);
1953 	if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
1954 		(*i) += 3;
1955 }
1956 
1957 /* Handle CSI SGR with a ISO parameter. */
1958 static void
1959 input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
1960 {
1961 	struct grid_cell	*gc = &ictx->cell.cell;
1962 	char			*s = ictx->param_list[i].str, *copy, *ptr, *out;
1963 	int			 p[8];
1964 	u_int			 n;
1965 	const char		*errstr;
1966 
1967 	for (n = 0; n < nitems(p); n++)
1968 		p[n] = -1;
1969 	n = 0;
1970 
1971 	ptr = copy = xstrdup(s);
1972 	while ((out = strsep(&ptr, ":")) != NULL) {
1973 		if (*out != '\0') {
1974 			p[n++] = strtonum(out, 0, INT_MAX, &errstr);
1975 			if (errstr != NULL || n == nitems(p)) {
1976 				free(copy);
1977 				return;
1978 			}
1979 		} else
1980 			n++;
1981 		log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
1982 	}
1983 	free(copy);
1984 
1985 	if (n == 0)
1986 		return;
1987 	if (p[0] == 4) {
1988 		if (n != 2)
1989 			return;
1990 		switch (p[1]) {
1991 		case 0:
1992 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1993 			break;
1994 		case 1:
1995 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1996 			gc->attr |= GRID_ATTR_UNDERSCORE;
1997 			break;
1998 		case 2:
1999 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2000 			gc->attr |= GRID_ATTR_UNDERSCORE_2;
2001 			break;
2002 		case 3:
2003 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2004 			gc->attr |= GRID_ATTR_UNDERSCORE_3;
2005 			break;
2006 		case 4:
2007 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2008 			gc->attr |= GRID_ATTR_UNDERSCORE_4;
2009 			break;
2010 		case 5:
2011 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2012 			gc->attr |= GRID_ATTR_UNDERSCORE_5;
2013 			break;
2014 		}
2015 		return;
2016 	}
2017 	if (n < 2 || (p[0] != 38 && p[0] != 48 && p[0] != 58))
2018 		return;
2019 	switch (p[1]) {
2020 	case 2:
2021 		if (n < 3)
2022 			break;
2023 		if (n == 5)
2024 			i = 2;
2025 		else
2026 			i = 3;
2027 		if (n < i + 3)
2028 			break;
2029 		input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
2030 		    p[i + 2]);
2031 		break;
2032 	case 5:
2033 		if (n < 3)
2034 			break;
2035 		input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
2036 		break;
2037 	}
2038 }
2039 
2040 /* Handle CSI SGR. */
2041 static void
2042 input_csi_dispatch_sgr(struct input_ctx *ictx)
2043 {
2044 	struct grid_cell	*gc = &ictx->cell.cell;
2045 	u_int			 i;
2046 	int			 n;
2047 
2048 	if (ictx->param_list_len == 0) {
2049 		memcpy(gc, &grid_default_cell, sizeof *gc);
2050 		return;
2051 	}
2052 
2053 	for (i = 0; i < ictx->param_list_len; i++) {
2054 		if (ictx->param_list[i].type == INPUT_STRING) {
2055 			input_csi_dispatch_sgr_colon(ictx, i);
2056 			continue;
2057 		}
2058 		n = input_get(ictx, i, 0, 0);
2059 		if (n == -1)
2060 			continue;
2061 
2062 		if (n == 38 || n == 48 || n == 58) {
2063 			i++;
2064 			switch (input_get(ictx, i, 0, -1)) {
2065 			case 2:
2066 				input_csi_dispatch_sgr_rgb(ictx, n, &i);
2067 				break;
2068 			case 5:
2069 				input_csi_dispatch_sgr_256(ictx, n, &i);
2070 				break;
2071 			}
2072 			continue;
2073 		}
2074 
2075 		switch (n) {
2076 		case 0:
2077 			memcpy(gc, &grid_default_cell, sizeof *gc);
2078 			break;
2079 		case 1:
2080 			gc->attr |= GRID_ATTR_BRIGHT;
2081 			break;
2082 		case 2:
2083 			gc->attr |= GRID_ATTR_DIM;
2084 			break;
2085 		case 3:
2086 			gc->attr |= GRID_ATTR_ITALICS;
2087 			break;
2088 		case 4:
2089 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2090 			gc->attr |= GRID_ATTR_UNDERSCORE;
2091 			break;
2092 		case 5:
2093 			gc->attr |= GRID_ATTR_BLINK;
2094 			break;
2095 		case 7:
2096 			gc->attr |= GRID_ATTR_REVERSE;
2097 			break;
2098 		case 8:
2099 			gc->attr |= GRID_ATTR_HIDDEN;
2100 			break;
2101 		case 9:
2102 			gc->attr |= GRID_ATTR_STRIKETHROUGH;
2103 			break;
2104 		case 22:
2105 			gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
2106 			break;
2107 		case 23:
2108 			gc->attr &= ~GRID_ATTR_ITALICS;
2109 			break;
2110 		case 24:
2111 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2112 			break;
2113 		case 25:
2114 			gc->attr &= ~GRID_ATTR_BLINK;
2115 			break;
2116 		case 27:
2117 			gc->attr &= ~GRID_ATTR_REVERSE;
2118 			break;
2119 		case 28:
2120 			gc->attr &= ~GRID_ATTR_HIDDEN;
2121 			break;
2122 		case 29:
2123 			gc->attr &= ~GRID_ATTR_STRIKETHROUGH;
2124 			break;
2125 		case 30:
2126 		case 31:
2127 		case 32:
2128 		case 33:
2129 		case 34:
2130 		case 35:
2131 		case 36:
2132 		case 37:
2133 			gc->fg = n - 30;
2134 			break;
2135 		case 39:
2136 			gc->fg = 8;
2137 			break;
2138 		case 40:
2139 		case 41:
2140 		case 42:
2141 		case 43:
2142 		case 44:
2143 		case 45:
2144 		case 46:
2145 		case 47:
2146 			gc->bg = n - 40;
2147 			break;
2148 		case 49:
2149 			gc->bg = 8;
2150 			break;
2151 		case 53:
2152 			gc->attr |= GRID_ATTR_OVERLINE;
2153 			break;
2154 		case 55:
2155 			gc->attr &= ~GRID_ATTR_OVERLINE;
2156 			break;
2157 		case 59:
2158 			gc->us = 0;
2159 			break;
2160 		case 90:
2161 		case 91:
2162 		case 92:
2163 		case 93:
2164 		case 94:
2165 		case 95:
2166 		case 96:
2167 		case 97:
2168 			gc->fg = n;
2169 			break;
2170 		case 100:
2171 		case 101:
2172 		case 102:
2173 		case 103:
2174 		case 104:
2175 		case 105:
2176 		case 106:
2177 		case 107:
2178 			gc->bg = n - 10;
2179 			break;
2180 		}
2181 	}
2182 }
2183 
2184 /* End of input with BEL. */
2185 static int
2186 input_end_bel(struct input_ctx *ictx)
2187 {
2188 	log_debug("%s", __func__);
2189 
2190 	ictx->input_end = INPUT_END_BEL;
2191 
2192 	return (0);
2193 }
2194 
2195 /* DCS string started. */
2196 static void
2197 input_enter_dcs(struct input_ctx *ictx)
2198 {
2199 	log_debug("%s", __func__);
2200 
2201 	input_clear(ictx);
2202 	input_start_timer(ictx);
2203 	ictx->last = -1;
2204 }
2205 
2206 /* DCS terminator (ST) received. */
2207 static int
2208 input_dcs_dispatch(struct input_ctx *ictx)
2209 {
2210 	struct screen_write_ctx	*sctx = &ictx->ctx;
2211 	u_char			*buf = ictx->input_buf;
2212 	size_t			 len = ictx->input_len;
2213 	const char		 prefix[] = "tmux;";
2214 	const u_int		 prefixlen = (sizeof prefix) - 1;
2215 
2216 	if (ictx->flags & INPUT_DISCARD)
2217 		return (0);
2218 
2219 	log_debug("%s: \"%s\"", __func__, buf);
2220 
2221 	if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0)
2222 		screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen);
2223 
2224 	return (0);
2225 }
2226 
2227 /* OSC string started. */
2228 static void
2229 input_enter_osc(struct input_ctx *ictx)
2230 {
2231 	log_debug("%s", __func__);
2232 
2233 	input_clear(ictx);
2234 	input_start_timer(ictx);
2235 	ictx->last = -1;
2236 }
2237 
2238 /* OSC terminator (ST) received. */
2239 static void
2240 input_exit_osc(struct input_ctx *ictx)
2241 {
2242 	struct screen_write_ctx	*sctx = &ictx->ctx;
2243 	struct window_pane	*wp = ictx->wp;
2244 	u_char			*p = ictx->input_buf;
2245 	u_int			 option;
2246 
2247 	if (ictx->flags & INPUT_DISCARD)
2248 		return;
2249 	if (ictx->input_len < 1 || *p < '0' || *p > '9')
2250 		return;
2251 
2252 	log_debug("%s: \"%s\" (end %s)", __func__, p,
2253 	    ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2254 
2255 	option = 0;
2256 	while (*p >= '0' && *p <= '9')
2257 		option = option * 10 + *p++ - '0';
2258 	if (*p == ';')
2259 		p++;
2260 
2261 	switch (option) {
2262 	case 0:
2263 	case 2:
2264 		if (screen_set_title(sctx->s, p) && wp != NULL) {
2265 			notify_pane("pane-title-changed", wp);
2266 			server_redraw_window_borders(wp->window);
2267 			server_status_window(wp->window);
2268 		}
2269 		break;
2270 	case 4:
2271 		input_osc_4(ictx, p);
2272 		break;
2273 	case 7:
2274 		if (utf8_isvalid(p)) {
2275 			screen_set_path(sctx->s, p);
2276 			if (wp != NULL) {
2277 				server_redraw_window_borders(wp->window);
2278 				server_status_window(wp->window);
2279 			}
2280 		}
2281 		break;
2282 	case 10:
2283 		input_osc_10(ictx, p);
2284 		break;
2285 	case 11:
2286 		input_osc_11(ictx, p);
2287 		break;
2288 	case 12:
2289 		if (utf8_isvalid(p) && *p != '?') /* ? is colour request */
2290 			screen_set_cursor_colour(sctx->s, p);
2291 		break;
2292 	case 52:
2293 		input_osc_52(ictx, p);
2294 		break;
2295 	case 104:
2296 		input_osc_104(ictx, p);
2297 		break;
2298 	case 112:
2299 		if (*p == '\0') /* no arguments allowed */
2300 			screen_set_cursor_colour(sctx->s, "");
2301 		break;
2302 	default:
2303 		log_debug("%s: unknown '%u'", __func__, option);
2304 		break;
2305 	}
2306 }
2307 
2308 /* APC string started. */
2309 static void
2310 input_enter_apc(struct input_ctx *ictx)
2311 {
2312 	log_debug("%s", __func__);
2313 
2314 	input_clear(ictx);
2315 	input_start_timer(ictx);
2316 	ictx->last = -1;
2317 }
2318 
2319 /* APC terminator (ST) received. */
2320 static void
2321 input_exit_apc(struct input_ctx *ictx)
2322 {
2323 	struct screen_write_ctx	*sctx = &ictx->ctx;
2324 	struct window_pane	*wp = ictx->wp;
2325 
2326 	if (ictx->flags & INPUT_DISCARD)
2327 		return;
2328 	log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2329 
2330 	if (screen_set_title(sctx->s, ictx->input_buf) && wp != NULL) {
2331 		notify_pane("pane-title-changed", wp);
2332 		server_redraw_window_borders(wp->window);
2333 		server_status_window(wp->window);
2334 	}
2335 }
2336 
2337 /* Rename string started. */
2338 static void
2339 input_enter_rename(struct input_ctx *ictx)
2340 {
2341 	log_debug("%s", __func__);
2342 
2343 	input_clear(ictx);
2344 	input_start_timer(ictx);
2345 	ictx->last = -1;
2346 }
2347 
2348 /* Rename terminator (ST) received. */
2349 static void
2350 input_exit_rename(struct input_ctx *ictx)
2351 {
2352 	struct window_pane	*wp = ictx->wp;
2353 	struct options_entry	*o;
2354 
2355 	if (wp == NULL)
2356 		return;
2357 	if (ictx->flags & INPUT_DISCARD)
2358 		return;
2359 	if (!options_get_number(ictx->wp->options, "allow-rename"))
2360 		return;
2361 	log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2362 
2363 	if (!utf8_isvalid(ictx->input_buf))
2364 		return;
2365 
2366 	if (ictx->input_len == 0) {
2367 		o = options_get_only(wp->window->options, "automatic-rename");
2368 		if (o != NULL)
2369 			options_remove_or_default(o, -1, NULL);
2370 		return;
2371 	}
2372 	window_set_name(wp->window, ictx->input_buf);
2373 	options_set_number(wp->window->options, "automatic-rename", 0);
2374 	server_redraw_window_borders(wp->window);
2375 	server_status_window(wp->window);
2376 }
2377 
2378 /* Open UTF-8 character. */
2379 static int
2380 input_top_bit_set(struct input_ctx *ictx)
2381 {
2382 	struct screen_write_ctx	*sctx = &ictx->ctx;
2383 	struct utf8_data	*ud = &ictx->utf8data;
2384 
2385 	ictx->last = -1;
2386 
2387 	if (!ictx->utf8started) {
2388 		if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2389 			return (0);
2390 		ictx->utf8started = 1;
2391 		return (0);
2392 	}
2393 
2394 	switch (utf8_append(ud, ictx->ch)) {
2395 	case UTF8_MORE:
2396 		return (0);
2397 	case UTF8_ERROR:
2398 		ictx->utf8started = 0;
2399 		return (0);
2400 	case UTF8_DONE:
2401 		break;
2402 	}
2403 	ictx->utf8started = 0;
2404 
2405 	log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2406 	    (int)ud->size, ud->data, ud->width);
2407 
2408 	utf8_copy(&ictx->cell.cell.data, ud);
2409 	screen_write_collect_add(sctx, &ictx->cell.cell);
2410 
2411 	return (0);
2412 }
2413 
2414 /* Parse colour from OSC. */
2415 static int
2416 input_osc_parse_colour(const char *p, u_int *r, u_int *g, u_int *b)
2417 {
2418 	u_int		 rsize, gsize, bsize;
2419 	const char	*cp, *s = p;
2420 
2421 	if (sscanf(p, "rgb:%x/%x/%x", r, g, b) != 3)
2422 		return (0);
2423 	p += 4;
2424 
2425 	cp = strchr(p, '/');
2426 	rsize = cp - p;
2427 	if (rsize == 1)
2428 		(*r) = (*r) | ((*r) << 4);
2429 	else if (rsize == 3)
2430 		(*r) >>= 4;
2431 	else if (rsize == 4)
2432 		(*r) >>= 8;
2433 	else if (rsize != 2)
2434 		return (0);
2435 
2436 	p = cp + 1;
2437 	cp = strchr(p, '/');
2438 	gsize = cp - p;
2439 	if (gsize == 1)
2440 		(*g) = (*g) | ((*g) << 4);
2441 	else if (gsize == 3)
2442 		(*g) >>= 4;
2443 	else if (gsize == 4)
2444 		(*g) >>= 8;
2445 	else if (gsize != 2)
2446 		return (0);
2447 
2448 	bsize = strlen(cp + 1);
2449 	if (bsize == 1)
2450 		(*b) = (*b) | ((*b) << 4);
2451 	else if (bsize == 3)
2452 		(*b) >>= 4;
2453 	else if (bsize == 4)
2454 		(*b) >>= 8;
2455 	else if (bsize != 2)
2456 		return (0);
2457 
2458 	log_debug("%s: %s = %02x%02x%02x", __func__, s, *r, *g, *b);
2459 	return (1);
2460 }
2461 
2462 /* Reply to a colour request. */
2463 static void
2464 input_osc_colour_reply(struct input_ctx *ictx, u_int n, int c)
2465 {
2466     u_char	 r, g, b;
2467     const char	*end;
2468 
2469     if (c == 8 || (~c & COLOUR_FLAG_RGB))
2470 	    return;
2471     colour_split_rgb(c, &r, &g, &b);
2472 
2473     if (ictx->input_end == INPUT_END_BEL)
2474 	    end = "\007";
2475     else
2476 	    end = "\033\\";
2477     input_reply(ictx, "\033]%u;rgb:%02hhx/%02hhx/%02hhx%s", n, r, g, b, end);
2478 }
2479 
2480 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2481 static void
2482 input_osc_4(struct input_ctx *ictx, const char *p)
2483 {
2484 	struct window_pane	*wp = ictx->wp;
2485 	char			*copy, *s, *next = NULL;
2486 	long			 idx;
2487 	u_int			 r, g, b;
2488 
2489 	if (wp == NULL)
2490 		return;
2491 
2492 	copy = s = xstrdup(p);
2493 	while (s != NULL && *s != '\0') {
2494 		idx = strtol(s, &next, 10);
2495 		if (*next++ != ';')
2496 			goto bad;
2497 		if (idx < 0 || idx >= 0x100)
2498 			goto bad;
2499 
2500 		s = strsep(&next, ";");
2501 		if (!input_osc_parse_colour(s, &r, &g, &b)) {
2502 			s = next;
2503 			continue;
2504 		}
2505 
2506 		window_pane_set_palette(wp, idx, colour_join_rgb(r, g, b));
2507 		s = next;
2508 	}
2509 
2510 	free(copy);
2511 	return;
2512 
2513 bad:
2514 	log_debug("bad OSC 4: %s", p);
2515 	free(copy);
2516 }
2517 
2518 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2519 static void
2520 input_osc_10(struct input_ctx *ictx, const char *p)
2521 {
2522 	struct window_pane	*wp = ictx->wp;
2523 	struct grid_cell	 defaults;
2524 	u_int			 r, g, b;
2525 
2526 	if (wp == NULL)
2527 		return;
2528 
2529 	if (strcmp(p, "?") == 0) {
2530 		tty_default_colours(&defaults, wp);
2531 		input_osc_colour_reply(ictx, 10, defaults.fg);
2532 		return;
2533 	}
2534 
2535 	if (!input_osc_parse_colour(p, &r, &g, &b))
2536 		goto bad;
2537 	wp->fg = colour_join_rgb(r, g, b);
2538 	wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
2539 
2540 	return;
2541 
2542 bad:
2543 	log_debug("bad OSC 10: %s", p);
2544 }
2545 
2546 /* Handle the OSC 11 sequence for setting and querying background colour. */
2547 static void
2548 input_osc_11(struct input_ctx *ictx, const char *p)
2549 {
2550 	struct window_pane	*wp = ictx->wp;
2551 	struct grid_cell	 defaults;
2552 	u_int			 r, g, b;
2553 
2554 	if (wp == NULL)
2555 		return;
2556 
2557 	if (strcmp(p, "?") == 0) {
2558 		tty_default_colours(&defaults, wp);
2559 		input_osc_colour_reply(ictx, 11, defaults.bg);
2560 		return;
2561 	}
2562 
2563 	if (!input_osc_parse_colour(p, &r, &g, &b))
2564 	    goto bad;
2565 	wp->bg = colour_join_rgb(r, g, b);
2566 	wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
2567 
2568 	return;
2569 
2570 bad:
2571 	log_debug("bad OSC 11: %s", p);
2572 }
2573 
2574 /* Handle the OSC 52 sequence for setting the clipboard. */
2575 static void
2576 input_osc_52(struct input_ctx *ictx, const char *p)
2577 {
2578 	struct window_pane	*wp = ictx->wp;
2579 	char			*end;
2580 	const char		*buf;
2581 	size_t			 len;
2582 	u_char			*out;
2583 	int			 outlen, state;
2584 	struct screen_write_ctx	 ctx;
2585 	struct paste_buffer	*pb;
2586 
2587 	if (wp == NULL)
2588 		return;
2589 	state = options_get_number(global_options, "set-clipboard");
2590 	if (state != 2)
2591 		return;
2592 
2593 	if ((end = strchr(p, ';')) == NULL)
2594 		return;
2595 	end++;
2596 	if (*end == '\0')
2597 		return;
2598 	log_debug("%s: %s", __func__, end);
2599 
2600 	if (strcmp(end, "?") == 0) {
2601 		if ((pb = paste_get_top(NULL)) != NULL) {
2602 			buf = paste_buffer_data(pb, &len);
2603 			outlen = 4 * ((len + 2) / 3) + 1;
2604 			out = xmalloc(outlen);
2605 			if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
2606 				free(out);
2607 				return;
2608 			}
2609 		} else {
2610 			outlen = 0;
2611 			out = NULL;
2612 		}
2613 		bufferevent_write(ictx->event, "\033]52;;", 6);
2614 		if (outlen != 0)
2615 			bufferevent_write(ictx->event, out, outlen);
2616 		if (ictx->input_end == INPUT_END_BEL)
2617 			bufferevent_write(ictx->event, "\007", 1);
2618 		else
2619 			bufferevent_write(ictx->event, "\033\\", 2);
2620 		free(out);
2621 		return;
2622 	}
2623 
2624 	len = (strlen(end) / 4) * 3;
2625 	if (len == 0)
2626 		return;
2627 
2628 	out = xmalloc(len);
2629 	if ((outlen = b64_pton(end, out, len)) == -1) {
2630 		free(out);
2631 		return;
2632 	}
2633 
2634 	screen_write_start_pane(&ctx, wp, NULL);
2635 	screen_write_setselection(&ctx, out, outlen);
2636 	screen_write_stop(&ctx);
2637 	notify_pane("pane-set-clipboard", wp);
2638 
2639 	paste_add(NULL, out, outlen);
2640 }
2641 
2642 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2643 static void
2644 input_osc_104(struct input_ctx *ictx, const char *p)
2645 {
2646 	struct window_pane	*wp = ictx->wp;
2647 	char			*copy, *s;
2648 	long			 idx;
2649 
2650 	if (wp == NULL)
2651 		return;
2652 
2653 	if (*p == '\0') {
2654 		window_pane_reset_palette(wp);
2655 		return;
2656 	}
2657 
2658 	copy = s = xstrdup(p);
2659 	while (*s != '\0') {
2660 		idx = strtol(s, &s, 10);
2661 		if (*s != '\0' && *s != ';')
2662 			goto bad;
2663 		if (idx < 0 || idx >= 0x100)
2664 			goto bad;
2665 
2666 		window_pane_unset_palette(wp, idx);
2667 		if (*s == ';')
2668 			s++;
2669 	}
2670 	free(copy);
2671 	return;
2672 
2673 bad:
2674 	log_debug("bad OSC 104: %s", p);
2675 	free(copy);
2676 }
2677