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