#!/usr/bin/perl # # 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'; # # 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; $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 "Unix Manual Page for $t\n"; } else { print "Unix Manual Page\n"; } if ($p = $query->param('P')) { $ENV{MANPATH} = $p; } ($host = `uname -n`) =~ s/\s*$//; ($stype = `uname -s`) =~ s/\s*$//; for $a (@ARGV) { print "Arg: \"$a\"\n" if $V>1; if ($a =~ /^-(.*)/) { print "
Option: $a\n" if $V>1; $S = $1; next; } $cmd = "man $S $a 2>&1"; print "Unix manual page for $a.$S (host=$host system=$stype)\n"; print "
\n"; print "Command: $cmd\n" if $V>1; print "
Hostname: ", `hostname` if $V>1; print "
System: ", `uname -a` if $V>1; print "
MANPATH: ", $ENV{MANPATH} if $V>1; print "
uname: ", `which uname` if $V>1; print "
man: ", `which man` if $V>1; print "
\n" if $V>1; open(P,"$cmd |") || die "### Can't run \"$cmd\" [$!]\n"; print "
\n";
	$wasblank = 1;
	while (chomp($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 "

\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; # 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 }