#!/usr/bin/perl $filename='/man.cgi'; @stylestack = (); $r = $ENV{PATH_INFO}; $ENV{TERM} = "dumb"; $ENV{PATH_INFO} =~ s/^\///g; $ENV{PATH_INFO} =~ s/\// /g; $ENV{QUERY_STRING} = $ENV{PATH_INFO} . $ENV{QUERY_STRING}; if ($ENV{QUERY_STRING} eq "") { $ENV{QUERY_STRING} = "man" } $ENV{QUERY_STRING} =~ s/%([0-9A-Fa-f]{2})/pack("c", hex($1))/ge; $ENV{QUERY_STRING} =~ s/\+/ /g; $ENV{QUERY_STRING} =~ s/^q=//g; ($c) = ($ENV{QUERY_STRING} =~ s/c=([01])//); ($s) = ($ENV{QUERY_STRING} =~ s/s=([01])//); ($f) = ($ENV{QUERY_STRING} =~ s/f=([01])//); ($t) = ($ENV{QUERY_STRING} =~ s/t=([01])//); if ($f eq 1) {$fchecked = 'checked="checked" '; $argappend .= "/f=1";} if ($t eq 1) {$tchecked = 'checked="checked" '; $argappend .= "/t=1";} ($QS) = ($ENV{QUERY_STRING} =~ /([-\:._A-Za-z0-9,! ]*)/); if($s eq "1") { print "Content-type: text/plain\r\n\r\n"; open(SOURCE, "<$filename"); { local $/; print ; } close(SOURCE); } if ($c eq 1) { print "Content-type: text/css\r\n\r\n"; print <Dump([\%ENV], [qw(ENV)]); #close(STDERR); use Digest::MD5 md5_hex; my $md5 = md5_hex("$QS:$f:$t"); my $md5_file = "/tmp/man.datacache/$md5"; if ( -f $md5_file ) { my $data = ''; open(FILE, "<", $md5_file); $title = ; chomp $title; { local $/; $data = ; } close(FILE); $man = $data; } else { $man = `sh -c 'yes | TERM=xterm MANPAGER=/bin/cat man $QS | cat'`; #print "Content-type: text/plain\r\n\r\nman: '$man' # #sh -c 'yes | TERM=xterm MANPAGER=/bin/cat man $QS 2> /dev/null | cat' #"; $man =~ s/&/&/g; $man =~ s/", $md5_file); if ($f ne 1) { if ( -f "/tmp/man.datadump" ) { my $data = ''; open(FILE, "; } close(FILE); eval $data; %mans = %$manstore; } else { @mans = qx[ ls -R -F -1 \$(man -W | tr ':' ' ') | perl -pe 'if (/^(\\/.*)\\:\$/){\$dir=\$1};print\$dir."/"' | grep -v '\\/\\/' | grep -v '\\/\$' | sed -e 's/[*\@]//g' ]; @mans = `apropos .`; for my $mans (@mans) { ($name, $section, $title) = ($mans =~ /^(.*?) +\((.*)\) - (.*$)/); if (defined $name) { if ($name =~ s/ \[(.*?)\]//) { push (@{$mans{'%alias%'}{$name}}, "$1" . '.' . "$section") if not grep("$1" . '.' . "$section", @{$mans{'%alias%'}{$name}}); $title = "$name - $title"; $name = $1; } else { $title = "$name - $title"; } my ($prev) = ($mans{$name}{$section} =~ /^(.+?) - /); if (defined $prev) { $mans{$name}{$section} = "$prev, $title"; } else { $mans{$name}{$section} = $title; } print STDERR "man.cgi: added to $name($section): $mans{$name}{$section}: \n$mans"; } else { print STDERR "man.cgi: didn't find anything in this line: \n$mans"; } } use Data::Dumper; my $data = Data::Dumper->Dump([\%mans], [qw(manstore)]); $data =~ s/^ +//gs; open(FILE, ">/tmp/man.datadump"); print FILE $data; close(FILE); } $man =~ s/([-\:\._A-Za-z0-9]+)/&replacetxt($1)/gse; $man =~ s/([-\:\._A-Za-z0-9]+)(- *\n +)([-\:\._A-Za-z0-9]+)/&replacetxt("$1$3","$1$2$3")/gse; $man =~ s/([a-z]+:\/[^>< \n]+)(- *\n +)([^>< \n]+[^>< \n.!?:;()])|([a-z]+:\/[^>< \n]+[^>< \n.!?:;()])/$4$1$2$3<\/a>/g; $man =~ s/(]*>)([^<]*)(- *\n +)([^<]*)(<\/a>)/$1$2$5$3$1$4$5/gs; $QStitle = $QS; $QStitle =~ s/([-\:._A-Za-z0-9]+)/&replacetxt($1)/gse; ($title) = ($QStitle =~ /title=\"([^<>"]*)\"/); $title .= " - " if $title ne ""; print FILEMD5 "$title\n"; } else { print FILEMD5 "\n"; } $man =~ s!!!g; $man =~ s!!!g; print FILEMD5 $man; close(FILEMD5); } my $status = "200 Manual Found"; if ($man eq "") { $man = " The requested manual: "${title}man ${QS}" was Not Found. Please retry with a different query or use the search features. "; $status = "404 Manual Not Found"; } if ($ENV{HTTP_USER_AGENT} =~ /MSIE/) { print "Content-type: text/html\r\nStatus: $status\r\n\r\n"; } else { print "Content-type: application/xhtml+xml\r\nStatus: $status\r\n\r\n"; } print < ${title}man $QS

