screen.c revision 173685
160812Sps/* $FreeBSD: head/contrib/less/screen.c 173685 2007-11-16 22:24:31Z delphij $ */ 260786Sps/* 3170259Sdelphij * Copyright (C) 1984-2007 Mark Nudelman 460786Sps * 560786Sps * You may distribute under the terms of either the GNU General Public 660786Sps * License or the Less License, as specified in the README file. 760786Sps * 860786Sps * For more information about less, or for information on how to 960786Sps * contact the author, see the README file. 1060786Sps */ 1160786Sps 1260786Sps 1360786Sps/* 1460786Sps * Routines which deal with the characteristics of the terminal. 1560786Sps * Uses termcap to be as terminal-independent as possible. 1660786Sps */ 1760786Sps 1860786Sps#include "less.h" 1960786Sps#include "cmd.h" 2060786Sps 2160786Sps#if MSDOS_COMPILER 2260786Sps#include "pckeys.h" 2360786Sps#if MSDOS_COMPILER==MSOFTC 2460786Sps#include <graph.h> 2560786Sps#else 2660786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 2760786Sps#include <conio.h> 2860786Sps#if MSDOS_COMPILER==DJGPPC 2960786Sps#include <pc.h> 3060786Spsextern int fd0; 3160786Sps#endif 3260786Sps#else 3360786Sps#if MSDOS_COMPILER==WIN32C 3460786Sps#include <windows.h> 3560786Sps#endif 3660786Sps#endif 3760786Sps#endif 3860786Sps#include <time.h> 3960786Sps 4060786Sps#else 4160786Sps 42161478Sdelphij#if HAVE_SYS_IOCTL_H 43161478Sdelphij#include <sys/ioctl.h> 44161478Sdelphij#endif 45161478Sdelphij 4660786Sps#if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS 4760786Sps#include <termios.h> 4860786Sps#else 4960786Sps#if HAVE_TERMIO_H 5060786Sps#include <termio.h> 5160786Sps#else 5260786Sps#if HAVE_SGSTAT_H 5360786Sps#include <sgstat.h> 5460786Sps#else 5560786Sps#include <sgtty.h> 5660786Sps#endif 5760786Sps#endif 5860786Sps#endif 5960786Sps 6060786Sps#if HAVE_TERMCAP_H 6160786Sps#include <termcap.h> 6260786Sps#endif 6360786Sps#ifdef _OSK 6460786Sps#include <signal.h> 6560786Sps#endif 6660786Sps#if OS2 6760786Sps#include <sys/signal.h> 6889022Sps#include "pckeys.h" 6960786Sps#endif 7060786Sps#if HAVE_SYS_STREAM_H 7160786Sps#include <sys/stream.h> 7260786Sps#endif 7360786Sps#if HAVE_SYS_PTEM_H 7460786Sps#include <sys/ptem.h> 7560786Sps#endif 7660786Sps 7760786Sps#endif /* MSDOS_COMPILER */ 7860786Sps 7960786Sps/* 8060786Sps * Check for broken termios package that forces you to manually 8160786Sps * set the line discipline. 8260786Sps */ 8360786Sps#ifdef __ultrix__ 8460786Sps#define MUST_SET_LINE_DISCIPLINE 1 8560786Sps#else 8660786Sps#define MUST_SET_LINE_DISCIPLINE 0 8760786Sps#endif 8860786Sps 8960786Sps#if OS2 9060786Sps#define DEFAULT_TERM "ansi" 9189022Spsstatic char *windowid; 9260786Sps#else 9360786Sps#define DEFAULT_TERM "unknown" 9460786Sps#endif 9560786Sps 9660786Sps#if MSDOS_COMPILER==MSOFTC 9760786Spsstatic int videopages; 9860786Spsstatic long msec_loops; 9960786Spsstatic int flash_created = 0; 10060786Sps#define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); } 10160786Sps#endif 10260786Sps 10360786Sps#if MSDOS_COMPILER==BORLANDC 10460786Spsstatic unsigned short *whitescreen; 10560786Spsstatic int flash_created = 0; 10660786Sps#endif 10760786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 10860786Sps#define _settextposition(y,x) gotoxy(x,y) 10960786Sps#define _clearscreen(m) clrscr() 11060786Sps#define _outtext(s) cputs(s) 11160786Sps#define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); } 11260786Spsextern int sc_height; 11360786Sps#endif 11460786Sps 11560786Sps#if MSDOS_COMPILER==WIN32C 11660786Spsstruct keyRecord 11760786Sps{ 11860786Sps int ascii; 11960786Sps int scan; 12060786Sps} currentKey; 12160786Sps 12260786Spsstatic int keyCount = 0; 12360786Spsstatic WORD curr_attr; 12460786Spsstatic int pending_scancode = 0; 12560786Spsstatic WORD *whitescreen; 12660786Sps 12760786Spsstatic HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */ 12860786Spsstatic HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */ 12960786SpsHANDLE con_out = INVALID_HANDLE_VALUE; /* current console */ 13060786Sps 13160786Spsextern int quitting; 13260786Spsstatic void win32_init_term(); 13360786Spsstatic void win32_deinit_term(); 13460786Sps 13560786Sps#define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY) 13660786Sps#define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY) 13760786Sps#define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4))) 13860786Sps#define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \ 13960786Sps if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \ 14060786Sps error("SETCOLORS failed"); } 14160786Sps#endif 14260786Sps 14360786Sps#if MSDOS_COMPILER 14460786Spspublic int nm_fg_color; /* Color of normal text */ 14560786Spspublic int nm_bg_color; 14660786Spspublic int bo_fg_color; /* Color of bold text */ 14760786Spspublic int bo_bg_color; 14860786Spspublic int ul_fg_color; /* Color of underlined text */ 14960786Spspublic int ul_bg_color; 15060786Spspublic int so_fg_color; /* Color of standout text */ 15160786Spspublic int so_bg_color; 15260786Spspublic int bl_fg_color; /* Color of blinking text */ 15360786Spspublic int bl_bg_color; 15460786Spsstatic int sy_fg_color; /* Color of system text (before less) */ 15560786Spsstatic int sy_bg_color; 15660786Sps 15760786Sps#else 15860786Sps 15960786Sps/* 16060786Sps * Strings passed to tputs() to do various terminal functions. 16160786Sps */ 16260786Spsstatic char 16360786Sps *sc_pad, /* Pad string */ 16460786Sps *sc_home, /* Cursor home */ 16560786Sps *sc_addline, /* Add line, scroll down following lines */ 16660786Sps *sc_lower_left, /* Cursor to last line, first column */ 167170259Sdelphij *sc_return, /* Cursor to beginning of current line */ 16860786Sps *sc_move, /* General cursor positioning */ 16960786Sps *sc_clear, /* Clear screen */ 17060786Sps *sc_eol_clear, /* Clear to end of line */ 17160786Sps *sc_eos_clear, /* Clear to end of screen */ 17260786Sps *sc_s_in, /* Enter standout (highlighted) mode */ 17360786Sps *sc_s_out, /* Exit standout mode */ 17460786Sps *sc_u_in, /* Enter underline mode */ 17560786Sps *sc_u_out, /* Exit underline mode */ 17660786Sps *sc_b_in, /* Enter bold mode */ 17760786Sps *sc_b_out, /* Exit bold mode */ 17860786Sps *sc_bl_in, /* Enter blink mode */ 17960786Sps *sc_bl_out, /* Exit blink mode */ 18060786Sps *sc_visual_bell, /* Visual bell (flash screen) sequence */ 18160786Sps *sc_backspace, /* Backspace cursor */ 18260786Sps *sc_s_keypad, /* Start keypad mode */ 18360786Sps *sc_e_keypad, /* End keypad mode */ 18460786Sps *sc_init, /* Startup terminal initialization */ 18560786Sps *sc_deinit; /* Exit terminal de-initialization */ 18660786Sps#endif 18760786Sps 18860786Spsstatic int init_done = 0; 18960786Sps 19060786Spspublic int auto_wrap; /* Terminal does \r\n when write past margin */ 19160786Spspublic int ignaw; /* Terminal ignores \n immediately after wrap */ 192161478Sdelphijpublic int erase_char; /* The user's erase char */ 193161478Sdelphijpublic int erase2_char; /* The user's other erase char */ 194161478Sdelphijpublic int kill_char; /* The user's line-kill char */ 19560786Spspublic int werase_char; /* The user's word-erase char */ 19660786Spspublic int sc_width, sc_height; /* Height & width of screen */ 19760786Spspublic int bo_s_width, bo_e_width; /* Printing width of boldface seq */ 19860786Spspublic int ul_s_width, ul_e_width; /* Printing width of underline seq */ 19960786Spspublic int so_s_width, so_e_width; /* Printing width of standout seq */ 20060786Spspublic int bl_s_width, bl_e_width; /* Printing width of blink seq */ 20160786Spspublic int above_mem, below_mem; /* Memory retained above/below screen */ 20260786Spspublic int can_goto_line; /* Can move cursor to any line */ 20360786Spspublic int clear_bg; /* Clear fills with background color */ 20460786Spspublic int missing_cap = 0; /* Some capability is missing */ 20560786Sps 20660786Spsstatic int attrmode = AT_NORMAL; 207161478Sdelphijextern int binattr; 20860786Sps 20960786Sps#if !MSDOS_COMPILER 21060786Spsstatic char *cheaper(); 21160786Spsstatic void tmodes(); 21260786Sps#endif 21360786Sps 21460786Sps/* 21560786Sps * These two variables are sometimes defined in, 21660786Sps * and needed by, the termcap library. 21760786Sps */ 21860786Sps#if MUST_DEFINE_OSPEED 21960786Spsextern short ospeed; /* Terminal output baud rate */ 22060786Spsextern char PC; /* Pad character */ 22160786Sps#endif 22260786Sps#ifdef _OSK 22360786Spsshort ospeed; 22460786Spschar PC_, *UP, *BC; 22560786Sps#endif 22660786Sps 22760786Spsextern int quiet; /* If VERY_QUIET, use visual bell for bell */ 22860786Spsextern int no_back_scroll; 22960786Spsextern int swindow; 23060786Spsextern int no_init; 23189022Spsextern int no_keypad; 23260786Spsextern int sigs; 23360786Spsextern int wscroll; 23460786Spsextern int screen_trashed; 235128348Stjrextern int tty; 236161478Sdelphijextern int top_scroll; 237170259Sdelphijextern int oldbot; 23860786Sps#if HILITE_SEARCH 23960786Spsextern int hilite_search; 24060786Sps#endif 24160786Sps 24260786Spsextern char *tgetstr(); 24360786Spsextern char *tgoto(); 24460786Sps 24560786Sps 24660786Sps/* 24760786Sps * Change terminal to "raw mode", or restore to "normal" mode. 24860786Sps * "Raw mode" means 24960786Sps * 1. An outstanding read will complete on receipt of a single keystroke. 25060786Sps * 2. Input is not echoed. 25160786Sps * 3. On output, \n is mapped to \r\n. 25260786Sps * 4. \t is NOT expanded into spaces. 25360786Sps * 5. Signal-causing characters such as ctrl-C (interrupt), 25460786Sps * etc. are NOT disabled. 25560786Sps * It doesn't matter whether an input \n is mapped to \r, or vice versa. 25660786Sps */ 25760786Sps public void 25860786Spsraw_mode(on) 25960786Sps int on; 26060786Sps{ 26160786Sps static int curr_on = 0; 26260786Sps 26360786Sps if (on == curr_on) 26460786Sps return; 265161478Sdelphij erase2_char = '\b'; /* in case OS doesn't know about erase2 */ 26660786Sps#if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS 26760786Sps { 26860786Sps struct termios s; 26960786Sps static struct termios save_term; 27060786Sps static int saved_term = 0; 27160786Sps 27260786Sps if (on) 27360786Sps { 27460786Sps /* 27560786Sps * Get terminal modes. 27660786Sps */ 277128348Stjr tcgetattr(tty, &s); 27860786Sps 27960786Sps /* 28060786Sps * Save modes and set certain variables dependent on modes. 28160786Sps */ 28260786Sps if (!saved_term) 28360786Sps { 28460786Sps save_term = s; 28560786Sps saved_term = 1; 28660786Sps } 28760786Sps#if HAVE_OSPEED 28860786Sps switch (cfgetospeed(&s)) 28960786Sps { 29060786Sps#ifdef B0 29160786Sps case B0: ospeed = 0; break; 29260786Sps#endif 29360786Sps#ifdef B50 29460786Sps case B50: ospeed = 1; break; 29560786Sps#endif 29660786Sps#ifdef B75 29760786Sps case B75: ospeed = 2; break; 29860786Sps#endif 29960786Sps#ifdef B110 30060786Sps case B110: ospeed = 3; break; 30160786Sps#endif 30260786Sps#ifdef B134 30360786Sps case B134: ospeed = 4; break; 30460786Sps#endif 30560786Sps#ifdef B150 30660786Sps case B150: ospeed = 5; break; 30760786Sps#endif 30860786Sps#ifdef B200 30960786Sps case B200: ospeed = 6; break; 31060786Sps#endif 31160786Sps#ifdef B300 31260786Sps case B300: ospeed = 7; break; 31360786Sps#endif 31460786Sps#ifdef B600 31560786Sps case B600: ospeed = 8; break; 31660786Sps#endif 31760786Sps#ifdef B1200 31860786Sps case B1200: ospeed = 9; break; 31960786Sps#endif 32060786Sps#ifdef B1800 32160786Sps case B1800: ospeed = 10; break; 32260786Sps#endif 32360786Sps#ifdef B2400 32460786Sps case B2400: ospeed = 11; break; 32560786Sps#endif 32660786Sps#ifdef B4800 32760786Sps case B4800: ospeed = 12; break; 32860786Sps#endif 32960786Sps#ifdef B9600 33060786Sps case B9600: ospeed = 13; break; 33160786Sps#endif 33260786Sps#ifdef EXTA 33360786Sps case EXTA: ospeed = 14; break; 33460786Sps#endif 33560786Sps#ifdef EXTB 33660786Sps case EXTB: ospeed = 15; break; 33760786Sps#endif 33860786Sps#ifdef B57600 33960786Sps case B57600: ospeed = 16; break; 34060786Sps#endif 34160786Sps#ifdef B115200 34260786Sps case B115200: ospeed = 17; break; 34360786Sps#endif 34460786Sps default: ; 34560786Sps } 34660786Sps#endif 34760786Sps erase_char = s.c_cc[VERASE]; 348161478Sdelphij#ifdef VERASE2 349161478Sdelphij erase2_char = s.c_cc[VERASE2]; 350161478Sdelphij#endif 35160786Sps kill_char = s.c_cc[VKILL]; 35260786Sps#ifdef VWERASE 35360786Sps werase_char = s.c_cc[VWERASE]; 35460786Sps#else 35560786Sps werase_char = CONTROL('W'); 35660786Sps#endif 35760786Sps 35860786Sps /* 35960786Sps * Set the modes to the way we want them. 36060786Sps */ 36160786Sps s.c_lflag &= ~(0 36260786Sps#ifdef ICANON 36360786Sps | ICANON 36460786Sps#endif 36560786Sps#ifdef ECHO 36660786Sps | ECHO 36760786Sps#endif 36860786Sps#ifdef ECHOE 36960786Sps | ECHOE 37060786Sps#endif 37160786Sps#ifdef ECHOK 37260786Sps | ECHOK 37360786Sps#endif 37460786Sps#if ECHONL 37560786Sps | ECHONL 37660786Sps#endif 37760786Sps ); 37860786Sps 37960786Sps s.c_oflag |= (0 38060786Sps#ifdef OXTABS 38160786Sps | OXTABS 38260786Sps#else 38360786Sps#ifdef TAB3 38460786Sps | TAB3 38560786Sps#else 38660786Sps#ifdef XTABS 38760786Sps | XTABS 38860786Sps#endif 38960786Sps#endif 39060786Sps#endif 39160786Sps#ifdef OPOST 39260786Sps | OPOST 39360786Sps#endif 39460786Sps#ifdef ONLCR 39560786Sps | ONLCR 39660786Sps#endif 39760786Sps ); 39860786Sps 39960786Sps s.c_oflag &= ~(0 40060786Sps#ifdef ONOEOT 40160786Sps | ONOEOT 40260786Sps#endif 40360786Sps#ifdef OCRNL 40460786Sps | OCRNL 40560786Sps#endif 40660786Sps#ifdef ONOCR 40760786Sps | ONOCR 40860786Sps#endif 40960786Sps#ifdef ONLRET 41060786Sps | ONLRET 41160786Sps#endif 41260786Sps ); 41360786Sps s.c_cc[VMIN] = 1; 41460786Sps s.c_cc[VTIME] = 0; 41560786Sps#ifdef VLNEXT 41660786Sps s.c_cc[VLNEXT] = 0; 41760786Sps#endif 41860786Sps#ifdef VDSUSP 41960786Sps s.c_cc[VDSUSP] = 0; 42060786Sps#endif 42160786Sps#if MUST_SET_LINE_DISCIPLINE 42260786Sps /* 42360786Sps * System's termios is broken; need to explicitly 42460786Sps * request TERMIODISC line discipline. 42560786Sps */ 42660786Sps s.c_line = TERMIODISC; 42760786Sps#endif 42860786Sps } else 42960786Sps { 43060786Sps /* 43160786Sps * Restore saved modes. 43260786Sps */ 43360786Sps s = save_term; 43460786Sps } 43589022Sps#if HAVE_FSYNC 436128348Stjr fsync(tty); 43789022Sps#endif 438128348Stjr tcsetattr(tty, TCSADRAIN, &s); 43960786Sps#if MUST_SET_LINE_DISCIPLINE 44060786Sps if (!on) 44160786Sps { 44260786Sps /* 44360786Sps * Broken termios *ignores* any line discipline 44460786Sps * except TERMIODISC. A different old line discipline 44560786Sps * is therefore not restored, yet. Restore the old 44660786Sps * line discipline by hand. 44760786Sps */ 448128348Stjr ioctl(tty, TIOCSETD, &save_term.c_line); 44960786Sps } 45060786Sps#endif 45160786Sps } 45260786Sps#else 45360786Sps#ifdef TCGETA 45460786Sps { 45560786Sps struct termio s; 45660786Sps static struct termio save_term; 45760786Sps static int saved_term = 0; 45860786Sps 45960786Sps if (on) 46060786Sps { 46160786Sps /* 46260786Sps * Get terminal modes. 46360786Sps */ 464128348Stjr ioctl(tty, TCGETA, &s); 46560786Sps 46660786Sps /* 46760786Sps * Save modes and set certain variables dependent on modes. 46860786Sps */ 46960786Sps if (!saved_term) 47060786Sps { 47160786Sps save_term = s; 47260786Sps saved_term = 1; 47360786Sps } 47460786Sps#if HAVE_OSPEED 47560786Sps ospeed = s.c_cflag & CBAUD; 47660786Sps#endif 47760786Sps erase_char = s.c_cc[VERASE]; 47860786Sps kill_char = s.c_cc[VKILL]; 47960786Sps#ifdef VWERASE 48060786Sps werase_char = s.c_cc[VWERASE]; 48160786Sps#else 48260786Sps werase_char = CONTROL('W'); 48360786Sps#endif 48460786Sps 48560786Sps /* 48660786Sps * Set the modes to the way we want them. 48760786Sps */ 48860786Sps s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); 48960786Sps s.c_oflag |= (OPOST|ONLCR|TAB3); 49060786Sps s.c_oflag &= ~(OCRNL|ONOCR|ONLRET); 49160786Sps s.c_cc[VMIN] = 1; 49260786Sps s.c_cc[VTIME] = 0; 49360786Sps } else 49460786Sps { 49560786Sps /* 49660786Sps * Restore saved modes. 49760786Sps */ 49860786Sps s = save_term; 49960786Sps } 500128348Stjr ioctl(tty, TCSETAW, &s); 50160786Sps } 50260786Sps#else 50360786Sps#ifdef TIOCGETP 50460786Sps { 50560786Sps struct sgttyb s; 50660786Sps static struct sgttyb save_term; 50760786Sps static int saved_term = 0; 50860786Sps 50960786Sps if (on) 51060786Sps { 51160786Sps /* 51260786Sps * Get terminal modes. 51360786Sps */ 514128348Stjr ioctl(tty, TIOCGETP, &s); 51560786Sps 51660786Sps /* 51760786Sps * Save modes and set certain variables dependent on modes. 51860786Sps */ 51960786Sps if (!saved_term) 52060786Sps { 52160786Sps save_term = s; 52260786Sps saved_term = 1; 52360786Sps } 52460786Sps#if HAVE_OSPEED 52560786Sps ospeed = s.sg_ospeed; 52660786Sps#endif 52760786Sps erase_char = s.sg_erase; 52860786Sps kill_char = s.sg_kill; 52960786Sps werase_char = CONTROL('W'); 53060786Sps 53160786Sps /* 53260786Sps * Set the modes to the way we want them. 53360786Sps */ 53460786Sps s.sg_flags |= CBREAK; 53560786Sps s.sg_flags &= ~(ECHO|XTABS); 53660786Sps } else 53760786Sps { 53860786Sps /* 53960786Sps * Restore saved modes. 54060786Sps */ 54160786Sps s = save_term; 54260786Sps } 543128348Stjr ioctl(tty, TIOCSETN, &s); 54460786Sps } 54560786Sps#else 54660786Sps#ifdef _OSK 54760786Sps { 54860786Sps struct sgbuf s; 54960786Sps static struct sgbuf save_term; 55060786Sps static int saved_term = 0; 55160786Sps 55260786Sps if (on) 55360786Sps { 55460786Sps /* 55560786Sps * Get terminal modes. 55660786Sps */ 557128348Stjr _gs_opt(tty, &s); 55860786Sps 55960786Sps /* 56060786Sps * Save modes and set certain variables dependent on modes. 56160786Sps */ 56260786Sps if (!saved_term) 56360786Sps { 56460786Sps save_term = s; 56560786Sps saved_term = 1; 56660786Sps } 56760786Sps erase_char = s.sg_bspch; 56860786Sps kill_char = s.sg_dlnch; 56960786Sps werase_char = CONTROL('W'); 57060786Sps 57160786Sps /* 57260786Sps * Set the modes to the way we want them. 57360786Sps */ 57460786Sps s.sg_echo = 0; 57560786Sps s.sg_eofch = 0; 57660786Sps s.sg_pause = 0; 57760786Sps s.sg_psch = 0; 57860786Sps } else 57960786Sps { 58060786Sps /* 58160786Sps * Restore saved modes. 58260786Sps */ 58360786Sps s = save_term; 58460786Sps } 585128348Stjr _ss_opt(tty, &s); 58660786Sps } 58760786Sps#else 58860786Sps /* MS-DOS, Windows, or OS2 */ 58960786Sps#if OS2 59060786Sps /* OS2 */ 59160786Sps LSIGNAL(SIGINT, SIG_IGN); 59260786Sps#endif 59360786Sps erase_char = '\b'; 59460786Sps#if MSDOS_COMPILER==DJGPPC 59560786Sps kill_char = CONTROL('U'); 59660786Sps /* 59760786Sps * So that when we shell out or run another program, its 59860786Sps * stdin is in cooked mode. We do not switch stdin to binary 59960786Sps * mode if fd0 is zero, since that means we were called before 60060786Sps * tty was reopened in open_getchr, in which case we would be 60160786Sps * changing the original stdin device outside less. 60260786Sps */ 60360786Sps if (fd0 != 0) 60460786Sps setmode(0, on ? O_BINARY : O_TEXT); 60560786Sps#else 60660786Sps kill_char = ESC; 60760786Sps#endif 60860786Sps werase_char = CONTROL('W'); 60960786Sps#endif 61060786Sps#endif 61160786Sps#endif 61260786Sps#endif 61360786Sps curr_on = on; 61460786Sps} 61560786Sps 61660786Sps#if !MSDOS_COMPILER 61760786Sps/* 61860786Sps * Some glue to prevent calling termcap functions if tgetent() failed. 61960786Sps */ 62060786Spsstatic int hardcopy; 62160786Sps 62260786Sps static char * 62360786Spsltget_env(capname) 62460786Sps char *capname; 62560786Sps{ 62660786Sps char name[16]; 627170967Sdelphij char *s; 62860786Sps 629170967Sdelphij s = lgetenv("LESS_TERMCAP_DEBUG"); 630170967Sdelphij if (s != NULL && *s != '\0') 631170967Sdelphij { 632170967Sdelphij struct env { struct env *next; char *name; char *value; }; 633170967Sdelphij static struct env *envs = NULL; 634170967Sdelphij struct env *p; 635170967Sdelphij for (p = envs; p != NULL; p = p->next) 636170967Sdelphij if (strcmp(p->name, capname) == 0) 637170967Sdelphij return p->value; 638170967Sdelphij p = (struct env *) ecalloc(1, sizeof(struct env)); 639170967Sdelphij p->name = save(capname); 640170967Sdelphij p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char)); 641170967Sdelphij sprintf(p->value, "<%s>", capname); 642170967Sdelphij p->next = envs; 643170967Sdelphij envs = p; 644170967Sdelphij return p->value; 645170967Sdelphij } 64660786Sps strcpy(name, "LESS_TERMCAP_"); 64760786Sps strcat(name, capname); 64860786Sps return (lgetenv(name)); 64960786Sps} 65060786Sps 65160786Sps static int 65260786Spsltgetflag(capname) 65360786Sps char *capname; 65460786Sps{ 65560786Sps char *s; 65660786Sps 65760786Sps if ((s = ltget_env(capname)) != NULL) 65860786Sps return (*s != '\0' && *s != '0'); 65960786Sps if (hardcopy) 66060786Sps return (0); 66160786Sps return (tgetflag(capname)); 66260786Sps} 66360786Sps 66460786Sps static int 66560786Spsltgetnum(capname) 66660786Sps char *capname; 66760786Sps{ 66860786Sps char *s; 66960786Sps 67060786Sps if ((s = ltget_env(capname)) != NULL) 67160786Sps return (atoi(s)); 67260786Sps if (hardcopy) 67360786Sps return (-1); 67460786Sps return (tgetnum(capname)); 67560786Sps} 67660786Sps 67760786Sps static char * 67860786Spsltgetstr(capname, pp) 67960786Sps char *capname; 68060786Sps char **pp; 68160786Sps{ 68260786Sps char *s; 68360786Sps 68460786Sps if ((s = ltget_env(capname)) != NULL) 68560786Sps return (s); 68660786Sps if (hardcopy) 68760786Sps return (NULL); 68860786Sps return (tgetstr(capname, pp)); 68960786Sps} 69060786Sps#endif /* MSDOS_COMPILER */ 69160786Sps 69260786Sps/* 69360786Sps * Get size of the output screen. 69460786Sps */ 69560786Sps public void 69660786Spsscrsize() 69760786Sps{ 69860786Sps register char *s; 69960786Sps int sys_height; 70060786Sps int sys_width; 70160786Sps#if !MSDOS_COMPILER 70260786Sps int n; 70360786Sps#endif 70460786Sps 70560786Sps#define DEF_SC_WIDTH 80 70660786Sps#if MSDOS_COMPILER 70760786Sps#define DEF_SC_HEIGHT 25 70860786Sps#else 70960786Sps#define DEF_SC_HEIGHT 24 71060786Sps#endif 71160786Sps 71260786Sps 71360786Sps sys_width = sys_height = 0; 71460786Sps 71560786Sps#if MSDOS_COMPILER==MSOFTC 71660786Sps { 71760786Sps struct videoconfig w; 71860786Sps _getvideoconfig(&w); 71960786Sps sys_height = w.numtextrows; 72060786Sps sys_width = w.numtextcols; 72160786Sps } 72260786Sps#else 72360786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 72460786Sps { 72560786Sps struct text_info w; 72660786Sps gettextinfo(&w); 72760786Sps sys_height = w.screenheight; 72860786Sps sys_width = w.screenwidth; 72960786Sps } 73060786Sps#else 73160786Sps#if MSDOS_COMPILER==WIN32C 73260786Sps { 73360786Sps CONSOLE_SCREEN_BUFFER_INFO scr; 73460786Sps GetConsoleScreenBufferInfo(con_out, &scr); 73560786Sps sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1; 73660786Sps sys_width = scr.srWindow.Right - scr.srWindow.Left + 1; 73760786Sps } 73860786Sps#else 73960786Sps#if OS2 74060786Sps { 74160786Sps int s[2]; 74260786Sps _scrsize(s); 74360786Sps sys_width = s[0]; 74460786Sps sys_height = s[1]; 74589022Sps /* 74689022Sps * When using terminal emulators for XFree86/OS2, the 74789022Sps * _scrsize function does not work well. 74889022Sps * Call the scrsize.exe program to get the window size. 74989022Sps */ 75089022Sps windowid = getenv("WINDOWID"); 75189022Sps if (windowid != NULL) 75289022Sps { 75389022Sps FILE *fd = popen("scrsize", "rt"); 75489022Sps if (fd != NULL) 75589022Sps { 75689022Sps int w, h; 75789022Sps fscanf(fd, "%i %i", &w, &h); 75889022Sps if (w > 0 && h > 0) 75989022Sps { 76089022Sps sys_width = w; 76189022Sps sys_height = h; 76289022Sps } 76389022Sps pclose(fd); 76489022Sps } 76589022Sps } 76660786Sps } 76760786Sps#else 76860786Sps#ifdef TIOCGWINSZ 76960786Sps { 77060786Sps struct winsize w; 77160786Sps if (ioctl(2, TIOCGWINSZ, &w) == 0) 77260786Sps { 77360786Sps if (w.ws_row > 0) 77460786Sps sys_height = w.ws_row; 77560786Sps if (w.ws_col > 0) 77660786Sps sys_width = w.ws_col; 77760786Sps } 77860786Sps } 77960786Sps#else 78060786Sps#ifdef WIOCGETD 78160786Sps { 78260786Sps struct uwdata w; 78360786Sps if (ioctl(2, WIOCGETD, &w) == 0) 78460786Sps { 78560786Sps if (w.uw_height > 0) 78660786Sps sys_height = w.uw_height / w.uw_vs; 78760786Sps if (w.uw_width > 0) 78860786Sps sys_width = w.uw_width / w.uw_hs; 78960786Sps } 79060786Sps } 79160786Sps#endif 79260786Sps#endif 79360786Sps#endif 79460786Sps#endif 79560786Sps#endif 79660786Sps#endif 79760786Sps 79860786Sps if (sys_height > 0) 79960786Sps sc_height = sys_height; 80060786Sps else if ((s = lgetenv("LINES")) != NULL) 80160786Sps sc_height = atoi(s); 80260786Sps#if !MSDOS_COMPILER 80360786Sps else if ((n = ltgetnum("li")) > 0) 80460786Sps sc_height = n; 80560786Sps#endif 80660786Sps else 80760786Sps sc_height = DEF_SC_HEIGHT; 80860786Sps 80960786Sps if (sys_width > 0) 81060786Sps sc_width = sys_width; 81160786Sps else if ((s = lgetenv("COLUMNS")) != NULL) 81260786Sps sc_width = atoi(s); 81360786Sps#if !MSDOS_COMPILER 81460786Sps else if ((n = ltgetnum("co")) > 0) 81560786Sps sc_width = n; 81660786Sps#endif 81760786Sps else 81860786Sps sc_width = DEF_SC_WIDTH; 81960786Sps} 82060786Sps 82160786Sps#if MSDOS_COMPILER==MSOFTC 82260786Sps/* 82360786Sps * Figure out how many empty loops it takes to delay a millisecond. 82460786Sps */ 82560786Sps static void 82660786Spsget_clock() 82760786Sps{ 82860786Sps clock_t start; 82960786Sps 83060786Sps /* 83160786Sps * Get synchronized at the start of a tick. 83260786Sps */ 83360786Sps start = clock(); 83460786Sps while (clock() == start) 83560786Sps ; 83660786Sps /* 83760786Sps * Now count loops till the next tick. 83860786Sps */ 83960786Sps start = clock(); 84060786Sps msec_loops = 0; 84160786Sps while (clock() == start) 84260786Sps msec_loops++; 84360786Sps /* 84460786Sps * Convert from (loops per clock) to (loops per millisecond). 84560786Sps */ 84660786Sps msec_loops *= CLOCKS_PER_SEC; 84760786Sps msec_loops /= 1000; 84860786Sps} 84960786Sps 85060786Sps/* 85160786Sps * Delay for a specified number of milliseconds. 85260786Sps */ 85360786Sps static void 85460786Spsdummy_func() 85560786Sps{ 85660786Sps static long delay_dummy = 0; 85760786Sps delay_dummy++; 85860786Sps} 85960786Sps 86060786Sps static void 86160786Spsdelay(msec) 86260786Sps int msec; 86360786Sps{ 86460786Sps long i; 86560786Sps 86660786Sps while (msec-- > 0) 86760786Sps { 86860786Sps for (i = 0; i < msec_loops; i++) 86960786Sps { 87060786Sps /* 87160786Sps * Make it look like we're doing something here, 87260786Sps * so the optimizer doesn't remove the whole loop. 87360786Sps */ 87460786Sps dummy_func(); 87560786Sps } 87660786Sps } 87760786Sps} 87860786Sps#endif 87960786Sps 88060786Sps/* 88160786Sps * Return the characters actually input by a "special" key. 88260786Sps */ 88360786Sps public char * 88460786Spsspecial_key_str(key) 88560786Sps int key; 88660786Sps{ 88760786Sps static char tbuf[40]; 88860786Sps char *s; 88989022Sps#if MSDOS_COMPILER || OS2 89060786Sps static char k_right[] = { '\340', PCK_RIGHT, 0 }; 89160786Sps static char k_left[] = { '\340', PCK_LEFT, 0 }; 89260786Sps static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 }; 89360786Sps static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 }; 89460786Sps static char k_insert[] = { '\340', PCK_INSERT, 0 }; 89560786Sps static char k_delete[] = { '\340', PCK_DELETE, 0 }; 89660786Sps static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 }; 89760786Sps static char k_ctl_backspace[] = { '\177', 0 }; 89860786Sps static char k_home[] = { '\340', PCK_HOME, 0 }; 89960786Sps static char k_end[] = { '\340', PCK_END, 0 }; 90060786Sps static char k_up[] = { '\340', PCK_UP, 0 }; 90160786Sps static char k_down[] = { '\340', PCK_DOWN, 0 }; 90260786Sps static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 }; 90360786Sps static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 }; 90460786Sps static char k_pageup[] = { '\340', PCK_PAGEUP, 0 }; 90560786Sps static char k_f1[] = { '\340', PCK_F1, 0 }; 90689022Sps#endif 90789022Sps#if !MSDOS_COMPILER 90860786Sps char *sp = tbuf; 90960786Sps#endif 91060786Sps 91160786Sps switch (key) 91260786Sps { 91389022Sps#if OS2 91489022Sps /* 91589022Sps * If windowid is not NULL, assume less is executed in 91689022Sps * the XFree86 environment. 91789022Sps */ 91889022Sps case SK_RIGHT_ARROW: 91989022Sps s = windowid ? ltgetstr("kr", &sp) : k_right; 92089022Sps break; 92189022Sps case SK_LEFT_ARROW: 92289022Sps s = windowid ? ltgetstr("kl", &sp) : k_left; 92389022Sps break; 92489022Sps case SK_UP_ARROW: 92589022Sps s = windowid ? ltgetstr("ku", &sp) : k_up; 92689022Sps break; 92789022Sps case SK_DOWN_ARROW: 92889022Sps s = windowid ? ltgetstr("kd", &sp) : k_down; 92989022Sps break; 93089022Sps case SK_PAGE_UP: 93189022Sps s = windowid ? ltgetstr("kP", &sp) : k_pageup; 93289022Sps break; 93389022Sps case SK_PAGE_DOWN: 93489022Sps s = windowid ? ltgetstr("kN", &sp) : k_pagedown; 93589022Sps break; 93689022Sps case SK_HOME: 93789022Sps s = windowid ? ltgetstr("kh", &sp) : k_home; 93889022Sps break; 93989022Sps case SK_END: 94089022Sps s = windowid ? ltgetstr("@7", &sp) : k_end; 94189022Sps break; 94289022Sps case SK_DELETE: 94389022Sps if (windowid) 94489022Sps { 94589022Sps s = ltgetstr("kD", &sp); 94689022Sps if (s == NULL) 94789022Sps { 94889022Sps tbuf[0] = '\177'; 94989022Sps tbuf[1] = '\0'; 95089022Sps s = tbuf; 95189022Sps } 95289022Sps } else 95389022Sps s = k_delete; 95489022Sps break; 95589022Sps#endif 95660786Sps#if MSDOS_COMPILER 95760786Sps case SK_RIGHT_ARROW: 95860786Sps s = k_right; 95960786Sps break; 96060786Sps case SK_LEFT_ARROW: 96160786Sps s = k_left; 96260786Sps break; 96360786Sps case SK_UP_ARROW: 96460786Sps s = k_up; 96560786Sps break; 96660786Sps case SK_DOWN_ARROW: 96760786Sps s = k_down; 96860786Sps break; 96960786Sps case SK_PAGE_UP: 97060786Sps s = k_pageup; 97160786Sps break; 97260786Sps case SK_PAGE_DOWN: 97360786Sps s = k_pagedown; 97460786Sps break; 97560786Sps case SK_HOME: 97660786Sps s = k_home; 97760786Sps break; 97860786Sps case SK_END: 97960786Sps s = k_end; 98060786Sps break; 98160786Sps case SK_DELETE: 98260786Sps s = k_delete; 98360786Sps break; 98489022Sps#endif 98589022Sps#if MSDOS_COMPILER || OS2 98660786Sps case SK_INSERT: 98760786Sps s = k_insert; 98860786Sps break; 98960786Sps case SK_CTL_LEFT_ARROW: 99060786Sps s = k_ctl_left; 99160786Sps break; 99260786Sps case SK_CTL_RIGHT_ARROW: 99360786Sps s = k_ctl_right; 99460786Sps break; 99560786Sps case SK_CTL_BACKSPACE: 99660786Sps s = k_ctl_backspace; 99760786Sps break; 99860786Sps case SK_CTL_DELETE: 99960786Sps s = k_ctl_delete; 100060786Sps break; 100160786Sps case SK_F1: 100260786Sps s = k_f1; 100360786Sps break; 100460786Sps case SK_BACKTAB: 100560786Sps s = k_backtab; 100660786Sps break; 100760786Sps#else 100860786Sps case SK_RIGHT_ARROW: 100960786Sps s = ltgetstr("kr", &sp); 101060786Sps break; 101160786Sps case SK_LEFT_ARROW: 101260786Sps s = ltgetstr("kl", &sp); 101360786Sps break; 101460786Sps case SK_UP_ARROW: 101560786Sps s = ltgetstr("ku", &sp); 101660786Sps break; 101760786Sps case SK_DOWN_ARROW: 101860786Sps s = ltgetstr("kd", &sp); 101960786Sps break; 102060786Sps case SK_PAGE_UP: 102160786Sps s = ltgetstr("kP", &sp); 102260786Sps break; 102360786Sps case SK_PAGE_DOWN: 102460786Sps s = ltgetstr("kN", &sp); 102560786Sps break; 102660786Sps case SK_HOME: 102760786Sps s = ltgetstr("kh", &sp); 102860786Sps break; 102960786Sps case SK_END: 103060786Sps s = ltgetstr("@7", &sp); 103160786Sps break; 103260786Sps case SK_DELETE: 103360786Sps s = ltgetstr("kD", &sp); 103460786Sps if (s == NULL) 103560786Sps { 103660786Sps tbuf[0] = '\177'; 103760786Sps tbuf[1] = '\0'; 103860786Sps s = tbuf; 103960786Sps } 104060786Sps break; 104160786Sps#endif 104260786Sps case SK_CONTROL_K: 104360786Sps tbuf[0] = CONTROL('K'); 104460786Sps tbuf[1] = '\0'; 104560786Sps s = tbuf; 104660786Sps break; 104760786Sps default: 104860786Sps return (NULL); 104960786Sps } 105060786Sps return (s); 105160786Sps} 105260786Sps 105360786Sps/* 105460786Sps * Get terminal capabilities via termcap. 105560786Sps */ 105660786Sps public void 105760786Spsget_term() 105860786Sps{ 105960786Sps#if MSDOS_COMPILER 106060786Sps auto_wrap = 1; 106160786Sps ignaw = 0; 106260786Sps can_goto_line = 1; 106360786Sps clear_bg = 1; 106460786Sps /* 106560786Sps * Set up default colors. 106660786Sps * The xx_s_width and xx_e_width vars are already initialized to 0. 106760786Sps */ 106860786Sps#if MSDOS_COMPILER==MSOFTC 106960786Sps sy_bg_color = _getbkcolor(); 107060786Sps sy_fg_color = _gettextcolor(); 107160786Sps get_clock(); 107260786Sps#else 107360786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 107460786Sps { 107560786Sps struct text_info w; 107660786Sps gettextinfo(&w); 107760786Sps sy_bg_color = (w.attribute >> 4) & 0x0F; 107860786Sps sy_fg_color = (w.attribute >> 0) & 0x0F; 107960786Sps } 108060786Sps#else 108160786Sps#if MSDOS_COMPILER==WIN32C 108260786Sps { 108360786Sps DWORD nread; 108460786Sps CONSOLE_SCREEN_BUFFER_INFO scr; 108560786Sps 108660786Sps con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE); 108760786Sps /* 108860786Sps * Always open stdin in binary. Note this *must* be done 108960786Sps * before any file operations have been done on fd0. 109060786Sps */ 109160786Sps SET_BINARY(0); 109260786Sps GetConsoleScreenBufferInfo(con_out, &scr); 109360786Sps ReadConsoleOutputAttribute(con_out, &curr_attr, 109460786Sps 1, scr.dwCursorPosition, &nread); 109560786Sps sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */ 109660786Sps sy_fg_color = curr_attr & FG_COLORS; 109760786Sps } 109860786Sps#endif 109960786Sps#endif 110060786Sps#endif 110160786Sps nm_fg_color = sy_fg_color; 110260786Sps nm_bg_color = sy_bg_color; 110360786Sps bo_fg_color = 11; 110460786Sps bo_bg_color = 0; 110560786Sps ul_fg_color = 9; 110660786Sps ul_bg_color = 0; 110760786Sps so_fg_color = 15; 110860786Sps so_bg_color = 9; 110960786Sps bl_fg_color = 15; 111060786Sps bl_bg_color = 0; 111160786Sps 111260786Sps /* 111360786Sps * Get size of the screen. 111460786Sps */ 111560786Sps scrsize(); 111660786Sps pos_init(); 111760786Sps 111860786Sps 111960786Sps#else /* !MSDOS_COMPILER */ 112060786Sps 112160786Sps char *sp; 112260786Sps register char *t1, *t2; 112360786Sps char *term; 112460786Sps char termbuf[TERMBUF_SIZE]; 112560786Sps 112660786Sps static char sbuf[TERMSBUF_SIZE]; 112760786Sps 112860786Sps#if OS2 112960786Sps /* 113060786Sps * Make sure the termcap database is available. 113160786Sps */ 113260786Sps sp = lgetenv("TERMCAP"); 113360786Sps if (sp == NULL || *sp == '\0') 113460786Sps { 113560786Sps char *termcap; 113660786Sps if ((sp = homefile("termcap.dat")) != NULL) 113760786Sps { 113860786Sps termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char)); 113960786Sps sprintf(termcap, "TERMCAP=%s", sp); 114060786Sps free(sp); 114160786Sps putenv(termcap); 114260786Sps } 114360786Sps } 114460786Sps#endif 114560786Sps /* 114660786Sps * Find out what kind of terminal this is. 114760786Sps */ 114860786Sps if ((term = lgetenv("TERM")) == NULL) 114960786Sps term = DEFAULT_TERM; 115060786Sps hardcopy = 0; 1151170259Sdelphij if (tgetent(termbuf, term) != TGETENT_OK) 115260786Sps hardcopy = 1; 115360786Sps if (ltgetflag("hc")) 115460786Sps hardcopy = 1; 115560786Sps 115660786Sps /* 115760786Sps * Get size of the screen. 115860786Sps */ 115960786Sps scrsize(); 116060786Sps pos_init(); 116160786Sps 116260786Sps auto_wrap = ltgetflag("am"); 116360786Sps ignaw = ltgetflag("xn"); 116460786Sps above_mem = ltgetflag("da"); 116560786Sps below_mem = ltgetflag("db"); 116660786Sps clear_bg = ltgetflag("ut"); 116760786Sps 116860786Sps /* 116960786Sps * Assumes termcap variable "sg" is the printing width of: 117060786Sps * the standout sequence, the end standout sequence, 117160786Sps * the underline sequence, the end underline sequence, 117260786Sps * the boldface sequence, and the end boldface sequence. 117360786Sps */ 117460786Sps if ((so_s_width = ltgetnum("sg")) < 0) 117560786Sps so_s_width = 0; 117660786Sps so_e_width = so_s_width; 117760786Sps 117860786Sps bo_s_width = bo_e_width = so_s_width; 117960786Sps ul_s_width = ul_e_width = so_s_width; 118060786Sps bl_s_width = bl_e_width = so_s_width; 118160786Sps 118260786Sps#if HILITE_SEARCH 118360786Sps if (so_s_width > 0 || so_e_width > 0) 118460786Sps /* 118560786Sps * Disable highlighting by default on magic cookie terminals. 118660786Sps * Turning on highlighting might change the displayed width 118760786Sps * of a line, causing the display to get messed up. 118860786Sps * The user can turn it back on with -g, 118960786Sps * but she won't like the results. 119060786Sps */ 119160786Sps hilite_search = 0; 119260786Sps#endif 119360786Sps 119460786Sps /* 119560786Sps * Get various string-valued capabilities. 119660786Sps */ 119760786Sps sp = sbuf; 119860786Sps 119960786Sps#if HAVE_OSPEED 120060786Sps sc_pad = ltgetstr("pc", &sp); 120160786Sps if (sc_pad != NULL) 120260786Sps PC = *sc_pad; 120360786Sps#endif 120460786Sps 120560786Sps sc_s_keypad = ltgetstr("ks", &sp); 120660786Sps if (sc_s_keypad == NULL) 120760786Sps sc_s_keypad = ""; 120860786Sps sc_e_keypad = ltgetstr("ke", &sp); 120960786Sps if (sc_e_keypad == NULL) 121060786Sps sc_e_keypad = ""; 121160786Sps 1212171009Sdelphij sc_init = ltgetstr("ti", &sp); 121360786Sps if (sc_init == NULL) 121460786Sps sc_init = ""; 121560786Sps 1216171009Sdelphij sc_deinit= ltgetstr("te", &sp); 121760786Sps if (sc_deinit == NULL) 121860786Sps sc_deinit = ""; 121960786Sps 122060786Sps sc_eol_clear = ltgetstr("ce", &sp); 122160786Sps if (sc_eol_clear == NULL || *sc_eol_clear == '\0') 122260786Sps { 122360786Sps missing_cap = 1; 122460786Sps sc_eol_clear = ""; 122560786Sps } 122660786Sps 122760786Sps sc_eos_clear = ltgetstr("cd", &sp); 122860786Sps if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0')) 122960786Sps { 123060786Sps missing_cap = 1; 1231161478Sdelphij sc_eos_clear = ""; 123260786Sps } 123360786Sps 123460786Sps sc_clear = ltgetstr("cl", &sp); 123560786Sps if (sc_clear == NULL || *sc_clear == '\0') 123660786Sps { 123760786Sps missing_cap = 1; 123860786Sps sc_clear = "\n\n"; 123960786Sps } 124060786Sps 124160786Sps sc_move = ltgetstr("cm", &sp); 124260786Sps if (sc_move == NULL || *sc_move == '\0') 124360786Sps { 124460786Sps /* 124560786Sps * This is not an error here, because we don't 124660786Sps * always need sc_move. 124760786Sps * We need it only if we don't have home or lower-left. 124860786Sps */ 124960786Sps sc_move = ""; 125060786Sps can_goto_line = 0; 125160786Sps } else 125260786Sps can_goto_line = 1; 125360786Sps 125460786Sps tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp); 125560786Sps tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp); 125660786Sps tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp); 125760786Sps tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp); 125860786Sps 125960786Sps sc_visual_bell = ltgetstr("vb", &sp); 126060786Sps if (sc_visual_bell == NULL) 126160786Sps sc_visual_bell = ""; 126260786Sps 126360786Sps if (ltgetflag("bs")) 126460786Sps sc_backspace = "\b"; 126560786Sps else 126660786Sps { 126760786Sps sc_backspace = ltgetstr("bc", &sp); 126860786Sps if (sc_backspace == NULL || *sc_backspace == '\0') 126960786Sps sc_backspace = "\b"; 127060786Sps } 127160786Sps 127260786Sps /* 127360786Sps * Choose between using "ho" and "cm" ("home" and "cursor move") 127460786Sps * to move the cursor to the upper left corner of the screen. 127560786Sps */ 127660786Sps t1 = ltgetstr("ho", &sp); 127760786Sps if (t1 == NULL) 127860786Sps t1 = ""; 127960786Sps if (*sc_move == '\0') 128060786Sps t2 = ""; 128160786Sps else 128260786Sps { 128360786Sps strcpy(sp, tgoto(sc_move, 0, 0)); 128460786Sps t2 = sp; 128560786Sps sp += strlen(sp) + 1; 128660786Sps } 128760786Sps sc_home = cheaper(t1, t2, "|\b^"); 128860786Sps 128960786Sps /* 129060786Sps * Choose between using "ll" and "cm" ("lower left" and "cursor move") 129160786Sps * to move the cursor to the lower left corner of the screen. 129260786Sps */ 129360786Sps t1 = ltgetstr("ll", &sp); 129460786Sps if (t1 == NULL) 129560786Sps t1 = ""; 129660786Sps if (*sc_move == '\0') 129760786Sps t2 = ""; 129860786Sps else 129960786Sps { 130060786Sps strcpy(sp, tgoto(sc_move, 0, sc_height-1)); 130160786Sps t2 = sp; 130260786Sps sp += strlen(sp) + 1; 130360786Sps } 130460786Sps sc_lower_left = cheaper(t1, t2, "\r"); 130560786Sps 130660786Sps /* 1307170259Sdelphij * Get carriage return string. 1308170259Sdelphij */ 1309170259Sdelphij sc_return = ltgetstr("cr", &sp); 1310170259Sdelphij if (sc_return == NULL) 1311170259Sdelphij sc_return = "\r"; 1312170259Sdelphij 1313170259Sdelphij /* 131460786Sps * Choose between using "al" or "sr" ("add line" or "scroll reverse") 131560786Sps * to add a line at the top of the screen. 131660786Sps */ 131760786Sps t1 = ltgetstr("al", &sp); 131860786Sps if (t1 == NULL) 131960786Sps t1 = ""; 132060786Sps t2 = ltgetstr("sr", &sp); 132160786Sps if (t2 == NULL) 132260786Sps t2 = ""; 132360786Sps#if OS2 132460786Sps if (*t1 == '\0' && *t2 == '\0') 132560786Sps sc_addline = ""; 132660786Sps else 132760786Sps#endif 132860786Sps if (above_mem) 132960786Sps sc_addline = t1; 133060786Sps else 133160786Sps sc_addline = cheaper(t1, t2, ""); 133260786Sps if (*sc_addline == '\0') 133360786Sps { 133460786Sps /* 133560786Sps * Force repaint on any backward movement. 133660786Sps */ 133760786Sps no_back_scroll = 1; 133860786Sps } 133960786Sps#endif /* MSDOS_COMPILER */ 134060786Sps} 134160786Sps 134260786Sps#if !MSDOS_COMPILER 134360786Sps/* 134460786Sps * Return the cost of displaying a termcap string. 134560786Sps * We use the trick of calling tputs, but as a char printing function 134660786Sps * we give it inc_costcount, which just increments "costcount". 134760786Sps * This tells us how many chars would be printed by using this string. 134860786Sps * {{ Couldn't we just use strlen? }} 134960786Sps */ 135060786Spsstatic int costcount; 135160786Sps 135260786Sps/*ARGSUSED*/ 135360786Sps static int 135460786Spsinc_costcount(c) 135560786Sps int c; 135660786Sps{ 135760786Sps costcount++; 135860786Sps return (c); 135960786Sps} 136060786Sps 136160786Sps static int 136260786Spscost(t) 136360786Sps char *t; 136460786Sps{ 136560786Sps costcount = 0; 136660786Sps tputs(t, sc_height, inc_costcount); 136760786Sps return (costcount); 136860786Sps} 136960786Sps 137060786Sps/* 137160786Sps * Return the "best" of the two given termcap strings. 137260786Sps * The best, if both exist, is the one with the lower 137360786Sps * cost (see cost() function). 137460786Sps */ 137560786Sps static char * 137660786Spscheaper(t1, t2, def) 137760786Sps char *t1, *t2; 137860786Sps char *def; 137960786Sps{ 138060786Sps if (*t1 == '\0' && *t2 == '\0') 138160786Sps { 138260786Sps missing_cap = 1; 138360786Sps return (def); 138460786Sps } 138560786Sps if (*t1 == '\0') 138660786Sps return (t2); 138760786Sps if (*t2 == '\0') 138860786Sps return (t1); 138960786Sps if (cost(t1) < cost(t2)) 139060786Sps return (t1); 139160786Sps return (t2); 139260786Sps} 139360786Sps 139460786Sps static void 139560786Spstmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp) 139660786Sps char *incap; 139760786Sps char *outcap; 139860786Sps char **instr; 139960786Sps char **outstr; 140060786Sps char *def_instr; 140160786Sps char *def_outstr; 140260786Sps char **spp; 140360786Sps{ 140460786Sps *instr = ltgetstr(incap, spp); 140560786Sps if (*instr == NULL) 140660786Sps { 140760786Sps /* Use defaults. */ 140860786Sps *instr = def_instr; 140960786Sps *outstr = def_outstr; 141060786Sps return; 141160786Sps } 141260786Sps 141360786Sps *outstr = ltgetstr(outcap, spp); 141460786Sps if (*outstr == NULL) 141560786Sps /* No specific out capability; use "me". */ 141660786Sps *outstr = ltgetstr("me", spp); 141760786Sps if (*outstr == NULL) 141860786Sps /* Don't even have "me"; use a null string. */ 141960786Sps *outstr = ""; 142060786Sps} 142160786Sps 142260786Sps#endif /* MSDOS_COMPILER */ 142360786Sps 142460786Sps 142560786Sps/* 142660786Sps * Below are the functions which perform all the 142760786Sps * terminal-specific screen manipulation. 142860786Sps */ 142960786Sps 143060786Sps 143160786Sps#if MSDOS_COMPILER 143260786Sps 143360786Sps#if MSDOS_COMPILER==WIN32C 143460786Sps static void 143560786Sps_settextposition(int row, int col) 143660786Sps{ 143760786Sps COORD cpos; 143860786Sps CONSOLE_SCREEN_BUFFER_INFO csbi; 143960786Sps 144060786Sps GetConsoleScreenBufferInfo(con_out, &csbi); 144160786Sps cpos.X = csbi.srWindow.Left + (col - 1); 144260786Sps cpos.Y = csbi.srWindow.Top + (row - 1); 144360786Sps SetConsoleCursorPosition(con_out, cpos); 144460786Sps} 144560786Sps#endif 144660786Sps 144760786Sps/* 144860786Sps * Initialize the screen to the correct color at startup. 144960786Sps */ 145060786Sps static void 145160786Spsinitcolor() 145260786Sps{ 145360786Sps SETCOLORS(nm_fg_color, nm_bg_color); 145460786Sps#if 0 145560786Sps /* 145660786Sps * This clears the screen at startup. This is different from 145760786Sps * the behavior of other versions of less. Disable it for now. 145860786Sps */ 145960786Sps char *blanks; 146060786Sps int row; 146160786Sps int col; 146260786Sps 146360786Sps /* 146460786Sps * Create a complete, blank screen using "normal" colors. 146560786Sps */ 146660786Sps SETCOLORS(nm_fg_color, nm_bg_color); 146760786Sps blanks = (char *) ecalloc(width+1, sizeof(char)); 146860786Sps for (col = 0; col < sc_width; col++) 146960786Sps blanks[col] = ' '; 147060786Sps blanks[sc_width] = '\0'; 147160786Sps for (row = 0; row < sc_height; row++) 147260786Sps _outtext(blanks); 147360786Sps free(blanks); 147460786Sps#endif 147560786Sps} 147660786Sps#endif 147760786Sps 147860786Sps#if MSDOS_COMPILER==WIN32C 147960786Sps 148060786Sps/* 148160786Sps * Termcap-like init with a private win32 console. 148260786Sps */ 148360786Sps static void 148460786Spswin32_init_term() 148560786Sps{ 148660786Sps CONSOLE_SCREEN_BUFFER_INFO scr; 148760786Sps COORD size; 148860786Sps 148960786Sps if (con_out_save == INVALID_HANDLE_VALUE) 149060786Sps return; 149160786Sps 149260786Sps GetConsoleScreenBufferInfo(con_out_save, &scr); 149360786Sps 149460786Sps if (con_out_ours == INVALID_HANDLE_VALUE) 149560786Sps { 149660786Sps /* 149760786Sps * Create our own screen buffer, so that we 149860786Sps * may restore the original when done. 149960786Sps */ 150060786Sps con_out_ours = CreateConsoleScreenBuffer( 150160786Sps GENERIC_WRITE | GENERIC_READ, 150260786Sps FILE_SHARE_WRITE | FILE_SHARE_READ, 150360786Sps (LPSECURITY_ATTRIBUTES) NULL, 150460786Sps CONSOLE_TEXTMODE_BUFFER, 150560786Sps (LPVOID) NULL); 150660786Sps } 150760786Sps 150860786Sps size.X = scr.srWindow.Right - scr.srWindow.Left + 1; 150960786Sps size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1; 151060786Sps SetConsoleScreenBufferSize(con_out_ours, size); 151160786Sps SetConsoleActiveScreenBuffer(con_out_ours); 151260786Sps con_out = con_out_ours; 151360786Sps} 151460786Sps 151560786Sps/* 151660786Sps * Restore the startup console. 151760786Sps */ 151860786Spsstatic void 151960786Spswin32_deinit_term() 152060786Sps{ 152160786Sps if (con_out_save == INVALID_HANDLE_VALUE) 152260786Sps return; 152360786Sps if (quitting) 152460786Sps (void) CloseHandle(con_out_ours); 152560786Sps SetConsoleActiveScreenBuffer(con_out_save); 152660786Sps con_out = con_out_save; 152760786Sps} 152860786Sps 152960786Sps#endif 153060786Sps 153160786Sps/* 153260786Sps * Initialize terminal 153360786Sps */ 153460786Sps public void 153560786Spsinit() 153660786Sps{ 153760786Sps#if !MSDOS_COMPILER 153889022Sps if (!no_init) 153989022Sps tputs(sc_init, sc_height, putchr); 154089022Sps if (!no_keypad) 154189022Sps tputs(sc_s_keypad, sc_height, putchr); 1542161478Sdelphij if (top_scroll) 1543161478Sdelphij { 1544161478Sdelphij int i; 1545161478Sdelphij 1546161478Sdelphij /* 1547161478Sdelphij * This is nice to terminals with no alternate screen, 1548161478Sdelphij * but with saved scrolled-off-the-top lines. This way, 1549161478Sdelphij * no previous line is lost, but we start with a whole 1550161478Sdelphij * screen to ourself. 1551161478Sdelphij */ 1552161478Sdelphij for (i = 1; i < sc_height; i++) 1553161478Sdelphij putchr('\n'); 1554161478Sdelphij } 155560786Sps#else 155660786Sps#if MSDOS_COMPILER==WIN32C 155789022Sps if (!no_init) 155889022Sps win32_init_term(); 155960786Sps#endif 156060786Sps initcolor(); 156160786Sps flush(); 156260786Sps#endif 156360786Sps init_done = 1; 156460786Sps} 156560786Sps 156660786Sps/* 156760786Sps * Deinitialize terminal 156860786Sps */ 156960786Sps public void 157060786Spsdeinit() 157160786Sps{ 157260786Sps if (!init_done) 157360786Sps return; 157460786Sps#if !MSDOS_COMPILER 157589022Sps if (!no_keypad) 157689022Sps tputs(sc_e_keypad, sc_height, putchr); 157789022Sps if (!no_init) 157889022Sps tputs(sc_deinit, sc_height, putchr); 157960786Sps#else 158089022Sps /* Restore system colors. */ 158160786Sps SETCOLORS(sy_fg_color, sy_bg_color); 158260786Sps#if MSDOS_COMPILER==WIN32C 158389022Sps if (!no_init) 158489022Sps win32_deinit_term(); 158589022Sps#else 158689022Sps /* Need clreol to make SETCOLORS take effect. */ 158789022Sps clreol(); 158860786Sps#endif 158960786Sps#endif 159060786Sps init_done = 0; 159160786Sps} 159260786Sps 159360786Sps/* 159460786Sps * Home cursor (move to upper left corner of screen). 159560786Sps */ 159660786Sps public void 159760786Spshome() 159860786Sps{ 159960786Sps#if !MSDOS_COMPILER 160060786Sps tputs(sc_home, 1, putchr); 160160786Sps#else 160260786Sps flush(); 160360786Sps _settextposition(1,1); 160460786Sps#endif 160560786Sps} 160660786Sps 160760786Sps/* 160860786Sps * Add a blank line (called with cursor at home). 160960786Sps * Should scroll the display down. 161060786Sps */ 161160786Sps public void 161260786Spsadd_line() 161360786Sps{ 161460786Sps#if !MSDOS_COMPILER 161560786Sps tputs(sc_addline, sc_height, putchr); 161660786Sps#else 161760786Sps flush(); 161860786Sps#if MSDOS_COMPILER==MSOFTC 161960786Sps _scrolltextwindow(_GSCROLLDOWN); 162060786Sps _settextposition(1,1); 162160786Sps#else 162260786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 162360786Sps movetext(1,1, sc_width,sc_height-1, 1,2); 162460786Sps gotoxy(1,1); 162560786Sps clreol(); 162660786Sps#else 162760786Sps#if MSDOS_COMPILER==WIN32C 162860786Sps { 162960786Sps CHAR_INFO fillchar; 163060786Sps SMALL_RECT rcSrc, rcClip; 163160786Sps COORD new_org; 163260786Sps CONSOLE_SCREEN_BUFFER_INFO csbi; 163360786Sps 163460786Sps GetConsoleScreenBufferInfo(con_out,&csbi); 163560786Sps 163660786Sps /* The clip rectangle is the entire visible screen. */ 163760786Sps rcClip.Left = csbi.srWindow.Left; 163860786Sps rcClip.Top = csbi.srWindow.Top; 163960786Sps rcClip.Right = csbi.srWindow.Right; 164060786Sps rcClip.Bottom = csbi.srWindow.Bottom; 164160786Sps 164260786Sps /* The source rectangle is the visible screen minus the last line. */ 164360786Sps rcSrc = rcClip; 164460786Sps rcSrc.Bottom--; 164560786Sps 164660786Sps /* Move the top left corner of the source window down one row. */ 164760786Sps new_org.X = rcSrc.Left; 164860786Sps new_org.Y = rcSrc.Top + 1; 164960786Sps 165060786Sps /* Fill the right character and attributes. */ 165160786Sps fillchar.Char.AsciiChar = ' '; 165260786Sps curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 165360786Sps fillchar.Attributes = curr_attr; 165460786Sps ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 165560786Sps _settextposition(1,1); 165660786Sps } 165760786Sps#endif 165860786Sps#endif 165960786Sps#endif 166060786Sps#endif 166160786Sps} 166260786Sps 166389022Sps#if 0 166460786Sps/* 166560786Sps * Remove the n topmost lines and scroll everything below it in the 166660786Sps * window upward. This is needed to stop leaking the topmost line 166760786Sps * into the scrollback buffer when we go down-one-line (in WIN32). 166860786Sps */ 166960786Sps public void 167060786Spsremove_top(n) 167160786Sps int n; 167260786Sps{ 167360786Sps#if MSDOS_COMPILER==WIN32C 167460786Sps SMALL_RECT rcSrc, rcClip; 167560786Sps CHAR_INFO fillchar; 167660786Sps COORD new_org; 167760786Sps CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ 167860786Sps 167960786Sps if (n >= sc_height - 1) 168060786Sps { 168160786Sps clear(); 168260786Sps home(); 168360786Sps return; 168460786Sps } 168560786Sps 168660786Sps flush(); 168760786Sps 168860786Sps GetConsoleScreenBufferInfo(con_out, &csbi); 168960786Sps 169060786Sps /* Get the extent of all-visible-rows-but-the-last. */ 169160786Sps rcSrc.Left = csbi.srWindow.Left; 169260786Sps rcSrc.Top = csbi.srWindow.Top + n; 169360786Sps rcSrc.Right = csbi.srWindow.Right; 169460786Sps rcSrc.Bottom = csbi.srWindow.Bottom; 169560786Sps 169660786Sps /* Get the clip rectangle. */ 169760786Sps rcClip.Left = rcSrc.Left; 169860786Sps rcClip.Top = csbi.srWindow.Top; 169960786Sps rcClip.Right = rcSrc.Right; 170060786Sps rcClip.Bottom = rcSrc.Bottom ; 170160786Sps 170260786Sps /* Move the source window up n rows. */ 170360786Sps new_org.X = rcSrc.Left; 170460786Sps new_org.Y = rcSrc.Top - n; 170560786Sps 170660786Sps /* Fill the right character and attributes. */ 170760786Sps fillchar.Char.AsciiChar = ' '; 170860786Sps curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 170960786Sps fillchar.Attributes = curr_attr; 171060786Sps 171160786Sps ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 171260786Sps 171360786Sps /* Position cursor on first blank line. */ 171460786Sps goto_line(sc_height - n - 1); 171560786Sps#endif 171660786Sps} 171789022Sps#endif 171860786Sps 171989022Sps#if MSDOS_COMPILER==WIN32C 172060786Sps/* 172189022Sps * Clear the screen. 172289022Sps */ 172389022Sps static void 172489022Spswin32_clear() 172589022Sps{ 172689022Sps /* 172789022Sps * This will clear only the currently visible rows of the NT 172889022Sps * console buffer, which means none of the precious scrollback 172989022Sps * rows are touched making for faster scrolling. Note that, if 173089022Sps * the window has fewer columns than the console buffer (i.e. 173189022Sps * there is a horizontal scrollbar as well), the entire width 173289022Sps * of the visible rows will be cleared. 173389022Sps */ 173489022Sps COORD topleft; 173589022Sps DWORD nchars; 173689022Sps DWORD winsz; 173789022Sps CONSOLE_SCREEN_BUFFER_INFO csbi; 173889022Sps 173989022Sps /* get the number of cells in the current buffer */ 174089022Sps GetConsoleScreenBufferInfo(con_out, &csbi); 174189022Sps winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1); 174289022Sps topleft.X = 0; 174389022Sps topleft.Y = csbi.srWindow.Top; 174489022Sps 174589022Sps curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 174689022Sps FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars); 174789022Sps FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars); 174889022Sps} 174989022Sps 175089022Sps/* 175189022Sps * Remove the n topmost lines and scroll everything below it in the 175289022Sps * window upward. 175389022Sps */ 175489022Sps public void 175589022Spswin32_scroll_up(n) 175689022Sps int n; 175789022Sps{ 175889022Sps SMALL_RECT rcSrc, rcClip; 175989022Sps CHAR_INFO fillchar; 176089022Sps COORD topleft; 176189022Sps COORD new_org; 176289022Sps DWORD nchars; 176389022Sps DWORD size; 176489022Sps CONSOLE_SCREEN_BUFFER_INFO csbi; 176589022Sps 176689022Sps if (n <= 0) 176789022Sps return; 176889022Sps 176989022Sps if (n >= sc_height - 1) 177089022Sps { 177189022Sps win32_clear(); 177289022Sps _settextposition(1,1); 177389022Sps return; 177489022Sps } 177589022Sps 177689022Sps /* Get the extent of what will remain visible after scrolling. */ 177789022Sps GetConsoleScreenBufferInfo(con_out, &csbi); 177889022Sps rcSrc.Left = csbi.srWindow.Left; 177989022Sps rcSrc.Top = csbi.srWindow.Top + n; 178089022Sps rcSrc.Right = csbi.srWindow.Right; 178189022Sps rcSrc.Bottom = csbi.srWindow.Bottom; 178289022Sps 178389022Sps /* Get the clip rectangle. */ 178489022Sps rcClip.Left = rcSrc.Left; 178589022Sps rcClip.Top = csbi.srWindow.Top; 178689022Sps rcClip.Right = rcSrc.Right; 178789022Sps rcClip.Bottom = rcSrc.Bottom ; 178889022Sps 178989022Sps /* Move the source text to the top of the screen. */ 179089022Sps new_org.X = rcSrc.Left; 179189022Sps new_org.Y = 0; 179289022Sps 179389022Sps /* Fill the right character and attributes. */ 179489022Sps fillchar.Char.AsciiChar = ' '; 179589022Sps fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color); 179689022Sps 179789022Sps /* Scroll the window. */ 179889022Sps SetConsoleTextAttribute(con_out, fillchar.Attributes); 179989022Sps ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar); 180089022Sps 180189022Sps /* Clear remaining lines at bottom. */ 180289022Sps topleft.X = csbi.dwCursorPosition.X; 180389022Sps topleft.Y = rcSrc.Bottom - n; 180489022Sps size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X); 180589022Sps FillConsoleOutputCharacter(con_out, ' ', size, topleft, 180689022Sps &nchars); 180789022Sps FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft, 180889022Sps &nchars); 180989022Sps SetConsoleTextAttribute(con_out, curr_attr); 181089022Sps 181189022Sps /* Move cursor n lines up from where it was. */ 181289022Sps csbi.dwCursorPosition.Y -= n; 181389022Sps SetConsoleCursorPosition(con_out, csbi.dwCursorPosition); 181489022Sps} 181589022Sps#endif 181689022Sps 181789022Sps/* 181860786Sps * Move cursor to lower left corner of screen. 181960786Sps */ 182060786Sps public void 182160786Spslower_left() 182260786Sps{ 182360786Sps#if !MSDOS_COMPILER 182460786Sps tputs(sc_lower_left, 1, putchr); 182560786Sps#else 182660786Sps flush(); 182760786Sps _settextposition(sc_height, 1); 182860786Sps#endif 182960786Sps} 183060786Sps 183160786Sps/* 1832170259Sdelphij * Move cursor to left position of current line. 1833170259Sdelphij */ 1834170259Sdelphij public void 1835170259Sdelphijline_left() 1836170259Sdelphij{ 1837170259Sdelphij#if !MSDOS_COMPILER 1838170259Sdelphij tputs(sc_return, 1, putchr); 1839170259Sdelphij#else 1840170259Sdelphij int row; 1841170259Sdelphij flush(); 1842170259Sdelphij#if MSDOS_COMPILER==WIN32C 1843170259Sdelphij { 1844170259Sdelphij CONSOLE_SCREEN_BUFFER_INFO scr; 1845170259Sdelphij GetConsoleScreenBufferInfo(con_out, &scr); 1846170259Sdelphij row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1; 1847170259Sdelphij } 1848170259Sdelphij#else 1849173685Sdelphij#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 1850173685Sdelphij row = wherey(); 1851173685Sdelphij#else 1852170259Sdelphij { 1853170259Sdelphij struct rccoord tpos = _gettextposition(); 1854170259Sdelphij row = tpos.row; 1855170259Sdelphij } 1856170259Sdelphij#endif 1857173685Sdelphij#endif 1858170259Sdelphij _settextposition(row, 1); 1859170259Sdelphij#endif 1860170259Sdelphij} 1861170259Sdelphij 1862170259Sdelphij/* 186360786Sps * Check if the console size has changed and reset internals 186460786Sps * (in lieu of SIGWINCH for WIN32). 186560786Sps */ 186660786Sps public void 186760786Spscheck_winch() 186860786Sps{ 186960786Sps#if MSDOS_COMPILER==WIN32C 187060786Sps CONSOLE_SCREEN_BUFFER_INFO scr; 187160786Sps COORD size; 187260786Sps 187360786Sps if (con_out == INVALID_HANDLE_VALUE) 187460786Sps return; 187560786Sps 187660786Sps flush(); 187760786Sps GetConsoleScreenBufferInfo(con_out, &scr); 187860786Sps size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1; 187960786Sps size.X = scr.srWindow.Right - scr.srWindow.Left + 1; 188060786Sps if (size.Y != sc_height || size.X != sc_width) 188160786Sps { 188260786Sps sc_height = size.Y; 188360786Sps sc_width = size.X; 188460786Sps if (!no_init && con_out_ours == con_out) 188560786Sps SetConsoleScreenBufferSize(con_out, size); 188660786Sps pos_init(); 188760786Sps wscroll = (sc_height + 1) / 2; 188860786Sps screen_trashed = 1; 188960786Sps } 189060786Sps#endif 189160786Sps} 189260786Sps 189360786Sps/* 189460786Sps * Goto a specific line on the screen. 189560786Sps */ 189660786Sps public void 189760786Spsgoto_line(slinenum) 189860786Sps int slinenum; 189960786Sps{ 190060786Sps#if !MSDOS_COMPILER 190160786Sps tputs(tgoto(sc_move, 0, slinenum), 1, putchr); 190260786Sps#else 190360786Sps flush(); 190460786Sps _settextposition(slinenum+1, 1); 190560786Sps#endif 190660786Sps} 190760786Sps 190860786Sps#if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC 190960786Sps/* 191060786Sps * Create an alternate screen which is all white. 191160786Sps * This screen is used to create a "flash" effect, by displaying it 191260786Sps * briefly and then switching back to the normal screen. 191360786Sps * {{ Yuck! There must be a better way to get a visual bell. }} 191460786Sps */ 191560786Sps static void 191660786Spscreate_flash() 191760786Sps{ 191860786Sps#if MSDOS_COMPILER==MSOFTC 191960786Sps struct videoconfig w; 192060786Sps char *blanks; 192160786Sps int row, col; 192260786Sps 192360786Sps _getvideoconfig(&w); 192460786Sps videopages = w.numvideopages; 192560786Sps if (videopages < 2) 192660786Sps { 1927161478Sdelphij at_enter(AT_STANDOUT); 1928161478Sdelphij at_exit(); 192960786Sps } else 193060786Sps { 193160786Sps _setactivepage(1); 1932161478Sdelphij at_enter(AT_STANDOUT); 193360786Sps blanks = (char *) ecalloc(w.numtextcols, sizeof(char)); 193460786Sps for (col = 0; col < w.numtextcols; col++) 193560786Sps blanks[col] = ' '; 193660786Sps for (row = w.numtextrows; row > 0; row--) 193760786Sps _outmem(blanks, w.numtextcols); 193860786Sps _setactivepage(0); 193960786Sps _setvisualpage(0); 194060786Sps free(blanks); 1941161478Sdelphij at_exit(); 194260786Sps } 194360786Sps#else 194460786Sps#if MSDOS_COMPILER==BORLANDC 194560786Sps register int n; 194660786Sps 194760786Sps whitescreen = (unsigned short *) 194860786Sps malloc(sc_width * sc_height * sizeof(short)); 194960786Sps if (whitescreen == NULL) 195060786Sps return; 195160786Sps for (n = 0; n < sc_width * sc_height; n++) 195260786Sps whitescreen[n] = 0x7020; 195360786Sps#else 195460786Sps#if MSDOS_COMPILER==WIN32C 195560786Sps register int n; 195660786Sps 195760786Sps whitescreen = (WORD *) 195860786Sps malloc(sc_height * sc_width * sizeof(WORD)); 195960786Sps if (whitescreen == NULL) 196060786Sps return; 196160786Sps /* Invert the standard colors. */ 196260786Sps for (n = 0; n < sc_width * sc_height; n++) 196360786Sps whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color); 196460786Sps#endif 196560786Sps#endif 196660786Sps#endif 196760786Sps flash_created = 1; 196860786Sps} 196960786Sps#endif /* MSDOS_COMPILER */ 197060786Sps 197160786Sps/* 197260786Sps * Output the "visual bell", if there is one. 197360786Sps */ 197460786Sps public void 197560786Spsvbell() 197660786Sps{ 197760786Sps#if !MSDOS_COMPILER 197860786Sps if (*sc_visual_bell == '\0') 197960786Sps return; 198060786Sps tputs(sc_visual_bell, sc_height, putchr); 198160786Sps#else 198260786Sps#if MSDOS_COMPILER==DJGPPC 198360786Sps ScreenVisualBell(); 198460786Sps#else 198560786Sps#if MSDOS_COMPILER==MSOFTC 198660786Sps /* 198760786Sps * Create a flash screen on the second video page. 198860786Sps * Switch to that page, then switch back. 198960786Sps */ 199060786Sps if (!flash_created) 199160786Sps create_flash(); 199260786Sps if (videopages < 2) 199360786Sps return; 199460786Sps _setvisualpage(1); 199560786Sps delay(100); 199660786Sps _setvisualpage(0); 199760786Sps#else 199860786Sps#if MSDOS_COMPILER==BORLANDC 199960786Sps unsigned short *currscreen; 200060786Sps 200160786Sps /* 200260786Sps * Get a copy of the current screen. 200360786Sps * Display the flash screen. 200460786Sps * Then restore the old screen. 200560786Sps */ 200660786Sps if (!flash_created) 200760786Sps create_flash(); 200860786Sps if (whitescreen == NULL) 200960786Sps return; 201060786Sps currscreen = (unsigned short *) 201160786Sps malloc(sc_width * sc_height * sizeof(short)); 201260786Sps if (currscreen == NULL) return; 201360786Sps gettext(1, 1, sc_width, sc_height, currscreen); 201460786Sps puttext(1, 1, sc_width, sc_height, whitescreen); 201560786Sps delay(100); 201660786Sps puttext(1, 1, sc_width, sc_height, currscreen); 201760786Sps free(currscreen); 201860786Sps#else 201960786Sps#if MSDOS_COMPILER==WIN32C 202060786Sps /* paint screen with an inverse color */ 202160786Sps clear(); 202260786Sps 202360786Sps /* leave it displayed for 100 msec. */ 202460786Sps Sleep(100); 202560786Sps 202660786Sps /* restore with a redraw */ 202760786Sps repaint(); 202860786Sps#endif 202960786Sps#endif 203060786Sps#endif 203160786Sps#endif 203260786Sps#endif 203360786Sps} 203460786Sps 203560786Sps/* 203660786Sps * Make a noise. 203760786Sps */ 203860786Sps static void 203960786Spsbeep() 204060786Sps{ 204160786Sps#if !MSDOS_COMPILER 204289022Sps putchr(CONTROL('G')); 204360786Sps#else 204460786Sps#if MSDOS_COMPILER==WIN32C 204560786Sps MessageBeep(0); 204660786Sps#else 204760786Sps write(1, "\7", 1); 204860786Sps#endif 204960786Sps#endif 205060786Sps} 205160786Sps 205260786Sps/* 205360786Sps * Ring the terminal bell. 205460786Sps */ 205560786Sps public void 205660786Spsbell() 205760786Sps{ 205860786Sps if (quiet == VERY_QUIET) 205960786Sps vbell(); 206060786Sps else 206160786Sps beep(); 206260786Sps} 206360786Sps 206460786Sps/* 206560786Sps * Clear the screen. 206660786Sps */ 206760786Sps public void 206860786Spsclear() 206960786Sps{ 207060786Sps#if !MSDOS_COMPILER 207160786Sps tputs(sc_clear, sc_height, putchr); 207260786Sps#else 207360786Sps flush(); 207460786Sps#if MSDOS_COMPILER==WIN32C 207589022Sps win32_clear(); 207660786Sps#else 207760786Sps _clearscreen(_GCLEARSCREEN); 207860786Sps#endif 207960786Sps#endif 208060786Sps} 208160786Sps 208260786Sps/* 208360786Sps * Clear from the cursor to the end of the cursor's line. 208460786Sps * {{ This must not move the cursor. }} 208560786Sps */ 208660786Sps public void 208760786Spsclear_eol() 208860786Sps{ 208960786Sps#if !MSDOS_COMPILER 209060786Sps tputs(sc_eol_clear, 1, putchr); 209160786Sps#else 209260786Sps#if MSDOS_COMPILER==MSOFTC 209360786Sps short top, left; 209460786Sps short bot, right; 209560786Sps struct rccoord tpos; 209660786Sps 209760786Sps flush(); 209860786Sps /* 209960786Sps * Save current state. 210060786Sps */ 210160786Sps tpos = _gettextposition(); 210260786Sps _gettextwindow(&top, &left, &bot, &right); 210360786Sps /* 210460786Sps * Set a temporary window to the current line, 210560786Sps * from the cursor's position to the right edge of the screen. 210660786Sps * Then clear that window. 210760786Sps */ 210860786Sps _settextwindow(tpos.row, tpos.col, tpos.row, sc_width); 210960786Sps _clearscreen(_GWINDOW); 211060786Sps /* 211160786Sps * Restore state. 211260786Sps */ 211360786Sps _settextwindow(top, left, bot, right); 211460786Sps _settextposition(tpos.row, tpos.col); 211560786Sps#else 211660786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 211760786Sps flush(); 211860786Sps clreol(); 211960786Sps#else 212060786Sps#if MSDOS_COMPILER==WIN32C 212160786Sps DWORD nchars; 212260786Sps COORD cpos; 212360786Sps CONSOLE_SCREEN_BUFFER_INFO scr; 212460786Sps 212560786Sps flush(); 212660786Sps memset(&scr, 0, sizeof(scr)); 212760786Sps GetConsoleScreenBufferInfo(con_out, &scr); 212860786Sps cpos.X = scr.dwCursorPosition.X; 212960786Sps cpos.Y = scr.dwCursorPosition.Y; 213060786Sps curr_attr = MAKEATTR(nm_fg_color, nm_bg_color); 213160786Sps FillConsoleOutputAttribute(con_out, curr_attr, 213260786Sps scr.dwSize.X - cpos.X, cpos, &nchars); 213360786Sps FillConsoleOutputCharacter(con_out, ' ', 213460786Sps scr.dwSize.X - cpos.X, cpos, &nchars); 213560786Sps#endif 213660786Sps#endif 213760786Sps#endif 213860786Sps#endif 213960786Sps} 214060786Sps 214160786Sps/* 214260786Sps * Clear the current line. 214360786Sps * Clear the screen if there's off-screen memory below the display. 214460786Sps */ 214560786Sps static void 214660786Spsclear_eol_bot() 214760786Sps{ 214860786Sps#if MSDOS_COMPILER 214960786Sps clear_eol(); 215060786Sps#else 215160786Sps if (below_mem) 215260786Sps tputs(sc_eos_clear, 1, putchr); 215360786Sps else 215460786Sps tputs(sc_eol_clear, 1, putchr); 215560786Sps#endif 215660786Sps} 215760786Sps 215860786Sps/* 215960786Sps * Clear the bottom line of the display. 216060786Sps * Leave the cursor at the beginning of the bottom line. 216160786Sps */ 216260786Sps public void 216360786Spsclear_bot() 216460786Sps{ 216560786Sps /* 216660786Sps * If we're in a non-normal attribute mode, temporarily exit 216760786Sps * the mode while we do the clear. Some terminals fill the 216860786Sps * cleared area with the current attribute. 216960786Sps */ 2170170259Sdelphij if (oldbot) 2171170259Sdelphij lower_left(); 2172170259Sdelphij else 2173170259Sdelphij line_left(); 2174170259Sdelphij 2175161478Sdelphij if (attrmode == AT_NORMAL) 2176161478Sdelphij clear_eol_bot(); 2177161478Sdelphij else 217860786Sps { 2179161478Sdelphij int saved_attrmode = attrmode; 2180161478Sdelphij 2181161478Sdelphij at_exit(); 218260786Sps clear_eol_bot(); 2183161478Sdelphij at_enter(saved_attrmode); 218460786Sps } 218560786Sps} 218660786Sps 218760786Sps public void 2188161478Sdelphijat_enter(attr) 2189161478Sdelphij int attr; 219060786Sps{ 2191161478Sdelphij attr = apply_at_specials(attr); 219260786Sps 219360786Sps#if !MSDOS_COMPILER 2194161478Sdelphij /* The one with the most priority is last. */ 2195161478Sdelphij if (attr & AT_UNDERLINE) 2196161478Sdelphij tputs(sc_u_in, 1, putchr); 2197161478Sdelphij if (attr & AT_BOLD) 2198161478Sdelphij tputs(sc_b_in, 1, putchr); 2199161478Sdelphij if (attr & AT_BLINK) 2200161478Sdelphij tputs(sc_bl_in, 1, putchr); 2201161478Sdelphij if (attr & AT_STANDOUT) 2202161478Sdelphij tputs(sc_s_in, 1, putchr); 220360786Sps#else 220460786Sps flush(); 2205161478Sdelphij /* The one with the most priority is first. */ 2206161478Sdelphij if (attr & AT_STANDOUT) 2207161478Sdelphij { 2208161478Sdelphij SETCOLORS(so_fg_color, so_bg_color); 2209161478Sdelphij } else if (attr & AT_BLINK) 2210161478Sdelphij { 2211161478Sdelphij SETCOLORS(bl_fg_color, bl_bg_color); 2212161478Sdelphij } 2213161478Sdelphij else if (attr & AT_BOLD) 2214161478Sdelphij { 2215161478Sdelphij SETCOLORS(bo_fg_color, bo_bg_color); 2216161478Sdelphij } 2217161478Sdelphij else if (attr & AT_UNDERLINE) 2218161478Sdelphij { 2219161478Sdelphij SETCOLORS(ul_fg_color, ul_bg_color); 2220161478Sdelphij } 222160786Sps#endif 222260786Sps 2223161478Sdelphij attrmode = attr; 222460786Sps} 222560786Sps 222660786Sps public void 2227161478Sdelphijat_exit() 222860786Sps{ 222960786Sps#if !MSDOS_COMPILER 2230161478Sdelphij /* Undo things in the reverse order we did them. */ 2231161478Sdelphij if (attrmode & AT_STANDOUT) 2232161478Sdelphij tputs(sc_s_out, 1, putchr); 2233161478Sdelphij if (attrmode & AT_BLINK) 2234161478Sdelphij tputs(sc_bl_out, 1, putchr); 2235161478Sdelphij if (attrmode & AT_BOLD) 2236161478Sdelphij tputs(sc_b_out, 1, putchr); 2237161478Sdelphij if (attrmode & AT_UNDERLINE) 2238161478Sdelphij tputs(sc_u_out, 1, putchr); 223960786Sps#else 224060786Sps flush(); 224160786Sps SETCOLORS(nm_fg_color, nm_bg_color); 224260786Sps#endif 2243161478Sdelphij 224460786Sps attrmode = AT_NORMAL; 224560786Sps} 224660786Sps 224760786Sps public void 2248161478Sdelphijat_switch(attr) 2249161478Sdelphij int attr; 225060786Sps{ 2251170259Sdelphij int new_attrmode = apply_at_specials(attr); 2252170259Sdelphij int ignore_modes = AT_ANSI; 2253170259Sdelphij 2254170259Sdelphij if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes)) 2255161478Sdelphij { 2256161478Sdelphij at_exit(); 2257161478Sdelphij at_enter(attr); 2258161478Sdelphij } 225960786Sps} 226060786Sps 2261161478Sdelphij public int 2262161478Sdelphijis_at_equiv(attr1, attr2) 2263161478Sdelphij int attr1; 2264161478Sdelphij int attr2; 226560786Sps{ 2266161478Sdelphij attr1 = apply_at_specials(attr1); 2267161478Sdelphij attr2 = apply_at_specials(attr2); 2268161478Sdelphij 2269161478Sdelphij return (attr1 == attr2); 227060786Sps} 227160786Sps 2272161478Sdelphij public int 2273161478Sdelphijapply_at_specials(attr) 2274161478Sdelphij int attr; 227560786Sps{ 2276161478Sdelphij if (attr & AT_BINARY) 2277161478Sdelphij attr |= binattr; 2278161478Sdelphij if (attr & AT_HILITE) 2279161478Sdelphij attr |= AT_STANDOUT; 2280161478Sdelphij attr &= ~(AT_BINARY|AT_HILITE); 228160786Sps 2282161478Sdelphij return attr; 228360786Sps} 228460786Sps 228560786Sps#if 0 /* No longer used */ 228660786Sps/* 228760786Sps * Erase the character to the left of the cursor 228860786Sps * and move the cursor left. 228960786Sps */ 229060786Sps public void 229160786Spsbackspace() 229260786Sps{ 229360786Sps#if !MSDOS_COMPILER 229460786Sps /* 229560786Sps * Erase the previous character by overstriking with a space. 229660786Sps */ 229760786Sps tputs(sc_backspace, 1, putchr); 229860786Sps putchr(' '); 229960786Sps tputs(sc_backspace, 1, putchr); 230060786Sps#else 230160786Sps#if MSDOS_COMPILER==MSOFTC 230260786Sps struct rccoord tpos; 230360786Sps 230460786Sps flush(); 230560786Sps tpos = _gettextposition(); 230660786Sps if (tpos.col <= 1) 230760786Sps return; 230860786Sps _settextposition(tpos.row, tpos.col-1); 230960786Sps _outtext(" "); 231060786Sps _settextposition(tpos.row, tpos.col-1); 231160786Sps#else 231260786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 231360786Sps cputs("\b"); 231460786Sps#else 231560786Sps#if MSDOS_COMPILER==WIN32C 231660786Sps COORD cpos; 231760786Sps DWORD cChars; 231860786Sps CONSOLE_SCREEN_BUFFER_INFO scr; 231960786Sps 232060786Sps flush(); 232160786Sps GetConsoleScreenBufferInfo(con_out, &scr); 232260786Sps cpos = scr.dwCursorPosition; 232360786Sps if (cpos.X <= 0) 232460786Sps return; 232560786Sps cpos.X--; 232660786Sps SetConsoleCursorPosition(con_out, cpos); 232760786Sps FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars); 232860786Sps SetConsoleCursorPosition(con_out, cpos); 232960786Sps#endif 233060786Sps#endif 233160786Sps#endif 233260786Sps#endif 233360786Sps} 233460786Sps#endif /* 0 */ 233560786Sps 233660786Sps/* 233760786Sps * Output a plain backspace, without erasing the previous char. 233860786Sps */ 233960786Sps public void 234060786Spsputbs() 234160786Sps{ 234260786Sps#if !MSDOS_COMPILER 234360786Sps tputs(sc_backspace, 1, putchr); 234460786Sps#else 234560786Sps int row, col; 234660786Sps 234760786Sps flush(); 234860786Sps { 234960786Sps#if MSDOS_COMPILER==MSOFTC 235060786Sps struct rccoord tpos; 235160786Sps tpos = _gettextposition(); 235260786Sps row = tpos.row; 235360786Sps col = tpos.col; 235460786Sps#else 235560786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC 235660786Sps row = wherey(); 235760786Sps col = wherex(); 235860786Sps#else 235960786Sps#if MSDOS_COMPILER==WIN32C 236060786Sps CONSOLE_SCREEN_BUFFER_INFO scr; 236160786Sps GetConsoleScreenBufferInfo(con_out, &scr); 236260786Sps row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1; 236360786Sps col = scr.dwCursorPosition.X - scr.srWindow.Left + 1; 236460786Sps#endif 236560786Sps#endif 236660786Sps#endif 236760786Sps } 236860786Sps if (col <= 1) 236960786Sps return; 237060786Sps _settextposition(row, col-1); 237160786Sps#endif /* MSDOS_COMPILER */ 237260786Sps} 237360786Sps 237460786Sps#if MSDOS_COMPILER==WIN32C 237560786Sps/* 237660786Sps * Determine whether an input character is waiting to be read. 237760786Sps */ 237860786Sps static int 237960786Spswin32_kbhit(tty) 238060786Sps HANDLE tty; 238160786Sps{ 238260786Sps INPUT_RECORD ip; 238360786Sps DWORD read; 238460786Sps 238560786Sps if (keyCount > 0) 238660786Sps return (TRUE); 238760786Sps 238860786Sps currentKey.ascii = 0; 238960786Sps currentKey.scan = 0; 239060786Sps 239160786Sps /* 239260786Sps * Wait for a real key-down event, but 239360786Sps * ignore SHIFT and CONTROL key events. 239460786Sps */ 239560786Sps do 239660786Sps { 239760786Sps PeekConsoleInput(tty, &ip, 1, &read); 239860786Sps if (read == 0) 239960786Sps return (FALSE); 240060786Sps ReadConsoleInput(tty, &ip, 1, &read); 240160786Sps } while (ip.EventType != KEY_EVENT || 240260786Sps ip.Event.KeyEvent.bKeyDown != TRUE || 240360786Sps ip.Event.KeyEvent.wVirtualScanCode == 0 || 240460786Sps ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT || 240560786Sps ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL || 240660786Sps ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU); 240760786Sps 240860786Sps currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar; 240960786Sps currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode; 241060786Sps keyCount = ip.Event.KeyEvent.wRepeatCount; 241160786Sps 241260786Sps if (ip.Event.KeyEvent.dwControlKeyState & 241360786Sps (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) 241460786Sps { 241560786Sps switch (currentKey.scan) 241660786Sps { 241760786Sps case PCK_ALT_E: /* letter 'E' */ 241860786Sps currentKey.ascii = 0; 241960786Sps break; 242060786Sps } 242160786Sps } else if (ip.Event.KeyEvent.dwControlKeyState & 242260786Sps (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) 242360786Sps { 242460786Sps switch (currentKey.scan) 242560786Sps { 242660786Sps case PCK_RIGHT: /* right arrow */ 242760786Sps currentKey.scan = PCK_CTL_RIGHT; 242860786Sps break; 242960786Sps case PCK_LEFT: /* left arrow */ 243060786Sps currentKey.scan = PCK_CTL_LEFT; 243160786Sps break; 243260786Sps case PCK_DELETE: /* delete */ 243360786Sps currentKey.scan = PCK_CTL_DELETE; 243460786Sps break; 243560786Sps } 243660786Sps } 243760786Sps return (TRUE); 243860786Sps} 243960786Sps 244060786Sps/* 244160786Sps * Read a character from the keyboard. 244260786Sps */ 244360786Sps public char 244460786SpsWIN32getch(tty) 244560786Sps int tty; 244660786Sps{ 244760786Sps int ascii; 244860786Sps 244960786Sps if (pending_scancode) 245060786Sps { 245160786Sps pending_scancode = 0; 245260786Sps return ((char)(currentKey.scan & 0x00FF)); 245360786Sps } 245460786Sps 245560786Sps while (win32_kbhit((HANDLE)tty) == FALSE) 245660786Sps { 245760786Sps Sleep(20); 245860786Sps if (ABORT_SIGS()) 245960786Sps return ('\003'); 246060786Sps continue; 246160786Sps } 246260786Sps keyCount --; 246360786Sps ascii = currentKey.ascii; 246460786Sps /* 246560786Sps * On PC's, the extended keys return a 2 byte sequence beginning 246660786Sps * with '00', so if the ascii code is 00, the next byte will be 246760786Sps * the lsb of the scan code. 246860786Sps */ 246960786Sps pending_scancode = (ascii == 0x00); 247060786Sps return ((char)ascii); 247160786Sps} 247260786Sps#endif 2473