xref: /freebsd/stand/libsa/pager.c (revision ec994981447e8a974426660b5071bc405280af73)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /*
27  * Simple paged-output and paged-viewing functions
28  */
29 
30 #include <sys/cdefs.h>
31 #include "stand.h"
32 #include <string.h>
33 
34 static int	p_maxlines = -1;
35 static int	p_freelines;
36 
37 static char *pager_prompt1 = " --more--  <space> page down <enter> line down <q> quit ";
38 static char *pager_blank   = "                                                        ";
39 
40 /*
41  * 'open' the pager
42  */
43 void
44 pager_open(void)
45 {
46     int		nlines;
47     char	*cp, *lp;
48 
49     nlines = 24;		/* sensible default */
50     if ((cp = getenv("LINES")) != NULL) {
51 	nlines = strtol(cp, &lp, 0);
52     }
53 
54     p_maxlines = nlines - 1;
55     if (p_maxlines < 1)
56 	p_maxlines = 1;
57     p_freelines = p_maxlines;
58 }
59 
60 /*
61  * 'close' the pager
62  */
63 void
64 pager_close(void)
65 {
66     p_maxlines = -1;
67 }
68 
69 /*
70  * Emit lines to the pager; may not return until the user
71  * has responded to the prompt.
72  *
73  * Will return nonzero if the user enters 'q' or 'Q' at the prompt.
74  *
75  * XXX note that this watches outgoing newlines (and eats them), but
76  *     does not handle wrap detection (req. count of columns).
77  */
78 
79 int
80 pager_output(const char *cp)
81 {
82     int		action;
83 
84     if (cp == NULL)
85 	return(0);
86 
87     for (;;) {
88 	if (*cp == 0)
89 	    return(0);
90 
91 	putchar(*cp);			/* always emit character */
92 
93 	if (*(cp++) == '\n') {		/* got a newline? */
94 	    p_freelines--;
95 	    if (p_freelines <= 0) {
96 		printf("%s", pager_prompt1);
97 		action = 0;
98 		while (action == 0) {
99 		    switch(getchar()) {
100 		    case '\r':
101 		    case '\n':
102 			p_freelines = 1;
103 			action = 1;
104 			break;
105 		    case ' ':
106 			p_freelines = p_maxlines;
107 			action = 1;
108 			break;
109 		    case 'q':
110 		    case 'Q':
111 			action = 2;
112 			break;
113 		    default:
114 			break;
115 		    }
116 		}
117 		printf("\r%s\r", pager_blank);
118 		if (action == 2)
119 		    return(1);
120 	    }
121 	}
122     }
123 }
124 
125 /*
126  * Display from (fd).
127  */
128 int
129 pager_file(const char *fname)
130 {
131     char	buf[80];
132     size_t	hmuch;
133     int		fd;
134     int		result;
135 
136     if ((fd = open(fname, O_RDONLY)) == -1) {
137 	printf("can't open '%s': %s\n", fname, strerror(errno));
138 	return(-1);
139     }
140 
141     for (;;) {
142 	hmuch = read(fd, buf, sizeof(buf) - 1);
143 	if (hmuch == -1) {
144 	    result = -1;
145 	    break;
146 	}
147 	if (hmuch == 0) {
148 	    result = 0;
149 	    break;
150 	}
151 	buf[hmuch] = 0;
152 	if (pager_output(buf)) {
153 	    result = 1;
154 	    break;
155 	}
156     }
157     close(fd);
158     return(result);
159 }
160