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