mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 15:07:45 +00:00
Shell: Implement the return
POSIX builtin
This is also available in the regular shell mode, as it's a generally useful builtin to have.
This commit is contained in:
parent
2d1c5dbfcb
commit
a8c7448ccb
4 changed files with 25 additions and 1 deletions
|
@ -1230,7 +1230,7 @@ ErrorOr<RefPtr<Value>> ForLoop::run(RefPtr<Shell> shell)
|
||||||
|
|
||||||
size_t consecutive_interruptions = 0;
|
size_t consecutive_interruptions = 0;
|
||||||
auto run = [&](auto& block_value) {
|
auto run = [&](auto& block_value) {
|
||||||
if (shell->has_error(Shell::ShellError::InternalControlFlowBreak)) {
|
if (shell->has_error(Shell::ShellError::InternalControlFlowBreak) || shell->has_error(Shell::ShellError::InternalControlFlowReturn)) {
|
||||||
shell->take_error();
|
shell->take_error();
|
||||||
return IterationDecision::Break;
|
return IterationDecision::Break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -268,6 +268,23 @@ ErrorOr<int> Shell::builtin_continue(Main::Arguments arguments)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<int> Shell::builtin_return(Main::Arguments arguments)
|
||||||
|
{
|
||||||
|
int return_code = last_return_code.value_or(0);
|
||||||
|
|
||||||
|
Core::ArgsParser parser;
|
||||||
|
parser.add_positional_argument(return_code, "Return code to return to the parent shell", "return-code", Core::ArgsParser::Required::No);
|
||||||
|
parser.set_general_help("Return from a function or source file");
|
||||||
|
|
||||||
|
if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
last_return_code = return_code & 0xff;
|
||||||
|
raise_error(ShellError::InternalControlFlowReturn, "POSIX return");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<int> Shell::builtin_bg(Main::Arguments arguments)
|
ErrorOr<int> Shell::builtin_bg(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
int job_id = -1;
|
int job_id = -1;
|
||||||
|
|
|
@ -487,6 +487,9 @@ bool Shell::invoke_function(const AST::Command& command, int& retval)
|
||||||
|
|
||||||
(void)function.body->run(*this);
|
(void)function.body->run(*this);
|
||||||
|
|
||||||
|
if (has_error(ShellError::InternalControlFlowReturn))
|
||||||
|
take_error();
|
||||||
|
|
||||||
retval = last_return_code.value_or(0);
|
retval = last_return_code.value_or(0);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -2435,6 +2438,7 @@ void Shell::possibly_print_error() const
|
||||||
break;
|
break;
|
||||||
case ShellError::InternalControlFlowBreak:
|
case ShellError::InternalControlFlowBreak:
|
||||||
case ShellError::InternalControlFlowContinue:
|
case ShellError::InternalControlFlowContinue:
|
||||||
|
case ShellError::InternalControlFlowReturn:
|
||||||
case ShellError::InternalControlFlowInterrupted:
|
case ShellError::InternalControlFlowInterrupted:
|
||||||
case ShellError::InternalControlFlowKilled:
|
case ShellError::InternalControlFlowKilled:
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -60,6 +60,7 @@
|
||||||
__ENUMERATE_SHELL_BUILTIN(noop, InAllModes) \
|
__ENUMERATE_SHELL_BUILTIN(noop, InAllModes) \
|
||||||
__ENUMERATE_SHELL_BUILTIN(break, OnlyInPOSIXMode) \
|
__ENUMERATE_SHELL_BUILTIN(break, OnlyInPOSIXMode) \
|
||||||
__ENUMERATE_SHELL_BUILTIN(continue, OnlyInPOSIXMode) \
|
__ENUMERATE_SHELL_BUILTIN(continue, OnlyInPOSIXMode) \
|
||||||
|
__ENUMERATE_SHELL_BUILTIN(return, InAllModes) \
|
||||||
__ENUMERATE_SHELL_BUILTIN(read, OnlyInPOSIXMode) \
|
__ENUMERATE_SHELL_BUILTIN(read, OnlyInPOSIXMode) \
|
||||||
__ENUMERATE_SHELL_BUILTIN(run_with_env, OnlyInPOSIXMode) \
|
__ENUMERATE_SHELL_BUILTIN(run_with_env, OnlyInPOSIXMode) \
|
||||||
__ENUMERATE_SHELL_BUILTIN(argsparser_parse, InAllModes) \
|
__ENUMERATE_SHELL_BUILTIN(argsparser_parse, InAllModes) \
|
||||||
|
@ -360,6 +361,7 @@ public:
|
||||||
None,
|
None,
|
||||||
InternalControlFlowBreak,
|
InternalControlFlowBreak,
|
||||||
InternalControlFlowContinue,
|
InternalControlFlowContinue,
|
||||||
|
InternalControlFlowReturn,
|
||||||
InternalControlFlowInterrupted,
|
InternalControlFlowInterrupted,
|
||||||
InternalControlFlowKilled,
|
InternalControlFlowKilled,
|
||||||
EvaluatedSyntaxError,
|
EvaluatedSyntaxError,
|
||||||
|
@ -396,6 +398,7 @@ public:
|
||||||
switch (error) {
|
switch (error) {
|
||||||
case ShellError::InternalControlFlowBreak:
|
case ShellError::InternalControlFlowBreak:
|
||||||
case ShellError::InternalControlFlowContinue:
|
case ShellError::InternalControlFlowContinue:
|
||||||
|
case ShellError::InternalControlFlowReturn:
|
||||||
case ShellError::InternalControlFlowInterrupted:
|
case ShellError::InternalControlFlowInterrupted:
|
||||||
case ShellError::InternalControlFlowKilled:
|
case ShellError::InternalControlFlowKilled:
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue