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