Home | History | Annotate | Download | only in build-aux
      1 #!/usr/bin/env perl
      2 
      3 # Generate a short man page from --help and --version output.
      4 # Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software
      5 # Foundation, Inc.
      6 
      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 2, or (at your option)
     10 # any later version.
     11 
     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.
     16 
     17 # You should have received a copy of the GNU General Public License
     18 # along with this program; if not, write to the Free Software Foundation,
     19 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
     20 
     21 # Written by Brendan O'Dea <bod (at] debian.org>
     22 # Available from ftp://ftp.gnu.org/gnu/help2man/
     23 
     24 use 5.005;
     25 use strict;
     26 use Getopt::Long;
     27 use Text::Tabs qw(expand);
     28 use POSIX qw(strftime setlocale LC_TIME);
     29 
     30 my $this_program = 'help2man';
     31 my $this_version = '1.28';
     32 my $version_info = <<EOT;
     33 GNU $this_program $this_version
     34 
     35 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
     36 This is free software; see the source for copying conditions.  There is NO
     37 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     38 
     39 Written by Brendan O'Dea <bod\@debian.org>
     40 EOT
     41 
     42 my $help_info = <<EOT;
     43 `$this_program' generates a man page out of `--help' and `--version' output.
     44 
     45 Usage: $this_program [OPTION]... EXECUTABLE
     46 
     47  -n, --name=STRING       description for the NAME paragraph
     48  -s, --section=SECTION   section number for manual page (1, 6, 8)
     49  -m, --manual=TEXT       name of manual (User Commands, ...)
     50  -S, --source=TEXT       source of program (FSF, Debian, ...)
     51  -i, --include=FILE      include material from `FILE'
     52  -I, --opt-include=FILE  include material from `FILE' if it exists
     53  -o, --output=FILE       send output to `FILE'
     54  -p, --info-page=TEXT    name of Texinfo manual
     55  -N, --no-info           suppress pointer to Texinfo manual
     56      --help              print this help, then exit
     57      --version           print version number, then exit
     58 
     59 EXECUTABLE should accept `--help' and `--version' options although
     60 alternatives may be specified using:
     61 
     62  -h, --help-option=STRING     help option string
     63  -v, --version-option=STRING  version option string
     64 
     65 Report bugs to <bug-help2man\@gnu.org>.
     66 EOT
     67 
     68 my $section = 1;
     69 my $manual = '';
     70 my $source = '';
     71 my $help_option = '--help';
     72 my $version_option = '--version';
     73 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info);
     74 
     75 my %opt_def = (
     76     'n|name=s'		 => \$opt_name,
     77     's|section=s'	 => \$section,
     78     'm|manual=s'	 => \$manual,
     79     'S|source=s'	 => \$source,
     80     'i|include=s'	 => sub { push @opt_include, [ pop, 1 ] },
     81     'I|opt-include=s'	 => sub { push @opt_include, [ pop, 0 ] },
     82     'o|output=s'	 => \$opt_output,
     83     'p|info-page=s'	 => \$opt_info,
     84     'N|no-info'		 => \$opt_no_info,
     85     'h|help-option=s'	 => \$help_option,
     86     'v|version-option=s' => \$version_option,
     87 );
     88 
     89 # Parse options.
     90 Getopt::Long::config('bundling');
     91 GetOptions (%opt_def,
     92     help    => sub { print $help_info; exit },
     93     version => sub { print $version_info; exit },
     94 ) or die $help_info;
     95 
     96 die $help_info unless @ARGV == 1;
     97 
     98 my %include = ();
     99 my %append = ();
    100 my @include = (); # retain order given in include file
    101 
    102 # Process include file (if given).  Format is:
    103 #
    104 #   [section name]
    105 #   verbatim text
    106 #
    107 # or
    108 #
    109 #   /pattern/
    110 #   verbatim text
    111 #
    112 
    113 while (@opt_include)
    114 {
    115     my ($inc, $required) = @{shift @opt_include};
    116 
    117     next unless -f $inc or $required;
    118     die "$this_program: can't open `$inc' ($!)\n"
    119 	unless open INC, $inc;
    120 
    121     my $key;
    122     my $hash = \%include;
    123 
    124     while (<INC>)
    125     {
    126 	# [section]
    127 	if (/^\[([^]]+)\]/)
    128 	{
    129 	    $key = uc $1;
    130 	    $key =~ s/^\s+//;
    131 	    $key =~ s/\s+$//;
    132 	    $hash = \%include;
    133 	    push @include, $key unless $include{$key};
    134 	    next;
    135 	}
    136 
    137 	# /pattern/
    138 	if (m!^/(.*)/([ims]*)!)
    139 	{
    140 	    my $pat = $2 ? "(?$2)$1" : $1;
    141 
    142 	    # Check pattern.
    143 	    eval { $key = qr($pat) };
    144 	    if ($@)
    145 	    {
    146 		$@ =~ s/ at .*? line \d.*//;
    147 		die "$inc:$.:$@";
    148 	    }
    149 
    150 	    $hash = \%append;
    151 	    next;
    152 	}
    153 
    154 	# Check for options before the first section--anything else is
    155 	# silently ignored, allowing the first for comments and
    156 	# revision info.
    157 	unless ($key)
    158 	{
    159 	    # handle options
    160 	    if (/^-/)
    161 	    {
    162 		local @ARGV = split;
    163 		GetOptions %opt_def;
    164 	    }
    165 
    166 	    next;
    167 	}
    168 
    169 	$hash->{$key} ||= '';
    170 	$hash->{$key} .= $_;
    171     }
    172 
    173     close INC;
    174 
    175     die "$this_program: no valid information found in `$inc'\n"
    176 	unless $key;
    177 }
    178 
    179 # Compress trailing blank lines.
    180 for my $hash (\(%include, %append))
    181 {
    182     for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
    183 }
    184 
    185 # Turn off localisation of executable's ouput.
    186 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
    187 
    188 # Turn off localisation of date (for strftime).
    189 setlocale LC_TIME, 'C';
    190 
    191 # Grab help and version info from executable.
    192 my ($help_text, $version_text) = map {
    193     join '', map { s/ +$//; expand $_ } `$ARGV[0] $_ 2>/dev/null`
    194 	or die "$this_program: can't get `$_' info from $ARGV[0]\n"
    195 } $help_option, $version_option;
    196 
    197 my $date = strftime "%B %Y", localtime;
    198 (my $program = $ARGV[0]) =~ s!.*/!!;
    199 my $package = $program;
    200 my $version;
    201 
    202 if ($opt_output)
    203 {
    204     unlink $opt_output
    205 	or die "$this_program: can't unlink $opt_output ($!)\n"
    206 	if -e $opt_output;
    207 
    208     open STDOUT, ">$opt_output"
    209 	or die "$this_program: can't create $opt_output ($!)\n";
    210 }
    211 
    212 # The first line of the --version information is assumed to be in one
    213 # of the following formats:
    214 #
    215 #   <version>
    216 #   <program> <version>
    217 #   {GNU,Free} <program> <version>
    218 #   <program> ({GNU,Free} <package>) <version>
    219 #   <program> - {GNU,Free} <package> <version>
    220 #
    221 # and seperated from any copyright/author details by a blank line.
    222 
    223 ($_, $version_text) = split /\n+/, $version_text, 2;
    224 
    225 if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
    226     /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
    227 {
    228     $program = $1;
    229     $package = $2;
    230     $version = $3;
    231 }
    232 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
    233 {
    234     $program = $2;
    235     $package = $1 ? "$1$2" : $2;
    236     $version = $3;
    237 }
    238 else
    239 {
    240     $version = $_;
    241 }
    242 
    243 $program =~ s!.*/!!;
    244 
    245 # No info for `info' itself.
    246 $opt_no_info = 1 if $program eq 'info';
    247 
    248 # --name overrides --include contents.
    249 $include{NAME} = "$program \\- $opt_name\n" if $opt_name;
    250 
    251 # Default (useless) NAME paragraph.
    252 $include{NAME} ||= "$program \\- manual page for $program $version\n";
    253 
    254 # Man pages traditionally have the page title in caps.
    255 my $PROGRAM = uc $program;
    256 
    257 # Set default page head/footers
    258 $source ||= "$program $version";
    259 unless ($manual)
    260 {
    261     for ($section)
    262     {
    263 	if (/^(1[Mm]|8)/) { $manual = 'System Administration Utilities' }
    264 	elsif (/^6/)	  { $manual = 'Games' }
    265 	else		  { $manual = 'User Commands' }
    266     }
    267 }
    268 
    269 # Extract usage clause(s) [if any] for SYNOPSIS.
    270 if ($help_text =~ s/^Usage:( +(\S+))(.*)((?:\n(?: {6}\1| *or: +\S).*)*)//m)
    271 {
    272     my @syn = $2 . $3;
    273 
    274     if ($_ = $4)
    275     {
    276 	s/^\n//;
    277 	for (split /\n/) { s/^ *(or: +)?//; push @syn, $_ }
    278     }
    279 
    280     my $synopsis = '';
    281     for (@syn)
    282     {
    283 	$synopsis .= ".br\n" if $synopsis;
    284 	s!^\S*/!!;
    285 	s/^(\S+) *//;
    286 	$synopsis .= ".B $1\n";
    287 	s/\s+$//;
    288 	s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
    289 	s/^/\\fI/ unless s/^\\fR//;
    290 	$_ .= '\fR';
    291 	s/(\\fI)( *)/$2$1/g;
    292 	s/\\fI\\fR//g;
    293 	s/^\\fR//;
    294 	s/\\fI$//;
    295 	s/^\./\\&./;
    296 
    297 	$synopsis .= "$_\n";
    298     }
    299 
    300     $include{SYNOPSIS} ||= $synopsis;
    301 }
    302 
    303 # Process text, initial section is DESCRIPTION.
    304 my $sect = 'DESCRIPTION';
    305 $_ = "$help_text\n\n$version_text";
    306 
    307 # Normalise paragraph breaks.
    308 s/^\n+//;
    309 s/\n*$/\n/;
    310 s/\n\n+/\n\n/g;
    311 
    312 # Temporarily exchange leading dots, apostrophes and backslashes for
    313 # tokens.
    314 s/^\./\x80/mg;
    315 s/^'/\x81/mg;
    316 s/\\/\x82/g;
    317 
    318 # Start a new paragraph (if required) for these.
    319 s/([^\n])\n(Report +bugs|Email +bug +reports +to|Written +by)/$1\n\n$2/g;
    320 
    321 sub convert_option;
    322 
    323 while (length)
    324 {
    325     # Convert some standard paragraph names.
    326     if (s/^(Options|Examples): *\n//)
    327     {
    328 	$sect = uc $1;
    329 	next;
    330     }
    331 
    332     # Copyright section
    333     if (/^Copyright +[(\xa9]/)
    334     {
    335 	$sect = 'COPYRIGHT';
    336 	$include{$sect} ||= '';
    337 	$include{$sect} .= ".PP\n" if $include{$sect};
    338 
    339 	my $copy;
    340 	($copy, $_) = split /\n\n/, $_, 2;
    341 
    342 	for ($copy)
    343 	{
    344 	    # Add back newline
    345 	    s/\n*$/\n/;
    346 
    347 	    # Convert iso9959-1 copyright symbol or (c) to nroff
    348 	    # character.
    349 	    s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
    350 
    351 	    # Insert line breaks before additional copyright messages
    352 	    # and the disclaimer.
    353 	    s/(.)\n(Copyright |This +is +free +software)/$1\n.br\n$2/g;
    354 
    355 	    # Join hyphenated lines.
    356 	    s/([A-Za-z])-\n */$1/g;
    357 	}
    358 
    359 	$include{$sect} .= $copy;
    360 	$_ ||= '';
    361 	next;
    362     }
    363 
    364     # Catch bug report text.
    365     if (/^(Report +bugs|Email +bug +reports +to) /)
    366     {
    367 	$sect = 'REPORTING BUGS';
    368     }
    369 
    370     # Author section.
    371     elsif (/^Written +by/)
    372     {
    373 	$sect = 'AUTHOR';
    374     }
    375 
    376     # Examples, indicated by an indented leading $, % or > are
    377     # rendered in a constant width font.
    378     if (/^( +)([\$\%>] )\S/)
    379     {
    380 	my $indent = $1;
    381 	my $prefix = $2;
    382 	my $break = '.IP';
    383 	$include{$sect} ||= '';
    384 	while (s/^$indent\Q$prefix\E(\S.*)\n*//)
    385 	{
    386 	    $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
    387 	    $break = '.br';
    388 	}
    389 
    390 	next;
    391     }
    392 
    393     my $matched = '';
    394     $include{$sect} ||= '';
    395 
    396     # Sub-sections have a trailing colon and the second line indented.
    397     if (s/^(\S.*:) *\n / /)
    398     {
    399 	$matched .= $& if %append;
    400 	$include{$sect} .= qq(.SS "$1"\n);
    401     }
    402 
    403     my $indent = 0;
    404     my $content = '';
    405 
    406     # Option with description.
    407     if (s/^( {1,10}([+-]\S.*?))(?:(  +(?!-))|\n( {20,}))(\S.*)\n//)
    408     {
    409 	$matched .= $& if %append;
    410 	$indent = length ($4 || "$1$3");
    411 	$content = ".TP\n\x83$2\n\x83$5\n";
    412 	unless ($4)
    413 	{
    414 	    # Indent may be different on second line.
    415 	    $indent = length $& if /^ {20,}/;
    416 	}
    417     }
    418 
    419     # Option without description.
    420     elsif (s/^ {1,10}([+-]\S.*)\n//)
    421     {
    422 	$matched .= $& if %append;
    423 	$content = ".HP\n\x83$1\n";
    424 	$indent = 80; # not continued
    425     }
    426 
    427     # Indented paragraph with tag.
    428     elsif (s/^( +(\S.*?)  +)(\S.*)\n//)
    429     {
    430 	$matched .= $& if %append;
    431 	$indent = length $1;
    432 	$content = ".TP\n\x83$2\n\x83$3\n";
    433     }
    434 
    435     # Indented paragraph.
    436     elsif (s/^( +)(\S.*)\n//)
    437     {
    438 	$matched .= $& if %append;
    439 	$indent = length $1;
    440 	$content = ".IP\n\x83$2\n";
    441     }
    442 
    443     # Left justified paragraph.
    444     else
    445     {
    446 	s/(.*)\n//;
    447 	$matched .= $& if %append;
    448 	$content = ".PP\n" if $include{$sect};
    449 	$content .= "$1\n";
    450     }
    451 
    452     # Append continuations.
    453     while (s/^ {$indent}(\S.*)\n//)
    454     {
    455 	$matched .= $& if %append;
    456 	$content .= "\x83$1\n"
    457     }
    458 
    459     # Move to next paragraph.
    460     s/^\n+//;
    461 
    462     for ($content)
    463     {
    464 	# Leading dot and apostrophe protection.
    465 	s/\x83\./\x80/g;
    466 	s/\x83'/\x81/g;
    467 	s/\x83//g;
    468 
    469 	# Convert options.
    470 	s/(^| )(-[][\w=-]+)/$1 . convert_option $2/mge;
    471     }
    472 
    473     # Check if matched paragraph contains /pat/.
    474     if (%append)
    475     {
    476 	for my $pat (keys %append)
    477 	{
    478 	    if ($matched =~ $pat)
    479 	    {
    480 		$content .= ".PP\n" unless $append{$pat} =~ /^\./;
    481 		$content .= $append{$pat};
    482 	    }
    483 	}
    484     }
    485 
    486     $include{$sect} .= $content;
    487 }
    488 
    489 # Refer to the real documentation.
    490 unless ($opt_no_info)
    491 {
    492     my $info_page = $opt_info || $program;
    493 
    494     $sect = 'SEE ALSO';
    495     $include{$sect} ||= '';
    496     $include{$sect} .= ".PP\n" if $include{$sect};
    497     $include{$sect} .= <<EOT;
    498 The full documentation for
    499 .B $program
    500 is maintained as a Texinfo manual.  If the
    501 .B info
    502 and
    503 .B $program
    504 programs are properly installed at your site, the command
    505 .IP
    506 .B info $info_page
    507 .PP
    508 should give you access to the complete manual.
    509 EOT
    510 }
    511 
    512 # Output header.
    513 print <<EOT;
    514 .\\" DO NOT MODIFY THIS FILE!  It was generated by $this_program $this_version.
    515 .TH $PROGRAM "$section" "$date" "$source" "$manual"
    516 EOT
    517 
    518 # Section ordering.
    519 my @pre = qw(NAME SYNOPSIS DESCRIPTION OPTIONS EXAMPLES);
    520 my @post = ('AUTHOR', 'REPORTING BUGS', 'COPYRIGHT', 'SEE ALSO');
    521 my $filter = join '|', @pre, @post;
    522 
    523 # Output content.
    524 for (@pre, (grep ! /^($filter)$/o, @include), @post)
    525 {
    526     if ($include{$_})
    527     {
    528 	my $quote = /\W/ ? '"' : '';
    529 	print ".SH $quote$_$quote\n";
    530 	
    531 	for ($include{$_})
    532 	{
    533 	    # Replace leading dot, apostrophe and backslash tokens.
    534 	    s/\x80/\\&./g;
    535 	    s/\x81/\\&'/g;
    536 	    s/\x82/\\e/g;
    537 	    print;
    538 	}
    539     }
    540 }
    541 
    542 exit;
    543 
    544 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
    545 # embolden.  Option arguments get italicised.
    546 sub convert_option
    547 {
    548     local $_ = '\fB' . shift;
    549 
    550     s/-/\\-/g;
    551     unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
    552     {
    553 	s/=(.)/\\fR=\\fI$1/;
    554 	s/ (.)/ \\fI$1/;
    555 	$_ .= '\fR';
    556     }
    557 
    558     $_;
    559 }
    560