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