2023-06-19 01:39:24 +03:00
|
|
|
#include <BAN/Optional.h>
|
2023-06-05 01:42:57 +03:00
|
|
|
#include <BAN/ScopeGuard.h>
|
|
|
|
#include <BAN/String.h>
|
|
|
|
#include <BAN/Vector.h>
|
2023-06-19 01:39:24 +03:00
|
|
|
|
2023-06-05 14:36:17 +03:00
|
|
|
#include <ctype.h>
|
2023-07-06 00:39:04 +03:00
|
|
|
#include <inttypes.h>
|
2023-08-14 14:55:23 +03:00
|
|
|
#include <pwd.h>
|
2023-05-16 19:22:46 +03:00
|
|
|
#include <stdio.h>
|
2023-06-05 01:42:57 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
2023-05-16 19:22:46 +03:00
|
|
|
#include <termios.h>
|
2023-06-05 01:42:57 +03:00
|
|
|
#include <unistd.h>
|
2023-05-16 19:22:46 +03:00
|
|
|
|
2023-07-06 23:22:57 +03:00
|
|
|
#define ERROR_RETURN(__msg, __ret) do { perror(__msg); return __ret; } while (false)
|
2023-07-06 00:39:04 +03:00
|
|
|
|
2023-05-16 19:22:46 +03:00
|
|
|
struct termios old_termios, new_termios;
|
|
|
|
|
2023-07-06 10:32:43 +03:00
|
|
|
extern char** environ;
|
|
|
|
|
2023-07-06 23:22:57 +03:00
|
|
|
static const char* argv0 = nullptr;
|
2023-08-15 09:17:46 +03:00
|
|
|
static int last_return = 0;
|
2023-07-06 23:22:57 +03:00
|
|
|
|
2023-08-22 15:33:01 +03:00
|
|
|
static BAN::String hostname;
|
|
|
|
|
2023-09-07 15:51:05 +03:00
|
|
|
static void clean_exit()
|
|
|
|
{
|
|
|
|
tcsetattr(0, TCSANOW, &old_termios);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView);
|
2023-07-06 23:22:57 +03:00
|
|
|
|
2023-06-19 01:39:24 +03:00
|
|
|
BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i)
|
|
|
|
{
|
|
|
|
ASSERT(command[i] == '$');
|
|
|
|
|
|
|
|
if (++i >= command.size())
|
|
|
|
return "$"sv;
|
2023-08-15 09:17:46 +03:00
|
|
|
|
|
|
|
if (command[i] == '?')
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
return BAN::String::formatted("{}", last_return);
|
|
|
|
}
|
2023-06-19 01:39:24 +03:00
|
|
|
if (isalnum(command[i]))
|
|
|
|
{
|
|
|
|
size_t len = 1;
|
|
|
|
for (; i + len < command.size(); len++)
|
|
|
|
if (!isalnum(command[i + len]))
|
|
|
|
break;
|
|
|
|
BAN::String name = command.substring(i, len);
|
|
|
|
i += len - 1;
|
|
|
|
|
|
|
|
if (const char* value = getenv(name.data()))
|
|
|
|
return BAN::StringView(value);
|
|
|
|
return ""sv;
|
|
|
|
}
|
|
|
|
else if (command[i] == '{')
|
|
|
|
{
|
|
|
|
size_t len = 1;
|
|
|
|
for (; i + len < command.size(); len++)
|
|
|
|
{
|
|
|
|
if (command[i + len] == '}')
|
|
|
|
break;
|
|
|
|
if (!isalnum(command[i + len]))
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i + len >= command.size())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
BAN::String name = command.substring(i + 1, len - 1);
|
|
|
|
i += len;
|
|
|
|
|
|
|
|
if (const char* value = getenv(name.data()))
|
|
|
|
return BAN::StringView(value);
|
|
|
|
return ""sv;
|
|
|
|
}
|
|
|
|
else if (command[i] == '[')
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
else if (command[i] == '(')
|
|
|
|
{
|
2023-07-06 23:22:57 +03:00
|
|
|
size_t len = 1;
|
|
|
|
int count = 1;
|
|
|
|
for (; i + len < command.size(); len++)
|
|
|
|
{
|
|
|
|
if (command[i + len] == '(')
|
|
|
|
count++;
|
|
|
|
if (command[i + len] == ')')
|
|
|
|
count--;
|
|
|
|
if (count == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count != 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
BAN::String subcommand = command.substring(i + 1, len - 1);
|
|
|
|
|
|
|
|
char temp[3] { '-', 'c', '\0' };
|
|
|
|
BAN::Vector<char*> argv;
|
|
|
|
MUST(argv.push_back((char*)argv0));
|
|
|
|
MUST(argv.push_back(temp));
|
|
|
|
MUST(argv.push_back((char*)subcommand.data()));
|
|
|
|
MUST(argv.push_back(nullptr));
|
|
|
|
|
|
|
|
int fds[2];
|
|
|
|
if (pipe(fds) == -1)
|
|
|
|
ERROR_RETURN("pipe", {});
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
if (dup2(fds[1], STDOUT_FILENO) == -1)
|
|
|
|
{
|
|
|
|
perror("dup2");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
|
|
|
|
execv(argv.front(), argv.data());
|
|
|
|
perror("execv");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (pid == -1)
|
|
|
|
ERROR_RETURN("fork", {});
|
|
|
|
|
|
|
|
close(fds[1]);
|
|
|
|
|
|
|
|
char buffer[100];
|
|
|
|
BAN::String output;
|
|
|
|
while (ssize_t ret = read(fds[0], buffer, sizeof(buffer)))
|
|
|
|
{
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
perror("read");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MUST(output.append(BAN::StringView(buffer, ret)));
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fds[0]);
|
|
|
|
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) == -1 && errno != ECHILD)
|
|
|
|
ERROR_RETURN("waitpid", {});
|
|
|
|
|
|
|
|
while (!output.empty() && output.back() == '\n')
|
|
|
|
output.pop_back();
|
|
|
|
|
|
|
|
i += len;
|
|
|
|
return output;
|
2023-06-19 01:39:24 +03:00
|
|
|
}
|
|
|
|
|
2023-10-12 22:24:27 +03:00
|
|
|
BAN::String temp = "$"sv;
|
|
|
|
MUST(temp.push_back(command[i]));
|
|
|
|
return temp;
|
2023-06-19 01:39:24 +03:00
|
|
|
}
|
|
|
|
|
2023-09-28 10:28:49 +03:00
|
|
|
BAN::StringView strip_whitespace(BAN::StringView sv)
|
|
|
|
{
|
|
|
|
size_t leading = 0;
|
|
|
|
while (leading < sv.size() && isspace(sv[leading]))
|
|
|
|
leading++;
|
|
|
|
sv = sv.substring(leading);
|
|
|
|
|
|
|
|
size_t trailing = 0;
|
|
|
|
while (trailing < sv.size() && isspace(sv[sv.size() - trailing - 1]))
|
|
|
|
trailing++;
|
|
|
|
sv = sv.substring(0, sv.size() - trailing);
|
|
|
|
|
|
|
|
return sv;
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView command_view)
|
2023-06-05 01:42:57 +03:00
|
|
|
{
|
2023-06-19 01:07:00 +03:00
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
Normal,
|
|
|
|
SingleQuote,
|
|
|
|
DoubleQuote,
|
|
|
|
};
|
|
|
|
|
2023-09-28 10:28:49 +03:00
|
|
|
command_view = strip_whitespace(command_view);
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
BAN::Vector<BAN::Vector<BAN::String>> result;
|
|
|
|
BAN::Vector<BAN::String> command_args;
|
2023-06-19 01:07:00 +03:00
|
|
|
|
|
|
|
State state = State::Normal;
|
2023-08-17 12:05:38 +03:00
|
|
|
BAN::String current_arg;
|
|
|
|
for (size_t i = 0; i < command_view.size(); i++)
|
2023-06-19 01:07:00 +03:00
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
char c = command_view[i];
|
2023-06-19 01:39:24 +03:00
|
|
|
|
2023-06-19 01:07:00 +03:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case State::Normal:
|
|
|
|
if (c == '\'')
|
|
|
|
state = State::SingleQuote;
|
|
|
|
else if (c == '"')
|
|
|
|
state = State::DoubleQuote;
|
2023-06-19 01:39:24 +03:00
|
|
|
else if (c == '$')
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
auto expansion = parse_dollar(command_view, i);
|
2023-06-19 01:39:24 +03:00
|
|
|
if (!expansion.has_value())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "bad substitution\n");
|
|
|
|
return {};
|
|
|
|
}
|
2023-08-17 12:05:38 +03:00
|
|
|
MUST(current_arg.append(expansion.value()));
|
|
|
|
}
|
|
|
|
else if (c == '|')
|
|
|
|
{
|
|
|
|
if (!current_arg.empty())
|
|
|
|
MUST(command_args.push_back(current_arg));
|
|
|
|
current_arg.clear();
|
|
|
|
|
|
|
|
MUST(result.push_back(command_args));
|
|
|
|
command_args.clear();
|
2023-06-19 01:39:24 +03:00
|
|
|
}
|
2023-06-19 01:07:00 +03:00
|
|
|
else if (!isspace(c))
|
2023-08-17 12:05:38 +03:00
|
|
|
MUST(current_arg.push_back(c));
|
2023-06-19 01:07:00 +03:00
|
|
|
else
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
if (!current_arg.empty())
|
2023-06-19 01:07:00 +03:00
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
MUST(command_args.push_back(current_arg));
|
|
|
|
current_arg.clear();
|
2023-06-19 01:07:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case State::SingleQuote:
|
|
|
|
if (c == '\'')
|
|
|
|
state = State::Normal;
|
|
|
|
else
|
2023-08-17 12:05:38 +03:00
|
|
|
MUST(current_arg.push_back(c));
|
2023-06-19 01:07:00 +03:00
|
|
|
break;
|
|
|
|
case State::DoubleQuote:
|
|
|
|
if (c == '"')
|
|
|
|
state = State::Normal;
|
2023-06-19 01:39:24 +03:00
|
|
|
else if (c != '$')
|
2023-08-17 12:05:38 +03:00
|
|
|
MUST(current_arg.push_back(c));
|
2023-06-19 01:39:24 +03:00
|
|
|
else
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
auto expansion = parse_dollar(command_view, i);
|
2023-06-19 01:39:24 +03:00
|
|
|
if (!expansion.has_value())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "bad substitution\n");
|
|
|
|
return {};
|
|
|
|
}
|
2023-08-17 12:05:38 +03:00
|
|
|
MUST(current_arg.append(expansion.value()));
|
2023-06-19 01:39:24 +03:00
|
|
|
}
|
2023-06-19 01:07:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: handle state != State::Normal
|
2023-08-17 12:05:38 +03:00
|
|
|
MUST(command_args.push_back(BAN::move(current_arg)));
|
|
|
|
MUST(result.push_back(BAN::move(command_args)));
|
2023-06-19 01:07:00 +03:00
|
|
|
|
2023-06-05 01:42:57 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
int execute_command(BAN::Vector<BAN::String>& args, int fd_in, int fd_out);
|
|
|
|
|
2023-10-03 10:24:10 +03:00
|
|
|
int source_script(const BAN::String& path);
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, int fd_out)
|
2023-05-16 19:22:46 +03:00
|
|
|
{
|
2023-06-05 01:42:57 +03:00
|
|
|
if (args.empty())
|
|
|
|
return 0;
|
2023-08-17 12:05:38 +03:00
|
|
|
|
|
|
|
FILE* fout = stdout;
|
|
|
|
bool should_close = false;
|
|
|
|
if (fd_out != STDOUT_FILENO)
|
|
|
|
{
|
|
|
|
int fd_dup = dup(fd_out);
|
|
|
|
if (fd_dup == -1)
|
|
|
|
ERROR_RETURN("dup", 1);
|
|
|
|
fout = fdopen(fd_dup, "w");
|
|
|
|
if (fout == nullptr)
|
|
|
|
ERROR_RETURN("fdopen", 1);
|
|
|
|
should_close = true;
|
|
|
|
}
|
|
|
|
BAN::ScopeGuard _([fout, should_close] { if (should_close) fclose(fout); });
|
|
|
|
|
2023-06-05 01:42:57 +03:00
|
|
|
if (args.front() == "clear"sv)
|
|
|
|
{
|
2023-11-28 23:55:37 +02:00
|
|
|
fprintf(fout, "\e[H\e[2J");
|
2023-08-17 12:05:38 +03:00
|
|
|
fflush(fout);
|
2023-06-05 01:42:57 +03:00
|
|
|
}
|
2023-06-09 01:51:23 +03:00
|
|
|
else if (args.front() == "exit"sv)
|
|
|
|
{
|
2023-09-07 15:51:05 +03:00
|
|
|
clean_exit();
|
2023-06-09 01:51:23 +03:00
|
|
|
}
|
2023-06-12 00:45:47 +03:00
|
|
|
else if (args.front() == "export"sv)
|
|
|
|
{
|
|
|
|
bool first = false;
|
2023-06-19 00:34:44 +03:00
|
|
|
for (const auto& arg : args)
|
2023-06-12 00:45:47 +03:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-19 00:34:44 +03:00
|
|
|
auto split = MUST(arg.sv().split('=', true));
|
|
|
|
if (split.size() != 2)
|
2023-06-12 00:45:47 +03:00
|
|
|
continue;
|
|
|
|
|
2023-06-19 00:34:44 +03:00
|
|
|
if (setenv(BAN::String(split[0]).data(), BAN::String(split[1]).data(), true) == -1)
|
2023-07-06 23:22:57 +03:00
|
|
|
ERROR_RETURN("setenv", 1);
|
2023-06-12 00:45:47 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 10:24:10 +03:00
|
|
|
else if (args.front() == "source"sv)
|
|
|
|
{
|
|
|
|
if (args.size() != 2)
|
|
|
|
{
|
|
|
|
fprintf(fout, "usage: source FILE\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return source_script(args[1]);
|
|
|
|
}
|
2023-07-06 10:32:43 +03:00
|
|
|
else if (args.front() == "env"sv)
|
|
|
|
{
|
|
|
|
char** current = environ;
|
|
|
|
while (*current)
|
2023-08-17 12:05:38 +03:00
|
|
|
fprintf(fout, "%s\n", *current++);
|
2023-07-06 10:32:43 +03:00
|
|
|
}
|
2023-07-30 14:17:39 +03:00
|
|
|
else if (args.front() == "page-fault-test"sv)
|
|
|
|
{
|
|
|
|
volatile int* ptr = nullptr;
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
2023-07-21 19:27:38 +03:00
|
|
|
else if (args.front() == "kill-test"sv)
|
2023-07-21 15:45:02 +03:00
|
|
|
{
|
2023-07-21 19:27:38 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
fprintf(fout, "child\n");
|
2023-07-21 19:27:38 +03:00
|
|
|
for (;;);
|
|
|
|
}
|
|
|
|
if (pid == -1)
|
|
|
|
{
|
|
|
|
perror("fork");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
if (kill(pid, SIGSEGV) == -1)
|
|
|
|
{
|
|
|
|
perror("kill");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-07-21 15:45:02 +03:00
|
|
|
}
|
2023-07-23 13:09:04 +03:00
|
|
|
else if (args.front() == "signal-test"sv)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
dup2(fileno(fout), STDOUT_FILENO);
|
2023-07-23 13:09:04 +03:00
|
|
|
if (signal(SIGSEGV, [](int) { printf("SIGSEGV\n"); }) == SIG_ERR)
|
|
|
|
{
|
|
|
|
perror("signal");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("child\n");
|
|
|
|
for (;;);
|
|
|
|
}
|
|
|
|
if (pid == -1)
|
|
|
|
{
|
|
|
|
perror("fork");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
if (kill(pid, SIGSEGV) == -1)
|
|
|
|
{
|
|
|
|
perror("kill");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
if (kill(pid, SIGTERM) == -1)
|
|
|
|
{
|
|
|
|
perror("kill");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 10:42:53 +03:00
|
|
|
else if (args.front() == "printf-test"sv)
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
fprintf(fout, " 0.0: %f\n", 0.0f);
|
|
|
|
fprintf(fout, " 123.0: %f\n", 123.0f);
|
|
|
|
fprintf(fout, " 0.123: %f\n", 0.123f);
|
|
|
|
fprintf(fout, " NAN: %f\n", NAN);
|
|
|
|
fprintf(fout, "+INF: %f\n", INFINITY);
|
|
|
|
fprintf(fout, "-INF: %f\n", -INFINITY);
|
2023-08-16 10:42:53 +03:00
|
|
|
}
|
2023-06-12 01:31:33 +03:00
|
|
|
else if (args.front() == "cd"sv)
|
|
|
|
{
|
|
|
|
if (args.size() > 2)
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
fprintf(fout, "cd: too many arguments\n");
|
2023-06-12 01:31:33 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-06-19 00:34:44 +03:00
|
|
|
BAN::StringView path;
|
2023-06-12 01:31:33 +03:00
|
|
|
|
|
|
|
if (args.size() == 1)
|
|
|
|
{
|
2023-06-19 00:34:44 +03:00
|
|
|
if (const char* path_env = getenv("HOME"))
|
|
|
|
path = path_env;
|
|
|
|
else
|
2023-06-12 01:31:33 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
path = args[1];
|
|
|
|
|
2023-06-19 00:34:44 +03:00
|
|
|
if (chdir(path.data()) == -1)
|
2023-07-06 23:22:57 +03:00
|
|
|
ERROR_RETURN("chdir", 1);
|
2023-06-12 01:31:33 +03:00
|
|
|
}
|
2023-07-06 09:14:14 +03:00
|
|
|
else if (args.front() == "time"sv)
|
2023-06-05 01:42:57 +03:00
|
|
|
{
|
2023-07-06 09:14:14 +03:00
|
|
|
args.remove(0);
|
|
|
|
|
|
|
|
timespec start, end;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
|
2023-07-06 23:22:57 +03:00
|
|
|
ERROR_RETURN("clock_gettime", 1);
|
2023-07-06 09:14:14 +03:00
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
int ret = execute_command(args, fd_in, fd_out);
|
2023-07-06 09:14:14 +03:00
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &end) == -1)
|
2023-07-06 23:22:57 +03:00
|
|
|
ERROR_RETURN("clock_gettime", 1);
|
2023-07-06 09:14:14 +03:00
|
|
|
|
|
|
|
uint64_t total_ns = 0;
|
|
|
|
total_ns += (end.tv_sec - start.tv_sec) * 1'000'000'000;
|
|
|
|
total_ns += end.tv_nsec - start.tv_nsec;
|
|
|
|
|
|
|
|
int secs = total_ns / 1'000'000'000;
|
|
|
|
int msecs = (total_ns % 1'000'000'000) / 1'000'000;
|
2023-07-06 00:39:04 +03:00
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
fprintf(fout, "took %d.%03d s\n", secs, msecs);
|
2023-07-06 09:14:14 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
return {};
|
|
|
|
}
|
2023-07-06 00:39:04 +03:00
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:54:50 +03:00
|
|
|
pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_out, pid_t pgrp)
|
2023-08-17 12:05:38 +03:00
|
|
|
{
|
|
|
|
if (args.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BAN::Vector<char*> cmd_args;
|
|
|
|
MUST(cmd_args.reserve(args.size() + 1));
|
|
|
|
for (const auto& arg : args)
|
|
|
|
MUST(cmd_args.push_back((char*)arg.data()));
|
|
|
|
MUST(cmd_args.push_back(nullptr));
|
|
|
|
|
2023-09-23 02:43:02 +03:00
|
|
|
// do PATH resolution
|
|
|
|
BAN::String executable_file;
|
|
|
|
if (!args.front().empty() && args.front().front() != '.' && args.front().front() != '/')
|
|
|
|
{
|
|
|
|
char* path_env_cstr = getenv("PATH");
|
|
|
|
if (path_env_cstr)
|
|
|
|
{
|
|
|
|
auto path_env_list = MUST(BAN::StringView(path_env_cstr).split(':'));
|
|
|
|
for (auto path_env : path_env_list)
|
|
|
|
{
|
|
|
|
BAN::String test_file = path_env;
|
|
|
|
MUST(test_file.push_back('/'));
|
|
|
|
MUST(test_file.append(args.front()));
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (stat(test_file.data(), &st) == 0)
|
|
|
|
{
|
|
|
|
executable_file = BAN::move(test_file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (executable_file.empty())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "command not found: %s\n", args.front().data());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
executable_file = args.front();
|
|
|
|
}
|
|
|
|
|
2023-12-04 22:57:27 +02:00
|
|
|
// Verify that the file exists is executable
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(executable_file.data(), &st) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "command not found: %s\n", args.front().data());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((st.st_mode & 0111) == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "permission denied: %s\n", executable_file.data());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
if (fd_in != STDIN_FILENO)
|
2023-06-05 01:42:57 +03:00
|
|
|
{
|
2023-08-17 12:05:38 +03:00
|
|
|
if (dup2(fd_in, STDIN_FILENO) == -1)
|
|
|
|
{
|
|
|
|
perror("dup2");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd_in);
|
2023-06-05 01:42:57 +03:00
|
|
|
}
|
2023-08-17 12:05:38 +03:00
|
|
|
if (fd_out != STDOUT_FILENO)
|
|
|
|
{
|
|
|
|
if (dup2(fd_out, STDOUT_FILENO) == -1)
|
|
|
|
{
|
|
|
|
perror("dup2");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd_out);
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:54:50 +03:00
|
|
|
if (pgrp == 0)
|
2023-08-22 11:37:04 +03:00
|
|
|
{
|
2023-08-22 14:54:50 +03:00
|
|
|
if(setpgid(0, 0) == -1)
|
|
|
|
{
|
|
|
|
perror("setpgid");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (tcsetpgrp(0, getpgrp()) == -1)
|
2023-08-22 11:37:04 +03:00
|
|
|
{
|
|
|
|
perror("tcsetpgrp");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2023-08-22 14:54:50 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
setpgid(0, pgrp);
|
|
|
|
}
|
2023-08-22 11:37:04 +03:00
|
|
|
|
2023-09-23 02:43:02 +03:00
|
|
|
execv(executable_file.data(), cmd_args.data());
|
2023-08-17 12:05:38 +03:00
|
|
|
perror("execv");
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-07-06 09:14:14 +03:00
|
|
|
|
2023-09-23 02:43:02 +03:00
|
|
|
if (pid == -1)
|
|
|
|
ERROR_RETURN("fork", -1);
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
return pid;
|
|
|
|
}
|
2023-07-28 18:10:36 +03:00
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
int execute_command(BAN::Vector<BAN::String>& args, int fd_in, int fd_out)
|
|
|
|
{
|
2023-08-22 14:54:50 +03:00
|
|
|
pid_t pid = execute_command_no_wait(args, fd_in, fd_out, 0);
|
2023-08-17 12:05:38 +03:00
|
|
|
if (pid == -1)
|
2023-09-23 02:43:02 +03:00
|
|
|
return 1;
|
2023-08-17 12:05:38 +03:00
|
|
|
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) == -1)
|
|
|
|
ERROR_RETURN("waitpid", 1);
|
|
|
|
|
2023-08-22 11:37:04 +03:00
|
|
|
if (tcsetpgrp(0, getpgrp()) == -1)
|
2023-08-17 12:05:38 +03:00
|
|
|
ERROR_RETURN("tcsetpgrp", 1);
|
|
|
|
|
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
fprintf(stderr, "Terminated by signal %d\n", WTERMSIG(status));
|
|
|
|
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int execute_piped_commands(BAN::Vector<BAN::Vector<BAN::String>>& commands)
|
|
|
|
{
|
|
|
|
if (commands.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (commands.size() == 1)
|
|
|
|
{
|
|
|
|
auto& command = commands.front();
|
|
|
|
if (auto ret = execute_builtin(command, STDIN_FILENO, STDOUT_FILENO); ret.has_value())
|
|
|
|
return ret.value();
|
|
|
|
return execute_command(command, STDIN_FILENO, STDOUT_FILENO);
|
|
|
|
}
|
|
|
|
|
|
|
|
BAN::Vector<int> exit_codes(commands.size(), 0);
|
|
|
|
BAN::Vector<pid_t> processes(commands.size(), -1);
|
2023-08-22 14:54:50 +03:00
|
|
|
pid_t pgrp = 0;
|
2023-08-17 12:05:38 +03:00
|
|
|
|
|
|
|
int next_stdin = STDIN_FILENO;
|
|
|
|
for (size_t i = 0; i < commands.size(); i++)
|
|
|
|
{
|
|
|
|
bool first = (i == 0);
|
|
|
|
bool last = (i == commands.size() - 1);
|
2023-07-06 00:39:04 +03:00
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
int pipefd[2] { -1, STDOUT_FILENO };
|
|
|
|
if (!last && pipe(pipefd) == -1)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
close(next_stdin);
|
|
|
|
perror("pipe");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto builtin_ret = execute_builtin(commands[i], next_stdin, pipefd[1]);
|
|
|
|
if (builtin_ret.has_value())
|
|
|
|
exit_codes[i] = builtin_ret.value();
|
|
|
|
else
|
|
|
|
{
|
2023-08-22 14:54:50 +03:00
|
|
|
pid_t pid = execute_command_no_wait(commands[i], next_stdin, pipefd[1], pgrp);
|
2023-08-17 12:05:38 +03:00
|
|
|
processes[i] = pid;
|
2023-08-22 14:54:50 +03:00
|
|
|
if (pgrp == 0)
|
|
|
|
pgrp = pid;
|
2023-08-17 12:05:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (next_stdin != STDIN_FILENO)
|
|
|
|
close(next_stdin);
|
|
|
|
if (pipefd[1] != STDOUT_FILENO)
|
|
|
|
close(pipefd[1]);
|
|
|
|
next_stdin = pipefd[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < commands.size(); i++)
|
|
|
|
{
|
|
|
|
if (processes[i] == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int status;
|
|
|
|
if (waitpid(processes[i], &status, 0) == -1)
|
|
|
|
{
|
|
|
|
perror("waitpid");
|
|
|
|
exit_codes[i] = 69420;
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-28 18:10:36 +03:00
|
|
|
|
2023-08-01 14:24:36 +03:00
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
fprintf(stderr, "Terminated by signal %d\n", WTERMSIG(status));
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
if (WEXITSTATUS(status))
|
|
|
|
exit_codes[i] = WEXITSTATUS(status);
|
2023-06-05 01:42:57 +03:00
|
|
|
}
|
2023-05-16 19:22:46 +03:00
|
|
|
|
2023-08-22 11:37:04 +03:00
|
|
|
if (tcsetpgrp(0, getpgrp()) == -1)
|
2023-08-17 12:05:38 +03:00
|
|
|
ERROR_RETURN("tcsetpgrp", 1);
|
|
|
|
|
|
|
|
return exit_codes.back();
|
2023-06-05 01:42:57 +03:00
|
|
|
}
|
|
|
|
|
2023-10-03 10:24:10 +03:00
|
|
|
int parse_and_execute_command(BAN::StringView command)
|
|
|
|
{
|
|
|
|
if (command.empty())
|
|
|
|
return 0;
|
|
|
|
auto parsed_commands = parse_command(command);
|
|
|
|
if (parsed_commands.empty())
|
|
|
|
return 0;
|
|
|
|
tcsetattr(0, TCSANOW, &old_termios);
|
|
|
|
int ret = execute_piped_commands(parsed_commands);
|
|
|
|
tcsetattr(0, TCSANOW, &new_termios);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int source_script(const BAN::String& path)
|
|
|
|
{
|
|
|
|
FILE* fp = fopen(path.data(), "r");
|
|
|
|
if (fp == nullptr)
|
|
|
|
ERROR_RETURN("fopen", 1);
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
BAN::String command;
|
|
|
|
char temp_buffer[128];
|
|
|
|
while (fgets(temp_buffer, sizeof(temp_buffer), fp))
|
|
|
|
{
|
|
|
|
MUST(command.append(temp_buffer));
|
|
|
|
if (command.back() != '\n')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
command.pop_back();
|
|
|
|
|
|
|
|
if (!command.empty())
|
|
|
|
if (int temp = parse_and_execute_command(command))
|
|
|
|
ret = temp;
|
|
|
|
command.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!command.empty())
|
|
|
|
if (int temp = parse_and_execute_command(command))
|
|
|
|
ret = temp;
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-03 10:24:36 +03:00
|
|
|
bool exists(const BAN::String& path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
return stat(path.data(), &st) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int source_shellrc()
|
|
|
|
{
|
|
|
|
if (char* home = getenv("HOME"))
|
|
|
|
{
|
|
|
|
BAN::String path(home);
|
|
|
|
MUST(path.append("/.shellrc"sv));
|
|
|
|
if (exists(path))
|
|
|
|
return source_script(path);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-09 01:24:08 +03:00
|
|
|
int character_length(BAN::StringView prompt)
|
2023-06-05 14:36:17 +03:00
|
|
|
{
|
|
|
|
int length { 0 };
|
|
|
|
bool in_escape { false };
|
|
|
|
for (char c : prompt)
|
|
|
|
{
|
|
|
|
if (in_escape)
|
|
|
|
{
|
|
|
|
if (isalpha(c))
|
|
|
|
in_escape = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c == '\e')
|
|
|
|
in_escape = true;
|
2023-06-09 01:24:08 +03:00
|
|
|
else if (((uint8_t)c & 0xC0) != 0x80)
|
2023-06-05 14:36:17 +03:00
|
|
|
length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2023-06-12 00:45:47 +03:00
|
|
|
BAN::String get_prompt()
|
|
|
|
{
|
|
|
|
const char* raw_prompt = getenv("PS1");
|
|
|
|
if (raw_prompt == nullptr)
|
2023-10-03 10:24:36 +03:00
|
|
|
return "$ "sv;
|
2023-06-12 00:45:47 +03:00
|
|
|
|
|
|
|
BAN::String prompt;
|
|
|
|
for (int i = 0; raw_prompt[i]; i++)
|
|
|
|
{
|
|
|
|
char ch = raw_prompt[i];
|
|
|
|
if (ch == '\\')
|
|
|
|
{
|
|
|
|
switch (raw_prompt[++i])
|
|
|
|
{
|
|
|
|
case 'e':
|
|
|
|
MUST(prompt.push_back('\e'));
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
MUST(prompt.push_back('\n'));
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
MUST(prompt.push_back('\\'));
|
|
|
|
break;
|
2023-06-12 01:31:33 +03:00
|
|
|
case '~':
|
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
if (getcwd(buffer, sizeof(buffer)) == nullptr)
|
|
|
|
strcpy(buffer, strerrorname_np(errno));
|
|
|
|
|
|
|
|
const char* home = getenv("HOME");
|
|
|
|
size_t home_len = home ? strlen(home) : 0;
|
|
|
|
if (home && strncmp(buffer, home, home_len) == 0)
|
|
|
|
{
|
|
|
|
MUST(prompt.push_back('~'));
|
|
|
|
MUST(prompt.append(buffer + home_len));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MUST(prompt.append(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-08-14 14:55:23 +03:00
|
|
|
case 'u':
|
|
|
|
{
|
2023-09-29 19:20:48 +03:00
|
|
|
static char* username = nullptr;
|
|
|
|
if (username == nullptr)
|
|
|
|
{
|
|
|
|
auto* passwd = getpwuid(geteuid());
|
|
|
|
if (passwd == nullptr)
|
|
|
|
break;
|
|
|
|
username = new char[strlen(passwd->pw_name) + 1];
|
|
|
|
strcpy(username, passwd->pw_name);
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
MUST(prompt.append(username));
|
2023-08-14 14:55:23 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-08-22 15:33:01 +03:00
|
|
|
case 'h':
|
|
|
|
{
|
|
|
|
MUST(prompt.append(hostname));
|
|
|
|
break;
|
|
|
|
}
|
2023-06-12 00:45:47 +03:00
|
|
|
case '\0':
|
|
|
|
MUST(prompt.push_back('\\'));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MUST(prompt.push_back('\\'));
|
|
|
|
MUST(prompt.push_back(*raw_prompt));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MUST(prompt.push_back(ch));
|
|
|
|
}
|
|
|
|
}
|
2023-06-12 01:31:33 +03:00
|
|
|
|
2023-06-12 00:45:47 +03:00
|
|
|
return prompt;
|
|
|
|
}
|
|
|
|
|
|
|
|
int prompt_length()
|
|
|
|
{
|
|
|
|
return character_length(get_prompt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_prompt()
|
|
|
|
{
|
|
|
|
auto prompt = get_prompt();
|
|
|
|
fprintf(stdout, "%.*s", prompt.size(), prompt.data());
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2023-06-05 01:42:57 +03:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2023-07-06 23:22:57 +03:00
|
|
|
argv0 = argv[0];
|
|
|
|
|
2023-07-28 18:10:36 +03:00
|
|
|
if (signal(SIGINT, [](int) {}) == SIG_ERR)
|
|
|
|
perror("signal");
|
|
|
|
|
2023-10-24 19:10:53 +03:00
|
|
|
tcgetattr(0, &old_termios);
|
|
|
|
|
2023-08-22 15:33:01 +03:00
|
|
|
{
|
|
|
|
FILE* fp = fopen("/etc/hostname", "r");
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
while (size_t nbyte = fread(buffer, 1, sizeof(buffer), fp))
|
|
|
|
{
|
|
|
|
if (nbyte == 0)
|
|
|
|
break;
|
|
|
|
MUST(hostname.append(BAN::StringView(buffer, nbyte)));
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
if (!hostname.empty() && hostname.back() == '\n')
|
|
|
|
hostname.pop_back();
|
|
|
|
}
|
|
|
|
|
2023-07-06 23:18:37 +03:00
|
|
|
if (argc >= 2)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "-c") == 0)
|
|
|
|
{
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
printf("-c requires an argument\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BAN::String command;
|
|
|
|
MUST(command.append(argv[2]));
|
|
|
|
|
2023-08-17 12:05:38 +03:00
|
|
|
auto commands = parse_command(command);
|
|
|
|
return execute_piped_commands(commands);
|
2023-07-06 23:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("unknown argument '%s'\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-06-18 23:35:51 +03:00
|
|
|
if (argc >= 1)
|
|
|
|
setenv("SHELL", argv[0], true);
|
2023-10-03 10:24:36 +03:00
|
|
|
|
|
|
|
source_shellrc();
|
2023-06-18 23:35:51 +03:00
|
|
|
|
2023-05-16 19:22:46 +03:00
|
|
|
new_termios = old_termios;
|
|
|
|
new_termios.c_lflag &= ~(ECHO | ICANON);
|
|
|
|
tcsetattr(0, TCSANOW, &new_termios);
|
|
|
|
|
2023-06-05 01:42:57 +03:00
|
|
|
BAN::Vector<BAN::String> buffers, history;
|
|
|
|
MUST(buffers.emplace_back(""sv));
|
|
|
|
size_t index = 0;
|
|
|
|
size_t col = 0;
|
|
|
|
|
2023-06-05 18:53:52 +03:00
|
|
|
int waiting_utf8 = 0;
|
2023-06-05 01:42:57 +03:00
|
|
|
|
2023-06-12 00:45:47 +03:00
|
|
|
print_prompt();
|
2023-06-05 01:42:57 +03:00
|
|
|
|
2023-05-16 19:22:46 +03:00
|
|
|
while (true)
|
|
|
|
{
|
2023-12-06 13:04:33 +02:00
|
|
|
int chi = getchar();
|
|
|
|
if (chi == EOF)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
clearerr(stdin);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
perror("getchar");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t ch = chi;
|
2023-06-05 01:42:57 +03:00
|
|
|
|
2023-06-05 18:53:52 +03:00
|
|
|
if (waiting_utf8 > 0)
|
|
|
|
{
|
|
|
|
waiting_utf8--;
|
|
|
|
|
|
|
|
ASSERT((ch & 0xC0) == 0x80);
|
|
|
|
|
|
|
|
fputc(ch, stdout);
|
|
|
|
MUST(buffers[index].insert(ch, col++));
|
|
|
|
if (waiting_utf8 == 0)
|
|
|
|
{
|
|
|
|
fprintf(stdout, "\e[s%s\e[u", buffers[index].data() + col);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ch & 0x80)
|
|
|
|
{
|
|
|
|
if ((ch & 0xE0) == 0xC0)
|
|
|
|
waiting_utf8 = 1;
|
|
|
|
else if ((ch & 0xF0) == 0xE0)
|
|
|
|
waiting_utf8 = 2;
|
|
|
|
else if ((ch & 0xF8) == 0xF0)
|
|
|
|
waiting_utf8 = 3;
|
|
|
|
else
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
|
|
|
fputc(ch, stdout);
|
|
|
|
MUST(buffers[index].insert(ch, col++));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ch)
|
2023-06-05 01:42:57 +03:00
|
|
|
{
|
|
|
|
case '\e':
|
2023-09-07 15:51:05 +03:00
|
|
|
ch = getchar();
|
2023-06-05 18:53:52 +03:00
|
|
|
if (ch != '[')
|
2023-06-05 01:42:57 +03:00
|
|
|
break;
|
2023-09-07 15:51:05 +03:00
|
|
|
ch = getchar();
|
2023-06-05 18:53:52 +03:00
|
|
|
switch (ch)
|
2023-06-05 01:42:57 +03:00
|
|
|
{
|
2023-06-12 00:45:47 +03:00
|
|
|
case 'A': if (index > 0) { index--; col = buffers[index].size(); fprintf(stdout, "\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break;
|
|
|
|
case 'B': if (index < buffers.size() - 1) { index++; col = buffers[index].size(); fprintf(stdout, "\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break;
|
2023-06-05 18:53:52 +03:00
|
|
|
case 'C': if (col < buffers[index].size()) { col++; while ((buffers[index][col - 1] & 0xC0) == 0x80) col++; fprintf(stdout, "\e[C"); fflush(stdout); } break;
|
|
|
|
case 'D': if (col > 0) { while ((buffers[index][col - 1] & 0xC0) == 0x80) col--; col--; fprintf(stdout, "\e[D"); fflush(stdout); } break;
|
2023-06-05 01:42:57 +03:00
|
|
|
}
|
|
|
|
break;
|
2023-06-09 01:24:08 +03:00
|
|
|
case '\x0C': // ^L
|
|
|
|
{
|
2023-06-12 00:45:47 +03:00
|
|
|
int x = prompt_length() + character_length(buffers[index].sv().substring(col)) + 1;
|
|
|
|
fprintf(stdout, "\e[H\e[J");
|
|
|
|
print_prompt();
|
|
|
|
fprintf(stdout, "%s\e[u\e[1;%dH", buffers[index].data(), x);
|
2023-06-09 01:24:08 +03:00
|
|
|
fflush(stdout);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-05 01:42:57 +03:00
|
|
|
case '\b':
|
|
|
|
if (col > 0)
|
|
|
|
{
|
2023-06-05 18:53:52 +03:00
|
|
|
while ((buffers[index][col - 1] & 0xC0) == 0x80)
|
|
|
|
buffers[index].remove(--col);
|
|
|
|
buffers[index].remove(--col);
|
|
|
|
fprintf(stdout, "\b\e[s%s \e[u", buffers[index].data() + col);
|
2023-06-05 18:24:41 +03:00
|
|
|
fflush(stdout);
|
2023-06-05 01:42:57 +03:00
|
|
|
}
|
|
|
|
break;
|
2023-08-14 12:26:22 +03:00
|
|
|
case '\x01': // ^A
|
|
|
|
col = 0;
|
|
|
|
fprintf(stdout, "\e[%dG", prompt_length() + 1);
|
|
|
|
fflush(stdout);
|
|
|
|
break;
|
2023-07-28 18:10:36 +03:00
|
|
|
case '\x03': // ^C
|
|
|
|
fputc('\n', stdout);
|
|
|
|
print_prompt();
|
|
|
|
buffers[index].clear();
|
|
|
|
col = 0;
|
|
|
|
break;
|
2023-09-07 15:51:05 +03:00
|
|
|
case '\x04':
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
clean_exit();
|
|
|
|
break;
|
2023-06-05 01:42:57 +03:00
|
|
|
case '\n':
|
|
|
|
fputc('\n', stdout);
|
|
|
|
if (!buffers[index].empty())
|
|
|
|
{
|
2023-10-03 10:24:10 +03:00
|
|
|
last_return = parse_and_execute_command(buffers[index]);
|
2023-06-05 01:42:57 +03:00
|
|
|
MUST(history.push_back(buffers[index]));
|
|
|
|
buffers = history;
|
|
|
|
MUST(buffers.emplace_back(""sv));
|
|
|
|
}
|
2023-06-12 00:45:47 +03:00
|
|
|
print_prompt();
|
2023-06-05 01:42:57 +03:00
|
|
|
index = buffers.size() - 1;
|
|
|
|
col = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2023-06-05 18:53:52 +03:00
|
|
|
MUST(buffers[index].insert(ch, col++));
|
|
|
|
fprintf(stdout, "%c\e[s%s\e[u", ch, buffers[index].data() + col);
|
2023-06-05 01:42:57 +03:00
|
|
|
fflush(stdout);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-16 19:22:46 +03:00
|
|
|
}
|
|
|
|
|
2023-09-07 15:51:05 +03:00
|
|
|
clean_exit();
|
2023-05-16 19:22:46 +03:00
|
|
|
}
|