1 /* 2 * Copyright (C) 1984-2015 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 #else 39 40 #if HAVE_SYS_IOCTL_H 41 #include <sys/ioctl.h> 42 #endif 43 44 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS 45 #include <termios.h> 46 #else 47 #if HAVE_TERMIO_H 48 #include <termio.h> 49 #else 50 #if HAVE_SGSTAT_H 51 #include <sgstat.h> 52 #else 53 #include <sgtty.h> 54 #endif 55 #endif 56 #endif 57 58 #if HAVE_TERMCAP_H 59 #include <termcap.h> 60 #endif 61 #ifdef _OSK 62 #include <signal.h> 63 #endif 64 #if OS2 65 #include <sys/signal.h> 66 #include "pckeys.h" 67 #endif 68 #if HAVE_SYS_STREAM_H 69 #include <sys/stream.h> 70 #endif 71 #if HAVE_SYS_PTEM_H 72 #include <sys/ptem.h> 73 #endif 74 75 #endif /* MSDOS_COMPILER */ 76 77 /* 78 * Check for broken termios package that forces you to manually 79 * set the line discipline. 80 */ 81 #ifdef __ultrix__ 82 #define MUST_SET_LINE_DISCIPLINE 1 83 #else 84 #define MUST_SET_LINE_DISCIPLINE 0 85 #endif 86 87 #if OS2 88 #define DEFAULT_TERM "ansi" 89 static char *windowid; 90 #else 91 #define DEFAULT_TERM "unknown" 92 #endif 93 94 #if MSDOS_COMPILER==MSOFTC 95 static int videopages; 96 static long msec_loops; 97 static int flash_created = 0; 98 #define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); } 99 #endif 100 101 #if MSDOS_COMPILER==BORLANDC 102 static unsigned short *whitescreen; 103 static int flash_created = 0; 104 #endif 105 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 106 #define _settextposition(y,x) gotoxy(x,y) 107 #define _clearscreen(m) clrscr() 108 #define _outtext(s) cputs(s) 109 #define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); } 110 extern int sc_height; 111 #endif 112 113 #if MSDOS_COMPILER==WIN32C 114 struct keyRecord 115 { 116 int ascii; 117 int scan; 118 } currentKey; 119 120 static int keyCount = 0; 121 static WORD curr_attr; 122 static int pending_scancode = 0; 123 static WORD *whitescreen; 124 125 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */ 126 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */ 127 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */ 128 129 extern int quitting; 130 static void win32_init_term(); 131 static void win32_deinit_term(); 132 133 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY) 134 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY) 135 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4))) 136 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \ 137 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \ 138 error("SETCOLORS failed"); } 139 #endif 140 141 #if MSDOS_COMPILER 142 public int nm_fg_color; /* Color of normal text */ 143 public int nm_bg_color; 144 public int bo_fg_color; /* Color of bold text */ 145 public int bo_bg_color; 146 public int ul_fg_color; /* Color of underlined text */ 147 public int ul_bg_color; 148 public int so_fg_color; /* Color of standout text */ 149 public int so_bg_color; 150 public int bl_fg_color; /* Color of blinking text */ 151 public int bl_bg_color; 152 static int sy_fg_color; /* Color of system text (before less) */ 153 static int sy_bg_color; 154 155 #else 156 157 /* 158 * Strings passed to tputs() to do various terminal functions. 159 */ 160 static char 161 *sc_pad, /* Pad string */ 162 *sc_home, /* Cursor home */ 163 *sc_addline, /* Add line, scroll down following lines */ 164 *sc_lower_left, /* Cursor to last line, first column */ 165 *sc_return, /* Cursor to beginning of current line */ 166 *sc_move, /* General cursor positioning */ 167 *sc_clear, /* Clear screen */ 168 *sc_eol_clear, /* Clear to end of line */ 169 *sc_eos_clear, /* Clear to end of screen */ 170 *sc_s_in, /* Enter standout (highlighted) mode */ 171 *sc_s_out, /* Exit standout mode */ 172 *sc_u_in, /* Enter underline mode */ 173 *sc_u_out, /* Exit underline mode */ 174 *sc_b_in, /* Enter bold mode */ 175 *sc_b_out, /* Exit bold mode */ 176 *sc_bl_in, /* Enter blink mode */ 177 *sc_bl_out, /* Exit blink mode */ 178 *sc_visual_bell, /* Visual bell (flash screen) sequence */ 179 *sc_backspace, /* Backspace cursor */ 180 *sc_s_keypad, /* Start keypad mode */ 181 *sc_e_keypad, /* End keypad mode */ 182 *sc_init, /* Startup terminal initialization */ 183 *sc_deinit; /* Exit terminal de-initialization */ 184 #endif 185 186 static int init_done = 0; 187 188 public int auto_wrap; /* Terminal does \r\n when write past margin */ 189 public int ignaw; /* Terminal ignores \n immediately after wrap */ 190 public int erase_char; /* The user's erase char */ 191 public int erase2_char; /* The user's other erase char */ 192 public int kill_char; /* The user's line-kill char */ 193 public int werase_char; /* The user's word-erase char */ 194 public int sc_width, sc_height; /* Height & width of screen */ 195 public int bo_s_width, bo_e_width; /* Printing width of boldface seq */ 196 public int ul_s_width, ul_e_width; /* Printing width of underline seq */ 197 public int so_s_width, so_e_width; /* Printing width of standout seq */ 198 public int bl_s_width, bl_e_width; /* Printing width of blink seq */ 199 public int above_mem, below_mem; /* Memory retained above/below screen */ 200 public int can_goto_line; /* Can move cursor to any line */ 201 public int clear_bg; /* Clear fills with background color */ 202 public int missing_cap = 0; /* Some capability is missing */ 203 204 static int attrmode = AT_NORMAL; 205 extern int binattr; 206 207 #if !MSDOS_COMPILER 208 static char *cheaper(); 209 static void tmodes(); 210 #endif 211 212 /* 213 * These two variables are sometimes defined in, 214 * and needed by, the termcap library. 215 */ 216 #if MUST_DEFINE_OSPEED 217 extern short ospeed; /* Terminal output baud rate */ 218 extern char PC; /* Pad character */ 219 #endif 220 #ifdef _OSK 221 short ospeed; 222 char PC_, *UP, *BC; 223 #endif 224 225 extern int quiet; /* If VERY_QUIET, use visual bell for bell */ 226 extern int no_back_scroll; 227 extern int swindow; 228 extern int no_init; 229 extern int no_keypad; 230 extern int sigs; 231 extern int wscroll; 232 extern int screen_trashed; 233 extern int tty; 234 extern int top_scroll; 235 extern int oldbot; 236 #if HILITE_SEARCH 237 extern int hilite_search; 238 #endif 239 240 extern char *tgetstr(); 241 extern char *tgoto(); 242 243 244 /* 245 * Change terminal to "raw mode", or restore to "normal" mode. 246 * "Raw mode" means 247 * 1. An outstanding read will complete on receipt of a single keystroke. 248 * 2. Input is not echoed. 249 * 3. On output, \n is mapped to \r\n. 250 * 4. \t is NOT expanded into spaces. 251 * 5. Signal-causing characters such as ctrl-C (interrupt), 252 * etc. are NOT disabled. 253 * It doesn't matter whether an input \n is mapped to \r, or vice versa. 254 */ 255 public void 256 raw_mode(on) 257 int on; 258 { 259 static int curr_on = 0; 260 261 if (on == curr_on) 262 return; 263 erase2_char = '\b'; /* in case OS doesn't know about erase2 */ 264 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS 265 { 266 struct termios s; 267 static struct termios save_term; 268 static int saved_term = 0; 269 270 if (on) 271 { 272 /* 273 * Get terminal modes. 274 */ 275 tcgetattr(tty, &s); 276 277 /* 278 * Save modes and set certain variables dependent on modes. 279 */ 280 if (!saved_term) 281 { 282 save_term = s; 283 saved_term = 1; 284 } 285 #if HAVE_OSPEED 286 switch (cfgetospeed(&s)) 287 { 288 #ifdef B0 289 case B0: ospeed = 0; break; 290 #endif 291 #ifdef B50 292 case B50: ospeed = 1; break; 293 #endif 294 #ifdef B75 295 case B75: ospeed = 2; break; 296 #endif 297 #ifdef B110 298 case B110: ospeed = 3; break; 299 #endif 300 #ifdef B134 301 case B134: ospeed = 4; break; 302 #endif 303 #ifdef B150 304 case B150: ospeed = 5; break; 305 #endif 306 #ifdef B200 307 case B200: ospeed = 6; break; 308 #endif 309 #ifdef B300 310 case B300: ospeed = 7; break; 311 #endif 312 #ifdef B600 313 case B600: ospeed = 8; break; 314 #endif 315 #ifdef B1200 316 case B1200: ospeed = 9; break; 317 #endif 318 #ifdef B1800 319 case B1800: ospeed = 10; break; 320 #endif 321 #ifdef B2400 322 case B2400: ospeed = 11; break; 323 #endif 324 #ifdef B4800 325 case B4800: ospeed = 12; break; 326 #endif 327 #ifdef B9600 328 case B9600: ospeed = 13; break; 329 #endif 330 #ifdef EXTA 331 case EXTA: ospeed = 14; break; 332 #endif 333 #ifdef EXTB 334 case EXTB: ospeed = 15; break; 335 #endif 336 #ifdef B57600 337 case B57600: ospeed = 16; break; 338 #endif 339 #ifdef B115200 340 case B115200: ospeed = 17; break; 341 #endif 342 default: ; 343 } 344 #endif 345 erase_char = s.c_cc[VERASE]; 346 #ifdef VERASE2 347 erase2_char = s.c_cc[VERASE2]; 348 #endif 349 kill_char = s.c_cc[VKILL]; 350 #ifdef VWERASE 351 werase_char = s.c_cc[VWERASE]; 352 #else 353 werase_char = CONTROL('W'); 354 #endif 355 356 /* 357 * Set the modes to the way we want them. 358 */ 359 s.c_lflag &= ~(0 360 #ifdef ICANON 361 | ICANON 362 #endif 363 #ifdef ECHO 364 | ECHO 365 #endif 366 #ifdef ECHOE 367 | ECHOE 368 #endif 369 #ifdef ECHOK 370 | ECHOK 371 #endif 372 #if ECHONL 373 | ECHONL 374 #endif 375 ); 376 377 s.c_oflag |= (0 378 #ifdef OXTABS 379 | OXTABS 380 #else 381 #ifdef TAB3 382 | TAB3 383 #else 384 #ifdef XTABS 385 | XTABS 386 #endif 387 #endif 388 #endif 389 #ifdef OPOST 390 | OPOST 391 #endif 392 #ifdef ONLCR 393 | ONLCR 394 #endif 395 ); 396 397 s.c_oflag &= ~(0 398 #ifdef ONOEOT 399 | ONOEOT 400 #endif 401 #ifdef OCRNL 402 | OCRNL 403 #endif 404 #ifdef ONOCR 405 | ONOCR 406 #endif 407 #ifdef ONLRET 408 | ONLRET 409 #endif 410 ); 411 s.c_cc[VMIN] = 1; 412 s.c_cc[VTIME] = 0; 413 #ifdef VLNEXT 414 s.c_cc[VLNEXT] = 0; 415 #endif 416 #ifdef VDSUSP 417 s.c_cc[VDSUSP] = 0; 418 #endif 419 #if MUST_SET_LINE_DISCIPLINE 420 /* 421 * System's termios is broken; need to explicitly 422 * request TERMIODISC line discipline. 423 */ 424 s.c_line = TERMIODISC; 425 #endif 426 } else 427 { 428 /* 429 * Restore saved modes. 430 */ 431 s = save_term; 432 } 433 #if HAVE_FSYNC 434 fsync(tty); 435 #endif 436 tcsetattr(tty, TCSADRAIN, &s); 437 #if MUST_SET_LINE_DISCIPLINE 438 if (!on) 439 { 440 /* 441 * Broken termios *ignores* any line discipline 442 * except TERMIODISC. A different old line discipline 443 * is therefore not restored, yet. Restore the old 444 * line discipline by hand. 445 */ 446 ioctl(tty, TIOCSETD, &save_term.c_line); 447 } 448 #endif 449 } 450 #else 451 #ifdef TCGETA 452 { 453 struct termio s; 454 static struct termio save_term; 455 static int saved_term = 0; 456 457 if (on) 458 { 459 /* 460 * Get terminal modes. 461 */ 462 ioctl(tty, TCGETA, &s); 463 464 /* 465 * Save modes and set certain variables dependent on modes. 466 */ 467 if (!saved_term) 468 { 469 save_term = s; 470 saved_term = 1; 471 } 472 #if HAVE_OSPEED 473 ospeed = s.c_cflag & CBAUD; 474 #endif 475 erase_char = s.c_cc[VERASE]; 476 kill_char = s.c_cc[VKILL]; 477 #ifdef VWERASE 478 werase_char = s.c_cc[VWERASE]; 479 #else 480 werase_char = CONTROL('W'); 481 #endif 482 483 /* 484 * Set the modes to the way we want them. 485 */ 486 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); 487 s.c_oflag |= (OPOST|ONLCR|TAB3); 488 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET); 489 s.c_cc[VMIN] = 1; 490 s.c_cc[VTIME] = 0; 491 } else 492 { 493 /* 494 * Restore saved modes. 495 */ 496 s = save_term; 497 } 498 ioctl(tty, TCSETAW, &s); 499 } 500 #else 501 #ifdef TIOCGETP 502 { 503 struct sgttyb s; 504 static struct sgttyb save_term; 505 static int saved_term = 0; 506 507 if (on) 508 { 509 /* 510 * Get terminal modes. 511 */ 512 ioctl(tty, TIOCGETP, &s); 513 514 /* 515 * Save modes and set certain variables dependent on modes. 516 */ 517 if (!saved_term) 518 { 519 save_term = s; 520 saved_term = 1; 521 } 522 #if HAVE_OSPEED 523 ospeed = s.sg_ospeed; 524 #endif 525 erase_char = s.sg_erase; 526 kill_char = s.sg_kill; 527 werase_char = CONTROL('W'); 528 529 /* 530 * Set the modes to the way we want them. 531 */ 532 s.sg_flags |= CBREAK; 533 s.sg_flags &= ~(ECHO|XTABS); 534 } else 535 { 536 /* 537 * Restore saved modes. 538 */ 539 s = save_term; 540 } 541 ioctl(tty, TIOCSETN, &s); 542 } 543 #else 544 #ifdef _OSK 545 { 546 struct sgbuf s; 547 static struct sgbuf save_term; 548 static int saved_term = 0; 549 550 if (on) 551 { 552 /* 553 * Get terminal modes. 554 */ 555 _gs_opt(tty, &s); 556 557 /* 558 * Save modes and set certain variables dependent on modes. 559 */ 560 if (!saved_term) 561 { 562 save_term = s; 563 saved_term = 1; 564 } 565 erase_char = s.sg_bspch; 566 kill_char = s.sg_dlnch; 567 werase_char = CONTROL('W'); 568 569 /* 570 * Set the modes to the way we want them. 571 */ 572 s.sg_echo = 0; 573 s.sg_eofch = 0; 574 s.sg_pause = 0; 575 s.sg_psch = 0; 576 } else 577 { 578 /* 579 * Restore saved modes. 580 */ 581 s = save_term; 582 } 583 _ss_opt(tty, &s); 584 } 585 #else 586 /* MS-DOS, Windows, or OS2 */ 587 #if OS2 588 /* OS2 */ 589 LSIGNAL(SIGINT, SIG_IGN); 590 #endif 591 erase_char = '\b'; 592 #if MSDOS_COMPILER==DJGPPC 593 kill_char = CONTROL('U'); 594 /* 595 * So that when we shell out or run another program, its 596 * stdin is in cooked mode. We do not switch stdin to binary 597 * mode if fd0 is zero, since that means we were called before 598 * tty was reopened in open_getchr, in which case we would be 599 * changing the original stdin device outside less. 600 */ 601 if (fd0 != 0) 602 setmode(0, on ? O_BINARY : O_TEXT); 603 #else 604 kill_char = ESC; 605 #endif 606 werase_char = CONTROL('W'); 607 #endif 608 #endif 609 #endif 610 #endif 611 curr_on = on; 612 } 613 614 #if !MSDOS_COMPILER 615 /* 616 * Some glue to prevent calling termcap functions if tgetent() failed. 617 */ 618 static int hardcopy; 619 620 static char * 621 ltget_env(capname) 622 char *capname; 623 { 624 char name[16]; 625 char *s; 626 627 s = lgetenv("LESS_TERMCAP_DEBUG"); 628 if (s != NULL && *s != '\0') 629 { 630 struct env { struct env *next; char *name; char *value; }; 631 static struct env *envs = NULL; 632 struct env *p; 633 for (p = envs; p != NULL; p = p->next) 634 if (strcmp(p->name, capname) == 0) 635 return p->value; 636 p = (struct env *) ecalloc(1, sizeof(struct env)); 637 p->name = save(capname); 638 p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char)); 639 sprintf(p->value, "<%s>", capname); 640 p->next = envs; 641 envs = p; 642 return p->value; 643 } 644 strcpy(name, "LESS_TERMCAP_"); 645 strcat(name, capname); 646 return (lgetenv(name)); 647 } 648 649 static int 650 ltgetflag(capname) 651 char *capname; 652 { 653 char *s; 654 655 if ((s = ltget_env(capname)) != NULL) 656 return (*s != '\0' && *s != '0'); 657 if (hardcopy) 658 return (0); 659 return (tgetflag(capname)); 660 } 661 662 static int 663 ltgetnum(capname) 664 char *capname; 665 { 666 char *s; 667 668 if ((s = ltget_env(capname)) != NULL) 669 return (atoi(s)); 670 if (hardcopy) 671 return (-1); 672 return (tgetnum(capname)); 673 } 674 675 static char * 676 ltgetstr(capname, pp) 677 char *capname; 678 char **pp; 679 { 680 char *s; 681 682 if ((s = ltget_env(capname)) != NULL) 683 return (s); 684 if (hardcopy) 685 return (NULL); 686 return (tgetstr(capname, pp)); 687 } 688 #endif /* MSDOS_COMPILER */ 689 690 /* 691 * Get size of the output screen. 692 */ 693 public void 694 scrsize() 695 { 696 register char *s; 697 int sys_height; 698 int sys_width; 699 #if !MSDOS_COMPILER 700 int n; 701 #endif 702 703 #define DEF_SC_WIDTH 80 704 #if MSDOS_COMPILER 705 #define DEF_SC_HEIGHT 25 706 #else 707 #define DEF_SC_HEIGHT 24 708 #endif 709 710 711 sys_width = sys_height = 0; 712 713 #if MSDOS_COMPILER==MSOFTC 714 { 715 struct videoconfig w; 716 _getvideoconfig(&w); 717 sys_height = w.numtextrows; 718 sys_width = w.numtextcols; 719 } 720 #else 721 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 722 { 723 struct text_info w; 724 gettextinfo(&w); 725 sys_height = w.screenheight; 726 sys_width = w.screenwidth; 727 } 728 #else 729 #if MSDOS_COMPILER==WIN32C 730 { 731 CONSOLE_SCREEN_BUFFER_INFO scr; 732 GetConsoleScreenBufferInfo(con_out, &scr); 733 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1; 734 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1; 735 } 736 #else 737 #if OS2 738 { 739 int s[2]; 740 _scrsize(s); 741 sys_width = s[0]; 742 sys_height = s[1]; 743 /* 744 * When using terminal emulators for XFree86/OS2, the 745 * _scrsize function does not work well. 746 * Call the scrsize.exe program to get the window size. 747 */ 748 windowid = getenv("WINDOWID"); 749 if (windowid != NULL) 750 { 751 FILE *fd = popen("scrsize", "rt"); 752 if (fd != NULL) 753 { 754 int w, h; 755 fscanf(fd, "%i %i", &w, &h); 756 if (w > 0 && h > 0) 757 { 758 sys_width = w; 759 sys_height = h; 760 } 761 pclose(fd); 762 } 763 } 764 } 765 #else 766 #ifdef TIOCGWINSZ 767 { 768 struct winsize w; 769 if (ioctl(2, TIOCGWINSZ, &w) == 0) 770 { 771 if (w.ws_row > 0) 772 sys_height = w.ws_row; 773 if (w.ws_col > 0) 774 sys_width = w.ws_col; 775 } 776 } 777 #else 778 #ifdef WIOCGETD 779 { 780 struct uwdata w; 781 if (ioctl(2, WIOCGETD, &w) == 0) 782 { 783 if (w.uw_height > 0) 784 sys_height = w.uw_height / w.uw_vs; 785 if (w.uw_width > 0) 786 sys_width = w.uw_width / w.uw_hs; 787 } 788 } 789 #endif 790 #endif 791 #endif 792 #endif 793 #endif 794 #endif 795 796 if (sys_height > 0) 797 sc_height = sys_height; 798 else if ((s = lgetenv("LINES")) != NULL) 799 sc_height = atoi(s); 800 #if !MSDOS_COMPILER 801 else if ((n = ltgetnum("li")) > 0) 802 sc_height = n; 803 #endif 804 if (sc_height <= 0) 805 sc_height = DEF_SC_HEIGHT; 806 807 if (sys_width > 0) 808 sc_width = sys_width; 809 else if ((s = lgetenv("COLUMNS")) != NULL) 810 sc_width = atoi(s); 811 #if !MSDOS_COMPILER 812 else if ((n = ltgetnum("co")) > 0) 813 sc_width = n; 814 #endif 815 if (sc_width <= 0) 816 sc_width = DEF_SC_WIDTH; 817 } 818 819 #if MSDOS_COMPILER==MSOFTC 820 /* 821 * Figure out how many empty loops it takes to delay a millisecond. 822 */ 823 static void 824 get_clock() 825 { 826 clock_t start; 827 828 /* 829 * Get synchronized at the start of a tick. 830 */ 831 start = clock(); 832 while (clock() == start) 833 ; 834 /* 835 * Now count loops till the next tick. 836 */ 837 start = clock(); 838 msec_loops = 0; 839 while (clock() == start) 840 msec_loops++; 841 /* 842 * Convert from (loops per clock) to (loops per millisecond). 843 */ 844 msec_loops *= CLOCKS_PER_SEC; 845 msec_loops /= 1000; 846 } 847 848 /* 849 * Delay for a specified number of milliseconds. 850 */ 851 static void 852 dummy_func() 853 { 854 static long delay_dummy = 0; 855 delay_dummy++; 856 } 857 858 static void 859 delay(msec) 860 int msec; 861 { 862 long i; 863 864 while (msec-- > 0) 865 { 866 for (i = 0; i < msec_loops; i++) 867 { 868 /* 869 * Make it look like we're doing something here, 870 * so the optimizer doesn't remove the whole loop. 871 */ 872 dummy_func(); 873 } 874 } 875 } 876 #endif 877 878 /* 879 * Return the characters actually input by a "special" key. 880 */ 881 public char * 882 special_key_str(key) 883 int key; 884 { 885 static char tbuf[40]; 886 char *s; 887 #if MSDOS_COMPILER || OS2 888 static char k_right[] = { '\340', PCK_RIGHT, 0 }; 889 static char k_left[] = { '\340', PCK_LEFT, 0 }; 890 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 }; 891 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 }; 892 static char k_insert[] = { '\340', PCK_INSERT, 0 }; 893 static char k_delete[] = { '\340', PCK_DELETE, 0 }; 894 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 }; 895 static char k_ctl_backspace[] = { '\177', 0 }; 896 static char k_home[] = { '\340', PCK_HOME, 0 }; 897 static char k_end[] = { '\340', PCK_END, 0 }; 898 static char k_up[] = { '\340', PCK_UP, 0 }; 899 static char k_down[] = { '\340', PCK_DOWN, 0 }; 900 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 }; 901 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 }; 902 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 }; 903 static char k_f1[] = { '\340', PCK_F1, 0 }; 904 #endif 905 #if !MSDOS_COMPILER 906 char *sp = tbuf; 907 #endif 908 909 switch (key) 910 { 911 #if OS2 912 /* 913 * If windowid is not NULL, assume less is executed in 914 * the XFree86 environment. 915 */ 916 case SK_RIGHT_ARROW: 917 s = windowid ? ltgetstr("kr", &sp) : k_right; 918 break; 919 case SK_LEFT_ARROW: 920 s = windowid ? ltgetstr("kl", &sp) : k_left; 921 break; 922 case SK_UP_ARROW: 923 s = windowid ? ltgetstr("ku", &sp) : k_up; 924 break; 925 case SK_DOWN_ARROW: 926 s = windowid ? ltgetstr("kd", &sp) : k_down; 927 break; 928 case SK_PAGE_UP: 929 s = windowid ? ltgetstr("kP", &sp) : k_pageup; 930 break; 931 case SK_PAGE_DOWN: 932 s = windowid ? ltgetstr("kN", &sp) : k_pagedown; 933 break; 934 case SK_HOME: 935 s = windowid ? ltgetstr("kh", &sp) : k_home; 936 break; 937 case SK_END: 938 s = windowid ? ltgetstr("@7", &sp) : k_end; 939 break; 940 case SK_DELETE: 941 if (windowid) 942 { 943 s = ltgetstr("kD", &sp); 944 if (s == NULL) 945 { 946 tbuf[0] = '\177'; 947 tbuf[1] = '\0'; 948 s = tbuf; 949 } 950 } else 951 s = k_delete; 952 break; 953 #endif 954 #if MSDOS_COMPILER 955 case SK_RIGHT_ARROW: 956 s = k_right; 957 break; 958 case SK_LEFT_ARROW: 959 s = k_left; 960 break; 961 case SK_UP_ARROW: 962 s = k_up; 963 break; 964 case SK_DOWN_ARROW: 965 s = k_down; 966 break; 967 case SK_PAGE_UP: 968 s = k_pageup; 969 break; 970 case SK_PAGE_DOWN: 971 s = k_pagedown; 972 break; 973 case SK_HOME: 974 s = k_home; 975 break; 976 case SK_END: 977 s = k_end; 978 break; 979 case SK_DELETE: 980 s = k_delete; 981 break; 982 #endif 983 #if MSDOS_COMPILER || OS2 984 case SK_INSERT: 985 s = k_insert; 986 break; 987 case SK_CTL_LEFT_ARROW: 988 s = k_ctl_left; 989 break; 990 case SK_CTL_RIGHT_ARROW: 991 s = k_ctl_right; 992 break; 993 case SK_CTL_BACKSPACE: 994 s = k_ctl_backspace; 995 break; 996 case SK_CTL_DELETE: 997 s = k_ctl_delete; 998 break; 999 case SK_F1: 1000 s = k_f1; 1001 break; 1002 case SK_BACKTAB: 1003 s = k_backtab; 1004 break; 1005 #else 1006 case SK_RIGHT_ARROW: 1007 s = ltgetstr("kr", &sp); 1008 break; 1009 case SK_LEFT_ARROW: 1010 s = ltgetstr("kl", &sp); 1011 break; 1012 case SK_UP_ARROW: 1013 s = ltgetstr("ku", &sp); 1014 break; 1015 case SK_DOWN_ARROW: 1016 s = ltgetstr("kd", &sp); 1017 break; 1018 case SK_PAGE_UP: 1019 s = ltgetstr("kP", &sp); 1020 break; 1021 case SK_PAGE_DOWN: 1022 s = ltgetstr("kN", &sp); 1023 break; 1024 case SK_HOME: 1025 s = ltgetstr("kh", &sp); 1026 break; 1027 case SK_END: 1028 s = ltgetstr("@7", &sp); 1029 break; 1030 case SK_DELETE: 1031 s = ltgetstr("kD", &sp); 1032 if (s == NULL) 1033 { 1034 tbuf[0] = '\177'; 1035 tbuf[1] = '\0'; 1036 s = tbuf; 1037 } 1038 break; 1039 #endif 1040 case SK_CONTROL_K: 1041 tbuf[0] = CONTROL('K'); 1042 tbuf[1] = '\0'; 1043 s = tbuf; 1044 break; 1045 default: 1046 return (NULL); 1047 } 1048 return (s); 1049 } 1050 1051 /* 1052 * Get terminal capabilities via termcap. 1053 */ 1054 public void 1055 get_term() 1056 { 1057 #if MSDOS_COMPILER 1058 auto_wrap = 1; 1059 ignaw = 0; 1060 can_goto_line = 1; 1061 clear_bg = 1; 1062 /* 1063 * Set up default colors. 1064 * The xx_s_width and xx_e_width vars are already initialized to 0. 1065 */ 1066 #if MSDOS_COMPILER==MSOFTC 1067 sy_bg_color = _getbkcolor(); 1068 sy_fg_color = _gettextcolor(); 1069 get_clock(); 1070 #else 1071 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 1072 { 1073 struct text_info w; 1074 gettextinfo(&w); 1075 sy_bg_color = (w.attribute >> 4) & 0x0F; 1076 sy_fg_color = (w.attribute >> 0) & 0x0F; 1077 } 1078 #else 1079 #if MSDOS_COMPILER==WIN32C 1080 { 1081 DWORD nread; 1082 CONSOLE_SCREEN_BUFFER_INFO scr; 1083 1084 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE); 1085 /* 1086 * Always open stdin in binary. Note this *must* be done 1087 * before any file operations have been done on fd0. 1088 */ 1089 SET_BINARY(0); 1090 GetConsoleScreenBufferInfo(con_out, &scr); 1091 ReadConsoleOutputAttribute(con_out, &curr_attr, 1092 1, scr.dwCursorPosition, &nread); 1093 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */ 1094 sy_fg_color = curr_attr & FG_COLORS; 1095 } 1096 #endif 1097 #endif 1098 #endif 1099 nm_fg_color = sy_fg_color; 1100 nm_bg_color = sy_bg_color; 1101 bo_fg_color = 11; 1102 bo_bg_color = 0; 1103 ul_fg_color = 9; 1104 ul_bg_color = 0; 1105 so_fg_color = 15; 1106 so_bg_color = 9; 1107 bl_fg_color = 15; 1108 bl_bg_color = 0; 1109 1110 /* 1111 * Get size of the screen. 1112 */ 1113 scrsize(); 1114 pos_init(); 1115 1116 1117 #else /* !MSDOS_COMPILER */ 1118 1119 char *sp; 1120 register char *t1, *t2; 1121 char *term; 1122 char termbuf[TERMBUF_SIZE]; 1123 1124 static char sbuf[TERMSBUF_SIZE]; 1125 1126 #if OS2 1127 /* 1128 * Make sure the termcap database is available. 1129 */ 1130 sp = lgetenv("TERMCAP"); 1131 if (sp == NULL || *sp == '\0') 1132 { 1133 char *termcap; 1134 if ((sp = homefile("termcap.dat")) != NULL) 1135 { 1136 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char)); 1137 sprintf(termcap, "TERMCAP=%s", sp); 1138 free(sp); 1139 putenv(termcap); 1140 } 1141 } 1142 #endif 1143 /* 1144 * Find out what kind of terminal this is. 1145 */ 1146 if ((term = lgetenv("TERM")) == NULL) 1147 term = DEFAULT_TERM; 1148 hardcopy = 0; 1149 if (tgetent(termbuf, term) != TGETENT_OK) 1150 hardcopy = 1; 1151 if (ltgetflag("hc")) 1152 hardcopy = 1; 1153 1154 /* 1155 * Get size of the screen. 1156 */ 1157 scrsize(); 1158 pos_init(); 1159 1160 auto_wrap = ltgetflag("am"); 1161 ignaw = ltgetflag("xn"); 1162 above_mem = ltgetflag("da"); 1163 below_mem = ltgetflag("db"); 1164 clear_bg = ltgetflag("ut"); 1165 1166 /* 1167 * Assumes termcap variable "sg" is the printing width of: 1168 * the standout sequence, the end standout sequence, 1169 * the underline sequence, the end underline sequence, 1170 * the boldface sequence, and the end boldface sequence. 1171 */ 1172 if ((so_s_width = ltgetnum("sg")) < 0) 1173 so_s_width = 0; 1174 so_e_width = so_s_width; 1175 1176 bo_s_width = bo_e_width = so_s_width; 1177 ul_s_width = ul_e_width = so_s_width; 1178 bl_s_width = bl_e_width = so_s_width; 1179 1180 #if HILITE_SEARCH 1181 if (so_s_width > 0 || so_e_width > 0) 1182 /* 1183 * Disable highlighting by default on magic cookie terminals. 1184 * Turning on highlighting might change the displayed width 1185 * of a line, causing the display to get messed up. 1186 * The user can turn it back on with -g, 1187 * but she won't like the results. 1188 */ 1189 hilite_search = 0; 1190 #endif 1191 1192 /* 1193 * Get various string-valued capabilities. 1194 */ 1195 sp = sbuf; 1196 1197 #if HAVE_OSPEED 1198 sc_pad = ltgetstr("pc", &sp); 1199 if (sc_pad != NULL) 1200 PC = *sc_pad; 1201 #endif 1202 1203 sc_s_keypad = ltgetstr("ks", &sp); 1204 if (sc_s_keypad == NULL) 1205 sc_s_keypad = ""; 1206 sc_e_keypad = ltgetstr("ke", &sp); 1207 if (sc_e_keypad == NULL) 1208 sc_e_keypad = ""; 1209 1210 sc_init = ltgetstr("ti", &sp); 1211 if (sc_init == NULL) 1212 sc_init = ""; 1213 1214 sc_deinit= ltgetstr("te", &sp); 1215 if (sc_deinit == NULL) 1216 sc_deinit = ""; 1217 1218 sc_eol_clear = ltgetstr("ce", &sp); 1219 if (sc_eol_clear == NULL || *sc_eol_clear == '\0') 1220 { 1221 missing_cap = 1; 1222 sc_eol_clear = ""; 1223 } 1224 1225 sc_eos_clear = ltgetstr("cd", &sp); 1226 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0')) 1227 { 1228 missing_cap = 1; 1229 sc_eos_clear = ""; 1230 } 1231 1232 sc_clear = ltgetstr("cl", &sp); 1233 if (sc_clear == NULL || *sc_clear == '\0') 1234 { 1235 missing_cap = 1; 1236 sc_clear = "\n\n"; 1237 } 1238 1239 sc_move = ltgetstr("cm", &sp); 1240 if (sc_move == NULL || *sc_move == '\0') 1241 { 1242 /* 1243 * This is not an error here, because we don't 1244 * always need sc_move. 1245 * We need it only if we don't have home or lower-left. 1246 */ 1247 sc_move = ""; 1248 can_goto_line = 0; 1249 } else 1250 can_goto_line = 1; 1251 1252 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp); 1253 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp); 1254 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp); 1255 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp); 1256 1257 sc_visual_bell = ltgetstr("vb", &sp); 1258 if (sc_visual_bell == NULL) 1259 sc_visual_bell = ""; 1260 1261 if (ltgetflag("bs")) 1262 sc_backspace = "\b"; 1263 else 1264 { 1265 sc_backspace = ltgetstr("bc", &sp); 1266 if (sc_backspace == NULL || *sc_backspace == '\0') 1267 sc_backspace = "\b"; 1268 } 1269 1270 /* 1271 * Choose between using "ho" and "cm" ("home" and "cursor move") 1272 * to move the cursor to the upper left corner of the screen. 1273 */ 1274 t1 = ltgetstr("ho", &sp); 1275 if (t1 == NULL) 1276 t1 = ""; 1277 if (*sc_move == '\0') 1278 t2 = ""; 1279 else 1280 { 1281 strcpy(sp, tgoto(sc_move, 0, 0)); 1282 t2 = sp; 1283 sp += strlen(sp) + 1; 1284 } 1285 sc_home = cheaper(t1, t2, "|\b^"); 1286 1287 /* 1288 * Choose between using "ll" and "cm" ("lower left" and "cursor move") 1289 * to move the cursor to the lower left corner of the screen. 1290 */ 1291 t1 = ltgetstr("ll", &sp); 1292 if (t1 == NULL) 1293 t1 = ""; 1294 if (*sc_move == '\0') 1295 t2 = ""; 1296 else 1297 { 1298 strcpy(sp, tgoto(sc_move, 0, sc_height-1)); 1299 t2 = sp; 1300 sp += strlen(sp) + 1; 1301 } 1302 sc_lower_left = cheaper(t1, t2, "\r"); 1303 1304 /* 1305 * Get carriage return string. 1306 */ 1307 sc_return = ltgetstr("cr", &sp); 1308 if (sc_return == NULL) 1309 sc_return = "\r"; 1310 1311 /* 1312 * Choose between using "al" or "sr" ("add line" or "scroll reverse") 1313 * to add a line at the top of the screen. 1314 */ 1315 t1 = ltgetstr("al", &sp); 1316 if (t1 == NULL) 1317 t1 = ""; 1318 t2 = ltgetstr("sr", &sp); 1319 if (t2 == NULL) 1320 t2 = ""; 1321 #if OS2 1322 if (*t1 == '\0' && *t2 == '\0') 1323 sc_addline = ""; 1324 else 1325 #endif 1326 if (above_mem) 1327 sc_addline = t1; 1328 else 1329 sc_addline = cheaper(t1, t2, ""); 1330 if (*sc_addline == '\0') 1331 { 1332 /* 1333 * Force repaint on any backward movement. 1334 */ 1335 no_back_scroll = 1; 1336 } 1337 #endif /* MSDOS_COMPILER */ 1338 } 1339 1340 #if !MSDOS_COMPILER 1341 /* 1342 * Return the cost of displaying a termcap string. 1343 * We use the trick of calling tputs, but as a char printing function 1344 * we give it inc_costcount, which just increments "costcount". 1345 * This tells us how many chars would be printed by using this string. 1346 * {{ Couldn't we just use strlen? }} 1347 */ 1348 static int costcount; 1349 1350 /*ARGSUSED*/ 1351 static int 1352 inc_costcount(c) 1353 int c; 1354 { 1355 costcount++; 1356 return (c); 1357 } 1358 1359 static int 1360 cost(t) 1361 char *t; 1362 { 1363 costcount = 0; 1364 tputs(t, sc_height, inc_costcount); 1365 return (costcount); 1366 } 1367 1368 /* 1369 * Return the "best" of the two given termcap strings. 1370 * The best, if both exist, is the one with the lower 1371 * cost (see cost() function). 1372 */ 1373 static char * 1374 cheaper(t1, t2, def) 1375 char *t1, *t2; 1376 char *def; 1377 { 1378 if (*t1 == '\0' && *t2 == '\0') 1379 { 1380 missing_cap = 1; 1381 return (def); 1382 } 1383 if (*t1 == '\0') 1384 return (t2); 1385 if (*t2 == '\0') 1386 return (t1); 1387 if (cost(t1) < cost(t2)) 1388 return (t1); 1389 return (t2); 1390 } 1391 1392 static void 1393 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp) 1394 char *incap; 1395 char *outcap; 1396 char **instr; 1397 char **outstr; 1398 char *def_instr; 1399 char *def_outstr; 1400 char **spp; 1401 { 1402 *instr = ltgetstr(incap, spp); 1403 if (*instr == NULL) 1404 { 1405 /* Use defaults. */ 1406 *instr = def_instr; 1407 *outstr = def_outstr; 1408 return; 1409 } 1410 1411 *outstr = ltgetstr(outcap, spp); 1412 if (*outstr == NULL) 1413 /* No specific out capability; use "me". */ 1414 *outstr = ltgetstr("me", spp); 1415 if (*outstr == NULL) 1416 /* Don't even have "me"; use a null string. */ 1417 *outstr = ""; 1418 } 1419 1420 #endif /* MSDOS_COMPILER */ 1421 1422 1423 /* 1424 * Below are the functions which perform all the 1425 * terminal-specific screen manipulation. 1426 */ 1427 1428 1429 #if MSDOS_COMPILER 1430 1431 #if MSDOS_COMPILER==WIN32C 1432 static void 1433 _settextposition(int row, int col) 1434 { 1435 COORD cpos; 1436 CONSOLE_SCREEN_BUFFER_INFO csbi; 1437 1438 GetConsoleScreenBufferInfo(con_out, &csbi); 1439 cpos.X = csbi.srWindow.Left + (col - 1); 1440 cpos.Y = csbi.srWindow.Top + (row - 1); 1441 SetConsoleCursorPosition(con_out, cpos); 1442 } 1443 #endif 1444 1445 /* 1446 * Initialize the screen to the correct color at startup. 1447 */ 1448 static void 1449 initcolor() 1450 { 1451 SETCOLORS(nm_fg_color, nm_bg_color); 1452 #if 0 1453 /* 1454 * This clears the screen at startup. This is different from 1455 * the behavior of other versions of less. Disable it for now. 1456 */ 1457 char *blanks; 1458 int row; 1459 int col; 1460 1461 /* 1462 * Create a complete, blank screen using "normal" colors. 1463 */ 1464 SETCOLORS(nm_fg_color, nm_bg_color); 1465 blanks = (char *) ecalloc(width+1, sizeof(char)); 1466 for (col = 0; col < sc_width; col++) 1467 blanks[col] = ' '; 1468 blanks[sc_width] = '\0'; 1469 for (row = 0; row < sc_height; row++) 1470 _outtext(blanks); 1471 free(blanks); 1472 #endif 1473 } 1474 #endif 1475 1476 #if MSDOS_COMPILER==WIN32C 1477 1478 /* 1479 * Termcap-like init with a private win32 console. 1480 */ 1481 static void 1482 win32_init_term() 1483 { 1484 CONSOLE_SCREEN_BUFFER_INFO scr; 1485 COORD size; 1486 1487 if (con_out_save == INVALID_HANDLE_VALUE) 1488 return; 1489 1490 GetConsoleScreenBufferInfo(con_out_save, &scr); 1491 1492 if (con_out_ours == INVALID_HANDLE_VALUE) 1493 { 1494 /* 1495 * Create our own screen buffer, so that we 1496 * may restore the original when done. 1497 */ 1498 con_out_ours = CreateConsoleScreenBuffer( 1499 GENERIC_WRITE | GENERIC_READ, 1500 FILE_SHARE_WRITE | FILE_SHARE_READ, 1501 (LPSECURITY_ATTRIBUTES) NULL, 1502 CONSOLE_TEXTMODE_BUFFER, 1503 (LPVOID) NULL); 1504 } 1505 1506 size.X = scr.srWindow.Right - scr.srWindow.Left + 1; 1507 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1; 1508 SetConsoleScreenBufferSize(con_out_ours, size); 1509 SetConsoleActiveScreenBuffer(con_out_ours); 1510 con_out = con_out_ours; 1511 } 1512 1513 /* 1514 * Restore the startup console. 1515 */ 1516 static void 1517 win32_deinit_term() 1518 { 1519 if (con_out_save == INVALID_HANDLE_VALUE) 1520 return; 1521 if (quitting) 1522 (void) CloseHandle(con_out_ours); 1523 SetConsoleActiveScreenBuffer(con_out_save); 1524 con_out = con_out_save; 1525 } 1526 1527 #endif 1528 1529 /* 1530 * Initialize terminal 1531 */ 1532 public void 1533 init() 1534 { 1535 #if !MSDOS_COMPILER 1536 if (!no_init) 1537 tputs(sc_init, sc_height, putchr); 1538 if (!no_keypad) 1539 tputs(sc_s_keypad, sc_height, putchr); 1540 if (top_scroll) 1541 { 1542 int i; 1543 1544 /* 1545 * This is nice to terminals with no alternate screen, 1546 * but with saved scrolled-off-the-top lines. This way, 1547 * no previous line is lost, but we start with a whole 1548 * screen to ourself. 1549 */ 1550 for (i = 1; i < sc_height; i++) 1551 putchr('\n'); 1552 } else 1553 line_left(); 1554 #else 1555 #if MSDOS_COMPILER==WIN32C 1556 if (!no_init) 1557 win32_init_term(); 1558 #endif 1559 initcolor(); 1560 flush(); 1561 #endif 1562 init_done = 1; 1563 } 1564 1565 /* 1566 * Deinitialize terminal 1567 */ 1568 public void 1569 deinit() 1570 { 1571 if (!init_done) 1572 return; 1573 #if !MSDOS_COMPILER 1574 if (!no_keypad) 1575 tputs(sc_e_keypad, sc_height, putchr); 1576 if (!no_init) 1577 tputs(sc_deinit, sc_height, putchr); 1578 #else 1579 /* Restore system colors. */ 1580 SETCOLORS(sy_fg_color, sy_bg_color); 1581 #if MSDOS_COMPILER==WIN32C 1582 if (!no_init) 1583 win32_deinit_term(); 1584 #else 1585 /* Need clreol to make SETCOLORS take effect. */ 1586 clreol(); 1587 #endif 1588 #endif 1589 init_done = 0; 1590 } 1591 1592 /* 1593 * Home cursor (move to upper left corner of screen). 1594 */ 1595 public void 1596 home() 1597 { 1598 #if !MSDOS_COMPILER 1599 tputs(sc_home, 1, putchr); 1600 #else 1601 flush(); 1602 _settextposition(1,1); 1603 #endif 1604 } 1605 1606 /* 1607 * Add a blank line (called with cursor at home). 1608 * Should scroll the display down. 1609 */ 1610 public void 1611 add_line() 1612 { 1613 #if !MSDOS_COMPILER 1614 tputs(sc_addline, sc_height, putchr); 1615 #else 1616 flush(); 1617 #if MSDOS_COMPILER==MSOFTC 1618 _scrolltextwindow(_GSCROLLDOWN); 1619 _settextposition(1,1); 1620 #else 1621 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 1622 movetext(1,1, sc_width,sc_height-1, 1,2); 1623 gotoxy(1,1); 1624 clreol(); 1625 #else 1626 #if MSDOS_COMPILER==WIN32C 1627 { 1628 CHAR_INFO fillchar; 1629 SMALL_RECT rcSrc, rcClip; 1630 COORD new_org; 1631 CONSOLE_SCREEN_BUFFER_INFO csbi; 1632 1633 GetConsoleScreenBufferInfo(con_out,&csbi); 1634 1635 /* The clip rectangle is the entire visible screen. */ 1636 rcClip.Left = csbi.srWindow.Left; 1637 rcClip.Top = csbi.srWindow.Top; 1638 rcClip.Right = csbi.srWindow.Right; 1639 rcClip.Bottom = csbi.srWindow.Bottom; 1640 1641 /* The source rectangle is the visible screen minus the last line. */ 1642 rcSrc = rcClip; 1643 rcSrc.Bottom--; 1644 1645 /* Move the top left corner of the source window down one row. */ 1646 new_org.X = rcSrc.Left; 1647 new_org.Y = rcSrc.Top + 1; 1648 1649 /* Fill the right character and attributes. */ 1650 fillchar.Char.AsciiChar = ' '; 1651 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 1652 fillchar.Attributes = curr_attr; 1653 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 1654 _settextposition(1,1); 1655 } 1656 #endif 1657 #endif 1658 #endif 1659 #endif 1660 } 1661 1662 #if 0 1663 /* 1664 * Remove the n topmost lines and scroll everything below it in the 1665 * window upward. This is needed to stop leaking the topmost line 1666 * into the scrollback buffer when we go down-one-line (in WIN32). 1667 */ 1668 public void 1669 remove_top(n) 1670 int n; 1671 { 1672 #if MSDOS_COMPILER==WIN32C 1673 SMALL_RECT rcSrc, rcClip; 1674 CHAR_INFO fillchar; 1675 COORD new_org; 1676 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ 1677 1678 if (n >= sc_height - 1) 1679 { 1680 clear(); 1681 home(); 1682 return; 1683 } 1684 1685 flush(); 1686 1687 GetConsoleScreenBufferInfo(con_out, &csbi); 1688 1689 /* Get the extent of all-visible-rows-but-the-last. */ 1690 rcSrc.Left = csbi.srWindow.Left; 1691 rcSrc.Top = csbi.srWindow.Top + n; 1692 rcSrc.Right = csbi.srWindow.Right; 1693 rcSrc.Bottom = csbi.srWindow.Bottom; 1694 1695 /* Get the clip rectangle. */ 1696 rcClip.Left = rcSrc.Left; 1697 rcClip.Top = csbi.srWindow.Top; 1698 rcClip.Right = rcSrc.Right; 1699 rcClip.Bottom = rcSrc.Bottom ; 1700 1701 /* Move the source window up n rows. */ 1702 new_org.X = rcSrc.Left; 1703 new_org.Y = rcSrc.Top - n; 1704 1705 /* Fill the right character and attributes. */ 1706 fillchar.Char.AsciiChar = ' '; 1707 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 1708 fillchar.Attributes = curr_attr; 1709 1710 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 1711 1712 /* Position cursor on first blank line. */ 1713 goto_line(sc_height - n - 1); 1714 #endif 1715 } 1716 #endif 1717 1718 #if MSDOS_COMPILER==WIN32C 1719 /* 1720 * Clear the screen. 1721 */ 1722 static void 1723 win32_clear() 1724 { 1725 /* 1726 * This will clear only the currently visible rows of the NT 1727 * console buffer, which means none of the precious scrollback 1728 * rows are touched making for faster scrolling. Note that, if 1729 * the window has fewer columns than the console buffer (i.e. 1730 * there is a horizontal scrollbar as well), the entire width 1731 * of the visible rows will be cleared. 1732 */ 1733 COORD topleft; 1734 DWORD nchars; 1735 DWORD winsz; 1736 CONSOLE_SCREEN_BUFFER_INFO csbi; 1737 1738 /* get the number of cells in the current buffer */ 1739 GetConsoleScreenBufferInfo(con_out, &csbi); 1740 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1); 1741 topleft.X = 0; 1742 topleft.Y = csbi.srWindow.Top; 1743 1744 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 1745 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars); 1746 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars); 1747 } 1748 1749 /* 1750 * Remove the n topmost lines and scroll everything below it in the 1751 * window upward. 1752 */ 1753 public void 1754 win32_scroll_up(n) 1755 int n; 1756 { 1757 SMALL_RECT rcSrc, rcClip; 1758 CHAR_INFO fillchar; 1759 COORD topleft; 1760 COORD new_org; 1761 DWORD nchars; 1762 DWORD size; 1763 CONSOLE_SCREEN_BUFFER_INFO csbi; 1764 1765 if (n <= 0) 1766 return; 1767 1768 if (n >= sc_height - 1) 1769 { 1770 win32_clear(); 1771 _settextposition(1,1); 1772 return; 1773 } 1774 1775 /* Get the extent of what will remain visible after scrolling. */ 1776 GetConsoleScreenBufferInfo(con_out, &csbi); 1777 rcSrc.Left = csbi.srWindow.Left; 1778 rcSrc.Top = csbi.srWindow.Top + n; 1779 rcSrc.Right = csbi.srWindow.Right; 1780 rcSrc.Bottom = csbi.srWindow.Bottom; 1781 1782 /* Get the clip rectangle. */ 1783 rcClip.Left = rcSrc.Left; 1784 rcClip.Top = csbi.srWindow.Top; 1785 rcClip.Right = rcSrc.Right; 1786 rcClip.Bottom = rcSrc.Bottom ; 1787 1788 /* Move the source text to the top of the screen. */ 1789 new_org.X = rcSrc.Left; 1790 new_org.Y = rcClip.Top; 1791 1792 /* Fill the right character and attributes. */ 1793 fillchar.Char.AsciiChar = ' '; 1794 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color); 1795 1796 /* Scroll the window. */ 1797 SetConsoleTextAttribute(con_out, fillchar.Attributes); 1798 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 1799 1800 /* Clear remaining lines at bottom. */ 1801 topleft.X = csbi.dwCursorPosition.X; 1802 topleft.Y = rcSrc.Bottom - n; 1803 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X); 1804 FillConsoleOutputCharacter(con_out, ' ', size, topleft, 1805 &nchars); 1806 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft, 1807 &nchars); 1808 SetConsoleTextAttribute(con_out, curr_attr); 1809 1810 /* Move cursor n lines up from where it was. */ 1811 csbi.dwCursorPosition.Y -= n; 1812 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition); 1813 } 1814 #endif 1815 1816 /* 1817 * Move cursor to lower left corner of screen. 1818 */ 1819 public void 1820 lower_left() 1821 { 1822 #if !MSDOS_COMPILER 1823 tputs(sc_lower_left, 1, putchr); 1824 #else 1825 flush(); 1826 _settextposition(sc_height, 1); 1827 #endif 1828 } 1829 1830 /* 1831 * Move cursor to left position of current line. 1832 */ 1833 public void 1834 line_left() 1835 { 1836 #if !MSDOS_COMPILER 1837 tputs(sc_return, 1, putchr); 1838 #else 1839 int row; 1840 flush(); 1841 #if MSDOS_COMPILER==WIN32C 1842 { 1843 CONSOLE_SCREEN_BUFFER_INFO scr; 1844 GetConsoleScreenBufferInfo(con_out, &scr); 1845 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1; 1846 } 1847 #else 1848 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 1849 row = wherey(); 1850 #else 1851 { 1852 struct rccoord tpos = _gettextposition(); 1853 row = tpos.row; 1854 } 1855 #endif 1856 #endif 1857 _settextposition(row, 1); 1858 #endif 1859 } 1860 1861 /* 1862 * Check if the console size has changed and reset internals 1863 * (in lieu of SIGWINCH for WIN32). 1864 */ 1865 public void 1866 check_winch() 1867 { 1868 #if MSDOS_COMPILER==WIN32C 1869 CONSOLE_SCREEN_BUFFER_INFO scr; 1870 COORD size; 1871 1872 if (con_out == INVALID_HANDLE_VALUE) 1873 return; 1874 1875 flush(); 1876 GetConsoleScreenBufferInfo(con_out, &scr); 1877 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1; 1878 size.X = scr.srWindow.Right - scr.srWindow.Left + 1; 1879 if (size.Y != sc_height || size.X != sc_width) 1880 { 1881 sc_height = size.Y; 1882 sc_width = size.X; 1883 if (!no_init && con_out_ours == con_out) 1884 SetConsoleScreenBufferSize(con_out, size); 1885 pos_init(); 1886 wscroll = (sc_height + 1) / 2; 1887 screen_trashed = 1; 1888 } 1889 #endif 1890 } 1891 1892 /* 1893 * Goto a specific line on the screen. 1894 */ 1895 public void 1896 goto_line(slinenum) 1897 int slinenum; 1898 { 1899 #if !MSDOS_COMPILER 1900 tputs(tgoto(sc_move, 0, slinenum), 1, putchr); 1901 #else 1902 flush(); 1903 _settextposition(slinenum+1, 1); 1904 #endif 1905 } 1906 1907 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC 1908 /* 1909 * Create an alternate screen which is all white. 1910 * This screen is used to create a "flash" effect, by displaying it 1911 * briefly and then switching back to the normal screen. 1912 * {{ Yuck! There must be a better way to get a visual bell. }} 1913 */ 1914 static void 1915 create_flash() 1916 { 1917 #if MSDOS_COMPILER==MSOFTC 1918 struct videoconfig w; 1919 char *blanks; 1920 int row, col; 1921 1922 _getvideoconfig(&w); 1923 videopages = w.numvideopages; 1924 if (videopages < 2) 1925 { 1926 at_enter(AT_STANDOUT); 1927 at_exit(); 1928 } else 1929 { 1930 _setactivepage(1); 1931 at_enter(AT_STANDOUT); 1932 blanks = (char *) ecalloc(w.numtextcols, sizeof(char)); 1933 for (col = 0; col < w.numtextcols; col++) 1934 blanks[col] = ' '; 1935 for (row = w.numtextrows; row > 0; row--) 1936 _outmem(blanks, w.numtextcols); 1937 _setactivepage(0); 1938 _setvisualpage(0); 1939 free(blanks); 1940 at_exit(); 1941 } 1942 #else 1943 #if MSDOS_COMPILER==BORLANDC 1944 register int n; 1945 1946 whitescreen = (unsigned short *) 1947 malloc(sc_width * sc_height * sizeof(short)); 1948 if (whitescreen == NULL) 1949 return; 1950 for (n = 0; n < sc_width * sc_height; n++) 1951 whitescreen[n] = 0x7020; 1952 #else 1953 #if MSDOS_COMPILER==WIN32C 1954 register int n; 1955 1956 whitescreen = (WORD *) 1957 malloc(sc_height * sc_width * sizeof(WORD)); 1958 if (whitescreen == NULL) 1959 return; 1960 /* Invert the standard colors. */ 1961 for (n = 0; n < sc_width * sc_height; n++) 1962 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color); 1963 #endif 1964 #endif 1965 #endif 1966 flash_created = 1; 1967 } 1968 #endif /* MSDOS_COMPILER */ 1969 1970 /* 1971 * Output the "visual bell", if there is one. 1972 */ 1973 public void 1974 vbell() 1975 { 1976 #if !MSDOS_COMPILER 1977 if (*sc_visual_bell == '\0') 1978 return; 1979 tputs(sc_visual_bell, sc_height, putchr); 1980 #else 1981 #if MSDOS_COMPILER==DJGPPC 1982 ScreenVisualBell(); 1983 #else 1984 #if MSDOS_COMPILER==MSOFTC 1985 /* 1986 * Create a flash screen on the second video page. 1987 * Switch to that page, then switch back. 1988 */ 1989 if (!flash_created) 1990 create_flash(); 1991 if (videopages < 2) 1992 return; 1993 _setvisualpage(1); 1994 delay(100); 1995 _setvisualpage(0); 1996 #else 1997 #if MSDOS_COMPILER==BORLANDC 1998 unsigned short *currscreen; 1999 2000 /* 2001 * Get a copy of the current screen. 2002 * Display the flash screen. 2003 * Then restore the old screen. 2004 */ 2005 if (!flash_created) 2006 create_flash(); 2007 if (whitescreen == NULL) 2008 return; 2009 currscreen = (unsigned short *) 2010 malloc(sc_width * sc_height * sizeof(short)); 2011 if (currscreen == NULL) return; 2012 gettext(1, 1, sc_width, sc_height, currscreen); 2013 puttext(1, 1, sc_width, sc_height, whitescreen); 2014 delay(100); 2015 puttext(1, 1, sc_width, sc_height, currscreen); 2016 free(currscreen); 2017 #else 2018 #if MSDOS_COMPILER==WIN32C 2019 /* paint screen with an inverse color */ 2020 clear(); 2021 2022 /* leave it displayed for 100 msec. */ 2023 Sleep(100); 2024 2025 /* restore with a redraw */ 2026 repaint(); 2027 #endif 2028 #endif 2029 #endif 2030 #endif 2031 #endif 2032 } 2033 2034 /* 2035 * Make a noise. 2036 */ 2037 static void 2038 beep() 2039 { 2040 #if !MSDOS_COMPILER 2041 putchr(CONTROL('G')); 2042 #else 2043 #if MSDOS_COMPILER==WIN32C 2044 MessageBeep(0); 2045 #else 2046 write(1, "\7", 1); 2047 #endif 2048 #endif 2049 } 2050 2051 /* 2052 * Ring the terminal bell. 2053 */ 2054 public void 2055 bell() 2056 { 2057 if (quiet == VERY_QUIET) 2058 vbell(); 2059 else 2060 beep(); 2061 } 2062 2063 /* 2064 * Clear the screen. 2065 */ 2066 public void 2067 clear() 2068 { 2069 #if !MSDOS_COMPILER 2070 tputs(sc_clear, sc_height, putchr); 2071 #else 2072 flush(); 2073 #if MSDOS_COMPILER==WIN32C 2074 win32_clear(); 2075 #else 2076 _clearscreen(_GCLEARSCREEN); 2077 #endif 2078 #endif 2079 } 2080 2081 /* 2082 * Clear from the cursor to the end of the cursor's line. 2083 * {{ This must not move the cursor. }} 2084 */ 2085 public void 2086 clear_eol() 2087 { 2088 #if !MSDOS_COMPILER 2089 tputs(sc_eol_clear, 1, putchr); 2090 #else 2091 #if MSDOS_COMPILER==MSOFTC 2092 short top, left; 2093 short bot, right; 2094 struct rccoord tpos; 2095 2096 flush(); 2097 /* 2098 * Save current state. 2099 */ 2100 tpos = _gettextposition(); 2101 _gettextwindow(&top, &left, &bot, &right); 2102 /* 2103 * Set a temporary window to the current line, 2104 * from the cursor's position to the right edge of the screen. 2105 * Then clear that window. 2106 */ 2107 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width); 2108 _clearscreen(_GWINDOW); 2109 /* 2110 * Restore state. 2111 */ 2112 _settextwindow(top, left, bot, right); 2113 _settextposition(tpos.row, tpos.col); 2114 #else 2115 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 2116 flush(); 2117 clreol(); 2118 #else 2119 #if MSDOS_COMPILER==WIN32C 2120 DWORD nchars; 2121 COORD cpos; 2122 CONSOLE_SCREEN_BUFFER_INFO scr; 2123 2124 flush(); 2125 memset(&scr, 0, sizeof(scr)); 2126 GetConsoleScreenBufferInfo(con_out, &scr); 2127 cpos.X = scr.dwCursorPosition.X; 2128 cpos.Y = scr.dwCursorPosition.Y; 2129 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 2130 FillConsoleOutputAttribute(con_out, curr_attr, 2131 scr.dwSize.X - cpos.X, cpos, &nchars); 2132 FillConsoleOutputCharacter(con_out, ' ', 2133 scr.dwSize.X - cpos.X, cpos, &nchars); 2134 #endif 2135 #endif 2136 #endif 2137 #endif 2138 } 2139 2140 /* 2141 * Clear the current line. 2142 * Clear the screen if there's off-screen memory below the display. 2143 */ 2144 static void 2145 clear_eol_bot() 2146 { 2147 #if MSDOS_COMPILER 2148 clear_eol(); 2149 #else 2150 if (below_mem) 2151 tputs(sc_eos_clear, 1, putchr); 2152 else 2153 tputs(sc_eol_clear, 1, putchr); 2154 #endif 2155 } 2156 2157 /* 2158 * Clear the bottom line of the display. 2159 * Leave the cursor at the beginning of the bottom line. 2160 */ 2161 public void 2162 clear_bot() 2163 { 2164 /* 2165 * If we're in a non-normal attribute mode, temporarily exit 2166 * the mode while we do the clear. Some terminals fill the 2167 * cleared area with the current attribute. 2168 */ 2169 if (oldbot) 2170 lower_left(); 2171 else 2172 line_left(); 2173 2174 if (attrmode == AT_NORMAL) 2175 clear_eol_bot(); 2176 else 2177 { 2178 int saved_attrmode = attrmode; 2179 2180 at_exit(); 2181 clear_eol_bot(); 2182 at_enter(saved_attrmode); 2183 } 2184 } 2185 2186 public void 2187 at_enter(attr) 2188 int attr; 2189 { 2190 attr = apply_at_specials(attr); 2191 2192 #if !MSDOS_COMPILER 2193 /* The one with the most priority is last. */ 2194 if (attr & AT_UNDERLINE) 2195 tputs(sc_u_in, 1, putchr); 2196 if (attr & AT_BOLD) 2197 tputs(sc_b_in, 1, putchr); 2198 if (attr & AT_BLINK) 2199 tputs(sc_bl_in, 1, putchr); 2200 if (attr & AT_STANDOUT) 2201 tputs(sc_s_in, 1, putchr); 2202 #else 2203 flush(); 2204 /* The one with the most priority is first. */ 2205 if (attr & AT_STANDOUT) 2206 { 2207 SETCOLORS(so_fg_color, so_bg_color); 2208 } else if (attr & AT_BLINK) 2209 { 2210 SETCOLORS(bl_fg_color, bl_bg_color); 2211 } 2212 else if (attr & AT_BOLD) 2213 { 2214 SETCOLORS(bo_fg_color, bo_bg_color); 2215 } 2216 else if (attr & AT_UNDERLINE) 2217 { 2218 SETCOLORS(ul_fg_color, ul_bg_color); 2219 } 2220 #endif 2221 2222 attrmode = attr; 2223 } 2224 2225 public void 2226 at_exit() 2227 { 2228 #if !MSDOS_COMPILER 2229 /* Undo things in the reverse order we did them. */ 2230 if (attrmode & AT_STANDOUT) 2231 tputs(sc_s_out, 1, putchr); 2232 if (attrmode & AT_BLINK) 2233 tputs(sc_bl_out, 1, putchr); 2234 if (attrmode & AT_BOLD) 2235 tputs(sc_b_out, 1, putchr); 2236 if (attrmode & AT_UNDERLINE) 2237 tputs(sc_u_out, 1, putchr); 2238 #else 2239 flush(); 2240 SETCOLORS(nm_fg_color, nm_bg_color); 2241 #endif 2242 2243 attrmode = AT_NORMAL; 2244 } 2245 2246 public void 2247 at_switch(attr) 2248 int attr; 2249 { 2250 int new_attrmode = apply_at_specials(attr); 2251 int ignore_modes = AT_ANSI; 2252 2253 if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes)) 2254 { 2255 at_exit(); 2256 at_enter(attr); 2257 } 2258 } 2259 2260 public int 2261 is_at_equiv(attr1, attr2) 2262 int attr1; 2263 int attr2; 2264 { 2265 attr1 = apply_at_specials(attr1); 2266 attr2 = apply_at_specials(attr2); 2267 2268 return (attr1 == attr2); 2269 } 2270 2271 public int 2272 apply_at_specials(attr) 2273 int attr; 2274 { 2275 if (attr & AT_BINARY) 2276 attr |= binattr; 2277 if (attr & AT_HILITE) 2278 attr |= AT_STANDOUT; 2279 attr &= ~(AT_BINARY|AT_HILITE); 2280 2281 return attr; 2282 } 2283 2284 #if 0 /* No longer used */ 2285 /* 2286 * Erase the character to the left of the cursor 2287 * and move the cursor left. 2288 */ 2289 public void 2290 backspace() 2291 { 2292 #if !MSDOS_COMPILER 2293 /* 2294 * Erase the previous character by overstriking with a space. 2295 */ 2296 tputs(sc_backspace, 1, putchr); 2297 putchr(' '); 2298 tputs(sc_backspace, 1, putchr); 2299 #else 2300 #if MSDOS_COMPILER==MSOFTC 2301 struct rccoord tpos; 2302 2303 flush(); 2304 tpos = _gettextposition(); 2305 if (tpos.col <= 1) 2306 return; 2307 _settextposition(tpos.row, tpos.col-1); 2308 _outtext(" "); 2309 _settextposition(tpos.row, tpos.col-1); 2310 #else 2311 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 2312 cputs("\b"); 2313 #else 2314 #if MSDOS_COMPILER==WIN32C 2315 COORD cpos; 2316 DWORD cChars; 2317 CONSOLE_SCREEN_BUFFER_INFO scr; 2318 2319 flush(); 2320 GetConsoleScreenBufferInfo(con_out, &scr); 2321 cpos = scr.dwCursorPosition; 2322 if (cpos.X <= 0) 2323 return; 2324 cpos.X--; 2325 SetConsoleCursorPosition(con_out, cpos); 2326 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars); 2327 SetConsoleCursorPosition(con_out, cpos); 2328 #endif 2329 #endif 2330 #endif 2331 #endif 2332 } 2333 #endif /* 0 */ 2334 2335 /* 2336 * Output a plain backspace, without erasing the previous char. 2337 */ 2338 public void 2339 putbs() 2340 { 2341 #if !MSDOS_COMPILER 2342 tputs(sc_backspace, 1, putchr); 2343 #else 2344 int row, col; 2345 2346 flush(); 2347 { 2348 #if MSDOS_COMPILER==MSOFTC 2349 struct rccoord tpos; 2350 tpos = _gettextposition(); 2351 row = tpos.row; 2352 col = tpos.col; 2353 #else 2354 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 2355 row = wherey(); 2356 col = wherex(); 2357 #else 2358 #if MSDOS_COMPILER==WIN32C 2359 CONSOLE_SCREEN_BUFFER_INFO scr; 2360 GetConsoleScreenBufferInfo(con_out, &scr); 2361 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1; 2362 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1; 2363 #endif 2364 #endif 2365 #endif 2366 } 2367 if (col <= 1) 2368 return; 2369 _settextposition(row, col-1); 2370 #endif /* MSDOS_COMPILER */ 2371 } 2372 2373 #if MSDOS_COMPILER==WIN32C 2374 /* 2375 * Determine whether an input character is waiting to be read. 2376 */ 2377 static int 2378 win32_kbhit(tty) 2379 HANDLE tty; 2380 { 2381 INPUT_RECORD ip; 2382 DWORD read; 2383 2384 if (keyCount > 0) 2385 return (TRUE); 2386 2387 currentKey.ascii = 0; 2388 currentKey.scan = 0; 2389 2390 /* 2391 * Wait for a real key-down event, but 2392 * ignore SHIFT and CONTROL key events. 2393 */ 2394 do 2395 { 2396 PeekConsoleInput(tty, &ip, 1, &read); 2397 if (read == 0) 2398 return (FALSE); 2399 ReadConsoleInput(tty, &ip, 1, &read); 2400 } while (ip.EventType != KEY_EVENT || 2401 ip.Event.KeyEvent.bKeyDown != TRUE || 2402 ip.Event.KeyEvent.wVirtualScanCode == 0 || 2403 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT || 2404 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL || 2405 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU); 2406 2407 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar; 2408 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode; 2409 keyCount = ip.Event.KeyEvent.wRepeatCount; 2410 2411 if (ip.Event.KeyEvent.dwControlKeyState & 2412 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) 2413 { 2414 switch (currentKey.scan) 2415 { 2416 case PCK_ALT_E: /* letter 'E' */ 2417 currentKey.ascii = 0; 2418 break; 2419 } 2420 } else if (ip.Event.KeyEvent.dwControlKeyState & 2421 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) 2422 { 2423 switch (currentKey.scan) 2424 { 2425 case PCK_RIGHT: /* right arrow */ 2426 currentKey.scan = PCK_CTL_RIGHT; 2427 break; 2428 case PCK_LEFT: /* left arrow */ 2429 currentKey.scan = PCK_CTL_LEFT; 2430 break; 2431 case PCK_DELETE: /* delete */ 2432 currentKey.scan = PCK_CTL_DELETE; 2433 break; 2434 } 2435 } 2436 return (TRUE); 2437 } 2438 2439 /* 2440 * Read a character from the keyboard. 2441 */ 2442 public char 2443 WIN32getch(tty) 2444 int tty; 2445 { 2446 int ascii; 2447 2448 if (pending_scancode) 2449 { 2450 pending_scancode = 0; 2451 return ((char)(currentKey.scan & 0x00FF)); 2452 } 2453 2454 while (win32_kbhit((HANDLE)tty) == FALSE) 2455 { 2456 Sleep(20); 2457 if (ABORT_SIGS()) 2458 return ('\003'); 2459 continue; 2460 } 2461 keyCount --; 2462 ascii = currentKey.ascii; 2463 /* 2464 * On PC's, the extended keys return a 2 byte sequence beginning 2465 * with '00', so if the ascii code is 00, the next byte will be 2466 * the lsb of the scan code. 2467 */ 2468 pending_scancode = (ascii == 0x00); 2469 return ((char)ascii); 2470 } 2471 #endif 2472 2473 #if MSDOS_COMPILER 2474 /* 2475 */ 2476 public void 2477 WIN32setcolors(fg, bg) 2478 int fg; 2479 int bg; 2480 { 2481 SETCOLORS(fg, bg); 2482 } 2483 2484 /* 2485 */ 2486 public void 2487 WIN32textout(text, len) 2488 char *text; 2489 int len; 2490 { 2491 #if MSDOS_COMPILER==WIN32C 2492 DWORD written; 2493 WriteConsole(con_out, text, len, &written, NULL); 2494 #else 2495 char c = text[len]; 2496 text[len] = '\0'; 2497 cputs(text); 2498 text[len] = c; 2499 #endif 2500 } 2501 #endif 2502