output.c revision 1.5 1 1.5 dan /* $NetBSD: output.c,v 1.5 1999/10/19 23:09:45 dan Exp $ */
2 1.3 perry
3 1.1 cjs /*
4 1.1 cjs * Copyright (c) 1988 Mark Nudleman
5 1.1 cjs * Copyright (c) 1988, 1993
6 1.1 cjs * The Regents of the University of California. All rights reserved.
7 1.1 cjs *
8 1.1 cjs * Redistribution and use in source and binary forms, with or without
9 1.1 cjs * modification, are permitted provided that the following conditions
10 1.1 cjs * are met:
11 1.1 cjs * 1. Redistributions of source code must retain the above copyright
12 1.1 cjs * notice, this list of conditions and the following disclaimer.
13 1.1 cjs * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cjs * notice, this list of conditions and the following disclaimer in the
15 1.1 cjs * documentation and/or other materials provided with the distribution.
16 1.1 cjs * 3. All advertising materials mentioning features or use of this software
17 1.1 cjs * must display the following acknowledgement:
18 1.1 cjs * This product includes software developed by the University of
19 1.1 cjs * California, Berkeley and its contributors.
20 1.1 cjs * 4. Neither the name of the University nor the names of its contributors
21 1.1 cjs * may be used to endorse or promote products derived from this software
22 1.1 cjs * without specific prior written permission.
23 1.1 cjs *
24 1.1 cjs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cjs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cjs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cjs * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cjs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cjs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cjs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cjs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cjs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cjs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cjs * SUCH DAMAGE.
35 1.1 cjs */
36 1.1 cjs
37 1.4 christos #include <sys/cdefs.h>
38 1.1 cjs #ifndef lint
39 1.4 christos #if 0
40 1.1 cjs static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 4/27/95";
41 1.4 christos #else
42 1.5 dan __RCSID("$NetBSD: output.c,v 1.5 1999/10/19 23:09:45 dan Exp $");
43 1.4 christos #endif
44 1.1 cjs #endif /* not lint */
45 1.1 cjs
46 1.1 cjs /*
47 1.1 cjs * High level routines dealing with the output to the screen.
48 1.1 cjs */
49 1.1 cjs
50 1.1 cjs #include <stdio.h>
51 1.2 cjs #include <string.h>
52 1.4 christos #include <unistd.h>
53 1.4 christos #include <stdlib.h>
54 1.4 christos
55 1.4 christos #include "less.h"
56 1.4 christos #include "extern.h"
57 1.1 cjs
58 1.1 cjs int errmsgs; /* Count of messages displayed by error() */
59 1.1 cjs
60 1.1 cjs /* display the line which is in the line buffer. */
61 1.4 christos void
62 1.1 cjs put_line()
63 1.1 cjs {
64 1.4 christos char *p;
65 1.4 christos int c;
66 1.4 christos int column;
67 1.1 cjs
68 1.1 cjs if (sigs)
69 1.1 cjs {
70 1.1 cjs /*
71 1.1 cjs * Don't output if a signal is pending.
72 1.1 cjs */
73 1.1 cjs screen_trashed = 1;
74 1.1 cjs return;
75 1.1 cjs }
76 1.1 cjs
77 1.1 cjs if (line == NULL)
78 1.1 cjs line = "";
79 1.1 cjs
80 1.1 cjs column = 0;
81 1.1 cjs for (p = line; *p != '\0'; p++)
82 1.1 cjs {
83 1.1 cjs switch (c = *p)
84 1.1 cjs {
85 1.1 cjs case UL_CHAR:
86 1.1 cjs ul_enter();
87 1.1 cjs column += ul_width +1;
88 1.1 cjs break;
89 1.1 cjs case UE_CHAR:
90 1.1 cjs ul_exit();
91 1.1 cjs column += ue_width;
92 1.1 cjs break;
93 1.1 cjs case BO_CHAR:
94 1.1 cjs bo_enter();
95 1.1 cjs column += bo_width +1;
96 1.1 cjs break;
97 1.1 cjs case BE_CHAR:
98 1.1 cjs bo_exit();
99 1.1 cjs column += be_width;
100 1.1 cjs break;
101 1.1 cjs case '\t':
102 1.1 cjs do
103 1.1 cjs {
104 1.1 cjs putchr(' ');
105 1.1 cjs column++;
106 1.1 cjs } while ((column % tabstop) != 0);
107 1.1 cjs break;
108 1.1 cjs case '\b':
109 1.1 cjs putbs();
110 1.1 cjs column--;
111 1.1 cjs break;
112 1.1 cjs default:
113 1.1 cjs if (c & 0200)
114 1.1 cjs {
115 1.1 cjs /*
116 1.1 cjs * Control characters arrive here as the
117 1.1 cjs * normal character [CARAT_CHAR(c)] with
118 1.1 cjs * the 0200 bit set. See pappend().
119 1.1 cjs */
120 1.1 cjs putchr('^');
121 1.1 cjs putchr(c & 0177);
122 1.1 cjs column += 2;
123 1.1 cjs } else
124 1.1 cjs {
125 1.1 cjs putchr(c);
126 1.1 cjs column++;
127 1.1 cjs }
128 1.1 cjs }
129 1.1 cjs }
130 1.1 cjs if (column < sc_width || !auto_wrap || ignaw)
131 1.1 cjs putchr('\n');
132 1.1 cjs }
133 1.1 cjs
134 1.1 cjs static char obuf[1024];
135 1.1 cjs static char *ob = obuf;
136 1.1 cjs
137 1.1 cjs /*
138 1.1 cjs * Flush buffered output.
139 1.1 cjs */
140 1.4 christos void
141 1.1 cjs flush()
142 1.1 cjs {
143 1.4 christos int n;
144 1.1 cjs
145 1.1 cjs n = ob - obuf;
146 1.1 cjs if (n == 0)
147 1.1 cjs return;
148 1.1 cjs if (write(1, obuf, n) != n)
149 1.1 cjs screen_trashed = 1;
150 1.1 cjs ob = obuf;
151 1.1 cjs }
152 1.1 cjs
153 1.1 cjs /*
154 1.1 cjs * Purge any pending output.
155 1.1 cjs */
156 1.4 christos void
157 1.1 cjs purge()
158 1.1 cjs {
159 1.1 cjs
160 1.1 cjs ob = obuf;
161 1.1 cjs }
162 1.1 cjs
163 1.1 cjs /*
164 1.1 cjs * Output a character.
165 1.1 cjs */
166 1.5 dan int
167 1.1 cjs putchr(c)
168 1.1 cjs int c;
169 1.1 cjs {
170 1.1 cjs if (ob >= &obuf[sizeof(obuf)])
171 1.1 cjs flush();
172 1.1 cjs *ob++ = c;
173 1.1 cjs }
174 1.1 cjs
175 1.1 cjs /*
176 1.1 cjs * Output a string.
177 1.1 cjs */
178 1.4 christos void
179 1.1 cjs putstr(s)
180 1.4 christos char *s;
181 1.1 cjs {
182 1.1 cjs while (*s != '\0')
183 1.1 cjs putchr(*s++);
184 1.1 cjs }
185 1.1 cjs
186 1.1 cjs int cmdstack;
187 1.1 cjs static char return_to_continue[] = "(press RETURN)";
188 1.1 cjs
189 1.1 cjs /*
190 1.1 cjs * Output a message in the lower left corner of the screen
191 1.1 cjs * and wait for carriage return.
192 1.1 cjs */
193 1.4 christos void
194 1.1 cjs error(s)
195 1.1 cjs char *s;
196 1.1 cjs {
197 1.1 cjs int ch;
198 1.1 cjs
199 1.1 cjs ++errmsgs;
200 1.1 cjs if (!any_display) {
201 1.1 cjs /*
202 1.1 cjs * Nothing has been displayed yet. Output this message on
203 1.1 cjs * error output (file descriptor 2) and don't wait for a
204 1.1 cjs * keystroke to continue.
205 1.1 cjs *
206 1.1 cjs * This has the desirable effect of producing all error
207 1.1 cjs * messages on error output if standard output is directed
208 1.1 cjs * to a file. It also does the same if we never produce
209 1.1 cjs * any real output; for example, if the input file(s) cannot
210 1.1 cjs * be opened. If we do eventually produce output, code in
211 1.1 cjs * edit() makes sure these messages can be seen before they
212 1.1 cjs * are overwritten or scrolled away.
213 1.1 cjs */
214 1.1 cjs if (s != NULL) {
215 1.1 cjs (void)write(2, s, strlen(s));
216 1.1 cjs (void)write(2, "\n", 1);
217 1.1 cjs }
218 1.1 cjs return;
219 1.1 cjs }
220 1.1 cjs
221 1.1 cjs lower_left();
222 1.1 cjs clear_eol();
223 1.1 cjs so_enter();
224 1.1 cjs if (s != NULL) {
225 1.1 cjs putstr(s);
226 1.1 cjs putstr(" ");
227 1.1 cjs }
228 1.1 cjs putstr(return_to_continue);
229 1.1 cjs so_exit();
230 1.1 cjs
231 1.1 cjs if ((ch = getchr()) != '\n') {
232 1.1 cjs if (ch == 'q')
233 1.1 cjs quit();
234 1.1 cjs cmdstack = ch;
235 1.1 cjs }
236 1.1 cjs lower_left();
237 1.1 cjs
238 1.1 cjs if ((s != NULL ? strlen(s) : 0) + sizeof(return_to_continue) +
239 1.1 cjs so_width + se_width + 1 > sc_width)
240 1.1 cjs /*
241 1.1 cjs * Printing the message has probably scrolled the screen.
242 1.1 cjs * {{ Unless the terminal doesn't have auto margins,
243 1.1 cjs * in which case we just hammered on the right margin. }}
244 1.1 cjs */
245 1.1 cjs repaint();
246 1.1 cjs flush();
247 1.1 cjs }
248 1.1 cjs
249 1.1 cjs static char intr_to_abort[] = "... (interrupt to abort)";
250 1.1 cjs
251 1.4 christos void
252 1.1 cjs ierror(s)
253 1.1 cjs char *s;
254 1.1 cjs {
255 1.1 cjs lower_left();
256 1.1 cjs clear_eol();
257 1.1 cjs so_enter();
258 1.1 cjs putstr(s);
259 1.1 cjs putstr(intr_to_abort);
260 1.1 cjs so_exit();
261 1.1 cjs flush();
262 1.1 cjs }
263