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