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, 2016, 2017, 2020, 2021 Free Software
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, see <https://www.gnu.org/licenses/>.
21 # Written by Brendan O'Dea <bod@debian.org>
22 # Available from https://ftp.gnu.org/gnu/help2man/
27 use Text
::ParseWords
qw(shellwords);
28 use Text
::Tabs
qw(expand);
29 use POSIX
qw(strftime setlocale LC_ALL);
31 my $this_program = 'help2man';
32 my $this_version = '1.48.5';
38 die "$this_program: no locale support (Locale::gettext required)\n"
39 unless $locale eq 'C';
44 sub enc_user
{ $_[0] }
45 sub kark
{ die +(sprintf shift, @_), "\n" }
51 sub fix_italic_spacing
;
53 my $version_info = enc_user
sprintf _
(<<'EOT'), $this_program, $this_version;
56 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009,
57 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2020, 2021 Free Software
59 This is free software; see the source for copying conditions. There is NO
60 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
62 Written by Brendan O'Dea <bod@debian.org>
65 my $help_info = enc_user
sprintf _
(<<'EOT'), $this_program, $this_program;
66 `%s' generates a man page out of `--help' and `--version' output.
68 Usage: %s [OPTION]... EXECUTABLE
70 -n, --name=STRING description for the NAME paragraph
71 -s, --section=SECTION section number for manual page (1, 6, 8)
72 -m, --manual=TEXT name of manual (User Commands, ...)
73 -S, --source=TEXT source of program (FSF, Debian, ...)
74 -L, --locale=STRING select locale (default "C")
75 -i, --include=FILE include material from `FILE'
76 -I, --opt-include=FILE include material from `FILE' if it exists
77 -o, --output=FILE send output to `FILE'
78 -p, --info-page=TEXT name of Texinfo manual
79 -N, --no-info suppress pointer to Texinfo manual
80 -l, --libtool exclude the `lt-' from the program name
81 -b, --bold-refs apply bold style to references
82 --help print this help, then exit
83 --version print version number, then exit
85 EXECUTABLE should accept `--help' and `--version' options and produce output on
86 stdout although alternatives may be specified using:
88 -h, --help-option=STRING help option string
89 -v, --version-option=STRING version option string
90 --version-string=STRING version string
91 --no-discard-stderr include stderr when parsing option output
93 Report bugs to <bug-help2man@gnu.org>.
99 my $help_option = '--help';
100 my $version_option = '--version';
101 my $discard_stderr = 1;
102 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info, $opt_libtool,
103 $opt_bold_refs, $version_text);
106 'n|name=s' => \
$opt_name,
107 's|section=s' => \
$section,
108 'm|manual=s' => \
$manual,
109 'S|source=s' => \
$source,
110 'L|locale=s' => sub { configure_locale
pop },
111 'i|include=s' => sub { push @opt_include, [ pop, 1 ] },
112 'I|opt-include=s' => sub { push @opt_include, [ pop, 0 ] },
113 'o|output=s' => \
$opt_output,
114 'p|info-page=s' => \
$opt_info,
115 'N|no-info' => \
$opt_no_info,
116 'l|libtool' => \
$opt_libtool,
117 'b|bold-refs' => \
$opt_bold_refs,
118 'help' => sub { print $help_info; exit },
119 'version' => sub { print $version_info; exit },
120 'h|help-option=s' => \
$help_option,
121 'v|version-option=s' => \
$version_option,
122 'version-string=s' => \
$version_text,
123 'discard-stderr!' => \
$discard_stderr,
127 Getopt
::Long
::config
('bundling');
128 die $help_info unless GetOptions
%opt_def and @ARGV == 1;
133 my %append_match = ();
134 my @sections = (); # retain order of include file or in-line *section*s
136 # Process include file (if given). Format is:
138 # Optional initial text, ignored. May include lines starting with `-'
139 # which are processed as options.
142 # Verbatim text to be included in the named section. By default at
143 # the start, but in the case of `name' and `synopsis' the content
144 # will replace the autogenerated contents.
147 # Verbatim text to be inserted at the start of the named section.
150 # Verbatim text to replace the named section.
153 # Verbatim text to be appended to the end of the named section.
156 # Verbatim text for inclusion below a paragraph matching `pattern'.
161 my ($inc, $required) = @
{shift @opt_include};
163 next unless -f
$inc or $required;
164 kark N_
("%s: can't open `%s' (%s)"), $this_program, $inc, $!
165 unless open INC
, $inc;
172 # Convert input to internal Perl format, so that multibyte
173 # sequences are treated as single characters.
177 if (/^\[([^]]+)\]\s*$/)
183 # Handle explicit [<section], [=section] and [>section]
184 if ($key =~ s/^([<>=])\s*//)
186 if ($1 eq '>') { $hash = \
%append; }
187 elsif ($1 eq '=') { $hash = \
%replace; }
189 # NAME/SYNOPSIS replace by default
190 elsif ($key eq _
('NAME') or $key eq _
('SYNOPSIS'))
199 push @sections, $key;
204 if (m!^/(.*)/([ims]*)\s*$!)
206 my $pat = $2 ?
"(?$2)$1" : $1;
209 eval { $key = qr
($pat) };
212 $@
=~ s/ at .*? line \d.*//;
216 $hash = \
%append_match;
220 # Check for options before the first section--anything else is
221 # silently ignored, allowing the first for comments and
228 local @ARGV = shellwords
$_;
240 kark N_
("%s: no valid information found in `%s'"), $this_program, $inc
244 # Compress trailing blank lines.
245 for my $hash (\
(%include, %replace, %append, %append_match))
247 for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
250 # Grab help and version info from executable.
251 my $help_text = get_option_value
$ARGV[0], $help_option;
252 $version_text ||= get_option_value
$ARGV[0], $version_option;
254 # By default the generated manual pages will include the current date. This may
255 # however be overridden by setting the environment variable $SOURCE_DATE_EPOCH
256 # to an integer value of the seconds since the UNIX epoch. This is primarily
257 # intended to support reproducible builds (wiki.debian.org/ReproducibleBuilds)
258 # and will additionally ensure that the output date string is UTC.
259 my $epoch_secs = time;
260 if (exists $ENV{SOURCE_DATE_EPOCH
} and $ENV{SOURCE_DATE_EPOCH
} =~ /^(\d+)$/)
266 # Translators: the following message is a strftime(3) format string, which in
267 # the English version expands to the month as a word and the full year. It
268 # is used on the footer of the generated manual pages. If in doubt, you may
269 # just use %x as the value (which should be the full locale-specific date).
270 my $date = enc strftime _
("%B %Y"), localtime $epoch_secs;
271 my $program = program_basename
$ARGV[0];
272 my $package = $program;
277 unlink $opt_output or kark N_
("%s: can't unlink %s (%s)"),
278 $this_program, $opt_output, $! if -e
$opt_output;
280 open STDOUT
, ">$opt_output"
281 or kark N_
("%s: can't create %s (%s)"), $this_program, $opt_output, $!;
284 # The first line of the --version information is assumed to be in one
285 # of the following formats:
288 # <program> <version>
289 # {GNU,Free} <program> <version>
290 # <program> ({GNU,Free,} <package>) <version>
291 # <program> - {GNU,Free,} <package> <version>
292 # <program> - {GNU,Free,} <package> - <version>
294 # and separated from any copyright/author details by a blank line.
296 ($_, $version_text) = ((split /\n+/, $version_text, 2), '');
298 if (/^(\S+) +\(((?:(?:GNU|Free) +)?[^)]+)\) +(\S.*)$/ or
299 /^(\S+) +- +((?:(?:GNU|Free) +)?\S.*) +- +(\S.*)$/ or
300 /^(\S+) +- +((?:(?:GNU|Free) +)?\S+) +(\S.*)$/)
302 $program = program_basename
$1;
306 elsif (/^((?:GNU|Free) +)?(\S+) +(\S.*)$/)
308 $program = program_basename
$2;
309 $package = $1 ?
"$1$program" : $program;
317 # No info for `info' itself.
318 $opt_no_info = 1 if $program eq 'info';
322 # --name overrides --include contents.
323 $replace{_
('NAME')} = "$program \\- $opt_name\n";
326 # Translators: "NAME", "SYNOPSIS" and other one or two word strings in all
327 # upper case are manual page section headings. The man(1) manual page in your
328 # language, if available should provide the conventional translations.
329 for ($replace{_
('NAME')} || ($include{_
('NAME')} ||= ''))
331 if ($_) # Use first name given as $program
333 $program = $1 if /^([^\s,]+)(?:,?\s*[^\s,\\-]+)*\s+\\?-/;
335 else # Set a default (useless) NAME paragraph.
337 $_ = sprintf _
("%s \\- manual page for %s %s") . "\n", $program,
342 # Man pages traditionally have the page title in caps.
343 my $PROGRAM = uc $program;
345 # Set default page head/footers
346 $source ||= "$package $version";
351 if (/^(1[Mm]|8)/) { $manual = enc _
('System Administration Utilities') }
352 elsif (/^6/) { $manual = enc _
('Games') }
353 else { $manual = enc _
('User Commands') }
357 # Extract usage clause(s) [if any] for SYNOPSIS.
358 # Translators: "Usage" and "or" here are patterns (regular expressions) which
359 # are used to match the usage synopsis in program output. An example from cp
360 # (GNU coreutils) which contains both strings:
361 # Usage: cp [OPTION]... [-T] SOURCE DEST
362 # or: cp [OPTION]... SOURCE... DIRECTORY
363 # or: cp [OPTION]... -t DIRECTORY SOURCE...
364 my $PAT_USAGE = _
('Usage');
365 my $PAT_USAGE_CONT = _
('or');
366 if ($help_text =~ s/^($PAT_USAGE):( +(\S+))(.*)((?:\n(?: {6}\1| *($PAT_USAGE_CONT): +\S).*)*)//om)
373 for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
379 $synopsis .= ".br\n" if $synopsis;
381 s/^lt-// if $opt_libtool;
383 $synopsis .= ".B $1\n";
385 s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
386 s/^/\\fI/ unless s/^\\fR//;
394 $_ = fix_italic_spacing
$_;
398 $include{_
('SYNOPSIS')} .= $synopsis;
401 # Process text, initial section is DESCRIPTION.
402 my $sect = _
('DESCRIPTION');
403 $_ = "$help_text\n\n$version_text";
405 # Normalise paragraph breaks.
410 # Join hyphenated lines.
411 s/([A-Za-z])-\n *([A-Za-z])/$1$2/g;
413 # Temporarily exchange leading dots, apostrophes and backslashes for
419 # Translators: patterns are used to match common program output. In the source
420 # these strings are all of the form of "my $PAT_something = _('...');" and are
421 # regular expressions. If there is more than one commonly used string, you
422 # may separate alternatives with "|". Spaces in these expressions are written
423 # as " +" to indicate that more than one space may be matched. The string
424 # "(?:[\\w-]+ +)?" in the bug reporting pattern is used to indicate an
425 # optional word, so that either "Report bugs" or "Report _program_ bugs" will
427 my $PAT_BUGS = _
('Report +(?:[\w-]+ +)?bugs|' .
428 'Email +bug +reports +to|' .
429 '.* +online +help:');
430 my $PAT_AUTHOR = _
('Written +by');
431 my $PAT_OPTIONS = _
('Options');
432 my $PAT_ENVIRONMENT = _
('Environment');
433 my $PAT_FILES = _
('Files');
434 my $PAT_EXAMPLES = _
('Examples');
435 my $PAT_FREE_SOFTWARE = _
('This +is +free +software');
436 my $PAT_SEE_ALSO = _
('Full +documentation');
438 # Start a new paragraph (if required) for these.
439 s/([^\n])\n($PAT_BUGS|$PAT_AUTHOR|$PAT_SEE_ALSO) /$1\n\n$2 /og;
441 # Convert iso-8859-1 copyright symbol or (c) to nroff
443 s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
447 # Convert some standard paragraph names.
448 if (s/^($PAT_OPTIONS): *\n+//o)
450 $sect = _
('OPTIONS');
453 if (s/^($PAT_ENVIRONMENT): *\n+//o)
455 $sect = _
('ENVIRONMENT');
458 if (s/^($PAT_FILES): *\n+//o)
463 elsif (s/^($PAT_EXAMPLES): *\n+//o)
465 $sect = _
('EXAMPLES');
469 # Custom section indicated by a line containing "*Section Name*".
470 if (s/^\*(\w(.*\w)?)\* *\n+//)
473 $sect =~ tr/*/ /; # also accept *Section*Name*
474 push @sections, $sect;
481 $sect = _
('COPYRIGHT');
484 # Bug reporting section.
485 elsif (/^($PAT_BUGS) /o)
487 $sect = _
('REPORTING BUGS');
491 elsif (/^($PAT_AUTHOR)/o)
496 elsif (/^($PAT_SEE_ALSO)/o)
498 $sect = _
('SEE ALSO');
502 # Examples, indicated by an indented leading $, % or > are
503 # rendered in a constant width font.
504 if (/^( +)([\$\%>] )\S/)
509 while (s/^$indent\Q$prefix\E(\S.*)\n*//)
511 $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
520 # Sub-sections have a trailing colon and the second line indented.
521 if (s/^(\S.*:) *\n / /)
523 $matched .= $& if %append_match;
524 $include{$sect} .= qq(.SS
"$1"\n);
530 # Option with description.
531 if (s/^( {1,10}([+-]\S.*?))(?:( +(?!-))|\n( {20,}))(\S.*)\n//)
533 $matched .= $& if %append_match;
534 $indent = length ($4 || "$1$3");
535 $content = ".TP\n\x84$2\n\x84$5\n";
538 # Indent may be different on second line.
539 $indent = length $& if /^ {20,}/;
543 # Option without description.
544 elsif (s/^ {1,10}([+-]\S.*)\n//)
546 $matched .= $& if %append_match;
547 $content = ".HP\n\x84$1\n";
548 $indent = 80; # not continued
551 # Indented paragraph with tag.
552 elsif (s/^( +(\S.*?) +)(\S.*)\n//)
554 $matched .= $& if %append_match;
556 $content = ".TP\n\x84$2\n\x84$3\n";
559 # Indented paragraph.
560 elsif (s/^( +)(\S.*)\n//)
562 $matched .= $& if %append_match;
564 $content = ".IP\n\x84$2\n";
567 # Left justified paragraph.
571 $matched .= $& if %append_match;
572 $content = ".PP\n" if $include{$sect};
576 # Append continuations.
577 while ($indent ?
s/^ {$indent}(\S.*)\n// : s/^(\S.*)\n//)
579 $matched .= $& if %append_match;
580 $content .= "\x84$1\n";
583 # Move to next paragraph.
588 # Leading dot and apostrophe protection.
593 # Examples should be verbatim.
594 unless ($sect eq _
('EXAMPLES'))
597 s/(^|[ (])(-[][\w=-]+)/$1 . convert_option $2/mge;
599 # Italicise filenames: /a/b, $VAR/c/d, ~/e/f
601 (^|[ (]) # space/punctuation before
603 (?
:\
$\w
+|~)?
# leading variable, or tilde
604 (?
:/\w
(?
:[\w
.-]*\w
)?
)+ # path components
606 ($|[ ,;.)]) # space/punctuation after
609 $_ = fix_italic_spacing
$_;
612 # Escape remaining hyphens.
615 if ($sect eq _
('COPYRIGHT'))
617 # Insert line breaks before additional copyright messages
618 # and the disclaimer.
619 s/\n(Copyright |$PAT_FREE_SOFTWARE)/\n.br\n$1/og;
621 elsif ($sect eq _
('REPORTING BUGS'))
623 # Handle multi-line bug reporting sections of the form:
625 # Report <program> bugs to <addr>
626 # GNU <package> home page: <url>
628 s/\n([[:upper:]])/\n.br\n$1/g;
630 elsif ($sect eq _
('SEE ALSO'))
632 # Handle external references of the form:
634 # GNU <package> online resources: <addr>
635 # Full documentation at: <addr>
636 # or available locally via: info ...
638 s/\'/\\(aq/g; # shell quotes for info command
639 s/\n(.)/\n.br\n$1/g; # separate lines for each item
643 # Check if matched paragraph contains /pat/.
646 for my $pat (keys %append_match)
648 if ($matched =~ $pat)
650 $content .= ".PP\n" unless $append_match{$pat} =~ /^\./;
651 $content .= $append_match{$pat};
656 $include{$sect} .= $content;
659 # Refer to the real documentation.
660 unless ($opt_no_info)
662 my $info_page = $opt_info || $program;
664 $sect = _
('SEE ALSO');
665 $include{$sect} .= ".PP\n" if $include{$sect};
666 $include{$sect} .= sprintf _
(<<'EOT'), $program, $program, $info_page;
667 The full documentation for
669 is maintained as a Texinfo manual. If the
673 programs are properly installed at your site, the command
677 should give you access to the complete manual.
681 # Append additional text.
682 while (my ($sect, $text) = each %append)
684 $include{$sect} .= $append{$sect};
688 while (my ($sect, $text) = each %replace)
690 $include{$sect} = $replace{$sect};
695 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
696 .TH $PROGRAM "$section" "$date" "$source" "$manual"
700 my @pre = (_
('NAME'), _
('SYNOPSIS'), _
('DESCRIPTION'), _
('OPTIONS'),
702 my @post = (_
('ENVIRONMENT'), _
('FILES'), _
('AUTHOR'),
703 _
('REPORTING BUGS'), _
('COPYRIGHT'), _
('SEE ALSO'));
704 my %filter = map { $_ => 1 } @pre, @post;
708 for my $sect (@pre, (grep !$filter{$_}, @sections), @post)
710 next if $done{$sect}++; # ignore duplicates
711 next unless $include{$sect};
714 my $quote = $sect =~ /\W/ ?
'"' : '';
715 print enc
".SH $quote$sect$quote\n";
717 for ($include{$sect})
719 # Add bold style around referenced pages.
722 # This will ignore entries already marked up (with \)
723 s/(^|\s|,)([\[\w\x83]+)\(([1-9][[:lower:]]?)\)/$1\\fB$2\\fP($3)/g;
726 # Replace leading dot, apostrophe, backslash and hyphen
733 # Convert some latin1 chars to troff equivalents
734 s/\xa0/\\ /g; # non-breaking space
741 close STDOUT
or kark N_
("%s: error writing to %s (%s)"), $this_program,
742 $opt_output || 'stdout', $!;
746 # Get program basename, and strip libtool "lt-" prefix if required.
751 s/^lt-// if $opt_libtool;
755 # Call program with given option and return results.
758 my ($prog, $opt) = @_;
759 my $stderr = $discard_stderr ?
'/dev/null' : '&1';
761 map { s/ +$//; expand
$_ }
763 `$prog $opt 2>$stderr`;
767 my $err = N_
("%s: can't get `%s' info from %s%s");
768 my $extra = $discard_stderr
769 ?
"\n" . N_
("Try `--no-discard-stderr' if option outputs to stderr")
772 kark
$err, $this_program, $opt, $prog, $extra;
778 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
779 # embolden. Option arguments get italicised.
782 local $_ = '\fB' . shift;
785 unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
795 # Insert spacing escape characters \, and \/ before and after italic text. See
796 # https://www.gnu.org/software/groff/manual/html_node/Ligatures-and-Kerning.html
797 sub fix_italic_spacing
800 s!\\fI(.*?)\\f([BRP])!\\fI\\,$1\\/\\f$2!g;