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