#!/bin/sh -- # A comment mentioning perl eval 'exec perl -S $0 ${1+"$@"}' if 0; $Program = &basename($0); ######################################################################### # Config: # check for faster loading "netscape_remote": system("sh -c 'type netscape_remote' >/dev/null 2>/dev/null"); if ( $? == 0 ) { $NETSCAPE_REMOTE = "netscape_remote"; } else { $NETSCAPE_REMOTE = "netscape"; } $GOTO_URL_CMD = "$NETSCAPE_REMOTE -noraise -remote 'openURL(%s,new-window)'"; $NS4_GOTO_URL_CMD = $GOTO_URL_CMD; $NS4_GOTO_URL_CMD =~ s/netscape /netscape4 /; $NS4_START_BROWSER_CMD = "netscape4 '%s'"; $NS7_GOTO_URL_CMD = "netscape7 -remote 'openURL(%s,new-window)'"; $NS7_START_BROWSER_CMD = "netscape7 '%s'"; $MOZ_GOTO_URL_CMD = "mozilla -remote 'openURL(%s,new-window)'"; $MOZ_START_BROWSER_CMD = "mozilla '%s'"; if ($ENV{BROWSER} ne '') { $BROWSER = $ENV{BROWSER}; } else { $BROWSER = "netscape4"; if (-f "$ENV{HOME}/.url_use_netscape4") { $BROWSER = "netscape4"; } elsif (-f "$ENV{HOME}/.url_use_netscape7") { $BROWSER = "netscape7"; } elsif (-f "$ENV{HOME}/.url_use_mozilla") { $BROWSER = "mozilla"; } } if ($ENV{URL_USE_NETSCAPE4}) { $BROWSER = "netscape4"; } elsif ($ENV{URL_USE_NETSCAPE7}) { $BROWSER = "netscape7"; } elsif ($ENV{URL_USE_MOZILLA}) { $BROWSER = "mozilla"; } if ($BROWSER eq 'netscape7') { unshift(@ARGV, "-N"); } elsif ($BROWSER eq 'mozilla') { unshift(@ARGV, "-M"); } $START_BROWSER_CMD = "$BROWSER '%s'"; $GOTO_JAVA_APPLET_CMD = "appletviewer '%s' 2>&1 &"; # The 2>&1 is in there to make sure the shell comes into play for '%s' and & # some url's reject GET and want POST $POST_REQUEST_CMD = "post_wrapper '%s'"; $GET_X_SELECTION_CMD = "selection.tcl"; # will check for existance of tkquery when needed. $ERROR_CMD = "tkquery -warp -no -b Dismiss::Dismiss_click"; if ( $Program =~ /^lurl/ || $BROWSER eq 'lynx' ) { $BROWSER = 'golynx'; $GOTO_URL_CMD = "$BROWSER '%s'"; $START_BROWSER_CMD = $GOTO_URL_CMD; } ######################################################################### $Usage = <<"END"; url version 0.3 Copyright (c) 1995-2001 by Karl J. Runge $Program: Script to make browser "$BROWSER" go to a URL on the command line, or the URL in the current X-selection (or cutbuffer). Usage: $Program [] Options: -D, -d Debug mode. Launch command not run. -v Verbose mode. commands are echoed to the screen. -h Prepend "http://" to -F Prepend "ftp://" to -g Prepend "gopher://" to -f Prepend "file:" to -H Assume input is HTML, not a URL. -j Assume input contains Java applet and run appletviewer on it $GOTO_JAVA_APPLET_CMD -M use mozilla -p Assume input is a post request (currently broken?) -n No new-window -help This help (or -h with no other args) Notes: If is not given, it uses the string in the current X-selection. If is "-" 1st line of stdin is used. Dependencies: Needed: To retrieve X selection: "$GET_X_SELECTION_CMD" A browser: "$BROWSER" A go to URL command: "$GOTO_URL_CMD" Not absolutely needed, but useful: To display error messages: "$ERROR_CMD" To launch POST requests: "$POST_REQUEST_CMD" Examples: $Program $Program http://www.lycos.com END $Debug = 0; $Verbose = 0; $Prefix = ""; $Special = ""; $Stdin = 0; if ( $ARGV[0] =~ /^(-h|-help)/ && $ARGV[1] eq '' ) { &help(); exit 0; } @args = (); @args = @ARGV; $dargs = ''; LOOP: while (@ARGV) { $_ = shift; CASE: { /^-j$/ && ($Special = '-j', last CASE); /^-p$/ && ($Special = '-p', last CASE); /^-H$/ && ($Special = '-H', last CASE); /^-M$/ && ($Special = '-M', last CASE); /^-N$/ && ($Special = '-N', last CASE); /^-N4$/ && ($Special = '-N4', last CASE); /^-n$/ && ($No_new_window = 1, last CASE); /^-h$/ && ($Prefix = 'http://', last CASE); /^-g$/ && ($Prefix = 'gopher://', last CASE); /^-f$/ && ($Prefix = 'file:', last CASE); /^-F$/ && ($Prefix = 'ftp://', last CASE); /^-$/ && ($Stdin = 1, last CASE); /^-d$/i && ($Debug = 1, $Verbose = 1, $dargs .= '-d ', $ERROR_CMD = 'echo', last CASE); /^-v$/ && ($Verbose = 1, $dargs .= '-v ', last CASE); /^--$/ && (last LOOP); # -- means end of switches /^-(-.*)$/ && (unshift(@ARGV, $1), last CASE); /^(-h|-help)$/ && ((&help), exit 0, last CASE); if ( /^-(..+)$/ ) { # split bundled switches: local($y, $x) = ($1, ''); foreach $x (reverse(split(//, $y))) { unshift(@ARGV,"-$x") }; last CASE; } /^-/ && ((print STDERR "Invalid arg: $_\n", &help()), exit 1, last CASE); unshift(@ARGV,$_); last LOOP; } } if ( $Stdin ) { while () { $utmp .= $_; } #$utmp = `cat`; } elsif ( $ARGV[0] eq '') { $utmp = `$GET_X_SELECTION_CMD 2>/dev/null`; } else { $utmp = join(' ', @ARGV); } $utmp = &trim($utmp); # trim leading and trailing spaces. $utmp1 = $utmp; @a = (); $n = scalar(@a = split(/\n/, $utmp)); if ( $n > 7 || $utmp =~ /<\s*HTML\s*>/i || $Special eq '-H' ) { $tmp = "/tmp/$Program.$$.html"; open(TMP, ">$tmp") || die "$!"; print TMP $utmp, "\n"; close(TMP); print "TIMEBOMB IN BG: system(\"sh -c '(sleep 60; rm -f $tmp)' &\")\n" if $Verbose; system("sh -c '(sleep 60; rm -f $tmp)' &"); system("$Program $dargs -f $tmp"); exit 0; } if ( $n == 2 || $n == 3 ) { # try to join accidentally split line: $utmp =~ s/\n//g; $utmp =~ s/\s//g; } # Now use first line no matter what: $utmp = &trim($utmp); # trim leading and trailing spaces again. $url = (split(/\n/, $utmp))[0]; # Clean up $url = &trim($url); # leading or trailing spaces. $url =~ s/^UR[LI]://i; # url:... construct $url = &trim($url); # leading or trailing spaces again. foreach my $i (1..2) { $url =~ s/^[<\("]*//; # leading < ( " $url =~ s/[>\)"]*$//; # trailing > ) " $url = &trim($url); # leading or trailing spaces again. } if ( $url =~ /^javascript:/i ) { $url =~ s/^javascript:.*?\(//i; # javascript( prefix (hope url is embedded) $url =~ s/[\(\)]//g; # no ( ) $url =~ s/^"*//; $url =~ s/"*$//; $url = &trim($url); # leading or trailing spaces again. } # once again: $url =~ s/^UR[LI]://i; $url = &trim($url); # leading or trailing spaces again. $url =~ s/\.*$//; $url = &trim($url); # leading or trailing spaces again. $url =~ s/^MAILTO:/mailto:/i; # quote shell metas and " ' (this leaves & ? in place). $url =~ s/([\$\*\(\){}\[\]";\\|<>~`'\n])/sprintf("%%%02x",unpack("c",$1))/eig; $url =~ s/\s+/+/g; # spaces -> + if ( $Prefix eq 'file:' && $url !~ /^file:/ && $url !~ m,^/, ) { chomp($Pwd = `pwd`); $url = "$Pwd/$url"; } elsif ( $Prefix eq '' && -f "$url" ) { if ( $url !~ m,^/, ) { chomp($Pwd = `pwd`); $url = "$Pwd/$url"; } $Prefix = 'file:'; } if ( $Prefix ne '' && $url !~ m,^$Prefix, ) { $url = "${$Prefix}$url"; } $url_ok = 1; if ( $url =~ /^\s*$/ ) { $url_ok = 0; } elsif ( $url !~ m,:/, && $url !~ /^mailto:/i ) { $url_ok = 0; } if ( ! $url_ok && $url =~ m/\@/ ) { $url = "mailto:$url"; $url_ok = 1; } if ( ! $url_ok ) { if ( $url =~ m,^//, ) { $url = "http:$url"; $url_ok = 1; } elsif ( $url =~ m,^/, ) { $url = "file:$url"; $url_ok = 1; } else { $url = "http://$url"; # force it... $url_ok = 1; } } if ( $Special eq '-j' ) { $GOTO_URL_CMD = $GOTO_JAVA_APPLET_CMD; $START_BROWSER_CMD = $GOTO_JAVA_APPLET_CMD; } elsif ( $Special eq '-p' ) { $GOTO_URL_CMD = $POST_REQUEST_CMD; $START_BROWSER_CMD = $POST_REQUEST_CMD; } elsif ( $Special eq '-M' ) { $GOTO_URL_CMD = $MOZ_GOTO_URL_CMD; $START_BROWSER_CMD = $MOZ_START_BROWSER_CMD; } elsif ( $Special eq '-N' ) { $GOTO_URL_CMD = $NS7_GOTO_URL_CMD; $START_BROWSER_CMD = $NS7_START_BROWSER_CMD; } elsif ( $Special eq '-N4' ) { $GOTO_URL_CMD = $NS4_GOTO_URL_CMD; $START_BROWSER_CMD = $NS4_START_BROWSER_CMD; } # $Special eq -H was handled above if ($No_new_window) { $GOTO_URL_CMD =~ s/,new-window//g; } $cmd1 = $GOTO_URL_CMD; $cmd2 = $START_BROWSER_CMD; $url =~ s/'/%27/g; if ( $url =~ /,/ && $cmd1 =~ /-remote.*openURL/ ) { $utmp = $url; $utmp =~ s/([,])/sprintf("%%%2x",unpack("c",$1))/eig; $utmp =~ s/,//g; $cmd1 =~ s/%s/$utmp/g; } else { $cmd1 =~ s/%s/$url/g; } $cmd2 =~ s/%s/$url/g; if ( $Debug || $Verbose ) { print "URL: $url\n"; print "CMD1: $cmd1\n"; print "CMD2: $cmd2\n"; exit 0 if $Debug; } $logfile = "$ENV{HOME}/.url.debug"; if ( -s $logfile && open(LOG, ">>$logfile") ) { print LOG "======== ", scalar(localtime), " ========\n"; print LOG "$0 ", join(' ', @args), "\n"; print LOG "utmp: $utmp1\n"; print LOG "URL: $url\n"; print LOG "CMD1: $cmd1\n"; print LOG "CMD2: $cmd2\n"; close(LOG); } print "RUNNING CMD1 ...\n" if $Verbose; system($cmd1); $rc = $?; $redo = 0; if ( $rc != 0) { if ($cmd1 ne $cmd2 ) { $redo = 1; } } elsif ($cmd1 =~ /netscape7/) { sleep 1; $test = `ps waux | grep $ENV{USER} | grep 'netscape.*remote' | grep -v grep`; if ($test =~ /^\s*$/) { $redo = 1; } } if ($redo) { print "CMD1 FAILED. RUNNING CMD2 ...\n" if $Verbose; if ( $cmd2 !~ /\&\s*$/ ) { system("$cmd2 &"); } else { system($cmd2); } } else { print "CMD1 OK.\n" if $Verbose; } exit $rc/256; ############################################################################# sub help { if (open(MORE, "|more")) { print MORE $Usage; close(MORE); } else { print STDOUT $Usage; } } sub trim { local($x) = @_; $x =~ s/^\s*//; $x =~ s/\s*$//; return $x; } sub basename { # Like basename(1), returns basename of a path. local($x) = @_; $x =~ s,/+$,,; # remove trailing /'s if ( $x =~ m,/([^/]+)$, ) { # check if matches / $x = $1; # grab stuff after the last / } $x = '.' if $x eq ''; # evidently input was null. return $x; }