output.c revision 1.3 1 1.3 perry /* $NetBSD: output.c,v 1.3 1998/01/09 08:03:33 perry 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.1 cjs #ifndef lint
38 1.1 cjs static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 4/27/95";
39 1.1 cjs #endif /* not lint */
40 1.1 cjs
41 1.1 cjs /*
42 1.1 cjs * High level routines dealing with the output to the screen.
43 1.1 cjs */
44 1.1 cjs
45 1.1 cjs #include <stdio.h>
46 1.2 cjs #include <string.h>
47 1.1 cjs #include <less.h>
48 1.1 cjs
49 1.1 cjs int errmsgs; /* Count of messages displayed by error() */
50 1.1 cjs
51 1.1 cjs extern int sigs;
52 1.1 cjs extern int sc_width, sc_height;
53 1.1 cjs extern int ul_width, ue_width;
54 1.1 cjs extern int so_width, se_width;
55 1.1 cjs extern int bo_width, be_width;
56 1.1 cjs extern int tabstop;
57 1.1 cjs extern int screen_trashed;
58 1.1 cjs extern int any_display;
59 1.1 cjs extern char *line;
60 1.1 cjs
61 1.1 cjs /* display the line which is in the line buffer. */
62 1.1 cjs put_line()
63 1.1 cjs {
64 1.1 cjs register char *p;
65 1.1 cjs register int c;
66 1.1 cjs register int column;
67 1.1 cjs extern int auto_wrap, ignaw;
68 1.1 cjs
69 1.1 cjs if (sigs)
70 1.1 cjs {
71 1.1 cjs /*
72 1.1 cjs * Don't output if a signal is pending.
73 1.1 cjs */
74 1.1 cjs screen_trashed = 1;
75 1.1 cjs return;
76 1.1 cjs }
77 1.1 cjs
78 1.1 cjs if (line == NULL)
79 1.1 cjs line = "";
80 1.1 cjs
81 1.1 cjs column = 0;
82 1.1 cjs for (p = line; *p != '\0'; p++)
83 1.1 cjs {
84 1.1 cjs switch (c = *p)
85 1.1 cjs {
86 1.1 cjs case UL_CHAR:
87 1.1 cjs ul_enter();
88 1.1 cjs column += ul_width +1;
89 1.1 cjs break;
90 1.1 cjs case UE_CHAR:
91 1.1 cjs ul_exit();
92 1.1 cjs column += ue_width;
93 1.1 cjs break;
94 1.1 cjs case BO_CHAR:
95 1.1 cjs bo_enter();
96 1.1 cjs column += bo_width +1;
97 1.1 cjs break;
98 1.1 cjs case BE_CHAR:
99 1.1 cjs bo_exit();
100 1.1 cjs column += be_width;
101 1.1 cjs break;
102 1.1 cjs case '\t':
103 1.1 cjs do
104 1.1 cjs {
105 1.1 cjs putchr(' ');
106 1.1 cjs column++;
107 1.1 cjs } while ((column % tabstop) != 0);
108 1.1 cjs break;
109 1.1 cjs case '\b':
110 1.1 cjs putbs();
111 1.1 cjs column--;
112 1.1 cjs break;
113 1.1 cjs default:
114 1.1 cjs if (c & 0200)
115 1.1 cjs {
116 1.1 cjs /*
117 1.1 cjs * Control characters arrive here as the
118 1.1 cjs * normal character [CARAT_CHAR(c)] with
119 1.1 cjs * the 0200 bit set. See pappend().
120 1.1 cjs */
121 1.1 cjs putchr('^');
122 1.1 cjs putchr(c & 0177);
123 1.1 cjs column += 2;
124 1.1 cjs } else
125 1.1 cjs {
126 1.1 cjs putchr(c);
127 1.1 cjs column++;
128 1.1 cjs }
129 1.1 cjs }
130 1.1 cjs }
131 1.1 cjs if (column < sc_width || !auto_wrap || ignaw)
132 1.1 cjs putchr('\n');
133 1.1 cjs }
134 1.1 cjs
135 1.1 cjs static char obuf[1024];
136 1.1 cjs static char *ob = obuf;
137 1.1 cjs
138 1.1 cjs /*
139 1.1 cjs * Flush buffered output.
140 1.1 cjs */
141 1.1 cjs flush()
142 1.1 cjs {
143 1.1 cjs register 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.1 cjs purge()
157 1.1 cjs {
158 1.1 cjs
159 1.1 cjs ob = obuf;
160 1.1 cjs }
161 1.1 cjs
162 1.1 cjs /*
163 1.1 cjs * Output a character.
164 1.1 cjs */
165 1.1 cjs putchr(c)
166 1.1 cjs int c;
167 1.1 cjs {
168 1.1 cjs if (ob >= &obuf[sizeof(obuf)])
169 1.1 cjs flush();
170 1.1 cjs *ob++ = c;
171 1.1 cjs }
172 1.1 cjs
173 1.1 cjs /*
174 1.1 cjs * Output a string.
175 1.1 cjs */
176 1.1 cjs putstr(s)
177 1.1 cjs register char *s;
178 1.1 cjs {
179 1.1 cjs while (*s != '\0')
180 1.1 cjs putchr(*s++);
181 1.1 cjs }
182 1.1 cjs
183 1.1 cjs int cmdstack;
184 1.1 cjs static char return_to_continue[] = "(press RETURN)";
185 1.1 cjs
186 1.1 cjs /*
187 1.1 cjs * Output a message in the lower left corner of the screen
188 1.1 cjs * and wait for carriage return.
189 1.1 cjs */
190 1.1 cjs error(s)
191 1.1 cjs char *s;
192 1.1 cjs {
193 1.1 cjs int ch;
194 1.1 cjs
195 1.1 cjs ++errmsgs;
196 1.1 cjs if (!any_display) {
197 1.1 cjs /*
198 1.1 cjs * Nothing has been displayed yet. Output this message on
199 1.1 cjs * error output (file descriptor 2) and don't wait for a
200 1.1 cjs * keystroke to continue.
201 1.1 cjs *
202 1.1 cjs * This has the desirable effect of producing all error
203 1.1 cjs * messages on error output if standard output is directed
204 1.1 cjs * to a file. It also does the same if we never produce
205 1.1 cjs * any real output; for example, if the input file(s) cannot
206 1.1 cjs * be opened. If we do eventually produce output, code in
207 1.1 cjs * edit() makes sure these messages can be seen before they
208 1.1 cjs * are overwritten or scrolled away.
209 1.1 cjs */
210 1.1 cjs if (s != NULL) {
211 1.1 cjs (void)write(2, s, strlen(s));
212 1.1 cjs (void)write(2, "\n", 1);
213 1.1 cjs }
214 1.1 cjs return;
215 1.1 cjs }
216 1.1 cjs
217 1.1 cjs lower_left();
218 1.1 cjs clear_eol();
219 1.1 cjs so_enter();
220 1.1 cjs if (s != NULL) {
221 1.1 cjs putstr(s);
222 1.1 cjs putstr(" ");
223 1.1 cjs }
224 1.1 cjs putstr(return_to_continue);
225 1.1 cjs so_exit();
226 1.1 cjs
227 1.1 cjs if ((ch = getchr()) != '\n') {
228 1.1 cjs if (ch == 'q')
229 1.1 cjs quit();
230 1.1 cjs cmdstack = ch;
231 1.1 cjs }
232 1.1 cjs lower_left();
233 1.1 cjs
234 1.1 cjs if ((s != NULL ? strlen(s) : 0) + sizeof(return_to_continue) +
235 1.1 cjs so_width + se_width + 1 > sc_width)
236 1.1 cjs /*
237 1.1 cjs * Printing the message has probably scrolled the screen.
238 1.1 cjs * {{ Unless the terminal doesn't have auto margins,
239 1.1 cjs * in which case we just hammered on the right margin. }}
240 1.1 cjs */
241 1.1 cjs repaint();
242 1.1 cjs flush();
243 1.1 cjs }
244 1.1 cjs
245 1.1 cjs static char intr_to_abort[] = "... (interrupt to abort)";
246 1.1 cjs
247 1.1 cjs ierror(s)
248 1.1 cjs char *s;
249 1.1 cjs {
250 1.1 cjs lower_left();
251 1.1 cjs clear_eol();
252 1.1 cjs so_enter();
253 1.1 cjs putstr(s);
254 1.1 cjs putstr(intr_to_abort);
255 1.1 cjs so_exit();
256 1.1 cjs flush();
257 1.1 cjs }
258