Eliminate more calls to principal_parser()

Require a parser to get a job from its pgid.
This commit is contained in:
ridiculousfish 2020-02-08 12:39:03 -08:00
parent d957f6b302
commit f1f97b6476
8 changed files with 13 additions and 21 deletions

View File

@ -90,7 +90,7 @@ int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
// Background all existing jobs that match the pids. // Background all existing jobs that match the pids.
// Non-existent jobs aren't an error, but information about them is useful. // Non-existent jobs aren't an error, but information about them is useful.
for (auto p : pids) { for (auto p : pids) {
if (job_t *j = job_t::from_pid(p)) { if (job_t *j = parser.job_get_from_pid(p)) {
retval |= send_to_bg(parser, streams, j); retval |= send_to_bg(parser, streams, j);
} else { } else {
streams.err.append_format(_(L"%ls: Could not find job '%d'\n"), cmd, p); streams.err.append_format(_(L"%ls: Could not find job '%d'\n"), cmd, p);

View File

@ -56,7 +56,7 @@ int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
bool found_job = false; bool found_job = false;
int pid = fish_wcstoi(argv[optind]); int pid = fish_wcstoi(argv[optind]);
if (errno == 0 && pid > 0) { if (errno == 0 && pid > 0) {
found_job = (job_t::from_pid(pid) != nullptr); found_job = (parser.job_get_from_pid(pid) != nullptr);
} }
if (found_job) { if (found_job) {
@ -73,7 +73,7 @@ int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
streams.err.append_format(BUILTIN_ERR_NOT_NUMBER, cmd, argv[optind]); streams.err.append_format(BUILTIN_ERR_NOT_NUMBER, cmd, argv[optind]);
builtin_print_error_trailer(parser, streams.err, cmd); builtin_print_error_trailer(parser, streams.err, cmd);
} else { } else {
job = job_t::from_pid(pid); job = parser.job_get_from_pid(pid);
if (!job || !job->is_constructed() || job->is_completed()) { if (!job || !job->is_constructed() || job->is_completed()) {
streams.err.append_format(_(L"%ls: No suitable job: %d\n"), cmd, pid); streams.err.append_format(_(L"%ls: No suitable job: %d\n"), cmd, pid);
job = nullptr; job = nullptr;

View File

@ -203,7 +203,7 @@ int builtin_jobs(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
argv[i]); argv[i]);
return STATUS_INVALID_ARGS; return STATUS_INVALID_ARGS;
} }
j = job_t::from_pid(pid); j = parser.job_get_from_pid(pid);
} }
if (j && !j->is_completed() && j->is_constructed()) { if (j && !j->is_completed() && j->is_constructed()) {

View File

@ -138,7 +138,7 @@ static int event_is_blocked(parser_t &parser, const event_t &e) {
return event_block_list_blocks_type(parser.global_event_blocks); return event_block_list_blocks_type(parser.global_event_blocks);
} }
wcstring event_get_desc(const event_t &evt) { wcstring event_get_desc(const parser_t &parser, const event_t &evt) {
const event_description_t &ed = evt.desc; const event_description_t &ed = evt.desc;
switch (ed.type) { switch (ed.type) {
case event_type_t::signal: { case event_type_t::signal: {
@ -155,7 +155,7 @@ wcstring event_get_desc(const event_t &evt) {
return format_string(_(L"exit handler for process %d"), ed.param1.pid); return format_string(_(L"exit handler for process %d"), ed.param1.pid);
} else { } else {
// In events, PGIDs are stored as negative PIDs // In events, PGIDs are stored as negative PIDs
job_t *j = job_t::from_pid(-ed.param1.pid); job_t *j = parser.job_get_from_pid(-ed.param1.pid);
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());

View File

@ -118,7 +118,7 @@ void event_enqueue_signal(int signal);
void event_print(io_streams_t &streams, maybe_t<event_type_t> type_filter); void event_print(io_streams_t &streams, maybe_t<event_type_t> type_filter);
/// Returns a string describing the specified event. /// Returns a string describing the specified event.
wcstring event_get_desc(const event_t &e); wcstring event_get_desc(const parser_t &parser, const event_t &e);
/// Fire a generic event with the specified name. /// Fire a generic event with the specified name.
void event_fire_generic(parser_t &parser, const wchar_t *name, void event_fire_generic(parser_t &parser, const wchar_t *name,

View File

@ -340,8 +340,8 @@ operation_context_t parser_t::context() {
} }
/// Append stack trace info for the block \p b to \p trace. /// Append stack trace info for the block \p b to \p trace.
static void append_block_description_to_stack_trace(const block_t &b, wcstring &trace, static void append_block_description_to_stack_trace(const parser_t &parser, const block_t &b,
const environment_t &vars) { wcstring &trace) {
bool print_call_site = false; bool print_call_site = false;
switch (b.type()) { switch (b.type()) {
case block_type_t::function_call: case block_type_t::function_call:
@ -375,13 +375,13 @@ static void append_block_description_to_stack_trace(const block_t &b, wcstring &
case block_type_t::source: { case block_type_t::source: {
const wchar_t *source_dest = b.sourced_file; const wchar_t *source_dest = b.sourced_file;
append_format(trace, _(L"from sourcing file %ls\n"), append_format(trace, _(L"from sourcing file %ls\n"),
user_presentable_path(source_dest, vars).c_str()); user_presentable_path(source_dest, parser.vars()).c_str());
print_call_site = true; print_call_site = true;
break; break;
} }
case block_type_t::event: { case block_type_t::event: {
assert(b.event && "Should have an event"); assert(b.event && "Should have an event");
wcstring description = event_get_desc(*b.event); wcstring description = event_get_desc(parser, *b.event);
append_format(trace, _(L"in event handler: %ls\n"), description.c_str()); append_format(trace, _(L"in event handler: %ls\n"), description.c_str());
print_call_site = true; print_call_site = true;
break; break;
@ -403,7 +403,7 @@ static void append_block_description_to_stack_trace(const block_t &b, wcstring &
const wchar_t *file = b.src_filename; const wchar_t *file = b.src_filename;
if (file) { if (file) {
append_format(trace, _(L"\tcalled on line %d of file %ls\n"), b.src_lineno, append_format(trace, _(L"\tcalled on line %d of file %ls\n"), b.src_lineno,
user_presentable_path(file, vars).c_str()); user_presentable_path(file, parser.vars()).c_str());
} else if (is_within_fish_initialization()) { } else if (is_within_fish_initialization()) {
append_format(trace, _(L"\tcalled during startup\n")); append_format(trace, _(L"\tcalled during startup\n"));
} }
@ -413,7 +413,7 @@ static void append_block_description_to_stack_trace(const block_t &b, wcstring &
wcstring parser_t::stack_trace() const { wcstring parser_t::stack_trace() const {
wcstring trace; wcstring trace;
for (const auto &b : blocks()) { for (const auto &b : blocks()) {
append_block_description_to_stack_trace(b, trace, vars()); append_block_description_to_stack_trace(*this, b, trace);
// Stop at event handler. No reason to believe that any other code is relevant. // Stop at event handler. No reason to believe that any other code is relevant.
// //

View File

@ -123,11 +123,6 @@ job_t *job_t::from_job_id(job_id_t id) {
return parser_t::principal_parser().job_get(id); return parser_t::principal_parser().job_get(id);
} }
job_t *job_t::from_pid(pid_t pid) {
ASSERT_IS_MAIN_THREAD();
return parser_t::principal_parser().job_get_from_pid(pid);
}
/// 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) {

View File

@ -517,9 +517,6 @@ class job_t {
/// Return the job instance matching this unique job id. /// Return the job instance matching this unique job id.
/// If id is 0 or less, return the last job used. /// If id is 0 or less, return the last job used.
static job_t *from_job_id(job_id_t id); static job_t *from_job_id(job_id_t id);
/// Return the job containing the process identified by the unique pid provided.
static job_t *from_pid(pid_t pid);
}; };
/// Whether this shell is attached to the keyboard at all. /// Whether this shell is attached to the keyboard at all.