]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/colcrt/colcrt.c
Fixes for cygstart; bump version number and documentation
[cygwin-apps/cygutils.git] / src / 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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
32 * added Native Language Support
33 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
34 * modified to work correctly in multi-byte locales
35 * 2002-07-12 Charles Wilson <cwilson@ece.gatech.edu>
36 * modified to work on cygwin; integrated into cygutils package
37 */
38
39 #if HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 #include "common.h"
43
44 /* included by common.h *//*
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 */
50
51 #ifndef LOCALEDIR
52 #define LOCALEDIR "/usr/share/locale"
53 #endif
54
55 #ifdef HAVE_LOCALE_H
56 # include <locale.h>
57 #endif
58 #if !HAVE_SETLOCALE
59 # undef setlocale(a, b)
60 # define setlocale(a, b) /* empty */
61 #endif
62
63 #ifdef ENABLE_NLS
64 # include <libintl.h>
65 # define _(Text) gettext (Text)
66 # ifdef gettext_noop
67 # define N_(String) gettext_noop (String)
68 # else
69 # define N_(String) (String)
70 # endif
71 #else
72 # undef bindtextdomain
73 # define bindtextdomain(Domain, Directory) /* empty */
74 # undef textdomain
75 # define textdomain(Domain) /* empty */
76 # define _(Text) (Text)
77 # define N_(Text) (Text)
78 #endif
79
80 #include "widechar.h"
81
82 int plus(wchar_t c, wchar_t d);
83 void move(int l, int m);
84 void pflush(int ol);
85
86 /*
87 * colcrt - replaces col for crts with new nroff esp. when using tbl.
88 * Bill Joy UCB July 14, 1977
89 *
90 * This filter uses a screen buffer, 267 half-lines by 132 columns.
91 * It interprets the up and down sequences generated by the new
92 * nroff when used with tbl and by \u \d and \r.
93 * General overstriking doesn't work correctly.
94 * Underlining is split onto multiple lines, etc.
95 *
96 * Option - suppresses all underlining.
97 * Option -2 forces printing of all half lines.
98 */
99
100 wchar_t page[267][132];
101
102 int outline = 1;
103 int outcol;
104
105 char suppresul;
106 char printall;
107
108 char *progname;
109 void colcrt(FILE *f);
110
111 int
112 main(int argc, char **argv) {
113 FILE *f;
114
115 setlocale(LC_ALL, "");
116 bindtextdomain(PACKAGE, LOCALEDIR);
117 textdomain(PACKAGE);
118
119 argc--;
120 progname = *argv++;
121 while (argc > 0 && argv[0][0] == '-') {
122 switch (argv[0][1]) {
123 case 0:
124 suppresul = 1;
125 break;
126 case '2':
127 printall = 1;
128 break;
129 default:
130 printf(_("usage: %s [ - ] [ -2 ] [ file ... ]\n"), progname);
131 fflush(stdout);
132 exit(1);
133 }
134 argc--;
135 argv++;
136 }
137 f = stdin;
138 do {
139 if (argc > 0) {
140 if (!(f = fopen(argv[0], "r"))) {
141 fflush(stdout);
142 perror(argv[0]);
143 exit (1);
144 }
145 argc--;
146 argv++;
147 }
148 colcrt(f);
149 if (f != stdin)
150 fclose(f);
151 } while (argc > 0);
152 fflush(stdout);
153 if (ferror(stdout) || fclose(stdout))
154 return 1;
155 return 0;
156 }
157
158 void
159 colcrt(FILE *f) {
160 wint_t c;
161 wchar_t *cp, *dp;
162 int i, w;
163
164 for (;;) {
165 c = getwc(f);
166 if (c == WEOF) {
167 pflush(outline);
168 fflush(stdout);
169 break;
170 }
171 switch (c) {
172 case '\n':
173 if (outline >= 265)
174 pflush(62);
175 outline += 2;
176 outcol = 0;
177 continue;
178 case '\016':
179 case '\017':
180 continue;
181 case 033:
182 c = getwc(f);
183 switch (c) {
184 case '9':
185 if (outline >= 266)
186 pflush(62);
187 outline++;
188 continue;
189 case '8':
190 if (outline >= 1)
191 outline--;
192 continue;
193 case '7':
194 outline -= 2;
195 if (outline < 0)
196 outline = 0;
197 continue;
198 default:
199 continue;
200 }
201 case '\b':
202 if (outcol)
203 outcol--;
204 continue;
205 case '\t':
206 outcol += 8;
207 outcol &= ~7;
208 outcol--;
209 c = ' ';
210 default:
211 w = wcwidth(c);
212 if (outcol + w > 132) {
213 outcol++;
214 continue;
215 }
216 cp = &page[outline][outcol];
217 outcol += w;
218 if (c == '_') {
219 if (suppresul)
220 continue;
221 cp += 132;
222 c = '-';
223 }
224 if (*cp == 0) {
225 /* trick! */
226 for (i=0; i<w; i++)
227 cp[i] = c;
228 dp = cp - (outcol-w);
229 for (cp--; cp >= dp && *cp == 0; cp--)
230 *cp = ' ';
231 } else {
232 if (plus(c, *cp) || plus(*cp, c))
233 *cp = '+';
234 else if (*cp == ' ' || *cp == 0) {
235 for (i=1; i<w; i++)
236 if (cp[i] != ' ' && cp[i] != 0)
237 continue;
238 for (i=0; i<w; i++)
239 cp[i] = c;
240 }
241 }
242 continue;
243 }
244 }
245 }
246
247 int plus(wchar_t c, wchar_t d)
248 {
249
250 return (c == '|' && (d == '-' || d == '_'));
251 }
252
253 int first;
254
255 void pflush(int ol)
256 {
257 register int i;
258 register wchar_t *cp;
259 char lastomit;
260 int l, w;
261
262 l = ol;
263 lastomit = 0;
264 if (l > 266)
265 l = 266;
266 else
267 l |= 1;
268 for (i = first | 1; i < l; i++) {
269 move(i, i - 1);
270 move(i, i + 1);
271 }
272 for (i = first; i < l; i++) {
273 cp = page[i];
274 if (printall == 0 && lastomit == 0 && *cp == 0) {
275 lastomit = 1;
276 continue;
277 }
278 lastomit = 0;
279 while (*cp) {
280 if ((w = wcwidth(*cp)) > 0) {
281 putwchar(*cp);
282 cp += w;
283 } else
284 cp++;
285 }
286 putwchar('\n');
287 }
288 bcopy(page[ol], page, (267 - ol) * 132 * sizeof(wchar_t));
289 bzero(page[267- ol], ol * 132 * sizeof(wchar_t));
290 outline -= ol;
291 outcol = 0;
292 first = 1;
293 }
294
295 void move(int l, int m)
296 {
297 register wchar_t *cp, *dp;
298
299 for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
300 switch (*cp) {
301 case '|':
302 if (*dp != ' ' && *dp != '|' && *dp != 0)
303 return;
304 break;
305 case ' ':
306 break;
307 default:
308 return;
309 }
310 }
311 if (*cp == 0) {
312 for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
313 if (*cp == '|')
314 *dp = '|';
315 else if (*dp == 0)
316 *dp = ' ';
317 page[l][0] = 0;
318 }
319 }
This page took 0.051869 seconds and 5 git commands to generate.