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