Home | History | Annotate | Line # | Download | only in fmt
fmt.c revision 1.18
      1  1.18       dsl /*	$NetBSD: fmt.c,v 1.18 2004/10/30 17:23:40 dsl 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.6     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     35   1.6     lukem 	The Regents of the University of California.  All rights reserved.\n");
     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.18       dsl __RCSID("$NetBSD: fmt.c,v 1.18 2004/10/30 17:23:40 dsl 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.3       cgd #include <string.h>
     50   1.1       cgd 
     51   1.1       cgd /*
     52   1.1       cgd  * fmt -- format the concatenation of input files or standard input
     53   1.1       cgd  * onto standard output.  Designed for use with Mail ~|
     54   1.1       cgd  *
     55   1.1       cgd  * Syntax : fmt [ goal [ max ] ] [ name ... ]
     56   1.1       cgd  * Authors: Kurt Shoens (UCB) 12/7/78;
     57   1.1       cgd  *          Liz Allen (UMCP) 2/24/83 [Addition of goal length concept].
     58   1.1       cgd  */
     59   1.1       cgd 
     60   1.1       cgd /* LIZ@UOM 6/18/85 -- Don't need LENGTH any more.
     61   1.1       cgd  * #define	LENGTH	72		Max line length in output
     62   1.1       cgd  */
     63   1.1       cgd #define	NOSTR	((char *) 0)	/* Null string pointer for lint */
     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.1       cgd int	goal_length;		/* Target or goal line length in output */
     69   1.1       cgd int	max_length;		/* Max line length in output */
     70   1.1       cgd int	pfx;			/* Current leading blank count */
     71   1.1       cgd int	lineno;			/* Current input line */
     72   1.1       cgd int	mark;			/* Last place we saw a head line */
     73  1.12       abs int	center;
     74   1.1       cgd 
     75   1.1       cgd char	*headnames[] = {"To", "Subject", "Cc", 0};
     76   1.1       cgd 
     77  1.16       wiz static void	fmt(FILE *);
     78  1.16       wiz static int	ispref(const char *, const char *);
     79  1.16       wiz static void	leadin(void);
     80  1.16       wiz static void	oflush(void);
     81  1.16       wiz static void	pack(const char *, int);
     82  1.16       wiz static void	prefix(const char *, int);
     83  1.16       wiz static void	setout(void);
     84  1.16       wiz static void	split(const char *, int);
     85  1.16       wiz static void	tabulate(char *);
     86  1.10  jdolecek 
     87  1.16       wiz int	ishead(const char *);
     88  1.16       wiz int	main(int, char **);
     89   1.6     lukem 
     90   1.1       cgd /*
     91   1.1       cgd  * Drive the whole formatter by managing input files.  Also,
     92   1.1       cgd  * cause initialization of the output stuff and flush it out
     93   1.1       cgd  * at the end.
     94   1.1       cgd  */
     95   1.1       cgd 
     96   1.6     lukem int
     97  1.16       wiz main(int argc, char **argv)
     98   1.1       cgd {
     99   1.6     lukem 	FILE *fi;
    100   1.6     lukem 	int errs = 0;
    101   1.1       cgd 	int number;		/* LIZ@UOM 6/18/85 */
    102   1.1       cgd 
    103   1.1       cgd 	goal_length = GOAL_LENGTH;
    104   1.1       cgd 	max_length = MAX_LENGTH;
    105   1.1       cgd 	setout();
    106   1.1       cgd 	lineno = 1;
    107   1.1       cgd 	mark = -10;
    108   1.5    kleink 
    109   1.5    kleink 	setlocale(LC_ALL, "");
    110   1.5    kleink 
    111   1.1       cgd 	/*
    112   1.1       cgd 	 * LIZ@UOM 6/18/85 -- Check for goal and max length arguments
    113   1.1       cgd 	 */
    114  1.13       abs 	if (argc > 1 && !strcmp(argv[1], "-C")) {
    115  1.12       abs 		center++;
    116  1.12       abs 		argc--;
    117  1.12       abs 		argv++;
    118  1.12       abs 	}
    119   1.1       cgd 	if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
    120   1.1       cgd 		argv++;
    121   1.1       cgd 		argc--;
    122   1.8      ross 		goal_length = abs(number);
    123   1.1       cgd 		if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) {
    124   1.1       cgd 			argv++;
    125   1.1       cgd 			argc--;
    126   1.8      ross 			max_length = abs(number);
    127   1.1       cgd 		}
    128   1.1       cgd 	}
    129   1.1       cgd 	if (max_length <= goal_length) {
    130   1.1       cgd 		fprintf(stderr, "Max length must be greater than %s\n",
    131   1.1       cgd 			"goal length");
    132   1.1       cgd 		exit(1);
    133   1.1       cgd 	}
    134   1.1       cgd 	if (argc < 2) {
    135   1.1       cgd 		fmt(stdin);
    136   1.1       cgd 		oflush();
    137   1.1       cgd 		exit(0);
    138   1.1       cgd 	}
    139   1.1       cgd 	while (--argc) {
    140   1.1       cgd 		if ((fi = fopen(*++argv, "r")) == NULL) {
    141   1.1       cgd 			perror(*argv);
    142   1.1       cgd 			errs++;
    143   1.1       cgd 			continue;
    144   1.1       cgd 		}
    145   1.1       cgd 		fmt(fi);
    146   1.1       cgd 		fclose(fi);
    147   1.1       cgd 	}
    148   1.1       cgd 	oflush();
    149   1.1       cgd 	exit(errs);
    150   1.1       cgd }
    151   1.1       cgd 
    152   1.1       cgd /*
    153   1.1       cgd  * Read up characters from the passed input file, forming lines,
    154   1.1       cgd  * doing ^H processing, expanding tabs, stripping trailing blanks,
    155   1.1       cgd  * and sending each line down for analysis.
    156   1.1       cgd  */
    157  1.10  jdolecek static void
    158  1.16       wiz fmt(FILE *fi)
    159   1.1       cgd {
    160   1.1       cgd 	char linebuf[BUFSIZ], canonb[BUFSIZ];
    161   1.6     lukem 	char *cp, *cp2;
    162  1.10  jdolecek 	int c, col, add_space;
    163   1.1       cgd 
    164  1.12       abs 	if (center) {
    165  1.12       abs 		while (1) {
    166  1.12       abs 			cp = fgets(linebuf, BUFSIZ, fi);
    167  1.12       abs 			if (!cp)
    168  1.12       abs 				return;
    169  1.18       dsl 			while (*cp && isspace((unsigned char)*cp))
    170  1.12       abs 				cp++;
    171  1.12       abs 			cp2 = cp + strlen(cp) - 1;
    172  1.18       dsl 			while (cp2 > cp && isspace((unsigned char)*cp2))
    173  1.12       abs 				cp2--;
    174  1.12       abs 			if (cp == cp2)
    175  1.12       abs 				putchar('\n');
    176  1.12       abs 			col = cp2 - cp;
    177  1.12       abs 			for (c = 0; c < (goal_length-col)/2; c++)
    178  1.12       abs 				putchar(' ');
    179  1.12       abs 			while (cp <= cp2)
    180  1.12       abs 				putchar(*cp++);
    181  1.12       abs 			putchar('\n');
    182  1.12       abs 		}
    183  1.12       abs 	}
    184   1.1       cgd 	c = getc(fi);
    185   1.1       cgd 	while (c != EOF) {
    186   1.1       cgd 		/*
    187   1.1       cgd 		 * Collect a line, doing ^H processing.
    188   1.1       cgd 		 * Leave tabs for now.
    189   1.1       cgd 		 */
    190   1.1       cgd 		cp = linebuf;
    191   1.1       cgd 		while (c != '\n' && c != EOF && cp-linebuf < BUFSIZ-1) {
    192   1.1       cgd 			if (c == '\b') {
    193   1.1       cgd 				if (cp > linebuf)
    194   1.1       cgd 					cp--;
    195   1.1       cgd 				c = getc(fi);
    196   1.1       cgd 				continue;
    197   1.1       cgd 			}
    198  1.15       abs 			if(!(isprint(c) || c == '\t' || c >= 160)) {
    199   1.1       cgd 				c = getc(fi);
    200   1.1       cgd 				continue;
    201   1.1       cgd 			}
    202   1.1       cgd 			*cp++ = c;
    203   1.1       cgd 			c = getc(fi);
    204   1.1       cgd 		}
    205   1.1       cgd 		*cp = '\0';
    206   1.1       cgd 
    207   1.1       cgd 		/*
    208  1.10  jdolecek 		 * By default, add space after the end of current input
    209  1.10  jdolecek 		 * (normally end of line)
    210  1.10  jdolecek 		 */
    211  1.10  jdolecek 		add_space = 1;
    212  1.10  jdolecek 
    213  1.10  jdolecek 		/*
    214  1.10  jdolecek 		 * If the input line is longer than linebuf buffer can hold,
    215  1.10  jdolecek 		 * process the data read so far as if it was a separate line -
    216  1.10  jdolecek 		 * if there is any whitespace character in the read data,
    217  1.10  jdolecek 		 * process all the data up to it, otherwise process all.
    218   1.1       cgd 		 */
    219  1.10  jdolecek 		if (c != '\n' && c != EOF && !isspace(c)) {
    220  1.10  jdolecek 			/*
    221  1.10  jdolecek 			 * Find out if any whitespace character has been read.
    222  1.10  jdolecek 			 */
    223  1.10  jdolecek 			for(cp2 = cp; cp2 >= linebuf
    224  1.10  jdolecek 				&& !isspace((unsigned char)*cp2); cp2--);
    225  1.10  jdolecek 
    226  1.10  jdolecek 			if (cp2 < linebuf) {
    227  1.10  jdolecek 				/*
    228  1.10  jdolecek 				 * ungetc() last read character so that it
    229  1.10  jdolecek 				 * won't get lost.
    230  1.10  jdolecek 				 */
    231  1.10  jdolecek 				ungetc(c, fi);
    232  1.10  jdolecek 				/*
    233  1.10  jdolecek 				 * Don't append space on the end in split().
    234  1.10  jdolecek 				 */
    235  1.10  jdolecek 				add_space = 0;
    236  1.10  jdolecek 			} else {
    237  1.10  jdolecek 				/*
    238  1.10  jdolecek 				 * To avoid splitting a word in a middle,
    239  1.10  jdolecek 				 * ungetc() all characters after last
    240  1.10  jdolecek 				 * whitespace char.
    241  1.10  jdolecek 				 */
    242  1.10  jdolecek 				while (!isspace(c) && (cp >= linebuf)) {
    243  1.10  jdolecek 					ungetc(c, fi);
    244  1.10  jdolecek 					c = *--cp;
    245  1.10  jdolecek 				}
    246  1.10  jdolecek 				*cp = '\0';
    247  1.10  jdolecek 			}
    248  1.10  jdolecek 		}
    249   1.1       cgd 
    250   1.1       cgd 		/*
    251   1.1       cgd 		 * Expand tabs on the way to canonb.
    252   1.1       cgd 		 */
    253   1.1       cgd 		col = 0;
    254   1.1       cgd 		cp = linebuf;
    255   1.1       cgd 		cp2 = canonb;
    256   1.6     lukem 		while ((c = *cp++) != 0) {
    257   1.1       cgd 			if (c != '\t') {
    258   1.1       cgd 				col++;
    259   1.1       cgd 				if (cp2-canonb < BUFSIZ-1)
    260   1.1       cgd 					*cp2++ = c;
    261   1.1       cgd 				continue;
    262   1.1       cgd 			}
    263   1.1       cgd 			do {
    264   1.1       cgd 				if (cp2-canonb < BUFSIZ-1)
    265   1.1       cgd 					*cp2++ = ' ';
    266   1.1       cgd 				col++;
    267   1.1       cgd 			} while ((col & 07) != 0);
    268   1.1       cgd 		}
    269   1.1       cgd 
    270   1.1       cgd 		/*
    271   1.1       cgd 		 * Swipe trailing blanks from the line.
    272   1.1       cgd 		 */
    273   1.1       cgd 		for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--)
    274   1.1       cgd 			;
    275   1.1       cgd 		*++cp2 = '\0';
    276  1.10  jdolecek 		prefix(canonb, add_space);
    277   1.1       cgd 		if (c != EOF)
    278   1.1       cgd 			c = getc(fi);
    279   1.1       cgd 	}
    280   1.1       cgd }
    281   1.1       cgd 
    282   1.1       cgd /*
    283   1.1       cgd  * Take a line devoid of tabs and other garbage and determine its
    284   1.1       cgd  * blank prefix.  If the indent changes, call for a linebreak.
    285   1.1       cgd  * If the input line is blank, echo the blank line on the output.
    286   1.1       cgd  * Finally, if the line minus the prefix is a mail header, try to keep
    287   1.1       cgd  * it on a line by itself.
    288   1.1       cgd  */
    289  1.10  jdolecek static void
    290  1.16       wiz prefix(const char line[], int add_space)
    291   1.1       cgd {
    292  1.10  jdolecek 	const char *cp;
    293  1.10  jdolecek 	char **hp;
    294   1.6     lukem 	int np, h;
    295   1.1       cgd 
    296   1.1       cgd 	if (strlen(line) == 0) {
    297   1.1       cgd 		oflush();
    298   1.1       cgd 		putchar('\n');
    299   1.1       cgd 		return;
    300   1.1       cgd 	}
    301   1.1       cgd 	for (cp = line; *cp == ' '; cp++)
    302   1.1       cgd 		;
    303   1.1       cgd 	np = cp - line;
    304   1.1       cgd 
    305   1.1       cgd 	/*
    306   1.1       cgd 	 * The following horrible expression attempts to avoid linebreaks
    307   1.1       cgd 	 * when the indent changes due to a paragraph.
    308   1.1       cgd 	 */
    309   1.1       cgd 	if (np != pfx && (np > pfx || abs(pfx-np) > 8))
    310   1.1       cgd 		oflush();
    311   1.6     lukem 	if ((h = ishead(cp)) != 0)
    312   1.1       cgd 		oflush(), mark = lineno;
    313   1.1       cgd 	if (lineno - mark < 3 && lineno - mark > 0)
    314   1.1       cgd 		for (hp = &headnames[0]; *hp != (char *) 0; hp++)
    315   1.1       cgd 			if (ispref(*hp, cp)) {
    316   1.1       cgd 				h = 1;
    317   1.1       cgd 				oflush();
    318   1.1       cgd 				break;
    319   1.1       cgd 			}
    320   1.1       cgd 	if (!h && (h = (*cp == '.')))
    321   1.1       cgd 		oflush();
    322   1.1       cgd 	pfx = np;
    323  1.10  jdolecek 	if (h) {
    324   1.4       jtc 		pack(cp, strlen(cp));
    325   1.1       cgd 		oflush();
    326  1.10  jdolecek 	} else
    327  1.10  jdolecek 		split(cp, add_space);
    328   1.1       cgd 	lineno++;
    329   1.1       cgd }
    330   1.1       cgd 
    331   1.1       cgd /*
    332   1.1       cgd  * Split up the passed line into output "words" which are
    333   1.1       cgd  * maximal strings of non-blanks with the blank separation
    334   1.1       cgd  * attached at the end.  Pass these words along to the output
    335   1.1       cgd  * line packer.
    336   1.1       cgd  */
    337  1.10  jdolecek static void
    338  1.16       wiz split(const char line[], int add_space)
    339   1.1       cgd {
    340  1.10  jdolecek 	const char *cp;
    341  1.10  jdolecek 	char *cp2;
    342   1.1       cgd 	char word[BUFSIZ];
    343   1.1       cgd 	int wordl;		/* LIZ@UOM 6/18/85 */
    344   1.1       cgd 
    345   1.1       cgd 	cp = line;
    346   1.1       cgd 	while (*cp) {
    347   1.1       cgd 		cp2 = word;
    348   1.1       cgd 		wordl = 0;	/* LIZ@UOM 6/18/85 */
    349   1.1       cgd 
    350   1.1       cgd 		/*
    351   1.1       cgd 		 * Collect a 'word,' allowing it to contain escaped white
    352   1.1       cgd 		 * space.
    353   1.1       cgd 		 */
    354   1.1       cgd 		while (*cp && *cp != ' ') {
    355   1.9  christos 			if (*cp == '\\' && isspace((unsigned char)cp[1]))
    356   1.1       cgd 				*cp2++ = *cp++;
    357   1.1       cgd 			*cp2++ = *cp++;
    358   1.1       cgd 			wordl++;/* LIZ@UOM 6/18/85 */
    359   1.1       cgd 		}
    360   1.1       cgd 
    361   1.1       cgd 		/*
    362   1.1       cgd 		 * Guarantee a space at end of line. Two spaces after end of
    363   1.1       cgd 		 * sentence punctuation.
    364   1.1       cgd 		 */
    365  1.10  jdolecek 		if (*cp == '\0' && add_space) {
    366   1.1       cgd 			*cp2++ = ' ';
    367   1.7     lukem 			if (strchr(".:!", cp[-1]))
    368   1.1       cgd 				*cp2++ = ' ';
    369   1.1       cgd 		}
    370   1.1       cgd 		while (*cp == ' ')
    371   1.1       cgd 			*cp2++ = *cp++;
    372   1.1       cgd 		*cp2 = '\0';
    373   1.1       cgd 		/*
    374   1.1       cgd 		 * LIZ@UOM 6/18/85 pack(word);
    375   1.1       cgd 		 */
    376   1.1       cgd 		pack(word, wordl);
    377   1.1       cgd 	}
    378   1.1       cgd }
    379   1.1       cgd 
    380   1.1       cgd /*
    381   1.1       cgd  * Output section.
    382   1.1       cgd  * Build up line images from the words passed in.  Prefix
    383   1.1       cgd  * each line with correct number of blanks.  The buffer "outbuf"
    384   1.1       cgd  * contains the current partial line image, including prefixed blanks.
    385   1.1       cgd  * "outp" points to the next available space therein.  When outp is NOSTR,
    386   1.1       cgd  * there ain't nothing in there yet.  At the bottom of this whole mess,
    387   1.1       cgd  * leading tabs are reinserted.
    388   1.1       cgd  */
    389   1.1       cgd char	outbuf[BUFSIZ];			/* Sandbagged output line image */
    390   1.1       cgd char	*outp;				/* Pointer in above */
    391   1.1       cgd 
    392   1.1       cgd /*
    393   1.1       cgd  * Initialize the output section.
    394   1.1       cgd  */
    395  1.10  jdolecek static void
    396  1.16       wiz setout(void)
    397   1.1       cgd {
    398   1.1       cgd 	outp = NOSTR;
    399   1.1       cgd }
    400   1.1       cgd 
    401   1.1       cgd /*
    402   1.1       cgd  * Pack a word onto the output line.  If this is the beginning of
    403   1.1       cgd  * the line, push on the appropriately-sized string of blanks first.
    404   1.1       cgd  * If the word won't fit on the current line, flush and begin a new
    405   1.1       cgd  * line.  If the word is too long to fit all by itself on a line,
    406   1.1       cgd  * just give it its own and hope for the best.
    407   1.1       cgd  *
    408   1.1       cgd  * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the
    409   1.1       cgd  *	goal length, take it.  If not, then check to see if the line
    410   1.1       cgd  *	will be over the max length; if so put the word on the next
    411   1.1       cgd  *	line.  If not, check to see if the line will be closer to the
    412   1.1       cgd  *	goal length with or without the word and take it or put it on
    413   1.1       cgd  *	the next line accordingly.
    414   1.1       cgd  */
    415   1.1       cgd 
    416   1.1       cgd /*
    417   1.1       cgd  * LIZ@UOM 6/18/85 -- pass in the length of the word as well
    418   1.1       cgd  * pack(word)
    419   1.1       cgd  *	char word[];
    420   1.1       cgd  */
    421  1.10  jdolecek static void
    422  1.16       wiz pack(const char word[], int wl)
    423   1.1       cgd {
    424  1.10  jdolecek 	const char *cp;
    425   1.6     lukem 	int s, t;
    426   1.1       cgd 
    427   1.1       cgd 	if (outp == NOSTR)
    428   1.1       cgd 		leadin();
    429   1.1       cgd 	/*
    430   1.1       cgd 	 * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the
    431   1.1       cgd 	 * length of the line before the word is added; t is now the length
    432   1.1       cgd 	 * of the line after the word is added
    433   1.1       cgd 	 *	t = strlen(word);
    434   1.1       cgd 	 *	if (t+s <= LENGTH)
    435   1.1       cgd 	 */
    436   1.1       cgd 	s = outp - outbuf;
    437   1.1       cgd 	t = wl + s;
    438   1.1       cgd 	if ((t <= goal_length) ||
    439   1.1       cgd 	    ((t <= max_length) && (t - goal_length <= goal_length - s))) {
    440   1.1       cgd 		/*
    441   1.1       cgd 		 * In like flint!
    442   1.1       cgd 		 */
    443   1.1       cgd 		for (cp = word; *cp; *outp++ = *cp++);
    444   1.1       cgd 		return;
    445   1.1       cgd 	}
    446   1.1       cgd 	if (s > pfx) {
    447   1.1       cgd 		oflush();
    448   1.1       cgd 		leadin();
    449   1.1       cgd 	}
    450   1.1       cgd 	for (cp = word; *cp; *outp++ = *cp++);
    451   1.1       cgd }
    452   1.1       cgd 
    453   1.1       cgd /*
    454   1.1       cgd  * If there is anything on the current output line, send it on
    455   1.1       cgd  * its way.  Set outp to NOSTR to indicate the absence of the current
    456   1.1       cgd  * line prefix.
    457   1.1       cgd  */
    458  1.10  jdolecek static void
    459  1.16       wiz oflush(void)
    460   1.1       cgd {
    461   1.1       cgd 	if (outp == NOSTR)
    462   1.1       cgd 		return;
    463   1.1       cgd 	*outp = '\0';
    464   1.1       cgd 	tabulate(outbuf);
    465   1.1       cgd 	outp = NOSTR;
    466   1.1       cgd }
    467   1.1       cgd 
    468   1.1       cgd /*
    469   1.1       cgd  * Take the passed line buffer, insert leading tabs where possible, and
    470   1.1       cgd  * output on standard output (finally).
    471   1.1       cgd  */
    472  1.10  jdolecek static void
    473  1.16       wiz tabulate(char line[])
    474   1.1       cgd {
    475   1.6     lukem 	char *cp;
    476   1.6     lukem 	int b, t;
    477   1.1       cgd 
    478   1.1       cgd 	/*
    479   1.1       cgd 	 * Toss trailing blanks in the output line.
    480   1.1       cgd 	 */
    481   1.1       cgd 	cp = line + strlen(line) - 1;
    482   1.1       cgd 	while (cp >= line && *cp == ' ')
    483   1.1       cgd 		cp--;
    484   1.1       cgd 	*++cp = '\0';
    485   1.1       cgd 
    486   1.1       cgd 	/*
    487   1.1       cgd 	 * Count the leading blank space and tabulate.
    488   1.1       cgd 	 */
    489   1.1       cgd 	for (cp = line; *cp == ' '; cp++)
    490   1.1       cgd 		;
    491   1.1       cgd 	b = cp-line;
    492   1.1       cgd 	t = b >> 3;
    493   1.1       cgd 	b &= 07;
    494   1.1       cgd 	if (t > 0)
    495   1.1       cgd 		do
    496   1.1       cgd 			putc('\t', stdout);
    497   1.1       cgd 		while (--t);
    498   1.1       cgd 	if (b > 0)
    499   1.1       cgd 		do
    500   1.1       cgd 			putc(' ', stdout);
    501   1.1       cgd 		while (--b);
    502   1.1       cgd 	while (*cp)
    503   1.1       cgd 		putc(*cp++, stdout);
    504   1.1       cgd 	putc('\n', stdout);
    505   1.1       cgd }
    506   1.1       cgd 
    507   1.1       cgd /*
    508   1.1       cgd  * Initialize the output line with the appropriate number of
    509   1.1       cgd  * leading blanks.
    510   1.1       cgd  */
    511  1.10  jdolecek static void
    512  1.16       wiz leadin(void)
    513   1.1       cgd {
    514   1.6     lukem 	int b;
    515   1.6     lukem 	char *cp;
    516   1.1       cgd 
    517   1.1       cgd 	for (b = 0, cp = outbuf; b < pfx; b++)
    518   1.1       cgd 		*cp++ = ' ';
    519   1.1       cgd 	outp = cp;
    520   1.1       cgd }
    521   1.1       cgd 
    522   1.1       cgd /*
    523   1.1       cgd  * Is s1 a prefix of s2??
    524   1.1       cgd  */
    525  1.10  jdolecek static int
    526  1.16       wiz ispref(const char *s1, const char *s2)
    527   1.1       cgd {
    528   1.1       cgd 
    529   1.1       cgd 	while (*s1++ == *s2)
    530   1.1       cgd 		;
    531   1.1       cgd 	return (*s1 == '\0');
    532   1.1       cgd }
    533