1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:07:34 +00:00

Shell: Add the 'break' and 'continue' POSIX builtins

These only support n=1 for now.
This commit is contained in:
Ali Mohammad Pur 2023-04-18 16:45:11 +03:30 committed by Ali Mohammad Pur
parent 0318ac5ce4
commit 79c76d67ce
2 changed files with 83 additions and 31 deletions

View file

@ -215,6 +215,56 @@ ErrorOr<int> Shell::builtin_unalias(Main::Arguments arguments)
return failed ? 1 : 0;
}
ErrorOr<int> Shell::builtin_break(Main::Arguments arguments)
{
if (!m_in_posix_mode) {
raise_error(ShellError::EvaluatedSyntaxError, "break: Invalid use of builtin break in non-POSIX mode");
return 1;
}
unsigned count = 1;
Core::ArgsParser parser;
parser.add_positional_argument(count, "Number of loops to 'break' out of", "count", Core::ArgsParser::Required::No);
if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
return 1;
if (count != 1) {
raise_error(ShellError::EvaluatedSyntaxError, "break: count must be equal to 1 (NYI)");
return 1;
}
raise_error(ShellError::InternalControlFlowBreak, "POSIX break");
return 0;
}
ErrorOr<int> Shell::builtin_continue(Main::Arguments arguments)
{
if (!m_in_posix_mode) {
raise_error(ShellError::EvaluatedSyntaxError, "break: Invalid use of builtin continue in non-POSIX mode");
return 1;
}
unsigned count = 1;
Core::ArgsParser parser;
parser.add_positional_argument(count, "Number of loops to 'continue' out of", "count", Core::ArgsParser::Required::No);
if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
return 1;
if (count != 0) {
raise_error(ShellError::EvaluatedSyntaxError, "continue: count must be equal to 1 (NYI)");
return 1;
}
raise_error(ShellError::InternalControlFlowContinue, "POSIX continue");
return 0;
}
ErrorOr<int> Shell::builtin_bg(Main::Arguments arguments)
{
int job_id = -1;

View file

@ -54,6 +54,8 @@
__ENUMERATE_SHELL_BUILTIN(dump) \
__ENUMERATE_SHELL_BUILTIN(kill) \
__ENUMERATE_SHELL_BUILTIN(noop) \
__ENUMERATE_SHELL_BUILTIN(break) \
__ENUMERATE_SHELL_BUILTIN(continue) \
__ENUMERATE_SHELL_BUILTIN(argsparser_parse)
#define ENUMERATE_SHELL_OPTIONS() \