2022-01-23 16:49:23 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# File: vm-exec
|
|
|
|
#
|
2022-02-10 19:58:56 +01:00
|
|
|
# Purpose: Process and send commands to the PBot Guest server (guest-server) on
|
|
|
|
# the default serial TCP port (5555). Use the PBOTVM_SERIAL environment variable
|
|
|
|
# to override the port. E.g., to use port 7777 instead:
|
2022-01-23 16:49:23 +01:00
|
|
|
#
|
2022-02-07 05:16:37 +01:00
|
|
|
# $ PBOTVM_SERIAL=7777 vm-exec -lang=sh echo test
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
use 5.020;
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
use feature qw(signatures);
|
|
|
|
no warnings qw(experimental::signatures);
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-01-29 21:22:48 +01:00
|
|
|
use constant {
|
2022-02-13 01:06:04 +01:00
|
|
|
SERIAL => $ENV{PBOTVM_SERIAL} // 5555,
|
|
|
|
CID => $ENV{PBOTVM_CID} // 7,
|
|
|
|
VPORT => $ENV{PBOTVM_VPORT} // 5555,
|
2022-01-29 21:22:48 +01:00
|
|
|
};
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
use File::Basename;
|
|
|
|
use JSON::XS;
|
|
|
|
use IPC::Open2;
|
|
|
|
use IO::Socket;
|
|
|
|
|
|
|
|
use FindBin qw($RealBin);
|
|
|
|
use lib "$RealBin/../lib";
|
|
|
|
|
|
|
|
sub connect_vsock($context) {
|
|
|
|
return undef if not $context->{'vm-cid'};
|
|
|
|
|
|
|
|
print STDERR "Connecting to remote VM socket CID $context->{'vm-cid'} port $context->{'vm-vport'}\n";
|
|
|
|
|
|
|
|
my $command = "socat - VSOCK-CONNECT:$context->{'vm-cid'}:$context->{'vm-vport'}";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
my ($pid, $input, $output) = eval {
|
|
|
|
my $pid = open2(my $output, my $input, $command);
|
|
|
|
return ($pid, $input, $output);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
print STDERR "Failed to connect to VM socket: $@\n";
|
|
|
|
return undef;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
if (not defined $pid) {
|
|
|
|
print STDERR "Failed to connect to VM socket: $!\n";
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($input, $output);
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
sub connect_serial($context) {
|
|
|
|
print STDERR "Connecting to remote VM serial port $context->{'vm-serial'}\n";
|
|
|
|
|
|
|
|
my $vm = IO::Socket::INET->new(
|
|
|
|
PeerAddr => '127.0.0.1',
|
|
|
|
PeerPort => $context->{'vm-serial'},
|
|
|
|
Proto => 'tcp',
|
|
|
|
Type => SOCK_STREAM
|
|
|
|
);
|
|
|
|
|
|
|
|
# return same $vm handle for ($input, $output)
|
|
|
|
return ($vm, $vm);
|
2022-02-08 18:55:00 +01:00
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
sub connect_vm($context) {
|
|
|
|
my ($input, $output);
|
|
|
|
|
|
|
|
# attempt preferred VSOCK connection
|
|
|
|
($input, $output) = connect_vsock($context);
|
|
|
|
|
|
|
|
# fallback to serial
|
|
|
|
if (not defined $input) {
|
|
|
|
($input, $output) = connect_serial($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $input) {
|
|
|
|
die "Could not create connection to VM: $!";
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
print STDERR "Connected to VM.\n";
|
|
|
|
return ($input, $output);
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
sub main() {
|
|
|
|
my $args = join ' ', @ARGV;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
my $context = eval { decode_json $args };
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
if ($@) {
|
|
|
|
# wasn't JSON; make structure manually
|
|
|
|
if ($args =~ s/^-lang=([^ ]+)\s+//) {
|
|
|
|
$context = { lang => $1, code => $args };
|
|
|
|
} else {
|
|
|
|
$context = { code => $args };
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
2022-02-14 07:01:06 +01:00
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
if (not exists $context->{code}) {
|
|
|
|
die "Usage: $0 <code>\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
# set any missing fields to default values
|
|
|
|
$context->{nick} //= 'vm';
|
|
|
|
$context->{channel} //= 'vm';
|
|
|
|
$context->{lang} //= 'c11';
|
|
|
|
$context->{'vm-serial'} //= SERIAL;
|
|
|
|
$context->{'vm-cid'} //= CID;
|
|
|
|
$context->{'vm-vport'} //= VPORT;
|
|
|
|
|
|
|
|
# parse -lang option
|
|
|
|
if ($context->{code} =~ s/^-lang=([^ ]+)\s+//) {
|
|
|
|
$context->{lang} = $1;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
my $language = lc $context->{lang};
|
|
|
|
|
|
|
|
eval {
|
|
|
|
require "Languages/$language.pm";
|
|
|
|
} or do {
|
|
|
|
my $found = 0;
|
|
|
|
my ($languages, $comma) = ('', '');
|
|
|
|
|
|
|
|
foreach my $module (sort glob "$RealBin/../lib/Languages/*.pm") {
|
|
|
|
$module = basename $module;
|
|
|
|
$module =~ s/.pm$//;
|
|
|
|
next if $module =~ m/^_/;
|
|
|
|
|
|
|
|
require "Languages/$module.pm" or die $!;
|
|
|
|
my $mod = "Languages::$module"->new;
|
|
|
|
|
|
|
|
if (exists $mod->{name} and $mod->{name} eq $language) {
|
|
|
|
$language = $module;
|
|
|
|
$found = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
$module = $mod->{name} if exists $mod->{name};
|
|
|
|
$languages .= "$comma$module";
|
|
|
|
$comma = ', ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not $found) {
|
|
|
|
print "Language '$language' is not supported.\nSupported languages are: $languages\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (not length $context->{code}) {
|
|
|
|
if (exists $context->{usage}) {
|
|
|
|
print "$context->{usage}\n";
|
|
|
|
} else {
|
|
|
|
print "Usage: cc [-lang=<language>] [-info] [-paste] [-args \"command-line arguments\"] [compiler/language options] <code> [-stdin <stdin input>]\n";
|
|
|
|
}
|
|
|
|
exit;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
my $lang = "Languages::$language"->new(%$context);
|
|
|
|
|
|
|
|
$lang->process_interactive_edit;
|
|
|
|
$lang->process_standard_options;
|
|
|
|
$lang->process_custom_options;
|
|
|
|
$lang->process_cmdline_options;
|
|
|
|
$lang->preprocess_code;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
($lang->{'vm-input'}, $lang->{'vm-output'}) = connect_vm($context);
|
|
|
|
$lang->execute;
|
|
|
|
|
|
|
|
$lang->postprocess_output;
|
|
|
|
$lang->show_output;
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
main();
|