diff --git a/Shell/Job.h b/Shell/Job.h index 5dd53cc4ac8..394feef7b6d 100644 --- a/Shell/Job.h +++ b/Shell/Job.h @@ -76,6 +76,7 @@ public: bool should_be_disowned() const { return m_should_be_disowned; } void disown() { m_should_be_disowned = true; } bool is_running_in_background() const { return m_running_in_background; } + bool should_announce_exit() const { return m_should_announce_exit; } bool is_suspended() const { return m_is_suspended; } void unblock() const { @@ -113,6 +114,8 @@ public: m_running_in_background = running_in_background; } + void set_should_announce_exit(bool value) { m_should_announce_exit = value; } + void deactivate() const { m_active = false; } enum class PrintStatusMode { @@ -143,6 +146,7 @@ private: String m_cmd; bool m_exited { false }; bool m_running_in_background { false }; + bool m_should_announce_exit { false }; int m_exit_code { -1 }; int m_term_sig { -1 }; Core::ElapsedTimer m_command_timer; diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index a1aaed521b8..c02b4c833f5 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -626,7 +626,7 @@ RefPtr Shell::run_command(const AST::Command& command) job->on_exit = [](auto job) { if (!job->exited()) return; - if (job->is_running_in_background()) + if (job->is_running_in_background() && job->should_announce_exit()) fprintf(stderr, "Shell: Job %" PRIu64 "(%s) exited\n", job->job_id(), job->cmd().characters()); job->disown(); }; @@ -670,10 +670,10 @@ NonnullRefPtrVector Shell::run_commands(Vector& commands) jobs_to_wait_for.append(*job); } else { if (command.is_pipe_source) { + job->set_running_in_background(true); jobs_to_wait_for.append(*job); } else if (command.should_notify_if_in_background) { - job->set_running_in_background(true); - restore_ios(); + job->set_should_announce_exit(true); } } } @@ -710,23 +710,26 @@ void Shell::block_on_job(RefPtr job) if (!job) return; + ScopeGuard io_restorer { [&]() { + if (job->exited() && !job->is_running_in_background()) { + restore_ios(); + } + } }; + Core::EventLoop loop; job->on_exit = [&, old_exit = move(job->on_exit)](auto job) { if (old_exit) old_exit(job); loop.quit(0); }; - if (job->exited()) { - restore_ios(); + + if (job->exited()) return; - } loop.exec(); if (job->is_suspended()) job->print_status(Job::PrintStatusMode::Basic); - - restore_ios(); } String Shell::get_history_path()