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