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)
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/
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';
37 die "$this_program: no locale support (Locale::gettext required)\n"
38 unless $locale eq 'C';
43 sub enc_user
{ $_[0] }
44 sub kark
{ die +(sprintf shift, @_), "\n" }
50 sub fix_italic_spacing
;
52 my $version_info = enc_user
sprintf _
(<<'EOT'), $this_program, $this_version;
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>
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>.
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,
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,
123 Getopt
::Long
::config
('bundling');
124 die $help_info unless GetOptions
%opt_def and @ARGV == 1;
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.
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.
143 # Verbatim text to be inserted at the start of the named section.
146 # Verbatim text to replace the named section.
149 # Verbatim text to be appended to the end of the named section.
152 # Verbatim text for inclusion below a paragraph matching `pattern'.
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;
168 # Convert input to internal Perl format, so that multibyte
169 # sequences are treated as single characters.
173 if (/^\[([^]]+)\]\s*$/)
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'))
195 push @sections, $key;
200 if (m!^/(.*)/([ims]*)\s*$!)
202 my $pat = $2 ?
"(?$2)$1" : $1;
205 eval { $key = qr
($pat) };
208 $@
=~ s/ at .*? line \d.*//;
212 $hash = \
%append_match;
216 # Check for options before the first section--anything else is
217 # silently ignored, allowing the first for comments and
224 local @ARGV = shellwords
$_;
236 kark N_
("%s: no valid information found in `%s'"), $this_program, $inc
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+)$/)
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;
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:
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;
300 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
302 $program = program_basename
$2;
303 $package = $1 ?
"$1$program" : $program;
311 # No info for `info' itself.
312 $opt_no_info = 1 if $program eq 'info';
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,
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";
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)
367 for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
373 $synopsis .= ".br\n" if $synopsis;
375 s/^lt-// if $opt_libtool;
377 $synopsis .= ".B $1\n";
379 s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
380 s/^/\\fI/ unless s/^\\fR//;
388 $_ = fix_italic_spacing
$_;
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.
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
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
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
437 s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
441 # Convert some standard paragraph names.
442 if (s/^($PAT_OPTIONS): *\n+//o)
444 $sect = _
('OPTIONS');
447 if (s/^($PAT_ENVIRONMENT): *\n+//o)
449 $sect = _
('ENVIRONMENT');
452 if (s/^($PAT_FILES): *\n+//o)
457 elsif (s/^($PAT_EXAMPLES): *\n+//o)
459 $sect = _
('EXAMPLES');
463 # Custom section indicated by a line containing "*Section Name*".
464 if (s/^\*(\w(.*\w)?)\* *\n+//)
467 $sect =~ tr/*/ /; # also accept *Section*Name*
468 push @sections, $sect;
475 $sect = _
('COPYRIGHT');
478 # Bug reporting section.
479 elsif (/^($PAT_BUGS) /o)
481 $sect = _
('REPORTING BUGS');
485 elsif (/^($PAT_AUTHOR)/o)
490 elsif (/^($PAT_SEE_ALSO)/o)
492 $sect = _
('SEE ALSO');
496 # Examples, indicated by an indented leading $, % or > are
497 # rendered in a constant width font.
498 if (/^( +)([\$\%>] )\S/)
503 while (s/^$indent\Q$prefix\E(\S.*)\n*//)
505 $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
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);
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";
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;
550 $content = ".TP\n\x84$2\n\x84$3\n";
553 # Indented paragraph.
554 elsif (s/^( +)(\S.*)\n//)
556 $matched .= $& if %append_match;
558 $content = ".IP\n\x84$2\n";
561 # Left justified paragraph.
565 $matched .= $& if %append_match;
566 $content = ".PP\n" if $include{$sect};
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.
582 # Leading dot and apostrophe protection.
587 # Examples should be verbatim.
588 unless ($sect eq _
('EXAMPLES'))
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
603 $_ = fix_italic_spacing
$_;
606 # Escape remaining hyphens.
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>
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/.
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
663 is maintained as a Texinfo manual. If the
667 programs are properly installed at your site, the command
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};
682 while (my ($sect, $text) = each %replace)
684 $include{$sect} = $replace{$sect};
689 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
690 .TH $PROGRAM "$section" "$date" "$source" "$manual"
694 my @pre = (_
('NAME'), _
('SYNOPSIS'), _
('DESCRIPTION'), _
('OPTIONS'),
696 my @post = (_
('ENVIRONMENT'), _
('FILES'), _
('AUTHOR'),
697 _
('REPORTING BUGS'), _
('COPYRIGHT'), _
('SEE ALSO'));
698 my %filter = map { $_ => 1 } @pre, @post;
702 for my $sect (@pre, (grep !$filter{$_}, @sections), @post)
704 next if $done{$sect}++; # ignore duplicates
705 next unless $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
720 # Convert some latin1 chars to troff equivalents
721 s/\xa0/\\ /g; # non-breaking space
728 close STDOUT
or kark N_
("%s: error writing to %s (%s)"), $this_program,
729 $opt_output || 'stdout', $!;
733 # Get program basename, and strip libtool "lt-" prefix if required.
738 s/^lt-// if $opt_libtool;
742 # Call program with given option and return results.
745 my ($prog, $opt) = @_;
746 my $stderr = $discard_stderr ?
'/dev/null' : '&1';
748 map { s/ +$//; expand
$_ }
750 `$prog $opt 2>$stderr`;
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")
759 kark
$err, $this_program, $opt, $prog, $extra;
765 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
766 # embolden. Option arguments get italicised.
769 local $_ = '\fB' . shift;
772 unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\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
787 s!\\fI(.*?)\\f([BRP])!\\fI\\,$1\\/\\f$2!g;