fmt.c revision 81510
1/*	$OpenBSD: fmt.c,v 1.16 2000/06/25 15:35:42 pjanzen Exp $	*/
2
3/* Sensible version of fmt
4 *
5 * Syntax: fmt [ options ] [ goal [ max ] ] [ filename ... ]
6 *
7 * Since the documentation for the original fmt is so poor, here
8 * is an accurate description of what this one does. It's usually
9 * the same. The *mechanism* used may differ from that suggested
10 * here. Note that we are *not* entirely compatible with fmt,
11 * because fmt gets so many things wrong.
12 *
13 * 1. Tabs are expanded, assuming 8-space tab stops.
14 *    If the `-t <n>' option is given, we assume <n>-space
15 *    tab stops instead.
16 *    Trailing blanks are removed from all lines.
17 *    x\b == nothing, for any x other than \b.
18 *    Other control characters are simply stripped. This
19 *    includes \r.
20 * 2. Each line is split into leading whitespace and
21 *    everything else. Maximal consecutive sequences of
22 *    lines with the same leading whitespace are considered
23 *    to form paragraphs, except that a blank line is always
24 *    a paragraph to itself.
25 *    If the `-p' option is given then the first line of a
26 *    paragraph is permitted to have indentation different
27 *    from that of the other lines.
28 *    If the `-m' option is given then a line that looks
29 *    like a mail message header, if it is not immediately
30 *    preceded by a non-blank non-message-header line, is
31 *    taken to start a new paragraph, which also contains
32 *    any subsequent lines with non-empty leading whitespace.
33 * 3. The "everything else" is split into words; a word
34 *    includes its trailing whitespace, and a word at the
35 *    end of a line is deemed to be followed by a single
36 *    space, or two spaces if it ends with a sentence-end
37 *    character. (See the `-d' option for how to change that.)
38 *    If the `-s' option has been given, then a word's trailing
39 *    whitespace is replaced by what it would have had if it
40 *    had occurred at end of line.
41 * 4. Each paragraph is sent to standard output as follows.
42 *    We output the leading whitespace, and then enough words
43 *    to make the line length as near as possible to the goal
44 *    without exceeding the maximum. (If a single word would
45 *    exceed the maximum, we output that anyway.) Of course
46 *    the trailing whitespace of the last word is ignored.
47 *    We then emit a newline and start again if there are any
48 *    words left.
49 *    Note that for a blank line this translates as "We emit
50 *    a newline".
51 *    If the `-l <n>' option is given, then leading whitespace
52 *    is modified slightly: <n> spaces are replaced by a tab.
53 *    Indented paragraphs (see above under `-p') make matters
54 *    more complicated than this suggests. Actually every paragraph
55 *    has two `leading whitespace' values; the value for the first
56 *    line, and the value for the most recent line. (While processing
57 *    the first line, the two are equal. When `-p' has not been
58 *    given, they are always equal.) The leading whitespace
59 *    actually output is that of the first line (for the first
60 *    line of *output*) or that of the most recent line (for
61 *    all other lines of output).
62 *    When `-m' has been given, message header paragraphs are
63 *    taken as having first-leading-whitespace empty and
64 *    subsequent-leading-whitespace two spaces.
65 *
66 * Multiple input files are formatted one at a time, so that a file
67 * never ends in the middle of a line.
68 *
69 * There's an alternative mode of operation, invoked by giving
70 * the `-c' option. In that case we just center every line,
71 * and most of the other options are ignored. This should
72 * really be in a separate program, but we must stay compatible
73 * with old `fmt'.
74 *
75 * QUERY: Should `-m' also try to do the right thing with quoted text?
76 * QUERY: `-b' to treat backslashed whitespace as old `fmt' does?
77 * QUERY: Option meaning `never join lines'?
78 * QUERY: Option meaning `split in mid-word to avoid overlong lines'?
79 * (Those last two might not be useful, since we have `fold'.)
80 *
81 * Differences from old `fmt':
82 *
83 *   - We have many more options. Options that aren't understood
84 *     generate a lengthy usage message, rather than being
85 *     treated as filenames.
86 *   - Even with `-m', our handling of message headers is
87 *     significantly different. (And much better.)
88 *   - We don't treat `\ ' as non-word-breaking.
89 *   - Downward changes of indentation start new paragraphs
90 *     for us, as well as upward. (I think old `fmt' behaves
91 *     in the way it does in order to allow indented paragraphs,
92 *     but this is a broken way of making indented paragraphs
93 *     behave right.)
94 *   - Given the choice of going over or under |goal_length|
95 *     by the same amount, we go over; old `fmt' goes under.
96 *   - We treat `?' as ending a sentence, and not `:'. Old `fmt'
97 *     does the reverse.
98 *   - We return approved return codes. Old `fmt' returns
99 *     1 for some errors, and *the number of unopenable files*
100 *     when that was all that went wrong.
101 *   - We have fewer crashes and more helpful error messages.
102 *   - We don't turn spaces into tabs at starts of lines unless
103 *     specifically requested.
104 *   - New `fmt' is somewhat smaller and slightly faster than
105 *     old `fmt'.
106 *
107 * Bugs:
108 *
109 *   None known. There probably are some, though.
110 *
111 * Portability:
112 *
113 *   I believe this code to be pretty portable. It does require
114 *   that you have `getopt'. If you need to include "getopt.h"
115 *   for this (e.g., if your system didn't come with `getopt'
116 *   and you installed it yourself) then you should arrange for
117 *   NEED_getopt_h to be #defined.
118 *
119 *   Everything here should work OK even on nasty 16-bit
120 *   machines and nice 64-bit ones. However, it's only really
121 *   been tested on my FreeBSD machine. Your mileage may vary.
122 */
123
124/* Copyright (c) 1997 Gareth McCaughan. All rights reserved.
125 *
126 * Redistribution and use of this code, in source or binary forms,
127 * with or without modification, are permitted subject to the following
128 * conditions:
129 *
130 *  - Redistribution of source code must retain the above copyright
131 *    notice, this list of conditions and the following disclaimer.
132 *
133 *  - If you distribute modified source code it must also include
134 *    a notice saying that it has been modified, and giving a brief
135 *    description of what changes have been made.
136 *
137 * Disclaimer: I am not responsible for the results of using this code.
138 *             If it formats your hard disc, sends obscene messages to
139 *             your boss and kills your children then that's your problem
140 *             not mine. I give absolutely no warranty of any sort as to
141 *             what the program will do, and absolutely refuse to be held
142 *             liable for any consequences of your using it.
143 *             Thank you. Have a nice day.
144 */
145
146/* RCS change log:
147 * Revision 1.5  1998/03/02 18:02:21  gjm11
148 * Minor changes for portability.
149 *
150 * Revision 1.4  1997/10/01 11:51:28  gjm11
151 * Repair broken indented-paragraph handling.
152 * Add mail message header stuff.
153 * Improve comments and layout.
154 * Make usable with non-BSD systems.
155 * Add revision display to usage message.
156 *
157 * Revision 1.3  1997/09/30 16:24:47  gjm11
158 * Add copyright notice, rcsid string and log message.
159 *
160 * Revision 1.2  1997/09/30 16:13:39  gjm11
161 * Add options: -d <chars>, -l <width>, -p, -s, -t <width>, -h .
162 * Parse options with `getopt'. Clean up code generally.
163 * Make comments more accurate.
164 *
165 * Revision 1.1  1997/09/30 11:29:57  gjm11
166 * Initial revision
167 */
168
169#ifndef lint
170static const char rcsid[] =
171  "$FreeBSD: head/usr.bin/fmt/fmt.c 81510 2001-08-11 00:49:11Z kris $";
172static const char copyright[] =
173  "Copyright (c) 1997 Gareth McCaughan. All rights reserved.\n";
174#endif /* not lint */
175
176#include <ctype.h>
177#include <err.h>
178#include <locale.h>
179#include <stdio.h>
180#include <stdlib.h>
181#include <string.h>
182#include <sysexits.h>
183#include <unistd.h>
184
185/* Something that, we hope, will never be a genuine line length,
186 * indentation etc.
187 */
188#define SILLY ((size_t)-1)
189
190/* I used to use |strtoul| for this, but (1) not all systems have it
191 * and (2) it's probably better to use |strtol| to detect negative
192 * numbers better.
193 * If |fussyp==0| then we don't complain about non-numbers
194 * (returning 0 instead), but we do complain about bad numbers.
195 */
196static size_t
197get_positive(const char *s, const char *err_mess, int fussyP) {
198  char *t;
199  long result = strtol(s,&t,0);
200  if (*t) { if (fussyP) goto Lose; else return 0; }
201  if (result<=0) { Lose: errx(EX_USAGE, "%s", err_mess); }
202  return (size_t) result;
203}
204
205/* Global variables */
206
207static int centerP=0;		/* Try to center lines? */
208static size_t goal_length=0;	/* Target length for output lines */
209static size_t max_length=0;	/* Maximum length for output lines */
210static int coalesce_spaces_P=0;	/* Coalesce multiple whitespace -> ' ' ? */
211static int allow_indented_paragraphs=0;	/* Can first line have diff. ind.? */
212static int tab_width=8;		/* Number of spaces per tab stop */
213static size_t output_tab_width=0;	/* Ditto, when squashing leading spaces */
214static const char *sentence_enders=".?!";	/* Double-space after these */
215static int grok_mail_headers=0;	/* treat embedded mail headers magically? */
216
217static int n_errors=0;		/* Number of failed files. Return on exit. */
218static char *output_buffer=0;	/* Output line will be built here */
219static size_t x;		/* Horizontal position in output line */
220static size_t x0;		/* Ditto, ignoring leading whitespace */
221static size_t pending_spaces;	/* Spaces to add before next word */
222static int output_in_paragraph=0;	/* Any of current para written out yet? */
223
224/* Prototypes */
225
226static void process_named_file (const char *);
227static void     process_stream (FILE *, const char *);
228static size_t    indent_length (const char *, size_t);
229static int     might_be_header (const unsigned char *);
230static void      new_paragraph (size_t, size_t);
231static void        output_word (size_t, size_t, const char *, size_t, size_t);
232static void      output_indent (size_t);
233static void      center_stream (FILE *, const char *);
234static char *         get_line (FILE *, size_t *);
235static void *         xrealloc (void *, size_t);
236
237#define XMALLOC(x) xrealloc(0,x)
238
239/* Here is perhaps the right place to mention that this code is
240 * all in top-down order. Hence, |main| comes first.
241 */
242int
243main(int argc, char *argv[]) {
244  int ch;			/* used for |getopt| processing */
245
246
247  (void) setlocale(LC_CTYPE, "");
248
249  /* 1. Grok parameters. */
250
251  while ((ch = getopt(argc, argv, "0123456789cd:hl:mpst:w:")) != -1)
252  switch(ch) {
253    case 'c':
254      centerP = 1;
255      continue;
256    case 'd':
257      sentence_enders = optarg;
258      continue;
259    case 'l':
260      output_tab_width
261        = get_positive(optarg, "output tab width must be positive", 1);
262      continue;
263    case 'm':
264      grok_mail_headers = 1;
265      continue;
266    case 'p':
267      allow_indented_paragraphs = 1;
268      continue;
269    case 's':
270      coalesce_spaces_P = 1;
271      continue;
272    case 't':
273      tab_width = get_positive(optarg, "tab width must be positive", 1);
274      continue;
275    case 'w':
276      goal_length = get_positive(optarg, "width must be positive", 1);
277      max_length = goal_length;
278      continue;
279    case '0': case '1': case '2': case '3': case '4': case '5':
280    case '6': case '7': case '8': case '9':
281    /* XXX  this is not a stylistically approved use of getopt() */
282      if (goal_length==0) {
283        char *p;
284        p = argv[optind - 1];
285        if (p[0] == '-' && p[1] == ch && !p[2])
286             goal_length = get_positive(++p, "width must be nonzero", 1);
287        else
288             goal_length = get_positive(argv[optind]+1,
289                 "width must be nonzero", 1);
290        max_length = goal_length;
291      }
292      continue;
293    case 'h': default:
294      fprintf(stderr,
295"Usage:   fmt [-cmps] [-d chars] [-l num] [-t num]\n"
296"             [-w width | -width | goal [maximum]] [file ...]\n"
297"Options: -c     center each line instead of formatting\n"
298"         -d <chars> double-space after <chars> at line end\n"
299"         -l <n> turn each <n> spaces at start of line into a tab\n"
300"         -m     try to make sure mail header lines stay separate\n"
301"         -p     allow indented paragraphs\n"
302"         -s     coalesce whitespace inside lines\n"
303"         -t <n> have tabs every <n> columns\n"
304"         -w <n> set maximum width to <n>\n"
305"         goal   set target width to goal\n");
306      exit(ch=='h' ? 0 : EX_USAGE);
307  }
308  argc -= optind; argv += optind;
309
310  /* [ goal [ maximum ] ] */
311
312  if (argc>0 && goal_length==0
313      && (goal_length=get_positive(*argv,"goal length must be positive", 0))
314         != 0) {
315    --argc; ++argv;
316    if (argc>0
317        && (max_length=get_positive(*argv,"max length must be positive", 0))
318           != 0) {
319      --argc; ++argv;
320      if (max_length<goal_length)
321        errx(EX_USAGE, "max length must be >= goal length");
322    }
323  }
324  if (goal_length==0) goal_length = 65;
325  if (max_length==0) max_length = goal_length+10;
326  output_buffer = XMALLOC(max_length+1);	/* really needn't be longer */
327
328  /* 2. Process files. */
329
330  if (argc>0) {
331    while (argc-->0) process_named_file(*argv++);
332  }
333  else {
334    process_stream(stdin, "standard input");
335  }
336
337  /* We're done. */
338
339  return n_errors ? EX_NOINPUT : 0;
340
341}
342
343/* Process a single file, given its name.
344 */
345static void
346process_named_file(const char *name) {
347  FILE *f=fopen(name, "r");
348  if (!f) { perror(name); ++n_errors; }
349  else {
350    process_stream(f, name);
351    fclose(f);
352  }
353}
354
355/* Types of mail header continuation lines:
356 */
357typedef enum {
358  hdr_ParagraphStart = -1,
359  hdr_NonHeader      = 0,
360  hdr_Header         = 1,
361  hdr_Continuation   = 2
362} HdrType;
363
364/* Process a stream. This is where the real work happens,
365 * except that centering is handled separately.
366 */
367static void
368process_stream(FILE *stream, const char *name) {
369  size_t last_indent=SILLY;	/* how many spaces in last indent? */
370  size_t para_line_number=0;	/* how many lines already read in this para? */
371  size_t first_indent=SILLY;	/* indentation of line 0 of paragraph */
372  HdrType prev_header_type=hdr_ParagraphStart;
373	/* ^-- header_type of previous line; -1 at para start */
374  char *line;
375  size_t length;
376
377  if (centerP) { center_stream(stream, name); return; }
378  while ((line=get_line(stream,&length)) != NULL) {
379    size_t np=indent_length(line, length);
380    { HdrType header_type=hdr_NonHeader;
381      if (grok_mail_headers && prev_header_type!=hdr_NonHeader) {
382        if (np==0 && might_be_header(line))
383          header_type = hdr_Header;
384        else if (np>0 && prev_header_type>hdr_NonHeader)
385          header_type = hdr_Continuation;
386      }
387      /* We need a new paragraph if and only if:
388       *   this line is blank,
389       *   OR it's a mail header,
390       *   OR it's not a mail header AND the last line was one,
391       *   OR the indentation has changed
392       *      AND the line isn't a mail header continuation line
393       *      AND this isn't the second line of an indented paragraph.
394       */
395      if ( length==0
396           || header_type==hdr_Header
397           || (header_type==hdr_NonHeader && prev_header_type>hdr_NonHeader)
398           || (np!=last_indent
399               && header_type != hdr_Continuation
400               && (!allow_indented_paragraphs || para_line_number != 1)) ) {
401        new_paragraph(output_in_paragraph ? last_indent : first_indent, np);
402        para_line_number = 0;
403        first_indent = np;
404        last_indent = np;
405        if (header_type==hdr_Header) last_indent=2;	/* for cont. lines */
406        if (length==0) {
407          putchar('\n');
408          prev_header_type=hdr_ParagraphStart;
409          continue;
410        }
411      }
412      else {
413        /* If this is an indented paragraph other than a mail header
414         * continuation, set |last_indent|.
415         */
416        if (np != last_indent && header_type != hdr_Continuation)
417          last_indent=np;
418      }
419      prev_header_type = header_type;
420    }
421
422    { size_t n=np;
423      while (n<length) {
424        /* Find word end and count spaces after it */
425        size_t word_length=0, space_length=0;
426        while (n+word_length < length && line[n+word_length] != ' ')
427          ++word_length;
428        space_length = word_length;
429        while (n+space_length < length && line[n+space_length] == ' ')
430          ++space_length;
431        /* Send the word to the output machinery. */
432        output_word(first_indent, last_indent,
433                    line+n, word_length, space_length-word_length);
434        n += space_length;
435      }
436    }
437    ++para_line_number;
438  }
439  new_paragraph(output_in_paragraph ? last_indent : first_indent, 0);
440  if (ferror(stream)) { perror(name); ++n_errors; }
441}
442
443/* How long is the indent on this line?
444 */
445static size_t
446indent_length(const char *line, size_t length) {
447  size_t n=0;
448  while (n<length && *line++ == ' ') ++n;
449  return n;
450}
451
452/* Might this line be a mail header?
453 * We deem a line to be a possible header if it matches the
454 * Perl regexp /^[A-Z][-A-Za-z0-9]*:\s/. This is *not* the same
455 * as in RFC whatever-number-it-is; we want to be gratuitously
456 * conservative to avoid mangling ordinary civilised text.
457 */
458static int
459might_be_header(const unsigned char *line) {
460  if (!isupper(*line++)) return 0;
461  while (*line && (isalnum(*line) || *line=='-')) ++line;
462  return (*line==':' && isspace(line[1]));
463}
464
465/* Begin a new paragraph with an indent of |indent| spaces.
466 */
467static void
468new_paragraph(size_t old_indent, size_t indent) {
469  if (x0) {
470    if (old_indent>0) output_indent(old_indent);
471    fwrite(output_buffer, 1, x0, stdout);
472    putchar('\n');
473  }
474  x=indent; x0=0; pending_spaces=0;
475  output_in_paragraph = 0;
476}
477
478/* Output spaces or tabs for leading indentation.
479 */
480static void
481output_indent(size_t n_spaces) {
482  if (output_tab_width) {
483    while (n_spaces >= output_tab_width) {
484      putchar('\t');
485      n_spaces -= output_tab_width;
486    }
487  }
488  while (n_spaces-- > 0) putchar(' ');
489}
490
491/* Output a single word, or add it to the buffer.
492 * indent0 and indent1 are the indents to use on the first and subsequent
493 * lines of a paragraph. They'll often be the same, of course.
494 */
495static void
496output_word(size_t indent0, size_t indent1, const char *word, size_t length, size_t spaces) {
497  size_t new_x = x+pending_spaces+length;
498  size_t indent = output_in_paragraph ? indent1 : indent0;
499
500  /* If either |spaces==0| (at end of line) or |coalesce_spaces_P|
501   * (squashing internal whitespace), then add just one space;
502   * except that if the last character was a sentence-ender we
503   * actually add two spaces.
504   */
505  if (coalesce_spaces_P || spaces==0)
506    spaces = strchr(sentence_enders, word[length-1]) ? 2 : 1;
507
508  if (new_x<=goal_length) {
509    /* After adding the word we still aren't at the goal length,
510     * so clearly we add it to the buffer rather than outputing it.
511     */
512    memset(output_buffer+x0, ' ', pending_spaces);
513    x0 += pending_spaces; x += pending_spaces;
514    memcpy(output_buffer+x0, word, length);
515    x0 += length; x += length;
516    pending_spaces = spaces;
517  }
518  else {
519    /* Adding the word takes us past the goal. Print the line-so-far,
520     * and the word too iff either (1) the lsf is empty or (2) that
521     * makes us nearer the goal but doesn't take us over the limit,
522     * or (3) the word on its own takes us over the limit.
523     * In case (3) we put a newline in between.
524     */
525    if (indent>0) output_indent(indent);
526    fwrite(output_buffer, 1, x0, stdout);
527    if (x0==0 || (new_x <= max_length && new_x-goal_length <= goal_length-x)) {
528      printf("%*s", (int)pending_spaces, "");
529      goto write_out_word;
530    }
531    else {
532      /* If the word takes us over the limit on its own, just
533       * spit it out and don't bother buffering it.
534       */
535      if (indent+length > max_length) {
536        putchar('\n');
537        if (indent>0) output_indent(indent);
538write_out_word:
539        fwrite(word, 1, length, stdout);
540        x0 = 0; x = indent1; pending_spaces = 0;
541      }
542      else {
543        memcpy(output_buffer, word, length);
544        x0 = length; x = length+indent1; pending_spaces = spaces;
545      }
546    }
547    putchar('\n');
548    output_in_paragraph = 1;
549  }
550}
551
552/* Process a stream, but just center its lines rather than trying to
553 * format them neatly.
554 */
555static void
556center_stream(FILE *stream, const char *name) {
557  char *line;
558  size_t length;
559  while ((line=get_line(stream, &length)) != 0) {
560    size_t l=length;
561    while (l>0 && isspace(*line)) { ++line; --l; }
562    length=l;
563    while (l<goal_length) { putchar(' '); l+=2; }
564    fwrite(line, 1, length, stdout);
565    putchar('\n');
566  }
567  if (ferror(stream)) { perror(name); ++n_errors; }
568}
569
570/* Get a single line from a stream. Expand tabs, strip control
571 * characters and trailing whitespace, and handle backspaces.
572 * Return the address of the buffer containing the line, and
573 * put the length of the line in |lengthp|.
574 * This can cope with arbitrarily long lines, and with lines
575 * without terminating \n.
576 * If there are no characters left or an error happens, we
577 * return 0.
578 * Don't confuse |spaces_pending| here with the global
579 * |pending_spaces|.
580 */
581static char *
582get_line(FILE *stream, size_t *lengthp) {
583  static char *buf=NULL;
584  static size_t length=0;
585  size_t len=0;
586  int ch;
587  size_t spaces_pending=0;
588
589  if (buf==NULL) { length=100; buf=XMALLOC(length); }
590  while ((ch=getc(stream)) != '\n' && ch != EOF) {
591    if (ch==' ') ++spaces_pending;
592    else if (isprint(ch)) {
593      while (len+spaces_pending >= length) {
594        length*=2; buf=xrealloc(buf, length);
595      }
596      while (spaces_pending > 0) { --spaces_pending; buf[len++]=' '; }
597      buf[len++] = ch;
598    }
599    else if (ch=='\t')
600      spaces_pending += tab_width - (len+spaces_pending)%tab_width;
601    else if (ch=='\b') { if (len) --len; }
602  }
603  *lengthp=len;
604  return (len>0 || ch!=EOF) ? buf : 0;
605}
606
607/* (Re)allocate some memory, exiting with an error if we can't.
608 */
609static void *
610xrealloc(void *ptr, size_t nbytes) {
611  void *p = realloc(ptr, nbytes);
612  if (p == NULL) errx(EX_OSERR, "out of memory");
613  return p;
614}
615