colcrt.c revision 62889
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 /* not lint */ 39 40#ifndef lint 41#if 0 42static const char sccsid[] = "@(#)colcrt.c 8.1 (Berkeley) 6/6/93"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/usr.bin/colcrt/colcrt.c 62889 2000-07-10 08:42:58Z kris $"; 46#endif /* not lint */ 47 48#include <err.h> 49#include <stdlib.h> 50#include <stdio.h> 51#include <string.h> 52#include <unistd.h> 53/* 54 * colcrt - replaces col for crts with new nroff esp. when using tbl. 55 * Bill Joy UCB July 14, 1977 56 * 57 * This filter uses a screen buffer, 267 half-lines by 132 columns. 58 * It interprets the up and down sequences generated by the new 59 * nroff when used with tbl and by \u \d and \r. 60 * General overstriking doesn't work correctly. 61 * Underlining is split onto multiple lines, etc. 62 * 63 * Option - suppresses all underlining. 64 * Option -2 forces printing of all half lines. 65 */ 66 67char page[267][132]; 68 69int outline = 1; 70int outcol; 71 72char suppresul; 73char printall; 74 75FILE *f; 76 77static void usage __P((void)); 78static void pflush __P((int)); 79static int plus __P((char, char)); 80static void move __P((int, int)); 81 82int 83main(argc, argv) 84 int argc; 85 char *argv[]; 86{ 87 int c; 88 char *cp, *dp; 89 90 argc--; 91 argv++; 92 while (argc > 0 && argv[0][0] == '-') { 93 switch (argv[0][1]) { 94 case 0: 95 suppresul = 1; 96 break; 97 case '2': 98 printall = 1; 99 break; 100 default: 101 usage(); 102 } 103 argc--; 104 argv++; 105 } 106 do { 107 if (argc > 0) { 108 close(0); 109 if (!(f = fopen(argv[0], "r"))) { 110 fflush(stdout); 111 err(1, "%s", argv[0]); 112 } 113 argc--; 114 argv++; 115 } 116 for (;;) { 117 c = getc(stdin); 118 if (c == -1) { 119 pflush(outline); 120 fflush(stdout); 121 break; 122 } 123 switch (c) { 124 case '\n': 125 if (outline >= 265) 126 pflush(62); 127 outline += 2; 128 outcol = 0; 129 continue; 130 case '\016': 131 case '\017': 132 continue; 133 case 033: 134 c = getc(stdin); 135 switch (c) { 136 case '9': 137 if (outline >= 266) 138 pflush(62); 139 outline++; 140 continue; 141 case '8': 142 if (outline >= 1) 143 outline--; 144 continue; 145 case '7': 146 outline -= 2; 147 if (outline < 0) 148 outline = 0; 149 continue; 150 default: 151 continue; 152 } 153 case '\b': 154 if (outcol) 155 outcol--; 156 continue; 157 case '\t': 158 outcol += 8; 159 outcol &= ~7; 160 outcol--; 161 c = ' '; 162 default: 163 if (outcol >= 132) { 164 outcol++; 165 continue; 166 } 167 cp = &page[outline][outcol]; 168 outcol++; 169 if (c == '_') { 170 if (suppresul) 171 continue; 172 cp += 132; 173 c = '-'; 174 } 175 if (*cp == 0) { 176 *cp = c; 177 dp = cp - outcol; 178 for (cp--; cp >= dp && *cp == 0; cp--) 179 *cp = ' '; 180 } else 181 if (plus(c, *cp) || plus(*cp, c)) 182 *cp = '+'; 183 else if (*cp == ' ' || *cp == 0) 184 *cp = c; 185 continue; 186 } 187 } 188 } while (argc > 0); 189 fflush(stdout); 190 exit(0); 191} 192 193static void 194usage() 195{ 196 fprintf(stderr, "usage: colcrt [ - ] [ -2 ] [ file ... ]\n"); 197 exit(1); 198} 199 200static int 201plus(c, d) 202 char c, d; 203{ 204 205 return ((c == '|' && d == '-') || d == '_'); 206} 207 208int first; 209 210static void 211pflush(ol) 212 int ol; 213{ 214 register int i; 215 register char *cp; 216 char lastomit; 217 int l; 218 219 l = ol; 220 lastomit = 0; 221 if (l > 266) 222 l = 266; 223 else 224 l |= 1; 225 for (i = first | 1; i < l; i++) { 226 move(i, i - 1); 227 move(i, i + 1); 228 } 229 for (i = first; i < l; i++) { 230 cp = page[i]; 231 if (printall == 0 && lastomit == 0 && *cp == 0) { 232 lastomit = 1; 233 continue; 234 } 235 lastomit = 0; 236 printf("%s\n", cp); 237 } 238 bcopy(page[ol], page, (267 - ol) * 132); 239 bzero(page[267- ol], ol * 132); 240 outline -= ol; 241 outcol = 0; 242 first = 1; 243} 244 245static void 246move(l, m) 247 int l, m; 248{ 249 register char *cp, *dp; 250 251 for (cp = page[l], dp = page[m]; *cp; cp++, dp++) { 252 switch (*cp) { 253 case '|': 254 if (*dp != ' ' && *dp != '|' && *dp != 0) 255 return; 256 break; 257 case ' ': 258 break; 259 default: 260 return; 261 } 262 } 263 if (*cp == 0) { 264 for (cp = page[l], dp = page[m]; *cp; cp++, dp++) 265 if (*cp == '|') 266 *dp = '|'; 267 else if (*dp == 0) 268 *dp = ' '; 269 page[l][0] = 0; 270 } 271} 272