Create a minimal interactive shell

This commit is contained in:
2024-02-22 21:29:25 +01:00
parent 86d579e8d5
commit ad29dbdc14
43 changed files with 1192 additions and 3264 deletions

49
shell/history.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "elna/history.hpp"
namespace elna
{
editor_history::editor_history()
: commands{}, current_pointer(commands.cend())
{
}
void editor_history::push(const std::string& entry)
{
commands.push_front(entry);
current_pointer = commands.cbegin();
}
void editor_history::clear()
{
commands.clear();
current_pointer = commands.cend();
}
editor_history::const_iterator editor_history::prev() noexcept
{
if (this->current_pointer != cbegin())
{
this->current_pointer = std::prev(this->current_pointer);
}
return this->current_pointer;
}
editor_history::const_iterator editor_history::next() noexcept
{
if (this->current_pointer != cend())
{
this->current_pointer = std::next(this->current_pointer);
}
return this->current_pointer;
}
editor_history::const_iterator editor_history::cbegin() const noexcept
{
return this->commands.cbegin();
}
editor_history::const_iterator editor_history::cend() const noexcept
{
return this->commands.cend();
}
}

254
shell/interactive.cpp Normal file
View File

@ -0,0 +1,254 @@
#include <boost/process.hpp>
#include <termios.h>
#include <filesystem>
#include "elna/interactive.hpp"
namespace elna
{
static termios original_termios;
interactive_exception::interactive_exception()
: runtime_error("read")
{
}
template<typename T>
static std::pair<T, unsigned char> read_number()
{
T position{ 0 };
unsigned char c{ 0 };
while (read(STDIN_FILENO, &c, 1) == 1 && std::isdigit(c))
{
position = position * 10 + (c - '0');
}
return std::make_pair(position, c);
}
void loop()
{
editor_history history;
do
{
auto line = read_line(history);
if (!line.has_value())
{
write(STDOUT_FILENO, "\r\n", 2);
break;
}
history.push(line.value().command_line());
auto tokens = lex(line.value().command_line());
if (!execute(tokens.assume_value()))
{
break;
}
}
while (true);
}
std::optional<editor_state> read_line(editor_history& history)
{
editor_state state;
std::string buffer;
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
while (true)
{
buffer.clear();
auto pressed_key = read_key();
if (pressed_key.empty())
{
continue;
}
switch (state.process_keypress(pressed_key))
{
case action::finalize:
if (pressed_key == key('d', modifier::ctrl))
{
return std::optional<editor_state>();
}
else if (pressed_key == '\r')
{
write(STDOUT_FILENO, "\r\n", 2);
return std::make_optional(state);
}
break;
case action::complete:
write(STDOUT_FILENO, "\x1b[1E\x1b[2K", 8);
state.insert(complete());
write(STDOUT_FILENO, "\x1b[1A", 4);
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
break;
case action::redraw:
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
break;
case action::write:
write(STDOUT_FILENO, &pressed_key, 1);
break;
case action::move:
buffer = cursor_to_column(state.absolute_position());
write(STDOUT_FILENO, buffer.data(), buffer.size());
break;
case action::ignore:
break;
case action::history:
editor_history::const_iterator history_iterator = history.cend();
if (pressed_key == key(special_key::arrow_up, modifier::ctrl))
{
history_iterator = history.next();
}
else if (pressed_key == key(special_key::arrow_down, modifier::ctrl))
{
history_iterator = history.prev();
}
if (history_iterator != history.cend())
{
state.load(*history_iterator);
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
}
break;
}
}
}
key read_key()
{
char c{ 0 };
if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN)
{
throw interactive_exception();
}
if (c != '\x1b')
{
return key(c);
}
if (read(STDIN_FILENO, &c, 1) != 1 || c != '[')
{
return key();
}
auto [number, last_char] = read_number<unsigned char>();
std::uint8_t modifiers{ 0 };
if (last_char == ';')
{
auto modifier_response = read_number<std::uint8_t>();
modifiers = modifier_response.first;
last_char = modifier_response.second;
}
if (number == 0 || number == 1)
{
switch (last_char)
{
case 'A':
return key(special_key::arrow_up, modifiers);
case 'B':
return key(special_key::arrow_down, modifiers);
case 'C':
return key(special_key::arrow_right, modifiers);
case 'D':
return key(special_key::arrow_left, modifiers);
case 'H':
return key(special_key::home_key, modifiers);
case 'F':
return key(special_key::end_key, modifiers);
}
}
else if (last_char == '~')
{
switch (number)
{
case 5:
return key(special_key::page_up, modifiers);
case 6:
return key(special_key::page_up, modifiers);
case 7:
return key(special_key::home_key, modifiers);
case 8:
return key(special_key::end_key, modifiers);
}
}
else if (last_char == 'u')
{
return key(number, modifiers);
}
return key();
}
bool execute(const std::vector<token>& tokens)
{
if (tokens.empty())
{
return true;
}
std::string program = tokens.front().identifier();
if (program == "exit")
{
return false;
}
else if (program == "cd")
{
std::filesystem::current_path(tokens[1].identifier());
return true;
}
std::vector<std::string> arguments;
for (auto argument = tokens.cbegin() + 1; argument != std::cend(tokens); ++argument)
{
arguments.push_back(argument->identifier());
}
launch(program, arguments);
return true;
}
void launch(const std::string& program, const std::vector<std::string>& arguments)
{
boost::process::system(boost::process::search_path(program), arguments);
}
bool enable_raw_mode()
{
if (tcgetattr(STDIN_FILENO, &original_termios) == -1)
{
return false;
}
termios raw = original_termios;
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= CS8;
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
write(STDOUT_FILENO, start_kitty_keybaord, strlen(start_kitty_keybaord));
return tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) != -1;
}
void disable_raw_mode()
{
write(STDOUT_FILENO, end_kitty_keybaord, strlen(end_kitty_keybaord));
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
}
std::string complete()
{
boost::process::ipstream output;
boost::process::system("/usr/bin/fzf", "--height=10", "--layout=reverse",
boost::process::std_out > output);
std::string selections;
std::getline(output, selections, '\n');
return selections;
}
}

