+sub protocol_expect ($&) {
+ my ($fh, $match) = @_;
+ local $_;
+ $_ = <$fh>;
+ defined && chomp or badproto $fh, "eof";
+ return if &$match;
+ badproto $fh, "\`$_'";
+}
+
+sub protocol_send_file ($$) {
+ my ($fh, $ourfn) = @_;
+ open PF, "<", $ourfn or die "$ourfn: $!";
+ for (;;) {
+ my $d;
+ my $got = read PF, $d, 65536;
+ die "$ourfn: $!" unless defined $got;
+ last if $got;
+ print $fh "data-block ".length($d)."\n" or die $!;
+ print $d or die $!;
+ }
+ print $fh "data-end\n" or die $!;
+ close PF;
+}
+
+sub protocol_receive_file ($$) {
+ my ($fh, $ourfn) = @_;
+ open PF, ">", $ourfn or die "$ourfn: $!";
+ for (;;) {
+ protocol_expect \*STDIN, { m/^data-block (\d{1,6})$|data-end$/ };
+ length $1 or last;
+ my $d;
+ my $got = read $fh, $d, $1;
+ $got==$1 or badproto $fh, "eof during data block";
+ print PF $d or die $!;
+ }
+}
+
+#---------- remote protocol support, responder ----------
+
+sub responder_send_command ($) {
+ my ($command) = @_;
+ return unless $we_are_responder;
+ # called even without $we_are_responder
+ print DEBUG "<< $command\n";
+ print $command, "\n" or die $!;
+}
+