2016-05-03 22:18:24 +00:00
|
|
|
// Fish needs it's own globbing implementation to support tab-expansion of globbed parameters. Also
|
|
|
|
// provides recursive wildcards using **.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2017-02-13 04:24:22 +00:00
|
|
|
#include <stddef.h>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <sys/stat.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <sys/types.h>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2017-02-13 04:24:22 +00:00
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <memory>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
|
|
|
#include "expand.h"
|
2016-05-03 22:18:24 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "reader.h"
|
|
|
|
#include "wildcard.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// Description for generic executable.
|
|
|
|
#define COMPLETE_EXEC_DESC _(L"Executable")
|
|
|
|
/// Description for link to executable.
|
|
|
|
#define COMPLETE_EXEC_LINK_DESC _(L"Executable link")
|
|
|
|
/// Description for regular file.
|
|
|
|
#define COMPLETE_FILE_DESC _(L"File")
|
|
|
|
/// Description for character device.
|
|
|
|
#define COMPLETE_CHAR_DESC _(L"Character device")
|
|
|
|
/// Description for block device.
|
|
|
|
#define COMPLETE_BLOCK_DESC _(L"Block device")
|
|
|
|
/// Description for fifo buffer.
|
|
|
|
#define COMPLETE_FIFO_DESC _(L"Fifo")
|
|
|
|
/// Description for symlink.
|
|
|
|
#define COMPLETE_SYMLINK_DESC _(L"Symbolic link")
|
|
|
|
/// Description for symlink.
|
|
|
|
#define COMPLETE_DIRECTORY_SYMLINK_DESC _(L"Symbolic link to directory")
|
|
|
|
/// Description for Rotten symlink.
|
|
|
|
#define COMPLETE_ROTTEN_SYMLINK_DESC _(L"Rotten symbolic link")
|
|
|
|
/// Description for symlink loop.
|
|
|
|
#define COMPLETE_LOOP_SYMLINK_DESC _(L"Symbolic link loop")
|
|
|
|
/// Description for socket files.
|
|
|
|
#define COMPLETE_SOCKET_DESC _(L"Socket")
|
|
|
|
/// Description for directories.
|
|
|
|
#define COMPLETE_DIRECTORY_DESC _(L"Directory")
|
|
|
|
|
|
|
|
/// Finds an internal (ANY_STRING, etc.) style wildcard, or wcstring::npos.
|
|
|
|
static size_t wildcard_find(const wchar_t *wc) {
|
|
|
|
for (size_t i = 0; wc[i] != L'\0'; i++) {
|
|
|
|
if (wc[i] == ANY_CHAR || wc[i] == ANY_STRING || wc[i] == ANY_STRING_RECURSIVE) {
|
2015-08-03 22:51:27 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wcstring::npos;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// Implementation of wildcard_has. Needs to take the length to handle embedded nulls (issue #1631).
|
|
|
|
static bool wildcard_has_impl(const wchar_t *str, size_t len, bool internal) {
|
2014-08-24 21:28:31 +00:00
|
|
|
assert(str != NULL);
|
|
|
|
const wchar_t *end = str + len;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (internal) {
|
|
|
|
for (; str < end; str++) {
|
2012-11-19 00:30:30 +00:00
|
|
|
if ((*str == ANY_CHAR) || (*str == ANY_STRING) || (*str == ANY_STRING_RECURSIVE))
|
2013-05-25 22:41:18 +00:00
|
|
|
return true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
|
|
|
wchar_t prev = 0;
|
|
|
|
for (; str < end; str++) {
|
|
|
|
if (((*str == L'*') || (*str == L'?')) && (prev != L'\\')) return true;
|
2012-11-19 00:30:30 +00:00
|
|
|
prev = *str;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 22:41:18 +00:00
|
|
|
return false;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool wildcard_has(const wchar_t *str, bool internal) {
|
2014-08-24 21:28:31 +00:00
|
|
|
assert(str != NULL);
|
|
|
|
return wildcard_has_impl(str, wcslen(str), internal);
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool wildcard_has(const wcstring &str, bool internal) {
|
2014-08-24 21:28:31 +00:00
|
|
|
return wildcard_has_impl(str.data(), str.size(), internal);
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// Check whether the string str matches the wildcard string wc.
|
|
|
|
///
|
|
|
|
/// \param str String to be matched.
|
|
|
|
/// \param wc The wildcard.
|
|
|
|
/// \param is_first Whether files beginning with dots should not be matched against wildcards.
|
|
|
|
static enum fuzzy_match_type_t wildcard_match_internal(const wchar_t *str, const wchar_t *wc,
|
|
|
|
bool leading_dots_fail_to_match,
|
|
|
|
bool is_first) {
|
|
|
|
if (*str == 0 && *wc == 0) {
|
2016-05-03 23:23:30 +00:00
|
|
|
return fuzzy_match_exact; // we're done
|
2016-05-03 22:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hackish fix for issue #270. Prevent wildcards from matching . or .., but we must still allow
|
|
|
|
// literal matches.
|
2017-04-05 04:28:57 +00:00
|
|
|
if (leading_dots_fail_to_match && is_first && (!wcscmp(str, L".") || !wcscmp(str, L".."))) {
|
2016-05-03 22:18:24 +00:00
|
|
|
// The string is '.' or '..'. Return true if the wildcard exactly matches.
|
2015-08-04 07:32:34 +00:00
|
|
|
return wcscmp(str, wc) ? fuzzy_match_none : fuzzy_match_exact;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
if (*wc == ANY_STRING || *wc == ANY_STRING_RECURSIVE) {
|
|
|
|
// Ignore hidden file
|
|
|
|
if (leading_dots_fail_to_match && is_first && *str == L'.') {
|
2015-08-04 07:32:34 +00:00
|
|
|
return fuzzy_match_none;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Common case of * at the end. In that case we can early out since we know it will match.
|
|
|
|
if (wc[1] == L'\0') {
|
2015-08-04 07:32:34 +00:00
|
|
|
return fuzzy_match_exact;
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// Try all submatches.
|
|
|
|
do {
|
|
|
|
enum fuzzy_match_type_t subresult =
|
|
|
|
wildcard_match_internal(str, wc + 1, leading_dots_fail_to_match, false);
|
|
|
|
if (subresult != fuzzy_match_none) {
|
2015-08-04 07:32:34 +00:00
|
|
|
return subresult;
|
2015-08-07 20:28:47 +00:00
|
|
|
}
|
2015-08-04 07:32:34 +00:00
|
|
|
} while (*str++ != 0);
|
|
|
|
return fuzzy_match_none;
|
2016-05-03 22:18:24 +00:00
|
|
|
} else if (*str == 0) {
|
|
|
|
// End of string, but not end of wildcard, and the next wildcard element is not a '*', so
|
|
|
|
// this is not a match.
|
2015-08-04 07:32:34 +00:00
|
|
|
return fuzzy_match_none;
|
2016-05-03 22:18:24 +00:00
|
|
|
} else if (*wc == ANY_CHAR) {
|
|
|
|
if (is_first && *str == L'.') {
|
2015-08-04 07:32:34 +00:00
|
|
|
return fuzzy_match_none;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
return wildcard_match_internal(str + 1, wc + 1, leading_dots_fail_to_match, false);
|
|
|
|
} else if (*wc == *str) {
|
|
|
|
return wildcard_match_internal(str + 1, wc + 1, leading_dots_fail_to_match, false);
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2015-08-04 07:32:34 +00:00
|
|
|
return fuzzy_match_none;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// This does something horrible refactored from an even more horrible function.
|
|
|
|
static wcstring resolve_description(wcstring *completion, const wchar_t *explicit_desc,
|
|
|
|
wcstring (*desc_func)(const wcstring &)) {
|
2015-08-03 22:51:27 +00:00
|
|
|
size_t complete_sep_loc = completion->find(PROG_COMPLETE_SEP);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (complete_sep_loc != wcstring::npos) {
|
|
|
|
// This completion has an embedded description, do not use the generic description.
|
2015-08-03 22:51:27 +00:00
|
|
|
const wcstring description = completion->substr(complete_sep_loc + 1);
|
|
|
|
completion->resize(complete_sep_loc);
|
|
|
|
return description;
|
2013-05-25 22:41:18 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
|
|
|
|
const wcstring func_result = (desc_func ? desc_func(*completion) : wcstring());
|
|
|
|
if (!func_result.empty()) {
|
|
|
|
return func_result;
|
|
|
|
}
|
|
|
|
return explicit_desc ? explicit_desc : L"";
|
2015-08-03 22:51:27 +00:00
|
|
|
}
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// A transient parameter pack needed by wildcard_complete.
|
|
|
|
struct wc_complete_pack_t {
|
|
|
|
const wcstring &orig; // the original string, transient
|
|
|
|
const wchar_t *desc; // literal description
|
|
|
|
wcstring (*desc_func)(const wcstring &); // function for generating descriptions
|
2015-08-03 22:51:27 +00:00
|
|
|
expand_flags_t expand_flags;
|
2016-05-03 22:18:24 +00:00
|
|
|
wc_complete_pack_t(const wcstring &str, const wchar_t *des, wcstring (*df)(const wcstring &),
|
|
|
|
expand_flags_t fl)
|
|
|
|
: orig(str), desc(des), desc_func(df), expand_flags(fl) {}
|
2015-08-03 22:51:27 +00:00
|
|
|
};
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// Weirdly specific and non-reusable helper function that makes its one call site much clearer.
|
|
|
|
static bool has_prefix_match(const std::vector<completion_t> *comps, size_t first) {
|
|
|
|
if (comps != NULL) {
|
2015-08-04 05:09:25 +00:00
|
|
|
const size_t after_count = comps->size();
|
2016-05-03 22:18:24 +00:00
|
|
|
for (size_t j = first; j < after_count; j++) {
|
|
|
|
if (comps->at(j).match.type <= fuzzy_match_prefix) {
|
2015-08-04 05:09:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// Matches the string against the wildcard, and if the wildcard is a possible completion of the
|
|
|
|
/// string, the remainder of the string is inserted into the out vector.
|
|
|
|
///
|
|
|
|
/// We ignore ANY_STRING_RECURSIVE here. The consequence is that you cannot tab complete **
|
|
|
|
/// wildcards. This is historic behavior.
|
|
|
|
static bool wildcard_complete_internal(const wchar_t *str, const wchar_t *wc,
|
|
|
|
const wc_complete_pack_t ¶ms, complete_flags_t flags,
|
|
|
|
std::vector<completion_t> *out, bool is_first_call = false) {
|
2015-08-03 22:51:27 +00:00
|
|
|
assert(str != NULL);
|
|
|
|
assert(wc != NULL);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Maybe early out for hidden files. We require that the wildcard match these exactly (i.e. a
|
|
|
|
// dot); ANY_STRING not allowed.
|
|
|
|
if (is_first_call && str[0] == L'.' && wc[0] != L'.') {
|
2015-08-03 22:51:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Locate the next wildcard character position, e.g. ANY_CHAR or ANY_STRING.
|
2015-08-07 20:28:47 +00:00
|
|
|
const size_t next_wc_char_pos = wildcard_find(wc);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Maybe we have no more wildcards at all. This includes the empty string.
|
|
|
|
if (next_wc_char_pos == wcstring::npos) {
|
2015-08-03 22:51:27 +00:00
|
|
|
string_fuzzy_match_t match = string_fuzzy_match_string(wc, str);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// If we're allowing fuzzy match, any match is OK. Otherwise we require a prefix match.
|
2015-08-03 22:51:27 +00:00
|
|
|
bool match_acceptable;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (params.expand_flags & EXPAND_FUZZY_MATCH) {
|
2015-08-03 22:51:27 +00:00
|
|
|
match_acceptable = match.type != fuzzy_match_none;
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
2015-08-03 22:51:27 +00:00
|
|
|
match_acceptable = match_type_shares_prefix(match.type);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-10-30 19:52:50 +00:00
|
|
|
if (!match_acceptable || out == NULL) {
|
|
|
|
return match_acceptable;
|
2015-08-03 22:51:27 +00:00
|
|
|
}
|
2016-10-30 19:52:50 +00:00
|
|
|
|
|
|
|
// Wildcard complete.
|
|
|
|
bool full_replacement =
|
|
|
|
match_type_requires_full_replacement(match.type) || (flags & COMPLETE_REPLACES_TOKEN);
|
|
|
|
|
|
|
|
// If we are not replacing the token, be careful to only store the part of the string after
|
|
|
|
// the wildcard.
|
|
|
|
assert(!full_replacement || wcslen(wc) <= wcslen(str));
|
|
|
|
wcstring out_completion = full_replacement ? params.orig : str + wcslen(wc);
|
|
|
|
wcstring out_desc = resolve_description(&out_completion, params.desc, params.desc_func);
|
|
|
|
|
|
|
|
// Note: out_completion may be empty if the completion really is empty, e.g. tab-completing
|
|
|
|
// 'foo' when a file 'foo' exists.
|
|
|
|
complete_flags_t local_flags = flags | (full_replacement ? COMPLETE_REPLACES_TOKEN : 0);
|
|
|
|
append_completion(out, out_completion, out_desc, local_flags, match);
|
2015-08-03 22:51:27 +00:00
|
|
|
return match_acceptable;
|
2016-05-03 22:18:24 +00:00
|
|
|
} else if (next_wc_char_pos > 0) {
|
|
|
|
// Here we have a non-wildcard prefix. Note that we don't do fuzzy matching for stuff before
|
|
|
|
// a wildcard, so just do case comparison and then recurse.
|
|
|
|
if (wcsncmp(str, wc, next_wc_char_pos) == 0) {
|
|
|
|
// Normal match.
|
|
|
|
return wildcard_complete_internal(str + next_wc_char_pos, wc + next_wc_char_pos, params,
|
|
|
|
flags, out);
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
|
|
|
if (wcsncasecmp(str, wc, next_wc_char_pos) == 0) {
|
2016-05-03 22:18:24 +00:00
|
|
|
// Case insensitive match.
|
|
|
|
return wildcard_complete_internal(str + next_wc_char_pos, wc + next_wc_char_pos, params,
|
|
|
|
flags | COMPLETE_REPLACES_TOKEN, out);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return false; // no match
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our first character is a wildcard.
|
|
|
|
assert(next_wc_char_pos == 0);
|
|
|
|
switch (wc[0]) {
|
|
|
|
case ANY_CHAR: {
|
|
|
|
if (str[0] == L'\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return wildcard_complete_internal(str + 1, wc + 1, params, flags, out);
|
|
|
|
}
|
|
|
|
case ANY_STRING: {
|
|
|
|
// Hackish. If this is the last character of the wildcard, then just complete with
|
|
|
|
// the empty string. This fixes cases like "f*<tab>" -> "f*o".
|
|
|
|
if (wc[1] == L'\0') {
|
|
|
|
return wildcard_complete_internal(L"", L"", params, flags, out);
|
2015-08-03 22:51:27 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
// Try all submatches. Issue #929: if the recursive call gives us a prefix match,
|
|
|
|
// just stop. This is sloppy - what we really want to do is say, once we've seen a
|
|
|
|
// match of a particular type, ignore all matches of that type further down the
|
|
|
|
// string, such that the wildcard produces the "minimal match.".
|
|
|
|
bool has_match = false;
|
|
|
|
for (size_t i = 0; str[i] != L'\0'; i++) {
|
|
|
|
const size_t before_count = out ? out->size() : 0;
|
|
|
|
if (wildcard_complete_internal(str + i, wc + 1, params, flags, out)) {
|
|
|
|
// We found a match.
|
|
|
|
has_match = true;
|
|
|
|
|
|
|
|
// If out is NULL, we don't care about the actual matches. If out is not
|
|
|
|
// NULL but we have a prefix match, stop there.
|
|
|
|
if (out == NULL || has_prefix_match(out, before_count)) {
|
|
|
|
break;
|
2013-09-11 09:33:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return has_match;
|
|
|
|
}
|
|
|
|
case ANY_STRING_RECURSIVE: {
|
|
|
|
// We don't even try with this one.
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-30 19:52:50 +00:00
|
|
|
default: {
|
2017-02-14 04:37:27 +00:00
|
|
|
DIE("unreachable code reached");
|
2016-10-30 19:52:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
|
2016-11-02 04:19:34 +00:00
|
|
|
DIE("unreachable code reached");
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool wildcard_complete(const wcstring &str, const wchar_t *wc, const wchar_t *desc,
|
|
|
|
wcstring (*desc_func)(const wcstring &), std::vector<completion_t> *out,
|
|
|
|
expand_flags_t expand_flags, complete_flags_t flags) {
|
|
|
|
// Note out may be NULL.
|
2015-08-04 05:09:25 +00:00
|
|
|
assert(wc != NULL);
|
2016-02-19 23:45:12 +00:00
|
|
|
wc_complete_pack_t params(str, desc, desc_func, expand_flags);
|
2015-08-03 22:51:27 +00:00
|
|
|
return wildcard_complete_internal(str.c_str(), wc, params, flags, out, true /* first call */);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool wildcard_match(const wcstring &str, const wcstring &wc, bool leading_dots_fail_to_match) {
|
|
|
|
enum fuzzy_match_type_t match = wildcard_match_internal(
|
|
|
|
str.c_str(), wc.c_str(), leading_dots_fail_to_match, true /* first */);
|
2015-08-04 07:32:34 +00:00
|
|
|
return match != fuzzy_match_none;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// Obtain a description string for the file specified by the filename.
|
|
|
|
///
|
|
|
|
/// The returned value is a string constant and should not be free'd.
|
|
|
|
///
|
|
|
|
/// \param filename The file for which to find a description string
|
|
|
|
/// \param lstat_res The result of calling lstat on the file
|
|
|
|
/// \param lbuf The struct buf output of calling lstat on the file
|
|
|
|
/// \param stat_res The result of calling stat on the file
|
|
|
|
/// \param buf The struct buf output of calling stat on the file
|
|
|
|
/// \param err The errno value after a failed stat call on the file.
|
|
|
|
static wcstring file_get_desc(const wcstring &filename, int lstat_res, const struct stat &lbuf,
|
|
|
|
int stat_res, const struct stat &buf, int err) {
|
2016-10-30 19:52:50 +00:00
|
|
|
if (lstat_res) {
|
|
|
|
return COMPLETE_FILE_DESC;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-10-30 19:52:50 +00:00
|
|
|
if (S_ISLNK(lbuf.st_mode)) {
|
|
|
|
if (!stat_res) {
|
|
|
|
if (S_ISDIR(buf.st_mode)) {
|
|
|
|
return COMPLETE_DIRECTORY_SYMLINK_DESC;
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
2016-10-30 19:52:50 +00:00
|
|
|
if (buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH) && waccess(filename, X_OK) == 0) {
|
|
|
|
// Weird group permissions and other such issues make it non-trivial to find out if
|
|
|
|
// we can actually execute a file using the result from stat. It is much safer to
|
|
|
|
// use the access function, since it tells us exactly what we want to know.
|
|
|
|
return COMPLETE_EXEC_LINK_DESC;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-10-30 19:52:50 +00:00
|
|
|
|
|
|
|
return COMPLETE_SYMLINK_DESC;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-10-30 19:52:50 +00:00
|
|
|
|
|
|
|
if (err == ENOENT) return COMPLETE_ROTTEN_SYMLINK_DESC;
|
|
|
|
if (err == ELOOP) return COMPLETE_LOOP_SYMLINK_DESC;
|
|
|
|
// On unknown errors we do nothing. The file will be given the default 'File'
|
|
|
|
// description or one based on the suffix.
|
|
|
|
} else if (S_ISCHR(buf.st_mode)) {
|
|
|
|
return COMPLETE_CHAR_DESC;
|
|
|
|
} else if (S_ISBLK(buf.st_mode)) {
|
|
|
|
return COMPLETE_BLOCK_DESC;
|
|
|
|
} else if (S_ISFIFO(buf.st_mode)) {
|
|
|
|
return COMPLETE_FIFO_DESC;
|
|
|
|
} else if (S_ISSOCK(buf.st_mode)) {
|
|
|
|
return COMPLETE_SOCKET_DESC;
|
|
|
|
} else if (S_ISDIR(buf.st_mode)) {
|
|
|
|
return COMPLETE_DIRECTORY_DESC;
|
|
|
|
} else if (buf.st_mode & (S_IXUSR | S_IXGRP | S_IXGRP) && waccess(filename, X_OK) == 0) {
|
|
|
|
// Weird group permissions and other such issues make it non-trivial to find out if we can
|
|
|
|
// actually execute a file using the result from stat. It is much safer to use the access
|
|
|
|
// function, since it tells us exactly what we want to know.
|
|
|
|
return COMPLETE_EXEC_DESC;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
return COMPLETE_FILE_DESC;
|
2007-02-25 09:05:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// Test if the given file is an executable (if EXECUTABLES_ONLY) or directory (if
|
|
|
|
/// DIRECTORIES_ONLY). If it matches, call wildcard_complete() with some description that we make
|
|
|
|
/// up. Note that the filename came from a readdir() call, so we know it exists.
|
|
|
|
static bool wildcard_test_flags_then_complete(const wcstring &filepath, const wcstring &filename,
|
|
|
|
const wchar_t *wc, expand_flags_t expand_flags,
|
|
|
|
std::vector<completion_t> *out) {
|
|
|
|
// Check if it will match before stat().
|
|
|
|
if (!wildcard_complete(filename, wc, NULL, NULL, NULL, expand_flags, 0)) {
|
2015-08-04 05:09:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:17:15 +00:00
|
|
|
struct stat lstat_buf = {}, stat_buf = {};
|
|
|
|
int stat_res = -1;
|
|
|
|
int stat_errno = 0;
|
|
|
|
int lstat_res = lwstat(filepath, &lstat_buf);
|
2016-11-02 03:42:02 +00:00
|
|
|
if (lstat_res >= 0) {
|
2016-05-03 22:18:24 +00:00
|
|
|
if (S_ISLNK(lstat_buf.st_mode)) {
|
2015-08-04 00:17:15 +00:00
|
|
|
stat_res = wstat(filepath, &stat_buf);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
if (stat_res < 0) {
|
|
|
|
// In order to differentiate between e.g. rotten symlinks and symlink loops, we also
|
|
|
|
// need to know the error status of wstat.
|
2015-08-04 00:17:15 +00:00
|
|
|
stat_errno = errno;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
2015-08-04 00:17:15 +00:00
|
|
|
stat_buf = lstat_buf;
|
2012-11-19 00:30:30 +00:00
|
|
|
stat_res = lstat_res;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-04 00:17:15 +00:00
|
|
|
const long long file_size = stat_res == 0 ? stat_buf.st_size : 0;
|
|
|
|
const bool is_directory = stat_res == 0 && S_ISDIR(stat_buf.st_mode);
|
|
|
|
const bool is_executable = stat_res == 0 && S_ISREG(stat_buf.st_mode);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-10-22 18:21:13 +00:00
|
|
|
const bool need_directory = expand_flags & DIRECTORIES_ONLY;
|
|
|
|
if (need_directory && !is_directory) {
|
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-10-22 18:21:13 +00:00
|
|
|
|
|
|
|
const bool executables_only = expand_flags & EXECUTABLES_ONLY;
|
|
|
|
if (executables_only && (!is_executable || waccess(filepath, X_OK) != 0)) {
|
|
|
|
return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Compute the description.
|
2015-08-04 00:17:15 +00:00
|
|
|
wcstring desc;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (!(expand_flags & EXPAND_NO_DESCRIPTIONS)) {
|
2015-08-04 00:17:15 +00:00
|
|
|
desc = file_get_desc(filepath, lstat_res, lstat_buf, stat_res, stat_buf, stat_errno);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
if (file_size >= 0) {
|
|
|
|
if (!desc.empty()) desc.append(L", ");
|
2015-08-04 00:17:15 +00:00
|
|
|
desc.append(format_size(file_size));
|
2014-09-20 07:26:10 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Append a / if this is a directory. Note this requirement may be the only reason we have to
|
|
|
|
// call stat() in some cases.
|
|
|
|
if (is_directory) {
|
|
|
|
return wildcard_complete(filename + L'/', wc, desc.c_str(), NULL, out, expand_flags,
|
|
|
|
COMPLETE_NO_SPACE);
|
2015-08-04 00:17:15 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return wildcard_complete(filename, wc, desc.c_str(), NULL, out, expand_flags, 0);
|
2012-08-07 09:50:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
class wildcard_expander_t {
|
2016-07-09 22:27:10 +00:00
|
|
|
// The working directory to resolve paths against
|
|
|
|
const wcstring working_directory;
|
2016-05-03 22:18:24 +00:00
|
|
|
// The set of items we have resolved, used to efficiently avoid duplication.
|
2015-08-01 23:02:46 +00:00
|
|
|
std::set<wcstring> completion_set;
|
2016-05-03 22:18:24 +00:00
|
|
|
// The set of file IDs we have visited, used to avoid symlink loops.
|
2015-08-01 23:02:46 +00:00
|
|
|
std::set<file_id_t> visited_files;
|
2016-05-03 22:18:24 +00:00
|
|
|
// Flags controlling expansion.
|
2015-08-01 23:02:46 +00:00
|
|
|
const expand_flags_t flags;
|
2016-05-03 22:18:24 +00:00
|
|
|
// Resolved items get inserted into here. This is transient of course.
|
2015-08-04 06:34:56 +00:00
|
|
|
std::vector<completion_t> *resolved_completions;
|
2016-05-03 22:18:24 +00:00
|
|
|
// Whether we have been interrupted.
|
2015-08-01 23:02:46 +00:00
|
|
|
bool did_interrupt;
|
2016-05-03 22:18:24 +00:00
|
|
|
// Whether we have successfully added any completions.
|
2015-08-01 23:02:46 +00:00
|
|
|
bool did_add;
|
2016-07-09 22:27:10 +00:00
|
|
|
// Whether some parent expansion is fuzzy, and therefore completions always prepend their prefix
|
|
|
|
// This variable is a little suspicious - it should be passed along, not stored here
|
|
|
|
// If we ever try to do parallel wildcard expansion we'll have to remove this
|
|
|
|
bool has_fuzzy_ancestor;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
/// We are a trailing slash - expand at the end.
|
2016-07-09 22:27:10 +00:00
|
|
|
void expand_trailing_slash(const wcstring &base_dir, const wcstring &prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
/// Given a directory base_dir, which is opened as base_dir_fp, expand an intermediate segment
|
|
|
|
/// of the wildcard. Treat ANY_STRING_RECURSIVE as ANY_STRING. wc_segment is the wildcard
|
2016-07-09 22:27:10 +00:00
|
|
|
/// segment for this directory, wc_remainder is the wildcard for subdirectories,
|
|
|
|
/// prefix is the prefix for completions.
|
2016-05-03 22:18:24 +00:00
|
|
|
void expand_intermediate_segment(const wcstring &base_dir, DIR *base_dir_fp,
|
2016-07-09 22:27:10 +00:00
|
|
|
const wcstring &wc_segment, const wchar_t *wc_remainder,
|
|
|
|
const wcstring &prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
/// Given a directory base_dir, which is opened as base_dir_fp, expand an intermediate literal
|
|
|
|
/// segment. Use a fuzzy matching algorithm.
|
|
|
|
void expand_literal_intermediate_segment_with_fuzz(const wcstring &base_dir, DIR *base_dir_fp,
|
|
|
|
const wcstring &wc_segment,
|
2016-07-09 22:27:10 +00:00
|
|
|
const wchar_t *wc_remainder,
|
|
|
|
const wcstring &prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
/// Given a directory base_dir, which is opened as base_dir_fp, expand the last segment of the
|
|
|
|
/// wildcard. Treat ANY_STRING_RECURSIVE as ANY_STRING. wc is the wildcard segment to use for
|
2016-07-22 02:36:32 +00:00
|
|
|
/// matching, wc_remainder is the wildcard for subdirectories, prefix is the prefix for
|
|
|
|
/// completions.
|
|
|
|
void expand_last_segment(const wcstring &base_dir, DIR *base_dir_fp, const wcstring &wc,
|
|
|
|
const wcstring &prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
/// Indicate whether we should cancel wildcard expansion. This latches 'interrupt'.
|
|
|
|
bool interrupted() {
|
|
|
|
if (!did_interrupt) {
|
|
|
|
did_interrupt =
|
|
|
|
(is_main_thread() ? reader_interrupted() : reader_thread_job_is_stale());
|
2015-08-03 22:58:37 +00:00
|
|
|
}
|
|
|
|
return did_interrupt;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
void add_expansion_result(const wcstring &result) {
|
|
|
|
// This function is only for the non-completions case.
|
2016-12-04 04:12:53 +00:00
|
|
|
assert(!static_cast<bool>(this->flags &
|
|
|
|
EXPAND_FOR_COMPLETIONS)); //!OCLINT(multiple unary operator)
|
2016-05-03 22:18:24 +00:00
|
|
|
if (this->completion_set.insert(result).second) {
|
2015-08-04 06:34:56 +00:00
|
|
|
append_completion(this->resolved_completions, result);
|
|
|
|
this->did_add = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Given a start point as an absolute path, for any directory that has exactly one non-hidden
|
|
|
|
// entity in it which is itself a directory, return that. The result is a relative path. For
|
|
|
|
// example, if start_point is '/usr' we may return 'local/bin/'.
|
|
|
|
//
|
|
|
|
// The result does not have a leading slash, but does have a trailing slash if non-empty.
|
|
|
|
wcstring descend_unique_hierarchy(const wcstring &start_point) {
|
|
|
|
assert(!start_point.empty() && start_point.at(0) == L'/');
|
|
|
|
|
2016-02-17 23:00:38 +00:00
|
|
|
wcstring unique_hierarchy;
|
|
|
|
wcstring abs_unique_hierarchy = start_point;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-02-17 23:00:38 +00:00
|
|
|
bool stop_descent = false;
|
|
|
|
DIR *dir;
|
2016-05-03 22:18:24 +00:00
|
|
|
while (!stop_descent && (dir = wopendir(abs_unique_hierarchy))) {
|
|
|
|
// We keep track of the single unique_entry entry. If we get more than one, it's not
|
|
|
|
// unique and we stop the descent.
|
2016-02-17 23:00:38 +00:00
|
|
|
wcstring unique_entry;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-02-17 23:00:38 +00:00
|
|
|
bool child_is_dir;
|
|
|
|
wcstring child_entry;
|
2016-05-03 22:18:24 +00:00
|
|
|
while (wreaddir_resolving(dir, abs_unique_hierarchy, child_entry, &child_is_dir)) {
|
|
|
|
if (child_entry.empty() || child_entry.at(0) == L'.') {
|
2016-05-03 23:23:30 +00:00
|
|
|
continue; // either hidden, or . and .. entries -- skip them
|
2016-05-03 22:18:24 +00:00
|
|
|
} else if (child_is_dir && unique_entry.empty()) {
|
2016-05-03 23:23:30 +00:00
|
|
|
unique_entry = child_entry; // first candidate
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
|
|
|
// We either have two or more candidates, or the child is not a directory. We're
|
|
|
|
// done.
|
2016-02-17 23:00:38 +00:00
|
|
|
stop_descent = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-07-09 22:27:10 +00:00
|
|
|
// We stop if we got two or more entries; also stop if we got zero or were interrupted
|
|
|
|
if (unique_entry.empty() || interrupted()) {
|
2016-02-17 23:00:38 +00:00
|
|
|
stop_descent = true;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
if (!stop_descent) {
|
|
|
|
// We have an entry in the unique hierarchy!
|
2016-02-17 23:00:38 +00:00
|
|
|
append_path_component(unique_hierarchy, unique_entry);
|
|
|
|
unique_hierarchy.push_back(L'/');
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-02-17 23:00:38 +00:00
|
|
|
append_path_component(abs_unique_hierarchy, unique_entry);
|
|
|
|
abs_unique_hierarchy.push_back(L'/');
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
return unique_hierarchy;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
void try_add_completion_result(const wcstring &filepath, const wcstring &filename,
|
2016-07-09 22:27:10 +00:00
|
|
|
const wcstring &wildcard, const wcstring &prefix) {
|
2016-05-03 22:18:24 +00:00
|
|
|
// This function is only for the completions case.
|
2015-08-04 06:34:56 +00:00
|
|
|
assert(this->flags & EXPAND_FOR_COMPLETIONS);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-07-09 22:27:10 +00:00
|
|
|
wcstring abs_path = this->working_directory;
|
2016-02-17 23:00:38 +00:00
|
|
|
append_path_component(abs_path, filepath);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-04 06:46:41 +00:00
|
|
|
size_t before = this->resolved_completions->size();
|
2016-05-03 22:18:24 +00:00
|
|
|
if (wildcard_test_flags_then_complete(abs_path, filename, wildcard.c_str(), this->flags,
|
|
|
|
this->resolved_completions)) {
|
|
|
|
// Hack. We added this completion result based on the last component of the wildcard.
|
2016-07-09 22:27:10 +00:00
|
|
|
// Prepend our prefix to each wildcard that replaces its token.
|
|
|
|
// Note that prepend_token_prefix is a no-op unless COMPLETE_REPLACES_TOKEN is set
|
2015-08-04 06:46:41 +00:00
|
|
|
size_t after = this->resolved_completions->size();
|
2016-05-03 22:18:24 +00:00
|
|
|
for (size_t i = before; i < after; i++) {
|
2016-07-09 22:27:10 +00:00
|
|
|
completion_t *c = &this->resolved_completions->at(i);
|
|
|
|
if (this->has_fuzzy_ancestor && !(c->flags & COMPLETE_REPLACES_TOKEN)) {
|
|
|
|
c->flags |= COMPLETE_REPLACES_TOKEN;
|
|
|
|
c->prepend_token_prefix(wildcard);
|
|
|
|
}
|
|
|
|
c->prepend_token_prefix(prefix);
|
2015-08-04 06:46:41 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Hack. Implement EXPAND_SPECIAL_FOR_CD by descending the deepest unique hierarchy we
|
|
|
|
// can, and then appending any components to each new result.
|
|
|
|
if (flags & EXPAND_SPECIAL_FOR_CD) {
|
2016-02-17 23:00:38 +00:00
|
|
|
wcstring unique_hierarchy = this->descend_unique_hierarchy(abs_path);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (!unique_hierarchy.empty()) {
|
|
|
|
for (size_t i = before; i < after; i++) {
|
2016-02-17 23:00:38 +00:00
|
|
|
completion_t &c = this->resolved_completions->at(i);
|
|
|
|
c.completion.append(unique_hierarchy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-04 06:34:56 +00:00
|
|
|
this->did_add = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Helper to resolve using our prefix.
|
|
|
|
DIR *open_dir(const wcstring &base_dir) const {
|
2016-07-09 22:27:10 +00:00
|
|
|
wcstring path = this->working_directory;
|
2016-02-08 09:29:23 +00:00
|
|
|
append_path_component(path, base_dir);
|
|
|
|
return wopendir(path);
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
public:
|
2016-07-09 22:27:10 +00:00
|
|
|
wildcard_expander_t(const wcstring &wd, expand_flags_t f, std::vector<completion_t> *r)
|
|
|
|
: working_directory(wd),
|
2016-05-03 22:18:24 +00:00
|
|
|
flags(f),
|
|
|
|
resolved_completions(r),
|
|
|
|
did_interrupt(false),
|
2016-07-09 22:27:10 +00:00
|
|
|
did_add(false),
|
|
|
|
has_fuzzy_ancestor(false) {
|
2015-08-04 06:34:56 +00:00
|
|
|
assert(resolved_completions != NULL);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Insert initial completions into our set to avoid duplicates.
|
|
|
|
for (std::vector<completion_t>::const_iterator iter = resolved_completions->begin();
|
|
|
|
iter != resolved_completions->end(); ++iter) {
|
2015-08-01 23:02:46 +00:00
|
|
|
this->completion_set.insert(iter->completion);
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Do wildcard expansion. This is recursive.
|
2016-07-09 22:27:10 +00:00
|
|
|
void expand(const wcstring &base_dir, const wchar_t *wc, const wcstring &prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
int status_code() const {
|
|
|
|
if (this->did_interrupt) {
|
2015-08-03 22:58:37 +00:00
|
|
|
return -1;
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return this->did_add ? 1 : 0;
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-09 22:27:10 +00:00
|
|
|
void wildcard_expander_t::expand_trailing_slash(const wcstring &base_dir, const wcstring &prefix) {
|
2016-05-03 22:18:24 +00:00
|
|
|
if (interrupted()) {
|
2015-08-01 23:02:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
if (!(flags & EXPAND_FOR_COMPLETIONS)) {
|
2017-04-09 04:47:05 +00:00
|
|
|
// Trailing slash and not accepting incomplete, e.g. `echo /xyz/`. Insert this file if it
|
2016-05-03 22:18:24 +00:00
|
|
|
// exists.
|
|
|
|
if (waccess(base_dir, F_OK) == 0) {
|
2015-08-04 06:34:56 +00:00
|
|
|
this->add_expansion_result(base_dir);
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
2017-04-09 04:47:05 +00:00
|
|
|
// Trailing slashes and accepting incomplete, e.g. `echo /xyz/<tab>`. Everything is added.
|
2015-08-01 23:02:46 +00:00
|
|
|
DIR *dir = open_dir(base_dir);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (dir) {
|
2015-08-01 23:02:46 +00:00
|
|
|
wcstring next;
|
2016-05-03 22:18:24 +00:00
|
|
|
while (wreaddir(dir, next) && !interrupted()) {
|
|
|
|
if (!next.empty() && next.at(0) != L'.') {
|
2016-07-09 22:27:10 +00:00
|
|
|
this->try_add_completion_result(base_dir + next, next, L"", prefix);
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
void wildcard_expander_t::expand_intermediate_segment(const wcstring &base_dir, DIR *base_dir_fp,
|
|
|
|
const wcstring &wc_segment,
|
2016-07-09 22:27:10 +00:00
|
|
|
const wchar_t *wc_remainder,
|
|
|
|
const wcstring &prefix) {
|
2015-08-01 23:02:46 +00:00
|
|
|
wcstring name_str;
|
2016-05-03 22:18:24 +00:00
|
|
|
while (!interrupted() && wreaddir_for_dirs(base_dir_fp, &name_str)) {
|
|
|
|
// Note that it's critical we ignore leading dots here, else we may descend into . and ..
|
|
|
|
if (!wildcard_match(name_str, wc_segment, true)) {
|
|
|
|
// Doesn't match the wildcard for this segment, skip it.
|
2015-08-01 23:02:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-01 23:02:46 +00:00
|
|
|
wcstring full_path = base_dir + name_str;
|
|
|
|
struct stat buf;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (0 != wstat(full_path, &buf) || !S_ISDIR(buf.st_mode)) {
|
|
|
|
// We either can't stat it, or we did but it's not a directory.
|
2015-08-01 23:02:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file_id_t file_id = file_id_t::file_id_from_stat(&buf);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (!this->visited_files.insert(file_id).second) {
|
|
|
|
// Symlink loop! This directory was already visited, so skip it.
|
2015-08-01 23:02:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// We made it through. Perform normal wildcard expansion on this new directory, starting at
|
|
|
|
// our tail_wc, which includes the ANY_STRING_RECURSIVE guy.
|
2015-08-01 23:02:46 +00:00
|
|
|
full_path.push_back(L'/');
|
2016-07-09 22:27:10 +00:00
|
|
|
this->expand(full_path, wc_remainder, prefix + wc_segment + L'/');
|
2015-09-28 00:19:17 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// Now remove the visited file. This is for #2414: only directories "beneath" us should be
|
|
|
|
// considered visited.
|
2015-09-28 00:19:17 +00:00
|
|
|
this->visited_files.erase(file_id);
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-07-22 02:36:32 +00:00
|
|
|
void wildcard_expander_t::expand_literal_intermediate_segment_with_fuzz(const wcstring &base_dir,
|
|
|
|
DIR *base_dir_fp,
|
|
|
|
const wcstring &wc_segment,
|
|
|
|
const wchar_t *wc_remainder,
|
|
|
|
const wcstring &prefix) {
|
2016-05-03 22:18:24 +00:00
|
|
|
// This only works with tab completions. Ordinary wildcard expansion should never go fuzzy.
|
2015-08-07 20:28:47 +00:00
|
|
|
wcstring name_str;
|
2016-07-09 22:27:10 +00:00
|
|
|
|
|
|
|
// Mark that we are fuzzy for the duration of this function
|
|
|
|
const scoped_push<bool> scoped_fuzzy(&this->has_fuzzy_ancestor, true);
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
while (!interrupted() && wreaddir_for_dirs(base_dir_fp, &name_str)) {
|
|
|
|
// Don't bother with . and ..
|
2017-04-05 04:28:57 +00:00
|
|
|
if (name_str == L"." || name_str == L"..") {
|
2015-08-07 20:28:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Skip cases that don't match or match exactly. The match-exactly case was handled directly
|
|
|
|
// in expand().
|
2015-08-07 20:28:47 +00:00
|
|
|
const string_fuzzy_match_t match = string_fuzzy_match_string(wc_segment, name_str);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (match.type == fuzzy_match_none || match.type == fuzzy_match_exact) {
|
2015-08-07 20:28:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-07 20:28:47 +00:00
|
|
|
wcstring new_full_path = base_dir + name_str;
|
|
|
|
new_full_path.push_back(L'/');
|
|
|
|
struct stat buf;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (0 != wstat(new_full_path, &buf) || !S_ISDIR(buf.st_mode)) {
|
2015-08-07 20:28:47 +00:00
|
|
|
/* We either can't stat it, or we did but it's not a directory */
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-07-09 22:27:10 +00:00
|
|
|
// Determine the effective prefix for our children
|
2016-07-22 02:36:32 +00:00
|
|
|
// Normally this would be the wildcard segment, but here we know our segment doesn't have
|
|
|
|
// wildcards
|
|
|
|
// ("literal") and we are doing fuzzy expansion, which means we replace the segment with
|
|
|
|
// files found
|
2016-07-09 22:27:10 +00:00
|
|
|
// through fuzzy matching
|
|
|
|
const wcstring child_prefix = prefix + name_str + L'/';
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-07-09 22:27:10 +00:00
|
|
|
// Ok, this directory matches. Recurse to it. Then mark each resulting completion as fuzzy.
|
|
|
|
const size_t before = this->resolved_completions->size();
|
|
|
|
this->expand(new_full_path, wc_remainder, child_prefix);
|
2015-08-07 20:28:47 +00:00
|
|
|
const size_t after = this->resolved_completions->size();
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-07 20:28:47 +00:00
|
|
|
assert(before <= after);
|
2016-05-03 22:18:24 +00:00
|
|
|
for (size_t i = before; i < after; i++) {
|
2015-08-07 20:28:47 +00:00
|
|
|
completion_t *c = &this->resolved_completions->at(i);
|
2016-05-03 22:18:24 +00:00
|
|
|
// Mark the completion as replacing.
|
|
|
|
if (!(c->flags & COMPLETE_REPLACES_TOKEN)) {
|
2015-08-07 20:28:47 +00:00
|
|
|
c->flags |= COMPLETE_REPLACES_TOKEN;
|
2016-07-09 22:27:10 +00:00
|
|
|
c->prepend_token_prefix(child_prefix);
|
2015-08-07 20:28:47 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
// And every match must be made at least as fuzzy as ours.
|
|
|
|
if (match.compare(c->match) > 0) {
|
|
|
|
// Our match is fuzzier.
|
2015-08-07 20:28:47 +00:00
|
|
|
c->match = match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-01 23:02:46 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
void wildcard_expander_t::expand_last_segment(const wcstring &base_dir, DIR *base_dir_fp,
|
2016-07-09 22:27:10 +00:00
|
|
|
const wcstring &wc, const wcstring &prefix) {
|
2015-08-01 23:02:46 +00:00
|
|
|
wcstring name_str;
|
2016-05-03 22:18:24 +00:00
|
|
|
while (wreaddir(base_dir_fp, name_str)) {
|
|
|
|
if (flags & EXPAND_FOR_COMPLETIONS) {
|
2016-07-09 22:27:10 +00:00
|
|
|
this->try_add_completion_result(base_dir + name_str, name_str, wc, prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
|
|
|
// Normal wildcard expansion, not for completions.
|
|
|
|
if (wildcard_match(name_str, wc, true /* skip files with leading dots */)) {
|
2015-08-04 06:34:56 +00:00
|
|
|
this->add_expansion_result(base_dir + name_str);
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// The real implementation of wildcard expansion is in this function. Other functions are just
|
|
|
|
/// wrappers around this one.
|
|
|
|
///
|
|
|
|
/// This function traverses the relevant directory tree looking for matches, and recurses when
|
|
|
|
/// needed to handle wildcrards spanning multiple components and recursive wildcards.
|
|
|
|
///
|
|
|
|
/// Because this function calls itself recursively with substrings, it's important that the
|
|
|
|
/// parameters be raw pointers instead of wcstring, which would be too expensive to construct for
|
|
|
|
/// all substrings.
|
|
|
|
///
|
|
|
|
/// Args:
|
|
|
|
/// base_dir: the "working directory" against which the wildcard is to be resolved
|
|
|
|
/// wc: the wildcard string itself, e.g. foo*bar/baz (where * is acutally ANY_CHAR)
|
2016-07-09 22:27:10 +00:00
|
|
|
/// prefix: the string that should be prepended for completions that replace their token.
|
|
|
|
// This is usually the same thing as the original wildcard, but for fuzzy matching, we
|
|
|
|
// expand intermediate segments. effective_prefix is always either empty, or ends with a slash
|
2016-07-22 02:36:32 +00:00
|
|
|
// Note: this is only used when doing completions (EXPAND_FOR_COMPLETIONS is true), not
|
|
|
|
// expansions
|
|
|
|
void wildcard_expander_t::expand(const wcstring &base_dir, const wchar_t *wc,
|
|
|
|
const wcstring &effective_prefix) {
|
2015-08-01 23:02:46 +00:00
|
|
|
assert(wc != NULL);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
if (interrupted()) {
|
2015-08-01 23:02:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Get the current segment and compute interesting properties about it.
|
2015-08-01 23:02:46 +00:00
|
|
|
const size_t wc_len = wcslen(wc);
|
2016-05-03 22:18:24 +00:00
|
|
|
const wchar_t *const next_slash = wcschr(wc, L'/');
|
2015-08-01 23:02:46 +00:00
|
|
|
const bool is_last_segment = (next_slash == NULL);
|
|
|
|
const size_t wc_segment_len = next_slash ? next_slash - wc : wc_len;
|
|
|
|
const wcstring wc_segment = wcstring(wc, wc_segment_len);
|
2016-05-03 22:18:24 +00:00
|
|
|
const bool segment_has_wildcards =
|
|
|
|
wildcard_has(wc_segment, true /* internal, i.e. look for ANY_CHAR instead of ? */);
|
2016-07-22 02:36:32 +00:00
|
|
|
const wchar_t *const wc_remainder = next_slash ? next_slash + 1 : NULL;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
if (wc_segment.empty()) {
|
|
|
|
// Handle empty segment.
|
2016-10-23 20:58:12 +00:00
|
|
|
assert(!segment_has_wildcards); //!OCLINT(multiple unary operator)
|
2016-05-03 22:18:24 +00:00
|
|
|
if (is_last_segment) {
|
2016-07-09 22:27:10 +00:00
|
|
|
this->expand_trailing_slash(base_dir, effective_prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
|
|
|
// Multiple adjacent slashes in the wildcard. Just skip them.
|
2016-07-09 22:27:10 +00:00
|
|
|
this->expand(base_dir, wc_remainder, effective_prefix + L'/');
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
} else if (!segment_has_wildcards && !is_last_segment) {
|
|
|
|
// Literal intermediate match. Note that we may not be able to actually read the directory
|
|
|
|
// (issue #2099).
|
2015-08-01 23:02:46 +00:00
|
|
|
assert(next_slash != NULL);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-10-02 00:58:15 +00:00
|
|
|
// Absolute path of the intermediate directory
|
|
|
|
const wcstring intermediate_dirpath = base_dir + wc_segment + L'/';
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// This just trumps everything.
|
2015-08-04 07:32:34 +00:00
|
|
|
size_t before = this->resolved_completions->size();
|
2016-10-02 00:58:15 +00:00
|
|
|
this->expand(intermediate_dirpath, wc_remainder, effective_prefix + wc_segment + L'/');
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Maybe try a fuzzy match (#94) if nothing was found with the literal match. Respect
|
|
|
|
// EXPAND_NO_DIRECTORY_ABBREVIATIONS (issue #2413).
|
2016-10-02 00:58:15 +00:00
|
|
|
// Don't do fuzzy matches if the literal segment was valid (#3211)
|
2016-05-03 22:18:24 +00:00
|
|
|
bool allow_fuzzy = (this->flags & (EXPAND_FUZZY_MATCH | EXPAND_NO_FUZZY_DIRECTORIES)) ==
|
|
|
|
EXPAND_FUZZY_MATCH;
|
2016-10-05 03:06:14 +00:00
|
|
|
if (allow_fuzzy && this->resolved_completions->size() == before &&
|
2016-10-02 00:58:15 +00:00
|
|
|
waccess(intermediate_dirpath, F_OK) != 0) {
|
2015-08-07 20:28:47 +00:00
|
|
|
assert(this->flags & EXPAND_FOR_COMPLETIONS);
|
2015-08-04 07:32:34 +00:00
|
|
|
DIR *base_dir_fd = open_dir(base_dir);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (base_dir_fd != NULL) {
|
2016-07-22 02:36:32 +00:00
|
|
|
this->expand_literal_intermediate_segment_with_fuzz(
|
|
|
|
base_dir, base_dir_fd, wc_segment, wc_remainder, effective_prefix);
|
2015-08-04 07:32:34 +00:00
|
|
|
closedir(base_dir_fd);
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
|
|
|
assert(!wc_segment.empty() && (segment_has_wildcards || is_last_segment));
|
2015-08-01 23:02:46 +00:00
|
|
|
DIR *dir = open_dir(base_dir);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (dir) {
|
|
|
|
if (is_last_segment) {
|
|
|
|
// Last wildcard segment, nonempty wildcard.
|
2016-07-09 22:27:10 +00:00
|
|
|
this->expand_last_segment(base_dir, dir, wc_segment, effective_prefix);
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
|
|
|
// Not the last segment, nonempty wildcard.
|
2015-08-01 23:02:46 +00:00
|
|
|
assert(next_slash != NULL);
|
2016-07-22 02:36:32 +00:00
|
|
|
this->expand_intermediate_segment(base_dir, dir, wc_segment, wc_remainder,
|
|
|
|
effective_prefix + wc_segment + L'/');
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Recursive wildcards require special handling.
|
2015-08-01 23:02:46 +00:00
|
|
|
size_t asr_idx = wc_segment.find(ANY_STRING_RECURSIVE);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (asr_idx != wcstring::npos) {
|
|
|
|
// Construct a "head + any" wildcard for matching stuff in this directory, and an
|
|
|
|
// "any + tail" wildcard for matching stuff in subdirectories. Note that the
|
|
|
|
// ANY_STRING_RECURSIVE character is present in both the head and the tail.
|
2015-08-03 00:34:05 +00:00
|
|
|
const wcstring head_any(wc_segment, 0, asr_idx + 1);
|
|
|
|
const wchar_t *any_tail = wc + asr_idx;
|
|
|
|
assert(head_any.at(head_any.size() - 1) == ANY_STRING_RECURSIVE);
|
|
|
|
assert(any_tail[0] == ANY_STRING_RECURSIVE);
|
|
|
|
|
2015-08-01 23:02:46 +00:00
|
|
|
rewinddir(dir);
|
2016-07-22 02:36:32 +00:00
|
|
|
this->expand_intermediate_segment(base_dir, dir, head_any, any_tail,
|
|
|
|
effective_prefix);
|
2015-08-01 23:02:46 +00:00
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int wildcard_expand_string(const wcstring &wc, const wcstring &working_directory,
|
|
|
|
expand_flags_t flags, std::vector<completion_t> *output) {
|
2015-07-28 01:45:47 +00:00
|
|
|
assert(output != NULL);
|
2016-05-03 22:18:24 +00:00
|
|
|
// Fuzzy matching only if we're doing completions.
|
2015-08-07 20:28:47 +00:00
|
|
|
assert((flags & (EXPAND_FUZZY_MATCH | EXPAND_FOR_COMPLETIONS)) != EXPAND_FUZZY_MATCH);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// EXPAND_SPECIAL_FOR_CD requires DIRECTORIES_ONLY and EXPAND_FOR_COMPLETIONS and
|
|
|
|
// EXPAND_NO_DESCRIPTIONS.
|
2016-04-07 22:24:52 +00:00
|
|
|
assert(!(flags & EXPAND_SPECIAL_FOR_CD) ||
|
2016-05-03 22:18:24 +00:00
|
|
|
((flags & DIRECTORIES_ONLY) && (flags & EXPAND_FOR_COMPLETIONS) &&
|
|
|
|
(flags & EXPAND_NO_DESCRIPTIONS)));
|
|
|
|
|
|
|
|
// Hackish fix for issue #1631. We are about to call c_str(), which will produce a string
|
|
|
|
// truncated at any embedded nulls. We could fix this by passing around the size, etc. However
|
|
|
|
// embedded nulls are never allowed in a filename, so we just check for them and return 0 (no
|
|
|
|
// matches) if there is an embedded null.
|
|
|
|
if (wc.find(L'\0') != wcstring::npos) {
|
2014-08-24 21:28:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Compute the prefix and base dir. The prefix is what we prepend for filesystem operations
|
|
|
|
// (i.e. the working directory), the base_dir is the part of the wildcard consumed thus far,
|
|
|
|
// which we also have to append. The difference is that the base_dir is returned as part of the
|
|
|
|
// expansion, and the prefix is not.
|
|
|
|
//
|
|
|
|
// Check for a leading slash. If we find one, we have an absolute path: the prefix is empty, the
|
|
|
|
// base dir is /, and the wildcard is the remainder. If we don't find one, the prefix is the
|
2016-07-09 22:27:10 +00:00
|
|
|
// working directory, the base dir is empty.
|
2016-02-08 09:29:23 +00:00
|
|
|
wcstring prefix, base_dir, effective_wc;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (string_prefixes_string(L"/", wc)) {
|
2016-02-08 09:29:23 +00:00
|
|
|
prefix = L"";
|
|
|
|
base_dir = L"/";
|
|
|
|
effective_wc = wc.substr(1);
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
2016-02-08 09:29:23 +00:00
|
|
|
prefix = working_directory;
|
|
|
|
base_dir = L"";
|
|
|
|
effective_wc = wc;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-07-09 22:27:10 +00:00
|
|
|
wildcard_expander_t expander(prefix, flags, output);
|
|
|
|
expander.expand(base_dir, effective_wc.c_str(), base_dir);
|
2015-08-08 15:44:04 +00:00
|
|
|
return expander.status_code();
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|