#!/usr/bin/perl

BEGIN {
    use CGI::Carp qw(fatalsToBrowser set_message);
    set_message("Congratulations! You found a bug in my program.");
}

# Security measures. See man 1 perlsec
$ENV{PATH} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

use strict;
use CGI;

my ($name) = CGI::param("name");
my ($mark) = "###man.cgi###";
my ($i, @s, @deco, @text, $a, $b, $c);

print <<eof;
Content-type: text/html

<html>
<body>
<pre>
eof

open F, "man $name |" or die;
while (<F>) {
    while (s/((.\x8.)+)/$mark/) {
	@s = split //, $1;
	$#deco = $#text = $#s/3;
	for ($i=0; $i<=$#deco; ++$i) {
	    $deco[$i] = $s[$i*3];
	    $text[$i] = $s[$i*3+2];
	}
	$a = join "", @deco;
	$b = join "", @text;
	$c = ($a eq $b) ? "b" : "u";
	s!$mark!<$c>$b</$c>!;
    }
    print;
}
close F;

print <<eof;
</pre>
</body>
</html>
eof

