Luna/apps/sh.cpp

134 lines
2.9 KiB
C++
Raw Normal View History

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-03-29 15:56:56 +00:00
2023-03-23 21:19:54 +00:00
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2023-03-23 21:42:24 +00:00
#include <sys/wait.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 14:49:05 +00:00
static Result<Vector<char*>> split_command_into_argv(StringView cmd, char** out)
2023-03-23 21:19:54 +00:00
{
2023-04-13 16:33:43 +00:00
char* str = strdup(cmd.chars());
2023-03-29 15:56:56 +00:00
Vector<char*> result;
2023-03-23 21:19:54 +00:00
char* segment = strtok(str, " \n");
2023-03-29 15:56:56 +00:00
TRY(result.try_append(segment));
2023-03-23 21:19:54 +00:00
2023-03-29 15:56:56 +00:00
if (segment == NULL) return result;
2023-03-23 21:19:54 +00:00
for (;;)
{
segment = strtok(NULL, " \n");
2023-03-29 15:56:56 +00:00
TRY(result.try_append(segment));
2023-03-23 21:19:54 +00:00
2023-03-29 15:56:56 +00:00
if (segment == NULL) return result;
2023-03-23 21:19:54 +00:00
}
2023-04-18 14:49:05 +00:00
if (out) *out = str;
2023-03-29 15:56:56 +00:00
return result;
2023-03-23 21:19:54 +00:00
}
2023-04-13 16:33:43 +00:00
[[noreturn]] static void execute_command(StringView command)
2023-03-23 21:19:54 +00:00
{
2023-04-08 10:09:43 +00:00
Vector<char*> argv;
2023-04-18 14:49:05 +00:00
bool ok = split_command_into_argv(command, nullptr).try_move_value_or_error(argv, errno);
2023-04-08 10:09:43 +00:00
if (!ok)
{
perror("failed to parse command");
exit(1);
}
if (argv[0] == NULL) exit(0);
execvp(argv[0], argv.data());
perror(argv[0]);
exit(1);
}
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
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-13 16:33:43 +00:00
parser.add_positional_argument(path, "path"_sv, "-"_sv);
2023-04-08 10:09:43 +00:00
parser.add_value_argument(command, 'c', "command"_sv, true);
parser.parse(argc, argv);
2023-04-13 16:33:43 +00:00
if (!command.is_empty()) execute_command(command);
2023-04-08 10:09:43 +00:00
2023-04-13 16:33:43 +00:00
if (path == "-")
2023-04-08 10:09:43 +00:00
{
2023-04-13 16:33:43 +00:00
input_file = File::standard_input();
interactive = true;
2023-04-08 10:09:43 +00:00
}
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-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());
printf("sh %s%c ", cwd.chars(), getuid() == 0 ? '#' : '$');
}
2023-03-23 21:19:54 +00:00
2023-04-13 16:33:43 +00:00
auto cmd = TRY(input_file->read_line());
if (cmd.is_empty()) 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;
if (!strncmp(cmd.chars(), "cd", 2))
{
2023-04-18 14:49:05 +00:00
char* copy = nullptr;
auto args = TRY(split_command_into_argv(cmd.view(), &copy));
2023-04-13 16:33:43 +00:00
check("cd"_sv == args[0]);
if (args.size() == 2)
{
auto home = TRY(os::FileSystem::home_directory());
if (chdir(home.chars()) < 0) perror("cd");
2023-04-18 14:49:05 +00:00
free(copy);
2023-04-13 16:33:43 +00:00
continue;
}
if (chdir(args[1]) < 0) perror("cd");
2023-04-18 14:49:05 +00:00
free(copy);
2023-04-13 16:33:43 +00:00
continue;
}
2023-03-24 19:53:53 +00:00
2023-03-23 21:19:54 +00:00
pid_t child = fork();
if (child < 0)
{
perror("fork");
return 1;
}
2023-03-23 21:19:54 +00:00
2023-04-13 16:33:43 +00:00
if (child == 0) { execute_command(cmd.view()); }
2023-03-23 21:19:54 +00:00
if (waitpid(child, NULL, 0) < 0)
{
perror("waitpid");
return 1;
}
2023-03-23 21:19:54 +00:00
}
2023-04-13 16:33:43 +00:00
return 0;
}