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