124
shell/lexer.cpp Normal file
View File

@ -0,0 +1,124 @@
#include "elna/lexer.hpp"
namespace elna
{
source::source(const std::string& buffer)
: m_buffer(buffer)
{
}
source::const_iterator source::begin() const
{
return source::const_iterator(std::cbegin(m_buffer), Position());
}
source::const_iterator source::end() const
{
Position end_position{ 0, 0 };
return source::const_iterator(std::cend(m_buffer), end_position);
}
source::const_iterator::const_iterator(std::string::const_iterator buffer, const Position& start_position)
: m_buffer(buffer), m_position(start_position)
{
}
const Position& source::const_iterator::position() const noexcept
{
return this->m_position;
}
source::const_iterator::reference source::const_iterator::operator*() const noexcept
{
return *m_buffer;
}
source::const_iterator::pointer source::const_iterator::operator->() const noexcept
{
return m_buffer.base();
}
source::const_iterator& source::const_iterator::operator++()
{
if (*this->m_buffer == '\n')
{
this->m_position.column = 1;
++this->m_position.line;
}
else
{
++this->m_position.column;
}
std::advance(this->m_buffer, 1);
return *this;
}
source::const_iterator& source::const_iterator::operator++(int)
{
auto tmp = *this;
++(*this);
return *this;
}
bool source::const_iterator::operator==(const source::const_iterator& that) const noexcept
{
return this->m_buffer == that.m_buffer;
}
bool source::const_iterator::operator!=(const source::const_iterator& that) const noexcept
{
return !(*this == that);
}
token::token(const type of, source::const_iterator begin, source::const_iterator end)
: m_type(of), m_value(begin, end)
{
}
token::type token::of() const noexcept
{
return m_type;
}
const token::value& token::identifier() const noexcept
{
return m_value;
}
const Position& token::position() const noexcept
{
return m_position;
}
result<std::vector<token>> lex(const std::string& buffer)
{
std::vector<token> tokens;
source input{ buffer };
for (auto iterator = input.begin(); iterator != input.end();)
{
if (*iterator == ' ' || *iterator == '\n')
{
}
else if (std::isgraph(*iterator))
{
auto current_position = iterator;
do
{
++current_position;
}
while (current_position != input.end() && std::isgraph(*current_position));
token new_token{ token::type::word, iterator, current_position };
tokens.push_back(new_token);
iterator = current_position;
continue;
}
++iterator;
}
return tokens;
}
}

16
shell/main.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <cstdlib>
#include <unistd.h>
#include "elna/interactive.hpp"
int main()
{
if (!elna::enable_raw_mode())
{
std::perror("tcsetattr");
return EXIT_FAILURE;
}
std::atexit(elna::disable_raw_mode);
elna::loop();
return EXIT_SUCCESS;
}

