mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-08 20:32:56 +03:00
Shell: Add support for getting environment variable values
This commit is contained in:
parent
615d823b55
commit
2bd181a14b
Notes:
sideshowbarker
2024-07-19 12:10:35 +09:00
Author: https://github.com/MinusGix 🔰 Commit: https://github.com/SerenityOS/serenity/commit/2bd181a14bf Pull-request: https://github.com/SerenityOS/serenity/pull/537 Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/bugaevc
@ -300,16 +300,42 @@ static Vector<String> expand_globs(const StringView& path, const StringView& bas
|
||||
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)
|
||||
{
|
||||
Vector<String> argv_string;
|
||||
for (auto& arg : args) {
|
||||
auto expanded = expand_globs(arg, "");
|
||||
if (expanded.is_empty())
|
||||
argv_string.append(arg);
|
||||
else
|
||||
for (auto& path : expand_globs(arg, ""))
|
||||
// This will return the text passed in if it wasn't a variable
|
||||
// This lets us just loop over its values
|
||||
auto expanded_parameters = expand_parameters(arg);
|
||||
|
||||
for (auto& exp_arg : expanded_parameters) {
|
||||
auto expanded_globs = expand_globs(exp_arg, "");
|
||||
for (auto& path : expanded_globs)
|
||||
argv_string.append(path);
|
||||
|
||||
if (expanded_globs.is_empty())
|
||||
argv_string.append(exp_arg);
|
||||
}
|
||||
}
|
||||
|
||||
return argv_string;
|
||||
|
Loading…
Reference in New Issue
Block a user