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