From cd0ddf27f3e3d347654475bc9081553a32b11b4f Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Mon, 14 Sep 2020 14:57:30 +0430 Subject: [PATCH] Shell: Allow builtins and functions as conditions for 'if' --- Shell/AST.cpp | 16 ++++++++-------- Shell/Shell.cpp | 9 ++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Shell/AST.cpp b/Shell/AST.cpp index b3bb5b71c1..7c4882b3e4 100644 --- a/Shell/AST.cpp +++ b/Shell/AST.cpp @@ -1209,16 +1209,16 @@ void IfCond::dump(int level) const RefPtr IfCond::run(RefPtr shell) { auto cond = m_condition->run(shell)->resolve_without_cast(shell); - ASSERT(cond->is_job()); + // The condition could be a builtin, in which case it has already run and exited. + if (cond && cond->is_job()) { + auto cond_job_value = static_cast(cond.ptr()); + auto cond_job = cond_job_value->job(); - auto cond_job_value = static_cast(cond.ptr()); - auto cond_job = cond_job_value->job(); - - shell->block_on_job(cond_job); - - if (cond_job->signaled()) - return create({}); // Exit early. + shell->block_on_job(cond_job); + if (cond_job->signaled()) + return create({}); // Exit early. + } if (shell->last_return_code == 0) { if (m_true_branch) return m_true_branch->run(shell); diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index 2c1ee0e212..a37ed43468 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -591,10 +591,9 @@ RefPtr Shell::run_command(const AST::Command& command) return nullptr; } - int retval = 0; - if (run_builtin(command, rewirings, retval)) { + if (run_builtin(command, rewirings, last_return_code)) { for (auto& next_in_chain : command.next_chain) - run_tail(next_in_chain, retval); + run_tail(next_in_chain, last_return_code); return nullptr; } @@ -610,9 +609,9 @@ RefPtr Shell::run_command(const AST::Command& command) } } - if (invoke_function(command, retval)) { + if (invoke_function(command, last_return_code)) { for (auto& next_in_chain : command.next_chain) - run_tail(next_in_chain, retval); + run_tail(next_in_chain, last_return_code); return nullptr; } }