25
shell/result.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "elna/result.hpp"
namespace elna
{
CompileError::CompileError(char const *message, Position position) noexcept
{
this->message_ = message;
this->position_ = position;
}
char const *CompileError::message() const noexcept
{
return this->message_;
}
std::size_t CompileError::line() const noexcept
{
return this->position_.line;
}
std::size_t CompileError::column() const noexcept
{
return this->position_.column;
}
}

214
shell/state.cpp Normal file
View File

@ -0,0 +1,214 @@
#include "elna/state.hpp"
namespace elna
{
std::string cursor_to_column(const std::uint16_t position)
{
std::string code = std::to_string(position);
code.insert(0, "\x1b[");
code.push_back('G');
return code;
}
key::key()
: store(static_cast<unsigned char>(0x1b))
{
}
key::key(const unsigned char c, const std::uint8_t modifiers)
: store(c), modifiers(modifiers == 0 ? 0 : modifiers - 1)
{
}
key::key(const unsigned char c, const modifier modifiers)
: store(c), modifiers(static_cast<uint8_t>(modifiers))
{
}
key::key(const special_key control, const std::uint8_t modifiers)
: store(control), modifiers(modifiers == 0 ? 0 : modifiers - 1)
{
}
key::key(const special_key control, const modifier modifiers)
: store(control), modifiers(static_cast<uint8_t>(modifiers))
{
}
unsigned char key::character() const
{
return std::get<unsigned char>(this->store);
}
special_key key::control() const
{
return std::get<special_key>(this->store);
}
bool key::modified(const std::uint8_t modifiers) const noexcept
{
return this->modifiers == modifiers;
}
bool key::modified(const modifier modifiers) const noexcept
{
return this->modifiers == static_cast<std::uint8_t>(modifiers);
}
bool key::operator==(const unsigned char c) const
{
return std::holds_alternative<unsigned char>(this->store)
&& std::get<unsigned char>(this->store) == c;
}
bool key::operator==(const special_key control) const
{
return std::holds_alternative<special_key>(this->store)
&& std::get<special_key>(this->store) == control;
}
bool key::operator==(const key& that) const
{
return this->store == that.store;
}
bool key::operator!=(const unsigned char c) const
{
return !(*this == c);
}
bool key::operator!=(const special_key control) const
{
return !(*this == control);
}
bool key::operator!=(const key& that) const
{
return !(*this == that);
}
bool key::is_character() const noexcept
{
return std::holds_alternative<unsigned char>(this->store);
}
bool key::is_control() const noexcept
{
return std::holds_alternative<special_key>(this->store);
}
bool key::empty() const noexcept
{
return *this == '\x1b';
}
bool operator==(const special_key control, const key& that)
{
return that == control;
}
bool operator==(const char c, const key& that)
{
return that == c;
}
bool operator!=(const special_key control, const key& that)
{
return that != control;
}
bool operator!=(const char c, const key& that)
{
return that != c;
}
editor_state::editor_state()
{
this->input.reserve(1024);
}
void editor_state::load(const std::string& text)
{
this->input = text;
}
const std::string& editor_state::command_line() const noexcept
{
return this->input;
}
std::size_t editor_state::absolute_position() const noexcept
{
return this->prompt.size() + this->position;
}
void editor_state::insert(const std::string& text)
{
this->input.insert(std::end(this->input), std::cbegin(text), std::cend(text));
}
action editor_state::process_keypress(const key& press)
{
if (press.is_control())
{
switch (press.control())
{
case special_key::arrow_left:
if (this->position > 1)
{
--this->position;
}
return action::move;
case special_key::arrow_right:
if (this->position + 1 < this->input.size() + this->prompt.size())
{
++this->position;
}
return action::move;
case special_key::arrow_up:
case special_key::arrow_down:
return action::history;
case special_key::home_key:
this->position = 1;
return action::move;
case special_key::end_key:
this->position = this->input.size() + 1;
return action::move;
default:
return action::ignore;
}
}
else if (press.modified(modifier::ctrl))
{
switch (press.character())
{
case 'd':
return action::finalize;
case 't':
return action::complete;
default:
return action::ignore;
}
}
else
{
switch (press.character())
{
case 127: // Backspace.
if (this->position <= this->input.size() + 1 && this->position > 1)
{
--this->position;
this->input.erase(this->position - 1, 1);
}
return action::redraw;
case '\r':
return action::finalize;
default:
++this->position;
this->input.push_back(press.character());
return action::write;
}
}
}
}