2 * Copyright (C) 1984-2017 Mark Nudelman
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.
7 * For more information, see the README file.
12 * Routines which deal with the characteristics of the terminal.
13 * Uses termcap to be as terminal-independent as possible.
21 #if MSDOS_COMPILER==MSOFTC
24 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
26 #if MSDOS_COMPILER==DJGPPC
31 #if MSDOS_COMPILER==WIN32C
41 #include <sys/ioctl.h>
44 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
65 #include <sys/signal.h>
69 #include <sys/stream.h>
75 #endif /* MSDOS_COMPILER */
78 * Check for broken termios package that forces you to manually
79 * set the line discipline.
82 #define MUST_SET_LINE_DISCIPLINE 1
84 #define MUST_SET_LINE_DISCIPLINE 0
88 #define DEFAULT_TERM "ansi"
89 static char *windowid;
91 #define DEFAULT_TERM "unknown"
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); }
101 #if MSDOS_COMPILER==BORLANDC
102 static unsigned short *whitescreen;
103 static int flash_created = 0;
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;
113 #if MSDOS_COMPILER==WIN32C
120 static int keyCount = 0;
121 static WORD curr_attr;
122 static int pending_scancode = 0;
123 static WORD *whitescreen;
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 */
131 static void win32_init_term();
132 static void win32_deinit_term();
134 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
135 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 4
138 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
139 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
140 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
141 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \
142 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
143 error("SETCOLORS failed", NULL_PARG); }
147 public int nm_fg_color; /* Color of normal text */
148 public int nm_bg_color;
149 public int bo_fg_color; /* Color of bold text */
150 public int bo_bg_color;
151 public int ul_fg_color; /* Color of underlined text */
152 public int ul_bg_color;
153 public int so_fg_color; /* Color of standout text */
154 public int so_bg_color;
155 public int bl_fg_color; /* Color of blinking text */
156 public int bl_bg_color;
157 static int sy_fg_color; /* Color of system text (before less) */
158 static int sy_bg_color;
159 public int sgr_mode; /* Honor ANSI sequences rather than using above */
160 #if MSDOS_COMPILER==WIN32C
161 public int have_ul; /* Is underline available? */
166 * Strings passed to tputs() to do various terminal functions.
169 *sc_pad, /* Pad string */
170 *sc_home, /* Cursor home */
171 *sc_addline, /* Add line, scroll down following lines */
172 *sc_lower_left, /* Cursor to last line, first column */
173 *sc_return, /* Cursor to beginning of current line */
174 *sc_move, /* General cursor positioning */
175 *sc_clear, /* Clear screen */
176 *sc_eol_clear, /* Clear to end of line */
177 *sc_eos_clear, /* Clear to end of screen */
178 *sc_s_in, /* Enter standout (highlighted) mode */
179 *sc_s_out, /* Exit standout mode */
180 *sc_u_in, /* Enter underline mode */
181 *sc_u_out, /* Exit underline mode */
182 *sc_b_in, /* Enter bold mode */
183 *sc_b_out, /* Exit bold mode */
184 *sc_bl_in, /* Enter blink mode */
185 *sc_bl_out, /* Exit blink mode */
186 *sc_visual_bell, /* Visual bell (flash screen) sequence */
187 *sc_backspace, /* Backspace cursor */
188 *sc_s_keypad, /* Start keypad mode */
189 *sc_e_keypad, /* End keypad mode */
190 *sc_init, /* Startup terminal initialization */
191 *sc_deinit; /* Exit terminal de-initialization */
194 static int init_done = 0;
196 public int auto_wrap; /* Terminal does \r\n when write past margin */
197 public int ignaw; /* Terminal ignores \n immediately after wrap */
198 public int erase_char; /* The user's erase char */
199 public int erase2_char; /* The user's other erase char */
200 public int kill_char; /* The user's line-kill char */
201 public int werase_char; /* The user's word-erase char */
202 public int sc_width, sc_height; /* Height & width of screen */
203 public int bo_s_width, bo_e_width; /* Printing width of boldface seq */
204 public int ul_s_width, ul_e_width; /* Printing width of underline seq */
205 public int so_s_width, so_e_width; /* Printing width of standout seq */
206 public int bl_s_width, bl_e_width; /* Printing width of blink seq */
207 public int above_mem, below_mem; /* Memory retained above/below screen */
208 public int can_goto_line; /* Can move cursor to any line */
209 public int clear_bg; /* Clear fills with background color */
210 public int missing_cap = 0; /* Some capability is missing */
211 public char *kent = NULL; /* Keypad ENTER sequence */
213 static int attrmode = AT_NORMAL;
215 extern int line_count;
218 static char *cheaper();
219 static void tmodes();
223 * These two variables are sometimes defined in,
224 * and needed by, the termcap library.
226 #if MUST_DEFINE_OSPEED
227 extern short ospeed; /* Terminal output baud rate */
228 extern char PC; /* Pad character */
235 extern int quiet; /* If VERY_QUIET, use visual bell for bell */
236 extern int no_back_scroll;
239 extern int no_keypad;
242 extern int screen_trashed;
244 extern int top_scroll;
245 extern int quit_if_one_screen;
248 extern int hilite_search;
251 extern char *tgetstr();
252 extern char *tgoto();
256 * Change terminal to "raw mode", or restore to "normal" mode.
258 * 1. An outstanding read will complete on receipt of a single keystroke.
259 * 2. Input is not echoed.
260 * 3. On output, \n is mapped to \r\n.
261 * 4. \t is NOT expanded into spaces.
262 * 5. Signal-causing characters such as ctrl-C (interrupt),
263 * etc. are NOT disabled.
264 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
270 static int curr_on = 0;
274 erase2_char = '\b'; /* in case OS doesn't know about erase2 */
275 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
278 static struct termios save_term;
279 static int saved_term = 0;
284 * Get terminal modes.
289 * Save modes and set certain variables dependent on modes.
297 switch (cfgetospeed(&s))
300 case B0: ospeed = 0; break;
303 case B50: ospeed = 1; break;
306 case B75: ospeed = 2; break;
309 case B110: ospeed = 3; break;
312 case B134: ospeed = 4; break;
315 case B150: ospeed = 5; break;
318 case B200: ospeed = 6; break;
321 case B300: ospeed = 7; break;
324 case B600: ospeed = 8; break;
327 case B1200: ospeed = 9; break;
330 case B1800: ospeed = 10; break;
333 case B2400: ospeed = 11; break;
336 case B4800: ospeed = 12; break;
339 case B9600: ospeed = 13; break;
342 case EXTA: ospeed = 14; break;
345 case EXTB: ospeed = 15; break;
348 case B57600: ospeed = 16; break;
351 case B115200: ospeed = 17; break;
356 erase_char = s.c_cc[VERASE];
358 erase2_char = s.c_cc[VERASE2];
360 kill_char = s.c_cc[VKILL];
362 werase_char = s.c_cc[VWERASE];
364 werase_char = CONTROL('W');
368 * Set the modes to the way we want them.
430 #if MUST_SET_LINE_DISCIPLINE
432 * System's termios is broken; need to explicitly
433 * request TERMIODISC line discipline.
435 s.c_line = TERMIODISC;
440 * Restore saved modes.
447 tcsetattr(tty, TCSADRAIN, &s);
448 #if MUST_SET_LINE_DISCIPLINE
452 * Broken termios *ignores* any line discipline
453 * except TERMIODISC. A different old line discipline
454 * is therefore not restored, yet. Restore the old
455 * line discipline by hand.
457 ioctl(tty, TIOCSETD, &save_term.c_line);
465 static struct termio save_term;
466 static int saved_term = 0;
471 * Get terminal modes.
473 ioctl(tty, TCGETA, &s);
476 * Save modes and set certain variables dependent on modes.
484 ospeed = s.c_cflag & CBAUD;
486 erase_char = s.c_cc[VERASE];
487 kill_char = s.c_cc[VKILL];
489 werase_char = s.c_cc[VWERASE];
491 werase_char = CONTROL('W');
495 * Set the modes to the way we want them.
497 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
498 s.c_oflag |= (OPOST|ONLCR|TAB3);
499 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
505 * Restore saved modes.
509 ioctl(tty, TCSETAW, &s);
515 static struct sgttyb save_term;
516 static int saved_term = 0;
521 * Get terminal modes.
523 ioctl(tty, TIOCGETP, &s);
526 * Save modes and set certain variables dependent on modes.
534 ospeed = s.sg_ospeed;
536 erase_char = s.sg_erase;
537 kill_char = s.sg_kill;
538 werase_char = CONTROL('W');
541 * Set the modes to the way we want them.
543 s.sg_flags |= CBREAK;
544 s.sg_flags &= ~(ECHO|XTABS);
548 * Restore saved modes.
552 ioctl(tty, TIOCSETN, &s);
558 static struct sgbuf save_term;
559 static int saved_term = 0;
564 * Get terminal modes.
569 * Save modes and set certain variables dependent on modes.
576 erase_char = s.sg_bspch;
577 kill_char = s.sg_dlnch;
578 werase_char = CONTROL('W');
581 * Set the modes to the way we want them.
590 * Restore saved modes.
597 /* MS-DOS, Windows, or OS2 */
600 LSIGNAL(SIGINT, SIG_IGN);
603 #if MSDOS_COMPILER==DJGPPC
604 kill_char = CONTROL('U');
606 * So that when we shell out or run another program, its
607 * stdin is in cooked mode. We do not switch stdin to binary
608 * mode if fd0 is zero, since that means we were called before
609 * tty was reopened in open_getchr, in which case we would be
610 * changing the original stdin device outside less.
613 setmode(0, on ? O_BINARY : O_TEXT);
617 werase_char = CONTROL('W');
627 * Some glue to prevent calling termcap functions if tgetent() failed.
638 s = lgetenv("LESS_TERMCAP_DEBUG");
639 if (s != NULL && *s != '\0')
641 struct env { struct env *next; char *name; char *value; };
642 static struct env *envs = NULL;
644 for (p = envs; p != NULL; p = p->next)
645 if (strcmp(p->name, capname) == 0)
647 p = (struct env *) ecalloc(1, sizeof(struct env));
648 p->name = save(capname);
649 p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
650 sprintf(p->value, "<%s>", capname);
655 strcpy(name, "LESS_TERMCAP_");
656 strcat(name, capname);
657 return (lgetenv(name));
666 if ((s = ltget_env(capname)) != NULL)
667 return (*s != '\0' && *s != '0');
670 return (tgetflag(capname));
679 if ((s = ltget_env(capname)) != NULL)
683 return (tgetnum(capname));
687 ltgetstr(capname, pp)
693 if ((s = ltget_env(capname)) != NULL)
697 return (tgetstr(capname, pp));
699 #endif /* MSDOS_COMPILER */
702 * Get size of the output screen.
714 #define DEF_SC_WIDTH 80
716 #define DEF_SC_HEIGHT 25
718 #define DEF_SC_HEIGHT 24
722 sys_width = sys_height = 0;
724 #if MSDOS_COMPILER==MSOFTC
726 struct videoconfig w;
728 sys_height = w.numtextrows;
729 sys_width = w.numtextcols;
732 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
736 sys_height = w.screenheight;
737 sys_width = w.screenwidth;
740 #if MSDOS_COMPILER==WIN32C
742 CONSOLE_SCREEN_BUFFER_INFO scr;
743 GetConsoleScreenBufferInfo(con_out, &scr);
744 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
745 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
755 * When using terminal emulators for XFree86/OS2, the
756 * _scrsize function does not work well.
757 * Call the scrsize.exe program to get the window size.
759 windowid = getenv("WINDOWID");
760 if (windowid != NULL)
762 FILE *fd = popen("scrsize", "rt");
766 fscanf(fd, "%i %i", &w, &h);
780 if (ioctl(2, TIOCGWINSZ, &w) == 0)
783 sys_height = w.ws_row;
785 sys_width = w.ws_col;
792 if (ioctl(2, WIOCGETD, &w) == 0)
795 sys_height = w.uw_height / w.uw_vs;
797 sys_width = w.uw_width / w.uw_hs;
808 sc_height = sys_height;
809 else if ((s = lgetenv("LINES")) != NULL)
812 else if ((n = ltgetnum("li")) > 0)
816 sc_height = DEF_SC_HEIGHT;
819 sc_width = sys_width;
820 else if ((s = lgetenv("COLUMNS")) != NULL)
823 else if ((n = ltgetnum("co")) > 0)
827 sc_width = DEF_SC_WIDTH;
830 #if MSDOS_COMPILER==MSOFTC
832 * Figure out how many empty loops it takes to delay a millisecond.
840 * Get synchronized at the start of a tick.
843 while (clock() == start)
846 * Now count loops till the next tick.
850 while (clock() == start)
853 * Convert from (loops per clock) to (loops per millisecond).
855 msec_loops *= CLOCKS_PER_SEC;
860 * Delay for a specified number of milliseconds.
865 static long delay_dummy = 0;
877 for (i = 0; i < msec_loops; i++)
880 * Make it look like we're doing something here,
881 * so the optimizer doesn't remove the whole loop.
890 * Return the characters actually input by a "special" key.
896 static char tbuf[40];
898 #if MSDOS_COMPILER || OS2
899 static char k_right[] = { '\340', PCK_RIGHT, 0 };
900 static char k_left[] = { '\340', PCK_LEFT, 0 };
901 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
902 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
903 static char k_insert[] = { '\340', PCK_INSERT, 0 };
904 static char k_delete[] = { '\340', PCK_DELETE, 0 };
905 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
906 static char k_ctl_backspace[] = { '\177', 0 };
907 static char k_home[] = { '\340', PCK_HOME, 0 };
908 static char k_end[] = { '\340', PCK_END, 0 };
909 static char k_up[] = { '\340', PCK_UP, 0 };
910 static char k_down[] = { '\340', PCK_DOWN, 0 };
911 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
912 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
913 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
914 static char k_f1[] = { '\340', PCK_F1, 0 };
924 * If windowid is not NULL, assume less is executed in
925 * the XFree86 environment.
928 s = windowid ? ltgetstr("kr", &sp) : k_right;
931 s = windowid ? ltgetstr("kl", &sp) : k_left;
934 s = windowid ? ltgetstr("ku", &sp) : k_up;
937 s = windowid ? ltgetstr("kd", &sp) : k_down;
940 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
943 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
946 s = windowid ? ltgetstr("kh", &sp) : k_home;
949 s = windowid ? ltgetstr("@7", &sp) : k_end;
954 s = ltgetstr("kD", &sp);
994 #if MSDOS_COMPILER || OS2
998 case SK_CTL_LEFT_ARROW:
1001 case SK_CTL_RIGHT_ARROW:
1004 case SK_CTL_BACKSPACE:
1005 s = k_ctl_backspace;
1017 case SK_RIGHT_ARROW:
1018 s = ltgetstr("kr", &sp);
1021 s = ltgetstr("kl", &sp);
1024 s = ltgetstr("ku", &sp);
1027 s = ltgetstr("kd", &sp);
1030 s = ltgetstr("kP", &sp);
1033 s = ltgetstr("kN", &sp);
1036 s = ltgetstr("kh", &sp);
1039 s = ltgetstr("@7", &sp);
1042 s = ltgetstr("kD", &sp);
1052 tbuf[0] = CONTROL('K');
1063 * Get terminal capabilities via termcap.
1074 * Set up default colors.
1075 * The xx_s_width and xx_e_width vars are already initialized to 0.
1077 #if MSDOS_COMPILER==MSOFTC
1078 sy_bg_color = _getbkcolor();
1079 sy_fg_color = _gettextcolor();
1082 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1086 sy_bg_color = (w.attribute >> 4) & 0x0F;
1087 sy_fg_color = (w.attribute >> 0) & 0x0F;
1090 #if MSDOS_COMPILER==WIN32C
1093 CONSOLE_SCREEN_BUFFER_INFO scr;
1095 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1097 * Always open stdin in binary. Note this *must* be done
1098 * before any file operations have been done on fd0.
1101 GetConsoleScreenBufferInfo(con_out, &scr);
1102 ReadConsoleOutputAttribute(con_out, &curr_attr,
1103 1, scr.dwCursorPosition, &nread);
1104 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1105 sy_fg_color = curr_attr & FG_COLORS;
1110 nm_fg_color = sy_fg_color;
1111 nm_bg_color = sy_bg_color;
1123 * Get size of the screen.
1129 #else /* !MSDOS_COMPILER */
1134 char termbuf[TERMBUF_SIZE];
1136 static char sbuf[TERMSBUF_SIZE];
1140 * Make sure the termcap database is available.
1142 sp = lgetenv("TERMCAP");
1143 if (sp == NULL || *sp == '\0')
1146 if ((sp = homefile("termcap.dat")) != NULL)
1148 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1149 sprintf(termcap, "TERMCAP=%s", sp);
1156 * Find out what kind of terminal this is.
1158 if ((term = lgetenv("TERM")) == NULL)
1159 term = DEFAULT_TERM;
1161 if (tgetent(termbuf, term) != TGETENT_OK)
1163 if (ltgetflag("hc"))
1167 * Get size of the screen.
1172 auto_wrap = ltgetflag("am");
1173 ignaw = ltgetflag("xn");
1174 above_mem = ltgetflag("da");
1175 below_mem = ltgetflag("db");
1176 clear_bg = ltgetflag("ut");
1179 * Assumes termcap variable "sg" is the printing width of:
1180 * the standout sequence, the end standout sequence,
1181 * the underline sequence, the end underline sequence,
1182 * the boldface sequence, and the end boldface sequence.
1184 if ((so_s_width = ltgetnum("sg")) < 0)
1186 so_e_width = so_s_width;
1188 bo_s_width = bo_e_width = so_s_width;
1189 ul_s_width = ul_e_width = so_s_width;
1190 bl_s_width = bl_e_width = so_s_width;
1193 if (so_s_width > 0 || so_e_width > 0)
1195 * Disable highlighting by default on magic cookie terminals.
1196 * Turning on highlighting might change the displayed width
1197 * of a line, causing the display to get messed up.
1198 * The user can turn it back on with -g,
1199 * but she won't like the results.
1205 * Get various string-valued capabilities.
1210 sc_pad = ltgetstr("pc", &sp);
1215 sc_s_keypad = ltgetstr("ks", &sp);
1216 if (sc_s_keypad == NULL)
1218 sc_e_keypad = ltgetstr("ke", &sp);
1219 if (sc_e_keypad == NULL)
1221 kent = ltgetstr("@8", &sp);
1223 sc_init = ltgetstr("ti", &sp);
1224 if (sc_init == NULL)
1227 sc_deinit= ltgetstr("te", &sp);
1228 if (sc_deinit == NULL)
1231 sc_eol_clear = ltgetstr("ce", &sp);
1232 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1238 sc_eos_clear = ltgetstr("cd", &sp);
1239 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1245 sc_clear = ltgetstr("cl", &sp);
1246 if (sc_clear == NULL || *sc_clear == '\0')
1252 sc_move = ltgetstr("cm", &sp);
1253 if (sc_move == NULL || *sc_move == '\0')
1256 * This is not an error here, because we don't
1257 * always need sc_move.
1258 * We need it only if we don't have home or lower-left.
1265 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1266 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1267 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1268 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1270 sc_visual_bell = ltgetstr("vb", &sp);
1271 if (sc_visual_bell == NULL)
1272 sc_visual_bell = "";
1274 if (ltgetflag("bs"))
1275 sc_backspace = "\b";
1278 sc_backspace = ltgetstr("bc", &sp);
1279 if (sc_backspace == NULL || *sc_backspace == '\0')
1280 sc_backspace = "\b";
1284 * Choose between using "ho" and "cm" ("home" and "cursor move")
1285 * to move the cursor to the upper left corner of the screen.
1287 t1 = ltgetstr("ho", &sp);
1290 if (*sc_move == '\0')
1294 strcpy(sp, tgoto(sc_move, 0, 0));
1296 sp += strlen(sp) + 1;
1298 sc_home = cheaper(t1, t2, "|\b^");
1301 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1302 * to move the cursor to the lower left corner of the screen.
1304 t1 = ltgetstr("ll", &sp);
1307 if (*sc_move == '\0')
1311 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1313 sp += strlen(sp) + 1;
1315 sc_lower_left = cheaper(t1, t2, "\r");
1318 * Get carriage return string.
1320 sc_return = ltgetstr("cr", &sp);
1321 if (sc_return == NULL)
1325 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1326 * to add a line at the top of the screen.
1328 t1 = ltgetstr("al", &sp);
1331 t2 = ltgetstr("sr", &sp);
1335 if (*t1 == '\0' && *t2 == '\0')
1342 sc_addline = cheaper(t1, t2, "");
1343 if (*sc_addline == '\0')
1346 * Force repaint on any backward movement.
1350 #endif /* MSDOS_COMPILER */
1355 * Return the cost of displaying a termcap string.
1356 * We use the trick of calling tputs, but as a char printing function
1357 * we give it inc_costcount, which just increments "costcount".
1358 * This tells us how many chars would be printed by using this string.
1359 * {{ Couldn't we just use strlen? }}
1361 static int costcount;
1377 tputs(t, sc_height, inc_costcount);
1382 * Return the "best" of the two given termcap strings.
1383 * The best, if both exist, is the one with the lower
1384 * cost (see cost() function).
1387 cheaper(t1, t2, def)
1391 if (*t1 == '\0' && *t2 == '\0')
1400 if (cost(t1) < cost(t2))
1406 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1415 *instr = ltgetstr(incap, spp);
1420 *outstr = def_outstr;
1424 *outstr = ltgetstr(outcap, spp);
1425 if (*outstr == NULL)
1426 /* No specific out capability; use "me". */
1427 *outstr = ltgetstr("me", spp);
1428 if (*outstr == NULL)
1429 /* Don't even have "me"; use a null string. */
1433 #endif /* MSDOS_COMPILER */
1437 * Below are the functions which perform all the
1438 * terminal-specific screen manipulation.
1444 #if MSDOS_COMPILER==WIN32C
1446 _settextposition(int row, int col)
1449 CONSOLE_SCREEN_BUFFER_INFO csbi;
1451 GetConsoleScreenBufferInfo(con_out, &csbi);
1452 cpos.X = csbi.srWindow.Left + (col - 1);
1453 cpos.Y = csbi.srWindow.Top + (row - 1);
1454 SetConsoleCursorPosition(con_out, cpos);
1459 * Initialize the screen to the correct color at startup.
1464 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
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)
1513 * Create our own screen buffer, so that we
1514 * may restore the original when done.
1516 con_out_ours = CreateConsoleScreenBuffer(
1517 GENERIC_WRITE | GENERIC_READ,
1518 FILE_SHARE_WRITE | FILE_SHARE_READ,
1519 (LPSECURITY_ATTRIBUTES) NULL,
1520 CONSOLE_TEXTMODE_BUFFER,
1523 * Enable underline, if available.
1525 GetConsoleMode(con_out_ours, &output_mode);
1526 have_ul = SetConsoleMode(con_out_ours,
1527 output_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
1530 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1531 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1532 SetConsoleScreenBufferSize(con_out_ours, size);
1533 SetConsoleActiveScreenBuffer(con_out_ours);
1534 con_out = con_out_ours;
1538 * Restore the startup console.
1543 if (con_out_save == INVALID_HANDLE_VALUE)
1546 (void) CloseHandle(con_out_ours);
1547 SetConsoleActiveScreenBuffer(con_out_save);
1548 con_out = con_out_save;
1554 * Initialize terminal
1560 if (quit_if_one_screen && line_count >= sc_height)
1561 quit_if_one_screen = FALSE;
1562 if (!no_init && !quit_if_one_screen)
1563 tputs(sc_init, sc_height, putchr);
1565 tputs(sc_s_keypad, sc_height, putchr);
1571 * This is nice to terminals with no alternate screen,
1572 * but with saved scrolled-off-the-top lines. This way,
1573 * no previous line is lost, but we start with a whole
1574 * screen to ourself.
1576 for (i = 1; i < sc_height; i++)
1581 #if MSDOS_COMPILER==WIN32C
1592 * Deinitialize terminal
1601 tputs(sc_e_keypad, sc_height, putchr);
1602 if (!no_init && !quit_if_one_screen)
1603 tputs(sc_deinit, sc_height, putchr);
1605 /* Restore system colors. */
1606 SETCOLORS(sy_fg_color, sy_bg_color);
1607 #if MSDOS_COMPILER==WIN32C
1609 win32_deinit_term();
1611 /* Need clreol to make SETCOLORS take effect. */
1619 * Home cursor (move to upper left corner of screen).
1625 tputs(sc_home, 1, putchr);
1628 _settextposition(1,1);
1633 * Add a blank line (called with cursor at home).
1634 * Should scroll the display down.
1640 tputs(sc_addline, sc_height, putchr);
1643 #if MSDOS_COMPILER==MSOFTC
1644 _scrolltextwindow(_GSCROLLDOWN);
1645 _settextposition(1,1);
1647 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1648 movetext(1,1, sc_width,sc_height-1, 1,2);
1652 #if MSDOS_COMPILER==WIN32C
1655 SMALL_RECT rcSrc, rcClip;
1657 CONSOLE_SCREEN_BUFFER_INFO csbi;
1659 GetConsoleScreenBufferInfo(con_out,&csbi);
1661 /* The clip rectangle is the entire visible screen. */
1662 rcClip.Left = csbi.srWindow.Left;
1663 rcClip.Top = csbi.srWindow.Top;
1664 rcClip.Right = csbi.srWindow.Right;
1665 rcClip.Bottom = csbi.srWindow.Bottom;
1667 /* The source rectangle is the visible screen minus the last line. */
1671 /* Move the top left corner of the source window down one row. */
1672 new_org.X = rcSrc.Left;
1673 new_org.Y = rcSrc.Top + 1;
1675 /* Fill the right character and attributes. */
1676 fillchar.Char.AsciiChar = ' ';
1677 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1678 fillchar.Attributes = curr_attr;
1679 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1680 _settextposition(1,1);
1690 * Remove the n topmost lines and scroll everything below it in the
1691 * window upward. This is needed to stop leaking the topmost line
1692 * into the scrollback buffer when we go down-one-line (in WIN32).
1698 #if MSDOS_COMPILER==WIN32C
1699 SMALL_RECT rcSrc, rcClip;
1702 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1704 if (n >= sc_height - 1)
1713 GetConsoleScreenBufferInfo(con_out, &csbi);
1715 /* Get the extent of all-visible-rows-but-the-last. */
1716 rcSrc.Left = csbi.srWindow.Left;
1717 rcSrc.Top = csbi.srWindow.Top + n;
1718 rcSrc.Right = csbi.srWindow.Right;
1719 rcSrc.Bottom = csbi.srWindow.Bottom;
1721 /* Get the clip rectangle. */
1722 rcClip.Left = rcSrc.Left;
1723 rcClip.Top = csbi.srWindow.Top;
1724 rcClip.Right = rcSrc.Right;
1725 rcClip.Bottom = rcSrc.Bottom ;
1727 /* Move the source window up n rows. */
1728 new_org.X = rcSrc.Left;
1729 new_org.Y = rcSrc.Top - n;
1731 /* Fill the right character and attributes. */
1732 fillchar.Char.AsciiChar = ' ';
1733 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1734 fillchar.Attributes = curr_attr;
1736 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1738 /* Position cursor on first blank line. */
1739 goto_line(sc_height - n - 1);
1744 #if MSDOS_COMPILER==WIN32C
1752 * This will clear only the currently visible rows of the NT
1753 * console buffer, which means none of the precious scrollback
1754 * rows are touched making for faster scrolling. Note that, if
1755 * the window has fewer columns than the console buffer (i.e.
1756 * there is a horizontal scrollbar as well), the entire width
1757 * of the visible rows will be cleared.
1762 CONSOLE_SCREEN_BUFFER_INFO csbi;
1764 /* get the number of cells in the current buffer */
1765 GetConsoleScreenBufferInfo(con_out, &csbi);
1766 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1768 topleft.Y = csbi.srWindow.Top;
1770 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1771 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1772 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1776 * Remove the n topmost lines and scroll everything below it in the
1783 SMALL_RECT rcSrc, rcClip;
1789 CONSOLE_SCREEN_BUFFER_INFO csbi;
1794 if (n >= sc_height - 1)
1797 _settextposition(1,1);
1801 /* Get the extent of what will remain visible after scrolling. */
1802 GetConsoleScreenBufferInfo(con_out, &csbi);
1803 rcSrc.Left = csbi.srWindow.Left;
1804 rcSrc.Top = csbi.srWindow.Top + n;
1805 rcSrc.Right = csbi.srWindow.Right;
1806 rcSrc.Bottom = csbi.srWindow.Bottom;
1808 /* Get the clip rectangle. */
1809 rcClip.Left = rcSrc.Left;
1810 rcClip.Top = csbi.srWindow.Top;
1811 rcClip.Right = rcSrc.Right;
1812 rcClip.Bottom = rcSrc.Bottom ;
1814 /* Move the source text to the top of the screen. */
1815 new_org.X = rcSrc.Left;
1816 new_org.Y = rcClip.Top;
1818 /* Fill the right character and attributes. */
1819 fillchar.Char.AsciiChar = ' ';
1820 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1822 /* Scroll the window. */
1823 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1824 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1826 /* Clear remaining lines at bottom. */
1827 topleft.X = csbi.dwCursorPosition.X;
1828 topleft.Y = rcSrc.Bottom - n;
1829 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1830 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1832 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1834 SetConsoleTextAttribute(con_out, curr_attr);
1836 /* Move cursor n lines up from where it was. */
1837 csbi.dwCursorPosition.Y -= n;
1838 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1843 * Move cursor to lower left corner of screen.
1851 tputs(sc_lower_left, 1, putchr);
1854 _settextposition(sc_height, 1);
1859 * Move cursor to left position of current line.
1865 tputs(sc_return, 1, putchr);
1869 #if MSDOS_COMPILER==WIN32C
1871 CONSOLE_SCREEN_BUFFER_INFO scr;
1872 GetConsoleScreenBufferInfo(con_out, &scr);
1873 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
1876 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1880 struct rccoord tpos = _gettextposition();
1885 _settextposition(row, 1);
1890 * Check if the console size has changed and reset internals
1891 * (in lieu of SIGWINCH for WIN32).
1896 #if MSDOS_COMPILER==WIN32C
1897 CONSOLE_SCREEN_BUFFER_INFO scr;
1900 if (con_out == INVALID_HANDLE_VALUE)
1904 GetConsoleScreenBufferInfo(con_out, &scr);
1905 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1906 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1907 if (size.Y != sc_height || size.X != sc_width)
1911 if (!no_init && con_out_ours == con_out)
1912 SetConsoleScreenBufferSize(con_out, size);
1914 wscroll = (sc_height + 1) / 2;
1921 * Goto a specific line on the screen.
1928 tputs(tgoto(sc_move, 0, sindex), 1, putchr);
1931 _settextposition(sindex+1, 1);
1935 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1937 * Create an alternate screen which is all white.
1938 * This screen is used to create a "flash" effect, by displaying it
1939 * briefly and then switching back to the normal screen.
1940 * {{ Yuck! There must be a better way to get a visual bell. }}
1945 #if MSDOS_COMPILER==MSOFTC
1946 struct videoconfig w;
1950 _getvideoconfig(&w);
1951 videopages = w.numvideopages;
1954 at_enter(AT_STANDOUT);
1959 at_enter(AT_STANDOUT);
1960 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
1961 for (col = 0; col < w.numtextcols; col++)
1963 for (row = w.numtextrows; row > 0; row--)
1964 _outmem(blanks, w.numtextcols);
1971 #if MSDOS_COMPILER==BORLANDC
1974 whitescreen = (unsigned short *)
1975 malloc(sc_width * sc_height * sizeof(short));
1976 if (whitescreen == NULL)
1978 for (n = 0; n < sc_width * sc_height; n++)
1979 whitescreen[n] = 0x7020;
1981 #if MSDOS_COMPILER==WIN32C
1984 whitescreen = (WORD *)
1985 malloc(sc_height * sc_width * sizeof(WORD));
1986 if (whitescreen == NULL)
1988 /* Invert the standard colors. */
1989 for (n = 0; n < sc_width * sc_height; n++)
1990 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
1996 #endif /* MSDOS_COMPILER */
1999 * Output the "visual bell", if there is one.
2005 if (*sc_visual_bell == '\0')
2007 tputs(sc_visual_bell, sc_height, putchr);
2009 #if MSDOS_COMPILER==DJGPPC
2012 #if MSDOS_COMPILER==MSOFTC
2014 * Create a flash screen on the second video page.
2015 * Switch to that page, then switch back.
2025 #if MSDOS_COMPILER==BORLANDC
2026 unsigned short *currscreen;
2029 * Get a copy of the current screen.
2030 * Display the flash screen.
2031 * Then restore the old screen.
2035 if (whitescreen == NULL)
2037 currscreen = (unsigned short *)
2038 malloc(sc_width * sc_height * sizeof(short));
2039 if (currscreen == NULL) return;
2040 gettext(1, 1, sc_width, sc_height, currscreen);
2041 puttext(1, 1, sc_width, sc_height, whitescreen);
2043 puttext(1, 1, sc_width, sc_height, currscreen);
2046 #if MSDOS_COMPILER==WIN32C
2047 /* paint screen with an inverse color */
2050 /* leave it displayed for 100 msec. */
2053 /* restore with a redraw */
2069 putchr(CONTROL('G'));
2071 #if MSDOS_COMPILER==WIN32C
2080 * Ring the terminal bell.
2085 if (quiet == VERY_QUIET)
2098 tputs(sc_clear, sc_height, putchr);
2101 #if MSDOS_COMPILER==WIN32C
2104 _clearscreen(_GCLEARSCREEN);
2110 * Clear from the cursor to the end of the cursor's line.
2111 * {{ This must not move the cursor. }}
2117 tputs(sc_eol_clear, 1, putchr);
2119 #if MSDOS_COMPILER==MSOFTC
2122 struct rccoord tpos;
2126 * Save current state.
2128 tpos = _gettextposition();
2129 _gettextwindow(&top, &left, &bot, &right);
2131 * Set a temporary window to the current line,
2132 * from the cursor's position to the right edge of the screen.
2133 * Then clear that window.
2135 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2136 _clearscreen(_GWINDOW);
2140 _settextwindow(top, left, bot, right);
2141 _settextposition(tpos.row, tpos.col);
2143 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2147 #if MSDOS_COMPILER==WIN32C
2150 CONSOLE_SCREEN_BUFFER_INFO scr;
2153 memset(&scr, 0, sizeof(scr));
2154 GetConsoleScreenBufferInfo(con_out, &scr);
2155 cpos.X = scr.dwCursorPosition.X;
2156 cpos.Y = scr.dwCursorPosition.Y;
2157 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2158 FillConsoleOutputAttribute(con_out, curr_attr,
2159 scr.dwSize.X - cpos.X, cpos, &nchars);
2160 FillConsoleOutputCharacter(con_out, ' ',
2161 scr.dwSize.X - cpos.X, cpos, &nchars);
2169 * Clear the current line.
2170 * Clear the screen if there's off-screen memory below the display.
2179 tputs(sc_eos_clear, 1, putchr);
2181 tputs(sc_eol_clear, 1, putchr);
2186 * Clear the bottom line of the display.
2187 * Leave the cursor at the beginning of the bottom line.
2193 * If we're in a non-normal attribute mode, temporarily exit
2194 * the mode while we do the clear. Some terminals fill the
2195 * cleared area with the current attribute.
2202 if (attrmode == AT_NORMAL)
2206 int saved_attrmode = attrmode;
2210 at_enter(saved_attrmode);
2218 attr = apply_at_specials(attr);
2221 /* The one with the most priority is last. */
2222 if (attr & AT_UNDERLINE)
2223 tputs(sc_u_in, 1, putchr);
2225 tputs(sc_b_in, 1, putchr);
2226 if (attr & AT_BLINK)
2227 tputs(sc_bl_in, 1, putchr);
2228 if (attr & AT_STANDOUT)
2229 tputs(sc_s_in, 1, putchr);
2232 /* The one with the most priority is first. */
2233 if (attr & AT_STANDOUT)
2235 SETCOLORS(so_fg_color, so_bg_color);
2236 } else if (attr & AT_BLINK)
2238 SETCOLORS(bl_fg_color, bl_bg_color);
2240 else if (attr & AT_BOLD)
2242 SETCOLORS(bo_fg_color, bo_bg_color);
2244 else if (attr & AT_UNDERLINE)
2246 SETCOLORS(ul_fg_color, ul_bg_color);
2257 /* Undo things in the reverse order we did them. */
2258 if (attrmode & AT_STANDOUT)
2259 tputs(sc_s_out, 1, putchr);
2260 if (attrmode & AT_BLINK)
2261 tputs(sc_bl_out, 1, putchr);
2262 if (attrmode & AT_BOLD)
2263 tputs(sc_b_out, 1, putchr);
2264 if (attrmode & AT_UNDERLINE)
2265 tputs(sc_u_out, 1, putchr);
2268 SETCOLORS(nm_fg_color, nm_bg_color);
2271 attrmode = AT_NORMAL;
2278 int new_attrmode = apply_at_specials(attr);
2279 int ignore_modes = AT_ANSI;
2281 if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
2289 is_at_equiv(attr1, attr2)
2293 attr1 = apply_at_specials(attr1);
2294 attr2 = apply_at_specials(attr2);
2296 return (attr1 == attr2);
2300 apply_at_specials(attr)
2303 if (attr & AT_BINARY)
2305 if (attr & AT_HILITE)
2306 attr |= AT_STANDOUT;
2307 attr &= ~(AT_BINARY|AT_HILITE);
2312 #if 0 /* No longer used */
2314 * Erase the character to the left of the cursor
2315 * and move the cursor left.
2322 * Erase the previous character by overstriking with a space.
2324 tputs(sc_backspace, 1, putchr);
2326 tputs(sc_backspace, 1, putchr);
2328 #if MSDOS_COMPILER==MSOFTC
2329 struct rccoord tpos;
2332 tpos = _gettextposition();
2335 _settextposition(tpos.row, tpos.col-1);
2337 _settextposition(tpos.row, tpos.col-1);
2339 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2342 #if MSDOS_COMPILER==WIN32C
2345 CONSOLE_SCREEN_BUFFER_INFO scr;
2348 GetConsoleScreenBufferInfo(con_out, &scr);
2349 cpos = scr.dwCursorPosition;
2353 SetConsoleCursorPosition(con_out, cpos);
2354 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2355 SetConsoleCursorPosition(con_out, cpos);
2364 * Output a plain backspace, without erasing the previous char.
2370 tputs(sc_backspace, 1, putchr);
2376 #if MSDOS_COMPILER==MSOFTC
2377 struct rccoord tpos;
2378 tpos = _gettextposition();
2382 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2386 #if MSDOS_COMPILER==WIN32C
2387 CONSOLE_SCREEN_BUFFER_INFO scr;
2388 GetConsoleScreenBufferInfo(con_out, &scr);
2389 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2390 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2397 _settextposition(row, col-1);
2398 #endif /* MSDOS_COMPILER */
2401 #if MSDOS_COMPILER==WIN32C
2403 * Determine whether an input character is waiting to be read.
2415 currentKey.ascii = 0;
2416 currentKey.scan = 0;
2419 * Wait for a real key-down event, but
2420 * ignore SHIFT and CONTROL key events.
2424 PeekConsoleInput(tty, &ip, 1, &read);
2427 ReadConsoleInput(tty, &ip, 1, &read);
2428 } while (ip.EventType != KEY_EVENT ||
2429 ip.Event.KeyEvent.bKeyDown != TRUE ||
2430 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2431 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2432 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2433 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2435 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2436 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2437 keyCount = ip.Event.KeyEvent.wRepeatCount;
2439 if (ip.Event.KeyEvent.dwControlKeyState &
2440 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2442 switch (currentKey.scan)
2444 case PCK_ALT_E: /* letter 'E' */
2445 currentKey.ascii = 0;
2448 } else if (ip.Event.KeyEvent.dwControlKeyState &
2449 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2451 switch (currentKey.scan)
2453 case PCK_RIGHT: /* right arrow */
2454 currentKey.scan = PCK_CTL_RIGHT;
2456 case PCK_LEFT: /* left arrow */
2457 currentKey.scan = PCK_CTL_LEFT;
2459 case PCK_DELETE: /* delete */
2460 currentKey.scan = PCK_CTL_DELETE;
2463 } else if (ip.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
2465 switch (currentKey.scan)
2467 case PCK_SHIFT_TAB: /* tab */
2468 currentKey.ascii = 0;
2477 * Read a character from the keyboard.
2485 if (pending_scancode)
2487 pending_scancode = 0;
2488 return ((char)(currentKey.scan & 0x00FF));
2491 while (win32_kbhit((HANDLE)tty) == FALSE)
2499 ascii = currentKey.ascii;
2501 * On PC's, the extended keys return a 2 byte sequence beginning
2502 * with '00', so if the ascii code is 00, the next byte will be
2503 * the lsb of the scan code.
2505 pending_scancode = (ascii == 0x00);
2506 return ((char)ascii);
2514 WIN32setcolors(fg, bg)
2524 WIN32textout(text, len)
2528 #if MSDOS_COMPILER==WIN32C
2533 * We've got UTF-8 text in a non-UTF-8 console. Convert it to
2534 * wide and use WriteConsoleW.
2537 len = MultiByteToWideChar(CP_UTF8, 0, text, len, wtext,
2538 sizeof(wtext)/sizeof(*wtext));
2539 WriteConsoleW(con_out, wtext, len, &written, NULL);
2541 WriteConsole(con_out, text, len, &written, NULL);