132586Sbostic /*
232586Sbostic  * Copyright (c) 1987 Regents of the University of California.
333682Sbostic  * All rights reserved.
433682Sbostic  *
5*42807Sbostic  * %sccs.include.redist.c%
632586Sbostic  */
732586Sbostic 
832586Sbostic #ifndef lint
932586Sbostic char copyright[] =
1032586Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\
1132586Sbostic  All rights reserved.\n";
1233682Sbostic #endif /* not lint */
1332586Sbostic 
1432586Sbostic #ifndef lint
15*42807Sbostic static char sccsid[] = "@(#)sidebyside.c	1.5 (Berkeley) 06/01/90";
1633682Sbostic #endif /* not lint */
1732586Sbostic 
181845Sroot #include <stdio.h>
191845Sroot /*
201845Sroot  * sidebyside -- make wide listings by placing pages side by side
211845Sroot  */
221845Sroot int	width = 90;
231845Sroot 
241845Sroot #define	LINELN 440
251845Sroot #define	EJLINE	86
261845Sroot #define	LMARG	10
271845Sroot 
281845Sroot char	screen[EJLINE][LINELN];
291845Sroot char	ul[EJLINE][LINELN];
301845Sroot char	anyul[EJLINE];
311845Sroot int	frame;
321845Sroot int	origin;
331845Sroot int	outline;
341845Sroot int	outcol;
351845Sroot 
main(argc,argv)361845Sroot main(argc, argv)
371845Sroot 	int argc;
381845Sroot 	char *argv[];
391845Sroot {
401845Sroot 
411845Sroot 	argc--, argv++;
421845Sroot 	while (argc > 0 && argv[0][0] == '-') {
431845Sroot 		switch (argv[0][1]) {
441845Sroot 
451845Sroot 		case 'w':
461845Sroot 			width = atoi(argv[0]+2);
471845Sroot 			break;
481845Sroot 
491845Sroot 		default:
501845Sroot 			fprintf(stderr, "usage: sidebyside [ -wwidth ] file ...\n");
511845Sroot 			break;
521845Sroot 		}
531845Sroot 		argc--, argv++;
541845Sroot 	}
551845Sroot 	clear(screen, EJLINE * LINELN);
561845Sroot 	origin = LMARG;
571845Sroot 	outcol = LMARG;
581845Sroot 	cutmark(LMARG);
591845Sroot 	do {
601845Sroot 		if (argc > 0) {
611845Sroot 			if (freopen(argv[0], "r", stdin) == NULL) {
621845Sroot 				perror(argv[0]);
631845Sroot 				argc--, argv++;
641845Sroot 				continue;
651845Sroot 			}
661845Sroot 			argc--, argv++;
671845Sroot 		}
681845Sroot 		process();
691845Sroot 	} while (argc > 0);
701845Sroot 	exit(0);
711845Sroot }
721845Sroot 
process()731845Sroot process()
741845Sroot {
751845Sroot 	char linebuf[BUFSIZ];
761845Sroot 	register char *cp;
771845Sroot 	register int c;
781845Sroot 
791845Sroot 	while (fgets(linebuf, sizeof linebuf, stdin)) {
801845Sroot 		for (cp = linebuf; c = *cp; cp++) switch (c) {
811845Sroot 
821845Sroot 		case '\t':
831845Sroot 			do {
841845Sroot 				int ooutcol = outcol;
851845Sroot 				outchar(' ');
861845Sroot 				if (outcol == ooutcol)
871845Sroot 					break;
881845Sroot 			} while ((outcol - origin) % 8 != 0);
891845Sroot 			break;
901845Sroot 
911845Sroot 		case '\b':
921845Sroot 			if (outcol > origin)
931845Sroot 				outcol--;
941845Sroot 			break;
951845Sroot 
961845Sroot 		case '\r':
971845Sroot 			outcol = origin + LMARG;
981845Sroot 			break;
991845Sroot 
1001845Sroot 		case '\f':
1011845Sroot 			outline = EJLINE - 1;
1021845Sroot 			/* fall into ... */
1031845Sroot 
1041845Sroot 		case '\n':
1051845Sroot 			outline++;
1061845Sroot 			if (outline == EJLINE) {
1071845Sroot 				origin += width;
1081845Sroot 				if (origin + width > LINELN) {
1091845Sroot 					cutmark(origin);
1101845Sroot 					oflush();
1111845Sroot 					break;
1121845Sroot 				}
1131845Sroot /*
1141845Sroot 				if (origin * 2 + LMARG < LINELN && origin * 3 > LINELN) {
1151845Sroot 					cutmark(origin);
1161845Sroot 					origin += LMARG;
1171845Sroot 				}
1181845Sroot */
1191845Sroot 				outline = 0;
1201845Sroot 				cutmark(origin);
1211845Sroot 			}
1221845Sroot 			outcol = origin;
1231845Sroot 			break;
1241845Sroot 
1251845Sroot 		default:
1261845Sroot 			outchar(c);
1271845Sroot 			break;
1281845Sroot 		}
1291845Sroot 	}
1301845Sroot 	if (outline || origin != LMARG) {
1311845Sroot 		cutmark(origin + width);
1321845Sroot 		oflush();
1331845Sroot 	}
1341845Sroot }
1351845Sroot 
outchar(c)1361845Sroot outchar(c)
1371845Sroot 	register int c;
1381845Sroot {
1391845Sroot 	register char *cp = screen[outline];
1401845Sroot 	register char *up;
1411845Sroot 	register int d;
1421845Sroot 
1431845Sroot 	if (c < 040 || c >= 0177)
1441845Sroot 		return;
1451845Sroot 	if (outcol < LINELN) {
1461845Sroot 		cp += outcol;
1471845Sroot 		d = *cp;
1481845Sroot 		if (d == ' ') {
1491845Sroot 			*cp = c;
1501845Sroot 			outcol++;
1511845Sroot 			return;
1521845Sroot 		}
1531845Sroot 		if (d == '_' || c == '_') {
1541845Sroot 			if (c == d) {
1551845Sroot 				outcol++;
1561845Sroot 				return;
1571845Sroot 			}
1581845Sroot 			if (anyul[outline] == 0)
1591845Sroot 				clear(ul[outline], LINELN);
1601845Sroot 			anyul[outline] = 1;
1611845Sroot 			ul[outline][outcol] = '_';
1621845Sroot 			if (c == '_')
1631845Sroot 				c = d;
1641845Sroot 		}
1651845Sroot 		*cp = c;
1661845Sroot 		outcol++;
1671845Sroot 	}
1681845Sroot }
1691845Sroot 
oflush()1701845Sroot oflush()
1711845Sroot {
1721845Sroot 	register char *cp, *dp;
1731845Sroot 	register int i, j, oc, dc, c;
1741845Sroot 
1751845Sroot 	frame++;
1761845Sroot /*
1771845Sroot 	if (frame > 1) {
1781845Sroot 		printf("\n\n\n");
1791845Sroot 		for (j = 0; j < LINELN; j++)
1801845Sroot 			putchar('_');
1811845Sroot 		printf("\n");
1821845Sroot 	}
1831845Sroot */
1841845Sroot 	printf("\n");
1851845Sroot 	for (i = 0; i < EJLINE; i++) {
1861845Sroot 		putline(screen[i]);
1871845Sroot 		if (anyul[i]) {
1881845Sroot 			putchar('\r');
1891845Sroot 			putline(ul[i]);
1901845Sroot 			anyul[i] = 0;
1911845Sroot 		}
1921845Sroot 		putchar('\n');
1931845Sroot 	}
1941845Sroot 	for (i = 0; i < LINELN; i++)
1951845Sroot 		putchar('_');
1961845Sroot 	putchar('\n');
1971845Sroot 	clear(screen, EJLINE * LINELN);
1981845Sroot 	outline = 0;
1991845Sroot 	outcol = LMARG;
2001845Sroot 	origin = LMARG;
2011845Sroot 	cutmark(LMARG);
2021845Sroot }
2031845Sroot 
clear(cp,i)2041845Sroot clear(cp, i)
2051845Sroot 	register char *cp;
2061845Sroot 	register int i;
2071845Sroot {
2081845Sroot 
2091845Sroot 	if (i > 0)
2101845Sroot 		do
2111845Sroot 			*cp++ = ' ';
2121845Sroot 		while (--i);
2131845Sroot }
2141845Sroot 
cutmark(o)2151845Sroot cutmark(o)
2161845Sroot 	register int o;
2171845Sroot {
2181845Sroot 	register int i;
2191845Sroot 
2201845Sroot 	screen[0][o - LMARG] = '|';
2211845Sroot 	screen[1][o - LMARG] = '|';
2221845Sroot 	screen[EJLINE - 1][o - LMARG] = '|';
2231845Sroot 	screen[EJLINE - 2][o - LMARG] = '|';
2241845Sroot }
2251845Sroot 
putline(cp)2261845Sroot putline(cp)
2271845Sroot 	register char *cp;
2281845Sroot {
2291845Sroot 	register int j = LINELN;
2301845Sroot 	register int c, oc, dc;
2311845Sroot 
2321845Sroot 	oc = dc = 0;
2331845Sroot 	do {
2341845Sroot 		if ((c = *cp++) == ' ') {
2351845Sroot 			dc++;
2361845Sroot 			continue;
2371845Sroot 		}
2381845Sroot 		while (((oc + 8) &~ 7) < dc) {
2391845Sroot 			putchar('\t');
2401845Sroot 			oc = (oc + 8) &~ 7;
2411845Sroot 		}
2421845Sroot 		while (oc < dc) {
2431845Sroot 			putchar(' ');
2441845Sroot 			oc++;
2451845Sroot 		}
2461845Sroot 		putchar(c);
2471845Sroot 		oc++, dc++;
2481845Sroot 	} while (--j != 0);
2491845Sroot }
250