1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 static void teken_subr_cursor_up(teken_t *, unsigned int); 32 static void teken_subr_erase_line(const teken_t *, unsigned int); 33 static void teken_subr_regular_character(teken_t *, teken_char_t); 34 static void teken_subr_reset_to_initial_state(teken_t *); 35 static void teken_subr_save_cursor(teken_t *); 36 37 static inline int 38 teken_tab_isset(const teken_t *t, unsigned int col) 39 { 40 unsigned int b, o; 41 42 if (col >= T_NUMCOL) 43 return ((col % 8) == 0); 44 45 b = col / (sizeof(unsigned int) * 8); 46 o = col % (sizeof(unsigned int) * 8); 47 48 return (t->t_tabstops[b] & (1U << o)); 49 } 50 51 static inline void 52 teken_tab_clear(teken_t *t, unsigned int col) 53 { 54 unsigned int b, o; 55 56 if (col >= T_NUMCOL) 57 return; 58 59 b = col / (sizeof(unsigned int) * 8); 60 o = col % (sizeof(unsigned int) * 8); 61 62 t->t_tabstops[b] &= ~(1U << o); 63 } 64 65 static inline void 66 teken_tab_set(teken_t *t, unsigned int col) 67 { 68 unsigned int b, o; 69 70 if (col >= T_NUMCOL) 71 return; 72 73 b = col / (sizeof(unsigned int) * 8); 74 o = col % (sizeof(unsigned int) * 8); 75 76 t->t_tabstops[b] |= 1U << o; 77 } 78 79 static void 80 teken_tab_default(teken_t *t) 81 { 82 unsigned int i; 83 84 memset(t->t_tabstops, 0, T_NUMCOL / 8); 85 86 for (i = 8; i < T_NUMCOL; i += 8) 87 teken_tab_set(t, i); 88 } 89 90 static void 91 teken_subr_do_scroll(const teken_t *t, int amount) 92 { 93 teken_rect_t tr; 94 teken_pos_t tp; 95 96 teken_assert(t->t_cursor.tp_row <= t->t_winsize.tp_row); 97 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row); 98 teken_assert(amount != 0); 99 100 /* Copy existing data 1 line up. */ 101 if (amount > 0) { 102 /* Scroll down. */ 103 104 /* Copy existing data up. */ 105 if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) { 106 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin + amount; 107 tr.tr_begin.tp_col = 0; 108 tr.tr_end.tp_row = t->t_scrollreg.ts_end; 109 tr.tr_end.tp_col = t->t_winsize.tp_col; 110 tp.tp_row = t->t_scrollreg.ts_begin; 111 tp.tp_col = 0; 112 teken_funcs_copy(t, &tr, &tp); 113 114 tr.tr_begin.tp_row = t->t_scrollreg.ts_end - amount; 115 } else { 116 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin; 117 } 118 119 /* Clear the last lines. */ 120 tr.tr_begin.tp_col = 0; 121 tr.tr_end.tp_row = t->t_scrollreg.ts_end; 122 tr.tr_end.tp_col = t->t_winsize.tp_col; 123 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 124 } else { 125 /* Scroll up. */ 126 amount = -amount; 127 128 /* Copy existing data down. */ 129 if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) { 130 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin; 131 tr.tr_begin.tp_col = 0; 132 tr.tr_end.tp_row = t->t_scrollreg.ts_end - amount; 133 tr.tr_end.tp_col = t->t_winsize.tp_col; 134 tp.tp_row = t->t_scrollreg.ts_begin + amount; 135 tp.tp_col = 0; 136 teken_funcs_copy(t, &tr, &tp); 137 138 tr.tr_end.tp_row = t->t_scrollreg.ts_begin + amount; 139 } else { 140 tr.tr_end.tp_row = t->t_scrollreg.ts_end; 141 } 142 143 /* Clear the first lines. */ 144 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin; 145 tr.tr_begin.tp_col = 0; 146 tr.tr_end.tp_col = t->t_winsize.tp_col; 147 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 148 } 149 } 150 151 static ssize_t 152 teken_subr_do_cpr(const teken_t *t, unsigned int cmd, char response[16]) 153 { 154 155 switch (cmd) { 156 case 5: /* Operating status. */ 157 strcpy(response, "0n"); 158 return (2); 159 case 6: { /* Cursor position. */ 160 int len; 161 162 len = snprintf(response, 16, "%u;%uR", 163 (t->t_cursor.tp_row - t->t_originreg.ts_begin) + 1, 164 t->t_cursor.tp_col + 1); 165 166 if (len >= 16) 167 return (-1); 168 return (len); 169 } 170 case 15: /* Printer status. */ 171 strcpy(response, "13n"); 172 return (3); 173 case 25: /* UDK status. */ 174 strcpy(response, "20n"); 175 return (3); 176 case 26: /* Keyboard status. */ 177 strcpy(response, "27;1n"); 178 return (5); 179 default: 180 teken_printf("Unknown DSR\n"); 181 return (-1); 182 } 183 } 184 185 static void 186 teken_subr_alignment_test(teken_t *t) 187 { 188 teken_rect_t tr; 189 190 t->t_cursor.tp_row = t->t_cursor.tp_col = 0; 191 t->t_scrollreg.ts_begin = 0; 192 t->t_scrollreg.ts_end = t->t_winsize.tp_row; 193 t->t_originreg = t->t_scrollreg; 194 t->t_stateflags &= ~(TS_WRAPPED|TS_ORIGIN); 195 teken_funcs_cursor(t); 196 197 tr.tr_begin.tp_row = 0; 198 tr.tr_begin.tp_col = 0; 199 tr.tr_end = t->t_winsize; 200 teken_funcs_fill(t, &tr, 'E', &t->t_defattr); 201 } 202 203 static void 204 teken_subr_backspace(teken_t *t) 205 { 206 207 if (t->t_stateflags & TS_CONS25) { 208 if (t->t_cursor.tp_col == 0) { 209 if (t->t_cursor.tp_row == t->t_originreg.ts_begin) 210 return; 211 t->t_cursor.tp_row--; 212 t->t_cursor.tp_col = t->t_winsize.tp_col - 1; 213 } else { 214 t->t_cursor.tp_col--; 215 } 216 } else { 217 if (t->t_cursor.tp_col == 0) 218 return; 219 220 t->t_cursor.tp_col--; 221 t->t_stateflags &= ~TS_WRAPPED; 222 } 223 224 teken_funcs_cursor(t); 225 } 226 227 static void 228 teken_subr_bell(const teken_t *t) 229 { 230 231 teken_funcs_bell(t); 232 } 233 234 static void 235 teken_subr_carriage_return(teken_t *t) 236 { 237 238 t->t_cursor.tp_col = 0; 239 t->t_stateflags &= ~TS_WRAPPED; 240 teken_funcs_cursor(t); 241 } 242 243 static void 244 teken_subr_cursor_backward(teken_t *t, unsigned int ncols) 245 { 246 247 if (ncols > t->t_cursor.tp_col) 248 t->t_cursor.tp_col = 0; 249 else 250 t->t_cursor.tp_col -= ncols; 251 t->t_stateflags &= ~TS_WRAPPED; 252 teken_funcs_cursor(t); 253 } 254 255 static void 256 teken_subr_cursor_backward_tabulation(teken_t *t, unsigned int ntabs) 257 { 258 259 do { 260 /* Stop when we've reached the beginning of the line. */ 261 if (t->t_cursor.tp_col == 0) 262 break; 263 264 t->t_cursor.tp_col--; 265 266 /* Tab marker set. */ 267 if (teken_tab_isset(t, t->t_cursor.tp_col)) 268 ntabs--; 269 } while (ntabs > 0); 270 271 teken_funcs_cursor(t); 272 } 273 274 static void 275 teken_subr_cursor_down(teken_t *t, unsigned int nrows) 276 { 277 278 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) 279 t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1; 280 else 281 t->t_cursor.tp_row += nrows; 282 t->t_stateflags &= ~TS_WRAPPED; 283 teken_funcs_cursor(t); 284 } 285 286 static void 287 teken_subr_cursor_forward(teken_t *t, unsigned int ncols) 288 { 289 290 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) 291 t->t_cursor.tp_col = t->t_winsize.tp_col - 1; 292 else 293 t->t_cursor.tp_col += ncols; 294 t->t_stateflags &= ~TS_WRAPPED; 295 teken_funcs_cursor(t); 296 } 297 298 static void 299 teken_subr_cursor_forward_tabulation(teken_t *t, unsigned int ntabs) 300 { 301 302 do { 303 /* Stop when we've reached the end of the line. */ 304 if (t->t_cursor.tp_col == t->t_winsize.tp_col - 1) 305 break; 306 307 t->t_cursor.tp_col++; 308 309 /* Tab marker set. */ 310 if (teken_tab_isset(t, t->t_cursor.tp_col)) 311 ntabs--; 312 } while (ntabs > 0); 313 314 teken_funcs_cursor(t); 315 } 316 317 static void 318 teken_subr_cursor_next_line(teken_t *t, unsigned int ncols) 319 { 320 321 t->t_cursor.tp_col = 0; 322 teken_subr_cursor_down(t, ncols); 323 } 324 325 static void 326 teken_subr_cursor_position(teken_t *t, unsigned int row, unsigned int col) 327 { 328 329 row = (row - 1) + t->t_originreg.ts_begin; 330 t->t_cursor.tp_row = row < t->t_originreg.ts_end ? 331 row : t->t_originreg.ts_end - 1; 332 333 col--; 334 t->t_cursor.tp_col = col < t->t_winsize.tp_col ? 335 col : t->t_winsize.tp_col - 1; 336 337 t->t_stateflags &= ~TS_WRAPPED; 338 teken_funcs_cursor(t); 339 } 340 341 static void 342 teken_subr_cursor_position_report(const teken_t *t, unsigned int cmd) 343 { 344 char response[18] = "\x1B["; 345 ssize_t len; 346 347 len = teken_subr_do_cpr(t, cmd, response + 2); 348 if (len < 0) 349 return; 350 351 teken_funcs_respond(t, response, len + 2); 352 } 353 354 static void 355 teken_subr_cursor_previous_line(teken_t *t, unsigned int ncols) 356 { 357 358 t->t_cursor.tp_col = 0; 359 teken_subr_cursor_up(t, ncols); 360 } 361 362 static void 363 teken_subr_cursor_up(teken_t *t, unsigned int nrows) 364 { 365 366 if (t->t_scrollreg.ts_begin + nrows >= t->t_cursor.tp_row) 367 t->t_cursor.tp_row = t->t_scrollreg.ts_begin; 368 else 369 t->t_cursor.tp_row -= nrows; 370 t->t_stateflags &= ~TS_WRAPPED; 371 teken_funcs_cursor(t); 372 } 373 374 static void 375 teken_subr_cursor_style(teken_t *t, unsigned int style) 376 { 377 378 /* TODO */ 379 380 /* 381 * CSI Ps SP q 382 * Set cursor style (DECSCUSR), VT520. 383 * Ps = 0 -> blinking block. 384 * Ps = 1 -> blinking block (default). 385 * Ps = 2 -> steady block. 386 * Ps = 3 -> blinking underline. 387 * Ps = 4 -> steady underline. 388 * Ps = 5 -> blinking bar (xterm). 389 * Ps = 6 -> steady bar (xterm). 390 */ 391 } 392 393 static void 394 teken_subr_delete_character(const teken_t *t, unsigned int ncols) 395 { 396 teken_rect_t tr; 397 398 tr.tr_begin.tp_row = t->t_cursor.tp_row; 399 tr.tr_end.tp_row = t->t_cursor.tp_row + 1; 400 tr.tr_end.tp_col = t->t_winsize.tp_col; 401 402 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) { 403 tr.tr_begin.tp_col = t->t_cursor.tp_col; 404 } else { 405 /* Copy characters to the left. */ 406 tr.tr_begin.tp_col = t->t_cursor.tp_col + ncols; 407 teken_funcs_copy(t, &tr, &t->t_cursor); 408 409 tr.tr_begin.tp_col = t->t_winsize.tp_col - ncols; 410 } 411 412 /* Blank trailing columns. */ 413 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 414 } 415 416 static void 417 teken_subr_delete_line(const teken_t *t, unsigned int nrows) 418 { 419 teken_rect_t tr; 420 421 /* Ignore if outside scrolling region. */ 422 if (t->t_cursor.tp_row < t->t_scrollreg.ts_begin || 423 t->t_cursor.tp_row >= t->t_scrollreg.ts_end) 424 return; 425 426 tr.tr_begin.tp_col = 0; 427 tr.tr_end.tp_row = t->t_scrollreg.ts_end; 428 tr.tr_end.tp_col = t->t_winsize.tp_col; 429 430 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) { 431 tr.tr_begin.tp_row = t->t_cursor.tp_row; 432 } else { 433 teken_pos_t tp; 434 435 /* Copy rows up. */ 436 tr.tr_begin.tp_row = t->t_cursor.tp_row + nrows; 437 tp.tp_row = t->t_cursor.tp_row; 438 tp.tp_col = 0; 439 teken_funcs_copy(t, &tr, &tp); 440 441 tr.tr_begin.tp_row = t->t_scrollreg.ts_end - nrows; 442 } 443 444 /* Blank trailing rows. */ 445 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 446 } 447 448 static void 449 teken_subr_device_control_string(teken_t *t) 450 { 451 452 teken_printf("Unsupported device control string\n"); 453 t->t_stateflags |= TS_INSTRING; 454 } 455 456 static void 457 teken_subr_device_status_report(const teken_t *t, unsigned int cmd) 458 { 459 char response[19] = "\x1B[?"; 460 ssize_t len; 461 462 len = teken_subr_do_cpr(t, cmd, response + 3); 463 if (len < 0) 464 return; 465 466 teken_funcs_respond(t, response, len + 3); 467 } 468 469 static void 470 teken_subr_double_height_double_width_line_top(const teken_t *t) 471 { 472 473 (void)t; 474 teken_printf("double height double width top\n"); 475 } 476 477 static void 478 teken_subr_double_height_double_width_line_bottom(const teken_t *t) 479 { 480 481 (void)t; 482 teken_printf("double height double width bottom\n"); 483 } 484 485 static void 486 teken_subr_erase_character(const teken_t *t, unsigned int ncols) 487 { 488 teken_rect_t tr; 489 490 tr.tr_begin = t->t_cursor; 491 tr.tr_end.tp_row = t->t_cursor.tp_row + 1; 492 493 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) 494 tr.tr_end.tp_col = t->t_winsize.tp_col; 495 else 496 tr.tr_end.tp_col = t->t_cursor.tp_col + ncols; 497 498 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 499 } 500 501 static void 502 teken_subr_erase_display(const teken_t *t, unsigned int mode) 503 { 504 teken_rect_t r; 505 506 r.tr_begin.tp_col = 0; 507 r.tr_end.tp_col = t->t_winsize.tp_col; 508 509 switch (mode) { 510 case 1: /* Erase from the top to the cursor. */ 511 teken_subr_erase_line(t, 1); 512 513 /* Erase lines above. */ 514 if (t->t_cursor.tp_row == 0) 515 return; 516 r.tr_begin.tp_row = 0; 517 r.tr_end.tp_row = t->t_cursor.tp_row; 518 break; 519 case 2: /* Erase entire display. */ 520 r.tr_begin.tp_row = 0; 521 r.tr_end.tp_row = t->t_winsize.tp_row; 522 break; 523 default: /* Erase from cursor to the bottom. */ 524 teken_subr_erase_line(t, 0); 525 526 /* Erase lines below. */ 527 if (t->t_cursor.tp_row == t->t_winsize.tp_row - 1) 528 return; 529 r.tr_begin.tp_row = t->t_cursor.tp_row + 1; 530 r.tr_end.tp_row = t->t_winsize.tp_row; 531 break; 532 } 533 534 teken_funcs_fill(t, &r, BLANK, &t->t_curattr); 535 } 536 537 static void 538 teken_subr_erase_line(const teken_t *t, unsigned int mode) 539 { 540 teken_rect_t r; 541 542 r.tr_begin.tp_row = t->t_cursor.tp_row; 543 r.tr_end.tp_row = t->t_cursor.tp_row + 1; 544 545 switch (mode) { 546 case 1: /* Erase from the beginning of the line to the cursor. */ 547 r.tr_begin.tp_col = 0; 548 r.tr_end.tp_col = t->t_cursor.tp_col + 1; 549 break; 550 case 2: /* Erase entire line. */ 551 r.tr_begin.tp_col = 0; 552 r.tr_end.tp_col = t->t_winsize.tp_col; 553 break; 554 default: /* Erase from cursor to the end of the line. */ 555 r.tr_begin.tp_col = t->t_cursor.tp_col; 556 r.tr_end.tp_col = t->t_winsize.tp_col; 557 break; 558 } 559 560 teken_funcs_fill(t, &r, BLANK, &t->t_curattr); 561 } 562 563 static void 564 teken_subr_g0_scs_special_graphics(teken_t *t) 565 { 566 567 t->t_scs[0] = teken_scs_special_graphics; 568 } 569 570 static void 571 teken_subr_g0_scs_uk_national(teken_t *t) 572 { 573 574 t->t_scs[0] = teken_scs_uk_national; 575 } 576 577 static void 578 teken_subr_g0_scs_us_ascii(teken_t *t) 579 { 580 581 t->t_scs[0] = teken_scs_us_ascii; 582 } 583 584 static void 585 teken_subr_g1_scs_special_graphics(teken_t *t) 586 { 587 588 t->t_scs[1] = teken_scs_special_graphics; 589 } 590 591 static void 592 teken_subr_g1_scs_uk_national(teken_t *t) 593 { 594 595 t->t_scs[1] = teken_scs_uk_national; 596 } 597 598 static void 599 teken_subr_g1_scs_us_ascii(teken_t *t) 600 { 601 602 t->t_scs[1] = teken_scs_us_ascii; 603 } 604 605 static void 606 teken_subr_horizontal_position_absolute(teken_t *t, unsigned int col) 607 { 608 609 col--; 610 t->t_cursor.tp_col = col < t->t_winsize.tp_col ? 611 col : t->t_winsize.tp_col - 1; 612 613 t->t_stateflags &= ~TS_WRAPPED; 614 teken_funcs_cursor(t); 615 } 616 617 static void 618 teken_subr_horizontal_tab(teken_t *t) 619 { 620 621 teken_subr_cursor_forward_tabulation(t, 1); 622 } 623 624 static void 625 teken_subr_horizontal_tab_set(teken_t *t) 626 { 627 628 teken_tab_set(t, t->t_cursor.tp_col); 629 } 630 631 static void 632 teken_subr_index(teken_t *t) 633 { 634 635 if (t->t_cursor.tp_row < t->t_scrollreg.ts_end - 1) { 636 t->t_cursor.tp_row++; 637 t->t_stateflags &= ~TS_WRAPPED; 638 teken_funcs_cursor(t); 639 } else { 640 teken_subr_do_scroll(t, 1); 641 } 642 } 643 644 static void 645 teken_subr_insert_character(const teken_t *t, unsigned int ncols) 646 { 647 teken_rect_t tr; 648 649 tr.tr_begin = t->t_cursor; 650 tr.tr_end.tp_row = t->t_cursor.tp_row + 1; 651 652 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) { 653 tr.tr_end.tp_col = t->t_winsize.tp_col; 654 } else { 655 teken_pos_t tp; 656 657 /* Copy characters to the right. */ 658 tr.tr_end.tp_col = t->t_winsize.tp_col - ncols; 659 tp.tp_row = t->t_cursor.tp_row; 660 tp.tp_col = t->t_cursor.tp_col + ncols; 661 teken_funcs_copy(t, &tr, &tp); 662 663 tr.tr_end.tp_col = t->t_cursor.tp_col + ncols; 664 } 665 666 /* Blank current location. */ 667 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 668 } 669 670 static void 671 teken_subr_insert_line(const teken_t *t, unsigned int nrows) 672 { 673 teken_rect_t tr; 674 675 /* Ignore if outside scrolling region. */ 676 if (t->t_cursor.tp_row < t->t_scrollreg.ts_begin || 677 t->t_cursor.tp_row >= t->t_scrollreg.ts_end) 678 return; 679 680 tr.tr_begin.tp_row = t->t_cursor.tp_row; 681 tr.tr_begin.tp_col = 0; 682 tr.tr_end.tp_col = t->t_winsize.tp_col; 683 684 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) { 685 tr.tr_end.tp_row = t->t_scrollreg.ts_end; 686 } else { 687 teken_pos_t tp; 688 689 /* Copy lines down. */ 690 tr.tr_end.tp_row = t->t_scrollreg.ts_end - nrows; 691 tp.tp_row = t->t_cursor.tp_row + nrows; 692 tp.tp_col = 0; 693 teken_funcs_copy(t, &tr, &tp); 694 695 tr.tr_end.tp_row = t->t_cursor.tp_row + nrows; 696 } 697 698 /* Blank current location. */ 699 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 700 } 701 702 static void 703 teken_subr_keypad_application_mode(const teken_t *t) 704 { 705 706 teken_funcs_param(t, TP_KEYPADAPP, 1); 707 } 708 709 static void 710 teken_subr_keypad_numeric_mode(const teken_t *t) 711 { 712 713 teken_funcs_param(t, TP_KEYPADAPP, 0); 714 } 715 716 static void 717 teken_subr_newline(teken_t *t) 718 { 719 720 t->t_cursor.tp_row++; 721 722 if (t->t_cursor.tp_row >= t->t_scrollreg.ts_end) { 723 teken_subr_do_scroll(t, 1); 724 t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1; 725 } 726 727 t->t_stateflags &= ~TS_WRAPPED; 728 teken_funcs_cursor(t); 729 } 730 731 static void 732 teken_subr_newpage(teken_t *t) 733 { 734 735 if (t->t_stateflags & TS_CONS25) { 736 teken_rect_t tr; 737 738 /* Clear screen. */ 739 tr.tr_begin.tp_row = t->t_originreg.ts_begin; 740 tr.tr_begin.tp_col = 0; 741 tr.tr_end.tp_row = t->t_originreg.ts_end; 742 tr.tr_end.tp_col = t->t_winsize.tp_col; 743 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 744 745 /* Cursor at top left. */ 746 t->t_cursor.tp_row = t->t_originreg.ts_begin; 747 t->t_cursor.tp_col = 0; 748 t->t_stateflags &= ~TS_WRAPPED; 749 teken_funcs_cursor(t); 750 } else { 751 teken_subr_newline(t); 752 } 753 } 754 755 static void 756 teken_subr_next_line(teken_t *t) 757 { 758 759 t->t_cursor.tp_col = 0; 760 teken_subr_newline(t); 761 } 762 763 static void 764 teken_subr_operating_system_command(teken_t *t) 765 { 766 767 teken_printf("Unsupported operating system command\n"); 768 t->t_stateflags |= TS_INSTRING; 769 } 770 771 static void 772 teken_subr_pan_down(const teken_t *t, unsigned int nrows) 773 { 774 775 teken_subr_do_scroll(t, (int)nrows); 776 } 777 778 static void 779 teken_subr_pan_up(const teken_t *t, unsigned int nrows) 780 { 781 782 teken_subr_do_scroll(t, -(int)nrows); 783 } 784 785 static void 786 teken_subr_primary_device_attributes(const teken_t *t, unsigned int request) 787 { 788 789 if (request == 0) { 790 const char response[] = "\x1B[?1;2c"; 791 792 teken_funcs_respond(t, response, sizeof response - 1); 793 } else { 794 teken_printf("Unknown DA1\n"); 795 } 796 } 797 798 static void 799 teken_subr_do_putchar(const teken_t *t, const teken_pos_t *tp, teken_char_t c, 800 int width) 801 { 802 803 if (t->t_stateflags & TS_INSERT && 804 tp->tp_col < t->t_winsize.tp_col - width) { 805 teken_rect_t ctr; 806 teken_pos_t ctp; 807 808 /* Insert mode. Move existing characters to the right. */ 809 ctr.tr_begin = *tp; 810 ctr.tr_end.tp_row = tp->tp_row + 1; 811 ctr.tr_end.tp_col = t->t_winsize.tp_col - width; 812 ctp.tp_row = tp->tp_row; 813 ctp.tp_col = tp->tp_col + width; 814 teken_funcs_copy(t, &ctr, &ctp); 815 } 816 817 teken_funcs_putchar(t, tp, c, &t->t_curattr); 818 819 if (width == 2 && tp->tp_col + 1 < t->t_winsize.tp_col) { 820 teken_pos_t tp2; 821 teken_attr_t attr; 822 823 /* Print second half of CJK fullwidth character. */ 824 tp2.tp_row = tp->tp_row; 825 tp2.tp_col = tp->tp_col + 1; 826 attr = t->t_curattr; 827 attr.ta_format |= TF_CJK_RIGHT; 828 teken_funcs_putchar(t, &tp2, c, &attr); 829 } 830 } 831 832 static void 833 teken_subr_regular_character(teken_t *t, teken_char_t c) 834 { 835 int width; 836 837 if (t->t_stateflags & TS_8BIT) { 838 if (!(t->t_stateflags & TS_CONS25) && (c <= 0x1b || c == 0x7f)) 839 return; 840 c = teken_scs_process(t, c); 841 width = 1; 842 } else { 843 c = teken_scs_process(t, c); 844 width = teken_wcwidth(c); 845 /* XXX: Don't process zero-width characters yet. */ 846 if (width <= 0) 847 return; 848 } 849 850 if (t->t_stateflags & TS_CONS25) { 851 teken_subr_do_putchar(t, &t->t_cursor, c, width); 852 t->t_cursor.tp_col += width; 853 854 if (t->t_cursor.tp_col >= t->t_winsize.tp_col) { 855 if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) { 856 /* Perform scrolling. */ 857 teken_subr_do_scroll(t, 1); 858 } else { 859 /* No scrolling needed. */ 860 if (t->t_cursor.tp_row < 861 t->t_winsize.tp_row - 1) 862 t->t_cursor.tp_row++; 863 } 864 t->t_cursor.tp_col = 0; 865 } 866 } else if (t->t_stateflags & TS_AUTOWRAP && 867 ((t->t_stateflags & TS_WRAPPED && 868 t->t_cursor.tp_col + 1 == t->t_winsize.tp_col) || 869 t->t_cursor.tp_col + width > t->t_winsize.tp_col)) { 870 teken_pos_t tp; 871 872 /* 873 * Perform line wrapping, if: 874 * - Autowrapping is enabled, and 875 * - We're in the wrapped state at the last column, or 876 * - The character to be printed does not fit anymore. 877 */ 878 if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) { 879 /* Perform scrolling. */ 880 teken_subr_do_scroll(t, 1); 881 tp.tp_row = t->t_scrollreg.ts_end - 1; 882 } else { 883 /* No scrolling needed. */ 884 tp.tp_row = t->t_cursor.tp_row + 1; 885 if (tp.tp_row == t->t_winsize.tp_row) { 886 /* 887 * Corner case: regular character 888 * outside scrolling region, but at the 889 * bottom of the screen. 890 */ 891 teken_subr_do_putchar(t, &t->t_cursor, 892 c, width); 893 return; 894 } 895 } 896 897 tp.tp_col = 0; 898 teken_subr_do_putchar(t, &tp, c, width); 899 900 t->t_cursor.tp_row = tp.tp_row; 901 t->t_cursor.tp_col = width; 902 t->t_stateflags &= ~TS_WRAPPED; 903 } else { 904 /* No line wrapping needed. */ 905 teken_subr_do_putchar(t, &t->t_cursor, c, width); 906 t->t_cursor.tp_col += width; 907 908 if (t->t_cursor.tp_col >= t->t_winsize.tp_col) { 909 t->t_stateflags |= TS_WRAPPED; 910 t->t_cursor.tp_col = t->t_winsize.tp_col - 1; 911 } else { 912 t->t_stateflags &= ~TS_WRAPPED; 913 } 914 } 915 916 teken_funcs_cursor(t); 917 } 918 919 static void 920 teken_subr_reset_dec_mode(teken_t *t, unsigned int cmd) 921 { 922 923 switch (cmd) { 924 case 1: /* Cursor keys mode. */ 925 t->t_stateflags &= ~TS_CURSORKEYS; 926 break; 927 case 2: /* DECANM: ANSI/VT52 mode. */ 928 teken_printf("DECRST VT52\n"); 929 break; 930 case 3: /* 132 column mode. */ 931 teken_funcs_param(t, TP_132COLS, 0); 932 teken_subr_reset_to_initial_state(t); 933 break; 934 case 5: /* Inverse video. */ 935 teken_printf("DECRST inverse video\n"); 936 break; 937 case 6: /* Origin mode. */ 938 t->t_stateflags &= ~TS_ORIGIN; 939 t->t_originreg.ts_begin = 0; 940 t->t_originreg.ts_end = t->t_winsize.tp_row; 941 t->t_cursor.tp_row = t->t_cursor.tp_col = 0; 942 t->t_stateflags &= ~TS_WRAPPED; 943 teken_funcs_cursor(t); 944 break; 945 case 7: /* Autowrap mode. */ 946 t->t_stateflags &= ~TS_AUTOWRAP; 947 break; 948 case 8: /* Autorepeat mode. */ 949 teken_funcs_param(t, TP_AUTOREPEAT, 0); 950 break; 951 case 25: /* Hide cursor. */ 952 teken_funcs_param(t, TP_SHOWCURSOR, 0); 953 break; 954 case 40: /* Disallow 132 columns. */ 955 teken_printf("DECRST allow 132\n"); 956 break; 957 case 45: /* Disable reverse wraparound. */ 958 teken_printf("DECRST reverse wraparound\n"); 959 break; 960 case 47: /* Switch to alternate buffer. */ 961 teken_printf("Switch to alternate buffer\n"); 962 break; 963 case 1000: /* Mouse input. */ 964 teken_funcs_param(t, TP_MOUSE, 0); 965 break; 966 default: 967 teken_printf("Unknown DECRST: %u\n", cmd); 968 } 969 } 970 971 static void 972 teken_subr_reset_mode(teken_t *t, unsigned int cmd) 973 { 974 975 switch (cmd) { 976 case 4: 977 t->t_stateflags &= ~TS_INSERT; 978 break; 979 default: 980 teken_printf("Unknown reset mode: %u\n", cmd); 981 } 982 } 983 984 static void 985 teken_subr_do_resize(teken_t *t) 986 { 987 988 t->t_scrollreg.ts_begin = 0; 989 t->t_scrollreg.ts_end = t->t_winsize.tp_row; 990 t->t_originreg = t->t_scrollreg; 991 } 992 993 static void 994 teken_subr_do_reset(teken_t *t) 995 { 996 997 t->t_curattr = t->t_defattr; 998 t->t_cursor.tp_row = t->t_cursor.tp_col = 0; 999 t->t_scrollreg.ts_begin = 0; 1000 t->t_scrollreg.ts_end = t->t_winsize.tp_row; 1001 t->t_originreg = t->t_scrollreg; 1002 t->t_stateflags &= TS_8BIT|TS_CONS25; 1003 t->t_stateflags |= TS_AUTOWRAP; 1004 1005 t->t_scs[0] = teken_scs_us_ascii; 1006 t->t_scs[1] = teken_scs_us_ascii; 1007 t->t_curscs = 0; 1008 1009 teken_subr_save_cursor(t); 1010 teken_tab_default(t); 1011 } 1012 1013 static void 1014 teken_subr_reset_to_initial_state(teken_t *t) 1015 { 1016 1017 teken_subr_do_reset(t); 1018 teken_subr_erase_display(t, 2); 1019 teken_funcs_param(t, TP_SHOWCURSOR, 1); 1020 teken_funcs_cursor(t); 1021 } 1022 1023 static void 1024 teken_subr_restore_cursor(teken_t *t) 1025 { 1026 1027 t->t_cursor = t->t_saved_cursor; 1028 t->t_curattr = t->t_saved_curattr; 1029 t->t_scs[t->t_curscs] = t->t_saved_curscs; 1030 t->t_stateflags &= ~TS_WRAPPED; 1031 1032 /* Get out of origin mode when the cursor is moved outside. */ 1033 if (t->t_cursor.tp_row < t->t_originreg.ts_begin || 1034 t->t_cursor.tp_row >= t->t_originreg.ts_end) { 1035 t->t_stateflags &= ~TS_ORIGIN; 1036 t->t_originreg.ts_begin = 0; 1037 t->t_originreg.ts_end = t->t_winsize.tp_row; 1038 } 1039 1040 teken_funcs_cursor(t); 1041 } 1042 1043 static void 1044 teken_subr_reverse_index(teken_t *t) 1045 { 1046 1047 if (t->t_cursor.tp_row > t->t_scrollreg.ts_begin) { 1048 t->t_cursor.tp_row--; 1049 t->t_stateflags &= ~TS_WRAPPED; 1050 teken_funcs_cursor(t); 1051 } else { 1052 teken_subr_do_scroll(t, -1); 1053 } 1054 } 1055 1056 static void 1057 teken_subr_save_cursor(teken_t *t) 1058 { 1059 1060 t->t_saved_cursor = t->t_cursor; 1061 t->t_saved_curattr = t->t_curattr; 1062 t->t_saved_curscs = t->t_scs[t->t_curscs]; 1063 } 1064 1065 static void 1066 teken_subr_secondary_device_attributes(const teken_t *t, unsigned int request) 1067 { 1068 1069 if (request == 0) { 1070 const char response[] = "\x1B[>0;10;0c"; 1071 teken_funcs_respond(t, response, sizeof response - 1); 1072 } else { 1073 teken_printf("Unknown DA2\n"); 1074 } 1075 } 1076 1077 static void 1078 teken_subr_set_dec_mode(teken_t *t, unsigned int cmd) 1079 { 1080 1081 switch (cmd) { 1082 case 1: /* Cursor keys mode. */ 1083 t->t_stateflags |= TS_CURSORKEYS; 1084 break; 1085 case 2: /* DECANM: ANSI/VT52 mode. */ 1086 teken_printf("DECSET VT52\n"); 1087 break; 1088 case 3: /* 132 column mode. */ 1089 teken_funcs_param(t, TP_132COLS, 1); 1090 teken_subr_reset_to_initial_state(t); 1091 break; 1092 case 5: /* Inverse video. */ 1093 teken_printf("DECSET inverse video\n"); 1094 break; 1095 case 6: /* Origin mode. */ 1096 t->t_stateflags |= TS_ORIGIN; 1097 t->t_originreg = t->t_scrollreg; 1098 t->t_cursor.tp_row = t->t_scrollreg.ts_begin; 1099 t->t_cursor.tp_col = 0; 1100 t->t_stateflags &= ~TS_WRAPPED; 1101 teken_funcs_cursor(t); 1102 break; 1103 case 7: /* Autowrap mode. */ 1104 t->t_stateflags |= TS_AUTOWRAP; 1105 break; 1106 case 8: /* Autorepeat mode. */ 1107 teken_funcs_param(t, TP_AUTOREPEAT, 1); 1108 break; 1109 case 25: /* Display cursor. */ 1110 teken_funcs_param(t, TP_SHOWCURSOR, 1); 1111 break; 1112 case 40: /* Allow 132 columns. */ 1113 teken_printf("DECSET allow 132\n"); 1114 break; 1115 case 45: /* Enable reverse wraparound. */ 1116 teken_printf("DECSET reverse wraparound\n"); 1117 break; 1118 case 47: /* Switch to alternate buffer. */ 1119 teken_printf("Switch away from alternate buffer\n"); 1120 break; 1121 case 1000: /* Mouse input. */ 1122 teken_funcs_param(t, TP_MOUSE, 1); 1123 break; 1124 default: 1125 teken_printf("Unknown DECSET: %u\n", cmd); 1126 } 1127 } 1128 1129 static void 1130 teken_subr_set_mode(teken_t *t, unsigned int cmd) 1131 { 1132 1133 switch (cmd) { 1134 case 4: 1135 teken_printf("Insert mode\n"); 1136 t->t_stateflags |= TS_INSERT; 1137 break; 1138 default: 1139 teken_printf("Unknown set mode: %u\n", cmd); 1140 } 1141 } 1142 1143 static void 1144 teken_subr_set_graphic_rendition(teken_t *t, unsigned int ncmds, 1145 const unsigned int cmds[]) 1146 { 1147 unsigned int i, n; 1148 1149 /* No attributes means reset. */ 1150 if (ncmds == 0) { 1151 t->t_curattr = t->t_defattr; 1152 return; 1153 } 1154 1155 for (i = 0; i < ncmds; i++) { 1156 n = cmds[i]; 1157 1158 switch (n) { 1159 case 0: /* Reset. */ 1160 t->t_curattr = t->t_defattr; 1161 break; 1162 case 1: /* Bold. */ 1163 t->t_curattr.ta_format |= TF_BOLD; 1164 break; 1165 case 4: /* Underline. */ 1166 t->t_curattr.ta_format |= TF_UNDERLINE; 1167 break; 1168 case 5: /* Blink. */ 1169 t->t_curattr.ta_format |= TF_BLINK; 1170 break; 1171 case 7: /* Reverse. */ 1172 t->t_curattr.ta_format |= TF_REVERSE; 1173 break; 1174 case 22: /* Remove bold. */ 1175 t->t_curattr.ta_format &= ~TF_BOLD; 1176 break; 1177 case 24: /* Remove underline. */ 1178 t->t_curattr.ta_format &= ~TF_UNDERLINE; 1179 break; 1180 case 25: /* Remove blink. */ 1181 t->t_curattr.ta_format &= ~TF_BLINK; 1182 break; 1183 case 27: /* Remove reverse. */ 1184 t->t_curattr.ta_format &= ~TF_REVERSE; 1185 break; 1186 case 30: /* Set foreground color: black */ 1187 case 31: /* Set foreground color: red */ 1188 case 32: /* Set foreground color: green */ 1189 case 33: /* Set foreground color: brown */ 1190 case 34: /* Set foreground color: blue */ 1191 case 35: /* Set foreground color: magenta */ 1192 case 36: /* Set foreground color: cyan */ 1193 case 37: /* Set foreground color: white */ 1194 t->t_curattr.ta_fgcolor = n - 30; 1195 break; 1196 case 38: /* Set foreground color: 256 color mode */ 1197 if (i + 2 >= ncmds || cmds[i + 1] != 5) 1198 continue; 1199 t->t_curattr.ta_fgcolor = cmds[i + 2]; 1200 i += 2; 1201 break; 1202 case 39: /* Set default foreground color. */ 1203 t->t_curattr.ta_fgcolor = t->t_defattr.ta_fgcolor; 1204 break; 1205 case 40: /* Set background color: black */ 1206 case 41: /* Set background color: red */ 1207 case 42: /* Set background color: green */ 1208 case 43: /* Set background color: brown */ 1209 case 44: /* Set background color: blue */ 1210 case 45: /* Set background color: magenta */ 1211 case 46: /* Set background color: cyan */ 1212 case 47: /* Set background color: white */ 1213 t->t_curattr.ta_bgcolor = n - 40; 1214 break; 1215 case 48: /* Set background color: 256 color mode */ 1216 if (i + 2 >= ncmds || cmds[i + 1] != 5) 1217 continue; 1218 t->t_curattr.ta_bgcolor = cmds[i + 2]; 1219 i += 2; 1220 break; 1221 case 49: /* Set default background color. */ 1222 t->t_curattr.ta_bgcolor = t->t_defattr.ta_bgcolor; 1223 break; 1224 case 90: /* Set bright foreground color: black */ 1225 case 91: /* Set bright foreground color: red */ 1226 case 92: /* Set bright foreground color: green */ 1227 case 93: /* Set bright foreground color: brown */ 1228 case 94: /* Set bright foreground color: blue */ 1229 case 95: /* Set bright foreground color: magenta */ 1230 case 96: /* Set bright foreground color: cyan */ 1231 case 97: /* Set bright foreground color: white */ 1232 t->t_curattr.ta_fgcolor = (n - 90) + 8; 1233 break; 1234 case 100: /* Set bright background color: black */ 1235 case 101: /* Set bright background color: red */ 1236 case 102: /* Set bright background color: green */ 1237 case 103: /* Set bright background color: brown */ 1238 case 104: /* Set bright background color: blue */ 1239 case 105: /* Set bright background color: magenta */ 1240 case 106: /* Set bright background color: cyan */ 1241 case 107: /* Set bright background color: white */ 1242 t->t_curattr.ta_bgcolor = (n - 100) + 8; 1243 break; 1244 default: 1245 teken_printf("unsupported attribute %u\n", n); 1246 } 1247 } 1248 } 1249 1250 static void 1251 teken_subr_set_top_and_bottom_margins(teken_t *t, unsigned int top, 1252 unsigned int bottom) 1253 { 1254 1255 /* Adjust top row number. */ 1256 if (top > 0) 1257 top--; 1258 /* Adjust bottom row number. */ 1259 if (bottom == 0 || bottom > t->t_winsize.tp_row) 1260 bottom = t->t_winsize.tp_row; 1261 1262 /* Invalid arguments. */ 1263 if (top >= bottom - 1) { 1264 top = 0; 1265 bottom = t->t_winsize.tp_row; 1266 } 1267 1268 /* Apply scrolling region. */ 1269 t->t_scrollreg.ts_begin = top; 1270 t->t_scrollreg.ts_end = bottom; 1271 if (t->t_stateflags & TS_ORIGIN) 1272 t->t_originreg = t->t_scrollreg; 1273 1274 /* Home cursor to the top left of the scrolling region. */ 1275 t->t_cursor.tp_row = t->t_originreg.ts_begin; 1276 t->t_cursor.tp_col = 0; 1277 t->t_stateflags &= ~TS_WRAPPED; 1278 teken_funcs_cursor(t); 1279 } 1280 1281 static void 1282 teken_subr_single_height_double_width_line(const teken_t *t) 1283 { 1284 1285 (void)t; 1286 teken_printf("single height double width???\n"); 1287 } 1288 1289 static void 1290 teken_subr_single_height_single_width_line(const teken_t *t) 1291 { 1292 1293 (void)t; 1294 teken_printf("single height single width???\n"); 1295 } 1296 1297 static void 1298 teken_subr_string_terminator(const teken_t *t) 1299 { 1300 1301 (void)t; 1302 /* 1303 * Strings are already terminated in teken_input_char() when ^[ 1304 * is inserted. 1305 */ 1306 } 1307 1308 static void 1309 teken_subr_tab_clear(teken_t *t, unsigned int cmd) 1310 { 1311 1312 switch (cmd) { 1313 case 0: 1314 teken_tab_clear(t, t->t_cursor.tp_col); 1315 break; 1316 case 3: 1317 memset(t->t_tabstops, 0, T_NUMCOL / 8); 1318 break; 1319 default: 1320 break; 1321 } 1322 } 1323 1324 static void 1325 teken_subr_vertical_position_absolute(teken_t *t, unsigned int row) 1326 { 1327 1328 row = (row - 1) + t->t_originreg.ts_begin; 1329 t->t_cursor.tp_row = row < t->t_originreg.ts_end ? 1330 row : t->t_originreg.ts_end - 1; 1331 1332 t->t_stateflags &= ~TS_WRAPPED; 1333 teken_funcs_cursor(t); 1334 } 1335