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