1 /* 2 * Copyright (C) 1984-2025 Mark Nudelman 3 * 4 * You may distribute under the terms of either the GNU General Public 5 * License or the Less License, as specified in the README file. 6 * 7 * For more information, see the README file. 8 */ 9 10 11 /* 12 * Routines which deal with the characteristics of the terminal. 13 * Uses termcap to be as terminal-independent as possible. 14 */ 15 16 #include "less.h" 17 #include "cmd.h" 18 19 #if MSDOS_COMPILER 20 #include "pckeys.h" 21 #if MSDOS_COMPILER==MSOFTC 22 #include <graph.h> 23 #else 24 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 25 #include <conio.h> 26 #if MSDOS_COMPILER==DJGPPC 27 #include <pc.h> 28 extern int fd0; 29 #endif 30 #else 31 #if MSDOS_COMPILER==WIN32C 32 #include <windows.h> 33 #endif 34 #endif 35 #endif 36 #include <time.h> 37 38 #ifndef FOREGROUND_BLUE 39 #define FOREGROUND_BLUE 0x0001 40 #endif 41 #ifndef FOREGROUND_GREEN 42 #define FOREGROUND_GREEN 0x0002 43 #endif 44 #ifndef FOREGROUND_RED 45 #define FOREGROUND_RED 0x0004 46 #endif 47 #ifndef FOREGROUND_INTENSITY 48 #define FOREGROUND_INTENSITY 0x0008 49 #endif 50 51 #else 52 53 #if HAVE_SYS_IOCTL_H 54 #include <sys/ioctl.h> 55 #endif 56 57 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS 58 #include <termios.h> 59 #else 60 #if HAVE_TERMIO_H 61 #include <termio.h> 62 #else 63 #if HAVE_SGSTAT_H 64 #include <sgstat.h> 65 #else 66 #include <sgtty.h> 67 #endif 68 #endif 69 #endif 70 71 #if HAVE_NCURSESW_TERMCAP_H 72 #include <ncursesw/termcap.h> 73 #else 74 #if HAVE_NCURSES_TERMCAP_H 75 #include <ncurses/termcap.h> 76 #else 77 #if HAVE_TERMCAP_H 78 #include <termcap.h> 79 #endif 80 #endif 81 #endif 82 #ifdef _OSK 83 #include <signal.h> 84 #endif 85 #if OS2 86 #include <sys/signal.h> 87 #include "pckeys.h" 88 #endif 89 #if HAVE_SYS_STREAM_H 90 #include <sys/stream.h> 91 #endif 92 #if HAVE_SYS_PTEM_H 93 #include <sys/ptem.h> 94 #endif 95 96 #endif /* MSDOS_COMPILER */ 97 98 /* 99 * Check for broken termios package that forces you to manually 100 * set the line discipline. 101 */ 102 #ifdef __ultrix__ 103 #define MUST_SET_LINE_DISCIPLINE 1 104 #else 105 #define MUST_SET_LINE_DISCIPLINE 0 106 #endif 107 108 #if OS2 109 #define DEFAULT_TERM "ansi" 110 static char *windowid; 111 #else 112 #define DEFAULT_TERM "unknown" 113 #endif 114 115 #if MSDOS_COMPILER==MSOFTC 116 static int videopages; 117 static long msec_loops; 118 static int flash_created = 0; 119 #define SET_FG_COLOR(fg) _settextcolor(fg) 120 #define SET_BG_COLOR(bg) _setbkcolor(bg) 121 #define SETCOLORS(fg,bg) { SET_FG_COLOR(fg); SET_BG_COLOR(bg); } 122 #endif 123 124 #if MSDOS_COMPILER==BORLANDC 125 static unsigned short *whitescreen; 126 static int flash_created = 0; 127 #endif 128 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 129 #define _settextposition(y,x) gotoxy(x,y) 130 #define _clearscreen(m) clrscr() 131 #define _outtext(s) cputs(s) 132 #define SET_FG_COLOR(fg) textcolor(fg) 133 #define SET_BG_COLOR(bg) textbackground(bg) 134 #define SETCOLORS(fg,bg) { SET_FG_COLOR(fg); SET_BG_COLOR(bg); } 135 extern int sc_height; 136 #endif 137 138 #if MSDOS_COMPILER==WIN32C 139 #define UTF8_MAX_LENGTH 4 140 141 static WORD curr_attr; 142 143 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */ 144 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */ 145 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */ 146 147 extern int utf_mode; 148 extern lbool quitting; 149 static void win32_init_term(); 150 static void win32_deinit_term(); 151 152 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING 153 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 4 154 #endif 155 156 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY) 157 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY) 158 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4))) 159 #define APPLY_COLORS() { if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \ 160 error("SETCOLORS failed", NULL_PARG); } 161 #define SET_FG_COLOR(fg) { curr_attr &= ~0x0f; curr_attr |= (fg); APPLY_COLORS(); } 162 #define SET_BG_COLOR(bg) { curr_attr &= ~0xf0; curr_attr |= ((bg)<<4); APPLY_COLORS(); } 163 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); APPLY_COLORS(); } 164 #endif 165 166 #if MSDOS_COMPILER 167 public int nm_fg_color = CV_ERROR; /* Color of normal text */ 168 public int nm_bg_color = CV_ERROR; 169 public int nm_attr = 0; 170 public int bo_fg_color = CV_ERROR; /* Color of bold text */ 171 public int bo_bg_color = CV_ERROR; 172 public int bo_attr = 0; 173 public int ul_fg_color = CV_ERROR; /* Color of underlined text */ 174 public int ul_bg_color = CV_ERROR; 175 public int ul_attr = 0; 176 public int so_fg_color = CV_ERROR; /* Color of standout text */ 177 public int so_bg_color = CV_ERROR; 178 public int so_attr = 0; 179 public int bl_fg_color = CV_ERROR; /* Color of blinking text */ 180 public int bl_bg_color = CV_ERROR; 181 public int bl_attr = 0; 182 static int sy_fg_color; /* Color of system text (before less) */ 183 static int sy_bg_color; 184 public int sgr_mode; /* Honor ANSI sequences rather than using above */ 185 #if MSDOS_COMPILER==WIN32C 186 static DWORD init_console_output_mode; 187 extern DWORD init_console_input_mode; 188 extern DWORD curr_console_input_mode; 189 extern DWORD base_console_input_mode; 190 extern DWORD mouse_console_input_mode; 191 public int vt_enabled = -1; /* Is virtual terminal processing available? */ 192 #endif 193 #else 194 195 /* 196 * Strings passed to tputs() to do various terminal functions. 197 */ 198 static constant char 199 *sc_pad, /* Pad string */ 200 *sc_home, /* Cursor home */ 201 *sc_addline, /* Add line, scroll down following lines */ 202 *sc_lower_left, /* Cursor to last line, first column */ 203 *sc_return, /* Cursor to beginning of current line */ 204 *sc_move, /* General cursor positioning */ 205 *sc_clear, /* Clear screen */ 206 *sc_eol_clear, /* Clear to end of line */ 207 *sc_eos_clear, /* Clear to end of screen */ 208 *sc_s_in, /* Enter standout (highlighted) mode */ 209 *sc_s_out, /* Exit standout mode */ 210 *sc_u_in, /* Enter underline mode */ 211 *sc_u_out, /* Exit underline mode */ 212 *sc_b_in, /* Enter bold mode */ 213 *sc_b_out, /* Exit bold mode */ 214 *sc_bl_in, /* Enter blink mode */ 215 *sc_bl_out, /* Exit blink mode */ 216 *sc_visual_bell, /* Visual bell (flash screen) sequence */ 217 *sc_backspace, /* Backspace cursor */ 218 *sc_s_keypad, /* Start keypad mode */ 219 *sc_e_keypad, /* End keypad mode */ 220 *sc_s_mousecap, /* Start mouse capture mode */ 221 *sc_e_mousecap, /* End mouse capture mode */ 222 *sc_s_bracketed_paste, /* Start bracketed paste mode */ 223 *sc_e_bracketed_paste, /* End bracketed paste mode */ 224 *sc_init, /* Startup terminal initialization */ 225 *sc_deinit; /* Exit terminal de-initialization */ 226 227 static int attrcolor = -1; 228 #endif 229 230 static int init_done = 0; 231 232 public int auto_wrap; /* Terminal does \r\n when write past margin */ 233 public int ignaw; /* Terminal ignores \n immediately after wrap */ 234 public int erase_char; /* The user's erase char */ 235 public int erase2_char; /* The user's other erase char */ 236 public int kill_char; /* The user's line-kill char */ 237 public int werase_char; /* The user's word-erase char */ 238 public int sc_width, sc_height; /* Height & width of screen */ 239 public int bo_s_width, bo_e_width; /* Printing width of boldface seq */ 240 public int ul_s_width, ul_e_width; /* Printing width of underline seq */ 241 public int so_s_width, so_e_width; /* Printing width of standout seq */ 242 public int bl_s_width, bl_e_width; /* Printing width of blink seq */ 243 public int above_mem, below_mem; /* Memory retained above/below screen */ 244 public int can_goto_line; /* Can move cursor to any line */ 245 public int clear_bg; /* Clear fills with background color */ 246 public lbool missing_cap = FALSE; /* Some capability is missing */ 247 public constant char *kent = NULL; /* Keypad ENTER sequence */ 248 public lbool term_init_done = FALSE; 249 public lbool full_screen = TRUE; 250 251 static int attrmode = AT_NORMAL; 252 static int termcap_debug = -1; 253 static int no_alt_screen; /* sc_init does not switch to alt screen */ 254 extern int binattr; 255 extern int one_screen; 256 extern int shell_lines; 257 258 #if !MSDOS_COMPILER 259 static constant char *cheaper(constant char *t1, constant char *t2, constant char *def); 260 static void tmodes(constant char *incap, constant char *outcap, constant char **instr, 261 constant char **outstr, constant char *def_instr, constant char *def_outstr, char **spp); 262 #endif 263 264 /* 265 * These two variables are sometimes defined in, 266 * and needed by, the termcap library. 267 */ 268 #if MUST_DEFINE_OSPEED 269 extern short ospeed; /* Terminal output baud rate */ 270 extern char PC; /* Pad character */ 271 #endif 272 #ifdef _OSK 273 short ospeed; 274 char PC_, *UP, *BC; 275 #endif 276 277 extern int quiet; /* If VERY_QUIET, use visual bell for bell */ 278 extern int no_vbell; 279 extern int no_back_scroll; 280 extern int no_init; 281 extern int no_keypad; 282 extern int sigs; 283 extern int top_scroll; 284 extern int quit_if_one_screen; 285 extern int oldbot; 286 extern int mousecap; 287 extern int is_tty; 288 extern int use_color; 289 extern int no_paste; 290 #if HILITE_SEARCH 291 extern int hilite_search; 292 #endif 293 #if MSDOS_COMPILER==WIN32C 294 extern int wscroll; 295 extern HANDLE tty; 296 #else 297 extern int tty; 298 #endif 299 300 #if (HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS) || defined(TCGETA) 301 /* 302 * Set termio flags for use by less. 303 */ 304 static void set_termio_flags( 305 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS 306 struct termios *s 307 #else 308 struct termio *s 309 #endif 310 ) 311 { 312 s->c_lflag &= ~(0 313 #ifdef ICANON 314 | ICANON 315 #endif 316 #ifdef ECHO 317 | ECHO 318 #endif 319 #ifdef ECHOE 320 | ECHOE 321 #endif 322 #ifdef ECHOK 323 | ECHOK 324 #endif 325 #ifdef ECHONL 326 | ECHONL 327 #endif 328 ); 329 330 s->c_oflag |= (0 331 #ifdef OPOST 332 | OPOST 333 #endif 334 #ifdef ONLCR 335 | ONLCR 336 #endif 337 ); 338 339 s->c_oflag &= ~(0 340 #ifdef ONOEOT 341 | ONOEOT 342 #endif 343 #ifdef OCRNL 344 | OCRNL 345 #endif 346 #ifdef ONOCR 347 | ONOCR 348 #endif 349 #ifdef ONLRET 350 | ONLRET 351 #endif 352 ); 353 } 354 #endif 355 356 /* 357 * Change terminal to "raw mode", or restore to "normal" mode. 358 * "Raw mode" means 359 * 1. An outstanding read will complete on receipt of a single keystroke. 360 * 2. Input is not echoed. 361 * 3. On output, \n is mapped to \r\n. 362 * 4. \t is NOT expanded into spaces. 363 * 5. Signal-causing characters such as ctrl-C (interrupt), 364 * etc. are NOT disabled. 365 * It doesn't matter whether an input \n is mapped to \r, or vice versa. 366 */ 367 public void raw_mode(int on) 368 { 369 static int curr_on = 0; 370 371 if (on == curr_on) 372 return; 373 erase2_char = '\b'; /* in case OS doesn't know about erase2 */ 374 #if LESSTEST 375 if (is_lesstest()) 376 { 377 /* {{ For consistent conditions when running tests. }} */ 378 erase_char = '\b'; 379 kill_char = CONTROL('U'); 380 werase_char = CONTROL('W'); 381 } else 382 #endif /*LESSTEST*/ 383 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS 384 { 385 struct termios s; 386 static struct termios save_term; 387 static int saved_term = 0; 388 389 if (on) 390 { 391 /* 392 * Get terminal modes. 393 */ 394 if (tcgetattr(tty, &s) < 0) 395 { 396 erase_char = '\b'; 397 kill_char = CONTROL('U'); 398 werase_char = CONTROL('W'); 399 } else 400 { 401 /* 402 * Save modes and set certain variables dependent on modes. 403 */ 404 if (!saved_term) 405 { 406 save_term = s; 407 saved_term = 1; 408 } 409 #if HAVE_OSPEED 410 switch (cfgetospeed(&s)) 411 { 412 #ifdef B0 413 case B0: ospeed = 0; break; 414 #endif 415 #ifdef B50 416 case B50: ospeed = 1; break; 417 #endif 418 #ifdef B75 419 case B75: ospeed = 2; break; 420 #endif 421 #ifdef B110 422 case B110: ospeed = 3; break; 423 #endif 424 #ifdef B134 425 case B134: ospeed = 4; break; 426 #endif 427 #ifdef B150 428 case B150: ospeed = 5; break; 429 #endif 430 #ifdef B200 431 case B200: ospeed = 6; break; 432 #endif 433 #ifdef B300 434 case B300: ospeed = 7; break; 435 #endif 436 #ifdef B600 437 case B600: ospeed = 8; break; 438 #endif 439 #ifdef B1200 440 case B1200: ospeed = 9; break; 441 #endif 442 #ifdef B1800 443 case B1800: ospeed = 10; break; 444 #endif 445 #ifdef B2400 446 case B2400: ospeed = 11; break; 447 #endif 448 #ifdef B4800 449 case B4800: ospeed = 12; break; 450 #endif 451 #ifdef B9600 452 case B9600: ospeed = 13; break; 453 #endif 454 #ifdef EXTA 455 case EXTA: ospeed = 14; break; 456 #endif 457 #ifdef EXTB 458 case EXTB: ospeed = 15; break; 459 #endif 460 #ifdef B57600 461 case B57600: ospeed = 16; break; 462 #endif 463 #ifdef B115200 464 case B115200: ospeed = 17; break; 465 #endif 466 default: ; 467 } 468 #endif 469 erase_char = s.c_cc[VERASE]; 470 #ifdef VERASE2 471 erase2_char = s.c_cc[VERASE2]; 472 #endif 473 kill_char = s.c_cc[VKILL]; 474 #ifdef VWERASE 475 werase_char = s.c_cc[VWERASE]; 476 #else 477 werase_char = CONTROL('W'); 478 #endif 479 480 /* 481 * Set the modes to the way we want them. 482 */ 483 set_termio_flags(&s); 484 s.c_cc[VMIN] = 1; 485 s.c_cc[VTIME] = 0; 486 #ifdef VLNEXT 487 s.c_cc[VLNEXT] = 0; 488 #endif 489 #ifdef VDSUSP 490 s.c_cc[VDSUSP] = 0; 491 #endif 492 #ifdef VSTOP 493 s.c_cc[VSTOP] = 0; 494 #endif 495 #ifdef VSTART 496 s.c_cc[VSTART] = 0; 497 #endif 498 #ifdef VDISCARD 499 s.c_cc[VDISCARD] = 0; 500 #endif 501 #if MUST_SET_LINE_DISCIPLINE 502 /* 503 * System's termios is broken; need to explicitly 504 * request TERMIODISC line discipline. 505 */ 506 s.c_line = TERMIODISC; 507 #endif 508 } 509 } else 510 { 511 /* 512 * Restore saved modes. 513 */ 514 s = save_term; 515 } 516 #if HAVE_FSYNC 517 fsync(tty); 518 #endif 519 tcsetattr(tty, TCSADRAIN, &s); 520 #if MUST_SET_LINE_DISCIPLINE 521 if (!on) 522 { 523 /* 524 * Broken termios *ignores* any line discipline 525 * except TERMIODISC. A different old line discipline 526 * is therefore not restored, yet. Restore the old 527 * line discipline by hand. 528 */ 529 ioctl(tty, TIOCSETD, &save_term.c_line); 530 } 531 #endif 532 } 533 #else 534 #ifdef TCGETA 535 { 536 struct termio s; 537 static struct termio save_term; 538 static int saved_term = 0; 539 540 if (on) 541 { 542 /* 543 * Get terminal modes. 544 */ 545 ioctl(tty, TCGETA, &s); 546 547 /* 548 * Save modes and set certain variables dependent on modes. 549 */ 550 if (!saved_term) 551 { 552 save_term = s; 553 saved_term = 1; 554 } 555 #if HAVE_OSPEED 556 ospeed = s.c_cflag & CBAUD; 557 #endif 558 erase_char = s.c_cc[VERASE]; 559 kill_char = s.c_cc[VKILL]; 560 #ifdef VWERASE 561 werase_char = s.c_cc[VWERASE]; 562 #else 563 werase_char = CONTROL('W'); 564 #endif 565 566 /* 567 * Set the modes to the way we want them. 568 */ 569 set_termio_flags(&s); 570 s.c_cc[VMIN] = 1; 571 s.c_cc[VTIME] = 0; 572 #ifdef VSTOP 573 s.c_cc[VSTOP] = 0; 574 #endif 575 #ifdef VSTART 576 s.c_cc[VSTART] = 0; 577 #endif 578 } else 579 { 580 /* 581 * Restore saved modes. 582 */ 583 s = save_term; 584 } 585 ioctl(tty, TCSETAW, &s); 586 } 587 #else 588 #ifdef TIOCGETP 589 { 590 struct sgttyb s; 591 static struct sgttyb save_term; 592 static int saved_term = 0; 593 594 if (on) 595 { 596 /* 597 * Get terminal modes. 598 */ 599 ioctl(tty, TIOCGETP, &s); 600 601 /* 602 * Save modes and set certain variables dependent on modes. 603 */ 604 if (!saved_term) 605 { 606 save_term = s; 607 saved_term = 1; 608 } 609 #if HAVE_OSPEED 610 ospeed = s.sg_ospeed; 611 #endif 612 erase_char = s.sg_erase; 613 kill_char = s.sg_kill; 614 werase_char = CONTROL('W'); 615 616 /* 617 * Set the modes to the way we want them. 618 */ 619 s.sg_flags |= CBREAK; 620 s.sg_flags &= ~(ECHO); 621 } else 622 { 623 /* 624 * Restore saved modes. 625 */ 626 s = save_term; 627 } 628 ioctl(tty, TIOCSETN, &s); 629 } 630 #else 631 #ifdef _OSK 632 { 633 struct sgbuf s; 634 static struct sgbuf save_term; 635 static int saved_term = 0; 636 637 if (on) 638 { 639 /* 640 * Get terminal modes. 641 */ 642 _gs_opt(tty, &s); 643 644 /* 645 * Save modes and set certain variables dependent on modes. 646 */ 647 if (!saved_term) 648 { 649 save_term = s; 650 saved_term = 1; 651 } 652 erase_char = s.sg_bspch; 653 kill_char = s.sg_dlnch; 654 werase_char = CONTROL('W'); 655 656 /* 657 * Set the modes to the way we want them. 658 */ 659 s.sg_echo = 0; 660 s.sg_eofch = 0; 661 s.sg_pause = 0; 662 s.sg_psch = 0; 663 } else 664 { 665 /* 666 * Restore saved modes. 667 */ 668 s = save_term; 669 } 670 _ss_opt(tty, &s); 671 } 672 #else 673 /* MS-DOS, Windows, or OS2 */ 674 #if OS2 675 /* OS2 */ 676 LSIGNAL(SIGINT, SIG_IGN); 677 #endif 678 erase_char = '\b'; 679 #if MSDOS_COMPILER==DJGPPC 680 kill_char = CONTROL('U'); 681 /* 682 * So that when we shell out or run another program, its 683 * stdin is in cooked mode. We do not switch stdin to binary 684 * mode if fd0 is zero, since that means we were called before 685 * tty was reopened in open_getchr, in which case we would be 686 * changing the original stdin device outside less. 687 */ 688 if (fd0 != 0) 689 setmode(0, on ? O_BINARY : O_TEXT); 690 #else 691 kill_char = ESC; 692 #endif 693 werase_char = CONTROL('W'); 694 #endif 695 #endif 696 #endif 697 #endif 698 curr_on = on; 699 } 700 701 #if !MSDOS_COMPILER 702 /* 703 * Some glue to prevent calling termcap functions if tgetent() failed. 704 */ 705 static int hardcopy; 706 707 static constant char * ltget_env(constant char *capname) 708 { 709 char name[64]; 710 711 if (termcap_debug) 712 { 713 struct env { struct env *next; char *name; char *value; }; 714 static struct env *envs = NULL; 715 struct env *p; 716 for (p = envs; p != NULL; p = p->next) 717 if (strcmp(p->name, capname) == 0) 718 return p->value; 719 p = (struct env *) ecalloc(1, sizeof(struct env)); 720 p->name = save(capname); 721 p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char)); 722 sprintf(p->value, "<%s>", capname); 723 p->next = envs; 724 envs = p; 725 return p->value; 726 } 727 SNPRINTF1(name, sizeof(name), "LESS_TERMCAP_%s", capname); 728 return (lgetenv(name)); 729 } 730 731 static int ltgetflag(constant char *capname) 732 { 733 constant char *s; 734 735 if ((s = ltget_env(capname)) != NULL) 736 return (*s != '\0' && *s != '0'); 737 if (hardcopy) 738 return (0); 739 return (tgetflag(capname)); 740 } 741 742 static int ltgetnum(constant char *capname) 743 { 744 constant char *s; 745 746 if ((s = ltget_env(capname)) != NULL) 747 return (atoi(s)); 748 if (hardcopy) 749 return (-1); 750 return (tgetnum(capname)); 751 } 752 753 static constant char * ltgetstr(constant char *capname, char **pp) 754 { 755 constant char *s; 756 757 if ((s = ltget_env(capname)) != NULL) 758 return (s); 759 if (hardcopy) 760 return (NULL); 761 return (tgetstr(capname, pp)); 762 } 763 #endif /* MSDOS_COMPILER */ 764 765 /* 766 * Get size of the output screen. 767 */ 768 static void scrsize(void) 769 { 770 constant char *s; 771 int sys_height; 772 int sys_width; 773 #if !MSDOS_COMPILER 774 int n; 775 #endif 776 777 #define DEF_SC_WIDTH 80 778 #if MSDOS_COMPILER 779 #define DEF_SC_HEIGHT 25 780 #else 781 #define DEF_SC_HEIGHT 24 782 #endif 783 784 785 sys_width = sys_height = 0; 786 787 #if LESSTEST 788 if (0) /* can't use is_lesstest(): ttyin_name may not be set by scan_option yet */ 789 #endif /*LESSTEST*/ 790 { 791 #if MSDOS_COMPILER==MSOFTC 792 { 793 struct videoconfig w; 794 _getvideoconfig(&w); 795 sys_height = w.numtextrows; 796 sys_width = w.numtextcols; 797 } 798 #else 799 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 800 { 801 struct text_info w; 802 gettextinfo(&w); 803 sys_height = w.screenheight; 804 sys_width = w.screenwidth; 805 } 806 #else 807 #if MSDOS_COMPILER==WIN32C 808 { 809 CONSOLE_SCREEN_BUFFER_INFO scr; 810 GetConsoleScreenBufferInfo(con_out, &scr); 811 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1; 812 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1; 813 } 814 #else 815 #if OS2 816 { 817 int s[2]; 818 _scrsize(s); 819 sys_width = s[0]; 820 sys_height = s[1]; 821 /* 822 * When using terminal emulators for XFree86/OS2, the 823 * _scrsize function does not work well. 824 * Call the scrsize.exe program to get the window size. 825 */ 826 windowid = getenv("WINDOWID"); 827 if (windowid != NULL) 828 { 829 FILE *fd = popen("scrsize", "rt"); 830 if (fd != NULL) 831 { 832 int w, h; 833 fscanf(fd, "%i %i", &w, &h); 834 if (w > 0 && h > 0) 835 { 836 sys_width = w; 837 sys_height = h; 838 } 839 pclose(fd); 840 } 841 } 842 } 843 #else 844 #ifdef TIOCGWINSZ 845 { 846 struct winsize w; 847 if (ioctl(2, TIOCGWINSZ, &w) == 0) 848 { 849 if (w.ws_row > 0) 850 sys_height = w.ws_row; 851 if (w.ws_col > 0) 852 sys_width = w.ws_col; 853 } 854 } 855 #else 856 #ifdef WIOCGETD 857 { 858 struct uwdata w; 859 if (ioctl(2, WIOCGETD, &w) == 0) 860 { 861 if (w.uw_height > 0) 862 sys_height = w.uw_height / w.uw_vs; 863 if (w.uw_width > 0) 864 sys_width = w.uw_width / w.uw_hs; 865 } 866 } 867 #endif 868 #endif 869 #endif 870 #endif 871 #endif 872 #endif 873 } 874 875 if (sys_height > 0) 876 sc_height = sys_height; 877 else if ((s = lgetenv("LINES")) != NULL) 878 sc_height = atoi(s); 879 #if !MSDOS_COMPILER 880 else if ((n = ltgetnum("li")) > 0) 881 sc_height = n; 882 #endif 883 if ((s = lgetenv("LESS_LINES")) != NULL) 884 { 885 int height = atoi(s); 886 sc_height = (height < 0) ? sc_height + height : height; 887 full_screen = FALSE; 888 } 889 if (sc_height <= 0) 890 sc_height = DEF_SC_HEIGHT; 891 892 if (sys_width > 0) 893 sc_width = sys_width; 894 else if ((s = lgetenv("COLUMNS")) != NULL) 895 sc_width = atoi(s); 896 #if !MSDOS_COMPILER 897 else if ((n = ltgetnum("co")) > 0) 898 sc_width = n; 899 #endif 900 if ((s = lgetenv("LESS_COLUMNS")) != NULL) 901 { 902 int width = atoi(s); 903 sc_width = (width < 0) ? sc_width + width : width; 904 } 905 if (sc_width <= 0) 906 sc_width = DEF_SC_WIDTH; 907 screen_size_changed(); 908 } 909 910 /* 911 * Recalculate things that depend on the screen size. 912 */ 913 public void screen_size_changed(void) 914 { 915 calc_jump_sline(); 916 calc_shift_count(); 917 calc_match_shift(); 918 } 919 920 #if MSDOS_COMPILER==MSOFTC 921 /* 922 * Figure out how many empty loops it takes to delay a millisecond. 923 */ 924 static void get_clock(void) 925 { 926 clock_t start; 927 928 /* 929 * Get synchronized at the start of a tick. 930 */ 931 start = clock(); 932 while (clock() == start) 933 ; 934 /* 935 * Now count loops till the next tick. 936 */ 937 start = clock(); 938 msec_loops = 0; 939 while (clock() == start) 940 msec_loops++; 941 /* 942 * Convert from (loops per clock) to (loops per millisecond). 943 */ 944 msec_loops *= CLOCKS_PER_SEC; 945 msec_loops /= 1000; 946 } 947 948 /* 949 * Delay for a specified number of milliseconds. 950 */ 951 static void delay(int msec) 952 { 953 long i; 954 955 while (msec-- > 0) 956 { 957 for (i = 0; i < msec_loops; i++) 958 (void) clock(); 959 } 960 } 961 #endif 962 963 /* 964 * Return the characters actually input by a "special" key. 965 */ 966 public constant char * special_key_str(int key) 967 { 968 static char tbuf[40]; 969 constant char *s; 970 #if MSDOS_COMPILER || OS2 971 static char k_right[] = { '\340', PCK_RIGHT, 0 }; 972 static char k_left[] = { '\340', PCK_LEFT, 0 }; 973 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 }; 974 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 }; 975 static char k_insert[] = { '\340', PCK_INSERT, 0 }; 976 static char k_delete[] = { '\340', PCK_DELETE, 0 }; 977 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 }; 978 static char k_ctl_backspace[] = { '\177', 0 }; 979 static char k_backspace[] = { '\b', 0 }; 980 static char k_home[] = { '\340', PCK_HOME, 0 }; 981 static char k_end[] = { '\340', PCK_END, 0 }; 982 static char k_up[] = { '\340', PCK_UP, 0 }; 983 static char k_down[] = { '\340', PCK_DOWN, 0 }; 984 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 }; 985 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 }; 986 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 }; 987 static char k_f1[] = { '\340', PCK_F1, 0 }; 988 #endif 989 #if !MSDOS_COMPILER 990 char *sp = tbuf; 991 #endif 992 993 switch (key) 994 { 995 #if OS2 996 /* 997 * If windowid is not NULL, assume less is executed in 998 * the XFree86 environment. 999 */ 1000 case SK_RIGHT_ARROW: 1001 s = windowid ? ltgetstr("kr", &sp) : k_right; 1002 break; 1003 case SK_LEFT_ARROW: 1004 s = windowid ? ltgetstr("kl", &sp) : k_left; 1005 break; 1006 case SK_UP_ARROW: 1007 s = windowid ? ltgetstr("ku", &sp) : k_up; 1008 break; 1009 case SK_DOWN_ARROW: 1010 s = windowid ? ltgetstr("kd", &sp) : k_down; 1011 break; 1012 case SK_PAGE_UP: 1013 s = windowid ? ltgetstr("kP", &sp) : k_pageup; 1014 break; 1015 case SK_PAGE_DOWN: 1016 s = windowid ? ltgetstr("kN", &sp) : k_pagedown; 1017 break; 1018 case SK_HOME: 1019 s = windowid ? ltgetstr("kh", &sp) : k_home; 1020 break; 1021 case SK_END: 1022 s = windowid ? ltgetstr("@7", &sp) : k_end; 1023 break; 1024 case SK_DELETE: 1025 s = windowid ? ltgetstr("kD", &sp) : k_delete; 1026 if (s == NULL) 1027 { 1028 tbuf[0] = '\177'; 1029 tbuf[1] = '\0'; 1030 s = tbuf; 1031 } 1032 break; 1033 #endif 1034 #if MSDOS_COMPILER 1035 case SK_RIGHT_ARROW: 1036 s = k_right; 1037 break; 1038 case SK_LEFT_ARROW: 1039 s = k_left; 1040 break; 1041 case SK_UP_ARROW: 1042 s = k_up; 1043 break; 1044 case SK_DOWN_ARROW: 1045 s = k_down; 1046 break; 1047 case SK_PAGE_UP: 1048 s = k_pageup; 1049 break; 1050 case SK_PAGE_DOWN: 1051 s = k_pagedown; 1052 break; 1053 case SK_HOME: 1054 s = k_home; 1055 break; 1056 case SK_END: 1057 s = k_end; 1058 break; 1059 case SK_DELETE: 1060 s = k_delete; 1061 break; 1062 #endif 1063 #if MSDOS_COMPILER || OS2 1064 case SK_INSERT: 1065 s = k_insert; 1066 break; 1067 case SK_CTL_LEFT_ARROW: 1068 s = k_ctl_left; 1069 break; 1070 case SK_CTL_RIGHT_ARROW: 1071 s = k_ctl_right; 1072 break; 1073 case SK_CTL_BACKSPACE: 1074 s = k_ctl_backspace; 1075 break; 1076 case SK_CTL_DELETE: 1077 s = k_ctl_delete; 1078 break; 1079 case SK_BACKSPACE: 1080 s = k_backspace; 1081 break; 1082 case SK_F1: 1083 s = k_f1; 1084 break; 1085 case SK_BACKTAB: 1086 s = k_backtab; 1087 break; 1088 #else 1089 case SK_RIGHT_ARROW: 1090 s = ltgetstr("kr", &sp); 1091 break; 1092 case SK_LEFT_ARROW: 1093 s = ltgetstr("kl", &sp); 1094 break; 1095 case SK_UP_ARROW: 1096 s = ltgetstr("ku", &sp); 1097 break; 1098 case SK_DOWN_ARROW: 1099 s = ltgetstr("kd", &sp); 1100 break; 1101 case SK_PAGE_UP: 1102 s = ltgetstr("kP", &sp); 1103 break; 1104 case SK_PAGE_DOWN: 1105 s = ltgetstr("kN", &sp); 1106 break; 1107 case SK_HOME: 1108 s = ltgetstr("kh", &sp); 1109 break; 1110 case SK_END: 1111 s = ltgetstr("@7", &sp); 1112 break; 1113 case SK_DELETE: 1114 s = ltgetstr("kD", &sp); 1115 if (s == NULL) 1116 { 1117 tbuf[0] = '\177'; 1118 tbuf[1] = '\0'; 1119 s = tbuf; 1120 } 1121 break; 1122 case SK_BACKSPACE: 1123 s = ltgetstr("kb", &sp); 1124 if (s == NULL) 1125 { 1126 tbuf[0] = '\b'; 1127 tbuf[1] = '\0'; 1128 s = tbuf; 1129 } 1130 break; 1131 #endif 1132 case SK_CONTROL_K: 1133 tbuf[0] = CONTROL('K'); 1134 tbuf[1] = '\0'; 1135 s = tbuf; 1136 break; 1137 default: 1138 return (NULL); 1139 } 1140 return (s); 1141 } 1142 1143 #if MSDOS_COMPILER 1144 public void init_win_colors(void) 1145 { 1146 if (nm_fg_color == CV_ERROR || nm_fg_color == CV_NOCHANGE) nm_fg_color = sy_fg_color; 1147 if (nm_bg_color == CV_ERROR || nm_bg_color == CV_NOCHANGE) nm_bg_color = sy_bg_color; 1148 if (bo_fg_color == CV_NOCHANGE) bo_fg_color = sy_fg_color; else if (bo_fg_color == CV_ERROR) bo_fg_color = sy_fg_color | 8; 1149 if (bo_bg_color == CV_NOCHANGE) bo_bg_color = sy_bg_color; else if (bo_bg_color == CV_ERROR) bo_bg_color = sy_bg_color; 1150 if (ul_fg_color == CV_NOCHANGE) ul_fg_color = sy_fg_color; else if (ul_fg_color == CV_ERROR) ul_fg_color = (sy_bg_color == 3 || sy_bg_color == 11) ? 0 : 11; 1151 if (ul_bg_color == CV_NOCHANGE) ul_bg_color = sy_bg_color; else if (ul_bg_color == CV_ERROR) ul_bg_color = sy_bg_color; 1152 if (so_fg_color == CV_NOCHANGE) so_fg_color = sy_fg_color; else if (so_fg_color == CV_ERROR) so_fg_color = sy_bg_color; 1153 if (so_bg_color == CV_NOCHANGE) so_bg_color = sy_bg_color; else if (so_bg_color == CV_ERROR) so_bg_color = sy_fg_color; 1154 if (bl_fg_color == CV_NOCHANGE) bl_fg_color = sy_fg_color; else if (bl_fg_color == CV_ERROR) bl_fg_color = ul_bg_color; 1155 if (bl_bg_color == CV_NOCHANGE) bl_bg_color = sy_bg_color; else if (bl_bg_color == CV_ERROR) bl_bg_color = ul_fg_color; 1156 nm_fg_color |= nm_attr; 1157 bo_fg_color |= bo_attr; 1158 ul_fg_color |= ul_attr; 1159 so_fg_color |= so_attr; 1160 bl_fg_color |= bl_attr; 1161 } 1162 #endif /* MSDOS_COMPILER */ 1163 1164 /* 1165 * Get terminal capabilities via termcap. 1166 */ 1167 public void get_term(void) 1168 { 1169 termcap_debug = !isnullenv(lgetenv("LESS_TERMCAP_DEBUG")); 1170 #if MSDOS_COMPILER 1171 auto_wrap = 1; 1172 ignaw = 0; 1173 can_goto_line = 1; 1174 clear_bg = 1; 1175 /* 1176 * Set up default colors. 1177 * The xx_s_width and xx_e_width vars are already initialized to 0. 1178 */ 1179 #if MSDOS_COMPILER==MSOFTC 1180 sy_bg_color = _getbkcolor(); 1181 sy_fg_color = _gettextcolor(); 1182 get_clock(); 1183 #else 1184 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 1185 { 1186 struct text_info w; 1187 gettextinfo(&w); 1188 sy_bg_color = (w.attribute >> 4) & 0x0F; 1189 sy_fg_color = (w.attribute >> 0) & 0x0F; 1190 } 1191 #else 1192 #if MSDOS_COMPILER==WIN32C 1193 { 1194 CONSOLE_SCREEN_BUFFER_INFO scr; 1195 1196 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE); 1197 /* 1198 * Always open stdin in binary. Note this *must* be done 1199 * before any file operations have been done on fd0. 1200 */ 1201 SET_BINARY(0); 1202 GetConsoleMode(con_out, &init_console_output_mode); 1203 GetConsoleScreenBufferInfo(con_out, &scr); 1204 curr_attr = scr.wAttributes; 1205 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */ 1206 sy_fg_color = curr_attr & FG_COLORS; 1207 } 1208 #endif 1209 #endif 1210 #endif 1211 init_win_colors(); 1212 1213 /* 1214 * Get size of the screen. 1215 */ 1216 scrsize(); 1217 pos_init(); 1218 1219 #else /* !MSDOS_COMPILER */ 1220 { 1221 char *sp; 1222 constant char *t1; 1223 constant char *t2; 1224 constant char *term; 1225 /* 1226 * Some termcap libraries assume termbuf is static 1227 * (accessible after tgetent returns). 1228 */ 1229 static char termbuf[TERMBUF_SIZE]; 1230 static char sbuf[TERMSBUF_SIZE]; 1231 1232 #if OS2 1233 /* 1234 * Make sure the termcap database is available. 1235 */ 1236 constant char *cp = lgetenv("TERMCAP"); 1237 if (isnullenv(cp)) 1238 { 1239 char *termcap; 1240 if ((sp = homefile("termcap.dat")) != NULL) 1241 { 1242 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char)); 1243 sprintf(termcap, "TERMCAP=%s", sp); 1244 free(sp); 1245 putenv(termcap); 1246 } 1247 } 1248 #endif 1249 /* 1250 * Find out what kind of terminal this is. 1251 */ 1252 if ((term = lgetenv("TERM")) == NULL) 1253 term = DEFAULT_TERM; 1254 hardcopy = 0; 1255 /* {{ Should probably just pass NULL instead of termbuf. }} */ 1256 if (tgetent(termbuf, term) != TGETENT_OK) 1257 hardcopy = 1; 1258 if (ltgetflag("hc")) 1259 hardcopy = 1; 1260 1261 /* 1262 * Get size of the screen. 1263 */ 1264 scrsize(); 1265 pos_init(); 1266 1267 auto_wrap = ltgetflag("am"); 1268 ignaw = ltgetflag("xn"); 1269 above_mem = ltgetflag("da"); 1270 below_mem = ltgetflag("db"); 1271 clear_bg = ltgetflag("ut"); 1272 no_alt_screen = ltgetflag("NR"); 1273 1274 /* 1275 * Assumes termcap variable "sg" is the printing width of: 1276 * the standout sequence, the end standout sequence, 1277 * the underline sequence, the end underline sequence, 1278 * the boldface sequence, and the end boldface sequence. 1279 */ 1280 if ((so_s_width = ltgetnum("sg")) < 0) 1281 so_s_width = 0; 1282 so_e_width = so_s_width; 1283 1284 bo_s_width = bo_e_width = so_s_width; 1285 ul_s_width = ul_e_width = so_s_width; 1286 bl_s_width = bl_e_width = so_s_width; 1287 1288 #if HILITE_SEARCH 1289 if (so_s_width > 0 || so_e_width > 0) 1290 /* 1291 * Disable highlighting by default on magic cookie terminals. 1292 * Turning on highlighting might change the displayed width 1293 * of a line, causing the display to get messed up. 1294 * The user can turn it back on with -g, 1295 * but she won't like the results. 1296 */ 1297 hilite_search = 0; 1298 #endif 1299 1300 /* 1301 * Get various string-valued capabilities. 1302 */ 1303 sp = sbuf; 1304 1305 #if HAVE_OSPEED 1306 sc_pad = ltgetstr("pc", &sp); 1307 if (sc_pad != NULL) 1308 PC = *sc_pad; 1309 #endif 1310 1311 sc_s_keypad = ltgetstr("ks", &sp); 1312 if (sc_s_keypad == NULL) 1313 sc_s_keypad = ""; 1314 sc_e_keypad = ltgetstr("ke", &sp); 1315 if (sc_e_keypad == NULL) 1316 sc_e_keypad = ""; 1317 kent = ltgetstr("@8", &sp); 1318 1319 sc_s_mousecap = ltgetstr("MOUSE_START", &sp); 1320 if (sc_s_mousecap == NULL) 1321 sc_s_mousecap = ESCS "[?1000h" ESCS "[?1002h" ESCS "[?1006h"; 1322 sc_e_mousecap = ltgetstr("MOUSE_END", &sp); 1323 if (sc_e_mousecap == NULL) 1324 sc_e_mousecap = ESCS "[?1006l" ESCS "[?1002l" ESCS "[?1000l"; 1325 1326 sc_s_bracketed_paste = ltgetstr("BRACKETED_PASTE_START", &sp); 1327 if (sc_s_bracketed_paste == NULL) 1328 sc_s_bracketed_paste = ESCS"[?2004h"; 1329 sc_e_bracketed_paste = ltgetstr("BRACKETED_PASTE_END", &sp); 1330 if (sc_e_bracketed_paste == NULL) 1331 sc_e_bracketed_paste = ESCS"[?2004l"; 1332 1333 sc_init = ltgetstr("ti", &sp); 1334 if (sc_init == NULL) 1335 sc_init = ""; 1336 1337 sc_deinit= ltgetstr("te", &sp); 1338 if (sc_deinit == NULL) 1339 sc_deinit = ""; 1340 1341 sc_eol_clear = ltgetstr("ce", &sp); 1342 if (sc_eol_clear == NULL || *sc_eol_clear == '\0') 1343 { 1344 missing_cap = TRUE; 1345 sc_eol_clear = ""; 1346 } 1347 1348 sc_eos_clear = ltgetstr("cd", &sp); 1349 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0')) 1350 { 1351 missing_cap = TRUE; 1352 sc_eos_clear = ""; 1353 } 1354 1355 sc_clear = ltgetstr("cl", &sp); 1356 if (sc_clear == NULL || *sc_clear == '\0') 1357 { 1358 missing_cap = TRUE; 1359 sc_clear = "\n\n"; 1360 } 1361 1362 sc_move = ltgetstr("cm", &sp); 1363 if (sc_move == NULL || *sc_move == '\0') 1364 { 1365 /* 1366 * This is not an error here, because we don't 1367 * always need sc_move. 1368 * We need it only if we don't have home or lower-left. 1369 */ 1370 sc_move = ""; 1371 can_goto_line = 0; 1372 } else 1373 can_goto_line = 1; 1374 1375 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp); 1376 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp); 1377 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp); 1378 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp); 1379 1380 sc_visual_bell = ltgetstr("vb", &sp); 1381 if (sc_visual_bell == NULL) 1382 sc_visual_bell = ""; 1383 1384 if (ltgetflag("bs")) 1385 sc_backspace = "\b"; 1386 else 1387 { 1388 sc_backspace = ltgetstr("bc", &sp); 1389 if (sc_backspace == NULL || *sc_backspace == '\0') 1390 sc_backspace = "\b"; 1391 } 1392 1393 /* 1394 * Choose between using "ho" and "cm" ("home" and "cursor move") 1395 * to move the cursor to the upper left corner of the screen. 1396 */ 1397 t1 = ltgetstr("ho", &sp); 1398 if (t1 == NULL) 1399 t1 = ""; 1400 if (*sc_move == '\0') 1401 t2 = ""; 1402 else 1403 { 1404 strcpy(sp, tgoto(sc_move, 0, 0)); 1405 t2 = sp; 1406 sp += strlen(sp) + 1; 1407 } 1408 sc_home = cheaper(t1, t2, "|\b^"); 1409 1410 /* 1411 * Choose between using "ll" and "cm" ("lower left" and "cursor move") 1412 * to move the cursor to the lower left corner of the screen. 1413 */ 1414 t1 = ltgetstr("ll", &sp); 1415 if (t1 == NULL || !full_screen) 1416 t1 = ""; 1417 if (*sc_move == '\0') 1418 t2 = ""; 1419 else 1420 { 1421 strcpy(sp, tgoto(sc_move, 0, sc_height-1)); 1422 t2 = sp; 1423 sp += strlen(sp) + 1; 1424 } 1425 sc_lower_left = cheaper(t1, t2, "\r"); 1426 1427 /* 1428 * Get carriage return string. 1429 */ 1430 sc_return = ltgetstr("cr", &sp); 1431 if (sc_return == NULL) 1432 sc_return = "\r"; 1433 1434 /* 1435 * Choose between using "al" or "sr" ("add line" or "scroll reverse") 1436 * to add a line at the top of the screen. 1437 */ 1438 t1 = ltgetstr("al", &sp); 1439 if (t1 == NULL) 1440 t1 = ""; 1441 t2 = ltgetstr("sr", &sp); 1442 if (t2 == NULL) 1443 t2 = ""; 1444 if (*t1 == '\0' && *t2 == '\0') 1445 sc_addline = ""; 1446 else if (above_mem) 1447 sc_addline = t1; 1448 else 1449 sc_addline = cheaper(t1, t2, ""); 1450 if (*sc_addline == '\0') 1451 { 1452 /* 1453 * Force repaint on any backward movement. 1454 */ 1455 no_back_scroll = 1; 1456 } 1457 } 1458 #endif /* MSDOS_COMPILER */ 1459 { 1460 const char *env = lgetenv("LESS_SHELL_LINES"); 1461 shell_lines = isnullenv(env) ? 1 : atoi(env); 1462 if (shell_lines >= sc_height) 1463 shell_lines = sc_height - 1; 1464 } 1465 } 1466 1467 #if !MSDOS_COMPILER 1468 /* 1469 * Return the cost of displaying a termcap string. 1470 * We use the trick of calling tputs, but as a char printing function 1471 * we give it inc_costcount, which just increments "costcount". 1472 * This tells us how many chars would be printed by using this string. 1473 * {{ Couldn't we just use strlen? }} 1474 */ 1475 static int costcount; 1476 1477 /*ARGSUSED*/ 1478 static int inc_costcount(int c) 1479 { 1480 costcount++; 1481 return (c); 1482 } 1483 1484 static int cost(constant char *t) 1485 { 1486 costcount = 0; 1487 tputs(t, sc_height, inc_costcount); 1488 return (costcount); 1489 } 1490 1491 /* 1492 * Return the "best" of the two given termcap strings. 1493 * The best, if both exist, is the one with the lower 1494 * cost (see cost() function). 1495 */ 1496 static constant char * cheaper(constant char *t1, constant char *t2, constant char *def) 1497 { 1498 if (*t1 == '\0' && *t2 == '\0') 1499 { 1500 missing_cap = TRUE; 1501 return (def); 1502 } 1503 if (*t1 == '\0') 1504 return (t2); 1505 if (*t2 == '\0') 1506 return (t1); 1507 if (cost(t1) < cost(t2)) 1508 return (t1); 1509 return (t2); 1510 } 1511 1512 static void tmodes(constant char *incap, constant char *outcap, constant char **instr, constant char **outstr, constant char *def_instr, constant char *def_outstr, char **spp) 1513 { 1514 *instr = ltgetstr(incap, spp); 1515 if (*instr == NULL) 1516 { 1517 /* Use defaults. */ 1518 *instr = def_instr; 1519 *outstr = def_outstr; 1520 return; 1521 } 1522 1523 *outstr = ltgetstr(outcap, spp); 1524 if (*outstr == NULL) 1525 /* No specific out capability; use "me". */ 1526 *outstr = ltgetstr("me", spp); 1527 if (*outstr == NULL) 1528 /* Don't even have "me"; use a null string. */ 1529 *outstr = ""; 1530 } 1531 1532 #endif /* MSDOS_COMPILER */ 1533 1534 1535 /* 1536 * Below are the functions which perform all the 1537 * terminal-specific screen manipulation. 1538 */ 1539 1540 1541 #if MSDOS_COMPILER 1542 1543 #if MSDOS_COMPILER==WIN32C 1544 static void _settextposition(int row, int col) 1545 { 1546 COORD cpos; 1547 CONSOLE_SCREEN_BUFFER_INFO csbi; 1548 1549 GetConsoleScreenBufferInfo(con_out, &csbi); 1550 cpos.X = csbi.srWindow.Left + (col - 1); 1551 cpos.Y = csbi.srWindow.Top + (row - 1); 1552 SetConsoleCursorPosition(con_out, cpos); 1553 } 1554 #endif 1555 1556 /* 1557 * Initialize the screen to the correct color at startup. 1558 */ 1559 static void initcolor(void) 1560 { 1561 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 1562 intensevideo(); 1563 #endif 1564 SETCOLORS(nm_fg_color, nm_bg_color); 1565 #if 0 1566 /* 1567 * This clears the screen at startup. This is different from 1568 * the behavior of other versions of less. Disable it for now. 1569 */ 1570 char *blanks; 1571 int row; 1572 int col; 1573 1574 /* 1575 * Create a complete, blank screen using "normal" colors. 1576 */ 1577 SETCOLORS(nm_fg_color, nm_bg_color); 1578 blanks = (char *) ecalloc(width+1, sizeof(char)); 1579 for (col = 0; col < sc_width; col++) 1580 blanks[col] = ' '; 1581 blanks[sc_width] = '\0'; 1582 for (row = 0; row < sc_height; row++) 1583 _outtext(blanks); 1584 free(blanks); 1585 #endif 1586 } 1587 #endif 1588 1589 #if MSDOS_COMPILER==WIN32C 1590 1591 /* 1592 * Enable virtual terminal processing, if available. 1593 */ 1594 static void win32_init_vt_term(void) 1595 { 1596 if (vt_enabled == 0 || (vt_enabled == 1 && con_out == con_out_ours)) 1597 return; // already initialized 1598 1599 /* don't care about the initial mode, and win VT hard-enables am+xn */ 1600 vt_enabled = SetConsoleMode(con_out, ENABLE_PROCESSED_OUTPUT | 1601 ENABLE_VIRTUAL_TERMINAL_PROCESSING | 1602 ENABLE_WRAP_AT_EOL_OUTPUT); 1603 if (vt_enabled) 1604 { 1605 auto_wrap = 1; 1606 ignaw = 1; 1607 } 1608 } 1609 1610 static void win32_deinit_vt_term(void) 1611 { 1612 if (vt_enabled == 1 && con_out == con_out_save) 1613 SetConsoleMode(con_out, init_console_output_mode); 1614 } 1615 1616 /* 1617 * Termcap-like init with a private win32 console. 1618 */ 1619 static void win32_init_term(void) 1620 { 1621 CONSOLE_SCREEN_BUFFER_INFO scr; 1622 COORD size; 1623 1624 if (con_out_save == INVALID_HANDLE_VALUE) 1625 return; 1626 1627 GetConsoleScreenBufferInfo(con_out_save, &scr); 1628 1629 if (con_out_ours == INVALID_HANDLE_VALUE) 1630 { 1631 /* 1632 * Create our own screen buffer, so that we 1633 * may restore the original when done. 1634 */ 1635 con_out_ours = CreateConsoleScreenBuffer( 1636 GENERIC_WRITE | GENERIC_READ, 1637 FILE_SHARE_WRITE | FILE_SHARE_READ, 1638 (LPSECURITY_ATTRIBUTES) NULL, 1639 CONSOLE_TEXTMODE_BUFFER, 1640 (LPVOID) NULL); 1641 1642 // we don't care about the initial state. we need processed 1643 // output without anything else (no wrap at EOL, no VT, 1644 // no disabled auto-return). 1645 if (SetConsoleMode(con_out_ours, ENABLE_PROCESSED_OUTPUT)) 1646 auto_wrap = 0; 1647 } 1648 1649 size.X = scr.srWindow.Right - scr.srWindow.Left + 1; 1650 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1; 1651 SetConsoleScreenBufferSize(con_out_ours, size); 1652 SetConsoleActiveScreenBuffer(con_out_ours); 1653 con_out = con_out_ours; 1654 } 1655 1656 /* 1657 * Restore the startup console. 1658 */ 1659 static void win32_deinit_term(void) 1660 { 1661 if (con_out_save == INVALID_HANDLE_VALUE) 1662 return; 1663 if (quitting) 1664 (void) CloseHandle(con_out_ours); 1665 SetConsoleActiveScreenBuffer(con_out_save); 1666 con_out = con_out_save; 1667 } 1668 1669 #endif 1670 1671 #if !MSDOS_COMPILER 1672 static void do_tputs(constant char *str, int affcnt, int (*f_putc)(int)) 1673 { 1674 #if LESSTEST 1675 if (is_lesstest() && f_putc == putchr) 1676 putstr(str); 1677 else 1678 #endif /*LESSTEST*/ 1679 tputs(str, affcnt, f_putc); 1680 } 1681 1682 /* 1683 * Like tputs but we handle $<...> delay strings here because 1684 * some implementations of tputs don't perform delays correctly. 1685 */ 1686 static void ltputs(constant char *str, int affcnt, int (*f_putc)(int)) 1687 { 1688 while (str != NULL && *str != '\0') 1689 { 1690 #if HAVE_STRSTR 1691 constant char *obrac = strstr(str, "$<"); 1692 if (obrac != NULL) 1693 { 1694 char str2[64]; 1695 size_t slen = ptr_diff(obrac, str); 1696 if (slen < sizeof(str2)) 1697 { 1698 int delay; 1699 /* Output first part of string (before "$<"). */ 1700 memcpy(str2, str, slen); 1701 str2[slen] = '\0'; 1702 do_tputs(str2, affcnt, f_putc); 1703 str += slen + 2; 1704 /* Perform the delay. */ 1705 delay = lstrtoic(str, &str, 10); 1706 if (*str == '*') 1707 if (ckd_mul(&delay, delay, affcnt)) 1708 delay = INT_MAX; 1709 flush(); 1710 sleep_ms(delay); 1711 /* Skip past closing ">" at end of delay string. */ 1712 str = strstr(str, ">"); 1713 if (str != NULL) 1714 str++; 1715 continue; 1716 } 1717 } 1718 #endif 1719 /* Pass the rest of the string to tputs and we're done. */ 1720 do_tputs(str, affcnt, f_putc); 1721 break; 1722 } 1723 } 1724 #endif /* MSDOS_COMPILER */ 1725 1726 /* 1727 * Configure the terminal so mouse clicks and wheel moves 1728 * produce input to less. 1729 */ 1730 public void init_mouse(void) 1731 { 1732 #if !MSDOS_COMPILER 1733 ltputs(sc_s_mousecap, sc_height, putchr); 1734 #else 1735 #if MSDOS_COMPILER==WIN32C 1736 curr_console_input_mode = mouse_console_input_mode; 1737 SetConsoleMode(tty, curr_console_input_mode); 1738 #endif 1739 #endif 1740 } 1741 1742 /* 1743 * Configure the terminal so mouse clicks and wheel moves 1744 * are handled by the system (so text can be selected, etc). 1745 */ 1746 public void deinit_mouse(void) 1747 { 1748 #if !MSDOS_COMPILER 1749 ltputs(sc_e_mousecap, sc_height, putchr); 1750 #else 1751 #if MSDOS_COMPILER==WIN32C 1752 curr_console_input_mode = base_console_input_mode; 1753 SetConsoleMode(tty, curr_console_input_mode); 1754 #endif 1755 #endif 1756 } 1757 1758 /* 1759 * Initialize terminal 1760 */ 1761 public void init(void) 1762 { 1763 clear_bot_if_needed(); 1764 #if !MSDOS_COMPILER 1765 if (!(quit_if_one_screen && one_screen)) 1766 { 1767 if (!no_init) 1768 { 1769 ltputs(sc_init, sc_height, putchr); 1770 /* 1771 * Some terminals leave the cursor unmoved when switching 1772 * to the alt screen. To avoid having the text appear at 1773 * a seemingly random line on the alt screen, move to 1774 * lower left if we are using an alt screen. 1775 */ 1776 if (*sc_init != '\0' && *sc_deinit != '\0' && !no_alt_screen) 1777 lower_left(); 1778 term_init_done = 1; 1779 } 1780 if (!no_keypad) 1781 ltputs(sc_s_keypad, sc_height, putchr); 1782 if (mousecap) 1783 init_mouse(); 1784 if (no_paste) 1785 init_bracketed_paste(); 1786 } 1787 init_done = 1; 1788 if (top_scroll) 1789 { 1790 int i; 1791 1792 /* 1793 * This is nice to terminals with no alternate screen, 1794 * but with saved scrolled-off-the-top lines. This way, 1795 * no previous line is lost, but we start with a whole 1796 * screen to ourself. 1797 */ 1798 for (i = 1; i < sc_height; i++) 1799 putchr('\n'); 1800 } else 1801 line_left(); 1802 #else 1803 #if MSDOS_COMPILER==WIN32C 1804 if (!(quit_if_one_screen && one_screen)) 1805 { 1806 if (!no_init) 1807 { 1808 win32_init_term(); 1809 term_init_done = 1; 1810 } 1811 if (mousecap) 1812 init_mouse(); 1813 1814 } 1815 win32_init_vt_term(); 1816 #endif 1817 init_done = 1; 1818 initcolor(); 1819 flush(); 1820 #endif 1821 } 1822 1823 /* 1824 * Deinitialize terminal 1825 */ 1826 public void deinit(void) 1827 { 1828 if (!init_done) 1829 return; 1830 #if !MSDOS_COMPILER 1831 if (!(quit_if_one_screen && one_screen)) 1832 { 1833 if (mousecap) 1834 deinit_mouse(); 1835 if (no_paste) 1836 deinit_bracketed_paste(); 1837 if (!no_keypad) 1838 ltputs(sc_e_keypad, sc_height, putchr); 1839 if (!no_init) 1840 ltputs(sc_deinit, sc_height, putchr); 1841 } 1842 #else 1843 /* Restore system colors. */ 1844 SETCOLORS(sy_fg_color, sy_bg_color); 1845 #if MSDOS_COMPILER==WIN32C 1846 win32_deinit_vt_term(); 1847 if (!(quit_if_one_screen && one_screen)) 1848 { 1849 if (mousecap) 1850 deinit_mouse(); 1851 if (!no_init) 1852 win32_deinit_term(); 1853 } 1854 #else 1855 /* Need clreol to make SETCOLORS take effect. */ 1856 clreol(); 1857 #endif 1858 #endif 1859 init_done = 0; 1860 } 1861 1862 /* 1863 * Are we interactive (ie. writing to an initialized tty)? 1864 */ 1865 public int interactive(void) 1866 { 1867 return (is_tty && init_done); 1868 } 1869 1870 static void assert_interactive(void) 1871 { 1872 if (interactive()) return; 1873 /* abort(); */ 1874 } 1875 1876 /* 1877 * Home cursor (move to upper left corner of screen). 1878 */ 1879 public void home(void) 1880 { 1881 assert_interactive(); 1882 #if !MSDOS_COMPILER 1883 ltputs(sc_home, 1, putchr); 1884 #else 1885 flush(); 1886 _settextposition(1,1); 1887 #endif 1888 } 1889 1890 #if LESSTEST 1891 public void dump_screen(void) 1892 { 1893 char dump_cmd[32]; 1894 SNPRINTF1(dump_cmd, sizeof(dump_cmd), ESCS"0;0;%dR", sc_width * sc_height); 1895 ltputs(dump_cmd, sc_height, putchr); 1896 flush(); 1897 } 1898 #endif /*LESSTEST*/ 1899 1900 /* 1901 * Add a blank line (called with cursor at home). 1902 * Should scroll the display down. 1903 */ 1904 public void add_line(void) 1905 { 1906 assert_interactive(); 1907 #if !MSDOS_COMPILER 1908 ltputs(sc_addline, sc_height, putchr); 1909 #else 1910 flush(); 1911 #if MSDOS_COMPILER==MSOFTC 1912 _scrolltextwindow(_GSCROLLDOWN); 1913 _settextposition(1,1); 1914 #else 1915 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 1916 movetext(1,1, sc_width,sc_height-1, 1,2); 1917 gotoxy(1,1); 1918 clreol(); 1919 #else 1920 #if MSDOS_COMPILER==WIN32C 1921 { 1922 CHAR_INFO fillchar; 1923 SMALL_RECT rcSrc, rcClip; 1924 COORD new_org; 1925 CONSOLE_SCREEN_BUFFER_INFO csbi; 1926 1927 GetConsoleScreenBufferInfo(con_out,&csbi); 1928 1929 /* The clip rectangle is the entire visible screen. */ 1930 rcClip.Left = csbi.srWindow.Left; 1931 rcClip.Top = csbi.srWindow.Top; 1932 rcClip.Right = csbi.srWindow.Right; 1933 rcClip.Bottom = csbi.srWindow.Bottom; 1934 1935 /* The source rectangle is the visible screen minus the last line. */ 1936 rcSrc = rcClip; 1937 rcSrc.Bottom--; 1938 1939 /* Move the top left corner of the source window down one row. */ 1940 new_org.X = rcSrc.Left; 1941 new_org.Y = rcSrc.Top + 1; 1942 1943 /* Fill the right character and attributes. */ 1944 fillchar.Char.AsciiChar = ' '; 1945 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 1946 fillchar.Attributes = curr_attr; 1947 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 1948 _settextposition(1,1); 1949 } 1950 #endif 1951 #endif 1952 #endif 1953 #endif 1954 } 1955 1956 #if 0 1957 /* 1958 * Remove the n topmost lines and scroll everything below it in the 1959 * window upward. This is needed to stop leaking the topmost line 1960 * into the scrollback buffer when we go down-one-line (in WIN32). 1961 */ 1962 public void remove_top(int n) 1963 { 1964 #if MSDOS_COMPILER==WIN32C 1965 SMALL_RECT rcSrc, rcClip; 1966 CHAR_INFO fillchar; 1967 COORD new_org; 1968 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ 1969 1970 if (n >= sc_height - 1) 1971 { 1972 clear(); 1973 home(); 1974 return; 1975 } 1976 1977 flush(); 1978 1979 GetConsoleScreenBufferInfo(con_out, &csbi); 1980 1981 /* Get the extent of all-visible-rows-but-the-last. */ 1982 rcSrc.Left = csbi.srWindow.Left; 1983 rcSrc.Top = csbi.srWindow.Top + n; 1984 rcSrc.Right = csbi.srWindow.Right; 1985 rcSrc.Bottom = csbi.srWindow.Bottom; 1986 1987 /* Get the clip rectangle. */ 1988 rcClip.Left = rcSrc.Left; 1989 rcClip.Top = csbi.srWindow.Top; 1990 rcClip.Right = rcSrc.Right; 1991 rcClip.Bottom = rcSrc.Bottom ; 1992 1993 /* Move the source window up n rows. */ 1994 new_org.X = rcSrc.Left; 1995 new_org.Y = rcSrc.Top - n; 1996 1997 /* Fill the right character and attributes. */ 1998 fillchar.Char.AsciiChar = ' '; 1999 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 2000 fillchar.Attributes = curr_attr; 2001 2002 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 2003 2004 /* Position cursor on first blank line. */ 2005 goto_line(sc_height - n - 1); 2006 #endif 2007 } 2008 #endif 2009 2010 #if MSDOS_COMPILER==WIN32C 2011 /* 2012 * Clear the screen. 2013 */ 2014 static void win32_clear(void) 2015 { 2016 /* 2017 * This will clear only the currently visible rows of the NT 2018 * console buffer, which means none of the precious scrollback 2019 * rows are touched making for faster scrolling. Note that, if 2020 * the window has fewer columns than the console buffer (i.e. 2021 * there is a horizontal scrollbar as well), the entire width 2022 * of the visible rows will be cleared. 2023 */ 2024 COORD topleft; 2025 DWORD nchars; 2026 DWORD winsz; 2027 CONSOLE_SCREEN_BUFFER_INFO csbi; 2028 2029 /* get the number of cells in the current buffer */ 2030 GetConsoleScreenBufferInfo(con_out, &csbi); 2031 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1); 2032 topleft.X = 0; 2033 topleft.Y = csbi.srWindow.Top; 2034 2035 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 2036 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars); 2037 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars); 2038 } 2039 2040 /* 2041 * Remove the n topmost lines and scroll everything below it in the 2042 * window upward. 2043 */ 2044 public void win32_scroll_up(int n) 2045 { 2046 SMALL_RECT rcSrc, rcClip; 2047 CHAR_INFO fillchar; 2048 COORD topleft; 2049 COORD new_org; 2050 DWORD nchars; 2051 DWORD size; 2052 CONSOLE_SCREEN_BUFFER_INFO csbi; 2053 2054 if (n <= 0) 2055 return; 2056 2057 if (n >= sc_height - 1) 2058 { 2059 win32_clear(); 2060 _settextposition(1,1); 2061 return; 2062 } 2063 2064 /* Get the extent of what will remain visible after scrolling. */ 2065 GetConsoleScreenBufferInfo(con_out, &csbi); 2066 rcSrc.Left = csbi.srWindow.Left; 2067 rcSrc.Top = csbi.srWindow.Top + n; 2068 rcSrc.Right = csbi.srWindow.Right; 2069 rcSrc.Bottom = csbi.srWindow.Bottom; 2070 2071 /* Get the clip rectangle. */ 2072 rcClip.Left = rcSrc.Left; 2073 rcClip.Top = csbi.srWindow.Top; 2074 rcClip.Right = rcSrc.Right; 2075 rcClip.Bottom = rcSrc.Bottom ; 2076 2077 /* Move the source text to the top of the screen. */ 2078 new_org.X = rcSrc.Left; 2079 new_org.Y = rcClip.Top; 2080 2081 /* Fill the right character and attributes. */ 2082 fillchar.Char.AsciiChar = ' '; 2083 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color); 2084 2085 /* Scroll the window. */ 2086 SetConsoleTextAttribute(con_out, fillchar.Attributes); 2087 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 2088 2089 /* Clear remaining lines at bottom. */ 2090 topleft.X = csbi.dwCursorPosition.X; 2091 topleft.Y = rcSrc.Bottom - n; 2092 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X); 2093 FillConsoleOutputCharacter(con_out, ' ', size, topleft, 2094 &nchars); 2095 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft, 2096 &nchars); 2097 SetConsoleTextAttribute(con_out, curr_attr); 2098 2099 /* Move cursor n lines up from where it was. */ 2100 csbi.dwCursorPosition.Y -= n; 2101 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition); 2102 } 2103 #endif 2104 2105 /* 2106 * Move cursor to lower left corner of screen. 2107 */ 2108 public void lower_left(void) 2109 { 2110 assert_interactive(); 2111 #if !MSDOS_COMPILER 2112 ltputs(sc_lower_left, 1, putchr); 2113 #else 2114 flush(); 2115 _settextposition(sc_height, 1); 2116 #endif 2117 } 2118 2119 /* 2120 * Move cursor to left position of current line. 2121 */ 2122 public void line_left(void) 2123 { 2124 assert_interactive(); 2125 #if !MSDOS_COMPILER 2126 ltputs(sc_return, 1, putchr); 2127 #else 2128 { 2129 int row; 2130 flush(); 2131 #if MSDOS_COMPILER==WIN32C 2132 { 2133 CONSOLE_SCREEN_BUFFER_INFO scr; 2134 GetConsoleScreenBufferInfo(con_out, &scr); 2135 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1; 2136 } 2137 #else 2138 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 2139 row = wherey(); 2140 #else 2141 { 2142 struct rccoord tpos = _gettextposition(); 2143 row = tpos.row; 2144 } 2145 #endif 2146 #endif 2147 _settextposition(row, 1); 2148 } 2149 #endif 2150 } 2151 2152 /* 2153 * Check if the console size has changed and reset internals 2154 * (in lieu of SIGWINCH for WIN32). 2155 */ 2156 public void check_winch(void) 2157 { 2158 #if MSDOS_COMPILER==WIN32C 2159 CONSOLE_SCREEN_BUFFER_INFO scr; 2160 COORD size; 2161 2162 if (con_out == INVALID_HANDLE_VALUE) 2163 return; 2164 2165 flush(); 2166 GetConsoleScreenBufferInfo(con_out, &scr); 2167 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1; 2168 size.X = scr.srWindow.Right - scr.srWindow.Left + 1; 2169 if (size.Y != sc_height || size.X != sc_width) 2170 { 2171 sc_height = size.Y; 2172 sc_width = size.X; 2173 if (!no_init && con_out_ours == con_out) 2174 SetConsoleScreenBufferSize(con_out, size); 2175 pos_init(); 2176 wscroll = (sc_height + 1) / 2; 2177 screen_trashed(); 2178 } 2179 #endif 2180 } 2181 2182 /* 2183 * Goto a specific line on the screen. 2184 */ 2185 public void goto_line(int sindex) 2186 { 2187 assert_interactive(); 2188 #if !MSDOS_COMPILER 2189 ltputs(tgoto(sc_move, 0, sindex), 1, putchr); 2190 #else 2191 flush(); 2192 _settextposition(sindex+1, 1); 2193 #endif 2194 } 2195 2196 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC 2197 /* 2198 * Create an alternate screen which is all white. 2199 * This screen is used to create a "flash" effect, by displaying it 2200 * briefly and then switching back to the normal screen. 2201 * {{ Yuck! There must be a better way to get a visual bell. }} 2202 */ 2203 static void create_flash(void) 2204 { 2205 #if MSDOS_COMPILER==MSOFTC 2206 struct videoconfig w; 2207 char *blanks; 2208 int row, col; 2209 2210 _getvideoconfig(&w); 2211 videopages = w.numvideopages; 2212 if (videopages < 2) 2213 { 2214 at_enter(AT_STANDOUT); 2215 at_exit(); 2216 } else 2217 { 2218 _setactivepage(1); 2219 at_enter(AT_STANDOUT); 2220 blanks = (char *) ecalloc(w.numtextcols, sizeof(char)); 2221 for (col = 0; col < w.numtextcols; col++) 2222 blanks[col] = ' '; 2223 for (row = w.numtextrows; row > 0; row--) 2224 _outmem(blanks, w.numtextcols); 2225 _setactivepage(0); 2226 _setvisualpage(0); 2227 free(blanks); 2228 at_exit(); 2229 } 2230 #else 2231 #if MSDOS_COMPILER==BORLANDC 2232 int n; 2233 2234 whitescreen = (unsigned short *) 2235 malloc(sc_width * sc_height * sizeof(short)); 2236 if (whitescreen == NULL) 2237 return; 2238 for (n = 0; n < sc_width * sc_height; n++) 2239 whitescreen[n] = 0x7020; 2240 #endif 2241 #endif 2242 flash_created = 1; 2243 } 2244 #endif /* MSDOS_COMPILER */ 2245 2246 /* 2247 * Output the "visual bell", if there is one. 2248 */ 2249 public void vbell(void) 2250 { 2251 if (no_vbell) 2252 return; 2253 #if !MSDOS_COMPILER 2254 if (*sc_visual_bell == '\0') 2255 return; 2256 ltputs(sc_visual_bell, sc_height, putchr); 2257 #else 2258 #if MSDOS_COMPILER==DJGPPC 2259 ScreenVisualBell(); 2260 #else 2261 #if MSDOS_COMPILER==MSOFTC 2262 /* 2263 * Create a flash screen on the second video page. 2264 * Switch to that page, then switch back. 2265 */ 2266 if (!flash_created) 2267 create_flash(); 2268 if (videopages < 2) 2269 return; 2270 _setvisualpage(1); 2271 delay(100); 2272 _setvisualpage(0); 2273 #else 2274 #if MSDOS_COMPILER==BORLANDC 2275 unsigned short *currscreen; 2276 2277 /* 2278 * Get a copy of the current screen. 2279 * Display the flash screen. 2280 * Then restore the old screen. 2281 */ 2282 if (!flash_created) 2283 create_flash(); 2284 if (whitescreen == NULL) 2285 return; 2286 currscreen = (unsigned short *) 2287 malloc(sc_width * sc_height * sizeof(short)); 2288 if (currscreen == NULL) return; 2289 gettext(1, 1, sc_width, sc_height, currscreen); 2290 puttext(1, 1, sc_width, sc_height, whitescreen); 2291 delay(100); 2292 puttext(1, 1, sc_width, sc_height, currscreen); 2293 free(currscreen); 2294 #else 2295 #if MSDOS_COMPILER==WIN32C 2296 /* paint screen with an inverse color */ 2297 clear(); 2298 2299 /* leave it displayed for 100 msec. */ 2300 Sleep(100); 2301 2302 /* restore with a redraw */ 2303 repaint(); 2304 #endif 2305 #endif 2306 #endif 2307 #endif 2308 #endif 2309 } 2310 2311 /* 2312 * Make a noise. 2313 */ 2314 static void beep(void) 2315 { 2316 #if !MSDOS_COMPILER 2317 putchr(CONTROL('G')); 2318 #else 2319 #if MSDOS_COMPILER==WIN32C 2320 MessageBeep(0); 2321 #else 2322 write(1, "\7", 1); 2323 #endif 2324 #endif 2325 } 2326 2327 /* 2328 * Ring the terminal bell. 2329 */ 2330 public void bell(void) 2331 { 2332 if (quiet == VERY_QUIET) 2333 vbell(); 2334 else 2335 beep(); 2336 } 2337 2338 /* 2339 * Clear the screen. 2340 */ 2341 public void clear(void) 2342 { 2343 assert_interactive(); 2344 #if !MSDOS_COMPILER 2345 ltputs(sc_clear, sc_height, putchr); 2346 #else 2347 flush(); 2348 #if MSDOS_COMPILER==WIN32C 2349 win32_clear(); 2350 #else 2351 _clearscreen(_GCLEARSCREEN); 2352 #endif 2353 #endif 2354 } 2355 2356 /* 2357 * Clear from the cursor to the end of the cursor's line. 2358 * {{ This must not move the cursor. }} 2359 */ 2360 public void clear_eol(void) 2361 { 2362 /* assert_interactive();*/ 2363 #if !MSDOS_COMPILER 2364 ltputs(sc_eol_clear, 1, putchr); 2365 #else 2366 #if MSDOS_COMPILER==MSOFTC 2367 short top, left; 2368 short bot, right; 2369 struct rccoord tpos; 2370 2371 flush(); 2372 /* 2373 * Save current state. 2374 */ 2375 tpos = _gettextposition(); 2376 _gettextwindow(&top, &left, &bot, &right); 2377 /* 2378 * Set a temporary window to the current line, 2379 * from the cursor's position to the right edge of the screen. 2380 * Then clear that window. 2381 */ 2382 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width); 2383 _clearscreen(_GWINDOW); 2384 /* 2385 * Restore state. 2386 */ 2387 _settextwindow(top, left, bot, right); 2388 _settextposition(tpos.row, tpos.col); 2389 #else 2390 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 2391 flush(); 2392 clreol(); 2393 #else 2394 #if MSDOS_COMPILER==WIN32C 2395 DWORD nchars; 2396 COORD cpos; 2397 CONSOLE_SCREEN_BUFFER_INFO scr; 2398 2399 flush(); 2400 memset(&scr, 0, sizeof(scr)); 2401 GetConsoleScreenBufferInfo(con_out, &scr); 2402 cpos.X = scr.dwCursorPosition.X; 2403 cpos.Y = scr.dwCursorPosition.Y; 2404 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 2405 FillConsoleOutputAttribute(con_out, curr_attr, 2406 scr.dwSize.X - cpos.X, cpos, &nchars); 2407 FillConsoleOutputCharacter(con_out, ' ', 2408 scr.dwSize.X - cpos.X, cpos, &nchars); 2409 #endif 2410 #endif 2411 #endif 2412 #endif 2413 } 2414 2415 /* 2416 * Clear the current line. 2417 * Clear the screen if there's off-screen memory below the display. 2418 */ 2419 static void clear_eol_bot(void) 2420 { 2421 assert_interactive(); 2422 #if MSDOS_COMPILER 2423 clear_eol(); 2424 #else 2425 if (below_mem) 2426 ltputs(sc_eos_clear, 1, putchr); 2427 else 2428 ltputs(sc_eol_clear, 1, putchr); 2429 #endif 2430 } 2431 2432 /* 2433 * Clear the bottom line of the display. 2434 * Leave the cursor at the beginning of the bottom line. 2435 */ 2436 public void clear_bot(void) 2437 { 2438 /* 2439 * If we're in a non-normal attribute mode, temporarily exit 2440 * the mode while we do the clear. Some terminals fill the 2441 * cleared area with the current attribute. 2442 */ 2443 if (oldbot) 2444 lower_left(); 2445 else 2446 line_left(); 2447 2448 if (attrmode == AT_NORMAL) 2449 clear_eol_bot(); 2450 else 2451 { 2452 int saved_attrmode = attrmode; 2453 2454 at_exit(); 2455 clear_eol_bot(); 2456 at_enter(saved_attrmode); 2457 } 2458 } 2459 2460 /* 2461 * Enable or disable bracketed paste mode. 2462 * When enabled, the terminal sends an "open bracket" sequence 2463 * before pasted content and "close bracket" after it. 2464 */ 2465 public void init_bracketed_paste(void) 2466 { 2467 #if !MSDOS_COMPILER 2468 ltputs(sc_s_bracketed_paste, 1, putchr); 2469 #endif 2470 } 2471 2472 public void deinit_bracketed_paste(void) 2473 { 2474 #if !MSDOS_COMPILER 2475 ltputs(sc_e_bracketed_paste, 1, putchr); 2476 #endif 2477 } 2478 2479 /* 2480 * Color string may be "x[y]" where x and y are 4-bit color chars, 2481 * or "N[.M]" where N and M are decimal integers> 2482 * Any of x,y,N,M may also be "-" to mean "unchanged". 2483 */ 2484 2485 /* 2486 * Parse a 4-bit color char. 2487 */ 2488 static int parse_color4(char ch) 2489 { 2490 switch (ch) 2491 { 2492 case 'k': return 0; 2493 case 'r': return CV_RED; 2494 case 'g': return CV_GREEN; 2495 case 'y': return CV_RED|CV_GREEN; 2496 case 'b': return CV_BLUE; 2497 case 'm': return CV_RED|CV_BLUE; 2498 case 'c': return CV_GREEN|CV_BLUE; 2499 case 'w': return CV_RED|CV_GREEN|CV_BLUE; 2500 case 'K': return 0|CV_BRIGHT; 2501 case 'R': return CV_RED|CV_BRIGHT; 2502 case 'G': return CV_GREEN|CV_BRIGHT; 2503 case 'Y': return CV_RED|CV_GREEN|CV_BRIGHT; 2504 case 'B': return CV_BLUE|CV_BRIGHT; 2505 case 'M': return CV_RED|CV_BLUE|CV_BRIGHT; 2506 case 'C': return CV_GREEN|CV_BLUE|CV_BRIGHT; 2507 case 'W': return CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT; 2508 case '-': return CV_NOCHANGE; 2509 default: return CV_ERROR; 2510 } 2511 } 2512 2513 /* 2514 * Parse a color as a decimal integer. 2515 */ 2516 static int parse_color6(constant char **ps) 2517 { 2518 if (**ps == '-') 2519 { 2520 (*ps)++; 2521 return CV_NOCHANGE; 2522 } else 2523 { 2524 constant char *os = *ps; 2525 int color = lstrtoic(os, ps, 10); 2526 if (color < 0 || *ps == os) 2527 return CV_ERROR; 2528 return color; 2529 } 2530 } 2531 2532 /* 2533 * Parse a color pair and return the foreground/background/attribute values. 2534 * Return type of color specifier: 2535 * CV_4BIT: fg/bg values are OR of CV_{RGB} bits. 2536 * CV_6BIT: fg/bg values are integers entered by user. 2537 */ 2538 public COLOR_TYPE parse_color(constant char *str, mutable int *p_fg, mutable int *p_bg, mutable CHAR_ATTR *p_cattr) 2539 { 2540 int fg; 2541 int bg = CV_ERROR; 2542 CHAR_ATTR cattr = CATTR_NULL; 2543 COLOR_TYPE type = CT_NULL; 2544 2545 if (str == NULL || *str == '\0') 2546 return CT_NULL; 2547 if (*str == '+') 2548 str++; /* ignore leading + */ 2549 2550 fg = parse_color4(*str); 2551 if (fg != CV_ERROR) 2552 { 2553 if (str[1] == '\0' || strchr("*~_&dsul", str[1]) != NULL) 2554 { 2555 bg = CV_NOCHANGE; 2556 str++; /* skip the fg char */ 2557 } else 2558 { 2559 bg = parse_color4(str[1]); 2560 if (bg != CV_ERROR) 2561 str += 2; /* skip both fg and bg chars */ 2562 } 2563 } 2564 if (fg != CV_ERROR && bg != CV_ERROR) 2565 type = CT_4BIT; 2566 else 2567 { 2568 fg = (*str == '.') ? CV_NOCHANGE : parse_color6(&str); 2569 if (fg != CV_ERROR) 2570 { 2571 if (*str != '.') 2572 bg = CV_NOCHANGE; 2573 else 2574 { 2575 str++; /* skip the dot */ 2576 bg = parse_color6(&str); 2577 } 2578 } 2579 if (fg != CV_ERROR && bg != CV_ERROR) 2580 type = CT_6BIT; 2581 } 2582 if (type != CT_NULL) 2583 { 2584 for (;; str++) 2585 { 2586 if (*str == '*' || *str == 'd') 2587 cattr |= CATTR_BOLD; 2588 else if (*str == '~' || *str == 's') 2589 cattr |= CATTR_STANDOUT; 2590 else if (*str == '_' || *str == 'u') 2591 cattr |= CATTR_UNDERLINE; 2592 else if (*str == '&' || *str == 'l') /* can't use 'k' because of conflict with "black" */ 2593 cattr |= CATTR_BLINK; 2594 else 2595 break; 2596 } 2597 if (p_fg != NULL) *p_fg = fg; 2598 if (p_bg != NULL) *p_bg = bg; 2599 if (p_cattr != NULL) *p_cattr = cattr; 2600 } 2601 return type; 2602 } 2603 2604 #if !MSDOS_COMPILER 2605 2606 static int sgr_color(int color) 2607 { 2608 switch (color) 2609 { 2610 case 0: return 30; 2611 case CV_RED: return 31; 2612 case CV_GREEN: return 32; 2613 case CV_RED|CV_GREEN: return 33; 2614 case CV_BLUE: return 34; 2615 case CV_RED|CV_BLUE: return 35; 2616 case CV_GREEN|CV_BLUE: return 36; 2617 case CV_RED|CV_GREEN|CV_BLUE: return 37; 2618 2619 case CV_BRIGHT: return 90; 2620 case CV_RED|CV_BRIGHT: return 91; 2621 case CV_GREEN|CV_BRIGHT: return 92; 2622 case CV_RED|CV_GREEN|CV_BRIGHT: return 93; 2623 case CV_BLUE|CV_BRIGHT: return 94; 2624 case CV_RED|CV_BLUE|CV_BRIGHT: return 95; 2625 case CV_GREEN|CV_BLUE|CV_BRIGHT: return 96; 2626 case CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT: return 97; 2627 2628 default: return color; 2629 } 2630 } 2631 2632 static void tput_fmt(constant char *fmt, int color, int (*f_putc)(int)) 2633 { 2634 char buf[INT_STRLEN_BOUND(int)+16]; 2635 if (color == attrcolor) 2636 return; 2637 SNPRINTF1(buf, sizeof(buf), fmt, color); 2638 ltputs(buf, 1, f_putc); 2639 attrcolor = color; 2640 } 2641 2642 static void tput_char_cattr(CHAR_ATTR cattr, int (*f_putc)(int)) 2643 { 2644 if (cattr & CATTR_UNDERLINE) 2645 ltputs(sc_u_in, 1, f_putc); 2646 if (cattr & CATTR_BOLD) 2647 ltputs(sc_b_in, 1, f_putc); 2648 if (cattr & CATTR_BLINK) 2649 ltputs(sc_bl_in, 1, f_putc); 2650 if (cattr & CATTR_STANDOUT) 2651 ltputs(sc_s_in, 1, f_putc); 2652 } 2653 2654 static void tput_color(constant char *str, int (*f_putc)(int)) 2655 { 2656 int fg; 2657 int bg; 2658 CHAR_ATTR cattr; 2659 2660 if (str != NULL && strcmp(str, "*") == 0) 2661 { 2662 /* Special case: reset to normal */ 2663 tput_fmt(ESCS"[m", -1, f_putc); 2664 return; 2665 } 2666 switch (parse_color(str, &fg, &bg, &cattr)) 2667 { 2668 case CT_4BIT: 2669 if (fg >= 0) 2670 tput_fmt(ESCS"[%dm", sgr_color(fg), f_putc); 2671 if (bg >= 0) 2672 tput_fmt(ESCS"[%dm", sgr_color(bg)+10, f_putc); 2673 tput_char_cattr(cattr, f_putc); 2674 break; 2675 case CT_6BIT: 2676 if (fg >= 0) 2677 tput_fmt(ESCS"[38;5;%dm", fg, f_putc); 2678 if (bg >= 0) 2679 tput_fmt(ESCS"[48;5;%dm", bg, f_putc); 2680 tput_char_cattr(cattr, f_putc); 2681 break; 2682 default: 2683 break; 2684 } 2685 } 2686 2687 static void tput_inmode(constant char *mode_str, int attr, int attr_bit, int (*f_putc)(int)) 2688 { 2689 constant char *color_str; 2690 if ((attr & attr_bit) == 0) 2691 return; 2692 color_str = get_color_map(attr_bit); 2693 if (color_str == NULL || *color_str == '\0' || *color_str == '+') 2694 { 2695 ltputs(mode_str, 1, f_putc); 2696 if (color_str == NULL || *color_str++ != '+') 2697 return; 2698 } 2699 /* Color overrides mode string */ 2700 tput_color(color_str, f_putc); 2701 } 2702 2703 static void tput_outmode(constant char *mode_str, int attr_bit, int (*f_putc)(int)) 2704 { 2705 if ((attrmode & attr_bit) == 0) 2706 return; 2707 ltputs(mode_str, 1, f_putc); 2708 } 2709 2710 #else /* MSDOS_COMPILER */ 2711 2712 #if MSDOS_COMPILER==WIN32C 2713 static lbool WIN32put_fmt(constant char *fmt, int color) 2714 { 2715 char buf[INT_STRLEN_BOUND(int)+16]; 2716 int len = (size_t) SNPRINTF1(buf, sizeof(buf), fmt, color); 2717 if (len > 0) 2718 WIN32textout(buf, (size_t) len); 2719 return TRUE; 2720 } 2721 2722 static void win_set_cattr(CHAR_ATTR cattr) 2723 { 2724 if (cattr & CATTR_UNDERLINE) 2725 WIN32textout(ESCS"[4m", 4); 2726 if (cattr & CATTR_BOLD) 2727 WIN32textout(ESCS"[1m", 4); 2728 if (cattr & CATTR_BLINK) 2729 WIN32textout(ESCS"[5m", 4); 2730 if (cattr & CATTR_STANDOUT) 2731 WIN32textout(ESCS"[7m", 4); 2732 } 2733 #endif 2734 2735 static lbool win_set_color(int attr) 2736 { 2737 int fg; 2738 int bg; 2739 CHAR_ATTR cattr; 2740 lbool out = FALSE; 2741 constant char *str = get_color_map(attr); 2742 if (str == NULL || str[0] == '\0') 2743 return FALSE; 2744 switch (parse_color(str, &fg, &bg, &cattr)) 2745 { 2746 case CT_4BIT: 2747 if (fg >= 0 && bg >= 0) 2748 { 2749 SETCOLORS(fg, bg); 2750 out = TRUE; 2751 } else if (fg >= 0) 2752 { 2753 SET_FG_COLOR(fg); 2754 out = TRUE; 2755 } else if (bg >= 0) 2756 { 2757 SET_BG_COLOR(bg); 2758 out = TRUE; 2759 } 2760 #if MSDOS_COMPILER==WIN32C 2761 if (vt_enabled) 2762 win_set_cattr(cattr); 2763 #endif 2764 break; 2765 #if MSDOS_COMPILER==WIN32C 2766 case CT_6BIT: 2767 if (vt_enabled) 2768 { 2769 if (fg > 0) 2770 out = WIN32put_fmt(ESCS"[38;5;%dm", fg); 2771 if (bg > 0) 2772 out = WIN32put_fmt(ESCS"[48;5;%dm", bg); 2773 win_set_cattr(cattr); 2774 } 2775 break; 2776 #endif 2777 default: 2778 break; 2779 } 2780 return out; 2781 } 2782 2783 #endif /* MSDOS_COMPILER */ 2784 2785 public void at_enter(int attr) 2786 { 2787 attr = apply_at_specials(attr); 2788 #if !MSDOS_COMPILER 2789 /* The one with the most priority is last. */ 2790 tput_inmode(sc_u_in, attr, AT_UNDERLINE, putchr); 2791 tput_inmode(sc_b_in, attr, AT_BOLD, putchr); 2792 tput_inmode(sc_bl_in, attr, AT_BLINK, putchr); 2793 /* Don't use standout and color at the same time. */ 2794 if (use_color && (attr & AT_COLOR)) 2795 tput_color(get_color_map(attr), putchr); 2796 else 2797 tput_inmode(sc_s_in, attr, AT_STANDOUT, putchr); 2798 #else 2799 flush(); 2800 /* The one with the most priority is first. */ 2801 if ((attr & AT_COLOR) && use_color) 2802 { 2803 win_set_color(attr); 2804 } else if (attr & AT_STANDOUT) 2805 { 2806 SETCOLORS(so_fg_color, so_bg_color); 2807 } else if (attr & AT_BLINK) 2808 { 2809 SETCOLORS(bl_fg_color, bl_bg_color); 2810 } else if (attr & AT_BOLD) 2811 { 2812 SETCOLORS(bo_fg_color, bo_bg_color); 2813 } else if (attr & AT_UNDERLINE) 2814 { 2815 SETCOLORS(ul_fg_color, ul_bg_color); 2816 } 2817 #endif 2818 attrmode = attr; 2819 } 2820 2821 public void at_exit(void) 2822 { 2823 #if !MSDOS_COMPILER 2824 /* Undo things in the reverse order we did them. */ 2825 tput_color("*", putchr); 2826 tput_outmode(sc_s_out, AT_STANDOUT, putchr); 2827 tput_outmode(sc_bl_out, AT_BLINK, putchr); 2828 tput_outmode(sc_b_out, AT_BOLD, putchr); 2829 tput_outmode(sc_u_out, AT_UNDERLINE, putchr); 2830 #else 2831 flush(); 2832 SETCOLORS(nm_fg_color, nm_bg_color); 2833 #endif 2834 attrmode = AT_NORMAL; 2835 } 2836 2837 public void at_switch(int attr) 2838 { 2839 int new_attrmode = apply_at_specials(attr); 2840 int ignore_modes = AT_ANSI; 2841 2842 if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes)) 2843 { 2844 at_exit(); 2845 at_enter(attr); 2846 } 2847 } 2848 2849 public lbool is_at_equiv(int attr1, int attr2) 2850 { 2851 attr1 = apply_at_specials(attr1); 2852 attr2 = apply_at_specials(attr2); 2853 2854 return (attr1 == attr2); 2855 } 2856 2857 public int apply_at_specials(int attr) 2858 { 2859 if (attr & AT_BINARY) 2860 attr |= binattr; 2861 if (attr & AT_HILITE) 2862 attr |= AT_STANDOUT; 2863 attr &= ~(AT_BINARY|AT_HILITE); 2864 2865 return attr; 2866 } 2867 2868 /* 2869 * Output a plain backspace, without erasing the previous char. 2870 */ 2871 public void putbs(void) 2872 { 2873 if (termcap_debug) 2874 putstr("<bs>"); 2875 else 2876 { 2877 #if !MSDOS_COMPILER 2878 ltputs(sc_backspace, 1, putchr); 2879 #else 2880 int row, col; 2881 2882 flush(); 2883 { 2884 #if MSDOS_COMPILER==MSOFTC 2885 struct rccoord tpos; 2886 tpos = _gettextposition(); 2887 row = tpos.row; 2888 col = tpos.col; 2889 #else 2890 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 2891 row = wherey(); 2892 col = wherex(); 2893 #else 2894 #if MSDOS_COMPILER==WIN32C 2895 CONSOLE_SCREEN_BUFFER_INFO scr; 2896 GetConsoleScreenBufferInfo(con_out, &scr); 2897 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1; 2898 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1; 2899 #endif 2900 #endif 2901 #endif 2902 } 2903 if (col <= 1) 2904 return; 2905 _settextposition(row, col-1); 2906 #endif /* MSDOS_COMPILER */ 2907 } 2908 } 2909 2910 #if MSDOS_COMPILER==WIN32C 2911 2912 #define WIN32_MAX_REPEAT 3 2913 #define LAST_DOWN_COUNT 8 2914 static LWCHAR last_downs[LAST_DOWN_COUNT] = { 0 }; 2915 static int last_down_index = 0; 2916 static LWCHAR hi_surr = 0; 2917 2918 typedef struct XINPUT_RECORD { 2919 INPUT_RECORD ir; 2920 LWCHAR ichar; /* because ir...UnicodeChar is only 16 bits */ 2921 } XINPUT_RECORD; 2922 2923 typedef struct WIN32_CHAR { 2924 struct WIN32_CHAR *wc_next; 2925 char wc_ch; 2926 } WIN32_CHAR; 2927 2928 static WIN32_CHAR *win32_queue = NULL; 2929 2930 /* 2931 * Is the win32_queue nonempty? 2932 */ 2933 static int win32_queued_char(void) 2934 { 2935 return (win32_queue != NULL); 2936 } 2937 2938 /* 2939 * Push a char onto the back of the win32_queue. 2940 */ 2941 static void win32_enqueue(char ch) 2942 { 2943 WIN32_CHAR *wch = (WIN32_CHAR *) ecalloc(1, sizeof(WIN32_CHAR)); 2944 wch->wc_ch = ch; 2945 wch->wc_next = NULL; 2946 if (win32_queue == NULL) 2947 win32_queue = wch; 2948 else 2949 { 2950 WIN32_CHAR *pch; 2951 for (pch = win32_queue; pch->wc_next != NULL; pch = pch->wc_next) 2952 continue; 2953 pch->wc_next = wch; 2954 } 2955 } 2956 2957 /* 2958 * Push a char onto the front of the win32_queue. 2959 * Makes the next call to WIN32getch return ch. 2960 */ 2961 public void WIN32ungetch(int ch) 2962 { 2963 WIN32_CHAR *wch = (WIN32_CHAR *) ecalloc(1, sizeof(WIN32_CHAR)); 2964 wch->wc_ch = ch; 2965 wch->wc_next = win32_queue; 2966 win32_queue = wch; 2967 } 2968 2969 /* 2970 * Get a char from the front of the win32_queue. 2971 */ 2972 static char win32_get_queue(void) 2973 { 2974 WIN32_CHAR *wch = win32_queue; 2975 char ch = wch->wc_ch; 2976 win32_queue = wch->wc_next; 2977 free(wch); 2978 return ch; 2979 } 2980 2981 /* 2982 * Handle a mouse input event. 2983 */ 2984 static lbool win32_mouse_event(XINPUT_RECORD *xip) 2985 { 2986 char b; 2987 2988 if (!mousecap || xip->ir.EventType != MOUSE_EVENT) 2989 return (FALSE); 2990 2991 /* Generate an X11 mouse sequence from the mouse event. */ 2992 /* TODO: switch to the 1006 protocol to allow specific-button-up reports */ 2993 switch (xip->ir.Event.MouseEvent.dwEventFlags) 2994 { 2995 case 0: /* press or release */ 2996 if (xip->ir.Event.MouseEvent.dwButtonState == 0) 2997 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON_REL; 2998 else if (xip->ir.Event.MouseEvent.dwButtonState == 1) /* leftmost */ 2999 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON1; 3000 else if (xip->ir.Event.MouseEvent.dwButtonState == 2) /* rightmost */ 3001 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON3; 3002 else if (xip->ir.Event.MouseEvent.dwButtonState == 4) /* middle ("next-to-leftmost") */ 3003 b = X11MOUSE_OFFSET + X11MOUSE_BUTTON2; 3004 else /* don't bother to figure out what changed */ 3005 return (FALSE); 3006 break; 3007 case MOUSE_WHEELED: 3008 b = X11MOUSE_OFFSET + (((int)xip->ir.Event.MouseEvent.dwButtonState < 0) ? X11MOUSE_WHEEL_DOWN : X11MOUSE_WHEEL_UP); 3009 break; 3010 case MOUSE_MOVED: 3011 if (xip->ir.Event.MouseEvent.dwButtonState != 1) 3012 return (FALSE); 3013 /* Drag with left button down. */ 3014 b = X11MOUSE_OFFSET + X11MOUSE_DRAG; 3015 break; 3016 default: 3017 return (FALSE); 3018 } 3019 /* {{ TODO: change to X11 1006 format. }} */ 3020 win32_enqueue(ESC); 3021 win32_enqueue('['); 3022 win32_enqueue('M'); 3023 win32_enqueue(b); 3024 win32_enqueue(X11MOUSE_OFFSET + xip->ir.Event.MouseEvent.dwMousePosition.X + 1); 3025 win32_enqueue(X11MOUSE_OFFSET + xip->ir.Event.MouseEvent.dwMousePosition.Y + 1); 3026 return (TRUE); 3027 } 3028 3029 static void set_last_down(LWCHAR ch) 3030 { 3031 if (ch == 0) return; 3032 last_downs[last_down_index] = ch; 3033 if (++last_down_index >= LAST_DOWN_COUNT) 3034 last_down_index = 0; 3035 } 3036 3037 static LWCHAR *find_last_down(LWCHAR ch) 3038 { 3039 int i; 3040 for (i = 0; i < LAST_DOWN_COUNT; ++i) 3041 if (last_downs[i] == ch) 3042 return &last_downs[i]; 3043 return NULL; 3044 } 3045 3046 /* 3047 * Get an input char from an INPUT_RECORD and store in xip->ichar. 3048 * Handles surrogate chars, and KeyUp without previous corresponding KeyDown. 3049 */ 3050 static lbool win32_get_ichar(XINPUT_RECORD *xip) 3051 { 3052 LWCHAR ch = xip->ir.Event.KeyEvent.uChar.UnicodeChar; 3053 xip->ichar = ch; 3054 if (!is_ascii_char(ch)) 3055 { 3056 int is_down = xip->ir.Event.KeyEvent.bKeyDown; 3057 LWCHAR *last_down = find_last_down(ch); 3058 if (last_down == NULL) { /* key was up */ 3059 if (is_down) { /* key was up, now is down */ 3060 set_last_down(ch); 3061 } else { /* key up without previous down: pretend this is a down. */ 3062 xip->ir.Event.KeyEvent.bKeyDown = 1; 3063 } 3064 } else if (!is_down) { /* key was down, now is up */ 3065 *last_down = 0; /* use this last_down only once */ 3066 } 3067 3068 if (ch >= 0xD800 && ch < 0xDC00) { /* high surrogate */ 3069 hi_surr = 0x10000 + ((ch - 0xD800) << 10); 3070 return (FALSE); /* get next input, which should be the low surrogate */ 3071 } 3072 if (ch >= 0xDC00 && ch < 0xE000) { /* low surrogate */ 3073 xip->ichar = hi_surr + (ch - 0xDC00); 3074 hi_surr = 0; 3075 } 3076 } 3077 return (TRUE); 3078 } 3079 3080 /* 3081 * Handle a scan code (non-ASCII) key input. 3082 */ 3083 static lbool win32_scan_code(XINPUT_RECORD *xip) 3084 { 3085 int scan = -1; 3086 if (xip->ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) 3087 { 3088 switch (xip->ir.Event.KeyEvent.wVirtualScanCode) 3089 { 3090 case PCK_RIGHT: /* right arrow */ 3091 scan = PCK_CTL_RIGHT; 3092 break; 3093 case PCK_LEFT: /* left arrow */ 3094 scan = PCK_CTL_LEFT; 3095 break; 3096 case PCK_DELETE: /* delete */ 3097 scan = PCK_CTL_DELETE; 3098 break; 3099 } 3100 } else if (xip->ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) 3101 { 3102 if (xip->ichar == '\t') 3103 scan = PCK_SHIFT_TAB; 3104 } 3105 if (scan < 0 && xip->ichar == 0) 3106 scan = xip->ir.Event.KeyEvent.wVirtualScanCode; 3107 if (scan < 0) 3108 return (FALSE); 3109 /* 3110 * An extended key returns a 2 byte sequence consisting of 3111 * a zero byte followed by the scan code. 3112 */ 3113 win32_enqueue('\0'); 3114 win32_enqueue(scan); 3115 return (TRUE); 3116 } 3117 3118 /* 3119 * Handle a key input event. 3120 */ 3121 static lbool win32_key_event(XINPUT_RECORD *xip) 3122 { 3123 int repeat; 3124 char utf8[UTF8_MAX_LENGTH]; 3125 char *up; 3126 3127 if (xip->ir.EventType != KEY_EVENT || 3128 ((xip->ir.Event.KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED)) == (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED) && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) || 3129 (xip->ir.Event.KeyEvent.wVirtualScanCode == 0 && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) || 3130 xip->ir.Event.KeyEvent.wVirtualScanCode == PCK_CAPS_LOCK || 3131 xip->ir.Event.KeyEvent.wVirtualScanCode == PCK_NUM_LOCK || 3132 (xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_MENU && xip->ir.Event.KeyEvent.uChar.UnicodeChar == 0) || 3133 xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_KANJI || 3134 xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT || 3135 xip->ir.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL) 3136 return (FALSE); 3137 3138 if (!win32_get_ichar(xip)) 3139 return (FALSE); 3140 if (!xip->ir.Event.KeyEvent.bKeyDown) 3141 return (FALSE); 3142 3143 if (win32_scan_code(xip)) 3144 return (TRUE); 3145 3146 repeat = xip->ir.Event.KeyEvent.wRepeatCount; 3147 if (repeat > WIN32_MAX_REPEAT) 3148 repeat = WIN32_MAX_REPEAT; 3149 up = utf8; 3150 put_wchar(&up, xip->ichar); 3151 for (; repeat > 0; --repeat) 3152 { 3153 constant char *p; 3154 for (p = utf8; p < up; ++p) 3155 win32_enqueue(*p); 3156 } 3157 return (TRUE); 3158 } 3159 3160 /* 3161 * Determine whether an input character is waiting to be read. 3162 */ 3163 public lbool win32_kbhit2(lbool no_queued) 3164 { 3165 XINPUT_RECORD xip; 3166 3167 if (!no_queued && win32_queued_char()) 3168 return (TRUE); 3169 3170 for (;;) 3171 { 3172 DWORD nread; 3173 DWORD console_input_mode; 3174 /* 3175 * When an input pipe closes, cmd may reset the console mode, 3176 * so set the mode every time we read input. 3177 */ 3178 if (GetConsoleMode(tty, &console_input_mode) && console_input_mode != curr_console_input_mode) 3179 SetConsoleMode(tty, curr_console_input_mode); 3180 PeekConsoleInputW(tty, &xip.ir, 1, &nread); 3181 if (nread == 0) 3182 return (FALSE); 3183 ReadConsoleInputW(tty, &xip.ir, 1, &nread); 3184 if (nread == 0) 3185 return (FALSE); 3186 if (win32_mouse_event(&xip) || win32_key_event(&xip)) 3187 break; 3188 } 3189 return (TRUE); 3190 } 3191 3192 public lbool win32_kbhit(void) 3193 { 3194 return win32_kbhit2(FALSE); 3195 } 3196 3197 /* 3198 * Read a character from the keyboard. 3199 */ 3200 public char WIN32getch(void) 3201 { 3202 while (!win32_kbhit()) 3203 { 3204 Sleep(20); 3205 if (ABORT_SIGS()) 3206 return ('\003'); 3207 } 3208 return (win32_get_queue()); 3209 } 3210 3211 public void win32_getch_clear(void) 3212 { 3213 while (win32_kbhit()) 3214 (void) WIN32getch(); 3215 } 3216 3217 #endif /* MSDOS_COMPILER==WIN32C */ 3218 3219 #if MSDOS_COMPILER 3220 /* 3221 */ 3222 public void WIN32setcolors(int fg, int bg) 3223 { 3224 SETCOLORS(fg, bg); 3225 } 3226 3227 /* 3228 */ 3229 public void WIN32textout(constant char *text, size_t len) 3230 { 3231 #if MSDOS_COMPILER==WIN32C 3232 DWORD written; 3233 if (utf_mode == 2) 3234 { 3235 /* 3236 * We've got UTF-8 text in a non-UTF-8 console. Convert it to 3237 * wide and use WriteConsoleW. 3238 * Biggest input len is OUTBUF_SIZE of obuf from win_flush, 3239 * which is also the biggest output count if it's ASCII. 3240 * "static" wtext is not a state - only avoid 16K on stack. 3241 */ 3242 static WCHAR wtext[OUTBUF_SIZE]; 3243 len = MultiByteToWideChar(CP_UTF8, 0, text, len, wtext, countof(wtext)); 3244 WriteConsoleW(con_out, wtext, len, &written, NULL); 3245 } else 3246 WriteConsole(con_out, text, (DWORD) len, &written, NULL); 3247 #else 3248 char buf[2048]; 3249 if (len >= sizeof(buf)) 3250 len = sizeof(buf) - 1; 3251 memcpy(buf, text, len); 3252 buf[len] = 0; 3253 cputs(buf); 3254 #endif 3255 } 3256 #endif 3257