#!/usr/bin/perl -T
#
# NAME
# m2h - Convert Unix man page to HTML.
#
# SYNOPSIS
# errno(2),
# csh(1),
# csh(1),
#
# DESCRIPTION
# Here's a CGI script that implements the Unix "man" command via the web. To
# use it, put it in your cgi-bin directory as "m2h", and put anchors like the
# above in your html documents. Then when the user clicks on the link, your
# system's "man" command will be run, and the output will be sent to the
# client with minimal HTML wrappings.
#
# This script can also be invoked in forms. It uses the cgi-lib.pl module to
# parse the HTML form arguments. There should be a Man.html file next to this
# script that shows you how you might do this to make your Unix-style "man"
# pages available to web browsers.
#
# REQUIRES
#
use CGI;
use CGI::Carp qw(fatalsToBrowser);
$ENV{MANPATH} = '/usr/man:/usr/share/man:/usr/local/man:/usr/local/lib/perl5/man:/usr/eecs/man';
$home = $ENV{HOME};
$ENV{PATH} = "/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/bin:/sbin:$home/p/cgi";
#
# SETTINGS
#
$cgidir = '/~jc/cgi';
#
# AUTHOR
# John Chambers
$| = 1;
($me = $0) =~ s".*/"";
$V = $ENV{"V_$me"} || 1;
print "Content-type: text/html\n\n";
print "\n";
print "\n" if $V;
# commands:
$man = '/usr/bin/man';
$query = new CGI;
if ($v = $query->param('V')) {$V = $v}
@names = $query->param;
if ($V>1) {
print "
Names:
\n";
for $n (@names) {
$v = $query->param($n);
print "name $n=\"$v\"
";
}
for $e (%ENV) {
print "ENV $e=\"$ENV{$e}\"
";
}
}
if ($s = $query->param('S')) {
splice @ARGV, 0, 0, "-$s";
print "
Section $s\n" if $V>1;
}
if ($t = $query->param('T')) {
push @ARGV, split(/\s+/,$t);
if ($s = $query->param('section')) {$t .= "($s)"}
print "
\n"; $wasblank = 1; while (chomp($line =\n"; } print "\n"; exit 0; # Here's a routine to do convert some of the things in a man # page to html. Mostly what we do is: # 0. Trim away trailing white stuff. # 1. replace html metachars with escape sequences, # 2. replace overstrikes and underscores with ... sub HTMLencode { my($line) = @_; # Trim whitespace. $line =~ s/\s+$//; # Reduce overstruck metachars. $line =~ s/<>/>/g; # Replace HTML metachars. $line =~ s/""/"/g; $line =~ s/&/&/g; $line =~ s/"/"/g; $line =~ s/</g; $line =~ s/>/>/g; # Replace overstrikes and underscores. $line =~ s"(.)\1""g; $line =~ s"(.)\1"$1"g; $line =~ s"_([^&])"$1"g; # Eliminate remaining overstrikes and formfeeds. $line =~ s/.//g; $line =~ s///g; # Reduce adjacent bolds. $line =~ s"""g; $line =~ s"\s+" "g; # Look for pointers to other man pages. This isn't quite perfect. # It can be fooled by things that look like references but aren't, # and some man pages use rather strange ways of pointing to other pages. # But it's close enough that there's not much motive to improve it. while (($a,$p,$s,$z) = ($line =~ /^(.*?)([-\w.]+)\s*\((\d+)\)(\s*-\s.*)$/)) { $line = "$a$p($s)$z"; } while (($a,$p,$s,$z) = ($line =~ /^(.*)([-\w.:]+)<\/b>\s*\((\d\w*)(<\/b>\).*)$/)) { $line = "$a$p($s)$z"; } while (($a,$p,$s,$z) = ($line =~ /^(.*)([\w.]+)<\/b>\s*\((\d\w*)<\/b>\)(.*)$/)) { $line = "$a$p($s)$z"; } while (($a,$p,$s,$z) = ($line =~ /^(.*)([-\w:.]+)\s*\((\d\w*)\)(<\/b>.*)$/)) { $line = "$a$p($s)$z"; } while (($a,$p,$s,$z) = ($line =~ /^(.*)([\w.]+)\s*\((\d\w*)\)<\/b>(.*)$/)) { $line = "$a$p($s)$z"; } while (($a,$p,$s,$z) = ($line =~ /^(.*?)\b([-\w:.]+)\s*\((\d\w*)\)(.*)$/)) { $line = "$a$p($s)$z"; } while (($a,$p,$s,$z) = ($line =~ /^(.*?)\b([\w.]+)\s*\((\d\w*)\)(.*)$/)) { $line = "$a$p($s)$z"; } $line })) { $line = &HTMLencode($line); if ($line =~ /^[A-Z][A-Za-z\s]+$/) { # Manual section marker. $line = '' . $line . ''; } print "Line: \"$line\"\n" if $V>4; print "$line\n" unless ($wasblank && !$line); $wasblank = $line ? 0 : 1; } print "