Luna/apps/sh.cpp
apio 546d900454
All checks were successful
continuous-integration/drone/push Build is passing
libc+apps: Start implementing POSIX-compliant termios.h wrappers around tty ioctls
2023-07-12 19:23:06 +02:00

158 lines
3.9 KiB
C++

#include <errno.h>
#include <luna/String.h>
#include <luna/Vector.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <os/FileSystem.h>
#include <os/Process.h>
#include <pwd.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <termios.h>
#include <unistd.h>
using os::File;
static Result<Vector<String>> split_command_into_args(StringView cmd)
{
return cmd.split(" \n"_sv);
}
static Result<void> execute_command(StringView command)
{
if (strcspn(command.chars(), " #") == 0) return {};
auto args = TRY(split_command_into_args(command));
if (args.size() < 1) exit(0);
return os::Process::exec(args[0].view(), args.slice());
}
// 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 = '$';
Result<int> luna_main(int argc, char** argv)
{
StringView path;
StringView command;
SharedPtr<File> input_file;
os::ArgumentParser parser;
parser.add_description("The Luna system's command shell."_sv);
parser.add_system_program_info("sh"_sv);
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.parse(argc, argv);
if (!command.is_empty()) TRY(execute_command(command));
if (path == "-") { input_file = File::standard_input(); }
else
{
input_file = TRY(File::open(path, File::ReadOnly));
input_file->set_close_on_exec();
}
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 = '#';
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));
}
while (1)
{
if (interactive)
{
auto cwd = TRY(os::FileSystem::working_directory());
os::print("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end);
}
auto maybe_cmd = input_file->read_line();
if (maybe_cmd.has_error())
{
if (maybe_cmd.error() == EINTR) continue;
return maybe_cmd.release_error();
}
auto cmd = maybe_cmd.release_value();
if (cmd.is_empty())
{
if (interactive) puts("exit");
break;
}
if (strspn(cmd.chars(), " \n") == cmd.length()) continue;
if (strcspn(cmd.chars(), " #") == 0) continue;
if (!strncmp(cmd.chars(), "cd", 2))
{
auto args = TRY(split_command_into_args(cmd.view()));
check(args[0].view() == "cd");
if (args.size() == 1)
{
auto home = TRY(os::FileSystem::home_directory());
TRY(os::FileSystem::change_directory(home.view()));
continue;
}
TRY(os::FileSystem::change_directory(args[1].view()));
continue;
}
pid_t child = TRY(os::Process::fork());
if (child == 0)
{
if (interactive)
{
setpgid(0, 0);
tcsetpgrp(STDIN_FILENO, getpid());
}
TRY(execute_command(cmd.view()));
}
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));
}
}
return 0;
}