forked from Bananymous/banan-os
Shell: Move builtin commands to a hash map
This allows accessing builtin commands outside of `execute_builtin`
This commit is contained in:
parent
6f90974896
commit
ae073a336d
|
@ -1,5 +1,5 @@
|
|||
#include <BAN/HashMap.h>
|
||||
#include <BAN/Optional.h>
|
||||
#include <BAN/ScopeGuard.h>
|
||||
#include <BAN/String.h>
|
||||
#include <BAN/Vector.h>
|
||||
|
||||
|
@ -52,6 +52,12 @@ struct CommandList
|
|||
BAN::Vector<Command> commands;
|
||||
};
|
||||
|
||||
struct BuiltinCommand
|
||||
{
|
||||
int (*function)(const SingleCommand&, FILE* fout, int fd_in, int fd_out);
|
||||
};
|
||||
static BAN::HashMap<BAN::String, BuiltinCommand> s_builtin_commands;
|
||||
|
||||
static BAN::StringView strip_whitespace(BAN::StringView sv)
|
||||
{
|
||||
size_t leading = 0;
|
||||
|
@ -374,6 +380,10 @@ static BAN::Optional<int> execute_builtin(const SingleCommand& command, int fd_i
|
|||
if (command.arguments.empty())
|
||||
return 0;
|
||||
|
||||
auto it = s_builtin_commands.find(command.arguments.front());
|
||||
if (it == s_builtin_commands.end())
|
||||
return {};
|
||||
|
||||
FILE* fout = stdout;
|
||||
bool should_close = false;
|
||||
if (fd_out != STDOUT_FILENO)
|
||||
|
@ -386,14 +396,28 @@ static BAN::Optional<int> execute_builtin(const SingleCommand& command, int fd_i
|
|||
ERROR_RETURN("fdopen", 1);
|
||||
should_close = true;
|
||||
}
|
||||
BAN::ScopeGuard _([fout, should_close] { if (should_close) fclose(fout); });
|
||||
|
||||
if (command.arguments.front() == "clear"_sv)
|
||||
int ret = it->value.function(command, fout, fd_in, fd_out);
|
||||
|
||||
if (should_close)
|
||||
fclose(fout);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void install_builtin_commands()
|
||||
{
|
||||
MUST(s_builtin_commands.emplace("clear"_sv,
|
||||
[](const SingleCommand&, FILE* fout, int, int) -> int
|
||||
{
|
||||
fprintf(fout, "\e[H\e[2J");
|
||||
fprintf(fout, "\e[H\e[3J");
|
||||
fflush(fout);
|
||||
return 0;
|
||||
}
|
||||
else if (command.arguments.front() == "exit"_sv)
|
||||
));
|
||||
|
||||
MUST(s_builtin_commands.emplace("exit"_sv,
|
||||
[](const SingleCommand& command, FILE*, int, int) -> int
|
||||
{
|
||||
int exit_code = 0;
|
||||
if (command.arguments.size() > 1)
|
||||
|
@ -403,8 +427,12 @@ static BAN::Optional<int> execute_builtin(const SingleCommand& command, int fd_i
|
|||
exit_code = (exit_code * 10) + (exit_string[i] - '0');
|
||||
}
|
||||
exit(exit_code);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
else if (command.arguments.front() == "export"_sv)
|
||||
));
|
||||
|
||||
MUST(s_builtin_commands.emplace("export"_sv,
|
||||
[](const SingleCommand& command, FILE*, int, int) -> int
|
||||
{
|
||||
bool first = false;
|
||||
for (const auto& argument : command.arguments)
|
||||
|
@ -422,8 +450,12 @@ static BAN::Optional<int> execute_builtin(const SingleCommand& command, int fd_i
|
|||
if (setenv(BAN::String(split[0]).data(), BAN::String(split[1]).data(), true) == -1)
|
||||
ERROR_RETURN("setenv", 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if (command.arguments.front() == "source"_sv)
|
||||
));
|
||||
|
||||
MUST(s_builtin_commands.emplace("source"_sv,
|
||||
[](const SingleCommand& command, FILE* fout, int, int) -> int
|
||||
{
|
||||
if (command.arguments.size() != 2)
|
||||
{
|
||||
|
@ -432,13 +464,20 @@ static BAN::Optional<int> execute_builtin(const SingleCommand& command, int fd_i
|
|||
}
|
||||
return source_script(command.arguments[1]);
|
||||
}
|
||||
else if (command.arguments.front() == "env"_sv)
|
||||
));
|
||||
|
||||
MUST(s_builtin_commands.emplace("env"_sv,
|
||||
[](const SingleCommand&, FILE* fout, int, int) -> int
|
||||
{
|
||||
char** current = environ;
|
||||
while (*current)
|
||||
while (current && *current)
|
||||
fprintf(fout, "%s\n", *current++);
|
||||
return 0;
|
||||
}
|
||||
else if (command.arguments.front() == "cd"_sv)
|
||||
));
|
||||
|
||||
MUST(s_builtin_commands.emplace("cd"_sv,
|
||||
[](const SingleCommand& command, FILE* fout, int, int) -> int
|
||||
{
|
||||
if (command.arguments.size() > 2)
|
||||
{
|
||||
|
@ -460,13 +499,18 @@ static BAN::Optional<int> execute_builtin(const SingleCommand& command, int fd_i
|
|||
|
||||
if (chdir(path.data()) == -1)
|
||||
ERROR_RETURN("chdir", 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
else if (command.arguments.front() == "time"_sv)
|
||||
));
|
||||
|
||||
MUST(s_builtin_commands.emplace("time"_sv,
|
||||
[](const SingleCommand& command, FILE* fout, int fd_in, int fd_out) -> int
|
||||
{
|
||||
SingleCommand timed_command;
|
||||
MUST(timed_command.arguments.reserve(command.arguments.size() - 1));
|
||||
for (size_t i = 1; i < command.arguments.size(); i++)
|
||||
timed_command.arguments[i - 1] = command.arguments[i];
|
||||
MUST(timed_command.arguments.emplace_back(command.arguments[i]));
|
||||
|
||||
timespec start, end;
|
||||
|
||||
|
@ -489,21 +533,22 @@ static BAN::Optional<int> execute_builtin(const SingleCommand& command, int fd_i
|
|||
|
||||
return ret;
|
||||
}
|
||||
else if (command.arguments.front() == "start-gui"_sv)
|
||||
));
|
||||
|
||||
MUST(s_builtin_commands.emplace("start-gui"_sv,
|
||||
[](const SingleCommand&, FILE*, int, int) -> int
|
||||
{
|
||||
pid_t pid = fork();
|
||||
const pid_t pid = fork();
|
||||
if (pid == -1)
|
||||
return 1;
|
||||
if (pid == 0)
|
||||
execl("/bin/WindowServer", "WindowServer", NULL);
|
||||
if (fork() == 0)
|
||||
execl("/bin/Terminal", "Terminal", NULL);
|
||||
waitpid(pid, nullptr, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
static pid_t execute_command_no_wait(const SingleCommand& command, int fd_in, int fd_out, pid_t pgrp)
|
||||
|
@ -930,6 +975,8 @@ int main(int argc, char** argv)
|
|||
|
||||
atexit([]() { tcsetattr(0, TCSANOW, &old_termios); });
|
||||
|
||||
install_builtin_commands();
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (argv[i][0] != '-')
|
||||
|
|
Loading…
Reference in New Issue