#!/Utils/bin/perl
# $Id: showhtml,v 1.8 1995/08/22 14:02:35 belinfan Exp $
# a script to handle text/html documents:
# find the Mosaic or Netscape process (or start a new one),
# and use the corresponding remote-execution feature to have
# it display the document.
# Author: Axel.Belinfante@cs.utwente.nl
#print stderr "testing....\n";
#foreach (keys %ENV) {
# print stderr $_.' : '.$ENV{$_}."\n";
#}
#exit;
$mcmd_prefix = '/tmp/Mosaic.';
$home = $ENV{'HOME'};
$symlinkfile = '/tmp/showhtml'.$$.'.html';
$default_viewer = 'mosaic';
$mpidfile = $home . '/' . '.mosaicpid';
$mprefix = $home . '/' . '.mosaic';
$npidfile = $home . '/' . '.netscapepid';
$nprefix = $home . '/' . '.netscape';
$ps_opt = '-a';
(@uname) = split(' ', `uname -a`);
if ($uname[0] =~ /sunos/i && $uname[2] =~ /^4/) {
$ps_opt = 'x';
}
($file_url, @flags) = @ARGV;
&main($file_url, @flags);
sub main {
local($file_url, @flags) = @_;
local($viewer) = &find_a_viewer(@flags);
if (grep(/^-html$/, @flags)) {
symlink($file_url, $symlinkfile);
&do_view($symlinkfile, '', $viewer);
sleep 10;
unlink($symlinkfile);
} elsif (grep(/^-url$/, @flags) || $file_url =~ /^http:/) {
&do_view('', $file_url, $viewer);
} else {
&do_view($file_url, '', $viewer);
}
}
sub find_a_viewer {
local(@flags) = @_;
local($mpid);
local($npid);
local($f);
if (! $ENV{'DISPLAY'}) {
$viewer = 'lynx';
} else {
$mpid = &find_viewer('', '', $mpidfile, '', 'Mosaic', '');
$npid = &find_viewer('', '', $npidfile, '', 'netscape', '');
if ($mpid || grep(/^-mosaic$/i, @flags)) {
$viewer = 'mosaic';
} elsif ($npid || grep(/^-netscape$/i, @flags)) {
$viewer = 'netscape';
} else {
foreach $f (<$mprefix-*>, <$nprefix-*>) {
$age{$f} = -M $f;
}
$last = (sort byage (keys %age))[0];
print STDERR "Last updated file: $last\n" if $debug;
if ($last =~ /^$mprefix\-/) {
$viewer = 'mosaic';
} elsif ($last =~ /^$nprefix\-/) {
$viewer = 'netscape';
} else {
$viewer = $default_viewer;
}
}
}
}
sub do_view {
local($out_file, $location, $viewer) = @_;
$viewer =~ /mosaic/i
&& &do_view_mosaic($out_file, $location);
$viewer =~ /netscape/i
&& &do_view_netscape($out_file, $location);
$viewer =~ /lynx/i
&& &do_view_lynx($out_file, $location);
}
sub do_view_mosaic {
local($out_file, $location) = @_;
local($mpid) = &find_viewer($out_file, $location, $mpidfile, 'start_mosaic', 'Mosaic', 'Mosaic');
&do_remote_mosaic($out_file, $location, $mpid) if $mpid > 0;
}
sub do_view_netscape {
local($out_file, $location) = @_;
local($wid) = &find_viewer($out_file, $location, $npidfile, 'start_netscape', 'netscape', 'Netscape');
&do_remote_netscape($out_file, $location, $wid) if $wid > 0;
}
sub do_view_lynx {
local($out_file, $location) = @_;
if ($location) {
print STDERR "Location: $location\n" if $debug;
system("lynx '$location'");
} else {
system("lynx 'file://localhost$out_file'");
}
}
sub start_mosaic {
local($out_file, $location, $mpidfile) = @_;
local($timer) = 10;
local($url) = $out_file || $location;
system("rm -f $mpidfile") if -r $mpidfile;
print STDERR "starting: 'Mosaic $url &'\n";
system("Mosaic $url &");
sleep 10;
while (! -r $mpidfile && $timer) {
sleep 1;
$timer--;
}
}
sub do_remote_mosaic {
local($out_file, $location, $mpid) = @_;
open(MCMD, '>'.$mcmd_prefix.$mpid) || die "$c: cannot open mcmd file $mcmd_prefix.$mpid: $!";
print MCMD "goto\n";
if ($location) {
print STDERR "Location: $location\n" if $debug;
print MCMD $location."\n";
} else {
print MCMD 'file://localhost'.$out_file."\n";
}
close(MCMD);
kill 'USR1', $mpid;
}
sub do_remote_netscape {
local($out_file, $location, $wid) = @_;
local($windowopt);
if ($location) {
print STDERR "Location: $location\n" if $debug;
system("netscape $windowopt -remote 'openURL($location)'");
} else {
system("netscape $windowopt -remote 'openURL(file://localhost$out_file)'");
}
}
sub start_netscape {
local($out_file, $location, $npidfile) = @_;
local($timer) = 10;
local($url) = $out_file || $location;
system("rm -f $npidfile") if -r $npidfile;
print STDERR "starting: 'netscape $url &'\n";
system("netscape $url &");
sleep 10;
# while (! -r $npidfile && $timer) {
# sleep 1;
# $timer--;
# }
}
##############################################
sub find_viewer {
local($out_file, $location, $pidfile, $starter, $program, $name) = @_;
local($pid);
$pid = &find_check_pid($pidfile) if -r $pidfile;
$pid = &read_psx($program) if ! $pid;
if (! $pid && $starter) {
&$starter($out_file, $location, $pidfile);
&flush_ps_cache;
$pid = - &find_check_pid($pidfile);
$pid = - &read_psx($program) if ! $pid;
}
print STDERR "Could not find pid nor start $name\n"
unless $pid || !$starter;
$pid;
}
sub ps_cache {
@cached_ps = split("\n", `ps $ps_opt`) unless @cached_ps;
return @cached_ps;
}
sub flush_ps_cache {
@cached_ps = ();
}
sub read_psx {
local($progname) = @_;
local($foundpid, $rest, @mpids);
local(@pidinfo) = grep(/$progname/, &ps_cache);
foreach (@pidinfo) {
print STDERR "pid output: ".$_."\n" if $debug;
($foundpid, $rest) = split(' ', $_, 2);
push(@mpids, $foundpid);
}
@mpids = sort(@mpids);
shift @mpids;
}
sub find_check_pid {
local($pidfile) = @_;
local($pid);
if (-r $pidfile) {
$pid = &read_pidfile($pidfile);
$pid = &read_ps_pid($pid);
system("rm -f $pidfile") if ! $pid;
}
$pid;
}
sub read_pidfile {
local($pidfile) = @_;
local($pid);
open (PID, $pidfile) || die "Cannot open pid file $pidfile: $!";
$pid = ;
chop($pid);
close(PID);
$pid;
}
sub read_ps_pid {
local($pid) = @_;
local($ok, $foundpid, $rest);
local(@pidinfo) = grep(/^$pid/, &ps_cache);
$ok = 0;
foreach (@pidinfo) {
($foundpid, $rest) = split(' ', $_, 2);
$ok = 1 if $foundpid == $pid;
}
$ok ? $pid : '';
}
sub byage { $age{$a} <=> $age{$b}; }