Home | History | Annotate | Line # | Download | only in fmt
fmt.c revision 1.32
      1  1.32  christos /*	$NetBSD: fmt.c,v 1.32 2012/06/30 21:31:15 christos Exp $	*/
      2   1.4       jtc 
      3   1.1       cgd /*
      4   1.4       jtc  * Copyright (c) 1980, 1993
      5   1.4       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.6     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.31     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
     35  1.31     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.4       jtc #if 0
     40   1.4       jtc static char sccsid[] = "@(#)fmt.c	8.1 (Berkeley) 7/20/93";
     41   1.4       jtc #endif
     42  1.32  christos __RCSID("$NetBSD: fmt.c,v 1.32 2012/06/30 21:31:15 christos Exp $");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45  1.16       wiz #include <ctype.h>
     46  1.16       wiz #include <locale.h>
     47   1.1       cgd #include <stdio.h>
     48   1.3       cgd #include <stdlib.h>
     49  1.26  christos #include <unistd.h>
     50  1.26  christos #include <errno.h>
     51  1.26  christos #include <err.h>
     52  1.26  christos #include <limits.h>
     53   1.3       cgd #include <string.h>
     54  1.19  christos #include "buffer.h"
     55   1.1       cgd 
     56   1.1       cgd /*
     57   1.1       cgd  * fmt -- format the concatenation of input files or standard input
     58   1.1       cgd  * onto standard output.  Designed for use with Mail ~|
     59   1.1       cgd  *
     60   1.1       cgd  * Syntax : fmt [ goal [ max ] ] [ name ... ]
     61   1.1       cgd  * Authors: Kurt Shoens (UCB) 12/7/78;
     62   1.1       cgd  *          Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].
     63   1.1       cgd  */
     64   1.1       cgd 
     65   1.1       cgd /* LIZ@UOM 6/18/85 --New variables goal_length and max_length */
     66   1.1       cgd #define GOAL_LENGTH 65
     67   1.1       cgd #define MAX_LENGTH 75
     68  1.19  christos static size_t	goal_length;	/* Target or goal line length in output */
     69  1.19  christos static size_t	max_length;	/* Max line length in output */
     70  1.19  christos static size_t	pfx;		/* Current leading blank count */
     71  1.26  christos static int	raw;		/* Don't treat mail specially */
     72  1.24  christos static int	lineno;		/* Current input line */
     73  1.24  christos static int	mark;		/* Last place we saw a head line */
     74  1.19  christos static int	center;
     75  1.19  christos static struct buffer outbuf;
     76   1.1       cgd 
     77  1.19  christos static const char	*headnames[] = {"To", "Subject", "Cc", 0};
     78   1.1       cgd 
     79  1.28     perry static void	usage(void) __dead;
     80  1.26  christos static int 	getnum(const char *, const char *, size_t *, int);
     81  1.16       wiz static void	fmt(FILE *);
     82  1.16       wiz static int	ispref(const char *, const char *);
     83  1.16       wiz static void	leadin(void);
     84  1.16       wiz static void	oflush(void);
     85  1.19  christos static void	pack(const char *, size_t);
     86  1.19  christos static void	prefix(const struct buffer *, int);
     87  1.16       wiz static void	split(const char *, int);
     88  1.19  christos static void	tabulate(struct buffer *);
     89  1.10  jdolecek 
     90  1.19  christos 
     91  1.19  christos int		ishead(const char *);
     92   1.6     lukem 
     93   1.1       cgd /*
     94   1.1       cgd  * Drive the whole formatter by managing input files.  Also,
     95   1.1       cgd  * cause initialization of the output stuff and flush it out
     96   1.1       cgd  * at the end.
     97   1.1       cgd  */
     98   1.1       cgd 
     99   1.6     lukem int
    100  1.16       wiz main(int argc, char **argv)
    101   1.1       cgd {
    102   1.6     lukem 	FILE *fi;
    103   1.6     lukem 	int errs = 0;
    104  1.26  christos 	int compat = 1;
    105  1.26  christos 	int c;
    106   1.1       cgd 
    107   1.1       cgd 	goal_length = GOAL_LENGTH;
    108   1.1       cgd 	max_length = MAX_LENGTH;
    109  1.19  christos 	buf_init(&outbuf);
    110   1.1       cgd 	lineno = 1;
    111  1.24  christos 	mark = -10;
    112   1.5    kleink 
    113  1.19  christos 	setprogname(*argv);
    114  1.23  christos 	(void)setlocale(LC_ALL, "");
    115   1.5    kleink 
    116  1.32  christos 	while ((c = getopt(argc, argv, "Cg:m:rw:")) != -1)
    117  1.26  christos 		switch (c) {
    118  1.26  christos 		case 'C':
    119  1.26  christos 			center++;
    120  1.26  christos 			break;
    121  1.26  christos 		case 'g':
    122  1.26  christos 			(void)getnum(optarg, "goal", &goal_length, 1);
    123  1.26  christos 			compat = 0;
    124  1.26  christos 			break;
    125  1.26  christos 		case 'm':
    126  1.32  christos 		case 'w':
    127  1.26  christos 			(void)getnum(optarg, "max", &max_length, 1);
    128  1.26  christos 			compat = 0;
    129  1.26  christos 			break;
    130  1.26  christos 		case 'r':
    131  1.26  christos 			raw++;
    132  1.26  christos 			break;
    133  1.26  christos 		default:
    134  1.26  christos 			usage();
    135  1.26  christos 		}
    136  1.26  christos 
    137  1.26  christos 	argc -= optind;
    138  1.26  christos 	argv += optind;
    139  1.26  christos 
    140   1.1       cgd 	/*
    141  1.26  christos 	 * compatibility with old usage.
    142   1.1       cgd 	 */
    143  1.27  christos 	if (compat && argc > 0 && getnum(*argv, "goal", &goal_length, 0)) {
    144   1.1       cgd 		argv++;
    145   1.1       cgd 		argc--;
    146  1.27  christos 		if (argc > 0 && getnum(*argv, "max", &max_length, 0)) {
    147   1.1       cgd 			argv++;
    148   1.1       cgd 			argc--;
    149   1.1       cgd 		}
    150   1.1       cgd 	}
    151  1.26  christos 
    152   1.1       cgd 	if (max_length <= goal_length) {
    153  1.19  christos 		errx(1, "Max length (%zu) must be greater than goal "
    154  1.19  christos 		    "length (%zu)", max_length, goal_length);
    155   1.1       cgd 	}
    156  1.26  christos 	if (argc == 0) {
    157   1.1       cgd 		fmt(stdin);
    158   1.1       cgd 		oflush();
    159  1.19  christos 		return 0;
    160   1.1       cgd 	}
    161  1.29  christos 	for (;argc; argc--, argv++) {
    162  1.29  christos 		if ((fi = fopen(*argv, "r")) == NULL) {
    163  1.19  christos 			warn("Cannot open `%s'", *argv);
    164   1.1       cgd 			errs++;
    165   1.1       cgd 			continue;
    166   1.1       cgd 		}
    167   1.1       cgd 		fmt(fi);
    168  1.23  christos 		(void)fclose(fi);
    169   1.1       cgd 	}
    170   1.1       cgd 	oflush();
    171  1.19  christos 	buf_end(&outbuf);
    172  1.19  christos 	return errs;
    173   1.1       cgd }
    174   1.1       cgd 
    175  1.26  christos static void
    176  1.26  christos usage(void)
    177  1.26  christos {
    178  1.26  christos 	(void)fprintf(stderr,
    179  1.32  christos 	    "Usage: %s [-Cr] [-g <goal>] [-m|w <max>] [<files>..]\n"
    180  1.26  christos 	    "\t %s [-Cr] [<goal>] [<max>] [<files>]\n",
    181  1.26  christos 	    getprogname(), getprogname());
    182  1.26  christos 	exit(1);
    183  1.26  christos }
    184  1.26  christos 
    185  1.26  christos static int
    186  1.26  christos getnum(const char *str, const char *what, size_t *res, int badnum)
    187  1.26  christos {
    188  1.26  christos 	unsigned long ul;
    189  1.26  christos 	char *ep;
    190  1.26  christos 
    191  1.26  christos 	errno = 0;
    192  1.26  christos 	ul = strtoul(str, &ep, 0);
    193  1.26  christos         if (*str != '\0' && *ep == '\0') {
    194  1.26  christos 		 if ((errno == ERANGE && ul == ULONG_MAX) || ul > SIZE_T_MAX)
    195  1.26  christos 			errx(1, "%s number `%s' too big", what, str);
    196  1.26  christos 		*res = (size_t)ul;
    197  1.26  christos 		return 1;
    198  1.26  christos 	} else if (badnum)
    199  1.26  christos 		errx(1, "Bad %s number `%s'", what, str);
    200  1.26  christos 
    201  1.26  christos 	return 0;
    202  1.26  christos }
    203  1.26  christos 
    204   1.1       cgd /*
    205   1.1       cgd  * Read up characters from the passed input file, forming lines,
    206   1.1       cgd  * doing ^H processing, expanding tabs, stripping trailing blanks,
    207   1.1       cgd  * and sending each line down for analysis.
    208   1.1       cgd  */
    209  1.10  jdolecek static void
    210  1.16       wiz fmt(FILE *fi)
    211   1.1       cgd {
    212  1.19  christos 	struct buffer lbuf, cbuf;
    213   1.6     lukem 	char *cp, *cp2;
    214  1.19  christos 	int c, add_space;
    215  1.30  dholland 	size_t len, col, i;
    216   1.1       cgd 
    217  1.12       abs 	if (center) {
    218  1.19  christos 		for (;;) {
    219  1.19  christos 			cp = fgetln(fi, &len);
    220  1.12       abs 			if (!cp)
    221  1.12       abs 				return;
    222  1.30  dholland 
    223  1.30  dholland 			/* skip over leading space */
    224  1.30  dholland 			while (len > 0) {
    225  1.30  dholland 				if (!isspace((unsigned char)*cp))
    226  1.30  dholland 					break;
    227  1.12       abs 				cp++;
    228  1.30  dholland 				len--;
    229  1.30  dholland 			}
    230  1.30  dholland 
    231  1.30  dholland 			/* clear trailing space */
    232  1.30  dholland 			while (len > 0) {
    233  1.30  dholland 				if (!isspace((unsigned char)cp[len-1]))
    234  1.30  dholland 					break;
    235  1.30  dholland 				len--;
    236  1.30  dholland 			}
    237  1.30  dholland 
    238  1.30  dholland 			if (len == 0) {
    239  1.30  dholland 				/* blank line */
    240  1.23  christos 				(void)putchar('\n');
    241  1.30  dholland 				continue;
    242  1.30  dholland 			}
    243  1.30  dholland 
    244  1.30  dholland 			if (goal_length > len) {
    245  1.30  dholland 				for (i = 0; i < (goal_length - len) / 2; i++) {
    246  1.23  christos 					(void)putchar(' ');
    247  1.30  dholland 				}
    248  1.30  dholland 			}
    249  1.30  dholland 			for (i = 0; i < len; i++) {
    250  1.30  dholland 				(void)putchar(cp[i]);
    251  1.30  dholland 			}
    252  1.23  christos 			(void)putchar('\n');
    253  1.12       abs 		}
    254  1.12       abs 	}
    255  1.19  christos 
    256  1.19  christos 	buf_init(&lbuf);
    257  1.19  christos 	buf_init(&cbuf);
    258   1.1       cgd 	c = getc(fi);
    259  1.19  christos 
    260   1.1       cgd 	while (c != EOF) {
    261   1.1       cgd 		/*
    262   1.1       cgd 		 * Collect a line, doing ^H processing.
    263   1.1       cgd 		 * Leave tabs for now.
    264   1.1       cgd 		 */
    265  1.19  christos 		buf_reset(&lbuf);
    266  1.19  christos 		while (c != '\n' && c != EOF) {
    267   1.1       cgd 			if (c == '\b') {
    268  1.23  christos 				(void)buf_unputc(&lbuf);
    269   1.1       cgd 				c = getc(fi);
    270   1.1       cgd 				continue;
    271   1.1       cgd 			}
    272  1.15       abs 			if(!(isprint(c) || c == '\t' || c >= 160)) {
    273   1.1       cgd 				c = getc(fi);
    274   1.1       cgd 				continue;
    275   1.1       cgd 			}
    276  1.19  christos 			buf_putc(&lbuf, c);
    277   1.1       cgd 			c = getc(fi);
    278   1.1       cgd 		}
    279  1.19  christos 		buf_putc(&lbuf, '\0');
    280  1.23  christos 		(void)buf_unputc(&lbuf);
    281  1.19  christos 		add_space = c != EOF;
    282  1.10  jdolecek 
    283  1.10  jdolecek 		/*
    284  1.19  christos 		 * Expand tabs on the way.
    285   1.1       cgd 		 */
    286   1.1       cgd 		col = 0;
    287  1.19  christos 		cp = lbuf.bptr;
    288  1.19  christos 		buf_reset(&cbuf);
    289  1.19  christos 		while ((c = *cp++) != '\0') {
    290   1.1       cgd 			if (c != '\t') {
    291   1.1       cgd 				col++;
    292  1.19  christos 				buf_putc(&cbuf, c);
    293   1.1       cgd 				continue;
    294   1.1       cgd 			}
    295   1.1       cgd 			do {
    296  1.19  christos 				buf_putc(&cbuf, ' ');
    297   1.1       cgd 				col++;
    298   1.1       cgd 			} while ((col & 07) != 0);
    299   1.1       cgd 		}
    300   1.1       cgd 
    301   1.1       cgd 		/*
    302   1.1       cgd 		 * Swipe trailing blanks from the line.
    303   1.1       cgd 		 */
    304  1.19  christos 		for (cp2 = cbuf.ptr - 1; cp2 >= cbuf.bptr && *cp2 == ' '; cp2--)
    305  1.19  christos 			continue;
    306  1.19  christos 		cbuf.ptr = cp2 + 1;
    307  1.19  christos 		buf_putc(&cbuf, '\0');
    308  1.23  christos 		(void)buf_unputc(&cbuf);
    309  1.19  christos 		prefix(&cbuf, add_space);
    310   1.1       cgd 		if (c != EOF)
    311   1.1       cgd 			c = getc(fi);
    312   1.1       cgd 	}
    313  1.19  christos 	buf_end(&cbuf);
    314  1.19  christos 	buf_end(&lbuf);
    315   1.1       cgd }
    316   1.1       cgd 
    317   1.1       cgd /*
    318   1.1       cgd  * Take a line devoid of tabs and other garbage and determine its
    319   1.1       cgd  * blank prefix.  If the indent changes, call for a linebreak.
    320   1.1       cgd  * If the input line is blank, echo the blank line on the output.
    321   1.1       cgd  * Finally, if the line minus the prefix is a mail header, try to keep
    322   1.1       cgd  * it on a line by itself.
    323   1.1       cgd  */
    324  1.10  jdolecek static void
    325  1.19  christos prefix(const struct buffer *buf, int add_space)
    326   1.1       cgd {
    327  1.10  jdolecek 	const char *cp;
    328  1.19  christos 	const char **hp;
    329  1.19  christos 	size_t np;
    330  1.19  christos 	int h;
    331   1.1       cgd 
    332  1.19  christos 	if (buf->ptr == buf->bptr) {
    333   1.1       cgd 		oflush();
    334  1.23  christos 		(void)putchar('\n');
    335   1.1       cgd 		return;
    336   1.1       cgd 	}
    337  1.19  christos 	for (cp = buf->bptr; *cp == ' '; cp++)
    338  1.19  christos 		continue;
    339  1.19  christos 	np = cp - buf->bptr;
    340   1.1       cgd 
    341   1.1       cgd 	/*
    342   1.1       cgd 	 * The following horrible expression attempts to avoid linebreaks
    343   1.1       cgd 	 * when the indent changes due to a paragraph.
    344   1.1       cgd 	 */
    345  1.19  christos 	if (np != pfx && (np > pfx || abs((int)(pfx - np)) > 8))
    346  1.19  christos 		oflush();
    347  1.26  christos 	if (!raw) {
    348  1.26  christos 		if ((h = ishead(cp)) != 0) {
    349  1.26  christos 			oflush();
    350  1.26  christos 			mark = lineno;
    351  1.26  christos 		}
    352  1.26  christos 		if (lineno - mark < 3 && lineno - mark > 0)
    353  1.26  christos 			for (hp = &headnames[0]; *hp != NULL; hp++)
    354  1.26  christos 				if (ispref(*hp, cp)) {
    355  1.26  christos 					h = 1;
    356  1.26  christos 					oflush();
    357  1.26  christos 					break;
    358  1.26  christos 				}
    359  1.26  christos 		if (!h && (h = (*cp == '.')))
    360  1.26  christos 			oflush();
    361  1.26  christos 	} else
    362  1.26  christos 		h = 0;
    363   1.1       cgd 	pfx = np;
    364  1.10  jdolecek 	if (h) {
    365  1.19  christos 		pack(cp, (size_t)(buf->ptr - cp));
    366   1.1       cgd 		oflush();
    367  1.10  jdolecek 	} else
    368  1.10  jdolecek 		split(cp, add_space);
    369   1.1       cgd 	lineno++;
    370   1.1       cgd }
    371   1.1       cgd 
    372   1.1       cgd /*
    373   1.1       cgd  * Split up the passed line into output "words" which are
    374   1.1       cgd  * maximal strings of non-blanks with the blank separation
    375   1.1       cgd  * attached at the end.  Pass these words along to the output
    376   1.1       cgd  * line packer.
    377   1.1       cgd  */
    378  1.10  jdolecek static void
    379  1.16       wiz split(const char line[], int add_space)
    380   1.1       cgd {
    381  1.10  jdolecek 	const char *cp;
    382  1.19  christos 	struct buffer word;
    383  1.19  christos 	size_t wlen;
    384   1.1       cgd 
    385  1.19  christos 	buf_init(&word);
    386   1.1       cgd 	cp = line;
    387   1.1       cgd 	while (*cp) {
    388  1.19  christos 		buf_reset(&word);
    389  1.19  christos 		wlen = 0;
    390   1.1       cgd 
    391   1.1       cgd 		/*
    392   1.1       cgd 		 * Collect a 'word,' allowing it to contain escaped white
    393   1.1       cgd 		 * space.
    394   1.1       cgd 		 */
    395   1.1       cgd 		while (*cp && *cp != ' ') {
    396   1.9  christos 			if (*cp == '\\' && isspace((unsigned char)cp[1]))
    397  1.19  christos 				buf_putc(&word, *cp++);
    398  1.19  christos 			buf_putc(&word, *cp++);
    399  1.19  christos 			wlen++;
    400   1.1       cgd 		}
    401   1.1       cgd 
    402   1.1       cgd 		/*
    403   1.1       cgd 		 * Guarantee a space at end of line. Two spaces after end of
    404   1.1       cgd 		 * sentence punctuation.
    405   1.1       cgd 		 */
    406  1.10  jdolecek 		if (*cp == '\0' && add_space) {
    407  1.19  christos 			buf_putc(&word, ' ');
    408   1.7     lukem 			if (strchr(".:!", cp[-1]))
    409  1.19  christos 				buf_putc(&word, ' ');
    410   1.1       cgd 		}
    411   1.1       cgd 		while (*cp == ' ')
    412  1.19  christos 			buf_putc(&word, *cp++);
    413  1.19  christos 
    414  1.19  christos 		buf_putc(&word, '\0');
    415  1.23  christos 		(void)buf_unputc(&word);
    416  1.19  christos 
    417  1.19  christos 		pack(word.bptr, wlen);
    418   1.1       cgd 	}
    419  1.19  christos 	buf_end(&word);
    420   1.1       cgd }
    421   1.1       cgd 
    422   1.1       cgd /*
    423   1.1       cgd  * Output section.
    424   1.1       cgd  * Build up line images from the words passed in.  Prefix
    425  1.20  christos  * each line with correct number of blanks.
    426  1.20  christos  *
    427  1.20  christos  * At the bottom of this whole mess, leading tabs are reinserted.
    428   1.1       cgd  */
    429   1.1       cgd 
    430   1.1       cgd /*
    431   1.1       cgd  * Pack a word onto the output line.  If this is the beginning of
    432   1.1       cgd  * the line, push on the appropriately-sized string of blanks first.
    433   1.1       cgd  * If the word won't fit on the current line, flush and begin a new
    434   1.1       cgd  * line.  If the word is too long to fit all by itself on a line,
    435   1.1       cgd  * just give it its own and hope for the best.
    436   1.1       cgd  *
    437   1.1       cgd  * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
    438   1.1       cgd  *	goal length, take it.  If not, then check to see if the line
    439   1.1       cgd  *	will be over the max length; if so put the word on the next
    440   1.1       cgd  *	line.  If not, check to see if the line will be closer to the
    441   1.1       cgd  *	goal length with or without the word and take it or put it on
    442   1.1       cgd  *	the next line accordingly.
    443   1.1       cgd  */
    444   1.1       cgd 
    445  1.10  jdolecek static void
    446  1.19  christos pack(const char *word, size_t wlen)
    447   1.1       cgd {
    448  1.10  jdolecek 	const char *cp;
    449  1.19  christos 	size_t s, t;
    450   1.1       cgd 
    451  1.19  christos 	if (outbuf.bptr == outbuf.ptr)
    452   1.1       cgd 		leadin();
    453   1.1       cgd 	/*
    454   1.1       cgd 	 * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
    455   1.1       cgd 	 * length of the line before the word is added; t is now the length
    456   1.1       cgd 	 * of the line after the word is added
    457   1.1       cgd 	 */
    458  1.19  christos 	s = outbuf.ptr - outbuf.bptr;
    459  1.19  christos 	t = wlen + s;
    460  1.22  christos 	if ((t <= goal_length) || ((t <= max_length) &&
    461  1.22  christos 	    (s <= goal_length) && (t - goal_length <= goal_length - s))) {
    462   1.1       cgd 		/*
    463   1.1       cgd 		 * In like flint!
    464   1.1       cgd 		 */
    465  1.19  christos 		for (cp = word; *cp;)
    466  1.19  christos 			buf_putc(&outbuf, *cp++);
    467   1.1       cgd 		return;
    468   1.1       cgd 	}
    469   1.1       cgd 	if (s > pfx) {
    470   1.1       cgd 		oflush();
    471   1.1       cgd 		leadin();
    472   1.1       cgd 	}
    473  1.19  christos 	for (cp = word; *cp;)
    474  1.19  christos 		buf_putc(&outbuf, *cp++);
    475   1.1       cgd }
    476   1.1       cgd 
    477   1.1       cgd /*
    478   1.1       cgd  * If there is anything on the current output line, send it on
    479  1.20  christos  * its way.  Reset outbuf.
    480   1.1       cgd  */
    481  1.10  jdolecek static void
    482  1.16       wiz oflush(void)
    483   1.1       cgd {
    484  1.19  christos 	if (outbuf.bptr == outbuf.ptr)
    485   1.1       cgd 		return;
    486  1.19  christos 	buf_putc(&outbuf, '\0');
    487  1.23  christos 	(void)buf_unputc(&outbuf);
    488  1.19  christos 	tabulate(&outbuf);
    489  1.19  christos 	buf_reset(&outbuf);
    490   1.1       cgd }
    491   1.1       cgd 
    492   1.1       cgd /*
    493   1.1       cgd  * Take the passed line buffer, insert leading tabs where possible, and
    494   1.1       cgd  * output on standard output (finally).
    495   1.1       cgd  */
    496  1.10  jdolecek static void
    497  1.19  christos tabulate(struct buffer *buf)
    498   1.1       cgd {
    499   1.6     lukem 	char *cp;
    500  1.19  christos 	size_t b, t;
    501   1.1       cgd 
    502   1.1       cgd 	/*
    503   1.1       cgd 	 * Toss trailing blanks in the output line.
    504   1.1       cgd 	 */
    505  1.25  christos 	for (cp = buf->ptr - 1; cp >= buf->bptr && *cp == ' '; cp--)
    506  1.19  christos 		continue;
    507  1.25  christos 	*++cp = '\0';
    508   1.1       cgd 
    509   1.1       cgd 	/*
    510   1.1       cgd 	 * Count the leading blank space and tabulate.
    511   1.1       cgd 	 */
    512  1.19  christos 	for (cp = buf->bptr; *cp == ' '; cp++)
    513  1.19  christos 		continue;
    514  1.19  christos 	b = cp - buf->bptr;
    515  1.20  christos 	t = b / 8;
    516  1.20  christos 	b = b % 8;
    517   1.1       cgd 	if (t > 0)
    518   1.1       cgd 		do
    519  1.23  christos 			(void)putchar('\t');
    520   1.1       cgd 		while (--t);
    521   1.1       cgd 	if (b > 0)
    522   1.1       cgd 		do
    523  1.23  christos 			(void)putchar(' ');
    524   1.1       cgd 		while (--b);
    525   1.1       cgd 	while (*cp)
    526  1.23  christos 		(void)putchar(*cp++);
    527  1.23  christos 	(void)putchar('\n');
    528   1.1       cgd }
    529   1.1       cgd 
    530   1.1       cgd /*
    531   1.1       cgd  * Initialize the output line with the appropriate number of
    532   1.1       cgd  * leading blanks.
    533   1.1       cgd  */
    534  1.10  jdolecek static void
    535  1.16       wiz leadin(void)
    536   1.1       cgd {
    537  1.19  christos 	size_t b;
    538  1.19  christos 
    539  1.19  christos 	buf_reset(&outbuf);
    540   1.1       cgd 
    541  1.19  christos 	for (b = 0; b < pfx; b++)
    542  1.19  christos 		buf_putc(&outbuf, ' ');
    543   1.1       cgd }
    544   1.1       cgd 
    545   1.1       cgd /*
    546   1.1       cgd  * Is s1 a prefix of s2??
    547   1.1       cgd  */
    548  1.10  jdolecek static int
    549  1.16       wiz ispref(const char *s1, const char *s2)
    550   1.1       cgd {
    551   1.1       cgd 
    552   1.1       cgd 	while (*s1++ == *s2)
    553  1.19  christos 		continue;
    554  1.19  christos 	return *s1 == '\0';
    555   1.1       cgd }
    556