Shell: Add the POSIX-only 'eval' builtin
This just concatenates its arguments together, and executes it as a command.
This commit is contained in:
parent
e2af20a69b
commit
21ea9cedff
Notes:
sideshowbarker
2024-07-17 02:55:44 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/21ea9cedff Pull-request: https://github.com/SerenityOS/serenity/pull/21173 Reviewed-by: https://github.com/gmta ✅
2 changed files with 26 additions and 0 deletions
|
@ -540,6 +540,31 @@ ErrorOr<int> Shell::builtin_dirs(Main::Arguments arguments)
|
|||
return 0;
|
||||
}
|
||||
|
||||
ErrorOr<int> Shell::builtin_eval(Main::Arguments arguments)
|
||||
{
|
||||
if (!m_in_posix_mode) {
|
||||
warnln("eval: This shell is not in POSIX mode");
|
||||
return 1;
|
||||
}
|
||||
|
||||
StringBuilder joined_arguments;
|
||||
for (size_t i = 1; i < arguments.strings.size(); ++i) {
|
||||
if (i != 1)
|
||||
joined_arguments.append(' ');
|
||||
joined_arguments.append(arguments.strings[i]);
|
||||
}
|
||||
|
||||
auto result = Posix::Parser { TRY(joined_arguments.to_string()) }.parse();
|
||||
if (!result)
|
||||
return 1;
|
||||
|
||||
auto value = TRY(result->run(*this));
|
||||
if (value && value->is_job())
|
||||
block_on_job(static_cast<AST::JobValue*>(value.ptr())->job());
|
||||
|
||||
return last_return_code.value_or(0);
|
||||
}
|
||||
|
||||
ErrorOr<int> Shell::builtin_exec(Main::Arguments arguments)
|
||||
{
|
||||
if (arguments.strings.size() < 2) {
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
__ENUMERATE_SHELL_BUILTIN(pwd, InAllModes) \
|
||||
__ENUMERATE_SHELL_BUILTIN(type, InAllModes) \
|
||||
__ENUMERATE_SHELL_BUILTIN(exec, InAllModes) \
|
||||
__ENUMERATE_SHELL_BUILTIN(eval, OnlyInPOSIXMode) \
|
||||
__ENUMERATE_SHELL_BUILTIN(exit, InAllModes) \
|
||||
__ENUMERATE_SHELL_BUILTIN(export, InAllModes) \
|
||||
__ENUMERATE_SHELL_BUILTIN(glob, InAllModes) \
|
||||
|
|
Loading…
Add table
Reference in a new issue