#!/afs/athena/contrib/perl/p

$doc = &wwwfetch($ARGV[0]);
$doc =~ s/<a [^>]*>([^<]+)<\/a>/\[4m\1\[0m/gi;
$doc =~ s/\<[^\>]*>//g;
print $doc;

sub wwwfetch {
    local($URL) = @_;
    $URL =~ /http:\/\/([^\/]+)\/(.*)/;
    $hostport = $1; $path = $2;

    ($them, $port) = split(/:/, $hostport);
    $port = 80 unless $port;
    print("Connecting to $them on port $port to get $path\n");
    $AF_INET = 2;
    $SOCK_STREAM = 1;
    
    $sockaddr = 'S n a4 x8';
    
    chop($hostname = `hostname`);
    
    ($name,$aliases,$proto) = getprotobyname('tcp');
    ($name,$aliases,$port) = getservbyname($port,'tcp')
        unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) =
        gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
    
    $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);
    # Make the socket filehandle.
 
    if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { 
    }
    else {
	return(0);
    }
    
# Give the socket an address.
 
    if (bind(S, $this)) {
    }
    else {
	return(0);
    }
    
    select(S); $|=1;
    select(STDOUT);
    if (connect(S,$that)) {
	print(S "GET /$path perlWWW\n\n\n");
	while(<S>){
	    $resp .= $_;
	}
	close(S);
	$resp;
    }
    else{
	return(0);
    }
}
