Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 249 lines (197 sloc) 5.892 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#!/usr/bin/perl -w

# Generate a short man page from --help and --version output.
# Copyright © 1997, 98 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

require 5.003;

use strict;
use Getopt::Long;
use POSIX 'strftime';

my $RCS_Id = '$Id: help2man.pl,v 1.1 1998/01/22 05:53:32 bod Exp $';
my $this_program = 'help2man';
my $this_version = '0.0';

if ($RCS_Id =~ /\$Id:\s+(\S+)\s+(\S+)/)
{
    $this_version = $2;
   ($this_program = $1) =~ s/(\.pl)?,v$//;
}

my $version_info = <<EOT;
$this_program $this_version

Copyright (C) 1997, 98 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOT

my $help_info = <<EOT;
`$this_program' generates a man page out of `--help' and `--version' output.

Usage: $this_program [OPTION]... EXECUTABLE

  --name=STRING use `STRING' as the description for the NAME paragraph
  --help print this help, then exit
  --version print $this_program program version number, then exit

EXECUTABLE should accept `--help' and `version' options.
EOT

my ($name, $opt_help, $opt_version);

# Parse options.
GetOptions (
    'name=s' => \$name,
    help => \$opt_help,
    version => \$opt_version,
) or die $help_info;

print $help_info and exit if $opt_help;
print $version_info and exit if $opt_version;

die $help_info unless @ARGV == 1;

# Turn off localisation of executable's ouput.
@ENV{qw(LANGUAGE LANG LC_ALL)} = qw(C C C);

# Grab help and version paragraphs from executable
my @help = split /\n\n+/, `$ARGV[0] --help 2>/dev/null`
    or die "$this_program: can't get `--help' info from $ARGV[0]\n";

my @version = split /\n\n+/, `$ARGV[0] --version 2>/dev/null`
    or die "$this_program: can't get `--version' info from $ARGV[0]\n";

my $date = strftime "%B %Y", localtime;
my $program = $ARGV[0]; $program =~ s!.*/!!;
my $package = $program;
my $version;

# The first line of the --version information is assumed to be in one
# of the following formats:
#
# <version>
# <program> <version>
# GNU <program> <version>
# <program> (GNU <package>) <version>
# <program> - GNU <package> <version>
#

$_ = shift @version;

if (/^(\S+)\s+\((GNU\s+[^)]+)\)\s+(.*)/ ||
    /^(\S+)\s+-\s*(GNU\s+\S+)\s+(.*)/)
{
    $program = $1;
    $package = $2;
    $version = $3;
}
elsif (/^(GNU\s+)?(\S+)\s+(.*)/)
{
    $program = $2;
    $package = $1 ? "$1$2" : $2;
    $version = $3;
}
else
{
    $version = $_;
}

# Default description for NAME paragraph
$name ||= "short documentation for $program $version";

# Man pages traditionally have the page title in caps.
my $PROGRAM = uc $program;

# Header.
print <<EOT;
.\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
.TH $PROGRAM 1 "$date" "$package $version" "GNU User's Manual"
.SH NAME
$program \\- $name
EOT

my $accumulate = 1;
my @description = ();

sub convert_option;

# Output converted --help information.
for (@help)
{
    chomp;

    if (s/^Usage:\s+\S+\s+(.*)\n?//)
    {
# Turn the usage clause into a synopsis.
print ".SH SYNOPSIS\n.B $program\n";

my $syn = $1;
$syn =~ s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
s/^/\\fI/ unless $syn =~ s/^\\fR//;

print "$syn\\fR\n";

# Dump any accumulated description text.
$accumulate = 0;
print ".SH DESCRIPTION\n";
print @description;

next unless $_;
    }

    # Accumulate text if the synopsis has not been produced yet.
    if ($accumulate)
    {
push @description, ".PP\n" if @description;
push @description, "$_\n";
next;
    }

    # Catch bug report text.
    if (/^Report bugs /)
    {
print ".SH BUGS\n$_\n";
next;
    }

    # Special case for tar 1.12: --label=NAME\nPATTERN.
    s{(\n[ \t]*)(-V,[ \t]+--label=NAME.*)\n[ \t]+PATTERN[ \t]+}
     {$1$2$1\\&...=PATTERN };

    # Convert options.
    s/(\s)(-[][\w=-]+|\\&\S+)/$1.convert_option $2/ge;

    # Option subsections have second line indented.
    print qq(.SS "$1"\n) if s/^(\S.*)\n(\s)/$2/;

    # Lines indented more than about 10 spaces may be assumed to be
    # continuations of the previous line.
    s/\n {10,}/ /g;

    # Indented paragraph.
    if (/^\s/)
    {
for (split /\n/)
{
s/^\s+//;
s/([^,])\s+/$1\n/;
print ".TP\n$_\n";
}
    }
    # Anything else.
    else
    {
print ".PP\n$_\n";
    }
}

# Refer to the real documentation.

print <<EOT;
.SH SEE ALSO
The full documentation for
.B $program
is maintained as a Texinfo manual. If the
.B info
and
.B $program
programs are properly installed at your site, the command
.IP
.B info $program
.PP
should allow you to access the manual as an hypertext.
EOT

# Output converted --version information.
for (@version)
{
    chomp;

    if (/^Copyright\s+\(C/) { print ".SH COPYRIGHT\n" }
    elsif (/^Written\s+by/) { print ".SH AUTHOR\n" }
    else { print ".PP\n"; }

    print "$_\n";
}

exit;

# Convert option dashes to \- to stop nroff from hyphenating 'em, and
# embolden. Option arguments get italicised.
sub convert_option
{
    my $option = '\fB' . shift;

    $option =~ s/-/\\-/g;
    unless ($option =~ s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
    {
$option =~ s/=(.)/\\fR=\\fI$1/;
$option .= '\fR';
    }

    $option;
}
Something went wrong with that request. Please try again.