160812Sps/* $FreeBSD$ */
260786Sps/*
3240121Sdelphij * Copyright (C) 1984-2012  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 *
8240121Sdelphij * For more information, see the README file.
960786Sps */
1060786Sps
1160786Sps
1260786Sps/*
1360786Sps * Routines which deal with the characteristics of the terminal.
1460786Sps * Uses termcap to be as terminal-independent as possible.
1560786Sps */
1660786Sps
1760786Sps#include "less.h"
1860786Sps#include "cmd.h"
1960786Sps
2060786Sps#if MSDOS_COMPILER
2160786Sps#include "pckeys.h"
2260786Sps#if MSDOS_COMPILER==MSOFTC
2360786Sps#include <graph.h>
2460786Sps#else
2560786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2660786Sps#include <conio.h>
2760786Sps#if MSDOS_COMPILER==DJGPPC
2860786Sps#include <pc.h>
2960786Spsextern int fd0;
3060786Sps#endif
3160786Sps#else
3260786Sps#if MSDOS_COMPILER==WIN32C
3360786Sps#include <windows.h>
3460786Sps#endif
3560786Sps#endif
3660786Sps#endif
3760786Sps#include <time.h>
3860786Sps
3960786Sps#else
4060786Sps
41161478Sdelphij#if HAVE_SYS_IOCTL_H
42161478Sdelphij#include <sys/ioctl.h>
43161478Sdelphij#endif
44161478Sdelphij
4560786Sps#if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
4660786Sps#include <termios.h>
4760786Sps#else
4860786Sps#if HAVE_TERMIO_H
4960786Sps#include <termio.h>
5060786Sps#else
5160786Sps#if HAVE_SGSTAT_H
5260786Sps#include <sgstat.h>
5360786Sps#else
5460786Sps#include <sgtty.h>
5560786Sps#endif
5660786Sps#endif
5760786Sps#endif
5860786Sps
5960786Sps#if HAVE_TERMCAP_H
6060786Sps#include <termcap.h>
6160786Sps#endif
6260786Sps#ifdef _OSK
6360786Sps#include <signal.h>
6460786Sps#endif
6560786Sps#if OS2
6660786Sps#include <sys/signal.h>
6789022Sps#include "pckeys.h"
6860786Sps#endif
6960786Sps#if HAVE_SYS_STREAM_H
7060786Sps#include <sys/stream.h>
7160786Sps#endif
7260786Sps#if HAVE_SYS_PTEM_H
7360786Sps#include <sys/ptem.h>
7460786Sps#endif
7560786Sps
7660786Sps#endif /* MSDOS_COMPILER */
7760786Sps
7860786Sps/*
7960786Sps * Check for broken termios package that forces you to manually
8060786Sps * set the line discipline.
8160786Sps */
8260786Sps#ifdef __ultrix__
8360786Sps#define MUST_SET_LINE_DISCIPLINE 1
8460786Sps#else
8560786Sps#define MUST_SET_LINE_DISCIPLINE 0
8660786Sps#endif
8760786Sps
8860786Sps#if OS2
8960786Sps#define	DEFAULT_TERM		"ansi"
9089022Spsstatic char *windowid;
9160786Sps#else
9260786Sps#define	DEFAULT_TERM		"unknown"
9360786Sps#endif
9460786Sps
9560786Sps#if MSDOS_COMPILER==MSOFTC
9660786Spsstatic int videopages;
9760786Spsstatic long msec_loops;
9860786Spsstatic int flash_created = 0;
9960786Sps#define	SETCOLORS(fg,bg)	{ _settextcolor(fg); _setbkcolor(bg); }
10060786Sps#endif
10160786Sps
10260786Sps#if MSDOS_COMPILER==BORLANDC
10360786Spsstatic unsigned short *whitescreen;
10460786Spsstatic int flash_created = 0;
10560786Sps#endif
10660786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
10760786Sps#define _settextposition(y,x)   gotoxy(x,y)
10860786Sps#define _clearscreen(m)         clrscr()
10960786Sps#define _outtext(s)             cputs(s)
11060786Sps#define	SETCOLORS(fg,bg)	{ textcolor(fg); textbackground(bg); }
11160786Spsextern int sc_height;
11260786Sps#endif
11360786Sps
11460786Sps#if MSDOS_COMPILER==WIN32C
11560786Spsstruct keyRecord
11660786Sps{
11760786Sps	int ascii;
11860786Sps	int scan;
11960786Sps} currentKey;
12060786Sps
12160786Spsstatic int keyCount = 0;
12260786Spsstatic WORD curr_attr;
12360786Spsstatic int pending_scancode = 0;
12460786Spsstatic WORD *whitescreen;
12560786Sps
12660786Spsstatic HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
12760786Spsstatic HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
12860786SpsHANDLE con_out = INVALID_HANDLE_VALUE;             /* current console */
12960786Sps
13060786Spsextern int quitting;
13160786Spsstatic void win32_init_term();
13260786Spsstatic void win32_deinit_term();
13360786Sps
13460786Sps#define FG_COLORS       (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
13560786Sps#define BG_COLORS       (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
13660786Sps#define	MAKEATTR(fg,bg)		((WORD)((fg)|((bg)<<4)))
13760786Sps#define	SETCOLORS(fg,bg)	{ curr_attr = MAKEATTR(fg,bg); \
13860786Sps				if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
13960786Sps				error("SETCOLORS failed"); }
14060786Sps#endif
14160786Sps
14260786Sps#if MSDOS_COMPILER
14360786Spspublic int nm_fg_color;		/* Color of normal text */
14460786Spspublic int nm_bg_color;
14560786Spspublic int bo_fg_color;		/* Color of bold text */
14660786Spspublic int bo_bg_color;
14760786Spspublic int ul_fg_color;		/* Color of underlined text */
14860786Spspublic int ul_bg_color;
14960786Spspublic int so_fg_color;		/* Color of standout text */
15060786Spspublic int so_bg_color;
15160786Spspublic int bl_fg_color;		/* Color of blinking text */
15260786Spspublic int bl_bg_color;
15360786Spsstatic int sy_fg_color;		/* Color of system text (before less) */
15460786Spsstatic int sy_bg_color;
15560786Sps
15660786Sps#else
15760786Sps
15860786Sps/*
15960786Sps * Strings passed to tputs() to do various terminal functions.
16060786Sps */
16160786Spsstatic char
16260786Sps	*sc_pad,		/* Pad string */
16360786Sps	*sc_home,		/* Cursor home */
16460786Sps	*sc_addline,		/* Add line, scroll down following lines */
16560786Sps	*sc_lower_left,		/* Cursor to last line, first column */
166170259Sdelphij	*sc_return,		/* Cursor to beginning of current line */
16760786Sps	*sc_move,		/* General cursor positioning */
16860786Sps	*sc_clear,		/* Clear screen */
16960786Sps	*sc_eol_clear,		/* Clear to end of line */
17060786Sps	*sc_eos_clear,		/* Clear to end of screen */
17160786Sps	*sc_s_in,		/* Enter standout (highlighted) mode */
17260786Sps	*sc_s_out,		/* Exit standout mode */
17360786Sps	*sc_u_in,		/* Enter underline mode */
17460786Sps	*sc_u_out,		/* Exit underline mode */
17560786Sps	*sc_b_in,		/* Enter bold mode */
17660786Sps	*sc_b_out,		/* Exit bold mode */
17760786Sps	*sc_bl_in,		/* Enter blink mode */
17860786Sps	*sc_bl_out,		/* Exit blink mode */
17960786Sps	*sc_visual_bell,	/* Visual bell (flash screen) sequence */
18060786Sps	*sc_backspace,		/* Backspace cursor */
18160786Sps	*sc_s_keypad,		/* Start keypad mode */
18260786Sps	*sc_e_keypad,		/* End keypad mode */
18360786Sps	*sc_init,		/* Startup terminal initialization */
18460786Sps	*sc_deinit;		/* Exit terminal de-initialization */
18560786Sps#endif
18660786Sps
18760786Spsstatic int init_done = 0;
18860786Sps
18960786Spspublic int auto_wrap;		/* Terminal does \r\n when write past margin */
19060786Spspublic int ignaw;		/* Terminal ignores \n immediately after wrap */
191161478Sdelphijpublic int erase_char;		/* The user's erase char */
192161478Sdelphijpublic int erase2_char;		/* The user's other erase char */
193161478Sdelphijpublic int kill_char;		/* The user's line-kill char */
19460786Spspublic int werase_char;		/* The user's word-erase char */
19560786Spspublic int sc_width, sc_height;	/* Height & width of screen */
19660786Spspublic int bo_s_width, bo_e_width;	/* Printing width of boldface seq */
19760786Spspublic int ul_s_width, ul_e_width;	/* Printing width of underline seq */
19860786Spspublic int so_s_width, so_e_width;	/* Printing width of standout seq */
19960786Spspublic int bl_s_width, bl_e_width;	/* Printing width of blink seq */
20060786Spspublic int above_mem, below_mem;	/* Memory retained above/below screen */
20160786Spspublic int can_goto_line;		/* Can move cursor to any line */
20260786Spspublic int clear_bg;		/* Clear fills with background color */
20360786Spspublic int missing_cap = 0;	/* Some capability is missing */
20460786Sps
20560786Spsstatic int attrmode = AT_NORMAL;
206161478Sdelphijextern int binattr;
20760786Sps
20860786Sps#if !MSDOS_COMPILER
20960786Spsstatic char *cheaper();
21060786Spsstatic void tmodes();
21160786Sps#endif
21260786Sps
21360786Sps/*
21460786Sps * These two variables are sometimes defined in,
21560786Sps * and needed by, the termcap library.
21660786Sps */
21760786Sps#if MUST_DEFINE_OSPEED
21860786Spsextern short ospeed;	/* Terminal output baud rate */
21960786Spsextern char PC;		/* Pad character */
22060786Sps#endif
22160786Sps#ifdef _OSK
22260786Spsshort ospeed;
22360786Spschar PC_, *UP, *BC;
22460786Sps#endif
22560786Sps
22660786Spsextern int quiet;		/* If VERY_QUIET, use visual bell for bell */
22760786Spsextern int no_back_scroll;
22860786Spsextern int swindow;
22960786Spsextern int no_init;
23089022Spsextern int no_keypad;
23160786Spsextern int sigs;
23260786Spsextern int wscroll;
23360786Spsextern int screen_trashed;
234128348Stjrextern int tty;
235161478Sdelphijextern int top_scroll;
236170259Sdelphijextern int oldbot;
23760786Sps#if HILITE_SEARCH
23860786Spsextern int hilite_search;
23960786Sps#endif
24060786Sps
24160786Spsextern char *tgetstr();
24260786Spsextern char *tgoto();
24360786Sps
24460786Sps
24560786Sps/*
24660786Sps * Change terminal to "raw mode", or restore to "normal" mode.
24760786Sps * "Raw mode" means
24860786Sps *	1. An outstanding read will complete on receipt of a single keystroke.
24960786Sps *	2. Input is not echoed.
25060786Sps *	3. On output, \n is mapped to \r\n.
25160786Sps *	4. \t is NOT expanded into spaces.
25260786Sps *	5. Signal-causing characters such as ctrl-C (interrupt),
25360786Sps *	   etc. are NOT disabled.
25460786Sps * It doesn't matter whether an input \n is mapped to \r, or vice versa.
25560786Sps */
25660786Sps	public void
25760786Spsraw_mode(on)
25860786Sps	int on;
25960786Sps{
26060786Sps	static int curr_on = 0;
26160786Sps
26260786Sps	if (on == curr_on)
26360786Sps		return;
264161478Sdelphij	erase2_char = '\b'; /* in case OS doesn't know about erase2 */
26560786Sps#if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
26660786Sps    {
26760786Sps	struct termios s;
26860786Sps	static struct termios save_term;
26960786Sps	static int saved_term = 0;
27060786Sps
27160786Sps	if (on)
27260786Sps	{
27360786Sps		/*
27460786Sps		 * Get terminal modes.
27560786Sps		 */
276128348Stjr		tcgetattr(tty, &s);
27760786Sps
27860786Sps		/*
27960786Sps		 * Save modes and set certain variables dependent on modes.
28060786Sps		 */
28160786Sps		if (!saved_term)
28260786Sps		{
28360786Sps			save_term = s;
28460786Sps			saved_term = 1;
28560786Sps		}
28660786Sps#if HAVE_OSPEED
28760786Sps		switch (cfgetospeed(&s))
28860786Sps		{
28960786Sps#ifdef B0
29060786Sps		case B0: ospeed = 0; break;
29160786Sps#endif
29260786Sps#ifdef B50
29360786Sps		case B50: ospeed = 1; break;
29460786Sps#endif
29560786Sps#ifdef B75
29660786Sps		case B75: ospeed = 2; break;
29760786Sps#endif
29860786Sps#ifdef B110
29960786Sps		case B110: ospeed = 3; break;
30060786Sps#endif
30160786Sps#ifdef B134
30260786Sps		case B134: ospeed = 4; break;
30360786Sps#endif
30460786Sps#ifdef B150
30560786Sps		case B150: ospeed = 5; break;
30660786Sps#endif
30760786Sps#ifdef B200
30860786Sps		case B200: ospeed = 6; break;
30960786Sps#endif
31060786Sps#ifdef B300
31160786Sps		case B300: ospeed = 7; break;
31260786Sps#endif
31360786Sps#ifdef B600
31460786Sps		case B600: ospeed = 8; break;
31560786Sps#endif
31660786Sps#ifdef B1200
31760786Sps		case B1200: ospeed = 9; break;
31860786Sps#endif
31960786Sps#ifdef B1800
32060786Sps		case B1800: ospeed = 10; break;
32160786Sps#endif
32260786Sps#ifdef B2400
32360786Sps		case B2400: ospeed = 11; break;
32460786Sps#endif
32560786Sps#ifdef B4800
32660786Sps		case B4800: ospeed = 12; break;
32760786Sps#endif
32860786Sps#ifdef B9600
32960786Sps		case B9600: ospeed = 13; break;
33060786Sps#endif
33160786Sps#ifdef EXTA
33260786Sps		case EXTA: ospeed = 14; break;
33360786Sps#endif
33460786Sps#ifdef EXTB
33560786Sps		case EXTB: ospeed = 15; break;
33660786Sps#endif
33760786Sps#ifdef B57600
33860786Sps		case B57600: ospeed = 16; break;
33960786Sps#endif
34060786Sps#ifdef B115200
34160786Sps		case B115200: ospeed = 17; break;
34260786Sps#endif
34360786Sps		default: ;
34460786Sps		}
34560786Sps#endif
34660786Sps		erase_char = s.c_cc[VERASE];
347161478Sdelphij#ifdef VERASE2
348161478Sdelphij		erase2_char = s.c_cc[VERASE2];
349161478Sdelphij#endif
35060786Sps		kill_char = s.c_cc[VKILL];
35160786Sps#ifdef VWERASE
35260786Sps		werase_char = s.c_cc[VWERASE];
35360786Sps#else
35460786Sps		werase_char = CONTROL('W');
35560786Sps#endif
35660786Sps
35760786Sps		/*
35860786Sps		 * Set the modes to the way we want them.
35960786Sps		 */
36060786Sps		s.c_lflag &= ~(0
36160786Sps#ifdef ICANON
36260786Sps			| ICANON
36360786Sps#endif
36460786Sps#ifdef ECHO
36560786Sps			| ECHO
36660786Sps#endif
36760786Sps#ifdef ECHOE
36860786Sps			| ECHOE
36960786Sps#endif
37060786Sps#ifdef ECHOK
37160786Sps			| ECHOK
37260786Sps#endif
37360786Sps#if ECHONL
37460786Sps			| ECHONL
37560786Sps#endif
37660786Sps		);
37760786Sps
37860786Sps		s.c_oflag |= (0
37960786Sps#ifdef OXTABS
38060786Sps			| OXTABS
38160786Sps#else
38260786Sps#ifdef TAB3
38360786Sps			| TAB3
38460786Sps#else
38560786Sps#ifdef XTABS
38660786Sps			| XTABS
38760786Sps#endif
38860786Sps#endif
38960786Sps#endif
39060786Sps#ifdef OPOST
39160786Sps			| OPOST
39260786Sps#endif
39360786Sps#ifdef ONLCR
39460786Sps			| ONLCR
39560786Sps#endif
39660786Sps		);
39760786Sps
39860786Sps		s.c_oflag &= ~(0
39960786Sps#ifdef ONOEOT
40060786Sps			| ONOEOT
40160786Sps#endif
40260786Sps#ifdef OCRNL
40360786Sps			| OCRNL
40460786Sps#endif
40560786Sps#ifdef ONOCR
40660786Sps			| ONOCR
40760786Sps#endif
40860786Sps#ifdef ONLRET
40960786Sps			| ONLRET
41060786Sps#endif
41160786Sps		);
41260786Sps		s.c_cc[VMIN] = 1;
41360786Sps		s.c_cc[VTIME] = 0;
41460786Sps#ifdef VLNEXT
41560786Sps		s.c_cc[VLNEXT] = 0;
41660786Sps#endif
41760786Sps#ifdef VDSUSP
41860786Sps		s.c_cc[VDSUSP] = 0;
41960786Sps#endif
42060786Sps#if MUST_SET_LINE_DISCIPLINE
42160786Sps		/*
42260786Sps		 * System's termios is broken; need to explicitly
42360786Sps		 * request TERMIODISC line discipline.
42460786Sps		 */
42560786Sps		s.c_line = TERMIODISC;
42660786Sps#endif
42760786Sps	} else
42860786Sps	{
42960786Sps		/*
43060786Sps		 * Restore saved modes.
43160786Sps		 */
43260786Sps		s = save_term;
43360786Sps	}
43489022Sps#if HAVE_FSYNC
435128348Stjr	fsync(tty);
43689022Sps#endif
437128348Stjr	tcsetattr(tty, TCSADRAIN, &s);
43860786Sps#if MUST_SET_LINE_DISCIPLINE
43960786Sps	if (!on)
44060786Sps	{
44160786Sps		/*
44260786Sps		 * Broken termios *ignores* any line discipline
44360786Sps		 * except TERMIODISC.  A different old line discipline
44460786Sps		 * is therefore not restored, yet.  Restore the old
44560786Sps		 * line discipline by hand.
44660786Sps		 */
447128348Stjr		ioctl(tty, TIOCSETD, &save_term.c_line);
44860786Sps	}
44960786Sps#endif
45060786Sps    }
45160786Sps#else
45260786Sps#ifdef TCGETA
45360786Sps    {
45460786Sps	struct termio s;
45560786Sps	static struct termio save_term;
45660786Sps	static int saved_term = 0;
45760786Sps
45860786Sps	if (on)
45960786Sps	{
46060786Sps		/*
46160786Sps		 * Get terminal modes.
46260786Sps		 */
463128348Stjr		ioctl(tty, TCGETA, &s);
46460786Sps
46560786Sps		/*
46660786Sps		 * Save modes and set certain variables dependent on modes.
46760786Sps		 */
46860786Sps		if (!saved_term)
46960786Sps		{
47060786Sps			save_term = s;
47160786Sps			saved_term = 1;
47260786Sps		}
47360786Sps#if HAVE_OSPEED
47460786Sps		ospeed = s.c_cflag & CBAUD;
47560786Sps#endif
47660786Sps		erase_char = s.c_cc[VERASE];
47760786Sps		kill_char = s.c_cc[VKILL];
47860786Sps#ifdef VWERASE
47960786Sps		werase_char = s.c_cc[VWERASE];
48060786Sps#else
48160786Sps		werase_char = CONTROL('W');
48260786Sps#endif
48360786Sps
48460786Sps		/*
48560786Sps		 * Set the modes to the way we want them.
48660786Sps		 */
48760786Sps		s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
48860786Sps		s.c_oflag |=  (OPOST|ONLCR|TAB3);
48960786Sps		s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
49060786Sps		s.c_cc[VMIN] = 1;
49160786Sps		s.c_cc[VTIME] = 0;
49260786Sps	} else
49360786Sps	{
49460786Sps		/*
49560786Sps		 * Restore saved modes.
49660786Sps		 */
49760786Sps		s = save_term;
49860786Sps	}
499128348Stjr	ioctl(tty, TCSETAW, &s);
50060786Sps    }
50160786Sps#else
50260786Sps#ifdef TIOCGETP
50360786Sps    {
50460786Sps	struct sgttyb s;
50560786Sps	static struct sgttyb save_term;
50660786Sps	static int saved_term = 0;
50760786Sps
50860786Sps	if (on)
50960786Sps	{
51060786Sps		/*
51160786Sps		 * Get terminal modes.
51260786Sps		 */
513128348Stjr		ioctl(tty, TIOCGETP, &s);
51460786Sps
51560786Sps		/*
51660786Sps		 * Save modes and set certain variables dependent on modes.
51760786Sps		 */
51860786Sps		if (!saved_term)
51960786Sps		{
52060786Sps			save_term = s;
52160786Sps			saved_term = 1;
52260786Sps		}
52360786Sps#if HAVE_OSPEED
52460786Sps		ospeed = s.sg_ospeed;
52560786Sps#endif
52660786Sps		erase_char = s.sg_erase;
52760786Sps		kill_char = s.sg_kill;
52860786Sps		werase_char = CONTROL('W');
52960786Sps
53060786Sps		/*
53160786Sps		 * Set the modes to the way we want them.
53260786Sps		 */
53360786Sps		s.sg_flags |= CBREAK;
53460786Sps		s.sg_flags &= ~(ECHO|XTABS);
53560786Sps	} else
53660786Sps	{
53760786Sps		/*
53860786Sps		 * Restore saved modes.
53960786Sps		 */
54060786Sps		s = save_term;
54160786Sps	}
542128348Stjr	ioctl(tty, TIOCSETN, &s);
54360786Sps    }
54460786Sps#else
54560786Sps#ifdef _OSK
54660786Sps    {
54760786Sps	struct sgbuf s;
54860786Sps	static struct sgbuf save_term;
54960786Sps	static int saved_term = 0;
55060786Sps
55160786Sps	if (on)
55260786Sps	{
55360786Sps		/*
55460786Sps		 * Get terminal modes.
55560786Sps		 */
556128348Stjr		_gs_opt(tty, &s);
55760786Sps
55860786Sps		/*
55960786Sps		 * Save modes and set certain variables dependent on modes.
56060786Sps		 */
56160786Sps		if (!saved_term)
56260786Sps		{
56360786Sps			save_term = s;
56460786Sps			saved_term = 1;
56560786Sps		}
56660786Sps		erase_char = s.sg_bspch;
56760786Sps		kill_char = s.sg_dlnch;
56860786Sps		werase_char = CONTROL('W');
56960786Sps
57060786Sps		/*
57160786Sps		 * Set the modes to the way we want them.
57260786Sps		 */
57360786Sps		s.sg_echo = 0;
57460786Sps		s.sg_eofch = 0;
57560786Sps		s.sg_pause = 0;
57660786Sps		s.sg_psch = 0;
57760786Sps	} else
57860786Sps	{
57960786Sps		/*
58060786Sps		 * Restore saved modes.
58160786Sps		 */
58260786Sps		s = save_term;
58360786Sps	}
584128348Stjr	_ss_opt(tty, &s);
58560786Sps    }
58660786Sps#else
58760786Sps	/* MS-DOS, Windows, or OS2 */
58860786Sps#if OS2
58960786Sps	/* OS2 */
59060786Sps	LSIGNAL(SIGINT, SIG_IGN);
59160786Sps#endif
59260786Sps	erase_char = '\b';
59360786Sps#if MSDOS_COMPILER==DJGPPC
59460786Sps	kill_char = CONTROL('U');
59560786Sps	/*
59660786Sps	 * So that when we shell out or run another program, its
59760786Sps	 * stdin is in cooked mode.  We do not switch stdin to binary
59860786Sps	 * mode if fd0 is zero, since that means we were called before
59960786Sps	 * tty was reopened in open_getchr, in which case we would be
60060786Sps	 * changing the original stdin device outside less.
60160786Sps	 */
60260786Sps	if (fd0 != 0)
60360786Sps		setmode(0, on ? O_BINARY : O_TEXT);
60460786Sps#else
60560786Sps	kill_char = ESC;
60660786Sps#endif
60760786Sps	werase_char = CONTROL('W');
60860786Sps#endif
60960786Sps#endif
61060786Sps#endif
61160786Sps#endif
61260786Sps	curr_on = on;
61360786Sps}
61460786Sps
61560786Sps#if !MSDOS_COMPILER
61660786Sps/*
61760786Sps * Some glue to prevent calling termcap functions if tgetent() failed.
61860786Sps */
61960786Spsstatic int hardcopy;
62060786Sps
62160786Sps	static char *
62260786Spsltget_env(capname)
62360786Sps	char *capname;
62460786Sps{
62560786Sps	char name[16];
626170967Sdelphij	char *s;
62760786Sps
628170967Sdelphij	s = lgetenv("LESS_TERMCAP_DEBUG");
629170967Sdelphij	if (s != NULL && *s != '\0')
630170967Sdelphij	{
631170967Sdelphij		struct env { struct env *next; char *name; char *value; };
632170967Sdelphij		static struct env *envs = NULL;
633170967Sdelphij		struct env *p;
634170967Sdelphij		for (p = envs;  p != NULL;  p = p->next)
635170967Sdelphij			if (strcmp(p->name, capname) == 0)
636170967Sdelphij				return p->value;
637170967Sdelphij		p = (struct env *) ecalloc(1, sizeof(struct env));
638170967Sdelphij		p->name = save(capname);
639170967Sdelphij		p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
640170967Sdelphij		sprintf(p->value, "<%s>", capname);
641170967Sdelphij		p->next = envs;
642170967Sdelphij		envs = p;
643170967Sdelphij		return p->value;
644170967Sdelphij	}
64560786Sps	strcpy(name, "LESS_TERMCAP_");
64660786Sps	strcat(name, capname);
64760786Sps	return (lgetenv(name));
64860786Sps}
64960786Sps
65060786Sps	static int
65160786Spsltgetflag(capname)
65260786Sps	char *capname;
65360786Sps{
65460786Sps	char *s;
65560786Sps
65660786Sps	if ((s = ltget_env(capname)) != NULL)
65760786Sps		return (*s != '\0' && *s != '0');
65860786Sps	if (hardcopy)
65960786Sps		return (0);
66060786Sps	return (tgetflag(capname));
66160786Sps}
66260786Sps
66360786Sps	static int
66460786Spsltgetnum(capname)
66560786Sps	char *capname;
66660786Sps{
66760786Sps	char *s;
66860786Sps
66960786Sps	if ((s = ltget_env(capname)) != NULL)
67060786Sps		return (atoi(s));
67160786Sps	if (hardcopy)
67260786Sps		return (-1);
67360786Sps	return (tgetnum(capname));
67460786Sps}
67560786Sps
67660786Sps	static char *
67760786Spsltgetstr(capname, pp)
67860786Sps	char *capname;
67960786Sps	char **pp;
68060786Sps{
68160786Sps	char *s;
68260786Sps
68360786Sps	if ((s = ltget_env(capname)) != NULL)
68460786Sps		return (s);
68560786Sps	if (hardcopy)
68660786Sps		return (NULL);
68760786Sps	return (tgetstr(capname, pp));
68860786Sps}
68960786Sps#endif /* MSDOS_COMPILER */
69060786Sps
69160786Sps/*
69260786Sps * Get size of the output screen.
69360786Sps */
69460786Sps	public void
69560786Spsscrsize()
69660786Sps{
69760786Sps	register char *s;
69860786Sps	int sys_height;
69960786Sps	int sys_width;
70060786Sps#if !MSDOS_COMPILER
70160786Sps	int n;
70260786Sps#endif
70360786Sps
70460786Sps#define	DEF_SC_WIDTH	80
70560786Sps#if MSDOS_COMPILER
70660786Sps#define	DEF_SC_HEIGHT	25
70760786Sps#else
70860786Sps#define	DEF_SC_HEIGHT	24
70960786Sps#endif
71060786Sps
71160786Sps
71260786Sps	sys_width = sys_height = 0;
71360786Sps
71460786Sps#if MSDOS_COMPILER==MSOFTC
71560786Sps	{
71660786Sps		struct videoconfig w;
71760786Sps		_getvideoconfig(&w);
71860786Sps		sys_height = w.numtextrows;
71960786Sps		sys_width = w.numtextcols;
72060786Sps	}
72160786Sps#else
72260786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
72360786Sps	{
72460786Sps		struct text_info w;
72560786Sps		gettextinfo(&w);
72660786Sps		sys_height = w.screenheight;
72760786Sps		sys_width = w.screenwidth;
72860786Sps	}
72960786Sps#else
73060786Sps#if MSDOS_COMPILER==WIN32C
73160786Sps	{
73260786Sps		CONSOLE_SCREEN_BUFFER_INFO scr;
73360786Sps		GetConsoleScreenBufferInfo(con_out, &scr);
73460786Sps		sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
73560786Sps		sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
73660786Sps	}
73760786Sps#else
73860786Sps#if OS2
73960786Sps	{
74060786Sps		int s[2];
74160786Sps		_scrsize(s);
74260786Sps		sys_width = s[0];
74360786Sps		sys_height = s[1];
74489022Sps		/*
74589022Sps		 * When using terminal emulators for XFree86/OS2, the
74689022Sps		 * _scrsize function does not work well.
74789022Sps		 * Call the scrsize.exe program to get the window size.
74889022Sps		 */
74989022Sps		windowid = getenv("WINDOWID");
75089022Sps		if (windowid != NULL)
75189022Sps		{
75289022Sps			FILE *fd = popen("scrsize", "rt");
75389022Sps			if (fd != NULL)
75489022Sps			{
75589022Sps				int w, h;
75689022Sps				fscanf(fd, "%i %i", &w, &h);
75789022Sps				if (w > 0 && h > 0)
75889022Sps				{
75989022Sps					sys_width = w;
76089022Sps					sys_height = h;
76189022Sps				}
76289022Sps				pclose(fd);
76389022Sps			}
76489022Sps		}
76560786Sps	}
76660786Sps#else
76760786Sps#ifdef TIOCGWINSZ
76860786Sps	{
76960786Sps		struct winsize w;
77060786Sps		if (ioctl(2, TIOCGWINSZ, &w) == 0)
77160786Sps		{
77260786Sps			if (w.ws_row > 0)
77360786Sps				sys_height = w.ws_row;
77460786Sps			if (w.ws_col > 0)
77560786Sps				sys_width = w.ws_col;
77660786Sps		}
77760786Sps	}
77860786Sps#else
77960786Sps#ifdef WIOCGETD
78060786Sps	{
78160786Sps		struct uwdata w;
78260786Sps		if (ioctl(2, WIOCGETD, &w) == 0)
78360786Sps		{
78460786Sps			if (w.uw_height > 0)
78560786Sps				sys_height = w.uw_height / w.uw_vs;
78660786Sps			if (w.uw_width > 0)
78760786Sps				sys_width = w.uw_width / w.uw_hs;
78860786Sps		}
78960786Sps	}
79060786Sps#endif
79160786Sps#endif
79260786Sps#endif
79360786Sps#endif
79460786Sps#endif
79560786Sps#endif
79660786Sps
79760786Sps	if (sys_height > 0)
79860786Sps		sc_height = sys_height;
79960786Sps	else if ((s = lgetenv("LINES")) != NULL)
80060786Sps		sc_height = atoi(s);
80160786Sps#if !MSDOS_COMPILER
80260786Sps	else if ((n = ltgetnum("li")) > 0)
80360786Sps 		sc_height = n;
80460786Sps#endif
805251154Sdelphij	if (sc_height <= 0)
80660786Sps		sc_height = DEF_SC_HEIGHT;
80760786Sps
80860786Sps	if (sys_width > 0)
80960786Sps		sc_width = sys_width;
81060786Sps	else if ((s = lgetenv("COLUMNS")) != NULL)
81160786Sps		sc_width = atoi(s);
81260786Sps#if !MSDOS_COMPILER
81360786Sps	else if ((n = ltgetnum("co")) > 0)
81460786Sps 		sc_width = n;
81560786Sps#endif
816251154Sdelphij	if (sc_width <= 0)
81760786Sps		sc_width = DEF_SC_WIDTH;
81860786Sps}
81960786Sps
82060786Sps#if MSDOS_COMPILER==MSOFTC
82160786Sps/*
82260786Sps * Figure out how many empty loops it takes to delay a millisecond.
82360786Sps */
82460786Sps	static void
82560786Spsget_clock()
82660786Sps{
82760786Sps	clock_t start;
82860786Sps
82960786Sps	/*
83060786Sps	 * Get synchronized at the start of a tick.
83160786Sps	 */
83260786Sps	start = clock();
83360786Sps	while (clock() == start)
83460786Sps		;
83560786Sps	/*
83660786Sps	 * Now count loops till the next tick.
83760786Sps	 */
83860786Sps	start = clock();
83960786Sps	msec_loops = 0;
84060786Sps	while (clock() == start)
84160786Sps		msec_loops++;
84260786Sps	/*
84360786Sps	 * Convert from (loops per clock) to (loops per millisecond).
84460786Sps	 */
84560786Sps	msec_loops *= CLOCKS_PER_SEC;
84660786Sps	msec_loops /= 1000;
84760786Sps}
84860786Sps
84960786Sps/*
85060786Sps * Delay for a specified number of milliseconds.
85160786Sps */
85260786Sps	static void
85360786Spsdummy_func()
85460786Sps{
85560786Sps	static long delay_dummy = 0;
85660786Sps	delay_dummy++;
85760786Sps}
85860786Sps
85960786Sps	static void
86060786Spsdelay(msec)
86160786Sps	int msec;
86260786Sps{
86360786Sps	long i;
86460786Sps
86560786Sps	while (msec-- > 0)
86660786Sps	{
86760786Sps		for (i = 0;  i < msec_loops;  i++)
86860786Sps		{
86960786Sps			/*
87060786Sps			 * Make it look like we're doing something here,
87160786Sps			 * so the optimizer doesn't remove the whole loop.
87260786Sps			 */
87360786Sps			dummy_func();
87460786Sps		}
87560786Sps	}
87660786Sps}
87760786Sps#endif
87860786Sps
87960786Sps/*
88060786Sps * Return the characters actually input by a "special" key.
88160786Sps */
88260786Sps	public char *
88360786Spsspecial_key_str(key)
88460786Sps	int key;
88560786Sps{
88660786Sps	static char tbuf[40];
88760786Sps	char *s;
88889022Sps#if MSDOS_COMPILER || OS2
88960786Sps	static char k_right[]		= { '\340', PCK_RIGHT, 0 };
89060786Sps	static char k_left[]		= { '\340', PCK_LEFT, 0  };
89160786Sps	static char k_ctl_right[]	= { '\340', PCK_CTL_RIGHT, 0  };
89260786Sps	static char k_ctl_left[]	= { '\340', PCK_CTL_LEFT, 0  };
89360786Sps	static char k_insert[]		= { '\340', PCK_INSERT, 0  };
89460786Sps	static char k_delete[]		= { '\340', PCK_DELETE, 0  };
89560786Sps	static char k_ctl_delete[]	= { '\340', PCK_CTL_DELETE, 0  };
89660786Sps	static char k_ctl_backspace[]	= { '\177', 0 };
89760786Sps	static char k_home[]		= { '\340', PCK_HOME, 0 };
89860786Sps	static char k_end[]		= { '\340', PCK_END, 0 };
89960786Sps	static char k_up[]		= { '\340', PCK_UP, 0 };
90060786Sps	static char k_down[]		= { '\340', PCK_DOWN, 0 };
90160786Sps	static char k_backtab[]		= { '\340', PCK_SHIFT_TAB, 0 };
90260786Sps	static char k_pagedown[]	= { '\340', PCK_PAGEDOWN, 0 };
90360786Sps	static char k_pageup[]		= { '\340', PCK_PAGEUP, 0 };
90460786Sps	static char k_f1[]		= { '\340', PCK_F1, 0 };
90589022Sps#endif
90689022Sps#if !MSDOS_COMPILER
90760786Sps	char *sp = tbuf;
90860786Sps#endif
90960786Sps
91060786Sps	switch (key)
91160786Sps	{
91289022Sps#if OS2
91389022Sps	/*
91489022Sps	 * If windowid is not NULL, assume less is executed in
91589022Sps	 * the XFree86 environment.
91689022Sps	 */
91789022Sps	case SK_RIGHT_ARROW:
91889022Sps		s = windowid ? ltgetstr("kr", &sp) : k_right;
91989022Sps		break;
92089022Sps	case SK_LEFT_ARROW:
92189022Sps		s = windowid ? ltgetstr("kl", &sp) : k_left;
92289022Sps		break;
92389022Sps	case SK_UP_ARROW:
92489022Sps		s = windowid ? ltgetstr("ku", &sp) : k_up;
92589022Sps		break;
92689022Sps	case SK_DOWN_ARROW:
92789022Sps		s = windowid ? ltgetstr("kd", &sp) : k_down;
92889022Sps		break;
92989022Sps	case SK_PAGE_UP:
93089022Sps		s = windowid ? ltgetstr("kP", &sp) : k_pageup;
93189022Sps		break;
93289022Sps	case SK_PAGE_DOWN:
93389022Sps		s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
93489022Sps		break;
93589022Sps	case SK_HOME:
93689022Sps		s = windowid ? ltgetstr("kh", &sp) : k_home;
93789022Sps		break;
93889022Sps	case SK_END:
93989022Sps		s = windowid ? ltgetstr("@7", &sp) : k_end;
94089022Sps		break;
94189022Sps	case SK_DELETE:
94289022Sps		if (windowid)
94389022Sps		{
94489022Sps			s = ltgetstr("kD", &sp);
94589022Sps			if (s == NULL)
94689022Sps			{
94789022Sps				tbuf[0] = '\177';
94889022Sps				tbuf[1] = '\0';
94989022Sps				s = tbuf;
95089022Sps			}
95189022Sps		} else
95289022Sps			s = k_delete;
95389022Sps		break;
95489022Sps#endif
95560786Sps#if MSDOS_COMPILER
95660786Sps	case SK_RIGHT_ARROW:
95760786Sps		s = k_right;
95860786Sps		break;
95960786Sps	case SK_LEFT_ARROW:
96060786Sps		s = k_left;
96160786Sps		break;
96260786Sps	case SK_UP_ARROW:
96360786Sps		s = k_up;
96460786Sps		break;
96560786Sps	case SK_DOWN_ARROW:
96660786Sps		s = k_down;
96760786Sps		break;
96860786Sps	case SK_PAGE_UP:
96960786Sps		s = k_pageup;
97060786Sps		break;
97160786Sps	case SK_PAGE_DOWN:
97260786Sps		s = k_pagedown;
97360786Sps		break;
97460786Sps	case SK_HOME:
97560786Sps		s = k_home;
97660786Sps		break;
97760786Sps	case SK_END:
97860786Sps		s = k_end;
97960786Sps		break;
98060786Sps	case SK_DELETE:
98160786Sps		s = k_delete;
98260786Sps		break;
98389022Sps#endif
98489022Sps#if MSDOS_COMPILER || OS2
98560786Sps	case SK_INSERT:
98660786Sps		s = k_insert;
98760786Sps		break;
98860786Sps	case SK_CTL_LEFT_ARROW:
98960786Sps		s = k_ctl_left;
99060786Sps		break;
99160786Sps	case SK_CTL_RIGHT_ARROW:
99260786Sps		s = k_ctl_right;
99360786Sps		break;
99460786Sps	case SK_CTL_BACKSPACE:
99560786Sps		s = k_ctl_backspace;
99660786Sps		break;
99760786Sps	case SK_CTL_DELETE:
99860786Sps		s = k_ctl_delete;
99960786Sps		break;
100060786Sps	case SK_F1:
100160786Sps		s = k_f1;
100260786Sps		break;
100360786Sps	case SK_BACKTAB:
100460786Sps		s = k_backtab;
100560786Sps		break;
100660786Sps#else
100760786Sps	case SK_RIGHT_ARROW:
100860786Sps		s = ltgetstr("kr", &sp);
100960786Sps		break;
101060786Sps	case SK_LEFT_ARROW:
101160786Sps		s = ltgetstr("kl", &sp);
101260786Sps		break;
101360786Sps	case SK_UP_ARROW:
101460786Sps		s = ltgetstr("ku", &sp);
101560786Sps		break;
101660786Sps	case SK_DOWN_ARROW:
101760786Sps		s = ltgetstr("kd", &sp);
101860786Sps		break;
101960786Sps	case SK_PAGE_UP:
102060786Sps		s = ltgetstr("kP", &sp);
102160786Sps		break;
102260786Sps	case SK_PAGE_DOWN:
102360786Sps		s = ltgetstr("kN", &sp);
102460786Sps		break;
102560786Sps	case SK_HOME:
102660786Sps		s = ltgetstr("kh", &sp);
102760786Sps		break;
102860786Sps	case SK_END:
102960786Sps		s = ltgetstr("@7", &sp);
103060786Sps		break;
103160786Sps	case SK_DELETE:
103260786Sps		s = ltgetstr("kD", &sp);
103360786Sps		if (s == NULL)
103460786Sps		{
103560786Sps			tbuf[0] = '\177';
103660786Sps			tbuf[1] = '\0';
103760786Sps			s = tbuf;
103860786Sps		}
103960786Sps		break;
104060786Sps#endif
104160786Sps	case SK_CONTROL_K:
104260786Sps		tbuf[0] = CONTROL('K');
104360786Sps		tbuf[1] = '\0';
104460786Sps		s = tbuf;
104560786Sps		break;
104660786Sps	default:
104760786Sps		return (NULL);
104860786Sps	}
104960786Sps	return (s);
105060786Sps}
105160786Sps
105260786Sps/*
105360786Sps * Get terminal capabilities via termcap.
105460786Sps */
105560786Sps	public void
105660786Spsget_term()
105760786Sps{
105860786Sps#if MSDOS_COMPILER
105960786Sps	auto_wrap = 1;
106060786Sps	ignaw = 0;
106160786Sps	can_goto_line = 1;
106260786Sps	clear_bg = 1;
106360786Sps	/*
106460786Sps	 * Set up default colors.
106560786Sps	 * The xx_s_width and xx_e_width vars are already initialized to 0.
106660786Sps	 */
106760786Sps#if MSDOS_COMPILER==MSOFTC
106860786Sps	sy_bg_color = _getbkcolor();
106960786Sps	sy_fg_color = _gettextcolor();
107060786Sps	get_clock();
107160786Sps#else
107260786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
107360786Sps    {
107460786Sps	struct text_info w;
107560786Sps	gettextinfo(&w);
107660786Sps	sy_bg_color = (w.attribute >> 4) & 0x0F;
107760786Sps	sy_fg_color = (w.attribute >> 0) & 0x0F;
107860786Sps    }
107960786Sps#else
108060786Sps#if MSDOS_COMPILER==WIN32C
108160786Sps    {
108260786Sps	DWORD nread;
108360786Sps	CONSOLE_SCREEN_BUFFER_INFO scr;
108460786Sps
108560786Sps	con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
108660786Sps	/*
108760786Sps	 * Always open stdin in binary. Note this *must* be done
108860786Sps	 * before any file operations have been done on fd0.
108960786Sps	 */
109060786Sps	SET_BINARY(0);
109160786Sps	GetConsoleScreenBufferInfo(con_out, &scr);
109260786Sps	ReadConsoleOutputAttribute(con_out, &curr_attr,
109360786Sps					1, scr.dwCursorPosition, &nread);
109460786Sps	sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
109560786Sps	sy_fg_color = curr_attr & FG_COLORS;
109660786Sps    }
109760786Sps#endif
109860786Sps#endif
109960786Sps#endif
110060786Sps	nm_fg_color = sy_fg_color;
110160786Sps	nm_bg_color = sy_bg_color;
110260786Sps	bo_fg_color = 11;
110360786Sps	bo_bg_color = 0;
110460786Sps	ul_fg_color = 9;
110560786Sps	ul_bg_color = 0;
110660786Sps	so_fg_color = 15;
110760786Sps	so_bg_color = 9;
110860786Sps	bl_fg_color = 15;
110960786Sps	bl_bg_color = 0;
111060786Sps
111160786Sps	/*
111260786Sps	 * Get size of the screen.
111360786Sps	 */
111460786Sps	scrsize();
111560786Sps	pos_init();
111660786Sps
111760786Sps
111860786Sps#else /* !MSDOS_COMPILER */
111960786Sps
112060786Sps	char *sp;
112160786Sps	register char *t1, *t2;
112260786Sps	char *term;
112360786Sps	char termbuf[TERMBUF_SIZE];
112460786Sps
112560786Sps	static char sbuf[TERMSBUF_SIZE];
112660786Sps
112760786Sps#if OS2
112860786Sps	/*
112960786Sps	 * Make sure the termcap database is available.
113060786Sps	 */
113160786Sps	sp = lgetenv("TERMCAP");
113260786Sps	if (sp == NULL || *sp == '\0')
113360786Sps	{
113460786Sps		char *termcap;
113560786Sps		if ((sp = homefile("termcap.dat")) != NULL)
113660786Sps		{
113760786Sps			termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
113860786Sps			sprintf(termcap, "TERMCAP=%s", sp);
113960786Sps			free(sp);
114060786Sps			putenv(termcap);
114160786Sps		}
114260786Sps	}
114360786Sps#endif
114460786Sps	/*
114560786Sps	 * Find out what kind of terminal this is.
114660786Sps	 */
114760786Sps 	if ((term = lgetenv("TERM")) == NULL)
114860786Sps 		term = DEFAULT_TERM;
114960786Sps	hardcopy = 0;
1150170259Sdelphij 	if (tgetent(termbuf, term) != TGETENT_OK)
115160786Sps 		hardcopy = 1;
115260786Sps 	if (ltgetflag("hc"))
115360786Sps		hardcopy = 1;
115460786Sps
115560786Sps	/*
115660786Sps	 * Get size of the screen.
115760786Sps	 */
115860786Sps	scrsize();
115960786Sps	pos_init();
116060786Sps
116160786Sps	auto_wrap = ltgetflag("am");
116260786Sps	ignaw = ltgetflag("xn");
116360786Sps	above_mem = ltgetflag("da");
116460786Sps	below_mem = ltgetflag("db");
116560786Sps	clear_bg = ltgetflag("ut");
116660786Sps
116760786Sps	/*
116860786Sps	 * Assumes termcap variable "sg" is the printing width of:
116960786Sps	 * the standout sequence, the end standout sequence,
117060786Sps	 * the underline sequence, the end underline sequence,
117160786Sps	 * the boldface sequence, and the end boldface sequence.
117260786Sps	 */
117360786Sps	if ((so_s_width = ltgetnum("sg")) < 0)
117460786Sps		so_s_width = 0;
117560786Sps	so_e_width = so_s_width;
117660786Sps
117760786Sps	bo_s_width = bo_e_width = so_s_width;
117860786Sps	ul_s_width = ul_e_width = so_s_width;
117960786Sps	bl_s_width = bl_e_width = so_s_width;
118060786Sps
118160786Sps#if HILITE_SEARCH
118260786Sps	if (so_s_width > 0 || so_e_width > 0)
118360786Sps		/*
118460786Sps		 * Disable highlighting by default on magic cookie terminals.
118560786Sps		 * Turning on highlighting might change the displayed width
118660786Sps		 * of a line, causing the display to get messed up.
118760786Sps		 * The user can turn it back on with -g,
118860786Sps		 * but she won't like the results.
118960786Sps		 */
119060786Sps		hilite_search = 0;
119160786Sps#endif
119260786Sps
119360786Sps	/*
119460786Sps	 * Get various string-valued capabilities.
119560786Sps	 */
119660786Sps	sp = sbuf;
119760786Sps
119860786Sps#if HAVE_OSPEED
119960786Sps	sc_pad = ltgetstr("pc", &sp);
120060786Sps	if (sc_pad != NULL)
120160786Sps		PC = *sc_pad;
120260786Sps#endif
120360786Sps
120460786Sps	sc_s_keypad = ltgetstr("ks", &sp);
120560786Sps	if (sc_s_keypad == NULL)
120660786Sps		sc_s_keypad = "";
120760786Sps	sc_e_keypad = ltgetstr("ke", &sp);
120860786Sps	if (sc_e_keypad == NULL)
120960786Sps		sc_e_keypad = "";
121060786Sps
1211171009Sdelphij	sc_init = ltgetstr("ti", &sp);
121260786Sps	if (sc_init == NULL)
121360786Sps		sc_init = "";
121460786Sps
1215171009Sdelphij	sc_deinit= ltgetstr("te", &sp);
121660786Sps	if (sc_deinit == NULL)
121760786Sps		sc_deinit = "";
121860786Sps
121960786Sps	sc_eol_clear = ltgetstr("ce", &sp);
122060786Sps	if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
122160786Sps	{
122260786Sps		missing_cap = 1;
122360786Sps		sc_eol_clear = "";
122460786Sps	}
122560786Sps
122660786Sps	sc_eos_clear = ltgetstr("cd", &sp);
122760786Sps	if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
122860786Sps	{
122960786Sps		missing_cap = 1;
1230161478Sdelphij		sc_eos_clear = "";
123160786Sps	}
123260786Sps
123360786Sps	sc_clear = ltgetstr("cl", &sp);
123460786Sps	if (sc_clear == NULL || *sc_clear == '\0')
123560786Sps	{
123660786Sps		missing_cap = 1;
123760786Sps		sc_clear = "\n\n";
123860786Sps	}
123960786Sps
124060786Sps	sc_move = ltgetstr("cm", &sp);
124160786Sps	if (sc_move == NULL || *sc_move == '\0')
124260786Sps	{
124360786Sps		/*
124460786Sps		 * This is not an error here, because we don't
124560786Sps		 * always need sc_move.
124660786Sps		 * We need it only if we don't have home or lower-left.
124760786Sps		 */
124860786Sps		sc_move = "";
124960786Sps		can_goto_line = 0;
125060786Sps	} else
125160786Sps		can_goto_line = 1;
125260786Sps
125360786Sps	tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
125460786Sps	tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
125560786Sps	tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
125660786Sps	tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
125760786Sps
125860786Sps	sc_visual_bell = ltgetstr("vb", &sp);
125960786Sps	if (sc_visual_bell == NULL)
126060786Sps		sc_visual_bell = "";
126160786Sps
126260786Sps	if (ltgetflag("bs"))
126360786Sps		sc_backspace = "\b";
126460786Sps	else
126560786Sps	{
126660786Sps		sc_backspace = ltgetstr("bc", &sp);
126760786Sps		if (sc_backspace == NULL || *sc_backspace == '\0')
126860786Sps			sc_backspace = "\b";
126960786Sps	}
127060786Sps
127160786Sps	/*
127260786Sps	 * Choose between using "ho" and "cm" ("home" and "cursor move")
127360786Sps	 * to move the cursor to the upper left corner of the screen.
127460786Sps	 */
127560786Sps	t1 = ltgetstr("ho", &sp);
127660786Sps	if (t1 == NULL)
127760786Sps		t1 = "";
127860786Sps	if (*sc_move == '\0')
127960786Sps		t2 = "";
128060786Sps	else
128160786Sps	{
128260786Sps		strcpy(sp, tgoto(sc_move, 0, 0));
128360786Sps		t2 = sp;
128460786Sps		sp += strlen(sp) + 1;
128560786Sps	}
128660786Sps	sc_home = cheaper(t1, t2, "|\b^");
128760786Sps
128860786Sps	/*
128960786Sps	 * Choose between using "ll" and "cm"  ("lower left" and "cursor move")
129060786Sps	 * to move the cursor to the lower left corner of the screen.
129160786Sps	 */
129260786Sps	t1 = ltgetstr("ll", &sp);
129360786Sps	if (t1 == NULL)
129460786Sps		t1 = "";
129560786Sps	if (*sc_move == '\0')
129660786Sps		t2 = "";
129760786Sps	else
129860786Sps	{
129960786Sps		strcpy(sp, tgoto(sc_move, 0, sc_height-1));
130060786Sps		t2 = sp;
130160786Sps		sp += strlen(sp) + 1;
130260786Sps	}
130360786Sps	sc_lower_left = cheaper(t1, t2, "\r");
130460786Sps
130560786Sps	/*
1306170259Sdelphij	 * Get carriage return string.
1307170259Sdelphij	 */
1308170259Sdelphij	sc_return = ltgetstr("cr", &sp);
1309170259Sdelphij	if (sc_return == NULL)
1310170259Sdelphij		sc_return = "\r";
1311170259Sdelphij
1312170259Sdelphij	/*
131360786Sps	 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
131460786Sps	 * to add a line at the top of the screen.
131560786Sps	 */
131660786Sps	t1 = ltgetstr("al", &sp);
131760786Sps	if (t1 == NULL)
131860786Sps		t1 = "";
131960786Sps	t2 = ltgetstr("sr", &sp);
132060786Sps	if (t2 == NULL)
132160786Sps		t2 = "";
132260786Sps#if OS2
132360786Sps	if (*t1 == '\0' && *t2 == '\0')
132460786Sps		sc_addline = "";
132560786Sps	else
132660786Sps#endif
132760786Sps	if (above_mem)
132860786Sps		sc_addline = t1;
132960786Sps	else
133060786Sps		sc_addline = cheaper(t1, t2, "");
133160786Sps	if (*sc_addline == '\0')
133260786Sps	{
133360786Sps		/*
133460786Sps		 * Force repaint on any backward movement.
133560786Sps		 */
133660786Sps		no_back_scroll = 1;
133760786Sps	}
133860786Sps#endif /* MSDOS_COMPILER */
133960786Sps}
134060786Sps
134160786Sps#if !MSDOS_COMPILER
134260786Sps/*
134360786Sps * Return the cost of displaying a termcap string.
134460786Sps * We use the trick of calling tputs, but as a char printing function
134560786Sps * we give it inc_costcount, which just increments "costcount".
134660786Sps * This tells us how many chars would be printed by using this string.
134760786Sps * {{ Couldn't we just use strlen? }}
134860786Sps */
134960786Spsstatic int costcount;
135060786Sps
135160786Sps/*ARGSUSED*/
135260786Sps	static int
135360786Spsinc_costcount(c)
135460786Sps	int c;
135560786Sps{
135660786Sps	costcount++;
135760786Sps	return (c);
135860786Sps}
135960786Sps
136060786Sps	static int
136160786Spscost(t)
136260786Sps	char *t;
136360786Sps{
136460786Sps	costcount = 0;
136560786Sps	tputs(t, sc_height, inc_costcount);
136660786Sps	return (costcount);
136760786Sps}
136860786Sps
136960786Sps/*
137060786Sps * Return the "best" of the two given termcap strings.
137160786Sps * The best, if both exist, is the one with the lower
137260786Sps * cost (see cost() function).
137360786Sps */
137460786Sps	static char *
137560786Spscheaper(t1, t2, def)
137660786Sps	char *t1, *t2;
137760786Sps	char *def;
137860786Sps{
137960786Sps	if (*t1 == '\0' && *t2 == '\0')
138060786Sps	{
138160786Sps		missing_cap = 1;
138260786Sps		return (def);
138360786Sps	}
138460786Sps	if (*t1 == '\0')
138560786Sps		return (t2);
138660786Sps	if (*t2 == '\0')
138760786Sps		return (t1);
138860786Sps	if (cost(t1) < cost(t2))
138960786Sps		return (t1);
139060786Sps	return (t2);
139160786Sps}
139260786Sps
139360786Sps	static void
139460786Spstmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
139560786Sps	char *incap;
139660786Sps	char *outcap;
139760786Sps	char **instr;
139860786Sps	char **outstr;
139960786Sps	char *def_instr;
140060786Sps	char *def_outstr;
140160786Sps	char **spp;
140260786Sps{
140360786Sps	*instr = ltgetstr(incap, spp);
140460786Sps	if (*instr == NULL)
140560786Sps	{
140660786Sps		/* Use defaults. */
140760786Sps		*instr = def_instr;
140860786Sps		*outstr = def_outstr;
140960786Sps		return;
141060786Sps	}
141160786Sps
141260786Sps	*outstr = ltgetstr(outcap, spp);
141360786Sps	if (*outstr == NULL)
141460786Sps		/* No specific out capability; use "me". */
141560786Sps		*outstr = ltgetstr("me", spp);
141660786Sps	if (*outstr == NULL)
141760786Sps		/* Don't even have "me"; use a null string. */
141860786Sps		*outstr = "";
141960786Sps}
142060786Sps
142160786Sps#endif /* MSDOS_COMPILER */
142260786Sps
142360786Sps
142460786Sps/*
142560786Sps * Below are the functions which perform all the
142660786Sps * terminal-specific screen manipulation.
142760786Sps */
142860786Sps
142960786Sps
143060786Sps#if MSDOS_COMPILER
143160786Sps
143260786Sps#if MSDOS_COMPILER==WIN32C
143360786Sps	static void
143460786Sps_settextposition(int row, int col)
143560786Sps{
143660786Sps	COORD cpos;
143760786Sps	CONSOLE_SCREEN_BUFFER_INFO csbi;
143860786Sps
143960786Sps	GetConsoleScreenBufferInfo(con_out, &csbi);
144060786Sps	cpos.X = csbi.srWindow.Left + (col - 1);
144160786Sps	cpos.Y = csbi.srWindow.Top + (row - 1);
144260786Sps	SetConsoleCursorPosition(con_out, cpos);
144360786Sps}
144460786Sps#endif
144560786Sps
144660786Sps/*
144760786Sps * Initialize the screen to the correct color at startup.
144860786Sps */
144960786Sps	static void
145060786Spsinitcolor()
145160786Sps{
145260786Sps	SETCOLORS(nm_fg_color, nm_bg_color);
145360786Sps#if 0
145460786Sps	/*
145560786Sps	 * This clears the screen at startup.  This is different from
145660786Sps	 * the behavior of other versions of less.  Disable it for now.
145760786Sps	 */
145860786Sps	char *blanks;
145960786Sps	int row;
146060786Sps	int col;
146160786Sps
146260786Sps	/*
146360786Sps	 * Create a complete, blank screen using "normal" colors.
146460786Sps	 */
146560786Sps	SETCOLORS(nm_fg_color, nm_bg_color);
146660786Sps	blanks = (char *) ecalloc(width+1, sizeof(char));
146760786Sps	for (col = 0;  col < sc_width;  col++)
146860786Sps		blanks[col] = ' ';
146960786Sps	blanks[sc_width] = '\0';
147060786Sps	for (row = 0;  row < sc_height;  row++)
147160786Sps		_outtext(blanks);
147260786Sps	free(blanks);
147360786Sps#endif
147460786Sps}
147560786Sps#endif
147660786Sps
147760786Sps#if MSDOS_COMPILER==WIN32C
147860786Sps
147960786Sps/*
148060786Sps * Termcap-like init with a private win32 console.
148160786Sps */
148260786Sps	static void
148360786Spswin32_init_term()
148460786Sps{
148560786Sps	CONSOLE_SCREEN_BUFFER_INFO scr;
148660786Sps	COORD size;
148760786Sps
148860786Sps	if (con_out_save == INVALID_HANDLE_VALUE)
148960786Sps		return;
149060786Sps
149160786Sps	GetConsoleScreenBufferInfo(con_out_save, &scr);
149260786Sps
149360786Sps	if (con_out_ours == INVALID_HANDLE_VALUE)
149460786Sps	{
149560786Sps		/*
149660786Sps		 * Create our own screen buffer, so that we
149760786Sps		 * may restore the original when done.
149860786Sps		 */
149960786Sps		con_out_ours = CreateConsoleScreenBuffer(
150060786Sps			GENERIC_WRITE | GENERIC_READ,
150160786Sps			FILE_SHARE_WRITE | FILE_SHARE_READ,
150260786Sps			(LPSECURITY_ATTRIBUTES) NULL,
150360786Sps			CONSOLE_TEXTMODE_BUFFER,
150460786Sps			(LPVOID) NULL);
150560786Sps	}
150660786Sps
150760786Sps	size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
150860786Sps	size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
150960786Sps	SetConsoleScreenBufferSize(con_out_ours, size);
151060786Sps	SetConsoleActiveScreenBuffer(con_out_ours);
151160786Sps	con_out = con_out_ours;
151260786Sps}
151360786Sps
151460786Sps/*
151560786Sps * Restore the startup console.
151660786Sps */
151760786Spsstatic void
151860786Spswin32_deinit_term()
151960786Sps{
152060786Sps	if (con_out_save == INVALID_HANDLE_VALUE)
152160786Sps		return;
152260786Sps	if (quitting)
152360786Sps		(void) CloseHandle(con_out_ours);
152460786Sps	SetConsoleActiveScreenBuffer(con_out_save);
152560786Sps	con_out = con_out_save;
152660786Sps}
152760786Sps
152860786Sps#endif
152960786Sps
153060786Sps/*
153160786Sps * Initialize terminal
153260786Sps */
153360786Sps	public void
153460786Spsinit()
153560786Sps{
153660786Sps#if !MSDOS_COMPILER
153789022Sps	if (!no_init)
153889022Sps		tputs(sc_init, sc_height, putchr);
153989022Sps	if (!no_keypad)
154089022Sps		tputs(sc_s_keypad, sc_height, putchr);
1541161478Sdelphij	if (top_scroll)
1542161478Sdelphij	{
1543161478Sdelphij		int i;
1544161478Sdelphij
1545161478Sdelphij		/*
1546161478Sdelphij		 * This is nice to terminals with no alternate screen,
1547161478Sdelphij		 * but with saved scrolled-off-the-top lines.  This way,
1548161478Sdelphij		 * no previous line is lost, but we start with a whole
1549161478Sdelphij		 * screen to ourself.
1550161478Sdelphij		 */
1551161478Sdelphij		for (i = 1; i < sc_height; i++)
1552161478Sdelphij			putchr('\n');
1553191930Sdelphij	} else
1554191930Sdelphij		line_left();
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;
1791221715Sdelphij	new_org.Y = rcClip.Top;
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
2473191930Sdelphij
2474191930Sdelphij#if MSDOS_COMPILER
2475191930Sdelphij/*
2476191930Sdelphij */
2477191930Sdelphij	public void
2478191930SdelphijWIN32setcolors(fg, bg)
2479191930Sdelphij	int fg;
2480191930Sdelphij	int bg;
2481191930Sdelphij{
2482191930Sdelphij	SETCOLORS(fg, bg);
2483191930Sdelphij}
2484191930Sdelphij
2485191930Sdelphij/*
2486191930Sdelphij */
2487191930Sdelphij	public void
2488191930SdelphijWIN32textout(text, len)
2489191930Sdelphij	char *text;
2490191930Sdelphij	int len;
2491191930Sdelphij{
2492191930Sdelphij#if MSDOS_COMPILER==WIN32C
2493191930Sdelphij	DWORD written;
2494191930Sdelphij	WriteConsole(con_out, text, len, &written, NULL);
2495191930Sdelphij#else
2496191930Sdelphij	char c = text[len];
2497191930Sdelphij	text[len] = '\0';
2498191930Sdelphij	cputs(text);
2499191930Sdelphij	text[len] = c;
2500191930Sdelphij#endif
2501191930Sdelphij}
2502191930Sdelphij#endif
2503