freebsd-skq/usr.bin/colcrt/colcrt.c

288 lines
6.1 KiB
C
Raw Normal View History

1994-05-27 12:33:43 +00:00
/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static const char copyright[] =
1994-05-27 12:33:43 +00:00
"@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n";
2001-12-02 23:34:16 +00:00
#endif
1994-05-27 12:33:43 +00:00
#if 0
1994-05-27 12:33:43 +00:00
#ifndef lint
static char sccsid[] = "@(#)colcrt.c 8.1 (Berkeley) 6/6/93";
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
1994-05-27 12:33:43 +00:00
1997-12-29 00:09:06 +00:00
#include <err.h>
#include <locale.h>
1994-05-27 12:33:43 +00:00
#include <stdio.h>
#include <stdlib.h>
1997-12-29 00:09:06 +00:00
#include <string.h>
#include <unistd.h>
#include <wchar.h>
1994-05-27 12:33:43 +00:00
/*
* colcrt - replaces col for crts with new nroff esp. when using tbl.
* Bill Joy UCB July 14, 1977
*
* This filter uses a screen buffer, 267 half-lines by 132 columns.
* It interprets the up and down sequences generated by the new
* nroff when used with tbl and by \u \d and \r.
* General overstriking doesn't work correctly.
* Underlining is split onto multiple lines, etc.
*
* Option - suppresses all underlining.
* Option -2 forces printing of all half lines.
*/
wchar_t page[267][132];
1994-05-27 12:33:43 +00:00
int outline = 1;
int outcol;
char suppresul;
char printall;
2002-03-22 01:22:50 +00:00
static void move(int, int);
static void pflush(int);
static int plus(wchar_t, wchar_t);
2002-03-22 01:22:50 +00:00
static void usage(void);
1997-06-26 11:23:47 +00:00
int
main(int argc, char *argv[])
1994-05-27 12:33:43 +00:00
{
wint_t c;
wchar_t *cp, *dp;
int ch, i, w;
setlocale(LC_ALL, "");
1994-05-27 12:33:43 +00:00
while ((ch = getopt(argc, argv, "-2")) != -1)
switch (ch) {
case '-':
suppresul = 1;
break;
case '2':
printall = 1;
break;
default:
usage();
1994-05-27 12:33:43 +00:00
}
argc -= optind;
argv += optind;
1994-05-27 12:33:43 +00:00
do {
if (argc > 0) {
if (freopen(argv[0], "r", stdin) == NULL) {
1994-05-27 12:33:43 +00:00
fflush(stdout);
err(1, "%s", argv[0]);
1994-05-27 12:33:43 +00:00
}
argc--;
argv++;
}
for (;;) {
c = getwc(stdin);
if (c == WEOF) {
1994-05-27 12:33:43 +00:00
pflush(outline);
fflush(stdout);
break;
}
switch (c) {
case '\n':
if (outline >= 265)
pflush(62);
outline += 2;
outcol = 0;
continue;
case '\016':
case '\017':
continue;
case 033:
c = getwc(stdin);
switch (c) {
case '9':
if (outline >= 266)
1994-05-27 12:33:43 +00:00
pflush(62);
outline++;
1994-05-27 12:33:43 +00:00
continue;
case '8':
if (outline >= 1)
outline--;
1994-05-27 12:33:43 +00:00
continue;
case '7':
outline -= 2;
if (outline < 0)
outline = 0;
1994-05-27 12:33:43 +00:00
continue;
default:
continue;
}
case '\b':
if (outcol)
outcol--;
continue;
case '\t':
outcol += 8;
outcol &= ~7;
outcol--;
c = ' ';
default:
if ((w = wcwidth(c)) <= 0)
w = 1; /* XXX */
if (outcol + w > 132) {
outcol += w;
1994-05-27 12:33:43 +00:00
continue;
}
cp = &page[outline][outcol];
outcol += w;
if (c == '_') {
if (suppresul)
continue;
cp += 132;
c = '-';
}
if (*cp == 0) {
for (i = 0; i < w; i++)
cp[i] = c;
dp = cp - (outcol - w);
for (cp--; cp >= dp && *cp == 0; cp--)
*cp = ' ';
} else {
if (plus(c, *cp) || plus(*cp, c))
*cp = '+';
else if (*cp == ' ' || *cp == 0) {
for (i = 1; i < w; i++)
if (cp[i] != ' ' &&
cp[i] != 0)
goto cont;
for (i = 0; i < w; i++)
cp[i] = c;
}
}
cont:
continue;
1994-05-27 12:33:43 +00:00
}
}
2004-07-31 06:22:57 +00:00
if (ferror(stdin))
err(1, NULL);
1994-05-27 12:33:43 +00:00
} while (argc > 0);
fflush(stdout);
exit(0);
}
1997-06-26 11:23:47 +00:00
static void
usage(void)
1997-06-26 11:23:47 +00:00
{
fprintf(stderr, "usage: colcrt [-] [-2] [file ...]\n");
1997-06-26 11:23:47 +00:00
exit(1);
}
1997-12-29 00:09:06 +00:00
static int
plus(wchar_t c, wchar_t d)
1994-05-27 12:33:43 +00:00
{
1997-12-29 00:09:06 +00:00
return ((c == '|' && d == '-') || d == '_');
1994-05-27 12:33:43 +00:00
}
1997-12-29 00:09:06 +00:00
static void
pflush(int ol)
1994-05-27 12:33:43 +00:00
{
static int first;
int i;
wchar_t *cp;
1994-05-27 12:33:43 +00:00
char lastomit;
int l, w;
1994-05-27 12:33:43 +00:00
l = ol;
lastomit = 0;
if (l > 266)
l = 266;
else
l |= 1;
for (i = first | 1; i < l; i++) {
move(i, i - 1);
move(i, i + 1);
}
for (i = first; i < l; i++) {
cp = page[i];
if (printall == 0 && lastomit == 0 && *cp == 0) {
lastomit = 1;
continue;
}
lastomit = 0;
while (*cp != L'\0') {
if ((w = wcwidth(*cp)) > 0) {
putwchar(*cp);
cp += w;
} else
cp++;
}
putwchar(L'\n');
1994-05-27 12:33:43 +00:00
}
wmemcpy(page[0], page[ol], (267 - ol) * 132);
wmemset(page[267- ol], L'\0', ol * 132);
1994-05-27 12:33:43 +00:00
outline -= ol;
outcol = 0;
first = 1;
}
1997-12-29 00:09:06 +00:00
static void
move(int l, int m)
1994-05-27 12:33:43 +00:00
{
wchar_t *cp, *dp;
1994-05-27 12:33:43 +00:00
for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
switch (*cp) {
case '|':
if (*dp != ' ' && *dp != '|' && *dp != 0)
return;
break;
case ' ':
break;
default:
return;
}
}
if (*cp == 0) {
for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
if (*cp == '|')
*dp = '|';
else if (*dp == 0)
*dp = ' ';
page[l][0] = 0;
}
}