maint: avoid a valgrind memory leak warning from pinky
[coreutils.git] / man / help2man
blob8e83f355d788dac5101466345f1dc70d30f4f651
1 #!/usr/bin/perl -w
3 # Generate a short man page from --help and --version output.
4 # Copyright (C) 1997-2013 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Written by Brendan O'Dea <bod@debian.org>
20 # Available from ftp://ftp.gnu.org/gnu/help2man/
22 use 5.005;
23 use strict;
24 use Getopt::Long;
25 use Text::Tabs qw(expand);
26 use POSIX qw(strftime setlocale LC_ALL);
27 use locale;
29 my $this_program = 'help2man';
30 my $this_version = '1.35';
32 my $have_gettext;
33 BEGIN {
34 eval {
35 require Locale::gettext;
36 Locale::gettext->import (qw(gettext textdomain));
37 $have_gettext = 1;
40 unless ($have_gettext)
42 *gettext = sub { $_[0] };
43 *textdomain = sub {};
47 sub _ { gettext @_ }
48 sub N_ { $_[0] }
50 textdomain $this_program;
52 my ($user_locale) = grep defined && length,
53 (map $ENV{$_}, qw(LANGUAGE LC_ALL LC_MESSAGES LANG)), 'C';
55 sub kark # die with message formatted in the invoking user's locale
57 setlocale LC_ALL, $user_locale;
58 my $fmt = gettext shift;
59 die +(sprintf $fmt, @_), "\n";
63 my $version_info = sprintf _(<<'EOT'), $this_program, $this_version;
64 GNU %s %s
66 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
67 Foundation, Inc.
68 This is free software; see the source for copying conditions. There is NO
69 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
71 Written by Brendan O'Dea <bod@debian.org>
72 EOT
74 my $help_info = sprintf _(<<'EOT'), $this_program, $this_program;
75 '%s' generates a man page out of '--help' and '--version' output.
77 Usage: %s [OPTION]... EXECUTABLE
79 -n, --name=STRING description for the NAME paragraph
80 -s, --section=SECTION section number for manual page (1, 6, 8)
81 -m, --manual=TEXT name of manual (User Commands, ...)
82 -S, --source=TEXT source of program (FSF, Debian, ...)
83 -L, --locale=STRING select locale (default "C")
84 -i, --include=FILE include material from 'FILE'
85 -I, --opt-include=FILE include material from 'FILE' if it exists
86 -o, --output=FILE send output to 'FILE'
87 -p, --info-page=TEXT name of Texinfo manual
88 -N, --no-info suppress pointer to Texinfo manual
89 --help print this help, then exit
90 --version print version number, then exit
92 EXECUTABLE should accept '--help' and '--version' options although
93 alternatives may be specified using:
95 -h, --help-option=STRING help option string
96 -v, --version-option=STRING version option string
98 Report bugs to <bug-help2man@gnu.org>.
99 EOT
101 my $section = 1;
102 my $manual = '';
103 my $source = '';
104 my $locale = 'C';
105 my $help_option = '--help';
106 my $version_option = '--version';
107 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info);
109 my %opt_def = (
110 'n|name=s' => \$opt_name,
111 's|section=s' => \$section,
112 'm|manual=s' => \$manual,
113 'S|source=s' => \$source,
114 'L|locale=s' => \$locale,
115 'i|include=s' => sub { push @opt_include, [ pop, 1 ] },
116 'I|opt-include=s' => sub { push @opt_include, [ pop, 0 ] },
117 'o|output=s' => \$opt_output,
118 'p|info-page=s' => \$opt_info,
119 'N|no-info' => \$opt_no_info,
120 'h|help-option=s' => \$help_option,
121 'v|version-option=s' => \$version_option,
124 # Parse options.
125 Getopt::Long::config('bundling');
126 GetOptions (%opt_def,
127 help => sub { print $help_info; exit },
128 version => sub { print $version_info; exit },
129 ) or die $help_info;
131 die $help_info unless @ARGV == 1;
133 die "$this_program: no locale support (Locale::gettext required)\n"
134 unless $locale eq 'C' or $have_gettext;
136 # Set localization of date and executable's output.
137 delete @ENV{qw(LANGUAGE LC_MESSAGES LANG)};
138 setlocale LC_ALL, $ENV{LC_ALL} = $locale;
140 my %include = ();
141 my %append = ();
142 my @include = (); # retain order given in include file
144 # Process include file (if given). Format is:
146 # [section name]
147 # verbatim text
149 # or
151 # /pattern/
152 # verbatim text
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 = \%include;
166 while (<INC>)
168 # [section]
169 if (/^\[([^]]+)\]/)
171 $key = uc $1;
172 $key =~ s/^\s+//;
173 $key =~ s/\s+$//;
174 $hash = \%include;
175 push @include, $key unless $include{$key};
176 next;
179 # /pattern/
180 if (m!^/(.*)/([ims]*)!)
182 my $pat = $2 ? "(?$2)$1" : $1;
184 # Check pattern.
185 eval { $key = qr($pat) };
186 if ($@)
188 $@ =~ s/ at .*? line \d.*//;
189 die "$inc:$.:$@";
192 $hash = \%append;
193 next;
196 # Check for options before the first section--anything else is
197 # silently ignored, allowing the first for comments and
198 # revision info.
199 unless ($key)
201 # handle options
202 if (/^-/)
204 local @ARGV = split;
205 GetOptions %opt_def;
208 next;
211 $hash->{$key} ||= '';
212 $hash->{$key} .= $_;
215 close INC;
217 kark N_("%s: no valid information found in '%s'"), $this_program, $inc
218 unless $key;
221 # Compress trailing blank lines.
222 for my $hash (\(%include, %append))
224 for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
227 # Grab help and version info from executable.
228 my ($help_text, $version_text) = map {
229 join '', map { s/ +$//; expand $_ } `$ARGV[0] $_ 2>/dev/null`
230 or kark N_("%s: can't get '%s' info from %s"), $this_program,
231 $_, $ARGV[0]
232 } $help_option, $version_option;
234 my $date = strftime "%B %Y", localtime;
235 (my $program = $ARGV[0]) =~ s!.*/!!;
236 my $package = $program;
237 my $version;
239 if ($opt_output)
241 unlink $opt_output or kark N_("%s: can't unlink %s (%s)"),
242 $this_program, $opt_output, $! if -e $opt_output;
244 open STDOUT, ">$opt_output"
245 or kark N_("%s: can't create %s (%s)"), $this_program, $opt_output, $!;
248 # The first line of the --version information is assumed to be in one
249 # of the following formats:
251 # <version>
252 # <program> <version>
253 # {GNU,Free} <program> <version>
254 # <program> ({GNU,Free} <package>) <version>
255 # <program> - {GNU,Free} <package> <version>
257 # and separated from any copyright/author details by a blank line.
259 ($_, $version_text) = split /\n+/, $version_text, 2;
261 if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
262 /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
264 $program = $1;
265 $package = $2;
266 $version = $3;
268 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
270 $program = $2;
271 $package = $1 ? "$1$2" : $2;
272 $version = $3;
274 else
276 $version = $_;
279 $program =~ s!.*/!!;
281 # No info for 'info' itself.
282 $opt_no_info = 1 if $program eq 'info';
284 for ($include{_('NAME')})
286 if ($opt_name) # --name overrides --include contents.
288 $_ = "$program \\- $opt_name\n";
290 elsif ($_) # Use first name given as $program
292 $program = $1 if /^([^\s,]+)(?:,?\s*[^\s,\\-]+)*\s+\\?-/;
294 else # Set a default (useless) NAME paragraph.
296 $_ = sprintf _("%s \\- manual page for %s %s") . "\n", $program,
297 $program, $version;
301 # Man pages traditionally have the page title in caps.
302 my $PROGRAM = uc $program;
304 # Set default page head/footers
305 $source ||= "$program $version";
306 unless ($manual)
308 for ($section)
310 if (/^(1[Mm]|8)/) { $manual = _('System Administration Utilities') }
311 elsif (/^6/) { $manual = _('Games') }
312 else { $manual = _('User Commands') }
316 # Extract usage clause(s) [if any] for SYNOPSIS.
317 my $PAT_USAGE = _('Usage');
318 my $PAT_USAGE_CONT = _('or');
319 if ($help_text =~ s/^($PAT_USAGE):
320 ([ ]+(\S+))
321 (.*)
322 ((?:\n(?:[ ]{6}\1|[ ]*($PAT_USAGE_CONT):[ ]+\S).*)*)//omx)
324 my @syn = $3 . $4;
326 if ($_ = $5)
328 s/^\n//;
329 for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
332 my $synopsis = '';
333 for (@syn)
335 $synopsis .= ".br\n" if $synopsis;
336 s!^\S*/!!;
337 s/^(\S+) *//;
338 $synopsis .= ".B $1\n";
339 s/\s+$//;
340 s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
341 s/^/\\fI/ unless s/^\\fR//;
342 $_ .= '\fR';
343 s/(\\fI)( *)/$2$1/g;
344 s/\\fI\\fR//g;
345 s/^\\fR//;
346 s/\\fI$//;
347 s/^\./\\&./;
349 $synopsis .= "$_\n";
352 $include{_('SYNOPSIS')} ||= $synopsis;
355 # Process text, initial section is DESCRIPTION.
356 my $sect = _('DESCRIPTION');
357 $_ = "$help_text\n\n$version_text";
359 # Normalise paragraph breaks.
360 s/^\n+//;
361 s/\n*$/\n/;
362 s/\n\n+/\n\n/g;
364 # Join hyphenated lines.
365 s/([A-Za-z])-\n *([A-Za-z])/$1$2/g;
367 # Temporarily exchange leading dots, apostrophes and backslashes for
368 # tokens.
369 s/^\./\x80/mg;
370 s/^'/\x81/mg;
371 s/\\/\x82/g;
373 my $PAT_BUGS = _('Report +(?:\w+ +)?bugs|Email +bug +reports +to');
374 my $PAT_AUTHOR = _('Written +by');
375 my $PAT_OPTIONS = _('Options');
376 my $PAT_EXAMPLES = _('Examples');
377 my $PAT_FREE_SOFTWARE = _('This +is +free +software');
378 my $PAT_INFO = _('For +complete +documentation');
380 # Start a new paragraph (if required) for these.
381 s/([^\n])\n($PAT_BUGS|$PAT_AUTHOR)/$1\n\n$2/og;
383 sub convert_option;
385 while (length)
387 # Convert some standard paragraph names.
388 if (s/^($PAT_OPTIONS): *\n//o)
390 $sect = _('OPTIONS');
391 next;
393 elsif (s/^($PAT_EXAMPLES): *\n//o)
395 $sect = _('EXAMPLES');
396 next;
398 # Skip any texinfo reference as that's handled separately
399 if (s/($PAT_INFO).*\n//o)
401 next;
404 # Copyright section
405 if (/^Copyright +[(\xa9]/)
407 $sect = _('COPYRIGHT');
408 $include{$sect} ||= '';
409 $include{$sect} .= ".PP\n" if $include{$sect};
411 my $copy;
412 ($copy, $_) = split /\n\n/, $_, 2;
414 for ($copy)
416 # Add back newline
417 s/\n*$/\n/;
419 # Convert iso9959-1 copyright symbol or (c) to nroff
420 # character.
421 s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
423 # Insert line breaks before additional copyright messages
424 # and the disclaimer.
425 s/(.)\n(Copyright |$PAT_FREE_SOFTWARE)/$1\n.br\n$2/og;
428 $include{$sect} .= $copy;
429 $_ ||= '';
430 next;
433 # Catch bug report text.
434 if (/^($PAT_BUGS) /o)
436 $sect = _('REPORTING BUGS');
439 # Author section.
440 elsif (/^($PAT_AUTHOR)/o)
442 $sect = _('AUTHOR');
445 # Examples, indicated by an indented leading $, % or > are
446 # rendered in a constant width font.
447 if (/^( +)([\$\%>] )\S/)
449 my $indent = $1;
450 my $prefix = $2;
451 my $break = '.IP';
452 $include{$sect} ||= '';
453 while (s/^$indent\Q$prefix\E(\S.*)\n*//)
455 $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
456 $break = '.br';
459 next;
462 my $matched = '';
463 $include{$sect} ||= '';
465 # Sub-sections have a trailing colon and the second line indented.
466 if (s/^(\S.*:) *\n / /)
468 $matched .= $& if %append;
469 $include{$sect} .= qq(.SS "$1"\n);
472 my $indent = 0;
473 my $content = '';
475 # Option with description.
476 if (s/^( {1,10}([+-]\S.*?))(?:( +(?!-))|\n( {20,}))(\S.*)\n//)
478 $matched .= $& if %append;
479 $indent = length ($4 || "$1$3");
480 $content = ".TP\n\x84$2\n\x84$5\n";
481 unless ($4)
483 # Indent may be different on second line.
484 $indent = length $& if /^ {20,}/;
488 # Option without description.
489 elsif (s/^ {1,10}([+-]\S.*)\n//)
491 $matched .= $& if %append;
492 $content = ".HP\n\x84$1\n";
493 $indent = 80; # not continued
496 # Indented paragraph with tag.
497 elsif (s/^( +(\S.*?) +)(\S.*)\n//)
499 $matched .= $& if %append;
500 $indent = length $1;
501 $content = ".TP\n\x84$2\n\x84$3\n";
504 # Indented paragraph.
505 elsif (s/^( +)(\S.*)\n//)
507 $matched .= $& if %append;
508 $indent = length $1;
509 $content = ".IP\n\x84$2\n";
512 # Left justified paragraph.
513 else
515 s/(.*)\n//;
516 $matched .= $& if %append;
517 $content = ".PP\n" if $include{$sect};
518 $content .= "$1\n";
521 # Append continuations.
522 while ($indent ? s/^ {$indent}(\S.*)\n// : s/^(\S.*)\n//)
524 $matched .= $& if %append;
525 $content .= "\x84$1\n"
528 # Move to next paragraph.
529 s/^\n+//;
531 for ($content)
533 # Leading dot and apostrophe protection.
534 s/\x84\./\x80/g;
535 s/\x84'/\x81/g;
536 s/\x84//g;
538 # Convert options.
539 s/(^| |\()(-[][\w=-]+)/$1 . convert_option $2/mge;
541 # Escape remaining hyphens
542 s/-/\x83/g;
545 # Check if matched paragraph contains /pat/.
546 if (%append)
548 for my $pat (keys %append)
550 if ($matched =~ $pat)
552 $content .= ".PP\n" unless $append{$pat} =~ /^\./;
553 $content .= $append{$pat};
558 $include{$sect} .= $content;
561 # Refer to the real documentation.
562 unless ($opt_no_info)
564 my $info_page = $opt_info || $program;
566 $sect = _('SEE ALSO');
567 $include{$sect} ||= '';
568 $include{$sect} .= ".PP\n" if $include{$sect};
569 $include{$sect} .= sprintf _(<<'EOT'), $program, $program, $info_page;
570 The full documentation for
571 .B %s
572 is maintained as a Texinfo manual. If the
573 .B info
575 .B %s
576 programs are properly installed at your site, the command
578 .B info coreutils \(aq%s invocation\(aq
580 should give you access to the complete manual.
584 # Output header.
585 print <<EOT;
586 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
587 .TH $PROGRAM "$section" "$date" "$source" "$manual"
590 # Section ordering.
591 my @pre = (_('NAME'), _('SYNOPSIS'), _('DESCRIPTION'), _('OPTIONS'),
592 _('EXAMPLES'));
594 my @post = (_('AUTHOR'), _('REPORTING BUGS'), _('COPYRIGHT'), _('SEE ALSO'));
595 my $filter = join '|', @pre, @post;
597 # Output content.
598 for my $sect (@pre, (grep ! /^($filter)$/o, @include), @post)
600 if ($include{$sect})
602 my $lsect = gettext $sect;
603 my $quote = $lsect =~ /\W/ ? '"' : '';
604 print ".SH $quote$lsect$quote\n";
606 for ($include{$sect})
608 # Replace leading dot, apostrophe, backslash and hyphen
609 # tokens.
610 s/\x80/\\&./g;
611 s/\x81/\\&'/g;
612 s/\x82/\\e/g;
613 s/\x83/\\-/g;
615 # Convert some latin1 chars to troff equivalents
616 s/\xa0/\\ /g; # non-breaking space
618 $sect eq 'REPORTING BUGS'
619 and s/\n(.)/\n.br\n$1/g;
621 print;
626 close STDOUT or kark N_("%s: error writing to %s (%s)"), $this_program,
627 $opt_output || 'stdout', $!;
629 exit;
631 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
632 # embolden. Option arguments get italicised.
633 sub convert_option
635 local $_ = '\fB' . shift;
637 s/-/\x83/g;
638 unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
640 s/=(.)/\\fR=\\fI$1/;
641 s/ (.)/ \\fI$1/;
642 $_ .= '\fR';