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