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