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