1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-24 01:05:08 +00:00

Shell: Convert builtins to use the modern main() style

That is, return ErrorOr<int>, handle fallible ops with TRY() and accept
a Main::Arguments.
Note that we do not populate the argc/argv members of Main::Arguments,
so all accesses have to go through .strings.
This commit is contained in:
Ali Mohammad Pur 2023-02-18 12:30:17 +03:30 committed by Ali Mohammad Pur
parent 6e5ba82929
commit 007767fc14
3 changed files with 175 additions and 148 deletions

View file

@ -700,7 +700,7 @@ ErrorOr<RefPtr<Job>> Shell::run_command(const AST::Command& command)
for (auto& redirection : command.redirections)
TRY(resolve_redirection(redirection));
if (int local_return_code = 0; command.should_wait && run_builtin(command, rewirings, local_return_code)) {
if (int local_return_code = 0; command.should_wait && TRY(run_builtin(command, rewirings, local_return_code))) {
last_return_code = local_return_code;
for (auto& next_in_chain : command.next_chain)
run_tail(command, next_in_chain, *last_return_code);
@ -791,7 +791,7 @@ ErrorOr<RefPtr<Job>> Shell::run_command(const AST::Command& command)
_exit(last_return_code.value_or(0));
}
if (int local_return_code = 0; run_builtin(command, {}, local_return_code))
if (int local_return_code = 0; TRY(run_builtin(command, {}, local_return_code)))
_exit(local_return_code);
if (int local_return_code = 0; invoke_function(command, local_return_code))
@ -877,6 +877,24 @@ ErrorOr<RefPtr<Job>> Shell::run_command(const AST::Command& command)
return *job;
}
ErrorOr<void> Shell::execute_process(Span<StringView> argv)
{
Vector<DeprecatedString> strings;
Vector<char const*> args;
TRY(strings.try_ensure_capacity(argv.size()));
TRY(args.try_ensure_capacity(argv.size() + 1));
for (auto& entry : argv) {
strings.unchecked_append(entry);
args.unchecked_append(strings.last().characters());
}
args.append(nullptr);
// NOTE: noreturn.
execute_process(move(args));
}
void Shell::execute_process(Vector<char const*>&& argv)
{
for (auto& promise : m_active_promises) {