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