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