1 /*
2  * Routines which deal with the characteristics of the terminal.
3  * Uses termcap to be as terminal-independent as possible.
4  *
5  * {{ Someday this should be rewritten to use curses. }}
6  */
7 
8 #include "less.h"
9 #if XENIX
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #endif
13 
14 #if TERMIO
15 # ifdef BSD4_4
16 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
17 #include <sys/termios.h>
18 #else
19 #include <termios.h>
20 #endif
21 # else
22 #include <termio.h>
23 # endif
24 #else
25 #include <sgtty.h>
26 #endif
27 
28 #ifdef TIOCGWINSZ
29 #include <sys/ioctl.h>
30 #else
31 /*
32  * For the Unix PC (ATT 7300 & 3B1):
33  * Since WIOCGETD is defined in sys/window.h, we can't use that to decide
34  * whether to include sys/window.h.  Use SIGPHONE from sys/signal.h instead.
35  */
36 #include <signal.h>
37 #ifdef SIGPHONE
38 #include <sys/window.h>
39 #endif
40 #endif
41 
42 /*
43  * Strings passed to tputs() to do various terminal functions.
44  */
45 static char
46 	*sc_pad,		/* Pad string */
47 	*sc_home,		/* Cursor home */
48 	*sc_addline,		/* Add line, scroll down following lines */
49 	*sc_lower_left,		/* Cursor to last line, first column */
50 	*sc_move,		/* General cursor positioning */
51 	*sc_clear,		/* Clear screen */
52 	*sc_eol_clear,		/* Clear to end of line */
53 	*sc_s_in,		/* Enter standout (highlighted) mode */
54 	*sc_s_out,		/* Exit standout mode */
55 	*sc_u_in,		/* Enter underline mode */
56 	*sc_u_out,		/* Exit underline mode */
57 	*sc_b_in,		/* Enter bold mode */
58 	*sc_b_out,		/* Exit bold mode */
59 	*sc_visual_bell,	/* Visual bell (flash screen) sequence */
60 	*sc_backspace,		/* Backspace cursor */
61 	*sc_init,		/* Startup terminal initialization */
62 	*sc_deinit;		/* Exit terminal de-intialization */
63 
64 public int auto_wrap;		/* Terminal does \r\n when write past margin */
65 public int ignaw;		/* Terminal ignores \n immediately after wrap */
66 public int erase_char, kill_char; /* The user's erase and line-kill chars */
67 public int sc_width, sc_height;	/* Height & width of screen */
68 public int sc_window = -1;	/* window size for forward and backward */
69 public int bo_width, be_width;	/* Printing width of boldface sequences */
70 public int ul_width, ue_width;	/* Printing width of underline sequences */
71 public int so_width, se_width;	/* Printing width of standout sequences */
72 
73 /*
74  * These two variables are sometimes defined in,
75  * and needed by, the termcap library.
76  * It may be necessary on some systems to declare them extern here.
77  */
78 /*extern*/ short ospeed;	/* Terminal output baud rate */
79 /*extern*/ char PC;		/* Pad character */
80 
81 extern int quiet;		/* If VERY_QUIET, use visual bell for bell */
82 extern int know_dumb;		/* Don't complain about a dumb terminal */
83 extern int back_scroll;
84 char *tgetstr();
85 char *tgoto();
86 
87 /*
88  * Change terminal to "raw mode", or restore to "normal" mode.
89  * "Raw mode" means
90  *	1. An outstanding read will complete on receipt of a single keystroke.
91  *	2. Input is not echoed.
92  *	3. On output, \n is mapped to \r\n.
93  *	4. \t is NOT expanded into spaces.
94  *	5. Signal-causing characters such as ctrl-C (interrupt),
95  *	   etc. are NOT disabled.
96  * It doesn't matter whether an input \n is mapped to \r, or vice versa.
97  */
98 	public void
raw_mode(on)99 raw_mode(on)
100 	int on;
101 {
102 #if TERMIO
103 #ifdef BSD4_4
104 	struct termios s;
105 	static struct termios save_term;
106 #else
107 	struct termio s;
108 	static struct termio save_term;
109 #endif
110 
111 	if (on)
112 	{
113 		/*
114 		 * Get terminal modes.
115 		 */
116 #ifdef BSD4_4
117 		tcgetattr(2, &s);
118 #else
119 		ioctl(2, TCGETA, &s);
120 #endif
121 
122 		/*
123 		 * Save modes and set certain variables dependent on modes.
124 		 */
125 		save_term = s;
126 		ospeed =
127 #ifdef BSD4_4
128 			cfgetospeed(&s);
129 #else
130 			s.c_cflag & CBAUD;
131 #endif
132 		erase_char = s.c_cc[VERASE];
133 		kill_char = s.c_cc[VKILL];
134 
135 		/*
136 		 * Set the modes to the way we want them.
137 		 */
138 		s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
139 		s.c_oflag |=
140 #ifdef BSD4_4
141 			      (OPOST|ONLCR|OXTABS);
142 #else
143 			      (OPOST|ONLCR|TABS3);
144 #endif
145 		s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
146 		s.c_cc[VMIN] = 1;
147 		s.c_cc[VTIME] = 0;
148 	} else
149 	{
150 		/*
151 		 * Restore saved modes.
152 		 */
153 		s = save_term;
154 	}
155 # ifdef BSD4_4
156 	tcsetattr(2, TCSANOW, &s);
157 # else
158 	ioctl(2, TCSETAW, &s);
159 # endif
160 #else
161 	struct sgttyb s;
162 	static struct sgttyb save_term;
163 
164 	if (on)
165 	{
166 		/*
167 		 * Get terminal modes.
168 		 */
169 		ioctl(2, TIOCGETP, &s);
170 
171 		/*
172 		 * Save modes and set certain variables dependent on modes.
173 		 */
174 		save_term = s;
175 		ospeed = s.sg_ospeed;
176 		erase_char = s.sg_erase;
177 		kill_char = s.sg_kill;
178 
179 		/*
180 		 * Set the modes to the way we want them.
181 		 */
182 		s.sg_flags |= CBREAK;
183 		s.sg_flags &= ~(ECHO|XTABS);
184 	} else
185 	{
186 		/*
187 		 * Restore saved modes.
188 		 */
189 		s = save_term;
190 	}
191 	ioctl(2, TIOCSETP, &s);
192 #endif
193 }
194 
195 	static void
cannot(s)196 cannot(s)
197 	char *s;
198 {
199 	char message[100];
200 
201 	if (know_dumb)
202 		/*
203 		 * He knows he has a dumb terminal, so don't tell him.
204 		 */
205 		return;
206 
207 	sprintf(message, "WARNING: terminal cannot \"%s\"", s);
208 	error(message);
209 }
210 
211 /*
212  * Get terminal capabilities via termcap.
213  */
214 	public void
get_term()215 get_term()
216 {
217 	char termbuf[2048];
218 	char *sp;
219 	char *term;
220 	int hard;
221 #ifdef TIOCGWINSZ
222 	struct winsize w;
223 #else
224 #ifdef WIOCGETD
225 	struct uwdata w;
226 #endif
227 #endif
228 	static char sbuf[1024];
229 
230 	char *getenv();
231 
232 	/*
233 	 * Find out what kind of terminal this is.
234 	 */
235  	if ((term = getenv("TERM")) == NULL)
236  		term = "unknown";
237  	if (tgetent(termbuf, term) <= 0)
238  		strcpy(termbuf, "dumb:co#80:hc:");
239 
240 	/*
241 	 * Get size of the screen.
242 	 */
243 #ifdef TIOCGWINSZ
244 	if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_row > 0)
245 		sc_height = w.ws_row;
246 	else
247 #else
248 #ifdef WIOCGETD
249 	if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_height > 0)
250 		sc_height = w.uw_height/w.uw_vs;
251 	else
252 #endif
253 #endif
254  		sc_height = tgetnum("li");
255  	hard = (sc_height < 0 || tgetflag("hc"));
256 	if (hard)
257 	{
258 		/* Oh no, this is a hardcopy terminal. */
259 		sc_height = 24;
260 	}
261 
262 #ifdef TIOCGWINSZ
263  	if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
264 		sc_width = w.ws_col;
265 	else
266 #ifdef WIOCGETD
267 	if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_width > 0)
268 		sc_width = w.uw_width/w.uw_hs;
269 	else
270 #endif
271 #endif
272  		sc_width = tgetnum("co");
273  	if (sc_width < 0)
274   		sc_width = 80;
275 
276 	auto_wrap = tgetflag("am");
277 	ignaw = tgetflag("xn");
278 
279 	/*
280 	 * Assumes termcap variable "sg" is the printing width of
281 	 * the standout sequence, the end standout sequence,
282 	 * the underline sequence, the end underline sequence,
283 	 * the boldface sequence, and the end boldface sequence.
284 	 */
285 	if ((so_width = tgetnum("sg")) < 0)
286 		so_width = 0;
287 	be_width = bo_width = ue_width = ul_width = se_width = so_width;
288 
289 	/*
290 	 * Get various string-valued capabilities.
291 	 */
292 	sp = sbuf;
293 
294 	sc_pad = tgetstr("pc", &sp);
295 	if (sc_pad != NULL)
296 		PC = *sc_pad;
297 
298 	sc_init = tgetstr("ti", &sp);
299 	if (sc_init == NULL)
300 		sc_init = "";
301 
302 	sc_deinit= tgetstr("te", &sp);
303 	if (sc_deinit == NULL)
304 		sc_deinit = "";
305 
306 	sc_eol_clear = tgetstr("ce", &sp);
307 	if (hard || sc_eol_clear == NULL || *sc_eol_clear == '\0')
308 	{
309 		cannot("clear to end of line");
310 		sc_eol_clear = "";
311 	}
312 
313 	sc_clear = tgetstr("cl", &sp);
314 	if (hard || sc_clear == NULL || *sc_clear == '\0')
315 	{
316 		cannot("clear screen");
317 		sc_clear = "\n\n";
318 	}
319 
320 	sc_move = tgetstr("cm", &sp);
321 	if (hard || sc_move == NULL || *sc_move == '\0')
322 	{
323 		/*
324 		 * This is not an error here, because we don't
325 		 * always need sc_move.
326 		 * We need it only if we don't have home or lower-left.
327 		 */
328 		sc_move = "";
329 	}
330 
331 	sc_s_in = tgetstr("so", &sp);
332 	if (hard || sc_s_in == NULL)
333 		sc_s_in = "";
334 
335 	sc_s_out = tgetstr("se", &sp);
336 	if (hard || sc_s_out == NULL)
337 		sc_s_out = "";
338 
339 	sc_u_in = tgetstr("us", &sp);
340 	if (hard || sc_u_in == NULL)
341 		sc_u_in = sc_s_in;
342 
343 	sc_u_out = tgetstr("ue", &sp);
344 	if (hard || sc_u_out == NULL)
345 		sc_u_out = sc_s_out;
346 
347 	sc_b_in = tgetstr("md", &sp);
348 	if (hard || sc_b_in == NULL)
349 	{
350 		sc_b_in = sc_s_in;
351 		sc_b_out = sc_s_out;
352 	} else
353 	{
354 		sc_b_out = tgetstr("me", &sp);
355 		if (hard || sc_b_out == NULL)
356 			sc_b_out = "";
357 	}
358 
359 	sc_visual_bell = tgetstr("vb", &sp);
360 	if (hard || sc_visual_bell == NULL)
361 		sc_visual_bell = "";
362 
363 	sc_home = tgetstr("ho", &sp);
364 	if (hard || sc_home == NULL || *sc_home == '\0')
365 	{
366 		if (*sc_move == '\0')
367 		{
368 			cannot("home cursor");
369 			/*
370 			 * This last resort for sc_home is supposed to
371 			 * be an up-arrow suggesting moving to the
372 			 * top of the "virtual screen". (The one in
373 			 * your imagination as you try to use this on
374 			 * a hard copy terminal.)
375 			 */
376 			sc_home = "|\b^";
377 		} else
378 		{
379 			/*
380 			 * No "home" string,
381 			 * but we can use "move(0,0)".
382 			 */
383 			strcpy(sp, tgoto(sc_move, 0, 0));
384 			sc_home = sp;
385 			sp += strlen(sp) + 1;
386 		}
387 	}
388 
389 	sc_lower_left = tgetstr("ll", &sp);
390 	if (hard || sc_lower_left == NULL || *sc_lower_left == '\0')
391 	{
392 		if (*sc_move == '\0')
393 		{
394 			cannot("move cursor to lower left of screen");
395 			sc_lower_left = "\r";
396 		} else
397 		{
398 			/*
399 			 * No "lower-left" string,
400 			 * but we can use "move(0,last-line)".
401 			 */
402 			strcpy(sp, tgoto(sc_move, 0, sc_height-1));
403 			sc_lower_left = sp;
404 			sp += strlen(sp) + 1;
405 		}
406 	}
407 
408 	/*
409 	 * To add a line at top of screen and scroll the display down,
410 	 * we use "al" (add line) or "sr" (scroll reverse).
411 	 */
412 	if ((sc_addline = tgetstr("al", &sp)) == NULL ||
413 		 *sc_addline == '\0')
414 		sc_addline = tgetstr("sr", &sp);
415 
416 	if (hard || sc_addline == NULL || *sc_addline == '\0')
417 	{
418 		cannot("scroll backwards");
419 		sc_addline = "";
420 		/* Force repaint on any backward movement */
421 		back_scroll = 0;
422 	}
423 
424 	if (tgetflag("bs"))
425 		sc_backspace = "\b";
426 	else
427 	{
428 		sc_backspace = tgetstr("bc", &sp);
429 		if (sc_backspace == NULL || *sc_backspace == '\0')
430 			sc_backspace = "\b";
431 	}
432 }
433 
434 
435 /*
436  * Below are the functions which perform all the
437  * terminal-specific screen manipulation.
438  */
439 
440 
441 /*
442  * Initialize terminal
443  */
444 	public void
init()445 init()
446 {
447 	tputs(sc_init, sc_height, putchr);
448 }
449 
450 /*
451  * Deinitialize terminal
452  */
453 	public void
deinit()454 deinit()
455 {
456 	tputs(sc_deinit, sc_height, putchr);
457 }
458 
459 /*
460  * Home cursor (move to upper left corner of screen).
461  */
462 	public void
home()463 home()
464 {
465 	tputs(sc_home, 1, putchr);
466 }
467 
468 /*
469  * Add a blank line (called with cursor at home).
470  * Should scroll the display down.
471  */
472 	public void
add_line()473 add_line()
474 {
475 	tputs(sc_addline, sc_height, putchr);
476 }
477 
478 /*
479  * Move cursor to lower left corner of screen.
480  */
481 	public void
lower_left()482 lower_left()
483 {
484 	tputs(sc_lower_left, 1, putchr);
485 }
486 
487 /*
488  * Ring the terminal bell.
489  */
490 	public void
bell()491 bell()
492 {
493 	if (quiet == VERY_QUIET)
494 		vbell();
495 	else
496 		putchr('\7');
497 }
498 
499 /*
500  * Output the "visual bell", if there is one.
501  */
502 	public void
vbell()503 vbell()
504 {
505 	if (*sc_visual_bell == '\0')
506 		return;
507 	tputs(sc_visual_bell, sc_height, putchr);
508 }
509 
510 /*
511  * Clear the screen.
512  */
513 	public void
clearscr()514 clearscr()
515 {
516 	tputs(sc_clear, sc_height, putchr);
517 }
518 
519 /*
520  * Clear from the cursor to the end of the cursor's line.
521  * {{ This must not move the cursor. }}
522  */
523 	public void
clear_eol()524 clear_eol()
525 {
526 	tputs(sc_eol_clear, 1, putchr);
527 }
528 
529 /*
530  * Begin "standout" (bold, underline, or whatever).
531  */
532 	public void
so_enter()533 so_enter()
534 {
535 	tputs(sc_s_in, 1, putchr);
536 }
537 
538 /*
539  * End "standout".
540  */
541 	public void
so_exit()542 so_exit()
543 {
544 	tputs(sc_s_out, 1, putchr);
545 }
546 
547 /*
548  * Begin "underline" (hopefully real underlining,
549  * otherwise whatever the terminal provides).
550  */
551 	public void
ul_enter()552 ul_enter()
553 {
554 	tputs(sc_u_in, 1, putchr);
555 }
556 
557 /*
558  * End "underline".
559  */
560 	public void
ul_exit()561 ul_exit()
562 {
563 	tputs(sc_u_out, 1, putchr);
564 }
565 
566 /*
567  * Begin "bold"
568  */
569 	public void
bo_enter()570 bo_enter()
571 {
572 	tputs(sc_b_in, 1, putchr);
573 }
574 
575 /*
576  * End "bold".
577  */
578 	public void
bo_exit()579 bo_exit()
580 {
581 	tputs(sc_b_out, 1, putchr);
582 }
583 
584 /*
585  * Erase the character to the left of the cursor
586  * and move the cursor left.
587  */
588 	public void
backspace()589 backspace()
590 {
591 	/*
592 	 * Try to erase the previous character by overstriking with a space.
593 	 */
594 	tputs(sc_backspace, 1, putchr);
595 	putchr(' ');
596 	tputs(sc_backspace, 1, putchr);
597 }
598 
599 /*
600  * Output a plain backspace, without erasing the previous char.
601  */
602 	public void
putbs()603 putbs()
604 {
605 	tputs(sc_backspace, 1, putchr);
606 }
607