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