Merge branch 'obsd-master'
[tmux.git] / input.c
blobd375ea2da0cfabf2d92f9c2fd50f6272f0e2980b
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <netinet/in.h>
23 #include <ctype.h>
24 #include <resolv.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
29 #include "tmux.h"
32 * Based on the description by Paul Williams at:
34 * https://vt100.net/emu/dec_ansi_parser
36 * With the following changes:
38 * - 7-bit only.
40 * - Support for UTF-8.
42 * - OSC (but not APC) may be terminated by \007 as well as ST.
44 * - A state for APC similar to OSC. Some terminals appear to use this to set
45 * the title.
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.
50 * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
51 * be passed to the underlying terminals.
54 /* Input parser cell. */
55 struct input_cell {
56 struct grid_cell cell;
57 int set;
58 int g0set; /* 1 if ACS */
59 int g1set; /* 1 if ACS */
62 /* Input parser argument. */
63 struct input_param {
64 enum {
65 INPUT_MISSING,
66 INPUT_NUMBER,
67 INPUT_STRING
68 } type;
69 union {
70 int num;
71 char *str;
75 /* Input parser context. */
76 struct input_ctx {
77 struct window_pane *wp;
78 struct bufferevent *event;
79 struct screen_write_ctx ctx;
80 struct colour_palette *palette;
82 struct input_cell cell;
84 struct input_cell old_cell;
85 u_int old_cx;
86 u_int old_cy;
87 int old_mode;
89 u_char interm_buf[4];
90 size_t interm_len;
92 u_char param_buf[64];
93 size_t param_len;
95 #define INPUT_BUF_START 32
96 #define INPUT_BUF_LIMIT 1048576
97 u_char *input_buf;
98 size_t input_len;
99 size_t input_space;
100 enum {
101 INPUT_END_ST,
102 INPUT_END_BEL
103 } input_end;
105 struct input_param param_list[24];
106 u_int param_list_len;
108 struct utf8_data utf8data;
109 int utf8started;
111 int ch;
112 int last;
114 int flags;
115 #define INPUT_DISCARD 0x1
117 const struct input_state *state;
119 struct event timer;
122 * All input received since we were last in the ground state. Sent to
123 * control clients on connection.
125 struct evbuffer *since_ground;
128 /* Helper functions. */
129 struct input_transition;
130 static int input_split(struct input_ctx *);
131 static int input_get(struct input_ctx *, u_int, int, int);
132 static void printflike(2, 3) input_reply(struct input_ctx *, const char *, ...);
133 static void input_set_state(struct input_ctx *,
134 const struct input_transition *);
135 static void input_reset_cell(struct input_ctx *);
137 static void input_osc_4(struct input_ctx *, const char *);
138 static void input_osc_8(struct input_ctx *, const char *);
139 static void input_osc_10(struct input_ctx *, const char *);
140 static void input_osc_11(struct input_ctx *, const char *);
141 static void input_osc_12(struct input_ctx *, const char *);
142 static void input_osc_52(struct input_ctx *, const char *);
143 static void input_osc_104(struct input_ctx *, const char *);
144 static void input_osc_110(struct input_ctx *, const char *);
145 static void input_osc_111(struct input_ctx *, const char *);
146 static void input_osc_112(struct input_ctx *, const char *);
147 static void input_osc_133(struct input_ctx *, const char *);
149 /* Transition entry/exit handlers. */
150 static void input_clear(struct input_ctx *);
151 static void input_ground(struct input_ctx *);
152 static void input_enter_dcs(struct input_ctx *);
153 static void input_enter_osc(struct input_ctx *);
154 static void input_exit_osc(struct input_ctx *);
155 static void input_enter_apc(struct input_ctx *);
156 static void input_exit_apc(struct input_ctx *);
157 static void input_enter_rename(struct input_ctx *);
158 static void input_exit_rename(struct input_ctx *);
160 /* Input state handlers. */
161 static int input_print(struct input_ctx *);
162 static int input_intermediate(struct input_ctx *);
163 static int input_parameter(struct input_ctx *);
164 static int input_input(struct input_ctx *);
165 static int input_c0_dispatch(struct input_ctx *);
166 static int input_esc_dispatch(struct input_ctx *);
167 static int input_csi_dispatch(struct input_ctx *);
168 static void input_csi_dispatch_rm(struct input_ctx *);
169 static void input_csi_dispatch_rm_private(struct input_ctx *);
170 static void input_csi_dispatch_sm(struct input_ctx *);
171 static void input_csi_dispatch_sm_private(struct input_ctx *);
172 static void input_csi_dispatch_sm_graphics(struct input_ctx *);
173 static void input_csi_dispatch_winops(struct input_ctx *);
174 static void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
175 static void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
176 static void input_csi_dispatch_sgr(struct input_ctx *);
177 static int input_dcs_dispatch(struct input_ctx *);
178 static int input_top_bit_set(struct input_ctx *);
179 static int input_end_bel(struct input_ctx *);
181 /* Command table comparison function. */
182 static int input_table_compare(const void *, const void *);
184 /* Command table entry. */
185 struct input_table_entry {
186 int ch;
187 const char *interm;
188 int type;
191 /* Escape commands. */
192 enum input_esc_type {
193 INPUT_ESC_DECALN,
194 INPUT_ESC_DECKPAM,
195 INPUT_ESC_DECKPNM,
196 INPUT_ESC_DECRC,
197 INPUT_ESC_DECSC,
198 INPUT_ESC_HTS,
199 INPUT_ESC_IND,
200 INPUT_ESC_NEL,
201 INPUT_ESC_RI,
202 INPUT_ESC_RIS,
203 INPUT_ESC_SCSG0_OFF,
204 INPUT_ESC_SCSG0_ON,
205 INPUT_ESC_SCSG1_OFF,
206 INPUT_ESC_SCSG1_ON,
207 INPUT_ESC_ST
210 /* Escape command table. */
211 static const struct input_table_entry input_esc_table[] = {
212 { '0', "(", INPUT_ESC_SCSG0_ON },
213 { '0', ")", INPUT_ESC_SCSG1_ON },
214 { '7', "", INPUT_ESC_DECSC },
215 { '8', "", INPUT_ESC_DECRC },
216 { '8', "#", INPUT_ESC_DECALN },
217 { '=', "", INPUT_ESC_DECKPAM },
218 { '>', "", INPUT_ESC_DECKPNM },
219 { 'B', "(", INPUT_ESC_SCSG0_OFF },
220 { 'B', ")", INPUT_ESC_SCSG1_OFF },
221 { 'D', "", INPUT_ESC_IND },
222 { 'E', "", INPUT_ESC_NEL },
223 { 'H', "", INPUT_ESC_HTS },
224 { 'M', "", INPUT_ESC_RI },
225 { '\\', "", INPUT_ESC_ST },
226 { 'c', "", INPUT_ESC_RIS },
229 /* Control (CSI) commands. */
230 enum input_csi_type {
231 INPUT_CSI_CBT,
232 INPUT_CSI_CNL,
233 INPUT_CSI_CPL,
234 INPUT_CSI_CUB,
235 INPUT_CSI_CUD,
236 INPUT_CSI_CUF,
237 INPUT_CSI_CUP,
238 INPUT_CSI_CUU,
239 INPUT_CSI_DA,
240 INPUT_CSI_DA_TWO,
241 INPUT_CSI_DCH,
242 INPUT_CSI_DECSCUSR,
243 INPUT_CSI_DECSTBM,
244 INPUT_CSI_DL,
245 INPUT_CSI_DSR,
246 INPUT_CSI_ECH,
247 INPUT_CSI_ED,
248 INPUT_CSI_EL,
249 INPUT_CSI_HPA,
250 INPUT_CSI_ICH,
251 INPUT_CSI_IL,
252 INPUT_CSI_MODOFF,
253 INPUT_CSI_MODSET,
254 INPUT_CSI_RCP,
255 INPUT_CSI_REP,
256 INPUT_CSI_RM,
257 INPUT_CSI_RM_PRIVATE,
258 INPUT_CSI_SCP,
259 INPUT_CSI_SD,
260 INPUT_CSI_SGR,
261 INPUT_CSI_SM,
262 INPUT_CSI_SM_PRIVATE,
263 INPUT_CSI_SM_GRAPHICS,
264 INPUT_CSI_SU,
265 INPUT_CSI_TBC,
266 INPUT_CSI_VPA,
267 INPUT_CSI_WINOPS,
268 INPUT_CSI_XDA
271 /* Control (CSI) command table. */
272 static const struct input_table_entry input_csi_table[] = {
273 { '@', "", INPUT_CSI_ICH },
274 { 'A', "", INPUT_CSI_CUU },
275 { 'B', "", INPUT_CSI_CUD },
276 { 'C', "", INPUT_CSI_CUF },
277 { 'D', "", INPUT_CSI_CUB },
278 { 'E', "", INPUT_CSI_CNL },
279 { 'F', "", INPUT_CSI_CPL },
280 { 'G', "", INPUT_CSI_HPA },
281 { 'H', "", INPUT_CSI_CUP },
282 { 'J', "", INPUT_CSI_ED },
283 { 'K', "", INPUT_CSI_EL },
284 { 'L', "", INPUT_CSI_IL },
285 { 'M', "", INPUT_CSI_DL },
286 { 'P', "", INPUT_CSI_DCH },
287 { 'S', "", INPUT_CSI_SU },
288 { 'S', "?", INPUT_CSI_SM_GRAPHICS },
289 { 'T', "", INPUT_CSI_SD },
290 { 'X', "", INPUT_CSI_ECH },
291 { 'Z', "", INPUT_CSI_CBT },
292 { '`', "", INPUT_CSI_HPA },
293 { 'b', "", INPUT_CSI_REP },
294 { 'c', "", INPUT_CSI_DA },
295 { 'c', ">", INPUT_CSI_DA_TWO },
296 { 'd', "", INPUT_CSI_VPA },
297 { 'f', "", INPUT_CSI_CUP },
298 { 'g', "", INPUT_CSI_TBC },
299 { 'h', "", INPUT_CSI_SM },
300 { 'h', "?", INPUT_CSI_SM_PRIVATE },
301 { 'l', "", INPUT_CSI_RM },
302 { 'l', "?", INPUT_CSI_RM_PRIVATE },
303 { 'm', "", INPUT_CSI_SGR },
304 { 'm', ">", INPUT_CSI_MODSET },
305 { 'n', "", INPUT_CSI_DSR },
306 { 'n', ">", INPUT_CSI_MODOFF },
307 { 'q', " ", INPUT_CSI_DECSCUSR },
308 { 'q', ">", INPUT_CSI_XDA },
309 { 'r', "", INPUT_CSI_DECSTBM },
310 { 's', "", INPUT_CSI_SCP },
311 { 't', "", INPUT_CSI_WINOPS },
312 { 'u', "", INPUT_CSI_RCP }
315 /* Input transition. */
316 struct input_transition {
317 int first;
318 int last;
320 int (*handler)(struct input_ctx *);
321 const struct input_state *state;
324 /* Input state. */
325 struct input_state {
326 const char *name;
327 void (*enter)(struct input_ctx *);
328 void (*exit)(struct input_ctx *);
329 const struct input_transition *transitions;
332 /* State transitions available from all states. */
333 #define INPUT_STATE_ANYWHERE \
334 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
335 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
336 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
338 /* Forward declarations of state tables. */
339 static const struct input_transition input_state_ground_table[];
340 static const struct input_transition input_state_esc_enter_table[];
341 static const struct input_transition input_state_esc_intermediate_table[];
342 static const struct input_transition input_state_csi_enter_table[];
343 static const struct input_transition input_state_csi_parameter_table[];
344 static const struct input_transition input_state_csi_intermediate_table[];
345 static const struct input_transition input_state_csi_ignore_table[];
346 static const struct input_transition input_state_dcs_enter_table[];
347 static const struct input_transition input_state_dcs_parameter_table[];
348 static const struct input_transition input_state_dcs_intermediate_table[];
349 static const struct input_transition input_state_dcs_handler_table[];
350 static const struct input_transition input_state_dcs_escape_table[];
351 static const struct input_transition input_state_dcs_ignore_table[];
352 static const struct input_transition input_state_osc_string_table[];
353 static const struct input_transition input_state_apc_string_table[];
354 static const struct input_transition input_state_rename_string_table[];
355 static const struct input_transition input_state_consume_st_table[];
357 /* ground state definition. */
358 static const struct input_state input_state_ground = {
359 "ground",
360 input_ground, NULL,
361 input_state_ground_table
364 /* esc_enter state definition. */
365 static const struct input_state input_state_esc_enter = {
366 "esc_enter",
367 input_clear, NULL,
368 input_state_esc_enter_table
371 /* esc_intermediate state definition. */
372 static const struct input_state input_state_esc_intermediate = {
373 "esc_intermediate",
374 NULL, NULL,
375 input_state_esc_intermediate_table
378 /* csi_enter state definition. */
379 static const struct input_state input_state_csi_enter = {
380 "csi_enter",
381 input_clear, NULL,
382 input_state_csi_enter_table
385 /* csi_parameter state definition. */
386 static const struct input_state input_state_csi_parameter = {
387 "csi_parameter",
388 NULL, NULL,
389 input_state_csi_parameter_table
392 /* csi_intermediate state definition. */
393 static const struct input_state input_state_csi_intermediate = {
394 "csi_intermediate",
395 NULL, NULL,
396 input_state_csi_intermediate_table
399 /* csi_ignore state definition. */
400 static const struct input_state input_state_csi_ignore = {
401 "csi_ignore",
402 NULL, NULL,
403 input_state_csi_ignore_table
406 /* dcs_enter state definition. */
407 static const struct input_state input_state_dcs_enter = {
408 "dcs_enter",
409 input_enter_dcs, NULL,
410 input_state_dcs_enter_table
413 /* dcs_parameter state definition. */
414 static const struct input_state input_state_dcs_parameter = {
415 "dcs_parameter",
416 NULL, NULL,
417 input_state_dcs_parameter_table
420 /* dcs_intermediate state definition. */
421 static const struct input_state input_state_dcs_intermediate = {
422 "dcs_intermediate",
423 NULL, NULL,
424 input_state_dcs_intermediate_table
427 /* dcs_handler state definition. */
428 static const struct input_state input_state_dcs_handler = {
429 "dcs_handler",
430 NULL, NULL,
431 input_state_dcs_handler_table
434 /* dcs_escape state definition. */
435 static const struct input_state input_state_dcs_escape = {
436 "dcs_escape",
437 NULL, NULL,
438 input_state_dcs_escape_table
441 /* dcs_ignore state definition. */
442 static const struct input_state input_state_dcs_ignore = {
443 "dcs_ignore",
444 NULL, NULL,
445 input_state_dcs_ignore_table
448 /* osc_string state definition. */
449 static const struct input_state input_state_osc_string = {
450 "osc_string",
451 input_enter_osc, input_exit_osc,
452 input_state_osc_string_table
455 /* apc_string state definition. */
456 static const struct input_state input_state_apc_string = {
457 "apc_string",
458 input_enter_apc, input_exit_apc,
459 input_state_apc_string_table
462 /* rename_string state definition. */
463 static const struct input_state input_state_rename_string = {
464 "rename_string",
465 input_enter_rename, input_exit_rename,
466 input_state_rename_string_table
469 /* consume_st state definition. */
470 static const struct input_state input_state_consume_st = {
471 "consume_st",
472 input_enter_rename, NULL, /* rename also waits for ST */
473 input_state_consume_st_table
476 /* ground state table. */
477 static const struct input_transition input_state_ground_table[] = {
478 INPUT_STATE_ANYWHERE,
480 { 0x00, 0x17, input_c0_dispatch, NULL },
481 { 0x19, 0x19, input_c0_dispatch, NULL },
482 { 0x1c, 0x1f, input_c0_dispatch, NULL },
483 { 0x20, 0x7e, input_print, NULL },
484 { 0x7f, 0x7f, NULL, NULL },
485 { 0x80, 0xff, input_top_bit_set, NULL },
487 { -1, -1, NULL, NULL }
490 /* esc_enter state table. */
491 static const struct input_transition input_state_esc_enter_table[] = {
492 INPUT_STATE_ANYWHERE,
494 { 0x00, 0x17, input_c0_dispatch, NULL },
495 { 0x19, 0x19, input_c0_dispatch, NULL },
496 { 0x1c, 0x1f, input_c0_dispatch, NULL },
497 { 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
498 { 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
499 { 0x50, 0x50, NULL, &input_state_dcs_enter },
500 { 0x51, 0x57, input_esc_dispatch, &input_state_ground },
501 { 0x58, 0x58, NULL, &input_state_consume_st },
502 { 0x59, 0x59, input_esc_dispatch, &input_state_ground },
503 { 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
504 { 0x5b, 0x5b, NULL, &input_state_csi_enter },
505 { 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
506 { 0x5d, 0x5d, NULL, &input_state_osc_string },
507 { 0x5e, 0x5e, NULL, &input_state_consume_st },
508 { 0x5f, 0x5f, NULL, &input_state_apc_string },
509 { 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
510 { 0x6b, 0x6b, NULL, &input_state_rename_string },
511 { 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
512 { 0x7f, 0xff, NULL, NULL },
514 { -1, -1, NULL, NULL }
517 /* esc_intermediate state table. */
518 static const struct input_transition input_state_esc_intermediate_table[] = {
519 INPUT_STATE_ANYWHERE,
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, NULL },
525 { 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
526 { 0x7f, 0xff, NULL, NULL },
528 { -1, -1, NULL, NULL }
531 /* csi_enter state table. */
532 static const struct input_transition input_state_csi_enter_table[] = {
533 INPUT_STATE_ANYWHERE,
535 { 0x00, 0x17, input_c0_dispatch, NULL },
536 { 0x19, 0x19, input_c0_dispatch, NULL },
537 { 0x1c, 0x1f, input_c0_dispatch, NULL },
538 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
539 { 0x30, 0x39, input_parameter, &input_state_csi_parameter },
540 { 0x3a, 0x3a, input_parameter, &input_state_csi_parameter },
541 { 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
542 { 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
543 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
544 { 0x7f, 0xff, NULL, NULL },
546 { -1, -1, NULL, NULL }
549 /* csi_parameter state table. */
550 static const struct input_transition input_state_csi_parameter_table[] = {
551 INPUT_STATE_ANYWHERE,
553 { 0x00, 0x17, input_c0_dispatch, NULL },
554 { 0x19, 0x19, input_c0_dispatch, NULL },
555 { 0x1c, 0x1f, input_c0_dispatch, NULL },
556 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
557 { 0x30, 0x39, input_parameter, NULL },
558 { 0x3a, 0x3a, input_parameter, NULL },
559 { 0x3b, 0x3b, input_parameter, NULL },
560 { 0x3c, 0x3f, NULL, &input_state_csi_ignore },
561 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
562 { 0x7f, 0xff, NULL, NULL },
564 { -1, -1, NULL, NULL }
567 /* csi_intermediate state table. */
568 static const struct input_transition input_state_csi_intermediate_table[] = {
569 INPUT_STATE_ANYWHERE,
571 { 0x00, 0x17, input_c0_dispatch, NULL },
572 { 0x19, 0x19, input_c0_dispatch, NULL },
573 { 0x1c, 0x1f, input_c0_dispatch, NULL },
574 { 0x20, 0x2f, input_intermediate, NULL },
575 { 0x30, 0x3f, NULL, &input_state_csi_ignore },
576 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
577 { 0x7f, 0xff, NULL, NULL },
579 { -1, -1, NULL, NULL }
582 /* csi_ignore state table. */
583 static const struct input_transition input_state_csi_ignore_table[] = {
584 INPUT_STATE_ANYWHERE,
586 { 0x00, 0x17, input_c0_dispatch, NULL },
587 { 0x19, 0x19, input_c0_dispatch, NULL },
588 { 0x1c, 0x1f, input_c0_dispatch, NULL },
589 { 0x20, 0x3f, NULL, NULL },
590 { 0x40, 0x7e, NULL, &input_state_ground },
591 { 0x7f, 0xff, NULL, NULL },
593 { -1, -1, NULL, NULL }
596 /* dcs_enter state table. */
597 static const struct input_transition input_state_dcs_enter_table[] = {
598 INPUT_STATE_ANYWHERE,
600 { 0x00, 0x17, NULL, NULL },
601 { 0x19, 0x19, NULL, NULL },
602 { 0x1c, 0x1f, NULL, NULL },
603 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
604 { 0x30, 0x39, input_parameter, &input_state_dcs_parameter },
605 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
606 { 0x3b, 0x3b, input_parameter, &input_state_dcs_parameter },
607 { 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
608 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
609 { 0x7f, 0xff, NULL, NULL },
611 { -1, -1, NULL, NULL }
614 /* dcs_parameter state table. */
615 static const struct input_transition input_state_dcs_parameter_table[] = {
616 INPUT_STATE_ANYWHERE,
618 { 0x00, 0x17, NULL, NULL },
619 { 0x19, 0x19, NULL, NULL },
620 { 0x1c, 0x1f, NULL, NULL },
621 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
622 { 0x30, 0x39, input_parameter, NULL },
623 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
624 { 0x3b, 0x3b, input_parameter, NULL },
625 { 0x3c, 0x3f, NULL, &input_state_dcs_ignore },
626 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
627 { 0x7f, 0xff, NULL, NULL },
629 { -1, -1, NULL, NULL }
632 /* dcs_intermediate state table. */
633 static const struct input_transition input_state_dcs_intermediate_table[] = {
634 INPUT_STATE_ANYWHERE,
636 { 0x00, 0x17, NULL, NULL },
637 { 0x19, 0x19, NULL, NULL },
638 { 0x1c, 0x1f, NULL, NULL },
639 { 0x20, 0x2f, input_intermediate, NULL },
640 { 0x30, 0x3f, NULL, &input_state_dcs_ignore },
641 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
642 { 0x7f, 0xff, NULL, NULL },
644 { -1, -1, NULL, NULL }
647 /* dcs_handler state table. */
648 static const struct input_transition input_state_dcs_handler_table[] = {
649 /* No INPUT_STATE_ANYWHERE */
651 { 0x00, 0x1a, input_input, NULL },
652 { 0x1b, 0x1b, NULL, &input_state_dcs_escape },
653 { 0x1c, 0xff, input_input, NULL },
655 { -1, -1, NULL, NULL }
658 /* dcs_escape state table. */
659 static const struct input_transition input_state_dcs_escape_table[] = {
660 /* No INPUT_STATE_ANYWHERE */
662 { 0x00, 0x5b, input_input, &input_state_dcs_handler },
663 { 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
664 { 0x5d, 0xff, input_input, &input_state_dcs_handler },
666 { -1, -1, NULL, NULL }
669 /* dcs_ignore state table. */
670 static const struct input_transition input_state_dcs_ignore_table[] = {
671 INPUT_STATE_ANYWHERE,
673 { 0x00, 0x17, NULL, NULL },
674 { 0x19, 0x19, NULL, NULL },
675 { 0x1c, 0x1f, NULL, NULL },
676 { 0x20, 0xff, NULL, NULL },
678 { -1, -1, NULL, NULL }
681 /* osc_string state table. */
682 static const struct input_transition input_state_osc_string_table[] = {
683 INPUT_STATE_ANYWHERE,
685 { 0x00, 0x06, NULL, NULL },
686 { 0x07, 0x07, input_end_bel, &input_state_ground },
687 { 0x08, 0x17, NULL, NULL },
688 { 0x19, 0x19, NULL, NULL },
689 { 0x1c, 0x1f, NULL, NULL },
690 { 0x20, 0xff, input_input, NULL },
692 { -1, -1, NULL, NULL }
695 /* apc_string state table. */
696 static const struct input_transition input_state_apc_string_table[] = {
697 INPUT_STATE_ANYWHERE,
699 { 0x00, 0x17, NULL, NULL },
700 { 0x19, 0x19, NULL, NULL },
701 { 0x1c, 0x1f, NULL, NULL },
702 { 0x20, 0xff, input_input, NULL },
704 { -1, -1, NULL, NULL }
707 /* rename_string state table. */
708 static const struct input_transition input_state_rename_string_table[] = {
709 INPUT_STATE_ANYWHERE,
711 { 0x00, 0x17, NULL, NULL },
712 { 0x19, 0x19, NULL, NULL },
713 { 0x1c, 0x1f, NULL, NULL },
714 { 0x20, 0xff, input_input, NULL },
716 { -1, -1, NULL, NULL }
719 /* consume_st state table. */
720 static const struct input_transition input_state_consume_st_table[] = {
721 INPUT_STATE_ANYWHERE,
723 { 0x00, 0x17, NULL, NULL },
724 { 0x19, 0x19, NULL, NULL },
725 { 0x1c, 0x1f, NULL, NULL },
726 { 0x20, 0xff, NULL, NULL },
728 { -1, -1, NULL, NULL }
731 /* Input table compare. */
732 static int
733 input_table_compare(const void *key, const void *value)
735 const struct input_ctx *ictx = key;
736 const struct input_table_entry *entry = value;
738 if (ictx->ch != entry->ch)
739 return (ictx->ch - entry->ch);
740 return (strcmp(ictx->interm_buf, entry->interm));
744 * Timer - if this expires then have been waiting for a terminator for too
745 * long, so reset to ground.
747 static void
748 input_timer_callback(__unused int fd, __unused short events, void *arg)
750 struct input_ctx *ictx = arg;
752 log_debug("%s: %s expired" , __func__, ictx->state->name);
753 input_reset(ictx, 0);
756 /* Start the timer. */
757 static void
758 input_start_timer(struct input_ctx *ictx)
760 struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
762 event_del(&ictx->timer);
763 event_add(&ictx->timer, &tv);
766 /* Reset cell state to default. */
767 static void
768 input_reset_cell(struct input_ctx *ictx)
770 memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
771 ictx->cell.set = 0;
772 ictx->cell.g0set = ictx->cell.g1set = 0;
774 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
775 ictx->old_cx = 0;
776 ictx->old_cy = 0;
779 /* Save screen state. */
780 static void
781 input_save_state(struct input_ctx *ictx)
783 struct screen_write_ctx *sctx = &ictx->ctx;
784 struct screen *s = sctx->s;
786 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
787 ictx->old_cx = s->cx;
788 ictx->old_cy = s->cy;
789 ictx->old_mode = s->mode;
792 /* Restore screen state. */
793 static void
794 input_restore_state(struct input_ctx *ictx)
796 struct screen_write_ctx *sctx = &ictx->ctx;
798 memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
799 if (ictx->old_mode & MODE_ORIGIN)
800 screen_write_mode_set(sctx, MODE_ORIGIN);
801 else
802 screen_write_mode_clear(sctx, MODE_ORIGIN);
803 screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy, 0);
806 /* Initialise input parser. */
807 struct input_ctx *
808 input_init(struct window_pane *wp, struct bufferevent *bev,
809 struct colour_palette *palette)
811 struct input_ctx *ictx;
813 ictx = xcalloc(1, sizeof *ictx);
814 ictx->wp = wp;
815 ictx->event = bev;
816 ictx->palette = palette;
818 ictx->input_space = INPUT_BUF_START;
819 ictx->input_buf = xmalloc(INPUT_BUF_START);
821 ictx->since_ground = evbuffer_new();
822 if (ictx->since_ground == NULL)
823 fatalx("out of memory");
825 evtimer_set(&ictx->timer, input_timer_callback, ictx);
827 input_reset(ictx, 0);
828 return (ictx);
831 /* Destroy input parser. */
832 void
833 input_free(struct input_ctx *ictx)
835 u_int i;
837 for (i = 0; i < ictx->param_list_len; i++) {
838 if (ictx->param_list[i].type == INPUT_STRING)
839 free(ictx->param_list[i].str);
842 event_del(&ictx->timer);
844 free(ictx->input_buf);
845 evbuffer_free(ictx->since_ground);
847 free(ictx);
850 /* Reset input state and clear screen. */
851 void
852 input_reset(struct input_ctx *ictx, int clear)
854 struct screen_write_ctx *sctx = &ictx->ctx;
855 struct window_pane *wp = ictx->wp;
857 input_reset_cell(ictx);
859 if (clear && wp != NULL) {
860 if (TAILQ_EMPTY(&wp->modes))
861 screen_write_start_pane(sctx, wp, &wp->base);
862 else
863 screen_write_start(sctx, &wp->base);
864 screen_write_reset(sctx);
865 screen_write_stop(sctx);
868 input_clear(ictx);
870 ictx->last = -1;
872 ictx->state = &input_state_ground;
873 ictx->flags = 0;
876 /* Return pending data. */
877 struct evbuffer *
878 input_pending(struct input_ctx *ictx)
880 return (ictx->since_ground);
883 /* Change input state. */
884 static void
885 input_set_state(struct input_ctx *ictx, const struct input_transition *itr)
887 if (ictx->state->exit != NULL)
888 ictx->state->exit(ictx);
889 ictx->state = itr->state;
890 if (ictx->state->enter != NULL)
891 ictx->state->enter(ictx);
894 /* Parse data. */
895 static void
896 input_parse(struct input_ctx *ictx, u_char *buf, size_t len)
898 struct screen_write_ctx *sctx = &ictx->ctx;
899 const struct input_state *state = NULL;
900 const struct input_transition *itr = NULL;
901 size_t off = 0;
903 /* Parse the input. */
904 while (off < len) {
905 ictx->ch = buf[off++];
907 /* Find the transition. */
908 if (ictx->state != state ||
909 itr == NULL ||
910 ictx->ch < itr->first ||
911 ictx->ch > itr->last) {
912 itr = ictx->state->transitions;
913 while (itr->first != -1 && itr->last != -1) {
914 if (ictx->ch >= itr->first &&
915 ictx->ch <= itr->last)
916 break;
917 itr++;
919 if (itr->first == -1 || itr->last == -1) {
920 /* No transition? Eh? */
921 fatalx("no transition from state");
924 state = ictx->state;
927 * Any state except print stops the current collection. This is
928 * an optimization to avoid checking if the attributes have
929 * changed for every character. It will stop unnecessarily for
930 * sequences that don't make a terminal change, but they should
931 * be the minority.
933 if (itr->handler != input_print)
934 screen_write_collect_end(sctx);
937 * Execute the handler, if any. Don't switch state if it
938 * returns non-zero.
940 if (itr->handler != NULL && itr->handler(ictx) != 0)
941 continue;
943 /* And switch state, if necessary. */
944 if (itr->state != NULL)
945 input_set_state(ictx, itr);
947 /* If not in ground state, save input. */
948 if (ictx->state != &input_state_ground)
949 evbuffer_add(ictx->since_ground, &ictx->ch, 1);
953 /* Parse input from pane. */
954 void
955 input_parse_pane(struct window_pane *wp)
957 void *new_data;
958 size_t new_size;
960 new_data = window_pane_get_new_data(wp, &wp->offset, &new_size);
961 input_parse_buffer(wp, new_data, new_size);
962 window_pane_update_used_data(wp, &wp->offset, new_size);
965 /* Parse given input. */
966 void
967 input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
969 struct input_ctx *ictx = wp->ictx;
970 struct screen_write_ctx *sctx = &ictx->ctx;
972 if (len == 0)
973 return;
975 window_update_activity(wp->window);
976 wp->flags |= PANE_CHANGED;
978 /* Flag new input while in a mode. */
979 if (!TAILQ_EMPTY(&wp->modes))
980 wp->flags |= PANE_UNSEENCHANGES;
982 /* NULL wp if there is a mode set as don't want to update the tty. */
983 if (TAILQ_EMPTY(&wp->modes))
984 screen_write_start_pane(sctx, wp, &wp->base);
985 else
986 screen_write_start(sctx, &wp->base);
988 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
989 ictx->state->name, len, (int)len, buf);
991 input_parse(ictx, buf, len);
992 screen_write_stop(sctx);
995 /* Parse given input for screen. */
996 void
997 input_parse_screen(struct input_ctx *ictx, struct screen *s,
998 screen_write_init_ctx_cb cb, void *arg, u_char *buf, size_t len)
1000 struct screen_write_ctx *sctx = &ictx->ctx;
1002 if (len == 0)
1003 return;
1005 screen_write_start_callback(sctx, s, cb, arg);
1006 input_parse(ictx, buf, len);
1007 screen_write_stop(sctx);
1010 /* Split the parameter list (if any). */
1011 static int
1012 input_split(struct input_ctx *ictx)
1014 const char *errstr;
1015 char *ptr, *out;
1016 struct input_param *ip;
1017 u_int i;
1019 for (i = 0; i < ictx->param_list_len; i++) {
1020 if (ictx->param_list[i].type == INPUT_STRING)
1021 free(ictx->param_list[i].str);
1023 ictx->param_list_len = 0;
1025 if (ictx->param_len == 0)
1026 return (0);
1027 ip = &ictx->param_list[0];
1029 ptr = ictx->param_buf;
1030 while ((out = strsep(&ptr, ";")) != NULL) {
1031 if (*out == '\0')
1032 ip->type = INPUT_MISSING;
1033 else {
1034 if (strchr(out, ':') != NULL) {
1035 ip->type = INPUT_STRING;
1036 ip->str = xstrdup(out);
1037 } else {
1038 ip->type = INPUT_NUMBER;
1039 ip->num = strtonum(out, 0, INT_MAX, &errstr);
1040 if (errstr != NULL)
1041 return (-1);
1044 ip = &ictx->param_list[++ictx->param_list_len];
1045 if (ictx->param_list_len == nitems(ictx->param_list))
1046 return (-1);
1049 for (i = 0; i < ictx->param_list_len; i++) {
1050 ip = &ictx->param_list[i];
1051 if (ip->type == INPUT_MISSING)
1052 log_debug("parameter %u: missing", i);
1053 else if (ip->type == INPUT_STRING)
1054 log_debug("parameter %u: string %s", i, ip->str);
1055 else if (ip->type == INPUT_NUMBER)
1056 log_debug("parameter %u: number %d", i, ip->num);
1059 return (0);
1062 /* Get an argument or return default value. */
1063 static int
1064 input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1066 struct input_param *ip;
1067 int retval;
1069 if (validx >= ictx->param_list_len)
1070 return (defval);
1071 ip = &ictx->param_list[validx];
1072 if (ip->type == INPUT_MISSING)
1073 return (defval);
1074 if (ip->type == INPUT_STRING)
1075 return (-1);
1076 retval = ip->num;
1077 if (retval < minval)
1078 return (minval);
1079 return (retval);
1082 /* Reply to terminal query. */
1083 static void
1084 input_reply(struct input_ctx *ictx, const char *fmt, ...)
1086 struct bufferevent *bev = ictx->event;
1087 va_list ap;
1088 char *reply;
1090 if (bev == NULL)
1091 return;
1093 va_start(ap, fmt);
1094 xvasprintf(&reply, fmt, ap);
1095 va_end(ap);
1097 log_debug("%s: %s", __func__, reply);
1098 bufferevent_write(bev, reply, strlen(reply));
1099 free(reply);
1102 /* Clear saved state. */
1103 static void
1104 input_clear(struct input_ctx *ictx)
1106 event_del(&ictx->timer);
1108 *ictx->interm_buf = '\0';
1109 ictx->interm_len = 0;
1111 *ictx->param_buf = '\0';
1112 ictx->param_len = 0;
1114 *ictx->input_buf = '\0';
1115 ictx->input_len = 0;
1117 ictx->input_end = INPUT_END_ST;
1119 ictx->flags &= ~INPUT_DISCARD;
1122 /* Reset for ground state. */
1123 static void
1124 input_ground(struct input_ctx *ictx)
1126 event_del(&ictx->timer);
1127 evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
1129 if (ictx->input_space > INPUT_BUF_START) {
1130 ictx->input_space = INPUT_BUF_START;
1131 ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1135 /* Output this character to the screen. */
1136 static int
1137 input_print(struct input_ctx *ictx)
1139 struct screen_write_ctx *sctx = &ictx->ctx;
1140 int set;
1142 ictx->utf8started = 0; /* can't be valid UTF-8 */
1144 set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1145 if (set == 1)
1146 ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1147 else
1148 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1150 utf8_set(&ictx->cell.cell.data, ictx->ch);
1151 screen_write_collect_add(sctx, &ictx->cell.cell);
1152 ictx->last = ictx->ch;
1154 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1156 return (0);
1159 /* Collect intermediate string. */
1160 static int
1161 input_intermediate(struct input_ctx *ictx)
1163 if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
1164 ictx->flags |= INPUT_DISCARD;
1165 else {
1166 ictx->interm_buf[ictx->interm_len++] = ictx->ch;
1167 ictx->interm_buf[ictx->interm_len] = '\0';
1170 return (0);
1173 /* Collect parameter string. */
1174 static int
1175 input_parameter(struct input_ctx *ictx)
1177 if (ictx->param_len == (sizeof ictx->param_buf) - 1)
1178 ictx->flags |= INPUT_DISCARD;
1179 else {
1180 ictx->param_buf[ictx->param_len++] = ictx->ch;
1181 ictx->param_buf[ictx->param_len] = '\0';
1184 return (0);
1187 /* Collect input string. */
1188 static int
1189 input_input(struct input_ctx *ictx)
1191 size_t available;
1193 available = ictx->input_space;
1194 while (ictx->input_len + 1 >= available) {
1195 available *= 2;
1196 if (available > INPUT_BUF_LIMIT) {
1197 ictx->flags |= INPUT_DISCARD;
1198 return (0);
1200 ictx->input_buf = xrealloc(ictx->input_buf, available);
1201 ictx->input_space = available;
1203 ictx->input_buf[ictx->input_len++] = ictx->ch;
1204 ictx->input_buf[ictx->input_len] = '\0';
1206 return (0);
1209 /* Execute C0 control sequence. */
1210 static int
1211 input_c0_dispatch(struct input_ctx *ictx)
1213 struct screen_write_ctx *sctx = &ictx->ctx;
1214 struct window_pane *wp = ictx->wp;
1215 struct screen *s = sctx->s;
1217 ictx->utf8started = 0; /* can't be valid UTF-8 */
1219 log_debug("%s: '%c'", __func__, ictx->ch);
1221 switch (ictx->ch) {
1222 case '\000': /* NUL */
1223 break;
1224 case '\007': /* BEL */
1225 if (wp != NULL)
1226 alerts_queue(wp->window, WINDOW_BELL);
1227 break;
1228 case '\010': /* BS */
1229 screen_write_backspace(sctx);
1230 break;
1231 case '\011': /* HT */
1232 /* Don't tab beyond the end of the line. */
1233 if (s->cx >= screen_size_x(s) - 1)
1234 break;
1236 /* Find the next tab point, or use the last column if none. */
1237 do {
1238 s->cx++;
1239 if (bit_test(s->tabs, s->cx))
1240 break;
1241 } while (s->cx < screen_size_x(s) - 1);
1242 break;
1243 case '\012': /* LF */
1244 case '\013': /* VT */
1245 case '\014': /* FF */
1246 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1247 if (s->mode & MODE_CRLF)
1248 screen_write_carriagereturn(sctx);
1249 break;
1250 case '\015': /* CR */
1251 screen_write_carriagereturn(sctx);
1252 break;
1253 case '\016': /* SO */
1254 ictx->cell.set = 1;
1255 break;
1256 case '\017': /* SI */
1257 ictx->cell.set = 0;
1258 break;
1259 default:
1260 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1261 break;
1264 ictx->last = -1;
1265 return (0);
1268 /* Execute escape sequence. */
1269 static int
1270 input_esc_dispatch(struct input_ctx *ictx)
1272 struct screen_write_ctx *sctx = &ictx->ctx;
1273 struct screen *s = sctx->s;
1274 struct input_table_entry *entry;
1276 if (ictx->flags & INPUT_DISCARD)
1277 return (0);
1278 log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1280 entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
1281 sizeof input_esc_table[0], input_table_compare);
1282 if (entry == NULL) {
1283 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1284 return (0);
1287 switch (entry->type) {
1288 case INPUT_ESC_RIS:
1289 colour_palette_clear(ictx->palette);
1290 input_reset_cell(ictx);
1291 screen_write_reset(sctx);
1292 screen_write_fullredraw(sctx);
1293 break;
1294 case INPUT_ESC_IND:
1295 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1296 break;
1297 case INPUT_ESC_NEL:
1298 screen_write_carriagereturn(sctx);
1299 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1300 break;
1301 case INPUT_ESC_HTS:
1302 if (s->cx < screen_size_x(s))
1303 bit_set(s->tabs, s->cx);
1304 break;
1305 case INPUT_ESC_RI:
1306 screen_write_reverseindex(sctx, ictx->cell.cell.bg);
1307 break;
1308 case INPUT_ESC_DECKPAM:
1309 screen_write_mode_set(sctx, MODE_KKEYPAD);
1310 break;
1311 case INPUT_ESC_DECKPNM:
1312 screen_write_mode_clear(sctx, MODE_KKEYPAD);
1313 break;
1314 case INPUT_ESC_DECSC:
1315 input_save_state(ictx);
1316 break;
1317 case INPUT_ESC_DECRC:
1318 input_restore_state(ictx);
1319 break;
1320 case INPUT_ESC_DECALN:
1321 screen_write_alignmenttest(sctx);
1322 break;
1323 case INPUT_ESC_SCSG0_ON:
1324 ictx->cell.g0set = 1;
1325 break;
1326 case INPUT_ESC_SCSG0_OFF:
1327 ictx->cell.g0set = 0;
1328 break;
1329 case INPUT_ESC_SCSG1_ON:
1330 ictx->cell.g1set = 1;
1331 break;
1332 case INPUT_ESC_SCSG1_OFF:
1333 ictx->cell.g1set = 0;
1334 break;
1335 case INPUT_ESC_ST:
1336 /* ST terminates OSC but the state transition already did it. */
1337 break;
1340 ictx->last = -1;
1341 return (0);
1344 /* Execute control sequence. */
1345 static int
1346 input_csi_dispatch(struct input_ctx *ictx)
1348 struct screen_write_ctx *sctx = &ictx->ctx;
1349 struct screen *s = sctx->s;
1350 struct input_table_entry *entry;
1351 int i, n, m;
1352 u_int cx, bg = ictx->cell.cell.bg;
1354 if (ictx->flags & INPUT_DISCARD)
1355 return (0);
1357 log_debug("%s: '%c' \"%s\" \"%s\"", __func__, ictx->ch,
1358 ictx->interm_buf, ictx->param_buf);
1360 if (input_split(ictx) != 0)
1361 return (0);
1363 entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
1364 sizeof input_csi_table[0], input_table_compare);
1365 if (entry == NULL) {
1366 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1367 return (0);
1370 switch (entry->type) {
1371 case INPUT_CSI_CBT:
1372 /* Find the previous tab point, n times. */
1373 cx = s->cx;
1374 if (cx > screen_size_x(s) - 1)
1375 cx = screen_size_x(s) - 1;
1376 n = input_get(ictx, 0, 1, 1);
1377 if (n == -1)
1378 break;
1379 while (cx > 0 && n-- > 0) {
1381 cx--;
1382 while (cx > 0 && !bit_test(s->tabs, cx));
1384 s->cx = cx;
1385 break;
1386 case INPUT_CSI_CUB:
1387 n = input_get(ictx, 0, 1, 1);
1388 if (n != -1)
1389 screen_write_cursorleft(sctx, n);
1390 break;
1391 case INPUT_CSI_CUD:
1392 n = input_get(ictx, 0, 1, 1);
1393 if (n != -1)
1394 screen_write_cursordown(sctx, n);
1395 break;
1396 case INPUT_CSI_CUF:
1397 n = input_get(ictx, 0, 1, 1);
1398 if (n != -1)
1399 screen_write_cursorright(sctx, n);
1400 break;
1401 case INPUT_CSI_CUP:
1402 n = input_get(ictx, 0, 1, 1);
1403 m = input_get(ictx, 1, 1, 1);
1404 if (n != -1 && m != -1)
1405 screen_write_cursormove(sctx, m - 1, n - 1, 1);
1406 break;
1407 case INPUT_CSI_MODSET:
1408 n = input_get(ictx, 0, 0, 0);
1409 m = input_get(ictx, 1, 0, 0);
1410 if (options_get_number(global_options, "extended-keys") == 2)
1411 break;
1412 if (n == 0 || (n == 4 && m == 0))
1413 screen_write_mode_clear(sctx, MODE_KEXTENDED);
1414 else if (n == 4 && (m == 1 || m == 2))
1415 screen_write_mode_set(sctx, MODE_KEXTENDED);
1416 break;
1417 case INPUT_CSI_MODOFF:
1418 n = input_get(ictx, 0, 0, 0);
1419 if (n == 4)
1420 screen_write_mode_clear(sctx, MODE_KEXTENDED);
1421 break;
1422 case INPUT_CSI_WINOPS:
1423 input_csi_dispatch_winops(ictx);
1424 break;
1425 case INPUT_CSI_CUU:
1426 n = input_get(ictx, 0, 1, 1);
1427 if (n != -1)
1428 screen_write_cursorup(sctx, n);
1429 break;
1430 case INPUT_CSI_CNL:
1431 n = input_get(ictx, 0, 1, 1);
1432 if (n != -1) {
1433 screen_write_carriagereturn(sctx);
1434 screen_write_cursordown(sctx, n);
1436 break;
1437 case INPUT_CSI_CPL:
1438 n = input_get(ictx, 0, 1, 1);
1439 if (n != -1) {
1440 screen_write_carriagereturn(sctx);
1441 screen_write_cursorup(sctx, n);
1443 break;
1444 case INPUT_CSI_DA:
1445 switch (input_get(ictx, 0, 0, 0)) {
1446 case -1:
1447 break;
1448 case 0:
1449 #ifdef ENABLE_SIXEL
1450 input_reply(ictx, "\033[?1;2;4c");
1451 #else
1452 input_reply(ictx, "\033[?1;2c");
1453 #endif
1454 break;
1455 default:
1456 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1457 break;
1459 break;
1460 case INPUT_CSI_DA_TWO:
1461 switch (input_get(ictx, 0, 0, 0)) {
1462 case -1:
1463 break;
1464 case 0:
1465 input_reply(ictx, "\033[>84;0;0c");
1466 break;
1467 default:
1468 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1469 break;
1471 break;
1472 case INPUT_CSI_ECH:
1473 n = input_get(ictx, 0, 1, 1);
1474 if (n != -1)
1475 screen_write_clearcharacter(sctx, n, bg);
1476 break;
1477 case INPUT_CSI_DCH:
1478 n = input_get(ictx, 0, 1, 1);
1479 if (n != -1)
1480 screen_write_deletecharacter(sctx, n, bg);
1481 break;
1482 case INPUT_CSI_DECSTBM:
1483 n = input_get(ictx, 0, 1, 1);
1484 m = input_get(ictx, 1, 1, screen_size_y(s));
1485 if (n != -1 && m != -1)
1486 screen_write_scrollregion(sctx, n - 1, m - 1);
1487 break;
1488 case INPUT_CSI_DL:
1489 n = input_get(ictx, 0, 1, 1);
1490 if (n != -1)
1491 screen_write_deleteline(sctx, n, bg);
1492 break;
1493 case INPUT_CSI_DSR:
1494 switch (input_get(ictx, 0, 0, 0)) {
1495 case -1:
1496 break;
1497 case 5:
1498 input_reply(ictx, "\033[0n");
1499 break;
1500 case 6:
1501 input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1502 break;
1503 default:
1504 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1505 break;
1507 break;
1508 case INPUT_CSI_ED:
1509 switch (input_get(ictx, 0, 0, 0)) {
1510 case -1:
1511 break;
1512 case 0:
1513 screen_write_clearendofscreen(sctx, bg);
1514 break;
1515 case 1:
1516 screen_write_clearstartofscreen(sctx, bg);
1517 break;
1518 case 2:
1519 screen_write_clearscreen(sctx, bg);
1520 break;
1521 case 3:
1522 if (input_get(ictx, 1, 0, 0) == 0) {
1524 * Linux console extension to clear history
1525 * (for example before locking the screen).
1527 screen_write_clearhistory(sctx);
1529 break;
1530 default:
1531 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1532 break;
1534 break;
1535 case INPUT_CSI_EL:
1536 switch (input_get(ictx, 0, 0, 0)) {
1537 case -1:
1538 break;
1539 case 0:
1540 screen_write_clearendofline(sctx, bg);
1541 break;
1542 case 1:
1543 screen_write_clearstartofline(sctx, bg);
1544 break;
1545 case 2:
1546 screen_write_clearline(sctx, bg);
1547 break;
1548 default:
1549 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1550 break;
1552 break;
1553 case INPUT_CSI_HPA:
1554 n = input_get(ictx, 0, 1, 1);
1555 if (n != -1)
1556 screen_write_cursormove(sctx, n - 1, -1, 1);
1557 break;
1558 case INPUT_CSI_ICH:
1559 n = input_get(ictx, 0, 1, 1);
1560 if (n != -1)
1561 screen_write_insertcharacter(sctx, n, bg);
1562 break;
1563 case INPUT_CSI_IL:
1564 n = input_get(ictx, 0, 1, 1);
1565 if (n != -1)
1566 screen_write_insertline(sctx, n, bg);
1567 break;
1568 case INPUT_CSI_REP:
1569 n = input_get(ictx, 0, 1, 1);
1570 if (n == -1)
1571 break;
1573 m = screen_size_x(s) - s->cx;
1574 if (n > m)
1575 n = m;
1577 if (ictx->last == -1)
1578 break;
1579 ictx->ch = ictx->last;
1581 for (i = 0; i < n; i++)
1582 input_print(ictx);
1583 break;
1584 case INPUT_CSI_RCP:
1585 input_restore_state(ictx);
1586 break;
1587 case INPUT_CSI_RM:
1588 input_csi_dispatch_rm(ictx);
1589 break;
1590 case INPUT_CSI_RM_PRIVATE:
1591 input_csi_dispatch_rm_private(ictx);
1592 break;
1593 case INPUT_CSI_SCP:
1594 input_save_state(ictx);
1595 break;
1596 case INPUT_CSI_SGR:
1597 input_csi_dispatch_sgr(ictx);
1598 break;
1599 case INPUT_CSI_SM:
1600 input_csi_dispatch_sm(ictx);
1601 break;
1602 case INPUT_CSI_SM_PRIVATE:
1603 input_csi_dispatch_sm_private(ictx);
1604 break;
1605 case INPUT_CSI_SM_GRAPHICS:
1606 input_csi_dispatch_sm_graphics(ictx);
1607 break;
1608 case INPUT_CSI_SU:
1609 n = input_get(ictx, 0, 1, 1);
1610 if (n != -1)
1611 screen_write_scrollup(sctx, n, bg);
1612 break;
1613 case INPUT_CSI_SD:
1614 n = input_get(ictx, 0, 1, 1);
1615 if (n != -1)
1616 screen_write_scrolldown(sctx, n, bg);
1617 break;
1618 case INPUT_CSI_TBC:
1619 switch (input_get(ictx, 0, 0, 0)) {
1620 case -1:
1621 break;
1622 case 0:
1623 if (s->cx < screen_size_x(s))
1624 bit_clear(s->tabs, s->cx);
1625 break;
1626 case 3:
1627 bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1628 break;
1629 default:
1630 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1631 break;
1633 break;
1634 case INPUT_CSI_VPA:
1635 n = input_get(ictx, 0, 1, 1);
1636 if (n != -1)
1637 screen_write_cursormove(sctx, -1, n - 1, 1);
1638 break;
1639 case INPUT_CSI_DECSCUSR:
1640 n = input_get(ictx, 0, 0, 0);
1641 if (n != -1)
1642 screen_set_cursor_style(n, &s->cstyle, &s->mode);
1643 break;
1644 case INPUT_CSI_XDA:
1645 n = input_get(ictx, 0, 0, 0);
1646 if (n == 0)
1647 input_reply(ictx, "\033P>|tmux %s\033\\", getversion());
1648 break;
1652 ictx->last = -1;
1653 return (0);
1656 /* Handle CSI RM. */
1657 static void
1658 input_csi_dispatch_rm(struct input_ctx *ictx)
1660 struct screen_write_ctx *sctx = &ictx->ctx;
1661 u_int i;
1663 for (i = 0; i < ictx->param_list_len; i++) {
1664 switch (input_get(ictx, i, 0, -1)) {
1665 case -1:
1666 break;
1667 case 4: /* IRM */
1668 screen_write_mode_clear(sctx, MODE_INSERT);
1669 break;
1670 case 34:
1671 screen_write_mode_set(sctx, MODE_CURSOR_VERY_VISIBLE);
1672 break;
1673 default:
1674 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1675 break;
1680 /* Handle CSI private RM. */
1681 static void
1682 input_csi_dispatch_rm_private(struct input_ctx *ictx)
1684 struct screen_write_ctx *sctx = &ictx->ctx;
1685 struct grid_cell *gc = &ictx->cell.cell;
1686 u_int i;
1688 for (i = 0; i < ictx->param_list_len; i++) {
1689 switch (input_get(ictx, i, 0, -1)) {
1690 case -1:
1691 break;
1692 case 1: /* DECCKM */
1693 screen_write_mode_clear(sctx, MODE_KCURSOR);
1694 break;
1695 case 3: /* DECCOLM */
1696 screen_write_cursormove(sctx, 0, 0, 1);
1697 screen_write_clearscreen(sctx, gc->bg);
1698 break;
1699 case 6: /* DECOM */
1700 screen_write_mode_clear(sctx, MODE_ORIGIN);
1701 screen_write_cursormove(sctx, 0, 0, 1);
1702 break;
1703 case 7: /* DECAWM */
1704 screen_write_mode_clear(sctx, MODE_WRAP);
1705 break;
1706 case 12:
1707 screen_write_mode_clear(sctx, MODE_CURSOR_BLINKING);
1708 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET);
1709 break;
1710 case 25: /* TCEM */
1711 screen_write_mode_clear(sctx, MODE_CURSOR);
1712 break;
1713 case 1000:
1714 case 1001:
1715 case 1002:
1716 case 1003:
1717 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1718 break;
1719 case 1004:
1720 screen_write_mode_clear(sctx, MODE_FOCUSON);
1721 break;
1722 case 1005:
1723 screen_write_mode_clear(sctx, MODE_MOUSE_UTF8);
1724 break;
1725 case 1006:
1726 screen_write_mode_clear(sctx, MODE_MOUSE_SGR);
1727 break;
1728 case 47:
1729 case 1047:
1730 screen_write_alternateoff(sctx, gc, 0);
1731 break;
1732 case 1049:
1733 screen_write_alternateoff(sctx, gc, 1);
1734 break;
1735 case 2004:
1736 screen_write_mode_clear(sctx, MODE_BRACKETPASTE);
1737 break;
1738 default:
1739 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1740 break;
1745 /* Handle CSI SM. */
1746 static void
1747 input_csi_dispatch_sm(struct input_ctx *ictx)
1749 struct screen_write_ctx *sctx = &ictx->ctx;
1750 u_int i;
1752 for (i = 0; i < ictx->param_list_len; i++) {
1753 switch (input_get(ictx, i, 0, -1)) {
1754 case -1:
1755 break;
1756 case 4: /* IRM */
1757 screen_write_mode_set(sctx, MODE_INSERT);
1758 break;
1759 case 34:
1760 screen_write_mode_clear(sctx, MODE_CURSOR_VERY_VISIBLE);
1761 break;
1762 default:
1763 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1764 break;
1769 /* Handle CSI private SM. */
1770 static void
1771 input_csi_dispatch_sm_private(struct input_ctx *ictx)
1773 struct screen_write_ctx *sctx = &ictx->ctx;
1774 struct grid_cell *gc = &ictx->cell.cell;
1775 u_int i;
1777 for (i = 0; i < ictx->param_list_len; i++) {
1778 switch (input_get(ictx, i, 0, -1)) {
1779 case -1:
1780 break;
1781 case 1: /* DECCKM */
1782 screen_write_mode_set(sctx, MODE_KCURSOR);
1783 break;
1784 case 3: /* DECCOLM */
1785 screen_write_cursormove(sctx, 0, 0, 1);
1786 screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1787 break;
1788 case 6: /* DECOM */
1789 screen_write_mode_set(sctx, MODE_ORIGIN);
1790 screen_write_cursormove(sctx, 0, 0, 1);
1791 break;
1792 case 7: /* DECAWM */
1793 screen_write_mode_set(sctx, MODE_WRAP);
1794 break;
1795 case 12:
1796 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING);
1797 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET);
1798 break;
1799 case 25: /* TCEM */
1800 screen_write_mode_set(sctx, MODE_CURSOR);
1801 break;
1802 case 1000:
1803 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1804 screen_write_mode_set(sctx, MODE_MOUSE_STANDARD);
1805 break;
1806 case 1002:
1807 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1808 screen_write_mode_set(sctx, MODE_MOUSE_BUTTON);
1809 break;
1810 case 1003:
1811 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1812 screen_write_mode_set(sctx, MODE_MOUSE_ALL);
1813 break;
1814 case 1004:
1815 screen_write_mode_set(sctx, MODE_FOCUSON);
1816 break;
1817 case 1005:
1818 screen_write_mode_set(sctx, MODE_MOUSE_UTF8);
1819 break;
1820 case 1006:
1821 screen_write_mode_set(sctx, MODE_MOUSE_SGR);
1822 break;
1823 case 47:
1824 case 1047:
1825 screen_write_alternateon(sctx, gc, 0);
1826 break;
1827 case 1049:
1828 screen_write_alternateon(sctx, gc, 1);
1829 break;
1830 case 2004:
1831 screen_write_mode_set(sctx, MODE_BRACKETPASTE);
1832 break;
1833 default:
1834 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1835 break;
1840 /* Handle CSI graphics SM. */
1841 static void
1842 input_csi_dispatch_sm_graphics(__unused struct input_ctx *ictx)
1844 #ifdef ENABLE_SIXEL
1845 int n, m, o;
1847 if (ictx->param_list_len > 3)
1848 return;
1849 n = input_get(ictx, 0, 0, 0);
1850 m = input_get(ictx, 1, 0, 0);
1851 o = input_get(ictx, 2, 0, 0);
1853 if (n == 1 && (m == 1 || m == 2 || m == 4))
1854 input_reply(ictx, "\033[?%d;0;%uS", n, SIXEL_COLOUR_REGISTERS);
1855 else
1856 input_reply(ictx, "\033[?%d;3;%dS", n, o);
1857 #endif
1860 /* Handle CSI window operations. */
1861 static void
1862 input_csi_dispatch_winops(struct input_ctx *ictx)
1864 struct screen_write_ctx *sctx = &ictx->ctx;
1865 struct screen *s = sctx->s;
1866 struct window_pane *wp = ictx->wp;
1867 struct window *w = NULL;
1868 u_int x = screen_size_x(s), y = screen_size_y(s);
1869 int n, m;
1871 if (wp != NULL)
1872 w = wp->window;
1874 m = 0;
1875 while ((n = input_get(ictx, m, 0, -1)) != -1) {
1876 switch (n) {
1877 case 1:
1878 case 2:
1879 case 5:
1880 case 6:
1881 case 7:
1882 case 11:
1883 case 13:
1884 case 20:
1885 case 21:
1886 case 24:
1887 break;
1888 case 3:
1889 case 4:
1890 case 8:
1891 m++;
1892 if (input_get(ictx, m, 0, -1) == -1)
1893 return;
1894 /* FALLTHROUGH */
1895 case 9:
1896 case 10:
1897 m++;
1898 if (input_get(ictx, m, 0, -1) == -1)
1899 return;
1900 break;
1901 case 14:
1902 if (w == NULL)
1903 break;
1904 input_reply(ictx, "\033[4;%u;%ut", y * w->ypixel,
1905 x * w->xpixel);
1906 break;
1907 case 15:
1908 if (w == NULL)
1909 break;
1910 input_reply(ictx, "\033[5;%u;%ut", y * w->ypixel,
1911 x * w->xpixel);
1912 break;
1913 case 16:
1914 if (w == NULL)
1915 break;
1916 input_reply(ictx, "\033[6;%u;%ut", w->ypixel,
1917 w->xpixel);
1918 break;
1919 case 18:
1920 input_reply(ictx, "\033[8;%u;%ut", y, x);
1921 break;
1922 case 19:
1923 input_reply(ictx, "\033[9;%u;%ut", y, x);
1924 break;
1925 case 22:
1926 m++;
1927 switch (input_get(ictx, m, 0, -1)) {
1928 case -1:
1929 return;
1930 case 0:
1931 case 2:
1932 screen_push_title(sctx->s);
1933 break;
1935 break;
1936 case 23:
1937 m++;
1938 switch (input_get(ictx, m, 0, -1)) {
1939 case -1:
1940 return;
1941 case 0:
1942 case 2:
1943 screen_pop_title(sctx->s);
1944 if (wp == NULL)
1945 break;
1946 notify_pane("pane-title-changed", wp);
1947 server_redraw_window_borders(w);
1948 server_status_window(w);
1949 break;
1951 break;
1952 default:
1953 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1954 break;
1956 m++;
1960 /* Helper for 256 colour SGR. */
1961 static int
1962 input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
1964 struct grid_cell *gc = &ictx->cell.cell;
1966 if (c == -1 || c > 255) {
1967 if (fgbg == 38)
1968 gc->fg = 8;
1969 else if (fgbg == 48)
1970 gc->bg = 8;
1971 } else {
1972 if (fgbg == 38)
1973 gc->fg = c | COLOUR_FLAG_256;
1974 else if (fgbg == 48)
1975 gc->bg = c | COLOUR_FLAG_256;
1976 else if (fgbg == 58)
1977 gc->us = c | COLOUR_FLAG_256;
1979 return (1);
1982 /* Handle CSI SGR for 256 colours. */
1983 static void
1984 input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
1986 int c;
1988 c = input_get(ictx, (*i) + 1, 0, -1);
1989 if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
1990 (*i)++;
1993 /* Helper for RGB colour SGR. */
1994 static int
1995 input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
1996 int b)
1998 struct grid_cell *gc = &ictx->cell.cell;
2000 if (r == -1 || r > 255)
2001 return (0);
2002 if (g == -1 || g > 255)
2003 return (0);
2004 if (b == -1 || b > 255)
2005 return (0);
2007 if (fgbg == 38)
2008 gc->fg = colour_join_rgb(r, g, b);
2009 else if (fgbg == 48)
2010 gc->bg = colour_join_rgb(r, g, b);
2011 else if (fgbg == 58)
2012 gc->us = colour_join_rgb(r, g, b);
2013 return (1);
2016 /* Handle CSI SGR for RGB colours. */
2017 static void
2018 input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
2020 int r, g, b;
2022 r = input_get(ictx, (*i) + 1, 0, -1);
2023 g = input_get(ictx, (*i) + 2, 0, -1);
2024 b = input_get(ictx, (*i) + 3, 0, -1);
2025 if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
2026 (*i) += 3;
2029 /* Handle CSI SGR with a ISO parameter. */
2030 static void
2031 input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
2033 struct grid_cell *gc = &ictx->cell.cell;
2034 char *s = ictx->param_list[i].str, *copy, *ptr, *out;
2035 int p[8];
2036 u_int n;
2037 const char *errstr;
2039 for (n = 0; n < nitems(p); n++)
2040 p[n] = -1;
2041 n = 0;
2043 ptr = copy = xstrdup(s);
2044 while ((out = strsep(&ptr, ":")) != NULL) {
2045 if (*out != '\0') {
2046 p[n++] = strtonum(out, 0, INT_MAX, &errstr);
2047 if (errstr != NULL || n == nitems(p)) {
2048 free(copy);
2049 return;
2051 } else {
2052 n++;
2053 if (n == nitems(p)) {
2054 free(copy);
2055 return;
2058 log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
2060 free(copy);
2062 if (n == 0)
2063 return;
2064 if (p[0] == 4) {
2065 if (n != 2)
2066 return;
2067 switch (p[1]) {
2068 case 0:
2069 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2070 break;
2071 case 1:
2072 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2073 gc->attr |= GRID_ATTR_UNDERSCORE;
2074 break;
2075 case 2:
2076 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2077 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2078 break;
2079 case 3:
2080 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2081 gc->attr |= GRID_ATTR_UNDERSCORE_3;
2082 break;
2083 case 4:
2084 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2085 gc->attr |= GRID_ATTR_UNDERSCORE_4;
2086 break;
2087 case 5:
2088 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2089 gc->attr |= GRID_ATTR_UNDERSCORE_5;
2090 break;
2092 return;
2094 if (n < 2 || (p[0] != 38 && p[0] != 48 && p[0] != 58))
2095 return;
2096 switch (p[1]) {
2097 case 2:
2098 if (n < 3)
2099 break;
2100 if (n == 5)
2101 i = 2;
2102 else
2103 i = 3;
2104 if (n < i + 3)
2105 break;
2106 input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
2107 p[i + 2]);
2108 break;
2109 case 5:
2110 if (n < 3)
2111 break;
2112 input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
2113 break;
2117 /* Handle CSI SGR. */
2118 static void
2119 input_csi_dispatch_sgr(struct input_ctx *ictx)
2121 struct grid_cell *gc = &ictx->cell.cell;
2122 u_int i, link;
2123 int n;
2125 if (ictx->param_list_len == 0) {
2126 memcpy(gc, &grid_default_cell, sizeof *gc);
2127 return;
2130 for (i = 0; i < ictx->param_list_len; i++) {
2131 if (ictx->param_list[i].type == INPUT_STRING) {
2132 input_csi_dispatch_sgr_colon(ictx, i);
2133 continue;
2135 n = input_get(ictx, i, 0, 0);
2136 if (n == -1)
2137 continue;
2139 if (n == 38 || n == 48 || n == 58) {
2140 i++;
2141 switch (input_get(ictx, i, 0, -1)) {
2142 case 2:
2143 input_csi_dispatch_sgr_rgb(ictx, n, &i);
2144 break;
2145 case 5:
2146 input_csi_dispatch_sgr_256(ictx, n, &i);
2147 break;
2149 continue;
2152 switch (n) {
2153 case 0:
2154 link = gc->link;
2155 memcpy(gc, &grid_default_cell, sizeof *gc);
2156 gc->link = link;
2157 break;
2158 case 1:
2159 gc->attr |= GRID_ATTR_BRIGHT;
2160 break;
2161 case 2:
2162 gc->attr |= GRID_ATTR_DIM;
2163 break;
2164 case 3:
2165 gc->attr |= GRID_ATTR_ITALICS;
2166 break;
2167 case 4:
2168 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2169 gc->attr |= GRID_ATTR_UNDERSCORE;
2170 break;
2171 case 5:
2172 case 6:
2173 gc->attr |= GRID_ATTR_BLINK;
2174 break;
2175 case 7:
2176 gc->attr |= GRID_ATTR_REVERSE;
2177 break;
2178 case 8:
2179 gc->attr |= GRID_ATTR_HIDDEN;
2180 break;
2181 case 9:
2182 gc->attr |= GRID_ATTR_STRIKETHROUGH;
2183 break;
2184 case 21:
2185 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2186 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2187 break;
2188 case 22:
2189 gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
2190 break;
2191 case 23:
2192 gc->attr &= ~GRID_ATTR_ITALICS;
2193 break;
2194 case 24:
2195 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2196 break;
2197 case 25:
2198 gc->attr &= ~GRID_ATTR_BLINK;
2199 break;
2200 case 27:
2201 gc->attr &= ~GRID_ATTR_REVERSE;
2202 break;
2203 case 28:
2204 gc->attr &= ~GRID_ATTR_HIDDEN;
2205 break;
2206 case 29:
2207 gc->attr &= ~GRID_ATTR_STRIKETHROUGH;
2208 break;
2209 case 30:
2210 case 31:
2211 case 32:
2212 case 33:
2213 case 34:
2214 case 35:
2215 case 36:
2216 case 37:
2217 gc->fg = n - 30;
2218 break;
2219 case 39:
2220 gc->fg = 8;
2221 break;
2222 case 40:
2223 case 41:
2224 case 42:
2225 case 43:
2226 case 44:
2227 case 45:
2228 case 46:
2229 case 47:
2230 gc->bg = n - 40;
2231 break;
2232 case 49:
2233 gc->bg = 8;
2234 break;
2235 case 53:
2236 gc->attr |= GRID_ATTR_OVERLINE;
2237 break;
2238 case 55:
2239 gc->attr &= ~GRID_ATTR_OVERLINE;
2240 break;
2241 case 59:
2242 gc->us = 8;
2243 break;
2244 case 90:
2245 case 91:
2246 case 92:
2247 case 93:
2248 case 94:
2249 case 95:
2250 case 96:
2251 case 97:
2252 gc->fg = n;
2253 break;
2254 case 100:
2255 case 101:
2256 case 102:
2257 case 103:
2258 case 104:
2259 case 105:
2260 case 106:
2261 case 107:
2262 gc->bg = n - 10;
2263 break;
2268 /* End of input with BEL. */
2269 static int
2270 input_end_bel(struct input_ctx *ictx)
2272 log_debug("%s", __func__);
2274 ictx->input_end = INPUT_END_BEL;
2276 return (0);
2279 /* DCS string started. */
2280 static void
2281 input_enter_dcs(struct input_ctx *ictx)
2283 log_debug("%s", __func__);
2285 input_clear(ictx);
2286 input_start_timer(ictx);
2287 ictx->last = -1;
2290 /* DCS terminator (ST) received. */
2291 static int
2292 input_dcs_dispatch(struct input_ctx *ictx)
2294 struct window_pane *wp = ictx->wp;
2295 struct screen_write_ctx *sctx = &ictx->ctx;
2296 u_char *buf = ictx->input_buf;
2297 size_t len = ictx->input_len;
2298 const char prefix[] = "tmux;";
2299 const u_int prefixlen = (sizeof prefix) - 1;
2300 long long allow_passthrough = 0;
2301 #ifdef ENABLE_SIXEL
2302 struct window *w;
2303 struct sixel_image *si;
2304 #endif
2306 if (wp == NULL)
2307 return (0);
2309 if (ictx->flags & INPUT_DISCARD) {
2310 log_debug("%s: %zu bytes (discard)", __func__, len);
2311 return (0);
2314 #ifdef ENABLE_SIXEL
2315 w = wp->window;
2316 if (buf[0] == 'q') {
2317 si = sixel_parse(buf, len, w->xpixel, w->ypixel);
2318 if (si != NULL)
2319 screen_write_sixelimage(sctx, si, ictx->cell.cell.bg);
2321 #endif
2323 allow_passthrough = options_get_number(wp->options, "allow-passthrough");
2324 if (!allow_passthrough)
2325 return (0);
2326 log_debug("%s: \"%s\"", __func__, buf);
2328 if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0) {
2329 screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen,
2330 allow_passthrough == 2);
2333 return (0);
2336 /* OSC string started. */
2337 static void
2338 input_enter_osc(struct input_ctx *ictx)
2340 log_debug("%s", __func__);
2342 input_clear(ictx);
2343 input_start_timer(ictx);
2344 ictx->last = -1;
2347 /* OSC terminator (ST) received. */
2348 static void
2349 input_exit_osc(struct input_ctx *ictx)
2351 struct screen_write_ctx *sctx = &ictx->ctx;
2352 struct window_pane *wp = ictx->wp;
2353 u_char *p = ictx->input_buf;
2354 u_int option;
2356 if (ictx->flags & INPUT_DISCARD)
2357 return;
2358 if (ictx->input_len < 1 || *p < '0' || *p > '9')
2359 return;
2361 log_debug("%s: \"%s\" (end %s)", __func__, p,
2362 ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2364 option = 0;
2365 while (*p >= '0' && *p <= '9')
2366 option = option * 10 + *p++ - '0';
2367 if (*p != ';' && *p != '\0')
2368 return;
2369 if (*p == ';')
2370 p++;
2372 switch (option) {
2373 case 0:
2374 case 2:
2375 if (wp != NULL &&
2376 options_get_number(wp->options, "allow-set-title") &&
2377 screen_set_title(sctx->s, p)) {
2378 notify_pane("pane-title-changed", wp);
2379 server_redraw_window_borders(wp->window);
2380 server_status_window(wp->window);
2382 break;
2383 case 4:
2384 input_osc_4(ictx, p);
2385 break;
2386 case 7:
2387 if (utf8_isvalid(p)) {
2388 screen_set_path(sctx->s, p);
2389 if (wp != NULL) {
2390 server_redraw_window_borders(wp->window);
2391 server_status_window(wp->window);
2394 break;
2395 case 8:
2396 input_osc_8(ictx, p);
2397 break;
2398 case 10:
2399 input_osc_10(ictx, p);
2400 break;
2401 case 11:
2402 input_osc_11(ictx, p);
2403 break;
2404 case 12:
2405 input_osc_12(ictx, p);
2406 break;
2407 case 52:
2408 input_osc_52(ictx, p);
2409 break;
2410 case 104:
2411 input_osc_104(ictx, p);
2412 break;
2413 case 110:
2414 input_osc_110(ictx, p);
2415 break;
2416 case 111:
2417 input_osc_111(ictx, p);
2418 break;
2419 case 112:
2420 input_osc_112(ictx, p);
2421 break;
2422 case 133:
2423 input_osc_133(ictx, p);
2424 break;
2425 default:
2426 log_debug("%s: unknown '%u'", __func__, option);
2427 break;
2431 /* APC string started. */
2432 static void
2433 input_enter_apc(struct input_ctx *ictx)
2435 log_debug("%s", __func__);
2437 input_clear(ictx);
2438 input_start_timer(ictx);
2439 ictx->last = -1;
2442 /* APC terminator (ST) received. */
2443 static void
2444 input_exit_apc(struct input_ctx *ictx)
2446 struct screen_write_ctx *sctx = &ictx->ctx;
2447 struct window_pane *wp = ictx->wp;
2449 if (ictx->flags & INPUT_DISCARD)
2450 return;
2451 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2453 if (screen_set_title(sctx->s, ictx->input_buf) && wp != NULL) {
2454 notify_pane("pane-title-changed", wp);
2455 server_redraw_window_borders(wp->window);
2456 server_status_window(wp->window);
2460 /* Rename string started. */
2461 static void
2462 input_enter_rename(struct input_ctx *ictx)
2464 log_debug("%s", __func__);
2466 input_clear(ictx);
2467 input_start_timer(ictx);
2468 ictx->last = -1;
2471 /* Rename terminator (ST) received. */
2472 static void
2473 input_exit_rename(struct input_ctx *ictx)
2475 struct window_pane *wp = ictx->wp;
2476 struct window *w;
2477 struct options_entry *o;
2479 if (wp == NULL)
2480 return;
2481 if (ictx->flags & INPUT_DISCARD)
2482 return;
2483 if (!options_get_number(ictx->wp->options, "allow-rename"))
2484 return;
2485 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2487 if (!utf8_isvalid(ictx->input_buf))
2488 return;
2489 w = wp->window;
2491 if (ictx->input_len == 0) {
2492 o = options_get_only(w->options, "automatic-rename");
2493 if (o != NULL)
2494 options_remove_or_default(o, -1, NULL);
2495 if (!options_get_number(w->options, "automatic-rename"))
2496 window_set_name(w, "");
2497 } else {
2498 options_set_number(w->options, "automatic-rename", 0);
2499 window_set_name(w, ictx->input_buf);
2501 server_redraw_window_borders(w);
2502 server_status_window(w);
2505 /* Open UTF-8 character. */
2506 static int
2507 input_top_bit_set(struct input_ctx *ictx)
2509 struct screen_write_ctx *sctx = &ictx->ctx;
2510 struct utf8_data *ud = &ictx->utf8data;
2512 ictx->last = -1;
2514 if (!ictx->utf8started) {
2515 if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2516 return (0);
2517 ictx->utf8started = 1;
2518 return (0);
2521 switch (utf8_append(ud, ictx->ch)) {
2522 case UTF8_MORE:
2523 return (0);
2524 case UTF8_ERROR:
2525 ictx->utf8started = 0;
2526 return (0);
2527 case UTF8_DONE:
2528 break;
2530 ictx->utf8started = 0;
2532 log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2533 (int)ud->size, ud->data, ud->width);
2535 utf8_copy(&ictx->cell.cell.data, ud);
2536 screen_write_collect_add(sctx, &ictx->cell.cell);
2538 return (0);
2541 /* Reply to a colour request. */
2542 static void
2543 input_osc_colour_reply(struct input_ctx *ictx, u_int n, int c)
2545 u_char r, g, b;
2546 const char *end;
2548 if (c != -1)
2549 c = colour_force_rgb(c);
2550 if (c == -1)
2551 return;
2552 colour_split_rgb(c, &r, &g, &b);
2554 if (ictx->input_end == INPUT_END_BEL)
2555 end = "\007";
2556 else
2557 end = "\033\\";
2558 input_reply(ictx, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2559 n, r, r, g, g, b, b, end);
2562 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2563 static void
2564 input_osc_4(struct input_ctx *ictx, const char *p)
2566 char *copy, *s, *next = NULL;
2567 long idx;
2568 int c, bad = 0, redraw = 0;
2570 copy = s = xstrdup(p);
2571 while (s != NULL && *s != '\0') {
2572 idx = strtol(s, &next, 10);
2573 if (*next++ != ';') {
2574 bad = 1;
2575 break;
2577 if (idx < 0 || idx >= 256) {
2578 bad = 1;
2579 break;
2582 s = strsep(&next, ";");
2583 if (strcmp(s, "?") == 0) {
2584 c = colour_palette_get(ictx->palette, idx);
2585 if (c != -1)
2586 input_osc_colour_reply(ictx, 4, c);
2587 continue;
2589 if ((c = colour_parseX11(s)) == -1) {
2590 s = next;
2591 continue;
2593 if (colour_palette_set(ictx->palette, idx, c))
2594 redraw = 1;
2595 s = next;
2597 if (bad)
2598 log_debug("bad OSC 4: %s", p);
2599 if (redraw)
2600 screen_write_fullredraw(&ictx->ctx);
2601 free(copy);
2604 /* Handle the OSC 8 sequence for embedding hyperlinks. */
2605 static void
2606 input_osc_8(struct input_ctx *ictx, const char *p)
2608 struct hyperlinks *hl = ictx->ctx.s->hyperlinks;
2609 struct grid_cell *gc = &ictx->cell.cell;
2610 const char *start, *end, *uri;
2611 char *id = NULL;
2613 for (start = p; (end = strpbrk(start, ":;")) != NULL; start = end + 1) {
2614 if (end - start >= 4 && strncmp(start, "id=", 3) == 0) {
2615 if (id != NULL)
2616 goto bad;
2617 id = xstrndup(start + 3, end - start - 3);
2620 /* The first ; is the end of parameters and start of the URI. */
2621 if (*end == ';')
2622 break;
2624 if (end == NULL || *end != ';')
2625 goto bad;
2626 uri = end + 1;
2627 if (*uri == '\0') {
2628 gc->link = 0;
2629 free(id);
2630 return;
2632 gc->link = hyperlinks_put(hl, uri, id);
2633 if (id == NULL)
2634 log_debug("hyperlink (anonymous) %s = %u", uri, gc->link);
2635 else
2636 log_debug("hyperlink (id=%s) %s = %u", id, uri, gc->link);
2637 free(id);
2638 return;
2640 bad:
2641 log_debug("bad OSC 8 %s", p);
2642 free(id);
2646 * Get a client with a foreground for the pane. There isn't much to choose
2647 * between them so just use the first.
2649 static int
2650 input_get_fg_client(struct window_pane *wp)
2652 struct window *w = wp->window;
2653 struct client *loop;
2655 TAILQ_FOREACH(loop, &clients, entry) {
2656 if (loop->flags & CLIENT_UNATTACHEDFLAGS)
2657 continue;
2658 if (loop->session == NULL || !session_has(loop->session, w))
2659 continue;
2660 if (loop->tty.fg == -1)
2661 continue;
2662 return (loop->tty.fg);
2664 return (-1);
2667 /* Get a client with a background for the pane. */
2668 static int
2669 input_get_bg_client(struct window_pane *wp)
2671 struct window *w = wp->window;
2672 struct client *loop;
2674 TAILQ_FOREACH(loop, &clients, entry) {
2675 if (loop->flags & CLIENT_UNATTACHEDFLAGS)
2676 continue;
2677 if (loop->session == NULL || !session_has(loop->session, w))
2678 continue;
2679 if (loop->tty.bg == -1)
2680 continue;
2681 return (loop->tty.bg);
2683 return (-1);
2686 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2687 static void
2688 input_osc_10(struct input_ctx *ictx, const char *p)
2690 struct window_pane *wp = ictx->wp;
2691 struct grid_cell defaults;
2692 int c;
2694 if (strcmp(p, "?") == 0) {
2695 if (wp == NULL)
2696 return;
2697 tty_default_colours(&defaults, wp);
2698 if (COLOUR_DEFAULT(defaults.fg))
2699 c = input_get_fg_client(wp);
2700 else
2701 c = defaults.fg;
2702 input_osc_colour_reply(ictx, 10, c);
2703 return;
2706 if ((c = colour_parseX11(p)) == -1) {
2707 log_debug("bad OSC 10: %s", p);
2708 return;
2710 if (ictx->palette != NULL) {
2711 ictx->palette->fg = c;
2712 if (wp != NULL)
2713 wp->flags |= PANE_STYLECHANGED;
2714 screen_write_fullredraw(&ictx->ctx);
2718 /* Handle the OSC 110 sequence for resetting foreground colour. */
2719 static void
2720 input_osc_110(struct input_ctx *ictx, const char *p)
2722 struct window_pane *wp = ictx->wp;
2724 if (*p != '\0')
2725 return;
2726 if (ictx->palette != NULL) {
2727 ictx->palette->fg = 8;
2728 if (wp != NULL)
2729 wp->flags |= PANE_STYLECHANGED;
2730 screen_write_fullredraw(&ictx->ctx);
2734 /* Handle the OSC 11 sequence for setting and querying background colour. */
2735 static void
2736 input_osc_11(struct input_ctx *ictx, const char *p)
2738 struct window_pane *wp = ictx->wp;
2739 struct grid_cell defaults;
2740 int c;
2742 if (strcmp(p, "?") == 0) {
2743 if (wp == NULL)
2744 return;
2745 tty_default_colours(&defaults, wp);
2746 if (COLOUR_DEFAULT(defaults.bg))
2747 c = input_get_bg_client(wp);
2748 else
2749 c = defaults.bg;
2750 input_osc_colour_reply(ictx, 11, c);
2751 return;
2754 if ((c = colour_parseX11(p)) == -1) {
2755 log_debug("bad OSC 11: %s", p);
2756 return;
2758 if (ictx->palette != NULL) {
2759 ictx->palette->bg = c;
2760 if (wp != NULL)
2761 wp->flags |= PANE_STYLECHANGED;
2762 screen_write_fullredraw(&ictx->ctx);
2766 /* Handle the OSC 111 sequence for resetting background colour. */
2767 static void
2768 input_osc_111(struct input_ctx *ictx, const char *p)
2770 struct window_pane *wp = ictx->wp;
2772 if (*p != '\0')
2773 return;
2774 if (ictx->palette != NULL) {
2775 ictx->palette->bg = 8;
2776 if (wp != NULL)
2777 wp->flags |= PANE_STYLECHANGED;
2778 screen_write_fullredraw(&ictx->ctx);
2782 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2783 static void
2784 input_osc_12(struct input_ctx *ictx, const char *p)
2786 struct window_pane *wp = ictx->wp;
2787 int c;
2789 if (strcmp(p, "?") == 0) {
2790 if (wp != NULL) {
2791 c = ictx->ctx.s->ccolour;
2792 if (c == -1)
2793 c = ictx->ctx.s->default_ccolour;
2794 input_osc_colour_reply(ictx, 12, c);
2796 return;
2799 if ((c = colour_parseX11(p)) == -1) {
2800 log_debug("bad OSC 12: %s", p);
2801 return;
2803 screen_set_cursor_colour(ictx->ctx.s, c);
2806 /* Handle the OSC 112 sequence for resetting cursor colour. */
2807 static void
2808 input_osc_112(struct input_ctx *ictx, const char *p)
2810 if (*p == '\0') /* no arguments allowed */
2811 screen_set_cursor_colour(ictx->ctx.s, -1);
2814 /* Handle the OSC 133 sequence. */
2815 static void
2816 input_osc_133(struct input_ctx *ictx, const char *p)
2818 struct grid *gd = ictx->ctx.s->grid;
2819 u_int line = ictx->ctx.s->cy + gd->hsize;
2820 struct grid_line *gl;
2822 if (line > gd->hsize + gd->sy - 1)
2823 return;
2824 gl = grid_get_line(gd, line);
2826 switch (*p) {
2827 case 'A':
2828 gl->flags |= GRID_LINE_START_PROMPT;
2829 break;
2830 case 'C':
2831 gl->flags |= GRID_LINE_START_OUTPUT;
2832 break;
2836 /* Handle the OSC 52 sequence for setting the clipboard. */
2837 static void
2838 input_osc_52(struct input_ctx *ictx, const char *p)
2840 struct window_pane *wp = ictx->wp;
2841 char *end;
2842 const char *buf = NULL;
2843 size_t len = 0;
2844 u_char *out;
2845 int outlen, state;
2846 struct screen_write_ctx ctx;
2847 struct paste_buffer *pb;
2848 const char* allow = "cpqs01234567";
2849 char flags[sizeof "cpqs01234567"] = "";
2850 u_int i, j = 0;
2852 if (wp == NULL)
2853 return;
2854 state = options_get_number(global_options, "set-clipboard");
2855 if (state != 2)
2856 return;
2858 if ((end = strchr(p, ';')) == NULL)
2859 return;
2860 end++;
2861 if (*end == '\0')
2862 return;
2863 log_debug("%s: %s", __func__, end);
2865 for (i = 0; p + i != end; i++) {
2866 if (strchr(allow, p[i]) != NULL && strchr(flags, p[i]) == NULL)
2867 flags[j++] = p[i];
2869 log_debug("%s: %.*s %s", __func__, (int)(end - p - 1), p, flags);
2871 if (strcmp(end, "?") == 0) {
2872 if ((pb = paste_get_top(NULL)) != NULL)
2873 buf = paste_buffer_data(pb, &len);
2874 if (ictx->input_end == INPUT_END_BEL)
2875 input_reply_clipboard(ictx->event, buf, len, "\007");
2876 else
2877 input_reply_clipboard(ictx->event, buf, len, "\033\\");
2878 return;
2881 len = (strlen(end) / 4) * 3;
2882 if (len == 0)
2883 return;
2885 out = xmalloc(len);
2886 if ((outlen = b64_pton(end, out, len)) == -1) {
2887 free(out);
2888 return;
2891 screen_write_start_pane(&ctx, wp, NULL);
2892 screen_write_setselection(&ctx, flags, out, outlen);
2893 screen_write_stop(&ctx);
2894 notify_pane("pane-set-clipboard", wp);
2896 paste_add(NULL, out, outlen);
2899 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2900 static void
2901 input_osc_104(struct input_ctx *ictx, const char *p)
2903 char *copy, *s;
2904 long idx;
2905 int bad = 0, redraw = 0;
2907 if (*p == '\0') {
2908 colour_palette_clear(ictx->palette);
2909 screen_write_fullredraw(&ictx->ctx);
2910 return;
2913 copy = s = xstrdup(p);
2914 while (*s != '\0') {
2915 idx = strtol(s, &s, 10);
2916 if (*s != '\0' && *s != ';') {
2917 bad = 1;
2918 break;
2920 if (idx < 0 || idx >= 256) {
2921 bad = 1;
2922 break;
2924 if (colour_palette_set(ictx->palette, idx, -1))
2925 redraw = 1;
2926 if (*s == ';')
2927 s++;
2929 if (bad)
2930 log_debug("bad OSC 104: %s", p);
2931 if (redraw)
2932 screen_write_fullredraw(&ictx->ctx);
2933 free(copy);
2936 void
2937 input_reply_clipboard(struct bufferevent *bev, const char *buf, size_t len,
2938 const char *end)
2940 char *out = NULL;
2941 int outlen = 0;
2943 if (buf != NULL && len != 0) {
2944 if (len >= ((size_t)INT_MAX * 3 / 4) - 1)
2945 return;
2946 outlen = 4 * ((len + 2) / 3) + 1;
2947 out = xmalloc(outlen);
2948 if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
2949 free(out);
2950 return;
2954 bufferevent_write(bev, "\033]52;;", 6);
2955 if (outlen != 0)
2956 bufferevent_write(bev, out, outlen);
2957 bufferevent_write(bev, end, strlen(end));
2958 free(out);