#!/bin/sh -- # A comment mentioning perl, indented bash's sake. eval 'exec perl -S $0 ${1+"$@"}' if 0; chop($Program = `basename $0`); $Mode = $Program; # Notes: # # Make many symlinks pointing to this script. Give them the names # of the keys of %url (see below). Then calling it by the various names # will automatically select the correct url string. Otherwise, specify # the mode via the -m switch. # # # If DISPLAY is set, netscape is used, otherwise lynx is used (lynx # might not always work or given anything particularly useful). # # Other flags: # -x launch X windows prompt (e.g. tkquery) # -s use whatever is in the X windows selection # otherwise the arguments in ARGV are used as the "keywords". if ( $ARGV[0] eq '-m' ) { shift; $Mode = shift; $mode_arg = $Mode if $Mode !~ /^\s*$/; } else { $mode_arg = ''; } $Location = "Bedford NH"; $tkquery = "tkquery -warp -i \"Alt-c:, $Location\" -i \"Alt-p:, $Location\" -i \"Alt-g:m:google:\""; $tk_font = '-f 120'; &initialize_mappings(); # see below for all of the mappings. $Usage = <<"END"; $Program: command line interface to launch WWW browser to various queries. Usage: $Program Options: -list, -h, List the names each action -help -v also list the URLs for each action -m mode Use action "mode" -x launch an X based UI using tkquery. -s use the current X selection for Examples: sel_url -m usenet linux amd crashes usenet linux amd crashes dictionary -x search -s Notes: make symlinks to this program ("sel_url") to define the action. Otherwise use "-m mode" END if ( ! @ARGV && $Program eq 'sel_url' ) { push(@ARGV, '-h'); } if ( $ARGV[0] =~ /^(-list|-h|-help|-v)$/ ) { $v = 1 if $1 eq '-v'; shift; $v = 1 if $ARGV[0] eq '-v'; $| = 1; print STDERR $Usage; print STDERR "Listing actions: (use these for symlink names)\n"; foreach $cmd (sort(keys(%ui))) { print STDOUT " - $cmd"; chop($loc = `type $cmd 2>&1`); $loc =~ s/^type:\s*//; $loc =~ s/^$cmd is\s*//; if ( length($cmd) < 5 ) { print "\t"; } print STDOUT "\t$loc\n"; if ( $v ) { print STDOUT " $url{$cmd}\n"; } } exit 0; } $Mode = 'yp_people' if $Mode eq 'phone'; if ( ! $url{$Mode} ) { print STDERR "$Program: Unknown Mode: $Mode\n"; exit 1; } if ( $ENV{DISPLAY} eq '' || $ENV{LYNX} ne '' ) { $doX = 0; } else { $doX = 1; } $user_input = 'NONE'; if ( $doX ) { if ( $ARGV[0] eq '-x' ) { my $cmd = $ui{$Mode}; my @A = @ARGV; shift @A; if (@A) { my $str = join(' ', @A); $str =~ s/["'`*;]//g; # not needed? $cmd =~ s/:: ::/::${str}::/; } $user_input = `$cmd`; } elsif ( $ARGV[0] =~ /^-s/ ) { $user_input = `selection.tcl`; $user_input =~ s/[\n\r]*$//; } } elsif ( ! @ARGV ) { # ugly prompt: $tmp = $ui{$Mode}; $match = $tkquery; $match =~ s/(\W)/\\$1/g; if ( $tmp =~ /$match/ ) { $tmp = "Query: $'"; } $tmp =~ s/\\n/\n\t /g; $tmp =~ s/-/\n\t-/g; print STDERR $tmp, "\n\nEnter words> "; chop($in = ); push(@ARGV, $in); } if ( $user_input ne 'NONE' ) { # todo \n -> , ? ###chop $user_input; $user_input =~ s/\s*$//; $user_input =~ s/[\n\r]*$//; if ( $user_input !~ /,/ ) { $user_input =~ s/\n/,/g; } else { $user_input =~ s/\n/ /g; } if ( $user_input =~ /^\s*m:([^:]+):/ ) { $user_input = $'; my $m= $1; $mode_arg = $m unless $m =~ /^\s*$/; } print STDERR "$Program: user_input is: $user_input, mode is $Mode/$mode_arg\n"; if ( $user_input =~ /^\s*$/ || $user_input =~ /^CANCEL$/i ) { exit 0; } if ( $mode_arg ne '' ) { exec $0, '-m', $mode_arg, $user_input; } else { exec $0, $user_input; } exit 1; } if ( $doX ) { $chk_ns_remote = system("sh -c 'type netscape_remote'"); if ( $chk_ns_remote == 0 ) { $cmd0 = 'netscape_remote -noraise -remote openURL(%URL%,new-window)'; } else { $cmd0 = 'netscape -noraise -remote openURL(%URL%,new-window)'; } if (-f "$ENV{HOME}/.url_use_mozilla") { $cmd1 = 'mozilla -remote openURL(%URL%,new-window)'; } elsif (-f "$ENV{HOME}/.url_use_netscape7") { $cmd1 = 'netscape7 -remote openURL(%URL%,new-window)'; } elsif (-f "$ENV{HOME}/.url_use_netscape4") { $cmd1 = $cmd0; } else { $cmd1 = $cmd0; } if ($ENV{URL_USE_MOZILLA}) { $cmd1 = 'mozilla -remote openURL(%URL%,new-window)'; } elsif ($ENV{URL_USE_NETSCAPE7}) { $cmd1 = 'netscape7 -remote openURL(%URL%,new-window)'; } elsif ($ENV{URL_USE_NETSCAPE4}) { $cmd1 = $cmd0; } print STDERR "cmd1: $cmd1\n"; $cmd2 = 'netscape %URL%'; } else { if ( $ENV{BROWSER} ne '' ) { $cmd1 = "$ENV{BROWSER} %URL%"; } else { $cmd1 = "lynx %URL%"; } } if ($Mode eq 'open_url' && $ARGV[0] =~ /\s/) { # hack for accidentally typing search terms in to Open_Url $Mode = 'search'; } $url = $url{$Mode}; $joined = join('+', @ARGV); $joined = &trim($joined); $joined =~ s/\s+/+/g; $joined =~ s/\++/+/g; if ($joined =~ /^\s*M:\s*/i) { $joined = $'; $cmd1 = 'mozilla -remote openURL(%URL%,new-window)'; } elsif ($joined =~ /^\s*N:\s*/i) { $joined = $'; $cmd1 = 'netscape4 -noraise -remote openURL(%URL%,new-window)'; } # here begins special case handling for all the different modes/urls: if ( $Mode =~ /^(map|map2|pocketmap)$/ ) { $joined =~ s/\n+/,/g; # todo if matches ,.*, decide what is best ($addr, $city) = split(/,/, $joined, 2); $addr = &trim($addr); $city = &trim($city); $addr =~ s/^\+//; $addr =~ s/\+$//; $addr =~ s/#/No./g; # apartment number problem. $city =~ s/^\+//; $city =~ s/\+$//; if ( $city =~ /^\w\w[+\s]/ ) { # probably just state+zip $rest = $'; if ( $rest !~ /^\w\w\b/ && $addr =~ /([^+]+)$/ ) { $addr = $`; $city = "$1+$city"; $addr =~ s/\+$//; $city =~ s/\+$//; } } $addr =~ s/,//g; $city =~ s/,//g; if ( $city =~ /^\s*\S\S\s*$/ ) { if ( $addr =~ /\+([^\+]*)$/ ) { $addr = $`; $city = "$1+$city"; } } $url =~ s/%ADDR%/$addr/; $url =~ s/%CITY%/$city/; } elsif ( $Mode =~ /^(yp_people|infospace)/ ) { ($name, $state) = split(/,/, $joined, 2); $name =~ s/^\+//; $name =~ s/\+$//; $state =~ s/^\+//; $state =~ s/\+$//; $first = ''; $last = ''; $city = ''; $st = ''; if ( $name =~ /\+([^\+]*)$/ ) { $first = $`; $last = $1; } else { $last = $name; } if ( $state =~ /\+([^\+]*)$/ ) { $city = $`; $st = $1; } else { $st = $state; } $url =~ s/%FIRST%/$first/; $url =~ s/%LAST%/$last/; $url =~ s/%CITY%/$city/; $url =~ s/%STATE%/$st/; } elsif ( $Mode eq 'yp_general' ) { ($name, $state) = split(/,/, $joined, 2); $name =~ s/^\+//; $name =~ s/\+$//; $state =~ s/^\+//; $state =~ s/\+$//; $first = ''; $last = ''; $city = ''; $st = ''; if ( $state =~ /\+([^\+]*)$/ ) { $city = $`; $st = $1; } else { $st = $state; } if ( $st =~ /^\d+$/ ) { # zipcode $st = "&Zip=$st"; } $url =~ s/%NAME%/$name/; $url =~ s/%CITY%/$city/; $url =~ s/%STATE%/$st/; } elsif ( $Mode eq 'stock' ) { if ( $joined =~ /^\./ ) { $joined = $'; } else { $joined =~ y/[a-z]/[A-Z]/; } $url =~ s/%SYMBOL%/$joined/; } elsif ( $Mode eq 'weather' ) { $url =~ s/%DEST%/$joined/; } elsif ( $Mode eq 'movies' ) { $url =~ s/%DEST%/$joined/; } elsif ( $Mode =~ /^(dictionary|thesaurus)$/ ) { $url =~ s/%WORD%/$joined/; } elsif ( $Mode =~ /^(zip|zipsearch)$/ ) { $url =~ s/%ZIP%/$joined/; $cmd1 = 'post_wrapper %URL%'; $cmd2 = $cmd1; } elsif ( $Mode =~ /(news|usenet)/ ) { $url =~ s/%QUERY%/$joined/; } elsif ( $Mode =~ /^(search|websearch|google|lycos)$/ ) { $url =~ s/%QUERY%/$joined/; } elsif ( $Mode =~ /^(streetprice|pricewatch)$/ ) { $url =~ s/%QUERY%/$joined/; } elsif ( $Mode eq 'open_url' ) { $url =~ s/%THEURL%/$joined/; } if ( $doX ) { local($user); chop($user = `id -un 2>/dev/null`); if ( $user eq '' ) { $user = $ENV{'USER'}; } if ( $user eq '' ) { chop($user = `whoami 2>/dev/null`); } if ( $user eq '' ) { $user = '.'; } $uname = `uname -sr`; if ( $uname =~ /(Linux|SunOS [34])/ ) { $ps = `ps waux | grep netscape | grep -v grep | grep $user`; } else { $ps = `ps -elf | grep netscape | grep -v grep | grep $user`; } if ( $ps eq '' ) { $cmd = $cmd2; } else { $cmd = $cmd1; $url =~ s/,/%2C/g; } } else { $cmd = $cmd1; $cmd =~ s/%URL%/$url/; exec split(/\s+/, $cmd); exit 1; } $cmd =~ s/%URL%/$url/; if ( fork ) { exit 0; } else { print STDERR "\n$Program: running: $cmd\n"; exec split(/\s+/, $cmd); } # subroutines: 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; } sub dirname { # Like dirname(1), returns dirname of a path. local($x) = @_; $x =~ s,/+$,,; # remove trailing /'s $x =~ s,/+,/,g; # change multiple /'s to single / if ( $x =~ m,/([^/]+)$, ) { # check if matches / $x = $`; # grab stuff before / $x = '/' if $x eq ''; # is root if null. } else { # it was a bare word without / $x = '.'; } $x = '.' if $x eq ''; # never return null string return $x; } # url data: sub initialize_mappings { # we put them here since all the crazy quoting screws up formatting! #$url{map} = 'http://maps.yahoo.com/yahoo/yt.hm?FAM=yahoo&CMD=GEO&SEC=geo&&AD2=%ADDR%&AD3=%CITY%'; #$url{map} = 'http://maps.yahoo.com/py/maps.py?addr=%ADDR%&city=%CITY%'; #$url{map} = 'http://us.rd.yahoo.com/maps/home/submit_a/*-http://maps.yahoo.com/maps?addr=%ADDR%&csz=%CITY%&srchtype=a'; $url{map} = 'http://maps.yahoo.com/maps?addr=%ADDR%&csz=%CITY%&srchtype=a'; #$url{map2} = 'http://www.mapblast.com/mapblast/blast.hm?CMD=GEO&AD2=%ADDR%&AD3=%CITY%'; #$url{map2} = 'http://www.mapblast.com/myblast/map.mb?CMD=GEO&CT=&IC=&GMI=&GAD1=&GAD2=&GAD3=&noPrefs=&remLoc=&AD4=USA&AD2_street=%ADDR%&AD3=%CITY%&apmenu=&apcode=&AD2=%ADDR%&req_action=crmap'; $url{pocketmap} = 'http://www.mapblast.com/pblast/pmap.mb?W=425&H=400&loc=us&DU=MI&CMD=GEO&AD2=%ADDR%&AD3=%CITY%&STYLE='; $url{map2} = 'http://www.mapblast.com/myblast/map.mb?CMD=GEO&CT=&IC=&GMI=&GAD1=&GAD2=&GAD3=&noPrefs=&remLoc=&AD4=USA&AD2_street=%ADDR%&AD3=%CITY%&apmenu=&apcode=&AD2=%ADDR%&req_action=crmap'; #$url{map2} = 'http://www.mapblast.com/myblast/map.mb?&CMD=GEO&GMI=1&AD2=­DR%&AD2_street=%ADDR%&AD3=%CITY%&AD4=USA&req_action=crmap&forceAddr=1'; #$url{stock} = 'http://www.stockmaster.com/wc/form/P1?template=sm/chart&Symbol=%SYMBOL%'; #$url{stock} = 'http://www.quote.com/quotecom/stocks/quotes.asp?symbols=%SYMBOL%'; $url{stock} = 'http://finance.lycos.com/home/stocks/charts.asp?symbols=%SYMBOL%'; #$url{news} = 'http://search.dejanews.com/dnquery.xp?QRY=%QUERY%&DBS=2&ST=PS&defaultOp=AND&LNG=ALL&format=terse&showsort=score&maxhits=100&subjects=&groups=&authors=&fromdate=&todate='; #$url{news} = 'http://www.deja.com/=dnc/[ST_rn=ps]/qs.xp?ST=PS&QRY=%QUERY%&defaultOp=AND&DBS=1&OP=dnquery.xp&LNG=ALL&subjects=&groups=&authors=&fromdate=&todate=&showsort=score&maxhits=100'; #$url{news} = 'http://groups.google.com/groups?q=%QUERY%&hl=en&lr=&safe=off&site=groups'; $url{news} = 'http://groups.google.com/groups?as_q=%QUERY%&num=100&btnG=Google+Search&as_oq=&as_epq=&as_eq=&as_ugroup=&as_usubject=&as_uauthors=+&as_umsgid=&lr='; #$url{yp_people} = 'http://people.yahoo.com/py/psPhoneSearch.py?Pyt=Tps&FirstName=%FIRST%&LastName=%LAST%&City=%CITY%&State=%STATE%&YY=11111'; $url{yp_people} = 'http://phone.people.yahoo.com/py/psPhoneSearch.py?srch=bas&D=1&FirstName=%FIRST%&LastName=%LAST%&City=%CITY%&State=%STATE%&Phone=&Search=Phone+and+Address+Search'; $url{infospace} = 'http://kevdb.infospace.com/info/kevdb?OTMPL=%2Fres%2Fr1.html&QFM=N&QK=5&QN=%LAST%&QS=%STATE%&KCFG=us'; $url{yp_general} = 'http://yp.yahoo.com/py/ypResults.py?Pyt=TFour11&city=%CITY%&state=%STATE%&cs=5&btype=default&stx=%NAME%&stp=a'; $url{lycos} = 'http://www.lycos.com/cgi-bin/pursuit?cat=lycos&query=%QUERY%&matchmode=and&minscore=.1&maxhits=40&terse=default'; $url{google} = 'http://www.google.com/search?q=%QUERY%&num=100&meta=hl%3Den%26lr%3D&safe=off'; $url{search} = $url{google}; $url{zip} = 'http://www.usps.gov/cgi-bin/zip4/ctystzip?ctystzip=%ZIP%'; $url{zipsearch} = $url{zip}; $url{weather} = 'http://www.weather.com/cgi-bin/uncgi/zip_city_search.cgi?destination=%DEST%'; $url{movies} = 'http://movies.yahoo.com/showtimes/showtimes.html?z=%DEST%&m=&t=&a=&r=adv&Submit+Info=Showtimes'; $url{dictionary} = 'http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va=%WORD%'; $url{thesaurus} = 'http://www.m-w.com/cgi-bin/thesaurus?book=Thesaurus&va=%WORD%'; $url{open_url} = '%THEURL%'; $url{streetprice} = 'http://orinaco.streetprices.com/Computers/search.pl?query=%QUERY%'; $url{pricewatch} = 'http://brook.pricewatch.com/search/search.asp?criteria=%QUERY%'; $ui{map} = "$tkquery -t Yahoo_maps -e 'Address:: ::35' 'Enter address using single comma as in:\\n \\\"123 S. Main St, Toledo OH\\\"\\nYou may also add the ZipCode at the end.' $tk_font"; $ui{map2} = "$ui{map}"; $ui{pocketmap} = "$ui{map}"; $ui{stock} = "$tkquery -t Stock -oneline -e 'Symbol:: ::10' 'Enter ticker symbol.' $tk_font"; $ui{news} = "$tkquery -t Deja_News -oneline -e 'Keywords:: ::40' 'Enter keywords to search USENET' $tk_font"; $ui{yp_people} = "$tkquery -t Yahoo_People -e 'Name:: ::40' 'Enter Name, State using single comma:\\n \\\"John Q. Smith, OH\\\"\\nYou may include city info after the comma.' $tk_font"; $ui{infospace} = "$tkquery -t InfoSpace_People -e 'Name:: ::40' 'Enter Name, State using single comma:\\n \\\"John Q. Smith, OH\\\"\\nYou may include city info after the comma.' $tk_font"; $ui{yp_general} = "$tkquery -t Yahoo_Yellow_Pages -e 'Name:: ::40' 'Enter Name, State (or Zip) using single comma:\\n \\\"ABC Rentals, Toledo OH\\\"\\nYou may include city info after the comma.' $tk_font"; $ui{search} = "$tkquery -t Search -oneline -e 'Keywords:: ::40' 'Enter keywords to search WWW' $tk_font"; $ui{google} = "$tkquery -t Google -oneline -e 'Keywords:: ::40' 'Enter keywords to Google search WWW' $tk_font"; $ui{lycos} = "$tkquery -t Lycos -oneline -e 'Keywords:: ::40' 'Enter keywords to Lycos search WWW' $tk_font"; $ui{zip} = "$tkquery -t Zip_Code_Search -oneline -e 'Lookup:: ::30' 'Enter a City or a Zip.' $tk_font"; $ui{zipsearch} = $ui{zip}; $ui{weather} = "$tkquery -t Weather -e 'Weather:: ::30' 'Enter a City or a Zip.\\n(it is best to skip the State)' $tk_font"; $ui{movies} = "$tkquery -t Movies -oneline -e 'City/Zip:: ::30' 'Enter a City or a Zip.' $tk_font"; $ui{dictionary} = "$tkquery -t Dictionary -oneline -e 'Word:: ::30' 'Enter a Word to look up.' $tk_font"; $ui{thesaurus} = "$tkquery -t Thesaurus -oneline -e 'Word:: ::30' 'Enter a Word to look up in Thesaurus.' $tk_font"; $ui{open_url} = "$tkquery -t Open_Url -oneline -e 'URL:: ::30' 'Enter the url to go to.' $tk_font"; $ui{streetprice}= "$tkquery -t StreetPrices -oneline -e 'Keywords:: ::40' 'Enter keywords search StreetPrices.com' $tk_font"; $ui{pricewatch}= "$tkquery -t PriceWatch -oneline -e 'Keywords:: ::40' 'Enter keywords search PriceWatch.com' $tk_font"; $url{usenet} = $url{news}; $ui{usenet} = $ui{news}; }