3 * Copyright (C) 1984-2000 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;
236 extern int hilite_search;
239 extern char *tgetstr();
240 extern char *tgoto();
244 * Change terminal to "raw mode", or restore to "normal" mode.
246 * 1. An outstanding read will complete on receipt of a single keystroke.
247 * 2. Input is not echoed.
248 * 3. On output, \n is mapped to \r\n.
249 * 4. \t is NOT expanded into spaces.
250 * 5. Signal-causing characters such as ctrl-C (interrupt),
251 * etc. are NOT disabled.
252 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
258 static int curr_on = 0;
262 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
265 static struct termios save_term;
266 static int saved_term = 0;
271 * Get terminal modes.
276 * Save modes and set certain variables dependent on modes.
284 switch (cfgetospeed(&s))
287 case B0: ospeed = 0; break;
290 case B50: ospeed = 1; break;
293 case B75: ospeed = 2; break;
296 case B110: ospeed = 3; break;
299 case B134: ospeed = 4; break;
302 case B150: ospeed = 5; break;
305 case B200: ospeed = 6; break;
308 case B300: ospeed = 7; break;
311 case B600: ospeed = 8; break;
314 case B1200: ospeed = 9; break;
317 case B1800: ospeed = 10; break;
320 case B2400: ospeed = 11; break;
323 case B4800: ospeed = 12; break;
326 case B9600: ospeed = 13; break;
329 case EXTA: ospeed = 14; break;
332 case EXTB: ospeed = 15; break;
335 case B57600: ospeed = 16; break;
338 case B115200: ospeed = 17; break;
343 erase_char = s.c_cc[VERASE];
344 kill_char = s.c_cc[VKILL];
346 werase_char = s.c_cc[VWERASE];
348 werase_char = CONTROL('W');
352 * Set the modes to the way we want them.
414 #if MUST_SET_LINE_DISCIPLINE
416 * System's termios is broken; need to explicitly
417 * request TERMIODISC line discipline.
419 s.c_line = TERMIODISC;
424 * Restore saved modes.
431 tcsetattr(2, TCSADRAIN, &s);
432 #if MUST_SET_LINE_DISCIPLINE
436 * Broken termios *ignores* any line discipline
437 * except TERMIODISC. A different old line discipline
438 * is therefore not restored, yet. Restore the old
439 * line discipline by hand.
441 ioctl(2, TIOCSETD, &save_term.c_line);
449 static struct termio save_term;
450 static int saved_term = 0;
455 * Get terminal modes.
457 ioctl(2, TCGETA, &s);
460 * Save modes and set certain variables dependent on modes.
468 ospeed = s.c_cflag & CBAUD;
470 erase_char = s.c_cc[VERASE];
471 kill_char = s.c_cc[VKILL];
473 werase_char = s.c_cc[VWERASE];
475 werase_char = CONTROL('W');
479 * Set the modes to the way we want them.
481 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
482 s.c_oflag |= (OPOST|ONLCR|TAB3);
483 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
489 * Restore saved modes.
493 ioctl(2, TCSETAW, &s);
499 static struct sgttyb save_term;
500 static int saved_term = 0;
505 * Get terminal modes.
507 ioctl(2, TIOCGETP, &s);
510 * Save modes and set certain variables dependent on modes.
518 ospeed = s.sg_ospeed;
520 erase_char = s.sg_erase;
521 kill_char = s.sg_kill;
522 werase_char = CONTROL('W');
525 * Set the modes to the way we want them.
527 s.sg_flags |= CBREAK;
528 s.sg_flags &= ~(ECHO|XTABS);
532 * Restore saved modes.
536 ioctl(2, TIOCSETN, &s);
542 static struct sgbuf save_term;
543 static int saved_term = 0;
548 * Get terminal modes.
553 * Save modes and set certain variables dependent on modes.
560 erase_char = s.sg_bspch;
561 kill_char = s.sg_dlnch;
562 werase_char = CONTROL('W');
565 * Set the modes to the way we want them.
574 * Restore saved modes.
581 /* MS-DOS, Windows, or OS2 */
584 LSIGNAL(SIGINT, SIG_IGN);
587 #if MSDOS_COMPILER==DJGPPC
588 kill_char = CONTROL('U');
590 * So that when we shell out or run another program, its
591 * stdin is in cooked mode. We do not switch stdin to binary
592 * mode if fd0 is zero, since that means we were called before
593 * tty was reopened in open_getchr, in which case we would be
594 * changing the original stdin device outside less.
597 setmode(0, on ? O_BINARY : O_TEXT);
601 werase_char = CONTROL('W');
611 * Some glue to prevent calling termcap functions if tgetent() failed.
621 strcpy(name, "LESS_TERMCAP_");
622 strcat(name, capname);
623 return (lgetenv(name));
632 if ((s = ltget_env(capname)) != NULL)
633 return (*s != '\0' && *s != '0');
636 return (tgetflag(capname));
645 if ((s = ltget_env(capname)) != NULL)
649 return (tgetnum(capname));
653 ltgetstr(capname, pp)
659 if ((s = ltget_env(capname)) != NULL)
663 return (tgetstr(capname, pp));
665 #endif /* MSDOS_COMPILER */
668 * Get size of the output screen.
680 #define DEF_SC_WIDTH 80
682 #define DEF_SC_HEIGHT 25
684 #define DEF_SC_HEIGHT 24
688 sys_width = sys_height = 0;
690 #if MSDOS_COMPILER==MSOFTC
692 struct videoconfig w;
694 sys_height = w.numtextrows;
695 sys_width = w.numtextcols;
698 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
702 sys_height = w.screenheight;
703 sys_width = w.screenwidth;
706 #if MSDOS_COMPILER==WIN32C
708 CONSOLE_SCREEN_BUFFER_INFO scr;
709 GetConsoleScreenBufferInfo(con_out, &scr);
710 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
711 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
721 * When using terminal emulators for XFree86/OS2, the
722 * _scrsize function does not work well.
723 * Call the scrsize.exe program to get the window size.
725 windowid = getenv("WINDOWID");
726 if (windowid != NULL)
728 FILE *fd = popen("scrsize", "rt");
732 fscanf(fd, "%i %i", &w, &h);
746 if (ioctl(2, TIOCGWINSZ, &w) == 0)
749 sys_height = w.ws_row;
751 sys_width = w.ws_col;
758 if (ioctl(2, WIOCGETD, &w) == 0)
761 sys_height = w.uw_height / w.uw_vs;
763 sys_width = w.uw_width / w.uw_hs;
774 sc_height = sys_height;
775 else if ((s = lgetenv("LINES")) != NULL)
778 else if ((n = ltgetnum("li")) > 0)
782 sc_height = DEF_SC_HEIGHT;
785 sc_width = sys_width;
786 else if ((s = lgetenv("COLUMNS")) != NULL)
789 else if ((n = ltgetnum("co")) > 0)
793 sc_width = DEF_SC_WIDTH;
796 #if MSDOS_COMPILER==MSOFTC
798 * Figure out how many empty loops it takes to delay a millisecond.
806 * Get synchronized at the start of a tick.
809 while (clock() == start)
812 * Now count loops till the next tick.
816 while (clock() == start)
819 * Convert from (loops per clock) to (loops per millisecond).
821 msec_loops *= CLOCKS_PER_SEC;
826 * Delay for a specified number of milliseconds.
831 static long delay_dummy = 0;
843 for (i = 0; i < msec_loops; i++)
846 * Make it look like we're doing something here,
847 * so the optimizer doesn't remove the whole loop.
856 * Return the characters actually input by a "special" key.
862 static char tbuf[40];
864 #if MSDOS_COMPILER || OS2
865 static char k_right[] = { '\340', PCK_RIGHT, 0 };
866 static char k_left[] = { '\340', PCK_LEFT, 0 };
867 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
868 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
869 static char k_insert[] = { '\340', PCK_INSERT, 0 };
870 static char k_delete[] = { '\340', PCK_DELETE, 0 };
871 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
872 static char k_ctl_backspace[] = { '\177', 0 };
873 static char k_home[] = { '\340', PCK_HOME, 0 };
874 static char k_end[] = { '\340', PCK_END, 0 };
875 static char k_up[] = { '\340', PCK_UP, 0 };
876 static char k_down[] = { '\340', PCK_DOWN, 0 };
877 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
878 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
879 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
880 static char k_f1[] = { '\340', PCK_F1, 0 };
890 * If windowid is not NULL, assume less is executed in
891 * the XFree86 environment.
894 s = windowid ? ltgetstr("kr", &sp) : k_right;
897 s = windowid ? ltgetstr("kl", &sp) : k_left;
900 s = windowid ? ltgetstr("ku", &sp) : k_up;
903 s = windowid ? ltgetstr("kd", &sp) : k_down;
906 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
909 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
912 s = windowid ? ltgetstr("kh", &sp) : k_home;
915 s = windowid ? ltgetstr("@7", &sp) : k_end;
920 s = ltgetstr("kD", &sp);
960 #if MSDOS_COMPILER || OS2
964 case SK_CTL_LEFT_ARROW:
967 case SK_CTL_RIGHT_ARROW:
970 case SK_CTL_BACKSPACE:
984 s = ltgetstr("kr", &sp);
987 s = ltgetstr("kl", &sp);
990 s = ltgetstr("ku", &sp);
993 s = ltgetstr("kd", &sp);
996 s = ltgetstr("kP", &sp);
999 s = ltgetstr("kN", &sp);
1002 s = ltgetstr("kh", &sp);
1005 s = ltgetstr("@7", &sp);
1008 s = ltgetstr("kD", &sp);
1018 tbuf[0] = CONTROL('K');
1029 * Get terminal capabilities via termcap.
1040 * Set up default colors.
1041 * The xx_s_width and xx_e_width vars are already initialized to 0.
1043 #if MSDOS_COMPILER==MSOFTC
1044 sy_bg_color = _getbkcolor();
1045 sy_fg_color = _gettextcolor();
1048 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1052 sy_bg_color = (w.attribute >> 4) & 0x0F;
1053 sy_fg_color = (w.attribute >> 0) & 0x0F;
1056 #if MSDOS_COMPILER==WIN32C
1059 CONSOLE_SCREEN_BUFFER_INFO scr;
1061 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1063 * Always open stdin in binary. Note this *must* be done
1064 * before any file operations have been done on fd0.
1067 GetConsoleScreenBufferInfo(con_out, &scr);
1068 ReadConsoleOutputAttribute(con_out, &curr_attr,
1069 1, scr.dwCursorPosition, &nread);
1070 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1071 sy_fg_color = curr_attr & FG_COLORS;
1076 nm_fg_color = sy_fg_color;
1077 nm_bg_color = sy_bg_color;
1088 * Get size of the screen.
1094 #else /* !MSDOS_COMPILER */
1097 register char *t1, *t2;
1099 char termbuf[TERMBUF_SIZE];
1101 static char sbuf[TERMSBUF_SIZE];
1105 * Make sure the termcap database is available.
1107 sp = lgetenv("TERMCAP");
1108 if (sp == NULL || *sp == '\0')
1111 if ((sp = homefile("termcap.dat")) != NULL)
1113 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1114 sprintf(termcap, "TERMCAP=%s", sp);
1121 * Find out what kind of terminal this is.
1123 if ((term = lgetenv("TERM")) == NULL)
1124 term = DEFAULT_TERM;
1126 if (tgetent(termbuf, term) <= 0)
1128 if (ltgetflag("hc"))
1132 * Get size of the screen.
1137 auto_wrap = ltgetflag("am");
1138 ignaw = ltgetflag("xn");
1139 above_mem = ltgetflag("da");
1140 below_mem = ltgetflag("db");
1141 clear_bg = ltgetflag("ut");
1144 * Assumes termcap variable "sg" is the printing width of:
1145 * the standout sequence, the end standout sequence,
1146 * the underline sequence, the end underline sequence,
1147 * the boldface sequence, and the end boldface sequence.
1149 if ((so_s_width = ltgetnum("sg")) < 0)
1151 so_e_width = so_s_width;
1153 bo_s_width = bo_e_width = so_s_width;
1154 ul_s_width = ul_e_width = so_s_width;
1155 bl_s_width = bl_e_width = so_s_width;
1158 if (so_s_width > 0 || so_e_width > 0)
1160 * Disable highlighting by default on magic cookie terminals.
1161 * Turning on highlighting might change the displayed width
1162 * of a line, causing the display to get messed up.
1163 * The user can turn it back on with -g,
1164 * but she won't like the results.
1170 * Get various string-valued capabilities.
1175 sc_pad = ltgetstr("pc", &sp);
1180 sc_s_keypad = ltgetstr("ks", &sp);
1181 if (sc_s_keypad == NULL)
1183 sc_e_keypad = ltgetstr("ke", &sp);
1184 if (sc_e_keypad == NULL)
1188 * This loses for terminals with termcap entries with ti/te strings
1189 * that switch to/from an alternate screen, and we're in quit_at_eof
1192 if (!quit_at_eof && !more_mode) {
1193 sc_init = ltgetstr("ti", &sp);
1194 sc_deinit = ltgetstr("te", &sp);
1197 if (sc_init == NULL)
1200 if (sc_deinit == NULL)
1203 sc_eol_clear = ltgetstr("ce", &sp);
1204 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1210 sc_eos_clear = ltgetstr("cd", &sp);
1211 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1217 sc_clear = ltgetstr("cl", &sp);
1218 if (sc_clear == NULL || *sc_clear == '\0')
1224 sc_move = ltgetstr("cm", &sp);
1225 if (sc_move == NULL || *sc_move == '\0')
1228 * This is not an error here, because we don't
1229 * always need sc_move.
1230 * We need it only if we don't have home or lower-left.
1237 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1238 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1239 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1240 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1242 sc_visual_bell = ltgetstr("vb", &sp);
1243 if (sc_visual_bell == NULL)
1244 sc_visual_bell = "";
1246 if (ltgetflag("bs"))
1247 sc_backspace = "\b";
1250 sc_backspace = ltgetstr("bc", &sp);
1251 if (sc_backspace == NULL || *sc_backspace == '\0')
1252 sc_backspace = "\b";
1256 * Choose between using "ho" and "cm" ("home" and "cursor move")
1257 * to move the cursor to the upper left corner of the screen.
1259 t1 = ltgetstr("ho", &sp);
1262 if (*sc_move == '\0')
1266 strcpy(sp, tgoto(sc_move, 0, 0));
1268 sp += strlen(sp) + 1;
1270 sc_home = cheaper(t1, t2, "|\b^");
1273 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1274 * to move the cursor to the lower left corner of the screen.
1276 t1 = ltgetstr("ll", &sp);
1279 if (*sc_move == '\0')
1283 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1285 sp += strlen(sp) + 1;
1287 sc_lower_left = cheaper(t1, t2, "\r");
1290 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1291 * to add a line at the top of the screen.
1293 t1 = ltgetstr("al", &sp);
1296 t2 = ltgetstr("sr", &sp);
1300 if (*t1 == '\0' && *t2 == '\0')
1307 sc_addline = cheaper(t1, t2, "");
1308 if (*sc_addline == '\0')
1311 * Force repaint on any backward movement.
1315 #endif /* MSDOS_COMPILER */
1320 * Return the cost of displaying a termcap string.
1321 * We use the trick of calling tputs, but as a char printing function
1322 * we give it inc_costcount, which just increments "costcount".
1323 * This tells us how many chars would be printed by using this string.
1324 * {{ Couldn't we just use strlen? }}
1326 static int costcount;
1342 tputs(t, sc_height, inc_costcount);
1347 * Return the "best" of the two given termcap strings.
1348 * The best, if both exist, is the one with the lower
1349 * cost (see cost() function).
1352 cheaper(t1, t2, def)
1356 if (*t1 == '\0' && *t2 == '\0')
1365 if (cost(t1) < cost(t2))
1371 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1380 *instr = ltgetstr(incap, spp);
1385 *outstr = def_outstr;
1389 *outstr = ltgetstr(outcap, spp);
1390 if (*outstr == NULL)
1391 /* No specific out capability; use "me". */
1392 *outstr = ltgetstr("me", spp);
1393 if (*outstr == NULL)
1394 /* Don't even have "me"; use a null string. */
1398 #endif /* MSDOS_COMPILER */
1402 * Below are the functions which perform all the
1403 * terminal-specific screen manipulation.
1409 #if MSDOS_COMPILER==WIN32C
1411 _settextposition(int row, int col)
1414 CONSOLE_SCREEN_BUFFER_INFO csbi;
1416 GetConsoleScreenBufferInfo(con_out, &csbi);
1417 cpos.X = csbi.srWindow.Left + (col - 1);
1418 cpos.Y = csbi.srWindow.Top + (row - 1);
1419 SetConsoleCursorPosition(con_out, cpos);
1424 * Initialize the screen to the correct color at startup.
1429 SETCOLORS(nm_fg_color, nm_bg_color);
1432 * This clears the screen at startup. This is different from
1433 * the behavior of other versions of less. Disable it for now.
1440 * Create a complete, blank screen using "normal" colors.
1442 SETCOLORS(nm_fg_color, nm_bg_color);
1443 blanks = (char *) ecalloc(width+1, sizeof(char));
1444 for (col = 0; col < sc_width; col++)
1446 blanks[sc_width] = '\0';
1447 for (row = 0; row < sc_height; row++)
1454 #if MSDOS_COMPILER==WIN32C
1457 * Termcap-like init with a private win32 console.
1462 CONSOLE_SCREEN_BUFFER_INFO scr;
1465 if (con_out_save == INVALID_HANDLE_VALUE)
1468 GetConsoleScreenBufferInfo(con_out_save, &scr);
1470 if (con_out_ours == INVALID_HANDLE_VALUE)
1473 * Create our own screen buffer, so that we
1474 * may restore the original when done.
1476 con_out_ours = CreateConsoleScreenBuffer(
1477 GENERIC_WRITE | GENERIC_READ,
1478 FILE_SHARE_WRITE | FILE_SHARE_READ,
1479 (LPSECURITY_ATTRIBUTES) NULL,
1480 CONSOLE_TEXTMODE_BUFFER,
1484 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1485 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1486 SetConsoleScreenBufferSize(con_out_ours, size);
1487 SetConsoleActiveScreenBuffer(con_out_ours);
1488 con_out = con_out_ours;
1492 * Restore the startup console.
1497 if (con_out_save == INVALID_HANDLE_VALUE)
1500 (void) CloseHandle(con_out_ours);
1501 SetConsoleActiveScreenBuffer(con_out_save);
1502 con_out = con_out_save;
1508 * Initialize terminal
1515 tputs(sc_init, sc_height, putchr);
1517 tputs(sc_s_keypad, sc_height, putchr);
1519 #if MSDOS_COMPILER==WIN32C
1530 * Deinitialize terminal
1539 tputs(sc_e_keypad, sc_height, putchr);
1541 tputs(sc_deinit, sc_height, putchr);
1543 /* Restore system colors. */
1544 SETCOLORS(sy_fg_color, sy_bg_color);
1545 #if MSDOS_COMPILER==WIN32C
1547 win32_deinit_term();
1549 /* Need clreol to make SETCOLORS take effect. */
1557 * Home cursor (move to upper left corner of screen).
1563 tputs(sc_home, 1, putchr);
1566 _settextposition(1,1);
1571 * Add a blank line (called with cursor at home).
1572 * Should scroll the display down.
1578 tputs(sc_addline, sc_height, putchr);
1581 #if MSDOS_COMPILER==MSOFTC
1582 _scrolltextwindow(_GSCROLLDOWN);
1583 _settextposition(1,1);
1585 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1586 movetext(1,1, sc_width,sc_height-1, 1,2);
1590 #if MSDOS_COMPILER==WIN32C
1593 SMALL_RECT rcSrc, rcClip;
1595 CONSOLE_SCREEN_BUFFER_INFO csbi;
1597 GetConsoleScreenBufferInfo(con_out,&csbi);
1599 /* The clip rectangle is the entire visible screen. */
1600 rcClip.Left = csbi.srWindow.Left;
1601 rcClip.Top = csbi.srWindow.Top;
1602 rcClip.Right = csbi.srWindow.Right;
1603 rcClip.Bottom = csbi.srWindow.Bottom;
1605 /* The source rectangle is the visible screen minus the last line. */
1609 /* Move the top left corner of the source window down one row. */
1610 new_org.X = rcSrc.Left;
1611 new_org.Y = rcSrc.Top + 1;
1613 /* Fill the right character and attributes. */
1614 fillchar.Char.AsciiChar = ' ';
1615 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1616 fillchar.Attributes = curr_attr;
1617 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1618 _settextposition(1,1);
1628 * Remove the n topmost lines and scroll everything below it in the
1629 * window upward. This is needed to stop leaking the topmost line
1630 * into the scrollback buffer when we go down-one-line (in WIN32).
1636 #if MSDOS_COMPILER==WIN32C
1637 SMALL_RECT rcSrc, rcClip;
1640 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1642 if (n >= sc_height - 1)
1651 GetConsoleScreenBufferInfo(con_out, &csbi);
1653 /* Get the extent of all-visible-rows-but-the-last. */
1654 rcSrc.Left = csbi.srWindow.Left;
1655 rcSrc.Top = csbi.srWindow.Top + n;
1656 rcSrc.Right = csbi.srWindow.Right;
1657 rcSrc.Bottom = csbi.srWindow.Bottom;
1659 /* Get the clip rectangle. */
1660 rcClip.Left = rcSrc.Left;
1661 rcClip.Top = csbi.srWindow.Top;
1662 rcClip.Right = rcSrc.Right;
1663 rcClip.Bottom = rcSrc.Bottom ;
1665 /* Move the source window up n rows. */
1666 new_org.X = rcSrc.Left;
1667 new_org.Y = rcSrc.Top - n;
1669 /* Fill the right character and attributes. */
1670 fillchar.Char.AsciiChar = ' ';
1671 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1672 fillchar.Attributes = curr_attr;
1674 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1676 /* Position cursor on first blank line. */
1677 goto_line(sc_height - n - 1);
1682 #if MSDOS_COMPILER==WIN32C
1690 * This will clear only the currently visible rows of the NT
1691 * console buffer, which means none of the precious scrollback
1692 * rows are touched making for faster scrolling. Note that, if
1693 * the window has fewer columns than the console buffer (i.e.
1694 * there is a horizontal scrollbar as well), the entire width
1695 * of the visible rows will be cleared.
1700 CONSOLE_SCREEN_BUFFER_INFO csbi;
1702 /* get the number of cells in the current buffer */
1703 GetConsoleScreenBufferInfo(con_out, &csbi);
1704 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1706 topleft.Y = csbi.srWindow.Top;
1708 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1709 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1710 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1714 * Remove the n topmost lines and scroll everything below it in the
1721 SMALL_RECT rcSrc, rcClip;
1727 CONSOLE_SCREEN_BUFFER_INFO csbi;
1732 if (n >= sc_height - 1)
1735 _settextposition(1,1);
1739 /* Get the extent of what will remain visible after scrolling. */
1740 GetConsoleScreenBufferInfo(con_out, &csbi);
1741 rcSrc.Left = csbi.srWindow.Left;
1742 rcSrc.Top = csbi.srWindow.Top + n;
1743 rcSrc.Right = csbi.srWindow.Right;
1744 rcSrc.Bottom = csbi.srWindow.Bottom;
1746 /* Get the clip rectangle. */
1747 rcClip.Left = rcSrc.Left;
1748 rcClip.Top = csbi.srWindow.Top;
1749 rcClip.Right = rcSrc.Right;
1750 rcClip.Bottom = rcSrc.Bottom ;
1752 /* Move the source text to the top of the screen. */
1753 new_org.X = rcSrc.Left;
1756 /* Fill the right character and attributes. */
1757 fillchar.Char.AsciiChar = ' ';
1758 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1760 /* Scroll the window. */
1761 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1762 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1764 /* Clear remaining lines at bottom. */
1765 topleft.X = csbi.dwCursorPosition.X;
1766 topleft.Y = rcSrc.Bottom - n;
1767 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1768 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1770 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1772 SetConsoleTextAttribute(con_out, curr_attr);
1774 /* Move cursor n lines up from where it was. */
1775 csbi.dwCursorPosition.Y -= n;
1776 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1781 * Move cursor to lower left corner of screen.
1787 tputs(sc_lower_left, 1, putchr);
1790 _settextposition(sc_height, 1);
1795 * Check if the console size has changed and reset internals
1796 * (in lieu of SIGWINCH for WIN32).
1801 #if MSDOS_COMPILER==WIN32C
1802 CONSOLE_SCREEN_BUFFER_INFO scr;
1805 if (con_out == INVALID_HANDLE_VALUE)
1809 GetConsoleScreenBufferInfo(con_out, &scr);
1810 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1811 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1812 if (size.Y != sc_height || size.X != sc_width)
1816 if (!no_init && con_out_ours == con_out)
1817 SetConsoleScreenBufferSize(con_out, size);
1819 wscroll = (sc_height + 1) / 2;
1826 * Goto a specific line on the screen.
1833 tputs(tgoto(sc_move, 0, slinenum), 1, putchr);
1836 _settextposition(slinenum+1, 1);
1840 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1842 * Create an alternate screen which is all white.
1843 * This screen is used to create a "flash" effect, by displaying it
1844 * briefly and then switching back to the normal screen.
1845 * {{ Yuck! There must be a better way to get a visual bell. }}
1850 #if MSDOS_COMPILER==MSOFTC
1851 struct videoconfig w;
1855 _getvideoconfig(&w);
1856 videopages = w.numvideopages;
1865 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
1866 for (col = 0; col < w.numtextcols; col++)
1868 for (row = w.numtextrows; row > 0; row--)
1869 _outmem(blanks, w.numtextcols);
1876 #if MSDOS_COMPILER==BORLANDC
1879 whitescreen = (unsigned short *)
1880 malloc(sc_width * sc_height * sizeof(short));
1881 if (whitescreen == NULL)
1883 for (n = 0; n < sc_width * sc_height; n++)
1884 whitescreen[n] = 0x7020;
1886 #if MSDOS_COMPILER==WIN32C
1889 whitescreen = (WORD *)
1890 malloc(sc_height * sc_width * sizeof(WORD));
1891 if (whitescreen == NULL)
1893 /* Invert the standard colors. */
1894 for (n = 0; n < sc_width * sc_height; n++)
1895 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
1901 #endif /* MSDOS_COMPILER */
1904 * Output the "visual bell", if there is one.
1910 if (*sc_visual_bell == '\0')
1912 tputs(sc_visual_bell, sc_height, putchr);
1914 #if MSDOS_COMPILER==DJGPPC
1917 #if MSDOS_COMPILER==MSOFTC
1919 * Create a flash screen on the second video page.
1920 * Switch to that page, then switch back.
1930 #if MSDOS_COMPILER==BORLANDC
1931 unsigned short *currscreen;
1934 * Get a copy of the current screen.
1935 * Display the flash screen.
1936 * Then restore the old screen.
1940 if (whitescreen == NULL)
1942 currscreen = (unsigned short *)
1943 malloc(sc_width * sc_height * sizeof(short));
1944 if (currscreen == NULL) return;
1945 gettext(1, 1, sc_width, sc_height, currscreen);
1946 puttext(1, 1, sc_width, sc_height, whitescreen);
1948 puttext(1, 1, sc_width, sc_height, currscreen);
1951 #if MSDOS_COMPILER==WIN32C
1952 /* paint screen with an inverse color */
1955 /* leave it displayed for 100 msec. */
1958 /* restore with a redraw */
1974 putchr(CONTROL('G'));
1976 #if MSDOS_COMPILER==WIN32C
1985 * Ring the terminal bell.
1990 if (quiet == VERY_QUIET)
2003 tputs(sc_clear, sc_height, putchr);
2006 #if MSDOS_COMPILER==WIN32C
2009 _clearscreen(_GCLEARSCREEN);
2015 * Clear from the cursor to the end of the cursor's line.
2016 * {{ This must not move the cursor. }}
2022 tputs(sc_eol_clear, 1, putchr);
2024 #if MSDOS_COMPILER==MSOFTC
2027 struct rccoord tpos;
2031 * Save current state.
2033 tpos = _gettextposition();
2034 _gettextwindow(&top, &left, &bot, &right);
2036 * Set a temporary window to the current line,
2037 * from the cursor's position to the right edge of the screen.
2038 * Then clear that window.
2040 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2041 _clearscreen(_GWINDOW);
2045 _settextwindow(top, left, bot, right);
2046 _settextposition(tpos.row, tpos.col);
2048 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2052 #if MSDOS_COMPILER==WIN32C
2055 CONSOLE_SCREEN_BUFFER_INFO scr;
2058 memset(&scr, 0, sizeof(scr));
2059 GetConsoleScreenBufferInfo(con_out, &scr);
2060 cpos.X = scr.dwCursorPosition.X;
2061 cpos.Y = scr.dwCursorPosition.Y;
2062 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2063 FillConsoleOutputAttribute(con_out, curr_attr,
2064 scr.dwSize.X - cpos.X, cpos, &nchars);
2065 FillConsoleOutputCharacter(con_out, ' ',
2066 scr.dwSize.X - cpos.X, cpos, &nchars);
2074 * Clear the current line.
2075 * Clear the screen if there's off-screen memory below the display.
2084 tputs(sc_eos_clear, 1, putchr);
2086 tputs(sc_eol_clear, 1, putchr);
2091 * Clear the bottom line of the display.
2092 * Leave the cursor at the beginning of the bottom line.
2098 * If we're in a non-normal attribute mode, temporarily exit
2099 * the mode while we do the clear. Some terminals fill the
2100 * cleared area with the current attribute.
2132 * Begin "standout" (bold, underline, or whatever).
2138 tputs(sc_s_in, 1, putchr);
2141 SETCOLORS(so_fg_color, so_bg_color);
2143 attrmode = AT_STANDOUT;
2153 tputs(sc_s_out, 1, putchr);
2156 SETCOLORS(nm_fg_color, nm_bg_color);
2158 attrmode = AT_NORMAL;
2162 * Begin "underline" (hopefully real underlining,
2163 * otherwise whatever the terminal provides).
2169 tputs(sc_u_in, 1, putchr);
2172 SETCOLORS(ul_fg_color, ul_bg_color);
2174 attrmode = AT_UNDERLINE;
2184 tputs(sc_u_out, 1, putchr);
2187 SETCOLORS(nm_fg_color, nm_bg_color);
2189 attrmode = AT_NORMAL;
2199 tputs(sc_b_in, 1, putchr);
2202 SETCOLORS(bo_fg_color, bo_bg_color);
2214 tputs(sc_b_out, 1, putchr);
2217 SETCOLORS(nm_fg_color, nm_bg_color);
2219 attrmode = AT_NORMAL;
2229 tputs(sc_bl_in, 1, putchr);
2232 SETCOLORS(bl_fg_color, bl_bg_color);
2234 attrmode = AT_BLINK;
2244 tputs(sc_bl_out, 1, putchr);
2247 SETCOLORS(nm_fg_color, nm_bg_color);
2249 attrmode = AT_NORMAL;
2252 #if 0 /* No longer used */
2254 * Erase the character to the left of the cursor
2255 * and move the cursor left.
2262 * Erase the previous character by overstriking with a space.
2264 tputs(sc_backspace, 1, putchr);
2266 tputs(sc_backspace, 1, putchr);
2268 #if MSDOS_COMPILER==MSOFTC
2269 struct rccoord tpos;
2272 tpos = _gettextposition();
2275 _settextposition(tpos.row, tpos.col-1);
2277 _settextposition(tpos.row, tpos.col-1);
2279 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2282 #if MSDOS_COMPILER==WIN32C
2285 CONSOLE_SCREEN_BUFFER_INFO scr;
2288 GetConsoleScreenBufferInfo(con_out, &scr);
2289 cpos = scr.dwCursorPosition;
2293 SetConsoleCursorPosition(con_out, cpos);
2294 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2295 SetConsoleCursorPosition(con_out, cpos);
2304 * Output a plain backspace, without erasing the previous char.
2310 tputs(sc_backspace, 1, putchr);
2316 #if MSDOS_COMPILER==MSOFTC
2317 struct rccoord tpos;
2318 tpos = _gettextposition();
2322 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2326 #if MSDOS_COMPILER==WIN32C
2327 CONSOLE_SCREEN_BUFFER_INFO scr;
2328 GetConsoleScreenBufferInfo(con_out, &scr);
2329 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2330 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2337 _settextposition(row, col-1);
2338 #endif /* MSDOS_COMPILER */
2341 #if MSDOS_COMPILER==WIN32C
2343 * Determine whether an input character is waiting to be read.
2355 currentKey.ascii = 0;
2356 currentKey.scan = 0;
2359 * Wait for a real key-down event, but
2360 * ignore SHIFT and CONTROL key events.
2364 PeekConsoleInput(tty, &ip, 1, &read);
2367 ReadConsoleInput(tty, &ip, 1, &read);
2368 } while (ip.EventType != KEY_EVENT ||
2369 ip.Event.KeyEvent.bKeyDown != TRUE ||
2370 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2371 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2372 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2373 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2375 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2376 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2377 keyCount = ip.Event.KeyEvent.wRepeatCount;
2379 if (ip.Event.KeyEvent.dwControlKeyState &
2380 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2382 switch (currentKey.scan)
2384 case PCK_ALT_E: /* letter 'E' */
2385 currentKey.ascii = 0;
2388 } else if (ip.Event.KeyEvent.dwControlKeyState &
2389 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2391 switch (currentKey.scan)
2393 case PCK_RIGHT: /* right arrow */
2394 currentKey.scan = PCK_CTL_RIGHT;
2396 case PCK_LEFT: /* left arrow */
2397 currentKey.scan = PCK_CTL_LEFT;
2399 case PCK_DELETE: /* delete */
2400 currentKey.scan = PCK_CTL_DELETE;
2408 * Read a character from the keyboard.
2416 if (pending_scancode)
2418 pending_scancode = 0;
2419 return ((char)(currentKey.scan & 0x00FF));
2422 while (win32_kbhit((HANDLE)tty) == FALSE)
2430 ascii = currentKey.ascii;
2432 * On PC's, the extended keys return a 2 byte sequence beginning
2433 * with '00', so if the ascii code is 00, the next byte will be
2434 * the lsb of the scan code.
2436 pending_scancode = (ascii == 0x00);
2437 return ((char)ascii);