-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsonrpc-run.pl
executable file
·63 lines (55 loc) · 1.19 KB
/
jsonrpc-run.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env perl
use strict;
use warnings;
use sigtrap qw(die normal-signals);
use Symbol 'gensym';
use IPC::Open2;
use IO::Handle;
use Getopt::Long qw(:config pass_through);
sub usage {
die <<END
Usage: jsonrpc-run.pl CMD ARGS...
For example: jsonrpc-run.pl -v deno --log-level=debug lsp
END
}
{
GetOptions("v|verbose" => \ (my $o_verbose))
or usage();
my ($cmd, @args) = @ARGV
or usage();
my $pid = open2(
my $chld_out, my $chld_in
, $cmd, @args
) or die "Can't run $cmd @args";
local @ARGV = "-";
while (<>) {
chomp;
if ($o_verbose) {
print "# Content-Length: ", length($_), "\n";
}
print $chld_in "Content-Length: ", length($_), "\r\n\r\n";
print $chld_in $_;
$chld_in->flush;
print "\n";
my $response = read_response($chld_out) or do {
print STDERR "NO response, exiting...\n";
last;
};
print "# ==>\n";
print $response, "\n";
}
}
sub read_response {
my ($in_fh) = @_;
local $/= "\r\n";
defined(my $line = <$in_fh>)
or return;
defined(scalar <$in_fh>)
or return;
chomp($line);
(undef, my $length) = split ": ", $line, 2;
my $buf = "";
read($in_fh, $buf, $length)
or return;
$buf;
}