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