Tizen 2.0 Release
[external/tizen-coreutils.git] / src / fmt.c
1 /* GNU fmt -- simple text formatter.
2    Copyright (C) 1994-2006 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Ross Paterson <rap@doc.ic.ac.uk>.  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <getopt.h>
24
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
28
29 #include "system.h"
30 #include "error.h"
31 #include "quote.h"
32 #include "xstrtol.h"
33
34 /* The official name of this program (e.g., no `g' prefix).  */
35 #define PROGRAM_NAME "fmt"
36
37 #define AUTHORS "Ross Paterson"
38
39 /* The following parameters represent the program's idea of what is
40    "best".  Adjust to taste, subject to the caveats given.  */
41
42 /* Default longest permitted line length (max_width).  */
43 #define WIDTH   75
44
45 /* Prefer lines to be LEEWAY % shorter than the maximum width, giving
46    room for optimization.  */
47 #define LEEWAY  7
48
49 /* The default secondary indent of tagged paragraph used for unindented
50    one-line paragraphs not preceded by any multi-line paragraphs.  */
51 #define DEF_INDENT 3
52
53 /* Costs and bonuses are expressed as the equivalent departure from the
54    optimal line length, multiplied by 10.  e.g. assigning something a
55    cost of 50 means that it is as bad as a line 5 characters too short
56    or too long.  The definition of SHORT_COST(n) should not be changed.
57    However, EQUIV(n) may need tuning.  */
58
59 /* FIXME: "fmt" misbehaves given large inputs or options.  One
60    possible workaround for part of the problem is to change COST to be
61    a floating-point type.  There are other problems besides COST,
62    though; see MAXWORDS below.  */
63
64 typedef long int COST;
65
66 #define MAXCOST TYPE_MAXIMUM (COST)
67
68 #define SQR(n)          ((n) * (n))
69 #define EQUIV(n)        SQR ((COST) (n))
70
71 /* Cost of a filled line n chars longer or shorter than best_width.  */
72 #define SHORT_COST(n)   EQUIV ((n) * 10)
73
74 /* Cost of the difference between adjacent filled lines.  */
75 #define RAGGED_COST(n)  (SHORT_COST (n) / 2)
76
77 /* Basic cost per line.  */
78 #define LINE_COST       EQUIV (70)
79
80 /* Cost of breaking a line after the first word of a sentence, where
81    the length of the word is N.  */
82 #define WIDOW_COST(n)   (EQUIV (200) / ((n) + 2))
83
84 /* Cost of breaking a line before the last word of a sentence, where
85    the length of the word is N.  */
86 #define ORPHAN_COST(n)  (EQUIV (150) / ((n) + 2))
87
88 /* Bonus for breaking a line at the end of a sentence.  */
89 #define SENTENCE_BONUS  EQUIV (50)
90
91 /* Cost of breaking a line after a period not marking end of a sentence.
92    With the definition of sentence we are using (borrowed from emacs, see
93    get_line()) such a break would then look like a sentence break.  Hence
94    we assign a very high cost -- it should be avoided unless things are
95    really bad.  */
96 #define NOBREAK_COST    EQUIV (600)
97
98 /* Bonus for breaking a line before open parenthesis.  */
99 #define PAREN_BONUS     EQUIV (40)
100
101 /* Bonus for breaking a line after other punctuation.  */
102 #define PUNCT_BONUS     EQUIV(40)
103
104 /* Credit for breaking a long paragraph one line later.  */
105 #define LINE_CREDIT     EQUIV(3)
106
107 /* Size of paragraph buffer, in words and characters.  Longer paragraphs
108    are handled neatly (cf. flush_paragraph()), so long as these values
109    are considerably greater than required by the width.  These values
110    cannot be extended indefinitely: doing so would run into size limits
111    and/or cause more overflows in cost calculations.  FIXME: Remove these
112    arbitrary limits.  */
113
114 #define MAXWORDS        1000
115 #define MAXCHARS        5000
116
117 /* Extra ctype(3)-style macros.  */
118
119 #define isopen(c)       (strchr ("([`'\"", c) != NULL)
120 #define isclose(c)      (strchr (")]'\"", c) != NULL)
121 #define isperiod(c)     (strchr (".?!", c) != NULL)
122
123 /* Size of a tab stop, for expansion on input and re-introduction on
124    output.  */
125 #define TABWIDTH        8
126
127 /* Word descriptor structure.  */
128
129 typedef struct Word WORD;
130
131 struct Word
132   {
133
134     /* Static attributes determined during input.  */
135
136     const char *text;           /* the text of the word */
137     int length;                 /* length of this word */
138     int space;                  /* the size of the following space */
139     unsigned int paren:1;       /* starts with open paren */
140     unsigned int period:1;      /* ends in [.?!])* */
141     unsigned int punct:1;       /* ends in punctuation */
142     unsigned int final:1;       /* end of sentence */
143
144     /* The remaining fields are computed during the optimization.  */
145
146     int line_length;            /* length of the best line starting here */
147     COST best_cost;             /* cost of best paragraph starting here */
148     WORD *next_break;           /* break which achieves best_cost */
149   };
150
151 /* Forward declarations.  */
152
153 static void set_prefix (char *p);
154 static void fmt (FILE *f);
155 static bool get_paragraph (FILE *f);
156 static int get_line (FILE *f, int c);
157 static int get_prefix (FILE *f);
158 static int get_space (FILE *f, int c);
159 static int copy_rest (FILE *f, int c);
160 static bool same_para (int c);
161 static void flush_paragraph (void);
162 static void fmt_paragraph (void);
163 static void check_punctuation (WORD *w);
164 static COST base_cost (WORD *this);
165 static COST line_cost (WORD *next, int len);
166 static void put_paragraph (WORD *finish);
167 static void put_line (WORD *w, int indent);
168 static void put_word (WORD *w);
169 static void put_space (int space);
170
171 /* The name this program was run with.  */
172 const char *program_name;
173
174 /* Option values.  */
175
176 /* If true, first 2 lines may have different indent (default false).  */
177 static bool crown;
178
179 /* If true, first 2 lines _must_ have different indent (default false).  */
180 static bool tagged;
181
182 /* If true, each line is a paragraph on its own (default false).  */
183 static bool split;
184
185 /* If true, don't preserve inter-word spacing (default false).  */
186 static bool uniform;
187
188 /* Prefix minus leading and trailing spaces (default "").  */
189 static const char *prefix;
190
191 /* User-supplied maximum line width (default WIDTH).  The only output
192    lines longer than this will each comprise a single word.  */
193 static int max_width;
194
195 /* Values derived from the option values.  */
196
197 /* The length of prefix minus leading space.  */
198 static int prefix_full_length;
199
200 /* The length of the leading space trimmed from the prefix.  */
201 static int prefix_lead_space;
202
203 /* The length of prefix minus leading and trailing space.  */
204 static int prefix_length;
205
206 /* The preferred width of text lines, set to LEEWAY % less than max_width.  */
207 static int best_width;
208
209 /* Dynamic variables.  */
210
211 /* Start column of the character most recently read from the input file.  */
212 static int in_column;
213
214 /* Start column of the next character to be written to stdout.  */
215 static int out_column;
216
217 /* Space for the paragraph text -- longer paragraphs are handled neatly
218    (cf. flush_paragraph()).  */
219 static char parabuf[MAXCHARS];
220
221 /* A pointer into parabuf, indicating the first unused character position.  */
222 static char *wptr;
223
224 /* The words of a paragraph -- longer paragraphs are handled neatly
225    (cf. flush_paragraph()).  */
226 static WORD word[MAXWORDS];
227
228 /* A pointer into the above word array, indicating the first position
229    after the last complete word.  Sometimes it will point at an incomplete
230    word.  */
231 static WORD *word_limit;
232
233 /* If true, current input file contains tab characters, and so tabs can be
234    used for white space on output.  */
235 static bool tabs;
236
237 /* Space before trimmed prefix on each line of the current paragraph.  */
238 static int prefix_indent;
239
240 /* Indentation of the first line of the current paragraph.  */
241 static int first_indent;
242
243 /* Indentation of other lines of the current paragraph */
244 static int other_indent;
245
246 /* To detect the end of a paragraph, we need to look ahead to the first
247    non-blank character after the prefix on the next line, or the first
248    character on the following line that failed to match the prefix.
249    We can reconstruct the lookahead from that character (next_char), its
250    position on the line (in_column) and the amount of space before the
251    prefix (next_prefix_indent).  See get_paragraph() and copy_rest().  */
252
253 /* The last character read from the input file.  */
254 static int next_char;
255
256 /* The space before the trimmed prefix (or part of it) on the next line
257    after the current paragraph.  */
258 static int next_prefix_indent;
259
260 /* If nonzero, the length of the last line output in the current
261    paragraph, used to charge for raggedness at the split point for long
262    paragraphs chosen by fmt_paragraph().  */
263 static int last_line_length;
264
265 void
266 usage (int status)
267 {
268   if (status != EXIT_SUCCESS)
269     fprintf (stderr, _("Try `%s --help' for more information.\n"),
270              program_name);
271   else
272     {
273       printf (_("Usage: %s [-DIGITS] [OPTION]... [FILE]...\n"), program_name);
274       fputs (_("\
275 Reformat each paragraph in the FILE(s), writing to standard output.\n\
276 If no FILE or if FILE is `-', read standard input.\n\
277 \n\
278 "), stdout);
279       fputs (_("\
280 Mandatory arguments to long options are mandatory for short options too.\n\
281 "), stdout);
282       fputs (_("\
283   -c, --crown-margin        preserve indentation of first two lines\n\
284   -p, --prefix=STRING       reformat only lines beginning with STRING,\n\
285                               reattaching the prefix to reformatted lines\n\
286   -s, --split-only          split long lines, but do not refill\n\
287 "),
288              stdout);
289       fputs (_("\
290   -t, --tagged-paragraph    indentation of first line different from second\n\
291   -u, --uniform-spacing     one space between words, two after sentences\n\
292   -w, --width=WIDTH         maximum line width (default of 75 columns)\n\
293 "), stdout);
294       fputs (HELP_OPTION_DESCRIPTION, stdout);
295       fputs (VERSION_OPTION_DESCRIPTION, stdout);
296       fputs (_("\
297 \n\
298 With no FILE, or when FILE is -, read standard input.\n"),
299              stdout);
300       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
301     }
302   exit (status);
303 }
304
305 /* Decode options and launch execution.  */
306
307 static const struct option long_options[] =
308 {
309   {"crown-margin", no_argument, NULL, 'c'},
310   {"prefix", required_argument, NULL, 'p'},
311   {"split-only", no_argument, NULL, 's'},
312   {"tagged-paragraph", no_argument, NULL, 't'},
313   {"uniform-spacing", no_argument, NULL, 'u'},
314   {"width", required_argument, NULL, 'w'},
315   {GETOPT_HELP_OPTION_DECL},
316   {GETOPT_VERSION_OPTION_DECL},
317   {NULL, 0, NULL, 0},
318 };
319
320 int
321 main (int argc, char **argv)
322 {
323   int optchar;
324   bool ok = true;
325   char const *max_width_option = NULL;
326
327   initialize_main (&argc, &argv);
328   program_name = argv[0];
329   setlocale (LC_ALL, "");
330   bindtextdomain (PACKAGE, LOCALEDIR);
331   textdomain (PACKAGE);
332
333   atexit (close_stdout);
334
335   crown = tagged = split = uniform = false;
336   max_width = WIDTH;
337   prefix = "";
338   prefix_length = prefix_lead_space = prefix_full_length = 0;
339
340   if (argc > 1 && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
341     {
342       /* Old option syntax; a dash followed by one or more digits.  */
343       max_width_option = argv[1] + 1;
344
345       /* Make the option we just parsed invisible to getopt.  */
346       argv[1] = argv[0];
347       argv++;
348       argc--;
349     }
350
351   while ((optchar = getopt_long (argc, argv, "0123456789cstuw:p:",
352                                  long_options, NULL))
353          != -1)
354     switch (optchar)
355       {
356       default:
357         if (ISDIGIT (optchar))
358           error (0, 0, _("invalid option -- %c; -WIDTH is recognized\
359  only when it is the first\noption; use -w N instead"),
360                  optchar);
361         usage (EXIT_FAILURE);
362
363       case 'c':
364         crown = true;
365         break;
366
367       case 's':
368         split = true;
369         break;
370
371       case 't':
372         tagged = true;
373         break;
374
375       case 'u':
376         uniform = true;
377         break;
378
379       case 'w':
380         max_width_option = optarg;
381         break;
382
383       case 'p':
384         set_prefix (optarg);
385         break;
386
387       case_GETOPT_HELP_CHAR;
388
389       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
390
391       }
392
393   if (max_width_option)
394     {
395       /* Limit max_width to MAXCHARS / 2; otherwise, the resulting
396          output can be quite ugly.  */
397       unsigned long int tmp;
398       if (! (xstrtoul (max_width_option, NULL, 10, &tmp, "") == LONGINT_OK
399              && tmp <= MAXCHARS / 2))
400         error (EXIT_FAILURE, 0, _("invalid width: %s"),
401                quote (max_width_option));
402       max_width = tmp;
403     }
404
405   best_width = max_width * (2 * (100 - LEEWAY) + 1) / 200;
406
407   if (optind == argc)
408     fmt (stdin);
409   else
410     {
411       for (; optind < argc; optind++)
412         {
413           char *file = argv[optind];
414           if (STREQ (file, "-"))
415             fmt (stdin);
416           else
417             {
418               FILE *in_stream;
419               in_stream = fopen (file, "r");
420               if (in_stream != NULL)
421                 {
422                   fmt (in_stream);
423                   if (fclose (in_stream) == EOF)
424                     {
425                       error (0, errno, "%s", file);
426                       ok = false;
427                     }
428                 }
429               else
430                 {
431                   error (0, errno, _("cannot open %s for reading"),
432                          quote (file));
433                   ok = false;
434                 }
435             }
436         }
437     }
438
439   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
440 }
441
442 /* Trim space from the front and back of the string P, yielding the prefix,
443    and record the lengths of the prefix and the space trimmed.  */
444
445 static void
446 set_prefix (char *p)
447 {
448   char *s;
449
450   prefix_lead_space = 0;
451   while (*p == ' ')
452     {
453       prefix_lead_space++;
454       p++;
455     }
456   prefix = p;
457   prefix_full_length = strlen (p);
458   s = p + prefix_full_length;
459   while (s > p && s[-1] == ' ')
460     s--;
461   *s = '\0';
462   prefix_length = s - p;
463 }
464
465 /* read file F and send formatted output to stdout.  */
466
467 static void
468 fmt (FILE *f)
469 {
470   tabs = false;
471   other_indent = 0;
472   next_char = get_prefix (f);
473   while (get_paragraph (f))
474     {
475       fmt_paragraph ();
476       put_paragraph (word_limit);
477     }
478 }
479
480 /* Set the global variable `other_indent' according to SAME_PARAGRAPH
481    and other global variables.  */
482
483 static void
484 set_other_indent (bool same_paragraph)
485 {
486   if (split)
487     other_indent = first_indent;
488   else if (crown)
489     {
490       other_indent = (same_paragraph ? in_column : first_indent);
491     }
492   else if (tagged)
493     {
494       if (same_paragraph && in_column != first_indent)
495         {
496           other_indent = in_column;
497         }
498
499       /* Only one line: use the secondary indent from last time if it
500          splits, or 0 if there have been no multi-line paragraphs in the
501          input so far.  But if these rules make the two indents the same,
502          pick a new secondary indent.  */
503
504       else if (other_indent == first_indent)
505         other_indent = first_indent == 0 ? DEF_INDENT : 0;
506     }
507   else
508     {
509       other_indent = first_indent;
510     }
511 }
512
513 /* Read a paragraph from input file F.  A paragraph consists of a
514    maximal number of non-blank (excluding any prefix) lines subject to:
515    * In split mode, a paragraph is a single non-blank line.
516    * In crown mode, the second and subsequent lines must have the
517    same indentation, but possibly different from the indent of the
518    first line.
519    * Tagged mode is similar, but the first and second lines must have
520    different indentations.
521    * Otherwise, all lines of a paragraph must have the same indent.
522    If a prefix is in effect, it must be present at the same indent for
523    each line in the paragraph.
524
525    Return false if end-of-file was encountered before the start of a
526    paragraph, else true.  */
527
528 static bool
529 get_paragraph (FILE *f)
530 {
531   int c;
532
533   last_line_length = 0;
534   c = next_char;
535
536   /* Scan (and copy) blank lines, and lines not introduced by the prefix.  */
537
538   while (c == '\n' || c == EOF
539          || next_prefix_indent < prefix_lead_space
540          || in_column < next_prefix_indent + prefix_full_length)
541     {
542       c = copy_rest (f, c);
543       if (c == EOF)
544         {
545           next_char = EOF;
546           return false;
547         }
548       putchar ('\n');
549       c = get_prefix (f);
550     }
551
552   /* Got a suitable first line for a paragraph.  */
553
554   prefix_indent = next_prefix_indent;
555   first_indent = in_column;
556   wptr = parabuf;
557   word_limit = word;
558   c = get_line (f, c);
559   set_other_indent (same_para (c));
560
561   /* Read rest of paragraph (unless split is specified).  */
562
563   if (split)
564     {
565       /* empty */
566     }
567   else if (crown)
568     {
569       if (same_para (c))
570         {
571           do
572             {                   /* for each line till the end of the para */
573               c = get_line (f, c);
574             }
575           while (same_para (c) && in_column == other_indent);
576         }
577     }
578   else if (tagged)
579     {
580       if (same_para (c) && in_column != first_indent)
581         {
582           do
583             {                   /* for each line till the end of the para */
584               c = get_line (f, c);
585             }
586           while (same_para (c) && in_column == other_indent);
587         }
588     }
589   else
590     {
591       while (same_para (c) && in_column == other_indent)
592         c = get_line (f, c);
593     }
594   (word_limit - 1)->period = (word_limit - 1)->final = true;
595   next_char = c;
596   return true;
597 }
598
599 /* Copy to the output a line that failed to match the prefix, or that
600    was blank after the prefix.  In the former case, C is the character
601    that failed to match the prefix.  In the latter, C is \n or EOF.
602    Return the character (\n or EOF) ending the line.  */
603
604 static int
605 copy_rest (FILE *f, int c)
606 {
607   const char *s;
608
609   out_column = 0;
610   if (in_column > next_prefix_indent || (c != '\n' && c != EOF))
611     {
612       put_space (next_prefix_indent);
613       for (s = prefix; out_column != in_column && *s; out_column++)
614         putchar (*s++);
615       if (c != EOF && c != '\n')
616         put_space (in_column - out_column);
617       if (c == EOF && in_column >= next_prefix_indent + prefix_length)
618         putchar ('\n');
619     }
620   while (c != '\n' && c != EOF)
621     {
622       putchar (c);
623       c = getc (f);
624     }
625   return c;
626 }
627
628 /* Return true if a line whose first non-blank character after the
629    prefix (if any) is C could belong to the current paragraph,
630    otherwise false.  */
631
632 static bool
633 same_para (int c)
634 {
635   return (next_prefix_indent == prefix_indent
636           && in_column >= next_prefix_indent + prefix_full_length
637           && c != '\n' && c != EOF);
638 }
639
640 /* Read a line from input file F, given first non-blank character C
641    after the prefix, and the following indent, and break it into words.
642    A word is a maximal non-empty string of non-white characters.  A word
643    ending in [.?!]["')\]]* and followed by end-of-line or at least two
644    spaces ends a sentence, as in emacs.
645
646    Return the first non-blank character of the next line.  */
647
648 static int
649 get_line (FILE *f, int c)
650 {
651   int start;
652   char *end_of_parabuf;
653   WORD *end_of_word;
654
655   end_of_parabuf = &parabuf[MAXCHARS];
656   end_of_word = &word[MAXWORDS - 2];
657
658   do
659     {                           /* for each word in a line */
660
661       /* Scan word.  */
662
663       word_limit->text = wptr;
664       do
665         {
666           if (wptr == end_of_parabuf)
667             {
668               set_other_indent (true);
669               flush_paragraph ();
670             }
671           *wptr++ = c;
672           c = getc (f);
673         }
674       while (c != EOF && !isspace (c));
675       in_column += word_limit->length = wptr - word_limit->text;
676       check_punctuation (word_limit);
677
678       /* Scan inter-word space.  */
679
680       start = in_column;
681       c = get_space (f, c);
682       word_limit->space = in_column - start;
683       word_limit->final = (c == EOF
684                            || (word_limit->period
685                                && (c == '\n' || word_limit->space > 1)));
686       if (c == '\n' || c == EOF || uniform)
687         word_limit->space = word_limit->final ? 2 : 1;
688       if (word_limit == end_of_word)
689         {
690           set_other_indent (true);
691           flush_paragraph ();
692         }
693       word_limit++;
694     }
695   while (c != '\n' && c != EOF);
696   return get_prefix (f);
697 }
698
699 /* Read a prefix from input file F.  Return either first non-matching
700    character, or first non-blank character after the prefix.  */
701
702 static int
703 get_prefix (FILE *f)
704 {
705   int c;
706
707   in_column = 0;
708   c = get_space (f, getc (f));
709   if (prefix_length == 0)
710     next_prefix_indent = prefix_lead_space < in_column ?
711       prefix_lead_space : in_column;
712   else
713     {
714       const char *p;
715       next_prefix_indent = in_column;
716       for (p = prefix; *p != '\0'; p++)
717         {
718           unsigned char pc = *p;
719           if (c != pc)
720             return c;
721           in_column++;
722           c = getc (f);
723         }
724       c = get_space (f, c);
725     }
726   return c;
727 }
728
729 /* Read blank characters from input file F, starting with C, and keeping
730    in_column up-to-date.  Return first non-blank character.  */
731
732 static int
733 get_space (FILE *f, int c)
734 {
735   for (;;)
736     {
737       if (c == ' ')
738         in_column++;
739       else if (c == '\t')
740         {
741           tabs = true;
742           in_column = (in_column / TABWIDTH + 1) * TABWIDTH;
743         }
744       else
745         return c;
746       c = getc (f);
747     }
748 }
749
750 /* Set extra fields in word W describing any attached punctuation.  */
751
752 static void
753 check_punctuation (WORD *w)
754 {
755   char const *start = w->text;
756   char const *finish = start + (w->length - 1);
757   unsigned char fin = *finish;
758
759   w->paren = isopen (*start);
760   w->punct = !! ispunct (fin);
761   while (start < finish && isclose (*finish))
762     finish--;
763   w->period = isperiod (*finish);
764 }
765
766 /* Flush part of the paragraph to make room.  This function is called on
767    hitting the limit on the number of words or characters.  */
768
769 static void
770 flush_paragraph (void)
771 {
772   WORD *split_point;
773   WORD *w;
774   int shift;
775   COST best_break;
776
777   /* In the special case where it's all one word, just flush it.  */
778
779   if (word_limit == word)
780     {
781       fwrite (parabuf, sizeof *parabuf, wptr - parabuf, stdout);
782       wptr = parabuf;
783       return;
784     }
785
786   /* Otherwise:
787      - format what you have so far as a paragraph,
788      - find a low-cost line break near the end,
789      - output to there,
790      - make that the start of the paragraph.  */
791
792   fmt_paragraph ();
793
794   /* Choose a good split point.  */
795
796   split_point = word_limit;
797   best_break = MAXCOST;
798   for (w = word->next_break; w != word_limit; w = w->next_break)
799     {
800       if (w->best_cost - w->next_break->best_cost < best_break)
801         {
802           split_point = w;
803           best_break = w->best_cost - w->next_break->best_cost;
804         }
805       if (best_break <= MAXCOST - LINE_CREDIT)
806         best_break += LINE_CREDIT;
807     }
808   put_paragraph (split_point);
809
810   /* Copy text of words down to start of parabuf -- we use memmove because
811      the source and target may overlap.  */
812
813   memmove (parabuf, split_point->text, wptr - split_point->text);
814   shift = split_point->text - parabuf;
815   wptr -= shift;
816
817   /* Adjust text pointers.  */
818
819   for (w = split_point; w <= word_limit; w++)
820     w->text -= shift;
821
822   /* Copy words from split_point down to word -- we use memmove because
823      the source and target may overlap.  */
824
825   memmove (word, split_point, (word_limit - split_point + 1) * sizeof *word);
826   word_limit -= split_point - word;
827 }
828
829 /* Compute the optimal formatting for the whole paragraph by computing
830    and remembering the optimal formatting for each suffix from the empty
831    one to the whole paragraph.  */
832
833 static void
834 fmt_paragraph (void)
835 {
836   WORD *start, *w;
837   int len;
838   COST wcost, best;
839   int saved_length;
840
841   word_limit->best_cost = 0;
842   saved_length = word_limit->length;
843   word_limit->length = max_width;       /* sentinel */
844
845   for (start = word_limit - 1; start >= word; start--)
846     {
847       best = MAXCOST;
848       len = start == word ? first_indent : other_indent;
849
850       /* At least one word, however long, in the line.  */
851
852       w = start;
853       len += w->length;
854       do
855         {
856           w++;
857
858           /* Consider breaking before w.  */
859
860           wcost = line_cost (w, len) + w->best_cost;
861           if (start == word && last_line_length > 0)
862             wcost += RAGGED_COST (len - last_line_length);
863           if (wcost < best)
864             {
865               best = wcost;
866               start->next_break = w;
867               start->line_length = len;
868             }
869
870           /* This is a kludge to keep us from computing `len' as the
871              sum of the sentinel length and some non-zero number.
872              Since the sentinel w->length may be INT_MAX, adding
873              to that would give a negative result.  */
874           if (w == word_limit)
875             break;
876
877           len += (w - 1)->space + w->length;    /* w > start >= word */
878         }
879       while (len < max_width);
880       start->best_cost = best + base_cost (start);
881     }
882
883   word_limit->length = saved_length;
884 }
885
886 /* Return the constant component of the cost of breaking before the
887    word THIS.  */
888
889 static COST
890 base_cost (WORD *this)
891 {
892   COST cost;
893
894   cost = LINE_COST;
895
896   if (this > word)
897     {
898       if ((this - 1)->period)
899         {
900           if ((this - 1)->final)
901             cost -= SENTENCE_BONUS;
902           else
903             cost += NOBREAK_COST;
904         }
905       else if ((this - 1)->punct)
906         cost -= PUNCT_BONUS;
907       else if (this > word + 1 && (this - 2)->final)
908         cost += WIDOW_COST ((this - 1)->length);
909     }
910
911   if (this->paren)
912     cost -= PAREN_BONUS;
913   else if (this->final)
914     cost += ORPHAN_COST (this->length);
915
916   return cost;
917 }
918
919 /* Return the component of the cost of breaking before word NEXT that
920    depends on LEN, the length of the line beginning there.  */
921
922 static COST
923 line_cost (WORD *next, int len)
924 {
925   int n;
926   COST cost;
927
928   if (next == word_limit)
929     return 0;
930   n = best_width - len;
931   cost = SHORT_COST (n);
932   if (next->next_break != word_limit)
933     {
934       n = len - next->line_length;
935       cost += RAGGED_COST (n);
936     }
937   return cost;
938 }
939
940 /* Output to stdout a paragraph from word up to (but not including)
941    FINISH, which must be in the next_break chain from word.  */
942
943 static void
944 put_paragraph (WORD *finish)
945 {
946   WORD *w;
947
948   put_line (word, first_indent);
949   for (w = word->next_break; w != finish; w = w->next_break)
950     put_line (w, other_indent);
951 }
952
953 /* Output to stdout the line beginning with word W, beginning in column
954    INDENT, including the prefix (if any).  */
955
956 static void
957 put_line (WORD *w, int indent)
958 {
959   WORD *endline;
960
961   out_column = 0;
962   put_space (prefix_indent);
963   fputs (prefix, stdout);
964   out_column += prefix_length;
965   put_space (indent - out_column);
966
967   endline = w->next_break - 1;
968   for (; w != endline; w++)
969     {
970       put_word (w);
971       put_space (w->space);
972     }
973   put_word (w);
974   last_line_length = out_column;
975   putchar ('\n');
976 }
977
978 /* Output to stdout the word W.  */
979
980 static void
981 put_word (WORD *w)
982 {
983   const char *s;
984   int n;
985
986   s = w->text;
987   for (n = w->length; n != 0; n--)
988     putchar (*s++);
989   out_column += w->length;
990 }
991
992 /* Output to stdout SPACE spaces, or equivalent tabs.  */
993
994 static void
995 put_space (int space)
996 {
997   int space_target, tab_target;
998
999   space_target = out_column + space;
1000   if (tabs)
1001     {
1002       tab_target = space_target / TABWIDTH * TABWIDTH;
1003       if (out_column + 1 < tab_target)
1004         while (out_column < tab_target)
1005           {
1006             putchar ('\t');
1007             out_column = (out_column / TABWIDTH + 1) * TABWIDTH;
1008           }
1009     }
1010   while (out_column < space_target)
1011     {
1012       putchar (' ');
1013       out_column++;
1014     }
1015 }