1N/A#!/usr/bin/perl -w
1N/A
1N/A# Generate a short man page from --help and --version output.
1N/A# Copyright � 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
1N/A
1N/A# This program is free software; you can redistribute it and/or modify
1N/A# it under the terms of the GNU General Public License as published by
1N/A# the Free Software Foundation; either version 2, or (at your option)
1N/A# any later version.
1N/A
1N/A# This program is distributed in the hope that it will be useful,
1N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A# GNU General Public License for more details.
1N/A
1N/A# You should have received a copy of the GNU General Public License
1N/A# along with this program; if not, write to the Free Software Foundation,
1N/A# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1N/A
1N/A# Written by Brendan O'Dea <bod@compusol.com.au>
1N/A# Available from ftp://ftp.gnu.org/gnu/help2man/
1N/A
1N/Ause 5.004;
1N/Ause strict;
1N/Ause Getopt::Long;
1N/Ause Text::Tabs qw(expand);
1N/Ause POSIX qw(strftime setlocale LC_TIME);
1N/A
1N/Amy $this_program = 'help2man';
1N/Amy $this_version = '1.23';
1N/Amy $version_info = <<EOT;
1N/AGNU $this_program $this_version
1N/A
1N/ACopyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
1N/AThis is free software; see the source for copying conditions. There is NO
1N/Awarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1N/A
1N/AWritten by Brendan O'Dea <bod\@compusol.com.au>
1N/AEOT
1N/A
1N/Amy $help_info = <<EOT;
1N/A`$this_program' generates a man page out of `--help' and `--version' output.
1N/A
1N/AUsage: $this_program [OPTION]... EXECUTABLE
1N/A
1N/A -n, --name=STRING use `STRING' as the description for the NAME paragraph
1N/A -s, --section=SECTION use `SECTION' as the section for the man page
1N/A -i, --include=FILE include material from `FILE'
1N/A -I, --opt-include=FILE include material from `FILE' if it exists
1N/A -o, --output=FILE send output to `FILE'
1N/A -N, --no-info suppress pointer to Texinfo manual
1N/A --help print this help, then exit
1N/A --version print version number, then exit
1N/A
1N/AEXECUTABLE should accept `--help' and `--version' options.
1N/A
1N/AReport bugs to <bug-help2man\@gnu.org>.
1N/AEOT
1N/A
1N/Amy $section = 1;
1N/Amy ($opt_name, @opt_include, $opt_output, $opt_no_info);
1N/A
1N/A# Parse options.
1N/AGetopt::Long::config('bundling');
1N/AGetOptions (
1N/A 'n|name=s' => \$opt_name,
1N/A 's|section=s' => \$section,
1N/A 'i|include=s' => sub { push @opt_include, [ pop, 1 ] },
1N/A 'I|opt-include=s' => sub { push @opt_include, [ pop, 0 ] },
1N/A 'o|output=s' => \$opt_output,
1N/A 'N|no-info' => \$opt_no_info,
1N/A help => sub { print $help_info; exit },
1N/A version => sub { print $version_info; exit },
1N/A) or die $help_info;
1N/A
1N/Adie $help_info unless @ARGV == 1;
1N/A
1N/Amy %include = ();
1N/Amy %append = ();
1N/Amy @include = (); # retain order given in include file
1N/A
1N/A# Provide replacement `quote-regex' operator for pre-5.005.
1N/ABEGIN { eval q(sub qr { '' =~ $_[0]; $_[0] }) if $] < 5.005 }
1N/A
1N/A# Process include file (if given). Format is:
1N/A#
1N/A# [section name]
1N/A# verbatim text
1N/A#
1N/A# or
1N/A#
1N/A# /pattern/
1N/A# verbatim text
1N/A#
1N/A
1N/Afor (@opt_include)
1N/A{
1N/A my ($inc, $required) = @$_;
1N/A
1N/A next unless -f $inc or $required;
1N/A die "$this_program: can't open `$inc' ($!)\n"
1N/A unless open INC, $inc;
1N/A
1N/A my $key;
1N/A my $hash = \%include;
1N/A
1N/A while (<INC>)
1N/A {
1N/A # [section]
1N/A if (/^\[([^]]+)\]/)
1N/A {
1N/A $key = uc $1;
1N/A $key =~ s/^\s+//;
1N/A $key =~ s/\s+$//;
1N/A $hash = \%include;
1N/A push @include, $key unless $include{$key};
1N/A next;
1N/A }
1N/A
1N/A # /pattern/
1N/A if (m!^/(.*)/([ims]*)!)
1N/A {
1N/A my $pat = $2 ? "(?$2)$1" : $1;
1N/A
1N/A # Check pattern.
1N/A eval { $key = qr($pat) };
1N/A if ($@)
1N/A {
1N/A $@ =~ s/ at .*? line \d.*//;
1N/A die "$inc:$.:$@";
1N/A }
1N/A
1N/A $hash = \%append;
1N/A next;
1N/A }
1N/A
1N/A # Silently ignore anything before the first
1N/A # section--allows for comments and revision info.
1N/A next unless $key;
1N/A
1N/A $hash->{$key} ||= '';
1N/A $hash->{$key} .= $_;
1N/A }
1N/A
1N/A close INC;
1N/A
1N/A die "$this_program: no valid information found in `$inc'\n"
1N/A unless $key;
1N/A}
1N/A
1N/A# Compress trailing blank lines.
1N/Afor my $hash (\(%include, %append))
1N/A{
1N/A for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
1N/A}
1N/A
1N/A# Turn off localisation of executable's ouput.
1N/A@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
1N/A
1N/A# Turn off localisation of date (for strftime).
1N/Asetlocale LC_TIME, 'C';
1N/A
1N/A# Grab help and version info from executable.
1N/Amy ($help_text, $version_text) = map {
1N/A join '', map { s/ +$//; expand $_ } `$ARGV[0] --$_ 2>/dev/null`
1N/A or die "$this_program: can't get `--$_' info from $ARGV[0]\n"
1N/A} qw(help version);
1N/A
1N/Amy $date = strftime "%B %Y", localtime;
1N/A(my $program = $ARGV[0]) =~ s!.*/!!;
1N/Amy $package = $program;
1N/Amy $version;
1N/A
1N/Aif ($opt_output)
1N/A{
1N/A unlink $opt_output
1N/A or die "$this_program: can't unlink $opt_output ($!)\n"
1N/A if -e $opt_output;
1N/A
1N/A open STDOUT, ">$opt_output"
1N/A or die "$this_program: can't create $opt_output ($!)\n";
1N/A}
1N/A
1N/A# The first line of the --version information is assumed to be in one
1N/A# of the following formats:
1N/A#
1N/A# <version>
1N/A# <program> <version>
1N/A# {GNU,Free} <program> <version>
1N/A# <program> ({GNU,Free} <package>) <version>
1N/A# <program> - {GNU,Free} <package> <version>
1N/A#
1N/A# and seperated from any copyright/author details by a blank line.
1N/A
1N/A($_, $version_text) = split /\n+/, $version_text, 2;
1N/A
1N/Aif (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
1N/A /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
1N/A{
1N/A $program = $1;
1N/A $package = $2;
1N/A $version = $3;
1N/A}
1N/Aelsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
1N/A{
1N/A $program = $2;
1N/A $package = $1 ? "$1$2" : $2;
1N/A $version = $3;
1N/A}
1N/Aelse
1N/A{
1N/A $version = $_;
1N/A}
1N/A
1N/A$program =~ s!.*/!!;
1N/A
1N/A# No info for `info' itself.
1N/A$opt_no_info = 1 if $program eq 'info';
1N/A
1N/A# --name overrides --include contents.
1N/A$include{NAME} = "$program \\- $opt_name\n" if $opt_name;
1N/A
1N/A# Default (useless) NAME paragraph.
1N/A$include{NAME} ||= "$program \\- manual page for $program $version\n";
1N/A
1N/A# Man pages traditionally have the page title in caps.
1N/Amy $PROGRAM = uc $program;
1N/A
1N/A# Extract usage clause(s) [if any] for SYNOPSIS.
1N/Aif ($help_text =~ s/^Usage:( +(\S+))(.*)((?:\n(?: {6}\1| *or: +\S).*)*)//m)
1N/A{
1N/A my @syn = $2 . $3;
1N/A
1N/A if ($_ = $4)
1N/A {
1N/A s/^\n//;
1N/A for (split /\n/) { s/^ *(or: +)?//; push @syn, $_ }
1N/A }
1N/A
1N/A my $synopsis = '';
1N/A for (@syn)
1N/A {
1N/A $synopsis .= ".br\n" if $synopsis;
1N/A s!^\S*/!!;
1N/A s/^(\S+) *//;
1N/A $synopsis .= ".B $1\n";
1N/A s/\s+$//;
1N/A s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
1N/A s/^/\\fI/ unless s/^\\fR//;
1N/A $_ .= '\fR';
1N/A s/(\\fI)( *)/$2$1/g;
1N/A s/\\fI\\fR//g;
1N/A s/^\\fR//;
1N/A s/\\fI$//;
1N/A s/^\./\\&./;
1N/A
1N/A $synopsis .= "$_\n";
1N/A }
1N/A
1N/A $include{SYNOPSIS} ||= $synopsis;
1N/A}
1N/A
1N/A# Process text, initial section is DESCRIPTION.
1N/Amy $sect = 'DESCRIPTION';
1N/A$_ = "$help_text\n\n$version_text";
1N/A
1N/A# Normalise paragraph breaks.
1N/As/^\n+//;
1N/As/\n*$/\n/;
1N/As/\n\n+/\n\n/g;
1N/A
1N/A# Temporarily exchange leading dots, apostrophes and backslashes for
1N/A# tokens.
1N/As/^\./\x80/mg;
1N/As/^'/\x81/mg;
1N/As/\\/\x82/g;
1N/A
1N/A# Start a new paragraph (if required) for these.
1N/As/([^\n])\n(Report +bugs|Email +bug +reports +to|Written +by)/$1\n\n$2/g;
1N/A
1N/Asub convert_option;
1N/A
1N/Awhile (length)
1N/A{
1N/A # Convert some standard paragraph names.
1N/A if (s/^(Options|Examples): *\n//)
1N/A {
1N/A $sect = uc $1;
1N/A next;
1N/A }
1N/A
1N/A # Copyright section
1N/A if (/^Copyright +[(\xa9]/)
1N/A {
1N/A $sect = 'COPYRIGHT';
1N/A $include{$sect} ||= '';
1N/A $include{$sect} .= ".PP\n" if $include{$sect};
1N/A
1N/A my $copy;
1N/A ($copy, $_) = split /\n\n/, $_, 2;
1N/A
1N/A for ($copy)
1N/A {
1N/A # Add back newline
1N/A s/\n*$/\n/;
1N/A
1N/A # Convert iso9959-1 copyright symbol or (c) to nroff
1N/A # character.
1N/A s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
1N/A
1N/A # Insert line breaks before additional copyright messages
1N/A # and the disclaimer.
1N/A s/(.)\n(Copyright |This +is +free +software)/$1\n.br\n$2/g;
1N/A
1N/A # Join hyphenated lines.
1N/A s/([A-Za-z])-\n */$1/g;
1N/A }
1N/A
1N/A $include{$sect} .= $copy;
1N/A $_ ||= '';
1N/A next;
1N/A }
1N/A
1N/A # Catch bug report text.
1N/A if (/^(Report +bugs|Email +bug +reports +to) /)
1N/A {
1N/A $sect = 'REPORTING BUGS';
1N/A }
1N/A
1N/A # Author section.
1N/A elsif (/^Written +by/)
1N/A {
1N/A $sect = 'AUTHOR';
1N/A }
1N/A
1N/A # Examples, indicated by an indented leading $, % or > are
1N/A # rendered in a constant width font.
1N/A if (/^( +)([\$\%>] )\S/)
1N/A {
1N/A my $indent = $1;
1N/A my $prefix = $2;
1N/A my $break = '.IP';
1N/A $include{$sect} ||= '';
1N/A while (s/^$indent\Q$prefix\E(\S.*)\n*//)
1N/A {
1N/A $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
1N/A $break = '.br';
1N/A }
1N/A
1N/A next;
1N/A }
1N/A
1N/A my $matched = '';
1N/A $include{$sect} ||= '';
1N/A
1N/A # Sub-sections have a trailing colon and the second line indented.
1N/A if (s/^(\S.*:) *\n / /)
1N/A {
1N/A $matched .= $& if %append;
1N/A $include{$sect} .= qq(.SS "$1"\n);
1N/A }
1N/A
1N/A my $indent = 0;
1N/A my $content = '';
1N/A
1N/A # Option with description.
1N/A if (s/^( {1,10}([+-]\S.*?))(?:( +)|\n( {20,}))(\S.*)\n//)
1N/A {
1N/A $matched .= $& if %append;
1N/A $indent = length ($4 || "$1$3");
1N/A $content = ".TP\n\x83$2\n\x83$5\n";
1N/A unless ($4)
1N/A {
1N/A # Indent may be different on second line.
1N/A $indent = length $& if /^ {20,}/;
1N/A }
1N/A }
1N/A
1N/A # Option without description.
1N/A elsif (s/^ {1,10}([+-]\S.*)\n//)
1N/A {
1N/A $matched .= $& if %append;
1N/A $content = ".HP\n\x83$1\n";
1N/A $indent = 80; # not continued
1N/A }
1N/A
1N/A # Indented paragraph with tag.
1N/A elsif (s/^( +(\S.*?) +)(\S.*)\n//)
1N/A {
1N/A $matched .= $& if %append;
1N/A $indent = length $1;
1N/A $content = ".TP\n\x83$2\n\x83$3\n";
1N/A }
1N/A
1N/A # Indented paragraph.
1N/A elsif (s/^( +)(\S.*)\n//)
1N/A {
1N/A $matched .= $& if %append;
1N/A $indent = length $1;
1N/A $content = ".IP\n\x83$2\n";
1N/A }
1N/A
1N/A # Left justified paragraph.
1N/A else
1N/A {
1N/A s/(.*)\n//;
1N/A $matched .= $& if %append;
1N/A $content = ".PP\n" if $include{$sect};
1N/A $content .= "$1\n";
1N/A }
1N/A
1N/A # Append continuations.
1N/A while (s/^ {$indent}(\S.*)\n//)
1N/A {
1N/A $matched .= $& if %append;
1N/A $content .= "\x83$1\n"
1N/A }
1N/A
1N/A # Move to next paragraph.
1N/A s/^\n+//;
1N/A
1N/A for ($content)
1N/A {
1N/A # Leading dot and apostrophe protection.
1N/A s/\x83\./\x80/g;
1N/A s/\x83'/\x81/g;
1N/A s/\x83//g;
1N/A
1N/A # Convert options.
1N/A s/(^| )(-[][\w=-]+)/$1 . convert_option $2/mge;
1N/A }
1N/A
1N/A # Check if matched paragraph contains /pat/.
1N/A if (%append)
1N/A {
1N/A for my $pat (keys %append)
1N/A {
1N/A if ($matched =~ $pat)
1N/A {
1N/A $content .= ".PP\n" unless $append{$pat} =~ /^\./;
1N/A $content .= $append{$pat};
1N/A }
1N/A }
1N/A }
1N/A
1N/A $include{$sect} .= $content;
1N/A}
1N/A
1N/A# Refer to the real documentation.
1N/Aunless ($opt_no_info)
1N/A{
1N/A $sect = 'SEE ALSO';
1N/A $include{$sect} ||= '';
1N/A $include{$sect} .= ".PP\n" if $include{$sect};
1N/A $include{$sect} .= <<EOT;
1N/AThe full documentation for
1N/A.B $program
1N/Ais maintained as a Texinfo manual. If the
1N/A.B info
1N/Aand
1N/A.B $program
1N/Aprograms are properly installed at your site, the command
1N/A.IP
1N/A.B info $program
1N/A.PP
1N/Ashould give you access to the complete manual.
1N/AEOT
1N/A}
1N/A
1N/A# Output header.
1N/Aprint <<EOT;
1N/A.\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
1N/A.TH $PROGRAM "$section" "$date" "$package $version" FSF
1N/AEOT
1N/A
1N/A# Section ordering.
1N/Amy @pre = qw(NAME SYNOPSIS DESCRIPTION OPTIONS EXAMPLES);
1N/Amy @post = ('AUTHOR', 'REPORTING BUGS', 'COPYRIGHT', 'SEE ALSO');
1N/Amy $filter = join '|', @pre, @post;
1N/A
1N/A# Output content.
1N/Afor (@pre, (grep ! /^($filter)$/o, @include), @post)
1N/A{
1N/A if ($include{$_})
1N/A {
1N/A my $quote = /\W/ ? '"' : '';
1N/A print ".SH $quote$_$quote\n";
1N/A
1N/A for ($include{$_})
1N/A {
1N/A # Replace leading dot, apostrophe and backslash tokens.
1N/A s/\x80/\\&./g;
1N/A s/\x81/\\&'/g;
1N/A s/\x82/\\e/g;
1N/A print;
1N/A }
1N/A }
1N/A}
1N/A
1N/Aexit;
1N/A
1N/A# Convert option dashes to \- to stop nroff from hyphenating 'em, and
1N/A# embolden. Option arguments get italicised.
1N/Asub convert_option
1N/A{
1N/A local $_ = '\fB' . shift;
1N/A
1N/A s/-/\\-/g;
1N/A unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
1N/A {
1N/A s/=(.)/\\fR=\\fI$1/;
1N/A s/ (.)/ \\fI$1/;
1N/A $_ .= '\fR';
1N/A }
1N/A
1N/A $_;
1N/A}