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