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