xref: /csrg-svn/usr.bin/colcrt/colcrt.c (revision 36172)
121550Sdist /*
2*36172Sbostic  * Copyright (c) 1980 The Regents of the University of California.
3*36172Sbostic  * All rights reserved.
4*36172Sbostic  *
5*36172Sbostic  * Redistribution and use in source and binary forms are permitted
6*36172Sbostic  * provided that the above copyright notice and this paragraph are
7*36172Sbostic  * duplicated in all such forms and that any documentation,
8*36172Sbostic  * advertising materials, and other materials related to such
9*36172Sbostic  * distribution and use acknowledge that the software was developed
10*36172Sbostic  * by the University of California, Berkeley.  The name of the
11*36172Sbostic  * University may not be used to endorse or promote products derived
12*36172Sbostic  * from this software without specific prior written permission.
13*36172Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*36172Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*36172Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621550Sdist  */
1717014Ssam 
1821550Sdist #ifndef lint
1921550Sdist char copyright[] =
20*36172Sbostic "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
2121550Sdist  All rights reserved.\n";
22*36172Sbostic #endif /* not lint */
2321550Sdist 
2421550Sdist #ifndef lint
25*36172Sbostic static char sccsid[] = "@(#)colcrt.c	5.3 (Berkeley) 10/26/88";
26*36172Sbostic #endif /* not lint */
2721550Sdist 
28984Sbill #include <stdio.h>
29984Sbill /*
30984Sbill  * colcrt - replaces col for crts with new nroff esp. when using tbl.
31984Sbill  * Bill Joy UCB July 14, 1977
32984Sbill  *
33984Sbill  * This filter uses a screen buffer, 267 half-lines by 132 columns.
34984Sbill  * It interprets the up and down sequences generated by the new
35984Sbill  * nroff when used with tbl and by \u \d and \r.
36984Sbill  * General overstriking doesn't work correctly.
37984Sbill  * Underlining is split onto multiple lines, etc.
38984Sbill  *
39984Sbill  * Option - suppresses all underlining.
40984Sbill  * Option -2 forces printing of all half lines.
41984Sbill  */
42984Sbill 
43984Sbill char	page[267][132];
44984Sbill 
45984Sbill int	outline = 1;
46984Sbill int	outcol;
47984Sbill 
48984Sbill char	suppresul;
49984Sbill char	printall;
50984Sbill 
51984Sbill char	*progname;
52984Sbill FILE	*f;
53984Sbill 
54984Sbill main(argc, argv)
55984Sbill 	int argc;
56984Sbill 	char *argv[];
57984Sbill {
58984Sbill 	register c;
59984Sbill 	register char *cp, *dp;
60984Sbill 
61984Sbill 	argc--;
62984Sbill 	progname = *argv++;
63984Sbill 	while (argc > 0 && argv[0][0] == '-') {
64984Sbill 		switch (argv[0][1]) {
65984Sbill 			case 0:
66984Sbill 				suppresul = 1;
67984Sbill 				break;
68984Sbill 			case '2':
69984Sbill 				printall = 1;
70984Sbill 				break;
71984Sbill 			default:
72984Sbill 				printf("usage: %s [ - ] [ -2 ] [ file ... ]\n", progname);
73984Sbill 				fflush(stdout);
74984Sbill 				exit(1);
75984Sbill 		}
76984Sbill 		argc--;
77984Sbill 		argv++;
78984Sbill 	}
79984Sbill 	do {
80984Sbill 		if (argc > 0) {
81984Sbill 			close(0);
8235264Sbostic 			if (!(f = fopen(argv[0], "r"))) {
83984Sbill 				fflush(stdout);
84984Sbill 				perror(argv[0]);
85984Sbill 				exit (1);
86984Sbill 			}
87984Sbill 			argc--;
88984Sbill 			argv++;
89984Sbill 		}
90984Sbill 		for (;;) {
91984Sbill 			c = getc(stdin);
92984Sbill 			if (c == -1) {
93984Sbill 				pflush(outline);
94984Sbill 				fflush(stdout);
95984Sbill 				break;
96984Sbill 			}
97984Sbill 			switch (c) {
98984Sbill 				case '\n':
99984Sbill 					if (outline >= 265)
100984Sbill 						pflush(62);
101984Sbill 					outline += 2;
102984Sbill 					outcol = 0;
103984Sbill 					continue;
104984Sbill 				case '\016':
105984Sbill 					case '\017':
106984Sbill 					continue;
107984Sbill 				case 033:
108984Sbill 					c = getc(stdin);
109984Sbill 					switch (c) {
110984Sbill 						case '9':
111984Sbill 							if (outline >= 266)
112984Sbill 								pflush(62);
113984Sbill 							outline++;
114984Sbill 							continue;
115984Sbill 						case '8':
116984Sbill 							if (outline >= 1)
117984Sbill 								outline--;
118984Sbill 							continue;
119984Sbill 						case '7':
120984Sbill 							outline -= 2;
121984Sbill 							if (outline < 0)
122984Sbill 								outline = 0;
123984Sbill 							continue;
124984Sbill 						default:
125984Sbill 							continue;
126984Sbill 					}
127984Sbill 				case '\b':
128984Sbill 					if (outcol)
129984Sbill 						outcol--;
130984Sbill 					continue;
131984Sbill 				case '\t':
132984Sbill 					outcol += 8;
133984Sbill 					outcol &= ~7;
134984Sbill 					outcol--;
135984Sbill 					c = ' ';
136984Sbill 				default:
137984Sbill 					if (outcol >= 132) {
138984Sbill 						outcol++;
139984Sbill 						continue;
140984Sbill 					}
141984Sbill 					cp = &page[outline][outcol];
142984Sbill 					outcol++;
143984Sbill 					if (c == '_') {
144984Sbill 						if (suppresul)
145984Sbill 							continue;
146984Sbill 						cp += 132;
147984Sbill 						c = '-';
148984Sbill 					}
149984Sbill 					if (*cp == 0) {
150984Sbill 						*cp = c;
151984Sbill 						dp = cp - outcol;
152984Sbill 						for (cp--; cp >= dp && *cp == 0; cp--)
153984Sbill 							*cp = ' ';
154984Sbill 					} else
155984Sbill 						if (plus(c, *cp) || plus(*cp, c))
156984Sbill 							*cp = '+';
157984Sbill 						else if (*cp == ' ' || *cp == 0)
158984Sbill 							*cp = c;
159984Sbill 					continue;
160984Sbill 			}
161984Sbill 		}
162984Sbill 	} while (argc > 0);
163984Sbill 	fflush(stdout);
164984Sbill 	exit(0);
165984Sbill }
166984Sbill 
167984Sbill plus(c, d)
168984Sbill 	char c, d;
169984Sbill {
170984Sbill 
171984Sbill 	return (c == '|' && d == '-' || d == '_');
172984Sbill }
173984Sbill 
174984Sbill int first;
175984Sbill 
176984Sbill pflush(ol)
177984Sbill 	int ol;
178984Sbill {
179984Sbill 	register int i, j;
180984Sbill 	register char *cp;
181984Sbill 	char lastomit;
182984Sbill 	int l;
183984Sbill 
184984Sbill 	l = ol;
185984Sbill 	lastomit = 0;
186984Sbill 	if (l > 266)
187984Sbill 		l = 266;
188984Sbill 	else
189984Sbill 		l |= 1;
190984Sbill 	for (i = first | 1; i < l; i++) {
191984Sbill 		move(i, i - 1);
192984Sbill 		move(i, i + 1);
193984Sbill 	}
194984Sbill 	for (i = first; i < l; i++) {
195984Sbill 		cp = page[i];
196984Sbill 		if (printall == 0 && lastomit == 0 && *cp == 0) {
197984Sbill 			lastomit = 1;
198984Sbill 			continue;
199984Sbill 		}
200984Sbill 		lastomit = 0;
201984Sbill 		printf("%s\n", cp);
202984Sbill 	}
20317014Ssam 	bcopy(page[ol], page, (267 - ol) * 132);
20417014Ssam 	bzero(page[267- ol], ol * 132);
205984Sbill 	outline -= ol;
206984Sbill 	outcol = 0;
207984Sbill 	first = 1;
208984Sbill }
20917014Ssam 
210984Sbill move(l, m)
211984Sbill 	int l, m;
212984Sbill {
213984Sbill 	register char *cp, *dp;
214984Sbill 
215984Sbill 	for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
216984Sbill 		switch (*cp) {
217984Sbill 			case '|':
218984Sbill 				if (*dp != ' ' && *dp != '|' && *dp != 0)
219984Sbill 					return;
220984Sbill 				break;
221984Sbill 			case ' ':
222984Sbill 				break;
223984Sbill 			default:
224984Sbill 				return;
225984Sbill 		}
226984Sbill 	}
227984Sbill 	if (*cp == 0) {
228984Sbill 		for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
229984Sbill 			if (*cp == '|')
230984Sbill 				*dp = '|';
231984Sbill 			else if (*dp == 0)
232984Sbill 				*dp = ' ';
233984Sbill 		page[l][0] = 0;
234984Sbill 	}
235984Sbill }
236