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