Shell: update cached_path when adding aliases

This has the nice side effect of fixing alias completion, because
cached_path is the source of truth for the completion system, and it was
only refreshed (with shell::cache_path()) in the shell's constructor,
before the rc files where loaded (ie no aliases)

This also means that shell::is_runnable can now rely on the cache, and
doesn't have to check the aliases itself.
This commit is contained in:
Mathieu PATUREL 2020-08-03 10:05:10 +10:00 committed by Andreas Kling
parent 2b4b9d212e
commit f6d4c4f02c
2 changed files with 14 additions and 5 deletions

View file

@ -64,6 +64,20 @@ int Shell::builtin_alias(int argc, const char** argv)
}
} else {
m_aliases.set(parts[0], parts[1]);
size_t index = 0;
auto match = binary_search(
cached_path.span(), parts[0], [](const String& name, const String& program) -> int {
return strcmp(name.characters(), program.characters());
},
&index);
if (match)
continue;
while (strcmp(cached_path[index].characters(), parts[0].characters()) < 0) {
index++;
}
cached_path.insert(index, parts[0]);
}
}

View file

@ -388,11 +388,6 @@ String Shell::resolve_alias(const String& name) const
bool Shell::is_runnable(const StringView& name)
{
// FIXME: for now, check aliases manually because cached path doesn't get
// updated with aliases. Should it?
if (!resolve_alias(name).is_null())
return true;
if (access(name.to_string().characters(), X_OK) == 0)
return true;