Don't modify argv in dd due to ',' in arguments.
[coreutils.git] / man / help2man
blob1bc12cf5af0de9f5a53221b00e1cf22256eeeee1
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, 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/
23 use 5.005;
24 use strict;
25 use Getopt::Long;
26 use Text::Tabs qw(expand);
27 use POSIX qw(strftime setlocale LC_ALL);
28 use locale;
30 my $this_program = 'help2man';
31 my $this_version = '1.35';
33 my $have_gettext;
34 BEGIN {
35 eval {
36 require Locale::gettext;
37 Locale::gettext->import;
38 $have_gettext = 1;
41 unless ($have_gettext)
43 *gettext = sub { $_[0] };
44 *textdomain = sub {};
48 sub _ { gettext @_ }
49 sub N_ { $_[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;
65 GNU %s %s
67 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
68 Foundation, Inc.
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>
73 EOT
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>.
102 my $section = 1;
103 my $manual = '';
104 my $source = '';
105 my $locale = 'C';
106 my $help_option = '--help';
107 my $version_option = '--version';
108 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info);
110 my %opt_def = (
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,
125 # Parse options.
126 Getopt::Long::config('bundling');
127 GetOptions (%opt_def,
128 help => sub { print $help_info; exit },
129 version => sub { print $version_info; exit },
130 ) or die $help_info;
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;
141 my %include = ();
142 my %append = ();
143 my @include = (); # retain order given in include file
145 # Process include file (if given). Format is:
147 # [section name]
148 # verbatim text
150 # or
152 # /pattern/
153 # verbatim text
156 while (@opt_include)
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;
164 my $key;
165 my $hash = \%include;
167 while (<INC>)
169 # [section]
170 if (/^\[([^]]+)\]/)
172 $key = uc $1;
173 $key =~ s/^\s+//;
174 $key =~ s/\s+$//;
175 $hash = \%include;
176 push @include, $key unless $include{$key};
177 next;
180 # /pattern/
181 if (m!^/(.*)/([ims]*)!)
183 my $pat = $2 ? "(?$2)$1" : $1;
185 # Check pattern.
186 eval { $key = qr($pat) };
187 if ($@)
189 $@ =~ s/ at .*? line \d.*//;
190 die "$inc:$.:$@";
193 $hash = \%append;
194 next;
197 # Check for options before the first section--anything else is
198 # silently ignored, allowing the first for comments and
199 # revision info.
200 unless ($key)
202 # handle options
203 if (/^-/)
205 local @ARGV = split;
206 GetOptions %opt_def;
209 next;
212 $hash->{$key} ||= '';
213 $hash->{$key} .= $_;
216 close INC;
218 kark N_("%s: no valid information found in `%s'"), $this_program, $inc
219 unless $key;
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,
232 $_, $ARGV[0]
233 } $help_option, $version_option;
235 my $date = strftime "%B %Y", localtime;
236 (my $program = $ARGV[0]) =~ s!.*/!!;
237 my $package = $program;
238 my $version;
240 if ($opt_output)
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:
252 # <version>
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+) +(.*)/)
265 $program = $1;
266 $package = $2;
267 $version = $3;
269 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
271 $program = $2;
272 $package = $1 ? "$1$2" : $2;
273 $version = $3;
275 else
277 $version = $_;
280 $program =~ s!.*/!!;
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,
298 $program, $version;
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";
307 unless ($manual)
309 for ($section)
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)
322 my @syn = $3 . $4;
324 if ($_ = $5)
326 s/^\n//;
327 for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
330 my $synopsis = '';
331 for (@syn)
333 $synopsis .= ".br\n" if $synopsis;
334 s!^\S*/!!;
335 s/^(\S+) *//;
336 $synopsis .= ".B $1\n";
337 s/\s+$//;
338 s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
339 s/^/\\fI/ unless s/^\\fR//;
340 $_ .= '\fR';
341 s/(\\fI)( *)/$2$1/g;
342 s/\\fI\\fR//g;
343 s/^\\fR//;
344 s/\\fI$//;
345 s/^\./\\&./;
347 $synopsis .= "$_\n";
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.
358 s/^\n+//;
359 s/\n*$/\n/;
360 s/\n\n+/\n\n/g;
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
366 # tokens.
367 s/^\./\x80/mg;
368 s/^'/\x81/mg;
369 s/\\/\x82/g;
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;
380 sub convert_option;
382 while (length)
384 # Convert some standard paragraph names.
385 if (s/^($PAT_OPTIONS): *\n//o)
387 $sect = _('OPTIONS');
388 next;
390 elsif (s/^($PAT_EXAMPLES): *\n//o)
392 $sect = _('EXAMPLES');
393 next;
396 # Copyright section
397 if (/^Copyright +[(\xa9]/)
399 $sect = _('COPYRIGHT');
400 $include{$sect} ||= '';
401 $include{$sect} .= ".PP\n" if $include{$sect};
403 my $copy;
404 ($copy, $_) = split /\n\n/, $_, 2;
406 for ($copy)
408 # Add back newline
409 s/\n*$/\n/;
411 # Convert iso9959-1 copyright symbol or (c) to nroff
412 # character.
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;
421 $_ ||= '';
422 next;
425 # Catch bug report text.
426 if (/^($PAT_BUGS) /o)
428 $sect = _('REPORTING BUGS');
431 # Author section.
432 elsif (/^($PAT_AUTHOR)/o)
434 $sect = _('AUTHOR');
437 # Examples, indicated by an indented leading $, % or > are
438 # rendered in a constant width font.
439 if (/^( +)([\$\%>] )\S/)
441 my $indent = $1;
442 my $prefix = $2;
443 my $break = '.IP';
444 $include{$sect} ||= '';
445 while (s/^$indent\Q$prefix\E(\S.*)\n*//)
447 $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
448 $break = '.br';
451 next;
454 my $matched = '';
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);
464 my $indent = 0;
465 my $content = '';
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";
473 unless ($4)
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;
492 $indent = length $1;
493 $content = ".TP\n\x84$2\n\x84$3\n";
496 # Indented paragraph.
497 elsif (s/^( +)(\S.*)\n//)
499 $matched .= $& if %append;
500 $indent = length $1;
501 $content = ".IP\n\x84$2\n";
504 # Left justified paragraph.
505 else
507 s/(.*)\n//;
508 $matched .= $& if %append;
509 $content = ".PP\n" if $include{$sect};
510 $content .= "$1\n";
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.
521 s/^\n+//;
523 for ($content)
525 # Leading dot and apostrophe protection.
526 s/\x84\./\x80/g;
527 s/\x84'/\x81/g;
528 s/\x84//g;
530 # Convert options.
531 s/(^| |\()(-[][\w=-]+)/$1 . convert_option $2/mge;
533 # Escape remaining hyphens
534 s/-/\x83/g;
537 # Check if matched paragraph contains /pat/.
538 if (%append)
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
563 .B %s
564 is maintained as a Texinfo manual. If the
565 .B info
567 .B %s
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.
576 # Output header.
577 print <<EOT;
578 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
579 .TH $PROGRAM "$section" "$date" "$source" "$manual"
582 # Section ordering.
583 my @pre = (_('NAME'), _('SYNOPSIS'), _('DESCRIPTION'), _('OPTIONS'),
584 _('EXAMPLES'));
586 my @post = (_('AUTHOR'), _('REPORTING BUGS'), _('COPYRIGHT'), _('SEE ALSO'));
587 my $filter = join '|', @pre, @post;
589 # Output content.
590 for my $sect (@pre, (grep ! /^($filter)$/o, @include), @post)
592 if ($include{$sect})
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
601 # tokens.
602 s/\x80/\\&./g;
603 s/\x81/\\&'/g;
604 s/\x82/\\e/g;
605 s/\x83/\\-/g;
607 # Convert some latin1 chars to troff equivalents
608 s/\xa0/\\ /g; # non-breaking space
610 print;
615 close STDOUT or kark N_("%s: error writing to %s (%s)"), $this_program,
616 $opt_output || 'stdout', $!;
618 exit;
620 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
621 # embolden. Option arguments get italicised.
622 sub convert_option
624 local $_ = '\fB' . shift;
626 s/-/\x83/g;
627 unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
629 s/=(.)/\\fR=\\fI$1/;
630 s/ (.)/ \\fI$1/;
631 $_ .= '\fR';