path_get_cdpath to accept string instead of env_var_t

Unclear why it ever needed an env_var_t. wcstring is sufficient and much
simpler.
This commit is contained in:
ridiculousfish 2018-09-30 17:38:35 -04:00
parent 16da066722
commit 6147e9ee0d
4 changed files with 12 additions and 19 deletions

View file

@ -33,33 +33,31 @@ int builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return STATUS_CMD_OK;
}
env_var_t dir_in;
wcstring dir_in;
wcstring dir;
if (argv[optind]) {
dir_in = env_var_t(wcstring(argv[optind]), 0); // unamed var
dir_in = argv[optind];
} else {
auto maybe_dir_in = env_get(L"HOME");
if (maybe_dir_in.missing_or_empty()) {
streams.err.append_format(_(L"%ls: Could not find home directory\n"), cmd);
return STATUS_CMD_ERROR;
}
dir_in = std::move(*maybe_dir_in);
dir_in = maybe_dir_in->as_string();
}
if (!path_get_cdpath(dir_in, &dir)) {
if (errno == ENOTDIR) {
streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), cmd,
dir_in.as_string().c_str());
streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), cmd, dir_in.c_str());
} else if (errno == ENOENT) {
streams.err.append_format(_(L"%ls: The directory '%ls' does not exist\n"), cmd,
dir_in.as_string().c_str());
dir_in.c_str());
} else if (errno == EROTTEN) {
streams.err.append_format(_(L"%ls: '%ls' is a rotten symlink\n"), cmd,
dir_in.as_string().c_str());
streams.err.append_format(_(L"%ls: '%ls' is a rotten symlink\n"), cmd, dir_in.c_str());
} else {
streams.err.append_format(_(L"%ls: Unknown error trying to locate directory '%ls'\n"),
cmd, dir_in.as_string().c_str());
cmd, dir_in.c_str());
}
if (!shell_is_interactive()) streams.err.append(parser.current_line());

View file

@ -350,8 +350,7 @@ bool autosuggest_validate_from_history(const history_item_t &item,
string_prefixes_string(cd_dir, L"--help") || string_prefixes_string(cd_dir, L"-h");
if (!is_help) {
wcstring path;
env_var_t dir_var(cd_dir, 0);
bool can_cd = path_get_cdpath(dir_var, &path, working_directory.c_str(), vars);
bool can_cd = path_get_cdpath(cd_dir, &path, working_directory.c_str(), vars);
if (can_cd && !paths_are_same_file(working_directory, path)) {
suggestionOK = true;
}

View file

@ -157,11 +157,10 @@ wcstring_list_t path_get_paths(const wcstring &cmd) {
return paths;
}
bool path_get_cdpath(const env_var_t &dir_var, wcstring *out, const wchar_t *wd,
bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd,
const env_vars_snapshot_t &env_vars) {
int err = ENOENT;
if (dir_var.empty()) return false;
wcstring dir = dir_var.as_string();
if (dir.empty()) return false;
if (wd) {
size_t len = wcslen(wd);
@ -229,10 +228,7 @@ bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wch
if (string_prefixes_string(L"/", exp_path) || string_prefixes_string(L"./", exp_path) ||
string_prefixes_string(L"../", exp_path) || string_suffixes_string(L"/", exp_path) ||
exp_path == L"..") {
// These paths can be implicit cd, so see if you cd to the path. Note that a single period
// cannot (that's used for sourcing files anyways).
env_var_t path_var(L"n/a", exp_path);
result = path_get_cdpath(path_var, out_path, wd, vars);
result = path_get_cdpath(exp_path, out_path, wd, vars);
}
return result;
}

View file

@ -61,7 +61,7 @@ wcstring_list_t path_get_paths(const wcstring &cmd);
/// \param vars The environment variable snapshot to use (for the CDPATH variable)
/// \return 0 if the command can not be found, the path of the command otherwise. The path should be
/// free'd with free().
bool path_get_cdpath(const env_var_t &dir, wcstring *out_or_NULL, const wchar_t *wd = NULL,
bool path_get_cdpath(const wcstring &dir, wcstring *out_or_NULL, const wchar_t *wd = NULL,
const env_vars_snapshot_t &vars = env_vars_snapshot_t::current());
/// Returns whether the path can be used for an implicit cd command; if so, also returns the path by