Add a way to force a colour to RGB and a format to display it.
[tmux-openbsd.git] / input.c
blob8a16281c906685a15c808125d7b02af0f4e02568
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_10(struct input_ctx *, const char *);
139 static void input_osc_11(struct input_ctx *, const char *);
140 static void input_osc_52(struct input_ctx *, const char *);
141 static void input_osc_104(struct input_ctx *, const char *);
142 static void input_osc_110(struct input_ctx *, const char *);
143 static void input_osc_111(struct input_ctx *, const char *);
145 /* Transition entry/exit handlers. */
146 static void input_clear(struct input_ctx *);
147 static void input_ground(struct input_ctx *);
148 static void input_enter_dcs(struct input_ctx *);
149 static void input_enter_osc(struct input_ctx *);
150 static void input_exit_osc(struct input_ctx *);
151 static void input_enter_apc(struct input_ctx *);
152 static void input_exit_apc(struct input_ctx *);
153 static void input_enter_rename(struct input_ctx *);
154 static void input_exit_rename(struct input_ctx *);
156 /* Input state handlers. */
157 static int input_print(struct input_ctx *);
158 static int input_intermediate(struct input_ctx *);
159 static int input_parameter(struct input_ctx *);
160 static int input_input(struct input_ctx *);
161 static int input_c0_dispatch(struct input_ctx *);
162 static int input_esc_dispatch(struct input_ctx *);
163 static int input_csi_dispatch(struct input_ctx *);
164 static void input_csi_dispatch_rm(struct input_ctx *);
165 static void input_csi_dispatch_rm_private(struct input_ctx *);
166 static void input_csi_dispatch_sm(struct input_ctx *);
167 static void input_csi_dispatch_sm_private(struct input_ctx *);
168 static void input_csi_dispatch_winops(struct input_ctx *);
169 static void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
170 static void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
171 static void input_csi_dispatch_sgr(struct input_ctx *);
172 static int input_dcs_dispatch(struct input_ctx *);
173 static int input_top_bit_set(struct input_ctx *);
174 static int input_end_bel(struct input_ctx *);
176 /* Command table comparison function. */
177 static int input_table_compare(const void *, const void *);
179 /* Command table entry. */
180 struct input_table_entry {
181 int ch;
182 const char *interm;
183 int type;
186 /* Escape commands. */
187 enum input_esc_type {
188 INPUT_ESC_DECALN,
189 INPUT_ESC_DECKPAM,
190 INPUT_ESC_DECKPNM,
191 INPUT_ESC_DECRC,
192 INPUT_ESC_DECSC,
193 INPUT_ESC_HTS,
194 INPUT_ESC_IND,
195 INPUT_ESC_NEL,
196 INPUT_ESC_RI,
197 INPUT_ESC_RIS,
198 INPUT_ESC_SCSG0_OFF,
199 INPUT_ESC_SCSG0_ON,
200 INPUT_ESC_SCSG1_OFF,
201 INPUT_ESC_SCSG1_ON,
202 INPUT_ESC_ST,
205 /* Escape command table. */
206 static const struct input_table_entry input_esc_table[] = {
207 { '0', "(", INPUT_ESC_SCSG0_ON },
208 { '0', ")", INPUT_ESC_SCSG1_ON },
209 { '7', "", INPUT_ESC_DECSC },
210 { '8', "", INPUT_ESC_DECRC },
211 { '8', "#", INPUT_ESC_DECALN },
212 { '=', "", INPUT_ESC_DECKPAM },
213 { '>', "", INPUT_ESC_DECKPNM },
214 { 'B', "(", INPUT_ESC_SCSG0_OFF },
215 { 'B', ")", INPUT_ESC_SCSG1_OFF },
216 { 'D', "", INPUT_ESC_IND },
217 { 'E', "", INPUT_ESC_NEL },
218 { 'H', "", INPUT_ESC_HTS },
219 { 'M', "", INPUT_ESC_RI },
220 { '\\', "", INPUT_ESC_ST },
221 { 'c', "", INPUT_ESC_RIS },
224 /* Control (CSI) commands. */
225 enum input_csi_type {
226 INPUT_CSI_CBT,
227 INPUT_CSI_CNL,
228 INPUT_CSI_CPL,
229 INPUT_CSI_CUB,
230 INPUT_CSI_CUD,
231 INPUT_CSI_CUF,
232 INPUT_CSI_CUP,
233 INPUT_CSI_CUU,
234 INPUT_CSI_DA,
235 INPUT_CSI_DA_TWO,
236 INPUT_CSI_DCH,
237 INPUT_CSI_DECSCUSR,
238 INPUT_CSI_DECSTBM,
239 INPUT_CSI_DL,
240 INPUT_CSI_DSR,
241 INPUT_CSI_ECH,
242 INPUT_CSI_ED,
243 INPUT_CSI_EL,
244 INPUT_CSI_HPA,
245 INPUT_CSI_ICH,
246 INPUT_CSI_IL,
247 INPUT_CSI_MODOFF,
248 INPUT_CSI_MODSET,
249 INPUT_CSI_RCP,
250 INPUT_CSI_REP,
251 INPUT_CSI_RM,
252 INPUT_CSI_RM_PRIVATE,
253 INPUT_CSI_SCP,
254 INPUT_CSI_SD,
255 INPUT_CSI_SGR,
256 INPUT_CSI_SM,
257 INPUT_CSI_SM_PRIVATE,
258 INPUT_CSI_SU,
259 INPUT_CSI_TBC,
260 INPUT_CSI_VPA,
261 INPUT_CSI_WINOPS,
262 INPUT_CSI_XDA,
265 /* Control (CSI) command table. */
266 static const struct input_table_entry input_csi_table[] = {
267 { '@', "", INPUT_CSI_ICH },
268 { 'A', "", INPUT_CSI_CUU },
269 { 'B', "", INPUT_CSI_CUD },
270 { 'C', "", INPUT_CSI_CUF },
271 { 'D', "", INPUT_CSI_CUB },
272 { 'E', "", INPUT_CSI_CNL },
273 { 'F', "", INPUT_CSI_CPL },
274 { 'G', "", INPUT_CSI_HPA },
275 { 'H', "", INPUT_CSI_CUP },
276 { 'J', "", INPUT_CSI_ED },
277 { 'K', "", INPUT_CSI_EL },
278 { 'L', "", INPUT_CSI_IL },
279 { 'M', "", INPUT_CSI_DL },
280 { 'P', "", INPUT_CSI_DCH },
281 { 'S', "", INPUT_CSI_SU },
282 { 'T', "", INPUT_CSI_SD },
283 { 'X', "", INPUT_CSI_ECH },
284 { 'Z', "", INPUT_CSI_CBT },
285 { '`', "", INPUT_CSI_HPA },
286 { 'b', "", INPUT_CSI_REP },
287 { 'c', "", INPUT_CSI_DA },
288 { 'c', ">", INPUT_CSI_DA_TWO },
289 { 'd', "", INPUT_CSI_VPA },
290 { 'f', "", INPUT_CSI_CUP },
291 { 'g', "", INPUT_CSI_TBC },
292 { 'h', "", INPUT_CSI_SM },
293 { 'h', "?", INPUT_CSI_SM_PRIVATE },
294 { 'l', "", INPUT_CSI_RM },
295 { 'l', "?", INPUT_CSI_RM_PRIVATE },
296 { 'm', "", INPUT_CSI_SGR },
297 { 'm', ">", INPUT_CSI_MODSET },
298 { 'n', "", INPUT_CSI_DSR },
299 { 'n', ">", INPUT_CSI_MODOFF },
300 { 'q', " ", INPUT_CSI_DECSCUSR },
301 { 'q', ">", INPUT_CSI_XDA },
302 { 'r', "", INPUT_CSI_DECSTBM },
303 { 's', "", INPUT_CSI_SCP },
304 { 't', "", INPUT_CSI_WINOPS },
305 { 'u', "", INPUT_CSI_RCP },
308 /* Input transition. */
309 struct input_transition {
310 int first;
311 int last;
313 int (*handler)(struct input_ctx *);
314 const struct input_state *state;
317 /* Input state. */
318 struct input_state {
319 const char *name;
320 void (*enter)(struct input_ctx *);
321 void (*exit)(struct input_ctx *);
322 const struct input_transition *transitions;
325 /* State transitions available from all states. */
326 #define INPUT_STATE_ANYWHERE \
327 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
328 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
329 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
331 /* Forward declarations of state tables. */
332 static const struct input_transition input_state_ground_table[];
333 static const struct input_transition input_state_esc_enter_table[];
334 static const struct input_transition input_state_esc_intermediate_table[];
335 static const struct input_transition input_state_csi_enter_table[];
336 static const struct input_transition input_state_csi_parameter_table[];
337 static const struct input_transition input_state_csi_intermediate_table[];
338 static const struct input_transition input_state_csi_ignore_table[];
339 static const struct input_transition input_state_dcs_enter_table[];
340 static const struct input_transition input_state_dcs_parameter_table[];
341 static const struct input_transition input_state_dcs_intermediate_table[];
342 static const struct input_transition input_state_dcs_handler_table[];
343 static const struct input_transition input_state_dcs_escape_table[];
344 static const struct input_transition input_state_dcs_ignore_table[];
345 static const struct input_transition input_state_osc_string_table[];
346 static const struct input_transition input_state_apc_string_table[];
347 static const struct input_transition input_state_rename_string_table[];
348 static const struct input_transition input_state_consume_st_table[];
350 /* ground state definition. */
351 static const struct input_state input_state_ground = {
352 "ground",
353 input_ground, NULL,
354 input_state_ground_table
357 /* esc_enter state definition. */
358 static const struct input_state input_state_esc_enter = {
359 "esc_enter",
360 input_clear, NULL,
361 input_state_esc_enter_table
364 /* esc_intermediate state definition. */
365 static const struct input_state input_state_esc_intermediate = {
366 "esc_intermediate",
367 NULL, NULL,
368 input_state_esc_intermediate_table
371 /* csi_enter state definition. */
372 static const struct input_state input_state_csi_enter = {
373 "csi_enter",
374 input_clear, NULL,
375 input_state_csi_enter_table
378 /* csi_parameter state definition. */
379 static const struct input_state input_state_csi_parameter = {
380 "csi_parameter",
381 NULL, NULL,
382 input_state_csi_parameter_table
385 /* csi_intermediate state definition. */
386 static const struct input_state input_state_csi_intermediate = {
387 "csi_intermediate",
388 NULL, NULL,
389 input_state_csi_intermediate_table
392 /* csi_ignore state definition. */
393 static const struct input_state input_state_csi_ignore = {
394 "csi_ignore",
395 NULL, NULL,
396 input_state_csi_ignore_table
399 /* dcs_enter state definition. */
400 static const struct input_state input_state_dcs_enter = {
401 "dcs_enter",
402 input_enter_dcs, NULL,
403 input_state_dcs_enter_table
406 /* dcs_parameter state definition. */
407 static const struct input_state input_state_dcs_parameter = {
408 "dcs_parameter",
409 NULL, NULL,
410 input_state_dcs_parameter_table
413 /* dcs_intermediate state definition. */
414 static const struct input_state input_state_dcs_intermediate = {
415 "dcs_intermediate",
416 NULL, NULL,
417 input_state_dcs_intermediate_table
420 /* dcs_handler state definition. */
421 static const struct input_state input_state_dcs_handler = {
422 "dcs_handler",
423 NULL, NULL,
424 input_state_dcs_handler_table
427 /* dcs_escape state definition. */
428 static const struct input_state input_state_dcs_escape = {
429 "dcs_escape",
430 NULL, NULL,
431 input_state_dcs_escape_table
434 /* dcs_ignore state definition. */
435 static const struct input_state input_state_dcs_ignore = {
436 "dcs_ignore",
437 NULL, NULL,
438 input_state_dcs_ignore_table
441 /* osc_string state definition. */
442 static const struct input_state input_state_osc_string = {
443 "osc_string",
444 input_enter_osc, input_exit_osc,
445 input_state_osc_string_table
448 /* apc_string state definition. */
449 static const struct input_state input_state_apc_string = {
450 "apc_string",
451 input_enter_apc, input_exit_apc,
452 input_state_apc_string_table
455 /* rename_string state definition. */
456 static const struct input_state input_state_rename_string = {
457 "rename_string",
458 input_enter_rename, input_exit_rename,
459 input_state_rename_string_table
462 /* consume_st state definition. */
463 static const struct input_state input_state_consume_st = {
464 "consume_st",
465 input_enter_rename, NULL, /* rename also waits for ST */
466 input_state_consume_st_table
469 /* ground state table. */
470 static const struct input_transition input_state_ground_table[] = {
471 INPUT_STATE_ANYWHERE,
473 { 0x00, 0x17, input_c0_dispatch, NULL },
474 { 0x19, 0x19, input_c0_dispatch, NULL },
475 { 0x1c, 0x1f, input_c0_dispatch, NULL },
476 { 0x20, 0x7e, input_print, NULL },
477 { 0x7f, 0x7f, NULL, NULL },
478 { 0x80, 0xff, input_top_bit_set, NULL },
480 { -1, -1, NULL, NULL }
483 /* esc_enter state table. */
484 static const struct input_transition input_state_esc_enter_table[] = {
485 INPUT_STATE_ANYWHERE,
487 { 0x00, 0x17, input_c0_dispatch, NULL },
488 { 0x19, 0x19, input_c0_dispatch, NULL },
489 { 0x1c, 0x1f, input_c0_dispatch, NULL },
490 { 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
491 { 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
492 { 0x50, 0x50, NULL, &input_state_dcs_enter },
493 { 0x51, 0x57, input_esc_dispatch, &input_state_ground },
494 { 0x58, 0x58, NULL, &input_state_consume_st },
495 { 0x59, 0x59, input_esc_dispatch, &input_state_ground },
496 { 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
497 { 0x5b, 0x5b, NULL, &input_state_csi_enter },
498 { 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
499 { 0x5d, 0x5d, NULL, &input_state_osc_string },
500 { 0x5e, 0x5e, NULL, &input_state_consume_st },
501 { 0x5f, 0x5f, NULL, &input_state_apc_string },
502 { 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
503 { 0x6b, 0x6b, NULL, &input_state_rename_string },
504 { 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
505 { 0x7f, 0xff, NULL, NULL },
507 { -1, -1, NULL, NULL }
510 /* esc_intermediate state table. */
511 static const struct input_transition input_state_esc_intermediate_table[] = {
512 INPUT_STATE_ANYWHERE,
514 { 0x00, 0x17, input_c0_dispatch, NULL },
515 { 0x19, 0x19, input_c0_dispatch, NULL },
516 { 0x1c, 0x1f, input_c0_dispatch, NULL },
517 { 0x20, 0x2f, input_intermediate, NULL },
518 { 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
519 { 0x7f, 0xff, NULL, NULL },
521 { -1, -1, NULL, NULL }
524 /* csi_enter state table. */
525 static const struct input_transition input_state_csi_enter_table[] = {
526 INPUT_STATE_ANYWHERE,
528 { 0x00, 0x17, input_c0_dispatch, NULL },
529 { 0x19, 0x19, input_c0_dispatch, NULL },
530 { 0x1c, 0x1f, input_c0_dispatch, NULL },
531 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
532 { 0x30, 0x39, input_parameter, &input_state_csi_parameter },
533 { 0x3a, 0x3a, input_parameter, &input_state_csi_parameter },
534 { 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
535 { 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
536 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
537 { 0x7f, 0xff, NULL, NULL },
539 { -1, -1, NULL, NULL }
542 /* csi_parameter state table. */
543 static const struct input_transition input_state_csi_parameter_table[] = {
544 INPUT_STATE_ANYWHERE,
546 { 0x00, 0x17, input_c0_dispatch, NULL },
547 { 0x19, 0x19, input_c0_dispatch, NULL },
548 { 0x1c, 0x1f, input_c0_dispatch, NULL },
549 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
550 { 0x30, 0x39, input_parameter, NULL },
551 { 0x3a, 0x3a, input_parameter, NULL },
552 { 0x3b, 0x3b, input_parameter, NULL },
553 { 0x3c, 0x3f, NULL, &input_state_csi_ignore },
554 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
555 { 0x7f, 0xff, NULL, NULL },
557 { -1, -1, NULL, NULL }
560 /* csi_intermediate state table. */
561 static const struct input_transition input_state_csi_intermediate_table[] = {
562 INPUT_STATE_ANYWHERE,
564 { 0x00, 0x17, input_c0_dispatch, NULL },
565 { 0x19, 0x19, input_c0_dispatch, NULL },
566 { 0x1c, 0x1f, input_c0_dispatch, NULL },
567 { 0x20, 0x2f, input_intermediate, NULL },
568 { 0x30, 0x3f, NULL, &input_state_csi_ignore },
569 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
570 { 0x7f, 0xff, NULL, NULL },
572 { -1, -1, NULL, NULL }
575 /* csi_ignore state table. */
576 static const struct input_transition input_state_csi_ignore_table[] = {
577 INPUT_STATE_ANYWHERE,
579 { 0x00, 0x17, input_c0_dispatch, NULL },
580 { 0x19, 0x19, input_c0_dispatch, NULL },
581 { 0x1c, 0x1f, input_c0_dispatch, NULL },
582 { 0x20, 0x3f, NULL, NULL },
583 { 0x40, 0x7e, NULL, &input_state_ground },
584 { 0x7f, 0xff, NULL, NULL },
586 { -1, -1, NULL, NULL }
589 /* dcs_enter state table. */
590 static const struct input_transition input_state_dcs_enter_table[] = {
591 INPUT_STATE_ANYWHERE,
593 { 0x00, 0x17, NULL, NULL },
594 { 0x19, 0x19, NULL, NULL },
595 { 0x1c, 0x1f, NULL, NULL },
596 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
597 { 0x30, 0x39, input_parameter, &input_state_dcs_parameter },
598 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
599 { 0x3b, 0x3b, input_parameter, &input_state_dcs_parameter },
600 { 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
601 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
602 { 0x7f, 0xff, NULL, NULL },
604 { -1, -1, NULL, NULL }
607 /* dcs_parameter state table. */
608 static const struct input_transition input_state_dcs_parameter_table[] = {
609 INPUT_STATE_ANYWHERE,
611 { 0x00, 0x17, NULL, NULL },
612 { 0x19, 0x19, NULL, NULL },
613 { 0x1c, 0x1f, NULL, NULL },
614 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
615 { 0x30, 0x39, input_parameter, NULL },
616 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
617 { 0x3b, 0x3b, input_parameter, NULL },
618 { 0x3c, 0x3f, NULL, &input_state_dcs_ignore },
619 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
620 { 0x7f, 0xff, NULL, NULL },
622 { -1, -1, NULL, NULL }
625 /* dcs_intermediate state table. */
626 static const struct input_transition input_state_dcs_intermediate_table[] = {
627 INPUT_STATE_ANYWHERE,
629 { 0x00, 0x17, NULL, NULL },
630 { 0x19, 0x19, NULL, NULL },
631 { 0x1c, 0x1f, NULL, NULL },
632 { 0x20, 0x2f, input_intermediate, NULL },
633 { 0x30, 0x3f, NULL, &input_state_dcs_ignore },
634 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
635 { 0x7f, 0xff, NULL, NULL },
637 { -1, -1, NULL, NULL }
640 /* dcs_handler state table. */
641 static const struct input_transition input_state_dcs_handler_table[] = {
642 /* No INPUT_STATE_ANYWHERE */
644 { 0x00, 0x1a, input_input, NULL },
645 { 0x1b, 0x1b, NULL, &input_state_dcs_escape },
646 { 0x1c, 0xff, input_input, NULL },
648 { -1, -1, NULL, NULL }
651 /* dcs_escape state table. */
652 static const struct input_transition input_state_dcs_escape_table[] = {
653 /* No INPUT_STATE_ANYWHERE */
655 { 0x00, 0x5b, input_input, &input_state_dcs_handler },
656 { 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
657 { 0x5d, 0xff, input_input, &input_state_dcs_handler },
659 { -1, -1, NULL, NULL }
662 /* dcs_ignore state table. */
663 static const struct input_transition input_state_dcs_ignore_table[] = {
664 INPUT_STATE_ANYWHERE,
666 { 0x00, 0x17, NULL, NULL },
667 { 0x19, 0x19, NULL, NULL },
668 { 0x1c, 0x1f, NULL, NULL },
669 { 0x20, 0xff, NULL, NULL },
671 { -1, -1, NULL, NULL }
674 /* osc_string state table. */
675 static const struct input_transition input_state_osc_string_table[] = {
676 INPUT_STATE_ANYWHERE,
678 { 0x00, 0x06, NULL, NULL },
679 { 0x07, 0x07, input_end_bel, &input_state_ground },
680 { 0x08, 0x17, NULL, NULL },
681 { 0x19, 0x19, NULL, NULL },
682 { 0x1c, 0x1f, NULL, NULL },
683 { 0x20, 0xff, input_input, NULL },
685 { -1, -1, NULL, NULL }
688 /* apc_string state table. */
689 static const struct input_transition input_state_apc_string_table[] = {
690 INPUT_STATE_ANYWHERE,
692 { 0x00, 0x17, NULL, NULL },
693 { 0x19, 0x19, NULL, NULL },
694 { 0x1c, 0x1f, NULL, NULL },
695 { 0x20, 0xff, input_input, NULL },
697 { -1, -1, NULL, NULL }
700 /* rename_string state table. */
701 static const struct input_transition input_state_rename_string_table[] = {
702 INPUT_STATE_ANYWHERE,
704 { 0x00, 0x17, NULL, NULL },
705 { 0x19, 0x19, NULL, NULL },
706 { 0x1c, 0x1f, NULL, NULL },
707 { 0x20, 0xff, input_input, NULL },
709 { -1, -1, NULL, NULL }
712 /* consume_st state table. */
713 static const struct input_transition input_state_consume_st_table[] = {
714 INPUT_STATE_ANYWHERE,
716 { 0x00, 0x17, NULL, NULL },
717 { 0x19, 0x19, NULL, NULL },
718 { 0x1c, 0x1f, NULL, NULL },
719 { 0x20, 0xff, NULL, NULL },
721 { -1, -1, NULL, NULL }
724 /* Input table compare. */
725 static int
726 input_table_compare(const void *key, const void *value)
728 const struct input_ctx *ictx = key;
729 const struct input_table_entry *entry = value;
731 if (ictx->ch != entry->ch)
732 return (ictx->ch - entry->ch);
733 return (strcmp(ictx->interm_buf, entry->interm));
737 * Timer - if this expires then have been waiting for a terminator for too
738 * long, so reset to ground.
740 static void
741 input_timer_callback(__unused int fd, __unused short events, void *arg)
743 struct input_ctx *ictx = arg;
745 log_debug("%s: %s expired" , __func__, ictx->state->name);
746 input_reset(ictx, 0);
749 /* Start the timer. */
750 static void
751 input_start_timer(struct input_ctx *ictx)
753 struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
755 event_del(&ictx->timer);
756 event_add(&ictx->timer, &tv);
759 /* Reset cell state to default. */
760 static void
761 input_reset_cell(struct input_ctx *ictx)
763 memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
764 ictx->cell.set = 0;
765 ictx->cell.g0set = ictx->cell.g1set = 0;
767 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
768 ictx->old_cx = 0;
769 ictx->old_cy = 0;
772 /* Save screen state. */
773 static void
774 input_save_state(struct input_ctx *ictx)
776 struct screen_write_ctx *sctx = &ictx->ctx;
777 struct screen *s = sctx->s;
779 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
780 ictx->old_cx = s->cx;
781 ictx->old_cy = s->cy;
782 ictx->old_mode = s->mode;
785 /* Restore screen state. */
786 static void
787 input_restore_state(struct input_ctx *ictx)
789 struct screen_write_ctx *sctx = &ictx->ctx;
791 memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
792 if (ictx->old_mode & MODE_ORIGIN)
793 screen_write_mode_set(sctx, MODE_ORIGIN);
794 else
795 screen_write_mode_clear(sctx, MODE_ORIGIN);
796 screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy, 0);
799 /* Initialise input parser. */
800 struct input_ctx *
801 input_init(struct window_pane *wp, struct bufferevent *bev,
802 struct colour_palette *palette)
804 struct input_ctx *ictx;
806 ictx = xcalloc(1, sizeof *ictx);
807 ictx->wp = wp;
808 ictx->event = bev;
809 ictx->palette = palette;
811 ictx->input_space = INPUT_BUF_START;
812 ictx->input_buf = xmalloc(INPUT_BUF_START);
814 ictx->since_ground = evbuffer_new();
815 if (ictx->since_ground == NULL)
816 fatalx("out of memory");
818 evtimer_set(&ictx->timer, input_timer_callback, ictx);
820 input_reset(ictx, 0);
821 return (ictx);
824 /* Destroy input parser. */
825 void
826 input_free(struct input_ctx *ictx)
828 u_int i;
830 for (i = 0; i < ictx->param_list_len; i++) {
831 if (ictx->param_list[i].type == INPUT_STRING)
832 free(ictx->param_list[i].str);
835 event_del(&ictx->timer);
837 free(ictx->input_buf);
838 evbuffer_free(ictx->since_ground);
840 free(ictx);
843 /* Reset input state and clear screen. */
844 void
845 input_reset(struct input_ctx *ictx, int clear)
847 struct screen_write_ctx *sctx = &ictx->ctx;
848 struct window_pane *wp = ictx->wp;
850 input_reset_cell(ictx);
852 if (clear && wp != NULL) {
853 if (TAILQ_EMPTY(&wp->modes))
854 screen_write_start_pane(sctx, wp, &wp->base);
855 else
856 screen_write_start(sctx, &wp->base);
857 screen_write_reset(sctx);
858 screen_write_stop(sctx);
861 input_clear(ictx);
863 ictx->last = -1;
865 ictx->state = &input_state_ground;
866 ictx->flags = 0;
869 /* Return pending data. */
870 struct evbuffer *
871 input_pending(struct input_ctx *ictx)
873 return (ictx->since_ground);
876 /* Change input state. */
877 static void
878 input_set_state(struct input_ctx *ictx, const struct input_transition *itr)
880 if (ictx->state->exit != NULL)
881 ictx->state->exit(ictx);
882 ictx->state = itr->state;
883 if (ictx->state->enter != NULL)
884 ictx->state->enter(ictx);
887 /* Parse data. */
888 static void
889 input_parse(struct input_ctx *ictx, u_char *buf, size_t len)
891 struct screen_write_ctx *sctx = &ictx->ctx;
892 const struct input_state *state = NULL;
893 const struct input_transition *itr = NULL;
894 size_t off = 0;
896 /* Parse the input. */
897 while (off < len) {
898 ictx->ch = buf[off++];
900 /* Find the transition. */
901 if (ictx->state != state ||
902 itr == NULL ||
903 ictx->ch < itr->first ||
904 ictx->ch > itr->last) {
905 itr = ictx->state->transitions;
906 while (itr->first != -1 && itr->last != -1) {
907 if (ictx->ch >= itr->first &&
908 ictx->ch <= itr->last)
909 break;
910 itr++;
912 if (itr->first == -1 || itr->last == -1) {
913 /* No transition? Eh? */
914 fatalx("no transition from state");
917 state = ictx->state;
920 * Any state except print stops the current collection. This is
921 * an optimization to avoid checking if the attributes have
922 * changed for every character. It will stop unnecessarily for
923 * sequences that don't make a terminal change, but they should
924 * be the minority.
926 if (itr->handler != input_print)
927 screen_write_collect_end(sctx);
930 * Execute the handler, if any. Don't switch state if it
931 * returns non-zero.
933 if (itr->handler != NULL && itr->handler(ictx) != 0)
934 continue;
936 /* And switch state, if necessary. */
937 if (itr->state != NULL)
938 input_set_state(ictx, itr);
940 /* If not in ground state, save input. */
941 if (ictx->state != &input_state_ground)
942 evbuffer_add(ictx->since_ground, &ictx->ch, 1);
946 /* Parse input from pane. */
947 void
948 input_parse_pane(struct window_pane *wp)
950 void *new_data;
951 size_t new_size;
953 new_data = window_pane_get_new_data(wp, &wp->offset, &new_size);
954 input_parse_buffer(wp, new_data, new_size);
955 window_pane_update_used_data(wp, &wp->offset, new_size);
958 /* Parse given input. */
959 void
960 input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
962 struct input_ctx *ictx = wp->ictx;
963 struct screen_write_ctx *sctx = &ictx->ctx;
965 if (len == 0)
966 return;
968 window_update_activity(wp->window);
969 wp->flags |= PANE_CHANGED;
971 /* NULL wp if there is a mode set as don't want to update the tty. */
972 if (TAILQ_EMPTY(&wp->modes))
973 screen_write_start_pane(sctx, wp, &wp->base);
974 else
975 screen_write_start(sctx, &wp->base);
977 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
978 ictx->state->name, len, (int)len, buf);
980 input_parse(ictx, buf, len);
981 screen_write_stop(sctx);
984 /* Parse given input for screen. */
985 void
986 input_parse_screen(struct input_ctx *ictx, struct screen *s,
987 screen_write_init_ctx_cb cb, void *arg, u_char *buf, size_t len)
989 struct screen_write_ctx *sctx = &ictx->ctx;
991 if (len == 0)
992 return;
994 screen_write_start_callback(sctx, s, cb, arg);
995 input_parse(ictx, buf, len);
996 screen_write_stop(sctx);
999 /* Split the parameter list (if any). */
1000 static int
1001 input_split(struct input_ctx *ictx)
1003 const char *errstr;
1004 char *ptr, *out;
1005 struct input_param *ip;
1006 u_int i;
1008 for (i = 0; i < ictx->param_list_len; i++) {
1009 if (ictx->param_list[i].type == INPUT_STRING)
1010 free(ictx->param_list[i].str);
1012 ictx->param_list_len = 0;
1014 if (ictx->param_len == 0)
1015 return (0);
1016 ip = &ictx->param_list[0];
1018 ptr = ictx->param_buf;
1019 while ((out = strsep(&ptr, ";")) != NULL) {
1020 if (*out == '\0')
1021 ip->type = INPUT_MISSING;
1022 else {
1023 if (strchr(out, ':') != NULL) {
1024 ip->type = INPUT_STRING;
1025 ip->str = xstrdup(out);
1026 } else {
1027 ip->type = INPUT_NUMBER;
1028 ip->num = strtonum(out, 0, INT_MAX, &errstr);
1029 if (errstr != NULL)
1030 return (-1);
1033 ip = &ictx->param_list[++ictx->param_list_len];
1034 if (ictx->param_list_len == nitems(ictx->param_list))
1035 return (-1);
1038 for (i = 0; i < ictx->param_list_len; i++) {
1039 ip = &ictx->param_list[i];
1040 if (ip->type == INPUT_MISSING)
1041 log_debug("parameter %u: missing", i);
1042 else if (ip->type == INPUT_STRING)
1043 log_debug("parameter %u: string %s", i, ip->str);
1044 else if (ip->type == INPUT_NUMBER)
1045 log_debug("parameter %u: number %d", i, ip->num);
1048 return (0);
1051 /* Get an argument or return default value. */
1052 static int
1053 input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1055 struct input_param *ip;
1056 int retval;
1058 if (validx >= ictx->param_list_len)
1059 return (defval);
1060 ip = &ictx->param_list[validx];
1061 if (ip->type == INPUT_MISSING)
1062 return (defval);
1063 if (ip->type == INPUT_STRING)
1064 return (-1);
1065 retval = ip->num;
1066 if (retval < minval)
1067 return (minval);
1068 return (retval);
1071 /* Reply to terminal query. */
1072 static void
1073 input_reply(struct input_ctx *ictx, const char *fmt, ...)
1075 struct bufferevent *bev = ictx->event;
1076 va_list ap;
1077 char *reply;
1079 va_start(ap, fmt);
1080 xvasprintf(&reply, fmt, ap);
1081 va_end(ap);
1083 bufferevent_write(bev, reply, strlen(reply));
1084 free(reply);
1087 /* Clear saved state. */
1088 static void
1089 input_clear(struct input_ctx *ictx)
1091 event_del(&ictx->timer);
1093 *ictx->interm_buf = '\0';
1094 ictx->interm_len = 0;
1096 *ictx->param_buf = '\0';
1097 ictx->param_len = 0;
1099 *ictx->input_buf = '\0';
1100 ictx->input_len = 0;
1102 ictx->input_end = INPUT_END_ST;
1104 ictx->flags &= ~INPUT_DISCARD;
1107 /* Reset for ground state. */
1108 static void
1109 input_ground(struct input_ctx *ictx)
1111 event_del(&ictx->timer);
1112 evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
1114 if (ictx->input_space > INPUT_BUF_START) {
1115 ictx->input_space = INPUT_BUF_START;
1116 ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1120 /* Output this character to the screen. */
1121 static int
1122 input_print(struct input_ctx *ictx)
1124 struct screen_write_ctx *sctx = &ictx->ctx;
1125 int set;
1127 ictx->utf8started = 0; /* can't be valid UTF-8 */
1129 set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1130 if (set == 1)
1131 ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1132 else
1133 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1135 utf8_set(&ictx->cell.cell.data, ictx->ch);
1136 screen_write_collect_add(sctx, &ictx->cell.cell);
1137 ictx->last = ictx->ch;
1139 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1141 return (0);
1144 /* Collect intermediate string. */
1145 static int
1146 input_intermediate(struct input_ctx *ictx)
1148 if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
1149 ictx->flags |= INPUT_DISCARD;
1150 else {
1151 ictx->interm_buf[ictx->interm_len++] = ictx->ch;
1152 ictx->interm_buf[ictx->interm_len] = '\0';
1155 return (0);
1158 /* Collect parameter string. */
1159 static int
1160 input_parameter(struct input_ctx *ictx)
1162 if (ictx->param_len == (sizeof ictx->param_buf) - 1)
1163 ictx->flags |= INPUT_DISCARD;
1164 else {
1165 ictx->param_buf[ictx->param_len++] = ictx->ch;
1166 ictx->param_buf[ictx->param_len] = '\0';
1169 return (0);
1172 /* Collect input string. */
1173 static int
1174 input_input(struct input_ctx *ictx)
1176 size_t available;
1178 available = ictx->input_space;
1179 while (ictx->input_len + 1 >= available) {
1180 available *= 2;
1181 if (available > INPUT_BUF_LIMIT) {
1182 ictx->flags |= INPUT_DISCARD;
1183 return (0);
1185 ictx->input_buf = xrealloc(ictx->input_buf, available);
1186 ictx->input_space = available;
1188 ictx->input_buf[ictx->input_len++] = ictx->ch;
1189 ictx->input_buf[ictx->input_len] = '\0';
1191 return (0);
1194 /* Execute C0 control sequence. */
1195 static int
1196 input_c0_dispatch(struct input_ctx *ictx)
1198 struct screen_write_ctx *sctx = &ictx->ctx;
1199 struct window_pane *wp = ictx->wp;
1200 struct screen *s = sctx->s;
1202 ictx->utf8started = 0; /* can't be valid UTF-8 */
1204 log_debug("%s: '%c'", __func__, ictx->ch);
1206 switch (ictx->ch) {
1207 case '\000': /* NUL */
1208 break;
1209 case '\007': /* BEL */
1210 if (wp != NULL)
1211 alerts_queue(wp->window, WINDOW_BELL);
1212 break;
1213 case '\010': /* BS */
1214 screen_write_backspace(sctx);
1215 break;
1216 case '\011': /* HT */
1217 /* Don't tab beyond the end of the line. */
1218 if (s->cx >= screen_size_x(s) - 1)
1219 break;
1221 /* Find the next tab point, or use the last column if none. */
1222 do {
1223 s->cx++;
1224 if (bit_test(s->tabs, s->cx))
1225 break;
1226 } while (s->cx < screen_size_x(s) - 1);
1227 break;
1228 case '\012': /* LF */
1229 case '\013': /* VT */
1230 case '\014': /* FF */
1231 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1232 if (s->mode & MODE_CRLF)
1233 screen_write_carriagereturn(sctx);
1234 break;
1235 case '\015': /* CR */
1236 screen_write_carriagereturn(sctx);
1237 break;
1238 case '\016': /* SO */
1239 ictx->cell.set = 1;
1240 break;
1241 case '\017': /* SI */
1242 ictx->cell.set = 0;
1243 break;
1244 default:
1245 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1246 break;
1249 ictx->last = -1;
1250 return (0);
1253 /* Execute escape sequence. */
1254 static int
1255 input_esc_dispatch(struct input_ctx *ictx)
1257 struct screen_write_ctx *sctx = &ictx->ctx;
1258 struct screen *s = sctx->s;
1259 struct input_table_entry *entry;
1261 if (ictx->flags & INPUT_DISCARD)
1262 return (0);
1263 log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1265 entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
1266 sizeof input_esc_table[0], input_table_compare);
1267 if (entry == NULL) {
1268 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1269 return (0);
1272 switch (entry->type) {
1273 case INPUT_ESC_RIS:
1274 colour_palette_clear(ictx->palette);
1275 input_reset_cell(ictx);
1276 screen_write_reset(sctx);
1277 screen_write_fullredraw(sctx);
1278 break;
1279 case INPUT_ESC_IND:
1280 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1281 break;
1282 case INPUT_ESC_NEL:
1283 screen_write_carriagereturn(sctx);
1284 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1285 break;
1286 case INPUT_ESC_HTS:
1287 if (s->cx < screen_size_x(s))
1288 bit_set(s->tabs, s->cx);
1289 break;
1290 case INPUT_ESC_RI:
1291 screen_write_reverseindex(sctx, ictx->cell.cell.bg);
1292 break;
1293 case INPUT_ESC_DECKPAM:
1294 screen_write_mode_set(sctx, MODE_KKEYPAD);
1295 break;
1296 case INPUT_ESC_DECKPNM:
1297 screen_write_mode_clear(sctx, MODE_KKEYPAD);
1298 break;
1299 case INPUT_ESC_DECSC:
1300 input_save_state(ictx);
1301 break;
1302 case INPUT_ESC_DECRC:
1303 input_restore_state(ictx);
1304 break;
1305 case INPUT_ESC_DECALN:
1306 screen_write_alignmenttest(sctx);
1307 break;
1308 case INPUT_ESC_SCSG0_ON:
1309 ictx->cell.g0set = 1;
1310 break;
1311 case INPUT_ESC_SCSG0_OFF:
1312 ictx->cell.g0set = 0;
1313 break;
1314 case INPUT_ESC_SCSG1_ON:
1315 ictx->cell.g1set = 1;
1316 break;
1317 case INPUT_ESC_SCSG1_OFF:
1318 ictx->cell.g1set = 0;
1319 break;
1320 case INPUT_ESC_ST:
1321 /* ST terminates OSC but the state transition already did it. */
1322 break;
1325 ictx->last = -1;
1326 return (0);
1329 /* Execute control sequence. */
1330 static int
1331 input_csi_dispatch(struct input_ctx *ictx)
1333 struct screen_write_ctx *sctx = &ictx->ctx;
1334 struct screen *s = sctx->s;
1335 struct input_table_entry *entry;
1336 int i, n, m;
1337 u_int cx, bg = ictx->cell.cell.bg;
1339 if (ictx->flags & INPUT_DISCARD)
1340 return (0);
1342 log_debug("%s: '%c' \"%s\" \"%s\"",
1343 __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
1345 if (input_split(ictx) != 0)
1346 return (0);
1348 entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
1349 sizeof input_csi_table[0], input_table_compare);
1350 if (entry == NULL) {
1351 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1352 return (0);
1355 switch (entry->type) {
1356 case INPUT_CSI_CBT:
1357 /* Find the previous tab point, n times. */
1358 cx = s->cx;
1359 if (cx > screen_size_x(s) - 1)
1360 cx = screen_size_x(s) - 1;
1361 n = input_get(ictx, 0, 1, 1);
1362 if (n == -1)
1363 break;
1364 while (cx > 0 && n-- > 0) {
1366 cx--;
1367 while (cx > 0 && !bit_test(s->tabs, cx));
1369 s->cx = cx;
1370 break;
1371 case INPUT_CSI_CUB:
1372 n = input_get(ictx, 0, 1, 1);
1373 if (n != -1)
1374 screen_write_cursorleft(sctx, n);
1375 break;
1376 case INPUT_CSI_CUD:
1377 n = input_get(ictx, 0, 1, 1);
1378 if (n != -1)
1379 screen_write_cursordown(sctx, n);
1380 break;
1381 case INPUT_CSI_CUF:
1382 n = input_get(ictx, 0, 1, 1);
1383 if (n != -1)
1384 screen_write_cursorright(sctx, n);
1385 break;
1386 case INPUT_CSI_CUP:
1387 n = input_get(ictx, 0, 1, 1);
1388 m = input_get(ictx, 1, 1, 1);
1389 if (n != -1 && m != -1)
1390 screen_write_cursormove(sctx, m - 1, n - 1, 1);
1391 break;
1392 case INPUT_CSI_MODSET:
1393 n = input_get(ictx, 0, 0, 0);
1394 m = input_get(ictx, 1, 0, 0);
1395 if (options_get_number(global_options, "extended-keys") == 2)
1396 break;
1397 if (n == 0 || (n == 4 && m == 0))
1398 screen_write_mode_clear(sctx, MODE_KEXTENDED);
1399 else if (n == 4 && (m == 1 || m == 2))
1400 screen_write_mode_set(sctx, MODE_KEXTENDED);
1401 break;
1402 case INPUT_CSI_MODOFF:
1403 n = input_get(ictx, 0, 0, 0);
1404 if (n == 4)
1405 screen_write_mode_clear(sctx, MODE_KEXTENDED);
1406 break;
1407 case INPUT_CSI_WINOPS:
1408 input_csi_dispatch_winops(ictx);
1409 break;
1410 case INPUT_CSI_CUU:
1411 n = input_get(ictx, 0, 1, 1);
1412 if (n != -1)
1413 screen_write_cursorup(sctx, n);
1414 break;
1415 case INPUT_CSI_CNL:
1416 n = input_get(ictx, 0, 1, 1);
1417 if (n != -1) {
1418 screen_write_carriagereturn(sctx);
1419 screen_write_cursordown(sctx, n);
1421 break;
1422 case INPUT_CSI_CPL:
1423 n = input_get(ictx, 0, 1, 1);
1424 if (n != -1) {
1425 screen_write_carriagereturn(sctx);
1426 screen_write_cursorup(sctx, n);
1428 break;
1429 case INPUT_CSI_DA:
1430 switch (input_get(ictx, 0, 0, 0)) {
1431 case -1:
1432 break;
1433 case 0:
1434 input_reply(ictx, "\033[?1;2c");
1435 break;
1436 default:
1437 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1438 break;
1440 break;
1441 case INPUT_CSI_DA_TWO:
1442 switch (input_get(ictx, 0, 0, 0)) {
1443 case -1:
1444 break;
1445 case 0:
1446 input_reply(ictx, "\033[>84;0;0c");
1447 break;
1448 default:
1449 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1450 break;
1452 break;
1453 case INPUT_CSI_ECH:
1454 n = input_get(ictx, 0, 1, 1);
1455 if (n != -1)
1456 screen_write_clearcharacter(sctx, n, bg);
1457 break;
1458 case INPUT_CSI_DCH:
1459 n = input_get(ictx, 0, 1, 1);
1460 if (n != -1)
1461 screen_write_deletecharacter(sctx, n, bg);
1462 break;
1463 case INPUT_CSI_DECSTBM:
1464 n = input_get(ictx, 0, 1, 1);
1465 m = input_get(ictx, 1, 1, screen_size_y(s));
1466 if (n != -1 && m != -1)
1467 screen_write_scrollregion(sctx, n - 1, m - 1);
1468 break;
1469 case INPUT_CSI_DL:
1470 n = input_get(ictx, 0, 1, 1);
1471 if (n != -1)
1472 screen_write_deleteline(sctx, n, bg);
1473 break;
1474 case INPUT_CSI_DSR:
1475 switch (input_get(ictx, 0, 0, 0)) {
1476 case -1:
1477 break;
1478 case 5:
1479 input_reply(ictx, "\033[0n");
1480 break;
1481 case 6:
1482 input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1483 break;
1484 default:
1485 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1486 break;
1488 break;
1489 case INPUT_CSI_ED:
1490 switch (input_get(ictx, 0, 0, 0)) {
1491 case -1:
1492 break;
1493 case 0:
1494 screen_write_clearendofscreen(sctx, bg);
1495 break;
1496 case 1:
1497 screen_write_clearstartofscreen(sctx, bg);
1498 break;
1499 case 2:
1500 screen_write_clearscreen(sctx, bg);
1501 break;
1502 case 3:
1503 if (input_get(ictx, 1, 0, 0) == 0) {
1505 * Linux console extension to clear history
1506 * (for example before locking the screen).
1508 screen_write_clearhistory(sctx);
1510 break;
1511 default:
1512 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1513 break;
1515 break;
1516 case INPUT_CSI_EL:
1517 switch (input_get(ictx, 0, 0, 0)) {
1518 case -1:
1519 break;
1520 case 0:
1521 screen_write_clearendofline(sctx, bg);
1522 break;
1523 case 1:
1524 screen_write_clearstartofline(sctx, bg);
1525 break;
1526 case 2:
1527 screen_write_clearline(sctx, bg);
1528 break;
1529 default:
1530 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1531 break;
1533 break;
1534 case INPUT_CSI_HPA:
1535 n = input_get(ictx, 0, 1, 1);
1536 if (n != -1)
1537 screen_write_cursormove(sctx, n - 1, -1, 1);
1538 break;
1539 case INPUT_CSI_ICH:
1540 n = input_get(ictx, 0, 1, 1);
1541 if (n != -1)
1542 screen_write_insertcharacter(sctx, n, bg);
1543 break;
1544 case INPUT_CSI_IL:
1545 n = input_get(ictx, 0, 1, 1);
1546 if (n != -1)
1547 screen_write_insertline(sctx, n, bg);
1548 break;
1549 case INPUT_CSI_REP:
1550 n = input_get(ictx, 0, 1, 1);
1551 if (n == -1)
1552 break;
1554 m = screen_size_x(s) - s->cx;
1555 if (n > m)
1556 n = m;
1558 if (ictx->last == -1)
1559 break;
1560 ictx->ch = ictx->last;
1562 for (i = 0; i < n; i++)
1563 input_print(ictx);
1564 break;
1565 case INPUT_CSI_RCP:
1566 input_restore_state(ictx);
1567 break;
1568 case INPUT_CSI_RM:
1569 input_csi_dispatch_rm(ictx);
1570 break;
1571 case INPUT_CSI_RM_PRIVATE:
1572 input_csi_dispatch_rm_private(ictx);
1573 break;
1574 case INPUT_CSI_SCP:
1575 input_save_state(ictx);
1576 break;
1577 case INPUT_CSI_SGR:
1578 input_csi_dispatch_sgr(ictx);
1579 break;
1580 case INPUT_CSI_SM:
1581 input_csi_dispatch_sm(ictx);
1582 break;
1583 case INPUT_CSI_SM_PRIVATE:
1584 input_csi_dispatch_sm_private(ictx);
1585 break;
1586 case INPUT_CSI_SU:
1587 n = input_get(ictx, 0, 1, 1);
1588 if (n != -1)
1589 screen_write_scrollup(sctx, n, bg);
1590 break;
1591 case INPUT_CSI_SD:
1592 n = input_get(ictx, 0, 1, 1);
1593 if (n != -1)
1594 screen_write_scrolldown(sctx, n, bg);
1595 break;
1596 case INPUT_CSI_TBC:
1597 switch (input_get(ictx, 0, 0, 0)) {
1598 case -1:
1599 break;
1600 case 0:
1601 if (s->cx < screen_size_x(s))
1602 bit_clear(s->tabs, s->cx);
1603 break;
1604 case 3:
1605 bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1606 break;
1607 default:
1608 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1609 break;
1611 break;
1612 case INPUT_CSI_VPA:
1613 n = input_get(ictx, 0, 1, 1);
1614 if (n != -1)
1615 screen_write_cursormove(sctx, -1, n - 1, 1);
1616 break;
1617 case INPUT_CSI_DECSCUSR:
1618 n = input_get(ictx, 0, 0, 0);
1619 if (n != -1)
1620 screen_set_cursor_style(s, n);
1621 break;
1622 case INPUT_CSI_XDA:
1623 n = input_get(ictx, 0, 0, 0);
1624 if (n == 0)
1625 input_reply(ictx, "\033P>|tmux %s\033\\", getversion());
1626 break;
1630 ictx->last = -1;
1631 return (0);
1634 /* Handle CSI RM. */
1635 static void
1636 input_csi_dispatch_rm(struct input_ctx *ictx)
1638 struct screen_write_ctx *sctx = &ictx->ctx;
1639 u_int i;
1641 for (i = 0; i < ictx->param_list_len; i++) {
1642 switch (input_get(ictx, i, 0, -1)) {
1643 case -1:
1644 break;
1645 case 4: /* IRM */
1646 screen_write_mode_clear(sctx, MODE_INSERT);
1647 break;
1648 case 34:
1649 screen_write_mode_set(sctx, MODE_CURSOR_VERY_VISIBLE);
1650 break;
1651 default:
1652 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1653 break;
1658 /* Handle CSI private RM. */
1659 static void
1660 input_csi_dispatch_rm_private(struct input_ctx *ictx)
1662 struct screen_write_ctx *sctx = &ictx->ctx;
1663 struct grid_cell *gc = &ictx->cell.cell;
1664 u_int i;
1666 for (i = 0; i < ictx->param_list_len; i++) {
1667 switch (input_get(ictx, i, 0, -1)) {
1668 case -1:
1669 break;
1670 case 1: /* DECCKM */
1671 screen_write_mode_clear(sctx, MODE_KCURSOR);
1672 break;
1673 case 3: /* DECCOLM */
1674 screen_write_cursormove(sctx, 0, 0, 1);
1675 screen_write_clearscreen(sctx, gc->bg);
1676 break;
1677 case 6: /* DECOM */
1678 screen_write_mode_clear(sctx, MODE_ORIGIN);
1679 screen_write_cursormove(sctx, 0, 0, 1);
1680 break;
1681 case 7: /* DECAWM */
1682 screen_write_mode_clear(sctx, MODE_WRAP);
1683 break;
1684 case 12:
1685 screen_write_mode_clear(sctx, MODE_CURSOR_BLINKING);
1686 break;
1687 case 25: /* TCEM */
1688 screen_write_mode_clear(sctx, MODE_CURSOR);
1689 break;
1690 case 1000:
1691 case 1001:
1692 case 1002:
1693 case 1003:
1694 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1695 break;
1696 case 1004:
1697 screen_write_mode_clear(sctx, MODE_FOCUSON);
1698 break;
1699 case 1005:
1700 screen_write_mode_clear(sctx, MODE_MOUSE_UTF8);
1701 break;
1702 case 1006:
1703 screen_write_mode_clear(sctx, MODE_MOUSE_SGR);
1704 break;
1705 case 47:
1706 case 1047:
1707 screen_write_alternateoff(sctx, gc, 0);
1708 break;
1709 case 1049:
1710 screen_write_alternateoff(sctx, gc, 1);
1711 break;
1712 case 2004:
1713 screen_write_mode_clear(sctx, MODE_BRACKETPASTE);
1714 break;
1715 default:
1716 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1717 break;
1722 /* Handle CSI SM. */
1723 static void
1724 input_csi_dispatch_sm(struct input_ctx *ictx)
1726 struct screen_write_ctx *sctx = &ictx->ctx;
1727 u_int i;
1729 for (i = 0; i < ictx->param_list_len; i++) {
1730 switch (input_get(ictx, i, 0, -1)) {
1731 case -1:
1732 break;
1733 case 4: /* IRM */
1734 screen_write_mode_set(sctx, MODE_INSERT);
1735 break;
1736 case 34:
1737 screen_write_mode_clear(sctx, MODE_CURSOR_VERY_VISIBLE);
1738 break;
1739 default:
1740 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1741 break;
1746 /* Handle CSI private SM. */
1747 static void
1748 input_csi_dispatch_sm_private(struct input_ctx *ictx)
1750 struct screen_write_ctx *sctx = &ictx->ctx;
1751 struct window_pane *wp = ictx->wp;
1752 struct grid_cell *gc = &ictx->cell.cell;
1753 u_int i;
1755 for (i = 0; i < ictx->param_list_len; i++) {
1756 switch (input_get(ictx, i, 0, -1)) {
1757 case -1:
1758 break;
1759 case 1: /* DECCKM */
1760 screen_write_mode_set(sctx, MODE_KCURSOR);
1761 break;
1762 case 3: /* DECCOLM */
1763 screen_write_cursormove(sctx, 0, 0, 1);
1764 screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1765 break;
1766 case 6: /* DECOM */
1767 screen_write_mode_set(sctx, MODE_ORIGIN);
1768 screen_write_cursormove(sctx, 0, 0, 1);
1769 break;
1770 case 7: /* DECAWM */
1771 screen_write_mode_set(sctx, MODE_WRAP);
1772 break;
1773 case 12:
1774 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING);
1775 break;
1776 case 25: /* TCEM */
1777 screen_write_mode_set(sctx, MODE_CURSOR);
1778 break;
1779 case 1000:
1780 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1781 screen_write_mode_set(sctx, MODE_MOUSE_STANDARD);
1782 break;
1783 case 1002:
1784 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1785 screen_write_mode_set(sctx, MODE_MOUSE_BUTTON);
1786 break;
1787 case 1003:
1788 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1789 screen_write_mode_set(sctx, MODE_MOUSE_ALL);
1790 break;
1791 case 1004:
1792 if (sctx->s->mode & MODE_FOCUSON)
1793 break;
1794 screen_write_mode_set(sctx, MODE_FOCUSON);
1795 if (wp == NULL)
1796 break;
1797 if (wp->flags & PANE_FOCUSED)
1798 bufferevent_write(wp->event, "\033[I", 3);
1799 else
1800 bufferevent_write(wp->event, "\033[O", 3);
1801 break;
1802 case 1005:
1803 screen_write_mode_set(sctx, MODE_MOUSE_UTF8);
1804 break;
1805 case 1006:
1806 screen_write_mode_set(sctx, MODE_MOUSE_SGR);
1807 break;
1808 case 47:
1809 case 1047:
1810 screen_write_alternateon(sctx, gc, 0);
1811 break;
1812 case 1049:
1813 screen_write_alternateon(sctx, gc, 1);
1814 break;
1815 case 2004:
1816 screen_write_mode_set(sctx, MODE_BRACKETPASTE);
1817 break;
1818 default:
1819 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1820 break;
1825 /* Handle CSI window operations. */
1826 static void
1827 input_csi_dispatch_winops(struct input_ctx *ictx)
1829 struct screen_write_ctx *sctx = &ictx->ctx;
1830 struct screen *s = sctx->s;
1831 struct window_pane *wp = ictx->wp;
1832 u_int x = screen_size_x(s), y = screen_size_y(s);
1833 int n, m;
1835 m = 0;
1836 while ((n = input_get(ictx, m, 0, -1)) != -1) {
1837 switch (n) {
1838 case 1:
1839 case 2:
1840 case 5:
1841 case 6:
1842 case 7:
1843 case 11:
1844 case 13:
1845 case 14:
1846 case 19:
1847 case 20:
1848 case 21:
1849 case 24:
1850 break;
1851 case 3:
1852 case 4:
1853 case 8:
1854 m++;
1855 if (input_get(ictx, m, 0, -1) == -1)
1856 return;
1857 /* FALLTHROUGH */
1858 case 9:
1859 case 10:
1860 m++;
1861 if (input_get(ictx, m, 0, -1) == -1)
1862 return;
1863 break;
1864 case 22:
1865 m++;
1866 switch (input_get(ictx, m, 0, -1)) {
1867 case -1:
1868 return;
1869 case 0:
1870 case 2:
1871 screen_push_title(sctx->s);
1872 break;
1874 break;
1875 case 23:
1876 m++;
1877 switch (input_get(ictx, m, 0, -1)) {
1878 case -1:
1879 return;
1880 case 0:
1881 case 2:
1882 screen_pop_title(sctx->s);
1883 if (wp == NULL)
1884 break;
1885 notify_pane("pane-title-changed", wp);
1886 server_redraw_window_borders(wp->window);
1887 server_status_window(wp->window);
1888 break;
1890 break;
1891 case 18:
1892 input_reply(ictx, "\033[8;%u;%ut", x, y);
1893 break;
1894 default:
1895 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1896 break;
1898 m++;
1902 /* Helper for 256 colour SGR. */
1903 static int
1904 input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
1906 struct grid_cell *gc = &ictx->cell.cell;
1908 if (c == -1 || c > 255) {
1909 if (fgbg == 38)
1910 gc->fg = 8;
1911 else if (fgbg == 48)
1912 gc->bg = 8;
1913 } else {
1914 if (fgbg == 38)
1915 gc->fg = c | COLOUR_FLAG_256;
1916 else if (fgbg == 48)
1917 gc->bg = c | COLOUR_FLAG_256;
1918 else if (fgbg == 58)
1919 gc->us = c | COLOUR_FLAG_256;
1921 return (1);
1924 /* Handle CSI SGR for 256 colours. */
1925 static void
1926 input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
1928 int c;
1930 c = input_get(ictx, (*i) + 1, 0, -1);
1931 if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
1932 (*i)++;
1935 /* Helper for RGB colour SGR. */
1936 static int
1937 input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
1938 int b)
1940 struct grid_cell *gc = &ictx->cell.cell;
1942 if (r == -1 || r > 255)
1943 return (0);
1944 if (g == -1 || g > 255)
1945 return (0);
1946 if (b == -1 || b > 255)
1947 return (0);
1949 if (fgbg == 38)
1950 gc->fg = colour_join_rgb(r, g, b);
1951 else if (fgbg == 48)
1952 gc->bg = colour_join_rgb(r, g, b);
1953 else if (fgbg == 58)
1954 gc->us = colour_join_rgb(r, g, b);
1955 return (1);
1958 /* Handle CSI SGR for RGB colours. */
1959 static void
1960 input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
1962 int r, g, b;
1964 r = input_get(ictx, (*i) + 1, 0, -1);
1965 g = input_get(ictx, (*i) + 2, 0, -1);
1966 b = input_get(ictx, (*i) + 3, 0, -1);
1967 if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
1968 (*i) += 3;
1971 /* Handle CSI SGR with a ISO parameter. */
1972 static void
1973 input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
1975 struct grid_cell *gc = &ictx->cell.cell;
1976 char *s = ictx->param_list[i].str, *copy, *ptr, *out;
1977 int p[8];
1978 u_int n;
1979 const char *errstr;
1981 for (n = 0; n < nitems(p); n++)
1982 p[n] = -1;
1983 n = 0;
1985 ptr = copy = xstrdup(s);
1986 while ((out = strsep(&ptr, ":")) != NULL) {
1987 if (*out != '\0') {
1988 p[n++] = strtonum(out, 0, INT_MAX, &errstr);
1989 if (errstr != NULL || n == nitems(p)) {
1990 free(copy);
1991 return;
1993 } else {
1994 n++;
1995 if (n == nitems(p)) {
1996 free(copy);
1997 return;
2000 log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
2002 free(copy);
2004 if (n == 0)
2005 return;
2006 if (p[0] == 4) {
2007 if (n != 2)
2008 return;
2009 switch (p[1]) {
2010 case 0:
2011 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2012 break;
2013 case 1:
2014 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2015 gc->attr |= GRID_ATTR_UNDERSCORE;
2016 break;
2017 case 2:
2018 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2019 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2020 break;
2021 case 3:
2022 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2023 gc->attr |= GRID_ATTR_UNDERSCORE_3;
2024 break;
2025 case 4:
2026 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2027 gc->attr |= GRID_ATTR_UNDERSCORE_4;
2028 break;
2029 case 5:
2030 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2031 gc->attr |= GRID_ATTR_UNDERSCORE_5;
2032 break;
2034 return;
2036 if (n < 2 || (p[0] != 38 && p[0] != 48 && p[0] != 58))
2037 return;
2038 switch (p[1]) {
2039 case 2:
2040 if (n < 3)
2041 break;
2042 if (n == 5)
2043 i = 2;
2044 else
2045 i = 3;
2046 if (n < i + 3)
2047 break;
2048 input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
2049 p[i + 2]);
2050 break;
2051 case 5:
2052 if (n < 3)
2053 break;
2054 input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
2055 break;
2059 /* Handle CSI SGR. */
2060 static void
2061 input_csi_dispatch_sgr(struct input_ctx *ictx)
2063 struct grid_cell *gc = &ictx->cell.cell;
2064 u_int i;
2065 int n;
2067 if (ictx->param_list_len == 0) {
2068 memcpy(gc, &grid_default_cell, sizeof *gc);
2069 return;
2072 for (i = 0; i < ictx->param_list_len; i++) {
2073 if (ictx->param_list[i].type == INPUT_STRING) {
2074 input_csi_dispatch_sgr_colon(ictx, i);
2075 continue;
2077 n = input_get(ictx, i, 0, 0);
2078 if (n == -1)
2079 continue;
2081 if (n == 38 || n == 48 || n == 58) {
2082 i++;
2083 switch (input_get(ictx, i, 0, -1)) {
2084 case 2:
2085 input_csi_dispatch_sgr_rgb(ictx, n, &i);
2086 break;
2087 case 5:
2088 input_csi_dispatch_sgr_256(ictx, n, &i);
2089 break;
2091 continue;
2094 switch (n) {
2095 case 0:
2096 memcpy(gc, &grid_default_cell, sizeof *gc);
2097 break;
2098 case 1:
2099 gc->attr |= GRID_ATTR_BRIGHT;
2100 break;
2101 case 2:
2102 gc->attr |= GRID_ATTR_DIM;
2103 break;
2104 case 3:
2105 gc->attr |= GRID_ATTR_ITALICS;
2106 break;
2107 case 4:
2108 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2109 gc->attr |= GRID_ATTR_UNDERSCORE;
2110 break;
2111 case 5:
2112 case 6:
2113 gc->attr |= GRID_ATTR_BLINK;
2114 break;
2115 case 7:
2116 gc->attr |= GRID_ATTR_REVERSE;
2117 break;
2118 case 8:
2119 gc->attr |= GRID_ATTR_HIDDEN;
2120 break;
2121 case 9:
2122 gc->attr |= GRID_ATTR_STRIKETHROUGH;
2123 break;
2124 case 21:
2125 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2126 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2127 break;
2128 case 22:
2129 gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
2130 break;
2131 case 23:
2132 gc->attr &= ~GRID_ATTR_ITALICS;
2133 break;
2134 case 24:
2135 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2136 break;
2137 case 25:
2138 gc->attr &= ~GRID_ATTR_BLINK;
2139 break;
2140 case 27:
2141 gc->attr &= ~GRID_ATTR_REVERSE;
2142 break;
2143 case 28:
2144 gc->attr &= ~GRID_ATTR_HIDDEN;
2145 break;
2146 case 29:
2147 gc->attr &= ~GRID_ATTR_STRIKETHROUGH;
2148 break;
2149 case 30:
2150 case 31:
2151 case 32:
2152 case 33:
2153 case 34:
2154 case 35:
2155 case 36:
2156 case 37:
2157 gc->fg = n - 30;
2158 break;
2159 case 39:
2160 gc->fg = 8;
2161 break;
2162 case 40:
2163 case 41:
2164 case 42:
2165 case 43:
2166 case 44:
2167 case 45:
2168 case 46:
2169 case 47:
2170 gc->bg = n - 40;
2171 break;
2172 case 49:
2173 gc->bg = 8;
2174 break;
2175 case 53:
2176 gc->attr |= GRID_ATTR_OVERLINE;
2177 break;
2178 case 55:
2179 gc->attr &= ~GRID_ATTR_OVERLINE;
2180 break;
2181 case 59:
2182 gc->us = 0;
2183 break;
2184 case 90:
2185 case 91:
2186 case 92:
2187 case 93:
2188 case 94:
2189 case 95:
2190 case 96:
2191 case 97:
2192 gc->fg = n;
2193 break;
2194 case 100:
2195 case 101:
2196 case 102:
2197 case 103:
2198 case 104:
2199 case 105:
2200 case 106:
2201 case 107:
2202 gc->bg = n - 10;
2203 break;
2208 /* End of input with BEL. */
2209 static int
2210 input_end_bel(struct input_ctx *ictx)
2212 log_debug("%s", __func__);
2214 ictx->input_end = INPUT_END_BEL;
2216 return (0);
2219 /* DCS string started. */
2220 static void
2221 input_enter_dcs(struct input_ctx *ictx)
2223 log_debug("%s", __func__);
2225 input_clear(ictx);
2226 input_start_timer(ictx);
2227 ictx->last = -1;
2230 /* DCS terminator (ST) received. */
2231 static int
2232 input_dcs_dispatch(struct input_ctx *ictx)
2234 struct screen_write_ctx *sctx = &ictx->ctx;
2235 u_char *buf = ictx->input_buf;
2236 size_t len = ictx->input_len;
2237 const char prefix[] = "tmux;";
2238 const u_int prefixlen = (sizeof prefix) - 1;
2240 if (ictx->flags & INPUT_DISCARD)
2241 return (0);
2243 log_debug("%s: \"%s\"", __func__, buf);
2245 if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0)
2246 screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen);
2248 return (0);
2251 /* OSC string started. */
2252 static void
2253 input_enter_osc(struct input_ctx *ictx)
2255 log_debug("%s", __func__);
2257 input_clear(ictx);
2258 input_start_timer(ictx);
2259 ictx->last = -1;
2262 /* OSC terminator (ST) received. */
2263 static void
2264 input_exit_osc(struct input_ctx *ictx)
2266 struct screen_write_ctx *sctx = &ictx->ctx;
2267 struct window_pane *wp = ictx->wp;
2268 u_char *p = ictx->input_buf;
2269 u_int option;
2271 if (ictx->flags & INPUT_DISCARD)
2272 return;
2273 if (ictx->input_len < 1 || *p < '0' || *p > '9')
2274 return;
2276 log_debug("%s: \"%s\" (end %s)", __func__, p,
2277 ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2279 option = 0;
2280 while (*p >= '0' && *p <= '9')
2281 option = option * 10 + *p++ - '0';
2282 if (*p == ';')
2283 p++;
2285 switch (option) {
2286 case 0:
2287 case 2:
2288 if (screen_set_title(sctx->s, p) && wp != NULL) {
2289 notify_pane("pane-title-changed", wp);
2290 server_redraw_window_borders(wp->window);
2291 server_status_window(wp->window);
2293 break;
2294 case 4:
2295 input_osc_4(ictx, p);
2296 break;
2297 case 7:
2298 if (utf8_isvalid(p)) {
2299 screen_set_path(sctx->s, p);
2300 if (wp != NULL) {
2301 server_redraw_window_borders(wp->window);
2302 server_status_window(wp->window);
2305 break;
2306 case 10:
2307 input_osc_10(ictx, p);
2308 break;
2309 case 11:
2310 input_osc_11(ictx, p);
2311 break;
2312 case 12:
2313 if (utf8_isvalid(p) && *p != '?') /* ? is colour request */
2314 screen_set_cursor_colour(sctx->s, p);
2315 break;
2316 case 52:
2317 input_osc_52(ictx, p);
2318 break;
2319 case 104:
2320 input_osc_104(ictx, p);
2321 break;
2322 case 110:
2323 input_osc_110(ictx, p);
2324 break;
2325 case 111:
2326 input_osc_111(ictx, p);
2327 break;
2328 case 112:
2329 if (*p == '\0') /* no arguments allowed */
2330 screen_set_cursor_colour(sctx->s, "");
2331 break;
2332 default:
2333 log_debug("%s: unknown '%u'", __func__, option);
2334 break;
2338 /* APC string started. */
2339 static void
2340 input_enter_apc(struct input_ctx *ictx)
2342 log_debug("%s", __func__);
2344 input_clear(ictx);
2345 input_start_timer(ictx);
2346 ictx->last = -1;
2349 /* APC terminator (ST) received. */
2350 static void
2351 input_exit_apc(struct input_ctx *ictx)
2353 struct screen_write_ctx *sctx = &ictx->ctx;
2354 struct window_pane *wp = ictx->wp;
2356 if (ictx->flags & INPUT_DISCARD)
2357 return;
2358 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2360 if (screen_set_title(sctx->s, ictx->input_buf) && wp != NULL) {
2361 notify_pane("pane-title-changed", wp);
2362 server_redraw_window_borders(wp->window);
2363 server_status_window(wp->window);
2367 /* Rename string started. */
2368 static void
2369 input_enter_rename(struct input_ctx *ictx)
2371 log_debug("%s", __func__);
2373 input_clear(ictx);
2374 input_start_timer(ictx);
2375 ictx->last = -1;
2378 /* Rename terminator (ST) received. */
2379 static void
2380 input_exit_rename(struct input_ctx *ictx)
2382 struct window_pane *wp = ictx->wp;
2383 struct options_entry *o;
2385 if (wp == NULL)
2386 return;
2387 if (ictx->flags & INPUT_DISCARD)
2388 return;
2389 if (!options_get_number(ictx->wp->options, "allow-rename"))
2390 return;
2391 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2393 if (!utf8_isvalid(ictx->input_buf))
2394 return;
2396 if (ictx->input_len == 0) {
2397 o = options_get_only(wp->window->options, "automatic-rename");
2398 if (o != NULL)
2399 options_remove_or_default(o, -1, NULL);
2400 return;
2402 window_set_name(wp->window, ictx->input_buf);
2403 options_set_number(wp->window->options, "automatic-rename", 0);
2404 server_redraw_window_borders(wp->window);
2405 server_status_window(wp->window);
2408 /* Open UTF-8 character. */
2409 static int
2410 input_top_bit_set(struct input_ctx *ictx)
2412 struct screen_write_ctx *sctx = &ictx->ctx;
2413 struct utf8_data *ud = &ictx->utf8data;
2415 ictx->last = -1;
2417 if (!ictx->utf8started) {
2418 if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2419 return (0);
2420 ictx->utf8started = 1;
2421 return (0);
2424 switch (utf8_append(ud, ictx->ch)) {
2425 case UTF8_MORE:
2426 return (0);
2427 case UTF8_ERROR:
2428 ictx->utf8started = 0;
2429 return (0);
2430 case UTF8_DONE:
2431 break;
2433 ictx->utf8started = 0;
2435 log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2436 (int)ud->size, ud->data, ud->width);
2438 utf8_copy(&ictx->cell.cell.data, ud);
2439 screen_write_collect_add(sctx, &ictx->cell.cell);
2441 return (0);
2444 /* Parse colour from OSC. */
2445 static int
2446 input_osc_parse_colour(const char *p)
2448 double c, m, y, k = 0;
2449 u_int r, g, b;
2450 size_t len = strlen(p);
2451 int colour = -1;
2452 char *copy;
2454 if ((len == 12 && sscanf(p, "rgb:%02x/%02x/%02x", &r, &g, &b) == 3) ||
2455 (len == 7 && sscanf(p, "#%02x%02x%02x", &r, &g, &b) == 3) ||
2456 sscanf(p, "%d,%d,%d", &r, &g, &b) == 3)
2457 colour = colour_join_rgb(r, g, b);
2458 else if ((len == 18 &&
2459 sscanf(p, "rgb:%04x/%04x/%04x", &r, &g, &b) == 3) ||
2460 (len == 13 && sscanf(p, "#%04x%04x%04x", &r, &g, &b) == 3))
2461 colour = colour_join_rgb(r >> 8, g >> 8, b >> 8);
2462 else if ((sscanf(p, "cmyk:%lf/%lf/%lf/%lf", &c, &m, &y, &k) == 4 ||
2463 sscanf(p, "cmy:%lf/%lf/%lf", &c, &m, &y) == 3) &&
2464 c >= 0 && c <= 1 && m >= 0 && m <= 1 &&
2465 y >= 0 && y <= 1 && k >= 0 && k <= 1) {
2466 colour = colour_join_rgb(
2467 (1 - c) * (1 - k) * 255,
2468 (1 - m) * (1 - k) * 255,
2469 (1 - y) * (1 - k) * 255);
2470 } else {
2471 while (len != 0 && *p == ' ') {
2472 p++;
2473 len--;
2475 while (len != 0 && p[len - 1] == ' ')
2476 len--;
2477 copy = xstrndup(p, len);
2478 colour = colour_byname(copy);
2479 free(copy);
2481 log_debug("%s: %s = %s", __func__, p, colour_tostring(colour));
2482 return (colour);
2485 /* Reply to a colour request. */
2486 static void
2487 input_osc_colour_reply(struct input_ctx *ictx, u_int n, int c)
2489 u_char r, g, b;
2490 const char *end;
2492 if (c == 8 || (~c & COLOUR_FLAG_RGB))
2493 return;
2494 colour_split_rgb(c, &r, &g, &b);
2496 if (ictx->input_end == INPUT_END_BEL)
2497 end = "\007";
2498 else
2499 end = "\033\\";
2500 input_reply(ictx, "\033]%u;rgb:%02hhx/%02hhx/%02hhx%s", n, r, g, b, end);
2503 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2504 static void
2505 input_osc_4(struct input_ctx *ictx, const char *p)
2507 char *copy, *s, *next = NULL;
2508 long idx;
2509 int c, bad = 0, redraw = 0;
2511 copy = s = xstrdup(p);
2512 while (s != NULL && *s != '\0') {
2513 idx = strtol(s, &next, 10);
2514 if (*next++ != ';') {
2515 bad = 1;
2516 break;
2518 if (idx < 0 || idx >= 256) {
2519 bad = 1;
2520 break;
2523 s = strsep(&next, ";");
2524 if ((c = input_osc_parse_colour(s)) == -1) {
2525 s = next;
2526 continue;
2528 if (colour_palette_set(ictx->palette, idx, c))
2529 redraw = 1;
2530 s = next;
2532 if (bad)
2533 log_debug("bad OSC 4: %s", p);
2534 if (redraw)
2535 screen_write_fullredraw(&ictx->ctx);
2536 free(copy);
2539 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2540 static void
2541 input_osc_10(struct input_ctx *ictx, const char *p)
2543 struct window_pane *wp = ictx->wp;
2544 struct grid_cell defaults;
2545 int c;
2547 if (strcmp(p, "?") == 0) {
2548 if (wp != NULL) {
2549 tty_default_colours(&defaults, wp);
2550 input_osc_colour_reply(ictx, 10, defaults.fg);
2552 return;
2555 if ((c = input_osc_parse_colour(p)) == -1) {
2556 log_debug("bad OSC 10: %s", p);
2557 return;
2559 if (ictx->palette != NULL) {
2560 ictx->palette->fg = c;
2561 if (wp != NULL)
2562 wp->flags |= PANE_STYLECHANGED;
2563 screen_write_fullredraw(&ictx->ctx);
2567 /* Handle the OSC 110 sequence for resetting background colour. */
2568 static void
2569 input_osc_110(struct input_ctx *ictx, const char *p)
2571 struct window_pane *wp = ictx->wp;
2573 if (*p != '\0')
2574 return;
2575 if (ictx->palette != NULL) {
2576 ictx->palette->fg = 8;
2577 if (wp != NULL)
2578 wp->flags |= PANE_STYLECHANGED;
2579 screen_write_fullredraw(&ictx->ctx);
2583 /* Handle the OSC 11 sequence for setting and querying background colour. */
2584 static void
2585 input_osc_11(struct input_ctx *ictx, const char *p)
2587 struct window_pane *wp = ictx->wp;
2588 struct grid_cell defaults;
2589 int c;
2591 if (strcmp(p, "?") == 0) {
2592 if (wp != NULL) {
2593 tty_default_colours(&defaults, wp);
2594 input_osc_colour_reply(ictx, 11, defaults.bg);
2596 return;
2599 if ((c = input_osc_parse_colour(p)) == -1) {
2600 log_debug("bad OSC 11: %s", p);
2601 return;
2603 if (ictx->palette != NULL) {
2604 ictx->palette->bg = c;
2605 if (wp != NULL)
2606 wp->flags |= PANE_STYLECHANGED;
2607 screen_write_fullredraw(&ictx->ctx);
2611 /* Handle the OSC 111 sequence for resetting background colour. */
2612 static void
2613 input_osc_111(struct input_ctx *ictx, const char *p)
2615 struct window_pane *wp = ictx->wp;
2617 if (*p != '\0')
2618 return;
2619 if (ictx->palette != NULL) {
2620 ictx->palette->bg = 8;
2621 if (wp != NULL)
2622 wp->flags |= PANE_STYLECHANGED;
2623 screen_write_fullredraw(&ictx->ctx);
2627 /* Handle the OSC 52 sequence for setting the clipboard. */
2628 static void
2629 input_osc_52(struct input_ctx *ictx, const char *p)
2631 struct window_pane *wp = ictx->wp;
2632 char *end;
2633 const char *buf;
2634 size_t len;
2635 u_char *out;
2636 int outlen, state;
2637 struct screen_write_ctx ctx;
2638 struct paste_buffer *pb;
2640 if (wp == NULL)
2641 return;
2642 state = options_get_number(global_options, "set-clipboard");
2643 if (state != 2)
2644 return;
2646 if ((end = strchr(p, ';')) == NULL)
2647 return;
2648 end++;
2649 if (*end == '\0')
2650 return;
2651 log_debug("%s: %s", __func__, end);
2653 if (strcmp(end, "?") == 0) {
2654 if ((pb = paste_get_top(NULL)) != NULL) {
2655 buf = paste_buffer_data(pb, &len);
2656 outlen = 4 * ((len + 2) / 3) + 1;
2657 out = xmalloc(outlen);
2658 if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
2659 free(out);
2660 return;
2662 } else {
2663 outlen = 0;
2664 out = NULL;
2666 bufferevent_write(ictx->event, "\033]52;;", 6);
2667 if (outlen != 0)
2668 bufferevent_write(ictx->event, out, outlen);
2669 if (ictx->input_end == INPUT_END_BEL)
2670 bufferevent_write(ictx->event, "\007", 1);
2671 else
2672 bufferevent_write(ictx->event, "\033\\", 2);
2673 free(out);
2674 return;
2677 len = (strlen(end) / 4) * 3;
2678 if (len == 0)
2679 return;
2681 out = xmalloc(len);
2682 if ((outlen = b64_pton(end, out, len)) == -1) {
2683 free(out);
2684 return;
2687 screen_write_start_pane(&ctx, wp, NULL);
2688 screen_write_setselection(&ctx, out, outlen);
2689 screen_write_stop(&ctx);
2690 notify_pane("pane-set-clipboard", wp);
2692 paste_add(NULL, out, outlen);
2695 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2696 static void
2697 input_osc_104(struct input_ctx *ictx, const char *p)
2699 char *copy, *s;
2700 long idx;
2701 int bad = 0, redraw = 0;
2703 if (*p == '\0') {
2704 colour_palette_clear(ictx->palette);
2705 screen_write_fullredraw(&ictx->ctx);
2706 return;
2709 copy = s = xstrdup(p);
2710 while (*s != '\0') {
2711 idx = strtol(s, &s, 10);
2712 if (*s != '\0' && *s != ';') {
2713 bad = 1;
2714 break;
2716 if (idx < 0 || idx >= 256) {
2717 bad = 1;
2718 break;
2720 if (colour_palette_set(ictx->palette, idx, -1))
2721 redraw = 1;
2722 if (*s == ';')
2723 s++;
2725 if (bad)
2726 log_debug("bad OSC 104: %s", p);
2727 if (redraw)
2728 screen_write_fullredraw(&ictx->ctx);
2729 free(copy);