xref: /freebsd/usr.bin/fmt/fmt.c (revision 63ffb113474aff32b0e8bad11d52b816655aa701)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
4163ffb113SPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)fmt.c	8.1 (Berkeley) 7/20/93";
4363ffb113SPhilippe Charnier #else
4463ffb113SPhilippe Charnier static const char rcsid[] =
4563ffb113SPhilippe Charnier 	"$Id$";
4663ffb113SPhilippe Charnier #endif
479b50d902SRodney W. Grimes #endif /* not lint */
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <ctype.h>
5063ffb113SPhilippe Charnier #include <err.h>
518bbd9072SAndrey A. Chernov #include <locale.h>
5263ffb113SPhilippe Charnier #include <stdio.h>
53e6c267f1SJoerg Wunsch #include <stdlib.h>
5463ffb113SPhilippe Charnier #include <string.h>
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes /*
579b50d902SRodney W. Grimes  * fmt -- format the concatenation of input files or standard input
589b50d902SRodney W. Grimes  * onto standard output.  Designed for use with Mail ~|
599b50d902SRodney W. Grimes  *
609b50d902SRodney W. Grimes  * Syntax : fmt [ goal [ max ] ] [ name ... ]
619b50d902SRodney W. Grimes  * Authors: Kurt Shoens (UCB) 12/7/78;
629b50d902SRodney W. Grimes  *          Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].
639b50d902SRodney W. Grimes  */
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes /* LIZ@UOM 6/18/85 -- Don't need LENGTH any more.
669b50d902SRodney W. Grimes  * #define	LENGTH	72		Max line length in output
679b50d902SRodney W. Grimes  */
689b50d902SRodney W. Grimes #define	NOSTR	((char *) 0)	/* Null string pointer for lint */
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes /* LIZ@UOM 6/18/85 --New variables goal_length and max_length */
719b50d902SRodney W. Grimes #define GOAL_LENGTH 65
729b50d902SRodney W. Grimes #define MAX_LENGTH 75
739b50d902SRodney W. Grimes int	goal_length;		/* Target or goal line length in output */
749b50d902SRodney W. Grimes int	max_length;		/* Max line length in output */
759b50d902SRodney W. Grimes int	pfx;			/* Current leading blank count */
769b50d902SRodney W. Grimes int	lineno;			/* Current input line */
779b50d902SRodney W. Grimes int	mark;			/* Last place we saw a head line */
783f8da92bSPoul-Henning Kamp int	center;
799b50d902SRodney W. Grimes 
809b50d902SRodney W. Grimes char	*headnames[] = {"To", "Subject", "Cc", 0};
819b50d902SRodney W. Grimes 
8263ffb113SPhilippe Charnier void fmt __P((FILE *));
8363ffb113SPhilippe Charnier int ispref __P((char *, char *));
8463ffb113SPhilippe Charnier void leadin __P((void));
8563ffb113SPhilippe Charnier void oflush __P((void));
8663ffb113SPhilippe Charnier void pack __P((char [], int));
8763ffb113SPhilippe Charnier void prefix __P((char []));
8863ffb113SPhilippe Charnier void setout __P((void));
8963ffb113SPhilippe Charnier void split __P((char []));
9063ffb113SPhilippe Charnier void tabulate __P((char []));
9163ffb113SPhilippe Charnier 
929b50d902SRodney W. Grimes /*
939b50d902SRodney W. Grimes  * Drive the whole formatter by managing input files.  Also,
949b50d902SRodney W. Grimes  * cause initialization of the output stuff and flush it out
959b50d902SRodney W. Grimes  * at the end.
969b50d902SRodney W. Grimes  */
979b50d902SRodney W. Grimes 
9863ffb113SPhilippe Charnier int
999b50d902SRodney W. Grimes main(argc, argv)
1009b50d902SRodney W. Grimes 	int argc;
1019b50d902SRodney W. Grimes 	char **argv;
1029b50d902SRodney W. Grimes {
1039b50d902SRodney W. Grimes 	register FILE *fi;
1049b50d902SRodney W. Grimes 	register int errs = 0;
1059b50d902SRodney W. Grimes 	int number;		/* LIZ@UOM 6/18/85 */
1069b50d902SRodney W. Grimes 
1078bbd9072SAndrey A. Chernov 	(void) setlocale(LC_CTYPE, "");
1088bbd9072SAndrey A. Chernov 
1099b50d902SRodney W. Grimes 	goal_length = GOAL_LENGTH;
1109b50d902SRodney W. Grimes 	max_length = MAX_LENGTH;
1119b50d902SRodney W. Grimes 	setout();
1129b50d902SRodney W. Grimes 	lineno = 1;
1139b50d902SRodney W. Grimes 	mark = -10;
1149b50d902SRodney W. Grimes 	/*
1159b50d902SRodney W. Grimes 	 * LIZ@UOM 6/18/85 -- Check for goal and max length arguments
1169b50d902SRodney W. Grimes 	 */
1173f8da92bSPoul-Henning Kamp 	if (argc > 1 && !strcmp(argv[1], "-c")) {
1183f8da92bSPoul-Henning Kamp 		center++;
1193f8da92bSPoul-Henning Kamp 		argc--;
1203f8da92bSPoul-Henning Kamp 		argv++;
1213f8da92bSPoul-Henning Kamp 	}
1229b50d902SRodney W. Grimes 	if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
1239b50d902SRodney W. Grimes 		argv++;
1249b50d902SRodney W. Grimes 		argc--;
1259b50d902SRodney W. Grimes 		goal_length = number;
1269b50d902SRodney W. Grimes 		if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
1279b50d902SRodney W. Grimes 			argv++;
1289b50d902SRodney W. Grimes 			argc--;
1299b50d902SRodney W. Grimes 			max_length = number;
1309b50d902SRodney W. Grimes 		}
1319b50d902SRodney W. Grimes 	}
13263ffb113SPhilippe Charnier 	if (max_length <= goal_length)
13363ffb113SPhilippe Charnier 		errx(1, "max length must be greater than goal length");
1349b50d902SRodney W. Grimes 	if (argc < 2) {
1359b50d902SRodney W. Grimes 		fmt(stdin);
1369b50d902SRodney W. Grimes 		oflush();
1379b50d902SRodney W. Grimes 		exit(0);
1389b50d902SRodney W. Grimes 	}
1399b50d902SRodney W. Grimes 	while (--argc) {
1409b50d902SRodney W. Grimes 		if ((fi = fopen(*++argv, "r")) == NULL) {
1419b50d902SRodney W. Grimes 			perror(*argv);
1429b50d902SRodney W. Grimes 			errs++;
1439b50d902SRodney W. Grimes 			continue;
1449b50d902SRodney W. Grimes 		}
1459b50d902SRodney W. Grimes 		fmt(fi);
1469b50d902SRodney W. Grimes 		fclose(fi);
1479b50d902SRodney W. Grimes 	}
1489b50d902SRodney W. Grimes 	oflush();
1499b50d902SRodney W. Grimes 	exit(errs);
1509b50d902SRodney W. Grimes }
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes /*
1539b50d902SRodney W. Grimes  * Read up characters from the passed input file, forming lines,
1549b50d902SRodney W. Grimes  * doing ^H processing, expanding tabs, stripping trailing blanks,
1559b50d902SRodney W. Grimes  * and sending each line down for analysis.
1569b50d902SRodney W. Grimes  */
15763ffb113SPhilippe Charnier void
1589b50d902SRodney W. Grimes fmt(fi)
1599b50d902SRodney W. Grimes 	FILE *fi;
1609b50d902SRodney W. Grimes {
161e6c267f1SJoerg Wunsch 	static char *linebuf = 0, *canonb = 0;
162d293af2dSAndrey A. Chernov 	register char *cp, *cp2, cc;
1639b50d902SRodney W. Grimes 	register int c, col;
164e6c267f1SJoerg Wunsch #define CHUNKSIZE 1024
165e6c267f1SJoerg Wunsch 	static int lbufsize = 0, cbufsize = 0;
1669b50d902SRodney W. Grimes 
1673f8da92bSPoul-Henning Kamp 	if (center) {
1683f8da92bSPoul-Henning Kamp 		linebuf = malloc(BUFSIZ);
1693f8da92bSPoul-Henning Kamp 		while (1) {
1703f8da92bSPoul-Henning Kamp 			cp = fgets(linebuf, BUFSIZ, fi);
1713f8da92bSPoul-Henning Kamp 			if (!cp)
1723f8da92bSPoul-Henning Kamp 				return;
1733f8da92bSPoul-Henning Kamp 			while (*cp && isspace(*cp))
1743f8da92bSPoul-Henning Kamp 				cp++;
1753f8da92bSPoul-Henning Kamp 			cp2 = cp + strlen(cp) - 1;
1763f8da92bSPoul-Henning Kamp 			while (cp2 > cp && isspace(*cp2))
1773f8da92bSPoul-Henning Kamp 				cp2--;
1783f8da92bSPoul-Henning Kamp 			if (cp == cp2)
1793f8da92bSPoul-Henning Kamp 				putchar('\n');
1803f8da92bSPoul-Henning Kamp 			col = cp2 - cp;
1813f8da92bSPoul-Henning Kamp 			for (c = 0; c < (goal_length-col)/2; c++)
1823f8da92bSPoul-Henning Kamp 				putchar(' ');
1833f8da92bSPoul-Henning Kamp 			while (cp <= cp2)
1843f8da92bSPoul-Henning Kamp 				putchar(*cp++);
1853f8da92bSPoul-Henning Kamp 			putchar('\n');
1863f8da92bSPoul-Henning Kamp 		}
1873f8da92bSPoul-Henning Kamp 	}
1889b50d902SRodney W. Grimes 	c = getc(fi);
1899b50d902SRodney W. Grimes 	while (c != EOF) {
1909b50d902SRodney W. Grimes 		/*
1919b50d902SRodney W. Grimes 		 * Collect a line, doing ^H processing.
1929b50d902SRodney W. Grimes 		 * Leave tabs for now.
1939b50d902SRodney W. Grimes 		 */
1949b50d902SRodney W. Grimes 		cp = linebuf;
195e6c267f1SJoerg Wunsch 		while (c != '\n' && c != EOF) {
196e6c267f1SJoerg Wunsch 			if (cp - linebuf >= lbufsize) {
197e6c267f1SJoerg Wunsch 				int offset = cp - linebuf;
198e6c267f1SJoerg Wunsch 				lbufsize += CHUNKSIZE;
199e6c267f1SJoerg Wunsch 				linebuf = realloc(linebuf, lbufsize);
200e6c267f1SJoerg Wunsch 				if(linebuf == 0)
201e6c267f1SJoerg Wunsch 					abort();
202e6c267f1SJoerg Wunsch 				cp = linebuf + offset;
203e6c267f1SJoerg Wunsch 			}
2049b50d902SRodney W. Grimes 			if (c == '\b') {
2059b50d902SRodney W. Grimes 				if (cp > linebuf)
2069b50d902SRodney W. Grimes 					cp--;
2079b50d902SRodney W. Grimes 				c = getc(fi);
2089b50d902SRodney W. Grimes 				continue;
2099b50d902SRodney W. Grimes 			}
210d293af2dSAndrey A. Chernov 			if (!isprint(c) && c != '\t') {
2119b50d902SRodney W. Grimes 				c = getc(fi);
2129b50d902SRodney W. Grimes 				continue;
2139b50d902SRodney W. Grimes 			}
2149b50d902SRodney W. Grimes 			*cp++ = c;
2159b50d902SRodney W. Grimes 			c = getc(fi);
2169b50d902SRodney W. Grimes 		}
2179b50d902SRodney W. Grimes 
2189b50d902SRodney W. Grimes 		/*
2199b50d902SRodney W. Grimes 		 * Toss anything remaining on the input line.
2209b50d902SRodney W. Grimes 		 */
2219b50d902SRodney W. Grimes 		while (c != '\n' && c != EOF)
2229b50d902SRodney W. Grimes 			c = getc(fi);
2239b50d902SRodney W. Grimes 
224003aaef8SSujal Patel 		if (cp != NULL) {
225003aaef8SSujal Patel 			*cp = '\0';
226003aaef8SSujal Patel 		} else {
227003aaef8SSujal Patel 			putchar('\n');
228003aaef8SSujal Patel 			c = getc(fi);
229003aaef8SSujal Patel 			continue;
230003aaef8SSujal Patel 		}
231003aaef8SSujal Patel 
2329b50d902SRodney W. Grimes 		/*
2339b50d902SRodney W. Grimes 		 * Expand tabs on the way to canonb.
2349b50d902SRodney W. Grimes 		 */
2359b50d902SRodney W. Grimes 		col = 0;
2369b50d902SRodney W. Grimes 		cp = linebuf;
2379b50d902SRodney W. Grimes 		cp2 = canonb;
23863ffb113SPhilippe Charnier 		while ((cc = *cp++)) {
239d293af2dSAndrey A. Chernov 			if (cc != '\t') {
2409b50d902SRodney W. Grimes 				col++;
241e6c267f1SJoerg Wunsch 				if (cp2 - canonb >= cbufsize) {
242e6c267f1SJoerg Wunsch 					int offset = cp2 - canonb;
243e6c267f1SJoerg Wunsch 					cbufsize += CHUNKSIZE;
244e6c267f1SJoerg Wunsch 					canonb = realloc(canonb, cbufsize);
245e6c267f1SJoerg Wunsch 					if(canonb == 0)
246e6c267f1SJoerg Wunsch 						abort();
247e6c267f1SJoerg Wunsch 					cp2 = canonb + offset;
248e6c267f1SJoerg Wunsch 				}
249d293af2dSAndrey A. Chernov 				*cp2++ = cc;
2509b50d902SRodney W. Grimes 				continue;
2519b50d902SRodney W. Grimes 			}
2529b50d902SRodney W. Grimes 			do {
253e6c267f1SJoerg Wunsch 				if (cp2 - canonb >= cbufsize) {
254e6c267f1SJoerg Wunsch 					int offset = cp2 - canonb;
255e6c267f1SJoerg Wunsch 					cbufsize += CHUNKSIZE;
256e6c267f1SJoerg Wunsch 					canonb = realloc(canonb, cbufsize);
257e6c267f1SJoerg Wunsch 					if(canonb == 0)
258e6c267f1SJoerg Wunsch 						abort();
259e6c267f1SJoerg Wunsch 					cp2 = canonb + offset;
260e6c267f1SJoerg Wunsch 				}
2619b50d902SRodney W. Grimes 				*cp2++ = ' ';
2629b50d902SRodney W. Grimes 				col++;
2639b50d902SRodney W. Grimes 			} while ((col & 07) != 0);
2649b50d902SRodney W. Grimes 		}
2659b50d902SRodney W. Grimes 
2669b50d902SRodney W. Grimes 		/*
2679b50d902SRodney W. Grimes 		 * Swipe trailing blanks from the line.
2689b50d902SRodney W. Grimes 		 */
2699b50d902SRodney W. Grimes 		for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--)
2709b50d902SRodney W. Grimes 			;
2719b50d902SRodney W. Grimes 		*++cp2 = '\0';
2729b50d902SRodney W. Grimes 		prefix(canonb);
2739b50d902SRodney W. Grimes 		if (c != EOF)
2749b50d902SRodney W. Grimes 			c = getc(fi);
2759b50d902SRodney W. Grimes 	}
2769b50d902SRodney W. Grimes }
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes /*
2799b50d902SRodney W. Grimes  * Take a line devoid of tabs and other garbage and determine its
2809b50d902SRodney W. Grimes  * blank prefix.  If the indent changes, call for a linebreak.
2819b50d902SRodney W. Grimes  * If the input line is blank, echo the blank line on the output.
2829b50d902SRodney W. Grimes  * Finally, if the line minus the prefix is a mail header, try to keep
2839b50d902SRodney W. Grimes  * it on a line by itself.
2849b50d902SRodney W. Grimes  */
28563ffb113SPhilippe Charnier void
2869b50d902SRodney W. Grimes prefix(line)
2879b50d902SRodney W. Grimes 	char line[];
2889b50d902SRodney W. Grimes {
2899b50d902SRodney W. Grimes 	register char *cp, **hp;
2909b50d902SRodney W. Grimes 	register int np, h;
2919b50d902SRodney W. Grimes 
292d293af2dSAndrey A. Chernov 	if (!*line) {
2939b50d902SRodney W. Grimes 		oflush();
2949b50d902SRodney W. Grimes 		putchar('\n');
2959b50d902SRodney W. Grimes 		return;
2969b50d902SRodney W. Grimes 	}
2979b50d902SRodney W. Grimes 	for (cp = line; *cp == ' '; cp++)
2989b50d902SRodney W. Grimes 		;
2999b50d902SRodney W. Grimes 	np = cp - line;
3009b50d902SRodney W. Grimes 
3019b50d902SRodney W. Grimes 	/*
3029b50d902SRodney W. Grimes 	 * The following horrible expression attempts to avoid linebreaks
3039b50d902SRodney W. Grimes 	 * when the indent changes due to a paragraph.
3049b50d902SRodney W. Grimes 	 */
3059b50d902SRodney W. Grimes 	if (np != pfx && (np > pfx || abs(pfx-np) > 8))
3069b50d902SRodney W. Grimes 		oflush();
30763ffb113SPhilippe Charnier 	if ((h = ishead(cp)))
3089b50d902SRodney W. Grimes 		oflush(), mark = lineno;
3099b50d902SRodney W. Grimes 	if (lineno - mark < 3 && lineno - mark > 0)
3109b50d902SRodney W. Grimes 		for (hp = &headnames[0]; *hp != (char *) 0; hp++)
3119b50d902SRodney W. Grimes 			if (ispref(*hp, cp)) {
3129b50d902SRodney W. Grimes 				h = 1;
3139b50d902SRodney W. Grimes 				oflush();
3149b50d902SRodney W. Grimes 				break;
3159b50d902SRodney W. Grimes 			}
3169b50d902SRodney W. Grimes 	if (!h && (h = (*cp == '.')))
3179b50d902SRodney W. Grimes 		oflush();
3189b50d902SRodney W. Grimes 	pfx = np;
3199b50d902SRodney W. Grimes 	if (h)
3209b50d902SRodney W. Grimes 		pack(cp, strlen(cp));
3219b50d902SRodney W. Grimes 	else	split(cp);
3229b50d902SRodney W. Grimes 	if (h)
3239b50d902SRodney W. Grimes 		oflush();
3249b50d902SRodney W. Grimes 	lineno++;
3259b50d902SRodney W. Grimes }
3269b50d902SRodney W. Grimes 
3279b50d902SRodney W. Grimes /*
3289b50d902SRodney W. Grimes  * Split up the passed line into output "words" which are
3299b50d902SRodney W. Grimes  * maximal strings of non-blanks with the blank separation
3309b50d902SRodney W. Grimes  * attached at the end.  Pass these words along to the output
3319b50d902SRodney W. Grimes  * line packer.
3329b50d902SRodney W. Grimes  */
33363ffb113SPhilippe Charnier void
3349b50d902SRodney W. Grimes split(line)
3359b50d902SRodney W. Grimes 	char line[];
3369b50d902SRodney W. Grimes {
3379b50d902SRodney W. Grimes 	register char *cp, *cp2;
3389b50d902SRodney W. Grimes 	char word[BUFSIZ];
3399b50d902SRodney W. Grimes 	int wordl;		/* LIZ@UOM 6/18/85 */
3409b50d902SRodney W. Grimes 
3419b50d902SRodney W. Grimes 	cp = line;
3429b50d902SRodney W. Grimes 	while (*cp) {
3439b50d902SRodney W. Grimes 		cp2 = word;
3449b50d902SRodney W. Grimes 		wordl = 0;	/* LIZ@UOM 6/18/85 */
3459b50d902SRodney W. Grimes 
3469b50d902SRodney W. Grimes 		/*
3479b50d902SRodney W. Grimes 		 * Collect a 'word,' allowing it to contain escaped white
3489b50d902SRodney W. Grimes 		 * space.
3499b50d902SRodney W. Grimes 		 */
3509b50d902SRodney W. Grimes 		while (*cp && *cp != ' ') {
3519b50d902SRodney W. Grimes 			if (*cp == '\\' && isspace(cp[1]))
3529b50d902SRodney W. Grimes 				*cp2++ = *cp++;
3539b50d902SRodney W. Grimes 			*cp2++ = *cp++;
3549b50d902SRodney W. Grimes 			wordl++;/* LIZ@UOM 6/18/85 */
3559b50d902SRodney W. Grimes 		}
3569b50d902SRodney W. Grimes 
3579b50d902SRodney W. Grimes 		/*
3589b50d902SRodney W. Grimes 		 * Guarantee a space at end of line. Two spaces after end of
3599b50d902SRodney W. Grimes 		 * sentence punctuation.
3609b50d902SRodney W. Grimes 		 */
3619b50d902SRodney W. Grimes 		if (*cp == '\0') {
3629b50d902SRodney W. Grimes 			*cp2++ = ' ';
3639b50d902SRodney W. Grimes 			if (index(".:!", cp[-1]))
3649b50d902SRodney W. Grimes 				*cp2++ = ' ';
3659b50d902SRodney W. Grimes 		}
3669b50d902SRodney W. Grimes 		while (*cp == ' ')
3679b50d902SRodney W. Grimes 			*cp2++ = *cp++;
3689b50d902SRodney W. Grimes 		*cp2 = '\0';
3699b50d902SRodney W. Grimes 		/*
3709b50d902SRodney W. Grimes 		 * LIZ@UOM 6/18/85 pack(word);
3719b50d902SRodney W. Grimes 		 */
3729b50d902SRodney W. Grimes 		pack(word, wordl);
3739b50d902SRodney W. Grimes 	}
3749b50d902SRodney W. Grimes }
3759b50d902SRodney W. Grimes 
3769b50d902SRodney W. Grimes /*
3779b50d902SRodney W. Grimes  * Output section.
3789b50d902SRodney W. Grimes  * Build up line images from the words passed in.  Prefix
3799b50d902SRodney W. Grimes  * each line with correct number of blanks.  The buffer "outbuf"
3809b50d902SRodney W. Grimes  * contains the current partial line image, including prefixed blanks.
3819b50d902SRodney W. Grimes  * "outp" points to the next available space therein.  When outp is NOSTR,
3829b50d902SRodney W. Grimes  * there ain't nothing in there yet.  At the bottom of this whole mess,
3839b50d902SRodney W. Grimes  * leading tabs are reinserted.
3849b50d902SRodney W. Grimes  */
3859b50d902SRodney W. Grimes char	outbuf[BUFSIZ];			/* Sandbagged output line image */
3869b50d902SRodney W. Grimes char	*outp;				/* Pointer in above */
3879b50d902SRodney W. Grimes 
3889b50d902SRodney W. Grimes /*
3899b50d902SRodney W. Grimes  * Initialize the output section.
3909b50d902SRodney W. Grimes  */
39163ffb113SPhilippe Charnier void
3929b50d902SRodney W. Grimes setout()
3939b50d902SRodney W. Grimes {
3949b50d902SRodney W. Grimes 	outp = NOSTR;
3959b50d902SRodney W. Grimes }
3969b50d902SRodney W. Grimes 
3979b50d902SRodney W. Grimes /*
3989b50d902SRodney W. Grimes  * Pack a word onto the output line.  If this is the beginning of
3999b50d902SRodney W. Grimes  * the line, push on the appropriately-sized string of blanks first.
4009b50d902SRodney W. Grimes  * If the word won't fit on the current line, flush and begin a new
4019b50d902SRodney W. Grimes  * line.  If the word is too long to fit all by itself on a line,
4029b50d902SRodney W. Grimes  * just give it its own and hope for the best.
4039b50d902SRodney W. Grimes  *
4049b50d902SRodney W. Grimes  * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
4059b50d902SRodney W. Grimes  *	goal length, take it.  If not, then check to see if the line
4069b50d902SRodney W. Grimes  *	will be over the max length; if so put the word on the next
4079b50d902SRodney W. Grimes  *	line.  If not, check to see if the line will be closer to the
4089b50d902SRodney W. Grimes  *	goal length with or without the word and take it or put it on
4099b50d902SRodney W. Grimes  *	the next line accordingly.
4109b50d902SRodney W. Grimes  */
4119b50d902SRodney W. Grimes 
4129b50d902SRodney W. Grimes /*
4139b50d902SRodney W. Grimes  * LIZ@UOM 6/18/85 -- pass in the length of the word as well
4149b50d902SRodney W. Grimes  * pack(word)
4159b50d902SRodney W. Grimes  *	char word[];
4169b50d902SRodney W. Grimes  */
41763ffb113SPhilippe Charnier void
4189b50d902SRodney W. Grimes pack(word,wl)
4199b50d902SRodney W. Grimes 	char word[];
4209b50d902SRodney W. Grimes 	int wl;
4219b50d902SRodney W. Grimes {
4229b50d902SRodney W. Grimes 	register char *cp;
4239b50d902SRodney W. Grimes 	register int s, t;
4249b50d902SRodney W. Grimes 
4259b50d902SRodney W. Grimes 	if (outp == NOSTR)
4269b50d902SRodney W. Grimes 		leadin();
4279b50d902SRodney W. Grimes 	/*
4289b50d902SRodney W. Grimes 	 * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
4299b50d902SRodney W. Grimes 	 * length of the line before the word is added; t is now the length
4309b50d902SRodney W. Grimes 	 * of the line after the word is added
4319b50d902SRodney W. Grimes 	 *	t = strlen(word);
4329b50d902SRodney W. Grimes 	 *	if (t+s <= LENGTH)
4339b50d902SRodney W. Grimes 	 */
4349b50d902SRodney W. Grimes 	s = outp - outbuf;
4359b50d902SRodney W. Grimes 	t = wl + s;
4369b50d902SRodney W. Grimes 	if ((t <= goal_length) ||
4379b50d902SRodney W. Grimes 	    ((t <= max_length) && (t - goal_length <= goal_length - s))) {
4389b50d902SRodney W. Grimes 		/*
4399b50d902SRodney W. Grimes 		 * In like flint!
4409b50d902SRodney W. Grimes 		 */
4419b50d902SRodney W. Grimes 		for (cp = word; *cp; *outp++ = *cp++);
4429b50d902SRodney W. Grimes 		return;
4439b50d902SRodney W. Grimes 	}
4449b50d902SRodney W. Grimes 	if (s > pfx) {
4459b50d902SRodney W. Grimes 		oflush();
4469b50d902SRodney W. Grimes 		leadin();
4479b50d902SRodney W. Grimes 	}
4489b50d902SRodney W. Grimes 	for (cp = word; *cp; *outp++ = *cp++);
4499b50d902SRodney W. Grimes }
4509b50d902SRodney W. Grimes 
4519b50d902SRodney W. Grimes /*
4529b50d902SRodney W. Grimes  * If there is anything on the current output line, send it on
4539b50d902SRodney W. Grimes  * its way.  Set outp to NOSTR to indicate the absence of the current
4549b50d902SRodney W. Grimes  * line prefix.
4559b50d902SRodney W. Grimes  */
45663ffb113SPhilippe Charnier void
4579b50d902SRodney W. Grimes oflush()
4589b50d902SRodney W. Grimes {
4599b50d902SRodney W. Grimes 	if (outp == NOSTR)
4609b50d902SRodney W. Grimes 		return;
4619b50d902SRodney W. Grimes 	*outp = '\0';
4629b50d902SRodney W. Grimes 	tabulate(outbuf);
4639b50d902SRodney W. Grimes 	outp = NOSTR;
4649b50d902SRodney W. Grimes }
4659b50d902SRodney W. Grimes 
4669b50d902SRodney W. Grimes /*
4679b50d902SRodney W. Grimes  * Take the passed line buffer, insert leading tabs where possible, and
4689b50d902SRodney W. Grimes  * output on standard output (finally).
4699b50d902SRodney W. Grimes  */
47063ffb113SPhilippe Charnier void
4719b50d902SRodney W. Grimes tabulate(line)
4729b50d902SRodney W. Grimes 	char line[];
4739b50d902SRodney W. Grimes {
4749b50d902SRodney W. Grimes 	register char *cp;
4759b50d902SRodney W. Grimes 	register int b, t;
4769b50d902SRodney W. Grimes 
4779b50d902SRodney W. Grimes 	/*
4789b50d902SRodney W. Grimes 	 * Toss trailing blanks in the output line.
4799b50d902SRodney W. Grimes 	 */
4809b50d902SRodney W. Grimes 	cp = line + strlen(line) - 1;
4819b50d902SRodney W. Grimes 	while (cp >= line && *cp == ' ')
4829b50d902SRodney W. Grimes 		cp--;
4839b50d902SRodney W. Grimes 	*++cp = '\0';
4849b50d902SRodney W. Grimes 
4859b50d902SRodney W. Grimes 	/*
4869b50d902SRodney W. Grimes 	 * Count the leading blank space and tabulate.
4879b50d902SRodney W. Grimes 	 */
4889b50d902SRodney W. Grimes 	for (cp = line; *cp == ' '; cp++)
4899b50d902SRodney W. Grimes 		;
4909b50d902SRodney W. Grimes 	b = cp-line;
4919b50d902SRodney W. Grimes 	t = b >> 3;
4929b50d902SRodney W. Grimes 	b &= 07;
4939b50d902SRodney W. Grimes 	if (t > 0)
4949b50d902SRodney W. Grimes 		do
4959b50d902SRodney W. Grimes 			putc('\t', stdout);
4969b50d902SRodney W. Grimes 		while (--t);
4979b50d902SRodney W. Grimes 	if (b > 0)
4989b50d902SRodney W. Grimes 		do
4999b50d902SRodney W. Grimes 			putc(' ', stdout);
5009b50d902SRodney W. Grimes 		while (--b);
5019b50d902SRodney W. Grimes 	while (*cp)
5029b50d902SRodney W. Grimes 		putc(*cp++, stdout);
5039b50d902SRodney W. Grimes 	putc('\n', stdout);
5049b50d902SRodney W. Grimes }
5059b50d902SRodney W. Grimes 
5069b50d902SRodney W. Grimes /*
5079b50d902SRodney W. Grimes  * Initialize the output line with the appropriate number of
5089b50d902SRodney W. Grimes  * leading blanks.
5099b50d902SRodney W. Grimes  */
51063ffb113SPhilippe Charnier void
5119b50d902SRodney W. Grimes leadin()
5129b50d902SRodney W. Grimes {
5139b50d902SRodney W. Grimes 	register int b;
5149b50d902SRodney W. Grimes 	register char *cp;
5159b50d902SRodney W. Grimes 
5169b50d902SRodney W. Grimes 	for (b = 0, cp = outbuf; b < pfx; b++)
5179b50d902SRodney W. Grimes 		*cp++ = ' ';
5189b50d902SRodney W. Grimes 	outp = cp;
5199b50d902SRodney W. Grimes }
5209b50d902SRodney W. Grimes 
5219b50d902SRodney W. Grimes /*
5229b50d902SRodney W. Grimes  * Save a string in dynamic space.
5239b50d902SRodney W. Grimes  * This little goodie is needed for
5249b50d902SRodney W. Grimes  * a headline detector in head.c
5259b50d902SRodney W. Grimes  */
5269b50d902SRodney W. Grimes char *
5279b50d902SRodney W. Grimes savestr(str)
5289b50d902SRodney W. Grimes 	char str[];
5299b50d902SRodney W. Grimes {
5309b50d902SRodney W. Grimes 	register char *top;
5319b50d902SRodney W. Grimes 
5329b50d902SRodney W. Grimes 	top = malloc(strlen(str) + 1);
53363ffb113SPhilippe Charnier 	if (top == NOSTR)
53463ffb113SPhilippe Charnier 		errx(1, "ran out of memory");
5359b50d902SRodney W. Grimes 	strcpy(top, str);
5369b50d902SRodney W. Grimes 	return (top);
5379b50d902SRodney W. Grimes }
5389b50d902SRodney W. Grimes 
5399b50d902SRodney W. Grimes /*
5409b50d902SRodney W. Grimes  * Is s1 a prefix of s2??
5419b50d902SRodney W. Grimes  */
54263ffb113SPhilippe Charnier int
5439b50d902SRodney W. Grimes ispref(s1, s2)
5449b50d902SRodney W. Grimes 	register char *s1, *s2;
5459b50d902SRodney W. Grimes {
5469b50d902SRodney W. Grimes 
5479b50d902SRodney W. Grimes 	while (*s1++ == *s2)
5489b50d902SRodney W. Grimes 		;
5499b50d902SRodney W. Grimes 	return (*s1 == '\0');
5509b50d902SRodney W. Grimes }
551