]> git.ipfire.org Git - thirdparty/util-linux.git/blame - text-utils/colcrt.c
Imported from util-linux-2.8 tarball.
[thirdparty/util-linux.git] / text-utils / colcrt.c
CommitLineData
6dbe3af9
KZ
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
6dbe3af9 34#include <stdio.h>
fd6b7a7f
KZ
35#include <unistd.h> /* for close() */
36#include <string.h>
37
38int plus(char c, char d);
39void move(int l, int m);
40void pflush(int ol);
41
6dbe3af9
KZ
42/*
43 * colcrt - replaces col for crts with new nroff esp. when using tbl.
44 * Bill Joy UCB July 14, 1977
45 *
46 * This filter uses a screen buffer, 267 half-lines by 132 columns.
47 * It interprets the up and down sequences generated by the new
48 * nroff when used with tbl and by \u \d and \r.
49 * General overstriking doesn't work correctly.
50 * Underlining is split onto multiple lines, etc.
51 *
52 * Option - suppresses all underlining.
53 * Option -2 forces printing of all half lines.
54 */
55
56char page[267][132];
57
58int outline = 1;
59int outcol;
60
61char suppresul;
62char printall;
63
64char *progname;
65FILE *f;
66
fd6b7a7f 67int main(int argc, char **argv)
6dbe3af9
KZ
68{
69 register c;
70 register char *cp, *dp;
71
72 argc--;
73 progname = *argv++;
74 while (argc > 0 && argv[0][0] == '-') {
75 switch (argv[0][1]) {
76 case 0:
77 suppresul = 1;
78 break;
79 case '2':
80 printall = 1;
81 break;
82 default:
83 printf("usage: %s [ - ] [ -2 ] [ file ... ]\n", progname);
84 fflush(stdout);
85 exit(1);
86 }
87 argc--;
88 argv++;
89 }
90 do {
91 if (argc > 0) {
92 close(0);
93 if (!(f = fopen(argv[0], "r"))) {
94 fflush(stdout);
95 perror(argv[0]);
96 exit (1);
97 }
98 argc--;
99 argv++;
100 }
101 for (;;) {
102 c = getc(stdin);
103 if (c == -1) {
104 pflush(outline);
105 fflush(stdout);
106 break;
107 }
108 switch (c) {
109 case '\n':
110 if (outline >= 265)
111 pflush(62);
112 outline += 2;
113 outcol = 0;
114 continue;
115 case '\016':
116 case '\017':
117 continue;
118 case 033:
119 c = getc(stdin);
120 switch (c) {
121 case '9':
122 if (outline >= 266)
123 pflush(62);
124 outline++;
125 continue;
126 case '8':
127 if (outline >= 1)
128 outline--;
129 continue;
130 case '7':
131 outline -= 2;
132 if (outline < 0)
133 outline = 0;
134 continue;
135 default:
136 continue;
137 }
138 case '\b':
139 if (outcol)
140 outcol--;
141 continue;
142 case '\t':
143 outcol += 8;
144 outcol &= ~7;
145 outcol--;
146 c = ' ';
147 default:
148 if (outcol >= 132) {
149 outcol++;
150 continue;
151 }
152 cp = &page[outline][outcol];
153 outcol++;
154 if (c == '_') {
155 if (suppresul)
156 continue;
157 cp += 132;
158 c = '-';
159 }
160 if (*cp == 0) {
161 *cp = c;
162 dp = cp - outcol;
163 for (cp--; cp >= dp && *cp == 0; cp--)
164 *cp = ' ';
165 } else
166 if (plus(c, *cp) || plus(*cp, c))
167 *cp = '+';
168 else if (*cp == ' ' || *cp == 0)
169 *cp = c;
170 continue;
171 }
172 }
173 } while (argc > 0);
174 fflush(stdout);
fd6b7a7f 175 return 0;
6dbe3af9
KZ
176}
177
fd6b7a7f 178int plus(char c, char d)
6dbe3af9
KZ
179{
180
fd6b7a7f 181 return (c == '|' && (d == '-' || d == '_'));
6dbe3af9
KZ
182}
183
184int first;
185
fd6b7a7f 186void pflush(int ol)
6dbe3af9 187{
fd6b7a7f 188 register int i;
6dbe3af9
KZ
189 register char *cp;
190 char lastomit;
191 int l;
192
193 l = ol;
194 lastomit = 0;
195 if (l > 266)
196 l = 266;
197 else
198 l |= 1;
199 for (i = first | 1; i < l; i++) {
200 move(i, i - 1);
201 move(i, i + 1);
202 }
203 for (i = first; i < l; i++) {
204 cp = page[i];
205 if (printall == 0 && lastomit == 0 && *cp == 0) {
206 lastomit = 1;
207 continue;
208 }
209 lastomit = 0;
210 printf("%s\n", cp);
211 }
212 bcopy(page[ol], page, (267 - ol) * 132);
213 bzero(page[267- ol], ol * 132);
214 outline -= ol;
215 outcol = 0;
216 first = 1;
217}
218
fd6b7a7f 219void move(int l, int m)
6dbe3af9
KZ
220{
221 register char *cp, *dp;
222
223 for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
224 switch (*cp) {
225 case '|':
226 if (*dp != ' ' && *dp != '|' && *dp != 0)
227 return;
228 break;
229 case ' ':
230 break;
231 default:
232 return;
233 }
234 }
235 if (*cp == 0) {
236 for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
237 if (*cp == '|')
238 *dp = '|';
239 else if (*dp == 0)
240 *dp = ' ';
241 page[l][0] = 0;
242 }
243}