diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-10-26 20:30:14 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-29 11:53:01 +0100 |
commit | 71bb62d03c7d9336e1a570cbc01f84a704819008 (patch) | |
tree | 9c117fa14d7bcd2c9340420591ee2bfb62d93be1 /Shell/Builtin.cpp | |
parent | 607931268eabb002fbe7ffaa1550d09e8dac159f (diff) | |
download | serenity-71bb62d03c7d9336e1a570cbc01f84a704819008.zip |
Shell: Add the `wait' builtin
This builtin...waits...for the jobs it's given (or all the existing
jobs).
Diffstat (limited to 'Shell/Builtin.cpp')
-rw-r--r-- | Shell/Builtin.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
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<const char*> 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<char**>(argv), false)) + return 1; + + Vector<NonnullRefPtr<Job>> 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<const char*> vars; |