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