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