3 * Copyright (C) 1984-2002 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information about less, or for information on how to
9 * contact the author, 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
42 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
44 #if HAVE_SYS_IOCTL_H && !defined(TIOCGWINSZ)
45 #include <sys/ioctl.h>
56 #if HAVE_SYS_IOCTL_H && (defined(TIOCGWINSZ) || defined(TCGETA) || defined(TIOCGETP) || defined(WIOCGETD))
57 #include <sys/ioctl.h>
69 #include <sys/signal.h>
73 #include <sys/stream.h>
79 #endif /* MSDOS_COMPILER */
82 * Check for broken termios package that forces you to manually
83 * set the line discipline.
86 #define MUST_SET_LINE_DISCIPLINE 1
88 #define MUST_SET_LINE_DISCIPLINE 0
92 #define DEFAULT_TERM "ansi"
93 static char *windowid;
95 #define DEFAULT_TERM "unknown"
98 #if MSDOS_COMPILER==MSOFTC
99 static int videopages;
100 static long msec_loops;
101 static int flash_created = 0;
102 #define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); }
105 #if MSDOS_COMPILER==BORLANDC
106 static unsigned short *whitescreen;
107 static int flash_created = 0;
109 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
110 #define _settextposition(y,x) gotoxy(x,y)
111 #define _clearscreen(m) clrscr()
112 #define _outtext(s) cputs(s)
113 #define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); }
114 extern int sc_height;
117 #if MSDOS_COMPILER==WIN32C
124 static int keyCount = 0;
125 static WORD curr_attr;
126 static int pending_scancode = 0;
127 static WORD *whitescreen;
129 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
130 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
131 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */
134 static void win32_init_term();
135 static void win32_deinit_term();
137 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
138 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
139 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
140 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \
141 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
142 error("SETCOLORS failed"); }
146 public int nm_fg_color; /* Color of normal text */
147 public int nm_bg_color;
148 public int bo_fg_color; /* Color of bold text */
149 public int bo_bg_color;
150 public int ul_fg_color; /* Color of underlined text */
151 public int ul_bg_color;
152 public int so_fg_color; /* Color of standout text */
153 public int so_bg_color;
154 public int bl_fg_color; /* Color of blinking text */
155 public int bl_bg_color;
156 static int sy_fg_color; /* Color of system text (before less) */
157 static int sy_bg_color;
162 * Strings passed to tputs() to do various terminal functions.
165 *sc_pad, /* Pad string */
166 *sc_home, /* Cursor home */
167 *sc_addline, /* Add line, scroll down following lines */
168 *sc_lower_left, /* Cursor to last line, first column */
169 *sc_move, /* General cursor positioning */
170 *sc_clear, /* Clear screen */
171 *sc_eol_clear, /* Clear to end of line */
172 *sc_eos_clear, /* Clear to end of screen */
173 *sc_s_in, /* Enter standout (highlighted) mode */
174 *sc_s_out, /* Exit standout mode */
175 *sc_u_in, /* Enter underline mode */
176 *sc_u_out, /* Exit underline mode */
177 *sc_b_in, /* Enter bold mode */
178 *sc_b_out, /* Exit bold mode */
179 *sc_bl_in, /* Enter blink mode */
180 *sc_bl_out, /* Exit blink mode */
181 *sc_visual_bell, /* Visual bell (flash screen) sequence */
182 *sc_backspace, /* Backspace cursor */
183 *sc_s_keypad, /* Start keypad mode */
184 *sc_e_keypad, /* End keypad mode */
185 *sc_init, /* Startup terminal initialization */
186 *sc_deinit; /* Exit terminal de-initialization */
189 static int init_done = 0;
191 public int auto_wrap; /* Terminal does \r\n when write past margin */
192 public int ignaw; /* Terminal ignores \n immediately after wrap */
193 public int erase_char, kill_char; /* The user's erase and line-kill chars */
194 public int werase_char; /* The user's word-erase char */
195 public int sc_width, sc_height; /* Height & width of screen */
196 public int bo_s_width, bo_e_width; /* Printing width of boldface seq */
197 public int ul_s_width, ul_e_width; /* Printing width of underline seq */
198 public int so_s_width, so_e_width; /* Printing width of standout seq */
199 public int bl_s_width, bl_e_width; /* Printing width of blink seq */
200 public int above_mem, below_mem; /* Memory retained above/below screen */
201 public int can_goto_line; /* Can move cursor to any line */
202 public int clear_bg; /* Clear fills with background color */
203 public int missing_cap = 0; /* Some capability is missing */
205 static int attrmode = AT_NORMAL;
208 static char *cheaper();
209 static void tmodes();
213 * These two variables are sometimes defined in,
214 * and needed by, the termcap library.
216 #if MUST_DEFINE_OSPEED
217 extern short ospeed; /* Terminal output baud rate */
218 extern char PC; /* Pad character */
225 extern int quiet; /* If VERY_QUIET, use visual bell for bell */
226 extern int no_back_scroll;
229 extern int quit_at_eof;
230 extern int more_mode;
231 extern int no_keypad;
234 extern int screen_trashed;
237 extern int hilite_search;
240 extern char *tgetstr();
241 extern char *tgoto();
245 * Change terminal to "raw mode", or restore to "normal" mode.
247 * 1. An outstanding read will complete on receipt of a single keystroke.
248 * 2. Input is not echoed.
249 * 3. On output, \n is mapped to \r\n.
250 * 4. \t is NOT expanded into spaces.
251 * 5. Signal-causing characters such as ctrl-C (interrupt),
252 * etc. are NOT disabled.
253 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
259 static int curr_on = 0;
263 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
266 static struct termios save_term;
267 static int saved_term = 0;
272 * Get terminal modes.
277 * Save modes and set certain variables dependent on modes.
285 switch (cfgetospeed(&s))
288 case B0: ospeed = 0; break;
291 case B50: ospeed = 1; break;
294 case B75: ospeed = 2; break;
297 case B110: ospeed = 3; break;
300 case B134: ospeed = 4; break;
303 case B150: ospeed = 5; break;
306 case B200: ospeed = 6; break;
309 case B300: ospeed = 7; break;
312 case B600: ospeed = 8; break;
315 case B1200: ospeed = 9; break;
318 case B1800: ospeed = 10; break;
321 case B2400: ospeed = 11; break;
324 case B4800: ospeed = 12; break;
327 case B9600: ospeed = 13; break;
330 case EXTA: ospeed = 14; break;
333 case EXTB: ospeed = 15; break;
336 case B57600: ospeed = 16; break;
339 case B115200: ospeed = 17; break;
344 erase_char = s.c_cc[VERASE];
345 kill_char = s.c_cc[VKILL];
347 werase_char = s.c_cc[VWERASE];
349 werase_char = CONTROL('W');
353 * Set the modes to the way we want them.
415 #if MUST_SET_LINE_DISCIPLINE
417 * System's termios is broken; need to explicitly
418 * request TERMIODISC line discipline.
420 s.c_line = TERMIODISC;
425 * Restore saved modes.
432 tcsetattr(tty, TCSADRAIN, &s);
433 #if MUST_SET_LINE_DISCIPLINE
437 * Broken termios *ignores* any line discipline
438 * except TERMIODISC. A different old line discipline
439 * is therefore not restored, yet. Restore the old
440 * line discipline by hand.
442 ioctl(tty, TIOCSETD, &save_term.c_line);
450 static struct termio save_term;
451 static int saved_term = 0;
456 * Get terminal modes.
458 ioctl(tty, TCGETA, &s);
461 * Save modes and set certain variables dependent on modes.
469 ospeed = s.c_cflag & CBAUD;
471 erase_char = s.c_cc[VERASE];
472 kill_char = s.c_cc[VKILL];
474 werase_char = s.c_cc[VWERASE];
476 werase_char = CONTROL('W');
480 * Set the modes to the way we want them.
482 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
483 s.c_oflag |= (OPOST|ONLCR|TAB3);
484 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
490 * Restore saved modes.
494 ioctl(tty, TCSETAW, &s);
500 static struct sgttyb save_term;
501 static int saved_term = 0;
506 * Get terminal modes.
508 ioctl(tty, TIOCGETP, &s);
511 * Save modes and set certain variables dependent on modes.
519 ospeed = s.sg_ospeed;
521 erase_char = s.sg_erase;
522 kill_char = s.sg_kill;
523 werase_char = CONTROL('W');
526 * Set the modes to the way we want them.
528 s.sg_flags |= CBREAK;
529 s.sg_flags &= ~(ECHO|XTABS);
533 * Restore saved modes.
537 ioctl(tty, TIOCSETN, &s);
543 static struct sgbuf save_term;
544 static int saved_term = 0;
549 * Get terminal modes.
554 * Save modes and set certain variables dependent on modes.
561 erase_char = s.sg_bspch;
562 kill_char = s.sg_dlnch;
563 werase_char = CONTROL('W');
566 * Set the modes to the way we want them.
575 * Restore saved modes.
582 /* MS-DOS, Windows, or OS2 */
585 LSIGNAL(SIGINT, SIG_IGN);
588 #if MSDOS_COMPILER==DJGPPC
589 kill_char = CONTROL('U');
591 * So that when we shell out or run another program, its
592 * stdin is in cooked mode. We do not switch stdin to binary
593 * mode if fd0 is zero, since that means we were called before
594 * tty was reopened in open_getchr, in which case we would be
595 * changing the original stdin device outside less.
598 setmode(0, on ? O_BINARY : O_TEXT);
602 werase_char = CONTROL('W');
612 * Some glue to prevent calling termcap functions if tgetent() failed.
622 strcpy(name, "LESS_TERMCAP_");
623 strcat(name, capname);
624 return (lgetenv(name));
633 if ((s = ltget_env(capname)) != NULL)
634 return (*s != '\0' && *s != '0');
637 return (tgetflag(capname));
646 if ((s = ltget_env(capname)) != NULL)
650 return (tgetnum(capname));
654 ltgetstr(capname, pp)
660 if ((s = ltget_env(capname)) != NULL)
664 return (tgetstr(capname, pp));
666 #endif /* MSDOS_COMPILER */
669 * Get size of the output screen.
681 #define DEF_SC_WIDTH 80
683 #define DEF_SC_HEIGHT 25
685 #define DEF_SC_HEIGHT 24
689 sys_width = sys_height = 0;
691 #if MSDOS_COMPILER==MSOFTC
693 struct videoconfig w;
695 sys_height = w.numtextrows;
696 sys_width = w.numtextcols;
699 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
703 sys_height = w.screenheight;
704 sys_width = w.screenwidth;
707 #if MSDOS_COMPILER==WIN32C
709 CONSOLE_SCREEN_BUFFER_INFO scr;
710 GetConsoleScreenBufferInfo(con_out, &scr);
711 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
712 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
722 * When using terminal emulators for XFree86/OS2, the
723 * _scrsize function does not work well.
724 * Call the scrsize.exe program to get the window size.
726 windowid = getenv("WINDOWID");
727 if (windowid != NULL)
729 FILE *fd = popen("scrsize", "rt");
733 fscanf(fd, "%i %i", &w, &h);
747 if (ioctl(2, TIOCGWINSZ, &w) == 0)
750 sys_height = w.ws_row;
752 sys_width = w.ws_col;
759 if (ioctl(2, WIOCGETD, &w) == 0)
762 sys_height = w.uw_height / w.uw_vs;
764 sys_width = w.uw_width / w.uw_hs;
775 sc_height = sys_height;
776 else if ((s = lgetenv("LINES")) != NULL)
779 else if ((n = ltgetnum("li")) > 0)
783 sc_height = DEF_SC_HEIGHT;
786 sc_width = sys_width;
787 else if ((s = lgetenv("COLUMNS")) != NULL)
790 else if ((n = ltgetnum("co")) > 0)
794 sc_width = DEF_SC_WIDTH;
797 #if MSDOS_COMPILER==MSOFTC
799 * Figure out how many empty loops it takes to delay a millisecond.
807 * Get synchronized at the start of a tick.
810 while (clock() == start)
813 * Now count loops till the next tick.
817 while (clock() == start)
820 * Convert from (loops per clock) to (loops per millisecond).
822 msec_loops *= CLOCKS_PER_SEC;
827 * Delay for a specified number of milliseconds.
832 static long delay_dummy = 0;
844 for (i = 0; i < msec_loops; i++)
847 * Make it look like we're doing something here,
848 * so the optimizer doesn't remove the whole loop.
857 * Return the characters actually input by a "special" key.
863 static char tbuf[40];
865 #if MSDOS_COMPILER || OS2
866 static char k_right[] = { '\340', PCK_RIGHT, 0 };
867 static char k_left[] = { '\340', PCK_LEFT, 0 };
868 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
869 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
870 static char k_insert[] = { '\340', PCK_INSERT, 0 };
871 static char k_delete[] = { '\340', PCK_DELETE, 0 };
872 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
873 static char k_ctl_backspace[] = { '\177', 0 };
874 static char k_home[] = { '\340', PCK_HOME, 0 };
875 static char k_end[] = { '\340', PCK_END, 0 };
876 static char k_up[] = { '\340', PCK_UP, 0 };
877 static char k_down[] = { '\340', PCK_DOWN, 0 };
878 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
879 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
880 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
881 static char k_f1[] = { '\340', PCK_F1, 0 };
891 * If windowid is not NULL, assume less is executed in
892 * the XFree86 environment.
895 s = windowid ? ltgetstr("kr", &sp) : k_right;
898 s = windowid ? ltgetstr("kl", &sp) : k_left;
901 s = windowid ? ltgetstr("ku", &sp) : k_up;
904 s = windowid ? ltgetstr("kd", &sp) : k_down;
907 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
910 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
913 s = windowid ? ltgetstr("kh", &sp) : k_home;
916 s = windowid ? ltgetstr("@7", &sp) : k_end;
921 s = ltgetstr("kD", &sp);
961 #if MSDOS_COMPILER || OS2
965 case SK_CTL_LEFT_ARROW:
968 case SK_CTL_RIGHT_ARROW:
971 case SK_CTL_BACKSPACE:
985 s = ltgetstr("kr", &sp);
988 s = ltgetstr("kl", &sp);
991 s = ltgetstr("ku", &sp);
994 s = ltgetstr("kd", &sp);
997 s = ltgetstr("kP", &sp);
1000 s = ltgetstr("kN", &sp);
1003 s = ltgetstr("kh", &sp);
1006 s = ltgetstr("@7", &sp);
1009 s = ltgetstr("kD", &sp);
1019 tbuf[0] = CONTROL('K');
1030 * Get terminal capabilities via termcap.
1041 * Set up default colors.
1042 * The xx_s_width and xx_e_width vars are already initialized to 0.
1044 #if MSDOS_COMPILER==MSOFTC
1045 sy_bg_color = _getbkcolor();
1046 sy_fg_color = _gettextcolor();
1049 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1053 sy_bg_color = (w.attribute >> 4) & 0x0F;
1054 sy_fg_color = (w.attribute >> 0) & 0x0F;
1057 #if MSDOS_COMPILER==WIN32C
1060 CONSOLE_SCREEN_BUFFER_INFO scr;
1062 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1064 * Always open stdin in binary. Note this *must* be done
1065 * before any file operations have been done on fd0.
1068 GetConsoleScreenBufferInfo(con_out, &scr);
1069 ReadConsoleOutputAttribute(con_out, &curr_attr,
1070 1, scr.dwCursorPosition, &nread);
1071 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1072 sy_fg_color = curr_attr & FG_COLORS;
1077 nm_fg_color = sy_fg_color;
1078 nm_bg_color = sy_bg_color;
1089 * Get size of the screen.
1095 #else /* !MSDOS_COMPILER */
1098 register char *t1, *t2;
1100 char termbuf[TERMBUF_SIZE];
1102 static char sbuf[TERMSBUF_SIZE];
1106 * Make sure the termcap database is available.
1108 sp = lgetenv("TERMCAP");
1109 if (sp == NULL || *sp == '\0')
1112 if ((sp = homefile("termcap.dat")) != NULL)
1114 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1115 sprintf(termcap, "TERMCAP=%s", sp);
1122 * Find out what kind of terminal this is.
1124 if ((term = lgetenv("TERM")) == NULL)
1125 term = DEFAULT_TERM;
1127 if (tgetent(termbuf, term) <= 0)
1129 if (ltgetflag("hc"))
1133 * Get size of the screen.
1138 auto_wrap = ltgetflag("am");
1139 ignaw = ltgetflag("xn");
1140 above_mem = ltgetflag("da");
1141 below_mem = ltgetflag("db");
1142 clear_bg = ltgetflag("ut");
1145 * Assumes termcap variable "sg" is the printing width of:
1146 * the standout sequence, the end standout sequence,
1147 * the underline sequence, the end underline sequence,
1148 * the boldface sequence, and the end boldface sequence.
1150 if ((so_s_width = ltgetnum("sg")) < 0)
1152 so_e_width = so_s_width;
1154 bo_s_width = bo_e_width = so_s_width;
1155 ul_s_width = ul_e_width = so_s_width;
1156 bl_s_width = bl_e_width = so_s_width;
1159 if (so_s_width > 0 || so_e_width > 0)
1161 * Disable highlighting by default on magic cookie terminals.
1162 * Turning on highlighting might change the displayed width
1163 * of a line, causing the display to get messed up.
1164 * The user can turn it back on with -g,
1165 * but she won't like the results.
1171 * Get various string-valued capabilities.
1176 sc_pad = ltgetstr("pc", &sp);
1181 sc_s_keypad = ltgetstr("ks", &sp);
1182 if (sc_s_keypad == NULL)
1184 sc_e_keypad = ltgetstr("ke", &sp);
1185 if (sc_e_keypad == NULL)
1189 * This loses for terminals with termcap entries with ti/te strings
1190 * that switch to/from an alternate screen, and we're in quit_at_eof
1193 if (!quit_at_eof && !more_mode) {
1194 sc_init = ltgetstr("ti", &sp);
1195 sc_deinit = ltgetstr("te", &sp);
1198 if (sc_init == NULL)
1201 if (sc_deinit == NULL)
1204 sc_eol_clear = ltgetstr("ce", &sp);
1205 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1211 sc_eos_clear = ltgetstr("cd", &sp);
1212 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1218 sc_clear = ltgetstr("cl", &sp);
1219 if (sc_clear == NULL || *sc_clear == '\0')
1225 sc_move = ltgetstr("cm", &sp);
1226 if (sc_move == NULL || *sc_move == '\0')
1229 * This is not an error here, because we don't
1230 * always need sc_move.
1231 * We need it only if we don't have home or lower-left.
1238 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1239 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1240 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1241 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1243 sc_visual_bell = ltgetstr("vb", &sp);
1244 if (sc_visual_bell == NULL)
1245 sc_visual_bell = "";
1247 if (ltgetflag("bs"))
1248 sc_backspace = "\b";
1251 sc_backspace = ltgetstr("bc", &sp);
1252 if (sc_backspace == NULL || *sc_backspace == '\0')
1253 sc_backspace = "\b";
1257 * Choose between using "ho" and "cm" ("home" and "cursor move")
1258 * to move the cursor to the upper left corner of the screen.
1260 t1 = ltgetstr("ho", &sp);
1263 if (*sc_move == '\0')
1267 strcpy(sp, tgoto(sc_move, 0, 0));
1269 sp += strlen(sp) + 1;
1271 sc_home = cheaper(t1, t2, "|\b^");
1274 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1275 * to move the cursor to the lower left corner of the screen.
1277 t1 = ltgetstr("ll", &sp);
1280 if (*sc_move == '\0')
1284 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1286 sp += strlen(sp) + 1;
1288 sc_lower_left = cheaper(t1, t2, "\r");
1291 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1292 * to add a line at the top of the screen.
1294 t1 = ltgetstr("al", &sp);
1297 t2 = ltgetstr("sr", &sp);
1301 if (*t1 == '\0' && *t2 == '\0')
1308 sc_addline = cheaper(t1, t2, "");
1309 if (*sc_addline == '\0')
1312 * Force repaint on any backward movement.
1316 #endif /* MSDOS_COMPILER */
1321 * Return the cost of displaying a termcap string.
1322 * We use the trick of calling tputs, but as a char printing function
1323 * we give it inc_costcount, which just increments "costcount".
1324 * This tells us how many chars would be printed by using this string.
1325 * {{ Couldn't we just use strlen? }}
1327 static int costcount;
1343 tputs(t, sc_height, inc_costcount);
1348 * Return the "best" of the two given termcap strings.
1349 * The best, if both exist, is the one with the lower
1350 * cost (see cost() function).
1353 cheaper(t1, t2, def)
1357 if (*t1 == '\0' && *t2 == '\0')
1366 if (cost(t1) < cost(t2))
1372 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1381 *instr = ltgetstr(incap, spp);
1386 *outstr = def_outstr;
1390 *outstr = ltgetstr(outcap, spp);
1391 if (*outstr == NULL)
1392 /* No specific out capability; use "me". */
1393 *outstr = ltgetstr("me", spp);
1394 if (*outstr == NULL)
1395 /* Don't even have "me"; use a null string. */
1399 #endif /* MSDOS_COMPILER */
1403 * Below are the functions which perform all the
1404 * terminal-specific screen manipulation.
1410 #if MSDOS_COMPILER==WIN32C
1412 _settextposition(int row, int col)
1415 CONSOLE_SCREEN_BUFFER_INFO csbi;
1417 GetConsoleScreenBufferInfo(con_out, &csbi);
1418 cpos.X = csbi.srWindow.Left + (col - 1);
1419 cpos.Y = csbi.srWindow.Top + (row - 1);
1420 SetConsoleCursorPosition(con_out, cpos);
1425 * Initialize the screen to the correct color at startup.
1430 SETCOLORS(nm_fg_color, nm_bg_color);
1433 * This clears the screen at startup. This is different from
1434 * the behavior of other versions of less. Disable it for now.
1441 * Create a complete, blank screen using "normal" colors.
1443 SETCOLORS(nm_fg_color, nm_bg_color);
1444 blanks = (char *) ecalloc(width+1, sizeof(char));
1445 for (col = 0; col < sc_width; col++)
1447 blanks[sc_width] = '\0';
1448 for (row = 0; row < sc_height; row++)
1455 #if MSDOS_COMPILER==WIN32C
1458 * Termcap-like init with a private win32 console.
1463 CONSOLE_SCREEN_BUFFER_INFO scr;
1466 if (con_out_save == INVALID_HANDLE_VALUE)
1469 GetConsoleScreenBufferInfo(con_out_save, &scr);
1471 if (con_out_ours == INVALID_HANDLE_VALUE)
1474 * Create our own screen buffer, so that we
1475 * may restore the original when done.
1477 con_out_ours = CreateConsoleScreenBuffer(
1478 GENERIC_WRITE | GENERIC_READ,
1479 FILE_SHARE_WRITE | FILE_SHARE_READ,
1480 (LPSECURITY_ATTRIBUTES) NULL,
1481 CONSOLE_TEXTMODE_BUFFER,
1485 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1486 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1487 SetConsoleScreenBufferSize(con_out_ours, size);
1488 SetConsoleActiveScreenBuffer(con_out_ours);
1489 con_out = con_out_ours;
1493 * Restore the startup console.
1498 if (con_out_save == INVALID_HANDLE_VALUE)
1501 (void) CloseHandle(con_out_ours);
1502 SetConsoleActiveScreenBuffer(con_out_save);
1503 con_out = con_out_save;
1509 * Initialize terminal
1516 tputs(sc_init, sc_height, putchr);
1518 tputs(sc_s_keypad, sc_height, putchr);
1520 #if MSDOS_COMPILER==WIN32C
1531 * Deinitialize terminal
1540 tputs(sc_e_keypad, sc_height, putchr);
1542 tputs(sc_deinit, sc_height, putchr);
1544 /* Restore system colors. */
1545 SETCOLORS(sy_fg_color, sy_bg_color);
1546 #if MSDOS_COMPILER==WIN32C
1548 win32_deinit_term();
1550 /* Need clreol to make SETCOLORS take effect. */
1558 * Home cursor (move to upper left corner of screen).
1564 tputs(sc_home, 1, putchr);
1567 _settextposition(1,1);
1572 * Add a blank line (called with cursor at home).
1573 * Should scroll the display down.
1579 tputs(sc_addline, sc_height, putchr);
1582 #if MSDOS_COMPILER==MSOFTC
1583 _scrolltextwindow(_GSCROLLDOWN);
1584 _settextposition(1,1);
1586 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1587 movetext(1,1, sc_width,sc_height-1, 1,2);
1591 #if MSDOS_COMPILER==WIN32C
1594 SMALL_RECT rcSrc, rcClip;
1596 CONSOLE_SCREEN_BUFFER_INFO csbi;
1598 GetConsoleScreenBufferInfo(con_out,&csbi);
1600 /* The clip rectangle is the entire visible screen. */
1601 rcClip.Left = csbi.srWindow.Left;
1602 rcClip.Top = csbi.srWindow.Top;
1603 rcClip.Right = csbi.srWindow.Right;
1604 rcClip.Bottom = csbi.srWindow.Bottom;
1606 /* The source rectangle is the visible screen minus the last line. */
1610 /* Move the top left corner of the source window down one row. */
1611 new_org.X = rcSrc.Left;
1612 new_org.Y = rcSrc.Top + 1;
1614 /* Fill the right character and attributes. */
1615 fillchar.Char.AsciiChar = ' ';
1616 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1617 fillchar.Attributes = curr_attr;
1618 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1619 _settextposition(1,1);
1629 * Remove the n topmost lines and scroll everything below it in the
1630 * window upward. This is needed to stop leaking the topmost line
1631 * into the scrollback buffer when we go down-one-line (in WIN32).
1637 #if MSDOS_COMPILER==WIN32C
1638 SMALL_RECT rcSrc, rcClip;
1641 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1643 if (n >= sc_height - 1)
1652 GetConsoleScreenBufferInfo(con_out, &csbi);
1654 /* Get the extent of all-visible-rows-but-the-last. */
1655 rcSrc.Left = csbi.srWindow.Left;
1656 rcSrc.Top = csbi.srWindow.Top + n;
1657 rcSrc.Right = csbi.srWindow.Right;
1658 rcSrc.Bottom = csbi.srWindow.Bottom;
1660 /* Get the clip rectangle. */
1661 rcClip.Left = rcSrc.Left;
1662 rcClip.Top = csbi.srWindow.Top;
1663 rcClip.Right = rcSrc.Right;
1664 rcClip.Bottom = rcSrc.Bottom ;
1666 /* Move the source window up n rows. */
1667 new_org.X = rcSrc.Left;
1668 new_org.Y = rcSrc.Top - n;
1670 /* Fill the right character and attributes. */
1671 fillchar.Char.AsciiChar = ' ';
1672 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1673 fillchar.Attributes = curr_attr;
1675 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1677 /* Position cursor on first blank line. */
1678 goto_line(sc_height - n - 1);
1683 #if MSDOS_COMPILER==WIN32C
1691 * This will clear only the currently visible rows of the NT
1692 * console buffer, which means none of the precious scrollback
1693 * rows are touched making for faster scrolling. Note that, if
1694 * the window has fewer columns than the console buffer (i.e.
1695 * there is a horizontal scrollbar as well), the entire width
1696 * of the visible rows will be cleared.
1701 CONSOLE_SCREEN_BUFFER_INFO csbi;
1703 /* get the number of cells in the current buffer */
1704 GetConsoleScreenBufferInfo(con_out, &csbi);
1705 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1707 topleft.Y = csbi.srWindow.Top;
1709 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1710 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1711 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1715 * Remove the n topmost lines and scroll everything below it in the
1722 SMALL_RECT rcSrc, rcClip;
1728 CONSOLE_SCREEN_BUFFER_INFO csbi;
1733 if (n >= sc_height - 1)
1736 _settextposition(1,1);
1740 /* Get the extent of what will remain visible after scrolling. */
1741 GetConsoleScreenBufferInfo(con_out, &csbi);
1742 rcSrc.Left = csbi.srWindow.Left;
1743 rcSrc.Top = csbi.srWindow.Top + n;
1744 rcSrc.Right = csbi.srWindow.Right;
1745 rcSrc.Bottom = csbi.srWindow.Bottom;
1747 /* Get the clip rectangle. */
1748 rcClip.Left = rcSrc.Left;
1749 rcClip.Top = csbi.srWindow.Top;
1750 rcClip.Right = rcSrc.Right;
1751 rcClip.Bottom = rcSrc.Bottom ;
1753 /* Move the source text to the top of the screen. */
1754 new_org.X = rcSrc.Left;
1757 /* Fill the right character and attributes. */
1758 fillchar.Char.AsciiChar = ' ';
1759 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1761 /* Scroll the window. */
1762 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1763 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1765 /* Clear remaining lines at bottom. */
1766 topleft.X = csbi.dwCursorPosition.X;
1767 topleft.Y = rcSrc.Bottom - n;
1768 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1769 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1771 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1773 SetConsoleTextAttribute(con_out, curr_attr);
1775 /* Move cursor n lines up from where it was. */
1776 csbi.dwCursorPosition.Y -= n;
1777 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1782 * Move cursor to lower left corner of screen.
1788 tputs(sc_lower_left, 1, putchr);
1791 _settextposition(sc_height, 1);
1796 * Check if the console size has changed and reset internals
1797 * (in lieu of SIGWINCH for WIN32).
1802 #if MSDOS_COMPILER==WIN32C
1803 CONSOLE_SCREEN_BUFFER_INFO scr;
1806 if (con_out == INVALID_HANDLE_VALUE)
1810 GetConsoleScreenBufferInfo(con_out, &scr);
1811 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1812 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1813 if (size.Y != sc_height || size.X != sc_width)
1817 if (!no_init && con_out_ours == con_out)
1818 SetConsoleScreenBufferSize(con_out, size);
1820 wscroll = (sc_height + 1) / 2;
1827 * Goto a specific line on the screen.
1834 tputs(tgoto(sc_move, 0, slinenum), 1, putchr);
1837 _settextposition(slinenum+1, 1);
1841 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1843 * Create an alternate screen which is all white.
1844 * This screen is used to create a "flash" effect, by displaying it
1845 * briefly and then switching back to the normal screen.
1846 * {{ Yuck! There must be a better way to get a visual bell. }}
1851 #if MSDOS_COMPILER==MSOFTC
1852 struct videoconfig w;
1856 _getvideoconfig(&w);
1857 videopages = w.numvideopages;
1866 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
1867 for (col = 0; col < w.numtextcols; col++)
1869 for (row = w.numtextrows; row > 0; row--)
1870 _outmem(blanks, w.numtextcols);
1877 #if MSDOS_COMPILER==BORLANDC
1880 whitescreen = (unsigned short *)
1881 malloc(sc_width * sc_height * sizeof(short));
1882 if (whitescreen == NULL)
1884 for (n = 0; n < sc_width * sc_height; n++)
1885 whitescreen[n] = 0x7020;
1887 #if MSDOS_COMPILER==WIN32C
1890 whitescreen = (WORD *)
1891 malloc(sc_height * sc_width * sizeof(WORD));
1892 if (whitescreen == NULL)
1894 /* Invert the standard colors. */
1895 for (n = 0; n < sc_width * sc_height; n++)
1896 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
1902 #endif /* MSDOS_COMPILER */
1905 * Output the "visual bell", if there is one.
1911 if (*sc_visual_bell == '\0')
1913 tputs(sc_visual_bell, sc_height, putchr);
1915 #if MSDOS_COMPILER==DJGPPC
1918 #if MSDOS_COMPILER==MSOFTC
1920 * Create a flash screen on the second video page.
1921 * Switch to that page, then switch back.
1931 #if MSDOS_COMPILER==BORLANDC
1932 unsigned short *currscreen;
1935 * Get a copy of the current screen.
1936 * Display the flash screen.
1937 * Then restore the old screen.
1941 if (whitescreen == NULL)
1943 currscreen = (unsigned short *)
1944 malloc(sc_width * sc_height * sizeof(short));
1945 if (currscreen == NULL) return;
1946 gettext(1, 1, sc_width, sc_height, currscreen);
1947 puttext(1, 1, sc_width, sc_height, whitescreen);
1949 puttext(1, 1, sc_width, sc_height, currscreen);
1952 #if MSDOS_COMPILER==WIN32C
1953 /* paint screen with an inverse color */
1956 /* leave it displayed for 100 msec. */
1959 /* restore with a redraw */
1975 putchr(CONTROL('G'));
1977 #if MSDOS_COMPILER==WIN32C
1986 * Ring the terminal bell.
1991 if (quiet == VERY_QUIET)
2004 tputs(sc_clear, sc_height, putchr);
2007 #if MSDOS_COMPILER==WIN32C
2010 _clearscreen(_GCLEARSCREEN);
2016 * Clear from the cursor to the end of the cursor's line.
2017 * {{ This must not move the cursor. }}
2023 tputs(sc_eol_clear, 1, putchr);
2025 #if MSDOS_COMPILER==MSOFTC
2028 struct rccoord tpos;
2032 * Save current state.
2034 tpos = _gettextposition();
2035 _gettextwindow(&top, &left, &bot, &right);
2037 * Set a temporary window to the current line,
2038 * from the cursor's position to the right edge of the screen.
2039 * Then clear that window.
2041 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2042 _clearscreen(_GWINDOW);
2046 _settextwindow(top, left, bot, right);
2047 _settextposition(tpos.row, tpos.col);
2049 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2053 #if MSDOS_COMPILER==WIN32C
2056 CONSOLE_SCREEN_BUFFER_INFO scr;
2059 memset(&scr, 0, sizeof(scr));
2060 GetConsoleScreenBufferInfo(con_out, &scr);
2061 cpos.X = scr.dwCursorPosition.X;
2062 cpos.Y = scr.dwCursorPosition.Y;
2063 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2064 FillConsoleOutputAttribute(con_out, curr_attr,
2065 scr.dwSize.X - cpos.X, cpos, &nchars);
2066 FillConsoleOutputCharacter(con_out, ' ',
2067 scr.dwSize.X - cpos.X, cpos, &nchars);
2075 * Clear the current line.
2076 * Clear the screen if there's off-screen memory below the display.
2085 tputs(sc_eos_clear, 1, putchr);
2087 tputs(sc_eol_clear, 1, putchr);
2092 * Clear the bottom line of the display.
2093 * Leave the cursor at the beginning of the bottom line.
2099 * If we're in a non-normal attribute mode, temporarily exit
2100 * the mode while we do the clear. Some terminals fill the
2101 * cleared area with the current attribute.
2133 * Begin "standout" (bold, underline, or whatever).
2139 tputs(sc_s_in, 1, putchr);
2142 SETCOLORS(so_fg_color, so_bg_color);
2144 attrmode = AT_STANDOUT;
2154 tputs(sc_s_out, 1, putchr);
2157 SETCOLORS(nm_fg_color, nm_bg_color);
2159 attrmode = AT_NORMAL;
2163 * Begin "underline" (hopefully real underlining,
2164 * otherwise whatever the terminal provides).
2170 tputs(sc_u_in, 1, putchr);
2173 SETCOLORS(ul_fg_color, ul_bg_color);
2175 attrmode = AT_UNDERLINE;
2185 tputs(sc_u_out, 1, putchr);
2188 SETCOLORS(nm_fg_color, nm_bg_color);
2190 attrmode = AT_NORMAL;
2200 tputs(sc_b_in, 1, putchr);
2203 SETCOLORS(bo_fg_color, bo_bg_color);
2215 tputs(sc_b_out, 1, putchr);
2218 SETCOLORS(nm_fg_color, nm_bg_color);
2220 attrmode = AT_NORMAL;
2230 tputs(sc_bl_in, 1, putchr);
2233 SETCOLORS(bl_fg_color, bl_bg_color);
2235 attrmode = AT_BLINK;
2245 tputs(sc_bl_out, 1, putchr);
2248 SETCOLORS(nm_fg_color, nm_bg_color);
2250 attrmode = AT_NORMAL;
2253 #if 0 /* No longer used */
2255 * Erase the character to the left of the cursor
2256 * and move the cursor left.
2263 * Erase the previous character by overstriking with a space.
2265 tputs(sc_backspace, 1, putchr);
2267 tputs(sc_backspace, 1, putchr);
2269 #if MSDOS_COMPILER==MSOFTC
2270 struct rccoord tpos;
2273 tpos = _gettextposition();
2276 _settextposition(tpos.row, tpos.col-1);
2278 _settextposition(tpos.row, tpos.col-1);
2280 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2283 #if MSDOS_COMPILER==WIN32C
2286 CONSOLE_SCREEN_BUFFER_INFO scr;
2289 GetConsoleScreenBufferInfo(con_out, &scr);
2290 cpos = scr.dwCursorPosition;
2294 SetConsoleCursorPosition(con_out, cpos);
2295 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2296 SetConsoleCursorPosition(con_out, cpos);
2305 * Output a plain backspace, without erasing the previous char.
2311 tputs(sc_backspace, 1, putchr);
2317 #if MSDOS_COMPILER==MSOFTC
2318 struct rccoord tpos;
2319 tpos = _gettextposition();
2323 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2327 #if MSDOS_COMPILER==WIN32C
2328 CONSOLE_SCREEN_BUFFER_INFO scr;
2329 GetConsoleScreenBufferInfo(con_out, &scr);
2330 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2331 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2338 _settextposition(row, col-1);
2339 #endif /* MSDOS_COMPILER */
2342 #if MSDOS_COMPILER==WIN32C
2344 * Determine whether an input character is waiting to be read.
2356 currentKey.ascii = 0;
2357 currentKey.scan = 0;
2360 * Wait for a real key-down event, but
2361 * ignore SHIFT and CONTROL key events.
2365 PeekConsoleInput(tty, &ip, 1, &read);
2368 ReadConsoleInput(tty, &ip, 1, &read);
2369 } while (ip.EventType != KEY_EVENT ||
2370 ip.Event.KeyEvent.bKeyDown != TRUE ||
2371 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2372 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2373 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2374 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2376 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2377 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2378 keyCount = ip.Event.KeyEvent.wRepeatCount;
2380 if (ip.Event.KeyEvent.dwControlKeyState &
2381 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2383 switch (currentKey.scan)
2385 case PCK_ALT_E: /* letter 'E' */
2386 currentKey.ascii = 0;
2389 } else if (ip.Event.KeyEvent.dwControlKeyState &
2390 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2392 switch (currentKey.scan)
2394 case PCK_RIGHT: /* right arrow */
2395 currentKey.scan = PCK_CTL_RIGHT;
2397 case PCK_LEFT: /* left arrow */
2398 currentKey.scan = PCK_CTL_LEFT;
2400 case PCK_DELETE: /* delete */
2401 currentKey.scan = PCK_CTL_DELETE;
2409 * Read a character from the keyboard.
2417 if (pending_scancode)
2419 pending_scancode = 0;
2420 return ((char)(currentKey.scan & 0x00FF));
2423 while (win32_kbhit((HANDLE)tty) == FALSE)
2431 ascii = currentKey.ascii;
2433 * On PC's, the extended keys return a 2 byte sequence beginning
2434 * with '00', so if the ascii code is 00, the next byte will be
2435 * the lsb of the scan code.
2437 pending_scancode = (ascii == 0x00);
2438 return ((char)ascii);