Imported Upstream version 487
[platform/upstream/less.git] / screen.c
1 /*
2  * Copyright (C) 1984-2016  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 public int sgr_mode;            /* Honor ANSI sequences rather than using above */
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         if (sc_height <= 0)
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         if (sc_width <= 0)
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         sgr_mode = 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                         termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1139                         sprintf(termcap, "TERMCAP=%s", sp);
1140                         free(sp);
1141                         putenv(termcap);
1142                 }
1143         }
1144 #endif
1145         /*
1146          * Find out what kind of terminal this is.
1147          */
1148         if ((term = lgetenv("TERM")) == NULL)
1149                 term = DEFAULT_TERM;
1150         hardcopy = 0;
1151         if (tgetent(termbuf, term) != TGETENT_OK)
1152                 hardcopy = 1;
1153         if (ltgetflag("hc"))
1154                 hardcopy = 1;
1155
1156         /*
1157          * Get size of the screen.
1158          */
1159         scrsize();
1160         pos_init();
1161
1162         auto_wrap = ltgetflag("am");
1163         ignaw = ltgetflag("xn");
1164         above_mem = ltgetflag("da");
1165         below_mem = ltgetflag("db");
1166         clear_bg = ltgetflag("ut");
1167
1168         /*
1169          * Assumes termcap variable "sg" is the printing width of:
1170          * the standout sequence, the end standout sequence,
1171          * the underline sequence, the end underline sequence,
1172          * the boldface sequence, and the end boldface sequence.
1173          */
1174         if ((so_s_width = ltgetnum("sg")) < 0)
1175                 so_s_width = 0;
1176         so_e_width = so_s_width;
1177
1178         bo_s_width = bo_e_width = so_s_width;
1179         ul_s_width = ul_e_width = so_s_width;
1180         bl_s_width = bl_e_width = so_s_width;
1181
1182 #if HILITE_SEARCH
1183         if (so_s_width > 0 || so_e_width > 0)
1184                 /*
1185                  * Disable highlighting by default on magic cookie terminals.
1186                  * Turning on highlighting might change the displayed width
1187                  * of a line, causing the display to get messed up.
1188                  * The user can turn it back on with -g, 
1189                  * but she won't like the results.
1190                  */
1191                 hilite_search = 0;
1192 #endif
1193
1194         /*
1195          * Get various string-valued capabilities.
1196          */
1197         sp = sbuf;
1198
1199 #if HAVE_OSPEED
1200         sc_pad = ltgetstr("pc", &sp);
1201         if (sc_pad != NULL)
1202                 PC = *sc_pad;
1203 #endif
1204
1205         sc_s_keypad = ltgetstr("ks", &sp);
1206         if (sc_s_keypad == NULL)
1207                 sc_s_keypad = "";
1208         sc_e_keypad = ltgetstr("ke", &sp);
1209         if (sc_e_keypad == NULL)
1210                 sc_e_keypad = "";
1211                 
1212         sc_init = ltgetstr("ti", &sp);
1213         if (sc_init == NULL)
1214                 sc_init = "";
1215
1216         sc_deinit= ltgetstr("te", &sp);
1217         if (sc_deinit == NULL)
1218                 sc_deinit = "";
1219
1220         sc_eol_clear = ltgetstr("ce", &sp);
1221         if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1222         {
1223                 missing_cap = 1;
1224                 sc_eol_clear = "";
1225         }
1226
1227         sc_eos_clear = ltgetstr("cd", &sp);
1228         if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1229         {
1230                 missing_cap = 1;
1231                 sc_eos_clear = "";
1232         }
1233
1234         sc_clear = ltgetstr("cl", &sp);
1235         if (sc_clear == NULL || *sc_clear == '\0')
1236         {
1237                 missing_cap = 1;
1238                 sc_clear = "\n\n";
1239         }
1240
1241         sc_move = ltgetstr("cm", &sp);
1242         if (sc_move == NULL || *sc_move == '\0')
1243         {
1244                 /*
1245                  * This is not an error here, because we don't 
1246                  * always need sc_move.
1247                  * We need it only if we don't have home or lower-left.
1248                  */
1249                 sc_move = "";
1250                 can_goto_line = 0;
1251         } else
1252                 can_goto_line = 1;
1253
1254         tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1255         tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1256         tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1257         tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1258
1259         sc_visual_bell = ltgetstr("vb", &sp);
1260         if (sc_visual_bell == NULL)
1261                 sc_visual_bell = "";
1262
1263         if (ltgetflag("bs"))
1264                 sc_backspace = "\b";
1265         else
1266         {
1267                 sc_backspace = ltgetstr("bc", &sp);
1268                 if (sc_backspace == NULL || *sc_backspace == '\0')
1269                         sc_backspace = "\b";
1270         }
1271
1272         /*
1273          * Choose between using "ho" and "cm" ("home" and "cursor move")
1274          * to move the cursor to the upper left corner of the screen.
1275          */
1276         t1 = ltgetstr("ho", &sp);
1277         if (t1 == NULL)
1278                 t1 = "";
1279         if (*sc_move == '\0')
1280                 t2 = "";
1281         else
1282         {
1283                 strcpy(sp, tgoto(sc_move, 0, 0));
1284                 t2 = sp;
1285                 sp += strlen(sp) + 1;
1286         }
1287         sc_home = cheaper(t1, t2, "|\b^");
1288
1289         /*
1290          * Choose between using "ll" and "cm"  ("lower left" and "cursor move")
1291          * to move the cursor to the lower left corner of the screen.
1292          */
1293         t1 = ltgetstr("ll", &sp);
1294         if (t1 == NULL)
1295                 t1 = "";
1296         if (*sc_move == '\0')
1297                 t2 = "";
1298         else
1299         {
1300                 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1301                 t2 = sp;
1302                 sp += strlen(sp) + 1;
1303         }
1304         sc_lower_left = cheaper(t1, t2, "\r");
1305
1306         /*
1307          * Get carriage return string.
1308          */
1309         sc_return = ltgetstr("cr", &sp);
1310         if (sc_return == NULL)
1311                 sc_return = "\r";
1312
1313         /*
1314          * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1315          * to add a line at the top of the screen.
1316          */
1317         t1 = ltgetstr("al", &sp);
1318         if (t1 == NULL)
1319                 t1 = "";
1320         t2 = ltgetstr("sr", &sp);
1321         if (t2 == NULL)
1322                 t2 = "";
1323 #if OS2
1324         if (*t1 == '\0' && *t2 == '\0')
1325                 sc_addline = "";
1326         else
1327 #endif
1328         if (above_mem)
1329                 sc_addline = t1;
1330         else
1331                 sc_addline = cheaper(t1, t2, "");
1332         if (*sc_addline == '\0')
1333         {
1334                 /*
1335                  * Force repaint on any backward movement.
1336                  */
1337                 no_back_scroll = 1;
1338         }
1339 #endif /* MSDOS_COMPILER */
1340 }
1341
1342 #if !MSDOS_COMPILER
1343 /*
1344  * Return the cost of displaying a termcap string.
1345  * We use the trick of calling tputs, but as a char printing function
1346  * we give it inc_costcount, which just increments "costcount".
1347  * This tells us how many chars would be printed by using this string.
1348  * {{ Couldn't we just use strlen? }}
1349  */
1350 static int costcount;
1351
1352 /*ARGSUSED*/
1353         static int
1354 inc_costcount(c)
1355         int c;
1356 {
1357         costcount++;
1358         return (c);
1359 }
1360
1361         static int
1362 cost(t)
1363         char *t;
1364 {
1365         costcount = 0;
1366         tputs(t, sc_height, inc_costcount);
1367         return (costcount);
1368 }
1369
1370 /*
1371  * Return the "best" of the two given termcap strings.
1372  * The best, if both exist, is the one with the lower 
1373  * cost (see cost() function).
1374  */
1375         static char *
1376 cheaper(t1, t2, def)
1377         char *t1, *t2;
1378         char *def;
1379 {
1380         if (*t1 == '\0' && *t2 == '\0')
1381         {
1382                 missing_cap = 1;
1383                 return (def);
1384         }
1385         if (*t1 == '\0')
1386                 return (t2);
1387         if (*t2 == '\0')
1388                 return (t1);
1389         if (cost(t1) < cost(t2))
1390                 return (t1);
1391         return (t2);
1392 }
1393
1394         static void
1395 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1396         char *incap;
1397         char *outcap;
1398         char **instr;
1399         char **outstr;
1400         char *def_instr;
1401         char *def_outstr;
1402         char **spp;
1403 {
1404         *instr = ltgetstr(incap, spp);
1405         if (*instr == NULL)
1406         {
1407                 /* Use defaults. */
1408                 *instr = def_instr;
1409                 *outstr = def_outstr;
1410                 return;
1411         }
1412
1413         *outstr = ltgetstr(outcap, spp);
1414         if (*outstr == NULL)
1415                 /* No specific out capability; use "me". */
1416                 *outstr = ltgetstr("me", spp);
1417         if (*outstr == NULL)
1418                 /* Don't even have "me"; use a null string. */
1419                 *outstr = "";
1420 }
1421
1422 #endif /* MSDOS_COMPILER */
1423
1424
1425 /*
1426  * Below are the functions which perform all the 
1427  * terminal-specific screen manipulation.
1428  */
1429
1430
1431 #if MSDOS_COMPILER
1432
1433 #if MSDOS_COMPILER==WIN32C
1434         static void
1435 _settextposition(int row, int col)
1436 {
1437         COORD cpos;
1438         CONSOLE_SCREEN_BUFFER_INFO csbi;
1439
1440         GetConsoleScreenBufferInfo(con_out, &csbi);
1441         cpos.X = csbi.srWindow.Left + (col - 1);
1442         cpos.Y = csbi.srWindow.Top + (row - 1);
1443         SetConsoleCursorPosition(con_out, cpos);
1444 }
1445 #endif
1446
1447 /*
1448  * Initialize the screen to the correct color at startup.
1449  */
1450         static void
1451 initcolor()
1452 {
1453 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1454         intensevideo();
1455 #endif
1456         SETCOLORS(nm_fg_color, nm_bg_color);
1457 #if 0
1458         /*
1459          * This clears the screen at startup.  This is different from
1460          * the behavior of other versions of less.  Disable it for now.
1461          */
1462         char *blanks;
1463         int row;
1464         int col;
1465         
1466         /*
1467          * Create a complete, blank screen using "normal" colors.
1468          */
1469         SETCOLORS(nm_fg_color, nm_bg_color);
1470         blanks = (char *) ecalloc(width+1, sizeof(char));
1471         for (col = 0;  col < sc_width;  col++)
1472                 blanks[col] = ' ';
1473         blanks[sc_width] = '\0';
1474         for (row = 0;  row < sc_height;  row++)
1475                 _outtext(blanks);
1476         free(blanks);
1477 #endif
1478 }
1479 #endif
1480
1481 #if MSDOS_COMPILER==WIN32C
1482
1483 /*
1484  * Termcap-like init with a private win32 console.
1485  */
1486         static void
1487 win32_init_term()
1488 {
1489         CONSOLE_SCREEN_BUFFER_INFO scr;
1490         COORD size;
1491
1492         if (con_out_save == INVALID_HANDLE_VALUE)
1493                 return;
1494
1495         GetConsoleScreenBufferInfo(con_out_save, &scr);
1496
1497         if (con_out_ours == INVALID_HANDLE_VALUE)
1498         {
1499                 /*
1500                  * Create our own screen buffer, so that we
1501                  * may restore the original when done.
1502                  */
1503                 con_out_ours = CreateConsoleScreenBuffer(
1504                         GENERIC_WRITE | GENERIC_READ,
1505                         FILE_SHARE_WRITE | FILE_SHARE_READ,
1506                         (LPSECURITY_ATTRIBUTES) NULL,
1507                         CONSOLE_TEXTMODE_BUFFER,
1508                         (LPVOID) NULL);
1509         }
1510
1511         size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1512         size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1513         SetConsoleScreenBufferSize(con_out_ours, size);
1514         SetConsoleActiveScreenBuffer(con_out_ours);
1515         con_out = con_out_ours;
1516 }
1517
1518 /*
1519  * Restore the startup console.
1520  */
1521 static void
1522 win32_deinit_term()
1523 {
1524         if (con_out_save == INVALID_HANDLE_VALUE)
1525                 return;
1526         if (quitting)
1527                 (void) CloseHandle(con_out_ours);
1528         SetConsoleActiveScreenBuffer(con_out_save);
1529         con_out = con_out_save;
1530 }
1531
1532 #endif
1533
1534 /*
1535  * Initialize terminal
1536  */
1537         public void
1538 init()
1539 {
1540 #if !MSDOS_COMPILER
1541         if (!no_init)
1542                 tputs(sc_init, sc_height, putchr);
1543         if (!no_keypad)
1544                 tputs(sc_s_keypad, sc_height, putchr);
1545         if (top_scroll) 
1546         {
1547                 int i;
1548
1549                 /*
1550                  * This is nice to terminals with no alternate screen,
1551                  * but with saved scrolled-off-the-top lines.  This way,
1552                  * no previous line is lost, but we start with a whole
1553                  * screen to ourself.
1554                  */
1555                 for (i = 1; i < sc_height; i++)
1556                         putchr('\n');
1557         } else
1558                 line_left();
1559 #else
1560 #if MSDOS_COMPILER==WIN32C
1561         if (!no_init)
1562                 win32_init_term();
1563 #endif
1564         initcolor();
1565         flush();
1566 #endif
1567         init_done = 1;
1568 }
1569
1570 /*
1571  * Deinitialize terminal
1572  */
1573         public void
1574 deinit()
1575 {
1576         if (!init_done)
1577                 return;
1578 #if !MSDOS_COMPILER
1579         if (!no_keypad)
1580                 tputs(sc_e_keypad, sc_height, putchr);
1581         if (!no_init)
1582                 tputs(sc_deinit, sc_height, putchr);
1583 #else
1584         /* Restore system colors. */
1585         SETCOLORS(sy_fg_color, sy_bg_color);
1586 #if MSDOS_COMPILER==WIN32C
1587         if (!no_init)
1588                 win32_deinit_term();
1589 #else
1590         /* Need clreol to make SETCOLORS take effect. */
1591         clreol();
1592 #endif
1593 #endif
1594         init_done = 0;
1595 }
1596
1597 /*
1598  * Home cursor (move to upper left corner of screen).
1599  */
1600         public void
1601 home()
1602 {
1603 #if !MSDOS_COMPILER
1604         tputs(sc_home, 1, putchr);
1605 #else
1606         flush();
1607         _settextposition(1,1);
1608 #endif
1609 }
1610
1611 /*
1612  * Add a blank line (called with cursor at home).
1613  * Should scroll the display down.
1614  */
1615         public void
1616 add_line()
1617 {
1618 #if !MSDOS_COMPILER
1619         tputs(sc_addline, sc_height, putchr);
1620 #else
1621         flush();
1622 #if MSDOS_COMPILER==MSOFTC
1623         _scrolltextwindow(_GSCROLLDOWN);
1624         _settextposition(1,1);
1625 #else
1626 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1627         movetext(1,1, sc_width,sc_height-1, 1,2);
1628         gotoxy(1,1);
1629         clreol();
1630 #else
1631 #if MSDOS_COMPILER==WIN32C
1632     {
1633         CHAR_INFO fillchar;
1634         SMALL_RECT rcSrc, rcClip;
1635         COORD new_org;
1636         CONSOLE_SCREEN_BUFFER_INFO csbi;
1637
1638         GetConsoleScreenBufferInfo(con_out,&csbi);
1639
1640         /* The clip rectangle is the entire visible screen. */
1641         rcClip.Left = csbi.srWindow.Left;
1642         rcClip.Top = csbi.srWindow.Top;
1643         rcClip.Right = csbi.srWindow.Right;
1644         rcClip.Bottom = csbi.srWindow.Bottom;
1645
1646         /* The source rectangle is the visible screen minus the last line. */
1647         rcSrc = rcClip;
1648         rcSrc.Bottom--;
1649
1650         /* Move the top left corner of the source window down one row. */
1651         new_org.X = rcSrc.Left;
1652         new_org.Y = rcSrc.Top + 1;
1653
1654         /* Fill the right character and attributes. */
1655         fillchar.Char.AsciiChar = ' ';
1656         curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1657         fillchar.Attributes = curr_attr;
1658         ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1659         _settextposition(1,1);
1660     }
1661 #endif
1662 #endif
1663 #endif
1664 #endif
1665 }
1666
1667 #if 0
1668 /*
1669  * Remove the n topmost lines and scroll everything below it in the 
1670  * window upward.  This is needed to stop leaking the topmost line 
1671  * into the scrollback buffer when we go down-one-line (in WIN32).
1672  */
1673         public void
1674 remove_top(n)
1675         int n;
1676 {
1677 #if MSDOS_COMPILER==WIN32C
1678         SMALL_RECT rcSrc, rcClip;
1679         CHAR_INFO fillchar;
1680         COORD new_org;
1681         CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1682
1683         if (n >= sc_height - 1)
1684         {
1685                 clear();
1686                 home();
1687                 return;
1688         }
1689
1690         flush();
1691
1692         GetConsoleScreenBufferInfo(con_out, &csbi);
1693
1694         /* Get the extent of all-visible-rows-but-the-last. */
1695         rcSrc.Left    = csbi.srWindow.Left;
1696         rcSrc.Top     = csbi.srWindow.Top + n;
1697         rcSrc.Right   = csbi.srWindow.Right;
1698         rcSrc.Bottom  = csbi.srWindow.Bottom;
1699
1700         /* Get the clip rectangle. */
1701         rcClip.Left   = rcSrc.Left;
1702         rcClip.Top    = csbi.srWindow.Top;
1703         rcClip.Right  = rcSrc.Right;
1704         rcClip.Bottom = rcSrc.Bottom ;
1705
1706         /* Move the source window up n rows. */
1707         new_org.X = rcSrc.Left;
1708         new_org.Y = rcSrc.Top - n;
1709
1710         /* Fill the right character and attributes. */
1711         fillchar.Char.AsciiChar = ' ';
1712         curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1713         fillchar.Attributes = curr_attr;
1714
1715         ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1716
1717         /* Position cursor on first blank line. */
1718         goto_line(sc_height - n - 1);
1719 #endif
1720 }
1721 #endif
1722
1723 #if MSDOS_COMPILER==WIN32C
1724 /*
1725  * Clear the screen.
1726  */
1727         static void
1728 win32_clear()
1729 {
1730         /*
1731          * This will clear only the currently visible rows of the NT
1732          * console buffer, which means none of the precious scrollback
1733          * rows are touched making for faster scrolling.  Note that, if
1734          * the window has fewer columns than the console buffer (i.e.
1735          * there is a horizontal scrollbar as well), the entire width
1736          * of the visible rows will be cleared.
1737          */
1738         COORD topleft;
1739         DWORD nchars;
1740         DWORD winsz;
1741         CONSOLE_SCREEN_BUFFER_INFO csbi;
1742
1743         /* get the number of cells in the current buffer */
1744         GetConsoleScreenBufferInfo(con_out, &csbi);
1745         winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1746         topleft.X = 0;
1747         topleft.Y = csbi.srWindow.Top;
1748
1749         curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1750         FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1751         FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1752 }
1753
1754 /*
1755  * Remove the n topmost lines and scroll everything below it in the 
1756  * window upward.
1757  */
1758         public void
1759 win32_scroll_up(n)
1760         int n;
1761 {
1762         SMALL_RECT rcSrc, rcClip;
1763         CHAR_INFO fillchar;
1764         COORD topleft;
1765         COORD new_org;
1766         DWORD nchars;
1767         DWORD size;
1768         CONSOLE_SCREEN_BUFFER_INFO csbi;
1769
1770         if (n <= 0)
1771                 return;
1772
1773         if (n >= sc_height - 1)
1774         {
1775                 win32_clear();
1776                 _settextposition(1,1);
1777                 return;
1778         }
1779
1780         /* Get the extent of what will remain visible after scrolling. */
1781         GetConsoleScreenBufferInfo(con_out, &csbi);
1782         rcSrc.Left    = csbi.srWindow.Left;
1783         rcSrc.Top     = csbi.srWindow.Top + n;
1784         rcSrc.Right   = csbi.srWindow.Right;
1785         rcSrc.Bottom  = csbi.srWindow.Bottom;
1786
1787         /* Get the clip rectangle. */
1788         rcClip.Left   = rcSrc.Left;
1789         rcClip.Top    = csbi.srWindow.Top;
1790         rcClip.Right  = rcSrc.Right;
1791         rcClip.Bottom = rcSrc.Bottom ;
1792
1793         /* Move the source text to the top of the screen. */
1794         new_org.X = rcSrc.Left;
1795         new_org.Y = rcClip.Top;
1796
1797         /* Fill the right character and attributes. */
1798         fillchar.Char.AsciiChar = ' ';
1799         fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1800
1801         /* Scroll the window. */
1802         SetConsoleTextAttribute(con_out, fillchar.Attributes);
1803         ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1804
1805         /* Clear remaining lines at bottom. */
1806         topleft.X = csbi.dwCursorPosition.X;
1807         topleft.Y = rcSrc.Bottom - n;
1808         size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1809         FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1810                 &nchars);
1811         FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1812                 &nchars);
1813         SetConsoleTextAttribute(con_out, curr_attr);
1814
1815         /* Move cursor n lines up from where it was. */
1816         csbi.dwCursorPosition.Y -= n;
1817         SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1818 }
1819 #endif
1820
1821 /*
1822  * Move cursor to lower left corner of screen.
1823  */
1824         public void
1825 lower_left()
1826 {
1827 #if !MSDOS_COMPILER
1828         tputs(sc_lower_left, 1, putchr);
1829 #else
1830         flush();
1831         _settextposition(sc_height, 1);
1832 #endif
1833 }
1834
1835 /*
1836  * Move cursor to left position of current line.
1837  */
1838         public void
1839 line_left()
1840 {
1841 #if !MSDOS_COMPILER
1842         tputs(sc_return, 1, putchr);
1843 #else
1844         int row;
1845         flush();
1846 #if MSDOS_COMPILER==WIN32C
1847         {
1848                 CONSOLE_SCREEN_BUFFER_INFO scr;
1849                 GetConsoleScreenBufferInfo(con_out, &scr);
1850                 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
1851         }
1852 #else
1853 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1854                 row = wherey();
1855 #else
1856         {
1857                 struct rccoord tpos = _gettextposition();
1858                 row = tpos.row;
1859         }
1860 #endif
1861 #endif
1862         _settextposition(row, 1);
1863 #endif
1864 }
1865
1866 /*
1867  * Check if the console size has changed and reset internals 
1868  * (in lieu of SIGWINCH for WIN32).
1869  */
1870         public void
1871 check_winch()
1872 {
1873 #if MSDOS_COMPILER==WIN32C
1874         CONSOLE_SCREEN_BUFFER_INFO scr;
1875         COORD size;
1876
1877         if (con_out == INVALID_HANDLE_VALUE)
1878                 return;
1879  
1880         flush();
1881         GetConsoleScreenBufferInfo(con_out, &scr);
1882         size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1883         size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1884         if (size.Y != sc_height || size.X != sc_width)
1885         {
1886                 sc_height = size.Y;
1887                 sc_width = size.X;
1888                 if (!no_init && con_out_ours == con_out)
1889                         SetConsoleScreenBufferSize(con_out, size);
1890                 pos_init();
1891                 wscroll = (sc_height + 1) / 2;
1892                 screen_trashed = 1;
1893         }
1894 #endif
1895 }
1896
1897 /*
1898  * Goto a specific line on the screen.
1899  */
1900         public void
1901 goto_line(slinenum)
1902         int slinenum;
1903 {
1904 #if !MSDOS_COMPILER
1905         tputs(tgoto(sc_move, 0, slinenum), 1, putchr);
1906 #else
1907         flush();
1908         _settextposition(slinenum+1, 1);
1909 #endif
1910 }
1911
1912 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1913 /*
1914  * Create an alternate screen which is all white.
1915  * This screen is used to create a "flash" effect, by displaying it
1916  * briefly and then switching back to the normal screen.
1917  * {{ Yuck!  There must be a better way to get a visual bell. }}
1918  */
1919         static void
1920 create_flash()
1921 {
1922 #if MSDOS_COMPILER==MSOFTC
1923         struct videoconfig w;
1924         char *blanks;
1925         int row, col;
1926         
1927         _getvideoconfig(&w);
1928         videopages = w.numvideopages;
1929         if (videopages < 2)
1930         {
1931                 at_enter(AT_STANDOUT);
1932                 at_exit();
1933         } else
1934         {
1935                 _setactivepage(1);
1936                 at_enter(AT_STANDOUT);
1937                 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
1938                 for (col = 0;  col < w.numtextcols;  col++)
1939                         blanks[col] = ' ';
1940                 for (row = w.numtextrows;  row > 0;  row--)
1941                         _outmem(blanks, w.numtextcols);
1942                 _setactivepage(0);
1943                 _setvisualpage(0);
1944                 free(blanks);
1945                 at_exit();
1946         }
1947 #else
1948 #if MSDOS_COMPILER==BORLANDC
1949         register int n;
1950
1951         whitescreen = (unsigned short *) 
1952                 malloc(sc_width * sc_height * sizeof(short));
1953         if (whitescreen == NULL)
1954                 return;
1955         for (n = 0;  n < sc_width * sc_height;  n++)
1956                 whitescreen[n] = 0x7020;
1957 #else
1958 #if MSDOS_COMPILER==WIN32C
1959         register int n;
1960
1961         whitescreen = (WORD *)
1962                 malloc(sc_height * sc_width * sizeof(WORD));
1963         if (whitescreen == NULL)
1964                 return;
1965         /* Invert the standard colors. */
1966         for (n = 0;  n < sc_width * sc_height;  n++)
1967                 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
1968 #endif
1969 #endif
1970 #endif
1971         flash_created = 1;
1972 }
1973 #endif /* MSDOS_COMPILER */
1974
1975 /*
1976  * Output the "visual bell", if there is one.
1977  */
1978         public void
1979 vbell()
1980 {
1981 #if !MSDOS_COMPILER
1982         if (*sc_visual_bell == '\0')
1983                 return;
1984         tputs(sc_visual_bell, sc_height, putchr);
1985 #else
1986 #if MSDOS_COMPILER==DJGPPC
1987         ScreenVisualBell();
1988 #else
1989 #if MSDOS_COMPILER==MSOFTC
1990         /*
1991          * Create a flash screen on the second video page.
1992          * Switch to that page, then switch back.
1993          */
1994         if (!flash_created)
1995                 create_flash();
1996         if (videopages < 2)
1997                 return;
1998         _setvisualpage(1);
1999         delay(100);
2000         _setvisualpage(0);
2001 #else
2002 #if MSDOS_COMPILER==BORLANDC
2003         unsigned short *currscreen;
2004
2005         /*
2006          * Get a copy of the current screen.
2007          * Display the flash screen.
2008          * Then restore the old screen.
2009          */
2010         if (!flash_created)
2011                 create_flash();
2012         if (whitescreen == NULL)
2013                 return;
2014         currscreen = (unsigned short *) 
2015                 malloc(sc_width * sc_height * sizeof(short));
2016         if (currscreen == NULL) return;
2017         gettext(1, 1, sc_width, sc_height, currscreen);
2018         puttext(1, 1, sc_width, sc_height, whitescreen);
2019         delay(100);
2020         puttext(1, 1, sc_width, sc_height, currscreen);
2021         free(currscreen);
2022 #else
2023 #if MSDOS_COMPILER==WIN32C
2024         /* paint screen with an inverse color */
2025         clear();
2026
2027         /* leave it displayed for 100 msec. */
2028         Sleep(100);
2029
2030         /* restore with a redraw */
2031         repaint();
2032 #endif
2033 #endif
2034 #endif
2035 #endif
2036 #endif
2037 }
2038
2039 /*
2040  * Make a noise.
2041  */
2042         static void
2043 beep()
2044 {
2045 #if !MSDOS_COMPILER
2046         putchr(CONTROL('G'));
2047 #else
2048 #if MSDOS_COMPILER==WIN32C
2049         MessageBeep(0);
2050 #else
2051         write(1, "\7", 1);
2052 #endif
2053 #endif
2054 }
2055
2056 /*
2057  * Ring the terminal bell.
2058  */
2059         public void
2060 bell()
2061 {
2062         if (quiet == VERY_QUIET)
2063                 vbell();
2064         else
2065                 beep();
2066 }
2067
2068 /*
2069  * Clear the screen.
2070  */
2071         public void
2072 clear()
2073 {
2074 #if !MSDOS_COMPILER
2075         tputs(sc_clear, sc_height, putchr);
2076 #else
2077         flush();
2078 #if MSDOS_COMPILER==WIN32C
2079         win32_clear();
2080 #else
2081         _clearscreen(_GCLEARSCREEN);
2082 #endif
2083 #endif
2084 }
2085
2086 /*
2087  * Clear from the cursor to the end of the cursor's line.
2088  * {{ This must not move the cursor. }}
2089  */
2090         public void
2091 clear_eol()
2092 {
2093 #if !MSDOS_COMPILER
2094         tputs(sc_eol_clear, 1, putchr);
2095 #else
2096 #if MSDOS_COMPILER==MSOFTC
2097         short top, left;
2098         short bot, right;
2099         struct rccoord tpos;
2100         
2101         flush();
2102         /*
2103          * Save current state.
2104          */
2105         tpos = _gettextposition();
2106         _gettextwindow(&top, &left, &bot, &right);
2107         /*
2108          * Set a temporary window to the current line,
2109          * from the cursor's position to the right edge of the screen.
2110          * Then clear that window.
2111          */
2112         _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2113         _clearscreen(_GWINDOW);
2114         /*
2115          * Restore state.
2116          */
2117         _settextwindow(top, left, bot, right);
2118         _settextposition(tpos.row, tpos.col);
2119 #else
2120 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2121         flush();
2122         clreol();
2123 #else
2124 #if MSDOS_COMPILER==WIN32C
2125         DWORD           nchars;
2126         COORD           cpos;
2127         CONSOLE_SCREEN_BUFFER_INFO scr;
2128
2129         flush();
2130         memset(&scr, 0, sizeof(scr));
2131         GetConsoleScreenBufferInfo(con_out, &scr);
2132         cpos.X = scr.dwCursorPosition.X;
2133         cpos.Y = scr.dwCursorPosition.Y;
2134         curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2135         FillConsoleOutputAttribute(con_out, curr_attr,
2136                 scr.dwSize.X - cpos.X, cpos, &nchars);
2137         FillConsoleOutputCharacter(con_out, ' ',
2138                 scr.dwSize.X - cpos.X, cpos, &nchars);
2139 #endif
2140 #endif
2141 #endif
2142 #endif
2143 }
2144
2145 /*
2146  * Clear the current line.
2147  * Clear the screen if there's off-screen memory below the display.
2148  */
2149         static void
2150 clear_eol_bot()
2151 {
2152 #if MSDOS_COMPILER
2153         clear_eol();
2154 #else
2155         if (below_mem)
2156                 tputs(sc_eos_clear, 1, putchr);
2157         else
2158                 tputs(sc_eol_clear, 1, putchr);
2159 #endif
2160 }
2161
2162 /*
2163  * Clear the bottom line of the display.
2164  * Leave the cursor at the beginning of the bottom line.
2165  */
2166         public void
2167 clear_bot()
2168 {
2169         /*
2170          * If we're in a non-normal attribute mode, temporarily exit
2171          * the mode while we do the clear.  Some terminals fill the
2172          * cleared area with the current attribute.
2173          */
2174         if (oldbot)
2175                 lower_left();
2176         else
2177                 line_left();
2178
2179         if (attrmode == AT_NORMAL)
2180                 clear_eol_bot();
2181         else
2182         {
2183                 int saved_attrmode = attrmode;
2184
2185                 at_exit();
2186                 clear_eol_bot();
2187                 at_enter(saved_attrmode);
2188         }
2189 }
2190
2191         public void
2192 at_enter(attr)
2193         int attr;
2194 {
2195         attr = apply_at_specials(attr);
2196
2197 #if !MSDOS_COMPILER
2198         /* The one with the most priority is last.  */
2199         if (attr & AT_UNDERLINE)
2200                 tputs(sc_u_in, 1, putchr);
2201         if (attr & AT_BOLD)
2202                 tputs(sc_b_in, 1, putchr);
2203         if (attr & AT_BLINK)
2204                 tputs(sc_bl_in, 1, putchr);
2205         if (attr & AT_STANDOUT)
2206                 tputs(sc_s_in, 1, putchr);
2207 #else
2208         flush();
2209         /* The one with the most priority is first.  */
2210         if (attr & AT_STANDOUT)
2211         {
2212                 SETCOLORS(so_fg_color, so_bg_color);
2213         } else if (attr & AT_BLINK)
2214         {
2215                 SETCOLORS(bl_fg_color, bl_bg_color);
2216         }
2217         else if (attr & AT_BOLD)
2218         {
2219                 SETCOLORS(bo_fg_color, bo_bg_color);
2220         }
2221         else if (attr & AT_UNDERLINE)
2222         {
2223                 SETCOLORS(ul_fg_color, ul_bg_color);
2224         }
2225 #endif
2226
2227         attrmode = attr;
2228 }
2229
2230         public void
2231 at_exit()
2232 {
2233 #if !MSDOS_COMPILER
2234         /* Undo things in the reverse order we did them.  */
2235         if (attrmode & AT_STANDOUT)
2236                 tputs(sc_s_out, 1, putchr);
2237         if (attrmode & AT_BLINK)
2238                 tputs(sc_bl_out, 1, putchr);
2239         if (attrmode & AT_BOLD)
2240                 tputs(sc_b_out, 1, putchr);
2241         if (attrmode & AT_UNDERLINE)
2242                 tputs(sc_u_out, 1, putchr);
2243 #else
2244         flush();
2245         SETCOLORS(nm_fg_color, nm_bg_color);
2246 #endif
2247
2248         attrmode = AT_NORMAL;
2249 }
2250
2251         public void
2252 at_switch(attr)
2253         int attr;
2254 {
2255         int new_attrmode = apply_at_specials(attr);
2256         int ignore_modes = AT_ANSI;
2257
2258         if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
2259         {
2260                 at_exit();
2261                 at_enter(attr);
2262         }
2263 }
2264
2265         public int
2266 is_at_equiv(attr1, attr2)
2267         int attr1;
2268         int attr2;
2269 {
2270         attr1 = apply_at_specials(attr1);
2271         attr2 = apply_at_specials(attr2);
2272
2273         return (attr1 == attr2);
2274 }
2275
2276         public int
2277 apply_at_specials(attr)
2278         int attr;
2279 {
2280         if (attr & AT_BINARY)
2281                 attr |= binattr;
2282         if (attr & AT_HILITE)
2283                 attr |= AT_STANDOUT;
2284         attr &= ~(AT_BINARY|AT_HILITE);
2285
2286         return attr;
2287 }
2288
2289 #if 0 /* No longer used */
2290 /*
2291  * Erase the character to the left of the cursor 
2292  * and move the cursor left.
2293  */
2294         public void
2295 backspace()
2296 {
2297 #if !MSDOS_COMPILER
2298         /* 
2299          * Erase the previous character by overstriking with a space.
2300          */
2301         tputs(sc_backspace, 1, putchr);
2302         putchr(' ');
2303         tputs(sc_backspace, 1, putchr);
2304 #else
2305 #if MSDOS_COMPILER==MSOFTC
2306         struct rccoord tpos;
2307         
2308         flush();
2309         tpos = _gettextposition();
2310         if (tpos.col <= 1)
2311                 return;
2312         _settextposition(tpos.row, tpos.col-1);
2313         _outtext(" ");
2314         _settextposition(tpos.row, tpos.col-1);
2315 #else
2316 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2317         cputs("\b");
2318 #else
2319 #if MSDOS_COMPILER==WIN32C
2320         COORD cpos;
2321         DWORD cChars;
2322         CONSOLE_SCREEN_BUFFER_INFO scr;
2323
2324         flush();
2325         GetConsoleScreenBufferInfo(con_out, &scr);
2326         cpos = scr.dwCursorPosition;
2327         if (cpos.X <= 0)
2328                 return;
2329         cpos.X--;
2330         SetConsoleCursorPosition(con_out, cpos);
2331         FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2332         SetConsoleCursorPosition(con_out, cpos);
2333 #endif
2334 #endif
2335 #endif
2336 #endif
2337 }
2338 #endif /* 0 */
2339
2340 /*
2341  * Output a plain backspace, without erasing the previous char.
2342  */
2343         public void
2344 putbs()
2345 {
2346 #if !MSDOS_COMPILER
2347         tputs(sc_backspace, 1, putchr);
2348 #else
2349         int row, col;
2350
2351         flush();
2352         {
2353 #if MSDOS_COMPILER==MSOFTC
2354                 struct rccoord tpos;
2355                 tpos = _gettextposition();
2356                 row = tpos.row;
2357                 col = tpos.col;
2358 #else
2359 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2360                 row = wherey();
2361                 col = wherex();
2362 #else
2363 #if MSDOS_COMPILER==WIN32C
2364                 CONSOLE_SCREEN_BUFFER_INFO scr;
2365                 GetConsoleScreenBufferInfo(con_out, &scr);
2366                 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2367                 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2368 #endif
2369 #endif
2370 #endif
2371         }
2372         if (col <= 1)
2373                 return;
2374         _settextposition(row, col-1);
2375 #endif /* MSDOS_COMPILER */
2376 }
2377
2378 #if MSDOS_COMPILER==WIN32C
2379 /*
2380  * Determine whether an input character is waiting to be read.
2381  */
2382         static int
2383 win32_kbhit(tty)
2384         HANDLE tty;
2385 {
2386         INPUT_RECORD ip;
2387         DWORD read;
2388
2389         if (keyCount > 0)
2390                 return (TRUE);
2391
2392         currentKey.ascii = 0;
2393         currentKey.scan = 0;
2394
2395         /*
2396          * Wait for a real key-down event, but
2397          * ignore SHIFT and CONTROL key events.
2398          */
2399         do
2400         {
2401                 PeekConsoleInput(tty, &ip, 1, &read);
2402                 if (read == 0)
2403                         return (FALSE);
2404                 ReadConsoleInput(tty, &ip, 1, &read);
2405         } while (ip.EventType != KEY_EVENT ||
2406                 ip.Event.KeyEvent.bKeyDown != TRUE ||
2407                 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2408                 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2409                 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2410                 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2411                 
2412         currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2413         currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2414         keyCount = ip.Event.KeyEvent.wRepeatCount;
2415
2416         if (ip.Event.KeyEvent.dwControlKeyState & 
2417                 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2418         {
2419                 switch (currentKey.scan)
2420                 {
2421                 case PCK_ALT_E:     /* letter 'E' */
2422                         currentKey.ascii = 0;
2423                         break;
2424                 }
2425         } else if (ip.Event.KeyEvent.dwControlKeyState & 
2426                 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2427         {
2428                 switch (currentKey.scan)
2429                 {
2430                 case PCK_RIGHT: /* right arrow */
2431                         currentKey.scan = PCK_CTL_RIGHT;
2432                         break;
2433                 case PCK_LEFT: /* left arrow */
2434                         currentKey.scan = PCK_CTL_LEFT;
2435                         break;
2436                 case PCK_DELETE: /* delete */
2437                         currentKey.scan = PCK_CTL_DELETE;
2438                         break;
2439                 }
2440         } else if (ip.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
2441         {
2442                 switch (currentKey.scan)
2443                 {
2444                 case PCK_SHIFT_TAB: /* tab */
2445                         currentKey.ascii = 0;
2446                         break;
2447                 }
2448         }
2449
2450         return (TRUE);
2451 }
2452
2453 /*
2454  * Read a character from the keyboard.
2455  */
2456         public char
2457 WIN32getch(tty)
2458         int tty;
2459 {
2460         int ascii;
2461
2462         if (pending_scancode)
2463         {
2464                 pending_scancode = 0;
2465                 return ((char)(currentKey.scan & 0x00FF));
2466         }
2467
2468         while (win32_kbhit((HANDLE)tty) == FALSE)
2469         {
2470                 Sleep(20);
2471                 if (ABORT_SIGS())
2472                         return ('\003');
2473                 continue;
2474         }
2475         keyCount --;
2476         ascii = currentKey.ascii;
2477         /*
2478          * On PC's, the extended keys return a 2 byte sequence beginning 
2479          * with '00', so if the ascii code is 00, the next byte will be 
2480          * the lsb of the scan code.
2481          */
2482         pending_scancode = (ascii == 0x00);
2483         return ((char)ascii);
2484 }
2485 #endif
2486
2487 #if MSDOS_COMPILER
2488 /*
2489  */
2490         public void
2491 WIN32setcolors(fg, bg)
2492         int fg;
2493         int bg;
2494 {
2495         SETCOLORS(fg, bg);
2496 }
2497
2498 /*
2499  */
2500         public void
2501 WIN32textout(text, len)
2502         char *text;
2503         int len;
2504 {
2505 #if MSDOS_COMPILER==WIN32C
2506         DWORD written;
2507         WriteConsole(con_out, text, len, &written, NULL);
2508 #else
2509         char c = text[len];
2510         text[len] = '\0';
2511         cputs(text);
2512         text[len] = c;
2513 #endif
2514 }
2515 #endif