1 /* GNU fmt -- simple text formatter.
2 Copyright (C) 1994-1999 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Ross Paterson <rap@doc.ic.ac.uk>. */
22 #include <sys/types.h>
25 /* Redefine. Otherwise, systems (Unicos for one) with headers that define
26 it to be a type get syntax errors for the variable declaration below. */
27 #define word unused_word_type
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "fmt"
36 #define AUTHORS "Ross Paterson"
38 /* The following parameters represent the program's idea of what is
39 "best". Adjust to taste, subject to the caveats given. */
41 /* Default longest permitted line length (max_width). */
44 /* Prefer lines to be LEEWAY % shorter than the maximum width, giving
45 room for optimization. */
48 /* The default secondary indent of tagged paragraph used for unindented
49 one-line paragraphs not preceded by any multi-line paragraphs. */
52 /* Costs and bonuses are expressed as the equivalent departure from the
53 optimal line length, multiplied by 10. e.g. assigning something a
54 cost of 50 means that it is as bad as a line 5 characters too short
55 or too long. The definition of SHORT_COST(n) should not be changed.
56 However, EQUIV(n) may need tuning. */
60 #define MAXCOST (~(((unsigned long) 1) << (8 * sizeof (COST) -1)))
62 #define SQR(n) ((n) * (n))
63 #define EQUIV(n) SQR ((COST) (n))
65 /* Cost of a filled line n chars longer or shorter than best_width. */
66 #define SHORT_COST(n) EQUIV ((n) * 10)
68 /* Cost of the difference between adjacent filled lines. */
69 #define RAGGED_COST(n) (SHORT_COST (n) / 2)
71 /* Basic cost per line. */
72 #define LINE_COST EQUIV (70)
74 /* Cost of breaking a line after the first word of a sentence, where
75 the length of the word is N. */
76 #define WIDOW_COST(n) (EQUIV (200) / ((n) + 2))
78 /* Cost of breaking a line before the last word of a sentence, where
79 the length of the word is N. */
80 #define ORPHAN_COST(n) (EQUIV (150) / ((n) + 2))
82 /* Bonus for breaking a line at the end of a sentence. */
83 #define SENTENCE_BONUS EQUIV (50)
85 /* Cost of breaking a line after a period not marking end of a sentence.
86 With the definition of sentence we are using (borrowed from emacs, see
87 get_line()) such a break would then look like a sentence break. Hence
88 we assign a very high cost -- it should be avoided unless things are
90 #define NOBREAK_COST EQUIV (600)
92 /* Bonus for breaking a line before open parenthesis. */
93 #define PAREN_BONUS EQUIV (40)
95 /* Bonus for breaking a line after other punctuation. */
96 #define PUNCT_BONUS EQUIV(40)
98 /* Credit for breaking a long paragraph one line later. */
99 #define LINE_CREDIT EQUIV(3)
101 /* Size of paragraph buffer, in words and characters. Longer paragraphs
102 are handled neatly (cf. flush_paragraph()), so there's little to gain
103 by making these larger. */
104 #define MAXWORDS 1000
105 #define MAXCHARS 5000
107 /* Extra ctype(3)-style macros. */
109 #define isopen(c) (strchr ("([`'\"", c) != NULL)
110 #define isclose(c) (strchr (")]'\"", c) != NULL)
111 #define isperiod(c) (strchr (".?!", c) != NULL)
113 /* Size of a tab stop, for expansion on input and re-introduction on
117 /* Miscellaneous definitions. */
119 typedef unsigned int bool;
125 /* Word descriptor structure. */
127 typedef struct Word WORD
;
132 /* Static attributes determined during input. */
134 const char *text
; /* the text of the word */
135 short length
; /* length of this word */
136 short space
; /* the size of the following space */
137 bool paren
:1; /* starts with open paren */
138 bool period
:1; /* ends in [.?!])* */
139 bool punct
:1; /* ends in punctuation */
140 bool final
:1; /* end of sentence */
142 /* The remaining fields are computed during the optimization. */
144 short line_length
; /* length of the best line starting here */
145 COST best_cost
; /* cost of best paragraph starting here */
146 WORD
*next_break
; /* break which achieves best_cost */
149 /* Forward declarations. */
151 static void set_prefix
PARAMS ((char *p
));
152 static void fmt
PARAMS ((FILE *f
));
153 static bool get_paragraph
PARAMS ((FILE *f
));
154 static int get_line
PARAMS ((FILE *f
, int c
));
155 static int get_prefix
PARAMS ((FILE *f
));
156 static int get_space
PARAMS ((FILE *f
, int c
));
157 static int copy_rest
PARAMS ((FILE *f
, int c
));
158 static bool same_para
PARAMS ((int c
));
159 static void flush_paragraph
PARAMS ((void));
160 static void fmt_paragraph
PARAMS ((void));
161 static void check_punctuation
PARAMS ((WORD
*w
));
162 static COST base_cost
PARAMS ((WORD
*this));
163 static COST line_cost
PARAMS ((WORD
*next
, int len
));
164 static void put_paragraph
PARAMS ((WORD
*finish
));
165 static void put_line
PARAMS ((WORD
*w
, int indent
));
166 static void put_word
PARAMS ((WORD
*w
));
167 static void put_space
PARAMS ((int space
));
169 /* The name this program was run with. */
170 const char *program_name
;
174 /* If TRUE, first 2 lines may have different indent (default FALSE). */
177 /* If TRUE, first 2 lines _must_ have different indent (default FALSE). */
180 /* If TRUE, each line is a paragraph on its own (default FALSE). */
183 /* If TRUE, don't preserve inter-word spacing (default FALSE). */
186 /* Prefix minus leading and trailing spaces (default ""). */
187 static const char *prefix
;
189 /* User-supplied maximum line width (default WIDTH). The only output
191 longer than this will each comprise a single word. */
192 static int max_width
;
194 /* Values derived from the option values. */
196 /* The length of prefix minus leading space. */
197 static int prefix_full_length
;
199 /* The length of the leading space trimmed from the prefix. */
200 static int prefix_lead_space
;
202 /* The length of prefix minus leading and trailing space. */
203 static int prefix_length
;
205 /* The preferred width of text lines, set to LEEWAY % less than max_width. */
206 static int best_width
;
208 /* Dynamic variables. */
210 /* Start column of the character most recently read from the input file. */
211 static int in_column
;
213 /* Start column of the next character to be written to stdout. */
214 static int out_column
;
216 /* Space for the paragraph text -- longer paragraphs are handled neatly
217 (cf. flush_paragraph()). */
218 static char parabuf
[MAXCHARS
];
220 /* A pointer into parabuf, indicating the first unused character position. */
223 /* The words of a paragraph -- longer paragraphs are handled neatly
224 (cf. flush_paragraph()). */
225 static WORD word
[MAXWORDS
];
227 /* A pointer into the above word array, indicating the first position
228 after the last complete word. Sometimes it will point at an incomplete
230 static WORD
*word_limit
;
232 /* If TRUE, current input file contains tab characters, and so tabs can be
233 used for white space on output. */
236 /* Space before trimmed prefix on each line of the current paragraph. */
237 static int prefix_indent
;
239 /* Indentation of the first line of the current paragraph. */
240 static int first_indent
;
242 /* Indentation of other lines of the current paragraph */
243 static int other_indent
;
245 /* To detect the end of a paragraph, we need to look ahead to the first
246 non-blank character after the prefix on the next line, or the first
247 character on the following line that failed to match the prefix.
248 We can reconstruct the lookahead from that character (next_char), its
249 position on the line (in_column) and the amount of space before the
250 prefix (next_prefix_indent). See get_paragraph() and copy_rest(). */
252 /* The last character read from the input file. */
253 static int next_char
;
255 /* The space before the trimmed prefix (or part of it) on the next line
256 after the current paragraph. */
257 static int next_prefix_indent
;
259 /* If nonzero, the length of the last line output in the current
260 paragraph, used to charge for raggedness at the split point for long
261 paragraphs chosen by fmt_paragraph(). */
262 static int last_line_length
;
268 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
272 printf (_("Usage: %s [-DIGITS] [OPTION]... [FILE]...\n"), program_name
);
274 Reformat each paragraph in the FILE(s), writing to standard output.\n\
275 If no FILE or if FILE is `-', read standard input.\n\
277 Mandatory arguments to long options are mandatory for short options too.\n\
278 -c, --crown-margin preserve indentation of first two lines\n\
279 -p, --prefix=STRING combine only lines having STRING as prefix\n\
280 -s, --split-only split long lines, but do not refill\n\
281 -t, --tagged-paragraph indentation of first line different from second\n\
282 -u, --uniform-spacing one space between words, two after sentences\n\
283 -w, --width=NUMBER maximum line width (default of 75 columns)\n\
284 --help display this help and exit\n\
285 --version output version information and exit\n\
287 In -wNUMBER, the letter `w' may be omitted.\n"),
289 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
291 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
294 /* Decode options and launch execution. */
296 static const struct option long_options
[] =
298 {"crown-margin", no_argument
, NULL
, 'c'},
299 {"prefix", required_argument
, NULL
, 'p'},
300 {"split-only", no_argument
, NULL
, 's'},
301 {"tagged-paragraph", no_argument
, NULL
, 't'},
302 {"uniform-spacing", no_argument
, NULL
, 'u'},
303 {"width", required_argument
, NULL
, 'w'},
304 {GETOPT_HELP_OPTION_DECL
},
305 {GETOPT_VERSION_OPTION_DECL
},
310 main (register int argc
, register char **argv
)
314 program_name
= argv
[0];
315 setlocale (LC_ALL
, "");
316 bindtextdomain (PACKAGE
, LOCALEDIR
);
317 textdomain (PACKAGE
);
319 crown
= tagged
= split
= uniform
= FALSE
;
322 prefix_length
= prefix_lead_space
= prefix_full_length
= 0;
324 if (argc
> 1 && argv
[1][0] == '-' && ISDIGIT (argv
[1][1]))
327 /* Old option syntax; a dash followed by one or more digits.
328 Move past the number. */
329 for (++argv
[1]; ISDIGIT (*argv
[1]); ++argv
[1])
331 /* FIXME: use strtol to detect overflow. */
332 max_width
= max_width
* 10 + *argv
[1] - '0';
334 /* Make the options we just parsed invisible to getopt. */
340 while ((optchar
= getopt_long (argc
, argv
, "0123456789cstuw:p:",
370 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
371 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
372 error (EXIT_FAILURE
, 0, _("invalid line number increment: `%s'"),
374 max_width
= (int) tmp_long
;
382 case_GETOPT_HELP_CHAR
;
384 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
388 best_width
= max_width
* (2 * (100 - LEEWAY
) + 1) / 200;
394 for (; optind
< argc
; optind
++)
396 char *file
= argv
[optind
];
397 if (STREQ (file
, "-"))
402 in_stream
= fopen (file
, "r");
403 if (in_stream
!= NULL
)
406 if (fclose (in_stream
) == EOF
)
407 error (EXIT_FAILURE
, errno
, "%s", file
);
410 error (0, errno
, "%s", file
);
415 if (ferror (stdout
) || fclose (stdout
) == EOF
)
416 error (EXIT_FAILURE
, errno
, _("write error"));
421 /* Trim space from the front and back of the string P, yielding the prefix,
422 and record the lengths of the prefix and the space trimmed. */
425 set_prefix (register char *p
)
429 prefix_lead_space
= 0;
436 prefix_full_length
= strlen (p
);
437 s
= p
+ prefix_full_length
;
438 while (s
> p
&& s
[-1] == ' ')
441 prefix_length
= s
- p
;
444 /* read file F and send formatted output to stdout. */
451 next_char
= get_prefix (f
);
452 while (get_paragraph (f
))
455 put_paragraph (word_limit
);
459 /* Read a paragraph from input file F. A paragraph consists of a
460 maximal number of non-blank (excluding any prefix) lines subject to:
461 * In split mode, a paragraph is a single non-blank line.
462 * In crown mode, the second and subsequent lines must have the
463 same indentation, but possibly different from the indent of the
465 * Tagged mode is similar, but the first and second lines must have
466 different indentations.
467 * Otherwise, all lines of a paragraph must have the same indent.
468 If a prefix is in effect, it must be present at the same indent for
469 each line in the paragraph.
471 Return FALSE if end-of-file was encountered before the start of a
472 paragraph, else TRUE. */
475 get_paragraph (FILE *f
)
479 last_line_length
= 0;
482 /* Scan (and copy) blank lines, and lines not introduced by the prefix. */
484 while (c
== '\n' || c
== EOF
485 || next_prefix_indent
< prefix_lead_space
486 || in_column
< next_prefix_indent
+ prefix_full_length
)
488 c
= copy_rest (f
, c
);
498 /* Got a suitable first line for a paragraph. */
500 prefix_indent
= next_prefix_indent
;
501 first_indent
= in_column
;
506 /* Read rest of paragraph (unless split is specified). */
509 other_indent
= first_indent
;
514 other_indent
= in_column
;
516 { /* for each line till the end of the para */
519 while (same_para (c
) && in_column
== other_indent
);
522 other_indent
= first_indent
;
526 if (same_para (c
) && in_column
!= first_indent
)
528 other_indent
= in_column
;
530 { /* for each line till the end of the para */
533 while (same_para (c
) && in_column
== other_indent
);
536 /* Only one line: use the secondary indent from last time if it
537 splits, or 0 if there have been no multi-line paragraphs in the
538 input so far. But if these rules make the two indents the same,
539 pick a new secondary indent. */
541 else if (other_indent
== first_indent
)
542 other_indent
= first_indent
== 0 ? DEF_INDENT
: 0;
546 other_indent
= first_indent
;
547 while (same_para (c
) && in_column
== other_indent
)
550 (word_limit
- 1)->period
= (word_limit
- 1)->final
= TRUE
;
555 /* Copy to the output a line that failed to match the prefix, or that
556 was blank after the prefix. In the former case, C is the character
557 that failed to match the prefix. In the latter, C is \n or EOF.
558 Return the character (\n or EOF) ending the line. */
561 copy_rest (FILE *f
, register int c
)
563 register const char *s
;
566 if (in_column
> next_prefix_indent
&& c
!= '\n' && c
!= EOF
)
568 put_space (next_prefix_indent
);
569 for (s
= prefix
; out_column
!= in_column
&& *s
; out_column
++)
571 put_space (in_column
- out_column
);
573 while (c
!= '\n' && c
!= EOF
)
581 /* Return TRUE if a line whose first non-blank character after the
582 prefix (if any) is C could belong to the current paragraph,
586 same_para (register int c
)
588 return (next_prefix_indent
== prefix_indent
589 && in_column
>= next_prefix_indent
+ prefix_full_length
590 && c
!= '\n' && c
!= EOF
);
593 /* Read a line from input file F, given first non-blank character C
594 after the prefix, and the following indent, and break it into words.
595 A word is a maximal non-empty string of non-white characters. A word
596 ending in [.?!]["')\]]* and followed by end-of-line or at least two
597 spaces ends a sentence, as in emacs.
599 Return the first non-blank character of the next line. */
602 get_line (FILE *f
, register int c
)
605 register char *end_of_parabuf
;
606 register WORD
*end_of_word
;
608 end_of_parabuf
= ¶buf
[MAXCHARS
];
609 end_of_word
= &word
[MAXWORDS
- 2];
612 { /* for each word in a line */
616 word_limit
->text
= wptr
;
619 if (wptr
== end_of_parabuf
)
624 while (c
!= EOF
&& !ISSPACE (c
));
625 in_column
+= word_limit
->length
= wptr
- word_limit
->text
;
626 check_punctuation (word_limit
);
628 /* Scan inter-word space. */
631 c
= get_space (f
, c
);
632 word_limit
->space
= in_column
- start
;
633 word_limit
->final
= (c
== EOF
634 || (word_limit
->period
635 && (c
== '\n' || word_limit
->space
> 1)));
636 if (c
== '\n' || c
== EOF
|| uniform
)
637 word_limit
->space
= word_limit
->final
? 2 : 1;
638 if (word_limit
== end_of_word
)
645 return get_prefix (f
);
648 /* Read a prefix from input file F. Return either first non-matching
649 character, or first non-blank character after the prefix. */
655 register const char *p
;
658 c
= get_space (f
, getc (f
));
659 if (prefix_length
== 0)
660 next_prefix_indent
= prefix_lead_space
< in_column
?
661 prefix_lead_space
: in_column
;
664 next_prefix_indent
= in_column
;
665 for (p
= prefix
; *p
!= '\0'; p
++)
672 c
= get_space (f
, c
);
677 /* Read blank characters from input file F, starting with C, and keeping
678 in_column up-to-date. Return first non-blank character. */
681 get_space (FILE *f
, register int c
)
690 in_column
= (in_column
/ TABWIDTH
+ 1) * TABWIDTH
;
698 /* Set extra fields in word W describing any attached punctuation. */
701 check_punctuation (register WORD
*w
)
703 const unsigned char *start
, *finish
;
705 start
= (unsigned char *) w
->text
;
706 finish
= start
+ (w
->length
- 1);
707 w
->paren
= isopen (*start
);
708 w
->punct
= ISPUNCT (*finish
);
709 while (isclose (*finish
) && finish
> start
)
711 w
->period
= isperiod (*finish
);
714 /* Flush part of the paragraph to make room. This function is called on
715 hitting the limit on the number of words or characters. */
718 flush_paragraph (void)
725 /* In the special case where it's all one word, just flush it. */
727 if (word_limit
== word
)
729 printf ("%*s", wptr
- parabuf
, parabuf
);
735 - format what you have so far as a paragraph,
736 - find a low-cost line break near the end,
738 - make that the start of the paragraph. */
742 /* Choose a good split point. */
744 split_point
= word_limit
;
745 best_break
= MAXCOST
;
746 for (w
= word
->next_break
; w
!= word_limit
; w
= w
->next_break
)
748 if (w
->best_cost
- w
->next_break
->best_cost
< best_break
)
751 best_break
= w
->best_cost
- w
->next_break
->best_cost
;
753 if (best_break
<= MAXCOST
- LINE_CREDIT
)
754 best_break
+= LINE_CREDIT
;
756 put_paragraph (split_point
);
758 /* Copy text of words down to start of parabuf -- we use memmove because
759 the source and target may overlap. */
761 memmove (parabuf
, split_point
->text
, (size_t) (wptr
- split_point
->text
));
762 shift
= split_point
->text
- parabuf
;
765 /* Adjust text pointers. */
767 for (w
= split_point
; w
<= word_limit
; w
++)
770 /* Copy words from split_point down to word -- we use memmove because
771 the source and target may overlap. */
773 memmove ((char *) word
, (char *) split_point
,
774 (word_limit
- split_point
+ 1) * sizeof (WORD
));
775 word_limit
-= split_point
- word
;
778 /* Compute the optimal formatting for the whole paragraph by computing
779 and remembering the optimal formatting for each suffix from the empty
780 one to the whole paragraph. */
785 register WORD
*start
, *w
;
787 register COST wcost
, best
;
790 word_limit
->best_cost
= 0;
791 saved_length
= word_limit
->length
;
792 word_limit
->length
= max_width
; /* sentinel */
794 for (start
= word_limit
- 1; start
>= word
; start
--)
797 len
= start
== word
? first_indent
: other_indent
;
799 /* At least one word, however long, in the line. */
807 /* Consider breaking before w. */
809 wcost
= line_cost (w
, len
) + w
->best_cost
;
810 if (start
== word
&& last_line_length
> 0)
811 wcost
+= RAGGED_COST (len
- last_line_length
);
815 start
->next_break
= w
;
816 start
->line_length
= len
;
818 len
+= (w
- 1)->space
+ w
->length
; /* w > start >= word */
820 while (len
< max_width
);
821 start
->best_cost
= best
+ base_cost (start
);
824 word_limit
->length
= saved_length
;
827 /* Return the constant component of the cost of breaking before the
831 base_cost (register WORD
*this)
839 if ((this - 1)->period
)
841 if ((this - 1)->final
)
842 cost
-= SENTENCE_BONUS
;
844 cost
+= NOBREAK_COST
;
846 else if ((this - 1)->punct
)
848 else if (this > word
+ 1 && (this - 2)->final
)
849 cost
+= WIDOW_COST ((this - 1)->length
);
854 else if (this->final
)
855 cost
+= ORPHAN_COST (this->length
);
860 /* Return the component of the cost of breaking before word NEXT that
861 depends on LEN, the length of the line beginning there. */
864 line_cost (register WORD
*next
, register int len
)
869 if (next
== word_limit
)
871 n
= best_width
- len
;
872 cost
= SHORT_COST (n
);
873 if (next
->next_break
!= word_limit
)
875 n
= len
- next
->line_length
;
876 cost
+= RAGGED_COST (n
);
881 /* Output to stdout a paragraph from word up to (but not including)
882 FINISH, which must be in the next_break chain from word. */
885 put_paragraph (register WORD
*finish
)
889 put_line (word
, first_indent
);
890 for (w
= word
->next_break
; w
!= finish
; w
= w
->next_break
)
891 put_line (w
, other_indent
);
894 /* Output to stdout the line beginning with word W, beginning in column
895 INDENT, including the prefix (if any). */
898 put_line (register WORD
*w
, int indent
)
900 register WORD
*endline
;
903 put_space (prefix_indent
);
904 fputs (prefix
, stdout
);
905 out_column
+= prefix_length
;
906 put_space (indent
- out_column
);
908 endline
= w
->next_break
- 1;
909 for (; w
!= endline
; w
++)
912 put_space (w
->space
);
915 last_line_length
= out_column
;
919 /* Output to stdout the word W. */
922 put_word (register WORD
*w
)
924 register const char *s
;
928 for (n
= w
->length
; n
!= 0; n
--)
930 out_column
+= w
->length
;
933 /* Output to stdout SPACE spaces, or equivalent tabs. */
936 put_space (int space
)
938 register int space_target
, tab_target
;
940 space_target
= out_column
+ space
;
943 tab_target
= space_target
/ TABWIDTH
* TABWIDTH
;
944 if (out_column
+ 1 < tab_target
)
945 while (out_column
< tab_target
)
948 out_column
= (out_column
/ TABWIDTH
+ 1) * TABWIDTH
;
951 while (out_column
< space_target
)