3 # Generate a short man page from --help and --version output.
4 # Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2008
5 # 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 of the License, or
10 # (at your option) 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 <http://www.gnu.org/licenses/>.
20 # Written by Brendan O'Dea <bod@debian.org>
21 # Available from ftp://ftp.gnu.org/gnu/help2man/
26 use Text
::Tabs
qw(expand);
27 use POSIX
qw(strftime setlocale LC_ALL);
30 my $this_program = 'help2man';
31 my $this_version = '1.35';
36 require Locale
::gettext
;
37 Locale
::gettext
->import;
41 unless ($have_gettext)
43 *gettext
= sub { $_[0] };
51 textdomain
$this_program;
53 my ($user_locale) = grep defined && length,
54 (map $ENV{$_}, qw(LANGUAGE LC_ALL LC_MESSAGES LANG)), 'C';
56 sub kark
# die with message formatted in the invoking user's locale
58 setlocale LC_ALL
, $user_locale;
59 my $fmt = gettext
shift;
60 die +(sprintf $fmt, @_), "\n";
64 my $version_info = sprintf _
(<<'EOT'), $this_program, $this_version;
67 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
69 This is free software; see the source for copying conditions. There is NO
70 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
72 Written by Brendan O'Dea <bod@debian.org>
75 my $help_info = sprintf _
(<<'EOT'), $this_program, $this_program;
76 `%s' generates a man page out of `--help' and `--version' output.
78 Usage: %s [OPTION]... EXECUTABLE
80 -n, --name=STRING description for the NAME paragraph
81 -s, --section=SECTION section number for manual page (1, 6, 8)
82 -m, --manual=TEXT name of manual (User Commands, ...)
83 -S, --source=TEXT source of program (FSF, Debian, ...)
84 -L, --locale=STRING select locale (default "C")
85 -i, --include=FILE include material from `FILE'
86 -I, --opt-include=FILE include material from `FILE' if it exists
87 -o, --output=FILE send output to `FILE'
88 -p, --info-page=TEXT name of Texinfo manual
89 -N, --no-info suppress pointer to Texinfo manual
90 --help print this help, then exit
91 --version print version number, then exit
93 EXECUTABLE should accept `--help' and `--version' options although
94 alternatives may be specified using:
96 -h, --help-option=STRING help option string
97 -v, --version-option=STRING version option string
99 Report bugs to <bug-help2man@gnu.org>.
106 my $help_option = '--help';
107 my $version_option = '--version';
108 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info);
111 'n|name=s' => \
$opt_name,
112 's|section=s' => \
$section,
113 'm|manual=s' => \
$manual,
114 'S|source=s' => \
$source,
115 'L|locale=s' => \
$locale,
116 'i|include=s' => sub { push @opt_include, [ pop, 1 ] },
117 'I|opt-include=s' => sub { push @opt_include, [ pop, 0 ] },
118 'o|output=s' => \
$opt_output,
119 'p|info-page=s' => \
$opt_info,
120 'N|no-info' => \
$opt_no_info,
121 'h|help-option=s' => \
$help_option,
122 'v|version-option=s' => \
$version_option,
126 Getopt
::Long
::config
('bundling');
127 GetOptions
(%opt_def,
128 help
=> sub { print $help_info; exit },
129 version
=> sub { print $version_info; exit },
132 die $help_info unless @ARGV == 1;
134 die "$this_program: no locale support (Locale::gettext required)\n"
135 unless $locale eq 'C' or $have_gettext;
137 # Set localisation of date and executable's ouput.
138 delete @ENV{qw(LANGUAGE LC_MESSAGES LANG)};
139 setlocale LC_ALL
, $ENV{LC_ALL
} = $locale;
143 my @include = (); # retain order given in include file
145 # Process include file (if given). Format is:
158 my ($inc, $required) = @
{shift @opt_include};
160 next unless -f
$inc or $required;
161 kark N_
("%s: can't open `%s' (%s)"), $this_program, $inc, $!
162 unless open INC
, $inc;
165 my $hash = \
%include;
176 push @include, $key unless $include{$key};
181 if (m!^/(.*)/([ims]*)!)
183 my $pat = $2 ?
"(?$2)$1" : $1;
186 eval { $key = qr
($pat) };
189 $@
=~ s/ at .*? line \d.*//;
197 # Check for options before the first section--anything else is
198 # silently ignored, allowing the first for comments and
212 $hash->{$key} ||= '';
218 kark N_
("%s: no valid information found in `%s'"), $this_program, $inc
222 # Compress trailing blank lines.
223 for my $hash (\
(%include, %append))
225 for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
228 # Grab help and version info from executable.
229 my ($help_text, $version_text) = map {
230 join '', map { s/ +$//; expand
$_ } `$ARGV[0] $_ 2>/dev/null`
231 or kark N_
("%s: can't get `%s' info from %s"), $this_program,
233 } $help_option, $version_option;
235 my $date = strftime
"%B %Y", localtime;
236 (my $program = $ARGV[0]) =~ s!.*/!!;
237 my $package = $program;
242 unlink $opt_output or kark N_
("%s: can't unlink %s (%s)"),
243 $this_program, $opt_output, $! if -e
$opt_output;
245 open STDOUT
, ">$opt_output"
246 or kark N_
("%s: can't create %s (%s)"), $this_program, $opt_output, $!;
249 # The first line of the --version information is assumed to be in one
250 # of the following formats:
253 # <program> <version>
254 # {GNU,Free} <program> <version>
255 # <program> ({GNU,Free} <package>) <version>
256 # <program> - {GNU,Free} <package> <version>
258 # and seperated from any copyright/author details by a blank line.
260 ($_, $version_text) = split /\n+/, $version_text, 2;
262 if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
263 /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
269 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
272 $package = $1 ?
"$1$2" : $2;
282 # No info for `info' itself.
283 $opt_no_info = 1 if $program eq 'info';
285 for ($include{_
('NAME')})
287 if ($opt_name) # --name overrides --include contents.
289 $_ = "$program \\- $opt_name\n";
291 elsif ($_) # Use first name given as $program
293 $program = $1 if /^([^\s,]+)(?:,?\s*[^\s,\\-]+)*\s+\\?-/;
295 else # Set a default (useless) NAME paragraph.
297 $_ = sprintf _
("%s \\- manual page for %s %s") . "\n", $program,
302 # Man pages traditionally have the page title in caps.
303 my $PROGRAM = uc $program;
305 # Set default page head/footers
306 $source ||= "$program $version";
311 if (/^(1[Mm]|8)/) { $manual = _
('System Administration Utilities') }
312 elsif (/^6/) { $manual = _
('Games') }
313 else { $manual = _
('User Commands') }
317 # Extract usage clause(s) [if any] for SYNOPSIS.
318 my $PAT_USAGE = _
('Usage');
319 my $PAT_USAGE_CONT = _
('or');
320 if ($help_text =~ s/^($PAT_USAGE):( +(\S+))(.*)((?:\n(?: {6}\1| *($PAT_USAGE_CONT): +\S).*)*)//om)
327 for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
333 $synopsis .= ".br\n" if $synopsis;
336 $synopsis .= ".B $1\n";
338 s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
339 s/^/\\fI/ unless s/^\\fR//;
350 $include{_
('SYNOPSIS')} ||= $synopsis;
353 # Process text, initial section is DESCRIPTION.
354 my $sect = _
('DESCRIPTION');
355 $_ = "$help_text\n\n$version_text";
357 # Normalise paragraph breaks.
362 # Join hyphenated lines.
363 s/([A-Za-z])-\n *([A-Za-z])/$1$2/g;
365 # Temporarily exchange leading dots, apostrophes and backslashes for
371 my $PAT_BUGS = _
('Report +bugs|Email +bug +reports +to');
372 my $PAT_AUTHOR = _
('Written +by');
373 my $PAT_OPTIONS = _
('Options');
374 my $PAT_EXAMPLES = _
('Examples');
375 my $PAT_FREE_SOFTWARE = _
('This +is +free +software');
377 # Start a new paragraph (if required) for these.
378 s/([^\n])\n($PAT_BUGS|$PAT_AUTHOR)/$1\n\n$2/og;
384 # Convert some standard paragraph names.
385 if (s/^($PAT_OPTIONS): *\n//o)
387 $sect = _
('OPTIONS');
390 elsif (s/^($PAT_EXAMPLES): *\n//o)
392 $sect = _
('EXAMPLES');
397 if (/^Copyright +[(\xa9]/)
399 $sect = _
('COPYRIGHT');
400 $include{$sect} ||= '';
401 $include{$sect} .= ".PP\n" if $include{$sect};
404 ($copy, $_) = split /\n\n/, $_, 2;
411 # Convert iso9959-1 copyright symbol or (c) to nroff
413 s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
415 # Insert line breaks before additional copyright messages
416 # and the disclaimer.
417 s/(.)\n(Copyright |$PAT_FREE_SOFTWARE)/$1\n.br\n$2/og;
420 $include{$sect} .= $copy;
425 # Catch bug report text.
426 if (/^($PAT_BUGS) /o)
428 $sect = _
('REPORTING BUGS');
432 elsif (/^($PAT_AUTHOR)/o)
437 # Examples, indicated by an indented leading $, % or > are
438 # rendered in a constant width font.
439 if (/^( +)([\$\%>] )\S/)
444 $include{$sect} ||= '';
445 while (s/^$indent\Q$prefix\E(\S.*)\n*//)
447 $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
455 $include{$sect} ||= '';
457 # Sub-sections have a trailing colon and the second line indented.
458 if (s/^(\S.*:) *\n / /)
460 $matched .= $& if %append;
461 $include{$sect} .= qq(.SS
"$1"\n);
467 # Option with description.
468 if (s/^( {1,10}([+-]\S.*?))(?:( +(?!-))|\n( {20,}))(\S.*)\n//)
470 $matched .= $& if %append;
471 $indent = length ($4 || "$1$3");
472 $content = ".TP\n\x84$2\n\x84$5\n";
475 # Indent may be different on second line.
476 $indent = length $& if /^ {20,}/;
480 # Option without description.
481 elsif (s/^ {1,10}([+-]\S.*)\n//)
483 $matched .= $& if %append;
484 $content = ".HP\n\x84$1\n";
485 $indent = 80; # not continued
488 # Indented paragraph with tag.
489 elsif (s/^( +(\S.*?) +)(\S.*)\n//)
491 $matched .= $& if %append;
493 $content = ".TP\n\x84$2\n\x84$3\n";
496 # Indented paragraph.
497 elsif (s/^( +)(\S.*)\n//)
499 $matched .= $& if %append;
501 $content = ".IP\n\x84$2\n";
504 # Left justified paragraph.
508 $matched .= $& if %append;
509 $content = ".PP\n" if $include{$sect};
513 # Append continuations.
514 while ($indent ?
s/^ {$indent}(\S.*)\n// : s/^(\S.*)\n//)
516 $matched .= $& if %append;
517 $content .= "\x84$1\n"
520 # Move to next paragraph.
525 # Leading dot and apostrophe protection.
531 s/(^| |\()(-[][\w=-]+)/$1 . convert_option $2/mge;
533 # Escape remaining hyphens
537 # Check if matched paragraph contains /pat/.
540 for my $pat (keys %append)
542 if ($matched =~ $pat)
544 $content .= ".PP\n" unless $append{$pat} =~ /^\./;
545 $content .= $append{$pat};
550 $include{$sect} .= $content;
553 # Refer to the real documentation.
554 unless ($opt_no_info)
556 my $info_page = $opt_info || $program;
558 $sect = _
('SEE ALSO');
559 $include{$sect} ||= '';
560 $include{$sect} .= ".PP\n" if $include{$sect};
561 $include{$sect} .= sprintf _
(<<'EOT'), $program, $program, $info_page;
562 The full documentation for
564 is maintained as a Texinfo manual. If the
568 programs are properly installed at your site, the command
570 .B info coreutils '%s invocation'
572 should give you access to the complete manual.
578 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
579 .TH $PROGRAM "$section" "$date" "$source" "$manual"
583 my @pre = (_
('NAME'), _
('SYNOPSIS'), _
('DESCRIPTION'), _
('OPTIONS'),
586 my @post = (_
('AUTHOR'), _
('REPORTING BUGS'), _
('COPYRIGHT'), _
('SEE ALSO'));
587 my $filter = join '|', @pre, @post;
590 for my $sect (@pre, (grep ! /^($filter)$/o, @include), @post)
594 my $lsect = gettext
$sect;
595 my $quote = $lsect =~ /\W/ ?
'"' : '';
596 print ".SH $quote$lsect$quote\n";
598 for ($include{$sect})
600 # Replace leading dot, apostrophe, backslash and hyphen
607 # Convert some latin1 chars to troff equivalents
608 s/\xa0/\\ /g; # non-breaking space
615 close STDOUT
or kark N_
("%s: error writing to %s (%s)"), $this_program,
616 $opt_output || 'stdout', $!;
620 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
621 # embolden. Option arguments get italicised.
624 local $_ = '\fB' . shift;
627 unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)