]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/colcrt.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / text-utils / 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 /*
35 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
36 * added Native Language Support
37 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
38 * modified to work correctly in multi-byte locales
39 */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h> /* for close() */
44 #include <string.h>
45 #include "nls.h"
46
47 #include "widechar.h"
48
49 int plus(wchar_t c, wchar_t d);
50 void move(int l, int m);
51 void pflush(int ol);
52
53 /*
54 * colcrt - replaces col for crts with new nroff esp. when using tbl.
55 * Bill Joy UCB July 14, 1977
56 *
57 * This filter uses a screen buffer, 267 half-lines by 132 columns.
58 * It interprets the up and down sequences generated by the new
59 * nroff when used with tbl and by \u \d and \r.
60 * General overstriking doesn't work correctly.
61 * Underlining is split onto multiple lines, etc.
62 *
63 * Option - suppresses all underlining.
64 * Option -2 forces printing of all half lines.
65 */
66
67 wchar_t page[267][132];
68
69 int outline = 1;
70 int outcol;
71
72 char suppresul;
73 char printall;
74
75 char *progname;
76 void colcrt(FILE *f);
77
78 int
79 main(int argc, char **argv) {
80 FILE *f;
81
82 setlocale(LC_ALL, "");
83 bindtextdomain(PACKAGE, LOCALEDIR);
84 textdomain(PACKAGE);
85
86 argc--;
87 progname = *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 printf(_("usage: %s [ - ] [ -2 ] [ file ... ]\n"), progname);
98 fflush(stdout);
99 exit(1);
100 }
101 argc--;
102 argv++;
103 }
104 f = stdin;
105 do {
106 if (argc > 0) {
107 if (!(f = fopen(argv[0], "r"))) {
108 fflush(stdout);
109 perror(argv[0]);
110 exit (1);
111 }
112 argc--;
113 argv++;
114 }
115 colcrt(f);
116 if (f != stdin)
117 fclose(f);
118 } while (argc > 0);
119 fflush(stdout);
120 if (ferror(stdout) || fclose(stdout))
121 return 1;
122 return 0;
123 }
124
125 void
126 colcrt(FILE *f) {
127 wint_t c;
128 wchar_t *cp, *dp;
129 int i, w;
130
131 for (;;) {
132 c = getwc(f);
133 if (c == WEOF) {
134 pflush(outline);
135 fflush(stdout);
136 break;
137 }
138 switch (c) {
139 case '\n':
140 if (outline >= 265)
141 pflush(62);
142 outline += 2;
143 outcol = 0;
144 continue;
145 case '\016':
146 case '\017':
147 continue;
148 case 033:
149 c = getwc(f);
150 switch (c) {
151 case '9':
152 if (outline >= 266)
153 pflush(62);
154 outline++;
155 continue;
156 case '8':
157 if (outline >= 1)
158 outline--;
159 continue;
160 case '7':
161 outline -= 2;
162 if (outline < 0)
163 outline = 0;
164 continue;
165 default:
166 continue;
167 }
168 case '\b':
169 if (outcol)
170 outcol--;
171 continue;
172 case '\t':
173 outcol += 8;
174 outcol &= ~7;
175 outcol--;
176 c = ' ';
177 default:
178 w = wcwidth(c);
179 if (outcol + w > 132) {
180 outcol++;
181 continue;
182 }
183 cp = &page[outline][outcol];
184 outcol += w;
185 if (c == '_') {
186 if (suppresul)
187 continue;
188 cp += 132;
189 c = '-';
190 }
191 if (*cp == 0) {
192 /* trick! */
193 for (i=0; i<w; i++)
194 cp[i] = c;
195 dp = cp - (outcol-w);
196 for (cp--; cp >= dp && *cp == 0; cp--)
197 *cp = ' ';
198 } else {
199 if (plus(c, *cp) || plus(*cp, c))
200 *cp = '+';
201 else if (*cp == ' ' || *cp == 0) {
202 for (i=1; i<w; i++)
203 if (cp[i] != ' ' && cp[i] != 0)
204 continue;
205 for (i=0; i<w; i++)
206 cp[i] = c;
207 }
208 }
209 continue;
210 }
211 }
212 }
213
214 int plus(wchar_t c, wchar_t d)
215 {
216
217 return (c == '|' && (d == '-' || d == '_'));
218 }
219
220 int first;
221
222 void pflush(int ol)
223 {
224 register int i;
225 register wchar_t *cp;
226 char lastomit;
227 int l, w;
228
229 l = ol;
230 lastomit = 0;
231 if (l > 266)
232 l = 266;
233 else
234 l |= 1;
235 for (i = first | 1; i < l; i++) {
236 move(i, i - 1);
237 move(i, i + 1);
238 }
239 for (i = first; i < l; i++) {
240 cp = page[i];
241 if (printall == 0 && lastomit == 0 && *cp == 0) {
242 lastomit = 1;
243 continue;
244 }
245 lastomit = 0;
246 while (*cp) {
247 if ((w = wcwidth(*cp)) > 0) {
248 putwchar(*cp);
249 cp += w;
250 } else
251 cp++;
252 }
253 putwchar('\n');
254 }
255 bcopy(page[ol], page, (267 - ol) * 132 * sizeof(wchar_t));
256 bzero(page[267- ol], ol * 132 * sizeof(wchar_t));
257 outline -= ol;
258 outcol = 0;
259 first = 1;
260 }
261
262 void move(int l, int m)
263 {
264 register wchar_t *cp, *dp;
265
266 for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
267 switch (*cp) {
268 case '|':
269 if (*dp != ' ' && *dp != '|' && *dp != 0)
270 return;
271 break;
272 case ' ':
273 break;
274 default:
275 return;
276 }
277 }
278 if (*cp == 0) {
279 for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
280 if (*cp == '|')
281 *dp = '|';
282 else if (*dp == 0)
283 *dp = ' ';
284 page[l][0] = 0;
285 }
286 }