2020-06-04 22:34:49 +00:00
|
|
|
#include <common/ReplxxLineReader.h>
|
2020-06-07 17:29:34 +00:00
|
|
|
#include <common/errnoToString.h>
|
2020-06-04 22:34:49 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <functional>
|
2020-06-04 21:31:51 +00:00
|
|
|
#include <sys/file.h>
|
2020-06-04 22:34:49 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Trim ending whitespace inplace
|
|
|
|
void trim(String & s)
|
|
|
|
{
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
|
|
|
|
}
|
|
|
|
|
2020-07-29 18:00:46 +00:00
|
|
|
// Uses separate replxx::Replxx instance to avoid loading them again in the
|
|
|
|
// current context (replxx::Replxx::history_load() will re-load the history
|
|
|
|
// from the file), since then they will overlaps with history from the current
|
|
|
|
// session (this will make behavior compatible with other interpreters, i.e.
|
|
|
|
// bash).
|
|
|
|
void history_save(const String & history_file_path, const String & line)
|
|
|
|
{
|
|
|
|
replxx::Replxx rx_no_overlap;
|
|
|
|
rx_no_overlap.history_load(history_file_path);
|
|
|
|
rx_no_overlap.history_add(line);
|
|
|
|
rx_no_overlap.history_save(history_file_path);
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReplxxLineReader::ReplxxLineReader(
|
2020-06-04 22:45:04 +00:00
|
|
|
const Suggest & suggest,
|
|
|
|
const String & history_file_path_,
|
|
|
|
bool multiline_,
|
|
|
|
Patterns extenders_,
|
|
|
|
Patterns delimiters_,
|
|
|
|
replxx::Replxx::highlighter_callback_t highlighter_)
|
|
|
|
: LineReader(history_file_path_, multiline_, std::move(extenders_), std::move(delimiters_)), highlighter(std::move(highlighter_))
|
2020-06-04 22:34:49 +00:00
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
using Replxx = replxx::Replxx;
|
|
|
|
|
|
|
|
if (!history_file_path.empty())
|
2020-06-04 21:31:51 +00:00
|
|
|
{
|
|
|
|
history_file_fd = open(history_file_path.c_str(), O_RDWR);
|
2020-06-04 22:10:22 +00:00
|
|
|
if (history_file_fd < 0)
|
|
|
|
{
|
2020-06-07 17:29:34 +00:00
|
|
|
rx.print("Open of history file failed: %s\n", errnoToString(errno).c_str());
|
2020-06-04 22:10:22 +00:00
|
|
|
}
|
2020-06-06 09:49:15 +00:00
|
|
|
else
|
2020-06-04 22:10:22 +00:00
|
|
|
{
|
2020-06-06 09:49:15 +00:00
|
|
|
if (flock(history_file_fd, LOCK_SH))
|
|
|
|
{
|
2020-06-07 17:29:34 +00:00
|
|
|
rx.print("Shared lock of history file failed: %s\n", errnoToString(errno).c_str());
|
2020-06-06 09:49:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rx.history_load(history_file_path);
|
|
|
|
|
|
|
|
if (flock(history_file_fd, LOCK_UN))
|
|
|
|
{
|
2020-06-07 17:29:34 +00:00
|
|
|
rx.print("Unlock of history file failed: %s\n", errnoToString(errno).c_str());
|
2020-06-06 09:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 22:10:22 +00:00
|
|
|
}
|
2020-06-04 21:31:51 +00:00
|
|
|
}
|
2020-06-04 22:34:49 +00:00
|
|
|
|
|
|
|
auto callback = [&suggest] (const String & context, size_t context_size)
|
|
|
|
{
|
|
|
|
auto range = suggest.getCompletions(context, context_size);
|
|
|
|
return Replxx::completions_t(range.first, range.second);
|
|
|
|
};
|
|
|
|
|
|
|
|
rx.set_completion_callback(callback);
|
|
|
|
rx.set_complete_on_empty(false);
|
|
|
|
rx.set_word_break_characters(word_break_characters);
|
|
|
|
|
2020-06-04 22:45:04 +00:00
|
|
|
if (highlighter)
|
|
|
|
rx.set_highlighter_callback(highlighter);
|
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
/// By default C-p/C-n binded to COMPLETE_NEXT/COMPLETE_PREV,
|
|
|
|
/// bind C-p/C-n to history-previous/history-next like readline.
|
|
|
|
rx.bind_key(Replxx::KEY::control('N'), [this](char32_t code) { return rx.invoke(Replxx::ACTION::HISTORY_NEXT, code); });
|
|
|
|
rx.bind_key(Replxx::KEY::control('P'), [this](char32_t code) { return rx.invoke(Replxx::ACTION::HISTORY_PREVIOUS, code); });
|
|
|
|
/// By default COMPLETE_NEXT/COMPLETE_PREV was binded to C-p/C-n, re-bind
|
|
|
|
/// to M-P/M-N (that was used for HISTORY_COMMON_PREFIX_SEARCH before, but
|
|
|
|
/// it also binded to M-p/M-n).
|
|
|
|
rx.bind_key(Replxx::KEY::meta('N'), [this](char32_t code) { return rx.invoke(Replxx::ACTION::COMPLETE_NEXT, code); });
|
|
|
|
rx.bind_key(Replxx::KEY::meta('P'), [this](char32_t code) { return rx.invoke(Replxx::ACTION::COMPLETE_PREVIOUS, code); });
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplxxLineReader::~ReplxxLineReader()
|
|
|
|
{
|
2020-06-04 22:36:40 +00:00
|
|
|
if (close(history_file_fd))
|
|
|
|
rx.print("Close of history file failed: %s\n", strerror(errno));
|
2020-06-04 22:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LineReader::InputStatus ReplxxLineReader::readOneLine(const String & prompt)
|
|
|
|
{
|
|
|
|
input.clear();
|
|
|
|
|
|
|
|
const char* cinput = rx.input(prompt);
|
|
|
|
if (cinput == nullptr)
|
|
|
|
return (errno != EAGAIN) ? ABORT : RESET_LINE;
|
|
|
|
input = cinput;
|
|
|
|
|
|
|
|
trim(input);
|
|
|
|
return INPUT_LINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplxxLineReader::addToHistory(const String & line)
|
|
|
|
{
|
2020-06-04 21:31:51 +00:00
|
|
|
// locking history file to prevent from inconsistent concurrent changes
|
2020-07-29 18:00:46 +00:00
|
|
|
//
|
|
|
|
// replxx::Replxx::history_save() already has lockf(),
|
|
|
|
// but replxx::Replxx::history_load() does not
|
|
|
|
// and that is why flock() is added here.
|
2020-06-06 09:51:42 +00:00
|
|
|
bool locked = false;
|
2020-06-04 22:10:22 +00:00
|
|
|
if (flock(history_file_fd, LOCK_EX))
|
|
|
|
rx.print("Lock of history file failed: %s\n", strerror(errno));
|
2020-06-06 09:51:42 +00:00
|
|
|
else
|
|
|
|
locked = true;
|
2020-06-04 21:31:51 +00:00
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
rx.history_add(line);
|
2020-06-04 21:31:51 +00:00
|
|
|
|
|
|
|
// flush changes to the disk
|
2020-07-29 18:00:46 +00:00
|
|
|
history_save(history_file_path, line);
|
2020-06-04 21:31:51 +00:00
|
|
|
|
2020-06-06 09:51:42 +00:00
|
|
|
if (locked && 0 != flock(history_file_fd, LOCK_UN))
|
2020-06-04 22:10:22 +00:00
|
|
|
rx.print("Unlock of history file failed: %s\n", strerror(errno));
|
2020-06-04 22:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReplxxLineReader::enableBracketedPaste()
|
|
|
|
{
|
|
|
|
rx.enable_bracketed_paste();
|
|
|
|
};
|