From 31aa1d586959c66a658419cd54e03e7c44f36877 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 8 Feb 2022 09:55:00 -0800 Subject: [PATCH] compiler_vm: simplify vm-client; move -lang option to vm-exec --- applets/compiler_vm/host/bin/vm-client | 28 ++++++--------------- applets/compiler_vm/host/bin/vm-exec | 34 ++++++++++++++------------ 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/applets/compiler_vm/host/bin/vm-client b/applets/compiler_vm/host/bin/vm-client index 4c604b5b..e9c39c13 100755 --- a/applets/compiler_vm/host/bin/vm-client +++ b/applets/compiler_vm/host/bin/vm-client @@ -2,12 +2,11 @@ # File: vm-client # -# Purpose: Interfaces with the PBot virtual machine server hosted at -# PeerAddr/PeerPort defined below. This allows us to host instances of -# virtual machines on remote servers. +# Purpose: Interfaces with the PBot virtual machine server hosted by +# `vm-server` at PeerAddr/PeerPort defined below. This allows us to +# host instances of virtual machines on remote servers. # -# This script is intended to be installed to PBot's applets directory -# and attached to a PBot command such as `cc`. +# This script is intended to be attached to a PBot command such as `cc`. # SPDX-FileCopyrightText: 2021 Pragmatic Software # SPDX-License-Identifier: MIT @@ -18,7 +17,6 @@ use warnings; use strict; use IO::Socket; -use JSON::XS; use constant { SERVER_PORT => $ENV{PBOTVM_SERVER} // 9000, @@ -31,22 +29,10 @@ my $sock = IO::Socket::INET->new( ); if (not defined $sock) { - print "Fatal error compiling: $!; try again later\n"; + print "Fatal error: $!; try again later\n"; die $!; } -my $json = join ' ', @ARGV; -my $h = decode_json $json; - -my $lang = $h->{lang} // "c11"; - -if ($h->{code} =~ s/-lang=([^ ]+)//) { $lang = lc $1; } - -$h->{lang} = $lang; -$json = encode_json $h; - -print $sock "$json\n"; - -while (my $line = <$sock>) { print "$line"; } - +print $sock "@ARGV\n"; +while (my $line = <$sock>) { print $line; } close $sock; diff --git a/applets/compiler_vm/host/bin/vm-exec b/applets/compiler_vm/host/bin/vm-exec index de861224..4802e02c 100755 --- a/applets/compiler_vm/host/bin/vm-exec +++ b/applets/compiler_vm/host/bin/vm-exec @@ -25,29 +25,33 @@ use constant { }; my $json = join ' ', @ARGV; - -my $args = eval { decode_json $json }; +my $data = eval { decode_json $json }; if ($@) { # wasn't JSON; make structure manually - if ($json =~ s/^-lang=([^ ]+)//) { - $args = { lang => $1, code => $json }; + if ($json =~ s/^-lang=([^ ]+)\s+//) { + $data = { lang => $1, code => $json }; } else { - $args = { code => $json }; + $data = { code => $json }; } } -if (not exists $args->{code}) { +if (not exists $data->{code}) { die "Usage: $0 \n"; } # set any missing fields to default values -$args->{nick} //= 'vm'; -$args->{channel} //= 'vm'; -$args->{lang} //= 'c11'; -$args->{'vm-port'} //= SERIAL_PORT; +$data->{nick} //= 'vm'; +$data->{channel} //= 'vm'; +$data->{lang} //= 'c11'; +$data->{'vm-port'} //= SERIAL_PORT; -my $language = lc $args->{lang}; +# parse -lang option +if ($data->{code} =~ s/^-lang=([^ ]+)\s+//) { + $data->{lang} = $1; +} + +my $language = lc $data->{lang}; eval { require "Languages/$language.pm"; @@ -80,16 +84,16 @@ eval { } }; -if (not length $args->{code}) { - if (exists $args->{usage}) { - print "$args->{usage}\n"; +if (not length $data->{code}) { + if (exists $data->{usage}) { + print "$data->{usage}\n"; } else { print "Usage: cc [-lang=] [-info] [-paste] [-args \"command-line arguments\"] [compiler/language options] [-stdin ]\n"; } exit; } -my $lang = "Languages::$language"->new(%{$args}); +my $lang = "Languages::$language"->new(%$data); $lang->{local} = $ENV{CC_LOCAL};