mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-01 09:20:46 +08:00
Eliminate yet more calls to principal_parser()
In particular, remove job_t::from_job_id
This commit is contained in:
parent
f1f97b6476
commit
fba3c83ba5
|
@ -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.
|
/// 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");
|
assert(!name.empty() && "Empty name");
|
||||||
wcstring out;
|
wcstring out;
|
||||||
wcstring desc, def;
|
wcstring desc, def;
|
||||||
|
@ -184,7 +184,7 @@ static wcstring functions_def(const wcstring &name) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case event_type_t::job_exit: {
|
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);
|
if (j) append_format(out, L" --on-job-exit %d", j->pgid);
|
||||||
break;
|
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");
|
if (i != optind) streams.out.append(L"\n");
|
||||||
const wchar_t *funcname = argv[optind];
|
const wchar_t *funcname = argv[optind];
|
||||||
report_function_metadata(funcname, opts.verbose, streams, parser, true);
|
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)) {
|
if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) {
|
||||||
std::vector<highlight_spec_t> colors;
|
std::vector<highlight_spec_t> colors;
|
||||||
|
|
|
@ -188,14 +188,13 @@ int builtin_jobs(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
||||||
const job_t *j = nullptr;
|
const job_t *j = nullptr;
|
||||||
|
|
||||||
if (argv[i][0] == L'%') {
|
if (argv[i][0] == L'%') {
|
||||||
int jobId = -1;
|
int job_id = fish_wcstoi(argv[i] + 1);
|
||||||
jobId = fish_wcstoi(argv[i] + 1);
|
if (errno || job_id < -1) {
|
||||||
if (errno || jobId < -1) {
|
|
||||||
streams.err.append_format(_(L"%ls: '%ls' is not a valid job id"), cmd,
|
streams.err.append_format(_(L"%ls: '%ls' is not a valid job id"), cmd,
|
||||||
argv[i]);
|
argv[i]);
|
||||||
return STATUS_INVALID_ARGS;
|
return STATUS_INVALID_ARGS;
|
||||||
}
|
}
|
||||||
j = job_t::from_job_id(jobId);
|
j = parser.job_get(job_id);
|
||||||
} else {
|
} else {
|
||||||
int pid = fish_wcstoi(argv[i]);
|
int pid = fish_wcstoi(argv[i]);
|
||||||
if (errno || pid < 0) {
|
if (errno || pid < 0) {
|
||||||
|
|
|
@ -76,9 +76,9 @@ static int wait_for_backgrounds(parser_t &parser, bool any_flag) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool all_specified_jobs_finished(const std::vector<job_id_t> &ids) {
|
static bool all_specified_jobs_finished(const parser_t &parser, const std::vector<job_id_t> &ids) {
|
||||||
for (auto id : 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 any specified job is not completed, return false.
|
||||||
// If there are stopped jobs, they are ignored.
|
// If there are stopped jobs, they are ignored.
|
||||||
if (j->is_constructed() && !j->is_completed() && !j->is_stopped()) {
|
if (j->is_constructed() && !j->is_completed() && !j->is_stopped()) {
|
||||||
|
@ -89,9 +89,9 @@ static bool all_specified_jobs_finished(const std::vector<job_id_t> &ids) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool any_specified_jobs_finished(const std::vector<job_id_t> &ids) {
|
static bool any_specified_jobs_finished(const parser_t &parser, const std::vector<job_id_t> &ids) {
|
||||||
for (auto id : 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 any specified job is completed, return true.
|
||||||
if (j->is_constructed() && (j->is_completed() || j->is_stopped())) {
|
if (j->is_constructed() && (j->is_completed() || j->is_stopped())) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -107,8 +107,8 @@ static bool any_specified_jobs_finished(const std::vector<job_id_t> &ids) {
|
||||||
static int wait_for_backgrounds_specified(parser_t &parser, const std::vector<job_id_t> &ids,
|
static int wait_for_backgrounds_specified(parser_t &parser, const std::vector<job_id_t> &ids,
|
||||||
bool any_flag) {
|
bool any_flag) {
|
||||||
sigint_checker_t sigint;
|
sigint_checker_t sigint;
|
||||||
while ((!any_flag && !all_specified_jobs_finished(ids)) ||
|
while ((!any_flag && !all_specified_jobs_finished(parser, ids)) ||
|
||||||
(any_flag && !any_specified_jobs_finished(ids))) {
|
(any_flag && !any_specified_jobs_finished(parser, ids))) {
|
||||||
if (sigint.check()) {
|
if (sigint.check()) {
|
||||||
return 128 + SIGINT;
|
return 128 + SIGINT;
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,7 +168,7 @@ wcstring event_get_desc(const parser_t &parser, const event_t &evt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case event_type_t::job_exit: {
|
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) {
|
if (j) {
|
||||||
return format_string(_(L"exit handler for job %d, '%ls'"), j->job_id(),
|
return format_string(_(L"exit handler for job %d, '%ls'"), j->job_id(),
|
||||||
j->command_wcstr());
|
j->command_wcstr());
|
||||||
|
|
|
@ -586,6 +586,13 @@ job_t *parser_t::job_get(job_id_t id) {
|
||||||
return nullptr;
|
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 {
|
job_t *parser_t::job_get_from_pid(pid_t pid) const {
|
||||||
pid_t pgid = getpgid(pid);
|
pid_t pgid = getpgid(pid);
|
||||||
|
|
||||||
|
|
|
@ -364,6 +364,7 @@ class parser_t : public std::enable_shared_from_this<parser_t> {
|
||||||
|
|
||||||
/// Return the job with the specified job id. If id is 0 or less, return the last job used.
|
/// 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);
|
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.
|
/// Returns the job with the given pid.
|
||||||
job_t *job_get_from_pid(pid_t pid) const;
|
job_t *job_get_from_pid(pid_t pid) const;
|
||||||
|
|
|
@ -118,11 +118,6 @@ void release_job_id(job_id_t jid) {
|
||||||
consumed_job_ids->erase(where);
|
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.
|
/// Return true if all processes in the job have stopped or completed.
|
||||||
bool job_t::is_stopped() const {
|
bool job_t::is_stopped() const {
|
||||||
for (const process_ptr_t &p : processes) {
|
for (const process_ptr_t &p : processes) {
|
||||||
|
|
|
@ -513,10 +513,6 @@ class job_t {
|
||||||
|
|
||||||
/// \returns the statuses for this job.
|
/// \returns the statuses for this job.
|
||||||
statuses_t get_statuses() const;
|
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.
|
/// Whether this shell is attached to the keyboard at all.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user