mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-25 23:11:12 -03:00
Lots of miscellaneous cleanup. Unified the path_get_cd_path, path_allocate_cd_path, etc. functions
This commit is contained in:
83
path.cpp
83
path.cpp
@@ -133,7 +133,7 @@ static bool path_get_path_core(const wcstring &cmd, wcstring *out_path, const en
|
||||
|
||||
bool path_get_path(const wcstring &cmd, wcstring *out_path, const env_vars_snapshot_t &vars)
|
||||
{
|
||||
return path_get_path_core(cmd, out_path, vars.get(wcstring(L"PATH")));
|
||||
return path_get_path_core(cmd, out_path, vars.get(L"PATH"));
|
||||
}
|
||||
|
||||
bool path_get_path(const wcstring &cmd, wcstring *out_path)
|
||||
@@ -141,7 +141,7 @@ bool path_get_path(const wcstring &cmd, wcstring *out_path)
|
||||
return path_get_path_core(cmd, out_path, env_get_string(L"PATH"));
|
||||
}
|
||||
|
||||
bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env_vars_snapshot_t &vars)
|
||||
bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env_var_t &cdpath)
|
||||
{
|
||||
wchar_t *res = 0;
|
||||
int err = ENOENT;
|
||||
@@ -168,13 +168,12 @@ bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env
|
||||
else
|
||||
{
|
||||
|
||||
const wchar_t *path = L".";
|
||||
wcstring path = L".";
|
||||
|
||||
// Respect CDPATH
|
||||
env_var_t cdpath = env_get_string(L"CDPATH");
|
||||
if (! cdpath.missing_or_empty()) {
|
||||
path = cdpath.c_str();
|
||||
printf("CDPATH: %ls\n", path);
|
||||
}
|
||||
|
||||
wcstokenizer tokenizer(path, ARRAY_SEP_STR);
|
||||
@@ -220,21 +219,19 @@ bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
wchar_t *path_allocate_cdpath( const wcstring &dir, const wchar_t *wd )
|
||||
bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, const env_vars_snapshot_t &env_vars)
|
||||
{
|
||||
wchar_t *res = NULL;
|
||||
int err = ENOENT;
|
||||
if (dir.empty())
|
||||
return NULL;
|
||||
return false;
|
||||
|
||||
if (wd) {
|
||||
if (wd)
|
||||
{
|
||||
size_t len = wcslen(wd);
|
||||
assert(wd[len - 1] == L'/');
|
||||
}
|
||||
|
||||
wcstring_list_t paths;
|
||||
|
||||
if (dir.at(0) == L'/') {
|
||||
/* Absolute path */
|
||||
paths.push_back(dir);
|
||||
@@ -248,41 +245,35 @@ wchar_t *path_allocate_cdpath( const wcstring &dir, const wchar_t *wd )
|
||||
path.append(dir);
|
||||
paths.push_back(path);
|
||||
} else {
|
||||
wchar_t *path_cpy;
|
||||
wchar_t *state;
|
||||
|
||||
// Respect CDPATH
|
||||
env_var_t path = env_get_string(L"CDPATH");
|
||||
if (path.missing_or_empty()) path = L"."; //We'll change this to the wd if we have one
|
||||
env_var_t path = env_vars.get(L"CDPATH");
|
||||
if (path.missing_or_empty())
|
||||
path = L"."; //We'll change this to the wd if we have one
|
||||
|
||||
path_cpy = wcsdup( path.c_str() );
|
||||
|
||||
for( const wchar_t *nxt_path = wcstok( path_cpy, ARRAY_SEP_STR, &state );
|
||||
nxt_path != NULL;
|
||||
nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) )
|
||||
wcstring nxt_path;
|
||||
wcstokenizer tokenizer(path, ARRAY_SEP_STR);
|
||||
while (tokenizer.next(nxt_path))
|
||||
{
|
||||
|
||||
if (! wcscmp(nxt_path, L".") && wd != NULL) {
|
||||
if (nxt_path == L"." && wd != NULL) {
|
||||
// nxt_path is just '.', and we have a working directory, so use the wd instead
|
||||
// TODO: if nxt_path starts with ./ we need to replace the . with the wd
|
||||
nxt_path = wd;
|
||||
}
|
||||
|
||||
wcstring expanded_path = nxt_path;
|
||||
expand_tilde(expanded_path);
|
||||
expand_tilde(nxt_path);
|
||||
|
||||
// debug( 2, L"woot %ls\n", expanded_path.c_str() );
|
||||
|
||||
if (expanded_path.empty())
|
||||
if (nxt_path.empty())
|
||||
continue;
|
||||
|
||||
wcstring whole_path = expanded_path;
|
||||
wcstring whole_path = nxt_path;
|
||||
append_path_component(whole_path, dir);
|
||||
paths.push_back(whole_path);
|
||||
}
|
||||
free( path_cpy );
|
||||
}
|
||||
|
||||
bool success = false;
|
||||
for (wcstring_list_t::const_iterator iter = paths.begin(); iter != paths.end(); ++iter) {
|
||||
struct stat buf;
|
||||
const wcstring &dir = *iter;
|
||||
@@ -290,7 +281,9 @@ wchar_t *path_allocate_cdpath( const wcstring &dir, const wchar_t *wd )
|
||||
{
|
||||
if( S_ISDIR(buf.st_mode) )
|
||||
{
|
||||
res = wcsdup(dir.c_str());
|
||||
success = true;
|
||||
if (out)
|
||||
out->assign(dir);
|
||||
break;
|
||||
}
|
||||
else
|
||||
@@ -300,24 +293,12 @@ wchar_t *path_allocate_cdpath( const wcstring &dir, const wchar_t *wd )
|
||||
}
|
||||
}
|
||||
|
||||
if( !res )
|
||||
{
|
||||
if (! success)
|
||||
errno = err;
|
||||
}
|
||||
|
||||
return res;
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
bool path_can_get_cdpath(const wcstring &in, const wchar_t *wd)
|
||||
{
|
||||
wchar_t *tmp = path_allocate_cdpath(in, wd);
|
||||
bool result = (tmp != NULL);
|
||||
free(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wchar_t *wd)
|
||||
bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wchar_t *wd, const env_vars_snapshot_t &vars)
|
||||
{
|
||||
wcstring exp_path = path;
|
||||
expand_tilde(exp_path);
|
||||
@@ -328,16 +309,8 @@ bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wch
|
||||
string_prefixes_string(L"../", exp_path) ||
|
||||
exp_path == L"..")
|
||||
{
|
||||
/* These paths can be implicit cd. Note that a single period cannot (that's used for sourcing files anyways) */
|
||||
wchar_t *cd_path = path_allocate_cdpath(exp_path, wd);
|
||||
if (cd_path)
|
||||
{
|
||||
/* It worked. Return the path if desired */
|
||||
if (out_path)
|
||||
out_path->assign(cd_path);
|
||||
free(cd_path);
|
||||
result = true;
|
||||
}
|
||||
/* 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) */
|
||||
result = path_get_cdpath(exp_path, out_path, wd, vars);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -426,10 +399,10 @@ bool path_is_valid(const wcstring &path, const wcstring &working_directory)
|
||||
/* Prepend the working directory. Note that we know path is not empty here. */
|
||||
wcstring tmp = working_directory;
|
||||
tmp.append(path);
|
||||
path_is_valid = (0 == waccess(tmp.c_str(), F_OK));
|
||||
path_is_valid = (0 == waccess(tmp, F_OK));
|
||||
} else {
|
||||
/* Simple check */
|
||||
path_is_valid = (0 == waccess(path.c_str(), F_OK));
|
||||
path_is_valid = (0 == waccess(path, F_OK));
|
||||
}
|
||||
return path_is_valid;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user