#!/usr/bin/perl ############################################################################### ## Custom Configuration ####################################################### # It's unlikely that any of these should be changed, and if they are, # update the man page as well. # default browser and cleanup rules $use_ns = ($ENV{'WB_BROWSER'} =~ /mosaic/i) ? 0 : 1; $use_sp = ($ENV{'WB_SLEEP'} =~ /at/i) ? 0 : 1; # default browser command and directories for temporary files $netscape = $ENV{'WB_NETSCAPE'} || 'netscape'; $nstmp = $ENV{'WB_NSTMP'} || $ENV{'WB_TMP'} || ((-d "$ENV{'HOME'}/.mozilla") ? "$ENV{'HOME'}/.mozilla" : (-d "$ENV{'HOME'}/.netscape") ? "$ENV{'HOME'}/.netscape" : (-d "$ENV{'HOME'}/tmp") ? "$ENV{'HOME'}/tmp" : '/tmp'); $mstmp = $ENV{'WB_MSTMP'} || $ENV{'WB_TMP'} || ((-d "$ENV{'HOME'}/tmp") ? "$ENV{'HOME'}/tmp" : '/tmp'); # default umask, tmp file hold time, browser window and pwd command $umask = (defined $ENV{'WB_UMASK'}) ? $ENV{'WB_UMASK'} : 077; $keep = (defined $ENV{'WB_KEEP'}) ? $ENV{'WB_KEEP'} : 10; $id = $ENV{'WB_ID'}; $pwd = $ENV{'WB_PWD'} || 'pwd'; ############################################################################### ## Usage ###################################################################### # webrowse -- browse the given input in a local web browser, with markup # Steve Kinzler, kinzler@cs.indiana.edu, May 96/Jan 98 # see website http://www.cs.indiana.edu/~kinzler/webrowse/ # http://www.cs.indiana.edu/~kinzler/home.html#web $version = '1.6.3'; ($Nnote, $Mnote) = ($use_ns) ? (' (default)', '') : ('', ' (default)'); ($Pnote, $Anote) = ($use_sp) ? (' (default)', '') : ('', ' (default)'); $usage = "usage: $0 [ -N | -M ] [ -s | -mq | -u | -c ] [ -k MINUTES ] [ -p | -a ] [ -i ID ] [ -w | -t ] [ -rnvo ] [ -h ] [ FILE ] -N use a Netscape or Mozilla (NS/Moz) browser$Nnote -M use a Mosaic browser$Mnote -s tag the standard input as HTML (default TEXT) -m link markup a TEXT copy of the input for browsing (heuristic) -q quote markup a TEXT copy of the input for browsing -u take the argument or stdin as a URL to browse -c take the argument or stdin as a remote command (NS/Moz only) -k minutes to keep any temporary input (default $keep) -p use a sleeping process to delete temporary input$Pnote -a use an at job to delete temporary input$Anote -i id of the window or process of the browser to use -w open a new browser window, default use first existing window -t open a new browser tab (newer NS/Moz only) -r don't raise the browser window (NS/Moz only) -n don't actually use the browser at all -v print the browser command to be run (NS/Moz only) -o print the processed input to stdout -h just print this help message The NS/Moz command that will be used is `$netscape`. MINUTES is specified with an integer number or the word 'forever'. Temporary copies of the input (from standard input and/or for markup) are stored in $nstmp (with NS/Moz) or $mstmp (with Mosaic). Version $version\n"; # parse and check command line options require 'getopts.pl'; &Getopts('NMsmquck:pai:wtrnvoh') || die $usage; die "Invalid minutes: $opt_k\n", $usage unless $opt_k =~ /(^\d*$|forever)/i; die $usage if $opt_h || $opt_u && $opt_c; # let command line options override environmental defaults $use_ns = ($opt_N) ? 1 : ($opt_M) ? 0 : $use_ns; $use_sp = ($opt_a) ? 0 : ($opt_p) ? 1 : $use_sp; $keep = ($opt_k ne '') ? $opt_k : $keep; $id = ($opt_i ne '') ? $opt_i : $id; $cwd = ''; ############################################################################### ## Execution ################################################################## # browse URLs, commands and files without markup straight-away and exit $url = (@ARGV) ? $ARGV[0] : <> . '', $ENV{'WB_RUNURL'} && system("$ENV{'WB_RUNURL'} '$url' &"), &browse($url, 'url'), exit if $opt_u; &browse((@ARGV) ? $ARGV[0] : <> . '', 'cmd'), exit if $opt_c; &browse(&abspath($ARGV[0]), 'exec'), exit if @ARGV && ! ($opt_m || $opt_q); # find a temporary filename to use $tmpdir = ($use_ns) ? $nstmp : $mstmp; $ext = ($opt_s || $opt_m || $opt_q) ? '.html' : '.txt'; $aa = ''; do { $aa = ($aa) ? ++$aa : 'AA'; $tmp = "$tmpdir/webrowse${$}$aa$ext"; } while -e $tmp; # open the temporary file, trapping signals for cleanup umask $umask; $SIG{'HUP'} = $SIG{'INT'} = $SIG{'PIPE'} = $SIG{'TERM'} = 'end'; open(TMP, "> $tmp") || do { $keep = 'forever'; &end("$0: cannot open $tmp ($!)\n"); }; # write the temporary file, marking up the text if appropriate if ($opt_m || $opt_q) { ($prog = $0) =~ s:.*/::; $title = &htmlencode("$prog: " . ((@ARGV) ? "@ARGV" : '-')); $base = (@ARGV) ? &abspath($ARGV[0]) : &abspath('.'); print TMP "
\n"; print TMP &markup(&mailquote(&htmlencode(<>))) if $opt_m && $opt_q; print TMP &markup(&htmlencode(<>)) if $opt_m; print TMP &mailquote(&htmlencode(<>)) if $opt_q; print TMP "\n\n\n"; } else { print TMP <>; } # close and browse and cleanup the temporary file and exit close TMP; &browse(&abspath($tmp)); &end(); ############################################################################### ## Subroutines ################################################################ # Print any given warning messages and schedule any temporary file for # cleanup via the chosen method (sleeping background shell process, at(1) # job, or no cleanup) then exit. sub end { warn @_ if @_; exit if $keep =~ /forever/i || ! -e $tmp; unlink($tmp) || warn("$0: cannot unlink $tmp ($!)\n"), exit if ! $keep; if ($use_sp) { system('(sleep ' . $keep * 60 . '; /bin/rm -f ' . &shencode($tmp) . ') &'); } else { open(AT, "| at now + $keep minutes"); print AT '/bin/rm -f ', &shencode($tmp), "\n"; close AT; warn("$0: cannot schedule rm at now + $keep minutes ($!)\n"), unlink($tmp) || warn("$0: cannot unlink $tmp ($!)\n") if $?; } exit; } # Return the absolute filesystem pathname for the given filename. sub abspath { local($_) = @_; s:/+:/:g; return $_ if /^\//; chop($cwd = `$pwd`) unless $cwd; chop($cwd = `/bin/pwd`) unless $cwd; (($cwd =~ /^\/+$/) ? '' : $cwd) . "/$_"; } ############################################################################### ## Subroutines ## Browser Remote Control ###################################### # Direct the browser to load a given target or run a given command, # and exit if requested. sub browse { local($file, $arg) = @_; chop($file) if $file =~ /\n$/; local($url, $flush, @args) = ($file, $|, ()); $url =~ s/^\"(.*)\"$/$1/, $url =~ s/^\'(.*)\'$/$1/, $url =~ s/^\((.*)\)$/$1/, $url =~ s/^\<(.*)\>$/$1/, $url =~ s/^\[(.*)\]$/$1/, $url =~ s/^\{(.*)\}$/$1/ if $arg eq 'url'; $url = "file:$url" if $arg ne 'url' && $arg ne 'cmd'; # construct the netscape/mozilla remote control command to run, # and print it if requested if ($use_ns) { $url =~ s/\(/%28/g; $url =~ s/\)/%29/g; $url =~ s/,/%2C/g; # Mozilla 1.7b seems to need this: $url .= '/' if $url =~ /^https?:\/\/[-\w.]+(:\d*)?$/i; $url .= ($opt_w) ? ', new-window' : ($opt_t) ? ', new-tab' : '' if $arg ne 'cmd'; push(@args, $netscape); push(@args, '-id', $id) if $id; push(@args, (($opt_r || $opt_w) ? '-noraise' : '-raise', '-remote', ($arg eq 'cmd') ? $url : "openURL($url)")); $| = 1, print("@args\n"), $| = $flush if $opt_v; } # Note: newer mozilla software includes mozila-xremote-client # for a similar alternative interface that allows one to specify # the browser application. See www.mozilla.org/unix/remote.html # if requested, # print the input and/or exit without any browser interaction system(($arg eq 'url' || $arg eq 'cmd') ? 'echo' : '/bin/cat', $file) if $opt_o; ($arg) ? exit : return if $opt_n; # remote control the browser # with NS/Moz, by running or execing the remote control command # with Mosaic, by writing a standard temporary command file and # signaling the pid of the browser process, read from ~/.mosaicpid if ($use_ns) { ($arg) ? exec @args : system @args; } else { die "$0: remote commands only available with Netscape" . " or Mozilla\n" if $arg eq 'cmd'; unless ($id) { $pid = "$ENV{'HOME'}/.mosaicpid"; $id =