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