Seth Woolley's Man Viewer

${title}man $QS

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

$man
@{[($status eq "200 Manual Found")?qq{

References for this manual (incoming links)

@{[&printrefs]}}:""]}
EOF ; sub replacetxt { my $toreplace = shift; my $bereplace = shift; if ($bereplace eq "") { $bereplace = $toreplace; } my $manpage = $toreplace; my @mana = (); if (defined $mans{'%alias%'}{$manpage}) { @manaliases = sort @{$mans{'%alias%'}{$manpage}}; for my $m (@manaliases) { my ($mtxt, $stxt) = ($m =~ /^(.+)\.([^.]+)$/); push(@mana, [$stxt, $mtxt]); } } if (defined $mans{$manpage}) { @mans = sort keys %{$mans{$manpage}}; if ($#mans eq 0) { my $i = $mans[0]; { if ($t ne "1") { $mandesc = " - " . &mandesc($manpage, $i); } else { $mandesc = ""; } $toreplace = "$bereplace"; } } else { $toreplace = "$bereplace("; for my $i (@mans) { if ($t ne "1") { $mandesc = " - " . &mandesc($manpage, $i); } else { $mandesc = ""; } $toreplace .= "$i,"; } if ($#mana ne -1) { for my $iv (@mana) { @i = @{$iv}; if ($t ne "1") { $mandesc = " - " . &mandesc($i[1], $i[0]); } else { $mandesc = ""; } $toreplace .= "$i[0] $i[1],"; } } $toreplace =~ s/,$/)<\/span><\/span>/; } } else { if ($#mana ne -1) { if ($#mana eq 0) { my @i = @{$mana[0]}; { if ($t ne "1") { $mandesc = " - " . &mandesc($i[1], $i[0]); } else { $mandesc = ""; } $toreplace = "$bereplace"; } } else { $toreplace = "$bereplace("; for my $iv (@mana) { @i = @{$iv}; if ($t ne "1") { $mandesc = " - " . &mandesc($i[1], $i[0]); } else { $mandesc = ""; } $toreplace .= "$i[0] $i[1],"; } $toreplace =~ s/,$/)<\/span><\/span>/; } } else { $toreplace = $bereplace; } } return $toreplace; } sub pr { $toprint = shift; if ($toprint eq "") { if (grep { $_ eq "strong" } @stylestack) { $toprint = ""; } else { push(@stylestack, "strong"); } } if ($toprint eq "") { if (grep { $_ eq "u" } @stylestack) { $toprint = ""; } else { push(@stylestack, "u"); } } if ($toprint eq "") { if (grep { $_ eq "strong" } @stylestack) { my $front = ""; my $back = ""; my @sst = (); while (1) { my $ent = pop(@stylestack); if ($ent ne "strong") { $front .= ""; $back .= "<$ent>"; push(@sst, $ent); } else { last; } } push(@stylestack, @sst); $toprint = $front . $toprint . $back; } else { $toprint = ""; } } if ($toprint eq "") { if (grep { $_ eq "u" } @stylestack) { my $front = ""; my $back = ""; my @sst = (); while (1) { my $ent = pop(@stylestack); if ($ent ne "u") { $front .= ""; $back .= "<$ent>"; push(@sst, $ent); } else { last; } } push(@stylestack, @sst); $toprint = $front . $toprint . $back; } else { $toprint = ""; } } return $toprint; } sub escapecode { my $txt = shift; $txt =~ s/\e[[]1m/&pr("")/ge; $txt =~ s/\e[[]0m/&pr("<\/strong>") . &pr("<\/u>")/ge; $txt =~ s/\e[[]4m|\e[[][34][0-9]m/&pr("")/ge; $txt =~ s/\e[[]22m/&pr("<\/strong>")/ge; $txt =~ s/\e[[]24m/&pr("<\/u>")/ge; return $txt; } sub mandesc { my $manpage = shift; my $section = shift; if (defined $mans{$manpage}{$section}) { return $mans{$manpage}{$section}; } return "No Title Available"; } ### references addition code sub printrefs { my $refsfile = "referindex.tab"; # referrers file, relative to docroot open(SOURCE, "<", "$refsfile") or warn "$!"; local @refsdata = ; close(SOURCE); return grabrefs("$filename$r", \@refsdata) } sub grabrefs { my $search = shift; my @refsdata = @{shift()}; my $searchregex = $search; ($prefix) = ($searchregex =~ /^(.*)\Q$filename\E/); $searchregex =~ s/^.*?\Q$filename\E//; $searchregex = "$prefix$filename" . &uriescape($searchregex); #print "Content-type: text/plain\r\n\r\n$searchregex\n\n"; my @refsgood = grep {m!^\Q$searchregex\E\t!} @refsdata; my %hash = (); my $returnstring = ''; my ($uri, $ref, $tit); for (@refsgood) { ($uri, $ref, $tit) = (/^([^\t]*)\t([^\t]*)\t([^\t]*)\n$/); $hash{$uri}{$ref}++; $hashtit{$ref} = $tit; } for $uri (sort keys %hash) { $returnstring .= qq`
@{[&plaintext($uri)]}
\n`; my %refhash = (); for $ref (keys %{$hash{$uri}}) { $refhash{$hash{$uri}{$ref}} .= qq`
$hash{$uri}{$ref}:@{[&plaintext(&cuturl($ref,60))]}
@{[&plaintext($hashtit{$ref})]}
\n`; } for $ref (reverse sort keys %refhash) { $returnstring .= $refhash{$ref}; } } return "
\n$returnstring
"; } sub plaintext() { local $_ = shift; my $mval = shift; my $mstr = ' ' x $mval; s/\t/ /g; s/ +$//mg; s/&/&/g; s/"/"/g; # s/'/'/g; s//>/g; s/ /  /g; s/ /  /g; if ($mval) { s/\n/
\n/g; s/\n/\n$mstr /gs; return "\n$mstr $_\n$mstr"; } else { s/\n/ /gs; return $_; } } sub uriescape { local $_ = shift; s/([^0-9A-Za-z\:\/])/&es($1)/gse; return $_; sub es { my $b = ord(shift()); # return '+' if $b == 32; # apache can't do + inside urls, only in query_strings my $a = int($b/16); $b = $b - $a * 16; return '%' . &hx($a) . &hx($b); } sub hx { my $a = shift; $a = $a>9 ? chr($a+55) : $a; return $a; } } sub cuturl() { local $_ = shift; my $l = shift; s/ / /g; s/[ \t\n\l\r\f]+/ /gs; if(length($_)<$l) { return $_; } else { $_ = substr($_,0,$l+1); s/[\/?]?[^\/?]*$/.../; return $_; } } What manual page do you want? Content-type: application/xhtml+xml Status: 404 Manual Not Found man

Seth Woolley's Man Viewer

man

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title


       The requested manual: "man " was Not Found.

       Please retry with a different query or use the search features.