xref: /csrg-svn/usr.bin/fmt/fmt.c (revision 34987)
122453Sdist /*
222453Sdist  * Copyright (c) 1980 Regents of the University of California.
333499Sbostic  * All rights reserved.
433499Sbostic  *
533499Sbostic  * Redistribution and use in source and binary forms are permitted
634905Sbostic  * provided that the above copyright notice and this paragraph are
734905Sbostic  * duplicated in all such forms and that any documentation,
834905Sbostic  * advertising materials, and other materials related to such
934905Sbostic  * distribution and use acknowledge that the software was developed
1034905Sbostic  * by the University of California, Berkeley.  The name of the
1134905Sbostic  * University may not be used to endorse or promote products derived
1234905Sbostic  * from this software without specific prior written permission.
1334905Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434905Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534905Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622453Sdist  */
1722453Sdist 
1814531Ssam #ifndef lint
1933499Sbostic char copyright[] =
2022453Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
2122453Sdist  All rights reserved.\n";
2233499Sbostic #endif /* not lint */
231232Skas 
2422453Sdist #ifndef lint
25*34987Sedward static char sccsid[] = "@(#)fmt.c	5.8 (Berkeley) 07/08/88";
2633499Sbostic #endif /* not lint */
2722453Sdist 
281232Skas #include <stdio.h>
291232Skas #include <ctype.h>
301232Skas 
311232Skas /*
321232Skas  * fmt -- format the concatenation of input files or standard input
331232Skas  * onto standard output.  Designed for use with Mail ~|
341232Skas  *
3529545Smckusick  * Syntax : fmt [ goal [ max ] ] [ name ... ]
3629545Smckusick  * Authors: Kurt Shoens (UCB) 12/7/78;
3729545Smckusick  *          Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].
381232Skas  */
391232Skas 
4029545Smckusick /* LIZ@UOM 6/18/85 -- Don't need LENGTH any more.
4129545Smckusick  * #define	LENGTH	72		Max line length in output
4229545Smckusick  */
431232Skas #define	NOSTR	((char *) 0)	/* Null string pointer for lint */
441232Skas 
4529545Smckusick /* LIZ@UOM 6/18/85 --New variables goal_length and max_length */
4634965Sedward #define GOAL_LENGTH 65
4734965Sedward #define MAX_LENGTH 75
4834965Sedward int	goal_length;		/* Target or goal line length in output */
4934965Sedward int	max_length;		/* Max line length in output */
501232Skas int	pfx;			/* Current leading blank count */
511232Skas int	lineno;			/* Current input line */
521232Skas int	mark;			/* Last place we saw a head line */
531232Skas 
5431142Sedward char	*malloc();		/* for lint . . . */
551232Skas char	*headnames[] = {"To", "Subject", "Cc", 0};
561232Skas 
571232Skas /*
581232Skas  * Drive the whole formatter by managing input files.  Also,
591232Skas  * cause initialization of the output stuff and flush it out
601232Skas  * at the end.
611232Skas  */
621232Skas 
631232Skas main(argc, argv)
6429545Smckusick 	int argc;
651232Skas 	char **argv;
661232Skas {
671232Skas 	register FILE *fi;
681232Skas 	register int errs = 0;
6929545Smckusick 	int number;		/* LIZ@UOM 6/18/85 */
701232Skas 
7134965Sedward 	goal_length = GOAL_LENGTH;
7234965Sedward 	max_length = MAX_LENGTH;
731232Skas 	setout();
741232Skas 	lineno = 1;
751232Skas 	mark = -10;
7629545Smckusick 	/*
7729545Smckusick 	 * LIZ@UOM 6/18/85 -- Check for goal and max length arguments
7829545Smckusick 	 */
7929545Smckusick 	if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
8029545Smckusick 		argv++;
8129545Smckusick 		argc--;
8229545Smckusick 		goal_length = number;
8329545Smckusick 		if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
8429545Smckusick 			argv++;
8529545Smckusick 			argc--;
8629545Smckusick 			max_length = number;
8729545Smckusick 		}
8829545Smckusick 	}
8929545Smckusick 	if (max_length <= goal_length) {
9029545Smckusick 		fprintf(stderr, "Max length must be greater than %s\n",
9129545Smckusick 			"goal length");
9229545Smckusick 		exit(1);
9329545Smckusick 	}
941232Skas 	if (argc < 2) {
951232Skas 		fmt(stdin);
961232Skas 		oflush();
971232Skas 		exit(0);
981232Skas 	}
991232Skas 	while (--argc) {
10029545Smckusick 		if ((fi = fopen(*++argv, "r")) == NULL) {
10129545Smckusick 			perror(*argv);
1021232Skas 			errs++;
1031232Skas 			continue;
1041232Skas 		}
1051232Skas 		fmt(fi);
1061232Skas 		fclose(fi);
1071232Skas 	}
1081232Skas 	oflush();
1091232Skas 	exit(errs);
1101232Skas }
1111232Skas 
1121232Skas /*
1131232Skas  * Read up characters from the passed input file, forming lines,
1141232Skas  * doing ^H processing, expanding tabs, stripping trailing blanks,
1151232Skas  * and sending each line down for analysis.
1161232Skas  */
1171232Skas fmt(fi)
1181232Skas 	FILE *fi;
1191232Skas {
1201232Skas 	char linebuf[BUFSIZ], canonb[BUFSIZ];
1211232Skas 	register char *cp, *cp2;
1221232Skas 	register int c, col;
1231232Skas 
1241232Skas 	c = getc(fi);
1251232Skas 	while (c != EOF) {
1261232Skas 		/*
1271232Skas 		 * Collect a line, doing ^H processing.
1281232Skas 		 * Leave tabs for now.
1291232Skas 		 */
1301232Skas 		cp = linebuf;
1311232Skas 		while (c != '\n' && c != EOF && cp-linebuf < BUFSIZ-1) {
1321232Skas 			if (c == '\b') {
1331232Skas 				if (cp > linebuf)
1341232Skas 					cp--;
1351232Skas 				c = getc(fi);
1361232Skas 				continue;
1371232Skas 			}
1381232Skas 			if ((c < ' ' || c >= 0177) && c != '\t') {
1391232Skas 				c = getc(fi);
1401232Skas 				continue;
1411232Skas 			}
1421232Skas 			*cp++ = c;
1431232Skas 			c = getc(fi);
1441232Skas 		}
1451232Skas 		*cp = '\0';
1461232Skas 
1471232Skas 		/*
1481232Skas 		 * Toss anything remaining on the input line.
1491232Skas 		 */
1501232Skas 		while (c != '\n' && c != EOF)
1511232Skas 			c = getc(fi);
1521232Skas 
1531232Skas 		/*
1541232Skas 		 * Expand tabs on the way to canonb.
1551232Skas 		 */
1561232Skas 		col = 0;
1571232Skas 		cp = linebuf;
1581232Skas 		cp2 = canonb;
1591232Skas 		while (c = *cp++) {
1601232Skas 			if (c != '\t') {
1611232Skas 				col++;
1621232Skas 				if (cp2-canonb < BUFSIZ-1)
1631232Skas 					*cp2++ = c;
1641232Skas 				continue;
1651232Skas 			}
1661232Skas 			do {
1671232Skas 				if (cp2-canonb < BUFSIZ-1)
1681232Skas 					*cp2++ = ' ';
1691232Skas 				col++;
1701232Skas 			} while ((col & 07) != 0);
1711232Skas 		}
1721232Skas 
1731232Skas 		/*
1741232Skas 		 * Swipe trailing blanks from the line.
1751232Skas 		 */
1761232Skas 		for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--)
1771232Skas 			;
1781232Skas 		*++cp2 = '\0';
1791232Skas 		prefix(canonb);
1801232Skas 		if (c != EOF)
1811232Skas 			c = getc(fi);
1821232Skas 	}
1831232Skas }
1841232Skas 
1851232Skas /*
1861232Skas  * Take a line devoid of tabs and other garbage and determine its
1871232Skas  * blank prefix.  If the indent changes, call for a linebreak.
1881232Skas  * If the input line is blank, echo the blank line on the output.
1891232Skas  * Finally, if the line minus the prefix is a mail header, try to keep
1901232Skas  * it on a line by itself.
1911232Skas  */
1921232Skas prefix(line)
1931232Skas 	char line[];
1941232Skas {
1951232Skas 	register char *cp, **hp;
1961232Skas 	register int np, h;
1971232Skas 
1981232Skas 	if (strlen(line) == 0) {
1991232Skas 		oflush();
2001232Skas 		putchar('\n');
2011232Skas 		return;
2021232Skas 	}
2031232Skas 	for (cp = line; *cp == ' '; cp++)
2041232Skas 		;
2051232Skas 	np = cp - line;
2061232Skas 
2071232Skas 	/*
2081232Skas 	 * The following horrible expression attempts to avoid linebreaks
2091232Skas 	 * when the indent changes due to a paragraph.
2101232Skas 	 */
2111232Skas 	if (np != pfx && (np > pfx || abs(pfx-np) > 8))
2121232Skas 		oflush();
2131232Skas 	if (h = ishead(cp))
2141232Skas 		oflush(), mark = lineno;
2151232Skas 	if (lineno - mark < 3 && lineno - mark > 0)
2161232Skas 		for (hp = &headnames[0]; *hp != (char *) 0; hp++)
2171232Skas 			if (ispref(*hp, cp)) {
2181232Skas 				h = 1;
2191232Skas 				oflush();
2201232Skas 				break;
2211232Skas 			}
2221232Skas 	if (!h && (h = (*cp == '.')))
2231232Skas 		oflush();
2241232Skas 	pfx = np;
2251232Skas 	split(cp);
2261232Skas 	if (h)
2271232Skas 		oflush();
2281232Skas 	lineno++;
2291232Skas }
2301232Skas 
2311232Skas /*
2321232Skas  * Split up the passed line into output "words" which are
2331232Skas  * maximal strings of non-blanks with the blank separation
2341232Skas  * attached at the end.  Pass these words along to the output
2351232Skas  * line packer.
2361232Skas  */
2371232Skas split(line)
2381232Skas 	char line[];
2391232Skas {
2401232Skas 	register char *cp, *cp2;
2411232Skas 	char word[BUFSIZ];
24229545Smckusick 	int wordl;		/* LIZ@UOM 6/18/85 */
2431232Skas 
2441232Skas 	cp = line;
2451232Skas 	while (*cp) {
2461232Skas 		cp2 = word;
24729545Smckusick 		wordl = 0;	/* LIZ@UOM 6/18/85 */
2481232Skas 
2491232Skas 		/*
25029545Smckusick 		 * Collect a 'word,' allowing it to contain escaped white
25129545Smckusick 		 * space.
2521232Skas 		 */
2531232Skas 		while (*cp && *cp != ' ') {
2541232Skas 			if (*cp == '\\' && isspace(cp[1]))
2551232Skas 				*cp2++ = *cp++;
2561232Skas 			*cp2++ = *cp++;
25729545Smckusick 			wordl++;/* LIZ@UOM 6/18/85 */
2581232Skas 		}
2591232Skas 
2601232Skas 		/*
26129545Smckusick 		 * Guarantee a space at end of line. Two spaces after end of
26229545Smckusick 		 * sentence punctuation.
2631232Skas 		 */
2641232Skas 		if (*cp == '\0') {
2651232Skas 			*cp2++ = ' ';
266*34987Sedward 			if (index(".:!", cp[-1]))
2671232Skas 				*cp2++ = ' ';
2681232Skas 		}
2691232Skas 		while (*cp == ' ')
2701232Skas 			*cp2++ = *cp++;
2711232Skas 		*cp2 = '\0';
27229545Smckusick 		/*
27329545Smckusick 		 * LIZ@UOM 6/18/85 pack(word);
27429545Smckusick 		 */
27529545Smckusick 		pack(word, wordl);
2761232Skas 	}
2771232Skas }
2781232Skas 
2791232Skas /*
2801232Skas  * Output section.
2811232Skas  * Build up line images from the words passed in.  Prefix
2821232Skas  * each line with correct number of blanks.  The buffer "outbuf"
2831232Skas  * contains the current partial line image, including prefixed blanks.
2841232Skas  * "outp" points to the next available space therein.  When outp is NOSTR,
2851232Skas  * there ain't nothing in there yet.  At the bottom of this whole mess,
2861232Skas  * leading tabs are reinserted.
2871232Skas  */
2881232Skas char	outbuf[BUFSIZ];			/* Sandbagged output line image */
2891232Skas char	*outp;				/* Pointer in above */
2901232Skas 
2911232Skas /*
2921232Skas  * Initialize the output section.
2931232Skas  */
2941232Skas setout()
2951232Skas {
2961232Skas 	outp = NOSTR;
2971232Skas }
2981232Skas 
2991232Skas /*
3001232Skas  * Pack a word onto the output line.  If this is the beginning of
3011232Skas  * the line, push on the appropriately-sized string of blanks first.
3021232Skas  * If the word won't fit on the current line, flush and begin a new
3031232Skas  * line.  If the word is too long to fit all by itself on a line,
3041232Skas  * just give it its own and hope for the best.
30529545Smckusick  *
30629545Smckusick  * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
30729545Smckusick  *	goal length, take it.  If not, then check to see if the line
30829545Smckusick  *	will be over the max length; if so put the word on the next
30929545Smckusick  *	line.  If not, check to see if the line will be closer to the
31029545Smckusick  *	goal length with or without the word and take it or put it on
31129545Smckusick  *	the next line accordingly.
3121232Skas  */
3131232Skas 
31429545Smckusick /*
31529545Smckusick  * LIZ@UOM 6/18/85 -- pass in the length of the word as well
31629545Smckusick  * pack(word)
31729545Smckusick  *	char word[];
31829545Smckusick  */
31929545Smckusick pack(word,wl)
3201232Skas 	char word[];
32129545Smckusick 	int wl;
3221232Skas {
3231232Skas 	register char *cp;
3241232Skas 	register int s, t;
3251232Skas 
3261232Skas 	if (outp == NOSTR)
3271232Skas 		leadin();
32829545Smckusick 	/*
32929545Smckusick 	 * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
33029545Smckusick 	 * length of the line before the word is added; t is now the length
33129545Smckusick 	 * of the line after the word is added
33229545Smckusick 	 *	t = strlen(word);
33329545Smckusick 	 *	if (t+s <= LENGTH)
33429545Smckusick 	 */
33529545Smckusick 	s = outp - outbuf;
33629545Smckusick 	t = wl + s;
33729545Smckusick 	if ((t <= goal_length) ||
33829545Smckusick 	    ((t <= max_length) && (t - goal_length <= goal_length - s))) {
3391232Skas 		/*
34029545Smckusick 		 * In like flint!
3411232Skas 		 */
34229545Smckusick 		for (cp = word; *cp; *outp++ = *cp++);
3431232Skas 		return;
3441232Skas 	}
3451232Skas 	if (s > pfx) {
3461232Skas 		oflush();
3471232Skas 		leadin();
3481232Skas 	}
34929545Smckusick 	for (cp = word; *cp; *outp++ = *cp++);
3501232Skas }
3511232Skas 
3521232Skas /*
3531232Skas  * If there is anything on the current output line, send it on
3541232Skas  * its way.  Set outp to NOSTR to indicate the absence of the current
3551232Skas  * line prefix.
3561232Skas  */
3571232Skas oflush()
3581232Skas {
3591232Skas 	if (outp == NOSTR)
3601232Skas 		return;
3611232Skas 	*outp = '\0';
3621232Skas 	tabulate(outbuf);
3631232Skas 	outp = NOSTR;
3641232Skas }
3651232Skas 
3661232Skas /*
3671232Skas  * Take the passed line buffer, insert leading tabs where possible, and
3681232Skas  * output on standard output (finally).
3691232Skas  */
3701232Skas tabulate(line)
3711232Skas 	char line[];
3721232Skas {
373*34987Sedward 	register char *cp;
3741232Skas 	register int b, t;
3751232Skas 
3761232Skas 	/*
3771232Skas 	 * Toss trailing blanks in the output line.
3781232Skas 	 */
3791232Skas 	cp = line + strlen(line) - 1;
3801232Skas 	while (cp >= line && *cp == ' ')
3811232Skas 		cp--;
3821232Skas 	*++cp = '\0';
3831232Skas 
3841232Skas 	/*
3851232Skas 	 * Count the leading blank space and tabulate.
3861232Skas 	 */
3871232Skas 	for (cp = line; *cp == ' '; cp++)
3881232Skas 		;
3891232Skas 	b = cp-line;
3901232Skas 	t = b >> 3;
3911232Skas 	b &= 07;
3921232Skas 	if (t > 0)
3931232Skas 		do
3941232Skas 			putc('\t', stdout);
3951232Skas 		while (--t);
3961232Skas 	if (b > 0)
3971232Skas 		do
3981232Skas 			putc(' ', stdout);
3991232Skas 		while (--b);
4001232Skas 	while (*cp)
4011232Skas 		putc(*cp++, stdout);
4021232Skas 	putc('\n', stdout);
4031232Skas }
4041232Skas 
4051232Skas /*
4061232Skas  * Initialize the output line with the appropriate number of
4071232Skas  * leading blanks.
4081232Skas  */
4091232Skas leadin()
4101232Skas {
4111232Skas 	register int b;
4121232Skas 	register char *cp;
4131232Skas 
4141232Skas 	for (b = 0, cp = outbuf; b < pfx; b++)
4151232Skas 		*cp++ = ' ';
4161232Skas 	outp = cp;
4171232Skas }
4181232Skas 
4191232Skas /*
4201232Skas  * Save a string in dynamic space.
4211232Skas  * This little goodie is needed for
4221232Skas  * a headline detector in head.c
4231232Skas  */
4241232Skas char *
4251232Skas savestr(str)
4261232Skas 	char str[];
4271232Skas {
4281232Skas 	register char *top;
4291232Skas 
43031142Sedward 	top = malloc(strlen(str) + 1);
4311232Skas 	if (top == NOSTR) {
4321232Skas 		fprintf(stderr, "fmt:  Ran out of memory\n");
4331232Skas 		exit(1);
4341232Skas 	}
43531142Sedward 	strcpy(top, str);
43629545Smckusick 	return (top);
4371232Skas }
4381232Skas 
4391232Skas /*
4401232Skas  * Is s1 a prefix of s2??
4411232Skas  */
4421232Skas ispref(s1, s2)
4431232Skas 	register char *s1, *s2;
4441232Skas {
4451232Skas 
4461232Skas 	while (*s1++ == *s2)
4471232Skas 		;
44829545Smckusick 	return (*s1 == '\0');
4491232Skas }
450