diff --git a/Userland/Shell/AST.cpp b/Userland/Shell/AST.cpp index b945fd9cb8..46d3db24ec 100644 --- a/Userland/Shell/AST.cpp +++ b/Userland/Shell/AST.cpp @@ -3564,7 +3564,7 @@ Vector SpecialVariableValue::resolve_as_list(RefPtr shell) switch (m_name) { case '?': - return { resolve_slices(shell, String::number(shell->last_return_code), m_slices) }; + return { resolve_slices(shell, String::number(shell->last_return_code.value_or(0)), m_slices) }; case '$': return { resolve_slices(shell, String::number(getpid()), m_slices) }; case '*': diff --git a/Userland/Shell/Builtin.cpp b/Userland/Shell/Builtin.cpp index f6ae701c11..f301cffc1b 100644 --- a/Userland/Shell/Builtin.cpp +++ b/Userland/Shell/Builtin.cpp @@ -1059,7 +1059,7 @@ int Shell::builtin_not(int argc, const char** argv) } // In case it was a function. if (!found_a_job) - exit_code = last_return_code; + exit_code = last_return_code.value_or(0); return exit_code == 0 ? 1 : 0; } diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp index 4b129aae76..28580ab6b6 100644 --- a/Userland/Shell/Shell.cpp +++ b/Userland/Shell/Shell.cpp @@ -469,7 +469,7 @@ bool Shell::invoke_function(const AST::Command& command, int& retval) (void)function.body->run(*this); - retval = last_return_code; + retval = last_return_code.value_or(0); return true; } @@ -536,8 +536,8 @@ int Shell::run_command(StringView cmd, Optional source_position_ take_error(); - // If we end up executing nothing, let's return a failing exit code. - last_return_code = 128; + if (!last_return_code.has_value()) + last_return_code = 0; ScopedValueRollback source_position_rollback { m_source_position }; if (source_position_override.has_value()) @@ -581,7 +581,7 @@ int Shell::run_command(StringView cmd, Optional source_position_ return 1; } - return last_return_code; + return last_return_code.value_or(0); } ErrorOr> Shell::run_command(const AST::Command& command) @@ -595,7 +595,7 @@ ErrorOr> Shell::run_command(const AST::Command& command) if (command.argv.is_empty() && !command.should_immediately_execute_next) { m_global_redirections.extend(command.redirections); for (auto& next_in_chain : command.next_chain) - run_tail(command, next_in_chain, last_return_code); + run_tail(command, next_in_chain, last_return_code.value_or(0)); return nullptr; } @@ -668,9 +668,10 @@ ErrorOr> Shell::run_command(const AST::Command& command) for (auto& redirection : command.redirections) TRY(resolve_redirection(redirection)); - if (command.should_wait && run_builtin(command, rewirings, last_return_code)) { + if (int local_return_code = 0; command.should_wait && 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); + run_tail(command, next_in_chain, *last_return_code); return nullptr; } @@ -681,9 +682,10 @@ ErrorOr> Shell::run_command(const AST::Command& command) for (auto& rewiring : rewirings) TRY(Core::System::dup2(rewiring.old_fd, rewiring.new_fd)); - if (invoke_function(command, last_return_code)) { + if (int local_return_code = 0; invoke_function(command, 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); + run_tail(command, next_in_chain, *last_return_code); return nullptr; } } @@ -695,7 +697,7 @@ ErrorOr> Shell::run_command(const AST::Command& command) && command.next_chain.first().node->should_override_execution_in_current_process()) { for (auto& next_in_chain : command.next_chain) - run_tail(command, next_in_chain, last_return_code); + run_tail(command, next_in_chain, last_return_code.value_or(0)); return nullptr; } @@ -752,14 +754,14 @@ ErrorOr> Shell::run_command(const AST::Command& command) for (auto& next_in_chain : command.next_chain) run_tail(command, next_in_chain, 0); - _exit(last_return_code); + _exit(last_return_code.value_or(0)); } - if (run_builtin(command, {}, last_return_code)) - _exit(last_return_code); + if (int local_return_code = 0; run_builtin(command, {}, local_return_code)) + _exit(local_return_code); - if (invoke_function(command, last_return_code)) - _exit(last_return_code); + if (int local_return_code = 0; invoke_function(command, local_return_code)) + _exit(local_return_code); // We no longer need the jobs here. jobs.clear(); diff --git a/Userland/Shell/Shell.h b/Userland/Shell/Shell.h index e12882e6d9..91c960d2e4 100644 --- a/Userland/Shell/Shell.h +++ b/Userland/Shell/Shell.h @@ -212,7 +212,7 @@ public: char hostname[HostNameSize]; uid_t uid; - int last_return_code { 0 }; + Optional last_return_code; Vector directory_stack; CircularQueue cd_history; // FIXME: have a configurable cd history length HashMap> jobs;