mirror of
https://github.com/RGBCube/serenity
synced 2025-05-23 18:45:07 +00:00
Shell: Add support for getting environment variable values
This commit is contained in:
parent
615d823b55
commit
2bd181a14b
1 changed files with 31 additions and 5 deletions
|
@ -300,16 +300,42 @@ static Vector<String> expand_globs(const StringView& path, const StringView& bas
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Vector<String> expand_parameters(const StringView& param)
|
||||||
|
{
|
||||||
|
bool is_variable = param.length() > 1 && param[0] == '$';
|
||||||
|
if (!is_variable)
|
||||||
|
return { param };
|
||||||
|
|
||||||
|
String variable_name = String(param.substring_view(1, param.length() - 1));
|
||||||
|
|
||||||
|
char* env_value = getenv(variable_name.characters());
|
||||||
|
if (env_value == nullptr)
|
||||||
|
return { "" };
|
||||||
|
|
||||||
|
Vector<String> res;
|
||||||
|
String str_env_value = String(env_value);
|
||||||
|
const auto& split_text = str_env_value.split_view(' ');
|
||||||
|
for (auto& part : split_text)
|
||||||
|
res.append(part);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static Vector<String> process_arguments(const Vector<String>& args)
|
static Vector<String> process_arguments(const Vector<String>& args)
|
||||||
{
|
{
|
||||||
Vector<String> argv_string;
|
Vector<String> argv_string;
|
||||||
for (auto& arg : args) {
|
for (auto& arg : args) {
|
||||||
auto expanded = expand_globs(arg, "");
|
// This will return the text passed in if it wasn't a variable
|
||||||
if (expanded.is_empty())
|
// This lets us just loop over its values
|
||||||
argv_string.append(arg);
|
auto expanded_parameters = expand_parameters(arg);
|
||||||
else
|
|
||||||
for (auto& path : expand_globs(arg, ""))
|
for (auto& exp_arg : expanded_parameters) {
|
||||||
|
auto expanded_globs = expand_globs(exp_arg, "");
|
||||||
|
for (auto& path : expanded_globs)
|
||||||
argv_string.append(path);
|
argv_string.append(path);
|
||||||
|
|
||||||
|
if (expanded_globs.is_empty())
|
||||||
|
argv_string.append(exp_arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return argv_string;
|
return argv_string;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue