Home | History | Annotate | Line # | Download | only in fmt
fmt.c revision 1.31
      1  1.31     lukem /*	$NetBSD: fmt.c,v 1.31 2008/07/21 14:19:22 lukem 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.31     lukem __RCSID("$NetBSD: fmt.c,v 1.31 2008/07/21 14:19:22 lukem 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.26  christos 	while ((c = getopt(argc, argv, "Cg:m:r")) != -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.26  christos 			(void)getnum(optarg, "max", &max_length, 1);
    127  1.26  christos 			compat = 0;
    128  1.26  christos 			break;
    129  1.26  christos 		case 'r':
    130  1.26  christos 			raw++;
    131  1.26  christos 			break;
    132  1.26  christos 		default:
    133  1.26  christos 			usage();
    134  1.26  christos 		}
    135  1.26  christos 
    136  1.26  christos 	argc -= optind;
    137  1.26  christos 	argv += optind;
    138  1.26  christos 
    139   1.1       cgd 	/*
    140  1.26  christos 	 * compatibility with old usage.
    141   1.1       cgd 	 */
    142  1.27  christos 	if (compat && argc > 0 && getnum(*argv, "goal", &goal_length, 0)) {
    143   1.1       cgd 		argv++;
    144   1.1       cgd 		argc--;
    145  1.27  christos 		if (argc > 0 && getnum(*argv, "max", &max_length, 0)) {
    146   1.1       cgd 			argv++;
    147   1.1       cgd 			argc--;
    148   1.1       cgd 		}
    149   1.1       cgd 	}
    150  1.26  christos 
    151   1.1       cgd 	if (max_length <= goal_length) {
    152  1.19  christos 		errx(1, "Max length (%zu) must be greater than goal "
    153  1.19  christos 		    "length (%zu)", max_length, goal_length);
    154   1.1       cgd 	}
    155  1.26  christos 	if (argc == 0) {
    156   1.1       cgd 		fmt(stdin);
    157   1.1       cgd 		oflush();
    158  1.19  christos 		return 0;
    159   1.1       cgd 	}
    160  1.29  christos 	for (;argc; argc--, argv++) {
    161  1.29  christos 		if ((fi = fopen(*argv, "r")) == NULL) {
    162  1.19  christos 			warn("Cannot open `%s'", *argv);
    163   1.1       cgd 			errs++;
    164   1.1       cgd 			continue;
    165   1.1       cgd 		}
    166   1.1       cgd 		fmt(fi);
    167  1.23  christos 		(void)fclose(fi);
    168   1.1       cgd 	}
    169   1.1       cgd 	oflush();
    170  1.19  christos 	buf_end(&outbuf);
    171  1.19  christos 	return errs;
    172   1.1       cgd }
    173   1.1       cgd 
    174  1.26  christos static void
    175  1.26  christos usage(void)
    176  1.26  christos {
    177  1.26  christos 	(void)fprintf(stderr,
    178  1.26  christos 	    "Usage: %s [-Cr] [-g <goal>] [-m <max>] [<files>..]\n"
    179  1.26  christos 	    "\t %s [-Cr] [<goal>] [<max>] [<files>]\n",
    180  1.26  christos 	    getprogname(), getprogname());
    181  1.26  christos 	exit(1);
    182  1.26  christos }
    183  1.26  christos 
    184  1.26  christos static int
    185  1.26  christos getnum(const char *str, const char *what, size_t *res, int badnum)
    186  1.26  christos {
    187  1.26  christos 	unsigned long ul;
    188  1.26  christos 	char *ep;
    189  1.26  christos 
    190  1.26  christos 	errno = 0;
    191  1.26  christos 	ul = strtoul(str, &ep, 0);
    192  1.26  christos         if (*str != '\0' && *ep == '\0') {
    193  1.26  christos 		 if ((errno == ERANGE && ul == ULONG_MAX) || ul > SIZE_T_MAX)
    194  1.26  christos 			errx(1, "%s number `%s' too big", what, str);
    195  1.26  christos 		*res = (size_t)ul;
    196  1.26  christos 		return 1;
    197  1.26  christos 	} else if (badnum)
    198  1.26  christos 		errx(1, "Bad %s number `%s'", what, str);
    199  1.26  christos 
    200  1.26  christos 	return 0;
    201  1.26  christos }
    202  1.26  christos 
    203   1.1       cgd /*
    204   1.1       cgd  * Read up characters from the passed input file, forming lines,
    205   1.1       cgd  * doing ^H processing, expanding tabs, stripping trailing blanks,
    206   1.1       cgd  * and sending each line down for analysis.
    207   1.1       cgd  */
    208  1.10  jdolecek static void
    209  1.16       wiz fmt(FILE *fi)
    210   1.1       cgd {
    211  1.19  christos 	struct buffer lbuf, cbuf;
    212   1.6     lukem 	char *cp, *cp2;
    213  1.19  christos 	int c, add_space;
    214  1.30  dholland 	size_t len, col, i;
    215   1.1       cgd 
    216  1.12       abs 	if (center) {
    217  1.19  christos 		for (;;) {
    218  1.19  christos 			cp = fgetln(fi, &len);
    219  1.12       abs 			if (!cp)
    220  1.12       abs 				return;
    221  1.30  dholland 
    222  1.30  dholland 			/* skip over leading space */
    223  1.30  dholland 			while (len > 0) {
    224  1.30  dholland 				if (!isspace((unsigned char)*cp))
    225  1.30  dholland 					break;
    226  1.12       abs 				cp++;
    227  1.30  dholland 				len--;
    228  1.30  dholland 			}
    229  1.30  dholland 
    230  1.30  dholland 			/* clear trailing space */
    231  1.30  dholland 			while (len > 0) {
    232  1.30  dholland 				if (!isspace((unsigned char)cp[len-1]))
    233  1.30  dholland 					break;
    234  1.30  dholland 				len--;
    235  1.30  dholland 			}
    236  1.30  dholland 
    237  1.30  dholland 			if (len == 0) {
    238  1.30  dholland 				/* blank line */
    239  1.23  christos 				(void)putchar('\n');
    240  1.30  dholland 				continue;
    241  1.30  dholland 			}
    242  1.30  dholland 
    243  1.30  dholland 			if (goal_length > len) {
    244  1.30  dholland 				for (i = 0; i < (goal_length - len) / 2; i++) {
    245  1.23  christos 					(void)putchar(' ');
    246  1.30  dholland 				}
    247  1.30  dholland 			}
    248  1.30  dholland 			for (i = 0; i < len; i++) {
    249  1.30  dholland 				(void)putchar(cp[i]);
    250  1.30  dholland 			}
    251  1.23  christos 			(void)putchar('\n');
    252  1.12       abs 		}
    253  1.12       abs 	}
    254  1.19  christos 
    255  1.19  christos 	buf_init(&lbuf);
    256  1.19  christos 	buf_init(&cbuf);
    257   1.1       cgd 	c = getc(fi);
    258  1.19  christos 
    259   1.1       cgd 	while (c != EOF) {
    260   1.1       cgd 		/*
    261   1.1       cgd 		 * Collect a line, doing ^H processing.
    262   1.1       cgd 		 * Leave tabs for now.
    263   1.1       cgd 		 */
    264  1.19  christos 		buf_reset(&lbuf);
    265  1.19  christos 		while (c != '\n' && c != EOF) {
    266   1.1       cgd 			if (c == '\b') {
    267  1.23  christos 				(void)buf_unputc(&lbuf);
    268   1.1       cgd 				c = getc(fi);
    269   1.1       cgd 				continue;
    270   1.1       cgd 			}
    271  1.15       abs 			if(!(isprint(c) || c == '\t' || c >= 160)) {
    272   1.1       cgd 				c = getc(fi);
    273   1.1       cgd 				continue;
    274   1.1       cgd 			}
    275  1.19  christos 			buf_putc(&lbuf, c);
    276   1.1       cgd 			c = getc(fi);
    277   1.1       cgd 		}
    278  1.19  christos 		buf_putc(&lbuf, '\0');
    279  1.23  christos 		(void)buf_unputc(&lbuf);
    280  1.19  christos 		add_space = c != EOF;
    281  1.10  jdolecek 
    282  1.10  jdolecek 		/*
    283  1.19  christos 		 * Expand tabs on the way.
    284   1.1       cgd 		 */
    285   1.1       cgd 		col = 0;
    286  1.19  christos 		cp = lbuf.bptr;
    287  1.19  christos 		buf_reset(&cbuf);
    288  1.19  christos 		while ((c = *cp++) != '\0') {
    289   1.1       cgd 			if (c != '\t') {
    290   1.1       cgd 				col++;
    291  1.19  christos 				buf_putc(&cbuf, c);
    292   1.1       cgd 				continue;
    293   1.1       cgd 			}
    294   1.1       cgd 			do {
    295  1.19  christos 				buf_putc(&cbuf, ' ');
    296   1.1       cgd 				col++;
    297   1.1       cgd 			} while ((col & 07) != 0);
    298   1.1       cgd 		}
    299   1.1       cgd 
    300   1.1       cgd 		/*
    301   1.1       cgd 		 * Swipe trailing blanks from the line.
    302   1.1       cgd 		 */
    303  1.19  christos 		for (cp2 = cbuf.ptr - 1; cp2 >= cbuf.bptr && *cp2 == ' '; cp2--)
    304  1.19  christos 			continue;
    305  1.19  christos 		cbuf.ptr = cp2 + 1;
    306  1.19  christos 		buf_putc(&cbuf, '\0');
    307  1.23  christos 		(void)buf_unputc(&cbuf);
    308  1.19  christos 		prefix(&cbuf, add_space);
    309   1.1       cgd 		if (c != EOF)
    310   1.1       cgd 			c = getc(fi);
    311   1.1       cgd 	}
    312  1.19  christos 	buf_end(&cbuf);
    313  1.19  christos 	buf_end(&lbuf);
    314   1.1       cgd }
    315   1.1       cgd 
    316   1.1       cgd /*
    317   1.1       cgd  * Take a line devoid of tabs and other garbage and determine its
    318   1.1       cgd  * blank prefix.  If the indent changes, call for a linebreak.
    319   1.1       cgd  * If the input line is blank, echo the blank line on the output.
    320   1.1       cgd  * Finally, if the line minus the prefix is a mail header, try to keep
    321   1.1       cgd  * it on a line by itself.
    322   1.1       cgd  */
    323  1.10  jdolecek static void
    324  1.19  christos prefix(const struct buffer *buf, int add_space)
    325   1.1       cgd {
    326  1.10  jdolecek 	const char *cp;
    327  1.19  christos 	const char **hp;
    328  1.19  christos 	size_t np;
    329  1.19  christos 	int h;
    330   1.1       cgd 
    331  1.19  christos 	if (buf->ptr == buf->bptr) {
    332   1.1       cgd 		oflush();
    333  1.23  christos 		(void)putchar('\n');
    334   1.1       cgd 		return;
    335   1.1       cgd 	}
    336  1.19  christos 	for (cp = buf->bptr; *cp == ' '; cp++)
    337  1.19  christos 		continue;
    338  1.19  christos 	np = cp - buf->bptr;
    339   1.1       cgd 
    340   1.1       cgd 	/*
    341   1.1       cgd 	 * The following horrible expression attempts to avoid linebreaks
    342   1.1       cgd 	 * when the indent changes due to a paragraph.
    343   1.1       cgd 	 */
    344  1.19  christos 	if (np != pfx && (np > pfx || abs((int)(pfx - np)) > 8))
    345  1.19  christos 		oflush();
    346  1.26  christos 	if (!raw) {
    347  1.26  christos 		if ((h = ishead(cp)) != 0) {
    348  1.26  christos 			oflush();
    349  1.26  christos 			mark = lineno;
    350  1.26  christos 		}
    351  1.26  christos 		if (lineno - mark < 3 && lineno - mark > 0)
    352  1.26  christos 			for (hp = &headnames[0]; *hp != NULL; hp++)
    353  1.26  christos 				if (ispref(*hp, cp)) {
    354  1.26  christos 					h = 1;
    355  1.26  christos 					oflush();
    356  1.26  christos 					break;
    357  1.26  christos 				}
    358  1.26  christos 		if (!h && (h = (*cp == '.')))
    359  1.26  christos 			oflush();
    360  1.26  christos 	} else
    361  1.26  christos 		h = 0;
    362   1.1       cgd 	pfx = np;
    363  1.10  jdolecek 	if (h) {
    364  1.19  christos 		pack(cp, (size_t)(buf->ptr - cp));
    365   1.1       cgd 		oflush();
    366  1.10  jdolecek 	} else
    367  1.10  jdolecek 		split(cp, add_space);
    368   1.1       cgd 	lineno++;
    369   1.1       cgd }
    370   1.1       cgd 
    371   1.1       cgd /*
    372   1.1       cgd  * Split up the passed line into output "words" which are
    373   1.1       cgd  * maximal strings of non-blanks with the blank separation
    374   1.1       cgd  * attached at the end.  Pass these words along to the output
    375   1.1       cgd  * line packer.
    376   1.1       cgd  */
    377  1.10  jdolecek static void
    378  1.16       wiz split(const char line[], int add_space)
    379   1.1       cgd {
    380  1.10  jdolecek 	const char *cp;
    381  1.19  christos 	struct buffer word;
    382  1.19  christos 	size_t wlen;
    383   1.1       cgd 
    384  1.19  christos 	buf_init(&word);
    385   1.1       cgd 	cp = line;
    386   1.1       cgd 	while (*cp) {
    387  1.19  christos 		buf_reset(&word);
    388  1.19  christos 		wlen = 0;
    389   1.1       cgd 
    390   1.1       cgd 		/*
    391   1.1       cgd 		 * Collect a 'word,' allowing it to contain escaped white
    392   1.1       cgd 		 * space.
    393   1.1       cgd 		 */
    394   1.1       cgd 		while (*cp && *cp != ' ') {
    395   1.9  christos 			if (*cp == '\\' && isspace((unsigned char)cp[1]))
    396  1.19  christos 				buf_putc(&word, *cp++);
    397  1.19  christos 			buf_putc(&word, *cp++);
    398  1.19  christos 			wlen++;
    399   1.1       cgd 		}
    400   1.1       cgd 
    401   1.1       cgd 		/*
    402   1.1       cgd 		 * Guarantee a space at end of line. Two spaces after end of
    403   1.1       cgd 		 * sentence punctuation.
    404   1.1       cgd 		 */
    405  1.10  jdolecek 		if (*cp == '\0' && add_space) {
    406  1.19  christos 			buf_putc(&word, ' ');
    407   1.7     lukem 			if (strchr(".:!", cp[-1]))
    408  1.19  christos 				buf_putc(&word, ' ');
    409   1.1       cgd 		}
    410   1.1       cgd 		while (*cp == ' ')
    411  1.19  christos 			buf_putc(&word, *cp++);
    412  1.19  christos 
    413  1.19  christos 		buf_putc(&word, '\0');
    414  1.23  christos 		(void)buf_unputc(&word);
    415  1.19  christos 
    416  1.19  christos 		pack(word.bptr, wlen);
    417   1.1       cgd 	}
    418  1.19  christos 	buf_end(&word);
    419   1.1       cgd }
    420   1.1       cgd 
    421   1.1       cgd /*
    422   1.1       cgd  * Output section.
    423   1.1       cgd  * Build up line images from the words passed in.  Prefix
    424  1.20  christos  * each line with correct number of blanks.
    425  1.20  christos  *
    426  1.20  christos  * At the bottom of this whole mess, leading tabs are reinserted.
    427   1.1       cgd  */
    428   1.1       cgd 
    429   1.1       cgd /*
    430   1.1       cgd  * Pack a word onto the output line.  If this is the beginning of
    431   1.1       cgd  * the line, push on the appropriately-sized string of blanks first.
    432   1.1       cgd  * If the word won't fit on the current line, flush and begin a new
    433   1.1       cgd  * line.  If the word is too long to fit all by itself on a line,
    434   1.1       cgd  * just give it its own and hope for the best.
    435   1.1       cgd  *
    436   1.1       cgd  * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
    437   1.1       cgd  *	goal length, take it.  If not, then check to see if the line
    438   1.1       cgd  *	will be over the max length; if so put the word on the next
    439   1.1       cgd  *	line.  If not, check to see if the line will be closer to the
    440   1.1       cgd  *	goal length with or without the word and take it or put it on
    441   1.1       cgd  *	the next line accordingly.
    442   1.1       cgd  */
    443   1.1       cgd 
    444  1.10  jdolecek static void
    445  1.19  christos pack(const char *word, size_t wlen)
    446   1.1       cgd {
    447  1.10  jdolecek 	const char *cp;
    448  1.19  christos 	size_t s, t;
    449   1.1       cgd 
    450  1.19  christos 	if (outbuf.bptr == outbuf.ptr)
    451   1.1       cgd 		leadin();
    452   1.1       cgd 	/*
    453   1.1       cgd 	 * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
    454   1.1       cgd 	 * length of the line before the word is added; t is now the length
    455   1.1       cgd 	 * of the line after the word is added
    456   1.1       cgd 	 */
    457  1.19  christos 	s = outbuf.ptr - outbuf.bptr;
    458  1.19  christos 	t = wlen + s;
    459  1.22  christos 	if ((t <= goal_length) || ((t <= max_length) &&
    460  1.22  christos 	    (s <= goal_length) && (t - goal_length <= goal_length - s))) {
    461   1.1       cgd 		/*
    462   1.1       cgd 		 * In like flint!
    463   1.1       cgd 		 */
    464  1.19  christos 		for (cp = word; *cp;)
    465  1.19  christos 			buf_putc(&outbuf, *cp++);
    466   1.1       cgd 		return;
    467   1.1       cgd 	}
    468   1.1       cgd 	if (s > pfx) {
    469   1.1       cgd 		oflush();
    470   1.1       cgd 		leadin();
    471   1.1       cgd 	}
    472  1.19  christos 	for (cp = word; *cp;)
    473  1.19  christos 		buf_putc(&outbuf, *cp++);
    474   1.1       cgd }
    475   1.1       cgd 
    476   1.1       cgd /*
    477   1.1       cgd  * If there is anything on the current output line, send it on
    478  1.20  christos  * its way.  Reset outbuf.
    479   1.1       cgd  */
    480  1.10  jdolecek static void
    481  1.16       wiz oflush(void)
    482   1.1       cgd {
    483  1.19  christos 	if (outbuf.bptr == outbuf.ptr)
    484   1.1       cgd 		return;
    485  1.19  christos 	buf_putc(&outbuf, '\0');
    486  1.23  christos 	(void)buf_unputc(&outbuf);
    487  1.19  christos 	tabulate(&outbuf);
    488  1.19  christos 	buf_reset(&outbuf);
    489   1.1       cgd }
    490   1.1       cgd 
    491   1.1       cgd /*
    492   1.1       cgd  * Take the passed line buffer, insert leading tabs where possible, and
    493   1.1       cgd  * output on standard output (finally).
    494   1.1       cgd  */
    495  1.10  jdolecek static void
    496  1.19  christos tabulate(struct buffer *buf)
    497   1.1       cgd {
    498   1.6     lukem 	char *cp;
    499  1.19  christos 	size_t b, t;
    500   1.1       cgd 
    501   1.1       cgd 	/*
    502   1.1       cgd 	 * Toss trailing blanks in the output line.
    503   1.1       cgd 	 */
    504  1.25  christos 	for (cp = buf->ptr - 1; cp >= buf->bptr && *cp == ' '; cp--)
    505  1.19  christos 		continue;
    506  1.25  christos 	*++cp = '\0';
    507   1.1       cgd 
    508   1.1       cgd 	/*
    509   1.1       cgd 	 * Count the leading blank space and tabulate.
    510   1.1       cgd 	 */
    511  1.19  christos 	for (cp = buf->bptr; *cp == ' '; cp++)
    512  1.19  christos 		continue;
    513  1.19  christos 	b = cp - buf->bptr;
    514  1.20  christos 	t = b / 8;
    515  1.20  christos 	b = b % 8;
    516   1.1       cgd 	if (t > 0)
    517   1.1       cgd 		do
    518  1.23  christos 			(void)putchar('\t');
    519   1.1       cgd 		while (--t);
    520   1.1       cgd 	if (b > 0)
    521   1.1       cgd 		do
    522  1.23  christos 			(void)putchar(' ');
    523   1.1       cgd 		while (--b);
    524   1.1       cgd 	while (*cp)
    525  1.23  christos 		(void)putchar(*cp++);
    526  1.23  christos 	(void)putchar('\n');
    527   1.1       cgd }
    528   1.1       cgd 
    529   1.1       cgd /*
    530   1.1       cgd  * Initialize the output line with the appropriate number of
    531   1.1       cgd  * leading blanks.
    532   1.1       cgd  */
    533  1.10  jdolecek static void
    534  1.16       wiz leadin(void)
    535   1.1       cgd {
    536  1.19  christos 	size_t b;
    537  1.19  christos 
    538  1.19  christos 	buf_reset(&outbuf);
    539   1.1       cgd 
    540  1.19  christos 	for (b = 0; b < pfx; b++)
    541  1.19  christos 		buf_putc(&outbuf, ' ');
    542   1.1       cgd }
    543   1.1       cgd 
    544   1.1       cgd /*
    545   1.1       cgd  * Is s1 a prefix of s2??
    546   1.1       cgd  */
    547  1.10  jdolecek static int
    548  1.16       wiz ispref(const char *s1, const char *s2)
    549   1.1       cgd {
    550   1.1       cgd 
    551   1.1       cgd 	while (*s1++ == *s2)
    552  1.19  christos 		continue;
    553  1.19  christos 	return *s1 == '\0';
    554   1.1       cgd }
    555