less -> more
[unix-history] / usr / src / usr.bin / more / output.c
CommitLineData
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
966c6ec0 21static char sccsid[] = "@(#)output.c 5.5 (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 31int errmsgs; /* Count of messages displayed by error() */
bfe13c81
KB
32
33extern int sigs;
34extern int sc_width, sc_height;
35extern int ul_width, ue_width;
36extern int so_width, se_width;
37extern int bo_width, be_width;
38extern int tabstop;
bfe13c81
KB
39extern int screen_trashed;
40extern int any_display;
41extern char *line;
bfe13c81
KB
42
43/*
44 * Display the line which is in the line buffer.
45 */
bfe13c81
KB
46put_line()
47{
48 register char *p;
49 register int c;
50 register int column;
51 extern int auto_wrap, ignaw;
52
53 if (sigs)
54 {
55 /*
56 * Don't output if a signal is pending.
57 */
58 screen_trashed = 1;
59 return;
60 }
61
62 if (line == NULL)
966c6ec0 63 line = "";
bfe13c81
KB
64
65 column = 0;
66 for (p = line; *p != '\0'; p++)
67 {
68 switch (c = *p)
69 {
70 case UL_CHAR:
71 ul_enter();
72 column += ul_width;
73 break;
74 case UE_CHAR:
75 ul_exit();
76 column += ue_width;
77 break;
78 case BO_CHAR:
79 bo_enter();
80 column += bo_width;
81 break;
82 case BE_CHAR:
83 bo_exit();
84 column += be_width;
85 break;
86 case '\t':
87 do
88 {
89 putchr(' ');
90 column++;
91 } while ((column % tabstop) != 0);
92 break;
93 case '\b':
94 putbs();
95 column--;
96 break;
97 default:
98 if (c & 0200)
99 {
100 /*
101 * Control characters arrive here as the
966c6ec0 102 * normal character [CARAT_CHAR(c)] with
bfe13c81
KB
103 * the 0200 bit set. See pappend().
104 */
105 putchr('^');
106 putchr(c & 0177);
107 column += 2;
108 } else
109 {
110 putchr(c);
111 column++;
112 }
113 }
114 }
115 if (column < sc_width || !auto_wrap || ignaw)
116 putchr('\n');
117}
118
bfe13c81
KB
119static char obuf[1024];
120static char *ob = obuf;
121
122/*
123 * Flush buffered output.
124 */
bfe13c81
KB
125flush()
126{
127 register int n;
128
129 n = ob - obuf;
130 if (n == 0)
131 return;
132 if (write(1, obuf, n) != n)
133 screen_trashed = 1;
134 ob = obuf;
135}
136
bfe13c81
KB
137/*
138 * Output a character.
139 */
bfe13c81
KB
140putchr(c)
141 int c;
142{
143 if (ob >= &obuf[sizeof(obuf)])
144 flush();
145 *ob++ = c;
146}
147
148/*
149 * Output a string.
150 */
bfe13c81
KB
151putstr(s)
152 register char *s;
153{
154 while (*s != '\0')
155 putchr(*s++);
156}
157
158/*
159 * Output a message in the lower left corner of the screen
160 * and wait for carriage return.
161 */
162
163static char return_to_continue[] = " (press RETURN)";
164
bfe13c81
KB
165error(s)
166 char *s;
167{
966c6ec0
KB
168 ++errmsgs;
169 if (!any_display) {
bfe13c81 170 /*
966c6ec0
KB
171 * Nothing has been displayed yet. Output this message on
172 * error output (file descriptor 2) and don't wait for a
173 * keystroke to continue.
bfe13c81 174 *
966c6ec0
KB
175 * This has the desirable effect of producing all error
176 * messages on error output if standard output is directed
177 * to a file. It also does the same if we never produce
178 * any real output; for example, if the input file(s) cannot
179 * be opened. If we do eventually produce output, code in
180 * edit() makes sure these messages can be seen before they
181 * are overwritten or scrolled away.
bfe13c81 182 */
966c6ec0
KB
183 (void)write(2, s, strlen(s));
184 (void)write(2, "\n", 1);
bfe13c81
KB
185 return;
186 }
187
188 lower_left();
189 clear_eol();
190 so_enter();
191 putstr(s);
192 putstr(return_to_continue);
193 so_exit();
194
966c6ec0 195 (void)getchr();
bfe13c81
KB
196 lower_left();
197
198 if (strlen(s) + sizeof(return_to_continue) +
199 so_width + se_width + 1 > sc_width)
200 /*
201 * Printing the message has probably scrolled the screen.
202 * {{ Unless the terminal doesn't have auto margins,
203 * in which case we just hammered on the right margin. }}
204 */
205 repaint();
bfe13c81
KB
206 flush();
207}
208
209static char intr_to_abort[] = "... (interrupt to abort)";
210
bfe13c81
KB
211ierror(s)
212 char *s;
213{
bfe13c81
KB
214 lower_left();
215 clear_eol();
216 so_enter();
217 putstr(s);
218 putstr(intr_to_abort);
219 so_exit();
220 flush();
221}