2023-05-02 08:50:39 +00:00
|
|
|
#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>
|
2023-04-24 20:13:06 +00:00
|
|
|
#include <pwd.h>
|
2023-07-10 19:39:22 +00:00
|
|
|
#include <signal.h>
|
2023-03-23 21:19:54 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-04-24 20:13:06 +00:00
|
|
|
#include <sys/utsname.h>
|
2023-07-12 17:23:06 +00:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2023-07-12 11:48:43 +00:00
|
|
|
// Do nothing, but we need a handler so read() returns EINTR.
|
|
|
|
static void sigint_handler(int)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:13:06 +00:00
|
|
|
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;
|
2023-04-19 17:16:45 +00:00
|
|
|
parser.add_description("The Luna system's command shell."_sv);
|
2023-04-28 14:33:05 +00:00
|
|
|
parser.add_system_program_info("sh"_sv);
|
2023-04-13 16:33:43 +00:00
|
|
|
parser.add_positional_argument(path, "path"_sv, "-"_sv);
|
2023-06-17 18:58:54 +00:00
|
|
|
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
|
|
|
|
2023-07-12 11:48: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
|
|
|
|
2023-07-12 11:48:43 +00:00
|
|
|
bool interactive = isatty(input_file->fd());
|
|
|
|
|
2023-04-24 20:13:06 +00:00
|
|
|
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
|
|
|
|
2023-05-11 18:10:10 +00:00
|
|
|
struct passwd* pw = getpwuid(getuid());
|
|
|
|
if (pw) { username = pw->pw_name; }
|
|
|
|
else { username = getenv("USER"); }
|
2023-05-18 19:47:46 +00:00
|
|
|
endpwent();
|
2023-07-12 11:48:43 +00:00
|
|
|
|
2023-07-12 11:49:37 +00:00
|
|
|
signal(SIGTTOU, SIG_IGN);
|
2023-07-12 11:48:43 +00:00
|
|
|
signal(SIGINT, sigint_handler);
|
2023-07-12 11:49:37 +00:00
|
|
|
|
2023-07-12 17:23:06 +00:00
|
|
|
tcsetpgrp(STDIN_FILENO, getpgid(0));
|
2023-04-24 20:13:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 21:19:54 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2023-04-13 16:33:43 +00:00
|
|
|
if (interactive)
|
2023-04-11 20:45:33 +00:00
|
|
|
{
|
2023-04-13 16:33:43 +00:00
|
|
|
auto cwd = TRY(os::FileSystem::working_directory());
|
2023-05-01 17:32:00 +00:00
|
|
|
os::print("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end);
|
2023-04-11 20:45:33 +00:00
|
|
|
}
|
2023-03-23 21:19:54 +00:00
|
|
|
|
2023-07-12 11:48:43 +00:00
|
|
|
auto maybe_cmd = input_file->read_line();
|
|
|
|
if (maybe_cmd.has_error())
|
|
|
|
{
|
2023-07-13 18:33:20 +00:00
|
|
|
if (maybe_cmd.error() == EINTR)
|
|
|
|
{
|
|
|
|
os::println("");
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-12 11:48:43 +00:00
|
|
|
return maybe_cmd.release_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cmd = maybe_cmd.release_value();
|
2023-05-20 10:07:56 +00:00
|
|
|
if (cmd.is_empty())
|
|
|
|
{
|
2023-05-26 15:29:41 +00:00
|
|
|
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
|
|
|
|
2023-07-12 11:49:37 +00:00
|
|
|
if (child == 0)
|
|
|
|
{
|
|
|
|
if (interactive)
|
|
|
|
{
|
|
|
|
setpgid(0, 0);
|
2023-07-12 17:23:06 +00:00
|
|
|
tcsetpgrp(STDIN_FILENO, getpid());
|
2023-07-12 11:49:37 +00:00
|
|
|
}
|
|
|
|
TRY(execute_command(cmd.view()));
|
|
|
|
}
|
2023-03-23 21:19:54 +00:00
|
|
|
|
2023-07-10 19:39:22 +00:00
|
|
|
int status;
|
|
|
|
TRY(os::Process::wait(child, &status));
|
|
|
|
|
2023-07-12 17:23:06 +00:00
|
|
|
if (interactive) tcsetpgrp(STDIN_FILENO, getpgid(0));
|
2023-07-12 11:49:37 +00:00
|
|
|
|
2023-07-10 19:39:22 +00:00
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
{
|
|
|
|
int sig = WTERMSIG(status);
|
|
|
|
if (sig != SIGINT && sig != SIGQUIT) os::println("[sh] Process %d exited: %s", child, strsignal(sig));
|
2023-07-13 18:33:20 +00:00
|
|
|
else
|
|
|
|
os::println("");
|
2023-07-10 19:39:22 +00:00
|
|
|
}
|
2023-03-23 21:19:54 +00:00
|
|
|
}
|
2023-04-13 16:33:43 +00:00
|
|
|
|
|
|
|
return 0;
|
2023-03-24 16:37:04 +00:00
|
|
|
}
|