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