xref: /csrg-svn/usr.bin/colcrt/colcrt.c (revision 21550)
1*21550Sdist /*
2*21550Sdist  * Copyright (c) 1980 Regents of the University of California.
3*21550Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21550Sdist  * specifies the terms and conditions for redistribution.
5*21550Sdist  */
617014Ssam 
7*21550Sdist #ifndef lint
8*21550Sdist char copyright[] =
9*21550Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21550Sdist  All rights reserved.\n";
11*21550Sdist #endif not lint
12*21550Sdist 
13*21550Sdist #ifndef lint
14*21550Sdist static char sccsid[] = "@(#)colcrt.c	5.1 (Berkeley) 05/31/85";
15*21550Sdist #endif not lint
16*21550Sdist 
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);
71984Sbill 			if ((f=fopen(argv[0], "r")
72984Sbill ) < 0) {
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 
157984Sbill plus(c, d)
158984Sbill 	char c, d;
159984Sbill {
160984Sbill 
161984Sbill 	return (c == '|' && d == '-' || d == '_');
162984Sbill }
163984Sbill 
164984Sbill int first;
165984Sbill 
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 
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