$hman="$ENV{HMANPRG} $ENV{HMANOPT}";
$ManPrg='/usr/bin/man';
$ENV{PAGER} = '/bin/cat';
($ManPathFind = $ENV{MANPATH}) =~ s/:/ /g;
$hmanpl='/cgi-bin/hman';
$emptyman=5;
$HtmlTail='<hr><A HREF="' . $hmanpl . '">Back to Hman</a>';
$HtmlHdr="Content-type: text/html\n\n";
@ManSections = (
'1', 'user commands',
'2', 'system calls',
'3', 'subroutines',
'4', 'devices',
'5', 'file formats',
'6', 'games',
'7', 'miscellanious',
'8', 'sys. admin.',
'9', 'Linux Internals',
'n', 'section n'
);
select((select(STDOUT), $| = 1)[0]);
print $HtmlHdr;
$string = $ENV{QUERY_STRING};
if($string eq "") { initialForm(); }
$DirectPath = $ManSection = $ManTopic = "";
$string =~ s/&/'; \$/g;
$string =~ s/=/='/g;
$string =~ s/^(.*)$/\$$1';/;
eval $string;
hmanDirect($DirectPath) if ($DirectPath ne "");
if ($ManTopic eq "") { badness("<code>Topic for man search needed</code>\n"); }
if ($ManSection eq "") { badness("<code>No section specified</code>\n"); }
$ManSection =~ s/all//;
if ($ManSection =~ /key/) { manKey($ManTopic); }
findIt($ManTopic);
open(MANOUT, "$ManPrg $ManSection $ManTopic |")
|| die "$hmanpl: can't run \"$ManPrg Section $ManTopic |\", $!\n";
for (0..$emptyman) {
$temp = <MANOUT> || last;
push @temp, $temp;
}
#
if (@temp < $emptyman) {
close(MANOUT);
print"<strong>Request failed for topic $ManTopic:</strong>\n<code>";
for (@temp) {print;}
print "</code><br><h3>Let's try a keyword search:</h3><hr>\n";
manKey($ManTopic);
}
#
$cmd = "$hman -r \"$hmanpl?ManTopic=%s&ManSection=%s\" -l \"Man page for $ManTopic";
if ($ManSection eq "") {
$cmd .= '"';
} else {
$cmd .= "($ManSection)\"";
}
#
open(HMANIN, "| $cmd") || die "$hmanpl: can't open $cmd: $!\n";
for (@temp) {print HMANIN;}
while(<MANOUT>){print HMANIN;}
close(MANOUT);
close(HMANIN);
exitIt();
###############################################################################
sub initialForm {
print <<EOF;
<h1>Select a manual page:</h1>
<FORM METHOD="GET" action="$hmanpl">
<table border=0>
<tr>
<td align=right>Section:</td>
<td>
<select name=ManSection>
<OPTION value=all>All</option>
<OPTION value=key>Keyword Search</option>
EOF
for ($ndx = 0; $ndx < @ManSections; $ndx += 2) {
print ' <OPTION value=' . $ManSections[$ndx] . '>'
. "$ManSections[$ndx] - $ManSections[$ndx + 1] "
. '</option>' . "\n";
}
print <<EOF;
</SELECT>
</td>
</tr>
<tr>
<td align=right>Topic:</td>
<td><input type="TEXT" name="ManTopic"></td>
</tr>
<tr>
<td align=right><INPUT TYPE="submit" VALUE="Search">
<td align=left><input type="reset" value="Reset">
</td>
</tr>
</table>
</FORM>
EOF
exitIt();
}
sub findIt {
my($topic) = ($_[0]);
my($cmd, $mixedCase, $navigation, $zcat);
$mixedCase = '';
foreach (split(/\s*/, $topic)) {
$mixedCase .= "[" . $_ . uc($_) . "]";
}
$cmd = 'find ' . $ManPathFind . ' \( -type f -o -type l \) -name '
. $mixedCase .'\* | sort -t. +1';
open(FINDIN, "$cmd |") || die "can't open pipe \"$cmd |\": $!";
$navigation = 0;
while (<FINDIN>) {
if ($navigation == 0) {
print "<UL>\n";
$navigation = 1;
}
$_ =~ s/[\n\r]*$//;
print "<li><A HREF=\"$hmanpl?DirectPath=$_\">$_</a>\n";
}
close(FINDIN);
print "</UL><hr>\n" if ($navigation == 1);
}
sub hmanDirect {
my($path) = ($_[0], $_[1]);
my($cmd, $dir, $topic, $section);
($dir = $path) =~ s/\.(gz|z|Z)$//;
($topic = $dir) =~ s,^.*/,,;
$dir =~ s,/[^/]*/[^/]*$,,;
# $dir =~ s,/[^/]*$,,;
($section=$topic)=~s/^.*\.([^\.]*)$/$1/;
$topic =~ s/\.[^\.]*$//;
findIt($topic);
$cmd = "cd $dir; (gzip -dc < $path 2>/dev/null || cat < $path) | $hman -r '"
. $hmanpl . '?ManTopic=%s&ManSection=%s' ."' -l 'Man page for $topic($section)'";
system($cmd) || warn "can't run command \"$cmd\": $!";
print $HtmlTail;
exit 0;
}
sub exitIt {
print $HtmlTail;
exit 0;
}
sub badness {
my($text) = ($_[0]);
print "<strong>Request failed:</strong> $text, Try again<hr>\n";
initialForm();
}
sub manKey {
my($topic) = ($_[0]);
open(TMPHTML,"$ManPrg -k $topic | sort -u |")
|| die "can't open pipe \"$ManPrg -k $topic | sort -u |\": $!\n";
print "<title>Keyword search results for \"$topic\"</title>\n";
print "<h1>Keyword search results for \"$topic\"</h1><hr>\n";
while(<TMPHTML>) {
s/\( \)//g;
next if (! /^([^(]+)\s*\(([^)]+)[^-]+-\s(.*)[\n\r]*$/);
@topics=split(/, /,$1);
next if ($2 eq "");
print "<h2>$3:</h2>\n";
print "<UL>\n";
for $topic (@topics) {
print "<li><A HREF=\"$hmanpl?ManSection=$2&ManTopic=$topic\">$topic</a>($2)\n";
}
print "</UL>\n";
}
close(TMPHTML);
exitIt();
}