#!/usr/bin/perl# use with perl man2html.pl whatfile > whatfile.htm# works only on files that are already in formatted format like the # ones in man1 and end on .1# other files can be formatted with nroff before transfering# Stefan Bleeck 20.2.2003# manServer - Unix man page to HTML converter# Rolf Howarth, rolf@squarebox.co.uk# Version 1.07 16 July 2001$version = "1.07";$manServerUrl = "
manServer $version";use Socket;$ENV{'PATH'} = "/bin:/usr/bin";initialise();$request = shift @ARGV;# Usage: manServer [-dn] filename | manServer [-s port]$root = "";$cgiMode = 0;$bodyTag = "BODY bgcolor=#F0F0F0 text=#000000 link=#0000ff vlink=#C000C0 alink=#ff0000";if ($ENV{'GATEWAY_INTERFACE'} ne ""){ *OUT = *STDOUT; open(LOG, ">>/tmp/manServer.log"); chmod(0666, '/tmp/manServer.log'); $root = $ENV{'SCRIPT_NAME'}; $url = $ENV{'PATH_INFO'}; if ($ENV{'REQUEST_METHOD'} eq "POST") { $args =
; chop $args; } else { $args = $ENV{'QUERY_STRING'}; } $url .= "?".$args if ($args); $cgiMode = 1; $date = &fmtTime(time); $remoteHost = $ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'}; $referer = $ENV{'HTTP_REFERER'}; $userAgent = $ENV{'HTTP_USER_AGENT'}; print LOG "$date\t$remoteHost\t$url\t$referer\t$userAgent\n"; processRequest($url);}elsif ($request eq "-s" || $request eq ""){ *LOG = *STDERR; startServer();}else{ $cmdLineMode = 1; if ($request =~ m/^-d(\d)/) { $debug = $1; $request = shift @ARGV; } *OUT = *STDOUT; *LOG = *STDERR; $file = findPage($request); man2html($file);}exit(0);##### Mini HTTP Server ####sub startServer{ ($port) = @ARGV; $port = 8888 unless $port; $sockaddr = 'S n a4 x8'; ($name, $aliases, $proto) = getprotobyname('tcp'); ($name, $aliases, $port) = getservbyname($port, 'tcp') unless $port =~ /^\d+$/; while(1) { $this = pack($sockaddr, AF_INET, $port, "\0\0\0\0"); select(NS); $| = 1; select(stdout); socket(S, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; if (bind(S, $this)) { last; } else { print STDERR "Failed to bind to port $port: $!\n"; ++$port; } } listen(S, 5) || die "connect: $!"; select(S); $| = 1; select(stdout); while(1) { print LOG "Waiting for connection on port $port\n"; ($addr = accept(NS,S)) || die $!; #print "accept ok\n"; ($af,$rport,$inetaddr) = unpack($sockaddr,$addr); @inetaddr = unpack('C4',$inetaddr); print LOG "Got connection from ", join(".",@inetaddr), "\n"; while () { if (m/^GET (\S+)/) { $url = $1; } last if (m/^\s*$/); } *OUT = *NS; processRequest($url); close NS ; }}sub processRequest{ $url = $_[0]; print LOG "Request = $url, root = $root\n"; if ( ($url =~ m/^([^?]*)\?(.*)$/) || ($url =~ m/^([^&]*)&(.*)$/) ) { $request = $1; $args = $2; } else { $request = $url; $args = ""; } @params = split(/[=&]/, $args); for ($i=0; $i<=$#params; ++$i) { $params[$i] =~ tr/+/ /; $params[$i] =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C",hex($1))/eg; } %params = @params; $request = $params{'q'} if ($params{'q'}); $searchType = $params{'t'}; $debug = $params{'d'}; $processed = 0; $file = ""; if ($searchType) { print OUT "HTTP/1.0 200 Ok\n" unless ($cgiMode); print OUT "Content-type: text/html\n\n"; print OUT "Searching not yet implemented
\n"; print LOG "Searching not implemented\n"; $processed = 1; } elsif ($request eq "/" || $request eq "") { print OUT "HTTP/1.0 200 Ok\n" unless ($cgiMode); print OUT "Content-type: text/html\n\n"; print LOG "Home page\n"; homePage(); $processed = 1; } elsif ($request =~ m,^/.*/$,) { print OUT "HTTP/1.0 200 Ok\n" unless ($cgiMode); print OUT "Content-type: text/html\n\n"; print LOG "List directory\n"; listDir($request); $processed = 1; } elsif (-f $request || -f "$request.gz" || -f "$request.bz2") { # Only allow fully specified files if they're in our manpath foreach $md (@manpath) { $dir = $md; if (substr($request,0,length($dir)) eq $dir) { print OUT "HTTP/1.0 200 Ok\n" unless ($cgiMode); print OUT "Content-type: text/html\n\n"; man2html($request); $processed = 1; last; } } } else { $file = findPage($request); if (@multipleMatches) { print OUT "HTTP/1.0 200 Ok\n" unless ($cgiMode); print OUT "Content-type: text/html\n\n"; print LOG "Multiple matches\n"; printMatches(); $processed = 1; } elsif ($file) { print OUT "HTTP/1.0 301 Redirected\n" unless ($cgiMode); $file .= "&d=$debug" if ($debug); print OUT "Location: $root$file\n\n"; print LOG "Redirect to $root$file\n"; $processed = 1; } } unless ($processed) { print OUT "HTTP/1.0 404 Not Found\n" unless ($cgiMode); print OUT "Content-type: text/html\n\n"; print OUT "\nNot Found\n<$bodyTag>\n"; print OUT "
Not Found
\nFailed to find man page /$request\n"; print OUT "
Main Index\n\n"; print STDERR "Failed to find /$request\n" unless ($cgiMode); }}sub homePage{ print OUT "
Manual Pages - Main Index<$bodyTag>
Manual Reference Pages - Main Index
\n"; loadManDirs(); foreach $dir (@mandirs) { ($section) = ($dir =~ m/man([0-9A-Za-z]+)$/); print OUT "$dir" ; print OUT "- $sectionName{$section}" if ($sectionName{$section}); print OUT "
\n"; } print OUT "
Generated by $manServerUrl from local unix man pages.\n\n";}sub listDir{ foreach $md (@manpath) { $dir = $md; if (substr($request,0,length($dir)) eq $dir) { $request =~ s,/$,,; ($section) = ($request =~ m/man([0-9A-Za-z]+)$/); $sectionName = $sectionName{$section}; $sectionName = "Manual Reference Pages" unless ($sectionName); print OUT "
Contents of $request\n<$bodyTag>\n"; print OUT "
$sectionName - Index of $request
\n"; print OUT "\n"; if (opendir(DIR, $request)) { @files = sort readdir DIR; foreach $f (@files) { next if ($f eq "." || $f eq ".." || $f !~ m/\./); $f =~ s/\.(gz|bz2)$//; # ($name) = ($f =~ m,/([^/]*)$,); print OUT "$f \n"; } closedir DIR; } print OUT "
Main Index\n\n"; print OUT "
Generated by $manServerUrl from local unix man pages.\n