xref: /openbsd-src/usr.bin/tmux/input.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /* $OpenBSD: input.c,v 1.157 2019/06/20 13:40:22 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 evbuffer		*evb = wp->event->input;
878 
879 	input_parse_buffer(wp, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb));
880 	evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
881 }
882 
883 /* Parse given input. */
884 void
885 input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
886 {
887 	struct input_ctx		*ictx = wp->ictx;
888 	struct screen_write_ctx		*sctx = &ictx->ctx;
889 	const struct input_transition	*itr;
890 	size_t				 off = 0;
891 
892 	if (len == 0)
893 		return;
894 
895 	window_update_activity(wp->window);
896 	wp->flags |= PANE_CHANGED;
897 	notify_input(wp, buf, len);
898 
899 	/*
900 	 * Open the screen. Use NULL wp if there is a mode set as don't want to
901 	 * update the tty.
902 	 */
903 	if (TAILQ_EMPTY(&wp->modes))
904 		screen_write_start(sctx, wp, &wp->base);
905 	else
906 		screen_write_start(sctx, NULL, &wp->base);
907 	ictx->wp = wp;
908 
909 	log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
910 	    ictx->state->name, len, (int)len, buf);
911 
912 	/* Parse the input. */
913 	while (off < len) {
914 		ictx->ch = buf[off++];
915 
916 		/* Find the transition. */
917 		itr = ictx->state->transitions;
918 		while (itr->first != -1 && itr->last != -1) {
919 			if (ictx->ch >= itr->first && ictx->ch <= itr->last)
920 				break;
921 			itr++;
922 		}
923 		if (itr->first == -1 || itr->last == -1) {
924 			/* No transition? Eh? */
925 			fatalx("no transition from state");
926 		}
927 
928 		/*
929 		 * Any state except print stops the current collection. This is
930 		 * an optimization to avoid checking if the attributes have
931 		 * changed for every character. It will stop unnecessarily for
932 		 * sequences that don't make a terminal change, but they should
933 		 * be the minority.
934 		 */
935 		if (itr->handler != input_print)
936 			screen_write_collect_end(sctx);
937 
938 		/*
939 		 * Execute the handler, if any. Don't switch state if it
940 		 * returns non-zero.
941 		 */
942 		if (itr->handler != NULL && itr->handler(ictx) != 0)
943 			continue;
944 
945 		/* And switch state, if necessary. */
946 		if (itr->state != NULL)
947 			input_set_state(wp, itr);
948 
949 		/* If not in ground state, save input. */
950 		if (ictx->state != &input_state_ground)
951 			evbuffer_add(ictx->since_ground, &ictx->ch, 1);
952 	}
953 
954 	/* Close the screen. */
955 	screen_write_stop(sctx);
956 }
957 
958 /* Split the parameter list (if any). */
959 static int
960 input_split(struct input_ctx *ictx)
961 {
962 	const char		*errstr;
963 	char			*ptr, *out;
964 	struct input_param	*ip;
965 	u_int			 i;
966 
967 	for (i = 0; i < ictx->param_list_len; i++) {
968 		if (ictx->param_list[i].type == INPUT_STRING)
969 			free(ictx->param_list[i].str);
970 	}
971 	ictx->param_list_len = 0;
972 
973 	if (ictx->param_len == 0)
974 		return (0);
975 	ip = &ictx->param_list[0];
976 
977 	ptr = ictx->param_buf;
978 	while ((out = strsep(&ptr, ";")) != NULL) {
979 		if (*out == '\0')
980 			ip->type = INPUT_MISSING;
981 		else {
982 			if (strchr(out, ':') != NULL) {
983 				ip->type = INPUT_STRING;
984 				ip->str = xstrdup(out);
985 			} else {
986 				ip->type = INPUT_NUMBER;
987 				ip->num = strtonum(out, 0, INT_MAX, &errstr);
988 				if (errstr != NULL)
989 					return (-1);
990 			}
991 		}
992 		ip = &ictx->param_list[++ictx->param_list_len];
993 		if (ictx->param_list_len == nitems(ictx->param_list))
994 			return (-1);
995 	}
996 
997 	for (i = 0; i < ictx->param_list_len; i++) {
998 		ip = &ictx->param_list[i];
999 		if (ip->type == INPUT_MISSING)
1000 			log_debug("parameter %u: missing", i);
1001 		else if (ip->type == INPUT_STRING)
1002 			log_debug("parameter %u: string %s", i, ip->str);
1003 		else if (ip->type == INPUT_NUMBER)
1004 			log_debug("parameter %u: number %d", i, ip->num);
1005 	}
1006 
1007 	return (0);
1008 }
1009 
1010 /* Get an argument or return default value. */
1011 static int
1012 input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1013 {
1014 	struct input_param	*ip;
1015 	int			 retval;
1016 
1017 	if (validx >= ictx->param_list_len)
1018 	    return (defval);
1019 	ip = &ictx->param_list[validx];
1020 	if (ip->type == INPUT_MISSING)
1021 		return (defval);
1022 	if (ip->type == INPUT_STRING)
1023 		return (-1);
1024 	retval = ip->num;
1025 	if (retval < minval)
1026 		return (minval);
1027 	return (retval);
1028 }
1029 
1030 /* Reply to terminal query. */
1031 static void
1032 input_reply(struct input_ctx *ictx, const char *fmt, ...)
1033 {
1034 	va_list	 ap;
1035 	char	*reply;
1036 
1037 	va_start(ap, fmt);
1038 	xvasprintf(&reply, fmt, ap);
1039 	va_end(ap);
1040 
1041 	bufferevent_write(ictx->wp->event, reply, strlen(reply));
1042 	free(reply);
1043 }
1044 
1045 /* Clear saved state. */
1046 static void
1047 input_clear(struct input_ctx *ictx)
1048 {
1049 	event_del(&ictx->timer);
1050 
1051 	*ictx->interm_buf = '\0';
1052 	ictx->interm_len = 0;
1053 
1054 	*ictx->param_buf = '\0';
1055 	ictx->param_len = 0;
1056 
1057 	*ictx->input_buf = '\0';
1058 	ictx->input_len = 0;
1059 
1060 	ictx->input_end = INPUT_END_ST;
1061 
1062 	ictx->flags &= ~INPUT_DISCARD;
1063 }
1064 
1065 /* Reset for ground state. */
1066 static void
1067 input_ground(struct input_ctx *ictx)
1068 {
1069 	event_del(&ictx->timer);
1070 	evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
1071 
1072 	if (ictx->input_space > INPUT_BUF_START) {
1073 		ictx->input_space = INPUT_BUF_START;
1074 		ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1075 	}
1076 }
1077 
1078 /* Output this character to the screen. */
1079 static int
1080 input_print(struct input_ctx *ictx)
1081 {
1082 	struct screen_write_ctx	*sctx = &ictx->ctx;
1083 	int			 set;
1084 
1085 	ictx->utf8started = 0; /* can't be valid UTF-8 */
1086 
1087 	set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1088 	if (set == 1)
1089 		ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1090 	else
1091 		ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1092 
1093 	utf8_set(&ictx->cell.cell.data, ictx->ch);
1094 	screen_write_collect_add(sctx, &ictx->cell.cell);
1095 	ictx->last = ictx->ch;
1096 
1097 	ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1098 
1099 	return (0);
1100 }
1101 
1102 /* Collect intermediate string. */
1103 static int
1104 input_intermediate(struct input_ctx *ictx)
1105 {
1106 	if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
1107 		ictx->flags |= INPUT_DISCARD;
1108 	else {
1109 		ictx->interm_buf[ictx->interm_len++] = ictx->ch;
1110 		ictx->interm_buf[ictx->interm_len] = '\0';
1111 	}
1112 
1113 	return (0);
1114 }
1115 
1116 /* Collect parameter string. */
1117 static int
1118 input_parameter(struct input_ctx *ictx)
1119 {
1120 	if (ictx->param_len == (sizeof ictx->param_buf) - 1)
1121 		ictx->flags |= INPUT_DISCARD;
1122 	else {
1123 		ictx->param_buf[ictx->param_len++] = ictx->ch;
1124 		ictx->param_buf[ictx->param_len] = '\0';
1125 	}
1126 
1127 	return (0);
1128 }
1129 
1130 /* Collect input string. */
1131 static int
1132 input_input(struct input_ctx *ictx)
1133 {
1134 	size_t available;
1135 
1136 	available = ictx->input_space;
1137 	while (ictx->input_len + 1 >= available) {
1138 		available *= 2;
1139 		if (available > INPUT_BUF_LIMIT) {
1140 			ictx->flags |= INPUT_DISCARD;
1141 			return (0);
1142 		}
1143 		ictx->input_buf = xrealloc(ictx->input_buf, available);
1144 		ictx->input_space = available;
1145 	}
1146 	ictx->input_buf[ictx->input_len++] = ictx->ch;
1147 	ictx->input_buf[ictx->input_len] = '\0';
1148 
1149 	return (0);
1150 }
1151 
1152 /* Execute C0 control sequence. */
1153 static int
1154 input_c0_dispatch(struct input_ctx *ictx)
1155 {
1156 	struct screen_write_ctx	*sctx = &ictx->ctx;
1157 	struct window_pane	*wp = ictx->wp;
1158 	struct screen		*s = sctx->s;
1159 
1160 	ictx->utf8started = 0; /* can't be valid UTF-8 */
1161 
1162 	log_debug("%s: '%c'", __func__, ictx->ch);
1163 
1164 	switch (ictx->ch) {
1165 	case '\000':	/* NUL */
1166 		break;
1167 	case '\007':	/* BEL */
1168 		alerts_queue(wp->window, WINDOW_BELL);
1169 		break;
1170 	case '\010':	/* BS */
1171 		screen_write_backspace(sctx);
1172 		break;
1173 	case '\011':	/* HT */
1174 		/* Don't tab beyond the end of the line. */
1175 		if (s->cx >= screen_size_x(s) - 1)
1176 			break;
1177 
1178 		/* Find the next tab point, or use the last column if none. */
1179 		do {
1180 			s->cx++;
1181 			if (bit_test(s->tabs, s->cx))
1182 				break;
1183 		} while (s->cx < screen_size_x(s) - 1);
1184 		break;
1185 	case '\012':	/* LF */
1186 	case '\013':	/* VT */
1187 	case '\014':	/* FF */
1188 		screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1189 		if (s->mode & MODE_CRLF)
1190 			screen_write_carriagereturn(sctx);
1191 		break;
1192 	case '\015':	/* CR */
1193 		screen_write_carriagereturn(sctx);
1194 		break;
1195 	case '\016':	/* SO */
1196 		ictx->cell.set = 1;
1197 		break;
1198 	case '\017':	/* SI */
1199 		ictx->cell.set = 0;
1200 		break;
1201 	default:
1202 		log_debug("%s: unknown '%c'", __func__, ictx->ch);
1203 		break;
1204 	}
1205 
1206 	ictx->last = -1;
1207 	return (0);
1208 }
1209 
1210 /* Execute escape sequence. */
1211 static int
1212 input_esc_dispatch(struct input_ctx *ictx)
1213 {
1214 	struct screen_write_ctx		*sctx = &ictx->ctx;
1215 	struct screen			*s = sctx->s;
1216 	struct input_table_entry	*entry;
1217 
1218 	if (ictx->flags & INPUT_DISCARD)
1219 		return (0);
1220 	log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1221 
1222 	entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
1223 	    sizeof input_esc_table[0], input_table_compare);
1224 	if (entry == NULL) {
1225 		log_debug("%s: unknown '%c'", __func__, ictx->ch);
1226 		return (0);
1227 	}
1228 
1229 	switch (entry->type) {
1230 	case INPUT_ESC_RIS:
1231 		window_pane_reset_palette(ictx->wp);
1232 		input_reset_cell(ictx);
1233 		screen_write_reset(sctx);
1234 		break;
1235 	case INPUT_ESC_IND:
1236 		screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1237 		break;
1238 	case INPUT_ESC_NEL:
1239 		screen_write_carriagereturn(sctx);
1240 		screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1241 		break;
1242 	case INPUT_ESC_HTS:
1243 		if (s->cx < screen_size_x(s))
1244 			bit_set(s->tabs, s->cx);
1245 		break;
1246 	case INPUT_ESC_RI:
1247 		screen_write_reverseindex(sctx, ictx->cell.cell.bg);
1248 		break;
1249 	case INPUT_ESC_DECKPAM:
1250 		screen_write_mode_set(sctx, MODE_KKEYPAD);
1251 		break;
1252 	case INPUT_ESC_DECKPNM:
1253 		screen_write_mode_clear(sctx, MODE_KKEYPAD);
1254 		break;
1255 	case INPUT_ESC_DECSC:
1256 		input_save_state(ictx);
1257 		break;
1258 	case INPUT_ESC_DECRC:
1259 		input_restore_state(ictx);
1260 		break;
1261 	case INPUT_ESC_DECALN:
1262 		screen_write_alignmenttest(sctx);
1263 		break;
1264 	case INPUT_ESC_SCSG0_ON:
1265 		ictx->cell.g0set = 1;
1266 		break;
1267 	case INPUT_ESC_SCSG0_OFF:
1268 		ictx->cell.g0set = 0;
1269 		break;
1270 	case INPUT_ESC_SCSG1_ON:
1271 		ictx->cell.g1set = 1;
1272 		break;
1273 	case INPUT_ESC_SCSG1_OFF:
1274 		ictx->cell.g1set = 0;
1275 		break;
1276 	case INPUT_ESC_ST:
1277 		/* ST terminates OSC but the state transition already did it. */
1278 		break;
1279 	}
1280 
1281 	ictx->last = -1;
1282 	return (0);
1283 }
1284 
1285 /* Execute control sequence. */
1286 static int
1287 input_csi_dispatch(struct input_ctx *ictx)
1288 {
1289 	struct screen_write_ctx	       *sctx = &ictx->ctx;
1290 	struct screen		       *s = sctx->s;
1291 	struct input_table_entry       *entry;
1292 	int				i, n, m;
1293 	u_int				cx, bg = ictx->cell.cell.bg;
1294 
1295 	if (ictx->flags & INPUT_DISCARD)
1296 		return (0);
1297 
1298 	log_debug("%s: '%c' \"%s\" \"%s\"",
1299 	    __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
1300 
1301 	if (input_split(ictx) != 0)
1302 		return (0);
1303 
1304 	entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
1305 	    sizeof input_csi_table[0], input_table_compare);
1306 	if (entry == NULL) {
1307 		log_debug("%s: unknown '%c'", __func__, ictx->ch);
1308 		return (0);
1309 	}
1310 
1311 	switch (entry->type) {
1312 	case INPUT_CSI_CBT:
1313 		/* Find the previous tab point, n times. */
1314 		cx = s->cx;
1315 		if (cx > screen_size_x(s) - 1)
1316 			cx = screen_size_x(s) - 1;
1317 		n = input_get(ictx, 0, 1, 1);
1318 		if (n == -1)
1319 			break;
1320 		while (cx > 0 && n-- > 0) {
1321 			do
1322 				cx--;
1323 			while (cx > 0 && !bit_test(s->tabs, cx));
1324 		}
1325 		s->cx = cx;
1326 		break;
1327 	case INPUT_CSI_CUB:
1328 		n = input_get(ictx, 0, 1, 1);
1329 		if (n != -1)
1330 			screen_write_cursorleft(sctx, n);
1331 		break;
1332 	case INPUT_CSI_CUD:
1333 		n = input_get(ictx, 0, 1, 1);
1334 		if (n != -1)
1335 			screen_write_cursordown(sctx, n);
1336 		break;
1337 	case INPUT_CSI_CUF:
1338 		n = input_get(ictx, 0, 1, 1);
1339 		if (n != -1)
1340 			screen_write_cursorright(sctx, n);
1341 		break;
1342 	case INPUT_CSI_CUP:
1343 		n = input_get(ictx, 0, 1, 1);
1344 		m = input_get(ictx, 1, 1, 1);
1345 		if (n != -1 && m != -1)
1346 			screen_write_cursormove(sctx, m - 1, n - 1, 1);
1347 		break;
1348 	case INPUT_CSI_WINOPS:
1349 		input_csi_dispatch_winops(ictx);
1350 		break;
1351 	case INPUT_CSI_CUU:
1352 		n = input_get(ictx, 0, 1, 1);
1353 		if (n != -1)
1354 			screen_write_cursorup(sctx, n);
1355 		break;
1356 	case INPUT_CSI_CNL:
1357 		n = input_get(ictx, 0, 1, 1);
1358 		if (n != -1) {
1359 			screen_write_carriagereturn(sctx);
1360 			screen_write_cursordown(sctx, n);
1361 		}
1362 		break;
1363 	case INPUT_CSI_CPL:
1364 		n = input_get(ictx, 0, 1, 1);
1365 		if (n != -1) {
1366 			screen_write_carriagereturn(sctx);
1367 			screen_write_cursorup(sctx, n);
1368 		}
1369 		break;
1370 	case INPUT_CSI_DA:
1371 		switch (input_get(ictx, 0, 0, 0)) {
1372 		case -1:
1373 			break;
1374 		case 0:
1375 			input_reply(ictx, "\033[?1;2c");
1376 			break;
1377 		default:
1378 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1379 			break;
1380 		}
1381 		break;
1382 	case INPUT_CSI_DA_TWO:
1383 		switch (input_get(ictx, 0, 0, 0)) {
1384 		case -1:
1385 			break;
1386 		case 0:
1387 			input_reply(ictx, "\033[>84;0;0c");
1388 			break;
1389 		default:
1390 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1391 			break;
1392 		}
1393 		break;
1394 	case INPUT_CSI_ECH:
1395 		n = input_get(ictx, 0, 1, 1);
1396 		if (n != -1)
1397 			screen_write_clearcharacter(sctx, n, bg);
1398 		break;
1399 	case INPUT_CSI_DCH:
1400 		n = input_get(ictx, 0, 1, 1);
1401 		if (n != -1)
1402 			screen_write_deletecharacter(sctx, n, bg);
1403 		break;
1404 	case INPUT_CSI_DECSTBM:
1405 		n = input_get(ictx, 0, 1, 1);
1406 		m = input_get(ictx, 1, 1, screen_size_y(s));
1407 		if (n != -1 && m != -1)
1408 			screen_write_scrollregion(sctx, n - 1, m - 1);
1409 		break;
1410 	case INPUT_CSI_DL:
1411 		n = input_get(ictx, 0, 1, 1);
1412 		if (n != -1)
1413 			screen_write_deleteline(sctx, n, bg);
1414 		break;
1415 	case INPUT_CSI_DSR:
1416 		switch (input_get(ictx, 0, 0, 0)) {
1417 		case -1:
1418 			break;
1419 		case 5:
1420 			input_reply(ictx, "\033[0n");
1421 			break;
1422 		case 6:
1423 			input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1424 			break;
1425 		default:
1426 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1427 			break;
1428 		}
1429 		break;
1430 	case INPUT_CSI_ED:
1431 		switch (input_get(ictx, 0, 0, 0)) {
1432 		case -1:
1433 			break;
1434 		case 0:
1435 			screen_write_clearendofscreen(sctx, bg);
1436 			break;
1437 		case 1:
1438 			screen_write_clearstartofscreen(sctx, bg);
1439 			break;
1440 		case 2:
1441 			screen_write_clearscreen(sctx, bg);
1442 			break;
1443 		case 3:
1444 			if (input_get(ictx, 1, 0, 0) == 0) {
1445 				/*
1446 				 * Linux console extension to clear history
1447 				 * (for example before locking the screen).
1448 				 */
1449 				screen_write_clearhistory(sctx);
1450 			}
1451 			break;
1452 		default:
1453 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1454 			break;
1455 		}
1456 		break;
1457 	case INPUT_CSI_EL:
1458 		switch (input_get(ictx, 0, 0, 0)) {
1459 		case -1:
1460 			break;
1461 		case 0:
1462 			screen_write_clearendofline(sctx, bg);
1463 			break;
1464 		case 1:
1465 			screen_write_clearstartofline(sctx, bg);
1466 			break;
1467 		case 2:
1468 			screen_write_clearline(sctx, bg);
1469 			break;
1470 		default:
1471 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1472 			break;
1473 		}
1474 		break;
1475 	case INPUT_CSI_HPA:
1476 		n = input_get(ictx, 0, 1, 1);
1477 		if (n != -1)
1478 			screen_write_cursormove(sctx, n - 1, -1, 1);
1479 		break;
1480 	case INPUT_CSI_ICH:
1481 		n = input_get(ictx, 0, 1, 1);
1482 		if (n != -1)
1483 			screen_write_insertcharacter(sctx, n, bg);
1484 		break;
1485 	case INPUT_CSI_IL:
1486 		n = input_get(ictx, 0, 1, 1);
1487 		if (n != -1)
1488 			screen_write_insertline(sctx, n, bg);
1489 		break;
1490 	case INPUT_CSI_REP:
1491 		n = input_get(ictx, 0, 1, 1);
1492 		if (n == -1)
1493 			break;
1494 
1495 		if (ictx->last == -1)
1496 			break;
1497 		ictx->ch = ictx->last;
1498 
1499 		for (i = 0; i < n; i++)
1500 			input_print(ictx);
1501 		break;
1502 	case INPUT_CSI_RCP:
1503 		input_restore_state(ictx);
1504 		break;
1505 	case INPUT_CSI_RM:
1506 		input_csi_dispatch_rm(ictx);
1507 		break;
1508 	case INPUT_CSI_RM_PRIVATE:
1509 		input_csi_dispatch_rm_private(ictx);
1510 		break;
1511 	case INPUT_CSI_SCP:
1512 		input_save_state(ictx);
1513 		break;
1514 	case INPUT_CSI_SGR:
1515 		input_csi_dispatch_sgr(ictx);
1516 		break;
1517 	case INPUT_CSI_SM:
1518 		input_csi_dispatch_sm(ictx);
1519 		break;
1520 	case INPUT_CSI_SM_PRIVATE:
1521 		input_csi_dispatch_sm_private(ictx);
1522 		break;
1523 	case INPUT_CSI_SU:
1524 		n = input_get(ictx, 0, 1, 1);
1525 		if (n != -1)
1526 			screen_write_scrollup(sctx, n, bg);
1527 		break;
1528 	case INPUT_CSI_TBC:
1529 		switch (input_get(ictx, 0, 0, 0)) {
1530 		case -1:
1531 			break;
1532 		case 0:
1533 			if (s->cx < screen_size_x(s))
1534 				bit_clear(s->tabs, s->cx);
1535 			break;
1536 		case 3:
1537 			bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1538 			break;
1539 		default:
1540 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1541 			break;
1542 		}
1543 		break;
1544 	case INPUT_CSI_VPA:
1545 		n = input_get(ictx, 0, 1, 1);
1546 		if (n != -1)
1547 			screen_write_cursormove(sctx, -1, n - 1, 1);
1548 		break;
1549 	case INPUT_CSI_DECSCUSR:
1550 		n = input_get(ictx, 0, 0, 0);
1551 		if (n != -1)
1552 			screen_set_cursor_style(s, n);
1553 		break;
1554 	}
1555 
1556 	ictx->last = -1;
1557 	return (0);
1558 }
1559 
1560 /* Handle CSI RM. */
1561 static void
1562 input_csi_dispatch_rm(struct input_ctx *ictx)
1563 {
1564 	struct screen_write_ctx	*sctx = &ictx->ctx;
1565 	u_int			 i;
1566 
1567 	for (i = 0; i < ictx->param_list_len; i++) {
1568 		switch (input_get(ictx, i, 0, -1)) {
1569 		case -1:
1570 			break;
1571 		case 4:		/* IRM */
1572 			screen_write_mode_clear(sctx, MODE_INSERT);
1573 			break;
1574 		case 34:
1575 			screen_write_mode_set(sctx, MODE_BLINKING);
1576 			break;
1577 		default:
1578 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1579 			break;
1580 		}
1581 	}
1582 }
1583 
1584 /* Handle CSI private RM. */
1585 static void
1586 input_csi_dispatch_rm_private(struct input_ctx *ictx)
1587 {
1588 	struct screen_write_ctx	*sctx = &ictx->ctx;
1589 	struct window_pane	*wp = ictx->wp;
1590 	u_int			 i;
1591 
1592 	for (i = 0; i < ictx->param_list_len; i++) {
1593 		switch (input_get(ictx, i, 0, -1)) {
1594 		case -1:
1595 			break;
1596 		case 1:		/* DECCKM */
1597 			screen_write_mode_clear(sctx, MODE_KCURSOR);
1598 			break;
1599 		case 3:		/* DECCOLM */
1600 			screen_write_cursormove(sctx, 0, 0, 1);
1601 			screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1602 			break;
1603 		case 6:		/* DECOM */
1604 			screen_write_mode_clear(sctx, MODE_ORIGIN);
1605 			screen_write_cursormove(sctx, 0, 0, 1);
1606 			break;
1607 		case 7:		/* DECAWM */
1608 			screen_write_mode_clear(sctx, MODE_WRAP);
1609 			break;
1610 		case 12:
1611 			screen_write_mode_clear(sctx, MODE_BLINKING);
1612 			break;
1613 		case 25:	/* TCEM */
1614 			screen_write_mode_clear(sctx, MODE_CURSOR);
1615 			break;
1616 		case 1000:
1617 		case 1001:
1618 		case 1002:
1619 		case 1003:
1620 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1621 			break;
1622 		case 1004:
1623 			screen_write_mode_clear(sctx, MODE_FOCUSON);
1624 			break;
1625 		case 1005:
1626 			screen_write_mode_clear(sctx, MODE_MOUSE_UTF8);
1627 			break;
1628 		case 1006:
1629 			screen_write_mode_clear(sctx, MODE_MOUSE_SGR);
1630 			break;
1631 		case 47:
1632 		case 1047:
1633 			window_pane_alternate_off(wp, &ictx->cell.cell, 0);
1634 			break;
1635 		case 1049:
1636 			window_pane_alternate_off(wp, &ictx->cell.cell, 1);
1637 			break;
1638 		case 2004:
1639 			screen_write_mode_clear(sctx, MODE_BRACKETPASTE);
1640 			break;
1641 		default:
1642 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1643 			break;
1644 		}
1645 	}
1646 }
1647 
1648 /* Handle CSI SM. */
1649 static void
1650 input_csi_dispatch_sm(struct input_ctx *ictx)
1651 {
1652 	struct screen_write_ctx	*sctx = &ictx->ctx;
1653 	u_int			 i;
1654 
1655 	for (i = 0; i < ictx->param_list_len; i++) {
1656 		switch (input_get(ictx, i, 0, -1)) {
1657 		case -1:
1658 			break;
1659 		case 4:		/* IRM */
1660 			screen_write_mode_set(sctx, MODE_INSERT);
1661 			break;
1662 		case 34:
1663 			screen_write_mode_clear(sctx, MODE_BLINKING);
1664 			break;
1665 		default:
1666 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1667 			break;
1668 		}
1669 	}
1670 }
1671 
1672 /* Handle CSI private SM. */
1673 static void
1674 input_csi_dispatch_sm_private(struct input_ctx *ictx)
1675 {
1676 	struct screen_write_ctx	*sctx = &ictx->ctx;
1677 	struct window_pane	*wp = ictx->wp;
1678 	u_int			 i;
1679 
1680 	for (i = 0; i < ictx->param_list_len; i++) {
1681 		switch (input_get(ictx, i, 0, -1)) {
1682 		case -1:
1683 			break;
1684 		case 1:		/* DECCKM */
1685 			screen_write_mode_set(sctx, MODE_KCURSOR);
1686 			break;
1687 		case 3:		/* DECCOLM */
1688 			screen_write_cursormove(sctx, 0, 0, 1);
1689 			screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1690 			break;
1691 		case 6:		/* DECOM */
1692 			screen_write_mode_set(sctx, MODE_ORIGIN);
1693 			screen_write_cursormove(sctx, 0, 0, 1);
1694 			break;
1695 		case 7:		/* DECAWM */
1696 			screen_write_mode_set(sctx, MODE_WRAP);
1697 			break;
1698 		case 12:
1699 			screen_write_mode_set(sctx, MODE_BLINKING);
1700 			break;
1701 		case 25:	/* TCEM */
1702 			screen_write_mode_set(sctx, MODE_CURSOR);
1703 			break;
1704 		case 1000:
1705 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1706 			screen_write_mode_set(sctx, MODE_MOUSE_STANDARD);
1707 			break;
1708 		case 1002:
1709 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1710 			screen_write_mode_set(sctx, MODE_MOUSE_BUTTON);
1711 			break;
1712 		case 1003:
1713 			screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1714 			screen_write_mode_set(sctx, MODE_MOUSE_ALL);
1715 			break;
1716 		case 1004:
1717 			if (sctx->s->mode & MODE_FOCUSON)
1718 				break;
1719 			screen_write_mode_set(sctx, MODE_FOCUSON);
1720 			wp->flags |= PANE_FOCUSPUSH; /* force update */
1721 			break;
1722 		case 1005:
1723 			screen_write_mode_set(sctx, MODE_MOUSE_UTF8);
1724 			break;
1725 		case 1006:
1726 			screen_write_mode_set(sctx, MODE_MOUSE_SGR);
1727 			break;
1728 		case 47:
1729 		case 1047:
1730 			window_pane_alternate_on(wp, &ictx->cell.cell, 0);
1731 			break;
1732 		case 1049:
1733 			window_pane_alternate_on(wp, &ictx->cell.cell, 1);
1734 			break;
1735 		case 2004:
1736 			screen_write_mode_set(sctx, MODE_BRACKETPASTE);
1737 			break;
1738 		default:
1739 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1740 			break;
1741 		}
1742 	}
1743 }
1744 
1745 /* Handle CSI window operations. */
1746 static void
1747 input_csi_dispatch_winops(struct input_ctx *ictx)
1748 {
1749 	struct screen_write_ctx	*sctx = &ictx->ctx;
1750 	struct window_pane	*wp = ictx->wp;
1751 	int			 n, m;
1752 
1753 	m = 0;
1754 	while ((n = input_get(ictx, m, 0, -1)) != -1) {
1755 		switch (n) {
1756 		case 1:
1757 		case 2:
1758 		case 5:
1759 		case 6:
1760 		case 7:
1761 		case 11:
1762 		case 13:
1763 		case 14:
1764 		case 19:
1765 		case 20:
1766 		case 21:
1767 		case 24:
1768 			break;
1769 		case 3:
1770 		case 4:
1771 		case 8:
1772 			m++;
1773 			if (input_get(ictx, m, 0, -1) == -1)
1774 				return;
1775 			/* FALLTHROUGH */
1776 		case 9:
1777 		case 10:
1778 			m++;
1779 			if (input_get(ictx, m, 0, -1) == -1)
1780 				return;
1781 			break;
1782 		case 22:
1783 			m++;
1784 			switch (input_get(ictx, m, 0, -1)) {
1785 			case -1:
1786 				return;
1787 			case 0:
1788 			case 2:
1789 				screen_push_title(sctx->s);
1790 				break;
1791 			}
1792 			break;
1793 		case 23:
1794 			m++;
1795 			switch (input_get(ictx, m, 0, -1)) {
1796 			case -1:
1797 				return;
1798 			case 0:
1799 			case 2:
1800 				screen_pop_title(sctx->s);
1801 				server_status_window(ictx->wp->window);
1802 				break;
1803 			}
1804 			break;
1805 		case 18:
1806 			input_reply(ictx, "\033[8;%u;%ut", wp->sy, wp->sx);
1807 			break;
1808 		default:
1809 			log_debug("%s: unknown '%c'", __func__, ictx->ch);
1810 			break;
1811 		}
1812 		m++;
1813 	}
1814 }
1815 
1816 /* Helper for 256 colour SGR. */
1817 static int
1818 input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
1819 {
1820 	struct grid_cell	*gc = &ictx->cell.cell;
1821 
1822 	if (c == -1 || c > 255) {
1823 		if (fgbg == 38)
1824 			gc->fg = 8;
1825 		else if (fgbg == 48)
1826 			gc->bg = 8;
1827 	} else {
1828 		if (fgbg == 38)
1829 			gc->fg = c | COLOUR_FLAG_256;
1830 		else if (fgbg == 48)
1831 			gc->bg = c | COLOUR_FLAG_256;
1832 	}
1833 	return (1);
1834 }
1835 
1836 /* Handle CSI SGR for 256 colours. */
1837 static void
1838 input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
1839 {
1840 	int	c;
1841 
1842 	c = input_get(ictx, (*i) + 1, 0, -1);
1843 	if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
1844 		(*i)++;
1845 }
1846 
1847 /* Helper for RGB colour SGR. */
1848 static int
1849 input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
1850     int b)
1851 {
1852 	struct grid_cell	*gc = &ictx->cell.cell;
1853 
1854 	if (r == -1 || r > 255)
1855 		return (0);
1856 	if (g == -1 || g > 255)
1857 		return (0);
1858 	if (b == -1 || b > 255)
1859 		return (0);
1860 
1861 	if (fgbg == 38)
1862 		gc->fg = colour_join_rgb(r, g, b);
1863 	else if (fgbg == 48)
1864 		gc->bg = colour_join_rgb(r, g, b);
1865 	return (1);
1866 }
1867 
1868 /* Handle CSI SGR for RGB colours. */
1869 static void
1870 input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
1871 {
1872 	int	r, g, b;
1873 
1874 	r = input_get(ictx, (*i) + 1, 0, -1);
1875 	g = input_get(ictx, (*i) + 2, 0, -1);
1876 	b = input_get(ictx, (*i) + 3, 0, -1);
1877 	if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
1878 		(*i) += 3;
1879 }
1880 
1881 /* Handle CSI SGR with a ISO parameter. */
1882 static void
1883 input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
1884 {
1885 	struct grid_cell	*gc = &ictx->cell.cell;
1886 	char			*s = ictx->param_list[i].str, *copy, *ptr, *out;
1887 	int			 p[8];
1888 	u_int			 n;
1889 	const char		*errstr;
1890 
1891 	for (n = 0; n < nitems(p); n++)
1892 		p[n] = -1;
1893 	n = 0;
1894 
1895 	ptr = copy = xstrdup(s);
1896 	while ((out = strsep(&ptr, ":")) != NULL) {
1897 		if (*out != '\0') {
1898 			p[n++] = strtonum(out, 0, INT_MAX, &errstr);
1899 			if (errstr != NULL || n == nitems(p)) {
1900 				free(copy);
1901 				return;
1902 			}
1903 		} else
1904 			n++;
1905 		log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
1906 	}
1907 	free(copy);
1908 
1909 	if (n == 0)
1910 		return;
1911 	if (p[0] == 4) {
1912 		if (n != 2)
1913 			return;
1914 		switch (p[1]) {
1915 		case 0:
1916 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1917 			break;
1918 		case 1:
1919 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1920 			gc->attr |= GRID_ATTR_UNDERSCORE;
1921 			break;
1922 		case 2:
1923 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1924 			gc->attr |= GRID_ATTR_UNDERSCORE_2;
1925 			break;
1926 		case 3:
1927 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1928 			gc->attr |= GRID_ATTR_UNDERSCORE_3;
1929 			break;
1930 		case 4:
1931 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1932 			gc->attr |= GRID_ATTR_UNDERSCORE_4;
1933 			break;
1934 		case 5:
1935 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
1936 			gc->attr |= GRID_ATTR_UNDERSCORE_5;
1937 			break;
1938 		}
1939 		return;
1940 	}
1941 	if (n < 2 || (p[0] != 38 && p[0] != 48))
1942 		return;
1943 	switch (p[1]) {
1944 	case 2:
1945 		if (n < 3)
1946 			break;
1947 		if (n == 5)
1948 			i = 2;
1949 		else
1950 			i = 3;
1951 		if (n < i + 3)
1952 			break;
1953 		input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
1954 		    p[i + 2]);
1955 		break;
1956 	case 5:
1957 		if (n < 3)
1958 			break;
1959 		input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
1960 		break;
1961 	}
1962 }
1963 
1964 /* Handle CSI SGR. */
1965 static void
1966 input_csi_dispatch_sgr(struct input_ctx *ictx)
1967 {
1968 	struct grid_cell	*gc = &ictx->cell.cell;
1969 	u_int			 i;
1970 	int			 n;
1971 
1972 	if (ictx->param_list_len == 0) {
1973 		memcpy(gc, &grid_default_cell, sizeof *gc);
1974 		return;
1975 	}
1976 
1977 	for (i = 0; i < ictx->param_list_len; i++) {
1978 		if (ictx->param_list[i].type == INPUT_STRING) {
1979 			input_csi_dispatch_sgr_colon(ictx, i);
1980 			continue;
1981 		}
1982 		n = input_get(ictx, i, 0, 0);
1983 		if (n == -1)
1984 			continue;
1985 
1986 		if (n == 38 || n == 48) {
1987 			i++;
1988 			switch (input_get(ictx, i, 0, -1)) {
1989 			case 2:
1990 				input_csi_dispatch_sgr_rgb(ictx, n, &i);
1991 				break;
1992 			case 5:
1993 				input_csi_dispatch_sgr_256(ictx, n, &i);
1994 				break;
1995 			}
1996 			continue;
1997 		}
1998 
1999 		switch (n) {
2000 		case 0:
2001 			memcpy(gc, &grid_default_cell, sizeof *gc);
2002 			break;
2003 		case 1:
2004 			gc->attr |= GRID_ATTR_BRIGHT;
2005 			break;
2006 		case 2:
2007 			gc->attr |= GRID_ATTR_DIM;
2008 			break;
2009 		case 3:
2010 			gc->attr |= GRID_ATTR_ITALICS;
2011 			break;
2012 		case 4:
2013 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2014 			gc->attr |= GRID_ATTR_UNDERSCORE;
2015 			break;
2016 		case 5:
2017 			gc->attr |= GRID_ATTR_BLINK;
2018 			break;
2019 		case 7:
2020 			gc->attr |= GRID_ATTR_REVERSE;
2021 			break;
2022 		case 8:
2023 			gc->attr |= GRID_ATTR_HIDDEN;
2024 			break;
2025 		case 9:
2026 			gc->attr |= GRID_ATTR_STRIKETHROUGH;
2027 			break;
2028 		case 22:
2029 			gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
2030 			break;
2031 		case 23:
2032 			gc->attr &= ~GRID_ATTR_ITALICS;
2033 			break;
2034 		case 24:
2035 			gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2036 			break;
2037 		case 25:
2038 			gc->attr &= ~GRID_ATTR_BLINK;
2039 			break;
2040 		case 27:
2041 			gc->attr &= ~GRID_ATTR_REVERSE;
2042 			break;
2043 		case 28:
2044 			gc->attr &= ~GRID_ATTR_HIDDEN;
2045 			break;
2046 		case 29:
2047 			gc->attr &= ~GRID_ATTR_STRIKETHROUGH;
2048 			break;
2049 		case 30:
2050 		case 31:
2051 		case 32:
2052 		case 33:
2053 		case 34:
2054 		case 35:
2055 		case 36:
2056 		case 37:
2057 			gc->fg = n - 30;
2058 			break;
2059 		case 39:
2060 			gc->fg = 8;
2061 			break;
2062 		case 40:
2063 		case 41:
2064 		case 42:
2065 		case 43:
2066 		case 44:
2067 		case 45:
2068 		case 46:
2069 		case 47:
2070 			gc->bg = n - 40;
2071 			break;
2072 		case 49:
2073 			gc->bg = 8;
2074 			break;
2075 		case 53:
2076 			gc->attr |= GRID_ATTR_OVERLINE;
2077 			break;
2078 		case 55:
2079 			gc->attr &= ~GRID_ATTR_OVERLINE;
2080 			break;
2081 		case 90:
2082 		case 91:
2083 		case 92:
2084 		case 93:
2085 		case 94:
2086 		case 95:
2087 		case 96:
2088 		case 97:
2089 			gc->fg = n;
2090 			break;
2091 		case 100:
2092 		case 101:
2093 		case 102:
2094 		case 103:
2095 		case 104:
2096 		case 105:
2097 		case 106:
2098 		case 107:
2099 			gc->bg = n - 10;
2100 			break;
2101 		}
2102 	}
2103 }
2104 
2105 /* End of input with BEL. */
2106 static int
2107 input_end_bel(struct input_ctx *ictx)
2108 {
2109 	log_debug("%s", __func__);
2110 
2111 	ictx->input_end = INPUT_END_BEL;
2112 
2113 	return (0);
2114 }
2115 
2116 /* DCS string started. */
2117 static void
2118 input_enter_dcs(struct input_ctx *ictx)
2119 {
2120 	log_debug("%s", __func__);
2121 
2122 	input_clear(ictx);
2123 	input_start_timer(ictx);
2124 	ictx->last = -1;
2125 }
2126 
2127 /* DCS terminator (ST) received. */
2128 static int
2129 input_dcs_dispatch(struct input_ctx *ictx)
2130 {
2131 	struct screen_write_ctx	*sctx = &ictx->ctx;
2132 	u_char			*buf = ictx->input_buf;
2133 	size_t			 len = ictx->input_len;
2134 	const char		 prefix[] = "tmux;";
2135 	const u_int		 prefixlen = (sizeof prefix) - 1;
2136 
2137 	if (ictx->flags & INPUT_DISCARD)
2138 		return (0);
2139 
2140 	log_debug("%s: \"%s\"", __func__, buf);
2141 
2142 	if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0)
2143 		screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen);
2144 
2145 	return (0);
2146 }
2147 
2148 /* OSC string started. */
2149 static void
2150 input_enter_osc(struct input_ctx *ictx)
2151 {
2152 	log_debug("%s", __func__);
2153 
2154 	input_clear(ictx);
2155 	input_start_timer(ictx);
2156 	ictx->last = -1;
2157 }
2158 
2159 /* OSC terminator (ST) received. */
2160 static void
2161 input_exit_osc(struct input_ctx *ictx)
2162 {
2163 	struct screen_write_ctx	*sctx = &ictx->ctx;
2164 	u_char			*p = ictx->input_buf;
2165 	u_int			 option;
2166 
2167 	if (ictx->flags & INPUT_DISCARD)
2168 		return;
2169 	if (ictx->input_len < 1 || *p < '0' || *p > '9')
2170 		return;
2171 
2172 	log_debug("%s: \"%s\" (end %s)", __func__, p,
2173 	    ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2174 
2175 	option = 0;
2176 	while (*p >= '0' && *p <= '9')
2177 		option = option * 10 + *p++ - '0';
2178 	if (*p == ';')
2179 		p++;
2180 
2181 	switch (option) {
2182 	case 0:
2183 	case 2:
2184 		if (utf8_isvalid(p)) {
2185 			screen_set_title(sctx->s, p);
2186 			server_status_window(ictx->wp->window);
2187 		}
2188 		break;
2189 	case 4:
2190 		input_osc_4(ictx, p);
2191 		break;
2192 	case 10:
2193 		input_osc_10(ictx, p);
2194 		break;
2195 	case 11:
2196 		input_osc_11(ictx, p);
2197 		break;
2198 	case 12:
2199 		if (utf8_isvalid(p) && *p != '?') /* ? is colour request */
2200 			screen_set_cursor_colour(sctx->s, p);
2201 		break;
2202 	case 52:
2203 		input_osc_52(ictx, p);
2204 		break;
2205 	case 104:
2206 		input_osc_104(ictx, p);
2207 		break;
2208 	case 112:
2209 		if (*p == '\0') /* no arguments allowed */
2210 			screen_set_cursor_colour(sctx->s, "");
2211 		break;
2212 	default:
2213 		log_debug("%s: unknown '%u'", __func__, option);
2214 		break;
2215 	}
2216 }
2217 
2218 /* APC string started. */
2219 static void
2220 input_enter_apc(struct input_ctx *ictx)
2221 {
2222 	log_debug("%s", __func__);
2223 
2224 	input_clear(ictx);
2225 	input_start_timer(ictx);
2226 	ictx->last = -1;
2227 }
2228 
2229 /* APC terminator (ST) received. */
2230 static void
2231 input_exit_apc(struct input_ctx *ictx)
2232 {
2233 	struct screen_write_ctx	*sctx = &ictx->ctx;
2234 
2235 	if (ictx->flags & INPUT_DISCARD)
2236 		return;
2237 	log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2238 
2239 	if (!utf8_isvalid(ictx->input_buf))
2240 		return;
2241 	screen_set_title(sctx->s, ictx->input_buf);
2242 	server_status_window(ictx->wp->window);
2243 }
2244 
2245 /* Rename string started. */
2246 static void
2247 input_enter_rename(struct input_ctx *ictx)
2248 {
2249 	log_debug("%s", __func__);
2250 
2251 	input_clear(ictx);
2252 	input_start_timer(ictx);
2253 	ictx->last = -1;
2254 }
2255 
2256 /* Rename terminator (ST) received. */
2257 static void
2258 input_exit_rename(struct input_ctx *ictx)
2259 {
2260 	if (ictx->flags & INPUT_DISCARD)
2261 		return;
2262 	if (!options_get_number(ictx->wp->options, "allow-rename"))
2263 		return;
2264 	log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2265 
2266 	if (!utf8_isvalid(ictx->input_buf))
2267 		return;
2268 	window_set_name(ictx->wp->window, ictx->input_buf);
2269 	options_set_number(ictx->wp->window->options, "automatic-rename", 0);
2270 	server_status_window(ictx->wp->window);
2271 }
2272 
2273 /* Open UTF-8 character. */
2274 static int
2275 input_top_bit_set(struct input_ctx *ictx)
2276 {
2277 	struct screen_write_ctx	*sctx = &ictx->ctx;
2278 	struct utf8_data	*ud = &ictx->utf8data;
2279 
2280 	ictx->last = -1;
2281 
2282 	if (!ictx->utf8started) {
2283 		if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2284 			return (0);
2285 		ictx->utf8started = 1;
2286 		return (0);
2287 	}
2288 
2289 	switch (utf8_append(ud, ictx->ch)) {
2290 	case UTF8_MORE:
2291 		return (0);
2292 	case UTF8_ERROR:
2293 		ictx->utf8started = 0;
2294 		return (0);
2295 	case UTF8_DONE:
2296 		break;
2297 	}
2298 	ictx->utf8started = 0;
2299 
2300 	log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2301 	    (int)ud->size, ud->data, ud->width);
2302 
2303 	utf8_copy(&ictx->cell.cell.data, ud);
2304 	screen_write_collect_add(sctx, &ictx->cell.cell);
2305 
2306 	return (0);
2307 }
2308 
2309 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2310 static void
2311 input_osc_4(struct input_ctx *ictx, const char *p)
2312 {
2313 	struct window_pane	*wp = ictx->wp;
2314 	char			*copy, *s, *next = NULL;
2315 	long	 		 idx;
2316 	u_int			 r, g, b;
2317 
2318 	copy = s = xstrdup(p);
2319 	while (s != NULL && *s != '\0') {
2320 		idx = strtol(s, &next, 10);
2321 		if (*next++ != ';')
2322 			goto bad;
2323 		if (idx < 0 || idx >= 0x100)
2324 			goto bad;
2325 
2326 		s = strsep(&next, ";");
2327 		if (sscanf(s, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3) {
2328 			s = next;
2329 			continue;
2330 		}
2331 
2332 		window_pane_set_palette(wp, idx, colour_join_rgb(r, g, b));
2333 		s = next;
2334 	}
2335 
2336 	free(copy);
2337 	return;
2338 
2339 bad:
2340 	log_debug("bad OSC 4: %s", p);
2341 	free(copy);
2342 }
2343 
2344 /* Handle the OSC 10 sequence for setting foreground colour. */
2345 static void
2346 input_osc_10(struct input_ctx *ictx, const char *p)
2347 {
2348 	struct window_pane	*wp = ictx->wp;
2349 	u_int			 r, g, b;
2350 	char			 tmp[16];
2351 
2352 	if (sscanf(p, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3)
2353 	    goto bad;
2354 	xsnprintf(tmp, sizeof tmp, "fg=#%02x%02x%02x", r, g, b);
2355 	options_set_style(wp->options, "window-style", 1, tmp);
2356 	options_set_style(wp->options, "window-active-style", 1, tmp);
2357 	wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
2358 
2359 	return;
2360 
2361 bad:
2362 	log_debug("bad OSC 10: %s", p);
2363 }
2364 
2365 /* Handle the OSC 11 sequence for setting background colour. */
2366 static void
2367 input_osc_11(struct input_ctx *ictx, const char *p)
2368 {
2369 	struct window_pane	*wp = ictx->wp;
2370 	u_int			 r, g, b;
2371 	char			 tmp[16];
2372 
2373 	if (sscanf(p, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3)
2374 	    goto bad;
2375 	xsnprintf(tmp, sizeof tmp, "bg=#%02x%02x%02x", r, g, b);
2376 	options_set_style(wp->options, "window-style", 1, tmp);
2377 	options_set_style(wp->options, "window-active-style", 1, tmp);
2378 	wp->flags |= (PANE_REDRAW|PANE_STYLECHANGED);
2379 
2380 	return;
2381 
2382 bad:
2383 	log_debug("bad OSC 11: %s", p);
2384 }
2385 
2386 /* Handle the OSC 52 sequence for setting the clipboard. */
2387 static void
2388 input_osc_52(struct input_ctx *ictx, const char *p)
2389 {
2390 	struct window_pane	*wp = ictx->wp;
2391 	char			*end;
2392 	const char		*buf;
2393 	size_t			 len;
2394 	u_char			*out;
2395 	int			 outlen, state;
2396 	struct screen_write_ctx	 ctx;
2397 	struct paste_buffer	*pb;
2398 
2399 	state = options_get_number(global_options, "set-clipboard");
2400 	if (state != 2)
2401 		return;
2402 
2403 	if ((end = strchr(p, ';')) == NULL)
2404 		return;
2405 	end++;
2406 	if (*end == '\0')
2407 		return;
2408 	log_debug("%s: %s", __func__, end);
2409 
2410 	if (strcmp(end, "?") == 0) {
2411 		if ((pb = paste_get_top(NULL)) != NULL) {
2412 			buf = paste_buffer_data(pb, &len);
2413 			outlen = 4 * ((len + 2) / 3) + 1;
2414 			out = xmalloc(outlen);
2415 			if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
2416 				free(out);
2417 				return;
2418 			}
2419 		} else {
2420 			outlen = 0;
2421 			out = NULL;
2422 		}
2423 		bufferevent_write(wp->event, "\033]52;;", 6);
2424 		if (outlen != 0)
2425 			bufferevent_write(wp->event, out, outlen);
2426 		if (ictx->input_end == INPUT_END_BEL)
2427 			bufferevent_write(wp->event, "\007", 1);
2428 		else
2429 			bufferevent_write(wp->event, "\033\\", 2);
2430 		free(out);
2431 		return;
2432 	}
2433 
2434 	len = (strlen(end) / 4) * 3;
2435 	if (len == 0)
2436 		return;
2437 
2438 	out = xmalloc(len);
2439 	if ((outlen = b64_pton(end, out, len)) == -1) {
2440 		free(out);
2441 		return;
2442 	}
2443 
2444 	screen_write_start(&ctx, wp, NULL);
2445 	screen_write_setselection(&ctx, out, outlen);
2446 	screen_write_stop(&ctx);
2447 	notify_pane("pane-set-clipboard", wp);
2448 
2449 	paste_add(NULL, out, outlen);
2450 }
2451 
2452 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2453 static void
2454 input_osc_104(struct input_ctx *ictx, const char *p)
2455 {
2456 	struct window_pane	*wp = ictx->wp;
2457 	char			*copy, *s;
2458 	long			 idx;
2459 
2460 	if (*p == '\0') {
2461 		window_pane_reset_palette(wp);
2462 		return;
2463 	}
2464 
2465 	copy = s = xstrdup(p);
2466 	while (*s != '\0') {
2467 		idx = strtol(s, &s, 10);
2468 		if (*s != '\0' && *s != ';')
2469 			goto bad;
2470 		if (idx < 0 || idx >= 0x100)
2471 			goto bad;
2472 
2473 		window_pane_unset_palette(wp, idx);
2474 		if (*s == ';')
2475 			s++;
2476 	}
2477 	free(copy);
2478 	return;
2479 
2480 bad:
2481 	log_debug("bad OSC 104: %s", p);
2482 	free(copy);
2483 }
2484