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