#!/usr/pkg/bin/perl -w
use strict;

foreach (@ARGV) {
open TXT, '<', $_ or print STDERR "Error: couldn't open $_ for reading: $!\n" and next;
s/\.txt$/.html/ or $_ .= '.html';
open HTML, '>', $_ or print STDERR "Error: couldn't open $_ for writing: $!\n" and next;
select HTML;
print '<pre>';
while (<TXT>) {
 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
 pos() = 0; # just making sure
 # The following regular expressions were taken from Jef Poskanzer's
 # texttohtml program (<http://www.acme.com/software/texttohtml/>).
 for (;;) {
  m!\G((?:https?|ftp|gopher)://[^>)}\],\s]+)!gci && do {
   print "<a href=\"$1\">$1</a>"; next;
  };
  m!\G(www\.[[:alnum:].\-]+\.[[:alpha:]]{2,5}(?:/[^>)}\],\s]*)?)!gci && do {
   print "<a href=\"http://$1\">$1</a>"; next;
  };
  m!\G((mailto:)?([\w+.\-]+@[[:alnum:]+.\-]+))!gci && do {
   print "<a href=\"", $2 || 'mailto:', "$3\">$1</a>"; next;
  };
  m!\G(telnet:[[:alnum:]+.:@\-]+)!gci && do {
   print "<a href=\"$1\">$1</a>"; next;
  }
  m!\G<!gc && do {print '&lt;'; next; };
  m!\G>!gc && do {print '&gt;'; next; };
  m!\G&!gc && do {print '&amp;'; next; };
  m!\G([^[:ascii:]])!gc && do {print '&#', ord $1, ';'; next; };
  m!\G(.)!gc && do {print $1; next; };
  last;
 }
}
print '</pre>';
}