xref: /csrg-svn/usr.bin/fmt/fmt.c (revision 34905)
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
6*34905Sbostic  * provided that the above copyright notice and this paragraph are
7*34905Sbostic  * duplicated in all such forms and that any documentation,
8*34905Sbostic  * advertising materials, and other materials related to such
9*34905Sbostic  * distribution and use acknowledge that the software was developed
10*34905Sbostic  * by the University of California, Berkeley.  The name of the
11*34905Sbostic  * University may not be used to endorse or promote products derived
12*34905Sbostic  * from this software without specific prior written permission.
13*34905Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34905Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34905Sbostic  * 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*34905Sbostic static char sccsid[] = "@(#)fmt.c	5.6 (Berkeley) 06/29/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 */
4629545Smckusick int	goal_length = 65;	/* Target or goal line length in output */
4729545Smckusick int	max_length = 75;	/* Max line length in output */
481232Skas int	pfx;			/* Current leading blank count */
491232Skas int	lineno;			/* Current input line */
501232Skas int	mark;			/* Last place we saw a head line */
511232Skas 
5231142Sedward char	*malloc();		/* for lint . . . */
531232Skas char	*headnames[] = {"To", "Subject", "Cc", 0};
541232Skas 
551232Skas /*
561232Skas  * Drive the whole formatter by managing input files.  Also,
571232Skas  * cause initialization of the output stuff and flush it out
581232Skas  * at the end.
591232Skas  */
601232Skas 
611232Skas main(argc, argv)
6229545Smckusick 	int argc;
631232Skas 	char **argv;
641232Skas {
651232Skas 	register FILE *fi;
661232Skas 	register int errs = 0;
6729545Smckusick 	int number;		/* LIZ@UOM 6/18/85 */
681232Skas 
691232Skas 	setout();
701232Skas 	lineno = 1;
711232Skas 	mark = -10;
7229545Smckusick 	/*
7329545Smckusick 	 * LIZ@UOM 6/18/85 -- Check for goal and max length arguments
7429545Smckusick 	 */
7529545Smckusick 	if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
7629545Smckusick 		argv++;
7729545Smckusick 		argc--;
7829545Smckusick 		goal_length = number;
7929545Smckusick 		if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
8029545Smckusick 			argv++;
8129545Smckusick 			argc--;
8229545Smckusick 			max_length = number;
8329545Smckusick 		}
8429545Smckusick 	}
8529545Smckusick 	if (max_length <= goal_length) {
8629545Smckusick 		fprintf(stderr, "Max length must be greater than %s\n",
8729545Smckusick 			"goal length");
8829545Smckusick 		exit(1);
8929545Smckusick 	}
901232Skas 	if (argc < 2) {
911232Skas 		fmt(stdin);
921232Skas 		oflush();
931232Skas 		exit(0);
941232Skas 	}
951232Skas 	while (--argc) {
9629545Smckusick 		if ((fi = fopen(*++argv, "r")) == NULL) {
9729545Smckusick 			perror(*argv);
981232Skas 			errs++;
991232Skas 			continue;
1001232Skas 		}
1011232Skas 		fmt(fi);
1021232Skas 		fclose(fi);
1031232Skas 	}
1041232Skas 	oflush();
1051232Skas 	exit(errs);
1061232Skas }
1071232Skas 
1081232Skas /*
1091232Skas  * Read up characters from the passed input file, forming lines,
1101232Skas  * doing ^H processing, expanding tabs, stripping trailing blanks,
1111232Skas  * and sending each line down for analysis.
1121232Skas  */
1131232Skas fmt(fi)
1141232Skas 	FILE *fi;
1151232Skas {
1161232Skas 	char linebuf[BUFSIZ], canonb[BUFSIZ];
1171232Skas 	register char *cp, *cp2;
1181232Skas 	register int c, col;
1191232Skas 
1201232Skas 	c = getc(fi);
1211232Skas 	while (c != EOF) {
1221232Skas 		/*
1231232Skas 		 * Collect a line, doing ^H processing.
1241232Skas 		 * Leave tabs for now.
1251232Skas 		 */
1261232Skas 		cp = linebuf;
1271232Skas 		while (c != '\n' && c != EOF && cp-linebuf < BUFSIZ-1) {
1281232Skas 			if (c == '\b') {
1291232Skas 				if (cp > linebuf)
1301232Skas 					cp--;
1311232Skas 				c = getc(fi);
1321232Skas 				continue;
1331232Skas 			}
1341232Skas 			if ((c < ' ' || c >= 0177) && c != '\t') {
1351232Skas 				c = getc(fi);
1361232Skas 				continue;
1371232Skas 			}
1381232Skas 			*cp++ = c;
1391232Skas 			c = getc(fi);
1401232Skas 		}
1411232Skas 		*cp = '\0';
1421232Skas 
1431232Skas 		/*
1441232Skas 		 * Toss anything remaining on the input line.
1451232Skas 		 */
1461232Skas 		while (c != '\n' && c != EOF)
1471232Skas 			c = getc(fi);
1481232Skas 
1491232Skas 		/*
1501232Skas 		 * Expand tabs on the way to canonb.
1511232Skas 		 */
1521232Skas 		col = 0;
1531232Skas 		cp = linebuf;
1541232Skas 		cp2 = canonb;
1551232Skas 		while (c = *cp++) {
1561232Skas 			if (c != '\t') {
1571232Skas 				col++;
1581232Skas 				if (cp2-canonb < BUFSIZ-1)
1591232Skas 					*cp2++ = c;
1601232Skas 				continue;
1611232Skas 			}
1621232Skas 			do {
1631232Skas 				if (cp2-canonb < BUFSIZ-1)
1641232Skas 					*cp2++ = ' ';
1651232Skas 				col++;
1661232Skas 			} while ((col & 07) != 0);
1671232Skas 		}
1681232Skas 
1691232Skas 		/*
1701232Skas 		 * Swipe trailing blanks from the line.
1711232Skas 		 */
1721232Skas 		for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--)
1731232Skas 			;
1741232Skas 		*++cp2 = '\0';
1751232Skas 		prefix(canonb);
1761232Skas 		if (c != EOF)
1771232Skas 			c = getc(fi);
1781232Skas 	}
1791232Skas }
1801232Skas 
1811232Skas /*
1821232Skas  * Take a line devoid of tabs and other garbage and determine its
1831232Skas  * blank prefix.  If the indent changes, call for a linebreak.
1841232Skas  * If the input line is blank, echo the blank line on the output.
1851232Skas  * Finally, if the line minus the prefix is a mail header, try to keep
1861232Skas  * it on a line by itself.
1871232Skas  */
1881232Skas prefix(line)
1891232Skas 	char line[];
1901232Skas {
1911232Skas 	register char *cp, **hp;
1921232Skas 	register int np, h;
1931232Skas 
1941232Skas 	if (strlen(line) == 0) {
1951232Skas 		oflush();
1961232Skas 		putchar('\n');
1971232Skas 		return;
1981232Skas 	}
1991232Skas 	for (cp = line; *cp == ' '; cp++)
2001232Skas 		;
2011232Skas 	np = cp - line;
2021232Skas 
2031232Skas 	/*
2041232Skas 	 * The following horrible expression attempts to avoid linebreaks
2051232Skas 	 * when the indent changes due to a paragraph.
2061232Skas 	 */
2071232Skas 	if (np != pfx && (np > pfx || abs(pfx-np) > 8))
2081232Skas 		oflush();
2091232Skas 	if (h = ishead(cp))
2101232Skas 		oflush(), mark = lineno;
2111232Skas 	if (lineno - mark < 3 && lineno - mark > 0)
2121232Skas 		for (hp = &headnames[0]; *hp != (char *) 0; hp++)
2131232Skas 			if (ispref(*hp, cp)) {
2141232Skas 				h = 1;
2151232Skas 				oflush();
2161232Skas 				break;
2171232Skas 			}
2181232Skas 	if (!h && (h = (*cp == '.')))
2191232Skas 		oflush();
2201232Skas 	pfx = np;
2211232Skas 	split(cp);
2221232Skas 	if (h)
2231232Skas 		oflush();
2241232Skas 	lineno++;
2251232Skas }
2261232Skas 
2271232Skas /*
2281232Skas  * Split up the passed line into output "words" which are
2291232Skas  * maximal strings of non-blanks with the blank separation
2301232Skas  * attached at the end.  Pass these words along to the output
2311232Skas  * line packer.
2321232Skas  */
2331232Skas split(line)
2341232Skas 	char line[];
2351232Skas {
2361232Skas 	register char *cp, *cp2;
2371232Skas 	char word[BUFSIZ];
23829545Smckusick 	int wordl;		/* LIZ@UOM 6/18/85 */
2391232Skas 
2401232Skas 	cp = line;
2411232Skas 	while (*cp) {
2421232Skas 		cp2 = word;
24329545Smckusick 		wordl = 0;	/* LIZ@UOM 6/18/85 */
2441232Skas 
2451232Skas 		/*
24629545Smckusick 		 * Collect a 'word,' allowing it to contain escaped white
24729545Smckusick 		 * space.
2481232Skas 		 */
2491232Skas 		while (*cp && *cp != ' ') {
2501232Skas 			if (*cp == '\\' && isspace(cp[1]))
2511232Skas 				*cp2++ = *cp++;
2521232Skas 			*cp2++ = *cp++;
25329545Smckusick 			wordl++;/* LIZ@UOM 6/18/85 */
2541232Skas 		}
2551232Skas 
2561232Skas 		/*
25729545Smckusick 		 * Guarantee a space at end of line. Two spaces after end of
25829545Smckusick 		 * sentence punctuation.
2591232Skas 		 */
2601232Skas 		if (*cp == '\0') {
2611232Skas 			*cp2++ = ' ';
26229545Smckusick 			if (any(cp[-1], ".:!"))
2631232Skas 				*cp2++ = ' ';
2641232Skas 		}
2651232Skas 		while (*cp == ' ')
2661232Skas 			*cp2++ = *cp++;
2671232Skas 		*cp2 = '\0';
26829545Smckusick 		/*
26929545Smckusick 		 * LIZ@UOM 6/18/85 pack(word);
27029545Smckusick 		 */
27129545Smckusick 		pack(word, wordl);
2721232Skas 	}
2731232Skas }
2741232Skas 
2751232Skas /*
2761232Skas  * Output section.
2771232Skas  * Build up line images from the words passed in.  Prefix
2781232Skas  * each line with correct number of blanks.  The buffer "outbuf"
2791232Skas  * contains the current partial line image, including prefixed blanks.
2801232Skas  * "outp" points to the next available space therein.  When outp is NOSTR,
2811232Skas  * there ain't nothing in there yet.  At the bottom of this whole mess,
2821232Skas  * leading tabs are reinserted.
2831232Skas  */
2841232Skas char	outbuf[BUFSIZ];			/* Sandbagged output line image */
2851232Skas char	*outp;				/* Pointer in above */
2861232Skas 
2871232Skas /*
2881232Skas  * Initialize the output section.
2891232Skas  */
2901232Skas setout()
2911232Skas {
2921232Skas 	outp = NOSTR;
2931232Skas }
2941232Skas 
2951232Skas /*
2961232Skas  * Pack a word onto the output line.  If this is the beginning of
2971232Skas  * the line, push on the appropriately-sized string of blanks first.
2981232Skas  * If the word won't fit on the current line, flush and begin a new
2991232Skas  * line.  If the word is too long to fit all by itself on a line,
3001232Skas  * just give it its own and hope for the best.
30129545Smckusick  *
30229545Smckusick  * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
30329545Smckusick  *	goal length, take it.  If not, then check to see if the line
30429545Smckusick  *	will be over the max length; if so put the word on the next
30529545Smckusick  *	line.  If not, check to see if the line will be closer to the
30629545Smckusick  *	goal length with or without the word and take it or put it on
30729545Smckusick  *	the next line accordingly.
3081232Skas  */
3091232Skas 
31029545Smckusick /*
31129545Smckusick  * LIZ@UOM 6/18/85 -- pass in the length of the word as well
31229545Smckusick  * pack(word)
31329545Smckusick  *	char word[];
31429545Smckusick  */
31529545Smckusick pack(word,wl)
3161232Skas 	char word[];
31729545Smckusick 	int wl;
3181232Skas {
3191232Skas 	register char *cp;
3201232Skas 	register int s, t;
3211232Skas 
3221232Skas 	if (outp == NOSTR)
3231232Skas 		leadin();
32429545Smckusick 	/*
32529545Smckusick 	 * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
32629545Smckusick 	 * length of the line before the word is added; t is now the length
32729545Smckusick 	 * of the line after the word is added
32829545Smckusick 	 *	t = strlen(word);
32929545Smckusick 	 *	if (t+s <= LENGTH)
33029545Smckusick 	 */
33129545Smckusick 	s = outp - outbuf;
33229545Smckusick 	t = wl + s;
33329545Smckusick 	if ((t <= goal_length) ||
33429545Smckusick 	    ((t <= max_length) && (t - goal_length <= goal_length - s))) {
3351232Skas 		/*
33629545Smckusick 		 * In like flint!
3371232Skas 		 */
33829545Smckusick 		for (cp = word; *cp; *outp++ = *cp++);
3391232Skas 		return;
3401232Skas 	}
3411232Skas 	if (s > pfx) {
3421232Skas 		oflush();
3431232Skas 		leadin();
3441232Skas 	}
34529545Smckusick 	for (cp = word; *cp; *outp++ = *cp++);
3461232Skas }
3471232Skas 
3481232Skas /*
3491232Skas  * If there is anything on the current output line, send it on
3501232Skas  * its way.  Set outp to NOSTR to indicate the absence of the current
3511232Skas  * line prefix.
3521232Skas  */
3531232Skas oflush()
3541232Skas {
3551232Skas 	if (outp == NOSTR)
3561232Skas 		return;
3571232Skas 	*outp = '\0';
3581232Skas 	tabulate(outbuf);
3591232Skas 	outp = NOSTR;
3601232Skas }
3611232Skas 
3621232Skas /*
3631232Skas  * Take the passed line buffer, insert leading tabs where possible, and
3641232Skas  * output on standard output (finally).
3651232Skas  */
3661232Skas tabulate(line)
3671232Skas 	char line[];
3681232Skas {
3691232Skas 	register char *cp, *cp2;
3701232Skas 	register int b, t;
3711232Skas 
3721232Skas 	/*
3731232Skas 	 * Toss trailing blanks in the output line.
3741232Skas 	 */
3751232Skas 	cp = line + strlen(line) - 1;
3761232Skas 	while (cp >= line && *cp == ' ')
3771232Skas 		cp--;
3781232Skas 	*++cp = '\0';
3791232Skas 
3801232Skas 	/*
3811232Skas 	 * Count the leading blank space and tabulate.
3821232Skas 	 */
3831232Skas 	for (cp = line; *cp == ' '; cp++)
3841232Skas 		;
3851232Skas 	b = cp-line;
3861232Skas 	t = b >> 3;
3871232Skas 	b &= 07;
3881232Skas 	if (t > 0)
3891232Skas 		do
3901232Skas 			putc('\t', stdout);
3911232Skas 		while (--t);
3921232Skas 	if (b > 0)
3931232Skas 		do
3941232Skas 			putc(' ', stdout);
3951232Skas 		while (--b);
3961232Skas 	while (*cp)
3971232Skas 		putc(*cp++, stdout);
3981232Skas 	putc('\n', stdout);
3991232Skas }
4001232Skas 
4011232Skas /*
4021232Skas  * Initialize the output line with the appropriate number of
4031232Skas  * leading blanks.
4041232Skas  */
4051232Skas leadin()
4061232Skas {
4071232Skas 	register int b;
4081232Skas 	register char *cp;
4091232Skas 
4101232Skas 	for (b = 0, cp = outbuf; b < pfx; b++)
4111232Skas 		*cp++ = ' ';
4121232Skas 	outp = cp;
4131232Skas }
4141232Skas 
4151232Skas /*
4161232Skas  * Save a string in dynamic space.
4171232Skas  * This little goodie is needed for
4181232Skas  * a headline detector in head.c
4191232Skas  */
4201232Skas char *
4211232Skas savestr(str)
4221232Skas 	char str[];
4231232Skas {
4241232Skas 	register char *top;
4251232Skas 
42631142Sedward 	top = malloc(strlen(str) + 1);
4271232Skas 	if (top == NOSTR) {
4281232Skas 		fprintf(stderr, "fmt:  Ran out of memory\n");
4291232Skas 		exit(1);
4301232Skas 	}
43131142Sedward 	strcpy(top, str);
43229545Smckusick 	return (top);
4331232Skas }
4341232Skas 
4351232Skas /*
4361232Skas  * Is s1 a prefix of s2??
4371232Skas  */
4381232Skas ispref(s1, s2)
4391232Skas 	register char *s1, *s2;
4401232Skas {
4411232Skas 
4421232Skas 	while (*s1++ == *s2)
4431232Skas 		;
44429545Smckusick 	return (*s1 == '\0');
4451232Skas }
446