diff options
-rw-r--r-- | usr.bin/fmt/Makefile | 6 | ||||
-rw-r--r-- | usr.bin/fmt/fmt.1 | 119 | ||||
-rw-r--r-- | usr.bin/fmt/fmt.c | 1087 |
3 files changed, 660 insertions, 552 deletions
diff --git a/usr.bin/fmt/Makefile b/usr.bin/fmt/Makefile index 7a51add..c635cb6 100644 --- a/usr.bin/fmt/Makefile +++ b/usr.bin/fmt/Makefile @@ -1,7 +1,9 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 +# $FreeBSD$ PROG= fmt -SRCS= fmt.c head.c -.PATH: ${.CURDIR}/../mail +SRCS= fmt.c + +WARNS= 2 .include <bsd.prog.mk> diff --git a/usr.bin/fmt/fmt.1 b/usr.bin/fmt/fmt.1 index e293738..63d89f7 100644 --- a/usr.bin/fmt/fmt.1 +++ b/usr.bin/fmt/fmt.1 @@ -32,46 +32,105 @@ .\" @(#)fmt.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" -.Dd June 6, 1993 +.\" Modified by Gareth McCaughan to describe the new version of `fmt' +.\" rather than the old one. +.Dd June 25, 2000 .Dt FMT 1 .Os .Sh NAME .Nm fmt .Nd simple text formatter .Sh SYNOPSIS -.Nm -.Op Fl c -.Oo -.Ar goal -.Op Ar maximum -.Oc -.Op name ... +.Nm fmt +.Op Fl cmps +.Op Fl d Ar chars +.Op Fl l Ar num +.Op Fl t Ar num +.Op Ar goal Oo Ar maximum Oc | Fl Ns Ar width | Fl w Ar width +.Op Ar .Sh DESCRIPTION -.Nm Fmt +.Nm is a simple text formatter which reads the concatenation of input files (or standard input if none are given) and produces on standard output a version of its input with lines as close to the .Ar goal length -as possible without exceeding the maximum. The +as possible without exceeding the +.Ar maximum . +The .Ar goal length defaults -to 65 and the maximum to 75. The spacing at the beginning of the -input lines is preserved in the output, as are blank lines and -interword spacing. +to 65 and the +.Ar maximum +to 10 more than the +.Ar goal +length. +Alternatively, a single +.Ar width +parameter can be specified either by prepending a hyphen to it or by using +.Fl w . +For example, +.Dq Li fmt -w 72 , +.Dq Li fmt -72 , +and +.Dq Li fmt 72 72 +all produce identical output. +The spacing at the beginning of the input lines is preserved in the output, +as are blank lines and interword spacing. +Lines are joined or split only at white space; that is, words are never +joined or hyphenated. .Pp -.Fl c -instructs -.Nm -to center the text. +The options are as follows: +.Bl -tag -width indent +.It Fl c +Center the text, line by line. +In this case, most of the other +options are ignored; no splitting or joining of lines is done. +.It Fl m +Try to format mail header lines contained in the input sensibly. +.It Fl p +Allow indented paragraphs. +Without the +.Fl p +flag, any change in the amount of whitespace at the start of a line +results in a new paragraph being begun. +.It Fl s +Collapse whitespace inside lines, so that multiple whitespace +characters are turned into a single space. +(Or, at the end of a +sentence, a double space.) +.It Fl d Ar chars +Treat the +.Ar chars +(and no others) as sentence-ending characters. +By default the +sentence-ending characters are full stop +.Pq Ql \&. , +question mark +.Pq Ql \&? +and exclamation mark +.Pq Ql \&! . +Remember that some characters may need to be +escaped to protect them from your shell. +.It Fl l Ar number +Replace multiple spaces with tabs at the start of each output +line, if possible. +.Ar number +spaces will be replaced with one tab. +.It Fl t Ar number +Assume that the input files' tabs assume +.Ar number +spaces per tab stop. +The default is 8. +.El .Pp -.Nm Fmt +.Nm is meant to format mail messages prior to sending, but may also be useful for other simple tasks. For instance, within visual mode of the .Xr ex 1 -editor (e.g. +editor (e.g., .Xr vi 1 ) the command .Pp @@ -87,10 +146,24 @@ The .Nm command appeared in .Bx 3 . -.\" .Sh AUTHOR -.\" Kurt Shoens -.\" .br -.\" Liz Allen (added goal length concept) +.Pp +The version described herein is a complete rewrite and appeared in +.Fx 4.4 . +.Sh AUTHORS +.An Kurt Shoens +.An Liz Allen +(added +.Ar goal +length concept) +.An Gareth McCaughan .Sh BUGS The program was designed to be simple and fast \- for more complex operations, the standard text processors are likely to be more appropriate. +.Pp +When the first line of an indented paragraph is very long (more than +about twice the goal length), the indentation in the output can be +wrong. +.Pp +.Nm +is not infallible in guessing what lines are mail headers and what +lines are not. diff --git a/usr.bin/fmt/fmt.c b/usr.bin/fmt/fmt.c index 94dd8c6..d41d745 100644 --- a/usr.bin/fmt/fmt.c +++ b/usr.bin/fmt/fmt.c @@ -1,49 +1,176 @@ -/* - * Copyright (c) 1980, 1993 - * The Regents of the University of California. All rights reserved. +/* $OpenBSD: fmt.c,v 1.16 2000/06/25 15:35:42 pjanzen Exp $ */ + +/* Sensible version of fmt + * + * Syntax: fmt [ options ] [ goal [ max ] ] [ filename ... ] + * + * Since the documentation for the original fmt is so poor, here + * is an accurate description of what this one does. It's usually + * the same. The *mechanism* used may differ from that suggested + * here. Note that we are *not* entirely compatible with fmt, + * because fmt gets so many things wrong. + * + * 1. Tabs are expanded, assuming 8-space tab stops. + * If the `-t <n>' option is given, we assume <n>-space + * tab stops instead. + * Trailing blanks are removed from all lines. + * x\b == nothing, for any x other than \b. + * Other control characters are simply stripped. This + * includes \r. + * 2. Each line is split into leading whitespace and + * everything else. Maximal consecutive sequences of + * lines with the same leading whitespace are considered + * to form paragraphs, except that a blank line is always + * a paragraph to itself. + * If the `-p' option is given then the first line of a + * paragraph is permitted to have indentation different + * from that of the other lines. + * If the `-m' option is given then a line that looks + * like a mail message header, if it is not immediately + * preceded by a non-blank non-message-header line, is + * taken to start a new paragraph, which also contains + * any subsequent lines with non-empty leading whitespace. + * 3. The "everything else" is split into words; a word + * includes its trailing whitespace, and a word at the + * end of a line is deemed to be followed by a single + * space, or two spaces if it ends with a sentence-end + * character. (See the `-d' option for how to change that.) + * If the `-s' option has been given, then a word's trailing + * whitespace is replaced by what it would have had if it + * had occurred at end of line. + * 4. Each paragraph is sent to standard output as follows. + * We output the leading whitespace, and then enough words + * to make the line length as near as possible to the goal + * without exceeding the maximum. (If a single word would + * exceed the maximum, we output that anyway.) Of course + * the trailing whitespace of the last word is ignored. + * We then emit a newline and start again if there are any + * words left. + * Note that for a blank line this translates as "We emit + * a newline". + * If the `-l <n>' option is given, then leading whitespace + * is modified slightly: <n> spaces are replaced by a tab. + * Indented paragraphs (see above under `-p') make matters + * more complicated than this suggests. Actually every paragraph + * has two `leading whitespace' values; the value for the first + * line, and the value for the most recent line. (While processing + * the first line, the two are equal. When `-p' has not been + * given, they are always equal.) The leading whitespace + * actually output is that of the first line (for the first + * line of *output*) or that of the most recent line (for + * all other lines of output). + * When `-m' has been given, message header paragraphs are + * taken as having first-leading-whitespace empty and + * subsequent-leading-whitespace two spaces. + * + * Multiple input files are formatted one at a time, so that a file + * never ends in the middle of a line. + * + * There's an alternative mode of operation, invoked by giving + * the `-c' option. In that case we just center every line, + * and most of the other options are ignored. This should + * really be in a separate program, but we must stay compatible + * with old `fmt'. + * + * QUERY: Should `-m' also try to do the right thing with quoted text? + * QUERY: `-b' to treat backslashed whitespace as old `fmt' does? + * QUERY: Option meaning `never join lines'? + * QUERY: Option meaning `split in mid-word to avoid overlong lines'? + * (Those last two might not be useful, since we have `fold'.) + * + * Differences from old `fmt': + * + * - We have many more options. Options that aren't understood + * generate a lengthy usage message, rather than being + * treated as filenames. + * - Even with `-m', our handling of message headers is + * significantly different. (And much better.) + * - We don't treat `\ ' as non-word-breaking. + * - Downward changes of indentation start new paragraphs + * for us, as well as upward. (I think old `fmt' behaves + * in the way it does in order to allow indented paragraphs, + * but this is a broken way of making indented paragraphs + * behave right.) + * - Given the choice of going over or under |goal_length| + * by the same amount, we go over; old `fmt' goes under. + * - We treat `?' as ending a sentence, and not `:'. Old `fmt' + * does the reverse. + * - We return approved return codes. Old `fmt' returns + * 1 for some errors, and *the number of unopenable files* + * when that was all that went wrong. + * - We have fewer crashes and more helpful error messages. + * - We don't turn spaces into tabs at starts of lines unless + * specifically requested. + * - New `fmt' is somewhat smaller and slightly faster than + * old `fmt'. + * + * Bugs: * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright + * None known. There probably are some, though. + * + * Portability: + * + * I believe this code to be pretty portable. It does require + * that you have `getopt'. If you need to include "getopt.h" + * for this (e.g., if your system didn't come with `getopt' + * and you installed it yourself) then you should arrange for + * NEED_getopt_h to be #defined. + * + * Everything here should work OK even on nasty 16-bit + * machines and nice 64-bit ones. However, it's only really + * been tested on my FreeBSD machine. Your mileage may vary. + */ + +/* Copyright (c) 1997 Gareth McCaughan. All rights reserved. + * + * Redistribution and use of this code, in source or binary forms, + * with or without modification, are permitted subject to the following + * conditions: + * + * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * - If you distribute modified source code it must also include + * a notice saying that it has been modified, and giving a brief + * description of what changes have been made. + * + * Disclaimer: I am not responsible for the results of using this code. + * If it formats your hard disc, sends obscene messages to + * your boss and kills your children then that's your problem + * not mine. I give absolutely no warranty of any sort as to + * what the program will do, and absolutely refuse to be held + * liable for any consequences of your using it. + * Thank you. Have a nice day. */ -#ifndef lint -static char copyright[] = -"@(#) Copyright (c) 1980, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ +/* RCS change log: + * Revision 1.5 1998/03/02 18:02:21 gjm11 + * Minor changes for portability. + * + * Revision 1.4 1997/10/01 11:51:28 gjm11 + * Repair broken indented-paragraph handling. + * Add mail message header stuff. + * Improve comments and layout. + * Make usable with non-BSD systems. + * Add revision display to usage message. + * + * Revision 1.3 1997/09/30 16:24:47 gjm11 + * Add copyright notice, rcsid string and log message. + * + * Revision 1.2 1997/09/30 16:13:39 gjm11 + * Add options: -d <chars>, -l <width>, -p, -s, -t <width>, -h . + * Parse options with `getopt'. Clean up code generally. + * Make comments more accurate. + * + * Revision 1.1 1997/09/30 11:29:57 gjm11 + * Initial revision + */ #ifndef lint -#if 0 -static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93"; -#else static const char rcsid[] = "$FreeBSD$"; -#endif +static const char copyright[] = + "Copyright (c) 1997 Gareth McCaughan. All rights reserved.\n"; #endif /* not lint */ #include <ctype.h> @@ -52,530 +179,436 @@ static const char rcsid[] = #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sysexits.h> +#include <unistd.h> -/* - * fmt -- format the concatenation of input files or standard input - * onto standard output. Designed for use with Mail ~| - * - * Syntax : fmt [ goal [ max ] ] [ name ... ] - * Authors: Kurt Shoens (UCB) 12/7/78; - * Liz Allen (UMCP) 2/24/83 [Addition of goal length concept]. +/* Something that, we hope, will never be a genuine line length, + * indentation etc. */ +#define SILLY ((size_t)-1) -/* LIZ@UOM 6/18/85 -- Don't need LENGTH any more. - * #define LENGTH 72 Max line length in output - */ -#define NOSTR ((char *) 0) /* Null string pointer for lint */ - -/* LIZ@UOM 6/18/85 --New variables goal_length and max_length */ -#define GOAL_LENGTH 65 -#define MAX_LENGTH 75 -int goal_length; /* Target or goal line length in output */ -int max_length; /* Max line length in output */ -int pfx; /* Current leading blank count */ -int lineno; /* Current input line */ -int mark; /* Last place we saw a head line */ -int center; - -char *headnames[] = {"To", "Subject", "Cc", 0}; - -void fmt __P((FILE *)); -int ispref __P((char *, char *)); -void leadin __P((void)); -void oflush __P((void)); -void pack __P((char [], int)); -void prefix __P((char [])); -void setout __P((void)); -void split __P((char [])); -void tabulate __P((char [])); - -/* - * Drive the whole formatter by managing input files. Also, - * cause initialization of the output stuff and flush it out - * at the end. +/* I used to use |strtoul| for this, but (1) not all systems have it + * and (2) it's probably better to use |strtol| to detect negative + * numbers better. + * If |fussyp==0| then we don't complain about non-numbers + * (returning 0 instead), but we do complain about bad numbers. */ - -int -main(argc, argv) - int argc; - char **argv; -{ - register FILE *fi; - register int errs = 0; - int number; /* LIZ@UOM 6/18/85 */ - - (void) setlocale(LC_CTYPE, ""); - - goal_length = GOAL_LENGTH; - max_length = MAX_LENGTH; - setout(); - lineno = 1; - mark = -10; - /* - * LIZ@UOM 6/18/85 -- Check for goal and max length arguments - */ - if (argc > 1 && !strcmp(argv[1], "-c")) { - center++; - argc--; - argv++; - } - if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) { - argv++; - argc--; - goal_length = number; - if (argc > 1 && (1 == (sscanf(argv[1], "%d", &number)))) { - argv++; - argc--; - max_length = number; - } - } - if (max_length <= goal_length) - errx(1, "max length must be greater than goal length"); - if (argc < 2) { - fmt(stdin); - oflush(); - exit(0); - } - while (--argc) { - if ((fi = fopen(*++argv, "r")) == NULL) { - perror(*argv); - errs++; - continue; - } - fmt(fi); - fclose(fi); - } - oflush(); - exit(errs); +static size_t +get_positive(const char *s, const char *err_mess, int fussyP) { + char *t; + long result = strtol(s,&t,0); + if (*t) { if (fussyP) goto Lose; else return 0; } + if (result<=0) { Lose: errx(EX_USAGE, err_mess); } + return (size_t) result; } -/* - * Read up characters from the passed input file, forming lines, - * doing ^H processing, expanding tabs, stripping trailing blanks, - * and sending each line down for analysis. +/* Global variables */ + +static int centerP=0; /* Try to center lines? */ +static size_t goal_length=0; /* Target length for output lines */ +static size_t max_length=0; /* Maximum length for output lines */ +static int coalesce_spaces_P=0; /* Coalesce multiple whitespace -> ' ' ? */ +static int allow_indented_paragraphs=0; /* Can first line have diff. ind.? */ +static int tab_width=8; /* Number of spaces per tab stop */ +static size_t output_tab_width=0; /* Ditto, when squashing leading spaces */ +static const char *sentence_enders=".?!"; /* Double-space after these */ +static int grok_mail_headers=0; /* treat embedded mail headers magically? */ + +static int n_errors=0; /* Number of failed files. Return on exit. */ +static char *output_buffer=0; /* Output line will be built here */ +static size_t x; /* Horizontal position in output line */ +static size_t x0; /* Ditto, ignoring leading whitespace */ +static size_t pending_spaces; /* Spaces to add before next word */ +static int output_in_paragraph=0; /* Any of current para written out yet? */ + +/* Prototypes */ + +static void process_named_file (const char *); +static void process_stream (FILE *, const char *); +static size_t indent_length (const char *, size_t); +static int might_be_header (const unsigned char *); +static void new_paragraph (size_t, size_t); +static void output_word (size_t, size_t, const char *, size_t, size_t); +static void output_indent (size_t); +static void center_stream (FILE *, const char *); +static char * get_line (FILE *, size_t *); +static void * xrealloc (void *, size_t); + +#define XMALLOC(x) xrealloc(0,x) + +/* Here is perhaps the right place to mention that this code is + * all in top-down order. Hence, |main| comes first. */ -void -fmt(fi) - FILE *fi; -{ - static char *linebuf = 0, *canonb = 0; - register char *cp, *cp2, cc; - register int c, col; -#define CHUNKSIZE 1024 - static int lbufsize = 0, cbufsize = CHUNKSIZE; - - canonb = malloc(CHUNKSIZE); - if (canonb == 0) - abort(); - - if (center) { - linebuf = malloc(BUFSIZ); - while (1) { - cp = fgets(linebuf, BUFSIZ, fi); - if (!cp) - return; - while (*cp && isspace(*cp)) - cp++; - cp2 = cp + strlen(cp) - 1; - while (cp2 > cp && isspace(*cp2)) - cp2--; - if (cp == cp2) - putchar('\n'); - col = cp2 - cp; - for (c = 0; c < (goal_length-col)/2; c++) - putchar(' '); - while (cp <= cp2) - putchar(*cp++); - putchar('\n'); - } - } - c = getc(fi); - while (c != EOF) { - /* - * Collect a line, doing ^H processing. - * Leave tabs for now. - */ - cp = linebuf; - while (c != '\n' && c != EOF) { - if (cp - linebuf >= lbufsize) { - int offset = cp - linebuf; - lbufsize += CHUNKSIZE; - linebuf = realloc(linebuf, lbufsize); - if(linebuf == 0) - abort(); - cp = linebuf + offset; - } - if (c == '\b') { - if (cp > linebuf) - cp--; - c = getc(fi); - continue; - } - if (!isprint(c) && c != '\t') { - c = getc(fi); - continue; - } - *cp++ = c; - c = getc(fi); - } - - /* - * Toss anything remaining on the input line. - */ - while (c != '\n' && c != EOF) - c = getc(fi); - - if (cp != NULL) { - *cp = '\0'; - } else { - putchar('\n'); - c = getc(fi); - continue; - } - - /* - * Expand tabs on the way to canonb. - */ - col = 0; - cp = linebuf; - cp2 = canonb; - while ((cc = *cp++)) { - if (cc != '\t') { - col++; - if (cp2 - canonb >= cbufsize) { - int offset = cp2 - canonb; - cbufsize += CHUNKSIZE; - canonb = realloc(canonb, cbufsize); - if(canonb == 0) - abort(); - cp2 = canonb + offset; - } - *cp2++ = cc; - continue; - } - do { - if (cp2 - canonb >= cbufsize) { - int offset = cp2 - canonb; - cbufsize += CHUNKSIZE; - canonb = realloc(canonb, cbufsize); - if(canonb == 0) - abort(); - cp2 = canonb + offset; - } - *cp2++ = ' '; - col++; - } while ((col & 07) != 0); - } - - /* - * Swipe trailing blanks from the line. - */ - for (cp2--; cp2 >= canonb && *cp2 == ' '; cp2--) - ; - *++cp2 = '\0'; - prefix(canonb); - if (c != EOF) - c = getc(fi); - } -} +int +main(int argc, char *argv[]) { + int ch; /* used for |getopt| processing */ + + + (void) setlocale(LC_CTYPE, ""); + + /* 1. Grok parameters. */ + + while ((ch = getopt(argc, argv, "0123456789cd:hl:mpst:w:")) != -1) + switch(ch) { + case 'c': + centerP = 1; + continue; + case 'd': + sentence_enders = optarg; + continue; + case 'l': + output_tab_width + = get_positive(optarg, "output tab width must be positive", 1); + continue; + case 'm': + grok_mail_headers = 1; + continue; + case 'p': + allow_indented_paragraphs = 1; + continue; + case 's': + coalesce_spaces_P = 1; + continue; + case 't': + tab_width = get_positive(optarg, "tab width must be positive", 1); + continue; + case 'w': + goal_length = get_positive(optarg, "width must be positive", 1); + max_length = goal_length; + continue; + case '0': case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '8': case '9': + /* XXX this is not a stylistically approved use of getopt() */ + if (goal_length==0) { + char *p; + p = argv[optind - 1]; + if (p[0] == '-' && p[1] == ch && !p[2]) + goal_length = get_positive(++p, "width must be nonzero", 1); + else + goal_length = get_positive(argv[optind]+1, + "width must be nonzero", 1); + max_length = goal_length; + } + continue; + case 'h': default: + fprintf(stderr, +"Usage: fmt [-cmps] [-d chars] [-l num] [-t num]\n" +" [-w width | -width | goal [maximum]] [file ...]\n" +"Options: -c center each line instead of formatting\n" +" -d <chars> double-space after <chars> at line end\n" +" -l <n> turn each <n> spaces at start of line into a tab\n" +" -m try to make sure mail header lines stay separate\n" +" -p allow indented paragraphs\n" +" -s coalesce whitespace inside lines\n" +" -t <n> have tabs every <n> columns\n" +" -w <n> set maximum width to <n>\n" +" goal set target width to goal\n"); + exit(ch=='h' ? 0 : EX_USAGE); + } + argc -= optind; argv += optind; + + /* [ goal [ maximum ] ] */ + + if (argc>0 && goal_length==0 + && (goal_length=get_positive(*argv,"goal length must be positive", 0)) + != 0) { + --argc; ++argv; + if (argc>0 + && (max_length=get_positive(*argv,"max length must be positive", 0)) + != 0) { + --argc; ++argv; + if (max_length<goal_length) + errx(EX_USAGE, "max length must be >= goal length"); + } + } + if (goal_length==0) goal_length = 65; + if (max_length==0) max_length = goal_length+10; + output_buffer = XMALLOC(max_length+1); /* really needn't be longer */ + + /* 2. Process files. */ + + if (argc>0) { + while (argc-->0) process_named_file(*argv++); + } + else { + process_stream(stdin, "standard input"); + } + + /* We're done. */ + + return n_errors ? EX_NOINPUT : 0; -/* - * Take a line devoid of tabs and other garbage and determine its - * blank prefix. If the indent changes, call for a linebreak. - * If the input line is blank, echo the blank line on the output. - * Finally, if the line minus the prefix is a mail header, try to keep - * it on a line by itself. - */ -void -prefix(line) - char line[]; -{ - register char *cp, **hp; - register int np, h; - - if (!*line) { - oflush(); - putchar('\n'); - return; - } - for (cp = line; *cp == ' '; cp++) - ; - np = cp - line; - - /* - * The following horrible expression attempts to avoid linebreaks - * when the indent changes due to a paragraph. - */ - if (np != pfx && (np > pfx || abs(pfx-np) > 8)) - oflush(); - if ((h = ishead(cp))) - oflush(), mark = lineno; - if (lineno - mark < 3 && lineno - mark > 0) - for (hp = &headnames[0]; *hp != (char *) 0; hp++) - if (ispref(*hp, cp)) { - h = 1; - oflush(); - break; - } - if (!h && (h = (*cp == '.'))) - oflush(); - pfx = np; - if (h) - pack(cp, strlen(cp)); - else split(cp); - if (h) - oflush(); - lineno++; } -/* - * Split up the passed line into output "words" which are - * maximal strings of non-blanks with the blank separation - * attached at the end. Pass these words along to the output - * line packer. +/* Process a single file, given its name. */ -void -split(line) - char line[]; -{ - register char *cp, *cp2; - static char *word=0; - static int wordsize=0; - int wordl; /* LIZ@UOM 6/18/85 */ - - { - int l = strlen(line); - if (l >= wordsize) { - if (word) - free(word); - wordsize = (l+66)&~63; - word = malloc(wordsize); - if (word == NULL) - abort(); - } - } - - cp = line; - while (*cp) { - cp2 = word; - wordl = 0; /* LIZ@UOM 6/18/85 */ - - /* - * Collect a 'word,' allowing it to contain escaped white - * space. - */ - while (*cp && *cp != ' ') { - if (*cp == '\\' && isspace(cp[1])) - *cp2++ = *cp++; - *cp2++ = *cp++; - wordl++;/* LIZ@UOM 6/18/85 */ - } - - /* - * Guarantee a space at end of line. Two spaces after end of - * sentence punctuation. - */ - if (*cp == '\0') { - *cp2++ = ' '; - if (cp != line && index(".:!", cp[-1])) - *cp2++ = ' '; - } - while (*cp == ' ') - *cp2++ = *cp++; - *cp2 = '\0'; - /* - * LIZ@UOM 6/18/85 pack(word); - */ - pack(word, wordl); - } +static void +process_named_file(const char *name) { + FILE *f=fopen(name, "r"); + if (!f) { perror(name); ++n_errors; } + else { + process_stream(f, name); + fclose(f); + } } -/* - * Output section. - * Build up line images from the words passed in. Prefix - * each line with correct number of blanks. The buffer "outbuf" - * contains the current partial line image, including prefixed blanks. - * "outp" points to the next available space therein. When outp is NOSTR, - * there ain't nothing in there yet. At the bottom of this whole mess, - * leading tabs are reinserted. +/* Types of mail header continuation lines: */ -char *outbuf; /* Sandbagged output line image */ -char *outp; /* Pointer in above */ -int outbuf_size; /* er, size of outbuf */ - -/* - * Initialize the output section. +typedef enum { + hdr_ParagraphStart = -1, + hdr_NonHeader = 0, + hdr_Header = 1, + hdr_Continuation = 2 +} HdrType; + +/* Process a stream. This is where the real work happens, + * except that centering is handled separately. */ -void -setout() -{ - outbuf = malloc(BUFSIZ); - if (outbuf == 0) - abort(); - outbuf_size = BUFSIZ; - outp = NOSTR; +static void +process_stream(FILE *stream, const char *name) { + size_t last_indent=SILLY; /* how many spaces in last indent? */ + size_t para_line_number=0; /* how many lines already read in this para? */ + size_t first_indent=SILLY; /* indentation of line 0 of paragraph */ + HdrType prev_header_type=hdr_ParagraphStart; + /* ^-- header_type of previous line; -1 at para start */ + char *line; + size_t length; + + if (centerP) { center_stream(stream, name); return; } + while ((line=get_line(stream,&length)) != NULL) { + size_t np=indent_length(line, length); + { HdrType header_type=hdr_NonHeader; + if (grok_mail_headers && prev_header_type!=hdr_NonHeader) { + if (np==0 && might_be_header(line)) + header_type = hdr_Header; + else if (np>0 && prev_header_type>hdr_NonHeader) + header_type = hdr_Continuation; + } + /* We need a new paragraph if and only if: + * this line is blank, + * OR it's a mail header, + * OR it's not a mail header AND the last line was one, + * OR the indentation has changed + * AND the line isn't a mail header continuation line + * AND this isn't the second line of an indented paragraph. + */ + if ( length==0 + || header_type==hdr_Header + || (header_type==hdr_NonHeader && prev_header_type>hdr_NonHeader) + || (np!=last_indent + && header_type != hdr_Continuation + && (!allow_indented_paragraphs || para_line_number != 1)) ) { + new_paragraph(output_in_paragraph ? last_indent : first_indent, np); + para_line_number = 0; + first_indent = np; + last_indent = np; + if (header_type==hdr_Header) last_indent=2; /* for cont. lines */ + if (length==0) { + putchar('\n'); + prev_header_type=hdr_ParagraphStart; + continue; + } + } + else { + /* If this is an indented paragraph other than a mail header + * continuation, set |last_indent|. + */ + if (np != last_indent && header_type != hdr_Continuation) + last_indent=np; + } + prev_header_type = header_type; + } + + { size_t n=np; + while (n<length) { + /* Find word end and count spaces after it */ + size_t word_length=0, space_length=0; + while (n+word_length < length && line[n+word_length] != ' ') + ++word_length; + space_length = word_length; + while (n+space_length < length && line[n+space_length] == ' ') + ++space_length; + /* Send the word to the output machinery. */ + output_word(first_indent, last_indent, + line+n, word_length, space_length-word_length); + n += space_length; + } + } + ++para_line_number; + } + new_paragraph(output_in_paragraph ? last_indent : first_indent, 0); + if (ferror(stream)) { perror(name); ++n_errors; } } -/* - * Pack a word onto the output line. If this is the beginning of - * the line, push on the appropriately-sized string of blanks first. - * If the word won't fit on the current line, flush and begin a new - * line. If the word is too long to fit all by itself on a line, - * just give it its own and hope for the best. - * - * LIZ@UOM 6/18/85 -- If the new word will fit in at less than the - * goal length, take it. If not, then check to see if the line - * will be over the max length; if so put the word on the next - * line. If not, check to see if the line will be closer to the - * goal length with or without the word and take it or put it on - * the next line accordingly. +/* How long is the indent on this line? */ +static size_t +indent_length(const char *line, size_t length) { + size_t n=0; + while (n<length && *line++ == ' ') ++n; + return n; +} -/* - * LIZ@UOM 6/18/85 -- pass in the length of the word as well - * pack(word) - * char word[]; +/* Might this line be a mail header? + * We deem a line to be a possible header if it matches the + * Perl regexp /^[A-Z][-A-Za-z0-9]*:\s/. This is *not* the same + * as in RFC whatever-number-it-is; we want to be gratuitously + * conservative to avoid mangling ordinary civilised text. */ -void -pack(word,wl) - char word[]; - int wl; -{ - register char *cp; - register int s, t; - - if (((outp==NOSTR) ? wl : outp-outbuf + wl) >= outbuf_size) { - char *old_outbuf = outbuf; - outbuf_size *= 2; - outbuf = realloc(outbuf, outbuf_size); - if (outbuf == 0) - abort(); - outp += outbuf-old_outbuf; - } - - if (outp == NOSTR) - leadin(); - /* - * LIZ@UOM 6/18/85 -- change condition to check goal_length; s is the - * length of the line before the word is added; t is now the length - * of the line after the word is added - * t = strlen(word); - * if (t+s <= LENGTH) - */ - s = outp - outbuf; - t = wl + s; - if ((t <= goal_length) || - ((t <= max_length) && (t - goal_length <= goal_length - s))) { - /* - * In like flint! - */ - for (cp = word; *cp; *outp++ = *cp++); - return; - } - if (s > pfx) { - oflush(); - leadin(); - } - for (cp = word; *cp; *outp++ = *cp++); +static int +might_be_header(const unsigned char *line) { + if (!isupper(*line++)) return 0; + while (*line && (isalnum(*line) || *line=='-')) ++line; + return (*line==':' && isspace(line[1])); } -/* - * If there is anything on the current output line, send it on - * its way. Set outp to NOSTR to indicate the absence of the current - * line prefix. +/* Begin a new paragraph with an indent of |indent| spaces. */ -void -oflush() -{ - if (outp == NOSTR) - return; - *outp = '\0'; - tabulate(outbuf); - outp = NOSTR; +static void +new_paragraph(size_t old_indent, size_t indent) { + if (x0) { + if (old_indent>0) output_indent(old_indent); + fwrite(output_buffer, 1, x0, stdout); + putchar('\n'); + } + x=indent; x0=0; pending_spaces=0; + output_in_paragraph = 0; } -/* - * Take the passed line buffer, insert leading tabs where possible, and - * output on standard output (finally). +/* Output spaces or tabs for leading indentation. */ -void -tabulate(line) - char line[]; -{ - register char *cp; - register int b, t; - - /* - * Toss trailing blanks in the output line. - */ - cp = line + strlen(line) - 1; - while (cp >= line && *cp == ' ') - cp--; - *++cp = '\0'; - - /* - * Count the leading blank space and tabulate. - */ - for (cp = line; *cp == ' '; cp++) - ; - b = cp-line; - t = b >> 3; - b &= 07; - if (t > 0) - do - putc('\t', stdout); - while (--t); - if (b > 0) - do - putc(' ', stdout); - while (--b); - while (*cp) - putc(*cp++, stdout); - putc('\n', stdout); +static void +output_indent(size_t n_spaces) { + if (output_tab_width) { + while (n_spaces >= output_tab_width) { + putchar('\t'); + n_spaces -= output_tab_width; + } + } + while (n_spaces-- > 0) putchar(' '); } -/* - * Initialize the output line with the appropriate number of - * leading blanks. +/* Output a single word, or add it to the buffer. + * indent0 and indent1 are the indents to use on the first and subsequent + * lines of a paragraph. They'll often be the same, of course. */ -void -leadin() -{ - register int b; - register char *cp; - - for (b = 0, cp = outbuf; b < pfx; b++) - *cp++ = ' '; - outp = cp; +static void +output_word(size_t indent0, size_t indent1, const char *word, size_t length, size_t spaces) { + size_t new_x = x+pending_spaces+length; + size_t indent = output_in_paragraph ? indent1 : indent0; + + /* If either |spaces==0| (at end of line) or |coalesce_spaces_P| + * (squashing internal whitespace), then add just one space; + * except that if the last character was a sentence-ender we + * actually add two spaces. + */ + if (coalesce_spaces_P || spaces==0) + spaces = strchr(sentence_enders, word[length-1]) ? 2 : 1; + + if (new_x<=goal_length) { + /* After adding the word we still aren't at the goal length, + * so clearly we add it to the buffer rather than outputing it. + */ + memset(output_buffer+x0, ' ', pending_spaces); + x0 += pending_spaces; x += pending_spaces; + memcpy(output_buffer+x0, word, length); + x0 += length; x += length; + pending_spaces = spaces; + } + else { + /* Adding the word takes us past the goal. Print the line-so-far, + * and the word too iff either (1) the lsf is empty or (2) that + * makes us nearer the goal but doesn't take us over the limit, + * or (3) the word on its own takes us over the limit. + * In case (3) we put a newline in between. + */ + if (indent>0) output_indent(indent); + fwrite(output_buffer, 1, x0, stdout); + if (x0==0 || (new_x <= max_length && new_x-goal_length <= goal_length-x)) { + printf("%*s", pending_spaces, ""); + goto write_out_word; + } + else { + /* If the word takes us over the limit on its own, just + * spit it out and don't bother buffering it. + */ + if (indent+length > max_length) { + putchar('\n'); + if (indent>0) output_indent(indent); +write_out_word: + fwrite(word, 1, length, stdout); + x0 = 0; x = indent1; pending_spaces = 0; + } + else { + memcpy(output_buffer, word, length); + x0 = length; x = length+indent1; pending_spaces = spaces; + } + } + putchar('\n'); + output_in_paragraph = 1; + } } -/* - * Save a string in dynamic space. - * This little goodie is needed for - * a headline detector in head.c +/* Process a stream, but just center its lines rather than trying to + * format them neatly. */ -char * -savestr(str) - char str[]; -{ - register char *top; - - top = malloc(strlen(str) + 1); - if (top == NOSTR) - errx(1, "ran out of memory"); - strcpy(top, str); - return (top); +static void +center_stream(FILE *stream, const char *name) { + char *line; + size_t length; + while ((line=get_line(stream, &length)) != 0) { + size_t l=length; + while (l>0 && isspace(*line)) { ++line; --l; } + length=l; + while (l<goal_length) { putchar(' '); l+=2; } + fwrite(line, 1, length, stdout); + putchar('\n'); + } + if (ferror(stream)) { perror(name); ++n_errors; } } -/* - * Is s1 a prefix of s2?? +/* Get a single line from a stream. Expand tabs, strip control + * characters and trailing whitespace, and handle backspaces. + * Return the address of the buffer containing the line, and + * put the length of the line in |lengthp|. + * This can cope with arbitrarily long lines, and with lines + * without terminating \n. + * If there are no characters left or an error happens, we + * return 0. + * Don't confuse |spaces_pending| here with the global + * |pending_spaces|. */ -int -ispref(s1, s2) - register char *s1, *s2; -{ +static char * +get_line(FILE *stream, size_t *lengthp) { + static char *buf=NULL; + static size_t length=0; + size_t len=0; + int ch; + size_t spaces_pending=0; + + if (buf==NULL) { length=100; buf=XMALLOC(length); } + while ((ch=getc(stream)) != '\n' && ch != EOF) { + if (ch==' ') ++spaces_pending; + else if (isprint(ch)) { + while (len+spaces_pending >= length) { + length*=2; buf=xrealloc(buf, length); + } + while (spaces_pending > 0) { --spaces_pending; buf[len++]=' '; } + buf[len++] = ch; + } + else if (ch=='\t') + spaces_pending += tab_width - (len+spaces_pending)%tab_width; + else if (ch=='\b') { if (len) --len; } + } + *lengthp=len; + return (len>0 || ch!=EOF) ? buf : 0; +} - while (*s1++ == *s2) - ; - return (*s1 == '\0'); +/* (Re)allocate some memory, exiting with an error if we can't. + */ +static void * +xrealloc(void *ptr, size_t nbytes) { + void *p = realloc(ptr, nbytes); + if (p == NULL) errx(EX_OSERR, "out of memory"); + return p; } |