1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:18:12 +00:00

Shell: Implement a 'source' builtin

This commit is contained in:
AnotherTest 2021-01-03 12:39:27 +03:30 committed by Andreas Kling
parent a698af88fc
commit aaa83e95c9
2 changed files with 34 additions and 0 deletions

View file

@ -24,8 +24,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "AST.h"
#include "Shell.h"
#include <AK/LexicalPath.h>
#include <AK/ScopeGuard.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
@ -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<const char*> 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<char**>(argv)))
return 1;
Vector<String> 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<AST::ListValue>(move(string_argv)));
if (!run_file(file_to_source, true))
return 126;
return 0;
}
int Shell::builtin_time(int argc, const char** argv)
{
Vector<const char*> args;