mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 09:37:45 +00:00
Shell: More general tilde expansion
Now expanding a tilde isn't hardcoded to just work for `cd`. It is instead expanded while processing shell arguments. Autocompletion still doesn't work, but this is definitely an improvement over the last iteration.
This commit is contained in:
parent
1ef3d4af69
commit
d0629d0a8c
1 changed files with 9 additions and 13 deletions
|
@ -147,15 +147,13 @@ static int sh_unset(int argc, const char** argv)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String expand_tilde(const char* expression)
|
static String expand_tilde(const String& expression)
|
||||||
{
|
{
|
||||||
int len = strlen(expression);
|
ASSERT(expression.starts_with('~'));
|
||||||
ASSERT(len > 0 && len + 1 <= PATH_MAX);
|
|
||||||
ASSERT(expression[0] == '~');
|
|
||||||
|
|
||||||
StringBuilder login_name;
|
StringBuilder login_name;
|
||||||
int first_slash_index = len;
|
size_t first_slash_index = expression.length();
|
||||||
for (int i = 1; i < len; ++i) {
|
for (size_t i = 1; i < expression.length(); ++i) {
|
||||||
if (expression[i] == '/') {
|
if (expression[i] == '/') {
|
||||||
first_slash_index = i;
|
first_slash_index = i;
|
||||||
break;
|
break;
|
||||||
|
@ -164,7 +162,7 @@ static String expand_tilde(const char* expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder path;
|
StringBuilder path;
|
||||||
for (int i = first_slash_index; i < len; ++i)
|
for (size_t i = first_slash_index; i < expression.length(); ++i)
|
||||||
path.append(expression[i]);
|
path.append(expression[i]);
|
||||||
|
|
||||||
if (login_name.is_empty()) {
|
if (login_name.is_empty()) {
|
||||||
|
@ -179,7 +177,7 @@ static String expand_tilde(const char* expression)
|
||||||
|
|
||||||
auto passwd = getpwnam(login_name.to_string().characters());
|
auto passwd = getpwnam(login_name.to_string().characters());
|
||||||
if (!passwd)
|
if (!passwd)
|
||||||
return String(expression);
|
return expression;
|
||||||
ASSERT(passwd->pw_dir);
|
ASSERT(passwd->pw_dir);
|
||||||
|
|
||||||
return String::format("%s/%s", passwd->pw_dir, path.to_string().characters());
|
return String::format("%s/%s", passwd->pw_dir, path.to_string().characters());
|
||||||
|
@ -206,11 +204,6 @@ static int sh_cd(int argc, const char** argv)
|
||||||
if (oldpwd == nullptr)
|
if (oldpwd == nullptr)
|
||||||
return 1;
|
return 1;
|
||||||
new_path = oldpwd;
|
new_path = oldpwd;
|
||||||
} else if (argv[1][0] == '~') {
|
|
||||||
auto path = expand_tilde(argv[1]);
|
|
||||||
if (path.is_empty())
|
|
||||||
return 1;
|
|
||||||
new_path = path;
|
|
||||||
} else if (argv[1][0] == '/') {
|
} else if (argv[1][0] == '/') {
|
||||||
new_path = argv[1];
|
new_path = argv[1];
|
||||||
} else {
|
} else {
|
||||||
|
@ -730,6 +723,9 @@ static Vector<String> process_arguments(const Vector<String>& args)
|
||||||
auto expanded_parameters = expand_parameters(arg);
|
auto expanded_parameters = expand_parameters(arg);
|
||||||
|
|
||||||
for (auto& exp_arg : expanded_parameters) {
|
for (auto& exp_arg : expanded_parameters) {
|
||||||
|
if (exp_arg.starts_with('~'))
|
||||||
|
exp_arg = expand_tilde(exp_arg);
|
||||||
|
|
||||||
auto expanded_globs = expand_globs(exp_arg, "");
|
auto expanded_globs = expand_globs(exp_arg, "");
|
||||||
for (auto& path : expanded_globs)
|
for (auto& path : expanded_globs)
|
||||||
argv_string.append(path);
|
argv_string.append(path);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue