xref: /csrg-svn/usr.bin/colcrt/colcrt.c (revision 61946)
121550Sdist /*
2*61946Sbostic  * Copyright (c) 1980, 1993
3*61946Sbostic  *	The Regents of the University of California.  All rights reserved.
436172Sbostic  *
542723Sbostic  * %sccs.include.redist.c%
621550Sdist  */
717014Ssam 
821550Sdist #ifndef lint
9*61946Sbostic static char copyright[] =
10*61946Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*61946Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1236172Sbostic #endif /* not lint */
1321550Sdist 
1421550Sdist #ifndef lint
15*61946Sbostic static char sccsid[] = "@(#)colcrt.c	8.1 (Berkeley) 06/06/93";
1636172Sbostic #endif /* not lint */
1721550Sdist 
18984Sbill #include <stdio.h>
19984Sbill /*
20984Sbill  * colcrt - replaces col for crts with new nroff esp. when using tbl.
21984Sbill  * Bill Joy UCB July 14, 1977
22984Sbill  *
23984Sbill  * This filter uses a screen buffer, 267 half-lines by 132 columns.
24984Sbill  * It interprets the up and down sequences generated by the new
25984Sbill  * nroff when used with tbl and by \u \d and \r.
26984Sbill  * General overstriking doesn't work correctly.
27984Sbill  * Underlining is split onto multiple lines, etc.
28984Sbill  *
29984Sbill  * Option - suppresses all underlining.
30984Sbill  * Option -2 forces printing of all half lines.
31984Sbill  */
32984Sbill 
33984Sbill char	page[267][132];
34984Sbill 
35984Sbill int	outline = 1;
36984Sbill int	outcol;
37984Sbill 
38984Sbill char	suppresul;
39984Sbill char	printall;
40984Sbill 
41984Sbill char	*progname;
42984Sbill FILE	*f;
43984Sbill 
main(argc,argv)44984Sbill main(argc, argv)
45984Sbill 	int argc;
46984Sbill 	char *argv[];
47984Sbill {
48984Sbill 	register c;
49984Sbill 	register char *cp, *dp;
50984Sbill 
51984Sbill 	argc--;
52984Sbill 	progname = *argv++;
53984Sbill 	while (argc > 0 && argv[0][0] == '-') {
54984Sbill 		switch (argv[0][1]) {
55984Sbill 			case 0:
56984Sbill 				suppresul = 1;
57984Sbill 				break;
58984Sbill 			case '2':
59984Sbill 				printall = 1;
60984Sbill 				break;
61984Sbill 			default:
62984Sbill 				printf("usage: %s [ - ] [ -2 ] [ file ... ]\n", progname);
63984Sbill 				fflush(stdout);
64984Sbill 				exit(1);
65984Sbill 		}
66984Sbill 		argc--;
67984Sbill 		argv++;
68984Sbill 	}
69984Sbill 	do {
70984Sbill 		if (argc > 0) {
71984Sbill 			close(0);
7235264Sbostic 			if (!(f = fopen(argv[0], "r"))) {
73984Sbill 				fflush(stdout);
74984Sbill 				perror(argv[0]);
75984Sbill 				exit (1);
76984Sbill 			}
77984Sbill 			argc--;
78984Sbill 			argv++;
79984Sbill 		}
80984Sbill 		for (;;) {
81984Sbill 			c = getc(stdin);
82984Sbill 			if (c == -1) {
83984Sbill 				pflush(outline);
84984Sbill 				fflush(stdout);
85984Sbill 				break;
86984Sbill 			}
87984Sbill 			switch (c) {
88984Sbill 				case '\n':
89984Sbill 					if (outline >= 265)
90984Sbill 						pflush(62);
91984Sbill 					outline += 2;
92984Sbill 					outcol = 0;
93984Sbill 					continue;
94984Sbill 				case '\016':
95984Sbill 					case '\017':
96984Sbill 					continue;
97984Sbill 				case 033:
98984Sbill 					c = getc(stdin);
99984Sbill 					switch (c) {
100984Sbill 						case '9':
101984Sbill 							if (outline >= 266)
102984Sbill 								pflush(62);
103984Sbill 							outline++;
104984Sbill 							continue;
105984Sbill 						case '8':
106984Sbill 							if (outline >= 1)
107984Sbill 								outline--;
108984Sbill 							continue;
109984Sbill 						case '7':
110984Sbill 							outline -= 2;
111984Sbill 							if (outline < 0)
112984Sbill 								outline = 0;
113984Sbill 							continue;
114984Sbill 						default:
115984Sbill 							continue;
116984Sbill 					}
117984Sbill 				case '\b':
118984Sbill 					if (outcol)
119984Sbill 						outcol--;
120984Sbill 					continue;
121984Sbill 				case '\t':
122984Sbill 					outcol += 8;
123984Sbill 					outcol &= ~7;
124984Sbill 					outcol--;
125984Sbill 					c = ' ';
126984Sbill 				default:
127984Sbill 					if (outcol >= 132) {
128984Sbill 						outcol++;
129984Sbill 						continue;
130984Sbill 					}
131984Sbill 					cp = &page[outline][outcol];
132984Sbill 					outcol++;
133984Sbill 					if (c == '_') {
134984Sbill 						if (suppresul)
135984Sbill 							continue;
136984Sbill 						cp += 132;
137984Sbill 						c = '-';
138984Sbill 					}
139984Sbill 					if (*cp == 0) {
140984Sbill 						*cp = c;
141984Sbill 						dp = cp - outcol;
142984Sbill 						for (cp--; cp >= dp && *cp == 0; cp--)
143984Sbill 							*cp = ' ';
144984Sbill 					} else
145984Sbill 						if (plus(c, *cp) || plus(*cp, c))
146984Sbill 							*cp = '+';
147984Sbill 						else if (*cp == ' ' || *cp == 0)
148984Sbill 							*cp = c;
149984Sbill 					continue;
150984Sbill 			}
151984Sbill 		}
152984Sbill 	} while (argc > 0);
153984Sbill 	fflush(stdout);
154984Sbill 	exit(0);
155984Sbill }
156984Sbill 
plus(c,d)157984Sbill plus(c, d)
158984Sbill 	char c, d;
159984Sbill {
160984Sbill 
161984Sbill 	return (c == '|' && d == '-' || d == '_');
162984Sbill }
163984Sbill 
164984Sbill int first;
165984Sbill 
pflush(ol)166984Sbill pflush(ol)
167984Sbill 	int ol;
168984Sbill {
169984Sbill 	register int i, j;
170984Sbill 	register char *cp;
171984Sbill 	char lastomit;
172984Sbill 	int l;
173984Sbill 
174984Sbill 	l = ol;
175984Sbill 	lastomit = 0;
176984Sbill 	if (l > 266)
177984Sbill 		l = 266;
178984Sbill 	else
179984Sbill 		l |= 1;
180984Sbill 	for (i = first | 1; i < l; i++) {
181984Sbill 		move(i, i - 1);
182984Sbill 		move(i, i + 1);
183984Sbill 	}
184984Sbill 	for (i = first; i < l; i++) {
185984Sbill 		cp = page[i];
186984Sbill 		if (printall == 0 && lastomit == 0 && *cp == 0) {
187984Sbill 			lastomit = 1;
188984Sbill 			continue;
189984Sbill 		}
190984Sbill 		lastomit = 0;
191984Sbill 		printf("%s\n", cp);
192984Sbill 	}
19317014Ssam 	bcopy(page[ol], page, (267 - ol) * 132);
19417014Ssam 	bzero(page[267- ol], ol * 132);
195984Sbill 	outline -= ol;
196984Sbill 	outcol = 0;
197984Sbill 	first = 1;
198984Sbill }
19917014Ssam 
move(l,m)200984Sbill move(l, m)
201984Sbill 	int l, m;
202984Sbill {
203984Sbill 	register char *cp, *dp;
204984Sbill 
205984Sbill 	for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
206984Sbill 		switch (*cp) {
207984Sbill 			case '|':
208984Sbill 				if (*dp != ' ' && *dp != '|' && *dp != 0)
209984Sbill 					return;
210984Sbill 				break;
211984Sbill 			case ' ':
212984Sbill 				break;
213984Sbill 			default:
214984Sbill 				return;
215984Sbill 		}
216984Sbill 	}
217984Sbill 	if (*cp == 0) {
218984Sbill 		for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
219984Sbill 			if (*cp == '|')
220984Sbill 				*dp = '|';
221984Sbill 			else if (*dp == 0)
222984Sbill 				*dp = ' ';
223984Sbill 		page[l][0] = 0;
224984Sbill 	}
225984Sbill }
226