path: drop path_get_paths_ffi

f77dc24 provides the pieces to call path_get_paths directly from Rust
code. Drop the C++ implementation and its FFI.
This commit is contained in:
David Adam 2023-06-21 21:39:45 +08:00
parent 1f67bcbb39
commit 14cfd268d8
5 changed files with 5 additions and 52 deletions

View file

@ -5,9 +5,8 @@ use crate::builtins::shared::{
STATUS_CMD_OK, STATUS_CMD_UNKNOWN, STATUS_INVALID_ARGS,
};
use crate::ffi::parser_t;
use crate::ffi::path_get_paths_ffi;
use crate::path::path_get_paths;
use crate::wchar::{wstr, WString, L};
use crate::wchar_ffi::{WCharFromFFI, WCharToFFI};
use crate::wgetopt::{wgetopter_t, wopt, woption, woption_argument_t};
use crate::wutil::sprintf;
@ -75,7 +74,7 @@ pub fn r#command(
// TODO: This always gets all paths, and then skips a bunch.
// For the common case, we want to get just the one path.
// Port this over once path.cpp is.
let paths: Vec<WString> = path_get_paths_ffi(&arg.to_ffi(), parser).from_ffi();
let paths: Vec<WString> = path_get_paths(arg, &*parser.get_vars());
for path in paths.iter() {
res = true;

View file

@ -12,8 +12,9 @@ use crate::ffi::{
builtin_exists, colorize_shell, function_get_annotated_definition,
function_get_copy_definition_file, function_get_copy_definition_lineno,
function_get_definition_file, function_get_definition_lineno, function_get_props_autoload,
function_is_copy, path_get_paths_ffi,
function_is_copy,
};
use crate::path::path_get_paths;
use crate::wchar::{wstr, WString, L};
use crate::wchar_ffi::WCharFromFFI;
use crate::wchar_ffi::WCharToFFI;
@ -189,7 +190,7 @@ pub fn r#type(
}
}
let paths: Vec<WString> = path_get_paths_ffi(&arg.to_ffi(), parser).from_ffi();
let paths: Vec<WString> = path_get_paths(arg, &*parser.get_vars());
for path in paths.iter() {
found += 1;

View file

@ -132,7 +132,6 @@ include_cpp! {
generate!("function_get_annotated_definition")
generate!("function_is_copy")
generate!("function_exists")
generate!("path_get_paths_ffi")
generate!("rgb_color_t")
generate_pod!("color24_t")

View file

@ -97,16 +97,6 @@ get_path_result_t path_try_get_path(const wcstring &cmd, const environment_t &va
return path_get_path_core(cmd, pathvar ? pathvar->as_list() : kDefaultPath);
}
static bool path_is_executable(const std::string &path) {
if (access(path.c_str(), X_OK)) return false;
struct stat buff;
if (stat(path.c_str(), &buff) == -1) {
if (errno != EACCES) wperror(L" stat");
return false;
}
return S_ISREG(buff.st_mode);
}
/// \return whether the given path is on a remote filesystem.
static dir_remoteness_t path_remoteness(const wcstring &path) {
std::string narrow = wcs2zstring(path);
@ -143,36 +133,6 @@ static dir_remoteness_t path_remoteness(const wcstring &path) {
#endif
}
std::vector<wcstring> path_get_paths(const wcstring &cmd, const environment_t &vars) {
FLOGF(path, L"path_get_paths('%ls')", cmd.c_str());
std::vector<wcstring> paths;
// If the command has a slash, it must be an absolute or relative path and thus we don't bother
// looking for matching commands in the PATH var.
if (cmd.find(L'/') != wcstring::npos) {
std::string narrow = wcs2zstring(cmd);
if (path_is_executable(narrow)) paths.push_back(cmd);
return paths;
}
auto path_var = vars.get(L"PATH");
if (!path_var) return paths;
const std::vector<wcstring> &pathsv = path_var->as_list();
for (auto path : pathsv) {
if (path.empty()) continue;
append_path_component(path, cmd);
std::string narrow = wcs2zstring(path);
if (path_is_executable(narrow)) paths.push_back(path);
}
return paths;
}
wcstring_list_ffi_t path_get_paths_ffi(const wcstring &cmd, const parser_t &parser) {
return path_get_paths(cmd, parser.vars());
}
std::vector<wcstring> path_apply_cdpath(const wcstring &dir, const wcstring &wd,
const environment_t &env_vars) {
std::vector<wcstring> paths;

View file

@ -63,12 +63,6 @@ struct get_path_result_t {
};
get_path_result_t path_try_get_path(const wcstring &cmd, const environment_t &vars);
/// Return all the paths that match the given command.
std::vector<wcstring> path_get_paths(const wcstring &cmd, const environment_t &vars);
// Needed because of issues with vectors of wstring and environment_t.
wcstring_list_ffi_t path_get_paths_ffi(const wcstring &cmd, const parser_t &parser);
/// Returns the full path of the specified directory, using the CDPATH variable as a list of base
/// directories for relative paths.
///