3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-05 19:49:32 +01:00

pbot-vm: improve exit-code handling

This commit is contained in:
Pragmatic Software 2022-03-06 13:51:33 -08:00
parent 164ecc45a7
commit 3e2204a6b0
3 changed files with 93 additions and 87 deletions

View File

@ -441,8 +441,6 @@ sub cmd_print_last_statement($context, $args) {
} }
sub handle_program_exit($context, $data) { sub handle_program_exit($context, $data) {
my $reason = $data->{reason};
if (not -s OUTPUT_FILENAME) { # -s gets size of file if (not -s OUTPUT_FILENAME) { # -s gets size of file
my $locals = locals_to_string($context->{locals_end}); my $locals = locals_to_string($context->{locals_end});
@ -451,6 +449,10 @@ sub handle_program_exit($context, $data) {
} }
} }
if (exists $data->{'exit-code'} && $data->{'exit-code'} != 0) {
$context->{'exit-code'} = oct $data->{'exit-code'};
}
_exit($context); _exit($context);
} }
@ -541,7 +543,7 @@ sub handle_exec_async_output($context, $output) {
if ($reason eq 'breakpoint-hit') { if ($reason eq 'breakpoint-hit') {
handle_breakpoint_hit($context, $output); handle_breakpoint_hit($context, $output);
} }
elsif ($reason eq 'exited-normally') { elsif ($reason eq 'exited-normally' || $reason eq 'exited') {
handle_program_exit($context, $output); handle_program_exit($context, $output);
} }
elsif ($reason eq 'signal-received') { elsif ($reason eq 'signal-received') {
@ -592,9 +594,9 @@ sub _exit($context) {
my $output = do { local $/; <$fh> }; my $output = do { local $/; <$fh> };
close $fh; close $fh;
print STDOUT "$output\n"; print STDOUT "$output";
exit; exit ($context->{'exit-code'} // 0);
} }
# send text to OUTPUT_FILENAME file # send text to OUTPUT_FILENAME file

View File

@ -150,9 +150,11 @@ sub run_command($command, $mod) {
if (not $mod->{error}) { if (not $mod->{error}) {
$mod->{output} .= "Success (no output).\n"; $mod->{output} .= "Success (no output).\n";
} else { } else {
$mod->{output} .= "Exit code $mod->{error}.\n"; $mod->{output} .= "Exit $mod->{error}.\n";
} }
} }
} elsif ($mod->{error}) {
$mod->{output} .= " [Exit $mod->{error}]";
} }
return $mod->{output}; return $mod->{output};

View File

@ -97,6 +97,8 @@ sub postprocess {
($exitval, $stdout, $stderr) = $self->execute(60, $input, '/bin/sh'); ($exitval, $stdout, $stderr) = $self->execute(60, $input, '/bin/sh');
} }
$self->{error} = $exitval;
my $result = $stderr; my $result = $stderr;
$result .= ' ' if length $result; $result .= ' ' if length $result;
$result .= $stdout; $result .= $stdout;