xref: /original-bsd/usr.bin/colcrt/colcrt.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)colcrt.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 /*
20  * colcrt - replaces col for crts with new nroff esp. when using tbl.
21  * Bill Joy UCB July 14, 1977
22  *
23  * This filter uses a screen buffer, 267 half-lines by 132 columns.
24  * It interprets the up and down sequences generated by the new
25  * nroff when used with tbl and by \u \d and \r.
26  * General overstriking doesn't work correctly.
27  * Underlining is split onto multiple lines, etc.
28  *
29  * Option - suppresses all underlining.
30  * Option -2 forces printing of all half lines.
31  */
32 
33 char	page[267][132];
34 
35 int	outline = 1;
36 int	outcol;
37 
38 char	suppresul;
39 char	printall;
40 
41 char	*progname;
42 FILE	*f;
43 
44 main(argc, argv)
45 	int argc;
46 	char *argv[];
47 {
48 	register c;
49 	register char *cp, *dp;
50 
51 	argc--;
52 	progname = *argv++;
53 	while (argc > 0 && argv[0][0] == '-') {
54 		switch (argv[0][1]) {
55 			case 0:
56 				suppresul = 1;
57 				break;
58 			case '2':
59 				printall = 1;
60 				break;
61 			default:
62 				printf("usage: %s [ - ] [ -2 ] [ file ... ]\n", progname);
63 				fflush(stdout);
64 				exit(1);
65 		}
66 		argc--;
67 		argv++;
68 	}
69 	do {
70 		if (argc > 0) {
71 			close(0);
72 			if (!(f = fopen(argv[0], "r"))) {
73 				fflush(stdout);
74 				perror(argv[0]);
75 				exit (1);
76 			}
77 			argc--;
78 			argv++;
79 		}
80 		for (;;) {
81 			c = getc(stdin);
82 			if (c == -1) {
83 				pflush(outline);
84 				fflush(stdout);
85 				break;
86 			}
87 			switch (c) {
88 				case '\n':
89 					if (outline >= 265)
90 						pflush(62);
91 					outline += 2;
92 					outcol = 0;
93 					continue;
94 				case '\016':
95 					case '\017':
96 					continue;
97 				case 033:
98 					c = getc(stdin);
99 					switch (c) {
100 						case '9':
101 							if (outline >= 266)
102 								pflush(62);
103 							outline++;
104 							continue;
105 						case '8':
106 							if (outline >= 1)
107 								outline--;
108 							continue;
109 						case '7':
110 							outline -= 2;
111 							if (outline < 0)
112 								outline = 0;
113 							continue;
114 						default:
115 							continue;
116 					}
117 				case '\b':
118 					if (outcol)
119 						outcol--;
120 					continue;
121 				case '\t':
122 					outcol += 8;
123 					outcol &= ~7;
124 					outcol--;
125 					c = ' ';
126 				default:
127 					if (outcol >= 132) {
128 						outcol++;
129 						continue;
130 					}
131 					cp = &page[outline][outcol];
132 					outcol++;
133 					if (c == '_') {
134 						if (suppresul)
135 							continue;
136 						cp += 132;
137 						c = '-';
138 					}
139 					if (*cp == 0) {
140 						*cp = c;
141 						dp = cp - outcol;
142 						for (cp--; cp >= dp && *cp == 0; cp--)
143 							*cp = ' ';
144 					} else
145 						if (plus(c, *cp) || plus(*cp, c))
146 							*cp = '+';
147 						else if (*cp == ' ' || *cp == 0)
148 							*cp = c;
149 					continue;
150 			}
151 		}
152 	} while (argc > 0);
153 	fflush(stdout);
154 	exit(0);
155 }
156 
157 plus(c, d)
158 	char c, d;
159 {
160 
161 	return (c == '|' && d == '-' || d == '_');
162 }
163 
164 int first;
165 
166 pflush(ol)
167 	int ol;
168 {
169 	register int i, j;
170 	register char *cp;
171 	char lastomit;
172 	int l;
173 
174 	l = ol;
175 	lastomit = 0;
176 	if (l > 266)
177 		l = 266;
178 	else
179 		l |= 1;
180 	for (i = first | 1; i < l; i++) {
181 		move(i, i - 1);
182 		move(i, i + 1);
183 	}
184 	for (i = first; i < l; i++) {
185 		cp = page[i];
186 		if (printall == 0 && lastomit == 0 && *cp == 0) {
187 			lastomit = 1;
188 			continue;
189 		}
190 		lastomit = 0;
191 		printf("%s\n", cp);
192 	}
193 	bcopy(page[ol], page, (267 - ol) * 132);
194 	bzero(page[267- ol], ol * 132);
195 	outline -= ol;
196 	outcol = 0;
197 	first = 1;
198 }
199 
200 move(l, m)
201 	int l, m;
202 {
203 	register char *cp, *dp;
204 
205 	for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
206 		switch (*cp) {
207 			case '|':
208 				if (*dp != ' ' && *dp != '|' && *dp != 0)
209 					return;
210 				break;
211 			case ' ':
212 				break;
213 			default:
214 				return;
215 		}
216 	}
217 	if (*cp == 0) {
218 		for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
219 			if (*cp == '|')
220 				*dp = '|';
221 			else if (*dp == 0)
222 				*dp = ' ';
223 		page[l][0] = 0;
224 	}
225 }
226