diff --git a/src/builtin_functions.cpp b/src/builtin_functions.cpp index 951a1d5b0..2c4377494 100644 --- a/src/builtin_functions.cpp +++ b/src/builtin_functions.cpp @@ -130,7 +130,7 @@ static int parse_cmd_opts(functions_cmd_opts_t &opts, int *optind, //!OCLINT(hi } /// Return a definition of the specified function. Used by the functions builtin. -static wcstring functions_def(const wcstring &name) { +static wcstring functions_def(const parser_t &parser, const wcstring &name) { assert(!name.empty() && "Empty name"); wcstring out; wcstring desc, def; @@ -184,7 +184,7 @@ static wcstring functions_def(const wcstring &name) { break; } case event_type_t::job_exit: { - const job_t *j = job_t::from_job_id(d.param1.job_id); + const job_t *j = parser.job_get(d.param1.job_id); if (j) append_format(out, L" --on-job-exit %d", j->pgid); break; } @@ -435,7 +435,7 @@ int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) { if (i != optind) streams.out.append(L"\n"); const wchar_t *funcname = argv[optind]; report_function_metadata(funcname, opts.verbose, streams, parser, true); - wcstring def = functions_def(funcname); + wcstring def = functions_def(parser, funcname); if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) { std::vector colors; diff --git a/src/builtin_jobs.cpp b/src/builtin_jobs.cpp index 254e40667..e9f292930 100644 --- a/src/builtin_jobs.cpp +++ b/src/builtin_jobs.cpp @@ -188,14 +188,13 @@ int builtin_jobs(parser_t &parser, io_streams_t &streams, wchar_t **argv) { const job_t *j = nullptr; if (argv[i][0] == L'%') { - int jobId = -1; - jobId = fish_wcstoi(argv[i] + 1); - if (errno || jobId < -1) { + int job_id = fish_wcstoi(argv[i] + 1); + if (errno || job_id < -1) { streams.err.append_format(_(L"%ls: '%ls' is not a valid job id"), cmd, argv[i]); return STATUS_INVALID_ARGS; } - j = job_t::from_job_id(jobId); + j = parser.job_get(job_id); } else { int pid = fish_wcstoi(argv[i]); if (errno || pid < 0) { diff --git a/src/builtin_wait.cpp b/src/builtin_wait.cpp index 3d21d04f8..861c92788 100644 --- a/src/builtin_wait.cpp +++ b/src/builtin_wait.cpp @@ -76,9 +76,9 @@ static int wait_for_backgrounds(parser_t &parser, bool any_flag) { return 0; } -static bool all_specified_jobs_finished(const std::vector &ids) { +static bool all_specified_jobs_finished(const parser_t &parser, const std::vector &ids) { for (auto id : ids) { - if (job_t *j = job_t::from_job_id(id)) { + if (const job_t *j = parser.job_get(id)) { // If any specified job is not completed, return false. // If there are stopped jobs, they are ignored. if (j->is_constructed() && !j->is_completed() && !j->is_stopped()) { @@ -89,9 +89,9 @@ static bool all_specified_jobs_finished(const std::vector &ids) { return true; } -static bool any_specified_jobs_finished(const std::vector &ids) { +static bool any_specified_jobs_finished(const parser_t &parser, const std::vector &ids) { for (auto id : ids) { - if (job_t *j = job_t::from_job_id(id)) { + if (const job_t *j = parser.job_get(id)) { // If any specified job is completed, return true. if (j->is_constructed() && (j->is_completed() || j->is_stopped())) { return true; @@ -107,8 +107,8 @@ static bool any_specified_jobs_finished(const std::vector &ids) { static int wait_for_backgrounds_specified(parser_t &parser, const std::vector &ids, bool any_flag) { sigint_checker_t sigint; - while ((!any_flag && !all_specified_jobs_finished(ids)) || - (any_flag && !any_specified_jobs_finished(ids))) { + while ((!any_flag && !all_specified_jobs_finished(parser, ids)) || + (any_flag && !any_specified_jobs_finished(parser, ids))) { if (sigint.check()) { return 128 + SIGINT; } diff --git a/src/event.cpp b/src/event.cpp index 4dd17567a..28a234b80 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -168,7 +168,7 @@ wcstring event_get_desc(const parser_t &parser, const event_t &evt) { } case event_type_t::job_exit: { - job_t *j = job_t::from_job_id(ed.param1.job_id); + const job_t *j = parser.job_get(ed.param1.job_id); if (j) { return format_string(_(L"exit handler for job %d, '%ls'"), j->job_id(), j->command_wcstr()); diff --git a/src/parser.cpp b/src/parser.cpp index 84601264e..320fa8649 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -586,6 +586,13 @@ job_t *parser_t::job_get(job_id_t id) { return nullptr; } +const job_t *parser_t::job_get(job_id_t id) const { + for (const auto &job : job_list) { + if (id <= 0 || job->job_id() == id) return job.get(); + } + return nullptr; +} + job_t *parser_t::job_get_from_pid(pid_t pid) const { pid_t pgid = getpgid(pid); diff --git a/src/parser.h b/src/parser.h index 2cb261235..425144df8 100644 --- a/src/parser.h +++ b/src/parser.h @@ -364,6 +364,7 @@ class parser_t : public std::enable_shared_from_this { /// Return the job with the specified job id. If id is 0 or less, return the last job used. job_t *job_get(job_id_t job_id); + const job_t *job_get(job_id_t job_id) const; /// Returns the job with the given pid. job_t *job_get_from_pid(pid_t pid) const; diff --git a/src/proc.cpp b/src/proc.cpp index bc3169553..30d079d4f 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -118,11 +118,6 @@ void release_job_id(job_id_t jid) { consumed_job_ids->erase(where); } -job_t *job_t::from_job_id(job_id_t id) { - ASSERT_IS_MAIN_THREAD(); - return parser_t::principal_parser().job_get(id); -} - /// Return true if all processes in the job have stopped or completed. bool job_t::is_stopped() const { for (const process_ptr_t &p : processes) { diff --git a/src/proc.h b/src/proc.h index b18ac5d52..930074984 100644 --- a/src/proc.h +++ b/src/proc.h @@ -513,10 +513,6 @@ class job_t { /// \returns the statuses for this job. statuses_t get_statuses() const; - - /// Return the job instance matching this unique job id. - /// If id is 0 or less, return the last job used. - static job_t *from_job_id(job_id_t id); }; /// Whether this shell is attached to the keyboard at all.