xref: /netbsd-src/external/bsd/less/dist/screen.c (revision 838f5788460f0f133b15d706e644d692a9d4d6ec)
1*838f5788Ssimonb /*	$NetBSD: screen.c,v 1.5 2023/10/06 05:49:49 simonb Exp $	*/
220006a0bStron 
320006a0bStron /*
4*838f5788Ssimonb  * Copyright (C) 1984-2023  Mark Nudelman
520006a0bStron  *
620006a0bStron  * You may distribute under the terms of either the GNU General Public
720006a0bStron  * License or the Less License, as specified in the README file.
820006a0bStron  *
9ec18bca0Stron  * For more information, see the README file.
1020006a0bStron  */
1120006a0bStron 
1220006a0bStron 
1320006a0bStron /*
1420006a0bStron  * Routines which deal with the characteristics of the terminal.
1520006a0bStron  * Uses termcap to be as terminal-independent as possible.
1620006a0bStron  */
1720006a0bStron 
1820006a0bStron #include "less.h"
1920006a0bStron #include "cmd.h"
2020006a0bStron 
2120006a0bStron #if MSDOS_COMPILER
2220006a0bStron #include "pckeys.h"
2320006a0bStron #if MSDOS_COMPILER==MSOFTC
2420006a0bStron #include <graph.h>
2520006a0bStron #else
2620006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2720006a0bStron #include <conio.h>
2820006a0bStron #if MSDOS_COMPILER==DJGPPC
2920006a0bStron #include <pc.h>
3020006a0bStron extern int fd0;
3120006a0bStron #endif
3220006a0bStron #else
3320006a0bStron #if MSDOS_COMPILER==WIN32C
3420006a0bStron #include <windows.h>
3520006a0bStron #endif
3620006a0bStron #endif
3720006a0bStron #endif
3820006a0bStron #include <time.h>
3920006a0bStron 
40*838f5788Ssimonb #ifndef FOREGROUND_BLUE
41*838f5788Ssimonb #define FOREGROUND_BLUE      0x0001
42*838f5788Ssimonb #endif
43*838f5788Ssimonb #ifndef FOREGROUND_GREEN
44*838f5788Ssimonb #define FOREGROUND_GREEN     0x0002
45*838f5788Ssimonb #endif
46*838f5788Ssimonb #ifndef FOREGROUND_RED
47*838f5788Ssimonb #define FOREGROUND_RED       0x0004
48*838f5788Ssimonb #endif
49*838f5788Ssimonb #ifndef FOREGROUND_INTENSITY
50*838f5788Ssimonb #define FOREGROUND_INTENSITY 0x0008
51*838f5788Ssimonb #endif
52*838f5788Ssimonb 
5320006a0bStron #else
5420006a0bStron 
5520006a0bStron #if HAVE_SYS_IOCTL_H
5620006a0bStron #include <sys/ioctl.h>
5720006a0bStron #endif
5820006a0bStron 
5920006a0bStron #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
6020006a0bStron #include <termios.h>
6120006a0bStron #else
6220006a0bStron #if HAVE_TERMIO_H
6320006a0bStron #include <termio.h>
6420006a0bStron #else
6520006a0bStron #if HAVE_SGSTAT_H
6620006a0bStron #include <sgstat.h>
6720006a0bStron #else
6820006a0bStron #include <sgtty.h>
6920006a0bStron #endif
7020006a0bStron #endif
7120006a0bStron #endif
7220006a0bStron 
73*838f5788Ssimonb #if HAVE_NCURSESW_TERMCAP_H
74*838f5788Ssimonb #include <ncursesw/termcap.h>
75*838f5788Ssimonb #else
76*838f5788Ssimonb #if HAVE_NCURSES_TERMCAP_H
77*838f5788Ssimonb #include <ncurses/termcap.h>
78*838f5788Ssimonb #else
7920006a0bStron #if HAVE_TERMCAP_H
8020006a0bStron #include <termcap.h>
8120006a0bStron #endif
82*838f5788Ssimonb #endif
83*838f5788Ssimonb #endif
8420006a0bStron #ifdef _OSK
8520006a0bStron #include <signal.h>
8620006a0bStron #endif
8720006a0bStron #if OS2
8820006a0bStron #include <sys/signal.h>
8920006a0bStron #include "pckeys.h"
9020006a0bStron #endif
9120006a0bStron #if HAVE_SYS_STREAM_H
9220006a0bStron #include <sys/stream.h>
9320006a0bStron #endif
9420006a0bStron #if HAVE_SYS_PTEM_H
9520006a0bStron #include <sys/ptem.h>
9620006a0bStron #endif
9720006a0bStron 
9820006a0bStron #endif /* MSDOS_COMPILER */
9920006a0bStron 
10020006a0bStron /*
10120006a0bStron  * Check for broken termios package that forces you to manually
10220006a0bStron  * set the line discipline.
10320006a0bStron  */
10420006a0bStron #ifdef __ultrix__
10520006a0bStron #define MUST_SET_LINE_DISCIPLINE 1
10620006a0bStron #else
10720006a0bStron #define MUST_SET_LINE_DISCIPLINE 0
10820006a0bStron #endif
10920006a0bStron 
11020006a0bStron #if OS2
11120006a0bStron #define DEFAULT_TERM            "ansi"
11220006a0bStron static char *windowid;
11320006a0bStron #else
11420006a0bStron #define DEFAULT_TERM            "unknown"
11520006a0bStron #endif
11620006a0bStron 
11720006a0bStron #if MSDOS_COMPILER==MSOFTC
11820006a0bStron static int videopages;
11920006a0bStron static long msec_loops;
12020006a0bStron static int flash_created = 0;
121*838f5788Ssimonb #define SET_FG_COLOR(fg)        _settextcolor(fg)
122*838f5788Ssimonb #define SET_BG_COLOR(bg)        _setbkcolor(bg)
123*838f5788Ssimonb #define SETCOLORS(fg,bg)        { SET_FG_COLOR(fg); SET_BG_COLOR(bg); }
12420006a0bStron #endif
12520006a0bStron 
12620006a0bStron #if MSDOS_COMPILER==BORLANDC
12720006a0bStron static unsigned short *whitescreen;
12820006a0bStron static int flash_created = 0;
12920006a0bStron #endif
13020006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
13120006a0bStron #define _settextposition(y,x)   gotoxy(x,y)
13220006a0bStron #define _clearscreen(m)         clrscr()
13320006a0bStron #define _outtext(s)             cputs(s)
134*838f5788Ssimonb #define SET_FG_COLOR(fg)        textcolor(fg)
135*838f5788Ssimonb #define SET_BG_COLOR(bg)        textbackground(bg)
136*838f5788Ssimonb #define SETCOLORS(fg,bg)        { SET_FG_COLOR(fg); SET_BG_COLOR(bg); }
13720006a0bStron extern int sc_height;
13820006a0bStron #endif
13920006a0bStron 
14020006a0bStron #if MSDOS_COMPILER==WIN32C
141*838f5788Ssimonb #define UTF8_MAX_LENGTH 4
14220006a0bStron struct keyRecord
14320006a0bStron {
144*838f5788Ssimonb 	WCHAR unicode;
14520006a0bStron 	int ascii;
14620006a0bStron 	int scan;
14720006a0bStron } currentKey;
14820006a0bStron 
14920006a0bStron static int keyCount = 0;
15020006a0bStron static WORD curr_attr;
15120006a0bStron static int pending_scancode = 0;
152*838f5788Ssimonb static char x11mousebuf[] = "[M???";    /* Mouse report, after ESC */
153*838f5788Ssimonb static int x11mousePos, x11mouseCount;
154*838f5788Ssimonb static int win_unget_pending = FALSE;
155*838f5788Ssimonb static int win_unget_data;
15620006a0bStron 
15720006a0bStron static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
15820006a0bStron static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
15920006a0bStron HANDLE con_out = INVALID_HANDLE_VALUE;             /* current console */
16020006a0bStron 
161*838f5788Ssimonb extern int utf_mode;
16220006a0bStron extern int quitting;
16320006a0bStron static void win32_init_term();
16420006a0bStron static void win32_deinit_term();
16520006a0bStron 
166*838f5788Ssimonb #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
167*838f5788Ssimonb #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 4
168*838f5788Ssimonb #endif
169*838f5788Ssimonb 
17020006a0bStron #define FG_COLORS       (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
17120006a0bStron #define BG_COLORS       (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
17220006a0bStron #define MAKEATTR(fg,bg)         ((WORD)((fg)|((bg)<<4)))
173*838f5788Ssimonb #define APPLY_COLORS()          { if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
174*838f5788Ssimonb                                   error("SETCOLORS failed", NULL_PARG); }
175*838f5788Ssimonb #define SET_FG_COLOR(fg)        { curr_attr &= ~0x0f; curr_attr |= (fg); APPLY_COLORS(); }
176*838f5788Ssimonb #define SET_BG_COLOR(bg)        { curr_attr &= ~0xf0; curr_attr |= ((bg)<<4); APPLY_COLORS(); }
177*838f5788Ssimonb #define SETCOLORS(fg,bg)        { curr_attr = MAKEATTR(fg,bg); APPLY_COLORS(); }
17820006a0bStron #endif
17920006a0bStron 
18020006a0bStron #if MSDOS_COMPILER
18120006a0bStron public int nm_fg_color;         /* Color of normal text */
18220006a0bStron public int nm_bg_color;
18320006a0bStron public int bo_fg_color;         /* Color of bold text */
18420006a0bStron public int bo_bg_color;
18520006a0bStron public int ul_fg_color;         /* Color of underlined text */
18620006a0bStron public int ul_bg_color;
18720006a0bStron public int so_fg_color;         /* Color of standout text */
18820006a0bStron public int so_bg_color;
18920006a0bStron public int bl_fg_color;         /* Color of blinking text */
19020006a0bStron public int bl_bg_color;
19120006a0bStron static int sy_fg_color;         /* Color of system text (before less) */
19220006a0bStron static int sy_bg_color;
193*838f5788Ssimonb public int sgr_mode;            /* Honor ANSI sequences rather than using above */
194*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
195*838f5788Ssimonb static DWORD init_output_mode;  /* The initial console output mode */
196*838f5788Ssimonb public int vt_enabled = -1;     /* Is virtual terminal processing available? */
197*838f5788Ssimonb #endif
19820006a0bStron #else
19920006a0bStron 
20020006a0bStron /*
20120006a0bStron  * Strings passed to tputs() to do various terminal functions.
20220006a0bStron  */
20320006a0bStron static char
20420006a0bStron 	*sc_pad,                /* Pad string */
20520006a0bStron 	*sc_home,               /* Cursor home */
20620006a0bStron 	*sc_addline,            /* Add line, scroll down following lines */
20720006a0bStron 	*sc_lower_left,         /* Cursor to last line, first column */
20820006a0bStron 	*sc_return,             /* Cursor to beginning of current line */
20920006a0bStron 	*sc_move,               /* General cursor positioning */
21020006a0bStron 	*sc_clear,              /* Clear screen */
21120006a0bStron 	*sc_eol_clear,          /* Clear to end of line */
21220006a0bStron 	*sc_eos_clear,          /* Clear to end of screen */
21320006a0bStron 	*sc_s_in,               /* Enter standout (highlighted) mode */
21420006a0bStron 	*sc_s_out,              /* Exit standout mode */
21520006a0bStron 	*sc_u_in,               /* Enter underline mode */
21620006a0bStron 	*sc_u_out,              /* Exit underline mode */
21720006a0bStron 	*sc_b_in,               /* Enter bold mode */
21820006a0bStron 	*sc_b_out,              /* Exit bold mode */
21920006a0bStron 	*sc_bl_in,              /* Enter blink mode */
22020006a0bStron 	*sc_bl_out,             /* Exit blink mode */
22120006a0bStron 	*sc_visual_bell,        /* Visual bell (flash screen) sequence */
22220006a0bStron 	*sc_backspace,          /* Backspace cursor */
22320006a0bStron 	*sc_s_keypad,           /* Start keypad mode */
22420006a0bStron 	*sc_e_keypad,           /* End keypad mode */
225*838f5788Ssimonb 	*sc_s_mousecap,         /* Start mouse capture mode */
226*838f5788Ssimonb 	*sc_e_mousecap,         /* End mouse capture mode */
22720006a0bStron 	*sc_init,               /* Startup terminal initialization */
22820006a0bStron 	*sc_deinit;             /* Exit terminal de-initialization */
229*838f5788Ssimonb 
230*838f5788Ssimonb static int attrcolor = -1;
23120006a0bStron #endif
23220006a0bStron 
23320006a0bStron static int init_done = 0;
23420006a0bStron 
23520006a0bStron public int auto_wrap;           /* Terminal does \r\n when write past margin */
23620006a0bStron public int ignaw;               /* Terminal ignores \n immediately after wrap */
23720006a0bStron public int erase_char;          /* The user's erase char */
23820006a0bStron public int erase2_char;         /* The user's other erase char */
23920006a0bStron public int kill_char;           /* The user's line-kill char */
24020006a0bStron public int werase_char;         /* The user's word-erase char */
24120006a0bStron public int sc_width, sc_height; /* Height & width of screen */
24220006a0bStron public int bo_s_width, bo_e_width;      /* Printing width of boldface seq */
24320006a0bStron public int ul_s_width, ul_e_width;      /* Printing width of underline seq */
24420006a0bStron public int so_s_width, so_e_width;      /* Printing width of standout seq */
24520006a0bStron public int bl_s_width, bl_e_width;      /* Printing width of blink seq */
24620006a0bStron public int above_mem, below_mem;        /* Memory retained above/below screen */
24720006a0bStron public int can_goto_line;               /* Can move cursor to any line */
24820006a0bStron public int clear_bg;            /* Clear fills with background color */
24920006a0bStron public int missing_cap = 0;     /* Some capability is missing */
250*838f5788Ssimonb public char *kent = NULL;       /* Keypad ENTER sequence */
251*838f5788Ssimonb public int term_init_done = FALSE;
252*838f5788Ssimonb public int full_screen = TRUE;
25320006a0bStron 
25420006a0bStron static int attrmode = AT_NORMAL;
255*838f5788Ssimonb static int termcap_debug = -1;
256*838f5788Ssimonb static int no_alt_screen;       /* sc_init does not switch to alt screen */
25720006a0bStron extern int binattr;
258*838f5788Ssimonb extern int one_screen;
259*838f5788Ssimonb #if LESSTEST
260*838f5788Ssimonb extern char *ttyin_name;
261*838f5788Ssimonb #endif /*LESSTEST*/
26220006a0bStron 
26320006a0bStron #if !MSDOS_COMPILER
264*838f5788Ssimonb static char *cheaper(char *t1, char *t2, char *def);
265*838f5788Ssimonb static void tmodes(char *incap, char *outcap, char **instr,
266*838f5788Ssimonb     char **outstr, char *def_instr, char *def_outstr, char **spp);
26720006a0bStron #endif
26820006a0bStron 
26920006a0bStron /*
27020006a0bStron  * These two variables are sometimes defined in,
27120006a0bStron  * and needed by, the termcap library.
27220006a0bStron  */
27320006a0bStron #if MUST_DEFINE_OSPEED
27420006a0bStron extern short ospeed;    /* Terminal output baud rate */
27520006a0bStron extern char PC;         /* Pad character */
27620006a0bStron #endif
27720006a0bStron #ifdef _OSK
27820006a0bStron short ospeed;
27920006a0bStron char PC_, *UP, *BC;
28020006a0bStron #endif
28120006a0bStron 
28220006a0bStron extern int quiet;               /* If VERY_QUIET, use visual bell for bell */
283*838f5788Ssimonb extern int no_vbell;
28420006a0bStron extern int no_back_scroll;
28520006a0bStron extern int swindow;
28620006a0bStron extern int no_init;
287824a88bbStron extern int quit_at_eof;
288824a88bbStron extern int more_mode;
28920006a0bStron extern int no_keypad;
29020006a0bStron extern int sigs;
29120006a0bStron extern int wscroll;
29220006a0bStron extern int screen_trashed;
29320006a0bStron extern int top_scroll;
294*838f5788Ssimonb extern int quit_if_one_screen;
29520006a0bStron extern int oldbot;
296*838f5788Ssimonb extern int mousecap;
297*838f5788Ssimonb extern int is_tty;
298*838f5788Ssimonb extern int use_color;
29920006a0bStron #if HILITE_SEARCH
30020006a0bStron extern int hilite_search;
30120006a0bStron #endif
302*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
303*838f5788Ssimonb extern HANDLE tty;
304*838f5788Ssimonb extern DWORD console_mode;
305*838f5788Ssimonb #ifndef ENABLE_EXTENDED_FLAGS
306*838f5788Ssimonb #define ENABLE_EXTENDED_FLAGS 0x80
307*838f5788Ssimonb #define ENABLE_QUICK_EDIT_MODE 0x40
308*838f5788Ssimonb #endif
309*838f5788Ssimonb #else
310*838f5788Ssimonb extern int tty;
311824a88bbStron #endif
31220006a0bStron 
313*838f5788Ssimonb #if (HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS) || defined(TCGETA)
314*838f5788Ssimonb /*
315*838f5788Ssimonb  * Set termio flags for use by less.
316*838f5788Ssimonb  */
set_termio_flags(struct termios * s)317*838f5788Ssimonb static void set_termio_flags(
318*838f5788Ssimonb #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
319*838f5788Ssimonb 	struct termios *s
320*838f5788Ssimonb #else
321*838f5788Ssimonb 	struct termio *s
322*838f5788Ssimonb #endif
323*838f5788Ssimonb 	)
324*838f5788Ssimonb {
325*838f5788Ssimonb 	s->c_lflag &= ~(0
326*838f5788Ssimonb #ifdef ICANON
327*838f5788Ssimonb 		| ICANON
328*838f5788Ssimonb #endif
329*838f5788Ssimonb #ifdef ECHO
330*838f5788Ssimonb 		| ECHO
331*838f5788Ssimonb #endif
332*838f5788Ssimonb #ifdef ECHOE
333*838f5788Ssimonb 		| ECHOE
334*838f5788Ssimonb #endif
335*838f5788Ssimonb #ifdef ECHOK
336*838f5788Ssimonb 		| ECHOK
337*838f5788Ssimonb #endif
338*838f5788Ssimonb #ifdef ECHONL
339*838f5788Ssimonb 		| ECHONL
340*838f5788Ssimonb #endif
341*838f5788Ssimonb 	);
342*838f5788Ssimonb 
343*838f5788Ssimonb 	s->c_oflag |= (0
344*838f5788Ssimonb #ifdef OXTABS
345*838f5788Ssimonb 		| OXTABS
346*838f5788Ssimonb #else
347*838f5788Ssimonb #ifdef TAB3
348*838f5788Ssimonb 		| TAB3
349*838f5788Ssimonb #else
350*838f5788Ssimonb #ifdef XTABS
351*838f5788Ssimonb 		| XTABS
352*838f5788Ssimonb #endif
353*838f5788Ssimonb #endif
354*838f5788Ssimonb #endif
355*838f5788Ssimonb #ifdef OPOST
356*838f5788Ssimonb 		| OPOST
357*838f5788Ssimonb #endif
358*838f5788Ssimonb #ifdef ONLCR
359*838f5788Ssimonb 		| ONLCR
360*838f5788Ssimonb #endif
361*838f5788Ssimonb 	);
362*838f5788Ssimonb 
363*838f5788Ssimonb 	s->c_oflag &= ~(0
364*838f5788Ssimonb #ifdef ONOEOT
365*838f5788Ssimonb 		| ONOEOT
366*838f5788Ssimonb #endif
367*838f5788Ssimonb #ifdef OCRNL
368*838f5788Ssimonb 		| OCRNL
369*838f5788Ssimonb #endif
370*838f5788Ssimonb #ifdef ONOCR
371*838f5788Ssimonb 		| ONOCR
372*838f5788Ssimonb #endif
373*838f5788Ssimonb #ifdef ONLRET
374*838f5788Ssimonb 		| ONLRET
375*838f5788Ssimonb #endif
376*838f5788Ssimonb 	);
377*838f5788Ssimonb }
378*838f5788Ssimonb #endif
37920006a0bStron 
38020006a0bStron /*
38120006a0bStron  * Change terminal to "raw mode", or restore to "normal" mode.
38220006a0bStron  * "Raw mode" means
38320006a0bStron  *      1. An outstanding read will complete on receipt of a single keystroke.
38420006a0bStron  *      2. Input is not echoed.
38520006a0bStron  *      3. On output, \n is mapped to \r\n.
38620006a0bStron  *      4. \t is NOT expanded into spaces.
38720006a0bStron  *      5. Signal-causing characters such as ctrl-C (interrupt),
38820006a0bStron  *         etc. are NOT disabled.
38920006a0bStron  * It doesn't matter whether an input \n is mapped to \r, or vice versa.
39020006a0bStron  */
raw_mode(int on)391*838f5788Ssimonb public void raw_mode(int on)
39220006a0bStron {
39320006a0bStron 	static int curr_on = 0;
39420006a0bStron 
39520006a0bStron 	if (on == curr_on)
39620006a0bStron 			return;
39720006a0bStron 	erase2_char = '\b'; /* in case OS doesn't know about erase2 */
398*838f5788Ssimonb #if LESSTEST
399*838f5788Ssimonb 	if (ttyin_name != NULL)
400*838f5788Ssimonb 	{
401*838f5788Ssimonb 		/* {{ For consistent conditions when running tests. }} */
402*838f5788Ssimonb 		erase_char = '\b';
403*838f5788Ssimonb 		kill_char = CONTROL('U');
404*838f5788Ssimonb 		werase_char = CONTROL('W');
405*838f5788Ssimonb 	} else
406*838f5788Ssimonb #endif /*LESSTEST*/
40720006a0bStron #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
40820006a0bStron     {
40920006a0bStron 	struct termios s;
41020006a0bStron 	static struct termios save_term;
41120006a0bStron 	static int saved_term = 0;
41220006a0bStron 
41320006a0bStron 	if (on)
41420006a0bStron 	{
41520006a0bStron 		/*
41620006a0bStron 		 * Get terminal modes.
41720006a0bStron 		 */
418*838f5788Ssimonb 		if (tcgetattr(tty, &s) < 0)
419*838f5788Ssimonb 		{
420*838f5788Ssimonb 			erase_char = '\b';
421*838f5788Ssimonb 			kill_char = CONTROL('U');
422*838f5788Ssimonb 			werase_char = CONTROL('W');
423*838f5788Ssimonb 		} else
424*838f5788Ssimonb 		{
42520006a0bStron 			/*
42620006a0bStron 			 * Save modes and set certain variables dependent on modes.
42720006a0bStron 			 */
42820006a0bStron 			if (!saved_term)
42920006a0bStron 			{
43020006a0bStron 				save_term = s;
43120006a0bStron 				saved_term = 1;
43220006a0bStron 			}
43320006a0bStron #if HAVE_OSPEED
43420006a0bStron 			switch (cfgetospeed(&s))
43520006a0bStron 			{
43620006a0bStron #ifdef B0
43720006a0bStron 			case B0: ospeed = 0; break;
43820006a0bStron #endif
43920006a0bStron #ifdef B50
44020006a0bStron 			case B50: ospeed = 1; break;
44120006a0bStron #endif
44220006a0bStron #ifdef B75
44320006a0bStron 			case B75: ospeed = 2; break;
44420006a0bStron #endif
44520006a0bStron #ifdef B110
44620006a0bStron 			case B110: ospeed = 3; break;
44720006a0bStron #endif
44820006a0bStron #ifdef B134
44920006a0bStron 			case B134: ospeed = 4; break;
45020006a0bStron #endif
45120006a0bStron #ifdef B150
45220006a0bStron 			case B150: ospeed = 5; break;
45320006a0bStron #endif
45420006a0bStron #ifdef B200
45520006a0bStron 			case B200: ospeed = 6; break;
45620006a0bStron #endif
45720006a0bStron #ifdef B300
45820006a0bStron 			case B300: ospeed = 7; break;
45920006a0bStron #endif
46020006a0bStron #ifdef B600
46120006a0bStron 			case B600: ospeed = 8; break;
46220006a0bStron #endif
46320006a0bStron #ifdef B1200
46420006a0bStron 			case B1200: ospeed = 9; break;
46520006a0bStron #endif
46620006a0bStron #ifdef B1800
46720006a0bStron 			case B1800: ospeed = 10; break;
46820006a0bStron #endif
46920006a0bStron #ifdef B2400
47020006a0bStron 			case B2400: ospeed = 11; break;
47120006a0bStron #endif
47220006a0bStron #ifdef B4800
47320006a0bStron 			case B4800: ospeed = 12; break;
47420006a0bStron #endif
47520006a0bStron #ifdef B9600
47620006a0bStron 			case B9600: ospeed = 13; break;
47720006a0bStron #endif
47820006a0bStron #ifdef EXTA
47920006a0bStron 			case EXTA: ospeed = 14; break;
48020006a0bStron #endif
48120006a0bStron #ifdef EXTB
48220006a0bStron 			case EXTB: ospeed = 15; break;
48320006a0bStron #endif
48420006a0bStron #ifdef B57600
48520006a0bStron 			case B57600: ospeed = 16; break;
48620006a0bStron #endif
48720006a0bStron #ifdef B115200
48820006a0bStron 			case B115200: ospeed = 17; break;
48920006a0bStron #endif
49020006a0bStron 			default: ;
49120006a0bStron 			}
49220006a0bStron #endif
49320006a0bStron 			erase_char = s.c_cc[VERASE];
49420006a0bStron #ifdef VERASE2
49520006a0bStron 			erase2_char = s.c_cc[VERASE2];
49620006a0bStron #endif
49720006a0bStron 			kill_char = s.c_cc[VKILL];
49820006a0bStron #ifdef VWERASE
49920006a0bStron 			werase_char = s.c_cc[VWERASE];
50020006a0bStron #else
50120006a0bStron 			werase_char = CONTROL('W');
50220006a0bStron #endif
50320006a0bStron 
50420006a0bStron 			/*
50520006a0bStron 			 * Set the modes to the way we want them.
50620006a0bStron 			 */
507*838f5788Ssimonb 			set_termio_flags(&s);
50820006a0bStron 			s.c_cc[VMIN] = 1;
50920006a0bStron 			s.c_cc[VTIME] = 0;
51020006a0bStron #ifdef VLNEXT
51120006a0bStron 			s.c_cc[VLNEXT] = 0;
51220006a0bStron #endif
51320006a0bStron #ifdef VDSUSP
51420006a0bStron 			s.c_cc[VDSUSP] = 0;
51520006a0bStron #endif
516*838f5788Ssimonb #ifdef VSTOP
517*838f5788Ssimonb 			s.c_cc[VSTOP] = 0;
518*838f5788Ssimonb #endif
519*838f5788Ssimonb #ifdef VSTART
520*838f5788Ssimonb 			s.c_cc[VSTART] = 0;
521*838f5788Ssimonb #endif
52220006a0bStron #if MUST_SET_LINE_DISCIPLINE
52320006a0bStron 			/*
52420006a0bStron 			 * System's termios is broken; need to explicitly
52520006a0bStron 			 * request TERMIODISC line discipline.
52620006a0bStron 			 */
52720006a0bStron 			s.c_line = TERMIODISC;
52820006a0bStron #endif
529*838f5788Ssimonb 		}
53020006a0bStron 	} else
53120006a0bStron 	{
53220006a0bStron 		/*
53320006a0bStron 		 * Restore saved modes.
53420006a0bStron 		 */
53520006a0bStron 		s = save_term;
53620006a0bStron 	}
53720006a0bStron #if HAVE_FSYNC
53820006a0bStron 	fsync(tty);
53920006a0bStron #endif
54020006a0bStron 	tcsetattr(tty, TCSADRAIN, &s);
54120006a0bStron #if MUST_SET_LINE_DISCIPLINE
54220006a0bStron 	if (!on)
54320006a0bStron 	{
54420006a0bStron 		/*
54520006a0bStron 		 * Broken termios *ignores* any line discipline
54620006a0bStron 		 * except TERMIODISC.  A different old line discipline
54720006a0bStron 		 * is therefore not restored, yet.  Restore the old
54820006a0bStron 		 * line discipline by hand.
54920006a0bStron 		 */
55020006a0bStron 		ioctl(tty, TIOCSETD, &save_term.c_line);
55120006a0bStron 	}
55220006a0bStron #endif
55320006a0bStron     }
55420006a0bStron #else
55520006a0bStron #ifdef TCGETA
55620006a0bStron     {
55720006a0bStron 	struct termio s;
55820006a0bStron 	static struct termio save_term;
55920006a0bStron 	static int saved_term = 0;
56020006a0bStron 
56120006a0bStron 	if (on)
56220006a0bStron 	{
56320006a0bStron 		/*
56420006a0bStron 		 * Get terminal modes.
56520006a0bStron 		 */
56620006a0bStron 		ioctl(tty, TCGETA, &s);
56720006a0bStron 
56820006a0bStron 		/*
56920006a0bStron 		 * Save modes and set certain variables dependent on modes.
57020006a0bStron 		 */
57120006a0bStron 		if (!saved_term)
57220006a0bStron 		{
57320006a0bStron 			save_term = s;
57420006a0bStron 			saved_term = 1;
57520006a0bStron 		}
57620006a0bStron #if HAVE_OSPEED
57720006a0bStron 		ospeed = s.c_cflag & CBAUD;
57820006a0bStron #endif
57920006a0bStron 		erase_char = s.c_cc[VERASE];
58020006a0bStron 		kill_char = s.c_cc[VKILL];
58120006a0bStron #ifdef VWERASE
58220006a0bStron 		werase_char = s.c_cc[VWERASE];
58320006a0bStron #else
58420006a0bStron 		werase_char = CONTROL('W');
58520006a0bStron #endif
58620006a0bStron 
58720006a0bStron 		/*
58820006a0bStron 		 * Set the modes to the way we want them.
58920006a0bStron 		 */
590*838f5788Ssimonb 		set_termio_flags(&s);
59120006a0bStron 		s.c_cc[VMIN] = 1;
59220006a0bStron 		s.c_cc[VTIME] = 0;
593*838f5788Ssimonb #ifdef VSTOP
594*838f5788Ssimonb 		s.c_cc[VSTOP] = 0;
595*838f5788Ssimonb #endif
596*838f5788Ssimonb #ifdef VSTART
597*838f5788Ssimonb 		s.c_cc[VSTART] = 0;
598*838f5788Ssimonb #endif
59920006a0bStron 	} else
60020006a0bStron 	{
60120006a0bStron 		/*
60220006a0bStron 		 * Restore saved modes.
60320006a0bStron 		 */
60420006a0bStron 		s = save_term;
60520006a0bStron 	}
60620006a0bStron 	ioctl(tty, TCSETAW, &s);
60720006a0bStron     }
60820006a0bStron #else
60920006a0bStron #ifdef TIOCGETP
61020006a0bStron     {
61120006a0bStron 	struct sgttyb s;
61220006a0bStron 	static struct sgttyb save_term;
61320006a0bStron 	static int saved_term = 0;
61420006a0bStron 
61520006a0bStron 	if (on)
61620006a0bStron 	{
61720006a0bStron 		/*
61820006a0bStron 		 * Get terminal modes.
61920006a0bStron 		 */
62020006a0bStron 		ioctl(tty, TIOCGETP, &s);
62120006a0bStron 
62220006a0bStron 		/*
62320006a0bStron 		 * Save modes and set certain variables dependent on modes.
62420006a0bStron 		 */
62520006a0bStron 		if (!saved_term)
62620006a0bStron 		{
62720006a0bStron 			save_term = s;
62820006a0bStron 			saved_term = 1;
62920006a0bStron 		}
63020006a0bStron #if HAVE_OSPEED
63120006a0bStron 		ospeed = s.sg_ospeed;
63220006a0bStron #endif
63320006a0bStron 		erase_char = s.sg_erase;
63420006a0bStron 		kill_char = s.sg_kill;
63520006a0bStron 		werase_char = CONTROL('W');
63620006a0bStron 
63720006a0bStron 		/*
63820006a0bStron 		 * Set the modes to the way we want them.
63920006a0bStron 		 */
64020006a0bStron 		s.sg_flags |= CBREAK;
64120006a0bStron 		s.sg_flags &= ~(ECHO|XTABS);
64220006a0bStron 	} else
64320006a0bStron 	{
64420006a0bStron 		/*
64520006a0bStron 		 * Restore saved modes.
64620006a0bStron 		 */
64720006a0bStron 		s = save_term;
64820006a0bStron 	}
64920006a0bStron 	ioctl(tty, TIOCSETN, &s);
65020006a0bStron     }
65120006a0bStron #else
65220006a0bStron #ifdef _OSK
65320006a0bStron     {
65420006a0bStron 	struct sgbuf s;
65520006a0bStron 	static struct sgbuf save_term;
65620006a0bStron 	static int saved_term = 0;
65720006a0bStron 
65820006a0bStron 	if (on)
65920006a0bStron 	{
66020006a0bStron 		/*
66120006a0bStron 		 * Get terminal modes.
66220006a0bStron 		 */
66320006a0bStron 		_gs_opt(tty, &s);
66420006a0bStron 
66520006a0bStron 		/*
66620006a0bStron 		 * Save modes and set certain variables dependent on modes.
66720006a0bStron 		 */
66820006a0bStron 		if (!saved_term)
66920006a0bStron 		{
67020006a0bStron 			save_term = s;
67120006a0bStron 			saved_term = 1;
67220006a0bStron 		}
67320006a0bStron 		erase_char = s.sg_bspch;
67420006a0bStron 		kill_char = s.sg_dlnch;
67520006a0bStron 		werase_char = CONTROL('W');
67620006a0bStron 
67720006a0bStron 		/*
67820006a0bStron 		 * Set the modes to the way we want them.
67920006a0bStron 		 */
68020006a0bStron 		s.sg_echo = 0;
68120006a0bStron 		s.sg_eofch = 0;
68220006a0bStron 		s.sg_pause = 0;
68320006a0bStron 		s.sg_psch = 0;
68420006a0bStron 	} else
68520006a0bStron 	{
68620006a0bStron 		/*
68720006a0bStron 		 * Restore saved modes.
68820006a0bStron 		 */
68920006a0bStron 		s = save_term;
69020006a0bStron 	}
69120006a0bStron 	_ss_opt(tty, &s);
69220006a0bStron     }
69320006a0bStron #else
69420006a0bStron 	/* MS-DOS, Windows, or OS2 */
69520006a0bStron #if OS2
69620006a0bStron 	/* OS2 */
69720006a0bStron 	LSIGNAL(SIGINT, SIG_IGN);
69820006a0bStron #endif
69920006a0bStron 	erase_char = '\b';
70020006a0bStron #if MSDOS_COMPILER==DJGPPC
70120006a0bStron 	kill_char = CONTROL('U');
70220006a0bStron 	/*
70320006a0bStron 	 * So that when we shell out or run another program, its
70420006a0bStron 	 * stdin is in cooked mode.  We do not switch stdin to binary
70520006a0bStron 	 * mode if fd0 is zero, since that means we were called before
70620006a0bStron 	 * tty was reopened in open_getchr, in which case we would be
70720006a0bStron 	 * changing the original stdin device outside less.
70820006a0bStron 	 */
70920006a0bStron 	if (fd0 != 0)
71020006a0bStron 		setmode(0, on ? O_BINARY : O_TEXT);
71120006a0bStron #else
71220006a0bStron 	kill_char = ESC;
71320006a0bStron #endif
71420006a0bStron 	werase_char = CONTROL('W');
71520006a0bStron #endif
71620006a0bStron #endif
71720006a0bStron #endif
71820006a0bStron #endif
71920006a0bStron 	curr_on = on;
72020006a0bStron }
72120006a0bStron 
72220006a0bStron #if !MSDOS_COMPILER
72320006a0bStron /*
72420006a0bStron  * Some glue to prevent calling termcap functions if tgetent() failed.
72520006a0bStron  */
72620006a0bStron static int hardcopy;
72720006a0bStron 
ltget_env(char * capname)728*838f5788Ssimonb static char * ltget_env(char *capname)
72920006a0bStron {
730*838f5788Ssimonb 	char name[64];
73120006a0bStron 
732*838f5788Ssimonb 	if (termcap_debug)
73320006a0bStron 	{
73420006a0bStron 		struct env { struct env *next; char *name; char *value; };
73520006a0bStron 		static struct env *envs = NULL;
73620006a0bStron 		struct env *p;
73720006a0bStron 		for (p = envs;  p != NULL;  p = p->next)
73820006a0bStron 			if (strcmp(p->name, capname) == 0)
73920006a0bStron 				return p->value;
74020006a0bStron 		p = (struct env *) ecalloc(1, sizeof(struct env));
74120006a0bStron 		p->name = save(capname);
74220006a0bStron 		p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
74320006a0bStron 		sprintf(p->value, "<%s>", capname);
74420006a0bStron 		p->next = envs;
74520006a0bStron 		envs = p;
74620006a0bStron 		return p->value;
74720006a0bStron 	}
748*838f5788Ssimonb 	SNPRINTF1(name, sizeof(name), "LESS_TERMCAP_%s", capname);
74920006a0bStron 	return (lgetenv(name));
75020006a0bStron }
75120006a0bStron 
ltgetflag(char * capname)752*838f5788Ssimonb static int ltgetflag(char *capname)
75320006a0bStron {
75420006a0bStron 	char *s;
75520006a0bStron 
75620006a0bStron 	if ((s = ltget_env(capname)) != NULL)
75720006a0bStron 		return (*s != '\0' && *s != '0');
75820006a0bStron 	if (hardcopy)
75920006a0bStron 		return (0);
76020006a0bStron 	return (tgetflag(capname));
76120006a0bStron }
76220006a0bStron 
ltgetnum(char * capname)763*838f5788Ssimonb static int ltgetnum(char *capname)
76420006a0bStron {
76520006a0bStron 	char *s;
76620006a0bStron 
76720006a0bStron 	if ((s = ltget_env(capname)) != NULL)
76820006a0bStron 		return (atoi(s));
76920006a0bStron 	if (hardcopy)
77020006a0bStron 		return (-1);
77120006a0bStron 	return (tgetnum(capname));
77220006a0bStron }
77320006a0bStron 
ltgetstr(char * capname,char ** pp)774*838f5788Ssimonb static char * ltgetstr(char *capname, char **pp)
77520006a0bStron {
77620006a0bStron 	char *s;
77720006a0bStron 
77820006a0bStron 	if ((s = ltget_env(capname)) != NULL)
77920006a0bStron 		return (s);
78020006a0bStron 	if (hardcopy)
78120006a0bStron 		return (NULL);
78220006a0bStron 	return (tgetstr(capname, pp));
78320006a0bStron }
78420006a0bStron #endif /* MSDOS_COMPILER */
78520006a0bStron 
78620006a0bStron /*
78720006a0bStron  * Get size of the output screen.
78820006a0bStron  */
scrsize(void)789*838f5788Ssimonb public void scrsize(void)
79020006a0bStron {
791*838f5788Ssimonb 	char *s;
79220006a0bStron 	int sys_height;
79320006a0bStron 	int sys_width;
79420006a0bStron #if !MSDOS_COMPILER
79520006a0bStron 	int n;
79620006a0bStron #endif
79720006a0bStron 
79820006a0bStron #define DEF_SC_WIDTH    80
79920006a0bStron #if MSDOS_COMPILER
80020006a0bStron #define DEF_SC_HEIGHT   25
80120006a0bStron #else
80220006a0bStron #define DEF_SC_HEIGHT   24
80320006a0bStron #endif
80420006a0bStron 
80520006a0bStron 
80620006a0bStron 	sys_width = sys_height = 0;
80720006a0bStron 
808*838f5788Ssimonb #if LESSTEST
809*838f5788Ssimonb 	if (ttyin_name != NULL)
810*838f5788Ssimonb #endif /*LESSTEST*/
811*838f5788Ssimonb 	{
81220006a0bStron #if MSDOS_COMPILER==MSOFTC
81320006a0bStron 	{
81420006a0bStron 		struct videoconfig w;
81520006a0bStron 		_getvideoconfig(&w);
81620006a0bStron 		sys_height = w.numtextrows;
81720006a0bStron 		sys_width = w.numtextcols;
81820006a0bStron 	}
81920006a0bStron #else
82020006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
82120006a0bStron 	{
82220006a0bStron 		struct text_info w;
82320006a0bStron 		gettextinfo(&w);
82420006a0bStron 		sys_height = w.screenheight;
82520006a0bStron 		sys_width = w.screenwidth;
82620006a0bStron 	}
82720006a0bStron #else
82820006a0bStron #if MSDOS_COMPILER==WIN32C
82920006a0bStron 	{
83020006a0bStron 		CONSOLE_SCREEN_BUFFER_INFO scr;
83120006a0bStron 		GetConsoleScreenBufferInfo(con_out, &scr);
83220006a0bStron 		sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
83320006a0bStron 		sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
83420006a0bStron 	}
83520006a0bStron #else
83620006a0bStron #if OS2
83720006a0bStron 	{
83820006a0bStron 		int s[2];
83920006a0bStron 		_scrsize(s);
84020006a0bStron 		sys_width = s[0];
84120006a0bStron 		sys_height = s[1];
84220006a0bStron 		/*
84320006a0bStron 		 * When using terminal emulators for XFree86/OS2, the
84420006a0bStron 		 * _scrsize function does not work well.
84520006a0bStron 		 * Call the scrsize.exe program to get the window size.
84620006a0bStron 		 */
84720006a0bStron 		windowid = getenv("WINDOWID");
84820006a0bStron 		if (windowid != NULL)
84920006a0bStron 		{
85020006a0bStron 			FILE *fd = popen("scrsize", "rt");
85120006a0bStron 			if (fd != NULL)
85220006a0bStron 			{
85320006a0bStron 				int w, h;
85420006a0bStron 				fscanf(fd, "%i %i", &w, &h);
85520006a0bStron 				if (w > 0 && h > 0)
85620006a0bStron 				{
85720006a0bStron 					sys_width = w;
85820006a0bStron 					sys_height = h;
85920006a0bStron 				}
86020006a0bStron 				pclose(fd);
86120006a0bStron 			}
86220006a0bStron 		}
86320006a0bStron 	}
86420006a0bStron #else
86520006a0bStron #ifdef TIOCGWINSZ
86620006a0bStron 	{
86720006a0bStron 		struct winsize w;
86820006a0bStron 		if (ioctl(2, TIOCGWINSZ, &w) == 0)
86920006a0bStron 		{
87020006a0bStron 			if (w.ws_row > 0)
87120006a0bStron 				sys_height = w.ws_row;
87220006a0bStron 			if (w.ws_col > 0)
87320006a0bStron 				sys_width = w.ws_col;
87420006a0bStron 		}
87520006a0bStron 	}
87620006a0bStron #else
87720006a0bStron #ifdef WIOCGETD
87820006a0bStron 	{
87920006a0bStron 		struct uwdata w;
88020006a0bStron 		if (ioctl(2, WIOCGETD, &w) == 0)
88120006a0bStron 		{
88220006a0bStron 			if (w.uw_height > 0)
88320006a0bStron 				sys_height = w.uw_height / w.uw_vs;
88420006a0bStron 			if (w.uw_width > 0)
88520006a0bStron 				sys_width = w.uw_width / w.uw_hs;
88620006a0bStron 		}
88720006a0bStron 	}
88820006a0bStron #endif
88920006a0bStron #endif
89020006a0bStron #endif
89120006a0bStron #endif
89220006a0bStron #endif
89320006a0bStron #endif
894*838f5788Ssimonb 	}
89520006a0bStron 
89620006a0bStron 	if (sys_height > 0)
89720006a0bStron 		sc_height = sys_height;
89820006a0bStron 	else if ((s = lgetenv("LINES")) != NULL)
89920006a0bStron 		sc_height = atoi(s);
90020006a0bStron #if !MSDOS_COMPILER
90120006a0bStron 	else if ((n = ltgetnum("li")) > 0)
90220006a0bStron 		sc_height = n;
90320006a0bStron #endif
904*838f5788Ssimonb 	if ((s = lgetenv("LESS_LINES")) != NULL)
905*838f5788Ssimonb 	{
906*838f5788Ssimonb 		int height = atoi(s);
907*838f5788Ssimonb 		sc_height = (height < 0) ? sc_height + height : height;
908*838f5788Ssimonb 		full_screen = FALSE;
909*838f5788Ssimonb 	}
910ec18bca0Stron 	if (sc_height <= 0)
91120006a0bStron 		sc_height = DEF_SC_HEIGHT;
91220006a0bStron 
91320006a0bStron 	if (sys_width > 0)
91420006a0bStron 		sc_width = sys_width;
91520006a0bStron 	else if ((s = lgetenv("COLUMNS")) != NULL)
91620006a0bStron 		sc_width = atoi(s);
91720006a0bStron #if !MSDOS_COMPILER
91820006a0bStron 	else if ((n = ltgetnum("co")) > 0)
91920006a0bStron 		sc_width = n;
92020006a0bStron #endif
921*838f5788Ssimonb 	if ((s = lgetenv("LESS_COLUMNS")) != NULL)
922*838f5788Ssimonb 	{
923*838f5788Ssimonb 		int width = atoi(s);
924*838f5788Ssimonb 		sc_width = (width < 0) ? sc_width + width : width;
925*838f5788Ssimonb 	}
926ec18bca0Stron 	if (sc_width <= 0)
92720006a0bStron 		sc_width = DEF_SC_WIDTH;
92820006a0bStron }
92920006a0bStron 
93020006a0bStron #if MSDOS_COMPILER==MSOFTC
93120006a0bStron /*
93220006a0bStron  * Figure out how many empty loops it takes to delay a millisecond.
93320006a0bStron  */
get_clock(void)934*838f5788Ssimonb static void get_clock(void)
93520006a0bStron {
93620006a0bStron 	clock_t start;
93720006a0bStron 
93820006a0bStron 	/*
93920006a0bStron 	 * Get synchronized at the start of a tick.
94020006a0bStron 	 */
94120006a0bStron 	start = clock();
94220006a0bStron 	while (clock() == start)
94320006a0bStron 		;
94420006a0bStron 	/*
94520006a0bStron 	 * Now count loops till the next tick.
94620006a0bStron 	 */
94720006a0bStron 	start = clock();
94820006a0bStron 	msec_loops = 0;
94920006a0bStron 	while (clock() == start)
95020006a0bStron 		msec_loops++;
95120006a0bStron 	/*
95220006a0bStron 	 * Convert from (loops per clock) to (loops per millisecond).
95320006a0bStron 	 */
95420006a0bStron 	msec_loops *= CLOCKS_PER_SEC;
95520006a0bStron 	msec_loops /= 1000;
95620006a0bStron }
95720006a0bStron 
95820006a0bStron /*
95920006a0bStron  * Delay for a specified number of milliseconds.
96020006a0bStron  */
delay(int msec)961*838f5788Ssimonb static void delay(int msec)
96220006a0bStron {
96320006a0bStron 	long i;
96420006a0bStron 
96520006a0bStron 	while (msec-- > 0)
96620006a0bStron 	{
96720006a0bStron 		for (i = 0;  i < msec_loops;  i++)
968*838f5788Ssimonb 			(void) clock();
96920006a0bStron 	}
97020006a0bStron }
97120006a0bStron #endif
97220006a0bStron 
97320006a0bStron /*
97420006a0bStron  * Return the characters actually input by a "special" key.
97520006a0bStron  */
special_key_str(int key)976*838f5788Ssimonb public char * special_key_str(int key)
97720006a0bStron {
97820006a0bStron 	static char tbuf[40];
97920006a0bStron 	char *s;
98020006a0bStron #if MSDOS_COMPILER || OS2
98120006a0bStron 	static char k_right[]           = { '\340', PCK_RIGHT, 0 };
98220006a0bStron 	static char k_left[]            = { '\340', PCK_LEFT, 0  };
98320006a0bStron 	static char k_ctl_right[]       = { '\340', PCK_CTL_RIGHT, 0  };
98420006a0bStron 	static char k_ctl_left[]        = { '\340', PCK_CTL_LEFT, 0  };
98520006a0bStron 	static char k_insert[]          = { '\340', PCK_INSERT, 0  };
98620006a0bStron 	static char k_delete[]          = { '\340', PCK_DELETE, 0  };
98720006a0bStron 	static char k_ctl_delete[]      = { '\340', PCK_CTL_DELETE, 0  };
98820006a0bStron 	static char k_ctl_backspace[]   = { '\177', 0 };
989*838f5788Ssimonb 	static char k_backspace[]       = { '\b', 0 };
99020006a0bStron 	static char k_home[]            = { '\340', PCK_HOME, 0 };
99120006a0bStron 	static char k_end[]             = { '\340', PCK_END, 0 };
99220006a0bStron 	static char k_up[]              = { '\340', PCK_UP, 0 };
99320006a0bStron 	static char k_down[]            = { '\340', PCK_DOWN, 0 };
99420006a0bStron 	static char k_backtab[]         = { '\340', PCK_SHIFT_TAB, 0 };
99520006a0bStron 	static char k_pagedown[]        = { '\340', PCK_PAGEDOWN, 0 };
99620006a0bStron 	static char k_pageup[]          = { '\340', PCK_PAGEUP, 0 };
99720006a0bStron 	static char k_f1[]              = { '\340', PCK_F1, 0 };
99820006a0bStron #endif
99920006a0bStron #if !MSDOS_COMPILER
100020006a0bStron 	char *sp = tbuf;
100120006a0bStron #endif
100220006a0bStron 
100320006a0bStron 	switch (key)
100420006a0bStron 	{
100520006a0bStron #if OS2
100620006a0bStron 	/*
100720006a0bStron 	 * If windowid is not NULL, assume less is executed in
100820006a0bStron 	 * the XFree86 environment.
100920006a0bStron 	 */
101020006a0bStron 	case SK_RIGHT_ARROW:
101120006a0bStron 		s = windowid ? ltgetstr("kr", &sp) : k_right;
101220006a0bStron 		break;
101320006a0bStron 	case SK_LEFT_ARROW:
101420006a0bStron 		s = windowid ? ltgetstr("kl", &sp) : k_left;
101520006a0bStron 		break;
101620006a0bStron 	case SK_UP_ARROW:
101720006a0bStron 		s = windowid ? ltgetstr("ku", &sp) : k_up;
101820006a0bStron 		break;
101920006a0bStron 	case SK_DOWN_ARROW:
102020006a0bStron 		s = windowid ? ltgetstr("kd", &sp) : k_down;
102120006a0bStron 		break;
102220006a0bStron 	case SK_PAGE_UP:
102320006a0bStron 		s = windowid ? ltgetstr("kP", &sp) : k_pageup;
102420006a0bStron 		break;
102520006a0bStron 	case SK_PAGE_DOWN:
102620006a0bStron 		s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
102720006a0bStron 		break;
102820006a0bStron 	case SK_HOME:
102920006a0bStron 		s = windowid ? ltgetstr("kh", &sp) : k_home;
103020006a0bStron 		break;
103120006a0bStron 	case SK_END:
103220006a0bStron 		s = windowid ? ltgetstr("@7", &sp) : k_end;
103320006a0bStron 		break;
103420006a0bStron 	case SK_DELETE:
1035*838f5788Ssimonb 		s = windowid ? ltgetstr("kD", &sp) : k_delete;
103620006a0bStron 		if (s == NULL)
103720006a0bStron 		{
103820006a0bStron 				tbuf[0] = '\177';
103920006a0bStron 				tbuf[1] = '\0';
104020006a0bStron 				s = tbuf;
104120006a0bStron 		}
104220006a0bStron 		break;
104320006a0bStron #endif
104420006a0bStron #if MSDOS_COMPILER
104520006a0bStron 	case SK_RIGHT_ARROW:
104620006a0bStron 		s = k_right;
104720006a0bStron 		break;
104820006a0bStron 	case SK_LEFT_ARROW:
104920006a0bStron 		s = k_left;
105020006a0bStron 		break;
105120006a0bStron 	case SK_UP_ARROW:
105220006a0bStron 		s = k_up;
105320006a0bStron 		break;
105420006a0bStron 	case SK_DOWN_ARROW:
105520006a0bStron 		s = k_down;
105620006a0bStron 		break;
105720006a0bStron 	case SK_PAGE_UP:
105820006a0bStron 		s = k_pageup;
105920006a0bStron 		break;
106020006a0bStron 	case SK_PAGE_DOWN:
106120006a0bStron 		s = k_pagedown;
106220006a0bStron 		break;
106320006a0bStron 	case SK_HOME:
106420006a0bStron 		s = k_home;
106520006a0bStron 		break;
106620006a0bStron 	case SK_END:
106720006a0bStron 		s = k_end;
106820006a0bStron 		break;
106920006a0bStron 	case SK_DELETE:
107020006a0bStron 		s = k_delete;
107120006a0bStron 		break;
107220006a0bStron #endif
107320006a0bStron #if MSDOS_COMPILER || OS2
107420006a0bStron 	case SK_INSERT:
107520006a0bStron 		s = k_insert;
107620006a0bStron 		break;
107720006a0bStron 	case SK_CTL_LEFT_ARROW:
107820006a0bStron 		s = k_ctl_left;
107920006a0bStron 		break;
108020006a0bStron 	case SK_CTL_RIGHT_ARROW:
108120006a0bStron 		s = k_ctl_right;
108220006a0bStron 		break;
108320006a0bStron 	case SK_CTL_BACKSPACE:
108420006a0bStron 		s = k_ctl_backspace;
108520006a0bStron 		break;
108620006a0bStron 	case SK_CTL_DELETE:
108720006a0bStron 		s = k_ctl_delete;
108820006a0bStron 		break;
1089*838f5788Ssimonb 	case SK_BACKSPACE:
1090*838f5788Ssimonb 		s = k_backspace;
1091*838f5788Ssimonb 		break;
109220006a0bStron 	case SK_F1:
109320006a0bStron 		s = k_f1;
109420006a0bStron 		break;
109520006a0bStron 	case SK_BACKTAB:
109620006a0bStron 		s = k_backtab;
109720006a0bStron 		break;
109820006a0bStron #else
109920006a0bStron 	case SK_RIGHT_ARROW:
110020006a0bStron 		s = ltgetstr("kr", &sp);
110120006a0bStron 		break;
110220006a0bStron 	case SK_LEFT_ARROW:
110320006a0bStron 		s = ltgetstr("kl", &sp);
110420006a0bStron 		break;
110520006a0bStron 	case SK_UP_ARROW:
110620006a0bStron 		s = ltgetstr("ku", &sp);
110720006a0bStron 		break;
110820006a0bStron 	case SK_DOWN_ARROW:
110920006a0bStron 		s = ltgetstr("kd", &sp);
111020006a0bStron 		break;
111120006a0bStron 	case SK_PAGE_UP:
111220006a0bStron 		s = ltgetstr("kP", &sp);
111320006a0bStron 		break;
111420006a0bStron 	case SK_PAGE_DOWN:
111520006a0bStron 		s = ltgetstr("kN", &sp);
111620006a0bStron 		break;
111720006a0bStron 	case SK_HOME:
111820006a0bStron 		s = ltgetstr("kh", &sp);
111920006a0bStron 		break;
112020006a0bStron 	case SK_END:
112120006a0bStron 		s = ltgetstr("@7", &sp);
112220006a0bStron 		break;
112320006a0bStron 	case SK_DELETE:
112420006a0bStron 		s = ltgetstr("kD", &sp);
112520006a0bStron 		if (s == NULL)
112620006a0bStron 		{
112720006a0bStron 				tbuf[0] = '\177';
112820006a0bStron 				tbuf[1] = '\0';
112920006a0bStron 				s = tbuf;
113020006a0bStron 		}
113120006a0bStron 		break;
1132*838f5788Ssimonb 	case SK_BACKSPACE:
1133*838f5788Ssimonb 		s = ltgetstr("kb", &sp);
1134*838f5788Ssimonb 		if (s == NULL)
1135*838f5788Ssimonb 		{
1136*838f5788Ssimonb 				tbuf[0] = '\b';
1137*838f5788Ssimonb 				tbuf[1] = '\0';
1138*838f5788Ssimonb 				s = tbuf;
1139*838f5788Ssimonb 		}
1140*838f5788Ssimonb 		break;
114120006a0bStron #endif
114220006a0bStron 	case SK_CONTROL_K:
114320006a0bStron 		tbuf[0] = CONTROL('K');
114420006a0bStron 		tbuf[1] = '\0';
114520006a0bStron 		s = tbuf;
114620006a0bStron 		break;
114720006a0bStron 	default:
114820006a0bStron 		return (NULL);
114920006a0bStron 	}
115020006a0bStron 	return (s);
115120006a0bStron }
115220006a0bStron 
115320006a0bStron /*
115420006a0bStron  * Get terminal capabilities via termcap.
115520006a0bStron  */
get_term(void)1156*838f5788Ssimonb public void get_term(void)
115720006a0bStron {
1158*838f5788Ssimonb 	termcap_debug = !isnullenv(lgetenv("LESS_TERMCAP_DEBUG"));
115920006a0bStron #if MSDOS_COMPILER
116020006a0bStron 	auto_wrap = 1;
116120006a0bStron 	ignaw = 0;
116220006a0bStron 	can_goto_line = 1;
116320006a0bStron 	clear_bg = 1;
116420006a0bStron 	/*
116520006a0bStron 	 * Set up default colors.
116620006a0bStron 	 * The xx_s_width and xx_e_width vars are already initialized to 0.
116720006a0bStron 	 */
116820006a0bStron #if MSDOS_COMPILER==MSOFTC
116920006a0bStron 	sy_bg_color = _getbkcolor();
117020006a0bStron 	sy_fg_color = _gettextcolor();
117120006a0bStron 	get_clock();
117220006a0bStron #else
117320006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
117420006a0bStron     {
117520006a0bStron 	struct text_info w;
117620006a0bStron 	gettextinfo(&w);
117720006a0bStron 	sy_bg_color = (w.attribute >> 4) & 0x0F;
117820006a0bStron 	sy_fg_color = (w.attribute >> 0) & 0x0F;
117920006a0bStron     }
118020006a0bStron #else
118120006a0bStron #if MSDOS_COMPILER==WIN32C
118220006a0bStron     {
118320006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO scr;
118420006a0bStron 
118520006a0bStron 	con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
118620006a0bStron 	/*
118720006a0bStron 	 * Always open stdin in binary. Note this *must* be done
118820006a0bStron 	 * before any file operations have been done on fd0.
118920006a0bStron 	 */
119020006a0bStron 	SET_BINARY(0);
1191*838f5788Ssimonb 	GetConsoleMode(con_out, &init_output_mode);
119220006a0bStron 	GetConsoleScreenBufferInfo(con_out, &scr);
1193*838f5788Ssimonb 	curr_attr = scr.wAttributes;
119420006a0bStron 	sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
119520006a0bStron 	sy_fg_color = curr_attr & FG_COLORS;
119620006a0bStron     }
119720006a0bStron #endif
119820006a0bStron #endif
119920006a0bStron #endif
120020006a0bStron 	nm_fg_color = sy_fg_color;
120120006a0bStron 	nm_bg_color = sy_bg_color;
120220006a0bStron 	bo_fg_color = 11;
120320006a0bStron 	bo_bg_color = 0;
120420006a0bStron 	ul_fg_color = 9;
120520006a0bStron 	ul_bg_color = 0;
120620006a0bStron 	so_fg_color = 15;
120720006a0bStron 	so_bg_color = 9;
120820006a0bStron 	bl_fg_color = 15;
120920006a0bStron 	bl_bg_color = 0;
1210*838f5788Ssimonb 	sgr_mode = 0;
121120006a0bStron 
121220006a0bStron 	/*
121320006a0bStron 	 * Get size of the screen.
121420006a0bStron 	 */
121520006a0bStron 	scrsize();
121620006a0bStron 	pos_init();
121720006a0bStron 
121820006a0bStron 
121920006a0bStron #else /* !MSDOS_COMPILER */
1220*838f5788Ssimonb {
122120006a0bStron 	char *sp;
1222*838f5788Ssimonb 	char *t1, *t2;
122320006a0bStron 	char *term;
1224*838f5788Ssimonb 	/*
1225*838f5788Ssimonb 	 * Some termcap libraries assume termbuf is static
1226*838f5788Ssimonb 	 * (accessible after tgetent returns).
1227*838f5788Ssimonb 	 */
1228*838f5788Ssimonb 	static char termbuf[TERMBUF_SIZE];
122920006a0bStron 	static char sbuf[TERMSBUF_SIZE];
123020006a0bStron 
123120006a0bStron #if OS2
123220006a0bStron 	/*
123320006a0bStron 	 * Make sure the termcap database is available.
123420006a0bStron 	 */
123520006a0bStron 	sp = lgetenv("TERMCAP");
1236*838f5788Ssimonb 	if (isnullenv(sp))
123720006a0bStron 	{
123820006a0bStron 		char *termcap;
123920006a0bStron 		if ((sp = homefile("termcap.dat")) != NULL)
124020006a0bStron 		{
124120006a0bStron 			termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
124220006a0bStron 			sprintf(termcap, "TERMCAP=%s", sp);
124320006a0bStron 			free(sp);
124420006a0bStron 			putenv(termcap);
124520006a0bStron 		}
124620006a0bStron 	}
124720006a0bStron #endif
124820006a0bStron 	/*
124920006a0bStron 	 * Find out what kind of terminal this is.
125020006a0bStron 	 */
125120006a0bStron 	if ((term = lgetenv("TERM")) == NULL)
125220006a0bStron 		term = DEFAULT_TERM;
125320006a0bStron 	hardcopy = 0;
1254*838f5788Ssimonb 	/* {{ Should probably just pass NULL instead of termbuf. }} */
125520006a0bStron 	if (tgetent(termbuf, term) != TGETENT_OK)
125620006a0bStron 		hardcopy = 1;
125720006a0bStron 	if (ltgetflag("hc"))
125820006a0bStron 		hardcopy = 1;
125920006a0bStron 
126020006a0bStron 	/*
126120006a0bStron 	 * Get size of the screen.
126220006a0bStron 	 */
126320006a0bStron 	scrsize();
126420006a0bStron 	pos_init();
126520006a0bStron 
126620006a0bStron 	auto_wrap = ltgetflag("am");
126720006a0bStron 	ignaw = ltgetflag("xn");
126820006a0bStron 	above_mem = ltgetflag("da");
126920006a0bStron 	below_mem = ltgetflag("db");
127020006a0bStron 	clear_bg = ltgetflag("ut");
1271*838f5788Ssimonb 	no_alt_screen = ltgetflag("NR");
127220006a0bStron 
127320006a0bStron 	/*
127420006a0bStron 	 * Assumes termcap variable "sg" is the printing width of:
127520006a0bStron 	 * the standout sequence, the end standout sequence,
127620006a0bStron 	 * the underline sequence, the end underline sequence,
127720006a0bStron 	 * the boldface sequence, and the end boldface sequence.
127820006a0bStron 	 */
127920006a0bStron 	if ((so_s_width = ltgetnum("sg")) < 0)
128020006a0bStron 		so_s_width = 0;
128120006a0bStron 	so_e_width = so_s_width;
128220006a0bStron 
128320006a0bStron 	bo_s_width = bo_e_width = so_s_width;
128420006a0bStron 	ul_s_width = ul_e_width = so_s_width;
128520006a0bStron 	bl_s_width = bl_e_width = so_s_width;
128620006a0bStron 
128720006a0bStron #if HILITE_SEARCH
128820006a0bStron 	if (so_s_width > 0 || so_e_width > 0)
128920006a0bStron 		/*
129020006a0bStron 		 * Disable highlighting by default on magic cookie terminals.
129120006a0bStron 		 * Turning on highlighting might change the displayed width
129220006a0bStron 		 * of a line, causing the display to get messed up.
129320006a0bStron 		 * The user can turn it back on with -g,
129420006a0bStron 		 * but she won't like the results.
129520006a0bStron 		 */
129620006a0bStron 		hilite_search = 0;
129720006a0bStron #endif
129820006a0bStron 
129920006a0bStron 	/*
130020006a0bStron 	 * Get various string-valued capabilities.
130120006a0bStron 	 */
130220006a0bStron 	sp = sbuf;
130320006a0bStron 
130420006a0bStron #if HAVE_OSPEED
130520006a0bStron 	sc_pad = ltgetstr("pc", &sp);
130620006a0bStron 	if (sc_pad != NULL)
130720006a0bStron 		PC = *sc_pad;
130820006a0bStron #endif
130920006a0bStron 
131020006a0bStron 	sc_s_keypad = ltgetstr("ks", &sp);
131120006a0bStron 	if (sc_s_keypad == NULL)
131220006a0bStron 		sc_s_keypad = "";
131320006a0bStron 	sc_e_keypad = ltgetstr("ke", &sp);
131420006a0bStron 	if (sc_e_keypad == NULL)
131520006a0bStron 		sc_e_keypad = "";
1316*838f5788Ssimonb 	kent = ltgetstr("@8", &sp);
1317*838f5788Ssimonb 
1318*838f5788Ssimonb 	sc_s_mousecap = ltgetstr("MOUSE_START", &sp);
1319*838f5788Ssimonb 	if (sc_s_mousecap == NULL)
1320*838f5788Ssimonb 		sc_s_mousecap = ESCS "[?1000h" ESCS "[?1006h";
1321*838f5788Ssimonb 	sc_e_mousecap = ltgetstr("MOUSE_END", &sp);
1322*838f5788Ssimonb 	if (sc_e_mousecap == NULL)
1323*838f5788Ssimonb 		sc_e_mousecap = ESCS "[?1006l" ESCS "[?1000l";
132420006a0bStron 
1325824a88bbStron 	/*
1326824a88bbStron 	 * This loses for terminals with termcap entries with ti/te strings
1327824a88bbStron 	 * that switch to/from an alternate screen, and we're in quit_at_eof
1328824a88bbStron 	 * (eg, more(1)).
1329824a88bbStron 	 */
1330824a88bbStron 	if (quit_at_eof != OPT_ONPLUS && !more_mode) {
133120006a0bStron 		sc_init = ltgetstr("ti", &sp);
1332824a88bbStron 		sc_deinit = ltgetstr("te", &sp);
1333824a88bbStron 	} else {
1334824a88bbStron 		sc_init = NULL;
1335824a88bbStron 		sc_deinit = NULL;
1336824a88bbStron 	}
133720006a0bStron 	if (sc_init == NULL)
133820006a0bStron 		sc_init = "";
133920006a0bStron 	if (sc_deinit == NULL)
134020006a0bStron 		sc_deinit = "";
134120006a0bStron 
134220006a0bStron 	sc_eol_clear = ltgetstr("ce", &sp);
134320006a0bStron 	if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
134420006a0bStron 	{
134520006a0bStron 		missing_cap = 1;
134620006a0bStron 		sc_eol_clear = "";
134720006a0bStron 	}
134820006a0bStron 
134920006a0bStron 	sc_eos_clear = ltgetstr("cd", &sp);
135020006a0bStron 	if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
135120006a0bStron 	{
135220006a0bStron 		missing_cap = 1;
135320006a0bStron 		sc_eos_clear = "";
135420006a0bStron 	}
135520006a0bStron 
135620006a0bStron 	sc_clear = ltgetstr("cl", &sp);
135720006a0bStron 	if (sc_clear == NULL || *sc_clear == '\0')
135820006a0bStron 	{
135920006a0bStron 		missing_cap = 1;
136020006a0bStron 		sc_clear = "\n\n";
136120006a0bStron 	}
136220006a0bStron 
136320006a0bStron 	sc_move = ltgetstr("cm", &sp);
136420006a0bStron 	if (sc_move == NULL || *sc_move == '\0')
136520006a0bStron 	{
136620006a0bStron 		/*
136720006a0bStron 		 * This is not an error here, because we don't
136820006a0bStron 		 * always need sc_move.
136920006a0bStron 		 * We need it only if we don't have home or lower-left.
137020006a0bStron 		 */
137120006a0bStron 		sc_move = "";
137220006a0bStron 		can_goto_line = 0;
137320006a0bStron 	} else
137420006a0bStron 		can_goto_line = 1;
137520006a0bStron 
137620006a0bStron 	tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
137720006a0bStron 	tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
137820006a0bStron 	tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
137920006a0bStron 	tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
138020006a0bStron 
138120006a0bStron 	sc_visual_bell = ltgetstr("vb", &sp);
138220006a0bStron 	if (sc_visual_bell == NULL)
138320006a0bStron 		sc_visual_bell = "";
138420006a0bStron 
138520006a0bStron 	if (ltgetflag("bs"))
138620006a0bStron 		sc_backspace = "\b";
138720006a0bStron 	else
138820006a0bStron 	{
138920006a0bStron 		sc_backspace = ltgetstr("bc", &sp);
139020006a0bStron 		if (sc_backspace == NULL || *sc_backspace == '\0')
139120006a0bStron 			sc_backspace = "\b";
139220006a0bStron 	}
139320006a0bStron 
139420006a0bStron 	/*
139520006a0bStron 	 * Choose between using "ho" and "cm" ("home" and "cursor move")
139620006a0bStron 	 * to move the cursor to the upper left corner of the screen.
139720006a0bStron 	 */
139820006a0bStron 	t1 = ltgetstr("ho", &sp);
139920006a0bStron 	if (t1 == NULL)
140020006a0bStron 		t1 = "";
140120006a0bStron 	if (*sc_move == '\0')
140220006a0bStron 		t2 = "";
140320006a0bStron 	else
140420006a0bStron 	{
140520006a0bStron 		strcpy(sp, tgoto(sc_move, 0, 0));
140620006a0bStron 		t2 = sp;
140720006a0bStron 		sp += strlen(sp) + 1;
140820006a0bStron 	}
140920006a0bStron 	sc_home = cheaper(t1, t2, "|\b^");
141020006a0bStron 
141120006a0bStron 	/*
141220006a0bStron 	 * Choose between using "ll" and "cm"  ("lower left" and "cursor move")
141320006a0bStron 	 * to move the cursor to the lower left corner of the screen.
141420006a0bStron 	 */
141520006a0bStron 	t1 = ltgetstr("ll", &sp);
1416*838f5788Ssimonb 	if (t1 == NULL || !full_screen)
141720006a0bStron 		t1 = "";
141820006a0bStron 	if (*sc_move == '\0')
141920006a0bStron 		t2 = "";
142020006a0bStron 	else
142120006a0bStron 	{
142220006a0bStron 		strcpy(sp, tgoto(sc_move, 0, sc_height-1));
142320006a0bStron 		t2 = sp;
142420006a0bStron 		sp += strlen(sp) + 1;
142520006a0bStron 	}
142620006a0bStron 	sc_lower_left = cheaper(t1, t2, "\r");
142720006a0bStron 
142820006a0bStron 	/*
142920006a0bStron 	 * Get carriage return string.
143020006a0bStron 	 */
143120006a0bStron 	sc_return = ltgetstr("cr", &sp);
143220006a0bStron 	if (sc_return == NULL)
143320006a0bStron 		sc_return = "\r";
143420006a0bStron 
143520006a0bStron 	/*
143620006a0bStron 	 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
143720006a0bStron 	 * to add a line at the top of the screen.
143820006a0bStron 	 */
143920006a0bStron 	t1 = ltgetstr("al", &sp);
144020006a0bStron 	if (t1 == NULL)
144120006a0bStron 		t1 = "";
144220006a0bStron 	t2 = ltgetstr("sr", &sp);
144320006a0bStron 	if (t2 == NULL)
144420006a0bStron 		t2 = "";
144520006a0bStron #if OS2
144620006a0bStron 	if (*t1 == '\0' && *t2 == '\0')
144720006a0bStron 		sc_addline = "";
144820006a0bStron 	else
144920006a0bStron #endif
145020006a0bStron 	if (above_mem)
145120006a0bStron 		sc_addline = t1;
145220006a0bStron 	else
145320006a0bStron 		sc_addline = cheaper(t1, t2, "");
145420006a0bStron 	if (*sc_addline == '\0')
145520006a0bStron 	{
145620006a0bStron 		/*
145720006a0bStron 		 * Force repaint on any backward movement.
145820006a0bStron 		 */
145920006a0bStron 		no_back_scroll = 1;
146020006a0bStron 	}
1461*838f5788Ssimonb }
146220006a0bStron #endif /* MSDOS_COMPILER */
146320006a0bStron }
146420006a0bStron 
146520006a0bStron #if !MSDOS_COMPILER
146620006a0bStron /*
146720006a0bStron  * Return the cost of displaying a termcap string.
146820006a0bStron  * We use the trick of calling tputs, but as a char printing function
146920006a0bStron  * we give it inc_costcount, which just increments "costcount".
147020006a0bStron  * This tells us how many chars would be printed by using this string.
147120006a0bStron  * {{ Couldn't we just use strlen? }}
147220006a0bStron  */
147320006a0bStron static int costcount;
147420006a0bStron 
147520006a0bStron /*ARGSUSED*/
inc_costcount(int c)1476*838f5788Ssimonb static int inc_costcount(int c)
147720006a0bStron {
147820006a0bStron 	costcount++;
147920006a0bStron 	return (c);
148020006a0bStron }
148120006a0bStron 
cost(char * t)1482*838f5788Ssimonb static int cost(char *t)
148320006a0bStron {
148420006a0bStron 	costcount = 0;
148520006a0bStron 	tputs(t, sc_height, inc_costcount);
148620006a0bStron 	return (costcount);
148720006a0bStron }
148820006a0bStron 
148920006a0bStron /*
149020006a0bStron  * Return the "best" of the two given termcap strings.
149120006a0bStron  * The best, if both exist, is the one with the lower
149220006a0bStron  * cost (see cost() function).
149320006a0bStron  */
cheaper(char * t1,char * t2,char * def)1494*838f5788Ssimonb static char * cheaper(char *t1, char *t2, char *def)
149520006a0bStron {
149620006a0bStron 	if (*t1 == '\0' && *t2 == '\0')
149720006a0bStron 	{
149820006a0bStron 		missing_cap = 1;
149920006a0bStron 		return (def);
150020006a0bStron 	}
150120006a0bStron 	if (*t1 == '\0')
150220006a0bStron 		return (t2);
150320006a0bStron 	if (*t2 == '\0')
150420006a0bStron 		return (t1);
150520006a0bStron 	if (cost(t1) < cost(t2))
150620006a0bStron 		return (t1);
150720006a0bStron 	return (t2);
150820006a0bStron }
150920006a0bStron 
tmodes(char * incap,char * outcap,char ** instr,char ** outstr,char * def_instr,char * def_outstr,char ** spp)1510*838f5788Ssimonb static void tmodes(char *incap, char *outcap, char **instr, char **outstr, char *def_instr, char *def_outstr, char **spp)
151120006a0bStron {
151220006a0bStron 	*instr = ltgetstr(incap, spp);
151320006a0bStron 	if (*instr == NULL)
151420006a0bStron 	{
151520006a0bStron 		/* Use defaults. */
151620006a0bStron 		*instr = def_instr;
151720006a0bStron 		*outstr = def_outstr;
151820006a0bStron 		return;
151920006a0bStron 	}
152020006a0bStron 
152120006a0bStron 	*outstr = ltgetstr(outcap, spp);
152220006a0bStron 	if (*outstr == NULL)
152320006a0bStron 		/* No specific out capability; use "me". */
152420006a0bStron 		*outstr = ltgetstr("me", spp);
152520006a0bStron 	if (*outstr == NULL)
152620006a0bStron 		/* Don't even have "me"; use a null string. */
152720006a0bStron 		*outstr = "";
152820006a0bStron }
152920006a0bStron 
153020006a0bStron #endif /* MSDOS_COMPILER */
153120006a0bStron 
153220006a0bStron 
153320006a0bStron /*
153420006a0bStron  * Below are the functions which perform all the
153520006a0bStron  * terminal-specific screen manipulation.
153620006a0bStron  */
153720006a0bStron 
153820006a0bStron 
153920006a0bStron #if MSDOS_COMPILER
154020006a0bStron 
154120006a0bStron #if MSDOS_COMPILER==WIN32C
_settextposition(int row,int col)1542*838f5788Ssimonb static void _settextposition(int row, int col)
154320006a0bStron {
154420006a0bStron 	COORD cpos;
154520006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO csbi;
154620006a0bStron 
154720006a0bStron 	GetConsoleScreenBufferInfo(con_out, &csbi);
154820006a0bStron 	cpos.X = csbi.srWindow.Left + (col - 1);
154920006a0bStron 	cpos.Y = csbi.srWindow.Top + (row - 1);
155020006a0bStron 	SetConsoleCursorPosition(con_out, cpos);
155120006a0bStron }
155220006a0bStron #endif
155320006a0bStron 
155420006a0bStron /*
155520006a0bStron  * Initialize the screen to the correct color at startup.
155620006a0bStron  */
initcolor(void)1557*838f5788Ssimonb static void initcolor(void)
155820006a0bStron {
1559*838f5788Ssimonb #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1560*838f5788Ssimonb 	intensevideo();
1561*838f5788Ssimonb #endif
156220006a0bStron 	SETCOLORS(nm_fg_color, nm_bg_color);
156320006a0bStron #if 0
156420006a0bStron 	/*
156520006a0bStron 	 * This clears the screen at startup.  This is different from
156620006a0bStron 	 * the behavior of other versions of less.  Disable it for now.
156720006a0bStron 	 */
156820006a0bStron 	char *blanks;
156920006a0bStron 	int row;
157020006a0bStron 	int col;
157120006a0bStron 
157220006a0bStron 	/*
157320006a0bStron 	 * Create a complete, blank screen using "normal" colors.
157420006a0bStron 	 */
157520006a0bStron 	SETCOLORS(nm_fg_color, nm_bg_color);
157620006a0bStron 	blanks = (char *) ecalloc(width+1, sizeof(char));
157720006a0bStron 	for (col = 0;  col < sc_width;  col++)
157820006a0bStron 		blanks[col] = ' ';
157920006a0bStron 	blanks[sc_width] = '\0';
158020006a0bStron 	for (row = 0;  row < sc_height;  row++)
158120006a0bStron 		_outtext(blanks);
158220006a0bStron 	free(blanks);
158320006a0bStron #endif
158420006a0bStron }
158520006a0bStron #endif
158620006a0bStron 
158720006a0bStron #if MSDOS_COMPILER==WIN32C
158820006a0bStron 
158920006a0bStron /*
1590*838f5788Ssimonb  * Enable virtual terminal processing, if available.
1591*838f5788Ssimonb  */
win32_init_vt_term(void)1592*838f5788Ssimonb static void win32_init_vt_term(void)
1593*838f5788Ssimonb {
1594*838f5788Ssimonb 	DWORD output_mode;
1595*838f5788Ssimonb 
1596*838f5788Ssimonb 	if (vt_enabled == 0 || (vt_enabled == 1 && con_out == con_out_ours))
1597*838f5788Ssimonb 		return;
1598*838f5788Ssimonb 
1599*838f5788Ssimonb 	GetConsoleMode(con_out, &output_mode);
1600*838f5788Ssimonb 	vt_enabled = SetConsoleMode(con_out,
1601*838f5788Ssimonb 		       output_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
1602*838f5788Ssimonb 	if (vt_enabled)
1603*838f5788Ssimonb 	{
1604*838f5788Ssimonb 	    auto_wrap = 0;
1605*838f5788Ssimonb 	    ignaw = 1;
1606*838f5788Ssimonb 	}
1607*838f5788Ssimonb }
1608*838f5788Ssimonb 
win32_deinit_vt_term(void)1609*838f5788Ssimonb static void win32_deinit_vt_term(void)
1610*838f5788Ssimonb {
1611*838f5788Ssimonb 	if (vt_enabled == 1 && con_out == con_out_save)
1612*838f5788Ssimonb 		SetConsoleMode(con_out, init_output_mode);
1613*838f5788Ssimonb }
1614*838f5788Ssimonb 
1615*838f5788Ssimonb /*
161620006a0bStron  * Termcap-like init with a private win32 console.
161720006a0bStron  */
win32_init_term(void)1618*838f5788Ssimonb static void win32_init_term(void)
161920006a0bStron {
162020006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO scr;
162120006a0bStron 	COORD size;
162220006a0bStron 
162320006a0bStron 	if (con_out_save == INVALID_HANDLE_VALUE)
162420006a0bStron 		return;
162520006a0bStron 
162620006a0bStron 	GetConsoleScreenBufferInfo(con_out_save, &scr);
162720006a0bStron 
162820006a0bStron 	if (con_out_ours == INVALID_HANDLE_VALUE)
162920006a0bStron 	{
163020006a0bStron 		/*
163120006a0bStron 		 * Create our own screen buffer, so that we
163220006a0bStron 		 * may restore the original when done.
163320006a0bStron 		 */
163420006a0bStron 		con_out_ours = CreateConsoleScreenBuffer(
163520006a0bStron 			GENERIC_WRITE | GENERIC_READ,
163620006a0bStron 			FILE_SHARE_WRITE | FILE_SHARE_READ,
163720006a0bStron 			(LPSECURITY_ATTRIBUTES) NULL,
163820006a0bStron 			CONSOLE_TEXTMODE_BUFFER,
163920006a0bStron 			(LPVOID) NULL);
164020006a0bStron 	}
164120006a0bStron 
164220006a0bStron 	size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
164320006a0bStron 	size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
164420006a0bStron 	SetConsoleScreenBufferSize(con_out_ours, size);
164520006a0bStron 	SetConsoleActiveScreenBuffer(con_out_ours);
164620006a0bStron 	con_out = con_out_ours;
164720006a0bStron }
164820006a0bStron 
164920006a0bStron /*
165020006a0bStron  * Restore the startup console.
165120006a0bStron  */
win32_deinit_term(void)1652*838f5788Ssimonb static void win32_deinit_term(void)
165320006a0bStron {
165420006a0bStron 	if (con_out_save == INVALID_HANDLE_VALUE)
165520006a0bStron 		return;
165620006a0bStron 	if (quitting)
165720006a0bStron 		(void) CloseHandle(con_out_ours);
165820006a0bStron 	SetConsoleActiveScreenBuffer(con_out_save);
165920006a0bStron 	con_out = con_out_save;
166020006a0bStron }
166120006a0bStron 
166220006a0bStron #endif
166320006a0bStron 
1664*838f5788Ssimonb #if !MSDOS_COMPILER
do_tputs(char * str,int affcnt,int (* f_putc)(int))1665*838f5788Ssimonb static void do_tputs(char *str, int affcnt, int (*f_putc)(int))
1666*838f5788Ssimonb {
1667*838f5788Ssimonb #if LESSTEST
1668*838f5788Ssimonb 	if (ttyin_name != NULL && f_putc == putchr)
1669*838f5788Ssimonb 		putstr(str);
1670*838f5788Ssimonb 	else
1671*838f5788Ssimonb #endif /*LESSTEST*/
1672*838f5788Ssimonb 		tputs(str, affcnt, f_putc);
1673*838f5788Ssimonb }
1674*838f5788Ssimonb 
1675*838f5788Ssimonb /*
1676*838f5788Ssimonb  * Like tputs but we handle $<...> delay strings here because
1677*838f5788Ssimonb  * some implementations of tputs don't perform delays correctly.
1678*838f5788Ssimonb  */
ltputs(char * str,int affcnt,int (* f_putc)(int))1679*838f5788Ssimonb static void ltputs(char *str, int affcnt, int (*f_putc)(int))
1680*838f5788Ssimonb {
1681*838f5788Ssimonb 	while (str != NULL && *str != '\0')
1682*838f5788Ssimonb 	{
1683*838f5788Ssimonb #if HAVE_STRSTR
1684*838f5788Ssimonb 		char *obrac = strstr(str, "$<");
1685*838f5788Ssimonb 		if (obrac != NULL)
1686*838f5788Ssimonb 		{
1687*838f5788Ssimonb 			char str2[64];
1688*838f5788Ssimonb 			int slen = obrac - str;
1689*838f5788Ssimonb 			if (slen < sizeof(str2))
1690*838f5788Ssimonb 			{
1691*838f5788Ssimonb 				int delay;
1692*838f5788Ssimonb 				/* Output first part of string (before "$<"). */
1693*838f5788Ssimonb 				memcpy(str2, str, slen);
1694*838f5788Ssimonb 				str2[slen] = '\0';
1695*838f5788Ssimonb 				do_tputs(str2, affcnt, f_putc);
1696*838f5788Ssimonb 				str += slen + 2;
1697*838f5788Ssimonb 				/* Perform the delay. */
1698*838f5788Ssimonb 				delay = lstrtoi(str, &str, 10);
1699*838f5788Ssimonb 				if (*str == '*')
1700*838f5788Ssimonb 					if (ckd_mul(&delay, delay, affcnt))
1701*838f5788Ssimonb 						delay = INT_MAX;
1702*838f5788Ssimonb 				flush();
1703*838f5788Ssimonb 				sleep_ms(delay);
1704*838f5788Ssimonb 				/* Skip past closing ">" at end of delay string. */
1705*838f5788Ssimonb 				str = strstr(str, ">");
1706*838f5788Ssimonb 				if (str != NULL)
1707*838f5788Ssimonb 					str++;
1708*838f5788Ssimonb 				continue;
1709*838f5788Ssimonb 			}
1710*838f5788Ssimonb 		}
1711*838f5788Ssimonb #endif
1712*838f5788Ssimonb 		/* Pass the rest of the string to tputs and we're done. */
1713*838f5788Ssimonb 		do_tputs(str, affcnt, f_putc);
1714*838f5788Ssimonb 		break;
1715*838f5788Ssimonb 	}
1716*838f5788Ssimonb }
1717*838f5788Ssimonb #endif /* MSDOS_COMPILER */
1718*838f5788Ssimonb 
1719*838f5788Ssimonb /*
1720*838f5788Ssimonb  * Configure the terminal so mouse clicks and wheel moves
1721*838f5788Ssimonb  * produce input to less.
1722*838f5788Ssimonb  */
init_mouse(void)1723*838f5788Ssimonb public void init_mouse(void)
1724*838f5788Ssimonb {
1725*838f5788Ssimonb #if !MSDOS_COMPILER
1726*838f5788Ssimonb 	ltputs(sc_s_mousecap, sc_height, putchr);
1727*838f5788Ssimonb #else
1728*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
1729*838f5788Ssimonb 	SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT
1730*838f5788Ssimonb 			    | ENABLE_EXTENDED_FLAGS /* disable quick edit */);
1731*838f5788Ssimonb 
1732*838f5788Ssimonb #endif
1733*838f5788Ssimonb #endif
1734*838f5788Ssimonb }
1735*838f5788Ssimonb 
1736*838f5788Ssimonb /*
1737*838f5788Ssimonb  * Configure the terminal so mouse clicks and wheel moves
1738*838f5788Ssimonb  * are handled by the system (so text can be selected, etc).
1739*838f5788Ssimonb  */
deinit_mouse(void)1740*838f5788Ssimonb public void deinit_mouse(void)
1741*838f5788Ssimonb {
1742*838f5788Ssimonb #if !MSDOS_COMPILER
1743*838f5788Ssimonb 	ltputs(sc_e_mousecap, sc_height, putchr);
1744*838f5788Ssimonb #else
1745*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
1746*838f5788Ssimonb 	SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_EXTENDED_FLAGS
1747*838f5788Ssimonb 			    | (console_mode & ENABLE_QUICK_EDIT_MODE));
1748*838f5788Ssimonb #endif
1749*838f5788Ssimonb #endif
1750*838f5788Ssimonb }
1751*838f5788Ssimonb 
175220006a0bStron /*
175320006a0bStron  * Initialize terminal
175420006a0bStron  */
init(void)1755*838f5788Ssimonb public void init(void)
175620006a0bStron {
1757*838f5788Ssimonb 	clear_bot_if_needed();
175820006a0bStron #if !MSDOS_COMPILER
1759*838f5788Ssimonb 	if (!(quit_if_one_screen && one_screen))
1760*838f5788Ssimonb 	{
176120006a0bStron 		if (!no_init)
1762*838f5788Ssimonb 		{
1763*838f5788Ssimonb 			ltputs(sc_init, sc_height, putchr);
1764*838f5788Ssimonb 			/*
1765*838f5788Ssimonb 			 * Some terminals leave the cursor unmoved when switching
1766*838f5788Ssimonb 			 * to the alt screen. To avoid having the text appear at
1767*838f5788Ssimonb 			 * a seemingly random line on the alt screen, move to
1768*838f5788Ssimonb 			 * lower left if we are using an alt screen.
1769*838f5788Ssimonb 			 */
1770*838f5788Ssimonb 			if (*sc_init != '\0' && *sc_deinit != '\0' && !no_alt_screen)
1771*838f5788Ssimonb 				lower_left();
1772*838f5788Ssimonb 			term_init_done = 1;
1773*838f5788Ssimonb 		}
177420006a0bStron 		if (!no_keypad)
1775*838f5788Ssimonb 			ltputs(sc_s_keypad, sc_height, putchr);
1776*838f5788Ssimonb 		if (mousecap)
1777*838f5788Ssimonb 			init_mouse();
1778*838f5788Ssimonb 	}
1779*838f5788Ssimonb 	init_done = 1;
178020006a0bStron 	if (top_scroll)
178120006a0bStron 	{
178220006a0bStron 		int i;
178320006a0bStron 
178420006a0bStron 		/*
178520006a0bStron 		 * This is nice to terminals with no alternate screen,
178620006a0bStron 		 * but with saved scrolled-off-the-top lines.  This way,
178720006a0bStron 		 * no previous line is lost, but we start with a whole
178820006a0bStron 		 * screen to ourself.
178920006a0bStron 		 */
179020006a0bStron 		for (i = 1; i < sc_height; i++)
179120006a0bStron 			putchr('\n');
179220006a0bStron 	} else
179320006a0bStron 		line_left();
179420006a0bStron #else
179520006a0bStron #if MSDOS_COMPILER==WIN32C
1796*838f5788Ssimonb 	if (!(quit_if_one_screen && one_screen))
1797*838f5788Ssimonb 	{
179820006a0bStron 		if (!no_init)
1799*838f5788Ssimonb 		{
180020006a0bStron 			win32_init_term();
1801*838f5788Ssimonb 			term_init_done = 1;
1802*838f5788Ssimonb 		}
1803*838f5788Ssimonb 		if (mousecap)
1804*838f5788Ssimonb 			init_mouse();
1805*838f5788Ssimonb 
1806*838f5788Ssimonb 	}
1807*838f5788Ssimonb 	win32_init_vt_term();
180820006a0bStron #endif
1809*838f5788Ssimonb 	init_done = 1;
181020006a0bStron 	initcolor();
181120006a0bStron 	flush();
181220006a0bStron #endif
181320006a0bStron }
181420006a0bStron 
181520006a0bStron /*
181620006a0bStron  * Deinitialize terminal
181720006a0bStron  */
deinit(void)1818*838f5788Ssimonb public void deinit(void)
181920006a0bStron {
182020006a0bStron 	if (!init_done)
182120006a0bStron 		return;
182220006a0bStron #if !MSDOS_COMPILER
1823*838f5788Ssimonb 	if (!(quit_if_one_screen && one_screen))
1824*838f5788Ssimonb 	{
1825*838f5788Ssimonb 		if (mousecap)
1826*838f5788Ssimonb 			deinit_mouse();
182720006a0bStron 		if (!no_keypad)
1828*838f5788Ssimonb 			ltputs(sc_e_keypad, sc_height, putchr);
182920006a0bStron 		if (!no_init)
1830*838f5788Ssimonb 			ltputs(sc_deinit, sc_height, putchr);
1831*838f5788Ssimonb 	}
183220006a0bStron #else
183320006a0bStron 	/* Restore system colors. */
183420006a0bStron 	SETCOLORS(sy_fg_color, sy_bg_color);
183520006a0bStron #if MSDOS_COMPILER==WIN32C
1836*838f5788Ssimonb 	win32_deinit_vt_term();
1837*838f5788Ssimonb 	if (!(quit_if_one_screen && one_screen))
1838*838f5788Ssimonb 	{
1839*838f5788Ssimonb 		if (mousecap)
1840*838f5788Ssimonb 			deinit_mouse();
184120006a0bStron 		if (!no_init)
184220006a0bStron 			win32_deinit_term();
1843*838f5788Ssimonb 	}
184420006a0bStron #else
184520006a0bStron 	/* Need clreol to make SETCOLORS take effect. */
184620006a0bStron 	clreol();
184720006a0bStron #endif
184820006a0bStron #endif
184920006a0bStron 	init_done = 0;
185020006a0bStron }
185120006a0bStron 
185220006a0bStron /*
1853*838f5788Ssimonb  * Are we interactive (ie. writing to an initialized tty)?
1854*838f5788Ssimonb  */
interactive(void)1855*838f5788Ssimonb public int interactive(void)
1856*838f5788Ssimonb {
1857*838f5788Ssimonb 	return (is_tty && init_done);
1858*838f5788Ssimonb }
1859*838f5788Ssimonb 
assert_interactive(void)1860*838f5788Ssimonb static void assert_interactive(void)
1861*838f5788Ssimonb {
1862*838f5788Ssimonb 	if (interactive()) return;
1863*838f5788Ssimonb 	/* abort(); */
1864*838f5788Ssimonb }
1865*838f5788Ssimonb 
1866*838f5788Ssimonb /*
186720006a0bStron  * Home cursor (move to upper left corner of screen).
186820006a0bStron  */
home(void)1869*838f5788Ssimonb public void home(void)
187020006a0bStron {
1871*838f5788Ssimonb 	assert_interactive();
187220006a0bStron #if !MSDOS_COMPILER
1873*838f5788Ssimonb 	ltputs(sc_home, 1, putchr);
187420006a0bStron #else
187520006a0bStron 	flush();
187620006a0bStron 	_settextposition(1,1);
187720006a0bStron #endif
187820006a0bStron }
187920006a0bStron 
1880*838f5788Ssimonb #if LESSTEST
dump_screen(void)1881*838f5788Ssimonb public void dump_screen(void)
1882*838f5788Ssimonb {
1883*838f5788Ssimonb 	char dump_cmd[32];
1884*838f5788Ssimonb 	SNPRINTF1(dump_cmd, sizeof(dump_cmd), ESCS"0;0;%dR", sc_width * sc_height);
1885*838f5788Ssimonb 	ltputs(dump_cmd, sc_height, putchr);
1886*838f5788Ssimonb 	flush();
1887*838f5788Ssimonb }
1888*838f5788Ssimonb #endif /*LESSTEST*/
1889*838f5788Ssimonb 
189020006a0bStron /*
189120006a0bStron  * Add a blank line (called with cursor at home).
189220006a0bStron  * Should scroll the display down.
189320006a0bStron  */
add_line(void)1894*838f5788Ssimonb public void add_line(void)
189520006a0bStron {
1896*838f5788Ssimonb 	assert_interactive();
189720006a0bStron #if !MSDOS_COMPILER
1898*838f5788Ssimonb 	ltputs(sc_addline, sc_height, putchr);
189920006a0bStron #else
190020006a0bStron 	flush();
190120006a0bStron #if MSDOS_COMPILER==MSOFTC
190220006a0bStron 	_scrolltextwindow(_GSCROLLDOWN);
190320006a0bStron 	_settextposition(1,1);
190420006a0bStron #else
190520006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
190620006a0bStron 	movetext(1,1, sc_width,sc_height-1, 1,2);
190720006a0bStron 	gotoxy(1,1);
190820006a0bStron 	clreol();
190920006a0bStron #else
191020006a0bStron #if MSDOS_COMPILER==WIN32C
191120006a0bStron     {
191220006a0bStron 	CHAR_INFO fillchar;
191320006a0bStron 	SMALL_RECT rcSrc, rcClip;
191420006a0bStron 	COORD new_org;
191520006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO csbi;
191620006a0bStron 
191720006a0bStron 	GetConsoleScreenBufferInfo(con_out,&csbi);
191820006a0bStron 
191920006a0bStron 	/* The clip rectangle is the entire visible screen. */
192020006a0bStron 	rcClip.Left = csbi.srWindow.Left;
192120006a0bStron 	rcClip.Top = csbi.srWindow.Top;
192220006a0bStron 	rcClip.Right = csbi.srWindow.Right;
192320006a0bStron 	rcClip.Bottom = csbi.srWindow.Bottom;
192420006a0bStron 
192520006a0bStron 	/* The source rectangle is the visible screen minus the last line. */
192620006a0bStron 	rcSrc = rcClip;
192720006a0bStron 	rcSrc.Bottom--;
192820006a0bStron 
192920006a0bStron 	/* Move the top left corner of the source window down one row. */
193020006a0bStron 	new_org.X = rcSrc.Left;
193120006a0bStron 	new_org.Y = rcSrc.Top + 1;
193220006a0bStron 
193320006a0bStron 	/* Fill the right character and attributes. */
193420006a0bStron 	fillchar.Char.AsciiChar = ' ';
193520006a0bStron 	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
193620006a0bStron 	fillchar.Attributes = curr_attr;
193720006a0bStron 	ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
193820006a0bStron 	_settextposition(1,1);
193920006a0bStron     }
194020006a0bStron #endif
194120006a0bStron #endif
194220006a0bStron #endif
194320006a0bStron #endif
194420006a0bStron }
194520006a0bStron 
194620006a0bStron #if 0
194720006a0bStron /*
194820006a0bStron  * Remove the n topmost lines and scroll everything below it in the
194920006a0bStron  * window upward.  This is needed to stop leaking the topmost line
195020006a0bStron  * into the scrollback buffer when we go down-one-line (in WIN32).
195120006a0bStron  */
1952*838f5788Ssimonb public void remove_top(int n)
195320006a0bStron {
195420006a0bStron #if MSDOS_COMPILER==WIN32C
195520006a0bStron 	SMALL_RECT rcSrc, rcClip;
195620006a0bStron 	CHAR_INFO fillchar;
195720006a0bStron 	COORD new_org;
195820006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
195920006a0bStron 
196020006a0bStron 	if (n >= sc_height - 1)
196120006a0bStron 	{
196220006a0bStron 		clear();
196320006a0bStron 		home();
196420006a0bStron 		return;
196520006a0bStron 	}
196620006a0bStron 
196720006a0bStron 	flush();
196820006a0bStron 
196920006a0bStron 	GetConsoleScreenBufferInfo(con_out, &csbi);
197020006a0bStron 
197120006a0bStron 	/* Get the extent of all-visible-rows-but-the-last. */
197220006a0bStron 	rcSrc.Left    = csbi.srWindow.Left;
197320006a0bStron 	rcSrc.Top     = csbi.srWindow.Top + n;
197420006a0bStron 	rcSrc.Right   = csbi.srWindow.Right;
197520006a0bStron 	rcSrc.Bottom  = csbi.srWindow.Bottom;
197620006a0bStron 
197720006a0bStron 	/* Get the clip rectangle. */
197820006a0bStron 	rcClip.Left   = rcSrc.Left;
197920006a0bStron 	rcClip.Top    = csbi.srWindow.Top;
198020006a0bStron 	rcClip.Right  = rcSrc.Right;
198120006a0bStron 	rcClip.Bottom = rcSrc.Bottom ;
198220006a0bStron 
198320006a0bStron 	/* Move the source window up n rows. */
198420006a0bStron 	new_org.X = rcSrc.Left;
198520006a0bStron 	new_org.Y = rcSrc.Top - n;
198620006a0bStron 
198720006a0bStron 	/* Fill the right character and attributes. */
198820006a0bStron 	fillchar.Char.AsciiChar = ' ';
198920006a0bStron 	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
199020006a0bStron 	fillchar.Attributes = curr_attr;
199120006a0bStron 
199220006a0bStron 	ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
199320006a0bStron 
199420006a0bStron 	/* Position cursor on first blank line. */
199520006a0bStron 	goto_line(sc_height - n - 1);
199620006a0bStron #endif
199720006a0bStron }
199820006a0bStron #endif
199920006a0bStron 
200020006a0bStron #if MSDOS_COMPILER==WIN32C
200120006a0bStron /*
200220006a0bStron  * Clear the screen.
200320006a0bStron  */
win32_clear(void)2004*838f5788Ssimonb static void win32_clear(void)
200520006a0bStron {
200620006a0bStron 	/*
200720006a0bStron 	 * This will clear only the currently visible rows of the NT
200820006a0bStron 	 * console buffer, which means none of the precious scrollback
200920006a0bStron 	 * rows are touched making for faster scrolling.  Note that, if
201020006a0bStron 	 * the window has fewer columns than the console buffer (i.e.
201120006a0bStron 	 * there is a horizontal scrollbar as well), the entire width
201220006a0bStron 	 * of the visible rows will be cleared.
201320006a0bStron 	 */
201420006a0bStron 	COORD topleft;
201520006a0bStron 	DWORD nchars;
201620006a0bStron 	DWORD winsz;
201720006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO csbi;
201820006a0bStron 
201920006a0bStron 	/* get the number of cells in the current buffer */
202020006a0bStron 	GetConsoleScreenBufferInfo(con_out, &csbi);
202120006a0bStron 	winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
202220006a0bStron 	topleft.X = 0;
202320006a0bStron 	topleft.Y = csbi.srWindow.Top;
202420006a0bStron 
202520006a0bStron 	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
202620006a0bStron 	FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
202720006a0bStron 	FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
202820006a0bStron }
202920006a0bStron 
203020006a0bStron /*
203120006a0bStron  * Remove the n topmost lines and scroll everything below it in the
203220006a0bStron  * window upward.
203320006a0bStron  */
win32_scroll_up(int n)2034*838f5788Ssimonb public void win32_scroll_up(int n)
203520006a0bStron {
203620006a0bStron 	SMALL_RECT rcSrc, rcClip;
203720006a0bStron 	CHAR_INFO fillchar;
203820006a0bStron 	COORD topleft;
203920006a0bStron 	COORD new_org;
204020006a0bStron 	DWORD nchars;
204120006a0bStron 	DWORD size;
204220006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO csbi;
204320006a0bStron 
204420006a0bStron 	if (n <= 0)
204520006a0bStron 		return;
204620006a0bStron 
204720006a0bStron 	if (n >= sc_height - 1)
204820006a0bStron 	{
204920006a0bStron 		win32_clear();
205020006a0bStron 		_settextposition(1,1);
205120006a0bStron 		return;
205220006a0bStron 	}
205320006a0bStron 
205420006a0bStron 	/* Get the extent of what will remain visible after scrolling. */
205520006a0bStron 	GetConsoleScreenBufferInfo(con_out, &csbi);
205620006a0bStron 	rcSrc.Left    = csbi.srWindow.Left;
205720006a0bStron 	rcSrc.Top     = csbi.srWindow.Top + n;
205820006a0bStron 	rcSrc.Right   = csbi.srWindow.Right;
205920006a0bStron 	rcSrc.Bottom  = csbi.srWindow.Bottom;
206020006a0bStron 
206120006a0bStron 	/* Get the clip rectangle. */
206220006a0bStron 	rcClip.Left   = rcSrc.Left;
206320006a0bStron 	rcClip.Top    = csbi.srWindow.Top;
206420006a0bStron 	rcClip.Right  = rcSrc.Right;
206520006a0bStron 	rcClip.Bottom = rcSrc.Bottom ;
206620006a0bStron 
206720006a0bStron 	/* Move the source text to the top of the screen. */
206820006a0bStron 	new_org.X = rcSrc.Left;
206920006a0bStron 	new_org.Y = rcClip.Top;
207020006a0bStron 
207120006a0bStron 	/* Fill the right character and attributes. */
207220006a0bStron 	fillchar.Char.AsciiChar = ' ';
207320006a0bStron 	fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
207420006a0bStron 
207520006a0bStron 	/* Scroll the window. */
207620006a0bStron 	SetConsoleTextAttribute(con_out, fillchar.Attributes);
207720006a0bStron 	ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
207820006a0bStron 
207920006a0bStron 	/* Clear remaining lines at bottom. */
208020006a0bStron 	topleft.X = csbi.dwCursorPosition.X;
208120006a0bStron 	topleft.Y = rcSrc.Bottom - n;
208220006a0bStron 	size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
208320006a0bStron 	FillConsoleOutputCharacter(con_out, ' ', size, topleft,
208420006a0bStron 		&nchars);
208520006a0bStron 	FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
208620006a0bStron 		&nchars);
208720006a0bStron 	SetConsoleTextAttribute(con_out, curr_attr);
208820006a0bStron 
208920006a0bStron 	/* Move cursor n lines up from where it was. */
209020006a0bStron 	csbi.dwCursorPosition.Y -= n;
209120006a0bStron 	SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
209220006a0bStron }
209320006a0bStron #endif
209420006a0bStron 
209520006a0bStron /*
209620006a0bStron  * Move cursor to lower left corner of screen.
209720006a0bStron  */
lower_left(void)2098*838f5788Ssimonb public void lower_left(void)
209920006a0bStron {
2100*838f5788Ssimonb 	assert_interactive();
210120006a0bStron #if !MSDOS_COMPILER
2102*838f5788Ssimonb 	ltputs(sc_lower_left, 1, putchr);
210320006a0bStron #else
210420006a0bStron 	flush();
210520006a0bStron 	_settextposition(sc_height, 1);
210620006a0bStron #endif
210720006a0bStron }
210820006a0bStron 
210920006a0bStron /*
211020006a0bStron  * Move cursor to left position of current line.
211120006a0bStron  */
line_left(void)2112*838f5788Ssimonb public void line_left(void)
211320006a0bStron {
2114*838f5788Ssimonb 	assert_interactive();
211520006a0bStron #if !MSDOS_COMPILER
2116*838f5788Ssimonb 	ltputs(sc_return, 1, putchr);
211720006a0bStron #else
2118*838f5788Ssimonb 	{
211920006a0bStron 		int row;
212020006a0bStron 		flush();
212120006a0bStron #if MSDOS_COMPILER==WIN32C
212220006a0bStron 		{
212320006a0bStron 			CONSOLE_SCREEN_BUFFER_INFO scr;
212420006a0bStron 			GetConsoleScreenBufferInfo(con_out, &scr);
212520006a0bStron 			row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
212620006a0bStron 		}
212720006a0bStron #else
212820006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
212920006a0bStron 			row = wherey();
213020006a0bStron #else
213120006a0bStron 		{
213220006a0bStron 			struct rccoord tpos = _gettextposition();
213320006a0bStron 			row = tpos.row;
213420006a0bStron 		}
213520006a0bStron #endif
213620006a0bStron #endif
213720006a0bStron 		_settextposition(row, 1);
2138*838f5788Ssimonb 	}
213920006a0bStron #endif
214020006a0bStron }
214120006a0bStron 
214220006a0bStron /*
214320006a0bStron  * Check if the console size has changed and reset internals
214420006a0bStron  * (in lieu of SIGWINCH for WIN32).
214520006a0bStron  */
check_winch(void)2146*838f5788Ssimonb public void check_winch(void)
214720006a0bStron {
214820006a0bStron #if MSDOS_COMPILER==WIN32C
214920006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO scr;
215020006a0bStron 	COORD size;
215120006a0bStron 
215220006a0bStron 	if (con_out == INVALID_HANDLE_VALUE)
215320006a0bStron 		return;
215420006a0bStron 
215520006a0bStron 	flush();
215620006a0bStron 	GetConsoleScreenBufferInfo(con_out, &scr);
215720006a0bStron 	size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
215820006a0bStron 	size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
215920006a0bStron 	if (size.Y != sc_height || size.X != sc_width)
216020006a0bStron 	{
216120006a0bStron 		sc_height = size.Y;
216220006a0bStron 		sc_width = size.X;
216320006a0bStron 		if (!no_init && con_out_ours == con_out)
216420006a0bStron 			SetConsoleScreenBufferSize(con_out, size);
216520006a0bStron 		pos_init();
216620006a0bStron 		wscroll = (sc_height + 1) / 2;
216720006a0bStron 		screen_trashed = 1;
216820006a0bStron 	}
216920006a0bStron #endif
217020006a0bStron }
217120006a0bStron 
217220006a0bStron /*
217320006a0bStron  * Goto a specific line on the screen.
217420006a0bStron  */
goto_line(int sindex)2175*838f5788Ssimonb public void goto_line(int sindex)
217620006a0bStron {
2177*838f5788Ssimonb 	assert_interactive();
217820006a0bStron #if !MSDOS_COMPILER
2179*838f5788Ssimonb 	ltputs(tgoto(sc_move, 0, sindex), 1, putchr);
218020006a0bStron #else
218120006a0bStron 	flush();
2182*838f5788Ssimonb 	_settextposition(sindex+1, 1);
218320006a0bStron #endif
218420006a0bStron }
218520006a0bStron 
218620006a0bStron #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
218720006a0bStron /*
218820006a0bStron  * Create an alternate screen which is all white.
218920006a0bStron  * This screen is used to create a "flash" effect, by displaying it
219020006a0bStron  * briefly and then switching back to the normal screen.
219120006a0bStron  * {{ Yuck!  There must be a better way to get a visual bell. }}
219220006a0bStron  */
create_flash(void)2193*838f5788Ssimonb static void create_flash(void)
219420006a0bStron {
219520006a0bStron #if MSDOS_COMPILER==MSOFTC
219620006a0bStron 	struct videoconfig w;
219720006a0bStron 	char *blanks;
219820006a0bStron 	int row, col;
219920006a0bStron 
220020006a0bStron 	_getvideoconfig(&w);
220120006a0bStron 	videopages = w.numvideopages;
220220006a0bStron 	if (videopages < 2)
220320006a0bStron 	{
220420006a0bStron 		at_enter(AT_STANDOUT);
220520006a0bStron 		at_exit();
220620006a0bStron 	} else
220720006a0bStron 	{
220820006a0bStron 		_setactivepage(1);
220920006a0bStron 		at_enter(AT_STANDOUT);
221020006a0bStron 		blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
221120006a0bStron 		for (col = 0;  col < w.numtextcols;  col++)
221220006a0bStron 			blanks[col] = ' ';
221320006a0bStron 		for (row = w.numtextrows;  row > 0;  row--)
221420006a0bStron 			_outmem(blanks, w.numtextcols);
221520006a0bStron 		_setactivepage(0);
221620006a0bStron 		_setvisualpage(0);
221720006a0bStron 		free(blanks);
221820006a0bStron 		at_exit();
221920006a0bStron 	}
222020006a0bStron #else
222120006a0bStron #if MSDOS_COMPILER==BORLANDC
2222*838f5788Ssimonb 	int n;
222320006a0bStron 
222420006a0bStron 	whitescreen = (unsigned short *)
222520006a0bStron 		malloc(sc_width * sc_height * sizeof(short));
222620006a0bStron 	if (whitescreen == NULL)
222720006a0bStron 		return;
222820006a0bStron 	for (n = 0;  n < sc_width * sc_height;  n++)
222920006a0bStron 		whitescreen[n] = 0x7020;
223020006a0bStron #endif
223120006a0bStron #endif
223220006a0bStron 	flash_created = 1;
223320006a0bStron }
223420006a0bStron #endif /* MSDOS_COMPILER */
223520006a0bStron 
223620006a0bStron /*
223720006a0bStron  * Output the "visual bell", if there is one.
223820006a0bStron  */
vbell(void)2239*838f5788Ssimonb public void vbell(void)
224020006a0bStron {
2241*838f5788Ssimonb 	if (no_vbell)
2242*838f5788Ssimonb 		return;
224320006a0bStron #if !MSDOS_COMPILER
224420006a0bStron 	if (*sc_visual_bell == '\0')
224520006a0bStron 		return;
2246*838f5788Ssimonb 	ltputs(sc_visual_bell, sc_height, putchr);
224720006a0bStron #else
224820006a0bStron #if MSDOS_COMPILER==DJGPPC
224920006a0bStron 	ScreenVisualBell();
225020006a0bStron #else
225120006a0bStron #if MSDOS_COMPILER==MSOFTC
225220006a0bStron 	/*
225320006a0bStron 	 * Create a flash screen on the second video page.
225420006a0bStron 	 * Switch to that page, then switch back.
225520006a0bStron 	 */
225620006a0bStron 	if (!flash_created)
225720006a0bStron 		create_flash();
225820006a0bStron 	if (videopages < 2)
225920006a0bStron 		return;
226020006a0bStron 	_setvisualpage(1);
226120006a0bStron 	delay(100);
226220006a0bStron 	_setvisualpage(0);
226320006a0bStron #else
226420006a0bStron #if MSDOS_COMPILER==BORLANDC
226520006a0bStron 	unsigned short *currscreen;
226620006a0bStron 
226720006a0bStron 	/*
226820006a0bStron 	 * Get a copy of the current screen.
226920006a0bStron 	 * Display the flash screen.
227020006a0bStron 	 * Then restore the old screen.
227120006a0bStron 	 */
227220006a0bStron 	if (!flash_created)
227320006a0bStron 		create_flash();
227420006a0bStron 	if (whitescreen == NULL)
227520006a0bStron 		return;
227620006a0bStron 	currscreen = (unsigned short *)
227720006a0bStron 		malloc(sc_width * sc_height * sizeof(short));
227820006a0bStron 	if (currscreen == NULL) return;
227920006a0bStron 	gettext(1, 1, sc_width, sc_height, currscreen);
228020006a0bStron 	puttext(1, 1, sc_width, sc_height, whitescreen);
228120006a0bStron 	delay(100);
228220006a0bStron 	puttext(1, 1, sc_width, sc_height, currscreen);
228320006a0bStron 	free(currscreen);
228420006a0bStron #else
228520006a0bStron #if MSDOS_COMPILER==WIN32C
228620006a0bStron 	/* paint screen with an inverse color */
228720006a0bStron 	clear();
228820006a0bStron 
228920006a0bStron 	/* leave it displayed for 100 msec. */
229020006a0bStron 	Sleep(100);
229120006a0bStron 
229220006a0bStron 	/* restore with a redraw */
229320006a0bStron 	repaint();
229420006a0bStron #endif
229520006a0bStron #endif
229620006a0bStron #endif
229720006a0bStron #endif
229820006a0bStron #endif
229920006a0bStron }
230020006a0bStron 
230120006a0bStron /*
230220006a0bStron  * Make a noise.
230320006a0bStron  */
beep(void)2304*838f5788Ssimonb static void beep(void)
230520006a0bStron {
230620006a0bStron #if !MSDOS_COMPILER
230720006a0bStron 	putchr(CONTROL('G'));
230820006a0bStron #else
230920006a0bStron #if MSDOS_COMPILER==WIN32C
231020006a0bStron 	MessageBeep(0);
231120006a0bStron #else
231220006a0bStron 	write(1, "\7", 1);
231320006a0bStron #endif
231420006a0bStron #endif
231520006a0bStron }
231620006a0bStron 
231720006a0bStron /*
231820006a0bStron  * Ring the terminal bell.
231920006a0bStron  */
bell(void)2320*838f5788Ssimonb public void bell(void)
232120006a0bStron {
232220006a0bStron 	if (quiet == VERY_QUIET)
232320006a0bStron 		vbell();
232420006a0bStron 	else
232520006a0bStron 		beep();
232620006a0bStron }
232720006a0bStron 
232820006a0bStron /*
232920006a0bStron  * Clear the screen.
233020006a0bStron  */
clear(void)2331*838f5788Ssimonb public void clear(void)
233220006a0bStron {
2333*838f5788Ssimonb 	assert_interactive();
233420006a0bStron #if !MSDOS_COMPILER
2335*838f5788Ssimonb 	ltputs(sc_clear, sc_height, putchr);
233620006a0bStron #else
233720006a0bStron 	flush();
233820006a0bStron #if MSDOS_COMPILER==WIN32C
233920006a0bStron 	win32_clear();
234020006a0bStron #else
234120006a0bStron 	_clearscreen(_GCLEARSCREEN);
234220006a0bStron #endif
234320006a0bStron #endif
234420006a0bStron }
234520006a0bStron 
234620006a0bStron /*
234720006a0bStron  * Clear from the cursor to the end of the cursor's line.
234820006a0bStron  * {{ This must not move the cursor. }}
234920006a0bStron  */
clear_eol(void)2350*838f5788Ssimonb public void clear_eol(void)
235120006a0bStron {
2352*838f5788Ssimonb 	/* assert_interactive();*/
235320006a0bStron #if !MSDOS_COMPILER
2354*838f5788Ssimonb 	ltputs(sc_eol_clear, 1, putchr);
235520006a0bStron #else
235620006a0bStron #if MSDOS_COMPILER==MSOFTC
235720006a0bStron 	short top, left;
235820006a0bStron 	short bot, right;
235920006a0bStron 	struct rccoord tpos;
236020006a0bStron 
236120006a0bStron 	flush();
236220006a0bStron 	/*
236320006a0bStron 	 * Save current state.
236420006a0bStron 	 */
236520006a0bStron 	tpos = _gettextposition();
236620006a0bStron 	_gettextwindow(&top, &left, &bot, &right);
236720006a0bStron 	/*
236820006a0bStron 	 * Set a temporary window to the current line,
236920006a0bStron 	 * from the cursor's position to the right edge of the screen.
237020006a0bStron 	 * Then clear that window.
237120006a0bStron 	 */
237220006a0bStron 	_settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
237320006a0bStron 	_clearscreen(_GWINDOW);
237420006a0bStron 	/*
237520006a0bStron 	 * Restore state.
237620006a0bStron 	 */
237720006a0bStron 	_settextwindow(top, left, bot, right);
237820006a0bStron 	_settextposition(tpos.row, tpos.col);
237920006a0bStron #else
238020006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
238120006a0bStron 	flush();
238220006a0bStron 	clreol();
238320006a0bStron #else
238420006a0bStron #if MSDOS_COMPILER==WIN32C
238520006a0bStron 	DWORD           nchars;
238620006a0bStron 	COORD           cpos;
238720006a0bStron 	CONSOLE_SCREEN_BUFFER_INFO scr;
238820006a0bStron 
238920006a0bStron 	flush();
239020006a0bStron 	memset(&scr, 0, sizeof(scr));
239120006a0bStron 	GetConsoleScreenBufferInfo(con_out, &scr);
239220006a0bStron 	cpos.X = scr.dwCursorPosition.X;
239320006a0bStron 	cpos.Y = scr.dwCursorPosition.Y;
239420006a0bStron 	curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
239520006a0bStron 	FillConsoleOutputAttribute(con_out, curr_attr,
239620006a0bStron 		scr.dwSize.X - cpos.X, cpos, &nchars);
239720006a0bStron 	FillConsoleOutputCharacter(con_out, ' ',
239820006a0bStron 		scr.dwSize.X - cpos.X, cpos, &nchars);
239920006a0bStron #endif
240020006a0bStron #endif
240120006a0bStron #endif
240220006a0bStron #endif
240320006a0bStron }
240420006a0bStron 
240520006a0bStron /*
240620006a0bStron  * Clear the current line.
240720006a0bStron  * Clear the screen if there's off-screen memory below the display.
240820006a0bStron  */
clear_eol_bot(void)2409*838f5788Ssimonb static void clear_eol_bot(void)
241020006a0bStron {
2411*838f5788Ssimonb 	assert_interactive();
241220006a0bStron #if MSDOS_COMPILER
241320006a0bStron 	clear_eol();
241420006a0bStron #else
241520006a0bStron 	if (below_mem)
2416*838f5788Ssimonb 		ltputs(sc_eos_clear, 1, putchr);
241720006a0bStron 	else
2418*838f5788Ssimonb 		ltputs(sc_eol_clear, 1, putchr);
241920006a0bStron #endif
242020006a0bStron }
242120006a0bStron 
242220006a0bStron /*
242320006a0bStron  * Clear the bottom line of the display.
242420006a0bStron  * Leave the cursor at the beginning of the bottom line.
242520006a0bStron  */
clear_bot(void)2426*838f5788Ssimonb public void clear_bot(void)
242720006a0bStron {
242820006a0bStron 	/*
242920006a0bStron 	 * If we're in a non-normal attribute mode, temporarily exit
243020006a0bStron 	 * the mode while we do the clear.  Some terminals fill the
243120006a0bStron 	 * cleared area with the current attribute.
243220006a0bStron 	 */
243320006a0bStron 	if (oldbot)
243420006a0bStron 		lower_left();
243520006a0bStron 	else
243620006a0bStron 		line_left();
243720006a0bStron 
243820006a0bStron 	if (attrmode == AT_NORMAL)
243920006a0bStron 		clear_eol_bot();
244020006a0bStron 	else
244120006a0bStron 	{
244220006a0bStron 		int saved_attrmode = attrmode;
244320006a0bStron 
244420006a0bStron 		at_exit();
244520006a0bStron 		clear_eol_bot();
244620006a0bStron 		at_enter(saved_attrmode);
244720006a0bStron 	}
244820006a0bStron }
244920006a0bStron 
2450*838f5788Ssimonb /*
2451*838f5788Ssimonb  * Color string may be "x[y]" where x and y are 4-bit color chars,
2452*838f5788Ssimonb  * or "N[.M]" where N and M are decimal integers>
2453*838f5788Ssimonb  * Any of x,y,N,M may also be "-" to mean "unchanged".
2454*838f5788Ssimonb  */
2455*838f5788Ssimonb 
2456*838f5788Ssimonb /*
2457*838f5788Ssimonb  * Parse a 4-bit color char.
2458*838f5788Ssimonb  */
parse_color4(char ch)2459*838f5788Ssimonb static int parse_color4(char ch)
246020006a0bStron {
2461*838f5788Ssimonb 	switch (ch)
2462*838f5788Ssimonb 	{
2463*838f5788Ssimonb 	case 'k': return 0;
2464*838f5788Ssimonb 	case 'r': return CV_RED;
2465*838f5788Ssimonb 	case 'g': return CV_GREEN;
2466*838f5788Ssimonb 	case 'y': return CV_RED|CV_GREEN;
2467*838f5788Ssimonb 	case 'b': return CV_BLUE;
2468*838f5788Ssimonb 	case 'm': return CV_RED|CV_BLUE;
2469*838f5788Ssimonb 	case 'c': return CV_GREEN|CV_BLUE;
2470*838f5788Ssimonb 	case 'w': return CV_RED|CV_GREEN|CV_BLUE;
2471*838f5788Ssimonb 	case 'K': return 0|CV_BRIGHT;
2472*838f5788Ssimonb 	case 'R': return CV_RED|CV_BRIGHT;
2473*838f5788Ssimonb 	case 'G': return CV_GREEN|CV_BRIGHT;
2474*838f5788Ssimonb 	case 'Y': return CV_RED|CV_GREEN|CV_BRIGHT;
2475*838f5788Ssimonb 	case 'B': return CV_BLUE|CV_BRIGHT;
2476*838f5788Ssimonb 	case 'M': return CV_RED|CV_BLUE|CV_BRIGHT;
2477*838f5788Ssimonb 	case 'C': return CV_GREEN|CV_BLUE|CV_BRIGHT;
2478*838f5788Ssimonb 	case 'W': return CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT;
2479*838f5788Ssimonb 	case '-': return CV_NOCHANGE;
2480*838f5788Ssimonb 	default:  return CV_ERROR;
2481*838f5788Ssimonb 	}
2482*838f5788Ssimonb }
2483*838f5788Ssimonb 
2484*838f5788Ssimonb /*
2485*838f5788Ssimonb  * Parse a color as a decimal integer.
2486*838f5788Ssimonb  */
parse_color6(char ** ps)2487*838f5788Ssimonb static int parse_color6(char **ps)
2488*838f5788Ssimonb {
2489*838f5788Ssimonb 	if (**ps == '-')
2490*838f5788Ssimonb 	{
2491*838f5788Ssimonb 		(*ps)++;
2492*838f5788Ssimonb 		return CV_NOCHANGE;
2493*838f5788Ssimonb 	} else
2494*838f5788Ssimonb 	{
2495*838f5788Ssimonb 		char *ops = *ps;
2496*838f5788Ssimonb 		int color = lstrtoi(ops, ps, 10);
2497*838f5788Ssimonb 		if (color < 0 || *ps == ops)
2498*838f5788Ssimonb 			return CV_ERROR;
2499*838f5788Ssimonb 		return color;
2500*838f5788Ssimonb 	}
2501*838f5788Ssimonb }
2502*838f5788Ssimonb 
2503*838f5788Ssimonb /*
2504*838f5788Ssimonb  * Parse a color pair and return the foreground/background values.
2505*838f5788Ssimonb  * Return type of color specifier:
2506*838f5788Ssimonb  *  CV_4BIT: fg/bg values are OR of CV_{RGB} bits.
2507*838f5788Ssimonb  *  CV_6BIT: fg/bg values are integers entered by user.
2508*838f5788Ssimonb  */
parse_color(char * str,int * p_fg,int * p_bg)2509*838f5788Ssimonb public COLOR_TYPE parse_color(char *str, int *p_fg, int *p_bg)
2510*838f5788Ssimonb {
2511*838f5788Ssimonb 	int fg;
2512*838f5788Ssimonb 	int bg;
2513*838f5788Ssimonb 	COLOR_TYPE type = CT_NULL;
2514*838f5788Ssimonb 
2515*838f5788Ssimonb 	if (str == NULL || *str == '\0')
2516*838f5788Ssimonb 		return CT_NULL;
2517*838f5788Ssimonb 	if (*str == '+')
2518*838f5788Ssimonb 		str++; /* ignore leading + */
2519*838f5788Ssimonb 
2520*838f5788Ssimonb 	fg = parse_color4(str[0]);
2521*838f5788Ssimonb 	bg = parse_color4((strlen(str) < 2) ? '-' : str[1]);
2522*838f5788Ssimonb 	if (fg != CV_ERROR && bg != CV_ERROR)
2523*838f5788Ssimonb 		type = CT_4BIT;
2524*838f5788Ssimonb 	else
2525*838f5788Ssimonb 	{
2526*838f5788Ssimonb 		fg = parse_color6(&str);
2527*838f5788Ssimonb 		bg = (fg != CV_ERROR && *str++ == '.') ? parse_color6(&str) : CV_NOCHANGE;
2528*838f5788Ssimonb 		if (fg != CV_ERROR && bg != CV_ERROR)
2529*838f5788Ssimonb 			type = CT_6BIT;
2530*838f5788Ssimonb 	}
2531*838f5788Ssimonb 	if (p_fg != NULL) *p_fg = fg;
2532*838f5788Ssimonb 	if (p_bg != NULL) *p_bg = bg;
2533*838f5788Ssimonb 	return type;
2534*838f5788Ssimonb }
253520006a0bStron 
253620006a0bStron #if !MSDOS_COMPILER
2537*838f5788Ssimonb 
sgr_color(int color)2538*838f5788Ssimonb static int sgr_color(int color)
2539*838f5788Ssimonb {
2540*838f5788Ssimonb 	switch (color)
2541*838f5788Ssimonb 	{
2542*838f5788Ssimonb 	case 0:                                    return 30;
2543*838f5788Ssimonb 	case CV_RED:                               return 31;
2544*838f5788Ssimonb 	case CV_GREEN:                             return 32;
2545*838f5788Ssimonb 	case CV_RED|CV_GREEN:                      return 33;
2546*838f5788Ssimonb 	case CV_BLUE:                              return 34;
2547*838f5788Ssimonb 	case CV_RED|CV_BLUE:                       return 35;
2548*838f5788Ssimonb 	case CV_GREEN|CV_BLUE:                     return 36;
2549*838f5788Ssimonb 	case CV_RED|CV_GREEN|CV_BLUE:              return 37;
2550*838f5788Ssimonb 
2551*838f5788Ssimonb 	case CV_BRIGHT:                            return 90;
2552*838f5788Ssimonb 	case CV_RED|CV_BRIGHT:                     return 91;
2553*838f5788Ssimonb 	case CV_GREEN|CV_BRIGHT:                   return 92;
2554*838f5788Ssimonb 	case CV_RED|CV_GREEN|CV_BRIGHT:            return 93;
2555*838f5788Ssimonb 	case CV_BLUE|CV_BRIGHT:                    return 94;
2556*838f5788Ssimonb 	case CV_RED|CV_BLUE|CV_BRIGHT:             return 95;
2557*838f5788Ssimonb 	case CV_GREEN|CV_BLUE|CV_BRIGHT:           return 96;
2558*838f5788Ssimonb 	case CV_RED|CV_GREEN|CV_BLUE|CV_BRIGHT:    return 97;
2559*838f5788Ssimonb 
2560*838f5788Ssimonb 	default: return color;
2561*838f5788Ssimonb 	}
2562*838f5788Ssimonb }
2563*838f5788Ssimonb 
tput_fmt(char * fmt,int color,int (* f_putc)(int))2564*838f5788Ssimonb static void tput_fmt(char *fmt, int color, int (*f_putc)(int))
2565*838f5788Ssimonb {
2566*838f5788Ssimonb 	char buf[INT_STRLEN_BOUND(int)+16];
2567*838f5788Ssimonb 	if (color == attrcolor)
2568*838f5788Ssimonb 		return;
2569*838f5788Ssimonb 	SNPRINTF1(buf, sizeof(buf), fmt, color);
2570*838f5788Ssimonb 	ltputs(buf, 1, f_putc);
2571*838f5788Ssimonb 	attrcolor = color;
2572*838f5788Ssimonb }
2573*838f5788Ssimonb 
tput_color(char * str,int (* f_putc)(int))2574*838f5788Ssimonb static void tput_color(char *str, int (*f_putc)(int))
2575*838f5788Ssimonb {
2576*838f5788Ssimonb 	int fg;
2577*838f5788Ssimonb 	int bg;
2578*838f5788Ssimonb 
2579*838f5788Ssimonb 	if (str != NULL && strcmp(str, "*") == 0)
2580*838f5788Ssimonb 	{
2581*838f5788Ssimonb 		/* Special case: reset to normal */
2582*838f5788Ssimonb 		tput_fmt(ESCS"[m", -1, f_putc);
2583*838f5788Ssimonb 		return;
2584*838f5788Ssimonb 	}
2585*838f5788Ssimonb 	switch (parse_color(str, &fg, &bg))
2586*838f5788Ssimonb 	{
2587*838f5788Ssimonb 	case CT_4BIT:
2588*838f5788Ssimonb 		if (fg >= 0)
2589*838f5788Ssimonb 			tput_fmt(ESCS"[%dm", sgr_color(fg), f_putc);
2590*838f5788Ssimonb 		if (bg >= 0)
2591*838f5788Ssimonb 			tput_fmt(ESCS"[%dm", sgr_color(bg)+10, f_putc);
2592*838f5788Ssimonb 		break;
2593*838f5788Ssimonb 	case CT_6BIT:
2594*838f5788Ssimonb 		if (fg >= 0)
2595*838f5788Ssimonb 			tput_fmt(ESCS"[38;5;%dm", fg, f_putc);
2596*838f5788Ssimonb 		if (bg >= 0)
2597*838f5788Ssimonb 			tput_fmt(ESCS"[48;5;%dm", bg, f_putc);
2598*838f5788Ssimonb 		break;
2599*838f5788Ssimonb 	default:
2600*838f5788Ssimonb 		break;
2601*838f5788Ssimonb 	}
2602*838f5788Ssimonb }
2603*838f5788Ssimonb 
tput_inmode(char * mode_str,int attr,int attr_bit,int (* f_putc)(int))2604*838f5788Ssimonb static void tput_inmode(char *mode_str, int attr, int attr_bit, int (*f_putc)(int))
2605*838f5788Ssimonb {
2606*838f5788Ssimonb 	char *color_str;
2607*838f5788Ssimonb 	if ((attr & attr_bit) == 0)
2608*838f5788Ssimonb 		return;
2609*838f5788Ssimonb 	color_str = get_color_map(attr_bit);
2610*838f5788Ssimonb 	if (color_str == NULL || *color_str == '\0' || *color_str == '+')
2611*838f5788Ssimonb 	{
2612*838f5788Ssimonb 		ltputs(mode_str, 1, f_putc);
2613*838f5788Ssimonb 		if (color_str == NULL || *color_str++ != '+')
2614*838f5788Ssimonb 			return;
2615*838f5788Ssimonb 	}
2616*838f5788Ssimonb 	/* Color overrides mode string */
2617*838f5788Ssimonb 	tput_color(color_str, f_putc);
2618*838f5788Ssimonb }
2619*838f5788Ssimonb 
tput_outmode(char * mode_str,int attr_bit,int (* f_putc)(int))2620*838f5788Ssimonb static void tput_outmode(char *mode_str, int attr_bit, int (*f_putc)(int))
2621*838f5788Ssimonb {
2622*838f5788Ssimonb 	if ((attrmode & attr_bit) == 0)
2623*838f5788Ssimonb 		return;
2624*838f5788Ssimonb 	ltputs(mode_str, 1, f_putc);
2625*838f5788Ssimonb }
2626*838f5788Ssimonb 
2627*838f5788Ssimonb #else /* MSDOS_COMPILER */
2628*838f5788Ssimonb 
2629*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
WIN32put_fmt(char * fmt,int color)2630*838f5788Ssimonb static int WIN32put_fmt(char *fmt, int color)
2631*838f5788Ssimonb {
2632*838f5788Ssimonb 	char buf[INT_STRLEN_BOUND(int)+16];
2633*838f5788Ssimonb 	int len = SNPRINTF1(buf, sizeof(buf), fmt, color);
2634*838f5788Ssimonb 	WIN32textout(buf, len);
2635*838f5788Ssimonb 	return TRUE;
2636*838f5788Ssimonb }
2637*838f5788Ssimonb #endif
2638*838f5788Ssimonb 
win_set_color(int attr)2639*838f5788Ssimonb static int win_set_color(int attr)
2640*838f5788Ssimonb {
2641*838f5788Ssimonb 	int fg;
2642*838f5788Ssimonb 	int bg;
2643*838f5788Ssimonb 	int out = FALSE;
2644*838f5788Ssimonb 	char *str = get_color_map(attr);
2645*838f5788Ssimonb 	if (str == NULL || str[0] == '\0')
2646*838f5788Ssimonb 		return FALSE;
2647*838f5788Ssimonb 	switch (parse_color(str, &fg, &bg))
2648*838f5788Ssimonb 	{
2649*838f5788Ssimonb 	case CT_4BIT:
2650*838f5788Ssimonb 		if (fg >= 0 && bg >= 0)
2651*838f5788Ssimonb 		{
2652*838f5788Ssimonb 			SETCOLORS(fg, bg);
2653*838f5788Ssimonb 			out = TRUE;
2654*838f5788Ssimonb 		} else if (fg >= 0)
2655*838f5788Ssimonb 		{
2656*838f5788Ssimonb 			SET_FG_COLOR(fg);
2657*838f5788Ssimonb 			out = TRUE;
2658*838f5788Ssimonb 		} else if (bg >= 0)
2659*838f5788Ssimonb 		{
2660*838f5788Ssimonb 			SET_BG_COLOR(bg);
2661*838f5788Ssimonb 			out = TRUE;
2662*838f5788Ssimonb 		}
2663*838f5788Ssimonb 		break;
2664*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
2665*838f5788Ssimonb 	case CT_6BIT:
2666*838f5788Ssimonb 		if (vt_enabled)
2667*838f5788Ssimonb 		{
2668*838f5788Ssimonb 			if (fg > 0)
2669*838f5788Ssimonb 				out = WIN32put_fmt(ESCS"[38;5;%dm", fg);
2670*838f5788Ssimonb 			if (bg > 0)
2671*838f5788Ssimonb 				out = WIN32put_fmt(ESCS"[48;5;%dm", bg);
2672*838f5788Ssimonb 		}
2673*838f5788Ssimonb 		break;
2674*838f5788Ssimonb #endif
2675*838f5788Ssimonb 	default:
2676*838f5788Ssimonb 		break;
2677*838f5788Ssimonb 	}
2678*838f5788Ssimonb 	return out;
2679*838f5788Ssimonb }
2680*838f5788Ssimonb 
2681*838f5788Ssimonb #endif /* MSDOS_COMPILER */
2682*838f5788Ssimonb 
at_enter(int attr)2683*838f5788Ssimonb public void at_enter(int attr)
2684*838f5788Ssimonb {
2685*838f5788Ssimonb 	attr = apply_at_specials(attr);
2686*838f5788Ssimonb #if !MSDOS_COMPILER
268720006a0bStron 	/* The one with the most priority is last.  */
2688*838f5788Ssimonb 	tput_inmode(sc_u_in, attr, AT_UNDERLINE, putchr);
2689*838f5788Ssimonb 	tput_inmode(sc_b_in, attr, AT_BOLD, putchr);
2690*838f5788Ssimonb 	tput_inmode(sc_bl_in, attr, AT_BLINK, putchr);
2691*838f5788Ssimonb 	/* Don't use standout and color at the same time. */
2692*838f5788Ssimonb 	if (use_color && (attr & AT_COLOR))
2693*838f5788Ssimonb 		tput_color(get_color_map(attr), putchr);
2694*838f5788Ssimonb 	else
2695*838f5788Ssimonb 		tput_inmode(sc_s_in, attr, AT_STANDOUT, putchr);
269620006a0bStron #else
269720006a0bStron 	flush();
269820006a0bStron 	/* The one with the most priority is first.  */
2699*838f5788Ssimonb 	if ((attr & AT_COLOR) && use_color)
2700*838f5788Ssimonb 	{
2701*838f5788Ssimonb 		win_set_color(attr);
2702*838f5788Ssimonb 	} else if (attr & AT_STANDOUT)
270320006a0bStron 	{
270420006a0bStron 		SETCOLORS(so_fg_color, so_bg_color);
270520006a0bStron 	} else if (attr & AT_BLINK)
270620006a0bStron 	{
270720006a0bStron 		SETCOLORS(bl_fg_color, bl_bg_color);
2708*838f5788Ssimonb 	} else if (attr & AT_BOLD)
270920006a0bStron 	{
271020006a0bStron 		SETCOLORS(bo_fg_color, bo_bg_color);
2711*838f5788Ssimonb 	} else if (attr & AT_UNDERLINE)
271220006a0bStron 	{
271320006a0bStron 		SETCOLORS(ul_fg_color, ul_bg_color);
271420006a0bStron 	}
271520006a0bStron #endif
271620006a0bStron 	attrmode = attr;
271720006a0bStron }
271820006a0bStron 
at_exit(void)2719*838f5788Ssimonb public void at_exit(void)
272020006a0bStron {
272120006a0bStron #if !MSDOS_COMPILER
272220006a0bStron 	/* Undo things in the reverse order we did them.  */
2723*838f5788Ssimonb 	tput_color("*", putchr);
2724*838f5788Ssimonb 	tput_outmode(sc_s_out, AT_STANDOUT, putchr);
2725*838f5788Ssimonb 	tput_outmode(sc_bl_out, AT_BLINK, putchr);
2726*838f5788Ssimonb 	tput_outmode(sc_b_out, AT_BOLD, putchr);
2727*838f5788Ssimonb 	tput_outmode(sc_u_out, AT_UNDERLINE, putchr);
272820006a0bStron #else
272920006a0bStron 	flush();
273020006a0bStron 	SETCOLORS(nm_fg_color, nm_bg_color);
273120006a0bStron #endif
273220006a0bStron 	attrmode = AT_NORMAL;
273320006a0bStron }
273420006a0bStron 
at_switch(int attr)2735*838f5788Ssimonb public void at_switch(int attr)
273620006a0bStron {
273720006a0bStron 	int new_attrmode = apply_at_specials(attr);
273820006a0bStron 	int ignore_modes = AT_ANSI;
273920006a0bStron 
274020006a0bStron 	if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
274120006a0bStron 	{
274220006a0bStron 		at_exit();
274320006a0bStron 		at_enter(attr);
274420006a0bStron 	}
274520006a0bStron }
274620006a0bStron 
is_at_equiv(int attr1,int attr2)2747*838f5788Ssimonb public int is_at_equiv(int attr1, int attr2)
274820006a0bStron {
274920006a0bStron 	attr1 = apply_at_specials(attr1);
275020006a0bStron 	attr2 = apply_at_specials(attr2);
275120006a0bStron 
275220006a0bStron 	return (attr1 == attr2);
275320006a0bStron }
275420006a0bStron 
apply_at_specials(int attr)2755*838f5788Ssimonb public int apply_at_specials(int attr)
275620006a0bStron {
275720006a0bStron 	if (attr & AT_BINARY)
275820006a0bStron 		attr |= binattr;
275920006a0bStron 	if (attr & AT_HILITE)
276020006a0bStron 		attr |= AT_STANDOUT;
276120006a0bStron 	attr &= ~(AT_BINARY|AT_HILITE);
276220006a0bStron 
276320006a0bStron 	return attr;
276420006a0bStron }
276520006a0bStron 
276620006a0bStron /*
276720006a0bStron  * Output a plain backspace, without erasing the previous char.
276820006a0bStron  */
putbs(void)2769*838f5788Ssimonb public void putbs(void)
2770*838f5788Ssimonb {
2771*838f5788Ssimonb 	if (termcap_debug)
2772*838f5788Ssimonb 		putstr("<bs>");
2773*838f5788Ssimonb 	else
277420006a0bStron 	{
277520006a0bStron #if !MSDOS_COMPILER
2776*838f5788Ssimonb 	ltputs(sc_backspace, 1, putchr);
277720006a0bStron #else
277820006a0bStron 	int row, col;
277920006a0bStron 
278020006a0bStron 	flush();
278120006a0bStron 	{
278220006a0bStron #if MSDOS_COMPILER==MSOFTC
278320006a0bStron 		struct rccoord tpos;
278420006a0bStron 		tpos = _gettextposition();
278520006a0bStron 		row = tpos.row;
278620006a0bStron 		col = tpos.col;
278720006a0bStron #else
278820006a0bStron #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
278920006a0bStron 		row = wherey();
279020006a0bStron 		col = wherex();
279120006a0bStron #else
279220006a0bStron #if MSDOS_COMPILER==WIN32C
279320006a0bStron 		CONSOLE_SCREEN_BUFFER_INFO scr;
279420006a0bStron 		GetConsoleScreenBufferInfo(con_out, &scr);
279520006a0bStron 		row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
279620006a0bStron 		col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
279720006a0bStron #endif
279820006a0bStron #endif
279920006a0bStron #endif
280020006a0bStron 	}
280120006a0bStron 	if (col <= 1)
280220006a0bStron 		return;
280320006a0bStron 	_settextposition(row, col-1);
280420006a0bStron #endif /* MSDOS_COMPILER */
280520006a0bStron 	}
2806*838f5788Ssimonb }
280720006a0bStron 
280820006a0bStron #if MSDOS_COMPILER==WIN32C
280920006a0bStron /*
281020006a0bStron  * Determine whether an input character is waiting to be read.
281120006a0bStron  */
win32_kbhit(void)2812*838f5788Ssimonb public int win32_kbhit(void)
281320006a0bStron {
281420006a0bStron 	INPUT_RECORD ip;
281520006a0bStron 	DWORD read;
281620006a0bStron 
2817*838f5788Ssimonb 	if (keyCount > 0 || win_unget_pending)
281820006a0bStron 		return (TRUE);
281920006a0bStron 
282020006a0bStron 	currentKey.ascii = 0;
282120006a0bStron 	currentKey.scan = 0;
282220006a0bStron 
2823*838f5788Ssimonb 	if (x11mouseCount > 0)
2824*838f5788Ssimonb 	{
2825*838f5788Ssimonb 		currentKey.ascii = x11mousebuf[x11mousePos++];
2826*838f5788Ssimonb 		--x11mouseCount;
2827*838f5788Ssimonb 		keyCount = 1;
2828*838f5788Ssimonb 		return (TRUE);
2829*838f5788Ssimonb 	}
2830*838f5788Ssimonb 
283120006a0bStron 	/*
283220006a0bStron 	 * Wait for a real key-down event, but
283320006a0bStron 	 * ignore SHIFT and CONTROL key events.
283420006a0bStron 	 */
283520006a0bStron 	do
283620006a0bStron 	{
2837*838f5788Ssimonb 		PeekConsoleInputW(tty, &ip, 1, &read);
283820006a0bStron 		if (read == 0)
283920006a0bStron 			return (FALSE);
2840*838f5788Ssimonb 		ReadConsoleInputW(tty, &ip, 1, &read);
2841*838f5788Ssimonb 		/* generate an X11 mouse sequence from the mouse event */
2842*838f5788Ssimonb 		if (mousecap && ip.EventType == MOUSE_EVENT &&
2843*838f5788Ssimonb 		    ip.Event.MouseEvent.dwEventFlags != MOUSE_MOVED)
2844*838f5788Ssimonb 		{
2845*838f5788Ssimonb 			x11mousebuf[3] = X11MOUSE_OFFSET + ip.Event.MouseEvent.dwMousePosition.X + 1;
2846*838f5788Ssimonb 			x11mousebuf[4] = X11MOUSE_OFFSET + ip.Event.MouseEvent.dwMousePosition.Y + 1;
2847*838f5788Ssimonb 			switch (ip.Event.MouseEvent.dwEventFlags)
2848*838f5788Ssimonb 			{
2849*838f5788Ssimonb 			case 0: /* press or release */
2850*838f5788Ssimonb 				if (ip.Event.MouseEvent.dwButtonState == 0)
2851*838f5788Ssimonb 					x11mousebuf[2] = X11MOUSE_OFFSET + X11MOUSE_BUTTON_REL;
2852*838f5788Ssimonb 				else if (ip.Event.MouseEvent.dwButtonState & (FROM_LEFT_3RD_BUTTON_PRESSED | FROM_LEFT_4TH_BUTTON_PRESSED))
2853*838f5788Ssimonb 					continue;
2854*838f5788Ssimonb 				else
2855*838f5788Ssimonb 					x11mousebuf[2] = X11MOUSE_OFFSET + X11MOUSE_BUTTON1 + ((int)ip.Event.MouseEvent.dwButtonState << 1);
2856*838f5788Ssimonb 				break;
2857*838f5788Ssimonb 			case MOUSE_WHEELED:
2858*838f5788Ssimonb 				x11mousebuf[2] = X11MOUSE_OFFSET + (((int)ip.Event.MouseEvent.dwButtonState < 0) ? X11MOUSE_WHEEL_DOWN : X11MOUSE_WHEEL_UP);
2859*838f5788Ssimonb 				break;
2860*838f5788Ssimonb 			default:
2861*838f5788Ssimonb 				continue;
2862*838f5788Ssimonb 			}
2863*838f5788Ssimonb 			x11mousePos = 0;
2864*838f5788Ssimonb 			x11mouseCount = 5;
2865*838f5788Ssimonb 			currentKey.ascii = ESC;
2866*838f5788Ssimonb 			keyCount = 1;
2867*838f5788Ssimonb 			return (TRUE);
2868*838f5788Ssimonb 		}
286920006a0bStron 	} while (ip.EventType != KEY_EVENT ||
287020006a0bStron 		ip.Event.KeyEvent.bKeyDown != TRUE ||
2871*838f5788Ssimonb 		(ip.Event.KeyEvent.wVirtualScanCode == 0 && ip.Event.KeyEvent.uChar.UnicodeChar == 0) ||
2872*838f5788Ssimonb 		((ip.Event.KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED)) == (RIGHT_ALT_PRESSED|LEFT_CTRL_PRESSED) && ip.Event.KeyEvent.uChar.UnicodeChar == 0) ||
2873*838f5788Ssimonb 		ip.Event.KeyEvent.wVirtualKeyCode == VK_KANJI ||
287420006a0bStron 		ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
287520006a0bStron 		ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
287620006a0bStron 		ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
287720006a0bStron 
2878*838f5788Ssimonb 	currentKey.unicode = ip.Event.KeyEvent.uChar.UnicodeChar;
287920006a0bStron 	currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
288020006a0bStron 	currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
288120006a0bStron 	keyCount = ip.Event.KeyEvent.wRepeatCount;
288220006a0bStron 
288320006a0bStron 	if (ip.Event.KeyEvent.dwControlKeyState &
288420006a0bStron 		(LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
288520006a0bStron 	{
288620006a0bStron 		switch (currentKey.scan)
288720006a0bStron 		{
288820006a0bStron 		case PCK_ALT_E:     /* letter 'E' */
288920006a0bStron 			currentKey.ascii = 0;
289020006a0bStron 			break;
289120006a0bStron 		}
289220006a0bStron 	} else if (ip.Event.KeyEvent.dwControlKeyState &
289320006a0bStron 		(LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
289420006a0bStron 	{
289520006a0bStron 		switch (currentKey.scan)
289620006a0bStron 		{
289720006a0bStron 		case PCK_RIGHT: /* right arrow */
289820006a0bStron 			currentKey.scan = PCK_CTL_RIGHT;
289920006a0bStron 			break;
290020006a0bStron 		case PCK_LEFT: /* left arrow */
290120006a0bStron 			currentKey.scan = PCK_CTL_LEFT;
290220006a0bStron 			break;
290320006a0bStron 		case PCK_DELETE: /* delete */
290420006a0bStron 			currentKey.scan = PCK_CTL_DELETE;
290520006a0bStron 			break;
290620006a0bStron 		}
2907*838f5788Ssimonb 	} else if (ip.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
2908*838f5788Ssimonb 	{
2909*838f5788Ssimonb 		switch (currentKey.scan)
2910*838f5788Ssimonb 		{
2911*838f5788Ssimonb 		case PCK_SHIFT_TAB: /* tab */
2912*838f5788Ssimonb 			currentKey.ascii = 0;
2913*838f5788Ssimonb 			break;
291420006a0bStron 		}
2915*838f5788Ssimonb 	}
2916*838f5788Ssimonb 
291720006a0bStron 	return (TRUE);
291820006a0bStron }
291920006a0bStron 
292020006a0bStron /*
292120006a0bStron  * Read a character from the keyboard.
2922*838f5788Ssimonb  *
2923*838f5788Ssimonb  * Known issues:
2924*838f5788Ssimonb  * - WIN32getch API should be int like libc (with unsigned char values or -1).
2925*838f5788Ssimonb  * - The unicode code below can return 0 - incorrectly indicating scan code.
2926*838f5788Ssimonb  * - UTF16-LE surrogate pairs don't work (and return 0).
2927*838f5788Ssimonb  * - If win32_kbhit returns true then WIN32getch should never block, but it
2928*838f5788Ssimonb  *   will block till the next keypress if it's numlock/capslock scan code.
292920006a0bStron  */
WIN32getch(void)2930*838f5788Ssimonb public char WIN32getch(void)
293120006a0bStron {
2932*838f5788Ssimonb 	char ascii;
2933*838f5788Ssimonb 	static unsigned char utf8[UTF8_MAX_LENGTH];
2934*838f5788Ssimonb 	static int utf8_size = 0;
2935*838f5788Ssimonb 	static int utf8_next_byte = 0;
2936*838f5788Ssimonb 
2937*838f5788Ssimonb 	if (win_unget_pending)
2938*838f5788Ssimonb 	{
2939*838f5788Ssimonb 		win_unget_pending = FALSE;
2940*838f5788Ssimonb 		return (char) win_unget_data;
2941*838f5788Ssimonb 	}
2942*838f5788Ssimonb 
2943*838f5788Ssimonb 	// Return the rest of multibyte character from the prior call
2944*838f5788Ssimonb 	if (utf8_next_byte < utf8_size)
2945*838f5788Ssimonb 	{
2946*838f5788Ssimonb 		ascii = utf8[utf8_next_byte++];
2947*838f5788Ssimonb 		return ascii;
2948*838f5788Ssimonb 	}
2949*838f5788Ssimonb 	utf8_size = 0;
295020006a0bStron 
295120006a0bStron 	if (pending_scancode)
295220006a0bStron 	{
295320006a0bStron 		pending_scancode = 0;
295420006a0bStron 		return ((char)(currentKey.scan & 0x00FF));
295520006a0bStron 	}
295620006a0bStron 
2957*838f5788Ssimonb 	do {
2958*838f5788Ssimonb 		while (!win32_kbhit())
295920006a0bStron 		{
296020006a0bStron 			Sleep(20);
296120006a0bStron 			if (ABORT_SIGS())
296220006a0bStron 				return ('\003');
296320006a0bStron 		}
296420006a0bStron 		keyCount--;
2965*838f5788Ssimonb 		// If multibyte character, return its first byte
2966*838f5788Ssimonb 		if (currentKey.unicode > 0x7f)
2967*838f5788Ssimonb 		{
2968*838f5788Ssimonb 			utf8_size = WideCharToMultiByte(CP_UTF8, 0, &currentKey.unicode, 1, (LPSTR) &utf8, sizeof(utf8), NULL, NULL);
2969*838f5788Ssimonb 			if (utf8_size == 0)
2970*838f5788Ssimonb 				return '\0';
2971*838f5788Ssimonb 			ascii = utf8[0];
2972*838f5788Ssimonb 			utf8_next_byte = 1;
2973*838f5788Ssimonb 		} else
297420006a0bStron 			ascii = currentKey.ascii;
297520006a0bStron 		/*
297620006a0bStron 		 * On PC's, the extended keys return a 2 byte sequence beginning
297720006a0bStron 		 * with '00', so if the ascii code is 00, the next byte will be
297820006a0bStron 		 * the lsb of the scan code.
297920006a0bStron 		 */
298020006a0bStron 		pending_scancode = (ascii == 0x00);
2981*838f5788Ssimonb 	} while (pending_scancode &&
2982*838f5788Ssimonb 		(currentKey.scan == PCK_CAPS_LOCK || currentKey.scan == PCK_NUM_LOCK));
2983*838f5788Ssimonb 
2984*838f5788Ssimonb 	return ascii;
2985*838f5788Ssimonb }
2986*838f5788Ssimonb 
2987*838f5788Ssimonb /*
2988*838f5788Ssimonb  * Make the next call to WIN32getch return ch without changing the queue state.
2989*838f5788Ssimonb  */
WIN32ungetch(int ch)2990*838f5788Ssimonb public void WIN32ungetch(int ch)
2991*838f5788Ssimonb {
2992*838f5788Ssimonb 	win_unget_pending = TRUE;
2993*838f5788Ssimonb 	win_unget_data = ch;
299420006a0bStron }
299520006a0bStron #endif
299620006a0bStron 
299720006a0bStron #if MSDOS_COMPILER
299820006a0bStron /*
299920006a0bStron  */
WIN32setcolors(int fg,int bg)3000*838f5788Ssimonb public void WIN32setcolors(int fg, int bg)
300120006a0bStron {
300220006a0bStron 	SETCOLORS(fg, bg);
300320006a0bStron }
300420006a0bStron 
300520006a0bStron /*
300620006a0bStron  */
WIN32textout(char * text,int len)3007*838f5788Ssimonb public void WIN32textout(char *text, int len)
300820006a0bStron {
300920006a0bStron #if MSDOS_COMPILER==WIN32C
301020006a0bStron 	DWORD written;
3011*838f5788Ssimonb 	if (utf_mode == 2)
3012*838f5788Ssimonb 	{
3013*838f5788Ssimonb 		/*
3014*838f5788Ssimonb 		 * We've got UTF-8 text in a non-UTF-8 console.  Convert it to
3015*838f5788Ssimonb 		 * wide and use WriteConsoleW.
3016*838f5788Ssimonb 		 */
3017*838f5788Ssimonb 		WCHAR wtext[1024];
3018*838f5788Ssimonb 		len = MultiByteToWideChar(CP_UTF8, 0, text, len, wtext,
3019*838f5788Ssimonb 					  sizeof(wtext)/sizeof(*wtext));
3020*838f5788Ssimonb 		WriteConsoleW(con_out, wtext, len, &written, NULL);
3021*838f5788Ssimonb 	} else
302220006a0bStron 		WriteConsole(con_out, text, len, &written, NULL);
302320006a0bStron #else
302420006a0bStron 	char c = text[len];
302520006a0bStron 	text[len] = '\0';
302620006a0bStron 	cputs(text);
302720006a0bStron 	text[len] = c;
302820006a0bStron #endif
302920006a0bStron }
303020006a0bStron #endif
3031*838f5788Ssimonb 
3032