Merge branch 'obsd-master'
[tmux.git] / input.c
blob86af45cf7714e49077438284fbaa5c787bd48646
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 struct utf8_data last;
114 int flags;
115 #define INPUT_DISCARD 0x1
116 #define INPUT_LAST 0x2
118 const struct input_state *state;
120 struct event timer;
123 * All input received since we were last in the ground state. Sent to
124 * control clients on connection.
126 struct evbuffer *since_ground;
129 /* Helper functions. */
130 struct input_transition;
131 static int input_split(struct input_ctx *);
132 static int input_get(struct input_ctx *, u_int, int, int);
133 static void printflike(2, 3) input_reply(struct input_ctx *, const char *, ...);
134 static void input_set_state(struct input_ctx *,
135 const struct input_transition *);
136 static void input_reset_cell(struct input_ctx *);
138 static void input_osc_4(struct input_ctx *, const char *);
139 static void input_osc_8(struct input_ctx *, const char *);
140 static void input_osc_10(struct input_ctx *, const char *);
141 static void input_osc_11(struct input_ctx *, const char *);
142 static void input_osc_12(struct input_ctx *, const char *);
143 static void input_osc_52(struct input_ctx *, const char *);
144 static void input_osc_104(struct input_ctx *, const char *);
145 static void input_osc_110(struct input_ctx *, const char *);
146 static void input_osc_111(struct input_ctx *, const char *);
147 static void input_osc_112(struct input_ctx *, const char *);
148 static void input_osc_133(struct input_ctx *, const char *);
150 /* Transition entry/exit handlers. */
151 static void input_clear(struct input_ctx *);
152 static void input_ground(struct input_ctx *);
153 static void input_enter_dcs(struct input_ctx *);
154 static void input_enter_osc(struct input_ctx *);
155 static void input_exit_osc(struct input_ctx *);
156 static void input_enter_apc(struct input_ctx *);
157 static void input_exit_apc(struct input_ctx *);
158 static void input_enter_rename(struct input_ctx *);
159 static void input_exit_rename(struct input_ctx *);
161 /* Input state handlers. */
162 static int input_print(struct input_ctx *);
163 static int input_intermediate(struct input_ctx *);
164 static int input_parameter(struct input_ctx *);
165 static int input_input(struct input_ctx *);
166 static int input_c0_dispatch(struct input_ctx *);
167 static int input_esc_dispatch(struct input_ctx *);
168 static int input_csi_dispatch(struct input_ctx *);
169 static void input_csi_dispatch_rm(struct input_ctx *);
170 static void input_csi_dispatch_rm_private(struct input_ctx *);
171 static void input_csi_dispatch_sm(struct input_ctx *);
172 static void input_csi_dispatch_sm_private(struct input_ctx *);
173 static void input_csi_dispatch_sm_graphics(struct input_ctx *);
174 static void input_csi_dispatch_winops(struct input_ctx *);
175 static void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
176 static void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
177 static void input_csi_dispatch_sgr(struct input_ctx *);
178 static int input_dcs_dispatch(struct input_ctx *);
179 static int input_top_bit_set(struct input_ctx *);
180 static int input_end_bel(struct input_ctx *);
182 /* Command table comparison function. */
183 static int input_table_compare(const void *, const void *);
185 /* Command table entry. */
186 struct input_table_entry {
187 int ch;
188 const char *interm;
189 int type;
192 /* Escape commands. */
193 enum input_esc_type {
194 INPUT_ESC_DECALN,
195 INPUT_ESC_DECKPAM,
196 INPUT_ESC_DECKPNM,
197 INPUT_ESC_DECRC,
198 INPUT_ESC_DECSC,
199 INPUT_ESC_HTS,
200 INPUT_ESC_IND,
201 INPUT_ESC_NEL,
202 INPUT_ESC_RI,
203 INPUT_ESC_RIS,
204 INPUT_ESC_SCSG0_OFF,
205 INPUT_ESC_SCSG0_ON,
206 INPUT_ESC_SCSG1_OFF,
207 INPUT_ESC_SCSG1_ON,
208 INPUT_ESC_ST
211 /* Escape command table. */
212 static const struct input_table_entry input_esc_table[] = {
213 { '0', "(", INPUT_ESC_SCSG0_ON },
214 { '0', ")", INPUT_ESC_SCSG1_ON },
215 { '7', "", INPUT_ESC_DECSC },
216 { '8', "", INPUT_ESC_DECRC },
217 { '8', "#", INPUT_ESC_DECALN },
218 { '=', "", INPUT_ESC_DECKPAM },
219 { '>', "", INPUT_ESC_DECKPNM },
220 { 'B', "(", INPUT_ESC_SCSG0_OFF },
221 { 'B', ")", INPUT_ESC_SCSG1_OFF },
222 { 'D', "", INPUT_ESC_IND },
223 { 'E', "", INPUT_ESC_NEL },
224 { 'H', "", INPUT_ESC_HTS },
225 { 'M', "", INPUT_ESC_RI },
226 { '\\', "", INPUT_ESC_ST },
227 { 'c', "", INPUT_ESC_RIS },
230 /* Control (CSI) commands. */
231 enum input_csi_type {
232 INPUT_CSI_CBT,
233 INPUT_CSI_CNL,
234 INPUT_CSI_CPL,
235 INPUT_CSI_CUB,
236 INPUT_CSI_CUD,
237 INPUT_CSI_CUF,
238 INPUT_CSI_CUP,
239 INPUT_CSI_CUU,
240 INPUT_CSI_DA,
241 INPUT_CSI_DA_TWO,
242 INPUT_CSI_DCH,
243 INPUT_CSI_DECSCUSR,
244 INPUT_CSI_DECSTBM,
245 INPUT_CSI_DL,
246 INPUT_CSI_DSR,
247 INPUT_CSI_ECH,
248 INPUT_CSI_ED,
249 INPUT_CSI_EL,
250 INPUT_CSI_HPA,
251 INPUT_CSI_ICH,
252 INPUT_CSI_IL,
253 INPUT_CSI_MODOFF,
254 INPUT_CSI_MODSET,
255 INPUT_CSI_RCP,
256 INPUT_CSI_REP,
257 INPUT_CSI_RM,
258 INPUT_CSI_RM_PRIVATE,
259 INPUT_CSI_SCP,
260 INPUT_CSI_SD,
261 INPUT_CSI_SGR,
262 INPUT_CSI_SM,
263 INPUT_CSI_SM_PRIVATE,
264 INPUT_CSI_SM_GRAPHICS,
265 INPUT_CSI_SU,
266 INPUT_CSI_TBC,
267 INPUT_CSI_VPA,
268 INPUT_CSI_WINOPS,
269 INPUT_CSI_XDA
272 /* Control (CSI) command table. */
273 static const struct input_table_entry input_csi_table[] = {
274 { '@', "", INPUT_CSI_ICH },
275 { 'A', "", INPUT_CSI_CUU },
276 { 'B', "", INPUT_CSI_CUD },
277 { 'C', "", INPUT_CSI_CUF },
278 { 'D', "", INPUT_CSI_CUB },
279 { 'E', "", INPUT_CSI_CNL },
280 { 'F', "", INPUT_CSI_CPL },
281 { 'G', "", INPUT_CSI_HPA },
282 { 'H', "", INPUT_CSI_CUP },
283 { 'J', "", INPUT_CSI_ED },
284 { 'K', "", INPUT_CSI_EL },
285 { 'L', "", INPUT_CSI_IL },
286 { 'M', "", INPUT_CSI_DL },
287 { 'P', "", INPUT_CSI_DCH },
288 { 'S', "", INPUT_CSI_SU },
289 { 'S', "?", INPUT_CSI_SM_GRAPHICS },
290 { 'T', "", INPUT_CSI_SD },
291 { 'X', "", INPUT_CSI_ECH },
292 { 'Z', "", INPUT_CSI_CBT },
293 { '`', "", INPUT_CSI_HPA },
294 { 'b', "", INPUT_CSI_REP },
295 { 'c', "", INPUT_CSI_DA },
296 { 'c', ">", INPUT_CSI_DA_TWO },
297 { 'd', "", INPUT_CSI_VPA },
298 { 'f', "", INPUT_CSI_CUP },
299 { 'g', "", INPUT_CSI_TBC },
300 { 'h', "", INPUT_CSI_SM },
301 { 'h', "?", INPUT_CSI_SM_PRIVATE },
302 { 'l', "", INPUT_CSI_RM },
303 { 'l', "?", INPUT_CSI_RM_PRIVATE },
304 { 'm', "", INPUT_CSI_SGR },
305 { 'm', ">", INPUT_CSI_MODSET },
306 { 'n', "", INPUT_CSI_DSR },
307 { 'n', ">", INPUT_CSI_MODOFF },
308 { 'q', " ", INPUT_CSI_DECSCUSR },
309 { 'q', ">", INPUT_CSI_XDA },
310 { 'r', "", INPUT_CSI_DECSTBM },
311 { 's', "", INPUT_CSI_SCP },
312 { 't', "", INPUT_CSI_WINOPS },
313 { 'u', "", INPUT_CSI_RCP }
316 /* Input transition. */
317 struct input_transition {
318 int first;
319 int last;
321 int (*handler)(struct input_ctx *);
322 const struct input_state *state;
325 /* Input state. */
326 struct input_state {
327 const char *name;
328 void (*enter)(struct input_ctx *);
329 void (*exit)(struct input_ctx *);
330 const struct input_transition *transitions;
333 /* State transitions available from all states. */
334 #define INPUT_STATE_ANYWHERE \
335 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
336 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
337 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
339 /* Forward declarations of state tables. */
340 static const struct input_transition input_state_ground_table[];
341 static const struct input_transition input_state_esc_enter_table[];
342 static const struct input_transition input_state_esc_intermediate_table[];
343 static const struct input_transition input_state_csi_enter_table[];
344 static const struct input_transition input_state_csi_parameter_table[];
345 static const struct input_transition input_state_csi_intermediate_table[];
346 static const struct input_transition input_state_csi_ignore_table[];
347 static const struct input_transition input_state_dcs_enter_table[];
348 static const struct input_transition input_state_dcs_parameter_table[];
349 static const struct input_transition input_state_dcs_intermediate_table[];
350 static const struct input_transition input_state_dcs_handler_table[];
351 static const struct input_transition input_state_dcs_escape_table[];
352 static const struct input_transition input_state_dcs_ignore_table[];
353 static const struct input_transition input_state_osc_string_table[];
354 static const struct input_transition input_state_apc_string_table[];
355 static const struct input_transition input_state_rename_string_table[];
356 static const struct input_transition input_state_consume_st_table[];
358 /* ground state definition. */
359 static const struct input_state input_state_ground = {
360 "ground",
361 input_ground, NULL,
362 input_state_ground_table
365 /* esc_enter state definition. */
366 static const struct input_state input_state_esc_enter = {
367 "esc_enter",
368 input_clear, NULL,
369 input_state_esc_enter_table
372 /* esc_intermediate state definition. */
373 static const struct input_state input_state_esc_intermediate = {
374 "esc_intermediate",
375 NULL, NULL,
376 input_state_esc_intermediate_table
379 /* csi_enter state definition. */
380 static const struct input_state input_state_csi_enter = {
381 "csi_enter",
382 input_clear, NULL,
383 input_state_csi_enter_table
386 /* csi_parameter state definition. */
387 static const struct input_state input_state_csi_parameter = {
388 "csi_parameter",
389 NULL, NULL,
390 input_state_csi_parameter_table
393 /* csi_intermediate state definition. */
394 static const struct input_state input_state_csi_intermediate = {
395 "csi_intermediate",
396 NULL, NULL,
397 input_state_csi_intermediate_table
400 /* csi_ignore state definition. */
401 static const struct input_state input_state_csi_ignore = {
402 "csi_ignore",
403 NULL, NULL,
404 input_state_csi_ignore_table
407 /* dcs_enter state definition. */
408 static const struct input_state input_state_dcs_enter = {
409 "dcs_enter",
410 input_enter_dcs, NULL,
411 input_state_dcs_enter_table
414 /* dcs_parameter state definition. */
415 static const struct input_state input_state_dcs_parameter = {
416 "dcs_parameter",
417 NULL, NULL,
418 input_state_dcs_parameter_table
421 /* dcs_intermediate state definition. */
422 static const struct input_state input_state_dcs_intermediate = {
423 "dcs_intermediate",
424 NULL, NULL,
425 input_state_dcs_intermediate_table
428 /* dcs_handler state definition. */
429 static const struct input_state input_state_dcs_handler = {
430 "dcs_handler",
431 NULL, NULL,
432 input_state_dcs_handler_table
435 /* dcs_escape state definition. */
436 static const struct input_state input_state_dcs_escape = {
437 "dcs_escape",
438 NULL, NULL,
439 input_state_dcs_escape_table
442 /* dcs_ignore state definition. */
443 static const struct input_state input_state_dcs_ignore = {
444 "dcs_ignore",
445 NULL, NULL,
446 input_state_dcs_ignore_table
449 /* osc_string state definition. */
450 static const struct input_state input_state_osc_string = {
451 "osc_string",
452 input_enter_osc, input_exit_osc,
453 input_state_osc_string_table
456 /* apc_string state definition. */
457 static const struct input_state input_state_apc_string = {
458 "apc_string",
459 input_enter_apc, input_exit_apc,
460 input_state_apc_string_table
463 /* rename_string state definition. */
464 static const struct input_state input_state_rename_string = {
465 "rename_string",
466 input_enter_rename, input_exit_rename,
467 input_state_rename_string_table
470 /* consume_st state definition. */
471 static const struct input_state input_state_consume_st = {
472 "consume_st",
473 input_enter_rename, NULL, /* rename also waits for ST */
474 input_state_consume_st_table
477 /* ground state table. */
478 static const struct input_transition input_state_ground_table[] = {
479 INPUT_STATE_ANYWHERE,
481 { 0x00, 0x17, input_c0_dispatch, NULL },
482 { 0x19, 0x19, input_c0_dispatch, NULL },
483 { 0x1c, 0x1f, input_c0_dispatch, NULL },
484 { 0x20, 0x7e, input_print, NULL },
485 { 0x7f, 0x7f, NULL, NULL },
486 { 0x80, 0xff, input_top_bit_set, NULL },
488 { -1, -1, NULL, NULL }
491 /* esc_enter state table. */
492 static const struct input_transition input_state_esc_enter_table[] = {
493 INPUT_STATE_ANYWHERE,
495 { 0x00, 0x17, input_c0_dispatch, NULL },
496 { 0x19, 0x19, input_c0_dispatch, NULL },
497 { 0x1c, 0x1f, input_c0_dispatch, NULL },
498 { 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
499 { 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
500 { 0x50, 0x50, NULL, &input_state_dcs_enter },
501 { 0x51, 0x57, input_esc_dispatch, &input_state_ground },
502 { 0x58, 0x58, NULL, &input_state_consume_st },
503 { 0x59, 0x59, input_esc_dispatch, &input_state_ground },
504 { 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
505 { 0x5b, 0x5b, NULL, &input_state_csi_enter },
506 { 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
507 { 0x5d, 0x5d, NULL, &input_state_osc_string },
508 { 0x5e, 0x5e, NULL, &input_state_consume_st },
509 { 0x5f, 0x5f, NULL, &input_state_apc_string },
510 { 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
511 { 0x6b, 0x6b, NULL, &input_state_rename_string },
512 { 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
513 { 0x7f, 0xff, NULL, NULL },
515 { -1, -1, NULL, NULL }
518 /* esc_intermediate state table. */
519 static const struct input_transition input_state_esc_intermediate_table[] = {
520 INPUT_STATE_ANYWHERE,
522 { 0x00, 0x17, input_c0_dispatch, NULL },
523 { 0x19, 0x19, input_c0_dispatch, NULL },
524 { 0x1c, 0x1f, input_c0_dispatch, NULL },
525 { 0x20, 0x2f, input_intermediate, NULL },
526 { 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
527 { 0x7f, 0xff, NULL, NULL },
529 { -1, -1, NULL, NULL }
532 /* csi_enter state table. */
533 static const struct input_transition input_state_csi_enter_table[] = {
534 INPUT_STATE_ANYWHERE,
536 { 0x00, 0x17, input_c0_dispatch, NULL },
537 { 0x19, 0x19, input_c0_dispatch, NULL },
538 { 0x1c, 0x1f, input_c0_dispatch, NULL },
539 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
540 { 0x30, 0x39, input_parameter, &input_state_csi_parameter },
541 { 0x3a, 0x3a, input_parameter, &input_state_csi_parameter },
542 { 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
543 { 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
544 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
545 { 0x7f, 0xff, NULL, NULL },
547 { -1, -1, NULL, NULL }
550 /* csi_parameter state table. */
551 static const struct input_transition input_state_csi_parameter_table[] = {
552 INPUT_STATE_ANYWHERE,
554 { 0x00, 0x17, input_c0_dispatch, NULL },
555 { 0x19, 0x19, input_c0_dispatch, NULL },
556 { 0x1c, 0x1f, input_c0_dispatch, NULL },
557 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
558 { 0x30, 0x39, input_parameter, NULL },
559 { 0x3a, 0x3a, input_parameter, NULL },
560 { 0x3b, 0x3b, input_parameter, NULL },
561 { 0x3c, 0x3f, NULL, &input_state_csi_ignore },
562 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
563 { 0x7f, 0xff, NULL, NULL },
565 { -1, -1, NULL, NULL }
568 /* csi_intermediate state table. */
569 static const struct input_transition input_state_csi_intermediate_table[] = {
570 INPUT_STATE_ANYWHERE,
572 { 0x00, 0x17, input_c0_dispatch, NULL },
573 { 0x19, 0x19, input_c0_dispatch, NULL },
574 { 0x1c, 0x1f, input_c0_dispatch, NULL },
575 { 0x20, 0x2f, input_intermediate, NULL },
576 { 0x30, 0x3f, NULL, &input_state_csi_ignore },
577 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
578 { 0x7f, 0xff, NULL, NULL },
580 { -1, -1, NULL, NULL }
583 /* csi_ignore state table. */
584 static const struct input_transition input_state_csi_ignore_table[] = {
585 INPUT_STATE_ANYWHERE,
587 { 0x00, 0x17, input_c0_dispatch, NULL },
588 { 0x19, 0x19, input_c0_dispatch, NULL },
589 { 0x1c, 0x1f, input_c0_dispatch, NULL },
590 { 0x20, 0x3f, NULL, NULL },
591 { 0x40, 0x7e, NULL, &input_state_ground },
592 { 0x7f, 0xff, NULL, NULL },
594 { -1, -1, NULL, NULL }
597 /* dcs_enter state table. */
598 static const struct input_transition input_state_dcs_enter_table[] = {
599 INPUT_STATE_ANYWHERE,
601 { 0x00, 0x17, NULL, NULL },
602 { 0x19, 0x19, NULL, NULL },
603 { 0x1c, 0x1f, NULL, NULL },
604 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
605 { 0x30, 0x39, input_parameter, &input_state_dcs_parameter },
606 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
607 { 0x3b, 0x3b, input_parameter, &input_state_dcs_parameter },
608 { 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
609 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
610 { 0x7f, 0xff, NULL, NULL },
612 { -1, -1, NULL, NULL }
615 /* dcs_parameter state table. */
616 static const struct input_transition input_state_dcs_parameter_table[] = {
617 INPUT_STATE_ANYWHERE,
619 { 0x00, 0x17, NULL, NULL },
620 { 0x19, 0x19, NULL, NULL },
621 { 0x1c, 0x1f, NULL, NULL },
622 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
623 { 0x30, 0x39, input_parameter, NULL },
624 { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
625 { 0x3b, 0x3b, input_parameter, NULL },
626 { 0x3c, 0x3f, NULL, &input_state_dcs_ignore },
627 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
628 { 0x7f, 0xff, NULL, NULL },
630 { -1, -1, NULL, NULL }
633 /* dcs_intermediate state table. */
634 static const struct input_transition input_state_dcs_intermediate_table[] = {
635 INPUT_STATE_ANYWHERE,
637 { 0x00, 0x17, NULL, NULL },
638 { 0x19, 0x19, NULL, NULL },
639 { 0x1c, 0x1f, NULL, NULL },
640 { 0x20, 0x2f, input_intermediate, NULL },
641 { 0x30, 0x3f, NULL, &input_state_dcs_ignore },
642 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
643 { 0x7f, 0xff, NULL, NULL },
645 { -1, -1, NULL, NULL }
648 /* dcs_handler state table. */
649 static const struct input_transition input_state_dcs_handler_table[] = {
650 /* No INPUT_STATE_ANYWHERE */
652 { 0x00, 0x1a, input_input, NULL },
653 { 0x1b, 0x1b, NULL, &input_state_dcs_escape },
654 { 0x1c, 0xff, input_input, NULL },
656 { -1, -1, NULL, NULL }
659 /* dcs_escape state table. */
660 static const struct input_transition input_state_dcs_escape_table[] = {
661 /* No INPUT_STATE_ANYWHERE */
663 { 0x00, 0x5b, input_input, &input_state_dcs_handler },
664 { 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
665 { 0x5d, 0xff, input_input, &input_state_dcs_handler },
667 { -1, -1, NULL, NULL }
670 /* dcs_ignore state table. */
671 static const struct input_transition input_state_dcs_ignore_table[] = {
672 INPUT_STATE_ANYWHERE,
674 { 0x00, 0x17, NULL, NULL },
675 { 0x19, 0x19, NULL, NULL },
676 { 0x1c, 0x1f, NULL, NULL },
677 { 0x20, 0xff, NULL, NULL },
679 { -1, -1, NULL, NULL }
682 /* osc_string state table. */
683 static const struct input_transition input_state_osc_string_table[] = {
684 INPUT_STATE_ANYWHERE,
686 { 0x00, 0x06, NULL, NULL },
687 { 0x07, 0x07, input_end_bel, &input_state_ground },
688 { 0x08, 0x17, NULL, NULL },
689 { 0x19, 0x19, NULL, NULL },
690 { 0x1c, 0x1f, NULL, NULL },
691 { 0x20, 0xff, input_input, NULL },
693 { -1, -1, NULL, NULL }
696 /* apc_string state table. */
697 static const struct input_transition input_state_apc_string_table[] = {
698 INPUT_STATE_ANYWHERE,
700 { 0x00, 0x17, NULL, NULL },
701 { 0x19, 0x19, NULL, NULL },
702 { 0x1c, 0x1f, NULL, NULL },
703 { 0x20, 0xff, input_input, NULL },
705 { -1, -1, NULL, NULL }
708 /* rename_string state table. */
709 static const struct input_transition input_state_rename_string_table[] = {
710 INPUT_STATE_ANYWHERE,
712 { 0x00, 0x17, NULL, NULL },
713 { 0x19, 0x19, NULL, NULL },
714 { 0x1c, 0x1f, NULL, NULL },
715 { 0x20, 0xff, input_input, NULL },
717 { -1, -1, NULL, NULL }
720 /* consume_st state table. */
721 static const struct input_transition input_state_consume_st_table[] = {
722 INPUT_STATE_ANYWHERE,
724 { 0x00, 0x17, NULL, NULL },
725 { 0x19, 0x19, NULL, NULL },
726 { 0x1c, 0x1f, NULL, NULL },
727 { 0x20, 0xff, NULL, NULL },
729 { -1, -1, NULL, NULL }
732 /* Input table compare. */
733 static int
734 input_table_compare(const void *key, const void *value)
736 const struct input_ctx *ictx = key;
737 const struct input_table_entry *entry = value;
739 if (ictx->ch != entry->ch)
740 return (ictx->ch - entry->ch);
741 return (strcmp(ictx->interm_buf, entry->interm));
745 * Timer - if this expires then have been waiting for a terminator for too
746 * long, so reset to ground.
748 static void
749 input_timer_callback(__unused int fd, __unused short events, void *arg)
751 struct input_ctx *ictx = arg;
753 log_debug("%s: %s expired" , __func__, ictx->state->name);
754 input_reset(ictx, 0);
757 /* Start the timer. */
758 static void
759 input_start_timer(struct input_ctx *ictx)
761 struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
763 event_del(&ictx->timer);
764 event_add(&ictx->timer, &tv);
767 /* Reset cell state to default. */
768 static void
769 input_reset_cell(struct input_ctx *ictx)
771 memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
772 ictx->cell.set = 0;
773 ictx->cell.g0set = ictx->cell.g1set = 0;
775 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
776 ictx->old_cx = 0;
777 ictx->old_cy = 0;
780 /* Save screen state. */
781 static void
782 input_save_state(struct input_ctx *ictx)
784 struct screen_write_ctx *sctx = &ictx->ctx;
785 struct screen *s = sctx->s;
787 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
788 ictx->old_cx = s->cx;
789 ictx->old_cy = s->cy;
790 ictx->old_mode = s->mode;
793 /* Restore screen state. */
794 static void
795 input_restore_state(struct input_ctx *ictx)
797 struct screen_write_ctx *sctx = &ictx->ctx;
799 memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
800 if (ictx->old_mode & MODE_ORIGIN)
801 screen_write_mode_set(sctx, MODE_ORIGIN);
802 else
803 screen_write_mode_clear(sctx, MODE_ORIGIN);
804 screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy, 0);
807 /* Initialise input parser. */
808 struct input_ctx *
809 input_init(struct window_pane *wp, struct bufferevent *bev,
810 struct colour_palette *palette)
812 struct input_ctx *ictx;
814 ictx = xcalloc(1, sizeof *ictx);
815 ictx->wp = wp;
816 ictx->event = bev;
817 ictx->palette = palette;
819 ictx->input_space = INPUT_BUF_START;
820 ictx->input_buf = xmalloc(INPUT_BUF_START);
822 ictx->since_ground = evbuffer_new();
823 if (ictx->since_ground == NULL)
824 fatalx("out of memory");
826 evtimer_set(&ictx->timer, input_timer_callback, ictx);
828 input_reset(ictx, 0);
829 return (ictx);
832 /* Destroy input parser. */
833 void
834 input_free(struct input_ctx *ictx)
836 u_int i;
838 for (i = 0; i < ictx->param_list_len; i++) {
839 if (ictx->param_list[i].type == INPUT_STRING)
840 free(ictx->param_list[i].str);
843 event_del(&ictx->timer);
845 free(ictx->input_buf);
846 evbuffer_free(ictx->since_ground);
848 free(ictx);
851 /* Reset input state and clear screen. */
852 void
853 input_reset(struct input_ctx *ictx, int clear)
855 struct screen_write_ctx *sctx = &ictx->ctx;
856 struct window_pane *wp = ictx->wp;
858 input_reset_cell(ictx);
860 if (clear && wp != NULL) {
861 if (TAILQ_EMPTY(&wp->modes))
862 screen_write_start_pane(sctx, wp, &wp->base);
863 else
864 screen_write_start(sctx, &wp->base);
865 screen_write_reset(sctx);
866 screen_write_stop(sctx);
869 input_clear(ictx);
871 ictx->state = &input_state_ground;
872 ictx->flags = 0;
875 /* Return pending data. */
876 struct evbuffer *
877 input_pending(struct input_ctx *ictx)
879 return (ictx->since_ground);
882 /* Change input state. */
883 static void
884 input_set_state(struct input_ctx *ictx, const struct input_transition *itr)
886 if (ictx->state->exit != NULL)
887 ictx->state->exit(ictx);
888 ictx->state = itr->state;
889 if (ictx->state->enter != NULL)
890 ictx->state->enter(ictx);
893 /* Parse data. */
894 static void
895 input_parse(struct input_ctx *ictx, u_char *buf, size_t len)
897 struct screen_write_ctx *sctx = &ictx->ctx;
898 const struct input_state *state = NULL;
899 const struct input_transition *itr = NULL;
900 size_t off = 0;
902 /* Parse the input. */
903 while (off < len) {
904 ictx->ch = buf[off++];
906 /* Find the transition. */
907 if (ictx->state != state ||
908 itr == NULL ||
909 ictx->ch < itr->first ||
910 ictx->ch > itr->last) {
911 itr = ictx->state->transitions;
912 while (itr->first != -1 && itr->last != -1) {
913 if (ictx->ch >= itr->first &&
914 ictx->ch <= itr->last)
915 break;
916 itr++;
918 if (itr->first == -1 || itr->last == -1) {
919 /* No transition? Eh? */
920 fatalx("no transition from state");
923 state = ictx->state;
926 * Any state except print stops the current collection. This is
927 * an optimization to avoid checking if the attributes have
928 * changed for every character. It will stop unnecessarily for
929 * sequences that don't make a terminal change, but they should
930 * be the minority.
932 if (itr->handler != input_print)
933 screen_write_collect_end(sctx);
936 * Execute the handler, if any. Don't switch state if it
937 * returns non-zero.
939 if (itr->handler != NULL && itr->handler(ictx) != 0)
940 continue;
942 /* And switch state, if necessary. */
943 if (itr->state != NULL)
944 input_set_state(ictx, itr);
946 /* If not in ground state, save input. */
947 if (ictx->state != &input_state_ground)
948 evbuffer_add(ictx->since_ground, &ictx->ch, 1);
952 /* Parse input from pane. */
953 void
954 input_parse_pane(struct window_pane *wp)
956 void *new_data;
957 size_t new_size;
959 new_data = window_pane_get_new_data(wp, &wp->offset, &new_size);
960 input_parse_buffer(wp, new_data, new_size);
961 window_pane_update_used_data(wp, &wp->offset, new_size);
964 /* Parse given input. */
965 void
966 input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
968 struct input_ctx *ictx = wp->ictx;
969 struct screen_write_ctx *sctx = &ictx->ctx;
971 if (len == 0)
972 return;
974 window_update_activity(wp->window);
975 wp->flags |= PANE_CHANGED;
977 /* Flag new input while in a mode. */
978 if (!TAILQ_EMPTY(&wp->modes))
979 wp->flags |= PANE_UNSEENCHANGES;
981 /* NULL wp if there is a mode set as don't want to update the tty. */
982 if (TAILQ_EMPTY(&wp->modes))
983 screen_write_start_pane(sctx, wp, &wp->base);
984 else
985 screen_write_start(sctx, &wp->base);
987 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
988 ictx->state->name, len, (int)len, buf);
990 input_parse(ictx, buf, len);
991 screen_write_stop(sctx);
994 /* Parse given input for screen. */
995 void
996 input_parse_screen(struct input_ctx *ictx, struct screen *s,
997 screen_write_init_ctx_cb cb, void *arg, u_char *buf, size_t len)
999 struct screen_write_ctx *sctx = &ictx->ctx;
1001 if (len == 0)
1002 return;
1004 screen_write_start_callback(sctx, s, cb, arg);
1005 input_parse(ictx, buf, len);
1006 screen_write_stop(sctx);
1009 /* Split the parameter list (if any). */
1010 static int
1011 input_split(struct input_ctx *ictx)
1013 const char *errstr;
1014 char *ptr, *out;
1015 struct input_param *ip;
1016 u_int i;
1018 for (i = 0; i < ictx->param_list_len; i++) {
1019 if (ictx->param_list[i].type == INPUT_STRING)
1020 free(ictx->param_list[i].str);
1022 ictx->param_list_len = 0;
1024 if (ictx->param_len == 0)
1025 return (0);
1026 ip = &ictx->param_list[0];
1028 ptr = ictx->param_buf;
1029 while ((out = strsep(&ptr, ";")) != NULL) {
1030 if (*out == '\0')
1031 ip->type = INPUT_MISSING;
1032 else {
1033 if (strchr(out, ':') != NULL) {
1034 ip->type = INPUT_STRING;
1035 ip->str = xstrdup(out);
1036 } else {
1037 ip->type = INPUT_NUMBER;
1038 ip->num = strtonum(out, 0, INT_MAX, &errstr);
1039 if (errstr != NULL)
1040 return (-1);
1043 ip = &ictx->param_list[++ictx->param_list_len];
1044 if (ictx->param_list_len == nitems(ictx->param_list))
1045 return (-1);
1048 for (i = 0; i < ictx->param_list_len; i++) {
1049 ip = &ictx->param_list[i];
1050 if (ip->type == INPUT_MISSING)
1051 log_debug("parameter %u: missing", i);
1052 else if (ip->type == INPUT_STRING)
1053 log_debug("parameter %u: string %s", i, ip->str);
1054 else if (ip->type == INPUT_NUMBER)
1055 log_debug("parameter %u: number %d", i, ip->num);
1058 return (0);
1061 /* Get an argument or return default value. */
1062 static int
1063 input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1065 struct input_param *ip;
1066 int retval;
1068 if (validx >= ictx->param_list_len)
1069 return (defval);
1070 ip = &ictx->param_list[validx];
1071 if (ip->type == INPUT_MISSING)
1072 return (defval);
1073 if (ip->type == INPUT_STRING)
1074 return (-1);
1075 retval = ip->num;
1076 if (retval < minval)
1077 return (minval);
1078 return (retval);
1081 /* Reply to terminal query. */
1082 static void
1083 input_reply(struct input_ctx *ictx, const char *fmt, ...)
1085 struct bufferevent *bev = ictx->event;
1086 va_list ap;
1087 char *reply;
1089 if (bev == NULL)
1090 return;
1092 va_start(ap, fmt);
1093 xvasprintf(&reply, fmt, ap);
1094 va_end(ap);
1096 log_debug("%s: %s", __func__, reply);
1097 bufferevent_write(bev, reply, strlen(reply));
1098 free(reply);
1101 /* Clear saved state. */
1102 static void
1103 input_clear(struct input_ctx *ictx)
1105 event_del(&ictx->timer);
1107 *ictx->interm_buf = '\0';
1108 ictx->interm_len = 0;
1110 *ictx->param_buf = '\0';
1111 ictx->param_len = 0;
1113 *ictx->input_buf = '\0';
1114 ictx->input_len = 0;
1116 ictx->input_end = INPUT_END_ST;
1118 ictx->flags &= ~INPUT_DISCARD;
1121 /* Reset for ground state. */
1122 static void
1123 input_ground(struct input_ctx *ictx)
1125 event_del(&ictx->timer);
1126 evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
1128 if (ictx->input_space > INPUT_BUF_START) {
1129 ictx->input_space = INPUT_BUF_START;
1130 ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1134 /* Output this character to the screen. */
1135 static int
1136 input_print(struct input_ctx *ictx)
1138 struct screen_write_ctx *sctx = &ictx->ctx;
1139 int set;
1141 ictx->utf8started = 0; /* can't be valid UTF-8 */
1143 set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1144 if (set == 1)
1145 ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1146 else
1147 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1148 utf8_set(&ictx->cell.cell.data, ictx->ch);
1149 screen_write_collect_add(sctx, &ictx->cell.cell);
1151 utf8_copy(&ictx->last, &ictx->cell.cell.data);
1152 ictx->flags |= INPUT_LAST;
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;
1216 struct grid_cell gc, first_gc;
1217 u_int cx = s->cx, line = s->cy + s->grid->hsize;
1218 u_int width;
1219 int has_content = 0;
1221 ictx->utf8started = 0; /* can't be valid UTF-8 */
1223 log_debug("%s: '%c'", __func__, ictx->ch);
1225 switch (ictx->ch) {
1226 case '\000': /* NUL */
1227 break;
1228 case '\007': /* BEL */
1229 if (wp != NULL)
1230 alerts_queue(wp->window, WINDOW_BELL);
1231 break;
1232 case '\010': /* BS */
1233 screen_write_backspace(sctx);
1234 break;
1235 case '\011': /* HT */
1236 /* Don't tab beyond the end of the line. */
1237 if (s->cx >= screen_size_x(s) - 1)
1238 break;
1240 /* Find the next tab point, or use the last column if none. */
1241 grid_get_cell(s->grid, s->cx, line, &first_gc);
1242 do {
1243 if (!has_content) {
1244 grid_get_cell(s->grid, cx, line, &gc);
1245 if (gc.data.size != 1 ||
1246 *gc.data.data != ' ' ||
1247 !grid_cells_look_equal(&gc, &first_gc))
1248 has_content = 1;
1250 cx++;
1251 if (bit_test(s->tabs, cx))
1252 break;
1253 } while (cx < screen_size_x(s) - 1);
1255 width = cx - s->cx;
1256 if (has_content || width > sizeof gc.data.data)
1257 s->cx = cx;
1258 else {
1259 grid_get_cell(s->grid, s->cx, line, &gc);
1260 grid_set_tab(&gc, width);
1261 screen_write_collect_add(sctx, &gc);
1263 break;
1264 case '\012': /* LF */
1265 case '\013': /* VT */
1266 case '\014': /* FF */
1267 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1268 if (s->mode & MODE_CRLF)
1269 screen_write_carriagereturn(sctx);
1270 break;
1271 case '\015': /* CR */
1272 screen_write_carriagereturn(sctx);
1273 break;
1274 case '\016': /* SO */
1275 ictx->cell.set = 1;
1276 break;
1277 case '\017': /* SI */
1278 ictx->cell.set = 0;
1279 break;
1280 default:
1281 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1282 break;
1285 ictx->flags &= ~INPUT_LAST;
1286 return (0);
1289 /* Execute escape sequence. */
1290 static int
1291 input_esc_dispatch(struct input_ctx *ictx)
1293 struct screen_write_ctx *sctx = &ictx->ctx;
1294 struct screen *s = sctx->s;
1295 struct input_table_entry *entry;
1297 if (ictx->flags & INPUT_DISCARD)
1298 return (0);
1299 log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1301 entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
1302 sizeof input_esc_table[0], input_table_compare);
1303 if (entry == NULL) {
1304 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1305 return (0);
1308 switch (entry->type) {
1309 case INPUT_ESC_RIS:
1310 colour_palette_clear(ictx->palette);
1311 input_reset_cell(ictx);
1312 screen_write_reset(sctx);
1313 screen_write_fullredraw(sctx);
1314 break;
1315 case INPUT_ESC_IND:
1316 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1317 break;
1318 case INPUT_ESC_NEL:
1319 screen_write_carriagereturn(sctx);
1320 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1321 break;
1322 case INPUT_ESC_HTS:
1323 if (s->cx < screen_size_x(s))
1324 bit_set(s->tabs, s->cx);
1325 break;
1326 case INPUT_ESC_RI:
1327 screen_write_reverseindex(sctx, ictx->cell.cell.bg);
1328 break;
1329 case INPUT_ESC_DECKPAM:
1330 screen_write_mode_set(sctx, MODE_KKEYPAD);
1331 break;
1332 case INPUT_ESC_DECKPNM:
1333 screen_write_mode_clear(sctx, MODE_KKEYPAD);
1334 break;
1335 case INPUT_ESC_DECSC:
1336 input_save_state(ictx);
1337 break;
1338 case INPUT_ESC_DECRC:
1339 input_restore_state(ictx);
1340 break;
1341 case INPUT_ESC_DECALN:
1342 screen_write_alignmenttest(sctx);
1343 break;
1344 case INPUT_ESC_SCSG0_ON:
1345 ictx->cell.g0set = 1;
1346 break;
1347 case INPUT_ESC_SCSG0_OFF:
1348 ictx->cell.g0set = 0;
1349 break;
1350 case INPUT_ESC_SCSG1_ON:
1351 ictx->cell.g1set = 1;
1352 break;
1353 case INPUT_ESC_SCSG1_OFF:
1354 ictx->cell.g1set = 0;
1355 break;
1356 case INPUT_ESC_ST:
1357 /* ST terminates OSC but the state transition already did it. */
1358 break;
1361 ictx->flags &= ~INPUT_LAST;
1362 return (0);
1365 /* Execute control sequence. */
1366 static int
1367 input_csi_dispatch(struct input_ctx *ictx)
1369 struct screen_write_ctx *sctx = &ictx->ctx;
1370 struct screen *s = sctx->s;
1371 struct input_table_entry *entry;
1372 int i, n, m, ek, set;
1373 u_int cx, bg = ictx->cell.cell.bg;
1375 if (ictx->flags & INPUT_DISCARD)
1376 return (0);
1378 log_debug("%s: '%c' \"%s\" \"%s\"", __func__, ictx->ch,
1379 ictx->interm_buf, ictx->param_buf);
1381 if (input_split(ictx) != 0)
1382 return (0);
1384 entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
1385 sizeof input_csi_table[0], input_table_compare);
1386 if (entry == NULL) {
1387 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1388 return (0);
1391 switch (entry->type) {
1392 case INPUT_CSI_CBT:
1393 /* Find the previous tab point, n times. */
1394 cx = s->cx;
1395 if (cx > screen_size_x(s) - 1)
1396 cx = screen_size_x(s) - 1;
1397 n = input_get(ictx, 0, 1, 1);
1398 if (n == -1)
1399 break;
1400 while (cx > 0 && n-- > 0) {
1402 cx--;
1403 while (cx > 0 && !bit_test(s->tabs, cx));
1405 s->cx = cx;
1406 break;
1407 case INPUT_CSI_CUB:
1408 n = input_get(ictx, 0, 1, 1);
1409 if (n != -1)
1410 screen_write_cursorleft(sctx, n);
1411 break;
1412 case INPUT_CSI_CUD:
1413 n = input_get(ictx, 0, 1, 1);
1414 if (n != -1)
1415 screen_write_cursordown(sctx, n);
1416 break;
1417 case INPUT_CSI_CUF:
1418 n = input_get(ictx, 0, 1, 1);
1419 if (n != -1)
1420 screen_write_cursorright(sctx, n);
1421 break;
1422 case INPUT_CSI_CUP:
1423 n = input_get(ictx, 0, 1, 1);
1424 m = input_get(ictx, 1, 1, 1);
1425 if (n != -1 && m != -1)
1426 screen_write_cursormove(sctx, m - 1, n - 1, 1);
1427 break;
1428 case INPUT_CSI_MODSET:
1429 n = input_get(ictx, 0, 0, 0);
1430 if (n != 4)
1431 break;
1432 m = input_get(ictx, 1, 0, 0);
1435 * Set the extended key reporting mode as per the client
1436 * request, unless "extended-keys" is set to "off".
1438 ek = options_get_number(global_options, "extended-keys");
1439 if (ek == 0)
1440 break;
1441 screen_write_mode_clear(sctx, EXTENDED_KEY_MODES);
1442 if (m == 2)
1443 screen_write_mode_set(sctx, MODE_KEYS_EXTENDED_2);
1444 else if (m == 1 || ek == 2)
1445 screen_write_mode_set(sctx, MODE_KEYS_EXTENDED);
1446 break;
1447 case INPUT_CSI_MODOFF:
1448 n = input_get(ictx, 0, 0, 0);
1449 if (n != 4)
1450 break;
1453 * Clear the extended key reporting mode as per the client
1454 * request, unless "extended-keys always" forces into mode 1.
1456 screen_write_mode_clear(sctx,
1457 MODE_KEYS_EXTENDED|MODE_KEYS_EXTENDED_2);
1458 if (options_get_number(global_options, "extended-keys") == 2)
1459 screen_write_mode_set(sctx, MODE_KEYS_EXTENDED);
1460 break;
1461 case INPUT_CSI_WINOPS:
1462 input_csi_dispatch_winops(ictx);
1463 break;
1464 case INPUT_CSI_CUU:
1465 n = input_get(ictx, 0, 1, 1);
1466 if (n != -1)
1467 screen_write_cursorup(sctx, n);
1468 break;
1469 case INPUT_CSI_CNL:
1470 n = input_get(ictx, 0, 1, 1);
1471 if (n != -1) {
1472 screen_write_carriagereturn(sctx);
1473 screen_write_cursordown(sctx, n);
1475 break;
1476 case INPUT_CSI_CPL:
1477 n = input_get(ictx, 0, 1, 1);
1478 if (n != -1) {
1479 screen_write_carriagereturn(sctx);
1480 screen_write_cursorup(sctx, n);
1482 break;
1483 case INPUT_CSI_DA:
1484 switch (input_get(ictx, 0, 0, 0)) {
1485 case -1:
1486 break;
1487 case 0:
1488 #ifdef ENABLE_SIXEL
1489 input_reply(ictx, "\033[?1;2;4c");
1490 #else
1491 input_reply(ictx, "\033[?1;2c");
1492 #endif
1493 break;
1494 default:
1495 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1496 break;
1498 break;
1499 case INPUT_CSI_DA_TWO:
1500 switch (input_get(ictx, 0, 0, 0)) {
1501 case -1:
1502 break;
1503 case 0:
1504 input_reply(ictx, "\033[>84;0;0c");
1505 break;
1506 default:
1507 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1508 break;
1510 break;
1511 case INPUT_CSI_ECH:
1512 n = input_get(ictx, 0, 1, 1);
1513 if (n != -1)
1514 screen_write_clearcharacter(sctx, n, bg);
1515 break;
1516 case INPUT_CSI_DCH:
1517 n = input_get(ictx, 0, 1, 1);
1518 if (n != -1)
1519 screen_write_deletecharacter(sctx, n, bg);
1520 break;
1521 case INPUT_CSI_DECSTBM:
1522 n = input_get(ictx, 0, 1, 1);
1523 m = input_get(ictx, 1, 1, screen_size_y(s));
1524 if (n != -1 && m != -1)
1525 screen_write_scrollregion(sctx, n - 1, m - 1);
1526 break;
1527 case INPUT_CSI_DL:
1528 n = input_get(ictx, 0, 1, 1);
1529 if (n != -1)
1530 screen_write_deleteline(sctx, n, bg);
1531 break;
1532 case INPUT_CSI_DSR:
1533 switch (input_get(ictx, 0, 0, 0)) {
1534 case -1:
1535 break;
1536 case 5:
1537 input_reply(ictx, "\033[0n");
1538 break;
1539 case 6:
1540 input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1541 break;
1542 default:
1543 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1544 break;
1546 break;
1547 case INPUT_CSI_ED:
1548 switch (input_get(ictx, 0, 0, 0)) {
1549 case -1:
1550 break;
1551 case 0:
1552 screen_write_clearendofscreen(sctx, bg);
1553 break;
1554 case 1:
1555 screen_write_clearstartofscreen(sctx, bg);
1556 break;
1557 case 2:
1558 screen_write_clearscreen(sctx, bg);
1559 break;
1560 case 3:
1561 if (input_get(ictx, 1, 0, 0) == 0) {
1563 * Linux console extension to clear history
1564 * (for example before locking the screen).
1566 screen_write_clearhistory(sctx);
1568 break;
1569 default:
1570 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1571 break;
1573 break;
1574 case INPUT_CSI_EL:
1575 switch (input_get(ictx, 0, 0, 0)) {
1576 case -1:
1577 break;
1578 case 0:
1579 screen_write_clearendofline(sctx, bg);
1580 break;
1581 case 1:
1582 screen_write_clearstartofline(sctx, bg);
1583 break;
1584 case 2:
1585 screen_write_clearline(sctx, bg);
1586 break;
1587 default:
1588 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1589 break;
1591 break;
1592 case INPUT_CSI_HPA:
1593 n = input_get(ictx, 0, 1, 1);
1594 if (n != -1)
1595 screen_write_cursormove(sctx, n - 1, -1, 1);
1596 break;
1597 case INPUT_CSI_ICH:
1598 n = input_get(ictx, 0, 1, 1);
1599 if (n != -1)
1600 screen_write_insertcharacter(sctx, n, bg);
1601 break;
1602 case INPUT_CSI_IL:
1603 n = input_get(ictx, 0, 1, 1);
1604 if (n != -1)
1605 screen_write_insertline(sctx, n, bg);
1606 break;
1607 case INPUT_CSI_REP:
1608 n = input_get(ictx, 0, 1, 1);
1609 if (n == -1)
1610 break;
1612 m = screen_size_x(s) - s->cx;
1613 if (n > m)
1614 n = m;
1616 if (~ictx->flags & INPUT_LAST)
1617 break;
1619 set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1620 if (set == 1)
1621 ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1622 else
1623 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1624 utf8_copy(&ictx->cell.cell.data, &ictx->last);
1625 for (i = 0; i < n; i++)
1626 screen_write_collect_add(sctx, &ictx->cell.cell);
1627 break;
1628 case INPUT_CSI_RCP:
1629 input_restore_state(ictx);
1630 break;
1631 case INPUT_CSI_RM:
1632 input_csi_dispatch_rm(ictx);
1633 break;
1634 case INPUT_CSI_RM_PRIVATE:
1635 input_csi_dispatch_rm_private(ictx);
1636 break;
1637 case INPUT_CSI_SCP:
1638 input_save_state(ictx);
1639 break;
1640 case INPUT_CSI_SGR:
1641 input_csi_dispatch_sgr(ictx);
1642 break;
1643 case INPUT_CSI_SM:
1644 input_csi_dispatch_sm(ictx);
1645 break;
1646 case INPUT_CSI_SM_PRIVATE:
1647 input_csi_dispatch_sm_private(ictx);
1648 break;
1649 case INPUT_CSI_SM_GRAPHICS:
1650 input_csi_dispatch_sm_graphics(ictx);
1651 break;
1652 case INPUT_CSI_SU:
1653 n = input_get(ictx, 0, 1, 1);
1654 if (n != -1)
1655 screen_write_scrollup(sctx, n, bg);
1656 break;
1657 case INPUT_CSI_SD:
1658 n = input_get(ictx, 0, 1, 1);
1659 if (n != -1)
1660 screen_write_scrolldown(sctx, n, bg);
1661 break;
1662 case INPUT_CSI_TBC:
1663 switch (input_get(ictx, 0, 0, 0)) {
1664 case -1:
1665 break;
1666 case 0:
1667 if (s->cx < screen_size_x(s))
1668 bit_clear(s->tabs, s->cx);
1669 break;
1670 case 3:
1671 bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1672 break;
1673 default:
1674 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1675 break;
1677 break;
1678 case INPUT_CSI_VPA:
1679 n = input_get(ictx, 0, 1, 1);
1680 if (n != -1)
1681 screen_write_cursormove(sctx, -1, n - 1, 1);
1682 break;
1683 case INPUT_CSI_DECSCUSR:
1684 n = input_get(ictx, 0, 0, 0);
1685 if (n != -1)
1686 screen_set_cursor_style(n, &s->cstyle, &s->mode);
1687 break;
1688 case INPUT_CSI_XDA:
1689 n = input_get(ictx, 0, 0, 0);
1690 if (n == 0)
1691 input_reply(ictx, "\033P>|tmux %s\033\\", getversion());
1692 break;
1696 ictx->flags &= ~INPUT_LAST;
1697 return (0);
1700 /* Handle CSI RM. */
1701 static void
1702 input_csi_dispatch_rm(struct input_ctx *ictx)
1704 struct screen_write_ctx *sctx = &ictx->ctx;
1705 u_int i;
1707 for (i = 0; i < ictx->param_list_len; i++) {
1708 switch (input_get(ictx, i, 0, -1)) {
1709 case -1:
1710 break;
1711 case 4: /* IRM */
1712 screen_write_mode_clear(sctx, MODE_INSERT);
1713 break;
1714 case 34:
1715 screen_write_mode_set(sctx, MODE_CURSOR_VERY_VISIBLE);
1716 break;
1717 default:
1718 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1719 break;
1724 /* Handle CSI private RM. */
1725 static void
1726 input_csi_dispatch_rm_private(struct input_ctx *ictx)
1728 struct screen_write_ctx *sctx = &ictx->ctx;
1729 struct grid_cell *gc = &ictx->cell.cell;
1730 u_int i;
1732 for (i = 0; i < ictx->param_list_len; i++) {
1733 switch (input_get(ictx, i, 0, -1)) {
1734 case -1:
1735 break;
1736 case 1: /* DECCKM */
1737 screen_write_mode_clear(sctx, MODE_KCURSOR);
1738 break;
1739 case 3: /* DECCOLM */
1740 screen_write_cursormove(sctx, 0, 0, 1);
1741 screen_write_clearscreen(sctx, gc->bg);
1742 break;
1743 case 6: /* DECOM */
1744 screen_write_mode_clear(sctx, MODE_ORIGIN);
1745 screen_write_cursormove(sctx, 0, 0, 1);
1746 break;
1747 case 7: /* DECAWM */
1748 screen_write_mode_clear(sctx, MODE_WRAP);
1749 break;
1750 case 12:
1751 screen_write_mode_clear(sctx, MODE_CURSOR_BLINKING);
1752 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET);
1753 break;
1754 case 25: /* TCEM */
1755 screen_write_mode_clear(sctx, MODE_CURSOR);
1756 break;
1757 case 1000:
1758 case 1001:
1759 case 1002:
1760 case 1003:
1761 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1762 break;
1763 case 1004:
1764 screen_write_mode_clear(sctx, MODE_FOCUSON);
1765 break;
1766 case 1005:
1767 screen_write_mode_clear(sctx, MODE_MOUSE_UTF8);
1768 break;
1769 case 1006:
1770 screen_write_mode_clear(sctx, MODE_MOUSE_SGR);
1771 break;
1772 case 47:
1773 case 1047:
1774 screen_write_alternateoff(sctx, gc, 0);
1775 break;
1776 case 1049:
1777 screen_write_alternateoff(sctx, gc, 1);
1778 break;
1779 case 2004:
1780 screen_write_mode_clear(sctx, MODE_BRACKETPASTE);
1781 break;
1782 default:
1783 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1784 break;
1789 /* Handle CSI SM. */
1790 static void
1791 input_csi_dispatch_sm(struct input_ctx *ictx)
1793 struct screen_write_ctx *sctx = &ictx->ctx;
1794 u_int i;
1796 for (i = 0; i < ictx->param_list_len; i++) {
1797 switch (input_get(ictx, i, 0, -1)) {
1798 case -1:
1799 break;
1800 case 4: /* IRM */
1801 screen_write_mode_set(sctx, MODE_INSERT);
1802 break;
1803 case 34:
1804 screen_write_mode_clear(sctx, MODE_CURSOR_VERY_VISIBLE);
1805 break;
1806 default:
1807 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1808 break;
1813 /* Handle CSI private SM. */
1814 static void
1815 input_csi_dispatch_sm_private(struct input_ctx *ictx)
1817 struct screen_write_ctx *sctx = &ictx->ctx;
1818 struct grid_cell *gc = &ictx->cell.cell;
1819 u_int i;
1821 for (i = 0; i < ictx->param_list_len; i++) {
1822 switch (input_get(ictx, i, 0, -1)) {
1823 case -1:
1824 break;
1825 case 1: /* DECCKM */
1826 screen_write_mode_set(sctx, MODE_KCURSOR);
1827 break;
1828 case 3: /* DECCOLM */
1829 screen_write_cursormove(sctx, 0, 0, 1);
1830 screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1831 break;
1832 case 6: /* DECOM */
1833 screen_write_mode_set(sctx, MODE_ORIGIN);
1834 screen_write_cursormove(sctx, 0, 0, 1);
1835 break;
1836 case 7: /* DECAWM */
1837 screen_write_mode_set(sctx, MODE_WRAP);
1838 break;
1839 case 12:
1840 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING);
1841 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET);
1842 break;
1843 case 25: /* TCEM */
1844 screen_write_mode_set(sctx, MODE_CURSOR);
1845 break;
1846 case 1000:
1847 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1848 screen_write_mode_set(sctx, MODE_MOUSE_STANDARD);
1849 break;
1850 case 1002:
1851 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1852 screen_write_mode_set(sctx, MODE_MOUSE_BUTTON);
1853 break;
1854 case 1003:
1855 screen_write_mode_clear(sctx, ALL_MOUSE_MODES);
1856 screen_write_mode_set(sctx, MODE_MOUSE_ALL);
1857 break;
1858 case 1004:
1859 screen_write_mode_set(sctx, MODE_FOCUSON);
1860 break;
1861 case 1005:
1862 screen_write_mode_set(sctx, MODE_MOUSE_UTF8);
1863 break;
1864 case 1006:
1865 screen_write_mode_set(sctx, MODE_MOUSE_SGR);
1866 break;
1867 case 47:
1868 case 1047:
1869 screen_write_alternateon(sctx, gc, 0);
1870 break;
1871 case 1049:
1872 screen_write_alternateon(sctx, gc, 1);
1873 break;
1874 case 2004:
1875 screen_write_mode_set(sctx, MODE_BRACKETPASTE);
1876 break;
1877 default:
1878 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1879 break;
1884 /* Handle CSI graphics SM. */
1885 static void
1886 input_csi_dispatch_sm_graphics(__unused struct input_ctx *ictx)
1888 #ifdef ENABLE_SIXEL
1889 int n, m, o;
1891 if (ictx->param_list_len > 3)
1892 return;
1893 n = input_get(ictx, 0, 0, 0);
1894 m = input_get(ictx, 1, 0, 0);
1895 o = input_get(ictx, 2, 0, 0);
1897 if (n == 1 && (m == 1 || m == 2 || m == 4))
1898 input_reply(ictx, "\033[?%d;0;%uS", n, SIXEL_COLOUR_REGISTERS);
1899 else
1900 input_reply(ictx, "\033[?%d;3;%dS", n, o);
1901 #endif
1904 /* Handle CSI window operations. */
1905 static void
1906 input_csi_dispatch_winops(struct input_ctx *ictx)
1908 struct screen_write_ctx *sctx = &ictx->ctx;
1909 struct screen *s = sctx->s;
1910 struct window_pane *wp = ictx->wp;
1911 struct window *w = NULL;
1912 u_int x = screen_size_x(s), y = screen_size_y(s);
1913 int n, m;
1915 if (wp != NULL)
1916 w = wp->window;
1918 m = 0;
1919 while ((n = input_get(ictx, m, 0, -1)) != -1) {
1920 switch (n) {
1921 case 1:
1922 case 2:
1923 case 5:
1924 case 6:
1925 case 7:
1926 case 11:
1927 case 13:
1928 case 20:
1929 case 21:
1930 case 24:
1931 break;
1932 case 3:
1933 case 4:
1934 case 8:
1935 m++;
1936 if (input_get(ictx, m, 0, -1) == -1)
1937 return;
1938 /* FALLTHROUGH */
1939 case 9:
1940 case 10:
1941 m++;
1942 if (input_get(ictx, m, 0, -1) == -1)
1943 return;
1944 break;
1945 case 14:
1946 if (w == NULL)
1947 break;
1948 input_reply(ictx, "\033[4;%u;%ut", y * w->ypixel,
1949 x * w->xpixel);
1950 break;
1951 case 15:
1952 if (w == NULL)
1953 break;
1954 input_reply(ictx, "\033[5;%u;%ut", y * w->ypixel,
1955 x * w->xpixel);
1956 break;
1957 case 16:
1958 if (w == NULL)
1959 break;
1960 input_reply(ictx, "\033[6;%u;%ut", w->ypixel,
1961 w->xpixel);
1962 break;
1963 case 18:
1964 input_reply(ictx, "\033[8;%u;%ut", y, x);
1965 break;
1966 case 19:
1967 input_reply(ictx, "\033[9;%u;%ut", y, x);
1968 break;
1969 case 22:
1970 m++;
1971 switch (input_get(ictx, m, 0, -1)) {
1972 case -1:
1973 return;
1974 case 0:
1975 case 2:
1976 screen_push_title(sctx->s);
1977 break;
1979 break;
1980 case 23:
1981 m++;
1982 switch (input_get(ictx, m, 0, -1)) {
1983 case -1:
1984 return;
1985 case 0:
1986 case 2:
1987 screen_pop_title(sctx->s);
1988 if (wp == NULL)
1989 break;
1990 notify_pane("pane-title-changed", wp);
1991 server_redraw_window_borders(w);
1992 server_status_window(w);
1993 break;
1995 break;
1996 default:
1997 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1998 break;
2000 m++;
2004 /* Helper for 256 colour SGR. */
2005 static int
2006 input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
2008 struct grid_cell *gc = &ictx->cell.cell;
2010 if (c == -1 || c > 255) {
2011 if (fgbg == 38)
2012 gc->fg = 8;
2013 else if (fgbg == 48)
2014 gc->bg = 8;
2015 } else {
2016 if (fgbg == 38)
2017 gc->fg = c | COLOUR_FLAG_256;
2018 else if (fgbg == 48)
2019 gc->bg = c | COLOUR_FLAG_256;
2020 else if (fgbg == 58)
2021 gc->us = c | COLOUR_FLAG_256;
2023 return (1);
2026 /* Handle CSI SGR for 256 colours. */
2027 static void
2028 input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
2030 int c;
2032 c = input_get(ictx, (*i) + 1, 0, -1);
2033 if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
2034 (*i)++;
2037 /* Helper for RGB colour SGR. */
2038 static int
2039 input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
2040 int b)
2042 struct grid_cell *gc = &ictx->cell.cell;
2044 if (r == -1 || r > 255)
2045 return (0);
2046 if (g == -1 || g > 255)
2047 return (0);
2048 if (b == -1 || b > 255)
2049 return (0);
2051 if (fgbg == 38)
2052 gc->fg = colour_join_rgb(r, g, b);
2053 else if (fgbg == 48)
2054 gc->bg = colour_join_rgb(r, g, b);
2055 else if (fgbg == 58)
2056 gc->us = colour_join_rgb(r, g, b);
2057 return (1);
2060 /* Handle CSI SGR for RGB colours. */
2061 static void
2062 input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
2064 int r, g, b;
2066 r = input_get(ictx, (*i) + 1, 0, -1);
2067 g = input_get(ictx, (*i) + 2, 0, -1);
2068 b = input_get(ictx, (*i) + 3, 0, -1);
2069 if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
2070 (*i) += 3;
2073 /* Handle CSI SGR with a ISO parameter. */
2074 static void
2075 input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
2077 struct grid_cell *gc = &ictx->cell.cell;
2078 char *s = ictx->param_list[i].str, *copy, *ptr, *out;
2079 int p[8];
2080 u_int n;
2081 const char *errstr;
2083 for (n = 0; n < nitems(p); n++)
2084 p[n] = -1;
2085 n = 0;
2087 ptr = copy = xstrdup(s);
2088 while ((out = strsep(&ptr, ":")) != NULL) {
2089 if (*out != '\0') {
2090 p[n++] = strtonum(out, 0, INT_MAX, &errstr);
2091 if (errstr != NULL || n == nitems(p)) {
2092 free(copy);
2093 return;
2095 } else {
2096 n++;
2097 if (n == nitems(p)) {
2098 free(copy);
2099 return;
2102 log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
2104 free(copy);
2106 if (n == 0)
2107 return;
2108 if (p[0] == 4) {
2109 if (n != 2)
2110 return;
2111 switch (p[1]) {
2112 case 0:
2113 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2114 break;
2115 case 1:
2116 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2117 gc->attr |= GRID_ATTR_UNDERSCORE;
2118 break;
2119 case 2:
2120 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2121 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2122 break;
2123 case 3:
2124 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2125 gc->attr |= GRID_ATTR_UNDERSCORE_3;
2126 break;
2127 case 4:
2128 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2129 gc->attr |= GRID_ATTR_UNDERSCORE_4;
2130 break;
2131 case 5:
2132 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2133 gc->attr |= GRID_ATTR_UNDERSCORE_5;
2134 break;
2136 return;
2138 if (n < 2 || (p[0] != 38 && p[0] != 48 && p[0] != 58))
2139 return;
2140 switch (p[1]) {
2141 case 2:
2142 if (n < 3)
2143 break;
2144 if (n == 5)
2145 i = 2;
2146 else
2147 i = 3;
2148 if (n < i + 3)
2149 break;
2150 input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
2151 p[i + 2]);
2152 break;
2153 case 5:
2154 if (n < 3)
2155 break;
2156 input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
2157 break;
2161 /* Handle CSI SGR. */
2162 static void
2163 input_csi_dispatch_sgr(struct input_ctx *ictx)
2165 struct grid_cell *gc = &ictx->cell.cell;
2166 u_int i, link;
2167 int n;
2169 if (ictx->param_list_len == 0) {
2170 memcpy(gc, &grid_default_cell, sizeof *gc);
2171 return;
2174 for (i = 0; i < ictx->param_list_len; i++) {
2175 if (ictx->param_list[i].type == INPUT_STRING) {
2176 input_csi_dispatch_sgr_colon(ictx, i);
2177 continue;
2179 n = input_get(ictx, i, 0, 0);
2180 if (n == -1)
2181 continue;
2183 if (n == 38 || n == 48 || n == 58) {
2184 i++;
2185 switch (input_get(ictx, i, 0, -1)) {
2186 case 2:
2187 input_csi_dispatch_sgr_rgb(ictx, n, &i);
2188 break;
2189 case 5:
2190 input_csi_dispatch_sgr_256(ictx, n, &i);
2191 break;
2193 continue;
2196 switch (n) {
2197 case 0:
2198 link = gc->link;
2199 memcpy(gc, &grid_default_cell, sizeof *gc);
2200 gc->link = link;
2201 break;
2202 case 1:
2203 gc->attr |= GRID_ATTR_BRIGHT;
2204 break;
2205 case 2:
2206 gc->attr |= GRID_ATTR_DIM;
2207 break;
2208 case 3:
2209 gc->attr |= GRID_ATTR_ITALICS;
2210 break;
2211 case 4:
2212 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2213 gc->attr |= GRID_ATTR_UNDERSCORE;
2214 break;
2215 case 5:
2216 case 6:
2217 gc->attr |= GRID_ATTR_BLINK;
2218 break;
2219 case 7:
2220 gc->attr |= GRID_ATTR_REVERSE;
2221 break;
2222 case 8:
2223 gc->attr |= GRID_ATTR_HIDDEN;
2224 break;
2225 case 9:
2226 gc->attr |= GRID_ATTR_STRIKETHROUGH;
2227 break;
2228 case 21:
2229 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2230 gc->attr |= GRID_ATTR_UNDERSCORE_2;
2231 break;
2232 case 22:
2233 gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
2234 break;
2235 case 23:
2236 gc->attr &= ~GRID_ATTR_ITALICS;
2237 break;
2238 case 24:
2239 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE;
2240 break;
2241 case 25:
2242 gc->attr &= ~GRID_ATTR_BLINK;
2243 break;
2244 case 27:
2245 gc->attr &= ~GRID_ATTR_REVERSE;
2246 break;
2247 case 28:
2248 gc->attr &= ~GRID_ATTR_HIDDEN;
2249 break;
2250 case 29:
2251 gc->attr &= ~GRID_ATTR_STRIKETHROUGH;
2252 break;
2253 case 30:
2254 case 31:
2255 case 32:
2256 case 33:
2257 case 34:
2258 case 35:
2259 case 36:
2260 case 37:
2261 gc->fg = n - 30;
2262 break;
2263 case 39:
2264 gc->fg = 8;
2265 break;
2266 case 40:
2267 case 41:
2268 case 42:
2269 case 43:
2270 case 44:
2271 case 45:
2272 case 46:
2273 case 47:
2274 gc->bg = n - 40;
2275 break;
2276 case 49:
2277 gc->bg = 8;
2278 break;
2279 case 53:
2280 gc->attr |= GRID_ATTR_OVERLINE;
2281 break;
2282 case 55:
2283 gc->attr &= ~GRID_ATTR_OVERLINE;
2284 break;
2285 case 59:
2286 gc->us = 8;
2287 break;
2288 case 90:
2289 case 91:
2290 case 92:
2291 case 93:
2292 case 94:
2293 case 95:
2294 case 96:
2295 case 97:
2296 gc->fg = n;
2297 break;
2298 case 100:
2299 case 101:
2300 case 102:
2301 case 103:
2302 case 104:
2303 case 105:
2304 case 106:
2305 case 107:
2306 gc->bg = n - 10;
2307 break;
2312 /* End of input with BEL. */
2313 static int
2314 input_end_bel(struct input_ctx *ictx)
2316 log_debug("%s", __func__);
2318 ictx->input_end = INPUT_END_BEL;
2320 return (0);
2323 /* DCS string started. */
2324 static void
2325 input_enter_dcs(struct input_ctx *ictx)
2327 log_debug("%s", __func__);
2329 input_clear(ictx);
2330 input_start_timer(ictx);
2331 ictx->flags &= ~INPUT_LAST;
2334 /* DCS terminator (ST) received. */
2335 static int
2336 input_dcs_dispatch(struct input_ctx *ictx)
2338 struct window_pane *wp = ictx->wp;
2339 struct screen_write_ctx *sctx = &ictx->ctx;
2340 u_char *buf = ictx->input_buf;
2341 size_t len = ictx->input_len;
2342 const char prefix[] = "tmux;";
2343 const u_int prefixlen = (sizeof prefix) - 1;
2344 long long allow_passthrough = 0;
2345 #ifdef ENABLE_SIXEL
2346 struct window *w;
2347 struct sixel_image *si;
2348 int p2;
2349 #endif
2351 if (wp == NULL)
2352 return (0);
2354 if (ictx->flags & INPUT_DISCARD) {
2355 log_debug("%s: %zu bytes (discard)", __func__, len);
2356 return (0);
2359 #ifdef ENABLE_SIXEL
2360 w = wp->window;
2361 if (buf[0] == 'q') {
2362 if (input_split(ictx) != 0)
2363 return (0);
2364 p2 = input_get(ictx, 1, 0, 0);
2365 if (p2 == -1)
2366 p2 = 0;
2367 si = sixel_parse(buf, len, p2, w->xpixel, w->ypixel);
2368 if (si != NULL)
2369 screen_write_sixelimage(sctx, si, ictx->cell.cell.bg);
2371 #endif
2373 allow_passthrough = options_get_number(wp->options, "allow-passthrough");
2374 if (!allow_passthrough)
2375 return (0);
2376 log_debug("%s: \"%s\"", __func__, buf);
2378 if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0) {
2379 screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen,
2380 allow_passthrough == 2);
2383 return (0);
2386 /* OSC string started. */
2387 static void
2388 input_enter_osc(struct input_ctx *ictx)
2390 log_debug("%s", __func__);
2392 input_clear(ictx);
2393 input_start_timer(ictx);
2394 ictx->flags &= ~INPUT_LAST;
2397 /* OSC terminator (ST) received. */
2398 static void
2399 input_exit_osc(struct input_ctx *ictx)
2401 struct screen_write_ctx *sctx = &ictx->ctx;
2402 struct window_pane *wp = ictx->wp;
2403 u_char *p = ictx->input_buf;
2404 u_int option;
2406 if (ictx->flags & INPUT_DISCARD)
2407 return;
2408 if (ictx->input_len < 1 || *p < '0' || *p > '9')
2409 return;
2411 log_debug("%s: \"%s\" (end %s)", __func__, p,
2412 ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2414 option = 0;
2415 while (*p >= '0' && *p <= '9')
2416 option = option * 10 + *p++ - '0';
2417 if (*p != ';' && *p != '\0')
2418 return;
2419 if (*p == ';')
2420 p++;
2422 switch (option) {
2423 case 0:
2424 case 2:
2425 if (wp != NULL &&
2426 options_get_number(wp->options, "allow-set-title") &&
2427 screen_set_title(sctx->s, p)) {
2428 notify_pane("pane-title-changed", wp);
2429 server_redraw_window_borders(wp->window);
2430 server_status_window(wp->window);
2432 break;
2433 case 4:
2434 input_osc_4(ictx, p);
2435 break;
2436 case 7:
2437 if (utf8_isvalid(p)) {
2438 screen_set_path(sctx->s, p);
2439 if (wp != NULL) {
2440 server_redraw_window_borders(wp->window);
2441 server_status_window(wp->window);
2444 break;
2445 case 8:
2446 input_osc_8(ictx, p);
2447 break;
2448 case 10:
2449 input_osc_10(ictx, p);
2450 break;
2451 case 11:
2452 input_osc_11(ictx, p);
2453 break;
2454 case 12:
2455 input_osc_12(ictx, p);
2456 break;
2457 case 52:
2458 input_osc_52(ictx, p);
2459 break;
2460 case 104:
2461 input_osc_104(ictx, p);
2462 break;
2463 case 110:
2464 input_osc_110(ictx, p);
2465 break;
2466 case 111:
2467 input_osc_111(ictx, p);
2468 break;
2469 case 112:
2470 input_osc_112(ictx, p);
2471 break;
2472 case 133:
2473 input_osc_133(ictx, p);
2474 break;
2475 default:
2476 log_debug("%s: unknown '%u'", __func__, option);
2477 break;
2481 /* APC string started. */
2482 static void
2483 input_enter_apc(struct input_ctx *ictx)
2485 log_debug("%s", __func__);
2487 input_clear(ictx);
2488 input_start_timer(ictx);
2489 ictx->flags &= ~INPUT_LAST;
2492 /* APC terminator (ST) received. */
2493 static void
2494 input_exit_apc(struct input_ctx *ictx)
2496 struct screen_write_ctx *sctx = &ictx->ctx;
2497 struct window_pane *wp = ictx->wp;
2499 if (ictx->flags & INPUT_DISCARD)
2500 return;
2501 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2503 if (screen_set_title(sctx->s, ictx->input_buf) && wp != NULL) {
2504 notify_pane("pane-title-changed", wp);
2505 server_redraw_window_borders(wp->window);
2506 server_status_window(wp->window);
2510 /* Rename string started. */
2511 static void
2512 input_enter_rename(struct input_ctx *ictx)
2514 log_debug("%s", __func__);
2516 input_clear(ictx);
2517 input_start_timer(ictx);
2518 ictx->flags &= ~INPUT_LAST;
2521 /* Rename terminator (ST) received. */
2522 static void
2523 input_exit_rename(struct input_ctx *ictx)
2525 struct window_pane *wp = ictx->wp;
2526 struct window *w;
2527 struct options_entry *o;
2529 if (wp == NULL)
2530 return;
2531 if (ictx->flags & INPUT_DISCARD)
2532 return;
2533 if (!options_get_number(ictx->wp->options, "allow-rename"))
2534 return;
2535 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2537 if (!utf8_isvalid(ictx->input_buf))
2538 return;
2539 w = wp->window;
2541 if (ictx->input_len == 0) {
2542 o = options_get_only(w->options, "automatic-rename");
2543 if (o != NULL)
2544 options_remove_or_default(o, -1, NULL);
2545 if (!options_get_number(w->options, "automatic-rename"))
2546 window_set_name(w, "");
2547 } else {
2548 options_set_number(w->options, "automatic-rename", 0);
2549 window_set_name(w, ictx->input_buf);
2551 server_redraw_window_borders(w);
2552 server_status_window(w);
2555 /* Open UTF-8 character. */
2556 static int
2557 input_top_bit_set(struct input_ctx *ictx)
2559 struct screen_write_ctx *sctx = &ictx->ctx;
2560 struct utf8_data *ud = &ictx->utf8data;
2562 ictx->flags &= ~INPUT_LAST;
2564 if (!ictx->utf8started) {
2565 if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2566 return (0);
2567 ictx->utf8started = 1;
2568 return (0);
2571 switch (utf8_append(ud, ictx->ch)) {
2572 case UTF8_MORE:
2573 return (0);
2574 case UTF8_ERROR:
2575 ictx->utf8started = 0;
2576 return (0);
2577 case UTF8_DONE:
2578 break;
2580 ictx->utf8started = 0;
2582 log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2583 (int)ud->size, ud->data, ud->width);
2585 utf8_copy(&ictx->cell.cell.data, ud);
2586 screen_write_collect_add(sctx, &ictx->cell.cell);
2588 utf8_copy(&ictx->last, &ictx->cell.cell.data);
2589 ictx->flags |= INPUT_LAST;
2591 return (0);
2594 /* Reply to a colour request. */
2595 static void
2596 input_osc_colour_reply(struct input_ctx *ictx, u_int n, int c)
2598 u_char r, g, b;
2599 const char *end;
2601 if (c != -1)
2602 c = colour_force_rgb(c);
2603 if (c == -1)
2604 return;
2605 colour_split_rgb(c, &r, &g, &b);
2607 if (ictx->input_end == INPUT_END_BEL)
2608 end = "\007";
2609 else
2610 end = "\033\\";
2611 input_reply(ictx, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2612 n, r, r, g, g, b, b, end);
2615 /* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2616 static void
2617 input_osc_4(struct input_ctx *ictx, const char *p)
2619 char *copy, *s, *next = NULL;
2620 long idx;
2621 int c, bad = 0, redraw = 0;
2623 copy = s = xstrdup(p);
2624 while (s != NULL && *s != '\0') {
2625 idx = strtol(s, &next, 10);
2626 if (*next++ != ';') {
2627 bad = 1;
2628 break;
2630 if (idx < 0 || idx >= 256) {
2631 bad = 1;
2632 break;
2635 s = strsep(&next, ";");
2636 if (strcmp(s, "?") == 0) {
2637 c = colour_palette_get(ictx->palette, idx);
2638 if (c != -1)
2639 input_osc_colour_reply(ictx, 4, c);
2640 continue;
2642 if ((c = colour_parseX11(s)) == -1) {
2643 s = next;
2644 continue;
2646 if (colour_palette_set(ictx->palette, idx, c))
2647 redraw = 1;
2648 s = next;
2650 if (bad)
2651 log_debug("bad OSC 4: %s", p);
2652 if (redraw)
2653 screen_write_fullredraw(&ictx->ctx);
2654 free(copy);
2657 /* Handle the OSC 8 sequence for embedding hyperlinks. */
2658 static void
2659 input_osc_8(struct input_ctx *ictx, const char *p)
2661 struct hyperlinks *hl = ictx->ctx.s->hyperlinks;
2662 struct grid_cell *gc = &ictx->cell.cell;
2663 const char *start, *end, *uri;
2664 char *id = NULL;
2666 for (start = p; (end = strpbrk(start, ":;")) != NULL; start = end + 1) {
2667 if (end - start >= 4 && strncmp(start, "id=", 3) == 0) {
2668 if (id != NULL)
2669 goto bad;
2670 id = xstrndup(start + 3, end - start - 3);
2673 /* The first ; is the end of parameters and start of the URI. */
2674 if (*end == ';')
2675 break;
2677 if (end == NULL || *end != ';')
2678 goto bad;
2679 uri = end + 1;
2680 if (*uri == '\0') {
2681 gc->link = 0;
2682 free(id);
2683 return;
2685 gc->link = hyperlinks_put(hl, uri, id);
2686 if (id == NULL)
2687 log_debug("hyperlink (anonymous) %s = %u", uri, gc->link);
2688 else
2689 log_debug("hyperlink (id=%s) %s = %u", id, uri, gc->link);
2690 free(id);
2691 return;
2693 bad:
2694 log_debug("bad OSC 8 %s", p);
2695 free(id);
2699 * Get a client with a foreground for the pane. There isn't much to choose
2700 * between them so just use the first.
2702 static int
2703 input_get_fg_client(struct window_pane *wp)
2705 struct window *w = wp->window;
2706 struct client *loop;
2708 TAILQ_FOREACH(loop, &clients, entry) {
2709 if (loop->flags & CLIENT_UNATTACHEDFLAGS)
2710 continue;
2711 if (loop->session == NULL || !session_has(loop->session, w))
2712 continue;
2713 if (loop->tty.fg == -1)
2714 continue;
2715 return (loop->tty.fg);
2717 return (-1);
2720 /* Get a client with a background for the pane. */
2721 static int
2722 input_get_bg_client(struct window_pane *wp)
2724 struct window *w = wp->window;
2725 struct client *loop;
2727 TAILQ_FOREACH(loop, &clients, entry) {
2728 if (loop->flags & CLIENT_UNATTACHEDFLAGS)
2729 continue;
2730 if (loop->session == NULL || !session_has(loop->session, w))
2731 continue;
2732 if (loop->tty.bg == -1)
2733 continue;
2734 return (loop->tty.bg);
2736 return (-1);
2740 * If any control mode client exists that has provided a bg color, return it.
2741 * Otherwise, return -1.
2743 static int
2744 input_get_bg_control_client(struct window_pane *wp)
2746 struct client *c;
2748 if (wp->control_bg == -1)
2749 return (-1);
2751 TAILQ_FOREACH(c, &clients, entry) {
2752 if (c->flags & CLIENT_CONTROL)
2753 return (wp->control_bg);
2755 return (-1);
2759 * If any control mode client exists that has provided a fg color, return it.
2760 * Otherwise, return -1.
2762 static int
2763 input_get_fg_control_client(struct window_pane *wp)
2765 struct client *c;
2767 if (wp->control_fg == -1)
2768 return (-1);
2770 TAILQ_FOREACH(c, &clients, entry) {
2771 if (c->flags & CLIENT_CONTROL)
2772 return (wp->control_fg);
2774 return (-1);
2777 /* Handle the OSC 10 sequence for setting and querying foreground colour. */
2778 static void
2779 input_osc_10(struct input_ctx *ictx, const char *p)
2781 struct window_pane *wp = ictx->wp;
2782 struct grid_cell defaults;
2783 int c;
2785 if (strcmp(p, "?") == 0) {
2786 if (wp == NULL)
2787 return;
2788 c = input_get_fg_control_client(wp);
2789 if (c == -1) {
2790 tty_default_colours(&defaults, wp);
2791 if (COLOUR_DEFAULT(defaults.fg))
2792 c = input_get_fg_client(wp);
2793 else
2794 c = defaults.fg;
2796 input_osc_colour_reply(ictx, 10, c);
2797 return;
2800 if ((c = colour_parseX11(p)) == -1) {
2801 log_debug("bad OSC 10: %s", p);
2802 return;
2804 if (ictx->palette != NULL) {
2805 ictx->palette->fg = c;
2806 if (wp != NULL)
2807 wp->flags |= PANE_STYLECHANGED;
2808 screen_write_fullredraw(&ictx->ctx);
2812 /* Handle the OSC 110 sequence for resetting foreground colour. */
2813 static void
2814 input_osc_110(struct input_ctx *ictx, const char *p)
2816 struct window_pane *wp = ictx->wp;
2818 if (*p != '\0')
2819 return;
2820 if (ictx->palette != NULL) {
2821 ictx->palette->fg = 8;
2822 if (wp != NULL)
2823 wp->flags |= PANE_STYLECHANGED;
2824 screen_write_fullredraw(&ictx->ctx);
2828 /* Handle the OSC 11 sequence for setting and querying background colour. */
2829 static void
2830 input_osc_11(struct input_ctx *ictx, const char *p)
2832 struct window_pane *wp = ictx->wp;
2833 struct grid_cell defaults;
2834 int c;
2836 if (strcmp(p, "?") == 0) {
2837 if (wp == NULL)
2838 return;
2839 c = input_get_bg_control_client(wp);
2840 if (c == -1) {
2841 tty_default_colours(&defaults, wp);
2842 if (COLOUR_DEFAULT(defaults.bg))
2843 c = input_get_bg_client(wp);
2844 else
2845 c = defaults.bg;
2847 input_osc_colour_reply(ictx, 11, c);
2848 return;
2851 if ((c = colour_parseX11(p)) == -1) {
2852 log_debug("bad OSC 11: %s", p);
2853 return;
2855 if (ictx->palette != NULL) {
2856 ictx->palette->bg = c;
2857 if (wp != NULL)
2858 wp->flags |= PANE_STYLECHANGED;
2859 screen_write_fullredraw(&ictx->ctx);
2863 /* Handle the OSC 111 sequence for resetting background colour. */
2864 static void
2865 input_osc_111(struct input_ctx *ictx, const char *p)
2867 struct window_pane *wp = ictx->wp;
2869 if (*p != '\0')
2870 return;
2871 if (ictx->palette != NULL) {
2872 ictx->palette->bg = 8;
2873 if (wp != NULL)
2874 wp->flags |= PANE_STYLECHANGED;
2875 screen_write_fullredraw(&ictx->ctx);
2879 /* Handle the OSC 12 sequence for setting and querying cursor colour. */
2880 static void
2881 input_osc_12(struct input_ctx *ictx, const char *p)
2883 struct window_pane *wp = ictx->wp;
2884 int c;
2886 if (strcmp(p, "?") == 0) {
2887 if (wp != NULL) {
2888 c = ictx->ctx.s->ccolour;
2889 if (c == -1)
2890 c = ictx->ctx.s->default_ccolour;
2891 input_osc_colour_reply(ictx, 12, c);
2893 return;
2896 if ((c = colour_parseX11(p)) == -1) {
2897 log_debug("bad OSC 12: %s", p);
2898 return;
2900 screen_set_cursor_colour(ictx->ctx.s, c);
2903 /* Handle the OSC 112 sequence for resetting cursor colour. */
2904 static void
2905 input_osc_112(struct input_ctx *ictx, const char *p)
2907 if (*p == '\0') /* no arguments allowed */
2908 screen_set_cursor_colour(ictx->ctx.s, -1);
2911 /* Handle the OSC 133 sequence. */
2912 static void
2913 input_osc_133(struct input_ctx *ictx, const char *p)
2915 struct grid *gd = ictx->ctx.s->grid;
2916 u_int line = ictx->ctx.s->cy + gd->hsize;
2917 struct grid_line *gl;
2919 if (line > gd->hsize + gd->sy - 1)
2920 return;
2921 gl = grid_get_line(gd, line);
2923 switch (*p) {
2924 case 'A':
2925 gl->flags |= GRID_LINE_START_PROMPT;
2926 break;
2927 case 'C':
2928 gl->flags |= GRID_LINE_START_OUTPUT;
2929 break;
2933 /* Handle the OSC 52 sequence for setting the clipboard. */
2934 static void
2935 input_osc_52(struct input_ctx *ictx, const char *p)
2937 struct window_pane *wp = ictx->wp;
2938 char *end;
2939 const char *buf = NULL;
2940 size_t len = 0;
2941 u_char *out;
2942 int outlen, state;
2943 struct screen_write_ctx ctx;
2944 struct paste_buffer *pb;
2945 const char* allow = "cpqs01234567";
2946 char flags[sizeof "cpqs01234567"] = "";
2947 u_int i, j = 0;
2949 if (wp == NULL)
2950 return;
2951 state = options_get_number(global_options, "set-clipboard");
2952 if (state != 2)
2953 return;
2955 if ((end = strchr(p, ';')) == NULL)
2956 return;
2957 end++;
2958 if (*end == '\0')
2959 return;
2960 log_debug("%s: %s", __func__, end);
2962 for (i = 0; p + i != end; i++) {
2963 if (strchr(allow, p[i]) != NULL && strchr(flags, p[i]) == NULL)
2964 flags[j++] = p[i];
2966 log_debug("%s: %.*s %s", __func__, (int)(end - p - 1), p, flags);
2968 if (strcmp(end, "?") == 0) {
2969 if ((pb = paste_get_top(NULL)) != NULL)
2970 buf = paste_buffer_data(pb, &len);
2971 if (ictx->input_end == INPUT_END_BEL)
2972 input_reply_clipboard(ictx->event, buf, len, "\007");
2973 else
2974 input_reply_clipboard(ictx->event, buf, len, "\033\\");
2975 return;
2978 len = (strlen(end) / 4) * 3;
2979 if (len == 0)
2980 return;
2982 out = xmalloc(len);
2983 if ((outlen = b64_pton(end, out, len)) == -1) {
2984 free(out);
2985 return;
2988 screen_write_start_pane(&ctx, wp, NULL);
2989 screen_write_setselection(&ctx, flags, out, outlen);
2990 screen_write_stop(&ctx);
2991 notify_pane("pane-set-clipboard", wp);
2993 paste_add(NULL, out, outlen);
2996 /* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2997 static void
2998 input_osc_104(struct input_ctx *ictx, const char *p)
3000 char *copy, *s;
3001 long idx;
3002 int bad = 0, redraw = 0;
3004 if (*p == '\0') {
3005 colour_palette_clear(ictx->palette);
3006 screen_write_fullredraw(&ictx->ctx);
3007 return;
3010 copy = s = xstrdup(p);
3011 while (*s != '\0') {
3012 idx = strtol(s, &s, 10);
3013 if (*s != '\0' && *s != ';') {
3014 bad = 1;
3015 break;
3017 if (idx < 0 || idx >= 256) {
3018 bad = 1;
3019 break;
3021 if (colour_palette_set(ictx->palette, idx, -1))
3022 redraw = 1;
3023 if (*s == ';')
3024 s++;
3026 if (bad)
3027 log_debug("bad OSC 104: %s", p);
3028 if (redraw)
3029 screen_write_fullredraw(&ictx->ctx);
3030 free(copy);
3033 void
3034 input_reply_clipboard(struct bufferevent *bev, const char *buf, size_t len,
3035 const char *end)
3037 char *out = NULL;
3038 int outlen = 0;
3040 if (buf != NULL && len != 0) {
3041 if (len >= ((size_t)INT_MAX * 3 / 4) - 1)
3042 return;
3043 outlen = 4 * ((len + 2) / 3) + 1;
3044 out = xmalloc(outlen);
3045 if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
3046 free(out);
3047 return;
3051 bufferevent_write(bev, "\033]52;;", 6);
3052 if (outlen != 0)
3053 bufferevent_write(bev, out, outlen);
3054 bufferevent_write(bev, end, strlen(end));
3055 free(out);