2 * Copyright (C) 1984-2005 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines which deal with the characteristics of the terminal.
14 * Uses termcap to be as terminal-independent as possible.
22 #if MSDOS_COMPILER==MSOFTC
25 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
27 #if MSDOS_COMPILER==DJGPPC
32 #if MSDOS_COMPILER==WIN32C
42 #include <sys/ioctl.h>
45 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
66 #include <sys/signal.h>
70 #include <sys/stream.h>
76 #endif /* MSDOS_COMPILER */
79 * Check for broken termios package that forces you to manually
80 * set the line discipline.
83 #define MUST_SET_LINE_DISCIPLINE 1
85 #define MUST_SET_LINE_DISCIPLINE 0
89 #define DEFAULT_TERM "ansi"
90 static char *windowid;
92 #define DEFAULT_TERM "unknown"
95 #if MSDOS_COMPILER==MSOFTC
96 static int videopages;
97 static long msec_loops;
98 static int flash_created = 0;
99 #define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); }
102 #if MSDOS_COMPILER==BORLANDC
103 static unsigned short *whitescreen;
104 static int flash_created = 0;
106 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
107 #define _settextposition(y,x) gotoxy(x,y)
108 #define _clearscreen(m) clrscr()
109 #define _outtext(s) cputs(s)
110 #define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); }
111 extern int sc_height;
114 #if MSDOS_COMPILER==WIN32C
121 static int keyCount = 0;
122 static WORD curr_attr;
123 static int pending_scancode = 0;
124 static WORD *whitescreen;
126 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
127 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
128 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */
131 static void win32_init_term();
132 static void win32_deinit_term();
134 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
135 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
136 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
137 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \
138 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
139 error("SETCOLORS failed"); }
143 public int nm_fg_color; /* Color of normal text */
144 public int nm_bg_color;
145 public int bo_fg_color; /* Color of bold text */
146 public int bo_bg_color;
147 public int ul_fg_color; /* Color of underlined text */
148 public int ul_bg_color;
149 public int so_fg_color; /* Color of standout text */
150 public int so_bg_color;
151 public int bl_fg_color; /* Color of blinking text */
152 public int bl_bg_color;
153 static int sy_fg_color; /* Color of system text (before less) */
154 static int sy_bg_color;
159 * Strings passed to tputs() to do various terminal functions.
162 *sc_pad, /* Pad string */
163 *sc_home, /* Cursor home */
164 *sc_addline, /* Add line, scroll down following lines */
165 *sc_lower_left, /* Cursor to last line, first column */
166 *sc_move, /* General cursor positioning */
167 *sc_clear, /* Clear screen */
168 *sc_eol_clear, /* Clear to end of line */
169 *sc_eos_clear, /* Clear to end of screen */
170 *sc_s_in, /* Enter standout (highlighted) mode */
171 *sc_s_out, /* Exit standout mode */
172 *sc_u_in, /* Enter underline mode */
173 *sc_u_out, /* Exit underline mode */
174 *sc_b_in, /* Enter bold mode */
175 *sc_b_out, /* Exit bold mode */
176 *sc_bl_in, /* Enter blink mode */
177 *sc_bl_out, /* Exit blink mode */
178 *sc_visual_bell, /* Visual bell (flash screen) sequence */
179 *sc_backspace, /* Backspace cursor */
180 *sc_s_keypad, /* Start keypad mode */
181 *sc_e_keypad, /* End keypad mode */
182 *sc_init, /* Startup terminal initialization */
183 *sc_deinit; /* Exit terminal de-initialization */
186 static int init_done = 0;
188 public int auto_wrap; /* Terminal does \r\n when write past margin */
189 public int ignaw; /* Terminal ignores \n immediately after wrap */
190 public int erase_char; /* The user's erase char */
191 public int erase2_char; /* The user's other erase char */
192 public int kill_char; /* The user's line-kill char */
193 public int werase_char; /* The user's word-erase char */
194 public int sc_width, sc_height; /* Height & width of screen */
195 public int bo_s_width, bo_e_width; /* Printing width of boldface seq */
196 public int ul_s_width, ul_e_width; /* Printing width of underline seq */
197 public int so_s_width, so_e_width; /* Printing width of standout seq */
198 public int bl_s_width, bl_e_width; /* Printing width of blink seq */
199 public int above_mem, below_mem; /* Memory retained above/below screen */
200 public int can_goto_line; /* Can move cursor to any line */
201 public int clear_bg; /* Clear fills with background color */
202 public int missing_cap = 0; /* Some capability is missing */
204 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 no_keypad;
232 extern int screen_trashed;
234 extern int top_scroll;
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 erase2_char = '\b'; /* in case OS doesn't know about erase2 */
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];
346 erase2_char = s.c_cc[VERASE2];
348 kill_char = s.c_cc[VKILL];
350 werase_char = s.c_cc[VWERASE];
352 werase_char = CONTROL('W');
356 * Set the modes to the way we want them.
418 #if MUST_SET_LINE_DISCIPLINE
420 * System's termios is broken; need to explicitly
421 * request TERMIODISC line discipline.
423 s.c_line = TERMIODISC;
428 * Restore saved modes.
435 tcsetattr(tty, TCSADRAIN, &s);
436 #if MUST_SET_LINE_DISCIPLINE
440 * Broken termios *ignores* any line discipline
441 * except TERMIODISC. A different old line discipline
442 * is therefore not restored, yet. Restore the old
443 * line discipline by hand.
445 ioctl(tty, TIOCSETD, &save_term.c_line);
453 static struct termio save_term;
454 static int saved_term = 0;
459 * Get terminal modes.
461 ioctl(tty, TCGETA, &s);
464 * Save modes and set certain variables dependent on modes.
472 ospeed = s.c_cflag & CBAUD;
474 erase_char = s.c_cc[VERASE];
475 kill_char = s.c_cc[VKILL];
477 werase_char = s.c_cc[VWERASE];
479 werase_char = CONTROL('W');
483 * Set the modes to the way we want them.
485 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
486 s.c_oflag |= (OPOST|ONLCR|TAB3);
487 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
493 * Restore saved modes.
497 ioctl(tty, TCSETAW, &s);
503 static struct sgttyb save_term;
504 static int saved_term = 0;
509 * Get terminal modes.
511 ioctl(tty, TIOCGETP, &s);
514 * Save modes and set certain variables dependent on modes.
522 ospeed = s.sg_ospeed;
524 erase_char = s.sg_erase;
525 kill_char = s.sg_kill;
526 werase_char = CONTROL('W');
529 * Set the modes to the way we want them.
531 s.sg_flags |= CBREAK;
532 s.sg_flags &= ~(ECHO|XTABS);
536 * Restore saved modes.
540 ioctl(tty, TIOCSETN, &s);
546 static struct sgbuf save_term;
547 static int saved_term = 0;
552 * Get terminal modes.
557 * Save modes and set certain variables dependent on modes.
564 erase_char = s.sg_bspch;
565 kill_char = s.sg_dlnch;
566 werase_char = CONTROL('W');
569 * Set the modes to the way we want them.
578 * Restore saved modes.
585 /* MS-DOS, Windows, or OS2 */
588 LSIGNAL(SIGINT, SIG_IGN);
591 #if MSDOS_COMPILER==DJGPPC
592 kill_char = CONTROL('U');
594 * So that when we shell out or run another program, its
595 * stdin is in cooked mode. We do not switch stdin to binary
596 * mode if fd0 is zero, since that means we were called before
597 * tty was reopened in open_getchr, in which case we would be
598 * changing the original stdin device outside less.
601 setmode(0, on ? O_BINARY : O_TEXT);
605 werase_char = CONTROL('W');
615 * Some glue to prevent calling termcap functions if tgetent() failed.
625 strcpy(name, "LESS_TERMCAP_");
626 strcat(name, capname);
627 return (lgetenv(name));
636 if ((s = ltget_env(capname)) != NULL)
637 return (*s != '\0' && *s != '0');
640 return (tgetflag(capname));
649 if ((s = ltget_env(capname)) != NULL)
653 return (tgetnum(capname));
657 ltgetstr(capname, pp)
663 if ((s = ltget_env(capname)) != NULL)
667 return (tgetstr(capname, pp));
669 #endif /* MSDOS_COMPILER */
672 * Get size of the output screen.
684 #define DEF_SC_WIDTH 80
686 #define DEF_SC_HEIGHT 25
688 #define DEF_SC_HEIGHT 24
692 sys_width = sys_height = 0;
694 #if MSDOS_COMPILER==MSOFTC
696 struct videoconfig w;
698 sys_height = w.numtextrows;
699 sys_width = w.numtextcols;
702 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
706 sys_height = w.screenheight;
707 sys_width = w.screenwidth;
710 #if MSDOS_COMPILER==WIN32C
712 CONSOLE_SCREEN_BUFFER_INFO scr;
713 GetConsoleScreenBufferInfo(con_out, &scr);
714 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
715 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
725 * When using terminal emulators for XFree86/OS2, the
726 * _scrsize function does not work well.
727 * Call the scrsize.exe program to get the window size.
729 windowid = getenv("WINDOWID");
730 if (windowid != NULL)
732 FILE *fd = popen("scrsize", "rt");
736 fscanf(fd, "%i %i", &w, &h);
750 if (ioctl(2, TIOCGWINSZ, &w) == 0)
753 sys_height = w.ws_row;
755 sys_width = w.ws_col;
762 if (ioctl(2, WIOCGETD, &w) == 0)
765 sys_height = w.uw_height / w.uw_vs;
767 sys_width = w.uw_width / w.uw_hs;
778 sc_height = sys_height;
779 else if ((s = lgetenv("LINES")) != NULL)
782 else if ((n = ltgetnum("li")) > 0)
786 sc_height = DEF_SC_HEIGHT;
789 sc_width = sys_width;
790 else if ((s = lgetenv("COLUMNS")) != NULL)
793 else if ((n = ltgetnum("co")) > 0)
797 sc_width = DEF_SC_WIDTH;
800 #if MSDOS_COMPILER==MSOFTC
802 * Figure out how many empty loops it takes to delay a millisecond.
810 * Get synchronized at the start of a tick.
813 while (clock() == start)
816 * Now count loops till the next tick.
820 while (clock() == start)
823 * Convert from (loops per clock) to (loops per millisecond).
825 msec_loops *= CLOCKS_PER_SEC;
830 * Delay for a specified number of milliseconds.
835 static long delay_dummy = 0;
847 for (i = 0; i < msec_loops; i++)
850 * Make it look like we're doing something here,
851 * so the optimizer doesn't remove the whole loop.
860 * Return the characters actually input by a "special" key.
866 static char tbuf[40];
868 #if MSDOS_COMPILER || OS2
869 static char k_right[] = { '\340', PCK_RIGHT, 0 };
870 static char k_left[] = { '\340', PCK_LEFT, 0 };
871 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
872 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
873 static char k_insert[] = { '\340', PCK_INSERT, 0 };
874 static char k_delete[] = { '\340', PCK_DELETE, 0 };
875 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
876 static char k_ctl_backspace[] = { '\177', 0 };
877 static char k_home[] = { '\340', PCK_HOME, 0 };
878 static char k_end[] = { '\340', PCK_END, 0 };
879 static char k_up[] = { '\340', PCK_UP, 0 };
880 static char k_down[] = { '\340', PCK_DOWN, 0 };
881 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
882 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
883 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
884 static char k_f1[] = { '\340', PCK_F1, 0 };
894 * If windowid is not NULL, assume less is executed in
895 * the XFree86 environment.
898 s = windowid ? ltgetstr("kr", &sp) : k_right;
901 s = windowid ? ltgetstr("kl", &sp) : k_left;
904 s = windowid ? ltgetstr("ku", &sp) : k_up;
907 s = windowid ? ltgetstr("kd", &sp) : k_down;
910 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
913 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
916 s = windowid ? ltgetstr("kh", &sp) : k_home;
919 s = windowid ? ltgetstr("@7", &sp) : k_end;
924 s = ltgetstr("kD", &sp);
964 #if MSDOS_COMPILER || OS2
968 case SK_CTL_LEFT_ARROW:
971 case SK_CTL_RIGHT_ARROW:
974 case SK_CTL_BACKSPACE:
988 s = ltgetstr("kr", &sp);
991 s = ltgetstr("kl", &sp);
994 s = ltgetstr("ku", &sp);
997 s = ltgetstr("kd", &sp);
1000 s = ltgetstr("kP", &sp);
1003 s = ltgetstr("kN", &sp);
1006 s = ltgetstr("kh", &sp);
1009 s = ltgetstr("@7", &sp);
1012 s = ltgetstr("kD", &sp);
1022 tbuf[0] = CONTROL('K');
1033 * Get terminal capabilities via termcap.
1044 * Set up default colors.
1045 * The xx_s_width and xx_e_width vars are already initialized to 0.
1047 #if MSDOS_COMPILER==MSOFTC
1048 sy_bg_color = _getbkcolor();
1049 sy_fg_color = _gettextcolor();
1052 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1056 sy_bg_color = (w.attribute >> 4) & 0x0F;
1057 sy_fg_color = (w.attribute >> 0) & 0x0F;
1060 #if MSDOS_COMPILER==WIN32C
1063 CONSOLE_SCREEN_BUFFER_INFO scr;
1065 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1067 * Always open stdin in binary. Note this *must* be done
1068 * before any file operations have been done on fd0.
1071 GetConsoleScreenBufferInfo(con_out, &scr);
1072 ReadConsoleOutputAttribute(con_out, &curr_attr,
1073 1, scr.dwCursorPosition, &nread);
1074 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1075 sy_fg_color = curr_attr & FG_COLORS;
1080 nm_fg_color = sy_fg_color;
1081 nm_bg_color = sy_bg_color;
1092 * Get size of the screen.
1098 #else /* !MSDOS_COMPILER */
1101 register char *t1, *t2;
1103 char termbuf[TERMBUF_SIZE];
1105 static char sbuf[TERMSBUF_SIZE];
1109 * Make sure the termcap database is available.
1111 sp = lgetenv("TERMCAP");
1112 if (sp == NULL || *sp == '\0')
1115 if ((sp = homefile("termcap.dat")) != NULL)
1117 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1118 sprintf(termcap, "TERMCAP=%s", sp);
1125 * Find out what kind of terminal this is.
1127 if ((term = lgetenv("TERM")) == NULL)
1128 term = DEFAULT_TERM;
1130 if (tgetent(termbuf, term) <= 0)
1132 if (ltgetflag("hc"))
1136 * Get size of the screen.
1141 auto_wrap = ltgetflag("am");
1142 ignaw = ltgetflag("xn");
1143 above_mem = ltgetflag("da");
1144 below_mem = ltgetflag("db");
1145 clear_bg = ltgetflag("ut");
1148 * Assumes termcap variable "sg" is the printing width of:
1149 * the standout sequence, the end standout sequence,
1150 * the underline sequence, the end underline sequence,
1151 * the boldface sequence, and the end boldface sequence.
1153 if ((so_s_width = ltgetnum("sg")) < 0)
1155 so_e_width = so_s_width;
1157 bo_s_width = bo_e_width = so_s_width;
1158 ul_s_width = ul_e_width = so_s_width;
1159 bl_s_width = bl_e_width = so_s_width;
1162 if (so_s_width > 0 || so_e_width > 0)
1164 * Disable highlighting by default on magic cookie terminals.
1165 * Turning on highlighting might change the displayed width
1166 * of a line, causing the display to get messed up.
1167 * The user can turn it back on with -g,
1168 * but she won't like the results.
1174 * Get various string-valued capabilities.
1179 sc_pad = ltgetstr("pc", &sp);
1184 sc_s_keypad = ltgetstr("ks", &sp);
1185 if (sc_s_keypad == NULL)
1187 sc_e_keypad = ltgetstr("ke", &sp);
1188 if (sc_e_keypad == NULL)
1191 sc_init = ltgetstr("ti", &sp);
1192 if (sc_init == NULL)
1195 sc_deinit= ltgetstr("te", &sp);
1196 if (sc_deinit == NULL)
1199 sc_eol_clear = ltgetstr("ce", &sp);
1200 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1206 sc_eos_clear = ltgetstr("cd", &sp);
1207 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1213 sc_clear = ltgetstr("cl", &sp);
1214 if (sc_clear == NULL || *sc_clear == '\0')
1220 sc_move = ltgetstr("cm", &sp);
1221 if (sc_move == NULL || *sc_move == '\0')
1224 * This is not an error here, because we don't
1225 * always need sc_move.
1226 * We need it only if we don't have home or lower-left.
1233 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1234 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1235 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1236 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1238 sc_visual_bell = ltgetstr("vb", &sp);
1239 if (sc_visual_bell == NULL)
1240 sc_visual_bell = "";
1242 if (ltgetflag("bs"))
1243 sc_backspace = "\b";
1246 sc_backspace = ltgetstr("bc", &sp);
1247 if (sc_backspace == NULL || *sc_backspace == '\0')
1248 sc_backspace = "\b";
1252 * Choose between using "ho" and "cm" ("home" and "cursor move")
1253 * to move the cursor to the upper left corner of the screen.
1255 t1 = ltgetstr("ho", &sp);
1258 if (*sc_move == '\0')
1262 strcpy(sp, tgoto(sc_move, 0, 0));
1264 sp += strlen(sp) + 1;
1266 sc_home = cheaper(t1, t2, "|\b^");
1269 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1270 * to move the cursor to the lower left corner of the screen.
1272 t1 = ltgetstr("ll", &sp);
1275 if (*sc_move == '\0')
1279 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1281 sp += strlen(sp) + 1;
1283 sc_lower_left = cheaper(t1, t2, "\r");
1286 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1287 * to add a line at the top of the screen.
1289 t1 = ltgetstr("al", &sp);
1292 t2 = ltgetstr("sr", &sp);
1296 if (*t1 == '\0' && *t2 == '\0')
1303 sc_addline = cheaper(t1, t2, "");
1304 if (*sc_addline == '\0')
1307 * Force repaint on any backward movement.
1311 #endif /* MSDOS_COMPILER */
1316 * Return the cost of displaying a termcap string.
1317 * We use the trick of calling tputs, but as a char printing function
1318 * we give it inc_costcount, which just increments "costcount".
1319 * This tells us how many chars would be printed by using this string.
1320 * {{ Couldn't we just use strlen? }}
1322 static int costcount;
1338 tputs(t, sc_height, inc_costcount);
1343 * Return the "best" of the two given termcap strings.
1344 * The best, if both exist, is the one with the lower
1345 * cost (see cost() function).
1348 cheaper(t1, t2, def)
1352 if (*t1 == '\0' && *t2 == '\0')
1361 if (cost(t1) < cost(t2))
1367 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1376 *instr = ltgetstr(incap, spp);
1381 *outstr = def_outstr;
1385 *outstr = ltgetstr(outcap, spp);
1386 if (*outstr == NULL)
1387 /* No specific out capability; use "me". */
1388 *outstr = ltgetstr("me", spp);
1389 if (*outstr == NULL)
1390 /* Don't even have "me"; use a null string. */
1394 #endif /* MSDOS_COMPILER */
1398 * Below are the functions which perform all the
1399 * terminal-specific screen manipulation.
1405 #if MSDOS_COMPILER==WIN32C
1407 _settextposition(int row, int col)
1410 CONSOLE_SCREEN_BUFFER_INFO csbi;
1412 GetConsoleScreenBufferInfo(con_out, &csbi);
1413 cpos.X = csbi.srWindow.Left + (col - 1);
1414 cpos.Y = csbi.srWindow.Top + (row - 1);
1415 SetConsoleCursorPosition(con_out, cpos);
1420 * Initialize the screen to the correct color at startup.
1425 SETCOLORS(nm_fg_color, nm_bg_color);
1428 * This clears the screen at startup. This is different from
1429 * the behavior of other versions of less. Disable it for now.
1436 * Create a complete, blank screen using "normal" colors.
1438 SETCOLORS(nm_fg_color, nm_bg_color);
1439 blanks = (char *) ecalloc(width+1, sizeof(char));
1440 for (col = 0; col < sc_width; col++)
1442 blanks[sc_width] = '\0';
1443 for (row = 0; row < sc_height; row++)
1450 #if MSDOS_COMPILER==WIN32C
1453 * Termcap-like init with a private win32 console.
1458 CONSOLE_SCREEN_BUFFER_INFO scr;
1461 if (con_out_save == INVALID_HANDLE_VALUE)
1464 GetConsoleScreenBufferInfo(con_out_save, &scr);
1466 if (con_out_ours == INVALID_HANDLE_VALUE)
1469 * Create our own screen buffer, so that we
1470 * may restore the original when done.
1472 con_out_ours = CreateConsoleScreenBuffer(
1473 GENERIC_WRITE | GENERIC_READ,
1474 FILE_SHARE_WRITE | FILE_SHARE_READ,
1475 (LPSECURITY_ATTRIBUTES) NULL,
1476 CONSOLE_TEXTMODE_BUFFER,
1480 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1481 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1482 SetConsoleScreenBufferSize(con_out_ours, size);
1483 SetConsoleActiveScreenBuffer(con_out_ours);
1484 con_out = con_out_ours;
1488 * Restore the startup console.
1493 if (con_out_save == INVALID_HANDLE_VALUE)
1496 (void) CloseHandle(con_out_ours);
1497 SetConsoleActiveScreenBuffer(con_out_save);
1498 con_out = con_out_save;
1504 * Initialize terminal
1511 tputs(sc_init, sc_height, putchr);
1513 tputs(sc_s_keypad, sc_height, putchr);
1519 * This is nice to terminals with no alternate screen,
1520 * but with saved scrolled-off-the-top lines. This way,
1521 * no previous line is lost, but we start with a whole
1522 * screen to ourself.
1524 for (i = 1; i < sc_height; i++)
1528 #if MSDOS_COMPILER==WIN32C
1539 * Deinitialize terminal
1548 tputs(sc_e_keypad, sc_height, putchr);
1550 tputs(sc_deinit, sc_height, putchr);
1552 /* Restore system colors. */
1553 SETCOLORS(sy_fg_color, sy_bg_color);
1554 #if MSDOS_COMPILER==WIN32C
1556 win32_deinit_term();
1558 /* Need clreol to make SETCOLORS take effect. */
1566 * Home cursor (move to upper left corner of screen).
1572 tputs(sc_home, 1, putchr);
1575 _settextposition(1,1);
1580 * Add a blank line (called with cursor at home).
1581 * Should scroll the display down.
1587 tputs(sc_addline, sc_height, putchr);
1590 #if MSDOS_COMPILER==MSOFTC
1591 _scrolltextwindow(_GSCROLLDOWN);
1592 _settextposition(1,1);
1594 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1595 movetext(1,1, sc_width,sc_height-1, 1,2);
1599 #if MSDOS_COMPILER==WIN32C
1602 SMALL_RECT rcSrc, rcClip;
1604 CONSOLE_SCREEN_BUFFER_INFO csbi;
1606 GetConsoleScreenBufferInfo(con_out,&csbi);
1608 /* The clip rectangle is the entire visible screen. */
1609 rcClip.Left = csbi.srWindow.Left;
1610 rcClip.Top = csbi.srWindow.Top;
1611 rcClip.Right = csbi.srWindow.Right;
1612 rcClip.Bottom = csbi.srWindow.Bottom;
1614 /* The source rectangle is the visible screen minus the last line. */
1618 /* Move the top left corner of the source window down one row. */
1619 new_org.X = rcSrc.Left;
1620 new_org.Y = rcSrc.Top + 1;
1622 /* Fill the right character and attributes. */
1623 fillchar.Char.AsciiChar = ' ';
1624 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1625 fillchar.Attributes = curr_attr;
1626 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1627 _settextposition(1,1);
1637 * Remove the n topmost lines and scroll everything below it in the
1638 * window upward. This is needed to stop leaking the topmost line
1639 * into the scrollback buffer when we go down-one-line (in WIN32).
1645 #if MSDOS_COMPILER==WIN32C
1646 SMALL_RECT rcSrc, rcClip;
1649 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1651 if (n >= sc_height - 1)
1660 GetConsoleScreenBufferInfo(con_out, &csbi);
1662 /* Get the extent of all-visible-rows-but-the-last. */
1663 rcSrc.Left = csbi.srWindow.Left;
1664 rcSrc.Top = csbi.srWindow.Top + n;
1665 rcSrc.Right = csbi.srWindow.Right;
1666 rcSrc.Bottom = csbi.srWindow.Bottom;
1668 /* Get the clip rectangle. */
1669 rcClip.Left = rcSrc.Left;
1670 rcClip.Top = csbi.srWindow.Top;
1671 rcClip.Right = rcSrc.Right;
1672 rcClip.Bottom = rcSrc.Bottom ;
1674 /* Move the source window up n rows. */
1675 new_org.X = rcSrc.Left;
1676 new_org.Y = rcSrc.Top - n;
1678 /* Fill the right character and attributes. */
1679 fillchar.Char.AsciiChar = ' ';
1680 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1681 fillchar.Attributes = curr_attr;
1683 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1685 /* Position cursor on first blank line. */
1686 goto_line(sc_height - n - 1);
1691 #if MSDOS_COMPILER==WIN32C
1699 * This will clear only the currently visible rows of the NT
1700 * console buffer, which means none of the precious scrollback
1701 * rows are touched making for faster scrolling. Note that, if
1702 * the window has fewer columns than the console buffer (i.e.
1703 * there is a horizontal scrollbar as well), the entire width
1704 * of the visible rows will be cleared.
1709 CONSOLE_SCREEN_BUFFER_INFO csbi;
1711 /* get the number of cells in the current buffer */
1712 GetConsoleScreenBufferInfo(con_out, &csbi);
1713 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1715 topleft.Y = csbi.srWindow.Top;
1717 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1718 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1719 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1723 * Remove the n topmost lines and scroll everything below it in the
1730 SMALL_RECT rcSrc, rcClip;
1736 CONSOLE_SCREEN_BUFFER_INFO csbi;
1741 if (n >= sc_height - 1)
1744 _settextposition(1,1);
1748 /* Get the extent of what will remain visible after scrolling. */
1749 GetConsoleScreenBufferInfo(con_out, &csbi);
1750 rcSrc.Left = csbi.srWindow.Left;
1751 rcSrc.Top = csbi.srWindow.Top + n;
1752 rcSrc.Right = csbi.srWindow.Right;
1753 rcSrc.Bottom = csbi.srWindow.Bottom;
1755 /* Get the clip rectangle. */
1756 rcClip.Left = rcSrc.Left;
1757 rcClip.Top = csbi.srWindow.Top;
1758 rcClip.Right = rcSrc.Right;
1759 rcClip.Bottom = rcSrc.Bottom ;
1761 /* Move the source text to the top of the screen. */
1762 new_org.X = rcSrc.Left;
1765 /* Fill the right character and attributes. */
1766 fillchar.Char.AsciiChar = ' ';
1767 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1769 /* Scroll the window. */
1770 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1771 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1773 /* Clear remaining lines at bottom. */
1774 topleft.X = csbi.dwCursorPosition.X;
1775 topleft.Y = rcSrc.Bottom - n;
1776 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1777 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1779 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1781 SetConsoleTextAttribute(con_out, curr_attr);
1783 /* Move cursor n lines up from where it was. */
1784 csbi.dwCursorPosition.Y -= n;
1785 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1790 * Move cursor to lower left corner of screen.
1796 tputs(sc_lower_left, 1, putchr);
1799 _settextposition(sc_height, 1);
1804 * Check if the console size has changed and reset internals
1805 * (in lieu of SIGWINCH for WIN32).
1810 #if MSDOS_COMPILER==WIN32C
1811 CONSOLE_SCREEN_BUFFER_INFO scr;
1814 if (con_out == INVALID_HANDLE_VALUE)
1818 GetConsoleScreenBufferInfo(con_out, &scr);
1819 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1820 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1821 if (size.Y != sc_height || size.X != sc_width)
1825 if (!no_init && con_out_ours == con_out)
1826 SetConsoleScreenBufferSize(con_out, size);
1828 wscroll = (sc_height + 1) / 2;
1835 * Goto a specific line on the screen.
1842 tputs(tgoto(sc_move, 0, slinenum), 1, putchr);
1845 _settextposition(slinenum+1, 1);
1849 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1851 * Create an alternate screen which is all white.
1852 * This screen is used to create a "flash" effect, by displaying it
1853 * briefly and then switching back to the normal screen.
1854 * {{ Yuck! There must be a better way to get a visual bell. }}
1859 #if MSDOS_COMPILER==MSOFTC
1860 struct videoconfig w;
1864 _getvideoconfig(&w);
1865 videopages = w.numvideopages;
1868 at_enter(AT_STANDOUT);
1873 at_enter(AT_STANDOUT);
1874 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
1875 for (col = 0; col < w.numtextcols; col++)
1877 for (row = w.numtextrows; row > 0; row--)
1878 _outmem(blanks, w.numtextcols);
1885 #if MSDOS_COMPILER==BORLANDC
1888 whitescreen = (unsigned short *)
1889 malloc(sc_width * sc_height * sizeof(short));
1890 if (whitescreen == NULL)
1892 for (n = 0; n < sc_width * sc_height; n++)
1893 whitescreen[n] = 0x7020;
1895 #if MSDOS_COMPILER==WIN32C
1898 whitescreen = (WORD *)
1899 malloc(sc_height * sc_width * sizeof(WORD));
1900 if (whitescreen == NULL)
1902 /* Invert the standard colors. */
1903 for (n = 0; n < sc_width * sc_height; n++)
1904 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
1910 #endif /* MSDOS_COMPILER */
1913 * Output the "visual bell", if there is one.
1919 if (*sc_visual_bell == '\0')
1921 tputs(sc_visual_bell, sc_height, putchr);
1923 #if MSDOS_COMPILER==DJGPPC
1926 #if MSDOS_COMPILER==MSOFTC
1928 * Create a flash screen on the second video page.
1929 * Switch to that page, then switch back.
1939 #if MSDOS_COMPILER==BORLANDC
1940 unsigned short *currscreen;
1943 * Get a copy of the current screen.
1944 * Display the flash screen.
1945 * Then restore the old screen.
1949 if (whitescreen == NULL)
1951 currscreen = (unsigned short *)
1952 malloc(sc_width * sc_height * sizeof(short));
1953 if (currscreen == NULL) return;
1954 gettext(1, 1, sc_width, sc_height, currscreen);
1955 puttext(1, 1, sc_width, sc_height, whitescreen);
1957 puttext(1, 1, sc_width, sc_height, currscreen);
1960 #if MSDOS_COMPILER==WIN32C
1961 /* paint screen with an inverse color */
1964 /* leave it displayed for 100 msec. */
1967 /* restore with a redraw */
1983 putchr(CONTROL('G'));
1985 #if MSDOS_COMPILER==WIN32C
1994 * Ring the terminal bell.
1999 if (quiet == VERY_QUIET)
2012 tputs(sc_clear, sc_height, putchr);
2015 #if MSDOS_COMPILER==WIN32C
2018 _clearscreen(_GCLEARSCREEN);
2024 * Clear from the cursor to the end of the cursor's line.
2025 * {{ This must not move the cursor. }}
2031 tputs(sc_eol_clear, 1, putchr);
2033 #if MSDOS_COMPILER==MSOFTC
2036 struct rccoord tpos;
2040 * Save current state.
2042 tpos = _gettextposition();
2043 _gettextwindow(&top, &left, &bot, &right);
2045 * Set a temporary window to the current line,
2046 * from the cursor's position to the right edge of the screen.
2047 * Then clear that window.
2049 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2050 _clearscreen(_GWINDOW);
2054 _settextwindow(top, left, bot, right);
2055 _settextposition(tpos.row, tpos.col);
2057 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2061 #if MSDOS_COMPILER==WIN32C
2064 CONSOLE_SCREEN_BUFFER_INFO scr;
2067 memset(&scr, 0, sizeof(scr));
2068 GetConsoleScreenBufferInfo(con_out, &scr);
2069 cpos.X = scr.dwCursorPosition.X;
2070 cpos.Y = scr.dwCursorPosition.Y;
2071 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2072 FillConsoleOutputAttribute(con_out, curr_attr,
2073 scr.dwSize.X - cpos.X, cpos, &nchars);
2074 FillConsoleOutputCharacter(con_out, ' ',
2075 scr.dwSize.X - cpos.X, cpos, &nchars);
2083 * Clear the current line.
2084 * Clear the screen if there's off-screen memory below the display.
2093 tputs(sc_eos_clear, 1, putchr);
2095 tputs(sc_eol_clear, 1, putchr);
2100 * Clear the bottom line of the display.
2101 * Leave the cursor at the beginning of the bottom line.
2107 * If we're in a non-normal attribute mode, temporarily exit
2108 * the mode while we do the clear. Some terminals fill the
2109 * cleared area with the current attribute.
2112 if (attrmode == AT_NORMAL)
2116 int saved_attrmode = attrmode;
2120 at_enter(saved_attrmode);
2128 attr = apply_at_specials(attr);
2131 /* The one with the most priority is last. */
2132 if (attr & AT_UNDERLINE)
2133 tputs(sc_u_in, 1, putchr);
2135 tputs(sc_b_in, 1, putchr);
2136 if (attr & AT_BLINK)
2137 tputs(sc_bl_in, 1, putchr);
2138 if (attr & AT_STANDOUT)
2139 tputs(sc_s_in, 1, putchr);
2142 /* The one with the most priority is first. */
2143 if (attr & AT_STANDOUT)
2145 SETCOLORS(so_fg_color, so_bg_color);
2146 } else if (attr & AT_BLINK)
2148 SETCOLORS(bl_fg_color, bl_bg_color);
2150 else if (attr & AT_BOLD)
2152 SETCOLORS(bo_fg_color, bo_bg_color);
2154 else if (attr & AT_UNDERLINE)
2156 SETCOLORS(ul_fg_color, ul_bg_color);
2167 /* Undo things in the reverse order we did them. */
2168 if (attrmode & AT_STANDOUT)
2169 tputs(sc_s_out, 1, putchr);
2170 if (attrmode & AT_BLINK)
2171 tputs(sc_bl_out, 1, putchr);
2172 if (attrmode & AT_BOLD)
2173 tputs(sc_b_out, 1, putchr);
2174 if (attrmode & AT_UNDERLINE)
2175 tputs(sc_u_out, 1, putchr);
2178 SETCOLORS(nm_fg_color, nm_bg_color);
2181 attrmode = AT_NORMAL;
2188 if (apply_at_specials(attr) != attrmode)
2196 is_at_equiv(attr1, attr2)
2200 attr1 = apply_at_specials(attr1);
2201 attr2 = apply_at_specials(attr2);
2203 return (attr1 == attr2);
2207 apply_at_specials(attr)
2210 if (attr & AT_BINARY)
2212 if (attr & AT_HILITE)
2213 attr |= AT_STANDOUT;
2214 attr &= ~(AT_BINARY|AT_HILITE);
2219 #if 0 /* No longer used */
2221 * Erase the character to the left of the cursor
2222 * and move the cursor left.
2229 * Erase the previous character by overstriking with a space.
2231 tputs(sc_backspace, 1, putchr);
2233 tputs(sc_backspace, 1, putchr);
2235 #if MSDOS_COMPILER==MSOFTC
2236 struct rccoord tpos;
2239 tpos = _gettextposition();
2242 _settextposition(tpos.row, tpos.col-1);
2244 _settextposition(tpos.row, tpos.col-1);
2246 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2249 #if MSDOS_COMPILER==WIN32C
2252 CONSOLE_SCREEN_BUFFER_INFO scr;
2255 GetConsoleScreenBufferInfo(con_out, &scr);
2256 cpos = scr.dwCursorPosition;
2260 SetConsoleCursorPosition(con_out, cpos);
2261 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2262 SetConsoleCursorPosition(con_out, cpos);
2271 * Output a plain backspace, without erasing the previous char.
2277 tputs(sc_backspace, 1, putchr);
2283 #if MSDOS_COMPILER==MSOFTC
2284 struct rccoord tpos;
2285 tpos = _gettextposition();
2289 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2293 #if MSDOS_COMPILER==WIN32C
2294 CONSOLE_SCREEN_BUFFER_INFO scr;
2295 GetConsoleScreenBufferInfo(con_out, &scr);
2296 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2297 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2304 _settextposition(row, col-1);
2305 #endif /* MSDOS_COMPILER */
2308 #if MSDOS_COMPILER==WIN32C
2310 * Determine whether an input character is waiting to be read.
2322 currentKey.ascii = 0;
2323 currentKey.scan = 0;
2326 * Wait for a real key-down event, but
2327 * ignore SHIFT and CONTROL key events.
2331 PeekConsoleInput(tty, &ip, 1, &read);
2334 ReadConsoleInput(tty, &ip, 1, &read);
2335 } while (ip.EventType != KEY_EVENT ||
2336 ip.Event.KeyEvent.bKeyDown != TRUE ||
2337 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2338 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2339 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2340 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2342 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2343 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2344 keyCount = ip.Event.KeyEvent.wRepeatCount;
2346 if (ip.Event.KeyEvent.dwControlKeyState &
2347 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2349 switch (currentKey.scan)
2351 case PCK_ALT_E: /* letter 'E' */
2352 currentKey.ascii = 0;
2355 } else if (ip.Event.KeyEvent.dwControlKeyState &
2356 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2358 switch (currentKey.scan)
2360 case PCK_RIGHT: /* right arrow */
2361 currentKey.scan = PCK_CTL_RIGHT;
2363 case PCK_LEFT: /* left arrow */
2364 currentKey.scan = PCK_CTL_LEFT;
2366 case PCK_DELETE: /* delete */
2367 currentKey.scan = PCK_CTL_DELETE;
2375 * Read a character from the keyboard.
2383 if (pending_scancode)
2385 pending_scancode = 0;
2386 return ((char)(currentKey.scan & 0x00FF));
2389 while (win32_kbhit((HANDLE)tty) == FALSE)
2397 ascii = currentKey.ascii;
2399 * On PC's, the extended keys return a 2 byte sequence beginning
2400 * with '00', so if the ascii code is 00, the next byte will be
2401 * the lsb of the scan code.
2403 pending_scancode = (ascii == 0x00);
2404 return ((char)ascii);