client and server manage to talk
[scpubgit/Tak.git] / jreplclient
CommitLineData
ff0abfba 1#!/usr/bin/env perl
2
3use strictures 1;
4use IPC::Open2;
5use Term::ReadLine;
6use JSON::PP qw(encode_json decode_json);
7
8my $cmd = do {
9 if (my $host = $ARGV[0]) {
ac134649 10 'ssh '.$host.' perl -'
ff0abfba 11 } else {
ac134649 12 'perl -'
ff0abfba 13 }
14};
15
16my $pid = open2(my $out, my $in, $cmd);
17
ac134649 18{
19 my $jr;
20 if (!eof(*DATA)) {
21 $jr = \*DATA
22 } else {
23 open $jr, '<', 'jsonrepl.packed' or die "No packed repl: $!";
24 }
25 while (<$jr>) { print $in $_ }
26 print $in "__END__\n";
27}
28
ff0abfba 29my $read = Term::ReadLine->new('REPL');
30
31while (1) {
32 my $line = $read->readline('re.pl$ ');
33 exit unless defined $line;
34 next unless length $line;
35 print $in encode_json([ EVAL => $line ])."\n";
36 my $reply = decode_json scalar readline($out);
37 if ($reply->[0] eq 'MISTAKE') {
38 die "Botch: ".join(': ', @{$reply}[1,2]);
39 }
40 my $ret = $reply->[1];
41 print $ret->{return};
42 if ($ret->{stdout}) {
43 chomp($ret->{stdout});
44 print "STDOUT:\n${\$ret->{stdout}}\n";
45 }
46 if ($ret->{stderr}) {
47 chomp($ret->{stderr});
48 print "STDERR:\n${\$ret->{stderr}}\n";
49 }
50}