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