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