mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-29 10:31:14 -03:00
Eliminate env_snapshot_t::current()
These uses are better served by passing in the real environment stack, now that we have environment_t as a shared base class.
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "fallback.h" // IWYU pragma: keep
|
#include "fallback.h" // IWYU pragma: keep
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
#include "parser.h"
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
#include "wgetopt.h"
|
#include "wgetopt.h"
|
||||||
#include "wutil.h" // IWYU pragma: keep
|
#include "wutil.h" // IWYU pragma: keep
|
||||||
@@ -102,7 +103,7 @@ int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
wcstring path;
|
wcstring path;
|
||||||
if (path_get_path(command_name, &path)) {
|
if (path_get_path(command_name, &path, parser.vars())) {
|
||||||
if (!opts.quiet) streams.out.append_format(L"%ls\n", path.c_str());
|
if (!opts.quiet) streams.out.append_format(L"%ls\n", path.c_str());
|
||||||
++found;
|
++found;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -455,7 +455,6 @@ int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|||||||
auto clear_remaining_vars = [&] () {
|
auto clear_remaining_vars = [&] () {
|
||||||
while (vars_left()) {
|
while (vars_left()) {
|
||||||
parser.vars().set_empty(*var_ptr, opts.place);
|
parser.vars().set_empty(*var_ptr, opts.place);
|
||||||
// env_set_one(*var_ptr, opts.place, L"");
|
|
||||||
++var_ptr;
|
++var_ptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -495,18 +495,19 @@ void complete_remove_all(const wcstring &cmd, bool cmd_is_path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Find the full path and commandname from a command string 'str'.
|
/// Find the full path and commandname from a command string 'str'.
|
||||||
static void parse_cmd_string(const wcstring &str, wcstring &path, wcstring &cmd) {
|
static void parse_cmd_string(const wcstring &str, wcstring *path, wcstring *cmd,
|
||||||
if (!path_get_path(str, &path)) {
|
const environment_t &vars) {
|
||||||
|
if (!path_get_path(str, path, vars)) {
|
||||||
/// Use the empty string as the 'path' for commands that can not be found.
|
/// Use the empty string as the 'path' for commands that can not be found.
|
||||||
path = L"";
|
*path = L"";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the path is not included in the command.
|
// Make sure the path is not included in the command.
|
||||||
size_t last_slash = str.find_last_of(L'/');
|
size_t last_slash = str.find_last_of(L'/');
|
||||||
if (last_slash != wcstring::npos) {
|
if (last_slash != wcstring::npos) {
|
||||||
cmd = str.substr(last_slash + 1);
|
*cmd = str.substr(last_slash + 1);
|
||||||
} else {
|
} else {
|
||||||
cmd = str;
|
*cmd = str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -865,7 +866,7 @@ bool completer_t::complete_param(const wcstring &cmd_orig, const wcstring &popt,
|
|||||||
bool use_common = 1, use_files = 1;
|
bool use_common = 1, use_files = 1;
|
||||||
|
|
||||||
wcstring cmd, path;
|
wcstring cmd, path;
|
||||||
parse_cmd_string(cmd_orig, path, cmd);
|
parse_cmd_string(cmd_orig, &path, &cmd, vars);
|
||||||
|
|
||||||
// mqudsi: run_on_main_thread() already just runs `func` if we're on the main thread,
|
// mqudsi: run_on_main_thread() already just runs `func` if we're on the main thread,
|
||||||
// but it makes a kcall to get the current thread id to ascertain that. Perhaps even
|
// but it makes a kcall to get the current thread id to ascertain that. Perhaps even
|
||||||
@@ -891,8 +892,8 @@ bool completer_t::complete_param(const wcstring &cmd_orig, const wcstring &popt,
|
|||||||
// may be faster, path_get_path can potentially do a lot of FS/IO access, so env.get() +
|
// may be faster, path_get_path can potentially do a lot of FS/IO access, so env.get() +
|
||||||
// function_exists() should still be faster.
|
// function_exists() should still be faster.
|
||||||
head_exists =
|
head_exists =
|
||||||
head_exists ||
|
head_exists || path_get_path(cmd_orig, nullptr,
|
||||||
path_get_path(cmd_orig, nullptr); // use cmd_orig here as it is potentially pathed
|
vars); // use cmd_orig here as it is potentially pathed
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!head_exists) {
|
if (!head_exists) {
|
||||||
|
|||||||
12
src/env.cpp
12
src/env.cpp
@@ -1658,19 +1658,7 @@ env_vars_snapshot_t::env_vars_snapshot_t(const environment_t &source, const wcha
|
|||||||
|
|
||||||
env_vars_snapshot_t::~env_vars_snapshot_t() = default;
|
env_vars_snapshot_t::~env_vars_snapshot_t() = default;
|
||||||
|
|
||||||
// The "current" variables are not a snapshot at all, but instead trampoline to env_get, etc.
|
|
||||||
// We identify the current snapshot based on pointer values.
|
|
||||||
// This is an ugly thing that has to go away.
|
|
||||||
const env_vars_snapshot_t env_vars_snapshot_t::s_current;
|
|
||||||
const env_vars_snapshot_t &env_vars_snapshot_t::current() { return s_current; }
|
|
||||||
|
|
||||||
bool env_vars_snapshot_t::is_current() const { return this == &s_current; }
|
|
||||||
|
|
||||||
maybe_t<env_var_t> env_vars_snapshot_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
maybe_t<env_var_t> env_vars_snapshot_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
||||||
// If we represent the current state, bounce to env_get.
|
|
||||||
if (this->is_current()) {
|
|
||||||
return env_get(key, mode);
|
|
||||||
}
|
|
||||||
auto iter = vars.find(key);
|
auto iter = vars.find(key);
|
||||||
if (iter == vars.end()) return none();
|
if (iter == vars.end()) return none();
|
||||||
return iter->second;
|
return iter->second;
|
||||||
|
|||||||
@@ -255,9 +255,6 @@ class env_stack_t : public environment_t {
|
|||||||
class env_vars_snapshot_t : public environment_t {
|
class env_vars_snapshot_t : public environment_t {
|
||||||
std::map<wcstring, env_var_t> vars;
|
std::map<wcstring, env_var_t> vars;
|
||||||
wcstring_list_t names;
|
wcstring_list_t names;
|
||||||
bool is_current() const;
|
|
||||||
|
|
||||||
static const env_vars_snapshot_t s_current;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
env_vars_snapshot_t() = default;
|
env_vars_snapshot_t() = default;
|
||||||
@@ -270,9 +267,6 @@ class env_vars_snapshot_t : public environment_t {
|
|||||||
|
|
||||||
wcstring_list_t get_names(int flags) const override;
|
wcstring_list_t get_names(int flags) const override;
|
||||||
|
|
||||||
// Returns the fake snapshot representing the live variables array.
|
|
||||||
static const env_vars_snapshot_t ¤t();
|
|
||||||
|
|
||||||
// Vars necessary for highlighting.
|
// Vars necessary for highlighting.
|
||||||
static const wchar_t *const highlighting_keys[];
|
static const wchar_t *const highlighting_keys[];
|
||||||
|
|
||||||
|
|||||||
@@ -529,7 +529,7 @@ int main(int argc, char *argv[]) {
|
|||||||
std::vector<highlight_spec_t> colors;
|
std::vector<highlight_spec_t> colors;
|
||||||
if (output_type != output_type_plain_text) {
|
if (output_type != output_type_plain_text) {
|
||||||
highlight_shell_no_io(output_wtext, colors, output_wtext.size(), NULL,
|
highlight_shell_no_io(output_wtext, colors, output_wtext.size(), NULL,
|
||||||
env_vars_snapshot_t::current());
|
env_stack_t::globals());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string colored_output;
|
std::string colored_output;
|
||||||
|
|||||||
@@ -364,7 +364,7 @@ bool autosuggest_validate_from_history(const history_item_t &item,
|
|||||||
|
|
||||||
// Not handled specially so handle it here.
|
// Not handled specially so handle it here.
|
||||||
bool cmd_ok = false;
|
bool cmd_ok = false;
|
||||||
if (path_get_path(parsed_command, NULL)) {
|
if (path_get_path(parsed_command, NULL, vars)) {
|
||||||
cmd_ok = true;
|
cmd_ok = true;
|
||||||
} else if (builtin_exists(parsed_command) ||
|
} else if (builtin_exists(parsed_command) ||
|
||||||
function_exists_no_autoload(parsed_command, vars)) {
|
function_exists_no_autoload(parsed_command, vars)) {
|
||||||
|
|||||||
@@ -800,7 +800,7 @@ parse_execution_result_t parse_execution_context_t::populate_plain_process(
|
|||||||
wcstring path_to_external_command;
|
wcstring path_to_external_command;
|
||||||
if (process_type == EXTERNAL || process_type == INTERNAL_EXEC) {
|
if (process_type == EXTERNAL || process_type == INTERNAL_EXEC) {
|
||||||
// Determine the actual command. This may be an implicit cd.
|
// Determine the actual command. This may be an implicit cd.
|
||||||
bool has_command = path_get_path(cmd, &path_to_external_command);
|
bool has_command = path_get_path(cmd, &path_to_external_command, parser->vars());
|
||||||
|
|
||||||
// If there was no command, then we care about the value of errno after checking for it, to
|
// If there was no command, then we care about the value of errno after checking for it, to
|
||||||
// distinguish between e.g. no file vs permissions problem.
|
// distinguish between e.g. no file vs permissions problem.
|
||||||
|
|||||||
@@ -34,13 +34,12 @@ bool path_get_data(wcstring &path);
|
|||||||
/// Args:
|
/// Args:
|
||||||
/// cmd - The name of the executable.
|
/// cmd - The name of the executable.
|
||||||
/// output_or_NULL - If non-NULL, store the full path.
|
/// output_or_NULL - If non-NULL, store the full path.
|
||||||
/// vars - The environment variables snapshot to use
|
/// vars - The environment variables to use
|
||||||
///
|
///
|
||||||
/// Returns:
|
/// Returns:
|
||||||
/// false if the command can not be found else true. The result
|
/// false if the command can not be found else true. The result
|
||||||
/// should be freed with free().
|
/// should be freed with free().
|
||||||
bool path_get_path(const wcstring &cmd, wcstring *output_or_NULL,
|
bool path_get_path(const wcstring &cmd, wcstring *output_or_NULL, const environment_t &vars);
|
||||||
const environment_t &vars = env_vars_snapshot_t::current());
|
|
||||||
|
|
||||||
/// Return all the paths that match the given command.
|
/// Return all the paths that match the given command.
|
||||||
wcstring_list_t path_get_paths(const wcstring &cmd);
|
wcstring_list_t path_get_paths(const wcstring &cmd);
|
||||||
|
|||||||
Reference in New Issue
Block a user