diff options
-rw-r--r-- | Shell/Builtin.cpp | 38 | ||||
-rw-r--r-- | Shell/Shell.h | 3 |
2 files changed, 40 insertions, 1 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; diff --git a/Shell/Shell.h b/Shell/Shell.h index 7b4a37dce3..fc0b5258dd 100644 --- a/Shell/Shell.h +++ b/Shell/Shell.h @@ -58,7 +58,8 @@ __ENUMERATE_SHELL_BUILTIN(jobs) \ __ENUMERATE_SHELL_BUILTIN(disown) \ __ENUMERATE_SHELL_BUILTIN(fg) \ - __ENUMERATE_SHELL_BUILTIN(bg) + __ENUMERATE_SHELL_BUILTIN(bg) \ + __ENUMERATE_SHELL_BUILTIN(wait) #define ENUMERATE_SHELL_OPTIONS() \ __ENUMERATE_SHELL_OPTION(inline_exec_keep_empty_segments, false, "Keep empty segments in inline execute $(...)") \ |