tests: add test for locale decimal processing
[coreutils.git] / man / help2man
blob04e4790be4ce0fc048871d1db403459a1f4664b5
1 #!/usr/bin/perl -w
3 # Generate a short man page from --help and --version output.
4 # Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009,
5 # 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <https://www.gnu.org/licenses/>.
20 # Written by Brendan O'Dea <bod@debian.org>
21 # Available from https://ftp.gnu.org/gnu/help2man/
23 use 5.008;
24 use strict;
25 use Getopt::Long;
26 use Text::ParseWords qw(shellwords);
27 use Text::Tabs qw(expand);
28 use POSIX qw(strftime setlocale LC_ALL);
30 my $this_program = 'help2man';
31 my $this_version = '1.47.3';
33 sub _ { $_[0] }
34 sub configure_locale
36 my $locale = shift;
37 die "$this_program: no locale support (Locale::gettext required)\n"
38 unless $locale eq 'C';
41 sub dec { $_[0] }
42 sub enc { $_[0] }
43 sub enc_user { $_[0] }
44 sub kark { die +(sprintf shift, @_), "\n" }
45 sub N_ { $_[0] }
47 sub program_basename;
48 sub get_option_value;
49 sub convert_option;
50 sub fix_italic_spacing;
52 my $version_info = enc_user sprintf _(<<'EOT'), $this_program, $this_version;
53 GNU %s %s
55 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009, 2010,
56 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
57 This is free software; see the source for copying conditions. There is NO
58 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
60 Written by Brendan O'Dea <bod@debian.org>
61 EOT
63 my $help_info = enc_user sprintf _(<<'EOT'), $this_program, $this_program;
64 `%s' generates a man page out of `--help' and `--version' output.
66 Usage: %s [OPTION]... EXECUTABLE
68 -n, --name=STRING description for the NAME paragraph
69 -s, --section=SECTION section number for manual page (1, 6, 8)
70 -m, --manual=TEXT name of manual (User Commands, ...)
71 -S, --source=TEXT source of program (FSF, Debian, ...)
72 -L, --locale=STRING select locale (default "C")
73 -i, --include=FILE include material from `FILE'
74 -I, --opt-include=FILE include material from `FILE' if it exists
75 -o, --output=FILE send output to `FILE'
76 -p, --info-page=TEXT name of Texinfo manual
77 -N, --no-info suppress pointer to Texinfo manual
78 -l, --libtool exclude the `lt-' from the program name
79 --help print this help, then exit
80 --version print version number, then exit
82 EXECUTABLE should accept `--help' and `--version' options and produce output on
83 stdout although alternatives may be specified using:
85 -h, --help-option=STRING help option string
86 -v, --version-option=STRING version option string
87 --version-string=STRING version string
88 --no-discard-stderr include stderr when parsing option output
90 Report bugs to <bug-help2man@gnu.org>.
91 EOT
93 my $section = 1;
94 my $manual = '';
95 my $source = '';
96 my $help_option = '--help';
97 my $version_option = '--version';
98 my $discard_stderr = 1;
99 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info, $opt_libtool,
100 $version_text);
102 my %opt_def = (
103 'n|name=s' => \$opt_name,
104 's|section=s' => \$section,
105 'm|manual=s' => \$manual,
106 'S|source=s' => \$source,
107 'L|locale=s' => sub { configure_locale pop },
108 'i|include=s' => sub { push @opt_include, [ pop, 1 ] },
109 'I|opt-include=s' => sub { push @opt_include, [ pop, 0 ] },
110 'o|output=s' => \$opt_output,
111 'p|info-page=s' => \$opt_info,
112 'N|no-info' => \$opt_no_info,
113 'l|libtool' => \$opt_libtool,
114 'help' => sub { print $help_info; exit },
115 'version' => sub { print $version_info; exit },
116 'h|help-option=s' => \$help_option,
117 'v|version-option=s' => \$version_option,
118 'version-string=s' => \$version_text,
119 'discard-stderr!' => \$discard_stderr,
122 # Parse options.
123 Getopt::Long::config('bundling');
124 die $help_info unless GetOptions %opt_def and @ARGV == 1;
126 my %include = ();
127 my %replace = ();
128 my %append = ();
129 my %append_match = ();
130 my @sections = (); # retain order of include file or in-line *section*s
132 # Process include file (if given). Format is:
134 # Optional initial text, ignored. May include lines starting with `-'
135 # which are processed as options.
137 # [section]
138 # Verbatim text to be included in the named section. By default at
139 # the start, but in the case of `name' and `synopsis' the content
140 # will replace the autogenerated contents.
142 # [<section]
143 # Verbatim text to be inserted at the start of the named section.
145 # [=section]
146 # Verbatim text to replace the named section.
148 # [>section]
149 # Verbatim text to be appended to the end of the named section.
151 # /pattern/
152 # Verbatim text for inclusion below a paragraph matching `pattern'.
155 while (@opt_include)
157 my ($inc, $required) = @{shift @opt_include};
159 next unless -f $inc or $required;
160 kark N_("%s: can't open `%s' (%s)"), $this_program, $inc, $!
161 unless open INC, $inc;
163 my $key;
164 my $hash;
166 while (<INC>)
168 # Convert input to internal Perl format, so that multibyte
169 # sequences are treated as single characters.
170 $_ = dec $_;
172 # [section]
173 if (/^\[([^]]+)\]\s*$/)
175 $key = uc $1;
176 $key =~ s/^\s+//;
177 $key =~ s/\s+$//;
178 $hash = \%include;
179 # Handle explicit [<section], [=section] and [>section]
180 if ($key =~ s/^([<>=])\s*//)
182 if ($1 eq '>') { $hash = \%append; }
183 elsif ($1 eq '=') { $hash = \%replace; }
185 # NAME/SYNOPSIS replace by default
186 elsif ($key eq _('NAME') or $key eq _('SYNOPSIS'))
188 $hash = \%replace;
190 else
192 $hash = \%include;
195 push @sections, $key;
196 next;
199 # /pattern/
200 if (m!^/(.*)/([ims]*)\s*$!)
202 my $pat = $2 ? "(?$2)$1" : $1;
204 # Check pattern.
205 eval { $key = qr($pat) };
206 if ($@)
208 $@ =~ s/ at .*? line \d.*//;
209 die "$inc:$.:$@";
212 $hash = \%append_match;
213 next;
216 # Check for options before the first section--anything else is
217 # silently ignored, allowing the first for comments and
218 # revision info.
219 unless ($key)
221 # handle options
222 if (/^-/)
224 local @ARGV = shellwords $_;
225 GetOptions %opt_def;
228 next;
231 $hash->{$key} .= $_;
234 close INC;
236 kark N_("%s: no valid information found in `%s'"), $this_program, $inc
237 unless $key;
240 # Compress trailing blank lines.
241 for my $hash (\(%include, %replace, %append, %append_match))
243 for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
246 # Grab help and version info from executable.
247 my $help_text = get_option_value $ARGV[0], $help_option;
248 $version_text ||= get_option_value $ARGV[0], $version_option;
250 # By default the generated manual pages will include the current date. This may
251 # however be overriden by setting the environment variable $SOURCE_DATE_EPOCH
252 # to an integer value of the seconds since the UNIX epoch. This is primarily
253 # intended to support reproducible builds (wiki.debian.org/ReproducibleBuilds)
254 # and will additionally ensure that the output date string is UTC.
255 my $epoch_secs = time;
256 if (exists $ENV{SOURCE_DATE_EPOCH} and $ENV{SOURCE_DATE_EPOCH} =~ /^(\d+)$/)
258 $epoch_secs = $1;
259 $ENV{TZ} = 'UTC0';
262 # Translators: the following message is a strftime(3) format string, which in
263 # the English version expands to the month as a word and the full year. It
264 # is used on the footer of the generated manual pages. If in doubt, you may
265 # just use %x as the value (which should be the full locale-specific date).
266 my $date = enc strftime _("%B %Y"), localtime $epoch_secs;
267 my $program = program_basename $ARGV[0];
268 my $package = $program;
269 my $version;
271 if ($opt_output)
273 unlink $opt_output or kark N_("%s: can't unlink %s (%s)"),
274 $this_program, $opt_output, $! if -e $opt_output;
276 open STDOUT, ">$opt_output"
277 or kark N_("%s: can't create %s (%s)"), $this_program, $opt_output, $!;
280 # The first line of the --version information is assumed to be in one
281 # of the following formats:
283 # <version>
284 # <program> <version>
285 # {GNU,Free} <program> <version>
286 # <program> ({GNU,Free} <package>) <version>
287 # <program> - {GNU,Free} <package> <version>
289 # and separated from any copyright/author details by a blank line.
291 ($_, $version_text) = ((split /\n+/, $version_text, 2), '');
293 if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
294 /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
296 $program = program_basename $1;
297 $package = $2;
298 $version = $3;
300 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
302 $program = program_basename $2;
303 $package = $1 ? "$1$program" : $program;
304 $version = $3;
306 else
308 $version = $_;
311 # No info for `info' itself.
312 $opt_no_info = 1 if $program eq 'info';
314 if ($opt_name)
316 # --name overrides --include contents.
317 $replace{_('NAME')} = "$program \\- $opt_name\n";
320 # Translators: "NAME", "SYNOPSIS" and other one or two word strings in all
321 # upper case are manual page section headings. The man(1) manual page in your
322 # language, if available should provide the conventional translations.
323 for ($replace{_('NAME')} || ($include{_('NAME')} ||= ''))
325 if ($_) # Use first name given as $program
327 $program = $1 if /^([^\s,]+)(?:,?\s*[^\s,\\-]+)*\s+\\?-/;
329 else # Set a default (useless) NAME paragraph.
331 $_ = sprintf _("%s \\- manual page for %s %s") . "\n", $program,
332 $program, $version;
336 # Man pages traditionally have the page title in caps.
337 my $PROGRAM = uc $program;
339 # Set default page head/footers
340 $source ||= "$program $version";
341 unless ($manual)
343 for ($section)
345 if (/^(1[Mm]|8)/) { $manual = enc _('System Administration Utilities') }
346 elsif (/^6/) { $manual = enc _('Games') }
347 else { $manual = enc _('User Commands') }
351 # Extract usage clause(s) [if any] for SYNOPSIS.
352 # Translators: "Usage" and "or" here are patterns (regular expressions) which
353 # are used to match the usage synopsis in program output. An example from cp
354 # (GNU coreutils) which contains both strings:
355 # Usage: cp [OPTION]... [-T] SOURCE DEST
356 # or: cp [OPTION]... SOURCE... DIRECTORY
357 # or: cp [OPTION]... -t DIRECTORY SOURCE...
358 my $PAT_USAGE = _('Usage');
359 my $PAT_USAGE_CONT = _('or');
360 if ($help_text =~ s/^($PAT_USAGE):( +(\S+))(.*)((?:\n(?: {6}\1| *($PAT_USAGE_CONT): +\S).*)*)//om)
362 my @syn = $3 . $4;
364 if ($_ = $5)
366 s/^\n//;
367 for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
370 my $synopsis = '';
371 for (@syn)
373 $synopsis .= ".br\n" if $synopsis;
374 s!^\S*/!!;
375 s/^lt-// if $opt_libtool;
376 s/^(\S+) *//;
377 $synopsis .= ".B $1\n";
378 s/\s+$//;
379 s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
380 s/^/\\fI/ unless s/^\\fR//;
381 $_ .= '\fR';
382 s/(\\fI)( *)/$2$1/g;
383 s/\\fI\\fR//g;
384 s/^\\fR//;
385 s/\\fI$//;
386 s/^\./\\&./;
388 $_ = fix_italic_spacing $_;
389 $synopsis .= "$_\n";
392 $include{_('SYNOPSIS')} .= $synopsis;
395 # Process text, initial section is DESCRIPTION.
396 my $sect = _('DESCRIPTION');
397 $_ = "$help_text\n\n$version_text";
399 # Normalise paragraph breaks.
400 s/^\n+//;
401 s/\n*$/\n/;
402 s/\n\n+/\n\n/g;
404 # Join hyphenated lines.
405 s/([A-Za-z])-\n *([A-Za-z])/$1$2/g;
407 # Temporarily exchange leading dots, apostrophes and backslashes for
408 # tokens.
409 s/^\./\x80/mg;
410 s/^'/\x81/mg;
411 s/\\/\x82/g;
413 # Translators: patterns are used to match common program output. In the source
414 # these strings are all of the form of "my $PAT_something = _('...');" and are
415 # regular expressions. If there is more than one commonly used string, you
416 # may separate alternatives with "|". Spaces in these expressions are written
417 # as " +" to indicate that more than one space may be matched. The string
418 # "(?:[\\w-]+ +)?" in the bug reporting pattern is used to indicate an
419 # optional word, so that either "Report bugs" or "Report _program_ bugs" will
420 # be matched.
421 my $PAT_BUGS = _('Report +(?:[\w-]+ +)?bugs|' .
422 'Email +bug +reports +to|' .
423 '.* +online +help:');
424 my $PAT_AUTHOR = _('Written +by');
425 my $PAT_OPTIONS = _('Options');
426 my $PAT_ENVIRONMENT = _('Environment');
427 my $PAT_FILES = _('Files');
428 my $PAT_EXAMPLES = _('Examples');
429 my $PAT_FREE_SOFTWARE = _('This +is +free +software');
430 my $PAT_SEE_ALSO = _('Full +documentation');
432 # Start a new paragraph (if required) for these.
433 s/([^\n])\n($PAT_BUGS|$PAT_AUTHOR|$PAT_SEE_ALSO) /$1\n\n$2 /og;
435 # Convert iso-8859-1 copyright symbol or (c) to nroff
436 # character.
437 s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
439 while (length)
441 # Convert some standard paragraph names.
442 if (s/^($PAT_OPTIONS): *\n+//o)
444 $sect = _('OPTIONS');
445 next;
447 if (s/^($PAT_ENVIRONMENT): *\n+//o)
449 $sect = _('ENVIRONMENT');
450 next;
452 if (s/^($PAT_FILES): *\n+//o)
454 $sect = _('FILES');
455 next;
457 elsif (s/^($PAT_EXAMPLES): *\n+//o)
459 $sect = _('EXAMPLES');
460 next;
463 # Custom section indicated by a line containing "*Section Name*".
464 if (s/^\*(\w(.*\w)?)\* *\n+//)
466 $sect = uc $1;
467 $sect =~ tr/*/ /; # also accept *Section*Name*
468 push @sections, $sect;
469 next;
472 # Copyright section.
473 if (/^Copyright /)
475 $sect = _('COPYRIGHT');
478 # Bug reporting section.
479 elsif (/^($PAT_BUGS) /o)
481 $sect = _('REPORTING BUGS');
484 # Author section.
485 elsif (/^($PAT_AUTHOR)/o)
487 $sect = _('AUTHOR');
490 elsif (/^($PAT_SEE_ALSO)/o)
492 $sect = _('SEE ALSO');
493 $opt_no_info = 1;
496 # Examples, indicated by an indented leading $, % or > are
497 # rendered in a constant width font.
498 if (/^( +)([\$\%>] )\S/)
500 my $indent = $1;
501 my $prefix = $2;
502 my $break = '.IP';
503 while (s/^$indent\Q$prefix\E(\S.*)\n*//)
505 $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
506 $break = '.br';
509 next;
512 my $matched = '';
514 # Sub-sections have a trailing colon and the second line indented.
515 if (s/^(\S.*:) *\n / /)
517 $matched .= $& if %append_match;
518 $include{$sect} .= qq(.SS "$1"\n);
521 my $indent = 0;
522 my $content = '';
524 # Option with description.
525 if (s/^( {1,10}([+-]\S.*?))(?:( +(?!-))|\n( {20,}))(\S.*)\n//)
527 $matched .= $& if %append_match;
528 $indent = length ($4 || "$1$3");
529 $content = ".TP\n\x84$2\n\x84$5\n";
530 unless ($4)
532 # Indent may be different on second line.
533 $indent = length $& if /^ {20,}/;
537 # Option without description.
538 elsif (s/^ {1,10}([+-]\S.*)\n//)
540 $matched .= $& if %append_match;
541 $content = ".HP\n\x84$1\n";
542 $indent = 80; # not continued
545 # Indented paragraph with tag.
546 elsif (s/^( +(\S.*?) +)(\S.*)\n//)
548 $matched .= $& if %append_match;
549 $indent = length $1;
550 $content = ".TP\n\x84$2\n\x84$3\n";
553 # Indented paragraph.
554 elsif (s/^( +)(\S.*)\n//)
556 $matched .= $& if %append_match;
557 $indent = length $1;
558 $content = ".IP\n\x84$2\n";
561 # Left justified paragraph.
562 else
564 s/(.*)\n//;
565 $matched .= $& if %append_match;
566 $content = ".PP\n" if $include{$sect};
567 $content .= "$1\n";
570 # Append continuations.
571 while ($indent ? s/^ {$indent}(\S.*)\n// : s/^(\S.*)\n//)
573 $matched .= $& if %append_match;
574 $content .= "\x84$1\n";
577 # Move to next paragraph.
578 s/^\n+//;
580 for ($content)
582 # Leading dot and apostrophe protection.
583 s/\x84\./\x80/g;
584 s/\x84'/\x81/g;
585 s/\x84//g;
587 # Examples should be verbatim.
588 unless ($sect eq _('EXAMPLES'))
590 # Convert options.
591 s/(^|[ (])(-[][\w=-]+)/$1 . convert_option $2/mge;
593 # Italicise filenames: /a/b, $VAR/c/d, ~/e/f
595 (^|[ (]) # space/punctuation before
597 (?:\$\w+|~)? # leading variable, or tilde
598 (?:/\w(?:[\w.-]*\w)?)+ # path components
600 ($|[ ,;.)]) # space/punctuation after
601 !$1\\fI$2\\fP$3!xmg;
603 $_ = fix_italic_spacing $_;
606 # Escape remaining hyphens.
607 s/-/\x83/g;
609 if ($sect eq _('COPYRIGHT'))
611 # Insert line breaks before additional copyright messages
612 # and the disclaimer.
613 s/\n(Copyright |$PAT_FREE_SOFTWARE)/\n.br\n$1/og;
615 elsif ($sect eq _('REPORTING BUGS'))
617 # Handle multi-line bug reporting sections of the form:
619 # Report <program> bugs to <addr>
620 # GNU <package> home page: <url>
621 # ...
622 s/\n([[:upper:]])/\n.br\n$1/g;
624 elsif ($sect eq _('SEE ALSO'))
626 # Handle external references of the form:
628 # GNU <package> online resources: <addr>
629 # Full documentation at: <addr>
630 # or available locally via: info ...
632 s/\'/\\(aq/g; # shell quotes for info command
633 s/\n(.)/\n.br\n$1/g; # separate lines for each item
637 # Check if matched paragraph contains /pat/.
638 if (%append_match)
640 for my $pat (keys %append_match)
642 if ($matched =~ $pat)
644 $content .= ".PP\n" unless $append_match{$pat} =~ /^\./;
645 $content .= $append_match{$pat};
650 $include{$sect} .= $content;
653 # Refer to the real documentation.
654 unless ($opt_no_info)
656 my $info_page = $opt_info || $program;
658 $sect = _('SEE ALSO');
659 $include{$sect} .= ".PP\n" if $include{$sect};
660 $include{$sect} .= sprintf _(<<'EOT'), $program, $program, $info_page;
661 The full documentation for
662 .B %s
663 is maintained as a Texinfo manual. If the
664 .B info
666 .B %s
667 programs are properly installed at your site, the command
669 .B info %s
671 should give you access to the complete manual.
675 # Append additional text.
676 while (my ($sect, $text) = each %append)
678 $include{$sect} .= $append{$sect};
681 # Replace sections.
682 while (my ($sect, $text) = each %replace)
684 $include{$sect} = $replace{$sect};
687 # Output header.
688 print <<EOT;
689 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
690 .TH $PROGRAM "$section" "$date" "$source" "$manual"
693 # Section ordering.
694 my @pre = (_('NAME'), _('SYNOPSIS'), _('DESCRIPTION'), _('OPTIONS'),
695 _('EXAMPLES'));
696 my @post = (_('ENVIRONMENT'), _('FILES'), _('AUTHOR'),
697 _('REPORTING BUGS'), _('COPYRIGHT'), _('SEE ALSO'));
698 my %filter = map { $_ => 1 } @pre, @post;
700 # Output content.
701 my %done;
702 for my $sect (@pre, (grep !$filter{$_}, @sections), @post)
704 next if $done{$sect}++; # ignore duplicates
705 next unless $include{$sect};
706 if ($include{$sect})
708 my $quote = $sect =~ /\W/ ? '"' : '';
709 print enc ".SH $quote$sect$quote\n";
711 for ($include{$sect})
713 # Replace leading dot, apostrophe, backslash and hyphen
714 # tokens.
715 s/\x80/\\&./g;
716 s/\x81/\\&'/g;
717 s/\x82/\\e/g;
718 s/\x83/\\-/g;
720 # Convert some latin1 chars to troff equivalents
721 s/\xa0/\\ /g; # non-breaking space
723 print enc $_;
728 close STDOUT or kark N_("%s: error writing to %s (%s)"), $this_program,
729 $opt_output || 'stdout', $!;
731 exit;
733 # Get program basename, and strip libtool "lt-" prefix if required.
734 sub program_basename
736 local $_ = shift;
737 s!.*/!!;
738 s/^lt-// if $opt_libtool;
742 # Call program with given option and return results.
743 sub get_option_value
745 my ($prog, $opt) = @_;
746 my $stderr = $discard_stderr ? '/dev/null' : '&1';
747 my $value = join '',
748 map { s/ +$//; expand $_ }
749 map { dec $_ }
750 `$prog $opt 2>$stderr`;
752 unless ($value)
754 my $err = N_("%s: can't get `%s' info from %s%s");
755 my $extra = $discard_stderr
756 ? "\n" . N_("Try `--no-discard-stderr' if option outputs to stderr")
757 : '';
759 kark $err, $this_program, $opt, $prog, $extra;
762 $value;
765 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
766 # embolden. Option arguments get italicised.
767 sub convert_option
769 local $_ = '\fB' . shift;
771 s/-/\x83/g;
772 unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
774 s/=(.)/\\fR=\\fI$1/;
775 s/ (.)/ \\fI$1/;
776 $_ .= '\fR';
782 # Insert spacing escape characters \, and \/ before and after italic text. See
783 # https://www.gnu.org/software/groff/manual/html_node/Ligatures-and-Kerning.html
784 sub fix_italic_spacing
786 local $_ = shift;
787 s!\\fI(.*?)\\f([BRP])!\\fI\\,$1\\/\\f$2!g;
788 return $_;