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