add function: system

This commit is contained in:
Pekka Laiho 2020-12-21 09:36:49 +07:00
parent c39064a682
commit c9a095a857

View File

@ -19,6 +19,7 @@ use MadLisp\Reader;
use MadLisp\Symbol; use MadLisp\Symbol;
use MadLisp\Tokenizer; use MadLisp\Tokenizer;
use MadLisp\UserFunc; use MadLisp\UserFunc;
use MadLisp\Vector;
class Core implements ILib class Core implements ILib
{ {
@ -113,6 +114,24 @@ class Core implements ILib
)); ));
} }
if (!$this->safemode) {
$env->set('system', new CoreFunc('system', 'Execute a command on the system.', 1, 1,
function (string $command) {
// Use passthru to capture the raw output
ob_start();
passthru($command, $status);
$output = ob_get_contents();
ob_end_clean();
return new Vector([
$status,
$output
]);
}
));
}
$env->set('throw', new CoreFunc('throw', 'Throw an exception. Takes one argument which is passed to catch.', 1, 1, $env->set('throw', new CoreFunc('throw', 'Throw an exception. Takes one argument which is passed to catch.', 1, 1,
function ($error) { function ($error) {
throw new MadLispUserException($error); throw new MadLispUserException($error);