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