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_delete_character(const teken_t *t, unsigned int ncols) 376 { 377 teken_rect_t tr; 378 379 tr.tr_begin.tp_row = t->t_cursor.tp_row; 380 tr.tr_end.tp_row = t->t_cursor.tp_row + 1; 381 tr.tr_end.tp_col = t->t_winsize.tp_col; 382 383 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) { 384 tr.tr_begin.tp_col = t->t_cursor.tp_col; 385 } else { 386 /* Copy characters to the left. */ 387 tr.tr_begin.tp_col = t->t_cursor.tp_col + ncols; 388 teken_funcs_copy(t, &tr, &t->t_cursor); 389 390 tr.tr_begin.tp_col = t->t_winsize.tp_col - ncols; 391 } 392 393 /* Blank trailing columns. */ 394 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 395 } 396 397 static void 398 teken_subr_delete_line(const teken_t *t, unsigned int nrows) 399 { 400 teken_rect_t tr; 401 402 /* Ignore if outside scrolling region. */ 403 if (t->t_cursor.tp_row < t->t_scrollreg.ts_begin || 404 t->t_cursor.tp_row >= t->t_scrollreg.ts_end) 405 return; 406 407 tr.tr_begin.tp_col = 0; 408 tr.tr_end.tp_row = t->t_scrollreg.ts_end; 409 tr.tr_end.tp_col = t->t_winsize.tp_col; 410 411 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) { 412 tr.tr_begin.tp_row = t->t_cursor.tp_row; 413 } else { 414 teken_pos_t tp; 415 416 /* Copy rows up. */ 417 tr.tr_begin.tp_row = t->t_cursor.tp_row + nrows; 418 tp.tp_row = t->t_cursor.tp_row; 419 tp.tp_col = 0; 420 teken_funcs_copy(t, &tr, &tp); 421 422 tr.tr_begin.tp_row = t->t_scrollreg.ts_end - nrows; 423 } 424 425 /* Blank trailing rows. */ 426 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 427 } 428 429 static void 430 teken_subr_device_control_string(teken_t *t) 431 { 432 433 teken_printf("Unsupported device control string\n"); 434 t->t_stateflags |= TS_INSTRING; 435 } 436 437 static void 438 teken_subr_device_status_report(const teken_t *t, unsigned int cmd) 439 { 440 char response[19] = "\x1B[?"; 441 ssize_t len; 442 443 len = teken_subr_do_cpr(t, cmd, response + 3); 444 if (len < 0) 445 return; 446 447 teken_funcs_respond(t, response, len + 3); 448 } 449 450 static void 451 teken_subr_double_height_double_width_line_top(const teken_t *t) 452 { 453 454 (void)t; 455 teken_printf("double height double width top\n"); 456 } 457 458 static void 459 teken_subr_double_height_double_width_line_bottom(const teken_t *t) 460 { 461 462 (void)t; 463 teken_printf("double height double width bottom\n"); 464 } 465 466 static void 467 teken_subr_erase_character(const teken_t *t, unsigned int ncols) 468 { 469 teken_rect_t tr; 470 471 tr.tr_begin = t->t_cursor; 472 tr.tr_end.tp_row = t->t_cursor.tp_row + 1; 473 474 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) 475 tr.tr_end.tp_col = t->t_winsize.tp_col; 476 else 477 tr.tr_end.tp_col = t->t_cursor.tp_col + ncols; 478 479 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 480 } 481 482 static void 483 teken_subr_erase_display(const teken_t *t, unsigned int mode) 484 { 485 teken_rect_t r; 486 487 r.tr_begin.tp_col = 0; 488 r.tr_end.tp_col = t->t_winsize.tp_col; 489 490 switch (mode) { 491 case 1: /* Erase from the top to the cursor. */ 492 teken_subr_erase_line(t, 1); 493 494 /* Erase lines above. */ 495 if (t->t_cursor.tp_row == 0) 496 return; 497 r.tr_begin.tp_row = 0; 498 r.tr_end.tp_row = t->t_cursor.tp_row; 499 break; 500 case 2: /* Erase entire display. */ 501 r.tr_begin.tp_row = 0; 502 r.tr_end.tp_row = t->t_winsize.tp_row; 503 break; 504 default: /* Erase from cursor to the bottom. */ 505 teken_subr_erase_line(t, 0); 506 507 /* Erase lines below. */ 508 if (t->t_cursor.tp_row == t->t_winsize.tp_row - 1) 509 return; 510 r.tr_begin.tp_row = t->t_cursor.tp_row + 1; 511 r.tr_end.tp_row = t->t_winsize.tp_row; 512 break; 513 } 514 515 teken_funcs_fill(t, &r, BLANK, &t->t_curattr); 516 } 517 518 static void 519 teken_subr_erase_line(const teken_t *t, unsigned int mode) 520 { 521 teken_rect_t r; 522 523 r.tr_begin.tp_row = t->t_cursor.tp_row; 524 r.tr_end.tp_row = t->t_cursor.tp_row + 1; 525 526 switch (mode) { 527 case 1: /* Erase from the beginning of the line to the cursor. */ 528 r.tr_begin.tp_col = 0; 529 r.tr_end.tp_col = t->t_cursor.tp_col + 1; 530 break; 531 case 2: /* Erase entire line. */ 532 r.tr_begin.tp_col = 0; 533 r.tr_end.tp_col = t->t_winsize.tp_col; 534 break; 535 default: /* Erase from cursor to the end of the line. */ 536 r.tr_begin.tp_col = t->t_cursor.tp_col; 537 r.tr_end.tp_col = t->t_winsize.tp_col; 538 break; 539 } 540 541 teken_funcs_fill(t, &r, BLANK, &t->t_curattr); 542 } 543 544 static void 545 teken_subr_g0_scs_special_graphics(teken_t *t) 546 { 547 548 t->t_scs[0] = teken_scs_special_graphics; 549 } 550 551 static void 552 teken_subr_g0_scs_uk_national(teken_t *t) 553 { 554 555 t->t_scs[0] = teken_scs_uk_national; 556 } 557 558 static void 559 teken_subr_g0_scs_us_ascii(teken_t *t) 560 { 561 562 t->t_scs[0] = teken_scs_us_ascii; 563 } 564 565 static void 566 teken_subr_g1_scs_special_graphics(teken_t *t) 567 { 568 569 t->t_scs[1] = teken_scs_special_graphics; 570 } 571 572 static void 573 teken_subr_g1_scs_uk_national(teken_t *t) 574 { 575 576 t->t_scs[1] = teken_scs_uk_national; 577 } 578 579 static void 580 teken_subr_g1_scs_us_ascii(teken_t *t) 581 { 582 583 t->t_scs[1] = teken_scs_us_ascii; 584 } 585 586 static void 587 teken_subr_horizontal_position_absolute(teken_t *t, unsigned int col) 588 { 589 590 col--; 591 t->t_cursor.tp_col = col < t->t_winsize.tp_col ? 592 col : t->t_winsize.tp_col - 1; 593 594 t->t_stateflags &= ~TS_WRAPPED; 595 teken_funcs_cursor(t); 596 } 597 598 static void 599 teken_subr_horizontal_tab(teken_t *t) 600 { 601 602 teken_subr_cursor_forward_tabulation(t, 1); 603 } 604 605 static void 606 teken_subr_horizontal_tab_set(teken_t *t) 607 { 608 609 teken_tab_set(t, t->t_cursor.tp_col); 610 } 611 612 static void 613 teken_subr_index(teken_t *t) 614 { 615 616 if (t->t_cursor.tp_row < t->t_scrollreg.ts_end - 1) { 617 t->t_cursor.tp_row++; 618 t->t_stateflags &= ~TS_WRAPPED; 619 teken_funcs_cursor(t); 620 } else { 621 teken_subr_do_scroll(t, 1); 622 } 623 } 624 625 static void 626 teken_subr_insert_character(const teken_t *t, unsigned int ncols) 627 { 628 teken_rect_t tr; 629 630 tr.tr_begin = t->t_cursor; 631 tr.tr_end.tp_row = t->t_cursor.tp_row + 1; 632 633 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) { 634 tr.tr_end.tp_col = t->t_winsize.tp_col; 635 } else { 636 teken_pos_t tp; 637 638 /* Copy characters to the right. */ 639 tr.tr_end.tp_col = t->t_winsize.tp_col - ncols; 640 tp.tp_row = t->t_cursor.tp_row; 641 tp.tp_col = t->t_cursor.tp_col + ncols; 642 teken_funcs_copy(t, &tr, &tp); 643 644 tr.tr_end.tp_col = t->t_cursor.tp_col + ncols; 645 } 646 647 /* Blank current location. */ 648 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 649 } 650 651 static void 652 teken_subr_insert_line(const teken_t *t, unsigned int nrows) 653 { 654 teken_rect_t tr; 655 656 /* Ignore if outside scrolling region. */ 657 if (t->t_cursor.tp_row < t->t_scrollreg.ts_begin || 658 t->t_cursor.tp_row >= t->t_scrollreg.ts_end) 659 return; 660 661 tr.tr_begin.tp_row = t->t_cursor.tp_row; 662 tr.tr_begin.tp_col = 0; 663 tr.tr_end.tp_col = t->t_winsize.tp_col; 664 665 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) { 666 tr.tr_end.tp_row = t->t_scrollreg.ts_end; 667 } else { 668 teken_pos_t tp; 669 670 /* Copy lines down. */ 671 tr.tr_end.tp_row = t->t_scrollreg.ts_end - nrows; 672 tp.tp_row = t->t_cursor.tp_row + nrows; 673 tp.tp_col = 0; 674 teken_funcs_copy(t, &tr, &tp); 675 676 tr.tr_end.tp_row = t->t_cursor.tp_row + nrows; 677 } 678 679 /* Blank current location. */ 680 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 681 } 682 683 static void 684 teken_subr_keypad_application_mode(const teken_t *t) 685 { 686 687 teken_funcs_param(t, TP_KEYPADAPP, 1); 688 } 689 690 static void 691 teken_subr_keypad_numeric_mode(const teken_t *t) 692 { 693 694 teken_funcs_param(t, TP_KEYPADAPP, 0); 695 } 696 697 static void 698 teken_subr_newline(teken_t *t) 699 { 700 701 t->t_cursor.tp_row++; 702 703 if (t->t_cursor.tp_row >= t->t_scrollreg.ts_end) { 704 teken_subr_do_scroll(t, 1); 705 t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1; 706 } 707 708 t->t_stateflags &= ~TS_WRAPPED; 709 teken_funcs_cursor(t); 710 } 711 712 static void 713 teken_subr_newpage(teken_t *t) 714 { 715 716 if (t->t_stateflags & TS_CONS25) { 717 teken_rect_t tr; 718 719 /* Clear screen. */ 720 tr.tr_begin.tp_row = t->t_originreg.ts_begin; 721 tr.tr_begin.tp_col = 0; 722 tr.tr_end.tp_row = t->t_originreg.ts_end; 723 tr.tr_end.tp_col = t->t_winsize.tp_col; 724 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr); 725 726 /* Cursor at top left. */ 727 t->t_cursor.tp_row = t->t_originreg.ts_begin; 728 t->t_cursor.tp_col = 0; 729 t->t_stateflags &= ~TS_WRAPPED; 730 teken_funcs_cursor(t); 731 } else { 732 teken_subr_newline(t); 733 } 734 } 735 736 static void 737 teken_subr_next_line(teken_t *t) 738 { 739 740 t->t_cursor.tp_col = 0; 741 teken_subr_newline(t); 742 } 743 744 static void 745 teken_subr_operating_system_command(teken_t *t) 746 { 747 748 teken_printf("Unsupported operating system command\n"); 749 t->t_stateflags |= TS_INSTRING; 750 } 751 752 static void 753 teken_subr_pan_down(const teken_t *t, unsigned int nrows) 754 { 755 756 teken_subr_do_scroll(t, (int)nrows); 757 } 758 759 static void 760 teken_subr_pan_up(const teken_t *t, unsigned int nrows) 761 { 762 763 teken_subr_do_scroll(t, -(int)nrows); 764 } 765 766 static void 767 teken_subr_primary_device_attributes(const teken_t *t, unsigned int request) 768 { 769 770 if (request == 0) { 771 const char response[] = "\x1B[?1;2c"; 772 773 teken_funcs_respond(t, response, sizeof response - 1); 774 } else { 775 teken_printf("Unknown DA1\n"); 776 } 777 } 778 779 static void 780 teken_subr_do_putchar(const teken_t *t, const teken_pos_t *tp, teken_char_t c, 781 int width) 782 { 783 784 if (t->t_stateflags & TS_INSERT && 785 tp->tp_col < t->t_winsize.tp_col - width) { 786 teken_rect_t ctr; 787 teken_pos_t ctp; 788 789 /* Insert mode. Move existing characters to the right. */ 790 ctr.tr_begin = *tp; 791 ctr.tr_end.tp_row = tp->tp_row + 1; 792 ctr.tr_end.tp_col = t->t_winsize.tp_col - width; 793 ctp.tp_row = tp->tp_row; 794 ctp.tp_col = tp->tp_col + width; 795 teken_funcs_copy(t, &ctr, &ctp); 796 } 797 798 teken_funcs_putchar(t, tp, c, &t->t_curattr); 799 800 if (width == 2 && tp->tp_col + 1 < t->t_winsize.tp_col) { 801 teken_pos_t tp2; 802 teken_attr_t attr; 803 804 /* Print second half of CJK fullwidth character. */ 805 tp2.tp_row = tp->tp_row; 806 tp2.tp_col = tp->tp_col + 1; 807 attr = t->t_curattr; 808 attr.ta_format |= TF_CJK_RIGHT; 809 teken_funcs_putchar(t, &tp2, c, &attr); 810 } 811 } 812 813 static void 814 teken_subr_regular_character(teken_t *t, teken_char_t c) 815 { 816 int width; 817 818 if (t->t_stateflags & TS_8BIT) { 819 if (!(t->t_stateflags & TS_CONS25) && (c <= 0x1b || c == 0x7f)) 820 return; 821 c = teken_scs_process(t, c); 822 width = 1; 823 } else { 824 c = teken_scs_process(t, c); 825 width = teken_wcwidth(c); 826 /* XXX: Don't process zero-width characters yet. */ 827 if (width <= 0) 828 return; 829 } 830 831 if (t->t_stateflags & TS_CONS25) { 832 teken_subr_do_putchar(t, &t->t_cursor, c, width); 833 t->t_cursor.tp_col += width; 834 835 if (t->t_cursor.tp_col >= t->t_winsize.tp_col) { 836 if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) { 837 /* Perform scrolling. */ 838 teken_subr_do_scroll(t, 1); 839 } else { 840 /* No scrolling needed. */ 841 if (t->t_cursor.tp_row < 842 t->t_winsize.tp_row - 1) 843 t->t_cursor.tp_row++; 844 } 845 t->t_cursor.tp_col = 0; 846 } 847 } else if (t->t_stateflags & TS_AUTOWRAP && 848 ((t->t_stateflags & TS_WRAPPED && 849 t->t_cursor.tp_col + 1 == t->t_winsize.tp_col) || 850 t->t_cursor.tp_col + width > t->t_winsize.tp_col)) { 851 teken_pos_t tp; 852 853 /* 854 * Perform line wrapping, if: 855 * - Autowrapping is enabled, and 856 * - We're in the wrapped state at the last column, or 857 * - The character to be printed does not fit anymore. 858 */ 859 if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) { 860 /* Perform scrolling. */ 861 teken_subr_do_scroll(t, 1); 862 tp.tp_row = t->t_scrollreg.ts_end - 1; 863 } else { 864 /* No scrolling needed. */ 865 tp.tp_row = t->t_cursor.tp_row + 1; 866 if (tp.tp_row == t->t_winsize.tp_row) { 867 /* 868 * Corner case: regular character 869 * outside scrolling region, but at the 870 * bottom of the screen. 871 */ 872 teken_subr_do_putchar(t, &t->t_cursor, 873 c, width); 874 return; 875 } 876 } 877 878 tp.tp_col = 0; 879 teken_subr_do_putchar(t, &tp, c, width); 880 881 t->t_cursor.tp_row = tp.tp_row; 882 t->t_cursor.tp_col = width; 883 t->t_stateflags &= ~TS_WRAPPED; 884 } else { 885 /* No line wrapping needed. */ 886 teken_subr_do_putchar(t, &t->t_cursor, c, width); 887 t->t_cursor.tp_col += width; 888 889 if (t->t_cursor.tp_col >= t->t_winsize.tp_col) { 890 t->t_stateflags |= TS_WRAPPED; 891 t->t_cursor.tp_col = t->t_winsize.tp_col - 1; 892 } else { 893 t->t_stateflags &= ~TS_WRAPPED; 894 } 895 } 896 897 teken_funcs_cursor(t); 898 } 899 900 static void 901 teken_subr_reset_dec_mode(teken_t *t, unsigned int cmd) 902 { 903 904 switch (cmd) { 905 case 1: /* Cursor keys mode. */ 906 t->t_stateflags &= ~TS_CURSORKEYS; 907 break; 908 case 2: /* DECANM: ANSI/VT52 mode. */ 909 teken_printf("DECRST VT52\n"); 910 break; 911 case 3: /* 132 column mode. */ 912 teken_funcs_param(t, TP_132COLS, 0); 913 teken_subr_reset_to_initial_state(t); 914 break; 915 case 5: /* Inverse video. */ 916 teken_printf("DECRST inverse video\n"); 917 break; 918 case 6: /* Origin mode. */ 919 t->t_stateflags &= ~TS_ORIGIN; 920 t->t_originreg.ts_begin = 0; 921 t->t_originreg.ts_end = t->t_winsize.tp_row; 922 t->t_cursor.tp_row = t->t_cursor.tp_col = 0; 923 t->t_stateflags &= ~TS_WRAPPED; 924 teken_funcs_cursor(t); 925 break; 926 case 7: /* Autowrap mode. */ 927 t->t_stateflags &= ~TS_AUTOWRAP; 928 break; 929 case 8: /* Autorepeat mode. */ 930 teken_funcs_param(t, TP_AUTOREPEAT, 0); 931 break; 932 case 25: /* Hide cursor. */ 933 teken_funcs_param(t, TP_SHOWCURSOR, 0); 934 break; 935 case 40: /* Disallow 132 columns. */ 936 teken_printf("DECRST allow 132\n"); 937 break; 938 case 45: /* Disable reverse wraparound. */ 939 teken_printf("DECRST reverse wraparound\n"); 940 break; 941 case 47: /* Switch to alternate buffer. */ 942 teken_printf("Switch to alternate buffer\n"); 943 break; 944 case 1000: /* Mouse input. */ 945 teken_funcs_param(t, TP_MOUSE, 0); 946 break; 947 default: 948 teken_printf("Unknown DECRST: %u\n", cmd); 949 } 950 } 951 952 static void 953 teken_subr_reset_mode(teken_t *t, unsigned int cmd) 954 { 955 956 switch (cmd) { 957 case 4: 958 t->t_stateflags &= ~TS_INSERT; 959 break; 960 default: 961 teken_printf("Unknown reset mode: %u\n", cmd); 962 } 963 } 964 965 static void 966 teken_subr_do_resize(teken_t *t) 967 { 968 969 t->t_scrollreg.ts_begin = 0; 970 t->t_scrollreg.ts_end = t->t_winsize.tp_row; 971 t->t_originreg = t->t_scrollreg; 972 } 973 974 static void 975 teken_subr_do_reset(teken_t *t) 976 { 977 978 t->t_curattr = t->t_defattr; 979 t->t_cursor.tp_row = t->t_cursor.tp_col = 0; 980 t->t_scrollreg.ts_begin = 0; 981 t->t_scrollreg.ts_end = t->t_winsize.tp_row; 982 t->t_originreg = t->t_scrollreg; 983 t->t_stateflags &= TS_8BIT|TS_CONS25; 984 t->t_stateflags |= TS_AUTOWRAP; 985 986 t->t_scs[0] = teken_scs_us_ascii; 987 t->t_scs[1] = teken_scs_us_ascii; 988 t->t_curscs = 0; 989 990 teken_subr_save_cursor(t); 991 teken_tab_default(t); 992 } 993 994 static void 995 teken_subr_reset_to_initial_state(teken_t *t) 996 { 997 998 teken_subr_do_reset(t); 999 teken_subr_erase_display(t, 2); 1000 teken_funcs_param(t, TP_SHOWCURSOR, 1); 1001 teken_funcs_cursor(t); 1002 } 1003 1004 static void 1005 teken_subr_restore_cursor(teken_t *t) 1006 { 1007 1008 t->t_cursor = t->t_saved_cursor; 1009 t->t_curattr = t->t_saved_curattr; 1010 t->t_scs[t->t_curscs] = t->t_saved_curscs; 1011 t->t_stateflags &= ~TS_WRAPPED; 1012 1013 /* Get out of origin mode when the cursor is moved outside. */ 1014 if (t->t_cursor.tp_row < t->t_originreg.ts_begin || 1015 t->t_cursor.tp_row >= t->t_originreg.ts_end) { 1016 t->t_stateflags &= ~TS_ORIGIN; 1017 t->t_originreg.ts_begin = 0; 1018 t->t_originreg.ts_end = t->t_winsize.tp_row; 1019 } 1020 1021 teken_funcs_cursor(t); 1022 } 1023 1024 static void 1025 teken_subr_reverse_index(teken_t *t) 1026 { 1027 1028 if (t->t_cursor.tp_row > t->t_scrollreg.ts_begin) { 1029 t->t_cursor.tp_row--; 1030 t->t_stateflags &= ~TS_WRAPPED; 1031 teken_funcs_cursor(t); 1032 } else { 1033 teken_subr_do_scroll(t, -1); 1034 } 1035 } 1036 1037 static void 1038 teken_subr_save_cursor(teken_t *t) 1039 { 1040 1041 t->t_saved_cursor = t->t_cursor; 1042 t->t_saved_curattr = t->t_curattr; 1043 t->t_saved_curscs = t->t_scs[t->t_curscs]; 1044 } 1045 1046 static void 1047 teken_subr_secondary_device_attributes(const teken_t *t, unsigned int request) 1048 { 1049 1050 if (request == 0) { 1051 const char response[] = "\x1B[>0;10;0c"; 1052 teken_funcs_respond(t, response, sizeof response - 1); 1053 } else { 1054 teken_printf("Unknown DA2\n"); 1055 } 1056 } 1057 1058 static void 1059 teken_subr_set_dec_mode(teken_t *t, unsigned int cmd) 1060 { 1061 1062 switch (cmd) { 1063 case 1: /* Cursor keys mode. */ 1064 t->t_stateflags |= TS_CURSORKEYS; 1065 break; 1066 case 2: /* DECANM: ANSI/VT52 mode. */ 1067 teken_printf("DECSET VT52\n"); 1068 break; 1069 case 3: /* 132 column mode. */ 1070 teken_funcs_param(t, TP_132COLS, 1); 1071 teken_subr_reset_to_initial_state(t); 1072 break; 1073 case 5: /* Inverse video. */ 1074 teken_printf("DECSET inverse video\n"); 1075 break; 1076 case 6: /* Origin mode. */ 1077 t->t_stateflags |= TS_ORIGIN; 1078 t->t_originreg = t->t_scrollreg; 1079 t->t_cursor.tp_row = t->t_scrollreg.ts_begin; 1080 t->t_cursor.tp_col = 0; 1081 t->t_stateflags &= ~TS_WRAPPED; 1082 teken_funcs_cursor(t); 1083 break; 1084 case 7: /* Autowrap mode. */ 1085 t->t_stateflags |= TS_AUTOWRAP; 1086 break; 1087 case 8: /* Autorepeat mode. */ 1088 teken_funcs_param(t, TP_AUTOREPEAT, 1); 1089 break; 1090 case 25: /* Display cursor. */ 1091 teken_funcs_param(t, TP_SHOWCURSOR, 1); 1092 break; 1093 case 40: /* Allow 132 columns. */ 1094 teken_printf("DECSET allow 132\n"); 1095 break; 1096 case 45: /* Enable reverse wraparound. */ 1097 teken_printf("DECSET reverse wraparound\n"); 1098 break; 1099 case 47: /* Switch to alternate buffer. */ 1100 teken_printf("Switch away from alternate buffer\n"); 1101 break; 1102 case 1000: /* Mouse input. */ 1103 teken_funcs_param(t, TP_MOUSE, 1); 1104 break; 1105 default: 1106 teken_printf("Unknown DECSET: %u\n", cmd); 1107 } 1108 } 1109 1110 static void 1111 teken_subr_set_mode(teken_t *t, unsigned int cmd) 1112 { 1113 1114 switch (cmd) { 1115 case 4: 1116 teken_printf("Insert mode\n"); 1117 t->t_stateflags |= TS_INSERT; 1118 break; 1119 default: 1120 teken_printf("Unknown set mode: %u\n", cmd); 1121 } 1122 } 1123 1124 static void 1125 teken_subr_set_graphic_rendition(teken_t *t, unsigned int ncmds, 1126 const unsigned int cmds[]) 1127 { 1128 unsigned int i, n; 1129 1130 /* No attributes means reset. */ 1131 if (ncmds == 0) { 1132 t->t_curattr = t->t_defattr; 1133 return; 1134 } 1135 1136 for (i = 0; i < ncmds; i++) { 1137 n = cmds[i]; 1138 1139 switch (n) { 1140 case 0: /* Reset. */ 1141 t->t_curattr = t->t_defattr; 1142 break; 1143 case 1: /* Bold. */ 1144 t->t_curattr.ta_format |= TF_BOLD; 1145 break; 1146 case 4: /* Underline. */ 1147 t->t_curattr.ta_format |= TF_UNDERLINE; 1148 break; 1149 case 5: /* Blink. */ 1150 t->t_curattr.ta_format |= TF_BLINK; 1151 break; 1152 case 7: /* Reverse. */ 1153 t->t_curattr.ta_format |= TF_REVERSE; 1154 break; 1155 case 22: /* Remove bold. */ 1156 t->t_curattr.ta_format &= ~TF_BOLD; 1157 break; 1158 case 24: /* Remove underline. */ 1159 t->t_curattr.ta_format &= ~TF_UNDERLINE; 1160 break; 1161 case 25: /* Remove blink. */ 1162 t->t_curattr.ta_format &= ~TF_BLINK; 1163 break; 1164 case 27: /* Remove reverse. */ 1165 t->t_curattr.ta_format &= ~TF_REVERSE; 1166 break; 1167 case 30: /* Set foreground color: black */ 1168 case 31: /* Set foreground color: red */ 1169 case 32: /* Set foreground color: green */ 1170 case 33: /* Set foreground color: brown */ 1171 case 34: /* Set foreground color: blue */ 1172 case 35: /* Set foreground color: magenta */ 1173 case 36: /* Set foreground color: cyan */ 1174 case 37: /* Set foreground color: white */ 1175 t->t_curattr.ta_fgcolor = n - 30; 1176 break; 1177 case 38: /* Set foreground color: 256 color mode */ 1178 if (i + 2 >= ncmds || cmds[i + 1] != 5) 1179 continue; 1180 t->t_curattr.ta_fgcolor = cmds[i + 2]; 1181 i += 2; 1182 break; 1183 case 39: /* Set default foreground color. */ 1184 t->t_curattr.ta_fgcolor = t->t_defattr.ta_fgcolor; 1185 break; 1186 case 40: /* Set background color: black */ 1187 case 41: /* Set background color: red */ 1188 case 42: /* Set background color: green */ 1189 case 43: /* Set background color: brown */ 1190 case 44: /* Set background color: blue */ 1191 case 45: /* Set background color: magenta */ 1192 case 46: /* Set background color: cyan */ 1193 case 47: /* Set background color: white */ 1194 t->t_curattr.ta_bgcolor = n - 40; 1195 break; 1196 case 48: /* Set background color: 256 color mode */ 1197 if (i + 2 >= ncmds || cmds[i + 1] != 5) 1198 continue; 1199 t->t_curattr.ta_bgcolor = cmds[i + 2]; 1200 i += 2; 1201 break; 1202 case 49: /* Set default background color. */ 1203 t->t_curattr.ta_bgcolor = t->t_defattr.ta_bgcolor; 1204 break; 1205 case 90: /* Set bright foreground color: black */ 1206 case 91: /* Set bright foreground color: red */ 1207 case 92: /* Set bright foreground color: green */ 1208 case 93: /* Set bright foreground color: brown */ 1209 case 94: /* Set bright foreground color: blue */ 1210 case 95: /* Set bright foreground color: magenta */ 1211 case 96: /* Set bright foreground color: cyan */ 1212 case 97: /* Set bright foreground color: white */ 1213 t->t_curattr.ta_fgcolor = (n - 90) + 8; 1214 break; 1215 case 100: /* Set bright background color: black */ 1216 case 101: /* Set bright background color: red */ 1217 case 102: /* Set bright background color: green */ 1218 case 103: /* Set bright background color: brown */ 1219 case 104: /* Set bright background color: blue */ 1220 case 105: /* Set bright background color: magenta */ 1221 case 106: /* Set bright background color: cyan */ 1222 case 107: /* Set bright background color: white */ 1223 t->t_curattr.ta_bgcolor = (n - 100) + 8; 1224 break; 1225 default: 1226 teken_printf("unsupported attribute %u\n", n); 1227 } 1228 } 1229 } 1230 1231 static void 1232 teken_subr_set_top_and_bottom_margins(teken_t *t, unsigned int top, 1233 unsigned int bottom) 1234 { 1235 1236 /* Adjust top row number. */ 1237 if (top > 0) 1238 top--; 1239 /* Adjust bottom row number. */ 1240 if (bottom == 0 || bottom > t->t_winsize.tp_row) 1241 bottom = t->t_winsize.tp_row; 1242 1243 /* Invalid arguments. */ 1244 if (top >= bottom - 1) { 1245 top = 0; 1246 bottom = t->t_winsize.tp_row; 1247 } 1248 1249 /* Apply scrolling region. */ 1250 t->t_scrollreg.ts_begin = top; 1251 t->t_scrollreg.ts_end = bottom; 1252 if (t->t_stateflags & TS_ORIGIN) 1253 t->t_originreg = t->t_scrollreg; 1254 1255 /* Home cursor to the top left of the scrolling region. */ 1256 t->t_cursor.tp_row = t->t_originreg.ts_begin; 1257 t->t_cursor.tp_col = 0; 1258 t->t_stateflags &= ~TS_WRAPPED; 1259 teken_funcs_cursor(t); 1260 } 1261 1262 static void 1263 teken_subr_single_height_double_width_line(const teken_t *t) 1264 { 1265 1266 (void)t; 1267 teken_printf("single height double width???\n"); 1268 } 1269 1270 static void 1271 teken_subr_single_height_single_width_line(const teken_t *t) 1272 { 1273 1274 (void)t; 1275 teken_printf("single height single width???\n"); 1276 } 1277 1278 static void 1279 teken_subr_string_terminator(const teken_t *t) 1280 { 1281 1282 (void)t; 1283 /* 1284 * Strings are already terminated in teken_input_char() when ^[ 1285 * is inserted. 1286 */ 1287 } 1288 1289 static void 1290 teken_subr_tab_clear(teken_t *t, unsigned int cmd) 1291 { 1292 1293 switch (cmd) { 1294 case 0: 1295 teken_tab_clear(t, t->t_cursor.tp_col); 1296 break; 1297 case 3: 1298 memset(t->t_tabstops, 0, T_NUMCOL / 8); 1299 break; 1300 default: 1301 break; 1302 } 1303 } 1304 1305 static void 1306 teken_subr_vertical_position_absolute(teken_t *t, unsigned int row) 1307 { 1308 1309 row = (row - 1) + t->t_originreg.ts_begin; 1310 t->t_cursor.tp_row = row < t->t_originreg.ts_end ? 1311 row : t->t_originreg.ts_end - 1; 1312 1313 t->t_stateflags &= ~TS_WRAPPED; 1314 teken_funcs_cursor(t); 1315 } 1316