]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/colcrt/colcrt.c
This commit was generated by cvs2svn to compensate for changes in r57422,
[FreeBSD/FreeBSD.git] / usr.bin / colcrt / colcrt.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 static const char sccsid[] = "@(#)colcrt.c      8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43
44 #include <err.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 /*
50  * colcrt - replaces col for crts with new nroff esp. when using tbl.
51  * Bill Joy UCB July 14, 1977
52  *
53  * This filter uses a screen buffer, 267 half-lines by 132 columns.
54  * It interprets the up and down sequences generated by the new
55  * nroff when used with tbl and by \u \d and \r.
56  * General overstriking doesn't work correctly.
57  * Underlining is split onto multiple lines, etc.
58  *
59  * Option - suppresses all underlining.
60  * Option -2 forces printing of all half lines.
61  */
62
63 char    page[267][132];
64
65 int     outline = 1;
66 int     outcol;
67
68 char    suppresul;
69 char    printall;
70
71 FILE    *f;
72
73 static void usage __P((void));
74 static void pflush __P((int));
75 static int plus __P((char, char));
76 static void move __P((int, int));
77
78 int
79 main(argc, argv)
80         int argc;
81         char *argv[];
82 {
83         int c;
84         char *cp, *dp;
85
86         argc--;
87         argv++;
88         while (argc > 0 && argv[0][0] == '-') {
89                 switch (argv[0][1]) {
90                         case 0:
91                                 suppresul = 1;
92                                 break;
93                         case '2':
94                                 printall = 1;
95                                 break;
96                         default:
97                                 usage();
98                 }
99                 argc--;
100                 argv++;
101         }
102         do {
103                 if (argc > 0) {
104                         close(0);
105                         if (!(f = fopen(argv[0], "r"))) {
106                                 fflush(stdout);
107                                 err(1, argv[0]);
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 static void
190 usage()
191 {
192         fprintf(stderr, "usage: colcrt [ - ] [ -2 ] [ file ... ]\n");
193         exit(1);
194 }
195
196 static int
197 plus(c, d)
198         char c, d;
199 {
200
201         return ((c == '|' && d == '-') || d == '_');
202 }
203
204 int first;
205
206 static void
207 pflush(ol)
208         int ol;
209 {
210         register int i;
211         register char *cp;
212         char lastomit;
213         int l;
214
215         l = ol;
216         lastomit = 0;
217         if (l > 266)
218                 l = 266;
219         else
220                 l |= 1;
221         for (i = first | 1; i < l; i++) {
222                 move(i, i - 1);
223                 move(i, i + 1);
224         }
225         for (i = first; i < l; i++) {
226                 cp = page[i];
227                 if (printall == 0 && lastomit == 0 && *cp == 0) {
228                         lastomit = 1;
229                         continue;
230                 }
231                 lastomit = 0;
232                 printf("%s\n", cp);
233         }
234         bcopy(page[ol], page, (267 - ol) * 132);
235         bzero(page[267- ol], ol * 132);
236         outline -= ol;
237         outcol = 0;
238         first = 1;
239 }
240
241 static void
242 move(l, m)
243         int l, m;
244 {
245         register char *cp, *dp;
246
247         for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
248                 switch (*cp) {
249                         case '|':
250                                 if (*dp != ' ' && *dp != '|' && *dp != 0)
251                                         return;
252                                 break;
253                         case ' ':
254                                 break;
255                         default:
256                                 return;
257                 }
258         }
259         if (*cp == 0) {
260                 for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
261                         if (*cp == '|')
262                                 *dp = '|';
263                         else if (*dp == 0)
264                                 *dp = ' ';
265                 page[l][0] = 0;
266         }
267 }