1 /* $NetBSD: screen.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Routines which deal with the characteristics of the terminal.
15 * Uses termcap to be as terminal-independent as possible.
23 #if MSDOS_COMPILER==MSOFTC
26 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
28 #if MSDOS_COMPILER==DJGPPC
33 #if MSDOS_COMPILER==WIN32C
43 #include <sys/ioctl.h>
46 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
67 #include <sys/signal.h>
71 #include <sys/stream.h>
77 #endif /* MSDOS_COMPILER */
80 * Check for broken termios package that forces you to manually
81 * set the line discipline.
84 #define MUST_SET_LINE_DISCIPLINE 1
86 #define MUST_SET_LINE_DISCIPLINE 0
90 #define DEFAULT_TERM "ansi"
91 static char *windowid
;
93 #define DEFAULT_TERM "unknown"
96 #if MSDOS_COMPILER==MSOFTC
97 static int videopages
;
98 static long msec_loops
;
99 static int flash_created
= 0;
100 #define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); }
103 #if MSDOS_COMPILER==BORLANDC
104 static unsigned short *whitescreen
;
105 static int flash_created
= 0;
107 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
108 #define _settextposition(y,x) gotoxy(x,y)
109 #define _clearscreen(m) clrscr()
110 #define _outtext(s) cputs(s)
111 #define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); }
112 extern int sc_height
;
115 #if MSDOS_COMPILER==WIN32C
122 static int keyCount
= 0;
123 static WORD curr_attr
;
124 static int pending_scancode
= 0;
125 static WORD
*whitescreen
;
127 static HANDLE con_out_save
= INVALID_HANDLE_VALUE
; /* previous console */
128 static HANDLE con_out_ours
= INVALID_HANDLE_VALUE
; /* our own */
129 HANDLE con_out
= INVALID_HANDLE_VALUE
; /* current console */
132 static void win32_init_term();
133 static void win32_deinit_term();
135 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
136 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
137 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
138 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \
139 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
140 error("SETCOLORS failed"); }
144 public int nm_fg_color
; /* Color of normal text */
145 public int nm_bg_color
;
146 public int bo_fg_color
; /* Color of bold text */
147 public int bo_bg_color
;
148 public int ul_fg_color
; /* Color of underlined text */
149 public int ul_bg_color
;
150 public int so_fg_color
; /* Color of standout text */
151 public int so_bg_color
;
152 public int bl_fg_color
; /* Color of blinking text */
153 public int bl_bg_color
;
154 static int sy_fg_color
; /* Color of system text (before less) */
155 static int sy_bg_color
;
160 * Strings passed to tputs() to do various terminal functions.
163 *sc_pad
, /* Pad string */
164 *sc_home
, /* Cursor home */
165 *sc_addline
, /* Add line, scroll down following lines */
166 *sc_lower_left
, /* Cursor to last line, first column */
167 *sc_return
, /* Cursor to beginning of current line */
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 */
188 static int init_done
= 0;
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
; /* The user's erase char */
193 public int erase2_char
; /* The user's other erase char */
194 public int kill_char
; /* The user's line-kill char */
195 public int werase_char
; /* The user's word-erase char */
196 public int sc_width
, sc_height
; /* Height & width of screen */
197 public int bo_s_width
, bo_e_width
; /* Printing width of boldface seq */
198 public int ul_s_width
, ul_e_width
; /* Printing width of underline seq */
199 public int so_s_width
, so_e_width
; /* Printing width of standout seq */
200 public int bl_s_width
, bl_e_width
; /* Printing width of blink seq */
201 public int above_mem
, below_mem
; /* Memory retained above/below screen */
202 public int can_goto_line
; /* Can move cursor to any line */
203 public int clear_bg
; /* Clear fills with background color */
204 public int missing_cap
= 0; /* Some capability is missing */
206 static int attrmode
= AT_NORMAL
;
210 static char *cheaper();
211 static void tmodes();
215 * These two variables are sometimes defined in,
216 * and needed by, the termcap library.
218 #if MUST_DEFINE_OSPEED
219 extern short ospeed
; /* Terminal output baud rate */
220 extern char PC
; /* Pad character */
227 extern int quiet
; /* If VERY_QUIET, use visual bell for bell */
228 extern int no_back_scroll
;
231 extern int quit_at_eof
;
232 extern int more_mode
;
233 extern int no_keypad
;
236 extern int screen_trashed
;
238 extern int top_scroll
;
241 extern int hilite_search
;
244 #ifndef HAVE_TERMCAP_H
245 extern char *tgetstr();
246 extern char *tgoto();
251 * Change terminal to "raw mode", or restore to "normal" mode.
253 * 1. An outstanding read will complete on receipt of a single keystroke.
254 * 2. Input is not echoed.
255 * 3. On output, \n is mapped to \r\n.
256 * 4. \t is NOT expanded into spaces.
257 * 5. Signal-causing characters such as ctrl-C (interrupt),
258 * etc. are NOT disabled.
259 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
265 static int curr_on
= 0;
269 erase2_char
= '\b'; /* in case OS doesn't know about erase2 */
270 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
273 static struct termios save_term
;
274 static int saved_term
= 0;
279 * Get terminal modes.
284 * Save modes and set certain variables dependent on modes.
292 switch (cfgetospeed(&s
))
295 case B0
: ospeed
= 0; break;
298 case B50
: ospeed
= 1; break;
301 case B75
: ospeed
= 2; break;
304 case B110
: ospeed
= 3; break;
307 case B134
: ospeed
= 4; break;
310 case B150
: ospeed
= 5; break;
313 case B200
: ospeed
= 6; break;
316 case B300
: ospeed
= 7; break;
319 case B600
: ospeed
= 8; break;
322 case B1200
: ospeed
= 9; break;
325 case B1800
: ospeed
= 10; break;
328 case B2400
: ospeed
= 11; break;
331 case B4800
: ospeed
= 12; break;
334 case B9600
: ospeed
= 13; break;
337 case EXTA
: ospeed
= 14; break;
340 case EXTB
: ospeed
= 15; break;
343 case B57600
: ospeed
= 16; break;
346 case B115200
: ospeed
= 17; break;
351 erase_char
= s
.c_cc
[VERASE
];
353 erase2_char
= s
.c_cc
[VERASE2
];
355 kill_char
= s
.c_cc
[VKILL
];
357 werase_char
= s
.c_cc
[VWERASE
];
359 werase_char
= CONTROL('W');
363 * Set the modes to the way we want them.
425 #if MUST_SET_LINE_DISCIPLINE
427 * System's termios is broken; need to explicitly
428 * request TERMIODISC line discipline.
430 s
.c_line
= TERMIODISC
;
435 * Restore saved modes.
442 tcsetattr(tty
, TCSADRAIN
, &s
);
443 #if MUST_SET_LINE_DISCIPLINE
447 * Broken termios *ignores* any line discipline
448 * except TERMIODISC. A different old line discipline
449 * is therefore not restored, yet. Restore the old
450 * line discipline by hand.
452 ioctl(tty
, TIOCSETD
, &save_term
.c_line
);
460 static struct termio save_term
;
461 static int saved_term
= 0;
466 * Get terminal modes.
468 ioctl(tty
, TCGETA
, &s
);
471 * Save modes and set certain variables dependent on modes.
479 ospeed
= s
.c_cflag
& CBAUD
;
481 erase_char
= s
.c_cc
[VERASE
];
482 kill_char
= s
.c_cc
[VKILL
];
484 werase_char
= s
.c_cc
[VWERASE
];
486 werase_char
= CONTROL('W');
490 * Set the modes to the way we want them.
492 s
.c_lflag
&= ~(ICANON
|ECHO
|ECHOE
|ECHOK
|ECHONL
);
493 s
.c_oflag
|= (OPOST
|ONLCR
|TAB3
);
494 s
.c_oflag
&= ~(OCRNL
|ONOCR
|ONLRET
);
500 * Restore saved modes.
504 ioctl(tty
, TCSETAW
, &s
);
510 static struct sgttyb save_term
;
511 static int saved_term
= 0;
516 * Get terminal modes.
518 ioctl(tty
, TIOCGETP
, &s
);
521 * Save modes and set certain variables dependent on modes.
529 ospeed
= s
.sg_ospeed
;
531 erase_char
= s
.sg_erase
;
532 kill_char
= s
.sg_kill
;
533 werase_char
= CONTROL('W');
536 * Set the modes to the way we want them.
538 s
.sg_flags
|= CBREAK
;
539 s
.sg_flags
&= ~(ECHO
|XTABS
);
543 * Restore saved modes.
547 ioctl(tty
, TIOCSETN
, &s
);
553 static struct sgbuf save_term
;
554 static int saved_term
= 0;
559 * Get terminal modes.
564 * Save modes and set certain variables dependent on modes.
571 erase_char
= s
.sg_bspch
;
572 kill_char
= s
.sg_dlnch
;
573 werase_char
= CONTROL('W');
576 * Set the modes to the way we want them.
585 * Restore saved modes.
592 /* MS-DOS, Windows, or OS2 */
595 LSIGNAL(SIGINT
, SIG_IGN
);
598 #if MSDOS_COMPILER==DJGPPC
599 kill_char
= CONTROL('U');
601 * So that when we shell out or run another program, its
602 * stdin is in cooked mode. We do not switch stdin to binary
603 * mode if fd0 is zero, since that means we were called before
604 * tty was reopened in open_getchr, in which case we would be
605 * changing the original stdin device outside less.
608 setmode(0, on
? O_BINARY
: O_TEXT
);
612 werase_char
= CONTROL('W');
622 * Some glue to prevent calling termcap functions if tgetent() failed.
633 s
= lgetenv("LESS_TERMCAP_DEBUG");
634 if (s
!= NULL
&& *s
!= '\0')
636 struct env
{ struct env
*next
; char *name
; char *value
; };
637 static struct env
*envs
= NULL
;
639 for (p
= envs
; p
!= NULL
; p
= p
->next
)
640 if (strcmp(p
->name
, capname
) == 0)
642 p
= (struct env
*) ecalloc(1, sizeof(struct env
));
643 p
->name
= save(capname
);
644 p
->value
= (char *) ecalloc(strlen(capname
)+3, sizeof(char));
645 sprintf(p
->value
, "<%s>", capname
);
650 strcpy(name
, "LESS_TERMCAP_");
651 strcat(name
, capname
);
652 return (lgetenv(name
));
661 if ((s
= ltget_env(capname
)) != NULL
)
662 return (*s
!= '\0' && *s
!= '0');
665 return (tgetflag(capname
));
674 if ((s
= ltget_env(capname
)) != NULL
)
678 return (tgetnum(capname
));
682 ltgetstr(capname
, pp
)
688 if ((s
= ltget_env(capname
)) != NULL
)
692 return (tgetstr(capname
, pp
));
694 #endif /* MSDOS_COMPILER */
697 * Get size of the output screen.
709 #define DEF_SC_WIDTH 80
711 #define DEF_SC_HEIGHT 25
713 #define DEF_SC_HEIGHT 24
717 sys_width
= sys_height
= 0;
719 #if MSDOS_COMPILER==MSOFTC
721 struct videoconfig w
;
723 sys_height
= w
.numtextrows
;
724 sys_width
= w
.numtextcols
;
727 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
731 sys_height
= w
.screenheight
;
732 sys_width
= w
.screenwidth
;
735 #if MSDOS_COMPILER==WIN32C
737 CONSOLE_SCREEN_BUFFER_INFO scr
;
738 GetConsoleScreenBufferInfo(con_out
, &scr
);
739 sys_height
= scr
.srWindow
.Bottom
- scr
.srWindow
.Top
+ 1;
740 sys_width
= scr
.srWindow
.Right
- scr
.srWindow
.Left
+ 1;
750 * When using terminal emulators for XFree86/OS2, the
751 * _scrsize function does not work well.
752 * Call the scrsize.exe program to get the window size.
754 windowid
= getenv("WINDOWID");
755 if (windowid
!= NULL
)
757 FILE *fd
= popen("scrsize", "rt");
761 fscanf(fd
, "%i %i", &w
, &h
);
775 if (ioctl(2, TIOCGWINSZ
, &w
) == 0)
778 sys_height
= w
.ws_row
;
780 sys_width
= w
.ws_col
;
787 if (ioctl(2, WIOCGETD
, &w
) == 0)
790 sys_height
= w
.uw_height
/ w
.uw_vs
;
792 sys_width
= w
.uw_width
/ w
.uw_hs
;
803 sc_height
= sys_height
;
804 else if ((s
= lgetenv("LINES")) != NULL
)
807 else if ((n
= ltgetnum("li")) > 0)
811 sc_height
= DEF_SC_HEIGHT
;
814 sc_width
= sys_width
;
815 else if ((s
= lgetenv("COLUMNS")) != NULL
)
818 else if ((n
= ltgetnum("co")) > 0)
822 sc_width
= DEF_SC_WIDTH
;
825 #if MSDOS_COMPILER==MSOFTC
827 * Figure out how many empty loops it takes to delay a millisecond.
835 * Get synchronized at the start of a tick.
838 while (clock() == start
)
841 * Now count loops till the next tick.
845 while (clock() == start
)
848 * Convert from (loops per clock) to (loops per millisecond).
850 msec_loops
*= CLOCKS_PER_SEC
;
855 * Delay for a specified number of milliseconds.
860 static long delay_dummy
= 0;
872 for (i
= 0; i
< msec_loops
; i
++)
875 * Make it look like we're doing something here,
876 * so the optimizer doesn't remove the whole loop.
885 * Return the characters actually input by a "special" key.
891 static char tbuf
[40];
893 #if MSDOS_COMPILER || OS2
894 static char k_right
[] = { '\340', PCK_RIGHT
, 0 };
895 static char k_left
[] = { '\340', PCK_LEFT
, 0 };
896 static char k_ctl_right
[] = { '\340', PCK_CTL_RIGHT
, 0 };
897 static char k_ctl_left
[] = { '\340', PCK_CTL_LEFT
, 0 };
898 static char k_insert
[] = { '\340', PCK_INSERT
, 0 };
899 static char k_delete
[] = { '\340', PCK_DELETE
, 0 };
900 static char k_ctl_delete
[] = { '\340', PCK_CTL_DELETE
, 0 };
901 static char k_ctl_backspace
[] = { '\177', 0 };
902 static char k_home
[] = { '\340', PCK_HOME
, 0 };
903 static char k_end
[] = { '\340', PCK_END
, 0 };
904 static char k_up
[] = { '\340', PCK_UP
, 0 };
905 static char k_down
[] = { '\340', PCK_DOWN
, 0 };
906 static char k_backtab
[] = { '\340', PCK_SHIFT_TAB
, 0 };
907 static char k_pagedown
[] = { '\340', PCK_PAGEDOWN
, 0 };
908 static char k_pageup
[] = { '\340', PCK_PAGEUP
, 0 };
909 static char k_f1
[] = { '\340', PCK_F1
, 0 };
919 * If windowid is not NULL, assume less is executed in
920 * the XFree86 environment.
923 s
= windowid
? ltgetstr("kr", &sp
) : k_right
;
926 s
= windowid
? ltgetstr("kl", &sp
) : k_left
;
929 s
= windowid
? ltgetstr("ku", &sp
) : k_up
;
932 s
= windowid
? ltgetstr("kd", &sp
) : k_down
;
935 s
= windowid
? ltgetstr("kP", &sp
) : k_pageup
;
938 s
= windowid
? ltgetstr("kN", &sp
) : k_pagedown
;
941 s
= windowid
? ltgetstr("kh", &sp
) : k_home
;
944 s
= windowid
? ltgetstr("@7", &sp
) : k_end
;
949 s
= ltgetstr("kD", &sp
);
989 #if MSDOS_COMPILER || OS2
993 case SK_CTL_LEFT_ARROW
:
996 case SK_CTL_RIGHT_ARROW
:
999 case SK_CTL_BACKSPACE
:
1000 s
= k_ctl_backspace
;
1012 case SK_RIGHT_ARROW
:
1013 s
= ltgetstr("kr", &sp
);
1016 s
= ltgetstr("kl", &sp
);
1019 s
= ltgetstr("ku", &sp
);
1022 s
= ltgetstr("kd", &sp
);
1025 s
= ltgetstr("kP", &sp
);
1028 s
= ltgetstr("kN", &sp
);
1031 s
= ltgetstr("kh", &sp
);
1034 s
= ltgetstr("@7", &sp
);
1037 s
= ltgetstr("kD", &sp
);
1047 tbuf
[0] = CONTROL('K');
1058 * Get terminal capabilities via termcap.
1069 * Set up default colors.
1070 * The xx_s_width and xx_e_width vars are already initialized to 0.
1072 #if MSDOS_COMPILER==MSOFTC
1073 sy_bg_color
= _getbkcolor();
1074 sy_fg_color
= _gettextcolor();
1077 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1081 sy_bg_color
= (w
.attribute
>> 4) & 0x0F;
1082 sy_fg_color
= (w
.attribute
>> 0) & 0x0F;
1085 #if MSDOS_COMPILER==WIN32C
1088 CONSOLE_SCREEN_BUFFER_INFO scr
;
1090 con_out_save
= con_out
= GetStdHandle(STD_OUTPUT_HANDLE
);
1092 * Always open stdin in binary. Note this *must* be done
1093 * before any file operations have been done on fd0.
1096 GetConsoleScreenBufferInfo(con_out
, &scr
);
1097 ReadConsoleOutputAttribute(con_out
, &curr_attr
,
1098 1, scr
.dwCursorPosition
, &nread
);
1099 sy_bg_color
= (curr_attr
& BG_COLORS
) >> 4; /* normalize */
1100 sy_fg_color
= curr_attr
& FG_COLORS
;
1105 nm_fg_color
= sy_fg_color
;
1106 nm_bg_color
= sy_bg_color
;
1117 * Get size of the screen.
1123 #else /* !MSDOS_COMPILER */
1126 register char *t1
, *t2
;
1128 char termbuf
[TERMBUF_SIZE
];
1130 static char sbuf
[TERMSBUF_SIZE
];
1134 * Make sure the termcap database is available.
1136 sp
= lgetenv("TERMCAP");
1137 if (sp
== NULL
|| *sp
== '\0')
1140 if ((sp
= homefile("termcap.dat")) != NULL
)
1142 termcap
= (char *) ecalloc(strlen(sp
)+9, sizeof(char));
1143 sprintf(termcap
, "TERMCAP=%s", sp
);
1150 * Find out what kind of terminal this is.
1152 if ((term
= lgetenv("TERM")) == NULL
)
1153 term
= DEFAULT_TERM
;
1155 if (tgetent(termbuf
, term
) != TGETENT_OK
)
1157 if (ltgetflag("hc"))
1161 * Get size of the screen.
1166 auto_wrap
= ltgetflag("am");
1167 ignaw
= ltgetflag("xn");
1168 above_mem
= ltgetflag("da");
1169 below_mem
= ltgetflag("db");
1170 clear_bg
= ltgetflag("ut");
1173 * Assumes termcap variable "sg" is the printing width of:
1174 * the standout sequence, the end standout sequence,
1175 * the underline sequence, the end underline sequence,
1176 * the boldface sequence, and the end boldface sequence.
1178 if ((so_s_width
= ltgetnum("sg")) < 0)
1180 so_e_width
= so_s_width
;
1182 bo_s_width
= bo_e_width
= so_s_width
;
1183 ul_s_width
= ul_e_width
= so_s_width
;
1184 bl_s_width
= bl_e_width
= so_s_width
;
1187 if (so_s_width
> 0 || so_e_width
> 0)
1189 * Disable highlighting by default on magic cookie terminals.
1190 * Turning on highlighting might change the displayed width
1191 * of a line, causing the display to get messed up.
1192 * The user can turn it back on with -g,
1193 * but she won't like the results.
1199 * Get various string-valued capabilities.
1204 sc_pad
= ltgetstr("pc", &sp
);
1209 sc_s_keypad
= ltgetstr("ks", &sp
);
1210 if (sc_s_keypad
== NULL
)
1212 sc_e_keypad
= ltgetstr("ke", &sp
);
1213 if (sc_e_keypad
== NULL
)
1217 * This loses for terminals with termcap entries with ti/te strings
1218 * that switch to/from an alternate screen, and we're in quit_at_eof
1221 if (quit_at_eof
!= OPT_ONPLUS
&& !more_mode
) {
1222 sc_init
= ltgetstr("ti", &sp
);
1223 sc_deinit
= ltgetstr("te", &sp
);
1229 if (sc_init
== NULL
)
1231 if (sc_deinit
== NULL
)
1234 sc_eol_clear
= ltgetstr("ce", &sp
);
1235 if (sc_eol_clear
== NULL
|| *sc_eol_clear
== '\0')
1241 sc_eos_clear
= ltgetstr("cd", &sp
);
1242 if (below_mem
&& (sc_eos_clear
== NULL
|| *sc_eos_clear
== '\0'))
1248 sc_clear
= ltgetstr("cl", &sp
);
1249 if (sc_clear
== NULL
|| *sc_clear
== '\0')
1255 sc_move
= ltgetstr("cm", &sp
);
1256 if (sc_move
== NULL
|| *sc_move
== '\0')
1259 * This is not an error here, because we don't
1260 * always need sc_move.
1261 * We need it only if we don't have home or lower-left.
1268 tmodes("so", "se", &sc_s_in
, &sc_s_out
, "", "", &sp
);
1269 tmodes("us", "ue", &sc_u_in
, &sc_u_out
, sc_s_in
, sc_s_out
, &sp
);
1270 tmodes("md", "me", &sc_b_in
, &sc_b_out
, sc_s_in
, sc_s_out
, &sp
);
1271 tmodes("mb", "me", &sc_bl_in
, &sc_bl_out
, sc_s_in
, sc_s_out
, &sp
);
1273 sc_visual_bell
= ltgetstr("vb", &sp
);
1274 if (sc_visual_bell
== NULL
)
1275 sc_visual_bell
= "";
1277 if (ltgetflag("bs"))
1278 sc_backspace
= "\b";
1281 sc_backspace
= ltgetstr("bc", &sp
);
1282 if (sc_backspace
== NULL
|| *sc_backspace
== '\0')
1283 sc_backspace
= "\b";
1287 * Choose between using "ho" and "cm" ("home" and "cursor move")
1288 * to move the cursor to the upper left corner of the screen.
1290 t1
= ltgetstr("ho", &sp
);
1293 if (*sc_move
== '\0')
1297 strcpy(sp
, tgoto(sc_move
, 0, 0));
1299 sp
+= strlen(sp
) + 1;
1301 sc_home
= cheaper(t1
, t2
, "|\b^");
1304 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1305 * to move the cursor to the lower left corner of the screen.
1307 t1
= ltgetstr("ll", &sp
);
1310 if (*sc_move
== '\0')
1314 strcpy(sp
, tgoto(sc_move
, 0, sc_height
-1));
1316 sp
+= strlen(sp
) + 1;
1318 sc_lower_left
= cheaper(t1
, t2
, "\r");
1321 * Get carriage return string.
1323 sc_return
= ltgetstr("cr", &sp
);
1324 if (sc_return
== NULL
)
1328 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1329 * to add a line at the top of the screen.
1331 t1
= ltgetstr("al", &sp
);
1334 t2
= ltgetstr("sr", &sp
);
1338 if (*t1
== '\0' && *t2
== '\0')
1345 sc_addline
= cheaper(t1
, t2
, "");
1346 if (*sc_addline
== '\0')
1349 * Force repaint on any backward movement.
1353 #endif /* MSDOS_COMPILER */
1358 * Return the cost of displaying a termcap string.
1359 * We use the trick of calling tputs, but as a char printing function
1360 * we give it inc_costcount, which just increments "costcount".
1361 * This tells us how many chars would be printed by using this string.
1362 * {{ Couldn't we just use strlen? }}
1364 static int costcount
;
1380 tputs(t
, sc_height
, inc_costcount
);
1385 * Return the "best" of the two given termcap strings.
1386 * The best, if both exist, is the one with the lower
1387 * cost (see cost() function).
1390 cheaper(t1
, t2
, def
)
1394 if (*t1
== '\0' && *t2
== '\0')
1403 if (cost(t1
) < cost(t2
))
1409 tmodes(incap
, outcap
, instr
, outstr
, def_instr
, def_outstr
, spp
)
1418 *instr
= ltgetstr(incap
, spp
);
1423 *outstr
= def_outstr
;
1427 *outstr
= ltgetstr(outcap
, spp
);
1428 if (*outstr
== NULL
)
1429 /* No specific out capability; use "me". */
1430 *outstr
= ltgetstr("me", spp
);
1431 if (*outstr
== NULL
)
1432 /* Don't even have "me"; use a null string. */
1436 #endif /* MSDOS_COMPILER */
1440 * Below are the functions which perform all the
1441 * terminal-specific screen manipulation.
1447 #if MSDOS_COMPILER==WIN32C
1449 _settextposition(int row
, int col
)
1452 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1454 GetConsoleScreenBufferInfo(con_out
, &csbi
);
1455 cpos
.X
= csbi
.srWindow
.Left
+ (col
- 1);
1456 cpos
.Y
= csbi
.srWindow
.Top
+ (row
- 1);
1457 SetConsoleCursorPosition(con_out
, cpos
);
1462 * Initialize the screen to the correct color at startup.
1467 SETCOLORS(nm_fg_color
, nm_bg_color
);
1470 * This clears the screen at startup. This is different from
1471 * the behavior of other versions of less. Disable it for now.
1478 * Create a complete, blank screen using "normal" colors.
1480 SETCOLORS(nm_fg_color
, nm_bg_color
);
1481 blanks
= (char *) ecalloc(width
+1, sizeof(char));
1482 for (col
= 0; col
< sc_width
; col
++)
1484 blanks
[sc_width
] = '\0';
1485 for (row
= 0; row
< sc_height
; row
++)
1492 #if MSDOS_COMPILER==WIN32C
1495 * Termcap-like init with a private win32 console.
1500 CONSOLE_SCREEN_BUFFER_INFO scr
;
1503 if (con_out_save
== INVALID_HANDLE_VALUE
)
1506 GetConsoleScreenBufferInfo(con_out_save
, &scr
);
1508 if (con_out_ours
== INVALID_HANDLE_VALUE
)
1511 * Create our own screen buffer, so that we
1512 * may restore the original when done.
1514 con_out_ours
= CreateConsoleScreenBuffer(
1515 GENERIC_WRITE
| GENERIC_READ
,
1516 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
1517 (LPSECURITY_ATTRIBUTES
) NULL
,
1518 CONSOLE_TEXTMODE_BUFFER
,
1522 size
.X
= scr
.srWindow
.Right
- scr
.srWindow
.Left
+ 1;
1523 size
.Y
= scr
.srWindow
.Bottom
- scr
.srWindow
.Top
+ 1;
1524 SetConsoleScreenBufferSize(con_out_ours
, size
);
1525 SetConsoleActiveScreenBuffer(con_out_ours
);
1526 con_out
= con_out_ours
;
1530 * Restore the startup console.
1535 if (con_out_save
== INVALID_HANDLE_VALUE
)
1538 (void) CloseHandle(con_out_ours
);
1539 SetConsoleActiveScreenBuffer(con_out_save
);
1540 con_out
= con_out_save
;
1546 * Initialize terminal
1553 tputs(sc_init
, sc_height
, putchr
);
1555 tputs(sc_s_keypad
, sc_height
, putchr
);
1561 * This is nice to terminals with no alternate screen,
1562 * but with saved scrolled-off-the-top lines. This way,
1563 * no previous line is lost, but we start with a whole
1564 * screen to ourself.
1566 for (i
= 1; i
< sc_height
; i
++)
1571 #if MSDOS_COMPILER==WIN32C
1582 * Deinitialize terminal
1591 tputs(sc_e_keypad
, sc_height
, putchr
);
1593 tputs(sc_deinit
, sc_height
, putchr
);
1595 /* Restore system colors. */
1596 SETCOLORS(sy_fg_color
, sy_bg_color
);
1597 #if MSDOS_COMPILER==WIN32C
1599 win32_deinit_term();
1601 /* Need clreol to make SETCOLORS take effect. */
1609 * Home cursor (move to upper left corner of screen).
1615 tputs(sc_home
, 1, putchr
);
1618 _settextposition(1,1);
1623 * Add a blank line (called with cursor at home).
1624 * Should scroll the display down.
1630 tputs(sc_addline
, sc_height
, putchr
);
1633 #if MSDOS_COMPILER==MSOFTC
1634 _scrolltextwindow(_GSCROLLDOWN
);
1635 _settextposition(1,1);
1637 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1638 movetext(1,1, sc_width
,sc_height
-1, 1,2);
1642 #if MSDOS_COMPILER==WIN32C
1645 SMALL_RECT rcSrc
, rcClip
;
1647 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1649 GetConsoleScreenBufferInfo(con_out
,&csbi
);
1651 /* The clip rectangle is the entire visible screen. */
1652 rcClip
.Left
= csbi
.srWindow
.Left
;
1653 rcClip
.Top
= csbi
.srWindow
.Top
;
1654 rcClip
.Right
= csbi
.srWindow
.Right
;
1655 rcClip
.Bottom
= csbi
.srWindow
.Bottom
;
1657 /* The source rectangle is the visible screen minus the last line. */
1661 /* Move the top left corner of the source window down one row. */
1662 new_org
.X
= rcSrc
.Left
;
1663 new_org
.Y
= rcSrc
.Top
+ 1;
1665 /* Fill the right character and attributes. */
1666 fillchar
.Char
.AsciiChar
= ' ';
1667 curr_attr
= MAKEATTR(nm_fg_color
, nm_bg_color
);
1668 fillchar
.Attributes
= curr_attr
;
1669 ScrollConsoleScreenBuffer(con_out
, &rcSrc
, &rcClip
, new_org
, &fillchar
);
1670 _settextposition(1,1);
1680 * Remove the n topmost lines and scroll everything below it in the
1681 * window upward. This is needed to stop leaking the topmost line
1682 * into the scrollback buffer when we go down-one-line (in WIN32).
1688 #if MSDOS_COMPILER==WIN32C
1689 SMALL_RECT rcSrc
, rcClip
;
1692 CONSOLE_SCREEN_BUFFER_INFO csbi
; /* to get buffer info */
1694 if (n
>= sc_height
- 1)
1703 GetConsoleScreenBufferInfo(con_out
, &csbi
);
1705 /* Get the extent of all-visible-rows-but-the-last. */
1706 rcSrc
.Left
= csbi
.srWindow
.Left
;
1707 rcSrc
.Top
= csbi
.srWindow
.Top
+ n
;
1708 rcSrc
.Right
= csbi
.srWindow
.Right
;
1709 rcSrc
.Bottom
= csbi
.srWindow
.Bottom
;
1711 /* Get the clip rectangle. */
1712 rcClip
.Left
= rcSrc
.Left
;
1713 rcClip
.Top
= csbi
.srWindow
.Top
;
1714 rcClip
.Right
= rcSrc
.Right
;
1715 rcClip
.Bottom
= rcSrc
.Bottom
;
1717 /* Move the source window up n rows. */
1718 new_org
.X
= rcSrc
.Left
;
1719 new_org
.Y
= rcSrc
.Top
- n
;
1721 /* Fill the right character and attributes. */
1722 fillchar
.Char
.AsciiChar
= ' ';
1723 curr_attr
= MAKEATTR(nm_fg_color
, nm_bg_color
);
1724 fillchar
.Attributes
= curr_attr
;
1726 ScrollConsoleScreenBuffer(con_out
, &rcSrc
, &rcClip
, new_org
, &fillchar
);
1728 /* Position cursor on first blank line. */
1729 goto_line(sc_height
- n
- 1);
1734 #if MSDOS_COMPILER==WIN32C
1742 * This will clear only the currently visible rows of the NT
1743 * console buffer, which means none of the precious scrollback
1744 * rows are touched making for faster scrolling. Note that, if
1745 * the window has fewer columns than the console buffer (i.e.
1746 * there is a horizontal scrollbar as well), the entire width
1747 * of the visible rows will be cleared.
1752 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1754 /* get the number of cells in the current buffer */
1755 GetConsoleScreenBufferInfo(con_out
, &csbi
);
1756 winsz
= csbi
.dwSize
.X
* (csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
+ 1);
1758 topleft
.Y
= csbi
.srWindow
.Top
;
1760 curr_attr
= MAKEATTR(nm_fg_color
, nm_bg_color
);
1761 FillConsoleOutputCharacter(con_out
, ' ', winsz
, topleft
, &nchars
);
1762 FillConsoleOutputAttribute(con_out
, curr_attr
, winsz
, topleft
, &nchars
);
1766 * Remove the n topmost lines and scroll everything below it in the
1773 SMALL_RECT rcSrc
, rcClip
;
1779 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1784 if (n
>= sc_height
- 1)
1787 _settextposition(1,1);
1791 /* Get the extent of what will remain visible after scrolling. */
1792 GetConsoleScreenBufferInfo(con_out
, &csbi
);
1793 rcSrc
.Left
= csbi
.srWindow
.Left
;
1794 rcSrc
.Top
= csbi
.srWindow
.Top
+ n
;
1795 rcSrc
.Right
= csbi
.srWindow
.Right
;
1796 rcSrc
.Bottom
= csbi
.srWindow
.Bottom
;
1798 /* Get the clip rectangle. */
1799 rcClip
.Left
= rcSrc
.Left
;
1800 rcClip
.Top
= csbi
.srWindow
.Top
;
1801 rcClip
.Right
= rcSrc
.Right
;
1802 rcClip
.Bottom
= rcSrc
.Bottom
;
1804 /* Move the source text to the top of the screen. */
1805 new_org
.X
= rcSrc
.Left
;
1806 new_org
.Y
= rcClip
.Top
;
1808 /* Fill the right character and attributes. */
1809 fillchar
.Char
.AsciiChar
= ' ';
1810 fillchar
.Attributes
= MAKEATTR(nm_fg_color
, nm_bg_color
);
1812 /* Scroll the window. */
1813 SetConsoleTextAttribute(con_out
, fillchar
.Attributes
);
1814 ScrollConsoleScreenBuffer(con_out
, &rcSrc
, &rcClip
, new_org
, &fillchar
);
1816 /* Clear remaining lines at bottom. */
1817 topleft
.X
= csbi
.dwCursorPosition
.X
;
1818 topleft
.Y
= rcSrc
.Bottom
- n
;
1819 size
= (n
* csbi
.dwSize
.X
) + (rcSrc
.Right
- topleft
.X
);
1820 FillConsoleOutputCharacter(con_out
, ' ', size
, topleft
,
1822 FillConsoleOutputAttribute(con_out
, fillchar
.Attributes
, size
, topleft
,
1824 SetConsoleTextAttribute(con_out
, curr_attr
);
1826 /* Move cursor n lines up from where it was. */
1827 csbi
.dwCursorPosition
.Y
-= n
;
1828 SetConsoleCursorPosition(con_out
, csbi
.dwCursorPosition
);
1833 * Move cursor to lower left corner of screen.
1839 tputs(sc_lower_left
, 1, putchr
);
1842 _settextposition(sc_height
, 1);
1847 * Move cursor to left position of current line.
1853 tputs(sc_return
, 1, putchr
);
1857 #if MSDOS_COMPILER==WIN32C
1859 CONSOLE_SCREEN_BUFFER_INFO scr
;
1860 GetConsoleScreenBufferInfo(con_out
, &scr
);
1861 row
= scr
.dwCursorPosition
.Y
- scr
.srWindow
.Top
+ 1;
1864 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1868 struct rccoord tpos
= _gettextposition();
1873 _settextposition(row
, 1);
1878 * Check if the console size has changed and reset internals
1879 * (in lieu of SIGWINCH for WIN32).
1884 #if MSDOS_COMPILER==WIN32C
1885 CONSOLE_SCREEN_BUFFER_INFO scr
;
1888 if (con_out
== INVALID_HANDLE_VALUE
)
1892 GetConsoleScreenBufferInfo(con_out
, &scr
);
1893 size
.Y
= scr
.srWindow
.Bottom
- scr
.srWindow
.Top
+ 1;
1894 size
.X
= scr
.srWindow
.Right
- scr
.srWindow
.Left
+ 1;
1895 if (size
.Y
!= sc_height
|| size
.X
!= sc_width
)
1899 if (!no_init
&& con_out_ours
== con_out
)
1900 SetConsoleScreenBufferSize(con_out
, size
);
1902 wscroll
= (sc_height
+ 1) / 2;
1909 * Goto a specific line on the screen.
1916 tputs(tgoto(sc_move
, 0, slinenum
), 1, putchr
);
1919 _settextposition(slinenum
+1, 1);
1923 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1925 * Create an alternate screen which is all white.
1926 * This screen is used to create a "flash" effect, by displaying it
1927 * briefly and then switching back to the normal screen.
1928 * {{ Yuck! There must be a better way to get a visual bell. }}
1933 #if MSDOS_COMPILER==MSOFTC
1934 struct videoconfig w
;
1938 _getvideoconfig(&w
);
1939 videopages
= w
.numvideopages
;
1942 at_enter(AT_STANDOUT
);
1947 at_enter(AT_STANDOUT
);
1948 blanks
= (char *) ecalloc(w
.numtextcols
, sizeof(char));
1949 for (col
= 0; col
< w
.numtextcols
; col
++)
1951 for (row
= w
.numtextrows
; row
> 0; row
--)
1952 _outmem(blanks
, w
.numtextcols
);
1959 #if MSDOS_COMPILER==BORLANDC
1962 whitescreen
= (unsigned short *)
1963 malloc(sc_width
* sc_height
* sizeof(short));
1964 if (whitescreen
== NULL
)
1966 for (n
= 0; n
< sc_width
* sc_height
; n
++)
1967 whitescreen
[n
] = 0x7020;
1969 #if MSDOS_COMPILER==WIN32C
1972 whitescreen
= (WORD
*)
1973 malloc(sc_height
* sc_width
* sizeof(WORD
));
1974 if (whitescreen
== NULL
)
1976 /* Invert the standard colors. */
1977 for (n
= 0; n
< sc_width
* sc_height
; n
++)
1978 whitescreen
[n
] = (WORD
)((nm_fg_color
<< 4) | nm_bg_color
);
1984 #endif /* MSDOS_COMPILER */
1987 * Output the "visual bell", if there is one.
1993 if (*sc_visual_bell
== '\0')
1995 tputs(sc_visual_bell
, sc_height
, putchr
);
1997 #if MSDOS_COMPILER==DJGPPC
2000 #if MSDOS_COMPILER==MSOFTC
2002 * Create a flash screen on the second video page.
2003 * Switch to that page, then switch back.
2013 #if MSDOS_COMPILER==BORLANDC
2014 unsigned short *currscreen
;
2017 * Get a copy of the current screen.
2018 * Display the flash screen.
2019 * Then restore the old screen.
2023 if (whitescreen
== NULL
)
2025 currscreen
= (unsigned short *)
2026 malloc(sc_width
* sc_height
* sizeof(short));
2027 if (currscreen
== NULL
) return;
2028 gettext(1, 1, sc_width
, sc_height
, currscreen
);
2029 puttext(1, 1, sc_width
, sc_height
, whitescreen
);
2031 puttext(1, 1, sc_width
, sc_height
, currscreen
);
2034 #if MSDOS_COMPILER==WIN32C
2035 /* paint screen with an inverse color */
2038 /* leave it displayed for 100 msec. */
2041 /* restore with a redraw */
2057 putchr(CONTROL('G'));
2059 #if MSDOS_COMPILER==WIN32C
2068 * Ring the terminal bell.
2073 if (quiet
== VERY_QUIET
)
2086 tputs(sc_clear
, sc_height
, putchr
);
2089 #if MSDOS_COMPILER==WIN32C
2092 _clearscreen(_GCLEARSCREEN
);
2098 * Clear from the cursor to the end of the cursor's line.
2099 * {{ This must not move the cursor. }}
2105 tputs(sc_eol_clear
, 1, putchr
);
2107 #if MSDOS_COMPILER==MSOFTC
2110 struct rccoord tpos
;
2114 * Save current state.
2116 tpos
= _gettextposition();
2117 _gettextwindow(&top
, &left
, &bot
, &right
);
2119 * Set a temporary window to the current line,
2120 * from the cursor's position to the right edge of the screen.
2121 * Then clear that window.
2123 _settextwindow(tpos
.row
, tpos
.col
, tpos
.row
, sc_width
);
2124 _clearscreen(_GWINDOW
);
2128 _settextwindow(top
, left
, bot
, right
);
2129 _settextposition(tpos
.row
, tpos
.col
);
2131 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2135 #if MSDOS_COMPILER==WIN32C
2138 CONSOLE_SCREEN_BUFFER_INFO scr
;
2141 memset(&scr
, 0, sizeof(scr
));
2142 GetConsoleScreenBufferInfo(con_out
, &scr
);
2143 cpos
.X
= scr
.dwCursorPosition
.X
;
2144 cpos
.Y
= scr
.dwCursorPosition
.Y
;
2145 curr_attr
= MAKEATTR(nm_fg_color
, nm_bg_color
);
2146 FillConsoleOutputAttribute(con_out
, curr_attr
,
2147 scr
.dwSize
.X
- cpos
.X
, cpos
, &nchars
);
2148 FillConsoleOutputCharacter(con_out
, ' ',
2149 scr
.dwSize
.X
- cpos
.X
, cpos
, &nchars
);
2157 * Clear the current line.
2158 * Clear the screen if there's off-screen memory below the display.
2167 tputs(sc_eos_clear
, 1, putchr
);
2169 tputs(sc_eol_clear
, 1, putchr
);
2174 * Clear the bottom line of the display.
2175 * Leave the cursor at the beginning of the bottom line.
2181 * If we're in a non-normal attribute mode, temporarily exit
2182 * the mode while we do the clear. Some terminals fill the
2183 * cleared area with the current attribute.
2190 if (attrmode
== AT_NORMAL
)
2194 int saved_attrmode
= attrmode
;
2198 at_enter(saved_attrmode
);
2206 attr
= apply_at_specials(attr
);
2209 /* The one with the most priority is last. */
2210 if (attr
& AT_UNDERLINE
)
2211 tputs(sc_u_in
, 1, putchr
);
2213 tputs(sc_b_in
, 1, putchr
);
2214 if (attr
& AT_BLINK
)
2215 tputs(sc_bl_in
, 1, putchr
);
2216 if (attr
& AT_STANDOUT
)
2217 tputs(sc_s_in
, 1, putchr
);
2220 /* The one with the most priority is first. */
2221 if (attr
& AT_STANDOUT
)
2223 SETCOLORS(so_fg_color
, so_bg_color
);
2224 } else if (attr
& AT_BLINK
)
2226 SETCOLORS(bl_fg_color
, bl_bg_color
);
2228 else if (attr
& AT_BOLD
)
2230 SETCOLORS(bo_fg_color
, bo_bg_color
);
2232 else if (attr
& AT_UNDERLINE
)
2234 SETCOLORS(ul_fg_color
, ul_bg_color
);
2245 /* Undo things in the reverse order we did them. */
2246 if (attrmode
& AT_STANDOUT
)
2247 tputs(sc_s_out
, 1, putchr
);
2248 if (attrmode
& AT_BLINK
)
2249 tputs(sc_bl_out
, 1, putchr
);
2250 if (attrmode
& AT_BOLD
)
2251 tputs(sc_b_out
, 1, putchr
);
2252 if (attrmode
& AT_UNDERLINE
)
2253 tputs(sc_u_out
, 1, putchr
);
2256 SETCOLORS(nm_fg_color
, nm_bg_color
);
2259 attrmode
= AT_NORMAL
;
2266 int new_attrmode
= apply_at_specials(attr
);
2267 int ignore_modes
= AT_ANSI
;
2269 if ((new_attrmode
& ~ignore_modes
) != (attrmode
& ~ignore_modes
))
2277 is_at_equiv(attr1
, attr2
)
2281 attr1
= apply_at_specials(attr1
);
2282 attr2
= apply_at_specials(attr2
);
2284 return (attr1
== attr2
);
2288 apply_at_specials(attr
)
2291 if (attr
& AT_BINARY
)
2293 if (attr
& AT_HILITE
)
2294 attr
|= AT_STANDOUT
;
2295 attr
&= ~(AT_BINARY
|AT_HILITE
);
2300 #if 0 /* No longer used */
2302 * Erase the character to the left of the cursor
2303 * and move the cursor left.
2310 * Erase the previous character by overstriking with a space.
2312 tputs(sc_backspace
, 1, putchr
);
2314 tputs(sc_backspace
, 1, putchr
);
2316 #if MSDOS_COMPILER==MSOFTC
2317 struct rccoord tpos
;
2320 tpos
= _gettextposition();
2323 _settextposition(tpos
.row
, tpos
.col
-1);
2325 _settextposition(tpos
.row
, tpos
.col
-1);
2327 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2330 #if MSDOS_COMPILER==WIN32C
2333 CONSOLE_SCREEN_BUFFER_INFO scr
;
2336 GetConsoleScreenBufferInfo(con_out
, &scr
);
2337 cpos
= scr
.dwCursorPosition
;
2341 SetConsoleCursorPosition(con_out
, cpos
);
2342 FillConsoleOutputCharacter(con_out
, (TCHAR
)' ', 1, cpos
, &cChars
);
2343 SetConsoleCursorPosition(con_out
, cpos
);
2352 * Output a plain backspace, without erasing the previous char.
2358 tputs(sc_backspace
, 1, putchr
);
2364 #if MSDOS_COMPILER==MSOFTC
2365 struct rccoord tpos
;
2366 tpos
= _gettextposition();
2370 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2374 #if MSDOS_COMPILER==WIN32C
2375 CONSOLE_SCREEN_BUFFER_INFO scr
;
2376 GetConsoleScreenBufferInfo(con_out
, &scr
);
2377 row
= scr
.dwCursorPosition
.Y
- scr
.srWindow
.Top
+ 1;
2378 col
= scr
.dwCursorPosition
.X
- scr
.srWindow
.Left
+ 1;
2385 _settextposition(row
, col
-1);
2386 #endif /* MSDOS_COMPILER */
2389 #if MSDOS_COMPILER==WIN32C
2391 * Determine whether an input character is waiting to be read.
2403 currentKey
.ascii
= 0;
2404 currentKey
.scan
= 0;
2407 * Wait for a real key-down event, but
2408 * ignore SHIFT and CONTROL key events.
2412 PeekConsoleInput(tty
, &ip
, 1, &read
);
2415 ReadConsoleInput(tty
, &ip
, 1, &read
);
2416 } while (ip
.EventType
!= KEY_EVENT
||
2417 ip
.Event
.KeyEvent
.bKeyDown
!= TRUE
||
2418 ip
.Event
.KeyEvent
.wVirtualScanCode
== 0 ||
2419 ip
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
||
2420 ip
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
||
2421 ip
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
);
2423 currentKey
.ascii
= ip
.Event
.KeyEvent
.uChar
.AsciiChar
;
2424 currentKey
.scan
= ip
.Event
.KeyEvent
.wVirtualScanCode
;
2425 keyCount
= ip
.Event
.KeyEvent
.wRepeatCount
;
2427 if (ip
.Event
.KeyEvent
.dwControlKeyState
&
2428 (LEFT_ALT_PRESSED
| RIGHT_ALT_PRESSED
))
2430 switch (currentKey
.scan
)
2432 case PCK_ALT_E
: /* letter 'E' */
2433 currentKey
.ascii
= 0;
2436 } else if (ip
.Event
.KeyEvent
.dwControlKeyState
&
2437 (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))
2439 switch (currentKey
.scan
)
2441 case PCK_RIGHT
: /* right arrow */
2442 currentKey
.scan
= PCK_CTL_RIGHT
;
2444 case PCK_LEFT
: /* left arrow */
2445 currentKey
.scan
= PCK_CTL_LEFT
;
2447 case PCK_DELETE
: /* delete */
2448 currentKey
.scan
= PCK_CTL_DELETE
;
2456 * Read a character from the keyboard.
2464 if (pending_scancode
)
2466 pending_scancode
= 0;
2467 return ((char)(currentKey
.scan
& 0x00FF));
2470 while (win32_kbhit((HANDLE
)tty
) == FALSE
)
2478 ascii
= currentKey
.ascii
;
2480 * On PC's, the extended keys return a 2 byte sequence beginning
2481 * with '00', so if the ascii code is 00, the next byte will be
2482 * the lsb of the scan code.
2484 pending_scancode
= (ascii
== 0x00);
2485 return ((char)ascii
);
2493 WIN32setcolors(fg
, bg
)
2503 WIN32textout(text
, len
)
2507 #if MSDOS_COMPILER==WIN32C
2509 WriteConsole(con_out
, text
, len
, &written
, NULL
);