2022-01-23 16:49:23 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# File: vm-exec
|
|
|
|
#
|
|
|
|
# Purpose: Process and send commands to the PBot virtual machine on the
|
2022-02-07 05:16:37 +01:00
|
|
|
# default 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
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use File::Basename;
|
|
|
|
use JSON::XS;
|
|
|
|
|
|
|
|
use FindBin qw($RealBin);
|
|
|
|
use lib "$RealBin/../lib";
|
|
|
|
|
2022-01-29 21:22:48 +01:00
|
|
|
use constant {
|
2022-02-07 05:13:13 +01:00
|
|
|
SERIAL_PORT => $ENV{PBOTVM_SERIAL} // 5555,
|
2022-01-29 21:22:48 +01:00
|
|
|
};
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
my $json = join ' ', @ARGV;
|
2022-02-08 18:55:00 +01:00
|
|
|
my $data = eval { decode_json $json };
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
# wasn't JSON; make structure manually
|
2022-02-08 18:55:00 +01:00
|
|
|
if ($json =~ s/^-lang=([^ ]+)\s+//) {
|
|
|
|
$data = { lang => $1, code => $json };
|
2022-01-23 16:49:23 +01:00
|
|
|
} else {
|
2022-02-08 18:55:00 +01:00
|
|
|
$data = { code => $json };
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:55:00 +01:00
|
|
|
if (not exists $data->{code}) {
|
2022-01-29 21:22:48 +01:00
|
|
|
die "Usage: $0 <code>\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# set any missing fields to default values
|
2022-02-08 18:55:00 +01:00
|
|
|
$data->{nick} //= 'vm';
|
|
|
|
$data->{channel} //= 'vm';
|
|
|
|
$data->{lang} //= 'c11';
|
|
|
|
$data->{'vm-port'} //= SERIAL_PORT;
|
|
|
|
|
|
|
|
# parse -lang option
|
|
|
|
if ($data->{code} =~ s/^-lang=([^ ]+)\s+//) {
|
|
|
|
$data->{lang} = $1;
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 18:55:00 +01:00
|
|
|
my $language = lc $data->{lang};
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-08 18:55:00 +01:00
|
|
|
if (not length $data->{code}) {
|
|
|
|
if (exists $data->{usage}) {
|
|
|
|
print "$data->{usage}\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
} else {
|
|
|
|
print "Usage: cc [-lang=<language>] [-info] [-paste] [-args \"command-line arguments\"] [compiler/language options] <code> [-stdin <stdin input>]\n";
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:55:00 +01:00
|
|
|
my $lang = "Languages::$language"->new(%$data);
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
$lang->{local} = $ENV{CC_LOCAL};
|
|
|
|
|
|
|
|
$lang->process_interactive_edit;
|
|
|
|
$lang->process_standard_options;
|
|
|
|
$lang->process_custom_options;
|
|
|
|
$lang->process_cmdline_options;
|
|
|
|
$lang->preprocess_code;
|
|
|
|
$lang->execute;
|
|
|
|
$lang->postprocess_output;
|
|
|
|
$lang->show_output;
|