2022-12-10 10:16:31 +00:00
|
|
|
#include <Client/ReplxxLineReader.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/errnoToString.h>
|
2020-06-04 22:34:49 +00:00
|
|
|
|
2022-12-26 15:04:26 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/copyData.h>
|
|
|
|
|
2023-03-29 09:20:27 +00:00
|
|
|
#include <algorithm>
|
2022-09-22 21:19:57 +00:00
|
|
|
#include <stdexcept>
|
2021-06-30 18:01:50 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2020-06-04 22:34:49 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <functional>
|
2020-06-04 21:31:51 +00:00
|
|
|
#include <sys/file.h>
|
2020-12-01 10:34:34 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <csignal>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fstream>
|
2022-09-23 12:23:14 +00:00
|
|
|
#include <filesystem>
|
2021-02-02 19:07:23 +00:00
|
|
|
#include <fmt/format.h>
|
2022-12-03 11:55:46 +00:00
|
|
|
#include "config.h" // USE_SKIM
|
|
|
|
|
|
|
|
#if USE_SKIM
|
|
|
|
#include <skim.h>
|
|
|
|
#endif
|
2021-02-02 19:07:23 +00:00
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Trim ending whitespace inplace
|
2022-10-05 07:57:24 +00:00
|
|
|
void rightTrim(String & s)
|
2020-06-04 22:34:49 +00:00
|
|
|
{
|
2021-11-07 08:02:40 +00:00
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:44:52 +00:00
|
|
|
std::string getEditor()
|
2021-11-16 18:07:33 +00:00
|
|
|
{
|
2022-08-21 18:24:17 +00:00
|
|
|
const char * editor = std::getenv("EDITOR"); // NOLINT(concurrency-mt-unsafe)
|
2021-11-16 18:07:33 +00:00
|
|
|
|
|
|
|
if (!editor || !*editor)
|
|
|
|
editor = "vim";
|
|
|
|
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:19:57 +00:00
|
|
|
/// See comments in ShellCommand::executeImpl()
|
|
|
|
/// (for the vfork via dlsym())
|
|
|
|
int executeCommand(char * const argv[])
|
|
|
|
{
|
2022-09-23 15:39:03 +00:00
|
|
|
#if !defined(USE_MUSL)
|
|
|
|
/** Here it is written that with a normal call `vfork`, there is a chance of deadlock in multithreaded programs,
|
|
|
|
* because of the resolving of symbols in the shared library
|
|
|
|
* http://www.oracle.com/technetwork/server-storage/solaris10/subprocess-136439.html
|
|
|
|
* Therefore, separate the resolving of the symbol from the call.
|
|
|
|
*/
|
2022-09-22 21:19:57 +00:00
|
|
|
static void * real_vfork = dlsym(RTLD_DEFAULT, "vfork");
|
2022-09-23 15:39:03 +00:00
|
|
|
#else
|
|
|
|
/// If we use Musl with static linking, there is no dlsym and no issue with vfork.
|
|
|
|
static void * real_vfork = reinterpret_cast<void *>(&vfork);
|
|
|
|
#endif
|
2022-09-22 21:19:57 +00:00
|
|
|
if (!real_vfork)
|
|
|
|
throw std::runtime_error("Cannot find vfork symbol");
|
|
|
|
|
|
|
|
pid_t pid = reinterpret_cast<pid_t (*)()>(real_vfork)();
|
|
|
|
|
|
|
|
if (-1 == pid)
|
|
|
|
throw std::runtime_error(fmt::format("Cannot vfork {}: {}", argv[0], errnoToString()));
|
|
|
|
|
|
|
|
/// Child
|
|
|
|
if (0 == pid)
|
|
|
|
{
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigprocmask(0, nullptr, &mask); // NOLINT(concurrency-mt-unsafe) // ok in newly created process
|
|
|
|
sigprocmask(SIG_UNBLOCK, &mask, nullptr); // NOLINT(concurrency-mt-unsafe) // ok in newly created process
|
|
|
|
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int status = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int exited_pid = waitpid(pid, &status, 0);
|
|
|
|
if (exited_pid != -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
throw std::runtime_error(fmt::format("Cannot waitpid {}: {}", pid, errnoToString()));
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeRetry(int fd, const std::string & data)
|
|
|
|
{
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
const char * begin = data.c_str();
|
|
|
|
size_t offset = data.size();
|
|
|
|
|
|
|
|
while (bytes_written != offset)
|
|
|
|
{
|
|
|
|
ssize_t res = ::write(fd, begin + bytes_written, offset - bytes_written);
|
|
|
|
if ((-1 == res || 0 == res) && errno != EINTR)
|
|
|
|
throw std::runtime_error(fmt::format("Cannot write to {}: {}", fd, errnoToString()));
|
|
|
|
bytes_written += res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string readFile(const std::string & path)
|
|
|
|
{
|
2022-12-26 15:04:26 +00:00
|
|
|
std::string out;
|
|
|
|
DB::WriteBufferFromString out_buffer(out);
|
|
|
|
DB::ReadBufferFromFile in_buffer(path);
|
|
|
|
DB::copyData(in_buffer, out_buffer);
|
|
|
|
return out;
|
2022-09-22 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Simple wrapper for temporary files.
|
|
|
|
class TemporaryFile
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string path;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit TemporaryFile(const char * pattern)
|
|
|
|
: path(pattern)
|
|
|
|
{
|
|
|
|
size_t dot_pos = path.rfind('.');
|
|
|
|
if (dot_pos != std::string::npos)
|
2022-10-07 10:46:45 +00:00
|
|
|
fd = ::mkstemps(path.data(), static_cast<int>(path.size() - dot_pos));
|
2022-09-22 21:19:57 +00:00
|
|
|
else
|
|
|
|
fd = ::mkstemp(path.data());
|
|
|
|
|
|
|
|
if (-1 == fd)
|
|
|
|
throw std::runtime_error(fmt::format("Cannot create temporary file {}: {}", path, errnoToString()));
|
|
|
|
}
|
|
|
|
~TemporaryFile()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
unlink();
|
|
|
|
}
|
|
|
|
catch (const std::runtime_error & e)
|
|
|
|
{
|
|
|
|
fmt::print(stderr, "{}", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void close()
|
|
|
|
{
|
|
|
|
if (fd == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (0 != ::close(fd))
|
|
|
|
throw std::runtime_error(fmt::format("Cannot close temporary file {}: {}", path, errnoToString()));
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(const std::string & data)
|
|
|
|
{
|
|
|
|
if (fd == -1)
|
|
|
|
throw std::runtime_error(fmt::format("Cannot write to uninitialized file {}", path));
|
|
|
|
|
|
|
|
writeRetry(fd, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlink()
|
|
|
|
{
|
|
|
|
if (0 != ::unlink(path.c_str()))
|
|
|
|
throw std::runtime_error(fmt::format("Cannot remove temporary file {}: {}", path, errnoToString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string & getPath() { return path; }
|
|
|
|
};
|
|
|
|
|
2021-07-02 01:25:07 +00:00
|
|
|
/// Copied from replxx::src/util.cxx::now_ms_str() under the terms of 3-clause BSD license of Replxx.
|
|
|
|
/// Copyright (c) 2017-2018, Marcin Konarski (amok at codestation.org)
|
|
|
|
/// Copyright (c) 2010, Salvatore Sanfilippo (antirez at gmail dot com)
|
|
|
|
/// Copyright (c) 2010, Pieter Noordhuis (pcnoordhuis at gmail dot com)
|
2021-07-02 05:51:53 +00:00
|
|
|
std::string replxx_now_ms_str()
|
2021-06-30 18:01:50 +00:00
|
|
|
{
|
|
|
|
std::chrono::milliseconds ms(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()));
|
|
|
|
time_t t = ms.count() / 1000;
|
|
|
|
tm broken;
|
|
|
|
if (!localtime_r(&t, &broken))
|
2022-05-31 02:19:08 +00:00
|
|
|
return {};
|
2021-06-30 18:01:50 +00:00
|
|
|
|
|
|
|
static int const BUFF_SIZE(32);
|
|
|
|
char str[BUFF_SIZE];
|
2022-05-31 02:19:08 +00:00
|
|
|
if (strftime(str, BUFF_SIZE, "%Y-%m-%d %H:%M:%S.", &broken) <= 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (snprintf(str + sizeof("YYYY-mm-dd HH:MM:SS"), 5, "%03d", static_cast<int>(ms.count() % 1000)) <= 0)
|
|
|
|
return {};
|
|
|
|
|
2021-06-30 18:01:50 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert from readline to replxx format.
|
|
|
|
///
|
|
|
|
/// replxx requires each history line to prepended with time line:
|
|
|
|
///
|
|
|
|
/// ### YYYY-MM-DD HH:MM:SS.SSS
|
|
|
|
/// select 1
|
|
|
|
///
|
|
|
|
/// And w/o those service lines it will load all lines from history file as
|
|
|
|
/// one history line for suggestion. And if there are lots of lines in file it
|
|
|
|
/// will take lots of time (getline() + tons of reallocations).
|
|
|
|
///
|
|
|
|
/// NOTE: this code uses std::ifstream/std::ofstream like original replxx code.
|
|
|
|
void convertHistoryFile(const std::string & path, replxx::Replxx & rx)
|
|
|
|
{
|
|
|
|
std::ifstream in(path);
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
rx.print("Cannot open %s reading (for conversion): %s\n",
|
2022-08-20 15:09:20 +00:00
|
|
|
path.c_str(), errnoToString().c_str());
|
2021-06-30 18:01:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string line;
|
2021-07-20 18:00:53 +00:00
|
|
|
if (getline(in, line).bad())
|
2021-06-30 18:01:50 +00:00
|
|
|
{
|
|
|
|
rx.print("Cannot read from %s (for conversion): %s\n",
|
2022-08-20 15:09:20 +00:00
|
|
|
path.c_str(), errnoToString().c_str());
|
2021-06-30 18:01:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is the marker of the date, no need to convert.
|
|
|
|
static char const REPLXX_TIMESTAMP_PATTERN[] = "### dddd-dd-dd dd:dd:dd.ddd";
|
2021-07-20 18:00:53 +00:00
|
|
|
if (line.empty() || (line.starts_with("### ") && line.size() == strlen(REPLXX_TIMESTAMP_PATTERN)))
|
2021-06-30 18:01:50 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
in.seekg(0);
|
|
|
|
while (getline(in, line).good())
|
|
|
|
{
|
|
|
|
lines.push_back(line);
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
size_t lines_size = lines.size();
|
|
|
|
std::sort(lines.begin(), lines.end());
|
|
|
|
lines.erase(std::unique(lines.begin(), lines.end()), lines.end());
|
|
|
|
rx.print("The history file (%s) is in old format. %zu lines, %zu unique lines.\n",
|
|
|
|
path.c_str(), lines_size, lines.size());
|
|
|
|
|
|
|
|
std::ofstream out(path);
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
rx.print("Cannot open %s for writing (for conversion): %s\n",
|
2022-08-20 15:09:20 +00:00
|
|
|
path.c_str(), errnoToString().c_str());
|
2021-06-30 18:01:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & timestamp = replxx_now_ms_str();
|
|
|
|
for (const auto & out_line : lines)
|
|
|
|
{
|
|
|
|
out << "### " << timestamp << "\n" << out_line << std::endl;
|
|
|
|
}
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 10:16:31 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-01-29 11:44:27 +00:00
|
|
|
static bool replxx_last_is_delimiter = false;
|
|
|
|
void ReplxxLineReader::setLastIsDelimiter(bool flag)
|
|
|
|
{
|
|
|
|
replxx_last_is_delimiter = flag;
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
ReplxxLineReader::ReplxxLineReader(
|
2022-01-18 18:03:51 +00:00
|
|
|
Suggest & suggest,
|
2020-06-04 22:45:04 +00:00
|
|
|
const String & history_file_path_,
|
|
|
|
bool multiline_,
|
|
|
|
Patterns extenders_,
|
|
|
|
Patterns delimiters_,
|
2023-08-04 07:20:01 +00:00
|
|
|
const char word_break_characters_[],
|
2020-06-04 22:45:04 +00:00
|
|
|
replxx::Replxx::highlighter_callback_t highlighter_)
|
|
|
|
: LineReader(history_file_path_, multiline_, std::move(extenders_), std::move(delimiters_)), highlighter(std::move(highlighter_))
|
2023-08-04 07:20:01 +00:00
|
|
|
, word_break_characters(word_break_characters_)
|
2021-11-16 21:44:52 +00:00
|
|
|
, editor(getEditor())
|
2020-06-04 22:34:49 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Open of history file failed: %s\n", errnoToString().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
|
|
|
{
|
2021-06-30 18:01:50 +00:00
|
|
|
convertHistoryFile(history_file_path, rx);
|
|
|
|
|
2020-06-06 09:49:15 +00:00
|
|
|
if (flock(history_file_fd, LOCK_SH))
|
|
|
|
{
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Shared lock of history file failed: %s\n", errnoToString().c_str());
|
2020-06-06 09:49:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-12 20:39:00 +00:00
|
|
|
if (!rx.history_load(history_file_path))
|
|
|
|
{
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Loading history failed: %s\n", errnoToString().c_str());
|
2020-10-12 20:39:00 +00:00
|
|
|
}
|
2020-06-06 09:49:15 +00:00
|
|
|
|
|
|
|
if (flock(history_file_fd, LOCK_UN))
|
|
|
|
{
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Unlock of history file failed: %s\n", errnoToString().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
|
|
|
|
2020-12-11 16:34:40 +00:00
|
|
|
rx.install_window_change_handler();
|
|
|
|
|
2023-08-04 07:20:01 +00:00
|
|
|
auto callback = [&suggest, this] (const String & context, size_t context_size)
|
2020-06-04 22:34:49 +00:00
|
|
|
{
|
2023-08-04 07:20:01 +00:00
|
|
|
return suggest.getCompletions(context, context_size, word_break_characters);
|
2020-06-04 22:34:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rx.set_completion_callback(callback);
|
|
|
|
rx.set_complete_on_empty(false);
|
|
|
|
rx.set_word_break_characters(word_break_characters);
|
2022-01-28 21:05:14 +00:00
|
|
|
rx.set_ignore_case(true);
|
2023-04-17 19:14:00 +00:00
|
|
|
rx.set_indent_multiline(false);
|
2020-06-04 22:34:49 +00:00
|
|
|
|
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); });
|
2021-10-14 15:56:28 +00:00
|
|
|
|
2021-11-07 08:02:40 +00:00
|
|
|
auto commit_action = [this](char32_t code)
|
|
|
|
{
|
|
|
|
/// If we allow multiline and there is already something in the input, start a newline.
|
2022-01-28 19:13:01 +00:00
|
|
|
/// NOTE: Lexer is only available if we use highlighter.
|
|
|
|
if (highlighter && multiline && !replxx_last_is_delimiter)
|
2021-11-07 08:02:40 +00:00
|
|
|
return rx.invoke(Replxx::ACTION::NEW_LINE, code);
|
2022-01-28 15:14:54 +00:00
|
|
|
replxx_last_is_delimiter = false;
|
2021-11-07 08:02:40 +00:00
|
|
|
return rx.invoke(Replxx::ACTION::COMMIT_LINE, code);
|
|
|
|
};
|
2021-10-14 15:56:28 +00:00
|
|
|
/// bind C-j to ENTER action.
|
2021-11-07 08:02:40 +00:00
|
|
|
rx.bind_key(Replxx::KEY::control('J'), commit_action);
|
|
|
|
rx.bind_key(Replxx::KEY::ENTER, commit_action);
|
2021-10-14 15:56:28 +00:00
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
/// 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); });
|
2021-05-02 20:22:54 +00:00
|
|
|
/// By default M-BACKSPACE is KILL_TO_WHITESPACE_ON_LEFT, while in readline it is backward-kill-word
|
|
|
|
rx.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [this](char32_t code) { return rx.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code); });
|
|
|
|
/// By default C-w is KILL_TO_BEGINING_OF_WORD, while in readline it is unix-word-rubout
|
|
|
|
rx.bind_key(Replxx::KEY::control('W'), [this](char32_t code) { return rx.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code); });
|
2020-12-01 10:34:34 +00:00
|
|
|
|
|
|
|
rx.bind_key(Replxx::KEY::meta('E'), [this](char32_t) { openEditor(); return Replxx::ACTION_RESULT::CONTINUE; });
|
2022-09-12 14:16:40 +00:00
|
|
|
|
|
|
|
/// readline insert-comment
|
|
|
|
auto insert_comment_action = [this](char32_t code)
|
|
|
|
{
|
|
|
|
replxx::Replxx::State state(rx.get_state());
|
|
|
|
const char * line = state.text();
|
|
|
|
const char * line_end = line + strlen(line);
|
|
|
|
|
|
|
|
std::string commented_line;
|
|
|
|
if (std::find(line, line_end, '\n') != line_end)
|
|
|
|
{
|
|
|
|
/// If query has multiple lines, multiline comment is used over
|
|
|
|
/// commenting each line separately for easier uncomment (though
|
|
|
|
/// with invoking editor it is simpler to uncomment multiple lines)
|
|
|
|
///
|
|
|
|
/// Note, that using multiline comment is OK even with nested
|
|
|
|
/// comments, since nested comments are supported.
|
|
|
|
commented_line = fmt::format("/* {} */", state.text());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In a simplest case use simple comment.
|
|
|
|
commented_line = fmt::format("-- {}", state.text());
|
|
|
|
}
|
2022-10-07 10:46:45 +00:00
|
|
|
rx.set_state(replxx::Replxx::State(commented_line.c_str(), static_cast<int>(commented_line.size())));
|
2022-09-12 14:16:40 +00:00
|
|
|
|
|
|
|
return rx.invoke(Replxx::ACTION::COMMIT_LINE, code);
|
|
|
|
};
|
|
|
|
rx.bind_key(Replxx::KEY::meta('#'), insert_comment_action);
|
2022-09-23 12:09:53 +00:00
|
|
|
|
2022-12-03 11:55:46 +00:00
|
|
|
#if USE_SKIM
|
|
|
|
auto interactive_history_search = [this](char32_t code)
|
2022-09-23 12:09:53 +00:00
|
|
|
{
|
2022-12-03 11:55:46 +00:00
|
|
|
std::vector<std::string> words;
|
2022-09-23 12:23:14 +00:00
|
|
|
{
|
2022-12-03 11:55:46 +00:00
|
|
|
auto hs(rx.history_scan());
|
|
|
|
while (hs.next())
|
|
|
|
words.push_back(hs.get().text());
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:58:32 +00:00
|
|
|
std::string current_query(rx.get_state().text());
|
2022-12-14 15:45:00 +00:00
|
|
|
std::string new_query;
|
|
|
|
try
|
|
|
|
{
|
2022-12-26 12:58:32 +00:00
|
|
|
new_query = std::string(skim(current_query, words));
|
2022-12-14 15:45:00 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
rx.print("skim failed: %s (consider using Ctrl-T for a regular non-fuzzy reverse search)\n", e.what());
|
|
|
|
}
|
2023-02-02 18:30:23 +00:00
|
|
|
|
|
|
|
/// REPAINT before to avoid prompt overlap by the query
|
|
|
|
rx.invoke(Replxx::ACTION::REPAINT, code);
|
|
|
|
|
2022-12-03 11:55:46 +00:00
|
|
|
if (!new_query.empty())
|
|
|
|
rx.set_state(replxx::Replxx::State(new_query.c_str(), static_cast<int>(new_query.size())));
|
|
|
|
|
|
|
|
if (bracketed_paste_enabled)
|
|
|
|
enableBracketedPaste();
|
|
|
|
|
|
|
|
rx.invoke(Replxx::ACTION::CLEAR_SELF, code);
|
|
|
|
return rx.invoke(Replxx::ACTION::REPAINT, code);
|
|
|
|
};
|
|
|
|
|
|
|
|
rx.bind_key(Replxx::KEY::control('R'), interactive_history_search);
|
2023-02-26 12:33:43 +00:00
|
|
|
#endif
|
2022-12-14 19:30:50 +00:00
|
|
|
|
|
|
|
/// Rebind regular incremental search to C-T.
|
|
|
|
///
|
|
|
|
/// NOTE: C-T by default this is a binding to swap adjustent chars
|
|
|
|
/// (TRANSPOSE_CHARACTERS), but for SQL it sounds pretty useless.
|
|
|
|
rx.bind_key(Replxx::KEY::control('T'), [this](char32_t)
|
|
|
|
{
|
|
|
|
/// Reverse search is detected by C-R.
|
|
|
|
uint32_t reverse_search = Replxx::KEY::control('R');
|
|
|
|
return rx.invoke(Replxx::ACTION::HISTORY_INCREMENTAL_SEARCH, reverse_search);
|
|
|
|
});
|
2023-11-25 21:00:56 +00:00
|
|
|
|
|
|
|
/// Change cursor style for overwrite mode to blinking (see console_codes(5))
|
|
|
|
rx.bind_key(Replxx::KEY::INSERT, [this](char32_t)
|
|
|
|
{
|
|
|
|
overwrite_mode = !overwrite_mode;
|
|
|
|
if (overwrite_mode)
|
|
|
|
rx.print("%s", "\033[5 q");
|
|
|
|
else
|
|
|
|
rx.print("%s", "\033[0 q");
|
|
|
|
return rx.invoke(Replxx::ACTION::TOGGLE_OVERWRITE_MODE, 0);
|
|
|
|
});
|
2020-06-04 22:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReplxxLineReader::~ReplxxLineReader()
|
|
|
|
{
|
2020-06-04 22:36:40 +00:00
|
|
|
if (close(history_file_fd))
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Close of history file failed: %s\n", errnoToString().c_str());
|
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;
|
|
|
|
|
2022-10-05 07:57:24 +00:00
|
|
|
rightTrim(input);
|
2020-06-04 22:34:49 +00:00
|
|
|
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))
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Lock of history file failed: %s\n", errnoToString().c_str());
|
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-10-12 20:39:00 +00:00
|
|
|
if (!rx.history_save(history_file_path))
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Saving history failed: %s\n", errnoToString().c_str());
|
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))
|
2022-08-20 15:09:20 +00:00
|
|
|
rx.print("Unlock of history file failed: %s\n", errnoToString().c_str());
|
2020-06-04 22:34:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:34:34 +00:00
|
|
|
void ReplxxLineReader::openEditor()
|
|
|
|
{
|
2022-09-22 21:19:57 +00:00
|
|
|
try
|
2020-12-02 02:25:22 +00:00
|
|
|
{
|
2023-12-06 19:02:39 +00:00
|
|
|
TemporaryFile editor_file("clickhouse_client_editor_XXXXXX.sql");
|
|
|
|
editor_file.write(rx.get_state().text());
|
|
|
|
editor_file.close();
|
|
|
|
|
|
|
|
char * const argv[] = {editor.data(), editor_file.getPath().data(), nullptr};
|
|
|
|
|
2022-09-22 21:19:57 +00:00
|
|
|
if (executeCommand(argv) == 0)
|
2020-12-02 02:25:22 +00:00
|
|
|
{
|
2022-09-22 21:19:57 +00:00
|
|
|
const std::string & new_query = readFile(editor_file.getPath());
|
2022-10-07 10:46:45 +00:00
|
|
|
rx.set_state(replxx::Replxx::State(new_query.c_str(), static_cast<int>(new_query.size())));
|
2020-12-02 02:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-22 21:19:57 +00:00
|
|
|
catch (const std::runtime_error & e)
|
2020-12-01 12:48:35 +00:00
|
|
|
{
|
2022-09-22 21:19:57 +00:00
|
|
|
rx.print(e.what());
|
2023-12-06 19:02:39 +00:00
|
|
|
rx.print("\n");
|
2020-12-01 10:34:34 +00:00
|
|
|
}
|
2020-12-01 13:28:55 +00:00
|
|
|
|
2020-12-01 10:34:34 +00:00
|
|
|
if (bracketed_paste_enabled)
|
|
|
|
enableBracketedPaste();
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:34:49 +00:00
|
|
|
void ReplxxLineReader::enableBracketedPaste()
|
|
|
|
{
|
2020-12-01 10:34:34 +00:00
|
|
|
bracketed_paste_enabled = true;
|
2020-06-04 22:34:49 +00:00
|
|
|
rx.enable_bracketed_paste();
|
2022-05-16 18:59:27 +00:00
|
|
|
}
|
2022-12-10 10:16:31 +00:00
|
|
|
|
2023-04-06 23:04:51 +00:00
|
|
|
void ReplxxLineReader::disableBracketedPaste()
|
|
|
|
{
|
|
|
|
bracketed_paste_enabled = false;
|
|
|
|
rx.disable_bracketed_paste();
|
|
|
|
}
|
|
|
|
|
2022-12-10 10:16:31 +00:00
|
|
|
}
|