Luna/apps/sh.cpp

164 lines
4.0 KiB
C++
Raw Normal View History

#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>
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 <pwd.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 <sys/utsname.h>
#include <termios.h>
2023-03-23 21:19:54 +00:00
#include <unistd.h>
2023-04-13 16:33:43 +00:00
using os::File;
2023-04-18 16:39:37 +00:00
static Result<Vector<String>> split_command_into_args(StringView cmd)
2023-03-23 21:19:54 +00:00
{
2023-04-18 16:39:37 +00:00
return cmd.split(" \n"_sv);
2023-03-23 21:19:54 +00:00
}
2023-04-18 16:39:37 +00:00
static Result<void> execute_command(StringView command)
2023-03-23 21:19:54 +00:00
{
2023-06-03 14:59:18 +00:00
if (strcspn(command.chars(), " #") == 0) return {};
2023-04-18 16:39:37 +00:00
auto args = TRY(split_command_into_args(command));
if (args.size() < 1) exit(0);
2023-04-08 10:09:43 +00:00
2023-04-18 16:39:37 +00:00
return os::Process::exec(args[0].view(), args.slice());
2023-04-08 10:09:43 +00:00
}
// Do nothing, but we need a handler so read() returns EINTR.
static void sigint_handler(int)
{
}
struct utsname g_sysinfo;
const char* hostname = "";
const char* username = "";
char prompt_end = '$';
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;
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);
2023-04-08 10:09:43 +00:00
parser.parse(argc, argv);
2023-04-18 16:39:37 +00:00
if (!command.is_empty()) TRY(execute_command(command));
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
bool interactive = isatty(input_file->fd());
if (interactive)
{
// Set up everything to form a prompt.
uname(&g_sysinfo);
hostname = g_sysinfo.nodename;
if (getuid() == 0) prompt_end = '#';
2023-05-06 10:19:54 +00:00
struct passwd* pw = getpwuid(getuid());
if (pw) { username = pw->pw_name; }
else { username = getenv("USER"); }
endpwent();
signal(SIGTTOU, SIG_IGN);
signal(SIGINT, sigint_handler);
tcsetpgrp(STDIN_FILENO, getpgid(0));
}
2023-03-23 21:19:54 +00:00
while (1)
{
2023-04-13 16:33:43 +00:00
if (interactive)
{
2023-04-13 16:33:43 +00:00
auto cwd = TRY(os::FileSystem::working_directory());
os::print("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end);
}
2023-03-23 21:19:54 +00:00
auto maybe_cmd = input_file->read_line();
if (maybe_cmd.has_error())
{
if (maybe_cmd.error() == EINTR)
{
os::println("");
continue;
}
return maybe_cmd.release_error();
}
auto cmd = maybe_cmd.release_value();
2023-05-20 10:07:56 +00:00
if (cmd.is_empty())
{
if (interactive) puts("exit");
2023-05-20 10:07:56 +00:00
break;
}
2023-03-23 21:19:54 +00:00
2023-04-13 16:33:43 +00:00
if (strspn(cmd.chars(), " \n") == cmd.length()) continue;
2023-06-03 14:59:18 +00:00
if (strcspn(cmd.chars(), " #") == 0) continue;
2023-04-13 16:33:43 +00:00
if (!strncmp(cmd.chars(), "cd", 2))
{
2023-04-18 16:39:37 +00:00
auto args = TRY(split_command_into_args(cmd.view()));
check(args[0].view() == "cd");
2023-04-13 16:33:43 +00:00
2023-04-18 16:39:37 +00:00
if (args.size() == 1)
2023-04-13 16:33:43 +00:00
{
auto home = TRY(os::FileSystem::home_directory());
2023-04-18 16:46:19 +00:00
TRY(os::FileSystem::change_directory(home.view()));
2023-04-13 16:33:43 +00:00
continue;
}
2023-04-18 16:46:19 +00:00
TRY(os::FileSystem::change_directory(args[1].view()));
2023-04-13 16:33:43 +00:00
continue;
}
2023-03-24 19:53:53 +00:00
2023-04-18 16:16:24 +00:00
pid_t child = TRY(os::Process::fork());
2023-03-23 21:19:54 +00:00
if (child == 0)
{
if (interactive)
{
setpgid(0, 0);
tcsetpgrp(STDIN_FILENO, getpid());
}
TRY(execute_command(cmd.view()));
}
2023-03-23 21:19:54 +00:00
int status;
TRY(os::Process::wait(child, &status));
if (interactive) tcsetpgrp(STDIN_FILENO, getpgid(0));
if (WIFSIGNALED(status))
{
int sig = WTERMSIG(status);
if (sig != SIGINT && sig != SIGQUIT) os::println("[sh] Process %d exited: %s", child, strsignal(sig));
else
os::println("");
}
2023-03-23 21:19:54 +00:00
}
2023-04-13 16:33:43 +00:00
return 0;
}