Luna/shell/main.cpp

119 lines
2.8 KiB
C++
Raw Normal View History

#include "Command.h"
#include "Prompt.h"
#include "sh.h"
#include <errno.h>
2023-04-13 16:33:43 +00:00
#include <luna/String.h>
2023-03-29 15:56:56 +00:00
#include <luna/Vector.h>
2023-04-08 10:09:43 +00:00
#include <os/ArgumentParser.h>
#include <os/Directory.h>
2023-04-13 16:33:43 +00:00
#include <os/File.h>
#include <os/FileSystem.h>
2023-04-18 16:16:24 +00:00
#include <os/Process.h>
#include <signal.h>
2023-03-23 21:19:54 +00:00
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
2023-03-23 21:19:54 +00:00
#include <unistd.h>
#include <luna/HashMap.h>
2023-04-13 16:33:43 +00:00
using os::File;
// Do nothing, but we need a handler so read() returns EINTR.
static void sigint_handler(int)
2023-03-23 21:19:54 +00:00
{
}
static Result<int> execute_loop(bool interactive, SharedPtr<File> input_file)
2023-03-23 21:19:54 +00:00
{
while (1)
{
if (interactive) display_prompt();
2023-06-03 14:59:18 +00:00
auto maybe_cmd = TRY(read_command(input_file));
if (!maybe_cmd.has_value())
{
if (interactive) puts("exit");
break;
}
2023-04-08 10:09:43 +00:00
auto cmd = maybe_cmd.release_value();
if (cmd.is_empty()) continue;
2023-04-08 10:09:43 +00:00
auto command = TRY(parse_command(cmd.view()));
if (command->args.size() < 1) continue;
TRY(execute_command_in_subprocess(move(command), input_file, interactive));
}
2023-07-21 19:06:10 +00:00
return 0;
2023-07-21 19:06:10 +00:00
}
Result<void> execute_rc_file()
2023-07-21 19:06:10 +00:00
{
static const StringView rc_file_name = ".shellrc"_sv;
auto home_path = TRY(os::FileSystem::home_directory());
auto home_dir = TRY(os::Directory::open(home_path.view()));
2023-07-21 19:06:10 +00:00
auto rc_file_path = os::Path { home_dir->fd(), rc_file_name };
if (os::FileSystem::exists(rc_file_path))
2023-07-21 19:06:10 +00:00
{
auto rc_file = TRY(os::File::open(rc_file_path, os::File::ReadOnly));
TRY(execute_loop(false, rc_file));
2023-07-21 19:06:10 +00:00
}
return {};
}
2023-04-13 15:04:59 +00:00
Result<int> luna_main(int argc, char** argv)
2023-04-08 10:09:43 +00:00
{
2023-04-13 16:33:43 +00:00
StringView path;
2023-04-08 10:09:43 +00:00
StringView command;
bool interactive { false };
2023-04-08 10:09:43 +00:00
2023-04-13 16:33:43 +00:00
SharedPtr<File> input_file;
2023-04-08 10:09:43 +00:00
os::ArgumentParser parser;
parser.add_description("The Luna system's command shell."_sv);
parser.add_system_program_info("sh"_sv);
2023-04-13 16:33:43 +00:00
parser.add_positional_argument(path, "path"_sv, "-"_sv);
parser.add_value_argument(command, 'c', "command"_sv, "execute a single command and then exit"_sv);
parser.add_switch_argument(interactive, 'i', "interactive"_sv, "run an interactive shell"_sv);
2023-04-08 10:09:43 +00:00
parser.parse(argc, argv);
TRY(init_builtins());
if (!command.is_empty())
{
auto cmd_obj = TRY(parse_command(command));
execute_command_or_builtin(move(cmd_obj));
}
2023-04-08 10:09:43 +00:00
if (path == "-") { input_file = File::standard_input(); }
2023-04-13 16:33:43 +00:00
else
2023-04-18 14:42:43 +00:00
{
2023-04-13 16:33:43 +00:00
input_file = TRY(File::open(path, File::ReadOnly));
2023-04-18 14:42:43 +00:00
input_file->set_close_on_exec();
}
2023-04-08 10:09:43 +00:00
if (isatty(input_file->fd())) interactive = true;
if (interactive)
{
setup_prompt();
signal(SIGTTOU, SIG_IGN);
signal(SIGINT, sigint_handler);
tcsetpgrp(input_file->fd(), getpgid(0));
2023-06-03 14:59:18 +00:00
TRY(execute_rc_file());
2023-03-23 21:19:54 +00:00
}
2023-04-13 16:33:43 +00:00
return execute_loop(interactive, input_file);
}