X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=remoteglot.pl;h=c6323870037adb08629e5f3aff0749a0c01a5ffe;hp=bd10cd020475f50a4b3ca0db6e08108e3babead6;hb=b85fa1c5d6ec02daaf4e615cb09af8a6895b20be;hpb=9e923c34a6788eb9780a151316b196d4a18c3bbd diff --git a/remoteglot.pl b/remoteglot.pl index bd10cd0..c632387 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -164,27 +164,7 @@ while (1) { # any fun on the UCI channel? if ($nfound > 0 && vec($rout, fileno($engine->{'read'}), 1) == 1) { - # - # Read until we've got a full line -- if the engine sends part of - # a line and then stops we're pretty much hosed, but that should - # never happen. - # - my $line = ''; - while ($line !~ /\n/) { - my $tmp; - my $ret = sysread $engine->{'read'}, $tmp, 1; - - if (!defined($ret)) { - next if ($!{EINTR}); - die "error in reading from the UCI engine: $!"; - } elsif ($ret == 0) { - die "EOF from UCI engine"; - } - - $line .= $tmp; - } - - $line =~ tr/\r\n//d; + my $line = read_line($engine->{'read'}); handle_uci($line); $sleep = 0; @@ -586,7 +566,7 @@ sub output_screen { # specified. # if (exists($uciinfo{'pv1'}) && !exists($uciinfo{'pv2'})) { - for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) { + for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) { if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) { $uciinfo{$key} = $uciinfo{$key . '1'}; } @@ -1021,3 +1001,30 @@ sub open_engine { return $engine; } + +sub read_line { + my $fh = shift; + + # + # Read until we've got a full line -- if the engine sends part of + # a line and then stops we're pretty much hosed, but that should + # never happen. + # + my $line = ''; + while ($line !~ /\n/) { + my $tmp; + my $ret = sysread $engine->{'read'}, $tmp, 1; + + if (!defined($ret)) { + next if ($!{EINTR}); + die "error in reading from the UCI engine: $!"; + } elsif ($ret == 0) { + die "EOF from UCI engine"; + } + + $line .= $tmp; + } + + $line =~ tr/\r\n//d; + return $line; +}