colcrt.c revision 87628
1/* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)colcrt.c 8.1 (Berkeley) 6/6/93"; 43#endif 44#endif 45 46#include <sys/cdefs.h> 47__FBSDID("$FreeBSD: head/usr.bin/colcrt/colcrt.c 87628 2001-12-10 21:13:08Z dwmalone $"); 48 49#include <err.h> 50#include <stdio.h> 51#include <stdlib.h> 52#include <string.h> 53#include <unistd.h> 54/* 55 * colcrt - replaces col for crts with new nroff esp. when using tbl. 56 * Bill Joy UCB July 14, 1977 57 * 58 * This filter uses a screen buffer, 267 half-lines by 132 columns. 59 * It interprets the up and down sequences generated by the new 60 * nroff when used with tbl and by \u \d and \r. 61 * General overstriking doesn't work correctly. 62 * Underlining is split onto multiple lines, etc. 63 * 64 * Option - suppresses all underlining. 65 * Option -2 forces printing of all half lines. 66 */ 67 68char page[267][132]; 69 70int outline = 1; 71int outcol; 72 73char suppresul; 74char printall; 75 76FILE *f; 77 78int main __P((int, char *[])); 79static void move __P((int, int)); 80static void pflush __P((int)); 81static int plus __P((char, char)); 82static void usage __P((void)); 83 84int 85main(argc, argv) 86 int argc; 87 char *argv[]; 88{ 89 int c; 90 char *cp, *dp; 91 int ch; 92 93 while ((ch = getopt(argc, argv, "-2")) != -1) 94 switch (ch) { 95 case '-': 96 suppresul = 1; 97 break; 98 case '2': 99 printall = 1; 100 break; 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 107 do { 108 if (argc > 0) { 109 close(0); 110 if (!(f = fopen(argv[0], "r"))) { 111 fflush(stdout); 112 err(1, "%s", argv[0]); 113 } 114 argc--; 115 argv++; 116 } 117 for (;;) { 118 c = getc(stdin); 119 if (c == -1) { 120 pflush(outline); 121 fflush(stdout); 122 break; 123 } 124 switch (c) { 125 case '\n': 126 if (outline >= 265) 127 pflush(62); 128 outline += 2; 129 outcol = 0; 130 continue; 131 case '\016': 132 case '\017': 133 continue; 134 case 033: 135 c = getc(stdin); 136 switch (c) { 137 case '9': 138 if (outline >= 266) 139 pflush(62); 140 outline++; 141 continue; 142 case '8': 143 if (outline >= 1) 144 outline--; 145 continue; 146 case '7': 147 outline -= 2; 148 if (outline < 0) 149 outline = 0; 150 continue; 151 default: 152 continue; 153 } 154 case '\b': 155 if (outcol) 156 outcol--; 157 continue; 158 case '\t': 159 outcol += 8; 160 outcol &= ~7; 161 outcol--; 162 c = ' '; 163 default: 164 if (outcol >= 132) { 165 outcol++; 166 continue; 167 } 168 cp = &page[outline][outcol]; 169 outcol++; 170 if (c == '_') { 171 if (suppresul) 172 continue; 173 cp += 132; 174 c = '-'; 175 } 176 if (*cp == 0) { 177 *cp = c; 178 dp = cp - outcol; 179 for (cp--; cp >= dp && *cp == 0; cp--) 180 *cp = ' '; 181 } else 182 if (plus(c, *cp) || plus(*cp, c)) 183 *cp = '+'; 184 else if (*cp == ' ' || *cp == 0) 185 *cp = c; 186 continue; 187 } 188 } 189 } while (argc > 0); 190 fflush(stdout); 191 exit(0); 192} 193 194static void 195usage() 196{ 197 fprintf(stderr, "usage: colcrt [-] [-2] [file ...]\n"); 198 exit(1); 199} 200 201static int 202plus(c, d) 203 char c, d; 204{ 205 206 return ((c == '|' && d == '-') || d == '_'); 207} 208 209int first; 210 211static void 212pflush(ol) 213 int ol; 214{ 215 register int i; 216 register char *cp; 217 char lastomit; 218 int l; 219 220 l = ol; 221 lastomit = 0; 222 if (l > 266) 223 l = 266; 224 else 225 l |= 1; 226 for (i = first | 1; i < l; i++) { 227 move(i, i - 1); 228 move(i, i + 1); 229 } 230 for (i = first; i < l; i++) { 231 cp = page[i]; 232 if (printall == 0 && lastomit == 0 && *cp == 0) { 233 lastomit = 1; 234 continue; 235 } 236 lastomit = 0; 237 printf("%s\n", cp); 238 } 239 bcopy(page[ol], page, (267 - ol) * 132); 240 bzero(page[267- ol], ol * 132); 241 outline -= ol; 242 outcol = 0; 243 first = 1; 244} 245 246static void 247move(l, m) 248 int l, m; 249{ 250 register char *cp, *dp; 251 252 for (cp = page[l], dp = page[m]; *cp; cp++, dp++) { 253 switch (*cp) { 254 case '|': 255 if (*dp != ' ' && *dp != '|' && *dp != 0) 256 return; 257 break; 258 case ' ': 259 break; 260 default: 261 return; 262 } 263 } 264 if (*cp == 0) { 265 for (cp = page[l], dp = page[m]; *cp; cp++, dp++) 266 if (*cp == '|') 267 *dp = '|'; 268 else if (*dp == 0) 269 *dp = ' '; 270 page[l][0] = 0; 271 } 272} 273