summaryrefslogtreecommitdiff
path: root/Shell/Shell.cpp
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-09-13 13:59:34 +0430
committerAndreas Kling <kling@serenityos.org>2020-09-14 17:40:18 +0200
commit0b57cdff82174af573fdd668d1bff36d41712b79 (patch)
treeca1078ea1b93ba75b8b10d4a268444f6fcd6cf4a /Shell/Shell.cpp
parent2f975904099b89fc77966b9f68f56711c1cc91e5 (diff)
downloadserenity-0b57cdff82174af573fdd668d1bff36d41712b79.zip
Shell: Add support for $0,$1,...
Diffstat (limited to 'Shell/Shell.cpp')
-rw-r--r--Shell/Shell.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp
index b3b406e0e5..057c1357c1 100644
--- a/Shell/Shell.cpp
+++ b/Shell/Shell.cpp
@@ -333,6 +333,33 @@ RefPtr<AST::Value> Shell::lookup_local_variable(const String& name)
if (auto* frame = find_frame_containing_local_variable(name))
return frame->local_variables.get(name).value();
+ if (auto index = name.to_uint(); index.has_value())
+ return get_argument(index.value());
+
+ return nullptr;
+}
+
+RefPtr<AST::Value> Shell::get_argument(size_t index)
+{
+ if (index == 0)
+ return adopt(*new AST::StringValue(current_script));
+
+ --index;
+ if (auto argv = lookup_local_variable("ARGV")) {
+ if (argv->is_list_without_resolution()) {
+ AST::ListValue* list = static_cast<AST::ListValue*>(argv.ptr());
+ if (list->values().size() <= index)
+ return nullptr;
+
+ return list->values().at(index);
+ }
+
+ if (index != 0)
+ return nullptr;
+
+ return argv;
+ }
+
return nullptr;
}
@@ -750,6 +777,7 @@ NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands)
bool Shell::run_file(const String& filename, bool explicitly_invoked)
{
+ TemporaryChange script_change { current_script, filename };
auto file_result = Core::File::open(filename, Core::File::ReadOnly);
if (file_result.is_error()) {
if (explicitly_invoked)