*** empty log message ***
[coreutils.git] / src / fmt.c
blobf86513f33cbd34a99b5848fcd217b319c451b9f8
1 /* GNU fmt -- simple text formatter.
2 Copyright (C) 1994-2001 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)
7 any later version.
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>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <getopt.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
29 #include "system.h"
30 #include "closeout.h"
31 #include "error.h"
32 #include "xstrtol.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "fmt"
37 #define AUTHORS "Ross Paterson"
39 /* The following parameters represent the program's idea of what is
40 "best". Adjust to taste, subject to the caveats given. */
42 /* Default longest permitted line length (max_width). */
43 #define WIDTH 75
45 /* Prefer lines to be LEEWAY % shorter than the maximum width, giving
46 room for optimization. */
47 #define LEEWAY 7
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
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. */
59 typedef long COST;
61 #define MAXCOST TYPE_MAXIMUM (COST)
63 #define SQR(n) ((n) * (n))
64 #define EQUIV(n) SQR ((COST) (n))
66 /* Cost of a filled line n chars longer or shorter than best_width. */
67 #define SHORT_COST(n) EQUIV ((n) * 10)
69 /* Cost of the difference between adjacent filled lines. */
70 #define RAGGED_COST(n) (SHORT_COST (n) / 2)
72 /* Basic cost per line. */
73 #define LINE_COST EQUIV (70)
75 /* Cost of breaking a line after the first word of a sentence, where
76 the length of the word is N. */
77 #define WIDOW_COST(n) (EQUIV (200) / ((n) + 2))
79 /* Cost of breaking a line before the last word of a sentence, where
80 the length of the word is N. */
81 #define ORPHAN_COST(n) (EQUIV (150) / ((n) + 2))
83 /* Bonus for breaking a line at the end of a sentence. */
84 #define SENTENCE_BONUS EQUIV (50)
86 /* Cost of breaking a line after a period not marking end of a sentence.
87 With the definition of sentence we are using (borrowed from emacs, see
88 get_line()) such a break would then look like a sentence break. Hence
89 we assign a very high cost -- it should be avoided unless things are
90 really bad. */
91 #define NOBREAK_COST EQUIV (600)
93 /* Bonus for breaking a line before open parenthesis. */
94 #define PAREN_BONUS EQUIV (40)
96 /* Bonus for breaking a line after other punctuation. */
97 #define PUNCT_BONUS EQUIV(40)
99 /* Credit for breaking a long paragraph one line later. */
100 #define LINE_CREDIT EQUIV(3)
102 /* Size of paragraph buffer, in words and characters. Longer paragraphs
103 are handled neatly (cf. flush_paragraph()), so there's little to gain
104 by making these larger. */
105 #define MAXWORDS 1000
106 #define MAXCHARS 5000
108 /* Extra ctype(3)-style macros. */
110 #define isopen(c) (strchr ("([`'\"", c) != NULL)
111 #define isclose(c) (strchr (")]'\"", c) != NULL)
112 #define isperiod(c) (strchr (".?!", c) != NULL)
114 /* Size of a tab stop, for expansion on input and re-introduction on
115 output. */
116 #define TABWIDTH 8
118 /* Miscellaneous definitions. */
120 #undef TRUE
121 #define TRUE true
122 #undef FALSE
123 #define FALSE false
125 /* Word descriptor structure. */
127 typedef struct Word WORD;
129 struct Word
132 /* Static attributes determined during input. */
134 const char *text; /* the text of the word */
135 int length; /* length of this word */
136 int 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 int 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;
172 /* Option values. */
174 /* If TRUE, first 2 lines may have different indent (default FALSE). */
175 static bool crown;
177 /* If TRUE, first 2 lines _must_ have different indent (default FALSE). */
178 static bool tagged;
180 /* If TRUE, each line is a paragraph on its own (default FALSE). */
181 static bool split;
183 /* If TRUE, don't preserve inter-word spacing (default FALSE). */
184 static bool uniform;
186 /* Prefix minus leading and trailing spaces (default ""). */
187 static const char *prefix;
189 /* User-supplied maximum line width (default WIDTH). The only output
190 lines longer than this will each comprise a single word. */
191 static int max_width;
193 /* Values derived from the option values. */
195 /* The length of prefix minus leading space. */
196 static int prefix_full_length;
198 /* The length of the leading space trimmed from the prefix. */
199 static int prefix_lead_space;
201 /* The length of prefix minus leading and trailing space. */
202 static int prefix_length;
204 /* The preferred width of text lines, set to LEEWAY % less than max_width. */
205 static int best_width;
207 /* Dynamic variables. */
209 /* Start column of the character most recently read from the input file. */
210 static int in_column;
212 /* Start column of the next character to be written to stdout. */
213 static int out_column;
215 /* Space for the paragraph text -- longer paragraphs are handled neatly
216 (cf. flush_paragraph()). */
217 static char parabuf[MAXCHARS];
219 /* A pointer into parabuf, indicating the first unused character position. */
220 static char *wptr;
222 /* The words of a paragraph -- longer paragraphs are handled neatly
223 (cf. flush_paragraph()). */
224 static WORD word[MAXWORDS];
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;
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;
235 /* Space before trimmed prefix on each line of the current paragraph. */
236 static int prefix_indent;
238 /* Indentation of the first line of the current paragraph. */
239 static int first_indent;
241 /* Indentation of other lines of the current paragraph */
242 static int other_indent;
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(). */
251 /* The last character read from the input file. */
252 static int next_char;
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;
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;
263 void
264 usage (int status)
266 if (status != 0)
267 fprintf (stderr, _("Try `%s --help' for more information.\n"),
268 program_name);
269 else
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\
276 "), stdout);
277 fputs (_("\
278 Mandatory arguments to long options are mandatory for short options too.\n\
279 "), stdout);
280 fputs (_("\
281 -c, --crown-margin preserve indentation of first two lines\n\
282 -p, --prefix=STRING combine only lines having STRING as prefix\n\
283 -s, --split-only split long lines, but do not refill\n\
285 stdout);
286 fputs (_("\
287 -t, --tagged-paragraph indentation of first line different from second\n\
288 -u, --uniform-spacing one space between words, two after sentences\n\
289 -w, --width=NUMBER maximum line width (default of 75 columns)\n\
290 "), stdout);
291 fputs (HELP_OPTION_DESCRIPTION, stdout);
292 fputs (VERSION_OPTION_DESCRIPTION, stdout);
293 fputs (_("\
295 In -wNUMBER, the letter `w' may be omitted.\n"),
296 stdout);
297 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
299 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
302 /* Decode options and launch execution. */
304 static const struct option long_options[] =
306 {"crown-margin", no_argument, NULL, 'c'},
307 {"prefix", required_argument, NULL, 'p'},
308 {"split-only", no_argument, NULL, 's'},
309 {"tagged-paragraph", no_argument, NULL, 't'},
310 {"uniform-spacing", no_argument, NULL, 'u'},
311 {"width", required_argument, NULL, 'w'},
312 {GETOPT_HELP_OPTION_DECL},
313 {GETOPT_VERSION_OPTION_DECL},
314 {0, 0, 0, 0},
318 main (register int argc, register char **argv)
320 int optchar;
322 program_name = argv[0];
323 setlocale (LC_ALL, "");
324 bindtextdomain (PACKAGE, LOCALEDIR);
325 textdomain (PACKAGE);
327 atexit (close_stdout);
329 crown = tagged = split = uniform = FALSE;
330 max_width = WIDTH;
331 prefix = "";
332 prefix_length = prefix_lead_space = prefix_full_length = 0;
334 if (argc > 1 && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
336 const char *s = argv[1] + 1;
337 max_width = 0;
338 /* Old option syntax; a dash followed by one or more digits.
339 Move past the number. */
340 for (; ISDIGIT (*s); ++s)
342 int old_max = max_width;
343 max_width = max_width * 10 + *s - '0';
344 if (INT_MAX / 10 < old_max || max_width < old_max)
345 error (EXIT_FAILURE, 0, _("invalid width option: `%s'"), argv[1]);
347 /* Make the options we just parsed invisible to getopt. */
348 argv[1] = argv[0];
349 argv++;
350 argc--;
353 while ((optchar = getopt_long (argc, argv, "0123456789cstuw:p:",
354 long_options, NULL))
355 != -1)
356 switch (optchar)
358 default:
359 usage (1);
361 case 0:
362 break;
364 case 'c':
365 crown = TRUE;
366 break;
368 case 's':
369 split = TRUE;
370 break;
372 case 't':
373 tagged = TRUE;
374 break;
376 case 'u':
377 uniform = TRUE;
378 break;
380 case 'w':
382 long int tmp_long;
383 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
384 || tmp_long <= 0 || tmp_long > INT_MAX)
385 error (EXIT_FAILURE, 0, _("invalid width: `%s'"),
386 optarg);
387 max_width = (int) tmp_long;
389 break;
391 case 'p':
392 set_prefix (optarg);
393 break;
395 case_GETOPT_HELP_CHAR;
397 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
401 best_width = max_width * (2 * (100 - LEEWAY) + 1) / 200;
403 if (optind == argc)
404 fmt (stdin);
405 else
407 for (; optind < argc; optind++)
409 char *file = argv[optind];
410 if (STREQ (file, "-"))
411 fmt (stdin);
412 else
414 FILE *in_stream;
415 in_stream = fopen (file, "r");
416 if (in_stream != NULL)
418 fmt (in_stream);
419 if (fclose (in_stream) == EOF)
420 error (EXIT_FAILURE, errno, "%s", file);
422 else
423 error (0, errno, "%s", file);
428 exit (EXIT_SUCCESS);
431 /* Trim space from the front and back of the string P, yielding the prefix,
432 and record the lengths of the prefix and the space trimmed. */
434 static void
435 set_prefix (register char *p)
437 register char *s;
439 prefix_lead_space = 0;
440 while (*p == ' ')
442 prefix_lead_space++;
443 p++;
445 prefix = p;
446 prefix_full_length = strlen (p);
447 s = p + prefix_full_length;
448 while (s > p && s[-1] == ' ')
449 s--;
450 *s = '\0';
451 prefix_length = s - p;
454 /* read file F and send formatted output to stdout. */
456 static void
457 fmt (FILE *f)
459 tabs = FALSE;
460 other_indent = 0;
461 next_char = get_prefix (f);
462 while (get_paragraph (f))
464 fmt_paragraph ();
465 put_paragraph (word_limit);
469 /* Read a paragraph from input file F. A paragraph consists of a
470 maximal number of non-blank (excluding any prefix) lines subject to:
471 * In split mode, a paragraph is a single non-blank line.
472 * In crown mode, the second and subsequent lines must have the
473 same indentation, but possibly different from the indent of the
474 first line.
475 * Tagged mode is similar, but the first and second lines must have
476 different indentations.
477 * Otherwise, all lines of a paragraph must have the same indent.
478 If a prefix is in effect, it must be present at the same indent for
479 each line in the paragraph.
481 Return FALSE if end-of-file was encountered before the start of a
482 paragraph, else TRUE. */
484 static bool
485 get_paragraph (FILE *f)
487 register int c;
489 last_line_length = 0;
490 c = next_char;
492 /* Scan (and copy) blank lines, and lines not introduced by the prefix. */
494 while (c == '\n' || c == EOF
495 || next_prefix_indent < prefix_lead_space
496 || in_column < next_prefix_indent + prefix_full_length)
498 c = copy_rest (f, c);
499 if (c == EOF)
501 next_char = EOF;
502 return FALSE;
504 putchar ('\n');
505 c = get_prefix (f);
508 /* Got a suitable first line for a paragraph. */
510 prefix_indent = next_prefix_indent;
511 first_indent = in_column;
512 wptr = parabuf;
513 word_limit = word;
514 c = get_line (f, c);
516 /* Read rest of paragraph (unless split is specified). */
518 if (split)
519 other_indent = first_indent;
520 else if (crown)
522 if (same_para (c))
524 other_indent = in_column;
526 { /* for each line till the end of the para */
527 c = get_line (f, c);
529 while (same_para (c) && in_column == other_indent);
531 else
532 other_indent = first_indent;
534 else if (tagged)
536 if (same_para (c) && in_column != first_indent)
538 other_indent = in_column;
540 { /* for each line till the end of the para */
541 c = get_line (f, c);
543 while (same_para (c) && in_column == other_indent);
546 /* Only one line: use the secondary indent from last time if it
547 splits, or 0 if there have been no multi-line paragraphs in the
548 input so far. But if these rules make the two indents the same,
549 pick a new secondary indent. */
551 else if (other_indent == first_indent)
552 other_indent = first_indent == 0 ? DEF_INDENT : 0;
554 else
556 other_indent = first_indent;
557 while (same_para (c) && in_column == other_indent)
558 c = get_line (f, c);
560 (word_limit - 1)->period = (word_limit - 1)->final = TRUE;
561 next_char = c;
562 return TRUE;
565 /* Copy to the output a line that failed to match the prefix, or that
566 was blank after the prefix. In the former case, C is the character
567 that failed to match the prefix. In the latter, C is \n or EOF.
568 Return the character (\n or EOF) ending the line. */
570 static int
571 copy_rest (FILE *f, register int c)
573 register const char *s;
575 out_column = 0;
576 if (in_column > next_prefix_indent && c != '\n' && c != EOF)
578 put_space (next_prefix_indent);
579 for (s = prefix; out_column != in_column && *s; out_column++)
580 putchar (*(unsigned char *)s++);
581 put_space (in_column - out_column);
583 while (c != '\n' && c != EOF)
585 putchar (c);
586 c = getc (f);
588 return c;
591 /* Return TRUE if a line whose first non-blank character after the
592 prefix (if any) is C could belong to the current paragraph,
593 otherwise FALSE. */
595 static bool
596 same_para (register int c)
598 return (next_prefix_indent == prefix_indent
599 && in_column >= next_prefix_indent + prefix_full_length
600 && c != '\n' && c != EOF);
603 /* Read a line from input file F, given first non-blank character C
604 after the prefix, and the following indent, and break it into words.
605 A word is a maximal non-empty string of non-white characters. A word
606 ending in [.?!]["')\]]* and followed by end-of-line or at least two
607 spaces ends a sentence, as in emacs.
609 Return the first non-blank character of the next line. */
611 static int
612 get_line (FILE *f, register int c)
614 int start;
615 register char *end_of_parabuf;
616 register WORD *end_of_word;
618 end_of_parabuf = &parabuf[MAXCHARS];
619 end_of_word = &word[MAXWORDS - 2];
622 { /* for each word in a line */
624 /* Scan word. */
626 word_limit->text = wptr;
629 if (wptr == end_of_parabuf)
630 flush_paragraph ();
631 *wptr++ = c;
632 c = getc (f);
634 while (c != EOF && !ISSPACE (c));
635 in_column += word_limit->length = wptr - word_limit->text;
636 check_punctuation (word_limit);
638 /* Scan inter-word space. */
640 start = in_column;
641 c = get_space (f, c);
642 word_limit->space = in_column - start;
643 word_limit->final = (c == EOF
644 || (word_limit->period
645 && (c == '\n' || word_limit->space > 1)));
646 if (c == '\n' || c == EOF || uniform)
647 word_limit->space = word_limit->final ? 2 : 1;
648 if (word_limit == end_of_word)
649 flush_paragraph ();
650 word_limit++;
651 if (c == EOF)
652 return EOF;
654 while (c != '\n');
655 return get_prefix (f);
658 /* Read a prefix from input file F. Return either first non-matching
659 character, or first non-blank character after the prefix. */
661 static int
662 get_prefix (FILE *f)
664 register int c;
666 in_column = 0;
667 c = get_space (f, getc (f));
668 if (prefix_length == 0)
669 next_prefix_indent = prefix_lead_space < in_column ?
670 prefix_lead_space : in_column;
671 else
673 const char *p;
674 next_prefix_indent = in_column;
675 for (p = prefix; *p != '\0'; p++)
677 if (c != *(unsigned char *)p)
678 return c;
679 in_column++;
680 c = getc (f);
682 c = get_space (f, c);
684 return c;
687 /* Read blank characters from input file F, starting with C, and keeping
688 in_column up-to-date. Return first non-blank character. */
690 static int
691 get_space (FILE *f, register int c)
693 for (;;)
695 if (c == ' ')
696 in_column++;
697 else if (c == '\t')
699 tabs = TRUE;
700 in_column = (in_column / TABWIDTH + 1) * TABWIDTH;
702 else
703 return c;
704 c = getc (f);
708 /* Set extra fields in word W describing any attached punctuation. */
710 static void
711 check_punctuation (register WORD *w)
713 const unsigned char *start, *finish;
715 start = (unsigned char *) w->text;
716 finish = start + (w->length - 1);
717 w->paren = isopen (*start);
718 w->punct = ISPUNCT (*finish);
719 while (isclose (*finish) && finish > start)
720 finish--;
721 w->period = isperiod (*finish);
724 /* Flush part of the paragraph to make room. This function is called on
725 hitting the limit on the number of words or characters. */
727 static void
728 flush_paragraph (void)
730 WORD *split_point;
731 register WORD *w;
732 int shift;
733 COST best_break;
735 /* In the special case where it's all one word, just flush it. */
737 if (word_limit == word)
739 printf ("%*s", wptr - parabuf, parabuf);
740 wptr = parabuf;
741 return;
744 /* Otherwise:
745 - format what you have so far as a paragraph,
746 - find a low-cost line break near the end,
747 - output to there,
748 - make that the start of the paragraph. */
750 fmt_paragraph ();
752 /* Choose a good split point. */
754 split_point = word_limit;
755 best_break = MAXCOST;
756 for (w = word->next_break; w != word_limit; w = w->next_break)
758 if (w->best_cost - w->next_break->best_cost < best_break)
760 split_point = w;
761 best_break = w->best_cost - w->next_break->best_cost;
763 if (best_break <= MAXCOST - LINE_CREDIT)
764 best_break += LINE_CREDIT;
766 put_paragraph (split_point);
768 /* Copy text of words down to start of parabuf -- we use memmove because
769 the source and target may overlap. */
771 memmove (parabuf, split_point->text, (size_t) (wptr - split_point->text));
772 shift = split_point->text - parabuf;
773 wptr -= shift;
775 /* Adjust text pointers. */
777 for (w = split_point; w <= word_limit; w++)
778 w->text -= shift;
780 /* Copy words from split_point down to word -- we use memmove because
781 the source and target may overlap. */
783 memmove ((char *) word, (char *) split_point,
784 (word_limit - split_point + 1) * sizeof (WORD));
785 word_limit -= split_point - word;
788 /* Compute the optimal formatting for the whole paragraph by computing
789 and remembering the optimal formatting for each suffix from the empty
790 one to the whole paragraph. */
792 static void
793 fmt_paragraph (void)
795 register WORD *start, *w;
796 register int len;
797 register COST wcost, best;
798 int saved_length;
800 word_limit->best_cost = 0;
801 saved_length = word_limit->length;
802 word_limit->length = max_width; /* sentinel */
804 for (start = word_limit - 1; start >= word; start--)
806 best = MAXCOST;
807 len = start == word ? first_indent : other_indent;
809 /* At least one word, however long, in the line. */
811 w = start;
812 len += w->length;
815 w++;
817 /* Consider breaking before w. */
819 wcost = line_cost (w, len) + w->best_cost;
820 if (start == word && last_line_length > 0)
821 wcost += RAGGED_COST (len - last_line_length);
822 if (wcost < best)
824 best = wcost;
825 start->next_break = w;
826 start->line_length = len;
829 /* This is a kludge to keep us from computing `len' as the
830 sum of the sentinel length and some non-zero number.
831 Since the sentinel w->length may be INT_MAX, adding
832 to that would give a negative result. */
833 if (w == word_limit)
834 break;
836 len += (w - 1)->space + w->length; /* w > start >= word */
838 while (len < max_width);
839 start->best_cost = best + base_cost (start);
842 word_limit->length = saved_length;
845 /* Return the constant component of the cost of breaking before the
846 word THIS. */
848 static COST
849 base_cost (register WORD *this)
851 register COST cost;
853 cost = LINE_COST;
855 if (this > word)
857 if ((this - 1)->period)
859 if ((this - 1)->final)
860 cost -= SENTENCE_BONUS;
861 else
862 cost += NOBREAK_COST;
864 else if ((this - 1)->punct)
865 cost -= PUNCT_BONUS;
866 else if (this > word + 1 && (this - 2)->final)
867 cost += WIDOW_COST ((this - 1)->length);
870 if (this->paren)
871 cost -= PAREN_BONUS;
872 else if (this->final)
873 cost += ORPHAN_COST (this->length);
875 return cost;
878 /* Return the component of the cost of breaking before word NEXT that
879 depends on LEN, the length of the line beginning there. */
881 static COST
882 line_cost (register WORD *next, register int len)
884 register int n;
885 register COST cost;
887 if (next == word_limit)
888 return 0;
889 n = best_width - len;
890 cost = SHORT_COST (n);
891 if (next->next_break != word_limit)
893 n = len - next->line_length;
894 cost += RAGGED_COST (n);
896 return cost;
899 /* Output to stdout a paragraph from word up to (but not including)
900 FINISH, which must be in the next_break chain from word. */
902 static void
903 put_paragraph (register WORD *finish)
905 register WORD *w;
907 put_line (word, first_indent);
908 for (w = word->next_break; w != finish; w = w->next_break)
909 put_line (w, other_indent);
912 /* Output to stdout the line beginning with word W, beginning in column
913 INDENT, including the prefix (if any). */
915 static void
916 put_line (register WORD *w, int indent)
918 register WORD *endline;
920 out_column = 0;
921 put_space (prefix_indent);
922 fputs (prefix, stdout);
923 out_column += prefix_length;
924 put_space (indent - out_column);
926 endline = w->next_break - 1;
927 for (; w != endline; w++)
929 put_word (w);
930 put_space (w->space);
932 put_word (w);
933 last_line_length = out_column;
934 putchar ('\n');
937 /* Output to stdout the word W. */
939 static void
940 put_word (register WORD *w)
942 register const char *s;
943 register int n;
945 s = w->text;
946 for (n = w->length; n != 0; n--)
947 putchar (*s++);
948 out_column += w->length;
951 /* Output to stdout SPACE spaces, or equivalent tabs. */
953 static void
954 put_space (int space)
956 register int space_target, tab_target;
958 space_target = out_column + space;
959 if (tabs)
961 tab_target = space_target / TABWIDTH * TABWIDTH;
962 if (out_column + 1 < tab_target)
963 while (out_column < tab_target)
965 putchar ('\t');
966 out_column = (out_column / TABWIDTH + 1) * TABWIDTH;
969 while (out_column < space_target)
971 putchar (' ');
972 out_column++;