1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 23:35:08 +00:00

Shell: Add support for ARGV (and $*, $#)

This patchset also adds the 'shift' builtin, as well as the usual tests.
closes #2948.
This commit is contained in:
AnotherTest 2020-08-04 09:27:25 +04:30 committed by Andreas Kling
parent 192b2383ac
commit 12af65c1c9
8 changed files with 79 additions and 1 deletions

View file

@ -678,6 +678,40 @@ int Shell::builtin_setopt(int argc, const char** argv)
return 0;
}
int Shell::builtin_shift(int argc, const char** argv)
{
int count = 1;
Core::ArgsParser parser;
parser.add_positional_argument(count, "Shift count", "count", Core::ArgsParser::Required::No);
if (!parser.parse(argc, const_cast<char**>(argv), false))
return 1;
if (count < 1)
return 0;
auto argv_ = lookup_local_variable("ARGV");
if (!argv_) {
fprintf(stderr, "shift: ARGV is unset\n");
return 1;
}
if (!argv_->is_list())
argv_ = *new AST::ListValue({ argv_ });
auto& values = static_cast<AST::ListValue*>(argv_.ptr())->values();
if ((size_t)count > values.size()) {
fprintf(stderr, "shift: shift count must not be greater than %zu\n", values.size());
return 1;
}
for (auto i = 0; i < count; ++i)
values.take_first();
return 0;
}
int Shell::builtin_time(int argc, const char** argv)
{
Vector<const char*> args;