2023-07-21 18:44:01 +00:00
|
|
|
#include "sh.h"
|
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-07-21 18:44:01 +00:00
|
|
|
#include <luna/HashMap.h>
|
|
|
|
|
|
|
|
extern shell_builtin_t builtin_cd;
|
2023-07-21 19:06:10 +00:00
|
|
|
extern shell_builtin_t builtin_exit;
|
|
|
|
extern shell_builtin_t builtin_set;
|
|
|
|
extern shell_builtin_t builtin_unset;
|
2023-07-21 18:44:01 +00:00
|
|
|
|
|
|
|
static HashMap<StringView, shell_builtin_t> s_builtins;
|
|
|
|
|
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-21 18:44:01 +00:00
|
|
|
static Result<void> init_builtins()
|
|
|
|
{
|
|
|
|
s_builtins = {};
|
|
|
|
|
|
|
|
TRY(s_builtins.try_set("cd"_sv, builtin_cd));
|
2023-07-21 19:06:10 +00:00
|
|
|
TRY(s_builtins.try_set("exit"_sv, builtin_exit));
|
|
|
|
TRY(s_builtins.try_set("set"_sv, builtin_set));
|
|
|
|
TRY(s_builtins.try_set("unset"_sv, builtin_unset));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Result<void> builtin_wrapper(const Vector<String>& args, shell_builtin_t builtin)
|
|
|
|
{
|
|
|
|
Vector<const char*> argv;
|
|
|
|
for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); }
|
|
|
|
TRY(argv.try_append(nullptr));
|
|
|
|
|
|
|
|
auto rc = builtin((int)args.size(), const_cast<char**>(argv.data()));
|
|
|
|
if (rc.has_error())
|
|
|
|
{
|
|
|
|
errno = rc.error();
|
|
|
|
perror(argv[0]);
|
|
|
|
}
|
2023-07-21 18:44:01 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
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-09-14 19:31:45 +00:00
|
|
|
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;
|
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-09-14 19:31:45 +00:00
|
|
|
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);
|
|
|
|
|
2023-07-21 19:06:10 +00:00
|
|
|
// TODO: This does not properly handle builtins.
|
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-09-14 19:31:45 +00:00
|
|
|
if (isatty(input_file->fd())) interactive = true;
|
2023-07-12 11:48:43 +00:00
|
|
|
|
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-21 19:08:27 +00:00
|
|
|
tcsetpgrp(input_file->fd(), getpgid(0));
|
2023-04-24 20:13:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 19:06:10 +00:00
|
|
|
TRY(init_builtins());
|
2023-07-21 18:44:01 +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-09-02 17:35:42 +00:00
|
|
|
os::print("\x1b[%dm%s\x1b[m@\x1b[36m%s\x1b[m:\x1b[1;34m%s\x1b[m%c ", getuid() == 0 ? 31 : 35, 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)
|
|
|
|
{
|
2024-03-29 13:42:38 +00:00
|
|
|
os::print("\n");
|
2023-07-13 18:33:20 +00:00
|
|
|
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-07-21 18:44:01 +00:00
|
|
|
auto args = TRY(split_command_into_args(cmd.view()));
|
|
|
|
if (args.size() < 1) continue;
|
|
|
|
|
|
|
|
if (!strchr(args[0].chars(), '/'))
|
2023-04-13 16:33:43 +00:00
|
|
|
{
|
2023-07-21 18:44:01 +00:00
|
|
|
auto maybe_builtin = s_builtins.try_get(args[0].view());
|
2023-04-13 16:33:43 +00:00
|
|
|
|
2023-07-21 18:44:01 +00:00
|
|
|
if (maybe_builtin.has_value())
|
2023-04-13 16:33:43 +00:00
|
|
|
{
|
2023-07-21 18:44:01 +00:00
|
|
|
auto builtin = maybe_builtin.value();
|
|
|
|
|
2023-07-21 19:06:10 +00:00
|
|
|
TRY(builtin_wrapper(args, builtin));
|
2023-07-21 18:44:01 +00:00
|
|
|
|
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-21 18:44:01 +00:00
|
|
|
tcsetpgrp(input_file->fd(), getpid());
|
2023-07-12 11:49:37 +00:00
|
|
|
}
|
2023-07-21 18:44:01 +00:00
|
|
|
TRY(os::Process::exec(args[0].view(), args.slice()));
|
2023-07-12 11:49:37 +00:00
|
|
|
}
|
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-21 18:44:01 +00:00
|
|
|
if (interactive) tcsetpgrp(input_file->fd(), 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
|
2024-03-29 13:42:38 +00:00
|
|
|
os::print("\n");
|
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
|
|
|
}
|