man.cgi


#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

$ENV{PATH} = "/bin:/usr/bin";
print header;
print start_html("Man pages");

# The only allowed topics will be words that are alphanumeric 
# (a-zA-Z0-9). Dashes and periods are also allowed; spaces 
# are not.
my $topic;
if (param('topic') =~ /^([\w\-\.]+)$/) {
    $topic = $1;
} else {
    &dienice("Bad topic: " . param('topic'));
}

my @out = `/usr/bin/man $topic`;
if ($#out < 0) {
   &dienice("No man page for `$topic'.");
}

print "<h2>$topic</h2>\n";
print "<pre>\n";
foreach $i (@out) {
# man pages are formatted with nroff, so we have to remove the 
# nroff control characters from them with this substitution:
    $i =~ s/.\cH//g;
    
    # now print the line
    print $i;
}
print "</pre>\n";
print end_html;

sub dienice {
    my($errmsg) = @_;
    print "<h2>Error</h2>\n";
    print "$errmsg<p>\n";
    print end_html;
    exit;
}