3 * Copyright (C) 1984-2005 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
43 #include <sys/ioctl.h>
46 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
67 #include <sys/signal.h>
71 #include <sys/stream.h>
77 #endif /* MSDOS_COMPILER */
80 * Check for broken termios package that forces you to manually
81 * set the line discipline.
84 #define MUST_SET_LINE_DISCIPLINE 1
86 #define MUST_SET_LINE_DISCIPLINE 0
90 #define DEFAULT_TERM "ansi"
91 static char *windowid;
93 #define DEFAULT_TERM "unknown"
96 #if MSDOS_COMPILER==MSOFTC
97 static int videopages;
98 static long msec_loops;
99 static int flash_created = 0;
100 #define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); }
103 #if MSDOS_COMPILER==BORLANDC
104 static unsigned short *whitescreen;
105 static int flash_created = 0;
107 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
108 #define _settextposition(y,x) gotoxy(x,y)
109 #define _clearscreen(m) clrscr()
110 #define _outtext(s) cputs(s)
111 #define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); }
112 extern int sc_height;
115 #if MSDOS_COMPILER==WIN32C
122 static int keyCount = 0;
123 static WORD curr_attr;
124 static int pending_scancode = 0;
125 static WORD *whitescreen;
127 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
128 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
129 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */
132 static void win32_init_term();
133 static void win32_deinit_term();
135 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
136 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
137 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
138 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \
139 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
140 error("SETCOLORS failed"); }
144 public int nm_fg_color; /* Color of normal text */
145 public int nm_bg_color;
146 public int bo_fg_color; /* Color of bold text */
147 public int bo_bg_color;
148 public int ul_fg_color; /* Color of underlined text */
149 public int ul_bg_color;
150 public int so_fg_color; /* Color of standout text */
151 public int so_bg_color;
152 public int bl_fg_color; /* Color of blinking text */
153 public int bl_bg_color;
154 static int sy_fg_color; /* Color of system text (before less) */
155 static int sy_bg_color;
160 * Strings passed to tputs() to do various terminal functions.
163 *sc_pad, /* Pad string */
164 *sc_home, /* Cursor home */
165 *sc_addline, /* Add line, scroll down following lines */
166 *sc_lower_left, /* Cursor to last line, first column */
167 *sc_move, /* General cursor positioning */
168 *sc_clear, /* Clear screen */
169 *sc_eol_clear, /* Clear to end of line */
170 *sc_eos_clear, /* Clear to end of screen */
171 *sc_s_in, /* Enter standout (highlighted) mode */
172 *sc_s_out, /* Exit standout mode */
173 *sc_u_in, /* Enter underline mode */
174 *sc_u_out, /* Exit underline mode */
175 *sc_b_in, /* Enter bold mode */
176 *sc_b_out, /* Exit bold mode */
177 *sc_bl_in, /* Enter blink mode */
178 *sc_bl_out, /* Exit blink mode */
179 *sc_visual_bell, /* Visual bell (flash screen) sequence */
180 *sc_backspace, /* Backspace cursor */
181 *sc_s_keypad, /* Start keypad mode */
182 *sc_e_keypad, /* End keypad mode */
183 *sc_init, /* Startup terminal initialization */
184 *sc_deinit; /* Exit terminal de-initialization */
187 static int init_done = 0;
189 public int auto_wrap; /* Terminal does \r\n when write past margin */
190 public int ignaw; /* Terminal ignores \n immediately after wrap */
191 public int erase_char; /* The user's erase char */
192 public int erase2_char; /* The user's other erase char */
193 public int kill_char; /* The user's line-kill char */
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;
209 static char *cheaper();
210 static void tmodes();
214 * These two variables are sometimes defined in,
215 * and needed by, the termcap library.
217 #if MUST_DEFINE_OSPEED
218 extern short ospeed; /* Terminal output baud rate */
219 extern char PC; /* Pad character */
226 extern int quiet; /* If VERY_QUIET, use visual bell for bell */
227 extern int no_back_scroll;
230 extern int quit_at_eof;
231 extern int more_mode;
232 extern int no_keypad;
235 extern int screen_trashed;
237 extern int top_scroll;
239 extern int hilite_search;
242 extern char *tgetstr();
243 extern char *tgoto();
247 * Change terminal to "raw mode", or restore to "normal" mode.
249 * 1. An outstanding read will complete on receipt of a single keystroke.
250 * 2. Input is not echoed.
251 * 3. On output, \n is mapped to \r\n.
252 * 4. \t is NOT expanded into spaces.
253 * 5. Signal-causing characters such as ctrl-C (interrupt),
254 * etc. are NOT disabled.
255 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
261 static int curr_on = 0;
265 erase2_char = '\b'; /* in case OS doesn't know about erase2 */
266 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
269 static struct termios save_term;
270 static int saved_term = 0;
275 * Get terminal modes.
280 * Save modes and set certain variables dependent on modes.
288 switch (cfgetospeed(&s))
291 case B0: ospeed = 0; break;
294 case B50: ospeed = 1; break;
297 case B75: ospeed = 2; break;
300 case B110: ospeed = 3; break;
303 case B134: ospeed = 4; break;
306 case B150: ospeed = 5; break;
309 case B200: ospeed = 6; break;
312 case B300: ospeed = 7; break;
315 case B600: ospeed = 8; break;
318 case B1200: ospeed = 9; break;
321 case B1800: ospeed = 10; break;
324 case B2400: ospeed = 11; break;
327 case B4800: ospeed = 12; break;
330 case B9600: ospeed = 13; break;
333 case EXTA: ospeed = 14; break;
336 case EXTB: ospeed = 15; break;
339 case B57600: ospeed = 16; break;
342 case B115200: ospeed = 17; break;
347 erase_char = s.c_cc[VERASE];
349 erase2_char = s.c_cc[VERASE2];
351 kill_char = s.c_cc[VKILL];
353 werase_char = s.c_cc[VWERASE];
355 werase_char = CONTROL('W');
359 * Set the modes to the way we want them.
421 #if MUST_SET_LINE_DISCIPLINE
423 * System's termios is broken; need to explicitly
424 * request TERMIODISC line discipline.
426 s.c_line = TERMIODISC;
431 * Restore saved modes.
438 tcsetattr(tty, TCSADRAIN, &s);
439 #if MUST_SET_LINE_DISCIPLINE
443 * Broken termios *ignores* any line discipline
444 * except TERMIODISC. A different old line discipline
445 * is therefore not restored, yet. Restore the old
446 * line discipline by hand.
448 ioctl(tty, TIOCSETD, &save_term.c_line);
456 static struct termio save_term;
457 static int saved_term = 0;
462 * Get terminal modes.
464 ioctl(tty, TCGETA, &s);
467 * Save modes and set certain variables dependent on modes.
475 ospeed = s.c_cflag & CBAUD;
477 erase_char = s.c_cc[VERASE];
478 kill_char = s.c_cc[VKILL];
480 werase_char = s.c_cc[VWERASE];
482 werase_char = CONTROL('W');
486 * Set the modes to the way we want them.
488 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
489 s.c_oflag |= (OPOST|ONLCR|TAB3);
490 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
496 * Restore saved modes.
500 ioctl(tty, TCSETAW, &s);
506 static struct sgttyb save_term;
507 static int saved_term = 0;
512 * Get terminal modes.
514 ioctl(tty, TIOCGETP, &s);
517 * Save modes and set certain variables dependent on modes.
525 ospeed = s.sg_ospeed;
527 erase_char = s.sg_erase;
528 kill_char = s.sg_kill;
529 werase_char = CONTROL('W');
532 * Set the modes to the way we want them.
534 s.sg_flags |= CBREAK;
535 s.sg_flags &= ~(ECHO|XTABS);
539 * Restore saved modes.
543 ioctl(tty, TIOCSETN, &s);
549 static struct sgbuf save_term;
550 static int saved_term = 0;
555 * Get terminal modes.
560 * Save modes and set certain variables dependent on modes.
567 erase_char = s.sg_bspch;
568 kill_char = s.sg_dlnch;
569 werase_char = CONTROL('W');
572 * Set the modes to the way we want them.
581 * Restore saved modes.
588 /* MS-DOS, Windows, or OS2 */
591 LSIGNAL(SIGINT, SIG_IGN);
594 #if MSDOS_COMPILER==DJGPPC
595 kill_char = CONTROL('U');
597 * So that when we shell out or run another program, its
598 * stdin is in cooked mode. We do not switch stdin to binary
599 * mode if fd0 is zero, since that means we were called before
600 * tty was reopened in open_getchr, in which case we would be
601 * changing the original stdin device outside less.
604 setmode(0, on ? O_BINARY : O_TEXT);
608 werase_char = CONTROL('W');
618 * Some glue to prevent calling termcap functions if tgetent() failed.
628 strcpy(name, "LESS_TERMCAP_");
629 strcat(name, capname);
630 return (lgetenv(name));
639 if ((s = ltget_env(capname)) != NULL)
640 return (*s != '\0' && *s != '0');
643 return (tgetflag(capname));
652 if ((s = ltget_env(capname)) != NULL)
656 return (tgetnum(capname));
660 ltgetstr(capname, pp)
666 if ((s = ltget_env(capname)) != NULL)
670 return (tgetstr(capname, pp));
672 #endif /* MSDOS_COMPILER */
675 * Get size of the output screen.
687 #define DEF_SC_WIDTH 80
689 #define DEF_SC_HEIGHT 25
691 #define DEF_SC_HEIGHT 24
695 sys_width = sys_height = 0;
697 #if MSDOS_COMPILER==MSOFTC
699 struct videoconfig w;
701 sys_height = w.numtextrows;
702 sys_width = w.numtextcols;
705 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
709 sys_height = w.screenheight;
710 sys_width = w.screenwidth;
713 #if MSDOS_COMPILER==WIN32C
715 CONSOLE_SCREEN_BUFFER_INFO scr;
716 GetConsoleScreenBufferInfo(con_out, &scr);
717 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
718 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
728 * When using terminal emulators for XFree86/OS2, the
729 * _scrsize function does not work well.
730 * Call the scrsize.exe program to get the window size.
732 windowid = getenv("WINDOWID");
733 if (windowid != NULL)
735 FILE *fd = popen("scrsize", "rt");
739 fscanf(fd, "%i %i", &w, &h);
753 if (ioctl(2, TIOCGWINSZ, &w) == 0)
756 sys_height = w.ws_row;
758 sys_width = w.ws_col;
765 if (ioctl(2, WIOCGETD, &w) == 0)
768 sys_height = w.uw_height / w.uw_vs;
770 sys_width = w.uw_width / w.uw_hs;
781 sc_height = sys_height;
782 else if ((s = lgetenv("LINES")) != NULL)
785 else if ((n = ltgetnum("li")) > 0)
789 sc_height = DEF_SC_HEIGHT;
792 sc_width = sys_width;
793 else if ((s = lgetenv("COLUMNS")) != NULL)
796 else if ((n = ltgetnum("co")) > 0)
800 sc_width = DEF_SC_WIDTH;
803 #if MSDOS_COMPILER==MSOFTC
805 * Figure out how many empty loops it takes to delay a millisecond.
813 * Get synchronized at the start of a tick.
816 while (clock() == start)
819 * Now count loops till the next tick.
823 while (clock() == start)
826 * Convert from (loops per clock) to (loops per millisecond).
828 msec_loops *= CLOCKS_PER_SEC;
833 * Delay for a specified number of milliseconds.
838 static long delay_dummy = 0;
850 for (i = 0; i < msec_loops; i++)
853 * Make it look like we're doing something here,
854 * so the optimizer doesn't remove the whole loop.
863 * Return the characters actually input by a "special" key.
869 static char tbuf[40];
871 #if MSDOS_COMPILER || OS2
872 static char k_right[] = { '\340', PCK_RIGHT, 0 };
873 static char k_left[] = { '\340', PCK_LEFT, 0 };
874 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
875 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
876 static char k_insert[] = { '\340', PCK_INSERT, 0 };
877 static char k_delete[] = { '\340', PCK_DELETE, 0 };
878 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
879 static char k_ctl_backspace[] = { '\177', 0 };
880 static char k_home[] = { '\340', PCK_HOME, 0 };
881 static char k_end[] = { '\340', PCK_END, 0 };
882 static char k_up[] = { '\340', PCK_UP, 0 };
883 static char k_down[] = { '\340', PCK_DOWN, 0 };
884 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
885 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
886 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
887 static char k_f1[] = { '\340', PCK_F1, 0 };
897 * If windowid is not NULL, assume less is executed in
898 * the XFree86 environment.
901 s = windowid ? ltgetstr("kr", &sp) : k_right;
904 s = windowid ? ltgetstr("kl", &sp) : k_left;
907 s = windowid ? ltgetstr("ku", &sp) : k_up;
910 s = windowid ? ltgetstr("kd", &sp) : k_down;
913 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
916 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
919 s = windowid ? ltgetstr("kh", &sp) : k_home;
922 s = windowid ? ltgetstr("@7", &sp) : k_end;
927 s = ltgetstr("kD", &sp);
967 #if MSDOS_COMPILER || OS2
971 case SK_CTL_LEFT_ARROW:
974 case SK_CTL_RIGHT_ARROW:
977 case SK_CTL_BACKSPACE:
991 s = ltgetstr("kr", &sp);
994 s = ltgetstr("kl", &sp);
997 s = ltgetstr("ku", &sp);
1000 s = ltgetstr("kd", &sp);
1003 s = ltgetstr("kP", &sp);
1006 s = ltgetstr("kN", &sp);
1009 s = ltgetstr("kh", &sp);
1012 s = ltgetstr("@7", &sp);
1015 s = ltgetstr("kD", &sp);
1025 tbuf[0] = CONTROL('K');
1036 * Get terminal capabilities via termcap.
1047 * Set up default colors.
1048 * The xx_s_width and xx_e_width vars are already initialized to 0.
1050 #if MSDOS_COMPILER==MSOFTC
1051 sy_bg_color = _getbkcolor();
1052 sy_fg_color = _gettextcolor();
1055 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1059 sy_bg_color = (w.attribute >> 4) & 0x0F;
1060 sy_fg_color = (w.attribute >> 0) & 0x0F;
1063 #if MSDOS_COMPILER==WIN32C
1066 CONSOLE_SCREEN_BUFFER_INFO scr;
1068 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1070 * Always open stdin in binary. Note this *must* be done
1071 * before any file operations have been done on fd0.
1074 GetConsoleScreenBufferInfo(con_out, &scr);
1075 ReadConsoleOutputAttribute(con_out, &curr_attr,
1076 1, scr.dwCursorPosition, &nread);
1077 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1078 sy_fg_color = curr_attr & FG_COLORS;
1083 nm_fg_color = sy_fg_color;
1084 nm_bg_color = sy_bg_color;
1095 * Get size of the screen.
1101 #else /* !MSDOS_COMPILER */
1104 register char *t1, *t2;
1106 char termbuf[TERMBUF_SIZE];
1108 static char sbuf[TERMSBUF_SIZE];
1112 * Make sure the termcap database is available.
1114 sp = lgetenv("TERMCAP");
1115 if (sp == NULL || *sp == '\0')
1118 if ((sp = homefile("termcap.dat")) != NULL)
1120 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1121 sprintf(termcap, "TERMCAP=%s", sp);
1128 * Find out what kind of terminal this is.
1130 if ((term = lgetenv("TERM")) == NULL)
1131 term = DEFAULT_TERM;
1133 if (tgetent(termbuf, term) <= 0)
1135 if (ltgetflag("hc"))
1139 * Get size of the screen.
1144 auto_wrap = ltgetflag("am");
1145 ignaw = ltgetflag("xn");
1146 above_mem = ltgetflag("da");
1147 below_mem = ltgetflag("db");
1148 clear_bg = ltgetflag("ut");
1151 * Assumes termcap variable "sg" is the printing width of:
1152 * the standout sequence, the end standout sequence,
1153 * the underline sequence, the end underline sequence,
1154 * the boldface sequence, and the end boldface sequence.
1156 if ((so_s_width = ltgetnum("sg")) < 0)
1158 so_e_width = so_s_width;
1160 bo_s_width = bo_e_width = so_s_width;
1161 ul_s_width = ul_e_width = so_s_width;
1162 bl_s_width = bl_e_width = so_s_width;
1165 if (so_s_width > 0 || so_e_width > 0)
1167 * Disable highlighting by default on magic cookie terminals.
1168 * Turning on highlighting might change the displayed width
1169 * of a line, causing the display to get messed up.
1170 * The user can turn it back on with -g,
1171 * but she won't like the results.
1177 * Get various string-valued capabilities.
1182 sc_pad = ltgetstr("pc", &sp);
1187 sc_s_keypad = ltgetstr("ks", &sp);
1188 if (sc_s_keypad == NULL)
1190 sc_e_keypad = ltgetstr("ke", &sp);
1191 if (sc_e_keypad == NULL)
1195 * This loses for terminals with termcap entries with ti/te strings
1196 * that switch to/from an alternate screen, and we're in quit_at_eof
1199 if (!quit_at_eof && !more_mode) {
1200 sc_init = ltgetstr("ti", &sp);
1201 sc_deinit = ltgetstr("te", &sp);
1204 if (sc_init == NULL)
1207 if (sc_deinit == NULL)
1210 sc_eol_clear = ltgetstr("ce", &sp);
1211 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1217 sc_eos_clear = ltgetstr("cd", &sp);
1218 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1224 sc_clear = ltgetstr("cl", &sp);
1225 if (sc_clear == NULL || *sc_clear == '\0')
1231 sc_move = ltgetstr("cm", &sp);
1232 if (sc_move == NULL || *sc_move == '\0')
1235 * This is not an error here, because we don't
1236 * always need sc_move.
1237 * We need it only if we don't have home or lower-left.
1244 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1245 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1246 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1247 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1249 sc_visual_bell = ltgetstr("vb", &sp);
1250 if (sc_visual_bell == NULL)
1251 sc_visual_bell = "";
1253 if (ltgetflag("bs"))
1254 sc_backspace = "\b";
1257 sc_backspace = ltgetstr("bc", &sp);
1258 if (sc_backspace == NULL || *sc_backspace == '\0')
1259 sc_backspace = "\b";
1263 * Choose between using "ho" and "cm" ("home" and "cursor move")
1264 * to move the cursor to the upper left corner of the screen.
1266 t1 = ltgetstr("ho", &sp);
1269 if (*sc_move == '\0')
1273 strcpy(sp, tgoto(sc_move, 0, 0));
1275 sp += strlen(sp) + 1;
1277 sc_home = cheaper(t1, t2, "|\b^");
1280 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1281 * to move the cursor to the lower left corner of the screen.
1283 t1 = ltgetstr("ll", &sp);
1286 if (*sc_move == '\0')
1290 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1292 sp += strlen(sp) + 1;
1294 sc_lower_left = cheaper(t1, t2, "\r");
1297 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1298 * to add a line at the top of the screen.
1300 t1 = ltgetstr("al", &sp);
1303 t2 = ltgetstr("sr", &sp);
1307 if (*t1 == '\0' && *t2 == '\0')
1314 sc_addline = cheaper(t1, t2, "");
1315 if (*sc_addline == '\0')
1318 * Force repaint on any backward movement.
1322 #endif /* MSDOS_COMPILER */
1327 * Return the cost of displaying a termcap string.
1328 * We use the trick of calling tputs, but as a char printing function
1329 * we give it inc_costcount, which just increments "costcount".
1330 * This tells us how many chars would be printed by using this string.
1331 * {{ Couldn't we just use strlen? }}
1333 static int costcount;
1349 tputs(t, sc_height, inc_costcount);
1354 * Return the "best" of the two given termcap strings.
1355 * The best, if both exist, is the one with the lower
1356 * cost (see cost() function).
1359 cheaper(t1, t2, def)
1363 if (*t1 == '\0' && *t2 == '\0')
1372 if (cost(t1) < cost(t2))
1378 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1387 *instr = ltgetstr(incap, spp);
1392 *outstr = def_outstr;
1396 *outstr = ltgetstr(outcap, spp);
1397 if (*outstr == NULL)
1398 /* No specific out capability; use "me". */
1399 *outstr = ltgetstr("me", spp);
1400 if (*outstr == NULL)
1401 /* Don't even have "me"; use a null string. */
1405 #endif /* MSDOS_COMPILER */
1409 * Below are the functions which perform all the
1410 * terminal-specific screen manipulation.
1416 #if MSDOS_COMPILER==WIN32C
1418 _settextposition(int row, int col)
1421 CONSOLE_SCREEN_BUFFER_INFO csbi;
1423 GetConsoleScreenBufferInfo(con_out, &csbi);
1424 cpos.X = csbi.srWindow.Left + (col - 1);
1425 cpos.Y = csbi.srWindow.Top + (row - 1);
1426 SetConsoleCursorPosition(con_out, cpos);
1431 * Initialize the screen to the correct color at startup.
1436 SETCOLORS(nm_fg_color, nm_bg_color);
1439 * This clears the screen at startup. This is different from
1440 * the behavior of other versions of less. Disable it for now.
1447 * Create a complete, blank screen using "normal" colors.
1449 SETCOLORS(nm_fg_color, nm_bg_color);
1450 blanks = (char *) ecalloc(width+1, sizeof(char));
1451 for (col = 0; col < sc_width; col++)
1453 blanks[sc_width] = '\0';
1454 for (row = 0; row < sc_height; row++)
1461 #if MSDOS_COMPILER==WIN32C
1464 * Termcap-like init with a private win32 console.
1469 CONSOLE_SCREEN_BUFFER_INFO scr;
1472 if (con_out_save == INVALID_HANDLE_VALUE)
1475 GetConsoleScreenBufferInfo(con_out_save, &scr);
1477 if (con_out_ours == INVALID_HANDLE_VALUE)
1480 * Create our own screen buffer, so that we
1481 * may restore the original when done.
1483 con_out_ours = CreateConsoleScreenBuffer(
1484 GENERIC_WRITE | GENERIC_READ,
1485 FILE_SHARE_WRITE | FILE_SHARE_READ,
1486 (LPSECURITY_ATTRIBUTES) NULL,
1487 CONSOLE_TEXTMODE_BUFFER,
1491 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1492 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1493 SetConsoleScreenBufferSize(con_out_ours, size);
1494 SetConsoleActiveScreenBuffer(con_out_ours);
1495 con_out = con_out_ours;
1499 * Restore the startup console.
1504 if (con_out_save == INVALID_HANDLE_VALUE)
1507 (void) CloseHandle(con_out_ours);
1508 SetConsoleActiveScreenBuffer(con_out_save);
1509 con_out = con_out_save;
1515 * Initialize terminal
1522 tputs(sc_init, sc_height, putchr);
1524 tputs(sc_s_keypad, sc_height, putchr);
1530 * This is nice to terminals with no alternate screen,
1531 * but with saved scrolled-off-the-top lines. This way,
1532 * no previous line is lost, but we start with a whole
1533 * screen to ourself.
1535 for (i = 1; i < sc_height; i++)
1539 #if MSDOS_COMPILER==WIN32C
1550 * Deinitialize terminal
1559 tputs(sc_e_keypad, sc_height, putchr);
1561 tputs(sc_deinit, sc_height, putchr);
1563 /* Restore system colors. */
1564 SETCOLORS(sy_fg_color, sy_bg_color);
1565 #if MSDOS_COMPILER==WIN32C
1567 win32_deinit_term();
1569 /* Need clreol to make SETCOLORS take effect. */
1577 * Home cursor (move to upper left corner of screen).
1583 tputs(sc_home, 1, putchr);
1586 _settextposition(1,1);
1591 * Add a blank line (called with cursor at home).
1592 * Should scroll the display down.
1598 tputs(sc_addline, sc_height, putchr);
1601 #if MSDOS_COMPILER==MSOFTC
1602 _scrolltextwindow(_GSCROLLDOWN);
1603 _settextposition(1,1);
1605 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1606 movetext(1,1, sc_width,sc_height-1, 1,2);
1610 #if MSDOS_COMPILER==WIN32C
1613 SMALL_RECT rcSrc, rcClip;
1615 CONSOLE_SCREEN_BUFFER_INFO csbi;
1617 GetConsoleScreenBufferInfo(con_out,&csbi);
1619 /* The clip rectangle is the entire visible screen. */
1620 rcClip.Left = csbi.srWindow.Left;
1621 rcClip.Top = csbi.srWindow.Top;
1622 rcClip.Right = csbi.srWindow.Right;
1623 rcClip.Bottom = csbi.srWindow.Bottom;
1625 /* The source rectangle is the visible screen minus the last line. */
1629 /* Move the top left corner of the source window down one row. */
1630 new_org.X = rcSrc.Left;
1631 new_org.Y = rcSrc.Top + 1;
1633 /* Fill the right character and attributes. */
1634 fillchar.Char.AsciiChar = ' ';
1635 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1636 fillchar.Attributes = curr_attr;
1637 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1638 _settextposition(1,1);
1648 * Remove the n topmost lines and scroll everything below it in the
1649 * window upward. This is needed to stop leaking the topmost line
1650 * into the scrollback buffer when we go down-one-line (in WIN32).
1656 #if MSDOS_COMPILER==WIN32C
1657 SMALL_RECT rcSrc, rcClip;
1660 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1662 if (n >= sc_height - 1)
1671 GetConsoleScreenBufferInfo(con_out, &csbi);
1673 /* Get the extent of all-visible-rows-but-the-last. */
1674 rcSrc.Left = csbi.srWindow.Left;
1675 rcSrc.Top = csbi.srWindow.Top + n;
1676 rcSrc.Right = csbi.srWindow.Right;
1677 rcSrc.Bottom = csbi.srWindow.Bottom;
1679 /* Get the clip rectangle. */
1680 rcClip.Left = rcSrc.Left;
1681 rcClip.Top = csbi.srWindow.Top;
1682 rcClip.Right = rcSrc.Right;
1683 rcClip.Bottom = rcSrc.Bottom ;
1685 /* Move the source window up n rows. */
1686 new_org.X = rcSrc.Left;
1687 new_org.Y = rcSrc.Top - n;
1689 /* Fill the right character and attributes. */
1690 fillchar.Char.AsciiChar = ' ';
1691 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1692 fillchar.Attributes = curr_attr;
1694 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1696 /* Position cursor on first blank line. */
1697 goto_line(sc_height - n - 1);
1702 #if MSDOS_COMPILER==WIN32C
1710 * This will clear only the currently visible rows of the NT
1711 * console buffer, which means none of the precious scrollback
1712 * rows are touched making for faster scrolling. Note that, if
1713 * the window has fewer columns than the console buffer (i.e.
1714 * there is a horizontal scrollbar as well), the entire width
1715 * of the visible rows will be cleared.
1720 CONSOLE_SCREEN_BUFFER_INFO csbi;
1722 /* get the number of cells in the current buffer */
1723 GetConsoleScreenBufferInfo(con_out, &csbi);
1724 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1726 topleft.Y = csbi.srWindow.Top;
1728 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1729 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1730 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1734 * Remove the n topmost lines and scroll everything below it in the
1741 SMALL_RECT rcSrc, rcClip;
1747 CONSOLE_SCREEN_BUFFER_INFO csbi;
1752 if (n >= sc_height - 1)
1755 _settextposition(1,1);
1759 /* Get the extent of what will remain visible after scrolling. */
1760 GetConsoleScreenBufferInfo(con_out, &csbi);
1761 rcSrc.Left = csbi.srWindow.Left;
1762 rcSrc.Top = csbi.srWindow.Top + n;
1763 rcSrc.Right = csbi.srWindow.Right;
1764 rcSrc.Bottom = csbi.srWindow.Bottom;
1766 /* Get the clip rectangle. */
1767 rcClip.Left = rcSrc.Left;
1768 rcClip.Top = csbi.srWindow.Top;
1769 rcClip.Right = rcSrc.Right;
1770 rcClip.Bottom = rcSrc.Bottom ;
1772 /* Move the source text to the top of the screen. */
1773 new_org.X = rcSrc.Left;
1776 /* Fill the right character and attributes. */
1777 fillchar.Char.AsciiChar = ' ';
1778 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1780 /* Scroll the window. */
1781 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1782 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1784 /* Clear remaining lines at bottom. */
1785 topleft.X = csbi.dwCursorPosition.X;
1786 topleft.Y = rcSrc.Bottom - n;
1787 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1788 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1790 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1792 SetConsoleTextAttribute(con_out, curr_attr);
1794 /* Move cursor n lines up from where it was. */
1795 csbi.dwCursorPosition.Y -= n;
1796 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1801 * Move cursor to lower left corner of screen.
1807 tputs(sc_lower_left, 1, putchr);
1810 _settextposition(sc_height, 1);
1815 * Check if the console size has changed and reset internals
1816 * (in lieu of SIGWINCH for WIN32).
1821 #if MSDOS_COMPILER==WIN32C
1822 CONSOLE_SCREEN_BUFFER_INFO scr;
1825 if (con_out == INVALID_HANDLE_VALUE)
1829 GetConsoleScreenBufferInfo(con_out, &scr);
1830 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1831 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1832 if (size.Y != sc_height || size.X != sc_width)
1836 if (!no_init && con_out_ours == con_out)
1837 SetConsoleScreenBufferSize(con_out, size);
1839 wscroll = (sc_height + 1) / 2;
1846 * Goto a specific line on the screen.
1853 tputs(tgoto(sc_move, 0, slinenum), 1, putchr);
1856 _settextposition(slinenum+1, 1);
1860 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1862 * Create an alternate screen which is all white.
1863 * This screen is used to create a "flash" effect, by displaying it
1864 * briefly and then switching back to the normal screen.
1865 * {{ Yuck! There must be a better way to get a visual bell. }}
1870 #if MSDOS_COMPILER==MSOFTC
1871 struct videoconfig w;
1875 _getvideoconfig(&w);
1876 videopages = w.numvideopages;
1879 at_enter(AT_STANDOUT);
1884 at_enter(AT_STANDOUT);
1885 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
1886 for (col = 0; col < w.numtextcols; col++)
1888 for (row = w.numtextrows; row > 0; row--)
1889 _outmem(blanks, w.numtextcols);
1896 #if MSDOS_COMPILER==BORLANDC
1899 whitescreen = (unsigned short *)
1900 malloc(sc_width * sc_height * sizeof(short));
1901 if (whitescreen == NULL)
1903 for (n = 0; n < sc_width * sc_height; n++)
1904 whitescreen[n] = 0x7020;
1906 #if MSDOS_COMPILER==WIN32C
1909 whitescreen = (WORD *)
1910 malloc(sc_height * sc_width * sizeof(WORD));
1911 if (whitescreen == NULL)
1913 /* Invert the standard colors. */
1914 for (n = 0; n < sc_width * sc_height; n++)
1915 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
1921 #endif /* MSDOS_COMPILER */
1924 * Output the "visual bell", if there is one.
1930 if (*sc_visual_bell == '\0')
1932 tputs(sc_visual_bell, sc_height, putchr);
1934 #if MSDOS_COMPILER==DJGPPC
1937 #if MSDOS_COMPILER==MSOFTC
1939 * Create a flash screen on the second video page.
1940 * Switch to that page, then switch back.
1950 #if MSDOS_COMPILER==BORLANDC
1951 unsigned short *currscreen;
1954 * Get a copy of the current screen.
1955 * Display the flash screen.
1956 * Then restore the old screen.
1960 if (whitescreen == NULL)
1962 currscreen = (unsigned short *)
1963 malloc(sc_width * sc_height * sizeof(short));
1964 if (currscreen == NULL) return;
1965 gettext(1, 1, sc_width, sc_height, currscreen);
1966 puttext(1, 1, sc_width, sc_height, whitescreen);
1968 puttext(1, 1, sc_width, sc_height, currscreen);
1971 #if MSDOS_COMPILER==WIN32C
1972 /* paint screen with an inverse color */
1975 /* leave it displayed for 100 msec. */
1978 /* restore with a redraw */
1994 putchr(CONTROL('G'));
1996 #if MSDOS_COMPILER==WIN32C
2005 * Ring the terminal bell.
2010 if (quiet == VERY_QUIET)
2023 tputs(sc_clear, sc_height, putchr);
2026 #if MSDOS_COMPILER==WIN32C
2029 _clearscreen(_GCLEARSCREEN);
2035 * Clear from the cursor to the end of the cursor's line.
2036 * {{ This must not move the cursor. }}
2042 tputs(sc_eol_clear, 1, putchr);
2044 #if MSDOS_COMPILER==MSOFTC
2047 struct rccoord tpos;
2051 * Save current state.
2053 tpos = _gettextposition();
2054 _gettextwindow(&top, &left, &bot, &right);
2056 * Set a temporary window to the current line,
2057 * from the cursor's position to the right edge of the screen.
2058 * Then clear that window.
2060 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2061 _clearscreen(_GWINDOW);
2065 _settextwindow(top, left, bot, right);
2066 _settextposition(tpos.row, tpos.col);
2068 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2072 #if MSDOS_COMPILER==WIN32C
2075 CONSOLE_SCREEN_BUFFER_INFO scr;
2078 memset(&scr, 0, sizeof(scr));
2079 GetConsoleScreenBufferInfo(con_out, &scr);
2080 cpos.X = scr.dwCursorPosition.X;
2081 cpos.Y = scr.dwCursorPosition.Y;
2082 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2083 FillConsoleOutputAttribute(con_out, curr_attr,
2084 scr.dwSize.X - cpos.X, cpos, &nchars);
2085 FillConsoleOutputCharacter(con_out, ' ',
2086 scr.dwSize.X - cpos.X, cpos, &nchars);
2094 * Clear the current line.
2095 * Clear the screen if there's off-screen memory below the display.
2104 tputs(sc_eos_clear, 1, putchr);
2106 tputs(sc_eol_clear, 1, putchr);
2111 * Clear the bottom line of the display.
2112 * Leave the cursor at the beginning of the bottom line.
2118 * If we're in a non-normal attribute mode, temporarily exit
2119 * the mode while we do the clear. Some terminals fill the
2120 * cleared area with the current attribute.
2123 if (attrmode == AT_NORMAL)
2127 int saved_attrmode = attrmode;
2131 at_enter(saved_attrmode);
2139 attr = apply_at_specials(attr);
2142 /* The one with the most priority is last. */
2143 if (attr & AT_UNDERLINE)
2144 tputs(sc_u_in, 1, putchr);
2146 tputs(sc_b_in, 1, putchr);
2147 if (attr & AT_BLINK)
2148 tputs(sc_bl_in, 1, putchr);
2149 if (attr & AT_STANDOUT)
2150 tputs(sc_s_in, 1, putchr);
2153 /* The one with the most priority is first. */
2154 if (attr & AT_STANDOUT)
2156 SETCOLORS(so_fg_color, so_bg_color);
2157 } else if (attr & AT_BLINK)
2159 SETCOLORS(bl_fg_color, bl_bg_color);
2161 else if (attr & AT_BOLD)
2163 SETCOLORS(bo_fg_color, bo_bg_color);
2165 else if (attr & AT_UNDERLINE)
2167 SETCOLORS(ul_fg_color, ul_bg_color);
2178 /* Undo things in the reverse order we did them. */
2179 if (attrmode & AT_STANDOUT)
2180 tputs(sc_s_out, 1, putchr);
2181 if (attrmode & AT_BLINK)
2182 tputs(sc_bl_out, 1, putchr);
2183 if (attrmode & AT_BOLD)
2184 tputs(sc_b_out, 1, putchr);
2185 if (attrmode & AT_UNDERLINE)
2186 tputs(sc_u_out, 1, putchr);
2189 SETCOLORS(nm_fg_color, nm_bg_color);
2192 attrmode = AT_NORMAL;
2199 if (apply_at_specials(attr) != attrmode)
2207 is_at_equiv(attr1, attr2)
2211 attr1 = apply_at_specials(attr1);
2212 attr2 = apply_at_specials(attr2);
2214 return (attr1 == attr2);
2218 apply_at_specials(attr)
2221 if (attr & AT_BINARY)
2223 if (attr & AT_HILITE)
2224 attr |= AT_STANDOUT;
2225 attr &= ~(AT_BINARY|AT_HILITE);
2230 #if 0 /* No longer used */
2232 * Erase the character to the left of the cursor
2233 * and move the cursor left.
2240 * Erase the previous character by overstriking with a space.
2242 tputs(sc_backspace, 1, putchr);
2244 tputs(sc_backspace, 1, putchr);
2246 #if MSDOS_COMPILER==MSOFTC
2247 struct rccoord tpos;
2250 tpos = _gettextposition();
2253 _settextposition(tpos.row, tpos.col-1);
2255 _settextposition(tpos.row, tpos.col-1);
2257 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2260 #if MSDOS_COMPILER==WIN32C
2263 CONSOLE_SCREEN_BUFFER_INFO scr;
2266 GetConsoleScreenBufferInfo(con_out, &scr);
2267 cpos = scr.dwCursorPosition;
2271 SetConsoleCursorPosition(con_out, cpos);
2272 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2273 SetConsoleCursorPosition(con_out, cpos);
2282 * Output a plain backspace, without erasing the previous char.
2288 tputs(sc_backspace, 1, putchr);
2294 #if MSDOS_COMPILER==MSOFTC
2295 struct rccoord tpos;
2296 tpos = _gettextposition();
2300 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2304 #if MSDOS_COMPILER==WIN32C
2305 CONSOLE_SCREEN_BUFFER_INFO scr;
2306 GetConsoleScreenBufferInfo(con_out, &scr);
2307 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2308 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2315 _settextposition(row, col-1);
2316 #endif /* MSDOS_COMPILER */
2319 #if MSDOS_COMPILER==WIN32C
2321 * Determine whether an input character is waiting to be read.
2333 currentKey.ascii = 0;
2334 currentKey.scan = 0;
2337 * Wait for a real key-down event, but
2338 * ignore SHIFT and CONTROL key events.
2342 PeekConsoleInput(tty, &ip, 1, &read);
2345 ReadConsoleInput(tty, &ip, 1, &read);
2346 } while (ip.EventType != KEY_EVENT ||
2347 ip.Event.KeyEvent.bKeyDown != TRUE ||
2348 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2349 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2350 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2351 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2353 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2354 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2355 keyCount = ip.Event.KeyEvent.wRepeatCount;
2357 if (ip.Event.KeyEvent.dwControlKeyState &
2358 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2360 switch (currentKey.scan)
2362 case PCK_ALT_E: /* letter 'E' */
2363 currentKey.ascii = 0;
2366 } else if (ip.Event.KeyEvent.dwControlKeyState &
2367 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2369 switch (currentKey.scan)
2371 case PCK_RIGHT: /* right arrow */
2372 currentKey.scan = PCK_CTL_RIGHT;
2374 case PCK_LEFT: /* left arrow */
2375 currentKey.scan = PCK_CTL_LEFT;
2377 case PCK_DELETE: /* delete */
2378 currentKey.scan = PCK_CTL_DELETE;
2386 * Read a character from the keyboard.
2394 if (pending_scancode)
2396 pending_scancode = 0;
2397 return ((char)(currentKey.scan & 0x00FF));
2400 while (win32_kbhit((HANDLE)tty) == FALSE)
2408 ascii = currentKey.ascii;
2410 * On PC's, the extended keys return a 2 byte sequence beginning
2411 * with '00', so if the ascii code is 00, the next byte will be
2412 * the lsb of the scan code.
2414 pending_scancode = (ascii == 0x00);
2415 return ((char)ascii);