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