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