From 71bb62d03c7d9336e1a570cbc01f84a704819008 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Mon, 26 Oct 2020 20:30:14 +0330 Subject: Shell: Add the `wait' builtin This builtin...waits...for the jobs it's given (or all the existing jobs). --- Shell/Builtin.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'Shell/Builtin.cpp') diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp index 44a1d8852e..5194d6ace4 100644 --- a/Shell/Builtin.cpp +++ b/Shell/Builtin.cpp @@ -743,6 +743,44 @@ int Shell::builtin_umask(int argc, const char** argv) return 1; } +int Shell::builtin_wait(int argc, const char** argv) +{ + Vector job_ids; + + Core::ArgsParser parser; + parser.add_positional_argument(job_ids, "Job IDs to wait for, defaults to all jobs if missing", "jobs", Core::ArgsParser::Required::No); + + if (!parser.parse(argc, const_cast(argv), false)) + return 1; + + Vector> jobs_to_wait_for; + + if (job_ids.is_empty()) { + for (auto it : jobs) + jobs_to_wait_for.append(it.value); + } else { + for (String id_s : job_ids) { + auto id_opt = id_s.to_uint(); + if (id_opt.has_value()) { + if (auto job = find_job(id_opt.value())) { + jobs_to_wait_for.append(*job); + continue; + } + } + + warnln("wait: invalid or nonexistent job id {}", id_s); + return 1; + } + } + + for (auto& job : jobs_to_wait_for) { + job->set_running_in_background(false); + block_on_job(job); + } + + return 0; +} + int Shell::builtin_unset(int argc, const char** argv) { Vector vars; -- cgit v1.2.3