1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:47:35 +00:00

Shell+LibLine: Handle escaped characters correctly

This patchset fixes incorrect handling of escaped tokens (`a\ b`) in
Shell autocompletion and LibLine.
The users of LibLine can now choose between two token splitting modes,
either taking into account escapes, or ignoring them.
This commit is contained in:
AnotherTest 2020-04-30 08:19:47 +04:30 committed by Andreas Kling
parent f2cdef5c47
commit a80ddf584f
3 changed files with 125 additions and 24 deletions

View file

@ -35,9 +35,10 @@
namespace Line {
Editor::Editor(bool always_refresh)
Editor::Editor(Configuration configuration)
: m_configuration(configuration)
{
m_always_refresh = always_refresh;
m_always_refresh = configuration.refresh_behaviour == Configuration::RefreshBehaviour::Eager;
m_pending_chars = ByteBuffer::create_uninitialized(0);
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
@ -352,21 +353,34 @@ String Editor::get_line(const String& prompt)
if (!on_tab_complete_first_token || !on_tab_complete_other_token)
continue;
bool is_empty_token = m_cursor == 0 || m_buffer[m_cursor - 1] == ' ';
auto should_break_token = [mode = m_configuration.split_mechanism](auto& buffer, size_t index) {
switch (mode) {
case Configuration::TokenSplitMechanism::Spaces:
return buffer[index] == ' ';
case Configuration::TokenSplitMechanism::UnescapedSpaces:
return buffer[index] == ' ' && (index == 0 || buffer[index - 1] != '\\');
}
ASSERT_NOT_REACHED();
return true;
};
bool is_empty_token = m_cursor == 0 || should_break_token(m_buffer, m_cursor - 1);
// reverse tab can count as regular tab here
m_times_tab_pressed++;
int token_start = m_cursor - 1;
if (!is_empty_token) {
while (token_start >= 0 && m_buffer[token_start] != ' ')
while (token_start >= 0 && !should_break_token(m_buffer, token_start))
--token_start;
++token_start;
}
bool is_first_token = true;
for (int i = token_start - 1; i >= 0; --i) {
if (m_buffer[i] != ' ') {
if (should_break_token(m_buffer, i)) {
is_first_token = false;
break;
}
@ -651,7 +665,7 @@ String Editor::get_line(const String& prompt)
for (auto ch : m_buffer)
m_pre_search_buffer.append(ch);
m_pre_search_cursor = m_cursor;
m_search_editor = make<Editor>(true); // Has anyone seen 'Inception'?
m_search_editor = make<Editor>(Configuration { Configuration::Eager, m_configuration.split_mechanism }); // Has anyone seen 'Inception'?
m_search_editor->on_display_refresh = [this](Editor& search_editor) {
search(StringView { search_editor.buffer().data(), search_editor.buffer().size() });
refresh_display();

View file

@ -75,9 +75,37 @@ struct CompletionSuggestion {
String trailing_trivia;
};
struct Configuration {
enum TokenSplitMechanism {
Spaces,
UnescapedSpaces,
};
enum RefreshBehaviour {
Lazy,
Eager,
};
Configuration()
{
}
template<typename Arg, typename... Rest>
Configuration(Arg arg, Rest... rest)
: Configuration(rest...)
{
set(arg);
}
void set(RefreshBehaviour refresh) { refresh_behaviour = refresh; }
void set(TokenSplitMechanism split) { split_mechanism = split; }
RefreshBehaviour refresh_behaviour { RefreshBehaviour::Lazy };
TokenSplitMechanism split_mechanism { TokenSplitMechanism::Spaces };
};
class Editor {
public:
explicit Editor(bool always_refresh = false);
explicit Editor(Configuration configuration = {});
~Editor();
String get_line(const String& prompt);
@ -308,6 +336,8 @@ private:
bool m_refresh_needed { false };
bool m_is_editing { false };
Configuration m_configuration;
};
}