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