colcrt.c revision 1.4 1 1.4 lukem /* $NetBSD: colcrt.c,v 1.4 1997/10/18 12:59:10 lukem Exp $ */
2 1.3 glass
3 1.1 cgd /*
4 1.3 glass * Copyright (c) 1980, 1993
5 1.3 glass * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.4 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.4 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 1.4 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.3 glass #if 0
44 1.3 glass static char sccsid[] = "@(#)colcrt.c 8.1 (Berkeley) 6/6/93";
45 1.3 glass #else
46 1.4 lukem __RCSID("$NetBSD: colcrt.c,v 1.4 1997/10/18 12:59:10 lukem Exp $");
47 1.3 glass #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <stdio.h>
51 1.4 lukem #include <string.h>
52 1.4 lukem #include <unistd.h>
53 1.4 lukem
54 1.1 cgd /*
55 1.1 cgd * colcrt - replaces col for crts with new nroff esp. when using tbl.
56 1.1 cgd * Bill Joy UCB July 14, 1977
57 1.1 cgd *
58 1.1 cgd * This filter uses a screen buffer, 267 half-lines by 132 columns.
59 1.1 cgd * It interprets the up and down sequences generated by the new
60 1.1 cgd * nroff when used with tbl and by \u \d and \r.
61 1.1 cgd * General overstriking doesn't work correctly.
62 1.1 cgd * Underlining is split onto multiple lines, etc.
63 1.1 cgd *
64 1.1 cgd * Option - suppresses all underlining.
65 1.1 cgd * Option -2 forces printing of all half lines.
66 1.1 cgd */
67 1.1 cgd
68 1.1 cgd char page[267][132];
69 1.1 cgd
70 1.1 cgd int outline = 1;
71 1.1 cgd int outcol;
72 1.1 cgd
73 1.1 cgd char suppresul;
74 1.1 cgd char printall;
75 1.1 cgd
76 1.1 cgd char *progname;
77 1.1 cgd FILE *f;
78 1.1 cgd
79 1.4 lukem int main __P((int, char **));
80 1.4 lukem void move __P((int, int));
81 1.4 lukem void pflush __P((int));
82 1.4 lukem int plus __P((char, char));
83 1.4 lukem
84 1.4 lukem int
85 1.1 cgd main(argc, argv)
86 1.1 cgd int argc;
87 1.1 cgd char *argv[];
88 1.1 cgd {
89 1.4 lukem int c;
90 1.4 lukem char *cp, *dp;
91 1.1 cgd
92 1.1 cgd argc--;
93 1.1 cgd progname = *argv++;
94 1.1 cgd while (argc > 0 && argv[0][0] == '-') {
95 1.1 cgd switch (argv[0][1]) {
96 1.1 cgd case 0:
97 1.1 cgd suppresul = 1;
98 1.1 cgd break;
99 1.1 cgd case '2':
100 1.1 cgd printall = 1;
101 1.1 cgd break;
102 1.1 cgd default:
103 1.1 cgd printf("usage: %s [ - ] [ -2 ] [ file ... ]\n", progname);
104 1.1 cgd fflush(stdout);
105 1.1 cgd exit(1);
106 1.1 cgd }
107 1.1 cgd argc--;
108 1.1 cgd argv++;
109 1.1 cgd }
110 1.1 cgd do {
111 1.1 cgd if (argc > 0) {
112 1.1 cgd close(0);
113 1.1 cgd if (!(f = fopen(argv[0], "r"))) {
114 1.1 cgd fflush(stdout);
115 1.1 cgd perror(argv[0]);
116 1.1 cgd exit (1);
117 1.1 cgd }
118 1.1 cgd argc--;
119 1.1 cgd argv++;
120 1.1 cgd }
121 1.1 cgd for (;;) {
122 1.1 cgd c = getc(stdin);
123 1.1 cgd if (c == -1) {
124 1.1 cgd pflush(outline);
125 1.1 cgd fflush(stdout);
126 1.1 cgd break;
127 1.1 cgd }
128 1.1 cgd switch (c) {
129 1.1 cgd case '\n':
130 1.1 cgd if (outline >= 265)
131 1.1 cgd pflush(62);
132 1.1 cgd outline += 2;
133 1.1 cgd outcol = 0;
134 1.1 cgd continue;
135 1.1 cgd case '\016':
136 1.1 cgd case '\017':
137 1.1 cgd continue;
138 1.1 cgd case 033:
139 1.1 cgd c = getc(stdin);
140 1.1 cgd switch (c) {
141 1.1 cgd case '9':
142 1.1 cgd if (outline >= 266)
143 1.1 cgd pflush(62);
144 1.1 cgd outline++;
145 1.1 cgd continue;
146 1.1 cgd case '8':
147 1.1 cgd if (outline >= 1)
148 1.1 cgd outline--;
149 1.1 cgd continue;
150 1.1 cgd case '7':
151 1.1 cgd outline -= 2;
152 1.1 cgd if (outline < 0)
153 1.1 cgd outline = 0;
154 1.1 cgd continue;
155 1.1 cgd default:
156 1.1 cgd continue;
157 1.1 cgd }
158 1.1 cgd case '\b':
159 1.1 cgd if (outcol)
160 1.1 cgd outcol--;
161 1.1 cgd continue;
162 1.1 cgd case '\t':
163 1.1 cgd outcol += 8;
164 1.1 cgd outcol &= ~7;
165 1.1 cgd outcol--;
166 1.1 cgd c = ' ';
167 1.1 cgd default:
168 1.1 cgd if (outcol >= 132) {
169 1.1 cgd outcol++;
170 1.1 cgd continue;
171 1.1 cgd }
172 1.1 cgd cp = &page[outline][outcol];
173 1.1 cgd outcol++;
174 1.1 cgd if (c == '_') {
175 1.1 cgd if (suppresul)
176 1.1 cgd continue;
177 1.1 cgd cp += 132;
178 1.1 cgd c = '-';
179 1.1 cgd }
180 1.1 cgd if (*cp == 0) {
181 1.1 cgd *cp = c;
182 1.1 cgd dp = cp - outcol;
183 1.1 cgd for (cp--; cp >= dp && *cp == 0; cp--)
184 1.1 cgd *cp = ' ';
185 1.1 cgd } else
186 1.1 cgd if (plus(c, *cp) || plus(*cp, c))
187 1.1 cgd *cp = '+';
188 1.1 cgd else if (*cp == ' ' || *cp == 0)
189 1.1 cgd *cp = c;
190 1.1 cgd continue;
191 1.1 cgd }
192 1.1 cgd }
193 1.1 cgd } while (argc > 0);
194 1.1 cgd fflush(stdout);
195 1.1 cgd exit(0);
196 1.1 cgd }
197 1.1 cgd
198 1.4 lukem int
199 1.1 cgd plus(c, d)
200 1.1 cgd char c, d;
201 1.1 cgd {
202 1.1 cgd
203 1.4 lukem return ((c == '|' && d == '-') || d == '_');
204 1.1 cgd }
205 1.1 cgd
206 1.1 cgd int first;
207 1.1 cgd
208 1.4 lukem void
209 1.1 cgd pflush(ol)
210 1.1 cgd int ol;
211 1.1 cgd {
212 1.4 lukem int i;
213 1.4 lukem char *cp;
214 1.1 cgd char lastomit;
215 1.1 cgd int l;
216 1.1 cgd
217 1.1 cgd l = ol;
218 1.1 cgd lastomit = 0;
219 1.1 cgd if (l > 266)
220 1.1 cgd l = 266;
221 1.1 cgd else
222 1.1 cgd l |= 1;
223 1.1 cgd for (i = first | 1; i < l; i++) {
224 1.1 cgd move(i, i - 1);
225 1.1 cgd move(i, i + 1);
226 1.1 cgd }
227 1.1 cgd for (i = first; i < l; i++) {
228 1.1 cgd cp = page[i];
229 1.1 cgd if (printall == 0 && lastomit == 0 && *cp == 0) {
230 1.1 cgd lastomit = 1;
231 1.1 cgd continue;
232 1.1 cgd }
233 1.1 cgd lastomit = 0;
234 1.1 cgd printf("%s\n", cp);
235 1.1 cgd }
236 1.4 lukem memmove(page, page[ol], (267 - ol) * 132);
237 1.4 lukem memset(page[267- ol], 0, ol * 132);
238 1.1 cgd outline -= ol;
239 1.1 cgd outcol = 0;
240 1.1 cgd first = 1;
241 1.1 cgd }
242 1.1 cgd
243 1.4 lukem void
244 1.1 cgd move(l, m)
245 1.1 cgd int l, m;
246 1.1 cgd {
247 1.4 lukem char *cp, *dp;
248 1.1 cgd
249 1.1 cgd for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
250 1.1 cgd switch (*cp) {
251 1.1 cgd case '|':
252 1.1 cgd if (*dp != ' ' && *dp != '|' && *dp != 0)
253 1.1 cgd return;
254 1.1 cgd break;
255 1.1 cgd case ' ':
256 1.1 cgd break;
257 1.1 cgd default:
258 1.1 cgd return;
259 1.1 cgd }
260 1.1 cgd }
261 1.1 cgd if (*cp == 0) {
262 1.1 cgd for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
263 1.1 cgd if (*cp == '|')
264 1.1 cgd *dp = '|';
265 1.1 cgd else if (*dp == 0)
266 1.1 cgd *dp = ' ';
267 1.1 cgd page[l][0] = 0;
268 1.1 cgd }
269 1.1 cgd }
270