2 * Copyright (C) 1984-2012 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * Routines which deal with the characteristics of the terminal.
13 * Uses termcap to be as terminal-independent as possible.
21 #if MSDOS_COMPILER==MSOFTC
24 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
26 #if MSDOS_COMPILER==DJGPPC
31 #if MSDOS_COMPILER==WIN32C
41 #include <sys/ioctl.h>
44 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
65 #include <sys/signal.h>
69 #include <sys/stream.h>
75 #endif /* MSDOS_COMPILER */
78 * Check for broken termios package that forces you to manually
79 * set the line discipline.
82 #define MUST_SET_LINE_DISCIPLINE 1
84 #define MUST_SET_LINE_DISCIPLINE 0
88 #define DEFAULT_TERM "ansi"
89 static char *windowid;
91 #define DEFAULT_TERM "unknown"
94 #if MSDOS_COMPILER==MSOFTC
95 static int videopages;
96 static long msec_loops;
97 static int flash_created = 0;
98 #define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); }
101 #if MSDOS_COMPILER==BORLANDC
102 static unsigned short *whitescreen;
103 static int flash_created = 0;
105 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
106 #define _settextposition(y,x) gotoxy(x,y)
107 #define _clearscreen(m) clrscr()
108 #define _outtext(s) cputs(s)
109 #define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); }
110 extern int sc_height;
113 #if MSDOS_COMPILER==WIN32C
120 static int keyCount = 0;
121 static WORD curr_attr;
122 static int pending_scancode = 0;
123 static WORD *whitescreen;
125 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
126 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
127 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */
130 static void win32_init_term();
131 static void win32_deinit_term();
133 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
134 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
135 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
136 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \
137 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
138 error("SETCOLORS failed"); }
142 public int nm_fg_color; /* Color of normal text */
143 public int nm_bg_color;
144 public int bo_fg_color; /* Color of bold text */
145 public int bo_bg_color;
146 public int ul_fg_color; /* Color of underlined text */
147 public int ul_bg_color;
148 public int so_fg_color; /* Color of standout text */
149 public int so_bg_color;
150 public int bl_fg_color; /* Color of blinking text */
151 public int bl_bg_color;
152 static int sy_fg_color; /* Color of system text (before less) */
153 static int sy_bg_color;
158 * Strings passed to tputs() to do various terminal functions.
161 *sc_pad, /* Pad string */
162 *sc_home, /* Cursor home */
163 *sc_addline, /* Add line, scroll down following lines */
164 *sc_lower_left, /* Cursor to last line, first column */
165 *sc_return, /* Cursor to beginning of current line */
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;
237 extern int hilite_search;
240 extern char *tgetstr();
241 extern char *tgoto();
245 * Change terminal to "raw mode", or restore to "normal" mode.
247 * 1. An outstanding read will complete on receipt of a single keystroke.
248 * 2. Input is not echoed.
249 * 3. On output, \n is mapped to \r\n.
250 * 4. \t is NOT expanded into spaces.
251 * 5. Signal-causing characters such as ctrl-C (interrupt),
252 * etc. are NOT disabled.
253 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
259 static int curr_on = 0;
263 erase2_char = '\b'; /* in case OS doesn't know about erase2 */
264 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
267 static struct termios save_term;
268 static int saved_term = 0;
273 * Get terminal modes.
278 * Save modes and set certain variables dependent on modes.
286 switch (cfgetospeed(&s))
289 case B0: ospeed = 0; break;
292 case B50: ospeed = 1; break;
295 case B75: ospeed = 2; break;
298 case B110: ospeed = 3; break;
301 case B134: ospeed = 4; break;
304 case B150: ospeed = 5; break;
307 case B200: ospeed = 6; break;
310 case B300: ospeed = 7; break;
313 case B600: ospeed = 8; break;
316 case B1200: ospeed = 9; break;
319 case B1800: ospeed = 10; break;
322 case B2400: ospeed = 11; break;
325 case B4800: ospeed = 12; break;
328 case B9600: ospeed = 13; break;
331 case EXTA: ospeed = 14; break;
334 case EXTB: ospeed = 15; break;
337 case B57600: ospeed = 16; break;
340 case B115200: ospeed = 17; break;
345 erase_char = s.c_cc[VERASE];
347 erase2_char = s.c_cc[VERASE2];
349 kill_char = s.c_cc[VKILL];
351 werase_char = s.c_cc[VWERASE];
353 werase_char = CONTROL('W');
357 * Set the modes to the way we want them.
419 #if MUST_SET_LINE_DISCIPLINE
421 * System's termios is broken; need to explicitly
422 * request TERMIODISC line discipline.
424 s.c_line = TERMIODISC;
429 * Restore saved modes.
436 tcsetattr(tty, TCSADRAIN, &s);
437 #if MUST_SET_LINE_DISCIPLINE
441 * Broken termios *ignores* any line discipline
442 * except TERMIODISC. A different old line discipline
443 * is therefore not restored, yet. Restore the old
444 * line discipline by hand.
446 ioctl(tty, TIOCSETD, &save_term.c_line);
454 static struct termio save_term;
455 static int saved_term = 0;
460 * Get terminal modes.
462 ioctl(tty, TCGETA, &s);
465 * Save modes and set certain variables dependent on modes.
473 ospeed = s.c_cflag & CBAUD;
475 erase_char = s.c_cc[VERASE];
476 kill_char = s.c_cc[VKILL];
478 werase_char = s.c_cc[VWERASE];
480 werase_char = CONTROL('W');
484 * Set the modes to the way we want them.
486 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
487 s.c_oflag |= (OPOST|ONLCR|TAB3);
488 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
494 * Restore saved modes.
498 ioctl(tty, TCSETAW, &s);
504 static struct sgttyb save_term;
505 static int saved_term = 0;
510 * Get terminal modes.
512 ioctl(tty, TIOCGETP, &s);
515 * Save modes and set certain variables dependent on modes.
523 ospeed = s.sg_ospeed;
525 erase_char = s.sg_erase;
526 kill_char = s.sg_kill;
527 werase_char = CONTROL('W');
530 * Set the modes to the way we want them.
532 s.sg_flags |= CBREAK;
533 s.sg_flags &= ~(ECHO|XTABS);
537 * Restore saved modes.
541 ioctl(tty, TIOCSETN, &s);
547 static struct sgbuf save_term;
548 static int saved_term = 0;
553 * Get terminal modes.
558 * Save modes and set certain variables dependent on modes.
565 erase_char = s.sg_bspch;
566 kill_char = s.sg_dlnch;
567 werase_char = CONTROL('W');
570 * Set the modes to the way we want them.
579 * Restore saved modes.
586 /* MS-DOS, Windows, or OS2 */
589 LSIGNAL(SIGINT, SIG_IGN);
592 #if MSDOS_COMPILER==DJGPPC
593 kill_char = CONTROL('U');
595 * So that when we shell out or run another program, its
596 * stdin is in cooked mode. We do not switch stdin to binary
597 * mode if fd0 is zero, since that means we were called before
598 * tty was reopened in open_getchr, in which case we would be
599 * changing the original stdin device outside less.
602 setmode(0, on ? O_BINARY : O_TEXT);
606 werase_char = CONTROL('W');
616 * Some glue to prevent calling termcap functions if tgetent() failed.
627 s = lgetenv("LESS_TERMCAP_DEBUG");
628 if (s != NULL && *s != '\0')
630 struct env { struct env *next; char *name; char *value; };
631 static struct env *envs = NULL;
633 for (p = envs; p != NULL; p = p->next)
634 if (strcmp(p->name, capname) == 0)
636 p = (struct env *) ecalloc(1, sizeof(struct env));
637 p->name = save(capname);
638 p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
639 sprintf(p->value, "<%s>", capname);
644 strcpy(name, "LESS_TERMCAP_");
645 strcat(name, capname);
646 return (lgetenv(name));
655 if ((s = ltget_env(capname)) != NULL)
656 return (*s != '\0' && *s != '0');
659 return (tgetflag(capname));
668 if ((s = ltget_env(capname)) != NULL)
672 return (tgetnum(capname));
676 ltgetstr(capname, pp)
682 if ((s = ltget_env(capname)) != NULL)
686 return (tgetstr(capname, pp));
688 #endif /* MSDOS_COMPILER */
691 * Get size of the output screen.
703 #define DEF_SC_WIDTH 80
705 #define DEF_SC_HEIGHT 25
707 #define DEF_SC_HEIGHT 24
711 sys_width = sys_height = 0;
713 #if MSDOS_COMPILER==MSOFTC
715 struct videoconfig w;
717 sys_height = w.numtextrows;
718 sys_width = w.numtextcols;
721 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
725 sys_height = w.screenheight;
726 sys_width = w.screenwidth;
729 #if MSDOS_COMPILER==WIN32C
731 CONSOLE_SCREEN_BUFFER_INFO scr;
732 GetConsoleScreenBufferInfo(con_out, &scr);
733 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
734 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
744 * When using terminal emulators for XFree86/OS2, the
745 * _scrsize function does not work well.
746 * Call the scrsize.exe program to get the window size.
748 windowid = getenv("WINDOWID");
749 if (windowid != NULL)
751 FILE *fd = popen("scrsize", "rt");
755 fscanf(fd, "%i %i", &w, &h);
769 if (ioctl(2, TIOCGWINSZ, &w) == 0)
772 sys_height = w.ws_row;
774 sys_width = w.ws_col;
781 if (ioctl(2, WIOCGETD, &w) == 0)
784 sys_height = w.uw_height / w.uw_vs;
786 sys_width = w.uw_width / w.uw_hs;
797 sc_height = sys_height;
798 else if ((s = lgetenv("LINES")) != NULL)
801 else if ((n = ltgetnum("li")) > 0)
805 sc_height = DEF_SC_HEIGHT;
808 sc_width = sys_width;
809 else if ((s = lgetenv("COLUMNS")) != NULL)
812 else if ((n = ltgetnum("co")) > 0)
816 sc_width = DEF_SC_WIDTH;
819 #if MSDOS_COMPILER==MSOFTC
821 * Figure out how many empty loops it takes to delay a millisecond.
829 * Get synchronized at the start of a tick.
832 while (clock() == start)
835 * Now count loops till the next tick.
839 while (clock() == start)
842 * Convert from (loops per clock) to (loops per millisecond).
844 msec_loops *= CLOCKS_PER_SEC;
849 * Delay for a specified number of milliseconds.
854 static long delay_dummy = 0;
866 for (i = 0; i < msec_loops; i++)
869 * Make it look like we're doing something here,
870 * so the optimizer doesn't remove the whole loop.
879 * Return the characters actually input by a "special" key.
885 static char tbuf[40];
887 #if MSDOS_COMPILER || OS2
888 static char k_right[] = { '\340', PCK_RIGHT, 0 };
889 static char k_left[] = { '\340', PCK_LEFT, 0 };
890 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
891 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
892 static char k_insert[] = { '\340', PCK_INSERT, 0 };
893 static char k_delete[] = { '\340', PCK_DELETE, 0 };
894 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
895 static char k_ctl_backspace[] = { '\177', 0 };
896 static char k_home[] = { '\340', PCK_HOME, 0 };
897 static char k_end[] = { '\340', PCK_END, 0 };
898 static char k_up[] = { '\340', PCK_UP, 0 };
899 static char k_down[] = { '\340', PCK_DOWN, 0 };
900 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
901 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
902 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
903 static char k_f1[] = { '\340', PCK_F1, 0 };
913 * If windowid is not NULL, assume less is executed in
914 * the XFree86 environment.
917 s = windowid ? ltgetstr("kr", &sp) : k_right;
920 s = windowid ? ltgetstr("kl", &sp) : k_left;
923 s = windowid ? ltgetstr("ku", &sp) : k_up;
926 s = windowid ? ltgetstr("kd", &sp) : k_down;
929 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
932 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
935 s = windowid ? ltgetstr("kh", &sp) : k_home;
938 s = windowid ? ltgetstr("@7", &sp) : k_end;
943 s = ltgetstr("kD", &sp);
983 #if MSDOS_COMPILER || OS2
987 case SK_CTL_LEFT_ARROW:
990 case SK_CTL_RIGHT_ARROW:
993 case SK_CTL_BACKSPACE:
1006 case SK_RIGHT_ARROW:
1007 s = ltgetstr("kr", &sp);
1010 s = ltgetstr("kl", &sp);
1013 s = ltgetstr("ku", &sp);
1016 s = ltgetstr("kd", &sp);
1019 s = ltgetstr("kP", &sp);
1022 s = ltgetstr("kN", &sp);
1025 s = ltgetstr("kh", &sp);
1028 s = ltgetstr("@7", &sp);
1031 s = ltgetstr("kD", &sp);
1041 tbuf[0] = CONTROL('K');
1052 * Get terminal capabilities via termcap.
1063 * Set up default colors.
1064 * The xx_s_width and xx_e_width vars are already initialized to 0.
1066 #if MSDOS_COMPILER==MSOFTC
1067 sy_bg_color = _getbkcolor();
1068 sy_fg_color = _gettextcolor();
1071 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1075 sy_bg_color = (w.attribute >> 4) & 0x0F;
1076 sy_fg_color = (w.attribute >> 0) & 0x0F;
1079 #if MSDOS_COMPILER==WIN32C
1082 CONSOLE_SCREEN_BUFFER_INFO scr;
1084 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1086 * Always open stdin in binary. Note this *must* be done
1087 * before any file operations have been done on fd0.
1090 GetConsoleScreenBufferInfo(con_out, &scr);
1091 ReadConsoleOutputAttribute(con_out, &curr_attr,
1092 1, scr.dwCursorPosition, &nread);
1093 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1094 sy_fg_color = curr_attr & FG_COLORS;
1099 nm_fg_color = sy_fg_color;
1100 nm_bg_color = sy_bg_color;
1111 * Get size of the screen.
1117 #else /* !MSDOS_COMPILER */
1120 register char *t1, *t2;
1122 char termbuf[TERMBUF_SIZE];
1124 static char sbuf[TERMSBUF_SIZE];
1128 * Make sure the termcap database is available.
1130 sp = lgetenv("TERMCAP");
1131 if (sp == NULL || *sp == '\0')
1134 if ((sp = homefile("termcap.dat")) != NULL)
1136 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1137 sprintf(termcap, "TERMCAP=%s", sp);
1144 * Find out what kind of terminal this is.
1146 if ((term = lgetenv("TERM")) == NULL)
1147 term = DEFAULT_TERM;
1149 if (tgetent(termbuf, term) != TGETENT_OK)
1151 if (ltgetflag("hc"))
1155 * Get size of the screen.
1160 auto_wrap = ltgetflag("am");
1161 ignaw = ltgetflag("xn");
1162 above_mem = ltgetflag("da");
1163 below_mem = ltgetflag("db");
1164 clear_bg = ltgetflag("ut");
1167 * Assumes termcap variable "sg" is the printing width of:
1168 * the standout sequence, the end standout sequence,
1169 * the underline sequence, the end underline sequence,
1170 * the boldface sequence, and the end boldface sequence.
1172 if ((so_s_width = ltgetnum("sg")) < 0)
1174 so_e_width = so_s_width;
1176 bo_s_width = bo_e_width = so_s_width;
1177 ul_s_width = ul_e_width = so_s_width;
1178 bl_s_width = bl_e_width = so_s_width;
1181 if (so_s_width > 0 || so_e_width > 0)
1183 * Disable highlighting by default on magic cookie terminals.
1184 * Turning on highlighting might change the displayed width
1185 * of a line, causing the display to get messed up.
1186 * The user can turn it back on with -g,
1187 * but she won't like the results.
1193 * Get various string-valued capabilities.
1198 sc_pad = ltgetstr("pc", &sp);
1203 sc_s_keypad = ltgetstr("ks", &sp);
1204 if (sc_s_keypad == NULL)
1206 sc_e_keypad = ltgetstr("ke", &sp);
1207 if (sc_e_keypad == NULL)
1210 sc_init = ltgetstr("ti", &sp);
1211 if (sc_init == NULL)
1214 sc_deinit= ltgetstr("te", &sp);
1215 if (sc_deinit == NULL)
1218 sc_eol_clear = ltgetstr("ce", &sp);
1219 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1225 sc_eos_clear = ltgetstr("cd", &sp);
1226 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1232 sc_clear = ltgetstr("cl", &sp);
1233 if (sc_clear == NULL || *sc_clear == '\0')
1239 sc_move = ltgetstr("cm", &sp);
1240 if (sc_move == NULL || *sc_move == '\0')
1243 * This is not an error here, because we don't
1244 * always need sc_move.
1245 * We need it only if we don't have home or lower-left.
1252 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1253 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1254 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1255 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1257 sc_visual_bell = ltgetstr("vb", &sp);
1258 if (sc_visual_bell == NULL)
1259 sc_visual_bell = "";
1261 if (ltgetflag("bs"))
1262 sc_backspace = "\b";
1265 sc_backspace = ltgetstr("bc", &sp);
1266 if (sc_backspace == NULL || *sc_backspace == '\0')
1267 sc_backspace = "\b";
1271 * Choose between using "ho" and "cm" ("home" and "cursor move")
1272 * to move the cursor to the upper left corner of the screen.
1274 t1 = ltgetstr("ho", &sp);
1277 if (*sc_move == '\0')
1281 strcpy(sp, tgoto(sc_move, 0, 0));
1283 sp += strlen(sp) + 1;
1285 sc_home = cheaper(t1, t2, "|\b^");
1288 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1289 * to move the cursor to the lower left corner of the screen.
1291 t1 = ltgetstr("ll", &sp);
1294 if (*sc_move == '\0')
1298 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1300 sp += strlen(sp) + 1;
1302 sc_lower_left = cheaper(t1, t2, "\r");
1305 * Get carriage return string.
1307 sc_return = ltgetstr("cr", &sp);
1308 if (sc_return == NULL)
1312 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1313 * to add a line at the top of the screen.
1315 t1 = ltgetstr("al", &sp);
1318 t2 = ltgetstr("sr", &sp);
1322 if (*t1 == '\0' && *t2 == '\0')
1329 sc_addline = cheaper(t1, t2, "");
1330 if (*sc_addline == '\0')
1333 * Force repaint on any backward movement.
1337 #endif /* MSDOS_COMPILER */
1342 * Return the cost of displaying a termcap string.
1343 * We use the trick of calling tputs, but as a char printing function
1344 * we give it inc_costcount, which just increments "costcount".
1345 * This tells us how many chars would be printed by using this string.
1346 * {{ Couldn't we just use strlen? }}
1348 static int costcount;
1364 tputs(t, sc_height, inc_costcount);
1369 * Return the "best" of the two given termcap strings.
1370 * The best, if both exist, is the one with the lower
1371 * cost (see cost() function).
1374 cheaper(t1, t2, def)
1378 if (*t1 == '\0' && *t2 == '\0')
1387 if (cost(t1) < cost(t2))
1393 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1402 *instr = ltgetstr(incap, spp);
1407 *outstr = def_outstr;
1411 *outstr = ltgetstr(outcap, spp);
1412 if (*outstr == NULL)
1413 /* No specific out capability; use "me". */
1414 *outstr = ltgetstr("me", spp);
1415 if (*outstr == NULL)
1416 /* Don't even have "me"; use a null string. */
1420 #endif /* MSDOS_COMPILER */
1424 * Below are the functions which perform all the
1425 * terminal-specific screen manipulation.
1431 #if MSDOS_COMPILER==WIN32C
1433 _settextposition(int row, int col)
1436 CONSOLE_SCREEN_BUFFER_INFO csbi;
1438 GetConsoleScreenBufferInfo(con_out, &csbi);
1439 cpos.X = csbi.srWindow.Left + (col - 1);
1440 cpos.Y = csbi.srWindow.Top + (row - 1);
1441 SetConsoleCursorPosition(con_out, cpos);
1446 * Initialize the screen to the correct color at startup.
1451 SETCOLORS(nm_fg_color, nm_bg_color);
1454 * This clears the screen at startup. This is different from
1455 * the behavior of other versions of less. Disable it for now.
1462 * Create a complete, blank screen using "normal" colors.
1464 SETCOLORS(nm_fg_color, nm_bg_color);
1465 blanks = (char *) ecalloc(width+1, sizeof(char));
1466 for (col = 0; col < sc_width; col++)
1468 blanks[sc_width] = '\0';
1469 for (row = 0; row < sc_height; row++)
1476 #if MSDOS_COMPILER==WIN32C
1479 * Termcap-like init with a private win32 console.
1484 CONSOLE_SCREEN_BUFFER_INFO scr;
1487 if (con_out_save == INVALID_HANDLE_VALUE)
1490 GetConsoleScreenBufferInfo(con_out_save, &scr);
1492 if (con_out_ours == INVALID_HANDLE_VALUE)
1495 * Create our own screen buffer, so that we
1496 * may restore the original when done.
1498 con_out_ours = CreateConsoleScreenBuffer(
1499 GENERIC_WRITE | GENERIC_READ,
1500 FILE_SHARE_WRITE | FILE_SHARE_READ,
1501 (LPSECURITY_ATTRIBUTES) NULL,
1502 CONSOLE_TEXTMODE_BUFFER,
1506 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1507 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1508 SetConsoleScreenBufferSize(con_out_ours, size);
1509 SetConsoleActiveScreenBuffer(con_out_ours);
1510 con_out = con_out_ours;
1514 * Restore the startup console.
1519 if (con_out_save == INVALID_HANDLE_VALUE)
1522 (void) CloseHandle(con_out_ours);
1523 SetConsoleActiveScreenBuffer(con_out_save);
1524 con_out = con_out_save;
1530 * Initialize terminal
1537 tputs(sc_init, sc_height, putchr);
1539 tputs(sc_s_keypad, sc_height, putchr);
1545 * This is nice to terminals with no alternate screen,
1546 * but with saved scrolled-off-the-top lines. This way,
1547 * no previous line is lost, but we start with a whole
1548 * screen to ourself.
1550 for (i = 1; i < sc_height; i++)
1555 #if MSDOS_COMPILER==WIN32C
1566 * Deinitialize terminal
1575 tputs(sc_e_keypad, sc_height, putchr);
1577 tputs(sc_deinit, sc_height, putchr);
1579 /* Restore system colors. */
1580 SETCOLORS(sy_fg_color, sy_bg_color);
1581 #if MSDOS_COMPILER==WIN32C
1583 win32_deinit_term();
1585 /* Need clreol to make SETCOLORS take effect. */
1593 * Home cursor (move to upper left corner of screen).
1599 tputs(sc_home, 1, putchr);
1602 _settextposition(1,1);
1607 * Add a blank line (called with cursor at home).
1608 * Should scroll the display down.
1614 tputs(sc_addline, sc_height, putchr);
1617 #if MSDOS_COMPILER==MSOFTC
1618 _scrolltextwindow(_GSCROLLDOWN);
1619 _settextposition(1,1);
1621 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1622 movetext(1,1, sc_width,sc_height-1, 1,2);
1626 #if MSDOS_COMPILER==WIN32C
1629 SMALL_RECT rcSrc, rcClip;
1631 CONSOLE_SCREEN_BUFFER_INFO csbi;
1633 GetConsoleScreenBufferInfo(con_out,&csbi);
1635 /* The clip rectangle is the entire visible screen. */
1636 rcClip.Left = csbi.srWindow.Left;
1637 rcClip.Top = csbi.srWindow.Top;
1638 rcClip.Right = csbi.srWindow.Right;
1639 rcClip.Bottom = csbi.srWindow.Bottom;
1641 /* The source rectangle is the visible screen minus the last line. */
1645 /* Move the top left corner of the source window down one row. */
1646 new_org.X = rcSrc.Left;
1647 new_org.Y = rcSrc.Top + 1;
1649 /* Fill the right character and attributes. */
1650 fillchar.Char.AsciiChar = ' ';
1651 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1652 fillchar.Attributes = curr_attr;
1653 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1654 _settextposition(1,1);
1664 * Remove the n topmost lines and scroll everything below it in the
1665 * window upward. This is needed to stop leaking the topmost line
1666 * into the scrollback buffer when we go down-one-line (in WIN32).
1672 #if MSDOS_COMPILER==WIN32C
1673 SMALL_RECT rcSrc, rcClip;
1676 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1678 if (n >= sc_height - 1)
1687 GetConsoleScreenBufferInfo(con_out, &csbi);
1689 /* Get the extent of all-visible-rows-but-the-last. */
1690 rcSrc.Left = csbi.srWindow.Left;
1691 rcSrc.Top = csbi.srWindow.Top + n;
1692 rcSrc.Right = csbi.srWindow.Right;
1693 rcSrc.Bottom = csbi.srWindow.Bottom;
1695 /* Get the clip rectangle. */
1696 rcClip.Left = rcSrc.Left;
1697 rcClip.Top = csbi.srWindow.Top;
1698 rcClip.Right = rcSrc.Right;
1699 rcClip.Bottom = rcSrc.Bottom ;
1701 /* Move the source window up n rows. */
1702 new_org.X = rcSrc.Left;
1703 new_org.Y = rcSrc.Top - n;
1705 /* Fill the right character and attributes. */
1706 fillchar.Char.AsciiChar = ' ';
1707 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1708 fillchar.Attributes = curr_attr;
1710 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1712 /* Position cursor on first blank line. */
1713 goto_line(sc_height - n - 1);
1718 #if MSDOS_COMPILER==WIN32C
1726 * This will clear only the currently visible rows of the NT
1727 * console buffer, which means none of the precious scrollback
1728 * rows are touched making for faster scrolling. Note that, if
1729 * the window has fewer columns than the console buffer (i.e.
1730 * there is a horizontal scrollbar as well), the entire width
1731 * of the visible rows will be cleared.
1736 CONSOLE_SCREEN_BUFFER_INFO csbi;
1738 /* get the number of cells in the current buffer */
1739 GetConsoleScreenBufferInfo(con_out, &csbi);
1740 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1742 topleft.Y = csbi.srWindow.Top;
1744 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1745 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1746 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1750 * Remove the n topmost lines and scroll everything below it in the
1757 SMALL_RECT rcSrc, rcClip;
1763 CONSOLE_SCREEN_BUFFER_INFO csbi;
1768 if (n >= sc_height - 1)
1771 _settextposition(1,1);
1775 /* Get the extent of what will remain visible after scrolling. */
1776 GetConsoleScreenBufferInfo(con_out, &csbi);
1777 rcSrc.Left = csbi.srWindow.Left;
1778 rcSrc.Top = csbi.srWindow.Top + n;
1779 rcSrc.Right = csbi.srWindow.Right;
1780 rcSrc.Bottom = csbi.srWindow.Bottom;
1782 /* Get the clip rectangle. */
1783 rcClip.Left = rcSrc.Left;
1784 rcClip.Top = csbi.srWindow.Top;
1785 rcClip.Right = rcSrc.Right;
1786 rcClip.Bottom = rcSrc.Bottom ;
1788 /* Move the source text to the top of the screen. */
1789 new_org.X = rcSrc.Left;
1790 new_org.Y = rcClip.Top;
1792 /* Fill the right character and attributes. */
1793 fillchar.Char.AsciiChar = ' ';
1794 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1796 /* Scroll the window. */
1797 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1798 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1800 /* Clear remaining lines at bottom. */
1801 topleft.X = csbi.dwCursorPosition.X;
1802 topleft.Y = rcSrc.Bottom - n;
1803 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1804 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1806 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1808 SetConsoleTextAttribute(con_out, curr_attr);
1810 /* Move cursor n lines up from where it was. */
1811 csbi.dwCursorPosition.Y -= n;
1812 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1817 * Move cursor to lower left corner of screen.
1823 tputs(sc_lower_left, 1, putchr);
1826 _settextposition(sc_height, 1);
1831 * Move cursor to left position of current line.
1837 tputs(sc_return, 1, putchr);
1841 #if MSDOS_COMPILER==WIN32C
1843 CONSOLE_SCREEN_BUFFER_INFO scr;
1844 GetConsoleScreenBufferInfo(con_out, &scr);
1845 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
1848 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1852 struct rccoord tpos = _gettextposition();
1857 _settextposition(row, 1);
1862 * Check if the console size has changed and reset internals
1863 * (in lieu of SIGWINCH for WIN32).
1868 #if MSDOS_COMPILER==WIN32C
1869 CONSOLE_SCREEN_BUFFER_INFO scr;
1872 if (con_out == INVALID_HANDLE_VALUE)
1876 GetConsoleScreenBufferInfo(con_out, &scr);
1877 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1878 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1879 if (size.Y != sc_height || size.X != sc_width)
1883 if (!no_init && con_out_ours == con_out)
1884 SetConsoleScreenBufferSize(con_out, size);
1886 wscroll = (sc_height + 1) / 2;
1893 * Goto a specific line on the screen.
1900 tputs(tgoto(sc_move, 0, slinenum), 1, putchr);
1903 _settextposition(slinenum+1, 1);
1907 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1909 * Create an alternate screen which is all white.
1910 * This screen is used to create a "flash" effect, by displaying it
1911 * briefly and then switching back to the normal screen.
1912 * {{ Yuck! There must be a better way to get a visual bell. }}
1917 #if MSDOS_COMPILER==MSOFTC
1918 struct videoconfig w;
1922 _getvideoconfig(&w);
1923 videopages = w.numvideopages;
1926 at_enter(AT_STANDOUT);
1931 at_enter(AT_STANDOUT);
1932 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
1933 for (col = 0; col < w.numtextcols; col++)
1935 for (row = w.numtextrows; row > 0; row--)
1936 _outmem(blanks, w.numtextcols);
1943 #if MSDOS_COMPILER==BORLANDC
1946 whitescreen = (unsigned short *)
1947 malloc(sc_width * sc_height * sizeof(short));
1948 if (whitescreen == NULL)
1950 for (n = 0; n < sc_width * sc_height; n++)
1951 whitescreen[n] = 0x7020;
1953 #if MSDOS_COMPILER==WIN32C
1956 whitescreen = (WORD *)
1957 malloc(sc_height * sc_width * sizeof(WORD));
1958 if (whitescreen == NULL)
1960 /* Invert the standard colors. */
1961 for (n = 0; n < sc_width * sc_height; n++)
1962 whitescreen[n] = (WORD)((nm_fg_color << 4) | nm_bg_color);
1968 #endif /* MSDOS_COMPILER */
1971 * Output the "visual bell", if there is one.
1977 if (*sc_visual_bell == '\0')
1979 tputs(sc_visual_bell, sc_height, putchr);
1981 #if MSDOS_COMPILER==DJGPPC
1984 #if MSDOS_COMPILER==MSOFTC
1986 * Create a flash screen on the second video page.
1987 * Switch to that page, then switch back.
1997 #if MSDOS_COMPILER==BORLANDC
1998 unsigned short *currscreen;
2001 * Get a copy of the current screen.
2002 * Display the flash screen.
2003 * Then restore the old screen.
2007 if (whitescreen == NULL)
2009 currscreen = (unsigned short *)
2010 malloc(sc_width * sc_height * sizeof(short));
2011 if (currscreen == NULL) return;
2012 gettext(1, 1, sc_width, sc_height, currscreen);
2013 puttext(1, 1, sc_width, sc_height, whitescreen);
2015 puttext(1, 1, sc_width, sc_height, currscreen);
2018 #if MSDOS_COMPILER==WIN32C
2019 /* paint screen with an inverse color */
2022 /* leave it displayed for 100 msec. */
2025 /* restore with a redraw */
2041 putchr(CONTROL('G'));
2043 #if MSDOS_COMPILER==WIN32C
2052 * Ring the terminal bell.
2057 if (quiet == VERY_QUIET)
2070 tputs(sc_clear, sc_height, putchr);
2073 #if MSDOS_COMPILER==WIN32C
2076 _clearscreen(_GCLEARSCREEN);
2082 * Clear from the cursor to the end of the cursor's line.
2083 * {{ This must not move the cursor. }}
2089 tputs(sc_eol_clear, 1, putchr);
2091 #if MSDOS_COMPILER==MSOFTC
2094 struct rccoord tpos;
2098 * Save current state.
2100 tpos = _gettextposition();
2101 _gettextwindow(&top, &left, &bot, &right);
2103 * Set a temporary window to the current line,
2104 * from the cursor's position to the right edge of the screen.
2105 * Then clear that window.
2107 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2108 _clearscreen(_GWINDOW);
2112 _settextwindow(top, left, bot, right);
2113 _settextposition(tpos.row, tpos.col);
2115 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2119 #if MSDOS_COMPILER==WIN32C
2122 CONSOLE_SCREEN_BUFFER_INFO scr;
2125 memset(&scr, 0, sizeof(scr));
2126 GetConsoleScreenBufferInfo(con_out, &scr);
2127 cpos.X = scr.dwCursorPosition.X;
2128 cpos.Y = scr.dwCursorPosition.Y;
2129 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2130 FillConsoleOutputAttribute(con_out, curr_attr,
2131 scr.dwSize.X - cpos.X, cpos, &nchars);
2132 FillConsoleOutputCharacter(con_out, ' ',
2133 scr.dwSize.X - cpos.X, cpos, &nchars);
2141 * Clear the current line.
2142 * Clear the screen if there's off-screen memory below the display.
2151 tputs(sc_eos_clear, 1, putchr);
2153 tputs(sc_eol_clear, 1, putchr);
2158 * Clear the bottom line of the display.
2159 * Leave the cursor at the beginning of the bottom line.
2165 * If we're in a non-normal attribute mode, temporarily exit
2166 * the mode while we do the clear. Some terminals fill the
2167 * cleared area with the current attribute.
2174 if (attrmode == AT_NORMAL)
2178 int saved_attrmode = attrmode;
2182 at_enter(saved_attrmode);
2190 attr = apply_at_specials(attr);
2193 /* The one with the most priority is last. */
2194 if (attr & AT_UNDERLINE)
2195 tputs(sc_u_in, 1, putchr);
2197 tputs(sc_b_in, 1, putchr);
2198 if (attr & AT_BLINK)
2199 tputs(sc_bl_in, 1, putchr);
2200 if (attr & AT_STANDOUT)
2201 tputs(sc_s_in, 1, putchr);
2204 /* The one with the most priority is first. */
2205 if (attr & AT_STANDOUT)
2207 SETCOLORS(so_fg_color, so_bg_color);
2208 } else if (attr & AT_BLINK)
2210 SETCOLORS(bl_fg_color, bl_bg_color);
2212 else if (attr & AT_BOLD)
2214 SETCOLORS(bo_fg_color, bo_bg_color);
2216 else if (attr & AT_UNDERLINE)
2218 SETCOLORS(ul_fg_color, ul_bg_color);
2229 /* Undo things in the reverse order we did them. */
2230 if (attrmode & AT_STANDOUT)
2231 tputs(sc_s_out, 1, putchr);
2232 if (attrmode & AT_BLINK)
2233 tputs(sc_bl_out, 1, putchr);
2234 if (attrmode & AT_BOLD)
2235 tputs(sc_b_out, 1, putchr);
2236 if (attrmode & AT_UNDERLINE)
2237 tputs(sc_u_out, 1, putchr);
2240 SETCOLORS(nm_fg_color, nm_bg_color);
2243 attrmode = AT_NORMAL;
2250 int new_attrmode = apply_at_specials(attr);
2251 int ignore_modes = AT_ANSI;
2253 if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
2261 is_at_equiv(attr1, attr2)
2265 attr1 = apply_at_specials(attr1);
2266 attr2 = apply_at_specials(attr2);
2268 return (attr1 == attr2);
2272 apply_at_specials(attr)
2275 if (attr & AT_BINARY)
2277 if (attr & AT_HILITE)
2278 attr |= AT_STANDOUT;
2279 attr &= ~(AT_BINARY|AT_HILITE);
2284 #if 0 /* No longer used */
2286 * Erase the character to the left of the cursor
2287 * and move the cursor left.
2294 * Erase the previous character by overstriking with a space.
2296 tputs(sc_backspace, 1, putchr);
2298 tputs(sc_backspace, 1, putchr);
2300 #if MSDOS_COMPILER==MSOFTC
2301 struct rccoord tpos;
2304 tpos = _gettextposition();
2307 _settextposition(tpos.row, tpos.col-1);
2309 _settextposition(tpos.row, tpos.col-1);
2311 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2314 #if MSDOS_COMPILER==WIN32C
2317 CONSOLE_SCREEN_BUFFER_INFO scr;
2320 GetConsoleScreenBufferInfo(con_out, &scr);
2321 cpos = scr.dwCursorPosition;
2325 SetConsoleCursorPosition(con_out, cpos);
2326 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2327 SetConsoleCursorPosition(con_out, cpos);
2336 * Output a plain backspace, without erasing the previous char.
2342 tputs(sc_backspace, 1, putchr);
2348 #if MSDOS_COMPILER==MSOFTC
2349 struct rccoord tpos;
2350 tpos = _gettextposition();
2354 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2358 #if MSDOS_COMPILER==WIN32C
2359 CONSOLE_SCREEN_BUFFER_INFO scr;
2360 GetConsoleScreenBufferInfo(con_out, &scr);
2361 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2362 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2369 _settextposition(row, col-1);
2370 #endif /* MSDOS_COMPILER */
2373 #if MSDOS_COMPILER==WIN32C
2375 * Determine whether an input character is waiting to be read.
2387 currentKey.ascii = 0;
2388 currentKey.scan = 0;
2391 * Wait for a real key-down event, but
2392 * ignore SHIFT and CONTROL key events.
2396 PeekConsoleInput(tty, &ip, 1, &read);
2399 ReadConsoleInput(tty, &ip, 1, &read);
2400 } while (ip.EventType != KEY_EVENT ||
2401 ip.Event.KeyEvent.bKeyDown != TRUE ||
2402 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2403 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2404 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2405 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2407 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2408 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2409 keyCount = ip.Event.KeyEvent.wRepeatCount;
2411 if (ip.Event.KeyEvent.dwControlKeyState &
2412 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2414 switch (currentKey.scan)
2416 case PCK_ALT_E: /* letter 'E' */
2417 currentKey.ascii = 0;
2420 } else if (ip.Event.KeyEvent.dwControlKeyState &
2421 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2423 switch (currentKey.scan)
2425 case PCK_RIGHT: /* right arrow */
2426 currentKey.scan = PCK_CTL_RIGHT;
2428 case PCK_LEFT: /* left arrow */
2429 currentKey.scan = PCK_CTL_LEFT;
2431 case PCK_DELETE: /* delete */
2432 currentKey.scan = PCK_CTL_DELETE;
2440 * Read a character from the keyboard.
2448 if (pending_scancode)
2450 pending_scancode = 0;
2451 return ((char)(currentKey.scan & 0x00FF));
2454 while (win32_kbhit((HANDLE)tty) == FALSE)
2462 ascii = currentKey.ascii;
2464 * On PC's, the extended keys return a 2 byte sequence beginning
2465 * with '00', so if the ascii code is 00, the next byte will be
2466 * the lsb of the scan code.
2468 pending_scancode = (ascii == 0x00);
2469 return ((char)ascii);
2477 WIN32setcolors(fg, bg)
2487 WIN32textout(text, len)
2491 #if MSDOS_COMPILER==WIN32C
2493 WriteConsole(con_out, text, len, &written, NULL);