プログラム

#!/usr/local/bin/perl
#
#	曖昧検索サーバを使う。
#
require 'clientserver.pl';
require "getopts.pl";
&Getopts('p');
$repeat = $opt_p;

$pat = shift;
exit if !$opt_p && !$pat;

&clientopen(5555,'CLIENTSOCK');

if($repeat){
	$| = 1;
	open(tty,"/dev/tty");
	while(1){
		print "英和> ";
		$pat = <tty>;
		last if $pat eq '';
		chop($pat);
		&output(&lookup($pat));
	}
	print "\n";
}
else {
	&output(&lookup($pat));
}
sub lookup {			# 侯補が見つかるまで曖昧度を増やしながら検索
	local($word) = @_;
	for $mismatch (0..3){
	 	print CLIENTSOCK "S$mismatch$word ";
		$_ = '';
		while(1){
			sysread(CLIENTSOCK, $s, 1024);
			$_ .= $s;
			last if /\n$/;
		}
		chop;
		return $_ if $_ > 0;
	}
	0;
}

sub output {			# 検索結果取得/出力
	local($n) = @_;
	$n = ($n > 10 ? 10 : $n);
	$n--;
 	print CLIENTSOCK "E0,$n";
	$_ = '';
	while(1){
		sysread(CLIENTSOCK, $s, 1024);
		$_ .= $s;
		last if /\n$/;
	}
	@entries = split(/\t/,$_);
 	print CLIENTSOCK "C0,$n";
	$_ = '';
	while(1){
		sysread(CLIENTSOCK, $s, 1024);
		$_ .= $s;
		last if /\n$/;
	}
	@japanese = split(/\t/,$_);
	for $i (0..$n){
		print "$entries[$i]\n\t$japanese[$i]\n";
	}
}