2000-02-29 15:50:56 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# xcat: an XMLterm wrapper for the UNIX "cat" command
|
2000-03-15 14:44:32 +00:00
|
|
|
# Usage: xcat [-h|--height]
|
2000-02-29 15:50:56 +00:00
|
|
|
|
|
|
|
use Cwd;
|
|
|
|
use Getopt::Long;
|
|
|
|
|
|
|
|
Getopt::Long::config('bundling');
|
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
if (!@ARGV) {
|
|
|
|
print STDERR "Usage: xcat [--height <pixels>] <file1> <URL2> ...\n";
|
2000-02-29 15:50:56 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
&GetOptions("height|h=i");
|
|
|
|
|
|
|
|
my $height = 240;
|
|
|
|
$height = $opt_height if $opt_height;
|
|
|
|
|
2000-02-29 15:50:56 +00:00
|
|
|
my $cookie = $ENV{LTERM_COOKIE}; # XMLTerm cookie
|
|
|
|
|
2001-12-28 22:39:45 +00:00
|
|
|
if (!$cookie) {
|
|
|
|
print 'Please use the "eval `xenv`" command to set-up the remote XMLterm environment'."\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2000-02-29 15:50:56 +00:00
|
|
|
my $dir = cwd();
|
|
|
|
|
|
|
|
foreach my $url (@ARGV) { # for each argument
|
2000-03-15 14:44:32 +00:00
|
|
|
my ($scheme, $username, $host, $port, $pathname);
|
2000-02-29 15:50:56 +00:00
|
|
|
|
2000-03-06 15:16:48 +00:00
|
|
|
# Check if argument is a valid URL
|
2000-02-29 15:50:56 +00:00
|
|
|
if ( $url =~ m%\b # initiator
|
2000-03-15 14:44:32 +00:00
|
|
|
([a-zA-Z]\w*)?: # scheme
|
|
|
|
(?=/) # lookahead
|
|
|
|
(?:// # slashpair
|
|
|
|
(?:([\w.]+)@)? # username
|
|
|
|
([\w\-]+(?:\.[\w\-]+)*)? # host
|
|
|
|
(?::(\d+))? # port
|
|
|
|
)?
|
|
|
|
(/|/[^/\s]\S*?)? # pathname
|
2000-02-29 15:50:56 +00:00
|
|
|
(?=>|\"|\'|\s|[.,!](?:\s|\Z)|\Z) # terminator (look ahead)
|
2000-03-15 14:44:32 +00:00
|
|
|
%x ) {
|
|
|
|
($scheme, $username, $host, $port, $pathname) = ($1, $2, $3, $4, $5);
|
2000-02-29 15:50:56 +00:00
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
## print STDERR "URL: scheme=$scheme, username=$username, host=$host, port=$port, pathname=$pathname\n";
|
2000-02-29 15:50:56 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
# Not an URL; assume it is a local file
|
|
|
|
|
|
|
|
# Prepend current working directory, if need be, to make full pathname
|
|
|
|
$url = $dir . "/" . $url if ($url and !($url =~ m%\A/%));
|
|
|
|
|
|
|
|
## print STDERR "Not an URL; assume local file $url\n";
|
|
|
|
|
2000-03-06 15:16:48 +00:00
|
|
|
# Create a file URL
|
2000-03-15 14:44:32 +00:00
|
|
|
($scheme, $username, $host, $port, $pathname) =
|
|
|
|
("file", "", "", "", $url);
|
2000-02-29 15:50:56 +00:00
|
|
|
}
|
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
if (($scheme ne "http") && ($scheme ne "file")) {
|
|
|
|
print STDERR "xcat: Cannot handle URI scheme $scheme:\n";
|
2000-02-29 15:50:56 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
if ($scheme eq "file") { # Local filename
|
2000-02-29 15:50:56 +00:00
|
|
|
|
|
|
|
if (!(-e $pathname)) {
|
|
|
|
print STDERR "xcat: File $pathname not found\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(-r $pathname)) {
|
|
|
|
print STDERR "xcat: Unable to read file $pathname\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-d $pathname) {
|
|
|
|
print STDERR "xcat: Use the 'xls' command to list contents of directory $pathname\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pathname =~ m%\A(.*?) (\.[^/.]*)?\Z%x # Deconstruct pathname
|
|
|
|
or die "xcat: Internal error; unable to deconstruct pathname\n";
|
|
|
|
|
|
|
|
($filename, $extension) = ($1, $2);
|
|
|
|
|
|
|
|
## print STDERR "Filename=$filename, extension=$extension\n";
|
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
if (($extension eq ".gif") ||
|
|
|
|
($extension eq ".png") ||
|
|
|
|
($extension eq ".jpg")) {
|
2000-02-29 15:50:56 +00:00
|
|
|
## print STDERR "Image file\n";
|
|
|
|
|
2000-04-21 13:10:44 +00:00
|
|
|
print "\e{S$cookie\n"; # HTML stream escape sequence
|
2000-03-15 14:44:32 +00:00
|
|
|
print "<IMG SRC='$scheme://${host}$pathname'>";
|
2000-02-29 15:50:56 +00:00
|
|
|
print "\000"; # Terminate HTML stream
|
|
|
|
|
2000-03-21 14:26:59 +00:00
|
|
|
} elsif (($scheme eq "file") && ($extension eq ".ps")) {
|
|
|
|
## print STDERR "PostScript local file\n";
|
|
|
|
|
|
|
|
system("ghostview $pathname&");
|
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
} elsif (($scheme eq "file") && ($extension eq ".url")) {
|
2000-02-29 15:50:56 +00:00
|
|
|
# URL
|
|
|
|
open INFILE, $pathname or next;
|
|
|
|
$_ = <INFILE>;
|
|
|
|
close INFILE;
|
|
|
|
|
|
|
|
my @urlvals;
|
|
|
|
my $nurl = 0;
|
|
|
|
while ( m%\b # initiator
|
2000-03-15 14:44:32 +00:00
|
|
|
(http|file|mailto): # scheme
|
|
|
|
(?=/) # lookahead
|
|
|
|
(?:// # slashpair
|
|
|
|
(?:([\w.]+)@)? # username
|
|
|
|
([\w\-]+(?:\.[\w\-]+)*)? # host
|
|
|
|
(?::(\d+))? # port
|
|
|
|
)?
|
|
|
|
(/|/[^/\s]\S*?)? # pathname
|
2001-12-28 22:39:45 +00:00
|
|
|
(?=>|\)|\"|\'|\s|[.,!](?:\s|\Z)|\Z) # URL terminator (look ahead)
|
2000-02-29 15:50:56 +00:00
|
|
|
%x ) {
|
|
|
|
$urlvals[$nurl] = $&;
|
|
|
|
s%$&%%;
|
|
|
|
$nurl++;
|
|
|
|
}
|
|
|
|
s%\A\s*(\S.*?)?\s*\Z%$1%;
|
|
|
|
|
|
|
|
if ($nurl >= 1) {
|
|
|
|
my $urldesc = $_;
|
|
|
|
my $urlstr = $urlvals[0];
|
|
|
|
$urldesc = $urlstr if !$urldesc;
|
|
|
|
|
|
|
|
my $clickcmd =
|
2000-03-06 15:16:48 +00:00
|
|
|
qq%onClick="return HandleEvent(event,'click','textlink',-\#,'$urlstr')"%;
|
2000-02-29 15:50:56 +00:00
|
|
|
|
2000-04-21 13:10:44 +00:00
|
|
|
print "\e{S$cookie\n"; # HTML stream escape sequence
|
2000-02-29 15:50:56 +00:00
|
|
|
if ($nurl >= 2) {
|
2001-12-28 22:39:45 +00:00
|
|
|
print "<a target='_new' href='$urlstr'><img src='$urlvals[1]'></a><br>";
|
2000-02-29 15:50:56 +00:00
|
|
|
}
|
2001-12-28 22:39:45 +00:00
|
|
|
print "<a target='_new' href='$urlstr'>$urldesc</a>";
|
2000-02-29 15:50:56 +00:00
|
|
|
print "\000"; # Terminate HTML stream
|
|
|
|
}
|
|
|
|
|
2000-03-15 14:44:32 +00:00
|
|
|
} elsif ( ($scheme eq "http") ||
|
|
|
|
(($scheme eq "file") && (($extension eq ".htm") ||
|
2000-11-27 22:15:50 +00:00
|
|
|
($extension eq ".html") ||
|
|
|
|
($extension eq ".xml")) ) ) {
|
2000-03-15 14:44:32 +00:00
|
|
|
## print STDERR "IFRAME\n";
|
2000-04-21 13:10:44 +00:00
|
|
|
print "\e{S$cookie\n"; # HTML stream escape sequence
|
2000-03-15 14:44:32 +00:00
|
|
|
print "<iframe frameborder=0 width='100%' height='$height' src='$scheme://${host}$pathname'></iframe>";
|
|
|
|
print "<div class='beginner'>";
|
|
|
|
print "Use shift-key + Home/End/PageUp/PageDown to scroll nested IFrame.";
|
|
|
|
print "</div>";
|
|
|
|
print "\000"; # Terminate HTML stream
|
|
|
|
|
2000-02-29 15:50:56 +00:00
|
|
|
} elsif ((-T $pathname) || ($extension eq "txt")) { # plain text file
|
|
|
|
## print STDERR "Text file\n";
|
|
|
|
|
|
|
|
open INFILE, $pathname or next;
|
2000-04-21 13:10:44 +00:00
|
|
|
print "\e{S$cookie\n"; # HTML stream escape sequence
|
2000-02-29 15:50:56 +00:00
|
|
|
print "<pre>";
|
|
|
|
|
|
|
|
while (<INFILE>) {
|
2000-03-06 15:16:48 +00:00
|
|
|
s/&/&/g; # Replace & with &
|
|
|
|
s/</</g; # Replace < with <
|
|
|
|
s/>/">"/g; # Temporarily replace > with ">"
|
|
|
|
# to allow termination of <http://xyz.com> etc.
|
|
|
|
|
|
|
|
s%\b # URL word boundary
|
2000-03-15 14:44:32 +00:00
|
|
|
([a-zA-Z]\w*)?: # scheme
|
|
|
|
(?=/) # lookahead
|
|
|
|
(?:// # slashpair
|
|
|
|
(?:([\w.]+)@)? # username
|
|
|
|
([\w\-]+(?:\.[\w\-]+)*)? # host
|
|
|
|
(?::(\d+))? # port
|
|
|
|
)?
|
|
|
|
(/|/[^/\s]\S*?)? # pathname
|
2001-12-28 22:39:45 +00:00
|
|
|
(?=>|\)|\"|\'|\s|[.,!](?:\s|\Z)|\Z) # URL terminator (look ahead)
|
|
|
|
%<a target='_new' href='$&'>$&</a>%xg;
|
2000-03-06 15:16:48 +00:00
|
|
|
|
|
|
|
s/">"/>/g; # Replace ">" with > in the end
|
2000-02-29 15:50:56 +00:00
|
|
|
|
|
|
|
print $_;
|
|
|
|
}
|
|
|
|
|
|
|
|
print "</pre>";
|
2000-03-06 15:16:48 +00:00
|
|
|
print "\000"; # Terminate HTML stream
|
2000-02-29 15:50:56 +00:00
|
|
|
close INFILE;
|
|
|
|
|
|
|
|
} else { # unknown file type
|
|
|
|
print STDERR "xcat: File type unknown for $pathname\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|