xref: /illumos-gate/usr/src/boot/libsa/pager.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 
32 #include "stand.h"
33 #include <string.h>
34 
35 static int	p_maxlines = -1;
36 static int	p_freelines;
37 
38 static char *pager_prompt1 = \
39 	" --more--  <space> page down <enter> line down <q> quit ";
40 static char *pager_blank   = \
41 	"                                                        ";
42 
43 /*
44  * 'open' the pager
45  */
46 void
47 pager_open(void)
48 {
49 	int	nlines;
50 	char	*cp, *lp;
51 
52 	nlines = 24;		/* sensible default */
53 	if ((cp = getenv("screen-#rows")) != NULL) {
54 		nlines = strtol(cp, &lp, 0);
55 	}
56 
57 	p_maxlines = nlines - 1;
58 	if (p_maxlines < 1)
59 		p_maxlines = 1;
60 	p_freelines = p_maxlines;
61 }
62 
63 /*
64  * 'close' the pager
65  */
66 void
67 pager_close(void)
68 {
69 	p_maxlines = -1;
70 }
71 
72 /*
73  * Emit lines to the pager; may not return until the user
74  * has responded to the prompt.
75  *
76  * Will return nonzero if the user enters 'q' or 'Q' at the prompt.
77  *
78  * XXX note that this watches outgoing newlines (and eats them), but
79  *     does not handle wrap detection (req. count of columns).
80  */
81 
82 int
83 pager_output(const char *cp)
84 {
85 	int	action;
86 
87 	if (cp == NULL)
88 		return (0);
89 
90 	for (;;) {
91 		if (*cp == 0)
92 			return (0);
93 
94 		putchar(*cp);			/* always emit character */
95 
96 		if (*(cp++) == '\n') {		/* got a newline? */
97 			p_freelines--;
98 			if (p_freelines <= 0) {
99 				printf("%s", pager_prompt1);
100 				action = 0;
101 				while (action == 0) {
102 					switch (getchar()) {
103 					case '\r':
104 					case '\n':
105 						p_freelines = 1;
106 						action = 1;
107 						break;
108 					case ' ':
109 						p_freelines = p_maxlines;
110 						action = 1;
111 						break;
112 					case 'q':
113 					case 'Q':
114 						action = 2;
115 						break;
116 					default:
117 						break;
118 					}
119 				}
120 				printf("\r%s\r", pager_blank);
121 				if (action == 2)
122 					return (1);
123 			}
124 		}
125 	}
126 }
127 
128 /*
129  * Display from (fd).
130  */
131 int
132 pager_file(const char *fname)
133 {
134 	char	buf[80];
135 	size_t	hmuch;
136 	int	fd;
137 	int	result;
138 
139 	if ((fd = open(fname, O_RDONLY)) == -1) {
140 		printf("can't open '%s': %s\n", fname, strerror(errno));
141 		return (-1);
142 	}
143 
144 	for (;;) {
145 		hmuch = read(fd, buf, sizeof (buf) - 1);
146 		if (hmuch == -1) {
147 			result = -1;
148 			break;
149 		}
150 		if (hmuch == 0) {
151 			result = 0;
152 			break;
153 		}
154 		buf[hmuch] = 0;
155 		if (pager_output(buf)) {
156 			result = 1;
157 			break;
158 		}
159 	}
160 	close(fd);
161 	return (result);
162 }
163