From aaa83e95c95619398610cde68ee2654abf45ccc8 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sun, 3 Jan 2021 12:39:27 +0330 Subject: Shell: Implement a 'source' builtin --- Shell/Builtin.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'Shell/Builtin.cpp') diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp index 99727f524a..fb3791695e 100644 --- a/Shell/Builtin.cpp +++ b/Shell/Builtin.cpp @@ -24,8 +24,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "AST.h" #include "Shell.h" #include +#include #include #include #include @@ -723,6 +725,37 @@ int Shell::builtin_shift(int argc, const char** argv) return 0; } +int Shell::builtin_source(int argc, const char** argv) +{ + const char* file_to_source = nullptr; + Vector args; + + Core::ArgsParser parser; + parser.add_positional_argument(file_to_source, "File to read commands from", "path"); + parser.add_positional_argument(args, "ARGV for the sourced file", "args", Core::ArgsParser::Required::No); + + if (!parser.parse(argc, const_cast(argv))) + return 1; + + Vector string_argv; + for (auto& arg : args) + string_argv.append(arg); + + auto previous_argv = lookup_local_variable("ARGV"); + ScopeGuard guard { [&] { + if (!args.is_empty()) + set_local_variable("ARGV", move(previous_argv)); + } }; + + if (!args.is_empty()) + set_local_variable("ARGV", AST::create(move(string_argv))); + + if (!run_file(file_to_source, true)) + return 126; + + return 0; +} + int Shell::builtin_time(int argc, const char** argv) { Vector args; -- cgit v1.2.3