usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / usr.bin / fmt / fmt.c
blobf4720d5cb11a99a3ced321799a9b49f23ee4e997
1 /* $OpenBSD: fmt.c,v 1.16 2000/06/25 15:35:42 pjanzen Exp $ */
3 /* Sensible version of fmt
5 * Syntax: fmt [ options ] [ goal [ max ] ] [ filename ... ]
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.
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.
68 * Multiple input files are formatted one at a time, so that a file
69 * never ends in the middle of a line.
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'.
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'.)
83 * Differences from old `fmt':
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'.
109 * Bugs:
111 * None known. There probably are some, though.
113 * Portability:
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.
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.
125 * $FreeBSD: head/usr.bin/fmt/fmt.c 200462 2009-12-13 03:14:06Z delphij $
128 /* Copyright (c) 1997 Gareth McCaughan. All rights reserved.
130 * Redistribution and use of this code, in source or binary forms,
131 * with or without modification, are permitted subject to the following
132 * conditions:
134 * - Redistribution of source code must retain the above copyright
135 * notice, this list of conditions and the following disclaimer.
137 * - If you distribute modified source code it must also include
138 * a notice saying that it has been modified, and giving a brief
139 * description of what changes have been made.
141 * Disclaimer: I am not responsible for the results of using this code.
142 * If it formats your hard disc, sends obscene messages to
143 * your boss and kills your children then that's your problem
144 * not mine. I give absolutely no warranty of any sort as to
145 * what the program will do, and absolutely refuse to be held
146 * liable for any consequences of your using it.
147 * Thank you. Have a nice day.
150 /* RCS change log:
151 * Revision 1.5 1998/03/02 18:02:21 gjm11
152 * Minor changes for portability.
154 * Revision 1.4 1997/10/01 11:51:28 gjm11
155 * Repair broken indented-paragraph handling.
156 * Add mail message header stuff.
157 * Improve comments and layout.
158 * Make usable with non-BSD systems.
159 * Add revision display to usage message.
161 * Revision 1.3 1997/09/30 16:24:47 gjm11
162 * Add copyright notice, rcsid string and log message.
164 * Revision 1.2 1997/09/30 16:13:39 gjm11
165 * Add options: -d <chars>, -l <width>, -p, -s, -t <width>, -h .
166 * Parse options with `getopt'. Clean up code generally.
167 * Make comments more accurate.
169 * Revision 1.1 1997/09/30 11:29:57 gjm11
170 * Initial revision
173 #include <err.h>
174 #include <limits.h>
175 #include <locale.h>
176 #include <stdio.h>
177 #include <stdlib.h>
178 #include <string.h>
179 #include <sysexits.h>
180 #include <unistd.h>
181 #include <wchar.h>
182 #include <wctype.h>
184 /* Something that, we hope, will never be a genuine line length,
185 * indentation etc.
187 #define SILLY ((size_t)-1)
189 /* I used to use |strtoul| for this, but (1) not all systems have it
190 * and (2) it's probably better to use |strtol| to detect negative
191 * numbers better.
192 * If |fussyp==0| then we don't complain about non-numbers
193 * (returning 0 instead), but we do complain about bad numbers.
195 static size_t
196 get_positive(const char *s, const char *err_mess, int fussyP) {
197 char *t;
198 long result = strtol(s,&t,0);
199 if (*t) { if (fussyP) goto Lose; else return 0; }
200 if (result<=0) { Lose: errx(EX_USAGE, "%s", err_mess); }
201 return (size_t) result;
204 static size_t
205 get_nonnegative(const char *s, const char *err_mess, int fussyP) {
206 char *t;
207 long result = strtol(s,&t,0);
208 if (*t) { if (fussyP) goto Lose; else return 0; }
209 if (result<0) { Lose: errx(EX_USAGE, "%s", err_mess); }
210 return (size_t) result;
213 /* Global variables */
215 static int centerP=0; /* Try to center lines? */
216 static size_t goal_length=0; /* Target length for output lines */
217 static size_t max_length=0; /* Maximum length for output lines */
218 static int coalesce_spaces_P=0; /* Coalesce multiple whitespace -> ' ' ? */
219 static int allow_indented_paragraphs=0; /* Can first line have diff. ind.? */
220 static int tab_width=8; /* Number of spaces per tab stop */
221 static size_t output_tab_width=8; /* Ditto, when squashing leading spaces */
222 static const wchar_t *sentence_enders=L".?!"; /* Double-space after these */
223 static int grok_mail_headers=0; /* treat embedded mail headers magically? */
224 static int format_troff=0; /* Format troff? */
226 static int n_errors=0; /* Number of failed files. Return on exit. */
227 static wchar_t *output_buffer=NULL; /* Output line will be built here */
228 static size_t x; /* Horizontal position in output line */
229 static size_t x0; /* Ditto, ignoring leading whitespace */
230 static size_t output_buffer_length = 0;
231 static size_t pending_spaces; /* Spaces to add before next word */
232 static int output_in_paragraph=0; /* Any of current para written out yet? */
234 /* Prototypes */
236 static void process_named_file (const char *);
237 static void process_stream (FILE *, const char *);
238 static size_t indent_length (const wchar_t *, size_t);
239 static int might_be_header (const wchar_t *);
240 static void new_paragraph (size_t, size_t);
241 static void output_word (size_t, size_t, const wchar_t *, size_t,
242 size_t);
243 static void output_indent (size_t);
244 static void center_stream (FILE *, const char *);
245 static wchar_t * get_line (FILE *, size_t *);
246 static void * xrealloc (void *, size_t);
248 #define XMALLOC(x) xrealloc(0,x)
250 /* Here is perhaps the right place to mention that this code is
251 * all in top-down order. Hence, |main| comes first.
254 main(int argc, char *argv[]) {
255 int ch; /* used for |getopt| processing */
256 wchar_t *tmp;
257 size_t len;
258 const char *src;
260 (void) setlocale(LC_CTYPE, "");
262 /* 1. Grok parameters. */
264 while ((ch = getopt(argc, argv, "0123456789cd:hl:mnpst:w:")) != -1)
265 switch(ch) {
266 case 'c':
267 centerP = 1;
268 format_troff = 1;
269 continue;
270 case 'd':
271 src = optarg;
272 len = mbsrtowcs(NULL, &src, 0, NULL);
273 if (len == (size_t)-1)
274 err(EX_USAGE, "bad sentence-ending character set");
275 tmp = XMALLOC((len + 1) * sizeof(wchar_t));
276 mbsrtowcs(tmp, &src, len + 1, NULL);
277 sentence_enders = tmp;
278 continue;
279 case 'l':
280 output_tab_width
281 = get_nonnegative(optarg, "output tab width must be non-negative", 1);
282 continue;
283 case 'm':
284 grok_mail_headers = 1;
285 continue;
286 case 'n':
287 format_troff = 1;
288 continue;
289 case 'p':
290 allow_indented_paragraphs = 1;
291 continue;
292 case 's':
293 coalesce_spaces_P = 1;
294 continue;
295 case 't':
296 tab_width = get_positive(optarg, "tab width must be positive", 1);
297 continue;
298 case 'w':
299 goal_length = get_positive(optarg, "width must be positive", 1);
300 max_length = goal_length;
301 continue;
302 case '0': case '1': case '2': case '3': case '4': case '5':
303 case '6': case '7': case '8': case '9':
304 /* XXX this is not a stylistically approved use of getopt() */
305 if (goal_length==0) {
306 char *p;
307 p = argv[optind - 1];
308 if (p[0] == '-' && p[1] == ch && !p[2])
309 goal_length = get_positive(++p, "width must be nonzero", 1);
310 else
311 goal_length = get_positive(argv[optind]+1,
312 "width must be nonzero", 1);
313 max_length = goal_length;
315 continue;
316 case 'h': default:
317 fprintf(stderr,
318 "usage: fmt [-cmps] [-d chars] [-l num] [-t num]\n"
319 " [-w width | -width | goal [maximum]] [file ...]\n"
320 "Options: -c center each line instead of formatting\n"
321 " -d <chars> double-space after <chars> at line end\n"
322 " -l <n> turn each <n> spaces at start of line into a tab\n"
323 " -m try to make sure mail header lines stay separate\n"
324 " -n format lines beginning with a dot\n"
325 " -p allow indented paragraphs\n"
326 " -s coalesce whitespace inside lines\n"
327 " -t <n> have tabs every <n> columns\n"
328 " -w <n> set maximum width to <n>\n"
329 " goal set target width to goal\n");
330 exit(ch=='h' ? 0 : EX_USAGE);
332 argc -= optind; argv += optind;
334 /* [ goal [ maximum ] ] */
336 if (argc>0 && goal_length==0
337 && (goal_length=get_positive(*argv,"goal length must be positive", 0))
338 != 0) {
339 --argc; ++argv;
340 if (argc>0
341 && (max_length=get_positive(*argv,"max length must be positive", 0))
342 != 0) {
343 --argc; ++argv;
344 if (max_length<goal_length)
345 errx(EX_USAGE, "max length must be >= goal length");
348 if (goal_length==0) goal_length = 65;
349 if (max_length==0) max_length = goal_length+10;
350 if (max_length >= SIZE_T_MAX / sizeof (wchar_t)) errx(EX_USAGE, "max length too large");
351 /* really needn't be longer */
352 output_buffer = XMALLOC((max_length+1) * sizeof(wchar_t));
354 /* 2. Process files. */
356 if (argc>0) {
357 while (argc-->0) process_named_file(*argv++);
359 else {
360 process_stream(stdin, "standard input");
363 /* We're done. */
365 return n_errors ? EX_NOINPUT : 0;
369 /* Process a single file, given its name.
371 static void
372 process_named_file(const char *name) {
373 FILE *f=fopen(name, "r");
374 if (!f) { warn("%s", name); ++n_errors; }
375 else {
376 process_stream(f, name);
377 if (ferror(f)) { warn("%s", name); ++n_errors; }
378 fclose(f);
382 /* Types of mail header continuation lines:
384 typedef enum {
385 hdr_ParagraphStart = -1,
386 hdr_NonHeader = 0,
387 hdr_Header = 1,
388 hdr_Continuation = 2
389 } HdrType;
391 /* Process a stream. This is where the real work happens,
392 * except that centering is handled separately.
394 static void
395 process_stream(FILE *stream, const char *name) {
396 size_t last_indent=SILLY; /* how many spaces in last indent? */
397 size_t para_line_number=0; /* how many lines already read in this para? */
398 size_t first_indent=SILLY; /* indentation of line 0 of paragraph */
399 HdrType prev_header_type=hdr_ParagraphStart;
400 /* ^-- header_type of previous line; -1 at para start */
401 wchar_t *line;
402 size_t length;
404 if (centerP) { center_stream(stream, name); return; }
405 while ((line=get_line(stream,&length)) != NULL) {
406 size_t np=indent_length(line, length);
407 { HdrType header_type=hdr_NonHeader;
408 if (grok_mail_headers && prev_header_type!=hdr_NonHeader) {
409 if (np==0 && might_be_header(line))
410 header_type = hdr_Header;
411 else if (np>0 && prev_header_type>hdr_NonHeader)
412 header_type = hdr_Continuation;
414 /* We need a new paragraph if and only if:
415 * this line is blank,
416 * OR it's a troff request (and we don't format troff),
417 * OR it's a mail header,
418 * OR it's not a mail header AND the last line was one,
419 * OR the indentation has changed
420 * AND the line isn't a mail header continuation line
421 * AND this isn't the second line of an indented paragraph.
423 if ( length==0
424 || (line[0]=='.' && !format_troff)
425 || header_type==hdr_Header
426 || (header_type==hdr_NonHeader && prev_header_type>hdr_NonHeader)
427 || (np!=last_indent
428 && header_type != hdr_Continuation
429 && (!allow_indented_paragraphs || para_line_number != 1)) ) {
430 new_paragraph(output_in_paragraph ? last_indent : first_indent, np);
431 para_line_number = 0;
432 first_indent = np;
433 last_indent = np;
434 if (header_type==hdr_Header) last_indent=2; /* for cont. lines */
435 if (length==0 || (line[0]=='.' && !format_troff)) {
436 if (length==0)
437 putwchar('\n');
438 else
439 wprintf(L"%.*ls\n", (int)length, line);
440 prev_header_type=hdr_ParagraphStart;
441 continue;
444 else {
445 /* If this is an indented paragraph other than a mail header
446 * continuation, set |last_indent|.
448 if (np != last_indent && header_type != hdr_Continuation)
449 last_indent=np;
451 prev_header_type = header_type;
454 { size_t n=np;
455 while (n<length) {
456 /* Find word end and count spaces after it */
457 size_t word_length=0, space_length=0;
458 while (n+word_length < length && line[n+word_length] != ' ')
459 ++word_length;
460 space_length = word_length;
461 while (n+space_length < length && line[n+space_length] == ' ')
462 ++space_length;
463 /* Send the word to the output machinery. */
464 output_word(first_indent, last_indent,
465 line+n, word_length, space_length-word_length);
466 n += space_length;
469 ++para_line_number;
471 new_paragraph(output_in_paragraph ? last_indent : first_indent, 0);
472 if (ferror(stream)) { warn("%s", name); ++n_errors; }
475 /* How long is the indent on this line?
477 static size_t
478 indent_length(const wchar_t *line, size_t length) {
479 size_t n=0;
480 while (n<length && *line++ == ' ') ++n;
481 return n;
484 /* Might this line be a mail header?
485 * We deem a line to be a possible header if it matches the
486 * Perl regexp /^[A-Z][-A-Za-z0-9]*:\s/. This is *not* the same
487 * as in RFC whatever-number-it-is; we want to be gratuitously
488 * conservative to avoid mangling ordinary civilised text.
490 static int
491 might_be_header(const wchar_t *line) {
492 if (!iswupper(*line++)) return 0;
493 while (*line && (iswalnum(*line) || *line=='-')) ++line;
494 return (*line==':' && iswspace(line[1]));
497 /* Begin a new paragraph with an indent of |indent| spaces.
499 static void
500 new_paragraph(size_t old_indent, size_t indent) {
501 if (output_buffer_length) {
502 if (old_indent>0) output_indent(old_indent);
503 wprintf(L"%.*ls\n", (int)output_buffer_length, output_buffer);
505 x=indent; x0=0; output_buffer_length=0; pending_spaces=0;
506 output_in_paragraph = 0;
509 /* Output spaces or tabs for leading indentation.
511 static void
512 output_indent(size_t n_spaces) {
513 if (output_tab_width) {
514 while (n_spaces >= output_tab_width) {
515 putwchar('\t');
516 n_spaces -= output_tab_width;
519 while (n_spaces-- > 0) putwchar(' ');
522 /* Output a single word, or add it to the buffer.
523 * indent0 and indent1 are the indents to use on the first and subsequent
524 * lines of a paragraph. They'll often be the same, of course.
526 static void
527 output_word(size_t indent0, size_t indent1, const wchar_t *word, size_t length, size_t spaces) {
528 size_t new_x;
529 size_t indent = output_in_paragraph ? indent1 : indent0;
530 size_t width;
531 const wchar_t *p;
532 int cwidth;
534 for (p = word, width = 0; p < &word[length]; p++)
535 width += (cwidth = wcwidth(*p)) > 0 ? cwidth : 1;
537 new_x = x + pending_spaces + width;
539 /* If either |spaces==0| (at end of line) or |coalesce_spaces_P|
540 * (squashing internal whitespace), then add just one space;
541 * except that if the last character was a sentence-ender we
542 * actually add two spaces.
544 if (coalesce_spaces_P || spaces==0)
545 spaces = wcschr(sentence_enders, word[length-1]) ? 2 : 1;
547 if (new_x<=goal_length) {
548 /* After adding the word we still aren't at the goal length,
549 * so clearly we add it to the buffer rather than outputing it.
551 wmemset(output_buffer+output_buffer_length, L' ', pending_spaces);
552 x0 += pending_spaces; x += pending_spaces;
553 output_buffer_length += pending_spaces;
554 wmemcpy(output_buffer+output_buffer_length, word, length);
555 x0 += width; x += width; output_buffer_length += length;
556 pending_spaces = spaces;
558 else {
559 /* Adding the word takes us past the goal. Print the line-so-far,
560 * and the word too iff either (1) the lsf is empty or (2) that
561 * makes us nearer the goal but doesn't take us over the limit,
562 * or (3) the word on its own takes us over the limit.
563 * In case (3) we put a newline in between.
565 if (indent>0) output_indent(indent);
566 wprintf(L"%.*ls", (int)output_buffer_length, output_buffer);
567 if (x0==0 || (new_x <= max_length && new_x-goal_length <= goal_length-x)) {
568 wprintf(L"%*ls", (int)pending_spaces, L"");
569 goto write_out_word;
571 else {
572 /* If the word takes us over the limit on its own, just
573 * spit it out and don't bother buffering it.
575 if (indent+width > max_length) {
576 putwchar('\n');
577 if (indent>0) output_indent(indent);
578 write_out_word:
579 wprintf(L"%.*ls", (int)length, word);
580 x0 = 0; x = indent1; pending_spaces = 0;
581 output_buffer_length = 0;
583 else {
584 wmemcpy(output_buffer, word, length);
585 x0 = width; x = width+indent1; pending_spaces = spaces;
586 output_buffer_length = length;
589 putwchar('\n');
590 output_in_paragraph = 1;
594 /* Process a stream, but just center its lines rather than trying to
595 * format them neatly.
597 static void
598 center_stream(FILE *stream, const char *name) {
599 wchar_t *line, *p;
600 size_t length;
601 size_t width;
602 int cwidth;
603 while ((line=get_line(stream, &length)) != NULL) {
604 size_t l=length;
605 while (l>0 && iswspace(*line)) { ++line; --l; }
606 length=l;
607 for (p = line, width = 0; p < &line[length]; p++)
608 width += (cwidth = wcwidth(*p)) > 0 ? cwidth : 1;
609 l = width;
610 while (l<goal_length) { putwchar(' '); l+=2; }
611 wprintf(L"%.*ls\n", (int)length, line);
613 if (ferror(stream)) { warn("%s", name); ++n_errors; }
616 /* Get a single line from a stream. Expand tabs, strip control
617 * characters and trailing whitespace, and handle backspaces.
618 * Return the address of the buffer containing the line, and
619 * put the length of the line in |lengthp|.
620 * This can cope with arbitrarily long lines, and with lines
621 * without terminating \n.
622 * If there are no characters left or an error happens, we
623 * return 0.
624 * Don't confuse |spaces_pending| here with the global
625 * |pending_spaces|.
627 static wchar_t *
628 get_line(FILE *stream, size_t *lengthp) {
629 static wchar_t *buf=NULL;
630 static size_t length=0;
631 size_t len=0;
632 wint_t ch;
633 size_t spaces_pending=0;
634 int troff=0;
635 size_t col=0;
636 int cwidth;
638 if (buf==NULL) { length=100; buf=XMALLOC(length * sizeof(wchar_t)); }
639 while ((ch=getwc(stream)) != '\n' && ch != WEOF) {
640 if (len+spaces_pending==0 && ch=='.' && !format_troff) troff=1;
641 if (ch==' ') ++spaces_pending;
642 else if (troff || iswprint(ch)) {
643 while (len+spaces_pending >= length) {
644 length*=2; buf=xrealloc(buf, length * sizeof(wchar_t));
646 while (spaces_pending > 0) { --spaces_pending; buf[len++]=' '; col++; }
647 buf[len++] = ch;
648 col += (cwidth = wcwidth(ch)) > 0 ? cwidth : 1;
650 else if (ch=='\t')
651 spaces_pending += tab_width - (col+spaces_pending)%tab_width;
652 else if (ch=='\b') { if (len) --len; if (col) --col; }
654 *lengthp=len;
655 return (len>0 || ch!=WEOF) ? buf : 0;
658 /* (Re)allocate some memory, exiting with an error if we can't.
660 static void *
661 xrealloc(void *ptr, size_t nbytes) {
662 void *p = realloc(ptr, nbytes);
663 if (p == NULL) errx(EX_OSERR, "out of memory");
664 return p;