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