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