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