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