screen.c revision 191930
160812Sps/* $FreeBSD: head/contrib/less/screen.c 191930 2009-05-09 01:35:27Z delphij $ */
260786Sps/*
3191930Sdelphij * Copyright (C) 1984-2008  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');
1554191930Sdelphij	} else
1555191930Sdelphij		line_left();
155660786Sps#else
155760786Sps#if MSDOS_COMPILER==WIN32C
155889022Sps	if (!no_init)
155989022Sps		win32_init_term();
156060786Sps#endif
156160786Sps	initcolor();
156260786Sps	flush();
156360786Sps#endif
156460786Sps	init_done = 1;
156560786Sps}
156660786Sps
156760786Sps/*
156860786Sps * Deinitialize terminal
156960786Sps */
157060786Sps	public void
157160786Spsdeinit()
157260786Sps{
157360786Sps	if (!init_done)
157460786Sps		return;
157560786Sps#if !MSDOS_COMPILER
157689022Sps	if (!no_keypad)
157789022Sps		tputs(sc_e_keypad, sc_height, putchr);
157889022Sps	if (!no_init)
157989022Sps		tputs(sc_deinit, sc_height, putchr);
158060786Sps#else
158189022Sps	/* Restore system colors. */
158260786Sps	SETCOLORS(sy_fg_color, sy_bg_color);
158360786Sps#if MSDOS_COMPILER==WIN32C
158489022Sps	if (!no_init)
158589022Sps		win32_deinit_term();
158689022Sps#else
158789022Sps	/* Need clreol to make SETCOLORS take effect. */
158889022Sps	clreol();
158960786Sps#endif
159060786Sps#endif
159160786Sps	init_done = 0;
159260786Sps}
159360786Sps
159460786Sps/*
159560786Sps * Home cursor (move to upper left corner of screen).
159660786Sps */
159760786Sps	public void
159860786Spshome()
159960786Sps{
160060786Sps#if !MSDOS_COMPILER
160160786Sps	tputs(sc_home, 1, putchr);
160260786Sps#else
160360786Sps	flush();
160460786Sps	_settextposition(1,1);
160560786Sps#endif
160660786Sps}
160760786Sps
160860786Sps/*
160960786Sps * Add a blank line (called with cursor at home).
161060786Sps * Should scroll the display down.
161160786Sps */
161260786Sps	public void
161360786Spsadd_line()
161460786Sps{
161560786Sps#if !MSDOS_COMPILER
161660786Sps	tputs(sc_addline, sc_height, putchr);
161760786Sps#else
161860786Sps	flush();
161960786Sps#if MSDOS_COMPILER==MSOFTC
162060786Sps	_scrolltextwindow(_GSCROLLDOWN);
162160786Sps	_settextposition(1,1);
162260786Sps#else
162360786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
162460786Sps	movetext(1,1, sc_width,sc_height-1, 1,2);
162560786Sps	gotoxy(1,1);
162660786Sps	clreol();
162760786Sps#else
162860786Sps#if MSDOS_COMPILER==WIN32C
162960786Sps    {
163060786Sps	CHAR_INFO fillchar;
163160786Sps	SMALL_RECT rcSrc, rcClip;
163260786Sps	COORD new_org;
163360786Sps	CONSOLE_SCREEN_BUFFER_INFO csbi;
163460786Sps
163560786Sps	GetConsoleScreenBufferInfo(con_out,&csbi);
163660786Sps
163760786Sps	/* The clip rectangle is the entire visible screen. */
163860786Sps	rcClip.Left = csbi.srWindow.Left;
163960786Sps	rcClip.Top = csbi.srWindow.Top;
164060786Sps	rcClip.Right = csbi.srWindow.Right;
164160786Sps	rcClip.Bottom = csbi.srWindow.Bottom;
164260786Sps
164360786Sps	/* The source rectangle is the visible screen minus the last line. */
164460786Sps	rcSrc = rcClip;
164560786Sps	rcSrc.Bottom--;
164660786Sps
164760786Sps	/* Move the top left corner of the source window down one row. */
164860786Sps	new_org.X = rcSrc.Left;
164960786Sps	new_org.Y = rcSrc.Top + 1;
165060786Sps
165160786Sps	/* Fill the right character and attributes. */
165260786Sps	fillchar.Char.AsciiChar = ' ';
165360786Sps	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
165460786Sps	fillchar.Attributes = curr_attr;
165560786Sps	ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
165660786Sps	_settextposition(1,1);
165760786Sps    }
165860786Sps#endif
165960786Sps#endif
166060786Sps#endif
166160786Sps#endif
166260786Sps}
166360786Sps
166489022Sps#if 0
166560786Sps/*
166660786Sps * Remove the n topmost lines and scroll everything below it in the
166760786Sps * window upward.  This is needed to stop leaking the topmost line
166860786Sps * into the scrollback buffer when we go down-one-line (in WIN32).
166960786Sps */
167060786Sps	public void
167160786Spsremove_top(n)
167260786Sps	int n;
167360786Sps{
167460786Sps#if MSDOS_COMPILER==WIN32C
167560786Sps	SMALL_RECT rcSrc, rcClip;
167660786Sps	CHAR_INFO fillchar;
167760786Sps	COORD new_org;
167860786Sps	CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
167960786Sps
168060786Sps	if (n >= sc_height - 1)
168160786Sps	{
168260786Sps		clear();
168360786Sps		home();
168460786Sps		return;
168560786Sps	}
168660786Sps
168760786Sps	flush();
168860786Sps
168960786Sps	GetConsoleScreenBufferInfo(con_out, &csbi);
169060786Sps
169160786Sps	/* Get the extent of all-visible-rows-but-the-last. */
169260786Sps	rcSrc.Left    = csbi.srWindow.Left;
169360786Sps	rcSrc.Top     = csbi.srWindow.Top + n;
169460786Sps	rcSrc.Right   = csbi.srWindow.Right;
169560786Sps	rcSrc.Bottom  = csbi.srWindow.Bottom;
169660786Sps
169760786Sps	/* Get the clip rectangle. */
169860786Sps	rcClip.Left   = rcSrc.Left;
169960786Sps	rcClip.Top    = csbi.srWindow.Top;
170060786Sps	rcClip.Right  = rcSrc.Right;
170160786Sps	rcClip.Bottom = rcSrc.Bottom ;
170260786Sps
170360786Sps	/* Move the source window up n rows. */
170460786Sps	new_org.X = rcSrc.Left;
170560786Sps	new_org.Y = rcSrc.Top - n;
170660786Sps
170760786Sps	/* Fill the right character and attributes. */
170860786Sps	fillchar.Char.AsciiChar = ' ';
170960786Sps	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
171060786Sps	fillchar.Attributes = curr_attr;
171160786Sps
171260786Sps	ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
171360786Sps
171460786Sps	/* Position cursor on first blank line. */
171560786Sps	goto_line(sc_height - n - 1);
171660786Sps#endif
171760786Sps}
171889022Sps#endif
171960786Sps
172089022Sps#if MSDOS_COMPILER==WIN32C
172160786Sps/*
172289022Sps * Clear the screen.
172389022Sps */
172489022Sps	static void
172589022Spswin32_clear()
172689022Sps{
172789022Sps	/*
172889022Sps	 * This will clear only the currently visible rows of the NT
172989022Sps	 * console buffer, which means none of the precious scrollback
173089022Sps	 * rows are touched making for faster scrolling.  Note that, if
173189022Sps	 * the window has fewer columns than the console buffer (i.e.
173289022Sps	 * there is a horizontal scrollbar as well), the entire width
173389022Sps	 * of the visible rows will be cleared.
173489022Sps	 */
173589022Sps	COORD topleft;
173689022Sps	DWORD nchars;
173789022Sps	DWORD winsz;
173889022Sps	CONSOLE_SCREEN_BUFFER_INFO csbi;
173989022Sps
174089022Sps	/* get the number of cells in the current buffer */
174189022Sps	GetConsoleScreenBufferInfo(con_out, &csbi);
174289022Sps	winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
174389022Sps	topleft.X = 0;
174489022Sps	topleft.Y = csbi.srWindow.Top;
174589022Sps
174689022Sps	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
174789022Sps	FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
174889022Sps	FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
174989022Sps}
175089022Sps
175189022Sps/*
175289022Sps * Remove the n topmost lines and scroll everything below it in the
175389022Sps * window upward.
175489022Sps */
175589022Sps	public void
175689022Spswin32_scroll_up(n)
175789022Sps	int n;
175889022Sps{
175989022Sps	SMALL_RECT rcSrc, rcClip;
176089022Sps	CHAR_INFO fillchar;
176189022Sps	COORD topleft;
176289022Sps	COORD new_org;
176389022Sps	DWORD nchars;
176489022Sps	DWORD size;
176589022Sps	CONSOLE_SCREEN_BUFFER_INFO csbi;
176689022Sps
176789022Sps	if (n <= 0)
176889022Sps		return;
176989022Sps
177089022Sps	if (n >= sc_height - 1)
177189022Sps	{
177289022Sps		win32_clear();
177389022Sps		_settextposition(1,1);
177489022Sps		return;
177589022Sps	}
177689022Sps
177789022Sps	/* Get the extent of what will remain visible after scrolling. */
177889022Sps	GetConsoleScreenBufferInfo(con_out, &csbi);
177989022Sps	rcSrc.Left    = csbi.srWindow.Left;
178089022Sps	rcSrc.Top     = csbi.srWindow.Top + n;
178189022Sps	rcSrc.Right   = csbi.srWindow.Right;
178289022Sps	rcSrc.Bottom  = csbi.srWindow.Bottom;
178389022Sps
178489022Sps	/* Get the clip rectangle. */
178589022Sps	rcClip.Left   = rcSrc.Left;
178689022Sps	rcClip.Top    = csbi.srWindow.Top;
178789022Sps	rcClip.Right  = rcSrc.Right;
178889022Sps	rcClip.Bottom = rcSrc.Bottom ;
178989022Sps
179089022Sps	/* Move the source text to the top of the screen. */
179189022Sps	new_org.X = rcSrc.Left;
1792191930Sdelphij	/* new_org.Y = rcClip.top; -- doesn't compile under MSVC6 */
179389022Sps
179489022Sps	/* Fill the right character and attributes. */
179589022Sps	fillchar.Char.AsciiChar = ' ';
179689022Sps	fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
179789022Sps
179889022Sps	/* Scroll the window. */
179989022Sps	SetConsoleTextAttribute(con_out, fillchar.Attributes);
180089022Sps	ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
180189022Sps
180289022Sps	/* Clear remaining lines at bottom. */
180389022Sps	topleft.X = csbi.dwCursorPosition.X;
180489022Sps	topleft.Y = rcSrc.Bottom - n;
180589022Sps	size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
180689022Sps	FillConsoleOutputCharacter(con_out, ' ', size, topleft,
180789022Sps		&nchars);
180889022Sps	FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
180989022Sps		&nchars);
181089022Sps	SetConsoleTextAttribute(con_out, curr_attr);
181189022Sps
181289022Sps	/* Move cursor n lines up from where it was. */
181389022Sps	csbi.dwCursorPosition.Y -= n;
181489022Sps	SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
181589022Sps}
181689022Sps#endif
181789022Sps
181889022Sps/*
181960786Sps * Move cursor to lower left corner of screen.
182060786Sps */
182160786Sps	public void
182260786Spslower_left()
182360786Sps{
182460786Sps#if !MSDOS_COMPILER
182560786Sps	tputs(sc_lower_left, 1, putchr);
182660786Sps#else
182760786Sps	flush();
182860786Sps	_settextposition(sc_height, 1);
182960786Sps#endif
183060786Sps}
183160786Sps
183260786Sps/*
1833170259Sdelphij * Move cursor to left position of current line.
1834170259Sdelphij */
1835170259Sdelphij	public void
1836170259Sdelphijline_left()
1837170259Sdelphij{
1838170259Sdelphij#if !MSDOS_COMPILER
1839170259Sdelphij	tputs(sc_return, 1, putchr);
1840170259Sdelphij#else
1841170259Sdelphij	int row;
1842170259Sdelphij	flush();
1843170259Sdelphij#if MSDOS_COMPILER==WIN32C
1844170259Sdelphij	{
1845170259Sdelphij		CONSOLE_SCREEN_BUFFER_INFO scr;
1846170259Sdelphij		GetConsoleScreenBufferInfo(con_out, &scr);
1847170259Sdelphij		row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
1848170259Sdelphij	}
1849170259Sdelphij#else
1850173685Sdelphij#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1851173685Sdelphij		row = wherey();
1852173685Sdelphij#else
1853170259Sdelphij	{
1854170259Sdelphij		struct rccoord tpos = _gettextposition();
1855170259Sdelphij		row = tpos.row;
1856170259Sdelphij	}
1857170259Sdelphij#endif
1858173685Sdelphij#endif
1859170259Sdelphij	_settextposition(row, 1);
1860170259Sdelphij#endif
1861170259Sdelphij}
1862170259Sdelphij
1863170259Sdelphij/*
186460786Sps * Check if the console size has changed and reset internals
186560786Sps * (in lieu of SIGWINCH for WIN32).
186660786Sps */
186760786Sps	public void
186860786Spscheck_winch()
186960786Sps{
187060786Sps#if MSDOS_COMPILER==WIN32C
187160786Sps	CONSOLE_SCREEN_BUFFER_INFO scr;
187260786Sps	COORD size;
187360786Sps
187460786Sps	if (con_out == INVALID_HANDLE_VALUE)
187560786Sps		return;
187660786Sps
187760786Sps	flush();
187860786Sps	GetConsoleScreenBufferInfo(con_out, &scr);
187960786Sps	size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
188060786Sps	size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
188160786Sps	if (size.Y != sc_height || size.X != sc_width)
188260786Sps	{
188360786Sps		sc_height = size.Y;
188460786Sps		sc_width = size.X;
188560786Sps		if (!no_init && con_out_ours == con_out)
188660786Sps			SetConsoleScreenBufferSize(con_out, size);
188760786Sps		pos_init();
188860786Sps		wscroll = (sc_height + 1) / 2;
188960786Sps		screen_trashed = 1;
189060786Sps	}
189160786Sps#endif
189260786Sps}
189360786Sps
189460786Sps/*
189560786Sps * Goto a specific line on the screen.
189660786Sps */
189760786Sps	public void
189860786Spsgoto_line(slinenum)
189960786Sps	int slinenum;
190060786Sps{
190160786Sps#if !MSDOS_COMPILER
190260786Sps	tputs(tgoto(sc_move, 0, slinenum), 1, putchr);
190360786Sps#else
190460786Sps	flush();
190560786Sps	_settextposition(slinenum+1, 1);
190660786Sps#endif
190760786Sps}
190860786Sps
190960786Sps#if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
191060786Sps/*
191160786Sps * Create an alternate screen which is all white.
191260786Sps * This screen is used to create a "flash" effect, by displaying it
191360786Sps * briefly and then switching back to the normal screen.
191460786Sps * {{ Yuck!  There must be a better way to get a visual bell. }}
191560786Sps */
191660786Sps	static void
191760786Spscreate_flash()
191860786Sps{
191960786Sps#if MSDOS_COMPILER==MSOFTC
192060786Sps	struct videoconfig w;
192160786Sps	char *blanks;
192260786Sps	int row, col;
192360786Sps
192460786Sps	_getvideoconfig(&w);
192560786Sps	videopages = w.numvideopages;
192660786Sps	if (videopages < 2)
192760786Sps	{
1928161478Sdelphij		at_enter(AT_STANDOUT);
1929161478Sdelphij		at_exit();
193060786Sps	} else
193160786Sps	{
193260786Sps		_setactivepage(1);
1933161478Sdelphij		at_enter(AT_STANDOUT);
193460786Sps		blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
193560786Sps		for (col = 0;  col < w.numtextcols;  col++)
193660786Sps			blanks[col] = ' ';
193760786Sps		for (row = w.numtextrows;  row > 0;  row--)
193860786Sps			_outmem(blanks, w.numtextcols);
193960786Sps		_setactivepage(0);
194060786Sps		_setvisualpage(0);
194160786Sps		free(blanks);
1942161478Sdelphij		at_exit();
194360786Sps	}
194460786Sps#else
194560786Sps#if MSDOS_COMPILER==BORLANDC
194660786Sps	register int n;
194760786Sps
194860786Sps	whitescreen = (unsigned short *)
194960786Sps		malloc(sc_width * sc_height * sizeof(short));
195060786Sps	if (whitescreen == NULL)
195160786Sps		return;
195260786Sps	for (n = 0;  n < sc_width * sc_height;  n++)
195360786Sps		whitescreen[n] = 0x7020;
195460786Sps#else
195560786Sps#if MSDOS_COMPILER==WIN32C
195660786Sps	register int n;
195760786Sps
195860786Sps	whitescreen = (WORD *)
195960786Sps		malloc(sc_height * sc_width * sizeof(WORD));
196060786Sps	if (whitescreen == NULL)
196160786Sps		return;
196260786Sps	/* Invert the standard colors. */
196360786Sps	for (n = 0;  n < sc_width * sc_height;  n++)
196460786Sps		whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
196560786Sps#endif
196660786Sps#endif
196760786Sps#endif
196860786Sps	flash_created = 1;
196960786Sps}
197060786Sps#endif /* MSDOS_COMPILER */
197160786Sps
197260786Sps/*
197360786Sps * Output the "visual bell", if there is one.
197460786Sps */
197560786Sps	public void
197660786Spsvbell()
197760786Sps{
197860786Sps#if !MSDOS_COMPILER
197960786Sps	if (*sc_visual_bell == '\0')
198060786Sps		return;
198160786Sps	tputs(sc_visual_bell, sc_height, putchr);
198260786Sps#else
198360786Sps#if MSDOS_COMPILER==DJGPPC
198460786Sps	ScreenVisualBell();
198560786Sps#else
198660786Sps#if MSDOS_COMPILER==MSOFTC
198760786Sps	/*
198860786Sps	 * Create a flash screen on the second video page.
198960786Sps	 * Switch to that page, then switch back.
199060786Sps	 */
199160786Sps	if (!flash_created)
199260786Sps		create_flash();
199360786Sps	if (videopages < 2)
199460786Sps		return;
199560786Sps	_setvisualpage(1);
199660786Sps	delay(100);
199760786Sps	_setvisualpage(0);
199860786Sps#else
199960786Sps#if MSDOS_COMPILER==BORLANDC
200060786Sps	unsigned short *currscreen;
200160786Sps
200260786Sps	/*
200360786Sps	 * Get a copy of the current screen.
200460786Sps	 * Display the flash screen.
200560786Sps	 * Then restore the old screen.
200660786Sps	 */
200760786Sps	if (!flash_created)
200860786Sps		create_flash();
200960786Sps	if (whitescreen == NULL)
201060786Sps		return;
201160786Sps	currscreen = (unsigned short *)
201260786Sps		malloc(sc_width * sc_height * sizeof(short));
201360786Sps	if (currscreen == NULL) return;
201460786Sps	gettext(1, 1, sc_width, sc_height, currscreen);
201560786Sps	puttext(1, 1, sc_width, sc_height, whitescreen);
201660786Sps	delay(100);
201760786Sps	puttext(1, 1, sc_width, sc_height, currscreen);
201860786Sps	free(currscreen);
201960786Sps#else
202060786Sps#if MSDOS_COMPILER==WIN32C
202160786Sps	/* paint screen with an inverse color */
202260786Sps	clear();
202360786Sps
202460786Sps	/* leave it displayed for 100 msec. */
202560786Sps	Sleep(100);
202660786Sps
202760786Sps	/* restore with a redraw */
202860786Sps	repaint();
202960786Sps#endif
203060786Sps#endif
203160786Sps#endif
203260786Sps#endif
203360786Sps#endif
203460786Sps}
203560786Sps
203660786Sps/*
203760786Sps * Make a noise.
203860786Sps */
203960786Sps	static void
204060786Spsbeep()
204160786Sps{
204260786Sps#if !MSDOS_COMPILER
204389022Sps	putchr(CONTROL('G'));
204460786Sps#else
204560786Sps#if MSDOS_COMPILER==WIN32C
204660786Sps	MessageBeep(0);
204760786Sps#else
204860786Sps	write(1, "\7", 1);
204960786Sps#endif
205060786Sps#endif
205160786Sps}
205260786Sps
205360786Sps/*
205460786Sps * Ring the terminal bell.
205560786Sps */
205660786Sps	public void
205760786Spsbell()
205860786Sps{
205960786Sps	if (quiet == VERY_QUIET)
206060786Sps		vbell();
206160786Sps	else
206260786Sps		beep();
206360786Sps}
206460786Sps
206560786Sps/*
206660786Sps * Clear the screen.
206760786Sps */
206860786Sps	public void
206960786Spsclear()
207060786Sps{
207160786Sps#if !MSDOS_COMPILER
207260786Sps	tputs(sc_clear, sc_height, putchr);
207360786Sps#else
207460786Sps	flush();
207560786Sps#if MSDOS_COMPILER==WIN32C
207689022Sps	win32_clear();
207760786Sps#else
207860786Sps	_clearscreen(_GCLEARSCREEN);
207960786Sps#endif
208060786Sps#endif
208160786Sps}
208260786Sps
208360786Sps/*
208460786Sps * Clear from the cursor to the end of the cursor's line.
208560786Sps * {{ This must not move the cursor. }}
208660786Sps */
208760786Sps	public void
208860786Spsclear_eol()
208960786Sps{
209060786Sps#if !MSDOS_COMPILER
209160786Sps	tputs(sc_eol_clear, 1, putchr);
209260786Sps#else
209360786Sps#if MSDOS_COMPILER==MSOFTC
209460786Sps	short top, left;
209560786Sps	short bot, right;
209660786Sps	struct rccoord tpos;
209760786Sps
209860786Sps	flush();
209960786Sps	/*
210060786Sps	 * Save current state.
210160786Sps	 */
210260786Sps	tpos = _gettextposition();
210360786Sps	_gettextwindow(&top, &left, &bot, &right);
210460786Sps	/*
210560786Sps	 * Set a temporary window to the current line,
210660786Sps	 * from the cursor's position to the right edge of the screen.
210760786Sps	 * Then clear that window.
210860786Sps	 */
210960786Sps	_settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
211060786Sps	_clearscreen(_GWINDOW);
211160786Sps	/*
211260786Sps	 * Restore state.
211360786Sps	 */
211460786Sps	_settextwindow(top, left, bot, right);
211560786Sps	_settextposition(tpos.row, tpos.col);
211660786Sps#else
211760786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
211860786Sps	flush();
211960786Sps	clreol();
212060786Sps#else
212160786Sps#if MSDOS_COMPILER==WIN32C
212260786Sps	DWORD           nchars;
212360786Sps	COORD           cpos;
212460786Sps	CONSOLE_SCREEN_BUFFER_INFO scr;
212560786Sps
212660786Sps	flush();
212760786Sps	memset(&scr, 0, sizeof(scr));
212860786Sps	GetConsoleScreenBufferInfo(con_out, &scr);
212960786Sps	cpos.X = scr.dwCursorPosition.X;
213060786Sps	cpos.Y = scr.dwCursorPosition.Y;
213160786Sps	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
213260786Sps	FillConsoleOutputAttribute(con_out, curr_attr,
213360786Sps		scr.dwSize.X - cpos.X, cpos, &nchars);
213460786Sps	FillConsoleOutputCharacter(con_out, ' ',
213560786Sps		scr.dwSize.X - cpos.X, cpos, &nchars);
213660786Sps#endif
213760786Sps#endif
213860786Sps#endif
213960786Sps#endif
214060786Sps}
214160786Sps
214260786Sps/*
214360786Sps * Clear the current line.
214460786Sps * Clear the screen if there's off-screen memory below the display.
214560786Sps */
214660786Sps	static void
214760786Spsclear_eol_bot()
214860786Sps{
214960786Sps#if MSDOS_COMPILER
215060786Sps	clear_eol();
215160786Sps#else
215260786Sps	if (below_mem)
215360786Sps		tputs(sc_eos_clear, 1, putchr);
215460786Sps	else
215560786Sps		tputs(sc_eol_clear, 1, putchr);
215660786Sps#endif
215760786Sps}
215860786Sps
215960786Sps/*
216060786Sps * Clear the bottom line of the display.
216160786Sps * Leave the cursor at the beginning of the bottom line.
216260786Sps */
216360786Sps	public void
216460786Spsclear_bot()
216560786Sps{
216660786Sps	/*
216760786Sps	 * If we're in a non-normal attribute mode, temporarily exit
216860786Sps	 * the mode while we do the clear.  Some terminals fill the
216960786Sps	 * cleared area with the current attribute.
217060786Sps	 */
2171170259Sdelphij	if (oldbot)
2172170259Sdelphij		lower_left();
2173170259Sdelphij	else
2174170259Sdelphij		line_left();
2175170259Sdelphij
2176161478Sdelphij	if (attrmode == AT_NORMAL)
2177161478Sdelphij		clear_eol_bot();
2178161478Sdelphij	else
217960786Sps	{
2180161478Sdelphij		int saved_attrmode = attrmode;
2181161478Sdelphij
2182161478Sdelphij		at_exit();
218360786Sps		clear_eol_bot();
2184161478Sdelphij		at_enter(saved_attrmode);
218560786Sps	}
218660786Sps}
218760786Sps
218860786Sps	public void
2189161478Sdelphijat_enter(attr)
2190161478Sdelphij	int attr;
219160786Sps{
2192161478Sdelphij	attr = apply_at_specials(attr);
219360786Sps
219460786Sps#if !MSDOS_COMPILER
2195161478Sdelphij	/* The one with the most priority is last.  */
2196161478Sdelphij	if (attr & AT_UNDERLINE)
2197161478Sdelphij		tputs(sc_u_in, 1, putchr);
2198161478Sdelphij	if (attr & AT_BOLD)
2199161478Sdelphij		tputs(sc_b_in, 1, putchr);
2200161478Sdelphij	if (attr & AT_BLINK)
2201161478Sdelphij		tputs(sc_bl_in, 1, putchr);
2202161478Sdelphij	if (attr & AT_STANDOUT)
2203161478Sdelphij		tputs(sc_s_in, 1, putchr);
220460786Sps#else
220560786Sps	flush();
2206161478Sdelphij	/* The one with the most priority is first.  */
2207161478Sdelphij	if (attr & AT_STANDOUT)
2208161478Sdelphij	{
2209161478Sdelphij		SETCOLORS(so_fg_color, so_bg_color);
2210161478Sdelphij	} else if (attr & AT_BLINK)
2211161478Sdelphij	{
2212161478Sdelphij		SETCOLORS(bl_fg_color, bl_bg_color);
2213161478Sdelphij	}
2214161478Sdelphij	else if (attr & AT_BOLD)
2215161478Sdelphij	{
2216161478Sdelphij		SETCOLORS(bo_fg_color, bo_bg_color);
2217161478Sdelphij	}
2218161478Sdelphij	else if (attr & AT_UNDERLINE)
2219161478Sdelphij	{
2220161478Sdelphij		SETCOLORS(ul_fg_color, ul_bg_color);
2221161478Sdelphij	}
222260786Sps#endif
222360786Sps
2224161478Sdelphij	attrmode = attr;
222560786Sps}
222660786Sps
222760786Sps	public void
2228161478Sdelphijat_exit()
222960786Sps{
223060786Sps#if !MSDOS_COMPILER
2231161478Sdelphij	/* Undo things in the reverse order we did them.  */
2232161478Sdelphij	if (attrmode & AT_STANDOUT)
2233161478Sdelphij		tputs(sc_s_out, 1, putchr);
2234161478Sdelphij	if (attrmode & AT_BLINK)
2235161478Sdelphij		tputs(sc_bl_out, 1, putchr);
2236161478Sdelphij	if (attrmode & AT_BOLD)
2237161478Sdelphij		tputs(sc_b_out, 1, putchr);
2238161478Sdelphij	if (attrmode & AT_UNDERLINE)
2239161478Sdelphij		tputs(sc_u_out, 1, putchr);
224060786Sps#else
224160786Sps	flush();
224260786Sps	SETCOLORS(nm_fg_color, nm_bg_color);
224360786Sps#endif
2244161478Sdelphij
224560786Sps	attrmode = AT_NORMAL;
224660786Sps}
224760786Sps
224860786Sps	public void
2249161478Sdelphijat_switch(attr)
2250161478Sdelphij	int attr;
225160786Sps{
2252170259Sdelphij	int new_attrmode = apply_at_specials(attr);
2253170259Sdelphij	int ignore_modes = AT_ANSI;
2254170259Sdelphij
2255170259Sdelphij	if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
2256161478Sdelphij	{
2257161478Sdelphij		at_exit();
2258161478Sdelphij		at_enter(attr);
2259161478Sdelphij	}
226060786Sps}
226160786Sps
2262161478Sdelphij	public int
2263161478Sdelphijis_at_equiv(attr1, attr2)
2264161478Sdelphij	int attr1;
2265161478Sdelphij	int attr2;
226660786Sps{
2267161478Sdelphij	attr1 = apply_at_specials(attr1);
2268161478Sdelphij	attr2 = apply_at_specials(attr2);
2269161478Sdelphij
2270161478Sdelphij	return (attr1 == attr2);
227160786Sps}
227260786Sps
2273161478Sdelphij	public int
2274161478Sdelphijapply_at_specials(attr)
2275161478Sdelphij	int attr;
227660786Sps{
2277161478Sdelphij	if (attr & AT_BINARY)
2278161478Sdelphij		attr |= binattr;
2279161478Sdelphij	if (attr & AT_HILITE)
2280161478Sdelphij		attr |= AT_STANDOUT;
2281161478Sdelphij	attr &= ~(AT_BINARY|AT_HILITE);
228260786Sps
2283161478Sdelphij	return attr;
228460786Sps}
228560786Sps
228660786Sps#if 0 /* No longer used */
228760786Sps/*
228860786Sps * Erase the character to the left of the cursor
228960786Sps * and move the cursor left.
229060786Sps */
229160786Sps	public void
229260786Spsbackspace()
229360786Sps{
229460786Sps#if !MSDOS_COMPILER
229560786Sps	/*
229660786Sps	 * Erase the previous character by overstriking with a space.
229760786Sps	 */
229860786Sps	tputs(sc_backspace, 1, putchr);
229960786Sps	putchr(' ');
230060786Sps	tputs(sc_backspace, 1, putchr);
230160786Sps#else
230260786Sps#if MSDOS_COMPILER==MSOFTC
230360786Sps	struct rccoord tpos;
230460786Sps
230560786Sps	flush();
230660786Sps	tpos = _gettextposition();
230760786Sps	if (tpos.col <= 1)
230860786Sps		return;
230960786Sps	_settextposition(tpos.row, tpos.col-1);
231060786Sps	_outtext(" ");
231160786Sps	_settextposition(tpos.row, tpos.col-1);
231260786Sps#else
231360786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
231460786Sps	cputs("\b");
231560786Sps#else
231660786Sps#if MSDOS_COMPILER==WIN32C
231760786Sps	COORD cpos;
231860786Sps	DWORD cChars;
231960786Sps	CONSOLE_SCREEN_BUFFER_INFO scr;
232060786Sps
232160786Sps	flush();
232260786Sps	GetConsoleScreenBufferInfo(con_out, &scr);
232360786Sps	cpos = scr.dwCursorPosition;
232460786Sps	if (cpos.X <= 0)
232560786Sps		return;
232660786Sps	cpos.X--;
232760786Sps	SetConsoleCursorPosition(con_out, cpos);
232860786Sps	FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
232960786Sps	SetConsoleCursorPosition(con_out, cpos);
233060786Sps#endif
233160786Sps#endif
233260786Sps#endif
233360786Sps#endif
233460786Sps}
233560786Sps#endif /* 0 */
233660786Sps
233760786Sps/*
233860786Sps * Output a plain backspace, without erasing the previous char.
233960786Sps */
234060786Sps	public void
234160786Spsputbs()
234260786Sps{
234360786Sps#if !MSDOS_COMPILER
234460786Sps	tputs(sc_backspace, 1, putchr);
234560786Sps#else
234660786Sps	int row, col;
234760786Sps
234860786Sps	flush();
234960786Sps	{
235060786Sps#if MSDOS_COMPILER==MSOFTC
235160786Sps		struct rccoord tpos;
235260786Sps		tpos = _gettextposition();
235360786Sps		row = tpos.row;
235460786Sps		col = tpos.col;
235560786Sps#else
235660786Sps#if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
235760786Sps		row = wherey();
235860786Sps		col = wherex();
235960786Sps#else
236060786Sps#if MSDOS_COMPILER==WIN32C
236160786Sps		CONSOLE_SCREEN_BUFFER_INFO scr;
236260786Sps		GetConsoleScreenBufferInfo(con_out, &scr);
236360786Sps		row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
236460786Sps		col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
236560786Sps#endif
236660786Sps#endif
236760786Sps#endif
236860786Sps	}
236960786Sps	if (col <= 1)
237060786Sps		return;
237160786Sps	_settextposition(row, col-1);
237260786Sps#endif /* MSDOS_COMPILER */
237360786Sps}
237460786Sps
237560786Sps#if MSDOS_COMPILER==WIN32C
237660786Sps/*
237760786Sps * Determine whether an input character is waiting to be read.
237860786Sps */
237960786Sps	static int
238060786Spswin32_kbhit(tty)
238160786Sps	HANDLE tty;
238260786Sps{
238360786Sps	INPUT_RECORD ip;
238460786Sps	DWORD read;
238560786Sps
238660786Sps	if (keyCount > 0)
238760786Sps		return (TRUE);
238860786Sps
238960786Sps	currentKey.ascii = 0;
239060786Sps	currentKey.scan = 0;
239160786Sps
239260786Sps	/*
239360786Sps	 * Wait for a real key-down event, but
239460786Sps	 * ignore SHIFT and CONTROL key events.
239560786Sps	 */
239660786Sps	do
239760786Sps	{
239860786Sps		PeekConsoleInput(tty, &ip, 1, &read);
239960786Sps		if (read == 0)
240060786Sps			return (FALSE);
240160786Sps		ReadConsoleInput(tty, &ip, 1, &read);
240260786Sps	} while (ip.EventType != KEY_EVENT ||
240360786Sps		ip.Event.KeyEvent.bKeyDown != TRUE ||
240460786Sps		ip.Event.KeyEvent.wVirtualScanCode == 0 ||
240560786Sps		ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
240660786Sps		ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
240760786Sps		ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
240860786Sps
240960786Sps	currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
241060786Sps	currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
241160786Sps	keyCount = ip.Event.KeyEvent.wRepeatCount;
241260786Sps
241360786Sps	if (ip.Event.KeyEvent.dwControlKeyState &
241460786Sps		(LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
241560786Sps	{
241660786Sps		switch (currentKey.scan)
241760786Sps		{
241860786Sps		case PCK_ALT_E:     /* letter 'E' */
241960786Sps			currentKey.ascii = 0;
242060786Sps			break;
242160786Sps		}
242260786Sps	} else if (ip.Event.KeyEvent.dwControlKeyState &
242360786Sps		(LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
242460786Sps	{
242560786Sps		switch (currentKey.scan)
242660786Sps		{
242760786Sps		case PCK_RIGHT: /* right arrow */
242860786Sps			currentKey.scan = PCK_CTL_RIGHT;
242960786Sps			break;
243060786Sps		case PCK_LEFT: /* left arrow */
243160786Sps			currentKey.scan = PCK_CTL_LEFT;
243260786Sps			break;
243360786Sps		case PCK_DELETE: /* delete */
243460786Sps			currentKey.scan = PCK_CTL_DELETE;
243560786Sps			break;
243660786Sps		}
243760786Sps	}
243860786Sps	return (TRUE);
243960786Sps}
244060786Sps
244160786Sps/*
244260786Sps * Read a character from the keyboard.
244360786Sps */
244460786Sps	public char
244560786SpsWIN32getch(tty)
244660786Sps	int tty;
244760786Sps{
244860786Sps	int ascii;
244960786Sps
245060786Sps	if (pending_scancode)
245160786Sps	{
245260786Sps		pending_scancode = 0;
245360786Sps		return ((char)(currentKey.scan & 0x00FF));
245460786Sps	}
245560786Sps
245660786Sps	while (win32_kbhit((HANDLE)tty) == FALSE)
245760786Sps	{
245860786Sps		Sleep(20);
245960786Sps		if (ABORT_SIGS())
246060786Sps			return ('\003');
246160786Sps		continue;
246260786Sps	}
246360786Sps	keyCount --;
246460786Sps	ascii = currentKey.ascii;
246560786Sps	/*
246660786Sps	 * On PC's, the extended keys return a 2 byte sequence beginning
246760786Sps	 * with '00', so if the ascii code is 00, the next byte will be
246860786Sps	 * the lsb of the scan code.
246960786Sps	 */
247060786Sps	pending_scancode = (ascii == 0x00);
247160786Sps	return ((char)ascii);
247260786Sps}
247360786Sps#endif
2474191930Sdelphij
2475191930Sdelphij#if MSDOS_COMPILER
2476191930Sdelphij/*
2477191930Sdelphij */
2478191930Sdelphij	public void
2479191930SdelphijWIN32setcolors(fg, bg)
2480191930Sdelphij	int fg;
2481191930Sdelphij	int bg;
2482191930Sdelphij{
2483191930Sdelphij	SETCOLORS(fg, bg);
2484191930Sdelphij}
2485191930Sdelphij
2486191930Sdelphij/*
2487191930Sdelphij */
2488191930Sdelphij	public void
2489191930SdelphijWIN32textout(text, len)
2490191930Sdelphij	char *text;
2491191930Sdelphij	int len;
2492191930Sdelphij{
2493191930Sdelphij#if MSDOS_COMPILER==WIN32C
2494191930Sdelphij	DWORD written;
2495191930Sdelphij	WriteConsole(con_out, text, len, &written, NULL);
2496191930Sdelphij#else
2497191930Sdelphij	char c = text[len];
2498191930Sdelphij	text[len] = '\0';
2499191930Sdelphij	cputs(text);
2500191930Sdelphij	text[len] = c;
2501191930Sdelphij#endif
2502191930Sdelphij}
2503191930Sdelphij#endif
2504