#!/usr/local/bin/perl
# A simple webster client in perl
# Copyright (c) 1990 Khun Yee Fung (clipper@csd.uwo.ca)
# No warranty. See the end of this program for details.

# Actually made to work by stergios.  This program could not talk to a
# webster server running on a next.  I got rid of the 
# "do sys/socket.h" stuff too.  It was needlessly comples IMHO.
# Also changed the options to be more intuitive.
# sterg sez "Use this program at your own risk.  It is a hack
# and is guaranteed to never do the right thing.  If you use this you
# are a fool."  For exactly the same thing see the origonal Author's
# comments and restrictions located at the bottom of this file.
# Ported to Solaris by opusl.  Added back sys/socket.h since you
# need it. (-:

do 'getopts.pl' || die "can\'t do getopts.pl: $@";
do Getopts(':dstpcuv');
$version = "1.1";

@prog = split("/", $0);
if (defined($opt_u) || $#ARGV < $[) {
  print "Usage: $prog[$#prog] [-d] [-t] [-p] [-c] [-s] [-u] [-v] [words]
   -d search for the word in the full dictionary
   -t check the word in the thesaurus instead of the dictionary
   -p list all words that start with this prefix
   -c check if the word is a complete word
   -s spell check a word
   -u print this message
   -v print the version of this program
  default is to check the definition of the words\n";
  exit 0;
}
elsif (defined($opt_v)) {
  print "$prog[$#prog], version $version\n";
  exit 0;
}

$sockaddr = 'S n a4 x8';            # Magic words!
$hostname = "webster-server.stanford.edu";    # our webster host.
$port = 765 ;
(($name, $aliases, $type, $len, $saddr) = gethostbyname($hostname))
    || die "no websterhost \"$hostname\"\n";
$sin = pack($sockaddr, 2, $port, $saddr);

$SIG{'INT'} = 'IGNORE';
eval 'require "sys/socket.ph"';
eval 'sub SOCK_STREAM {-f "/vmunix" ? 1 : 2;}' if $@;
socket(SERVER, 2, &SOCK_STREAM , 0) || die "socket: $!";
connect(SERVER, $sin) || die "connect: $!";
$SIG{'INT'} = 'dokill';

select(SERVER); $| = 1; select(STDOUT); $| = 1;
$key = "DEFINE";
option: {
  defined($opt_t) && ((printf SERVER "INDEX thesaurus\n"), last option);
  defined($opt_d) && ((printf SERVER "INDEX dictionary\n"), last option);
  defined($opt_s) && ($key = "SPELL", last option);
  defined($opt_p) && ($key = "ENDINGS", last option);
  defined($opt_c) && ($key = "COMPLETE", last option);
}

while ($word = shift) {
  print "---> ", $word, "\n";
  printf SERVER "%s %s\n", $key, $word;

  $_ = <SERVER>;

  response: {
    /WILD 0/ && ((print "No match\n"), last response);
    /WILD/ && ((print "Matches are:\n"), do listlines(), last response);
    /SPELLING 0/ && ((print "Can\'t find this word\n"), last response);
    /SPELLING 1/ && ((print "Correct Spelling\n"), last response);
    /SPELLING/ && ((print "Maybe you mean:\n"), do listlines(), last response);
    /DEFINITION ([\d]+)/ && (do listlines(), last response);
    /ERROR FATAL/ && ((print "Fatal Error:", $'), last response);
    /ERROR/ && ((print "Error: ", $'), last response);
    /AMBIGUOUS ([\d]+)/ && ((print "$1 possible matches\n"), last response);
    /COMPLETION (.+)/ && ((print $1, "\n"), last response);
    /MATCHS 0/ && ((print "Matches nothing\n"), last response);
    /MATCHS/ && ((print "Matches:\n"), do listlines(), last response);
    print "$_\n";
  }
}
close SERVER;


# SUBROUTINES
sub listlines{
  $/ = "\200";
  $buf = <SERVER>;
  $buf =~ s/[\r\200]//g;
  print $buf;
  $/ = "\n";
}

# kill the connection
sub dokill { close SERVER; exit 1; }

# Copyright 1990 Khun Yee Fung <clipper@csd.uwo.ca>
#
# Permission to use, copy, modify, and distribute, this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of the copyright holders be used in
# advertising or publicity pertaining to distribution of the software with
# specific, written prior permission, and that no fee is charged for further
# distribution of this software, or any modifications thereof.  The copyright
# holder make no representations about the suitability of this software for
# any purpose.  It is provided "as is" without express or implied warranty.
#
# THE COPYRIGHT HOLDER DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
# EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
